InputHandler.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 "../GameEngine.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 "../../lib/CConfigHandler.h"
  27. #include <SDL_events.h>
  28. #include <SDL_timer.h>
  29. #include <SDL_clipboard.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. ENGINE->events().dispatchInputModeChanged(modi);
  128. }
  129. }
  130. InputMode InputHandler::getCurrentInputMode()
  131. {
  132. return currentInputMode;
  133. }
  134. void InputHandler::copyToClipBoard(const std::string & text)
  135. {
  136. SDL_SetClipboardText(text.c_str());
  137. }
  138. std::vector<SDL_Event> InputHandler::acquireEvents()
  139. {
  140. boost::unique_lock<boost::mutex> lock(eventsMutex);
  141. std::vector<SDL_Event> result;
  142. std::swap(result, eventsQueue);
  143. return result;
  144. }
  145. void InputHandler::processEvents()
  146. {
  147. std::vector<SDL_Event> eventsToProcess = acquireEvents();
  148. for(const auto & currentEvent : eventsToProcess)
  149. handleCurrentEvent(currentEvent);
  150. gameControllerHandler->handleUpdate();
  151. fingerHandler->handleUpdate();
  152. }
  153. bool InputHandler::ignoreEventsUntilInput()
  154. {
  155. bool inputFound = false;
  156. boost::unique_lock<boost::mutex> lock(eventsMutex);
  157. for(const auto & event : eventsQueue)
  158. {
  159. switch(event.type)
  160. {
  161. case SDL_MOUSEBUTTONDOWN:
  162. case SDL_FINGERDOWN:
  163. case SDL_KEYDOWN:
  164. case SDL_CONTROLLERBUTTONDOWN:
  165. inputFound = true;
  166. }
  167. }
  168. eventsQueue.clear();
  169. return inputFound;
  170. }
  171. void InputHandler::preprocessEvent(const SDL_Event & ev)
  172. {
  173. if(ev.type == SDL_QUIT)
  174. {
  175. boost::mutex::scoped_lock interfaceLock(ENGINE->interfaceMutex);
  176. #ifdef VCMI_ANDROID
  177. handleQuit(false);
  178. #else
  179. handleQuit(true);
  180. #endif
  181. return;
  182. }
  183. else if(ev.type == SDL_KEYDOWN)
  184. {
  185. if(ev.key.keysym.sym == SDLK_F4 && (ev.key.keysym.mod & KMOD_ALT))
  186. {
  187. // FIXME: dead code? Looks like intercepted by OS/SDL and delivered as SDL_Quit instead?
  188. boost::mutex::scoped_lock interfaceLock(ENGINE->interfaceMutex);
  189. handleQuit(true);
  190. return;
  191. }
  192. if(ev.key.keysym.scancode == SDL_SCANCODE_AC_BACK && !settings["input"]["handleBackRightMouseButton"].Bool())
  193. {
  194. boost::mutex::scoped_lock interfaceLock(ENGINE->interfaceMutex);
  195. handleQuit(true);
  196. return;
  197. }
  198. }
  199. else if(ev.type == SDL_USEREVENT)
  200. {
  201. boost::mutex::scoped_lock interfaceLock(ENGINE->interfaceMutex);
  202. handleUserEvent(ev.user);
  203. return;
  204. }
  205. else if(ev.type == SDL_WINDOWEVENT)
  206. {
  207. switch (ev.window.event) {
  208. case SDL_WINDOWEVENT_RESTORED:
  209. #ifndef VCMI_IOS
  210. {
  211. boost::mutex::scoped_lock interfaceLock(ENGINE->interfaceMutex);
  212. ENGINE->onScreenResize(false);
  213. }
  214. #endif
  215. break;
  216. case SDL_WINDOWEVENT_SIZE_CHANGED:
  217. #ifdef VCMI_ANDROID
  218. {
  219. boost::mutex::scoped_lock interfaceLock(ENGINE->interfaceMutex);
  220. ENGINE->onScreenResize(true);
  221. }
  222. #endif
  223. break;
  224. case SDL_WINDOWEVENT_FOCUS_GAINED:
  225. {
  226. boost::mutex::scoped_lock interfaceLock(ENGINE->interfaceMutex);
  227. if(settings["general"]["audioMuteFocus"].Bool()) {
  228. ENGINE->music().setVolume(settings["general"]["music"].Integer());
  229. ENGINE->sound().setVolume(settings["general"]["sound"].Integer());
  230. }
  231. }
  232. break;
  233. case SDL_WINDOWEVENT_FOCUS_LOST:
  234. {
  235. boost::mutex::scoped_lock interfaceLock(ENGINE->interfaceMutex);
  236. if(settings["general"]["audioMuteFocus"].Bool()) {
  237. ENGINE->music().setVolume(0);
  238. ENGINE->sound().setVolume(0);
  239. }
  240. }
  241. break;
  242. }
  243. return;
  244. }
  245. else if(ev.type == SDL_SYSWMEVENT)
  246. {
  247. boost::mutex::scoped_lock interfaceLock(ENGINE->interfaceMutex);
  248. if(!settings["session"]["headless"].Bool() && settings["general"]["notifications"].Bool())
  249. {
  250. NotificationHandler::handleSdlEvent(ev);
  251. }
  252. }
  253. else if(ev.type == SDL_CONTROLLERDEVICEADDED)
  254. {
  255. gameControllerHandler->handleEventDeviceAdded(ev.cdevice);
  256. return;
  257. }
  258. else if(ev.type == SDL_CONTROLLERDEVICEREMOVED)
  259. {
  260. gameControllerHandler->handleEventDeviceRemoved(ev.cdevice);
  261. return;
  262. }
  263. else if(ev.type == SDL_CONTROLLERDEVICEREMAPPED)
  264. {
  265. gameControllerHandler->handleEventDeviceRemapped(ev.cdevice);
  266. return;
  267. }
  268. //preprocessing
  269. if(ev.type == SDL_MOUSEMOTION)
  270. {
  271. boost::mutex::scoped_lock interfaceLock(ENGINE->interfaceMutex);
  272. ENGINE->cursor().cursorMove(ev.motion.x, ev.motion.y);
  273. }
  274. {
  275. boost::unique_lock<boost::mutex> lock(eventsMutex);
  276. // In a sequence of motion events, skip all but the last one.
  277. // This prevents freezes when every motion event takes longer to handle than interval at which
  278. // the events arrive (like dragging on the minimap in world view, with redraw at every event)
  279. // so that the events would start piling up faster than they can be processed.
  280. if (!eventsQueue.empty())
  281. {
  282. const SDL_Event & prev = eventsQueue.back();
  283. if(ev.type == SDL_MOUSEMOTION && prev.type == SDL_MOUSEMOTION)
  284. {
  285. SDL_Event accumulated = ev;
  286. accumulated.motion.xrel += prev.motion.xrel;
  287. accumulated.motion.yrel += prev.motion.yrel;
  288. eventsQueue.back() = accumulated;
  289. return;
  290. }
  291. if(ev.type == SDL_FINGERMOTION && prev.type == SDL_FINGERMOTION && ev.tfinger.fingerId == prev.tfinger.fingerId)
  292. {
  293. SDL_Event accumulated = ev;
  294. accumulated.tfinger.dx += prev.tfinger.dx;
  295. accumulated.tfinger.dy += prev.tfinger.dy;
  296. eventsQueue.back() = accumulated;
  297. return;
  298. }
  299. }
  300. eventsQueue.push_back(ev);
  301. }
  302. }
  303. void InputHandler::fetchEvents()
  304. {
  305. SDL_Event ev;
  306. while(1 == SDL_PollEvent(&ev))
  307. {
  308. preprocessEvent(ev);
  309. }
  310. }
  311. bool InputHandler::isKeyboardCmdDown() const
  312. {
  313. return keyboardHandler->isKeyboardCmdDown();
  314. }
  315. bool InputHandler::isKeyboardCtrlDown() const
  316. {
  317. return keyboardHandler->isKeyboardCtrlDown();
  318. }
  319. bool InputHandler::isKeyboardAltDown() const
  320. {
  321. return keyboardHandler->isKeyboardAltDown();
  322. }
  323. bool InputHandler::isKeyboardShiftDown() const
  324. {
  325. return keyboardHandler->isKeyboardShiftDown();
  326. }
  327. void InputHandler::moveCursorPosition(const Point & distance)
  328. {
  329. setCursorPosition(getCursorPosition() + distance);
  330. }
  331. void InputHandler::setCursorPosition(const Point & position)
  332. {
  333. cursorPosition = position;
  334. ENGINE->events().dispatchMouseMoved(Point(0, 0), position);
  335. }
  336. void InputHandler::startTextInput(const Rect & where)
  337. {
  338. textHandler->startTextInput(where);
  339. }
  340. void InputHandler::stopTextInput()
  341. {
  342. textHandler->stopTextInput();
  343. }
  344. void InputHandler::hapticFeedback()
  345. {
  346. if(currentInputMode == InputMode::TOUCH)
  347. fingerHandler->hapticFeedback();
  348. }
  349. uint32_t InputHandler::getTicks()
  350. {
  351. return SDL_GetTicks();
  352. }
  353. bool InputHandler::hasTouchInputDevice() const
  354. {
  355. return fingerHandler->hasTouchInputDevice();
  356. }
  357. int InputHandler::getNumTouchFingers() const
  358. {
  359. if(currentInputMode != InputMode::TOUCH)
  360. return 0;
  361. return fingerHandler->getNumTouchFingers();
  362. }
  363. void InputHandler::dispatchMainThread(const std::function<void()> & functor)
  364. {
  365. auto heapFunctor = new std::function<void()>(functor);
  366. SDL_Event event;
  367. event.user.type = SDL_USEREVENT;
  368. event.user.code = 0;
  369. event.user.data1 = static_cast <void*>(heapFunctor);
  370. event.user.data2 = nullptr;
  371. SDL_PushEvent(&event);
  372. }
  373. void InputHandler::handleUserEvent(const SDL_UserEvent & current)
  374. {
  375. auto heapFunctor = static_cast<std::function<void()>*>(current.data1);
  376. (*heapFunctor)();
  377. delete heapFunctor;
  378. }
  379. const Point & InputHandler::getCursorPosition() const
  380. {
  381. return cursorPosition;
  382. }