InputHandler.cpp 9.2 KB

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