2
0

InputHandler.cpp 7.7 KB

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