InputHandler.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * InputHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "InputHandler.h"
  12. #include "NotificationHandler.h"
  13. #include "InputSourceMouse.h"
  14. #include "InputSourceKeyboard.h"
  15. #include "InputSourceTouch.h"
  16. #include "InputSourceText.h"
  17. #include "../gui/CGuiHandler.h"
  18. #include "../gui/CursorHandler.h"
  19. #include "../gui/EventDispatcher.h"
  20. #include "../gui/MouseButton.h"
  21. #include "../CMT.h"
  22. #include "../CPlayerInterface.h"
  23. #include "../CGameInfo.h"
  24. #include "../../lib/CConfigHandler.h"
  25. #include <SDL_events.h>
  26. #include <SDL_timer.h>
  27. InputHandler::InputHandler()
  28. : mouseHandler(std::make_unique<InputSourceMouse>())
  29. , keyboardHandler(std::make_unique<InputSourceKeyboard>())
  30. , fingerHandler(std::make_unique<InputSourceTouch>())
  31. , textHandler(std::make_unique<InputSourceText>())
  32. {
  33. }
  34. InputHandler::~InputHandler() = default;
  35. void InputHandler::handleCurrentEvent(const SDL_Event & current)
  36. {
  37. switch (current.type)
  38. {
  39. case SDL_KEYDOWN:
  40. return keyboardHandler->handleEventKeyDown(current.key);
  41. case SDL_KEYUP:
  42. return keyboardHandler->handleEventKeyUp(current.key);
  43. #ifndef VCMI_EMULATE_TOUCHSCREEN_WITH_MOUSE
  44. case SDL_MOUSEMOTION:
  45. return mouseHandler->handleEventMouseMotion(current.motion);
  46. case SDL_MOUSEBUTTONDOWN:
  47. return mouseHandler->handleEventMouseButtonDown(current.button);
  48. case SDL_MOUSEBUTTONUP:
  49. return mouseHandler->handleEventMouseButtonUp(current.button);
  50. case SDL_MOUSEWHEEL:
  51. return mouseHandler->handleEventMouseWheel(current.wheel);
  52. #endif
  53. case SDL_TEXTINPUT:
  54. return textHandler->handleEventTextInput(current.text);
  55. case SDL_TEXTEDITING:
  56. return textHandler->handleEventTextEditing(current.edit);
  57. case SDL_FINGERMOTION:
  58. return fingerHandler->handleEventFingerMotion(current.tfinger);
  59. case SDL_FINGERDOWN:
  60. return fingerHandler->handleEventFingerDown(current.tfinger);
  61. case SDL_FINGERUP:
  62. return fingerHandler->handleEventFingerUp(current.tfinger);
  63. }
  64. }
  65. std::vector<SDL_Event> InputHandler::acquireEvents()
  66. {
  67. boost::unique_lock<boost::mutex> lock(eventsMutex);
  68. std::vector<SDL_Event> result;
  69. std::swap(result, eventsQueue);
  70. return result;
  71. }
  72. void InputHandler::processEvents()
  73. {
  74. std::vector<SDL_Event> eventsToProcess = acquireEvents();
  75. for(const auto & currentEvent : eventsToProcess)
  76. handleCurrentEvent(currentEvent);
  77. fingerHandler->handleUpdate();
  78. }
  79. bool InputHandler::ignoreEventsUntilInput()
  80. {
  81. bool inputFound = false;
  82. boost::unique_lock<boost::mutex> lock(eventsMutex);
  83. for(const auto & event : eventsQueue)
  84. {
  85. switch(event.type)
  86. {
  87. case SDL_MOUSEBUTTONDOWN:
  88. case SDL_FINGERDOWN:
  89. case SDL_KEYDOWN:
  90. inputFound = true;
  91. }
  92. }
  93. eventsQueue.clear();
  94. return inputFound;
  95. }
  96. void InputHandler::preprocessEvent(const SDL_Event & ev)
  97. {
  98. if((ev.type==SDL_QUIT) ||(ev.type == SDL_KEYDOWN && ev.key.keysym.sym==SDLK_F4 && (ev.key.keysym.mod & KMOD_ALT)))
  99. {
  100. #ifdef VCMI_ANDROID
  101. handleQuit(false);
  102. #else
  103. handleQuit();
  104. #endif
  105. return;
  106. }
  107. #ifdef VCMI_ANDROID
  108. else if (ev.type == SDL_KEYDOWN && ev.key.keysym.scancode == SDL_SCANCODE_AC_BACK)
  109. {
  110. handleQuit(true);
  111. }
  112. #endif
  113. else if(ev.type == SDL_KEYDOWN && ev.key.keysym.sym==SDLK_F4)
  114. {
  115. boost::unique_lock<boost::recursive_mutex> lock(*CPlayerInterface::pim);
  116. Settings full = settings.write["video"]["fullscreen"];
  117. full->Bool() = !full->Bool();
  118. GH.onScreenResize();
  119. return;
  120. }
  121. else if(ev.type == SDL_USEREVENT)
  122. {
  123. handleUserEvent(ev.user);
  124. return;
  125. }
  126. else if(ev.type == SDL_WINDOWEVENT)
  127. {
  128. switch (ev.window.event) {
  129. case SDL_WINDOWEVENT_RESTORED:
  130. #ifndef VCMI_IOS
  131. {
  132. boost::unique_lock<boost::recursive_mutex> lock(*CPlayerInterface::pim);
  133. GH.onScreenResize();
  134. }
  135. #endif
  136. break;
  137. }
  138. return;
  139. }
  140. else if(ev.type == SDL_SYSWMEVENT)
  141. {
  142. if(!settings["session"]["headless"].Bool() && settings["general"]["notifications"].Bool())
  143. {
  144. NotificationHandler::handleSdlEvent(ev);
  145. }
  146. }
  147. //preprocessing
  148. if(ev.type == SDL_MOUSEMOTION)
  149. {
  150. if (CCS && CCS->curh)
  151. CCS->curh->cursorMove(ev.motion.x, ev.motion.y);
  152. }
  153. {
  154. boost::unique_lock<boost::mutex> lock(eventsMutex);
  155. // In a sequence of motion events, skip all but the last one.
  156. // This prevents freezes when every motion event takes longer to handle than interval at which
  157. // the events arrive (like dragging on the minimap in world view, with redraw at every event)
  158. // so that the events would start piling up faster than they can be processed.
  159. if (!eventsQueue.empty())
  160. {
  161. const SDL_Event & prev = eventsQueue.back();
  162. if(ev.type == SDL_MOUSEMOTION && prev.type == SDL_MOUSEMOTION)
  163. {
  164. SDL_Event accumulated = ev;
  165. accumulated.motion.xrel += prev.motion.xrel;
  166. accumulated.motion.yrel += prev.motion.yrel;
  167. eventsQueue.back() = accumulated;
  168. return;
  169. }
  170. if(ev.type == SDL_FINGERMOTION && prev.type == SDL_FINGERMOTION && ev.tfinger.fingerId == prev.tfinger.fingerId)
  171. {
  172. SDL_Event accumulated = ev;
  173. accumulated.tfinger.dx += prev.tfinger.dx;
  174. accumulated.tfinger.dy += prev.tfinger.dy;
  175. eventsQueue.back() = accumulated;
  176. return;
  177. }
  178. }
  179. eventsQueue.push_back(ev);
  180. }
  181. }
  182. void InputHandler::fetchEvents()
  183. {
  184. SDL_Event ev;
  185. while(1 == SDL_PollEvent(&ev))
  186. {
  187. preprocessEvent(ev);
  188. }
  189. }
  190. bool InputHandler::isKeyboardCtrlDown() const
  191. {
  192. return keyboardHandler->isKeyboardCtrlDown();
  193. }
  194. bool InputHandler::isKeyboardAltDown() const
  195. {
  196. return keyboardHandler->isKeyboardAltDown();
  197. }
  198. bool InputHandler::isKeyboardShiftDown() const
  199. {
  200. return keyboardHandler->isKeyboardShiftDown();
  201. }
  202. void InputHandler::moveCursorPosition(const Point & distance)
  203. {
  204. setCursorPosition(getCursorPosition() + distance);
  205. }
  206. void InputHandler::setCursorPosition(const Point & position)
  207. {
  208. cursorPosition = position;
  209. GH.events().dispatchMouseMoved(Point(0, 0), position);
  210. }
  211. void InputHandler::startTextInput(const Rect & where)
  212. {
  213. textHandler->startTextInput(where);
  214. }
  215. void InputHandler::stopTextInput()
  216. {
  217. textHandler->stopTextInput();
  218. }
  219. void InputHandler::hapticFeedback()
  220. {
  221. fingerHandler->hapticFeedback();
  222. }
  223. uint32_t InputHandler::getTicks()
  224. {
  225. return SDL_GetTicks();
  226. }
  227. bool InputHandler::hasTouchInputDevice() const
  228. {
  229. return fingerHandler->hasTouchInputDevice();
  230. }
  231. void InputHandler::dispatchMainThread(const std::function<void()> & functor)
  232. {
  233. auto heapFunctor = new std::function<void()>(functor);
  234. SDL_Event event;
  235. event.type = SDL_USEREVENT;
  236. event.user.code = 0;
  237. event.user.data1 = static_cast <void*>(heapFunctor);
  238. event.user.data2 = nullptr;
  239. SDL_PushEvent(&event);
  240. }
  241. void InputHandler::handleUserEvent(const SDL_UserEvent & current)
  242. {
  243. auto heapFunctor = static_cast<std::function<void()>*>(current.data1);
  244. (*heapFunctor)();
  245. }
  246. const Point & InputHandler::getCursorPosition() const
  247. {
  248. return cursorPosition;
  249. }