Event.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /**
  2. * Copyright (c) 2006-2017 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "Event.h"
  21. #include "filesystem/DroppedFile.h"
  22. #include "filesystem/Filesystem.h"
  23. #include "keyboard/sdl/Keyboard.h"
  24. #include "joystick/JoystickModule.h"
  25. #include "joystick/sdl/Joystick.h"
  26. #include "touch/sdl/Touch.h"
  27. #include "graphics/Graphics.h"
  28. #include "window/Window.h"
  29. #include "common/Exception.h"
  30. #include "audio/Audio.h"
  31. #include "common/config.h"
  32. #include "timer/Timer.h"
  33. #include <cmath>
  34. namespace love
  35. {
  36. namespace event
  37. {
  38. namespace sdl
  39. {
  40. // SDL reports mouse coordinates in the window coordinate system in OS X, but
  41. // we want them in pixel coordinates (may be different with high-DPI enabled.)
  42. static void windowToDPICoords(double *x, double *y)
  43. {
  44. auto window = Module::getInstance<window::Window>(Module::M_WINDOW);
  45. if (window)
  46. window->windowToDPICoords(x, y);
  47. }
  48. #ifndef LOVE_MACOSX
  49. static void normalizedToDPICoords(double *x, double *y)
  50. {
  51. double w = 1.0, h = 1.0;
  52. auto window = Module::getInstance<window::Window>(Module::M_WINDOW);
  53. if (window)
  54. {
  55. w = window->getWidth();
  56. h = window->getHeight();
  57. window->windowToDPICoords(&w, &h);
  58. }
  59. if (x)
  60. *x = ((*x) * w);
  61. if (y)
  62. *y = ((*y) * h);
  63. }
  64. #endif
  65. // SDL's event watch callbacks trigger when the event is actually posted inside
  66. // SDL, unlike with SDL_PollEvents. This is useful for some events which require
  67. // handling inside the function which triggered them on some backends.
  68. static int SDLCALL watchAppEvents(void * /*udata*/, SDL_Event *event)
  69. {
  70. auto gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS);
  71. switch (event->type)
  72. {
  73. // On iOS, calling any OpenGL ES function after the function which triggers
  74. // SDL_APP_DIDENTERBACKGROUND is called will kill the app, so we handle it
  75. // with an event watch callback, which will be called inside that function.
  76. case SDL_APP_DIDENTERBACKGROUND:
  77. case SDL_APP_WILLENTERFOREGROUND:
  78. if (gfx)
  79. gfx->setActive(event->type == SDL_APP_WILLENTERFOREGROUND);
  80. break;
  81. default:
  82. break;
  83. }
  84. return 1;
  85. }
  86. const char *Event::getName() const
  87. {
  88. return "love.event.sdl";
  89. }
  90. Event::Event()
  91. {
  92. if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0)
  93. throw love::Exception("Could not initialize SDL events subsystem (%s)", SDL_GetError());
  94. SDL_AddEventWatch(watchAppEvents, this);
  95. }
  96. Event::~Event()
  97. {
  98. SDL_DelEventWatch(watchAppEvents, this);
  99. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  100. }
  101. void Event::pump()
  102. {
  103. exceptionIfInRenderPass();
  104. SDL_Event e;
  105. while (SDL_PollEvent(&e))
  106. {
  107. Message *msg = convert(e);
  108. if (msg)
  109. {
  110. push(msg);
  111. msg->release();
  112. }
  113. }
  114. }
  115. Message *Event::wait()
  116. {
  117. exceptionIfInRenderPass();
  118. SDL_Event e;
  119. if (SDL_WaitEvent(&e) != 1)
  120. return nullptr;
  121. return convert(e);
  122. }
  123. void Event::clear()
  124. {
  125. exceptionIfInRenderPass();
  126. SDL_Event e;
  127. while (SDL_PollEvent(&e))
  128. {
  129. // Do nothing with 'e' ...
  130. }
  131. love::event::Event::clear();
  132. }
  133. void Event::exceptionIfInRenderPass()
  134. {
  135. // Some core OS graphics functionality (e.g. swap buffers on some platforms)
  136. // happens inside SDL_PumpEvents - which is called by SDL_PollEvent and
  137. // friends. It's probably a bad idea to call those functions while a Canvas
  138. // is active.
  139. auto gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS);
  140. if (gfx != nullptr && gfx->isCanvasActive())
  141. throw love::Exception("Cannot call this function while a Canvas is active in love.graphics.");
  142. }
  143. Message *Event::convert(const SDL_Event &e)
  144. {
  145. Message *msg = nullptr;
  146. std::vector<Variant> vargs;
  147. vargs.reserve(4);
  148. love::filesystem::Filesystem *filesystem = nullptr;
  149. love::keyboard::Keyboard::Key key = love::keyboard::Keyboard::KEY_UNKNOWN;
  150. love::keyboard::Keyboard::Scancode scancode = love::keyboard::Keyboard::SCANCODE_UNKNOWN;
  151. const char *txt;
  152. const char *txt2;
  153. std::map<SDL_Keycode, love::keyboard::Keyboard::Key>::const_iterator keyit;
  154. #ifndef LOVE_MACOSX
  155. love::touch::sdl::Touch *touchmodule = nullptr;
  156. love::touch::Touch::TouchInfo touchinfo;
  157. #endif
  158. #ifdef LOVE_LINUX
  159. static bool touchNormalizationBug = false;
  160. #endif
  161. switch (e.type)
  162. {
  163. case SDL_KEYDOWN:
  164. if (e.key.repeat)
  165. {
  166. auto kb = Module::getInstance<love::keyboard::Keyboard>(Module::M_KEYBOARD);
  167. if (kb && !kb->hasKeyRepeat())
  168. break;
  169. }
  170. keyit = keys.find(e.key.keysym.sym);
  171. if (keyit != keys.end())
  172. key = keyit->second;
  173. if (!love::keyboard::Keyboard::getConstant(key, txt))
  174. txt = "unknown";
  175. love::keyboard::sdl::Keyboard::getConstant(e.key.keysym.scancode, scancode);
  176. if (!love::keyboard::Keyboard::getConstant(scancode, txt2))
  177. txt2 = "unknown";
  178. vargs.emplace_back(txt, strlen(txt));
  179. vargs.emplace_back(txt2, strlen(txt2));
  180. vargs.emplace_back(e.key.repeat != 0);
  181. msg = new Message("keypressed", vargs);
  182. break;
  183. case SDL_KEYUP:
  184. keyit = keys.find(e.key.keysym.sym);
  185. if (keyit != keys.end())
  186. key = keyit->second;
  187. if (!love::keyboard::Keyboard::getConstant(key, txt))
  188. txt = "unknown";
  189. love::keyboard::sdl::Keyboard::getConstant(e.key.keysym.scancode, scancode);
  190. if (!love::keyboard::Keyboard::getConstant(scancode, txt2))
  191. txt2 = "unknown";
  192. vargs.emplace_back(txt, strlen(txt));
  193. vargs.emplace_back(txt2, strlen(txt2));
  194. msg = new Message("keyreleased", vargs);
  195. break;
  196. case SDL_TEXTINPUT:
  197. txt = e.text.text;
  198. vargs.emplace_back(txt, strlen(txt));
  199. msg = new Message("textinput", vargs);
  200. break;
  201. case SDL_TEXTEDITING:
  202. txt = e.edit.text;
  203. vargs.emplace_back(txt, strlen(txt));
  204. vargs.emplace_back((double) e.edit.start);
  205. vargs.emplace_back((double) e.edit.length);
  206. msg = new Message("textedited", vargs);
  207. break;
  208. case SDL_MOUSEMOTION:
  209. {
  210. double x = (double) e.motion.x;
  211. double y = (double) e.motion.y;
  212. double xrel = (double) e.motion.xrel;
  213. double yrel = (double) e.motion.yrel;
  214. windowToDPICoords(&x, &y);
  215. windowToDPICoords(&xrel, &yrel);
  216. vargs.emplace_back(x);
  217. vargs.emplace_back(y);
  218. vargs.emplace_back(xrel);
  219. vargs.emplace_back(yrel);
  220. vargs.emplace_back(e.motion.which == SDL_TOUCH_MOUSEID);
  221. msg = new Message("mousemoved", vargs);
  222. }
  223. break;
  224. case SDL_MOUSEBUTTONDOWN:
  225. case SDL_MOUSEBUTTONUP:
  226. {
  227. // SDL uses button 3 for the right mouse button, but we use button 2
  228. int button = e.button.button;
  229. switch (button)
  230. {
  231. case SDL_BUTTON_RIGHT:
  232. button = 2;
  233. break;
  234. case SDL_BUTTON_MIDDLE:
  235. button = 3;
  236. break;
  237. }
  238. double px = (double) e.button.x;
  239. double py = (double) e.button.y;
  240. windowToDPICoords(&px, &py);
  241. vargs.emplace_back(px);
  242. vargs.emplace_back(py);
  243. vargs.emplace_back((double) button);
  244. vargs.emplace_back(e.button.which == SDL_TOUCH_MOUSEID);
  245. vargs.emplace_back((double) e.button.clicks);
  246. bool down = e.type == SDL_MOUSEBUTTONDOWN;
  247. msg = new Message(down ? "mousepressed" : "mousereleased", vargs);
  248. }
  249. break;
  250. case SDL_MOUSEWHEEL:
  251. vargs.emplace_back((double) e.wheel.x);
  252. vargs.emplace_back((double) e.wheel.y);
  253. msg = new Message("wheelmoved", vargs);
  254. break;
  255. case SDL_FINGERDOWN:
  256. case SDL_FINGERUP:
  257. case SDL_FINGERMOTION:
  258. // Touch events are disabled in OS X because we only actually want touch
  259. // screen events, but most touch devices in OS X aren't touch screens
  260. // (and SDL doesn't differentiate.) Non-screen touch devices like Mac
  261. // trackpads won't give touch coords in the window's coordinate-space.
  262. #ifndef LOVE_MACOSX
  263. touchinfo.id = (int64) e.tfinger.fingerId;
  264. touchinfo.x = e.tfinger.x;
  265. touchinfo.y = e.tfinger.y;
  266. touchinfo.dx = e.tfinger.dx;
  267. touchinfo.dy = e.tfinger.dy;
  268. touchinfo.pressure = e.tfinger.pressure;
  269. #ifdef LOVE_LINUX
  270. // FIXME: hacky workaround for SDL not normalizing touch coordinates in
  271. // its X11 backend: https://bugzilla.libsdl.org/show_bug.cgi?id=2307
  272. if (touchNormalizationBug || fabs(touchinfo.x) >= 1.5 || fabs(touchinfo.y) >= 1.5 || fabs(touchinfo.dx) >= 1.5 || fabs(touchinfo.dy) >= 1.5)
  273. {
  274. touchNormalizationBug = true;
  275. windowToDPICoords(&touchinfo.x, &touchinfo.y);
  276. windowToDPICoords(&touchinfo.dx, &touchinfo.dy);
  277. }
  278. else
  279. #endif
  280. {
  281. // SDL's coords are normalized to [0, 1], but we want screen coords.
  282. normalizedToDPICoords(&touchinfo.x, &touchinfo.y);
  283. normalizedToDPICoords(&touchinfo.dx, &touchinfo.dy);
  284. }
  285. // We need to update the love.touch.sdl internal state from here.
  286. touchmodule = (touch::sdl::Touch *) Module::getInstance("love.touch.sdl");
  287. if (touchmodule)
  288. touchmodule->onEvent(e.type, touchinfo);
  289. // This is a bit hackish and we lose the higher 32 bits of the id on
  290. // 32-bit systems, but SDL only ever gives id's that at most use as many
  291. // bits as can fit in a pointer (for now.)
  292. // We use lightuserdata instead of a lua_Number (double) because doubles
  293. // can't represent all possible id values on 64-bit systems.
  294. vargs.emplace_back((void *) (intptr_t) touchinfo.id);
  295. vargs.emplace_back(touchinfo.x);
  296. vargs.emplace_back(touchinfo.y);
  297. vargs.emplace_back(touchinfo.dx);
  298. vargs.emplace_back(touchinfo.dy);
  299. vargs.emplace_back(touchinfo.pressure);
  300. if (e.type == SDL_FINGERDOWN)
  301. txt = "touchpressed";
  302. else if (e.type == SDL_FINGERUP)
  303. txt = "touchreleased";
  304. else
  305. txt = "touchmoved";
  306. msg = new Message(txt, vargs);
  307. #endif
  308. break;
  309. case SDL_JOYBUTTONDOWN:
  310. case SDL_JOYBUTTONUP:
  311. case SDL_JOYAXISMOTION:
  312. case SDL_JOYBALLMOTION:
  313. case SDL_JOYHATMOTION:
  314. case SDL_JOYDEVICEADDED:
  315. case SDL_JOYDEVICEREMOVED:
  316. case SDL_CONTROLLERBUTTONDOWN:
  317. case SDL_CONTROLLERBUTTONUP:
  318. case SDL_CONTROLLERAXISMOTION:
  319. msg = convertJoystickEvent(e);
  320. break;
  321. case SDL_WINDOWEVENT:
  322. msg = convertWindowEvent(e);
  323. break;
  324. case SDL_DROPFILE:
  325. filesystem = Module::getInstance<filesystem::Filesystem>(Module::M_FILESYSTEM);
  326. if (filesystem != nullptr)
  327. {
  328. // Allow mounting any dropped path, so zips or dirs can be mounted.
  329. filesystem->allowMountingForPath(e.drop.file);
  330. if (filesystem->isRealDirectory(e.drop.file))
  331. {
  332. vargs.emplace_back(e.drop.file, strlen(e.drop.file));
  333. msg = new Message("directorydropped", vargs);
  334. }
  335. else
  336. {
  337. auto *file = new love::filesystem::DroppedFile(e.drop.file);
  338. vargs.emplace_back(&love::filesystem::DroppedFile::type, file);
  339. msg = new Message("filedropped", vargs);
  340. file->release();
  341. }
  342. }
  343. SDL_free(e.drop.file);
  344. break;
  345. case SDL_QUIT:
  346. case SDL_APP_TERMINATING:
  347. msg = new Message("quit");
  348. break;
  349. case SDL_APP_LOWMEMORY:
  350. msg = new Message("lowmemory");
  351. break;
  352. default:
  353. break;
  354. }
  355. return msg;
  356. }
  357. Message *Event::convertJoystickEvent(const SDL_Event &e) const
  358. {
  359. auto joymodule = Module::getInstance<joystick::JoystickModule>(Module::M_JOYSTICK);
  360. if (!joymodule)
  361. return nullptr;
  362. Message *msg = nullptr;
  363. std::vector<Variant> vargs;
  364. vargs.reserve(4);
  365. love::Type *joysticktype = &love::joystick::Joystick::type;
  366. love::joystick::Joystick *stick = nullptr;
  367. love::joystick::Joystick::Hat hat;
  368. love::joystick::Joystick::GamepadButton padbutton;
  369. love::joystick::Joystick::GamepadAxis padaxis;
  370. const char *txt;
  371. switch (e.type)
  372. {
  373. case SDL_JOYBUTTONDOWN:
  374. case SDL_JOYBUTTONUP:
  375. stick = joymodule->getJoystickFromID(e.jbutton.which);
  376. if (!stick)
  377. break;
  378. vargs.emplace_back(joysticktype, stick);
  379. vargs.emplace_back((double)(e.jbutton.button+1));
  380. msg = new Message((e.type == SDL_JOYBUTTONDOWN) ?
  381. "joystickpressed" : "joystickreleased",
  382. vargs);
  383. break;
  384. case SDL_JOYAXISMOTION:
  385. {
  386. stick = joymodule->getJoystickFromID(e.jaxis.which);
  387. if (!stick)
  388. break;
  389. vargs.emplace_back(joysticktype, stick);
  390. vargs.emplace_back((double)(e.jaxis.axis+1));
  391. float value = joystick::Joystick::clampval(e.jaxis.value / 32768.0f);
  392. vargs.emplace_back((double) value);
  393. msg = new Message("joystickaxis", vargs);
  394. }
  395. break;
  396. case SDL_JOYHATMOTION:
  397. if (!joystick::sdl::Joystick::getConstant(e.jhat.value, hat) || !joystick::Joystick::getConstant(hat, txt))
  398. break;
  399. stick = joymodule->getJoystickFromID(e.jhat.which);
  400. if (!stick)
  401. break;
  402. vargs.emplace_back(joysticktype, stick);
  403. vargs.emplace_back((double)(e.jhat.hat+1));
  404. vargs.emplace_back(txt, strlen(txt));
  405. msg = new Message("joystickhat", vargs);
  406. break;
  407. case SDL_CONTROLLERBUTTONDOWN:
  408. case SDL_CONTROLLERBUTTONUP:
  409. if (!joystick::sdl::Joystick::getConstant((SDL_GameControllerButton) e.cbutton.button, padbutton))
  410. break;
  411. if (!joystick::Joystick::getConstant(padbutton, txt))
  412. break;
  413. stick = joymodule->getJoystickFromID(e.cbutton.which);
  414. if (!stick)
  415. break;
  416. vargs.emplace_back(joysticktype, stick);
  417. vargs.emplace_back(txt, strlen(txt));
  418. msg = new Message(e.type == SDL_CONTROLLERBUTTONDOWN ?
  419. "gamepadpressed" : "gamepadreleased", vargs);
  420. break;
  421. case SDL_CONTROLLERAXISMOTION:
  422. if (joystick::sdl::Joystick::getConstant((SDL_GameControllerAxis) e.caxis.axis, padaxis))
  423. {
  424. if (!joystick::Joystick::getConstant(padaxis, txt))
  425. break;
  426. stick = joymodule->getJoystickFromID(e.caxis.which);
  427. if (!stick)
  428. break;
  429. vargs.emplace_back(joysticktype, stick);
  430. vargs.emplace_back(txt, strlen(txt));
  431. float value = joystick::Joystick::clampval(e.caxis.value / 32768.0f);
  432. vargs.emplace_back((double) value);
  433. msg = new Message("gamepadaxis", vargs);
  434. }
  435. break;
  436. case SDL_JOYDEVICEADDED:
  437. // jdevice.which is the joystick device index.
  438. stick = joymodule->addJoystick(e.jdevice.which);
  439. if (stick)
  440. {
  441. vargs.emplace_back(joysticktype, stick);
  442. msg = new Message("joystickadded", vargs);
  443. }
  444. break;
  445. case SDL_JOYDEVICEREMOVED:
  446. // jdevice.which is the joystick instance ID now.
  447. stick = joymodule->getJoystickFromID(e.jdevice.which);
  448. if (stick)
  449. {
  450. joymodule->removeJoystick(stick);
  451. vargs.emplace_back(joysticktype, stick);
  452. msg = new Message("joystickremoved", vargs);
  453. }
  454. break;
  455. default:
  456. break;
  457. }
  458. return msg;
  459. }
  460. Message *Event::convertWindowEvent(const SDL_Event &e)
  461. {
  462. Message *msg = nullptr;
  463. std::vector<Variant> vargs;
  464. vargs.reserve(4);
  465. window::Window *win = nullptr;
  466. graphics::Graphics *gfx = nullptr;
  467. if (e.type != SDL_WINDOWEVENT)
  468. return nullptr;
  469. switch (e.window.event)
  470. {
  471. case SDL_WINDOWEVENT_FOCUS_GAINED:
  472. case SDL_WINDOWEVENT_FOCUS_LOST:
  473. vargs.emplace_back(e.window.event == SDL_WINDOWEVENT_FOCUS_GAINED);
  474. msg = new Message("focus", vargs);
  475. break;
  476. case SDL_WINDOWEVENT_ENTER:
  477. case SDL_WINDOWEVENT_LEAVE:
  478. vargs.emplace_back(e.window.event == SDL_WINDOWEVENT_ENTER);
  479. msg = new Message("mousefocus", vargs);
  480. break;
  481. case SDL_WINDOWEVENT_SHOWN:
  482. case SDL_WINDOWEVENT_HIDDEN:
  483. vargs.emplace_back(e.window.event == SDL_WINDOWEVENT_SHOWN);
  484. msg = new Message("visible", vargs);
  485. break;
  486. case SDL_WINDOWEVENT_RESIZED:
  487. {
  488. double width = e.window.data1;
  489. double height = e.window.data2;
  490. gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS);
  491. win = Module::getInstance<window::Window>(Module::M_WINDOW);
  492. // WINDOWEVENT_SIZE_CHANGED will always occur before RESIZED.
  493. // The size values in the Window aren't necessarily the same as the
  494. // graphics size, which is what we want to output.
  495. if (gfx)
  496. {
  497. width = gfx->getWidth();
  498. height = gfx->getHeight();
  499. }
  500. else if (win)
  501. {
  502. width = win->getWidth();
  503. height = win->getHeight();
  504. windowToDPICoords(&width, &height);
  505. }
  506. vargs.emplace_back(width);
  507. vargs.emplace_back(height);
  508. msg = new Message("resize", vargs);
  509. }
  510. break;
  511. case SDL_WINDOWEVENT_SIZE_CHANGED:
  512. win = Module::getInstance<window::Window>(Module::M_WINDOW);
  513. if (win)
  514. win->onSizeChanged(e.window.data1, e.window.data2);
  515. break;
  516. case SDL_WINDOWEVENT_MINIMIZED:
  517. case SDL_WINDOWEVENT_RESTORED:
  518. #ifdef LOVE_ANDROID
  519. if (auto audio = Module::getInstance<audio::Audio>(Module::M_AUDIO))
  520. {
  521. if (e.window.event == SDL_WINDOWEVENT_MINIMIZED)
  522. {
  523. for (auto &src : pausedSources)
  524. src->release();
  525. pausedSources = audio->pause();
  526. for (auto &src : pausedSources)
  527. src->retain();
  528. }
  529. else if (e.window.event == SDL_WINDOWEVENT_RESTORED)
  530. {
  531. audio->play(pausedSources);
  532. for (auto &src : pausedSources)
  533. src->release();
  534. pausedSources.resize(0);
  535. }
  536. }
  537. #endif
  538. break;
  539. }
  540. return msg;
  541. }
  542. std::map<SDL_Keycode, love::keyboard::Keyboard::Key> Event::createKeyMap()
  543. {
  544. using love::keyboard::Keyboard;
  545. std::map<SDL_Keycode, Keyboard::Key> k;
  546. k[SDLK_UNKNOWN] = Keyboard::KEY_UNKNOWN;
  547. k[SDLK_RETURN] = Keyboard::KEY_RETURN;
  548. k[SDLK_ESCAPE] = Keyboard::KEY_ESCAPE;
  549. k[SDLK_BACKSPACE] = Keyboard::KEY_BACKSPACE;
  550. k[SDLK_TAB] = Keyboard::KEY_TAB;
  551. k[SDLK_SPACE] = Keyboard::KEY_SPACE;
  552. k[SDLK_EXCLAIM] = Keyboard::KEY_EXCLAIM;
  553. k[SDLK_QUOTEDBL] = Keyboard::KEY_QUOTEDBL;
  554. k[SDLK_HASH] = Keyboard::KEY_HASH;
  555. k[SDLK_PERCENT] = Keyboard::KEY_PERCENT;
  556. k[SDLK_DOLLAR] = Keyboard::KEY_DOLLAR;
  557. k[SDLK_AMPERSAND] = Keyboard::KEY_AMPERSAND;
  558. k[SDLK_QUOTE] = Keyboard::KEY_QUOTE;
  559. k[SDLK_LEFTPAREN] = Keyboard::KEY_LEFTPAREN;
  560. k[SDLK_RIGHTPAREN] = Keyboard::KEY_RIGHTPAREN;
  561. k[SDLK_ASTERISK] = Keyboard::KEY_ASTERISK;
  562. k[SDLK_PLUS] = Keyboard::KEY_PLUS;
  563. k[SDLK_COMMA] = Keyboard::KEY_COMMA;
  564. k[SDLK_MINUS] = Keyboard::KEY_MINUS;
  565. k[SDLK_PERIOD] = Keyboard::KEY_PERIOD;
  566. k[SDLK_SLASH] = Keyboard::KEY_SLASH;
  567. k[SDLK_0] = Keyboard::KEY_0;
  568. k[SDLK_1] = Keyboard::KEY_1;
  569. k[SDLK_2] = Keyboard::KEY_2;
  570. k[SDLK_3] = Keyboard::KEY_3;
  571. k[SDLK_4] = Keyboard::KEY_4;
  572. k[SDLK_5] = Keyboard::KEY_5;
  573. k[SDLK_6] = Keyboard::KEY_6;
  574. k[SDLK_7] = Keyboard::KEY_7;
  575. k[SDLK_8] = Keyboard::KEY_8;
  576. k[SDLK_9] = Keyboard::KEY_9;
  577. k[SDLK_COLON] = Keyboard::KEY_COLON;
  578. k[SDLK_SEMICOLON] = Keyboard::KEY_SEMICOLON;
  579. k[SDLK_LESS] = Keyboard::KEY_LESS;
  580. k[SDLK_EQUALS] = Keyboard::KEY_EQUALS;
  581. k[SDLK_GREATER] = Keyboard::KEY_GREATER;
  582. k[SDLK_QUESTION] = Keyboard::KEY_QUESTION;
  583. k[SDLK_AT] = Keyboard::KEY_AT;
  584. k[SDLK_LEFTBRACKET] = Keyboard::KEY_LEFTBRACKET;
  585. k[SDLK_BACKSLASH] = Keyboard::KEY_BACKSLASH;
  586. k[SDLK_RIGHTBRACKET] = Keyboard::KEY_RIGHTBRACKET;
  587. k[SDLK_CARET] = Keyboard::KEY_CARET;
  588. k[SDLK_UNDERSCORE] = Keyboard::KEY_UNDERSCORE;
  589. k[SDLK_BACKQUOTE] = Keyboard::KEY_BACKQUOTE;
  590. k[SDLK_a] = Keyboard::KEY_A;
  591. k[SDLK_b] = Keyboard::KEY_B;
  592. k[SDLK_c] = Keyboard::KEY_C;
  593. k[SDLK_d] = Keyboard::KEY_D;
  594. k[SDLK_e] = Keyboard::KEY_E;
  595. k[SDLK_f] = Keyboard::KEY_F;
  596. k[SDLK_g] = Keyboard::KEY_G;
  597. k[SDLK_h] = Keyboard::KEY_H;
  598. k[SDLK_i] = Keyboard::KEY_I;
  599. k[SDLK_j] = Keyboard::KEY_J;
  600. k[SDLK_k] = Keyboard::KEY_K;
  601. k[SDLK_l] = Keyboard::KEY_L;
  602. k[SDLK_m] = Keyboard::KEY_M;
  603. k[SDLK_n] = Keyboard::KEY_N;
  604. k[SDLK_o] = Keyboard::KEY_O;
  605. k[SDLK_p] = Keyboard::KEY_P;
  606. k[SDLK_q] = Keyboard::KEY_Q;
  607. k[SDLK_r] = Keyboard::KEY_R;
  608. k[SDLK_s] = Keyboard::KEY_S;
  609. k[SDLK_t] = Keyboard::KEY_T;
  610. k[SDLK_u] = Keyboard::KEY_U;
  611. k[SDLK_v] = Keyboard::KEY_V;
  612. k[SDLK_w] = Keyboard::KEY_W;
  613. k[SDLK_x] = Keyboard::KEY_X;
  614. k[SDLK_y] = Keyboard::KEY_Y;
  615. k[SDLK_z] = Keyboard::KEY_Z;
  616. k[SDLK_CAPSLOCK] = Keyboard::KEY_CAPSLOCK;
  617. k[SDLK_F1] = Keyboard::KEY_F1;
  618. k[SDLK_F2] = Keyboard::KEY_F2;
  619. k[SDLK_F3] = Keyboard::KEY_F3;
  620. k[SDLK_F4] = Keyboard::KEY_F4;
  621. k[SDLK_F5] = Keyboard::KEY_F5;
  622. k[SDLK_F6] = Keyboard::KEY_F6;
  623. k[SDLK_F7] = Keyboard::KEY_F7;
  624. k[SDLK_F8] = Keyboard::KEY_F8;
  625. k[SDLK_F9] = Keyboard::KEY_F9;
  626. k[SDLK_F10] = Keyboard::KEY_F10;
  627. k[SDLK_F11] = Keyboard::KEY_F11;
  628. k[SDLK_F12] = Keyboard::KEY_F12;
  629. k[SDLK_PRINTSCREEN] = Keyboard::KEY_PRINTSCREEN;
  630. k[SDLK_SCROLLLOCK] = Keyboard::KEY_SCROLLLOCK;
  631. k[SDLK_PAUSE] = Keyboard::KEY_PAUSE;
  632. k[SDLK_INSERT] = Keyboard::KEY_INSERT;
  633. k[SDLK_HOME] = Keyboard::KEY_HOME;
  634. k[SDLK_PAGEUP] = Keyboard::KEY_PAGEUP;
  635. k[SDLK_DELETE] = Keyboard::KEY_DELETE;
  636. k[SDLK_END] = Keyboard::KEY_END;
  637. k[SDLK_PAGEDOWN] = Keyboard::KEY_PAGEDOWN;
  638. k[SDLK_RIGHT] = Keyboard::KEY_RIGHT;
  639. k[SDLK_LEFT] = Keyboard::KEY_LEFT;
  640. k[SDLK_DOWN] = Keyboard::KEY_DOWN;
  641. k[SDLK_UP] = Keyboard::KEY_UP;
  642. k[SDLK_NUMLOCKCLEAR] = Keyboard::KEY_NUMLOCKCLEAR;
  643. k[SDLK_KP_DIVIDE] = Keyboard::KEY_KP_DIVIDE;
  644. k[SDLK_KP_MULTIPLY] = Keyboard::KEY_KP_MULTIPLY;
  645. k[SDLK_KP_MINUS] = Keyboard::KEY_KP_MINUS;
  646. k[SDLK_KP_PLUS] = Keyboard::KEY_KP_PLUS;
  647. k[SDLK_KP_ENTER] = Keyboard::KEY_KP_ENTER;
  648. k[SDLK_KP_0] = Keyboard::KEY_KP_0;
  649. k[SDLK_KP_1] = Keyboard::KEY_KP_1;
  650. k[SDLK_KP_2] = Keyboard::KEY_KP_2;
  651. k[SDLK_KP_3] = Keyboard::KEY_KP_3;
  652. k[SDLK_KP_4] = Keyboard::KEY_KP_4;
  653. k[SDLK_KP_5] = Keyboard::KEY_KP_5;
  654. k[SDLK_KP_6] = Keyboard::KEY_KP_6;
  655. k[SDLK_KP_7] = Keyboard::KEY_KP_7;
  656. k[SDLK_KP_8] = Keyboard::KEY_KP_8;
  657. k[SDLK_KP_9] = Keyboard::KEY_KP_9;
  658. k[SDLK_KP_PERIOD] = Keyboard::KEY_KP_PERIOD;
  659. k[SDLK_KP_COMMA] = Keyboard::KEY_KP_COMMA;
  660. k[SDLK_KP_EQUALS] = Keyboard::KEY_KP_EQUALS;
  661. k[SDLK_APPLICATION] = Keyboard::KEY_APPLICATION;
  662. k[SDLK_POWER] = Keyboard::KEY_POWER;
  663. k[SDLK_F13] = Keyboard::KEY_F13;
  664. k[SDLK_F14] = Keyboard::KEY_F14;
  665. k[SDLK_F15] = Keyboard::KEY_F15;
  666. k[SDLK_F16] = Keyboard::KEY_F16;
  667. k[SDLK_F17] = Keyboard::KEY_F17;
  668. k[SDLK_F18] = Keyboard::KEY_F18;
  669. k[SDLK_F19] = Keyboard::KEY_F19;
  670. k[SDLK_F20] = Keyboard::KEY_F20;
  671. k[SDLK_F21] = Keyboard::KEY_F21;
  672. k[SDLK_F22] = Keyboard::KEY_F22;
  673. k[SDLK_F23] = Keyboard::KEY_F23;
  674. k[SDLK_F24] = Keyboard::KEY_F24;
  675. k[SDLK_EXECUTE] = Keyboard::KEY_EXECUTE;
  676. k[SDLK_HELP] = Keyboard::KEY_HELP;
  677. k[SDLK_MENU] = Keyboard::KEY_MENU;
  678. k[SDLK_SELECT] = Keyboard::KEY_SELECT;
  679. k[SDLK_STOP] = Keyboard::KEY_STOP;
  680. k[SDLK_AGAIN] = Keyboard::KEY_AGAIN;
  681. k[SDLK_UNDO] = Keyboard::KEY_UNDO;
  682. k[SDLK_CUT] = Keyboard::KEY_CUT;
  683. k[SDLK_COPY] = Keyboard::KEY_COPY;
  684. k[SDLK_PASTE] = Keyboard::KEY_PASTE;
  685. k[SDLK_FIND] = Keyboard::KEY_FIND;
  686. k[SDLK_MUTE] = Keyboard::KEY_MUTE;
  687. k[SDLK_VOLUMEUP] = Keyboard::KEY_VOLUMEUP;
  688. k[SDLK_VOLUMEDOWN] = Keyboard::KEY_VOLUMEDOWN;
  689. k[SDLK_ALTERASE] = Keyboard::KEY_ALTERASE;
  690. k[SDLK_SYSREQ] = Keyboard::KEY_SYSREQ;
  691. k[SDLK_CANCEL] = Keyboard::KEY_CANCEL;
  692. k[SDLK_CLEAR] = Keyboard::KEY_CLEAR;
  693. k[SDLK_PRIOR] = Keyboard::KEY_PRIOR;
  694. k[SDLK_RETURN2] = Keyboard::KEY_RETURN2;
  695. k[SDLK_SEPARATOR] = Keyboard::KEY_SEPARATOR;
  696. k[SDLK_OUT] = Keyboard::KEY_OUT;
  697. k[SDLK_OPER] = Keyboard::KEY_OPER;
  698. k[SDLK_CLEARAGAIN] = Keyboard::KEY_CLEARAGAIN;
  699. k[SDLK_THOUSANDSSEPARATOR] = Keyboard::KEY_THOUSANDSSEPARATOR;
  700. k[SDLK_DECIMALSEPARATOR] = Keyboard::KEY_DECIMALSEPARATOR;
  701. k[SDLK_CURRENCYUNIT] = Keyboard::KEY_CURRENCYUNIT;
  702. k[SDLK_CURRENCYSUBUNIT] = Keyboard::KEY_CURRENCYSUBUNIT;
  703. k[SDLK_LCTRL] = Keyboard::KEY_LCTRL;
  704. k[SDLK_LSHIFT] = Keyboard::KEY_LSHIFT;
  705. k[SDLK_LALT] = Keyboard::KEY_LALT;
  706. k[SDLK_LGUI] = Keyboard::KEY_LGUI;
  707. k[SDLK_RCTRL] = Keyboard::KEY_RCTRL;
  708. k[SDLK_RSHIFT] = Keyboard::KEY_RSHIFT;
  709. k[SDLK_RALT] = Keyboard::KEY_RALT;
  710. k[SDLK_RGUI] = Keyboard::KEY_RGUI;
  711. k[SDLK_MODE] = Keyboard::KEY_MODE;
  712. k[SDLK_AUDIONEXT] = Keyboard::KEY_AUDIONEXT;
  713. k[SDLK_AUDIOPREV] = Keyboard::KEY_AUDIOPREV;
  714. k[SDLK_AUDIOSTOP] = Keyboard::KEY_AUDIOSTOP;
  715. k[SDLK_AUDIOPLAY] = Keyboard::KEY_AUDIOPLAY;
  716. k[SDLK_AUDIOMUTE] = Keyboard::KEY_AUDIOMUTE;
  717. k[SDLK_MEDIASELECT] = Keyboard::KEY_MEDIASELECT;
  718. k[SDLK_WWW] = Keyboard::KEY_WWW;
  719. k[SDLK_MAIL] = Keyboard::KEY_MAIL;
  720. k[SDLK_CALCULATOR] = Keyboard::KEY_CALCULATOR;
  721. k[SDLK_COMPUTER] = Keyboard::KEY_COMPUTER;
  722. k[SDLK_AC_SEARCH] = Keyboard::KEY_APP_SEARCH;
  723. k[SDLK_AC_HOME] = Keyboard::KEY_APP_HOME;
  724. k[SDLK_AC_BACK] = Keyboard::KEY_APP_BACK;
  725. k[SDLK_AC_FORWARD] = Keyboard::KEY_APP_FORWARD;
  726. k[SDLK_AC_STOP] = Keyboard::KEY_APP_STOP;
  727. k[SDLK_AC_REFRESH] = Keyboard::KEY_APP_REFRESH;
  728. k[SDLK_AC_BOOKMARKS] = Keyboard::KEY_APP_BOOKMARKS;
  729. k[SDLK_BRIGHTNESSDOWN] = Keyboard::KEY_BRIGHTNESSDOWN;
  730. k[SDLK_BRIGHTNESSUP] = Keyboard::KEY_BRIGHTNESSUP;
  731. k[SDLK_DISPLAYSWITCH] = Keyboard::KEY_DISPLAYSWITCH;
  732. k[SDLK_KBDILLUMTOGGLE] = Keyboard::KEY_KBDILLUMTOGGLE;
  733. k[SDLK_KBDILLUMDOWN] = Keyboard::KEY_KBDILLUMDOWN;
  734. k[SDLK_KBDILLUMUP] = Keyboard::KEY_KBDILLUMUP;
  735. k[SDLK_EJECT] = Keyboard::KEY_EJECT;
  736. k[SDLK_SLEEP] = Keyboard::KEY_SLEEP;
  737. #ifdef LOVE_ANDROID
  738. k[SDLK_AC_BACK] = Keyboard::KEY_ESCAPE;
  739. #endif
  740. return k;
  741. }
  742. std::map<SDL_Keycode, love::keyboard::Keyboard::Key> Event::keys = Event::createKeyMap();
  743. } // sdl
  744. } // event
  745. } // love