InputHandler.cpp 9.7 KB

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