InputHandler.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 && !settings["input"]["handleBackRightMouseButton"].Bool())
  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_SIZE_CHANGED:
  218. #ifdef VCMI_ANDROID
  219. {
  220. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  221. GH.onScreenResize(true);
  222. }
  223. #endif
  224. break;
  225. case SDL_WINDOWEVENT_FOCUS_GAINED:
  226. {
  227. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  228. if(settings["general"]["audioMuteFocus"].Bool()) {
  229. CCS->musich->setVolume(settings["general"]["music"].Integer());
  230. CCS->soundh->setVolume(settings["general"]["sound"].Integer());
  231. }
  232. }
  233. break;
  234. case SDL_WINDOWEVENT_FOCUS_LOST:
  235. {
  236. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  237. if(settings["general"]["audioMuteFocus"].Bool()) {
  238. CCS->musich->setVolume(0);
  239. CCS->soundh->setVolume(0);
  240. }
  241. }
  242. break;
  243. }
  244. return;
  245. }
  246. else if(ev.type == SDL_SYSWMEVENT)
  247. {
  248. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  249. if(!settings["session"]["headless"].Bool() && settings["general"]["notifications"].Bool())
  250. {
  251. NotificationHandler::handleSdlEvent(ev);
  252. }
  253. }
  254. else if(ev.type == SDL_CONTROLLERDEVICEADDED)
  255. {
  256. gameControllerHandler->handleEventDeviceAdded(ev.cdevice);
  257. return;
  258. }
  259. else if(ev.type == SDL_CONTROLLERDEVICEREMOVED)
  260. {
  261. gameControllerHandler->handleEventDeviceRemoved(ev.cdevice);
  262. return;
  263. }
  264. else if(ev.type == SDL_CONTROLLERDEVICEREMAPPED)
  265. {
  266. gameControllerHandler->handleEventDeviceRemapped(ev.cdevice);
  267. return;
  268. }
  269. //preprocessing
  270. if(ev.type == SDL_MOUSEMOTION)
  271. {
  272. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  273. if (CCS && CCS->curh)
  274. CCS->curh->cursorMove(ev.motion.x, ev.motion.y);
  275. }
  276. {
  277. boost::unique_lock<boost::mutex> lock(eventsMutex);
  278. // In a sequence of motion events, skip all but the last one.
  279. // This prevents freezes when every motion event takes longer to handle than interval at which
  280. // the events arrive (like dragging on the minimap in world view, with redraw at every event)
  281. // so that the events would start piling up faster than they can be processed.
  282. if (!eventsQueue.empty())
  283. {
  284. const SDL_Event & prev = eventsQueue.back();
  285. if(ev.type == SDL_MOUSEMOTION && prev.type == SDL_MOUSEMOTION)
  286. {
  287. SDL_Event accumulated = ev;
  288. accumulated.motion.xrel += prev.motion.xrel;
  289. accumulated.motion.yrel += prev.motion.yrel;
  290. eventsQueue.back() = accumulated;
  291. return;
  292. }
  293. if(ev.type == SDL_FINGERMOTION && prev.type == SDL_FINGERMOTION && ev.tfinger.fingerId == prev.tfinger.fingerId)
  294. {
  295. SDL_Event accumulated = ev;
  296. accumulated.tfinger.dx += prev.tfinger.dx;
  297. accumulated.tfinger.dy += prev.tfinger.dy;
  298. eventsQueue.back() = accumulated;
  299. return;
  300. }
  301. }
  302. eventsQueue.push_back(ev);
  303. }
  304. }
  305. void InputHandler::fetchEvents()
  306. {
  307. SDL_Event ev;
  308. while(1 == SDL_PollEvent(&ev))
  309. {
  310. preprocessEvent(ev);
  311. }
  312. }
  313. bool InputHandler::isKeyboardCmdDown() const
  314. {
  315. return keyboardHandler->isKeyboardCmdDown();
  316. }
  317. bool InputHandler::isKeyboardCtrlDown() const
  318. {
  319. return keyboardHandler->isKeyboardCtrlDown();
  320. }
  321. bool InputHandler::isKeyboardAltDown() const
  322. {
  323. return keyboardHandler->isKeyboardAltDown();
  324. }
  325. bool InputHandler::isKeyboardShiftDown() const
  326. {
  327. return keyboardHandler->isKeyboardShiftDown();
  328. }
  329. void InputHandler::moveCursorPosition(const Point & distance)
  330. {
  331. setCursorPosition(getCursorPosition() + distance);
  332. }
  333. void InputHandler::setCursorPosition(const Point & position)
  334. {
  335. cursorPosition = position;
  336. GH.events().dispatchMouseMoved(Point(0, 0), position);
  337. }
  338. void InputHandler::startTextInput(const Rect & where)
  339. {
  340. textHandler->startTextInput(where);
  341. }
  342. void InputHandler::stopTextInput()
  343. {
  344. textHandler->stopTextInput();
  345. }
  346. void InputHandler::hapticFeedback()
  347. {
  348. if(currentInputMode == InputMode::TOUCH)
  349. fingerHandler->hapticFeedback();
  350. }
  351. uint32_t InputHandler::getTicks()
  352. {
  353. return SDL_GetTicks();
  354. }
  355. bool InputHandler::hasTouchInputDevice() const
  356. {
  357. return fingerHandler->hasTouchInputDevice();
  358. }
  359. int InputHandler::getNumTouchFingers() const
  360. {
  361. if(currentInputMode != InputMode::TOUCH)
  362. return 0;
  363. return fingerHandler->getNumTouchFingers();
  364. }
  365. void InputHandler::dispatchMainThread(const std::function<void()> & functor)
  366. {
  367. auto heapFunctor = new std::function<void()>(functor);
  368. SDL_Event event;
  369. event.user.type = SDL_USEREVENT;
  370. event.user.code = 0;
  371. event.user.data1 = static_cast <void*>(heapFunctor);
  372. event.user.data2 = nullptr;
  373. SDL_PushEvent(&event);
  374. }
  375. void InputHandler::handleUserEvent(const SDL_UserEvent & current)
  376. {
  377. auto heapFunctor = static_cast<std::function<void()>*>(current.data1);
  378. (*heapFunctor)();
  379. delete heapFunctor;
  380. }
  381. const Point & InputHandler::getCursorPosition() const
  382. {
  383. return cursorPosition;
  384. }