InputHandler.cpp 9.1 KB

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