InputHandler.cpp 9.7 KB

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