InputHandler.cpp 9.4 KB

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