InputHandler.cpp 7.2 KB

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