InputHandler.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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)
  100. {
  101. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  102. handleQuit(false);
  103. return;
  104. }
  105. else if(ev.type == SDL_KEYDOWN)
  106. {
  107. if(ev.key.keysym.sym == SDLK_F4 && (ev.key.keysym.mod & KMOD_ALT))
  108. {
  109. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  110. handleQuit(true);
  111. return;
  112. }
  113. if(ev.key.keysym.scancode == SDL_SCANCODE_AC_BACK)
  114. {
  115. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  116. handleQuit(true);
  117. return;
  118. }
  119. if(ev.type == SDL_KEYDOWN && ev.key.keysym.sym == SDLK_F4)
  120. {
  121. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  122. Settings full = settings.write["video"]["fullscreen"];
  123. full->Bool() = !full->Bool();
  124. GH.onScreenResize();
  125. return;
  126. }
  127. }
  128. else if(ev.type == SDL_USEREVENT)
  129. {
  130. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  131. handleUserEvent(ev.user);
  132. return;
  133. }
  134. else if(ev.type == SDL_WINDOWEVENT)
  135. {
  136. switch (ev.window.event) {
  137. case SDL_WINDOWEVENT_RESTORED:
  138. #ifndef VCMI_IOS
  139. {
  140. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  141. GH.onScreenResize();
  142. }
  143. #endif
  144. break;
  145. case SDL_WINDOWEVENT_FOCUS_GAINED:
  146. {
  147. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  148. if(settings["general"]["enableUiEnhancements"].Bool()) {
  149. CCS->musich->setVolume(settings["general"]["music"].Integer());
  150. CCS->soundh->setVolume(settings["general"]["sound"].Integer());
  151. }
  152. }
  153. break;
  154. case SDL_WINDOWEVENT_FOCUS_LOST:
  155. {
  156. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  157. if(settings["general"]["enableUiEnhancements"].Bool()) {
  158. CCS->musich->setVolume(0);
  159. CCS->soundh->setVolume(0);
  160. }
  161. }
  162. break;
  163. }
  164. return;
  165. }
  166. else if(ev.type == SDL_SYSWMEVENT)
  167. {
  168. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  169. if(!settings["session"]["headless"].Bool() && settings["general"]["notifications"].Bool())
  170. {
  171. NotificationHandler::handleSdlEvent(ev);
  172. }
  173. }
  174. //preprocessing
  175. if(ev.type == SDL_MOUSEMOTION)
  176. {
  177. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  178. if (CCS && CCS->curh)
  179. CCS->curh->cursorMove(ev.motion.x, ev.motion.y);
  180. }
  181. {
  182. boost::unique_lock<boost::mutex> lock(eventsMutex);
  183. // In a sequence of motion events, skip all but the last one.
  184. // This prevents freezes when every motion event takes longer to handle than interval at which
  185. // the events arrive (like dragging on the minimap in world view, with redraw at every event)
  186. // so that the events would start piling up faster than they can be processed.
  187. if (!eventsQueue.empty())
  188. {
  189. const SDL_Event & prev = eventsQueue.back();
  190. if(ev.type == SDL_MOUSEMOTION && prev.type == SDL_MOUSEMOTION)
  191. {
  192. SDL_Event accumulated = ev;
  193. accumulated.motion.xrel += prev.motion.xrel;
  194. accumulated.motion.yrel += prev.motion.yrel;
  195. eventsQueue.back() = accumulated;
  196. return;
  197. }
  198. if(ev.type == SDL_FINGERMOTION && prev.type == SDL_FINGERMOTION && ev.tfinger.fingerId == prev.tfinger.fingerId)
  199. {
  200. SDL_Event accumulated = ev;
  201. accumulated.tfinger.dx += prev.tfinger.dx;
  202. accumulated.tfinger.dy += prev.tfinger.dy;
  203. eventsQueue.back() = accumulated;
  204. return;
  205. }
  206. }
  207. eventsQueue.push_back(ev);
  208. }
  209. }
  210. void InputHandler::fetchEvents()
  211. {
  212. SDL_Event ev;
  213. while(1 == SDL_PollEvent(&ev))
  214. {
  215. preprocessEvent(ev);
  216. }
  217. }
  218. bool InputHandler::isKeyboardCtrlDown() const
  219. {
  220. return keyboardHandler->isKeyboardCtrlDown();
  221. }
  222. bool InputHandler::isKeyboardAltDown() const
  223. {
  224. return keyboardHandler->isKeyboardAltDown();
  225. }
  226. bool InputHandler::isKeyboardShiftDown() const
  227. {
  228. return keyboardHandler->isKeyboardShiftDown();
  229. }
  230. void InputHandler::moveCursorPosition(const Point & distance)
  231. {
  232. setCursorPosition(getCursorPosition() + distance);
  233. }
  234. void InputHandler::setCursorPosition(const Point & position)
  235. {
  236. cursorPosition = position;
  237. GH.events().dispatchMouseMoved(Point(0, 0), position);
  238. }
  239. void InputHandler::startTextInput(const Rect & where)
  240. {
  241. textHandler->startTextInput(where);
  242. }
  243. void InputHandler::stopTextInput()
  244. {
  245. textHandler->stopTextInput();
  246. }
  247. void InputHandler::hapticFeedback()
  248. {
  249. fingerHandler->hapticFeedback();
  250. }
  251. uint32_t InputHandler::getTicks()
  252. {
  253. return SDL_GetTicks();
  254. }
  255. bool InputHandler::hasTouchInputDevice() const
  256. {
  257. return fingerHandler->hasTouchInputDevice();
  258. }
  259. void InputHandler::dispatchMainThread(const std::function<void()> & functor)
  260. {
  261. auto heapFunctor = new std::function<void()>(functor);
  262. SDL_Event event;
  263. event.type = SDL_USEREVENT;
  264. event.user.code = 0;
  265. event.user.data1 = static_cast <void*>(heapFunctor);
  266. event.user.data2 = nullptr;
  267. SDL_PushEvent(&event);
  268. }
  269. void InputHandler::handleUserEvent(const SDL_UserEvent & current)
  270. {
  271. auto heapFunctor = static_cast<std::function<void()>*>(current.data1);
  272. (*heapFunctor)();
  273. }
  274. const Point & InputHandler::getCursorPosition() const
  275. {
  276. return cursorPosition;
  277. }