InputHandler.cpp 8.7 KB

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