SDL_emscriptenevents.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
  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. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_EMSCRIPTEN
  20. #include <emscripten/html5.h>
  21. #include "../../events/SDL_events_c.h"
  22. #include "../../events/SDL_keyboard_c.h"
  23. #include "../../events/SDL_touch_c.h"
  24. #include "SDL_emscriptenevents.h"
  25. #include "SDL_emscriptenvideo.h"
  26. #include "SDL_hints.h"
  27. #define FULLSCREEN_MASK ( SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN )
  28. /*
  29. .keyCode to scancode
  30. https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent
  31. https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
  32. */
  33. static const SDL_Scancode emscripten_scancode_table[] = {
  34. /* 0 */ SDL_SCANCODE_UNKNOWN,
  35. /* 1 */ SDL_SCANCODE_UNKNOWN,
  36. /* 2 */ SDL_SCANCODE_UNKNOWN,
  37. /* 3 */ SDL_SCANCODE_CANCEL,
  38. /* 4 */ SDL_SCANCODE_UNKNOWN,
  39. /* 5 */ SDL_SCANCODE_UNKNOWN,
  40. /* 6 */ SDL_SCANCODE_HELP,
  41. /* 7 */ SDL_SCANCODE_UNKNOWN,
  42. /* 8 */ SDL_SCANCODE_BACKSPACE,
  43. /* 9 */ SDL_SCANCODE_TAB,
  44. /* 10 */ SDL_SCANCODE_UNKNOWN,
  45. /* 11 */ SDL_SCANCODE_UNKNOWN,
  46. /* 12 */ SDL_SCANCODE_UNKNOWN,
  47. /* 13 */ SDL_SCANCODE_RETURN,
  48. /* 14 */ SDL_SCANCODE_UNKNOWN,
  49. /* 15 */ SDL_SCANCODE_UNKNOWN,
  50. /* 16 */ SDL_SCANCODE_LSHIFT,
  51. /* 17 */ SDL_SCANCODE_LCTRL,
  52. /* 18 */ SDL_SCANCODE_LALT,
  53. /* 19 */ SDL_SCANCODE_PAUSE,
  54. /* 20 */ SDL_SCANCODE_CAPSLOCK,
  55. /* 21 */ SDL_SCANCODE_UNKNOWN,
  56. /* 22 */ SDL_SCANCODE_UNKNOWN,
  57. /* 23 */ SDL_SCANCODE_UNKNOWN,
  58. /* 24 */ SDL_SCANCODE_UNKNOWN,
  59. /* 25 */ SDL_SCANCODE_UNKNOWN,
  60. /* 26 */ SDL_SCANCODE_UNKNOWN,
  61. /* 27 */ SDL_SCANCODE_ESCAPE,
  62. /* 28 */ SDL_SCANCODE_UNKNOWN,
  63. /* 29 */ SDL_SCANCODE_UNKNOWN,
  64. /* 30 */ SDL_SCANCODE_UNKNOWN,
  65. /* 31 */ SDL_SCANCODE_UNKNOWN,
  66. /* 32 */ SDL_SCANCODE_SPACE,
  67. /* 33 */ SDL_SCANCODE_PAGEUP,
  68. /* 34 */ SDL_SCANCODE_PAGEDOWN,
  69. /* 35 */ SDL_SCANCODE_END,
  70. /* 36 */ SDL_SCANCODE_HOME,
  71. /* 37 */ SDL_SCANCODE_LEFT,
  72. /* 38 */ SDL_SCANCODE_UP,
  73. /* 39 */ SDL_SCANCODE_RIGHT,
  74. /* 40 */ SDL_SCANCODE_DOWN,
  75. /* 41 */ SDL_SCANCODE_UNKNOWN,
  76. /* 42 */ SDL_SCANCODE_UNKNOWN,
  77. /* 43 */ SDL_SCANCODE_UNKNOWN,
  78. /* 44 */ SDL_SCANCODE_UNKNOWN,
  79. /* 45 */ SDL_SCANCODE_INSERT,
  80. /* 46 */ SDL_SCANCODE_DELETE,
  81. /* 47 */ SDL_SCANCODE_UNKNOWN,
  82. /* 48 */ SDL_SCANCODE_0,
  83. /* 49 */ SDL_SCANCODE_1,
  84. /* 50 */ SDL_SCANCODE_2,
  85. /* 51 */ SDL_SCANCODE_3,
  86. /* 52 */ SDL_SCANCODE_4,
  87. /* 53 */ SDL_SCANCODE_5,
  88. /* 54 */ SDL_SCANCODE_6,
  89. /* 55 */ SDL_SCANCODE_7,
  90. /* 56 */ SDL_SCANCODE_8,
  91. /* 57 */ SDL_SCANCODE_9,
  92. /* 58 */ SDL_SCANCODE_UNKNOWN,
  93. /* 59 */ SDL_SCANCODE_SEMICOLON,
  94. /* 60 */ SDL_SCANCODE_UNKNOWN,
  95. /* 61 */ SDL_SCANCODE_EQUALS,
  96. /* 62 */ SDL_SCANCODE_UNKNOWN,
  97. /* 63 */ SDL_SCANCODE_UNKNOWN,
  98. /* 64 */ SDL_SCANCODE_UNKNOWN,
  99. /* 65 */ SDL_SCANCODE_A,
  100. /* 66 */ SDL_SCANCODE_B,
  101. /* 67 */ SDL_SCANCODE_C,
  102. /* 68 */ SDL_SCANCODE_D,
  103. /* 69 */ SDL_SCANCODE_E,
  104. /* 70 */ SDL_SCANCODE_F,
  105. /* 71 */ SDL_SCANCODE_G,
  106. /* 72 */ SDL_SCANCODE_H,
  107. /* 73 */ SDL_SCANCODE_I,
  108. /* 74 */ SDL_SCANCODE_J,
  109. /* 75 */ SDL_SCANCODE_K,
  110. /* 76 */ SDL_SCANCODE_L,
  111. /* 77 */ SDL_SCANCODE_M,
  112. /* 78 */ SDL_SCANCODE_N,
  113. /* 79 */ SDL_SCANCODE_O,
  114. /* 80 */ SDL_SCANCODE_P,
  115. /* 81 */ SDL_SCANCODE_Q,
  116. /* 82 */ SDL_SCANCODE_R,
  117. /* 83 */ SDL_SCANCODE_S,
  118. /* 84 */ SDL_SCANCODE_T,
  119. /* 85 */ SDL_SCANCODE_U,
  120. /* 86 */ SDL_SCANCODE_V,
  121. /* 87 */ SDL_SCANCODE_W,
  122. /* 88 */ SDL_SCANCODE_X,
  123. /* 89 */ SDL_SCANCODE_Y,
  124. /* 90 */ SDL_SCANCODE_Z,
  125. /* 91 */ SDL_SCANCODE_LGUI,
  126. /* 92 */ SDL_SCANCODE_UNKNOWN,
  127. /* 93 */ SDL_SCANCODE_APPLICATION,
  128. /* 94 */ SDL_SCANCODE_UNKNOWN,
  129. /* 95 */ SDL_SCANCODE_UNKNOWN,
  130. /* 96 */ SDL_SCANCODE_KP_0,
  131. /* 97 */ SDL_SCANCODE_KP_1,
  132. /* 98 */ SDL_SCANCODE_KP_2,
  133. /* 99 */ SDL_SCANCODE_KP_3,
  134. /* 100 */ SDL_SCANCODE_KP_4,
  135. /* 101 */ SDL_SCANCODE_KP_5,
  136. /* 102 */ SDL_SCANCODE_KP_6,
  137. /* 103 */ SDL_SCANCODE_KP_7,
  138. /* 104 */ SDL_SCANCODE_KP_8,
  139. /* 105 */ SDL_SCANCODE_KP_9,
  140. /* 106 */ SDL_SCANCODE_KP_MULTIPLY,
  141. /* 107 */ SDL_SCANCODE_KP_PLUS,
  142. /* 108 */ SDL_SCANCODE_UNKNOWN,
  143. /* 109 */ SDL_SCANCODE_KP_MINUS,
  144. /* 110 */ SDL_SCANCODE_KP_PERIOD,
  145. /* 111 */ SDL_SCANCODE_KP_DIVIDE,
  146. /* 112 */ SDL_SCANCODE_F1,
  147. /* 113 */ SDL_SCANCODE_F2,
  148. /* 114 */ SDL_SCANCODE_F3,
  149. /* 115 */ SDL_SCANCODE_F4,
  150. /* 116 */ SDL_SCANCODE_F5,
  151. /* 117 */ SDL_SCANCODE_F6,
  152. /* 118 */ SDL_SCANCODE_F7,
  153. /* 119 */ SDL_SCANCODE_F8,
  154. /* 120 */ SDL_SCANCODE_F9,
  155. /* 121 */ SDL_SCANCODE_F10,
  156. /* 122 */ SDL_SCANCODE_F11,
  157. /* 123 */ SDL_SCANCODE_F12,
  158. /* 124 */ SDL_SCANCODE_F13,
  159. /* 125 */ SDL_SCANCODE_F14,
  160. /* 126 */ SDL_SCANCODE_F15,
  161. /* 127 */ SDL_SCANCODE_F16,
  162. /* 128 */ SDL_SCANCODE_F17,
  163. /* 129 */ SDL_SCANCODE_F18,
  164. /* 130 */ SDL_SCANCODE_F19,
  165. /* 131 */ SDL_SCANCODE_F20,
  166. /* 132 */ SDL_SCANCODE_F21,
  167. /* 133 */ SDL_SCANCODE_F22,
  168. /* 134 */ SDL_SCANCODE_F23,
  169. /* 135 */ SDL_SCANCODE_F24,
  170. /* 136 */ SDL_SCANCODE_UNKNOWN,
  171. /* 137 */ SDL_SCANCODE_UNKNOWN,
  172. /* 138 */ SDL_SCANCODE_UNKNOWN,
  173. /* 139 */ SDL_SCANCODE_UNKNOWN,
  174. /* 140 */ SDL_SCANCODE_UNKNOWN,
  175. /* 141 */ SDL_SCANCODE_UNKNOWN,
  176. /* 142 */ SDL_SCANCODE_UNKNOWN,
  177. /* 143 */ SDL_SCANCODE_UNKNOWN,
  178. /* 144 */ SDL_SCANCODE_NUMLOCKCLEAR,
  179. /* 145 */ SDL_SCANCODE_SCROLLLOCK,
  180. /* 146 */ SDL_SCANCODE_UNKNOWN,
  181. /* 147 */ SDL_SCANCODE_UNKNOWN,
  182. /* 148 */ SDL_SCANCODE_UNKNOWN,
  183. /* 149 */ SDL_SCANCODE_UNKNOWN,
  184. /* 150 */ SDL_SCANCODE_UNKNOWN,
  185. /* 151 */ SDL_SCANCODE_UNKNOWN,
  186. /* 152 */ SDL_SCANCODE_UNKNOWN,
  187. /* 153 */ SDL_SCANCODE_UNKNOWN,
  188. /* 154 */ SDL_SCANCODE_UNKNOWN,
  189. /* 155 */ SDL_SCANCODE_UNKNOWN,
  190. /* 156 */ SDL_SCANCODE_UNKNOWN,
  191. /* 157 */ SDL_SCANCODE_UNKNOWN,
  192. /* 158 */ SDL_SCANCODE_UNKNOWN,
  193. /* 159 */ SDL_SCANCODE_UNKNOWN,
  194. /* 160 */ SDL_SCANCODE_UNKNOWN,
  195. /* 161 */ SDL_SCANCODE_UNKNOWN,
  196. /* 162 */ SDL_SCANCODE_UNKNOWN,
  197. /* 163 */ SDL_SCANCODE_UNKNOWN,
  198. /* 164 */ SDL_SCANCODE_UNKNOWN,
  199. /* 165 */ SDL_SCANCODE_UNKNOWN,
  200. /* 166 */ SDL_SCANCODE_UNKNOWN,
  201. /* 167 */ SDL_SCANCODE_UNKNOWN,
  202. /* 168 */ SDL_SCANCODE_UNKNOWN,
  203. /* 169 */ SDL_SCANCODE_UNKNOWN,
  204. /* 170 */ SDL_SCANCODE_UNKNOWN,
  205. /* 171 */ SDL_SCANCODE_UNKNOWN,
  206. /* 172 */ SDL_SCANCODE_UNKNOWN,
  207. /* 173 */ SDL_SCANCODE_MINUS, /*FX*/
  208. /* 174 */ SDL_SCANCODE_VOLUMEDOWN, /*IE, Chrome*/
  209. /* 175 */ SDL_SCANCODE_VOLUMEUP, /*IE, Chrome*/
  210. /* 176 */ SDL_SCANCODE_AUDIONEXT, /*IE, Chrome*/
  211. /* 177 */ SDL_SCANCODE_AUDIOPREV, /*IE, Chrome*/
  212. /* 178 */ SDL_SCANCODE_UNKNOWN,
  213. /* 179 */ SDL_SCANCODE_AUDIOPLAY, /*IE, Chrome*/
  214. /* 180 */ SDL_SCANCODE_UNKNOWN,
  215. /* 181 */ SDL_SCANCODE_AUDIOMUTE, /*FX*/
  216. /* 182 */ SDL_SCANCODE_VOLUMEDOWN, /*FX*/
  217. /* 183 */ SDL_SCANCODE_VOLUMEUP, /*FX*/
  218. /* 184 */ SDL_SCANCODE_UNKNOWN,
  219. /* 185 */ SDL_SCANCODE_UNKNOWN,
  220. /* 186 */ SDL_SCANCODE_SEMICOLON, /*IE, Chrome, D3E legacy*/
  221. /* 187 */ SDL_SCANCODE_EQUALS, /*IE, Chrome, D3E legacy*/
  222. /* 188 */ SDL_SCANCODE_COMMA,
  223. /* 189 */ SDL_SCANCODE_MINUS, /*IE, Chrome, D3E legacy*/
  224. /* 190 */ SDL_SCANCODE_PERIOD,
  225. /* 191 */ SDL_SCANCODE_SLASH,
  226. /* 192 */ SDL_SCANCODE_GRAVE, /*FX, D3E legacy (SDL_SCANCODE_APOSTROPHE in IE/Chrome)*/
  227. /* 193 */ SDL_SCANCODE_UNKNOWN,
  228. /* 194 */ SDL_SCANCODE_UNKNOWN,
  229. /* 195 */ SDL_SCANCODE_UNKNOWN,
  230. /* 196 */ SDL_SCANCODE_UNKNOWN,
  231. /* 197 */ SDL_SCANCODE_UNKNOWN,
  232. /* 198 */ SDL_SCANCODE_UNKNOWN,
  233. /* 199 */ SDL_SCANCODE_UNKNOWN,
  234. /* 200 */ SDL_SCANCODE_UNKNOWN,
  235. /* 201 */ SDL_SCANCODE_UNKNOWN,
  236. /* 202 */ SDL_SCANCODE_UNKNOWN,
  237. /* 203 */ SDL_SCANCODE_UNKNOWN,
  238. /* 204 */ SDL_SCANCODE_UNKNOWN,
  239. /* 205 */ SDL_SCANCODE_UNKNOWN,
  240. /* 206 */ SDL_SCANCODE_UNKNOWN,
  241. /* 207 */ SDL_SCANCODE_UNKNOWN,
  242. /* 208 */ SDL_SCANCODE_UNKNOWN,
  243. /* 209 */ SDL_SCANCODE_UNKNOWN,
  244. /* 210 */ SDL_SCANCODE_UNKNOWN,
  245. /* 211 */ SDL_SCANCODE_UNKNOWN,
  246. /* 212 */ SDL_SCANCODE_UNKNOWN,
  247. /* 213 */ SDL_SCANCODE_UNKNOWN,
  248. /* 214 */ SDL_SCANCODE_UNKNOWN,
  249. /* 215 */ SDL_SCANCODE_UNKNOWN,
  250. /* 216 */ SDL_SCANCODE_UNKNOWN,
  251. /* 217 */ SDL_SCANCODE_UNKNOWN,
  252. /* 218 */ SDL_SCANCODE_UNKNOWN,
  253. /* 219 */ SDL_SCANCODE_LEFTBRACKET,
  254. /* 220 */ SDL_SCANCODE_BACKSLASH,
  255. /* 221 */ SDL_SCANCODE_RIGHTBRACKET,
  256. /* 222 */ SDL_SCANCODE_APOSTROPHE, /*FX, D3E legacy*/
  257. };
  258. /* "borrowed" from SDL_windowsevents.c */
  259. static int
  260. Emscripten_ConvertUTF32toUTF8(Uint32 codepoint, char * text)
  261. {
  262. if (codepoint <= 0x7F) {
  263. text[0] = (char) codepoint;
  264. text[1] = '\0';
  265. } else if (codepoint <= 0x7FF) {
  266. text[0] = 0xC0 | (char) ((codepoint >> 6) & 0x1F);
  267. text[1] = 0x80 | (char) (codepoint & 0x3F);
  268. text[2] = '\0';
  269. } else if (codepoint <= 0xFFFF) {
  270. text[0] = 0xE0 | (char) ((codepoint >> 12) & 0x0F);
  271. text[1] = 0x80 | (char) ((codepoint >> 6) & 0x3F);
  272. text[2] = 0x80 | (char) (codepoint & 0x3F);
  273. text[3] = '\0';
  274. } else if (codepoint <= 0x10FFFF) {
  275. text[0] = 0xF0 | (char) ((codepoint >> 18) & 0x0F);
  276. text[1] = 0x80 | (char) ((codepoint >> 12) & 0x3F);
  277. text[2] = 0x80 | (char) ((codepoint >> 6) & 0x3F);
  278. text[3] = 0x80 | (char) (codepoint & 0x3F);
  279. text[4] = '\0';
  280. } else {
  281. return SDL_FALSE;
  282. }
  283. return SDL_TRUE;
  284. }
  285. static EM_BOOL
  286. Emscripten_HandlePointerLockChange(int eventType, const EmscriptenPointerlockChangeEvent *changeEvent, void *userData)
  287. {
  288. SDL_WindowData *window_data = (SDL_WindowData *) userData;
  289. /* keep track of lock losses, so we can regrab if/when appropriate. */
  290. window_data->has_pointer_lock = changeEvent->isActive;
  291. return 0;
  292. }
  293. static EM_BOOL
  294. Emscripten_HandleMouseMove(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
  295. {
  296. SDL_WindowData *window_data = userData;
  297. const int isPointerLocked = window_data->has_pointer_lock;
  298. int mx, my;
  299. static double residualx = 0, residualy = 0;
  300. /* rescale (in case canvas is being scaled)*/
  301. double client_w, client_h, xscale, yscale;
  302. emscripten_get_element_css_size(NULL, &client_w, &client_h);
  303. xscale = window_data->window->w / client_w;
  304. yscale = window_data->window->h / client_h;
  305. if (isPointerLocked) {
  306. residualx += mouseEvent->movementX * xscale;
  307. residualy += mouseEvent->movementY * yscale;
  308. /* Let slow sub-pixel motion accumulate. Don't lose it. */
  309. mx = residualx;
  310. residualx -= mx;
  311. my = residualy;
  312. residualy -= my;
  313. } else {
  314. mx = mouseEvent->canvasX * xscale;
  315. my = mouseEvent->canvasY * yscale;
  316. }
  317. SDL_SendMouseMotion(window_data->window, 0, isPointerLocked, mx, my);
  318. return 0;
  319. }
  320. static EM_BOOL
  321. Emscripten_HandleMouseButton(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
  322. {
  323. SDL_WindowData *window_data = userData;
  324. Uint8 sdl_button;
  325. Uint8 sdl_button_state;
  326. SDL_EventType sdl_event_type;
  327. switch (mouseEvent->button) {
  328. case 0:
  329. sdl_button = SDL_BUTTON_LEFT;
  330. break;
  331. case 1:
  332. sdl_button = SDL_BUTTON_MIDDLE;
  333. break;
  334. case 2:
  335. sdl_button = SDL_BUTTON_RIGHT;
  336. break;
  337. default:
  338. return 0;
  339. }
  340. if (eventType == EMSCRIPTEN_EVENT_MOUSEDOWN) {
  341. if (SDL_GetMouse()->relative_mode && !window_data->has_pointer_lock) {
  342. emscripten_request_pointerlock(NULL, 0); /* try to regrab lost pointer lock. */
  343. }
  344. sdl_button_state = SDL_PRESSED;
  345. sdl_event_type = SDL_MOUSEBUTTONDOWN;
  346. } else {
  347. sdl_button_state = SDL_RELEASED;
  348. sdl_event_type = SDL_MOUSEBUTTONUP;
  349. }
  350. SDL_SendMouseButton(window_data->window, 0, sdl_button_state, sdl_button);
  351. return SDL_GetEventState(sdl_event_type) == SDL_ENABLE;
  352. }
  353. static EM_BOOL
  354. Emscripten_HandleMouseFocus(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
  355. {
  356. SDL_WindowData *window_data = userData;
  357. int mx = mouseEvent->canvasX, my = mouseEvent->canvasY;
  358. const int isPointerLocked = window_data->has_pointer_lock;
  359. if (!isPointerLocked) {
  360. /* rescale (in case canvas is being scaled)*/
  361. double client_w, client_h;
  362. emscripten_get_element_css_size(NULL, &client_w, &client_h);
  363. mx = mx * (window_data->window->w / client_w);
  364. my = my * (window_data->window->h / client_h);
  365. SDL_SendMouseMotion(window_data->window, 0, isPointerLocked, mx, my);
  366. }
  367. SDL_SetMouseFocus(eventType == EMSCRIPTEN_EVENT_MOUSEENTER ? window_data->window : NULL);
  368. return SDL_GetEventState(SDL_WINDOWEVENT) == SDL_ENABLE;
  369. }
  370. static EM_BOOL
  371. Emscripten_HandleWheel(int eventType, const EmscriptenWheelEvent *wheelEvent, void *userData)
  372. {
  373. SDL_WindowData *window_data = userData;
  374. SDL_SendMouseWheel(window_data->window, 0, (float)wheelEvent->deltaX, (float)-wheelEvent->deltaY, SDL_MOUSEWHEEL_NORMAL);
  375. return SDL_GetEventState(SDL_MOUSEWHEEL) == SDL_ENABLE;
  376. }
  377. static EM_BOOL
  378. Emscripten_HandleFocus(int eventType, const EmscriptenFocusEvent *wheelEvent, void *userData)
  379. {
  380. SDL_WindowData *window_data = userData;
  381. /* If the user switches away while keys are pressed (such as
  382. * via Alt+Tab), key release events won't be received. */
  383. if (eventType == EMSCRIPTEN_EVENT_BLUR) {
  384. SDL_ResetKeyboard();
  385. }
  386. SDL_SendWindowEvent(window_data->window, eventType == EMSCRIPTEN_EVENT_FOCUS ? SDL_WINDOWEVENT_FOCUS_GAINED : SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
  387. return SDL_GetEventState(SDL_WINDOWEVENT) == SDL_ENABLE;
  388. }
  389. static EM_BOOL
  390. Emscripten_HandleTouch(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData)
  391. {
  392. SDL_WindowData *window_data = userData;
  393. int i;
  394. double client_w, client_h;
  395. int preventDefault = 0;
  396. SDL_TouchID deviceId = 1;
  397. if (SDL_AddTouch(deviceId, SDL_TOUCH_DEVICE_DIRECT, "") < 0) {
  398. return 0;
  399. }
  400. emscripten_get_element_css_size(NULL, &client_w, &client_h);
  401. for (i = 0; i < touchEvent->numTouches; i++) {
  402. SDL_FingerID id;
  403. float x, y;
  404. int mx, my;
  405. if (!touchEvent->touches[i].isChanged)
  406. continue;
  407. id = touchEvent->touches[i].identifier;
  408. x = touchEvent->touches[i].canvasX / client_w;
  409. y = touchEvent->touches[i].canvasY / client_h;
  410. mx = x * window_data->window->w;
  411. my = y * window_data->window->h;
  412. if (eventType == EMSCRIPTEN_EVENT_TOUCHSTART) {
  413. if (!window_data->finger_touching) {
  414. window_data->finger_touching = SDL_TRUE;
  415. window_data->first_finger = id;
  416. SDL_SendMouseMotion(window_data->window, SDL_TOUCH_MOUSEID, 0, mx, my);
  417. SDL_SendMouseButton(window_data->window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
  418. }
  419. SDL_SendTouch(deviceId, id, SDL_TRUE, x, y, 1.0f);
  420. if (!preventDefault && SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
  421. preventDefault = 1;
  422. }
  423. } else if (eventType == EMSCRIPTEN_EVENT_TOUCHMOVE) {
  424. if ((window_data->finger_touching) && (window_data->first_finger == id)) {
  425. SDL_SendMouseMotion(window_data->window, SDL_TOUCH_MOUSEID, 0, mx, my);
  426. }
  427. SDL_SendTouchMotion(deviceId, id, x, y, 1.0f);
  428. if (!preventDefault && SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
  429. preventDefault = 1;
  430. }
  431. } else {
  432. if ((window_data->finger_touching) && (window_data->first_finger == id)) {
  433. SDL_SendMouseButton(window_data->window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
  434. window_data->finger_touching = SDL_FALSE;
  435. }
  436. SDL_SendTouch(deviceId, id, SDL_FALSE, x, y, 1.0f);
  437. if (!preventDefault && SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
  438. preventDefault = 1;
  439. }
  440. }
  441. }
  442. return preventDefault;
  443. }
  444. static EM_BOOL
  445. Emscripten_HandleKey(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData)
  446. {
  447. Uint32 scancode;
  448. SDL_bool prevent_default;
  449. SDL_bool is_nav_key;
  450. /* .keyCode is deprecated, but still the most reliable way to get keys */
  451. if (keyEvent->keyCode < SDL_arraysize(emscripten_scancode_table)) {
  452. scancode = emscripten_scancode_table[keyEvent->keyCode];
  453. if (scancode != SDL_SCANCODE_UNKNOWN) {
  454. if (keyEvent->location == DOM_KEY_LOCATION_RIGHT) {
  455. switch (scancode) {
  456. case SDL_SCANCODE_LSHIFT:
  457. scancode = SDL_SCANCODE_RSHIFT;
  458. break;
  459. case SDL_SCANCODE_LCTRL:
  460. scancode = SDL_SCANCODE_RCTRL;
  461. break;
  462. case SDL_SCANCODE_LALT:
  463. scancode = SDL_SCANCODE_RALT;
  464. break;
  465. case SDL_SCANCODE_LGUI:
  466. scancode = SDL_SCANCODE_RGUI;
  467. break;
  468. }
  469. }
  470. SDL_SendKeyboardKey(eventType == EMSCRIPTEN_EVENT_KEYDOWN ? SDL_PRESSED : SDL_RELEASED, scancode);
  471. }
  472. }
  473. prevent_default = SDL_GetEventState(eventType == EMSCRIPTEN_EVENT_KEYDOWN ? SDL_KEYDOWN : SDL_KEYUP) == SDL_ENABLE;
  474. /* if TEXTINPUT events are enabled we can't prevent keydown or we won't get keypress
  475. * we need to ALWAYS prevent backspace and tab otherwise chrome takes action and does bad navigation UX
  476. */
  477. is_nav_key = keyEvent->keyCode == 8 /* backspace */ ||
  478. keyEvent->keyCode == 9 /* tab */ ||
  479. keyEvent->keyCode == 37 /* left */ ||
  480. keyEvent->keyCode == 38 /* up */ ||
  481. keyEvent->keyCode == 39 /* right */ ||
  482. keyEvent->keyCode == 40 /* down */;
  483. if (eventType == EMSCRIPTEN_EVENT_KEYDOWN && SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE && !is_nav_key)
  484. prevent_default = SDL_FALSE;
  485. return prevent_default;
  486. }
  487. static EM_BOOL
  488. Emscripten_HandleKeyPress(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData)
  489. {
  490. char text[5];
  491. if (Emscripten_ConvertUTF32toUTF8(keyEvent->charCode, text)) {
  492. SDL_SendKeyboardText(text);
  493. }
  494. return SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE;
  495. }
  496. static EM_BOOL
  497. Emscripten_HandleFullscreenChange(int eventType, const EmscriptenFullscreenChangeEvent *fullscreenChangeEvent, void *userData)
  498. {
  499. SDL_WindowData *window_data = userData;
  500. SDL_VideoDisplay *display;
  501. if(fullscreenChangeEvent->isFullscreen)
  502. {
  503. window_data->window->flags |= window_data->requested_fullscreen_mode;
  504. window_data->requested_fullscreen_mode = 0;
  505. if(!window_data->requested_fullscreen_mode)
  506. window_data->window->flags |= SDL_WINDOW_FULLSCREEN; /*we didn't reqest fullscreen*/
  507. }
  508. else
  509. {
  510. window_data->window->flags &= ~FULLSCREEN_MASK;
  511. /* reset fullscreen window if the browser left fullscreen */
  512. display = SDL_GetDisplayForWindow(window_data->window);
  513. if (display->fullscreen_window == window_data->window) {
  514. display->fullscreen_window = NULL;
  515. }
  516. }
  517. return 0;
  518. }
  519. static EM_BOOL
  520. Emscripten_HandleResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData)
  521. {
  522. SDL_WindowData *window_data = userData;
  523. /* update pixel ratio */
  524. if (window_data->window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
  525. window_data->pixel_ratio = emscripten_get_device_pixel_ratio();
  526. }
  527. if(!(window_data->window->flags & FULLSCREEN_MASK))
  528. {
  529. /* this will only work if the canvas size is set through css */
  530. if(window_data->window->flags & SDL_WINDOW_RESIZABLE)
  531. {
  532. double w = window_data->window->w;
  533. double h = window_data->window->h;
  534. if(window_data->external_size) {
  535. emscripten_get_element_css_size(NULL, &w, &h);
  536. }
  537. emscripten_set_canvas_size(w * window_data->pixel_ratio, h * window_data->pixel_ratio);
  538. /* set_canvas_size unsets this */
  539. if (!window_data->external_size && window_data->pixel_ratio != 1.0f) {
  540. emscripten_set_element_css_size(NULL, w, h);
  541. }
  542. SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, w, h);
  543. }
  544. }
  545. return 0;
  546. }
  547. EM_BOOL
  548. Emscripten_HandleCanvasResize(int eventType, const void *reserved, void *userData)
  549. {
  550. /*this is used during fullscreen changes*/
  551. SDL_WindowData *window_data = userData;
  552. if(window_data->fullscreen_resize)
  553. {
  554. double css_w, css_h;
  555. emscripten_get_element_css_size(NULL, &css_w, &css_h);
  556. SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, css_w, css_h);
  557. }
  558. return 0;
  559. }
  560. static EM_BOOL
  561. Emscripten_HandleVisibilityChange(int eventType, const EmscriptenVisibilityChangeEvent *visEvent, void *userData)
  562. {
  563. SDL_WindowData *window_data = userData;
  564. SDL_SendWindowEvent(window_data->window, visEvent->hidden ? SDL_WINDOWEVENT_HIDDEN : SDL_WINDOWEVENT_SHOWN, 0, 0);
  565. return 0;
  566. }
  567. void
  568. Emscripten_RegisterEventHandlers(SDL_WindowData *data)
  569. {
  570. const char *keyElement;
  571. /* There is only one window and that window is the canvas */
  572. emscripten_set_mousemove_callback("#canvas", data, 0, Emscripten_HandleMouseMove);
  573. emscripten_set_mousedown_callback("#canvas", data, 0, Emscripten_HandleMouseButton);
  574. emscripten_set_mouseup_callback("#document", data, 0, Emscripten_HandleMouseButton);
  575. emscripten_set_mouseenter_callback("#canvas", data, 0, Emscripten_HandleMouseFocus);
  576. emscripten_set_mouseleave_callback("#canvas", data, 0, Emscripten_HandleMouseFocus);
  577. emscripten_set_wheel_callback("#canvas", data, 0, Emscripten_HandleWheel);
  578. emscripten_set_focus_callback("#window", data, 0, Emscripten_HandleFocus);
  579. emscripten_set_blur_callback("#window", data, 0, Emscripten_HandleFocus);
  580. emscripten_set_touchstart_callback("#canvas", data, 0, Emscripten_HandleTouch);
  581. emscripten_set_touchend_callback("#canvas", data, 0, Emscripten_HandleTouch);
  582. emscripten_set_touchmove_callback("#canvas", data, 0, Emscripten_HandleTouch);
  583. emscripten_set_touchcancel_callback("#canvas", data, 0, Emscripten_HandleTouch);
  584. emscripten_set_pointerlockchange_callback("#document", data, 0, Emscripten_HandlePointerLockChange);
  585. /* Keyboard events are awkward */
  586. keyElement = SDL_GetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT);
  587. if (!keyElement) keyElement = "#window";
  588. emscripten_set_keydown_callback(keyElement, data, 0, Emscripten_HandleKey);
  589. emscripten_set_keyup_callback(keyElement, data, 0, Emscripten_HandleKey);
  590. emscripten_set_keypress_callback(keyElement, data, 0, Emscripten_HandleKeyPress);
  591. emscripten_set_fullscreenchange_callback("#document", data, 0, Emscripten_HandleFullscreenChange);
  592. emscripten_set_resize_callback("#window", data, 0, Emscripten_HandleResize);
  593. emscripten_set_visibilitychange_callback(data, 0, Emscripten_HandleVisibilityChange);
  594. }
  595. void
  596. Emscripten_UnregisterEventHandlers(SDL_WindowData *data)
  597. {
  598. const char *target;
  599. /* only works due to having one window */
  600. emscripten_set_mousemove_callback("#canvas", NULL, 0, NULL);
  601. emscripten_set_mousedown_callback("#canvas", NULL, 0, NULL);
  602. emscripten_set_mouseup_callback("#document", NULL, 0, NULL);
  603. emscripten_set_mouseenter_callback("#canvas", NULL, 0, NULL);
  604. emscripten_set_mouseleave_callback("#canvas", NULL, 0, NULL);
  605. emscripten_set_wheel_callback("#canvas", NULL, 0, NULL);
  606. emscripten_set_focus_callback("#window", NULL, 0, NULL);
  607. emscripten_set_blur_callback("#window", NULL, 0, NULL);
  608. emscripten_set_touchstart_callback("#canvas", NULL, 0, NULL);
  609. emscripten_set_touchend_callback("#canvas", NULL, 0, NULL);
  610. emscripten_set_touchmove_callback("#canvas", NULL, 0, NULL);
  611. emscripten_set_touchcancel_callback("#canvas", NULL, 0, NULL);
  612. emscripten_set_pointerlockchange_callback("#document", NULL, 0, NULL);
  613. target = SDL_GetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT);
  614. if (!target) {
  615. target = "#window";
  616. }
  617. emscripten_set_keydown_callback(target, NULL, 0, NULL);
  618. emscripten_set_keyup_callback(target, NULL, 0, NULL);
  619. emscripten_set_keypress_callback(target, NULL, 0, NULL);
  620. emscripten_set_fullscreenchange_callback("#document", NULL, 0, NULL);
  621. emscripten_set_resize_callback("#window", NULL, 0, NULL);
  622. emscripten_set_visibilitychange_callback(NULL, 0, NULL);
  623. }
  624. #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
  625. /* vi: set ts=4 sw=4 expandtab: */