InputHandler.cpp 9.9 KB

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