InputHandler.cpp 6.7 KB

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