1
0

SDL_emscriptenevents.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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. #ifdef SDL_VIDEO_DRIVER_EMSCRIPTEN
  20. #include <emscripten/html5.h>
  21. #include <emscripten/dom_pk_codes.h>
  22. #include "../../events/SDL_dropevents_c.h"
  23. #include "../../events/SDL_events_c.h"
  24. #include "../../events/SDL_keyboard_c.h"
  25. #include "../../events/SDL_touch_c.h"
  26. #include "SDL_emscriptenevents.h"
  27. #include "SDL_emscriptenvideo.h"
  28. /*
  29. Emscripten PK code to scancode
  30. https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent
  31. https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code
  32. */
  33. static const SDL_Scancode emscripten_scancode_table[] = {
  34. /* 0x00 "Unidentified" */ SDL_SCANCODE_UNKNOWN,
  35. /* 0x01 "Escape" */ SDL_SCANCODE_ESCAPE,
  36. /* 0x02 "Digit0" */ SDL_SCANCODE_0,
  37. /* 0x03 "Digit1" */ SDL_SCANCODE_1,
  38. /* 0x04 "Digit2" */ SDL_SCANCODE_2,
  39. /* 0x05 "Digit3" */ SDL_SCANCODE_3,
  40. /* 0x06 "Digit4" */ SDL_SCANCODE_4,
  41. /* 0x07 "Digit5" */ SDL_SCANCODE_5,
  42. /* 0x08 "Digit6" */ SDL_SCANCODE_6,
  43. /* 0x09 "Digit7" */ SDL_SCANCODE_7,
  44. /* 0x0A "Digit8" */ SDL_SCANCODE_8,
  45. /* 0x0B "Digit9" */ SDL_SCANCODE_9,
  46. /* 0x0C "Minus" */ SDL_SCANCODE_MINUS,
  47. /* 0x0D "Equal" */ SDL_SCANCODE_EQUALS,
  48. /* 0x0E "Backspace" */ SDL_SCANCODE_BACKSPACE,
  49. /* 0x0F "Tab" */ SDL_SCANCODE_TAB,
  50. /* 0x10 "KeyQ" */ SDL_SCANCODE_Q,
  51. /* 0x11 "KeyW" */ SDL_SCANCODE_W,
  52. /* 0x12 "KeyE" */ SDL_SCANCODE_E,
  53. /* 0x13 "KeyR" */ SDL_SCANCODE_R,
  54. /* 0x14 "KeyT" */ SDL_SCANCODE_T,
  55. /* 0x15 "KeyY" */ SDL_SCANCODE_Y,
  56. /* 0x16 "KeyU" */ SDL_SCANCODE_U,
  57. /* 0x17 "KeyI" */ SDL_SCANCODE_I,
  58. /* 0x18 "KeyO" */ SDL_SCANCODE_O,
  59. /* 0x19 "KeyP" */ SDL_SCANCODE_P,
  60. /* 0x1A "BracketLeft" */ SDL_SCANCODE_LEFTBRACKET,
  61. /* 0x1B "BracketRight" */ SDL_SCANCODE_RIGHTBRACKET,
  62. /* 0x1C "Enter" */ SDL_SCANCODE_RETURN,
  63. /* 0x1D "ControlLeft" */ SDL_SCANCODE_LCTRL,
  64. /* 0x1E "KeyA" */ SDL_SCANCODE_A,
  65. /* 0x1F "KeyS" */ SDL_SCANCODE_S,
  66. /* 0x20 "KeyD" */ SDL_SCANCODE_D,
  67. /* 0x21 "KeyF" */ SDL_SCANCODE_F,
  68. /* 0x22 "KeyG" */ SDL_SCANCODE_G,
  69. /* 0x23 "KeyH" */ SDL_SCANCODE_H,
  70. /* 0x24 "KeyJ" */ SDL_SCANCODE_J,
  71. /* 0x25 "KeyK" */ SDL_SCANCODE_K,
  72. /* 0x26 "KeyL" */ SDL_SCANCODE_L,
  73. /* 0x27 "Semicolon" */ SDL_SCANCODE_SEMICOLON,
  74. /* 0x28 "Quote" */ SDL_SCANCODE_APOSTROPHE,
  75. /* 0x29 "Backquote" */ SDL_SCANCODE_GRAVE,
  76. /* 0x2A "ShiftLeft" */ SDL_SCANCODE_LSHIFT,
  77. /* 0x2B "Backslash" */ SDL_SCANCODE_BACKSLASH,
  78. /* 0x2C "KeyZ" */ SDL_SCANCODE_Z,
  79. /* 0x2D "KeyX" */ SDL_SCANCODE_X,
  80. /* 0x2E "KeyC" */ SDL_SCANCODE_C,
  81. /* 0x2F "KeyV" */ SDL_SCANCODE_V,
  82. /* 0x30 "KeyB" */ SDL_SCANCODE_B,
  83. /* 0x31 "KeyN" */ SDL_SCANCODE_N,
  84. /* 0x32 "KeyM" */ SDL_SCANCODE_M,
  85. /* 0x33 "Comma" */ SDL_SCANCODE_COMMA,
  86. /* 0x34 "Period" */ SDL_SCANCODE_PERIOD,
  87. /* 0x35 "Slash" */ SDL_SCANCODE_SLASH,
  88. /* 0x36 "ShiftRight" */ SDL_SCANCODE_RSHIFT,
  89. /* 0x37 "NumpadMultiply" */ SDL_SCANCODE_KP_MULTIPLY,
  90. /* 0x38 "AltLeft" */ SDL_SCANCODE_LALT,
  91. /* 0x39 "Space" */ SDL_SCANCODE_SPACE,
  92. /* 0x3A "CapsLock" */ SDL_SCANCODE_CAPSLOCK,
  93. /* 0x3B "F1" */ SDL_SCANCODE_F1,
  94. /* 0x3C "F2" */ SDL_SCANCODE_F2,
  95. /* 0x3D "F3" */ SDL_SCANCODE_F3,
  96. /* 0x3E "F4" */ SDL_SCANCODE_F4,
  97. /* 0x3F "F5" */ SDL_SCANCODE_F5,
  98. /* 0x40 "F6" */ SDL_SCANCODE_F6,
  99. /* 0x41 "F7" */ SDL_SCANCODE_F7,
  100. /* 0x42 "F8" */ SDL_SCANCODE_F8,
  101. /* 0x43 "F9" */ SDL_SCANCODE_F9,
  102. /* 0x44 "F10" */ SDL_SCANCODE_F10,
  103. /* 0x45 "Pause" */ SDL_SCANCODE_PAUSE,
  104. /* 0x46 "ScrollLock" */ SDL_SCANCODE_SCROLLLOCK,
  105. /* 0x47 "Numpad7" */ SDL_SCANCODE_KP_7,
  106. /* 0x48 "Numpad8" */ SDL_SCANCODE_KP_8,
  107. /* 0x49 "Numpad9" */ SDL_SCANCODE_KP_9,
  108. /* 0x4A "NumpadSubtract" */ SDL_SCANCODE_KP_MINUS,
  109. /* 0x4B "Numpad4" */ SDL_SCANCODE_KP_4,
  110. /* 0x4C "Numpad5" */ SDL_SCANCODE_KP_5,
  111. /* 0x4D "Numpad6" */ SDL_SCANCODE_KP_6,
  112. /* 0x4E "NumpadAdd" */ SDL_SCANCODE_KP_PLUS,
  113. /* 0x4F "Numpad1" */ SDL_SCANCODE_KP_1,
  114. /* 0x50 "Numpad2" */ SDL_SCANCODE_KP_2,
  115. /* 0x51 "Numpad3" */ SDL_SCANCODE_KP_3,
  116. /* 0x52 "Numpad0" */ SDL_SCANCODE_KP_0,
  117. /* 0x53 "NumpadDecimal" */ SDL_SCANCODE_KP_PERIOD,
  118. /* 0x54 "PrintScreen" */ SDL_SCANCODE_PRINTSCREEN,
  119. /* 0x55 */ SDL_SCANCODE_UNKNOWN,
  120. /* 0x56 "IntlBackslash" */ SDL_SCANCODE_NONUSBACKSLASH,
  121. /* 0x57 "F11" */ SDL_SCANCODE_F11,
  122. /* 0x58 "F12" */ SDL_SCANCODE_F12,
  123. /* 0x59 "NumpadEqual" */ SDL_SCANCODE_KP_EQUALS,
  124. /* 0x5A */ SDL_SCANCODE_UNKNOWN,
  125. /* 0x5B */ SDL_SCANCODE_UNKNOWN,
  126. /* 0x5C */ SDL_SCANCODE_UNKNOWN,
  127. /* 0x5D */ SDL_SCANCODE_UNKNOWN,
  128. /* 0x5E */ SDL_SCANCODE_UNKNOWN,
  129. /* 0x5F */ SDL_SCANCODE_UNKNOWN,
  130. /* 0x60 */ SDL_SCANCODE_UNKNOWN,
  131. /* 0x61 */ SDL_SCANCODE_UNKNOWN,
  132. /* 0x62 */ SDL_SCANCODE_UNKNOWN,
  133. /* 0x63 */ SDL_SCANCODE_UNKNOWN,
  134. /* 0x64 "F13" */ SDL_SCANCODE_F13,
  135. /* 0x65 "F14" */ SDL_SCANCODE_F14,
  136. /* 0x66 "F15" */ SDL_SCANCODE_F15,
  137. /* 0x67 "F16" */ SDL_SCANCODE_F16,
  138. /* 0x68 "F17" */ SDL_SCANCODE_F17,
  139. /* 0x69 "F18" */ SDL_SCANCODE_F18,
  140. /* 0x6A "F19" */ SDL_SCANCODE_F19,
  141. /* 0x6B "F20" */ SDL_SCANCODE_F20,
  142. /* 0x6C "F21" */ SDL_SCANCODE_F21,
  143. /* 0x6D "F22" */ SDL_SCANCODE_F22,
  144. /* 0x6E "F23" */ SDL_SCANCODE_F23,
  145. /* 0x6F */ SDL_SCANCODE_UNKNOWN,
  146. /* 0x70 "KanaMode" */ SDL_SCANCODE_INTERNATIONAL2,
  147. /* 0x71 "Lang2" */ SDL_SCANCODE_LANG2,
  148. /* 0x72 "Lang1" */ SDL_SCANCODE_LANG1,
  149. /* 0x73 "IntlRo" */ SDL_SCANCODE_INTERNATIONAL1,
  150. /* 0x74 */ SDL_SCANCODE_UNKNOWN,
  151. /* 0x75 */ SDL_SCANCODE_UNKNOWN,
  152. /* 0x76 "F24" */ SDL_SCANCODE_F24,
  153. /* 0x77 */ SDL_SCANCODE_UNKNOWN,
  154. /* 0x78 */ SDL_SCANCODE_UNKNOWN,
  155. /* 0x79 "Convert" */ SDL_SCANCODE_INTERNATIONAL4,
  156. /* 0x7A */ SDL_SCANCODE_UNKNOWN,
  157. /* 0x7B "NonConvert" */ SDL_SCANCODE_INTERNATIONAL5,
  158. /* 0x7C */ SDL_SCANCODE_UNKNOWN,
  159. /* 0x7D "IntlYen" */ SDL_SCANCODE_INTERNATIONAL3,
  160. /* 0x7E "NumpadComma" */ SDL_SCANCODE_KP_COMMA
  161. };
  162. static SDL_Scancode Emscripten_MapScanCode(const char *code)
  163. {
  164. const DOM_PK_CODE_TYPE pk_code = emscripten_compute_dom_pk_code(code);
  165. if (pk_code < SDL_arraysize(emscripten_scancode_table)) {
  166. return emscripten_scancode_table[pk_code];
  167. }
  168. switch (pk_code) {
  169. case DOM_PK_PASTE:
  170. return SDL_SCANCODE_PASTE;
  171. case DOM_PK_MEDIA_TRACK_PREVIOUS:
  172. return SDL_SCANCODE_MEDIA_PREVIOUS_TRACK;
  173. case DOM_PK_CUT:
  174. return SDL_SCANCODE_CUT;
  175. case DOM_PK_COPY:
  176. return SDL_SCANCODE_COPY;
  177. case DOM_PK_MEDIA_TRACK_NEXT:
  178. return SDL_SCANCODE_MEDIA_NEXT_TRACK;
  179. case DOM_PK_NUMPAD_ENTER:
  180. return SDL_SCANCODE_KP_ENTER;
  181. case DOM_PK_CONTROL_RIGHT:
  182. return SDL_SCANCODE_RCTRL;
  183. case DOM_PK_AUDIO_VOLUME_MUTE:
  184. return SDL_SCANCODE_MUTE;
  185. case DOM_PK_MEDIA_PLAY_PAUSE:
  186. return SDL_SCANCODE_MEDIA_PLAY_PAUSE;
  187. case DOM_PK_MEDIA_STOP:
  188. return SDL_SCANCODE_MEDIA_STOP;
  189. case DOM_PK_EJECT:
  190. return SDL_SCANCODE_MEDIA_EJECT;
  191. case DOM_PK_AUDIO_VOLUME_DOWN:
  192. return SDL_SCANCODE_VOLUMEDOWN;
  193. case DOM_PK_AUDIO_VOLUME_UP:
  194. return SDL_SCANCODE_VOLUMEUP;
  195. case DOM_PK_BROWSER_HOME:
  196. return SDL_SCANCODE_AC_HOME;
  197. case DOM_PK_NUMPAD_DIVIDE:
  198. return SDL_SCANCODE_KP_DIVIDE;
  199. case DOM_PK_ALT_RIGHT:
  200. return SDL_SCANCODE_RALT;
  201. case DOM_PK_HELP:
  202. return SDL_SCANCODE_HELP;
  203. case DOM_PK_NUM_LOCK:
  204. return SDL_SCANCODE_NUMLOCKCLEAR;
  205. case DOM_PK_HOME:
  206. return SDL_SCANCODE_HOME;
  207. case DOM_PK_ARROW_UP:
  208. return SDL_SCANCODE_UP;
  209. case DOM_PK_PAGE_UP:
  210. return SDL_SCANCODE_PAGEUP;
  211. case DOM_PK_ARROW_LEFT:
  212. return SDL_SCANCODE_LEFT;
  213. case DOM_PK_ARROW_RIGHT:
  214. return SDL_SCANCODE_RIGHT;
  215. case DOM_PK_END:
  216. return SDL_SCANCODE_END;
  217. case DOM_PK_ARROW_DOWN:
  218. return SDL_SCANCODE_DOWN;
  219. case DOM_PK_PAGE_DOWN:
  220. return SDL_SCANCODE_PAGEDOWN;
  221. case DOM_PK_INSERT:
  222. return SDL_SCANCODE_INSERT;
  223. case DOM_PK_DELETE:
  224. return SDL_SCANCODE_DELETE;
  225. case DOM_PK_META_LEFT:
  226. return SDL_SCANCODE_LGUI;
  227. case DOM_PK_META_RIGHT:
  228. return SDL_SCANCODE_RGUI;
  229. case DOM_PK_CONTEXT_MENU:
  230. return SDL_SCANCODE_APPLICATION;
  231. case DOM_PK_POWER:
  232. return SDL_SCANCODE_POWER;
  233. case DOM_PK_BROWSER_SEARCH:
  234. return SDL_SCANCODE_AC_SEARCH;
  235. case DOM_PK_BROWSER_FAVORITES:
  236. return SDL_SCANCODE_AC_BOOKMARKS;
  237. case DOM_PK_BROWSER_REFRESH:
  238. return SDL_SCANCODE_AC_REFRESH;
  239. case DOM_PK_BROWSER_STOP:
  240. return SDL_SCANCODE_AC_STOP;
  241. case DOM_PK_BROWSER_FORWARD:
  242. return SDL_SCANCODE_AC_FORWARD;
  243. case DOM_PK_BROWSER_BACK:
  244. return SDL_SCANCODE_AC_BACK;
  245. case DOM_PK_MEDIA_SELECT:
  246. return SDL_SCANCODE_MEDIA_SELECT;
  247. }
  248. return SDL_SCANCODE_UNKNOWN;
  249. }
  250. static SDL_Window *Emscripten_GetFocusedWindow(SDL_VideoDevice *device)
  251. {
  252. SDL_Window *window;
  253. for (window = device->windows; window; window = window->next) {
  254. SDL_WindowData *wdata = window->internal;
  255. const int focused = MAIN_THREAD_EM_ASM_INT({
  256. var id = UTF8ToString($0);
  257. try
  258. {
  259. var canvas = document.querySelector(id);
  260. if (canvas) {
  261. return canvas === document.activeElement;
  262. }
  263. }
  264. catch (e)
  265. {
  266. // querySelector throws if not a valid selector
  267. }
  268. return false;
  269. }, wdata->canvas_id);
  270. if (focused) {
  271. break;
  272. }
  273. }
  274. // If the DOM is focused, then at least one canvas in the DOM should be considered focused.
  275. // So in this case, just assume that the first canvas is focused.
  276. if (!window) {
  277. const int focused = MAIN_THREAD_EM_ASM_INT({
  278. return document.hasFocus();
  279. });
  280. if (focused) {
  281. window = device->windows;
  282. }
  283. }
  284. return window;
  285. }
  286. static EM_BOOL 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 Emscripten_HandlePointerLockChangeGlobal(int eventType, const EmscriptenPointerlockChangeEvent *changeEvent, void *userData)
  294. {
  295. SDL_VideoDevice *device = userData;
  296. bool prevent_default = false;
  297. SDL_Window *window;
  298. for (window = device->windows; window; window = window->next) {
  299. prevent_default |= Emscripten_HandlePointerLockChange(eventType, changeEvent, window->internal);
  300. }
  301. return prevent_default;
  302. }
  303. static EM_BOOL Emscripten_HandleWheel(int eventType, const EmscriptenWheelEvent *wheelEvent, void *userData)
  304. {
  305. SDL_WindowData *window_data = userData;
  306. float deltaY = wheelEvent->deltaY;
  307. float deltaX = wheelEvent->deltaX;
  308. switch (wheelEvent->deltaMode) {
  309. case DOM_DELTA_PIXEL:
  310. deltaX /= 100; // 100 pixels make up a step
  311. deltaY /= 100; // 100 pixels make up a step
  312. break;
  313. case DOM_DELTA_LINE:
  314. deltaX /= 3; // 3 lines make up a step
  315. deltaY /= 3; // 3 lines make up a step
  316. break;
  317. case DOM_DELTA_PAGE:
  318. deltaX *= 80; // A page makes up 80 steps
  319. deltaY *= 80; // A page makes up 80 steps
  320. break;
  321. }
  322. SDL_SendMouseWheel(0, window_data->window, SDL_DEFAULT_MOUSE_ID, deltaX, -deltaY, SDL_MOUSEWHEEL_NORMAL);
  323. return SDL_EventEnabled(SDL_EVENT_MOUSE_WHEEL);
  324. }
  325. static EM_BOOL Emscripten_HandleFocus(int eventType, const EmscriptenFocusEvent *focusEvent, void *userData)
  326. {
  327. SDL_VideoDevice *device = userData;
  328. SDL_Window *window = Emscripten_GetFocusedWindow(device);
  329. SDL_EventType sdl_event_type;
  330. /* If the user switches away while keys are pressed (such as
  331. * via Alt+Tab), key release events won't be received. */
  332. if (eventType == EMSCRIPTEN_EVENT_BLUR) {
  333. SDL_ResetKeyboard();
  334. }
  335. sdl_event_type = (eventType == EMSCRIPTEN_EVENT_FOCUS) ? SDL_EVENT_WINDOW_FOCUS_GAINED : SDL_EVENT_WINDOW_FOCUS_LOST;
  336. SDL_SetKeyboardFocus(sdl_event_type == SDL_EVENT_WINDOW_FOCUS_GAINED ? window : NULL);
  337. return SDL_EventEnabled(sdl_event_type);
  338. }
  339. static bool IsFunctionKey(SDL_Scancode scancode)
  340. {
  341. if (scancode >= SDL_SCANCODE_F1 && scancode <= SDL_SCANCODE_F12) {
  342. return true;
  343. }
  344. if (scancode >= SDL_SCANCODE_F13 && scancode <= SDL_SCANCODE_F24) {
  345. return true;
  346. }
  347. return false;
  348. }
  349. /* This is a great tool to see web keyboard events live:
  350. * https://w3c.github.io/uievents/tools/key-event-viewer.html
  351. */
  352. static EM_BOOL Emscripten_HandleKey(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData)
  353. {
  354. SDL_WindowData *window_data = (SDL_WindowData *)userData;
  355. SDL_Scancode scancode = Emscripten_MapScanCode(keyEvent->code);
  356. SDL_Keycode keycode = SDLK_UNKNOWN;
  357. bool prevent_default = false;
  358. bool is_nav_key = false;
  359. if (scancode == SDL_SCANCODE_UNKNOWN) {
  360. if (SDL_strcmp(keyEvent->key, "Sleep") == 0) {
  361. scancode = SDL_SCANCODE_SLEEP;
  362. } else if (SDL_strcmp(keyEvent->key, "ChannelUp") == 0) {
  363. scancode = SDL_SCANCODE_CHANNEL_INCREMENT;
  364. } else if (SDL_strcmp(keyEvent->key, "ChannelDown") == 0) {
  365. scancode = SDL_SCANCODE_CHANNEL_DECREMENT;
  366. } else if (SDL_strcmp(keyEvent->key, "MediaPlay") == 0) {
  367. scancode = SDL_SCANCODE_MEDIA_PLAY;
  368. } else if (SDL_strcmp(keyEvent->key, "MediaPause") == 0) {
  369. scancode = SDL_SCANCODE_MEDIA_PAUSE;
  370. } else if (SDL_strcmp(keyEvent->key, "MediaRecord") == 0) {
  371. scancode = SDL_SCANCODE_MEDIA_RECORD;
  372. } else if (SDL_strcmp(keyEvent->key, "MediaFastForward") == 0) {
  373. scancode = SDL_SCANCODE_MEDIA_FAST_FORWARD;
  374. } else if (SDL_strcmp(keyEvent->key, "MediaRewind") == 0) {
  375. scancode = SDL_SCANCODE_MEDIA_REWIND;
  376. } else if (SDL_strcmp(keyEvent->key, "Close") == 0) {
  377. scancode = SDL_SCANCODE_AC_CLOSE;
  378. } else if (SDL_strcmp(keyEvent->key, "New") == 0) {
  379. scancode = SDL_SCANCODE_AC_NEW;
  380. } else if (SDL_strcmp(keyEvent->key, "Open") == 0) {
  381. scancode = SDL_SCANCODE_AC_OPEN;
  382. } else if (SDL_strcmp(keyEvent->key, "Print") == 0) {
  383. scancode = SDL_SCANCODE_AC_PRINT;
  384. } else if (SDL_strcmp(keyEvent->key, "Save") == 0) {
  385. scancode = SDL_SCANCODE_AC_SAVE;
  386. } else if (SDL_strcmp(keyEvent->key, "Props") == 0) {
  387. scancode = SDL_SCANCODE_AC_PROPERTIES;
  388. }
  389. }
  390. if (scancode == SDL_SCANCODE_UNKNOWN) {
  391. // KaiOS Left Soft Key and Right Soft Key, they act as OK/Next/Menu and Cancel/Back/Clear
  392. if (SDL_strcmp(keyEvent->key, "SoftLeft") == 0) {
  393. scancode = SDL_SCANCODE_AC_FORWARD;
  394. } else if (SDL_strcmp(keyEvent->key, "SoftRight") == 0) {
  395. scancode = SDL_SCANCODE_AC_BACK;
  396. }
  397. }
  398. if (keyEvent->location == 0 && SDL_utf8strlen(keyEvent->key) == 1) {
  399. const char *key = keyEvent->key;
  400. keycode = SDL_StepUTF8(&key, NULL);
  401. if (keycode == SDL_INVALID_UNICODE_CODEPOINT) {
  402. keycode = SDLK_UNKNOWN;
  403. }
  404. }
  405. if (keycode != SDLK_UNKNOWN) {
  406. prevent_default = SDL_SendKeyboardKeyAndKeycode(0, SDL_DEFAULT_KEYBOARD_ID, 0, scancode, keycode, (eventType == EMSCRIPTEN_EVENT_KEYDOWN));
  407. } else {
  408. prevent_default = SDL_SendKeyboardKey(0, SDL_DEFAULT_KEYBOARD_ID, 0, scancode, (eventType == EMSCRIPTEN_EVENT_KEYDOWN));
  409. }
  410. /* if TEXTINPUT events are enabled we can't prevent keydown or we won't get keypress
  411. * we need to ALWAYS prevent backspace and tab otherwise chrome takes action and does bad navigation UX
  412. */
  413. if ((scancode == SDL_SCANCODE_BACKSPACE) ||
  414. (scancode == SDL_SCANCODE_TAB) ||
  415. (scancode == SDL_SCANCODE_LEFT) ||
  416. (scancode == SDL_SCANCODE_UP) ||
  417. (scancode == SDL_SCANCODE_RIGHT) ||
  418. (scancode == SDL_SCANCODE_DOWN) ||
  419. IsFunctionKey(scancode) ||
  420. keyEvent->ctrlKey) {
  421. is_nav_key = true;
  422. }
  423. if ((eventType == EMSCRIPTEN_EVENT_KEYDOWN) && SDL_TextInputActive(window_data->window) && !is_nav_key) {
  424. prevent_default = false;
  425. }
  426. return prevent_default;
  427. }
  428. static EM_BOOL Emscripten_HandleKeyPress(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData)
  429. {
  430. SDL_WindowData *window_data = (SDL_WindowData *)userData;
  431. if (SDL_TextInputActive(window_data->window)) {
  432. char text[5];
  433. char *end = SDL_UCS4ToUTF8(keyEvent->charCode, text);
  434. *end = '\0';
  435. SDL_SendKeyboardText(text);
  436. return EM_TRUE;
  437. }
  438. return EM_FALSE;
  439. }
  440. static EM_BOOL Emscripten_HandleFullscreenChange(int eventType, const EmscriptenFullscreenChangeEvent *fullscreenChangeEvent, void *userData)
  441. {
  442. SDL_WindowData *window_data = userData;
  443. window_data->fullscreen_change_in_progress = false;
  444. if (fullscreenChangeEvent->isFullscreen) {
  445. SDL_SendWindowEvent(window_data->window, SDL_EVENT_WINDOW_ENTER_FULLSCREEN, 0, 0);
  446. window_data->fullscreen_mode_flags = 0;
  447. } else {
  448. SDL_SendWindowEvent(window_data->window, SDL_EVENT_WINDOW_LEAVE_FULLSCREEN, 0, 0);
  449. }
  450. SDL_UpdateFullscreenMode(window_data->window, fullscreenChangeEvent->isFullscreen, false);
  451. return 0;
  452. }
  453. static EM_BOOL Emscripten_HandleFullscreenChangeGlobal(int eventType, const EmscriptenFullscreenChangeEvent *fullscreenChangeEvent, void *userData)
  454. {
  455. SDL_VideoDevice *device = userData;
  456. SDL_Window *window = NULL;
  457. for (window = device->windows; window != NULL; window = window->next) {
  458. const char *canvas_id = window->internal->canvas_id;
  459. if (*canvas_id == '#') {
  460. canvas_id++;
  461. }
  462. if (SDL_strcmp(fullscreenChangeEvent->id, canvas_id) == 0) {
  463. break; // this is the window.
  464. }
  465. }
  466. if (window) {
  467. return Emscripten_HandleFullscreenChange(eventType, fullscreenChangeEvent, window->internal);
  468. }
  469. return EM_FALSE;
  470. }
  471. static EM_BOOL Emscripten_HandleResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData)
  472. {
  473. SDL_WindowData *window_data = userData;
  474. bool force = false;
  475. // update pixel ratio
  476. if (window_data->window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
  477. if (window_data->pixel_ratio != emscripten_get_device_pixel_ratio()) {
  478. window_data->pixel_ratio = emscripten_get_device_pixel_ratio();
  479. force = true;
  480. }
  481. }
  482. const bool fill_document = (Emscripten_fill_document_window == window_data->window);
  483. const bool fullscreen = (window_data->window->flags & SDL_WINDOW_FULLSCREEN) != 0; // fullscreen windows can resize on Emscripten, and the canvas should fill it.
  484. const bool resizable = (window_data->window->flags & SDL_WINDOW_RESIZABLE) != 0;
  485. if (fill_document || fullscreen || resizable) {
  486. double w, h;
  487. if (fill_document || fullscreen) {
  488. w = (double) uiEvent->windowInnerWidth;
  489. h = (double) uiEvent->windowInnerHeight;
  490. } else {
  491. SDL_assert(window_data->window->flags & SDL_WINDOW_RESIZABLE);
  492. w = window_data->window->w;
  493. h = window_data->window->h;
  494. // this will only work if the canvas size is set through css
  495. if (window_data->external_size) {
  496. emscripten_get_element_css_size(window_data->canvas_id, &w, &h);
  497. }
  498. }
  499. emscripten_set_canvas_element_size(window_data->canvas_id, SDL_lroundf(w * window_data->pixel_ratio), SDL_lroundf(h * window_data->pixel_ratio));
  500. // set_canvas_size unsets this
  501. if (!window_data->external_size && window_data->pixel_ratio != 1.0f) {
  502. emscripten_set_element_css_size(window_data->canvas_id, w, h);
  503. }
  504. if (force) {
  505. // force the event to trigger, so pixel ratio changes can be handled
  506. window_data->window->w = 0;
  507. window_data->window->h = 0;
  508. }
  509. SDL_SendWindowEvent(window_data->window, SDL_EVENT_WINDOW_RESIZED, SDL_lroundf(w), SDL_lroundf(h));
  510. }
  511. return 0;
  512. }
  513. static EM_BOOL Emscripten_HandleResizeGlobal(int eventType, const EmscriptenUiEvent *uiEvent, void *userData)
  514. {
  515. SDL_VideoDevice *device = userData;
  516. bool prevent_default = false;
  517. SDL_Window *window;
  518. for (window = device->windows; window; window = window->next) {
  519. prevent_default |= Emscripten_HandleResize(eventType, uiEvent, window->internal);
  520. }
  521. return prevent_default;
  522. }
  523. EM_BOOL
  524. Emscripten_HandleCanvasResize(int eventType, const void *reserved, void *userData)
  525. {
  526. // this is used during fullscreen changes
  527. SDL_WindowData *window_data = userData;
  528. if (window_data->fullscreen_resize) {
  529. double css_w, css_h;
  530. emscripten_get_element_css_size(window_data->canvas_id, &css_w, &css_h);
  531. SDL_SendWindowEvent(window_data->window, SDL_EVENT_WINDOW_RESIZED, SDL_lroundf(css_w), SDL_lroundf(css_h));
  532. }
  533. return 0;
  534. }
  535. static EM_BOOL Emscripten_HandleVisibilityChange(int eventType, const EmscriptenVisibilityChangeEvent *visEvent, void *userData)
  536. {
  537. SDL_WindowData *window_data = userData;
  538. SDL_SendWindowEvent(window_data->window, visEvent->hidden ? SDL_EVENT_WINDOW_HIDDEN : SDL_EVENT_WINDOW_SHOWN, 0, 0);
  539. return 0;
  540. }
  541. static const char *Emscripten_HandleBeforeUnload(int eventType, const void *reserved, void *userData)
  542. {
  543. /* This event will need to be handled synchronously, e.g. using
  544. SDL_AddEventWatch, as the page is being closed *now*. */
  545. // No need to send a SDL_EVENT_QUIT, the app won't get control again.
  546. SDL_SendAppEvent(SDL_EVENT_TERMINATING);
  547. return ""; // don't trigger confirmation dialog
  548. }
  549. static EM_BOOL Emscripten_HandleOrientationChange(int eventType, const EmscriptenOrientationChangeEvent *orientationChangeEvent, void *userData)
  550. {
  551. SDL_DisplayOrientation orientation;
  552. switch (orientationChangeEvent->orientationIndex) {
  553. #define CHECK_ORIENTATION(emsdk, sdl) case EMSCRIPTEN_ORIENTATION_##emsdk: orientation = SDL_ORIENTATION_##sdl; break
  554. CHECK_ORIENTATION(LANDSCAPE_PRIMARY, LANDSCAPE);
  555. CHECK_ORIENTATION(LANDSCAPE_SECONDARY, LANDSCAPE_FLIPPED);
  556. CHECK_ORIENTATION(PORTRAIT_PRIMARY, PORTRAIT);
  557. CHECK_ORIENTATION(PORTRAIT_SECONDARY, PORTRAIT_FLIPPED);
  558. #undef CHECK_ORIENTATION
  559. default: orientation = SDL_ORIENTATION_UNKNOWN; break;
  560. }
  561. SDL_WindowData *window_data = (SDL_WindowData *) userData;
  562. SDL_SendDisplayEvent(SDL_GetVideoDisplayForWindow(window_data->window), SDL_EVENT_DISPLAY_ORIENTATION, orientation, 0);
  563. // fake a UI event so we can tell the app the canvas might have resized.
  564. EmscriptenUiEvent uiEvent;
  565. SDL_zero(uiEvent);
  566. uiEvent.documentBodyClientWidth = MAIN_THREAD_EM_ASM_INT( { return document.body.clientWidth; } );
  567. uiEvent.documentBodyClientHeight = MAIN_THREAD_EM_ASM_INT( { return document.body.clientHeight; } );
  568. uiEvent.windowInnerWidth = MAIN_THREAD_EM_ASM_INT( { return window.innerWidth; } );
  569. uiEvent.windowInnerHeight = MAIN_THREAD_EM_ASM_INT( { return window.innerHeight; } );
  570. uiEvent.windowOuterWidth = MAIN_THREAD_EM_ASM_INT( { return window.outerWidth; } );
  571. uiEvent.windowOuterHeight = MAIN_THREAD_EM_ASM_INT( { return window.outerHeight; } );
  572. uiEvent.scrollTop = MAIN_THREAD_EM_ASM_INT( { return window.pageXOffset; } );
  573. uiEvent.scrollLeft = MAIN_THREAD_EM_ASM_INT( { return window.pageYOffset; } );
  574. Emscripten_HandleResize(EMSCRIPTEN_EVENT_RESIZE, &uiEvent, userData);
  575. return 0;
  576. }
  577. // IF YOU CHANGE THIS STRUCTURE, YOU NEED TO UPDATE THE JAVASCRIPT THAT FILLS IT IN: SDL3.makePointerEventCStruct, below.
  578. #define PTRTYPE_MOUSE 1
  579. #define PTRTYPE_TOUCH 2
  580. #define PTRTYPE_PEN 3
  581. typedef struct Emscripten_PointerEvent
  582. {
  583. int pointer_type;
  584. int pointerid;
  585. int button;
  586. int buttons;
  587. int down;
  588. float movementX;
  589. float movementY;
  590. float targetX;
  591. float targetY;
  592. float pressure;
  593. float tangential_pressure;
  594. float tiltx;
  595. float tilty;
  596. float rotation;
  597. } Emscripten_PointerEvent;
  598. static void Emscripten_HandleMouseButton(SDL_WindowData *window_data, const Emscripten_PointerEvent *event)
  599. {
  600. Uint8 sdl_button;
  601. bool down = false;
  602. switch (event->button) {
  603. #define CHECK_MOUSE_BUTTON(jsbutton, downflag, sdlbutton) case jsbutton: sdl_button = SDL_BUTTON_##sdlbutton; down = (event->down != 0) || ((event->buttons & downflag) != 0); break
  604. CHECK_MOUSE_BUTTON(0, 1, LEFT);
  605. CHECK_MOUSE_BUTTON(1, 4, MIDDLE);
  606. CHECK_MOUSE_BUTTON(2, 2, RIGHT);
  607. CHECK_MOUSE_BUTTON(3, 8, X1);
  608. CHECK_MOUSE_BUTTON(4, 16, X2);
  609. #undef CHECK_MOUSE_BUTTON
  610. default: sdl_button = 0; break;
  611. }
  612. if (sdl_button) {
  613. const SDL_Mouse *mouse = SDL_GetMouse();
  614. SDL_assert(mouse != NULL);
  615. if (down) {
  616. if (mouse->relative_mode && !window_data->has_pointer_lock) {
  617. emscripten_request_pointerlock(window_data->canvas_id, 0); // try to regrab lost pointer lock.
  618. }
  619. }
  620. SDL_SendMouseButton(0, window_data->window, SDL_DEFAULT_MOUSE_ID, sdl_button, down);
  621. // We have an imaginary mouse capture, because we need SDL to not drop our imaginary mouse focus when we leave the canvas.
  622. if (mouse->auto_capture) {
  623. if (SDL_GetMouseState(NULL, NULL) != 0) {
  624. window_data->window->flags |= SDL_WINDOW_MOUSE_CAPTURE;
  625. } else {
  626. window_data->window->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
  627. }
  628. }
  629. if (!down && window_data->mouse_focus_loss_pending) {
  630. window_data->mouse_focus_loss_pending = (window_data->window->flags & SDL_WINDOW_MOUSE_CAPTURE) != 0;
  631. if (!window_data->mouse_focus_loss_pending) {
  632. SDL_SetMouseFocus(NULL);
  633. }
  634. }
  635. }
  636. }
  637. static void Emscripten_UpdateMouseFromEvent(SDL_WindowData *window_data, const Emscripten_PointerEvent *event)
  638. {
  639. SDL_assert(event->pointer_type == PTRTYPE_MOUSE);
  640. // Hidden windows (e.g. a shared GL-context window) may share the DOM canvas with the
  641. // visible window. Their pointer-event listeners would otherwise fire alongside the
  642. // visible window's, fighting over `mouse->focus` and producing events tagged with the
  643. // hidden window's ID -- causing downstream consumers that key by window ID to silently
  644. // drop them. Hidden windows shouldn't take part in user-input dispatch.
  645. if (window_data->window->flags & SDL_WINDOW_HIDDEN) {
  646. return;
  647. }
  648. // rescale (in case canvas is being scaled)
  649. double client_w, client_h;
  650. emscripten_get_element_css_size(window_data->canvas_id, &client_w, &client_h);
  651. const double xscale = window_data->window->w / client_w;
  652. const double yscale = window_data->window->h / client_h;
  653. const bool isPointerLocked = window_data->has_pointer_lock;
  654. float mx, my;
  655. if (isPointerLocked) {
  656. mx = (float)(event->movementX * xscale);
  657. my = (float)(event->movementY * yscale);
  658. } else {
  659. mx = (float)(event->targetX * xscale);
  660. my = (float)(event->targetY * yscale);
  661. }
  662. SDL_SendMouseMotion(0, window_data->window, SDL_DEFAULT_MOUSE_ID, isPointerLocked, mx, my);
  663. Emscripten_HandleMouseButton(window_data, event);
  664. }
  665. static void Emscripten_UpdateTouchFromEvent(SDL_WindowData *window_data, const Emscripten_PointerEvent *event)
  666. {
  667. SDL_assert(event->pointer_type == PTRTYPE_TOUCH);
  668. const SDL_TouchID deviceId = 1;
  669. if (SDL_AddTouch(deviceId, SDL_TOUCH_DEVICE_DIRECT, "") < 0) {
  670. return;
  671. }
  672. double client_w, client_h;
  673. emscripten_get_element_css_size(window_data->canvas_id, &client_w, &client_h);
  674. const SDL_FingerID id = event->pointerid + 1;
  675. float x, y;
  676. if (client_w <= 1) {
  677. x = 0.5f;
  678. } else {
  679. x = event->targetX / (client_w - 1);
  680. }
  681. if (client_h <= 1) {
  682. y = 0.5f;
  683. } else {
  684. y = event->targetY / (client_h - 1);
  685. }
  686. const bool down = (event->buttons & 1) != 0;
  687. if (event->button == 0) { // touch is starting or ending if this is zero (-1 means "no change").
  688. if (down) {
  689. SDL_SendTouch(0, deviceId, id, window_data->window, SDL_EVENT_FINGER_DOWN, x, y, 1.0f);
  690. }
  691. }
  692. SDL_SendTouchMotion(0, deviceId, id, window_data->window, x, y, 1.0f);
  693. if (event->button == 0) { // touch is starting or ending if this is zero (-1 means "no change").
  694. if (!down) {
  695. SDL_SendTouch(0, deviceId, id, window_data->window, SDL_EVENT_FINGER_UP, x, y, 1.0f);
  696. }
  697. }
  698. }
  699. static void Emscripten_UpdatePenFromEvent(SDL_WindowData *window_data, const Emscripten_PointerEvent *event)
  700. {
  701. SDL_assert(event->pointer_type == PTRTYPE_PEN);
  702. const SDL_PenID pen = SDL_FindPenByHandle((void *) (size_t) 1); // something > 0 for the single pen handle.
  703. if (pen) {
  704. // rescale (in case canvas is being scaled)
  705. double client_w, client_h;
  706. emscripten_get_element_css_size(window_data->canvas_id, &client_w, &client_h);
  707. const double xscale = window_data->window->w / client_w;
  708. const double yscale = window_data->window->h / client_h;
  709. const bool isPointerLocked = window_data->has_pointer_lock;
  710. float mx, my;
  711. if (isPointerLocked) {
  712. mx = (float)(event->movementX * xscale);
  713. my = (float)(event->movementY * yscale);
  714. } else {
  715. mx = (float)(event->targetX * xscale);
  716. my = (float)(event->targetY * yscale);
  717. }
  718. SDL_SendPenMotion(0, pen, window_data->window, mx, my);
  719. if (event->button == 0) { // pen touch
  720. bool down = ((event->buttons & 1) != 0);
  721. SDL_SendPenTouch(0, pen, window_data->window, false, down);
  722. } else if (event->button == 5) { // eraser touch...? Not sure if this is right...
  723. bool down = ((event->buttons & 32) != 0);
  724. SDL_SendPenTouch(0, pen, window_data->window, true, down);
  725. } else if (event->button == 1) {
  726. bool down = ((event->buttons & 4) != 0);
  727. SDL_SendPenButton(0, pen, window_data->window, 2, down);
  728. } else if (event->button == 2) {
  729. bool down = ((event->buttons & 2) != 0);
  730. SDL_SendPenButton(0, pen, window_data->window, 1, down);
  731. }
  732. SDL_SendPenAxis(0, pen, window_data->window, SDL_PEN_AXIS_PRESSURE, event->pressure);
  733. SDL_SendPenAxis(0, pen, window_data->window, SDL_PEN_AXIS_TANGENTIAL_PRESSURE, event->tangential_pressure);
  734. SDL_SendPenAxis(0, pen, window_data->window, SDL_PEN_AXIS_XTILT, event->tiltx);
  735. SDL_SendPenAxis(0, pen, window_data->window, SDL_PEN_AXIS_YTILT, event->tilty);
  736. SDL_SendPenAxis(0, pen, window_data->window, SDL_PEN_AXIS_ROTATION, event->rotation);
  737. }
  738. }
  739. static void Emscripten_UpdatePointerFromEvent(SDL_WindowData *window_data, const Emscripten_PointerEvent *event)
  740. {
  741. SDL_assert(event != NULL);
  742. if (event->pointer_type == PTRTYPE_MOUSE) {
  743. Emscripten_UpdateMouseFromEvent(window_data, event);
  744. } else if (event->pointer_type == PTRTYPE_TOUCH) {
  745. Emscripten_UpdateTouchFromEvent(window_data, event);
  746. } else if (event->pointer_type == PTRTYPE_PEN) {
  747. Emscripten_UpdatePenFromEvent(window_data, event);
  748. } else {
  749. SDL_assert(!"Unexpected pointer event type");
  750. }
  751. }
  752. static void Emscripten_HandleMouseFocus(SDL_WindowData *window_data, const Emscripten_PointerEvent *event, bool isenter)
  753. {
  754. SDL_assert(event->pointer_type == PTRTYPE_MOUSE);
  755. // Hidden windows shouldn't ever become the mouse-focus target.
  756. if (window_data->window->flags & SDL_WINDOW_HIDDEN) {
  757. return;
  758. }
  759. const bool isPointerLocked = window_data->has_pointer_lock;
  760. if (!isPointerLocked) {
  761. // rescale (in case canvas is being scaled)
  762. float mx, my;
  763. double client_w, client_h;
  764. emscripten_get_element_css_size(window_data->canvas_id, &client_w, &client_h);
  765. mx = (float)(event->targetX * (window_data->window->w / client_w));
  766. my = (float)(event->targetY * (window_data->window->h / client_h));
  767. SDL_SendMouseMotion(0, window_data->window, SDL_GLOBAL_MOUSE_ID, isPointerLocked, mx, my);
  768. }
  769. if (isenter && window_data->mouse_focus_loss_pending) {
  770. window_data->mouse_focus_loss_pending = false; // just drop the state, but don't send the enter event.
  771. } else if (!isenter && (window_data->window->flags & SDL_WINDOW_MOUSE_CAPTURE)) {
  772. window_data->mouse_focus_loss_pending = true; // waiting on a mouse button to let go before we send the mouse focus update.
  773. } else {
  774. SDL_SetMouseFocus(isenter ? window_data->window : NULL);
  775. }
  776. }
  777. static void Emscripten_HandlePenEnter(SDL_WindowData *window_data, const Emscripten_PointerEvent *event)
  778. {
  779. SDL_assert(event->pointer_type == PTRTYPE_PEN);
  780. // event->pointerid is one continuous interaction; it doesn't necessarily track a specific tool over time, like the same finger's ID changed on each new touch event.
  781. // as such, we only expose a single pen, and when the touch ends, we say it lost proximity instead of the calling SDL_RemovePenDevice().
  782. SDL_PenID pen = SDL_FindPenByHandle((void *) (size_t) 1); // something > 0 for the single pen handle.
  783. if (pen) {
  784. SDL_SendPenProximity(0, pen, window_data->window, true, true);
  785. } else {
  786. // Web browsers offer almost none of this information as specifics, but can without warning offer any of these specific things.
  787. SDL_PenInfo peninfo;
  788. SDL_zero(peninfo);
  789. peninfo.capabilities = SDL_PEN_CAPABILITY_PRESSURE | SDL_PEN_CAPABILITY_ROTATION | SDL_PEN_CAPABILITY_XTILT | SDL_PEN_CAPABILITY_YTILT | SDL_PEN_CAPABILITY_TANGENTIAL_PRESSURE | SDL_PEN_CAPABILITY_ERASER;
  790. peninfo.max_tilt = 90.0f;
  791. peninfo.num_buttons = 2;
  792. peninfo.subtype = SDL_PEN_TYPE_PEN;
  793. SDL_AddPenDevice(0, NULL, window_data->window, &peninfo, (void *) (size_t) 1, true);
  794. }
  795. Emscripten_UpdatePenFromEvent(window_data, event);
  796. }
  797. EMSCRIPTEN_KEEPALIVE void Emscripten_HandlePointerEnter(SDL_WindowData *window_data, const Emscripten_PointerEvent *event)
  798. {
  799. SDL_assert(event != NULL);
  800. if (event->pointer_type == PTRTYPE_MOUSE) {
  801. Emscripten_HandleMouseFocus(window_data, event, true);
  802. } else if (event->pointer_type == PTRTYPE_PEN) {
  803. Emscripten_HandlePenEnter(window_data, event);
  804. } else if (event->pointer_type == PTRTYPE_TOUCH) {
  805. // do nothing.
  806. } else {
  807. SDL_assert(!"Unexpected pointer event type");
  808. }
  809. }
  810. static void Emscripten_HandlePenLeave(SDL_WindowData *window_data, const Emscripten_PointerEvent *event)
  811. {
  812. const SDL_PenID pen = SDL_FindPenByHandle((void *) (size_t) 1); // something > 0 for the single pen handle.
  813. if (pen) {
  814. Emscripten_UpdatePointerFromEvent(window_data, event); // last data updates?
  815. SDL_SendPenProximity(0, pen, window_data->window, false, false);
  816. }
  817. }
  818. static void Emscripten_HandleTouchCancel(SDL_WindowData *window_data, const Emscripten_PointerEvent *event)
  819. {
  820. SDL_assert(event->pointer_type == PTRTYPE_TOUCH);
  821. const SDL_TouchID deviceId = 1;
  822. if (SDL_AddTouch(deviceId, SDL_TOUCH_DEVICE_DIRECT, "") < 0) {
  823. return;
  824. }
  825. double client_w, client_h;
  826. emscripten_get_element_css_size(window_data->canvas_id, &client_w, &client_h);
  827. const SDL_FingerID id = event->pointerid + 1;
  828. float x, y;
  829. if (client_w <= 1) {
  830. x = 0.5f;
  831. } else {
  832. x = event->targetX / (client_w - 1);
  833. }
  834. if (client_h <= 1) {
  835. y = 0.5f;
  836. } else {
  837. y = event->targetY / (client_h - 1);
  838. }
  839. SDL_SendTouch(0, deviceId, id, window_data->window, SDL_EVENT_FINGER_CANCELED, x, y, 1.0f);
  840. }
  841. EMSCRIPTEN_KEEPALIVE void Emscripten_HandlePointerLeave(SDL_WindowData *window_data, const Emscripten_PointerEvent *event)
  842. {
  843. SDL_assert(event != NULL);
  844. if (event->pointer_type == PTRTYPE_MOUSE) {
  845. Emscripten_HandleMouseFocus(window_data, event, false);
  846. } else if (event->pointer_type == PTRTYPE_PEN) {
  847. Emscripten_HandlePenLeave(window_data, event);
  848. } else if (event->pointer_type == PTRTYPE_TOUCH) {
  849. Emscripten_HandleTouchCancel(window_data, event);
  850. } else {
  851. SDL_assert(!"Unexpected pointer event type");
  852. }
  853. }
  854. EMSCRIPTEN_KEEPALIVE void Emscripten_HandlePointerGeneric(SDL_WindowData *window_data, const Emscripten_PointerEvent *event)
  855. {
  856. SDL_assert(event != NULL);
  857. Emscripten_UpdatePointerFromEvent(window_data, event);
  858. }
  859. static void Emscripten_prep_pointer_event_callbacks(void)
  860. {
  861. MAIN_THREAD_EM_ASM({
  862. var SDL3 = Module['SDL3'];
  863. if (SDL3.makePointerEventCStruct === undefined) {
  864. SDL3.makePointerEventCStruct = function(left, top, event) {
  865. var ptrtype = 0;
  866. if (event.pointerType == "mouse") {
  867. ptrtype = 1;
  868. } else if (event.pointerType == "touch") {
  869. ptrtype = 2;
  870. } else if (event.pointerType == "pen") {
  871. ptrtype = 3;
  872. } else {
  873. return 0;
  874. }
  875. var ptr = _SDL_malloc($0);
  876. if (ptr != 0) {
  877. var idx = SDL3.CPtrToHeap32Index(ptr);
  878. HEAP32[idx++] = ptrtype;
  879. HEAP32[idx++] = event.pointerId;
  880. HEAP32[idx++] = (typeof(event.button) !== "undefined") ? event.button : -1;
  881. HEAP32[idx++] = event.buttons;
  882. HEAP32[idx++] = (event.type == "pointerdown") ? 1 : 0;
  883. HEAPF32[idx++] = event.movementX;
  884. HEAPF32[idx++] = event.movementY;
  885. HEAPF32[idx++] = event.clientX - left;
  886. HEAPF32[idx++] = event.clientY - top;
  887. if (ptrtype == 3) {
  888. HEAPF32[idx++] = event.pressure;
  889. HEAPF32[idx++] = event.tangentialPressure;
  890. HEAPF32[idx++] = event.tiltX;
  891. HEAPF32[idx++] = event.tiltY;
  892. HEAPF32[idx++] = event.twist;
  893. }
  894. }
  895. return ptr;
  896. };
  897. }
  898. }, sizeof (Emscripten_PointerEvent));
  899. }
  900. static void Emscripten_set_pointer_event_callbacks(SDL_WindowData *data)
  901. {
  902. Emscripten_prep_pointer_event_callbacks();
  903. MAIN_THREAD_EM_ASM({
  904. var target = document.querySelector(UTF8ToString($1));
  905. if (target) {
  906. var SDL3 = Module['SDL3'];
  907. var data = $0;
  908. target.sdlEventHandlerPointerEnter = function(event) {
  909. var rect = target.getBoundingClientRect();
  910. var d = SDL3.makePointerEventCStruct(rect.left, rect.top, event);
  911. if (d != 0)
  912. {
  913. _Emscripten_HandlePointerEnter(SDL3.JSVarToCPtr(data), d);
  914. _SDL_free(d);
  915. }
  916. };
  917. target.sdlEventHandlerPointerLeave = function(event) {
  918. var rect = target.getBoundingClientRect();
  919. var d = SDL3.makePointerEventCStruct(rect.left, rect.top, event);
  920. if (d != 0)
  921. {
  922. _Emscripten_HandlePointerLeave(SDL3.JSVarToCPtr(data), d);
  923. _SDL_free(d);
  924. }
  925. };
  926. target.sdlEventHandlerPointerGeneric = function(event) {
  927. var rect = target.getBoundingClientRect();
  928. var d = SDL3.makePointerEventCStruct(rect.left, rect.top, event);
  929. if (d != 0)
  930. {
  931. _Emscripten_HandlePointerGeneric(SDL3.JSVarToCPtr(data), d);
  932. _SDL_free(d);
  933. }
  934. };
  935. target.style.touchAction = "none"; // or mobile devices will scroll as your touch moves across the element.
  936. target.addEventListener("pointerenter", target.sdlEventHandlerPointerEnter);
  937. target.addEventListener("pointerleave", target.sdlEventHandlerPointerLeave);
  938. target.addEventListener("pointercancel", target.sdlEventHandlerPointerLeave);
  939. target.addEventListener("pointerdown", target.sdlEventHandlerPointerGeneric);
  940. target.addEventListener("pointermove", target.sdlEventHandlerPointerGeneric);
  941. target.addEventListener("pointerup", target.sdlEventHandlerPointerGeneric);
  942. }
  943. }, data, data->canvas_id);
  944. }
  945. static void Emscripten_unset_pointer_event_callbacks(SDL_WindowData *data)
  946. {
  947. MAIN_THREAD_EM_ASM({
  948. var target = document.querySelector(UTF8ToString($0));
  949. if (target) {
  950. target.removeEventListener("pointerenter", target.sdlEventHandlerPointerEnter);
  951. target.removeEventListener("pointerleave", target.sdlEventHandlerPointerLeave);
  952. target.removeEventListener("pointercancel", target.sdlEventHandlerPointerLeave);
  953. target.removeEventListener("pointerdown", target.sdlEventHandlerPointerGeneric);
  954. target.removeEventListener("pointermove", target.sdlEventHandlerPointerGeneric);
  955. target.removeEventListener("pointerup", target.sdlEventHandlerPointerGeneric);
  956. target.style.touchAction = ""; // let mobile devices scroll again as your touch moves across the element.
  957. target.sdlEventHandlerPointerEnter = undefined;
  958. target.sdlEventHandlerPointerLeave = undefined;
  959. target.sdlEventHandlerPointerGeneric = undefined;
  960. }
  961. }, data->canvas_id);
  962. }
  963. EMSCRIPTEN_KEEPALIVE void Emscripten_HandleMouseButtonUpGlobal(SDL_VideoDevice *device, const Emscripten_PointerEvent *event)
  964. {
  965. SDL_assert(device != NULL);
  966. SDL_assert(event != NULL);
  967. if (event->pointer_type == PTRTYPE_MOUSE) {
  968. for (SDL_Window *window = device->windows; window; window = window->next) {
  969. Emscripten_HandleMouseButton(window->internal, event);
  970. }
  971. }
  972. }
  973. static void Emscripten_set_global_mouseup_callback(SDL_VideoDevice *device)
  974. {
  975. Emscripten_prep_pointer_event_callbacks();
  976. MAIN_THREAD_EM_ASM({
  977. var target = document;
  978. if (target) {
  979. target.sdlEventHandlerMouseButtonUpGlobal = function(event) {
  980. var SDL3 = Module['SDL3'];
  981. var d = SDL3.makePointerEventCStruct(0, 0, event);
  982. if (d != 0)
  983. {
  984. _Emscripten_HandleMouseButtonUpGlobal(SDL3.JSVarToCPtr($0), d);
  985. _SDL_free(d);
  986. }
  987. };
  988. target.addEventListener("pointerup", target.sdlEventHandlerMouseButtonUpGlobal);
  989. }
  990. }, device);
  991. }
  992. static void Emscripten_unset_global_mouseup_callback(SDL_VideoDevice *device)
  993. {
  994. MAIN_THREAD_EM_ASM({
  995. var target = document;
  996. if (target) {
  997. target.removeEventListener("pointerup", target.sdlEventHandlerMouseButtonUpGlobal);
  998. target.sdlEventHandlerMouseButtonUpGlobal = undefined;
  999. }
  1000. });
  1001. }
  1002. // IF YOU CHANGE THIS STRUCTURE, YOU NEED TO UPDATE THE JAVASCRIPT THAT FILLS IT IN: makeDropEventCStruct, below.
  1003. typedef struct Emscripten_DropEvent
  1004. {
  1005. int x;
  1006. int y;
  1007. } Emscripten_DropEvent;
  1008. EMSCRIPTEN_KEEPALIVE void Emscripten_SendDragEvent(SDL_WindowData *window_data, const Emscripten_DropEvent *event)
  1009. {
  1010. SDL_SendDropPosition(window_data->window, event->x, event->y);
  1011. }
  1012. EMSCRIPTEN_KEEPALIVE void Emscripten_SendDragCompleteEvent(SDL_WindowData *window_data)
  1013. {
  1014. SDL_SendDropComplete(window_data->window);
  1015. }
  1016. EMSCRIPTEN_KEEPALIVE void Emscripten_SendDragTextEvent(SDL_WindowData *window_data, char *text)
  1017. {
  1018. SDL_SendDropText(window_data->window, text);
  1019. }
  1020. EMSCRIPTEN_KEEPALIVE void Emscripten_SendDragFileEvent(SDL_WindowData *window_data, char *filename)
  1021. {
  1022. SDL_SendDropFile(window_data->window, NULL, filename);
  1023. }
  1024. EM_JS_DEPS(dragndrop, "$writeArrayToMemory");
  1025. static void Emscripten_set_drag_event_callbacks(SDL_WindowData *data)
  1026. {
  1027. MAIN_THREAD_EM_ASM({
  1028. var target = document.querySelector(UTF8ToString($1));
  1029. if (target) {
  1030. var data = $0;
  1031. var SDL3 = Module['SDL3'];
  1032. var makeDropEventCStruct = function(event) {
  1033. var ptr = 0;
  1034. ptr = _SDL_malloc($2);
  1035. if (ptr != 0) {
  1036. var idx = ptr >> 2;
  1037. var rect = target.getBoundingClientRect();
  1038. HEAP32[idx++] = event.clientX - rect.left;
  1039. HEAP32[idx++] = event.clientY - rect.top;
  1040. }
  1041. return ptr;
  1042. };
  1043. SDL3.eventHandlerDropDragover = function(event) {
  1044. event.preventDefault();
  1045. var d = makeDropEventCStruct(event); if (d != 0) { _Emscripten_SendDragEvent(data, d); _SDL_free(d); }
  1046. };
  1047. target.addEventListener("dragover", SDL3.eventHandlerDropDragover);
  1048. SDL3.drop_count = 0;
  1049. // FS.* functions throw exceptions when there are errors (such as the temp dir already existing),
  1050. // but we ignore all of these in a catch handler; you just won't get the drop event if there's a problem.
  1051. try { FS.mkdir("/tmp/filedrop"); } catch (e) {}
  1052. SDL3.eventHandlerDropDrop = function(event) {
  1053. event.preventDefault();
  1054. if (event.dataTransfer.types.includes("text/plain")) {
  1055. let plain_text = stringToNewUTF8(event.dataTransfer.getData("text/plain"));
  1056. _Emscripten_SendDragTextEvent(data, plain_text);
  1057. _Emscripten_force_free(plain_text);
  1058. } else if (event.dataTransfer.types.includes("Files")) {
  1059. let files_read = 0;
  1060. const files_to_read = event.dataTransfer.files.length;
  1061. for (let i = 0; i < files_to_read; i++) {
  1062. const file = event.dataTransfer.files.item(i);
  1063. const file_reader = new FileReader();
  1064. file_reader.readAsArrayBuffer(file);
  1065. file_reader.onload = function(event) {
  1066. const fs_dropdir = `/tmp/filedrop/${SDL3.drop_count}`;
  1067. SDL3.drop_count += 1;
  1068. const fs_filepath = `${fs_dropdir}/${file.name}`;
  1069. const c_fs_filepath = stringToNewUTF8(fs_filepath);
  1070. const contents_array8 = new Uint8Array(event.target.result);
  1071. try {
  1072. FS.mkdir(fs_dropdir);
  1073. var stream = FS.open(fs_filepath, "w");
  1074. FS.write(stream, contents_array8, 0, contents_array8.length, 0);
  1075. FS.close(stream);
  1076. _Emscripten_SendDragFileEvent(data, c_fs_filepath);
  1077. } catch (e) {
  1078. // if this threw an exception at any point, we skip this drop event. Sorry!
  1079. }
  1080. _Emscripten_force_free(c_fs_filepath);
  1081. onFileRead();
  1082. };
  1083. file_reader.onerror = function(event) {
  1084. // Handle when error occurs to ensure that the drag event can still complete
  1085. onFileRead();
  1086. };
  1087. }
  1088. function onFileRead() {
  1089. ++files_read;
  1090. if (files_read === files_to_read) {
  1091. _Emscripten_SendDragCompleteEvent(data);
  1092. }
  1093. }
  1094. }
  1095. _Emscripten_SendDragCompleteEvent(data);
  1096. };
  1097. target.addEventListener("drop", SDL3.eventHandlerDropDrop);
  1098. SDL3.eventHandlerDropDragend = function(event) {
  1099. event.preventDefault();
  1100. _Emscripten_SendDragCompleteEvent(data);
  1101. };
  1102. target.addEventListener("dragend", SDL3.eventHandlerDropDragend);
  1103. target.addEventListener("dragleave", SDL3.eventHandlerDropDragend);
  1104. }
  1105. }, data, data->canvas_id, sizeof (Emscripten_DropEvent));
  1106. }
  1107. static void Emscripten_unset_drag_event_callbacks(SDL_WindowData *data)
  1108. {
  1109. MAIN_THREAD_EM_ASM({
  1110. var target = document.querySelector(UTF8ToString($0));
  1111. if (target) {
  1112. var SDL3 = Module['SDL3'];
  1113. target.removeEventListener("dragleave", SDL3.eventHandlerDropDragend);
  1114. target.removeEventListener("dragend", SDL3.eventHandlerDropDragend);
  1115. target.removeEventListener("drop", SDL3.eventHandlerDropDrop);
  1116. SDL3.drop_count = undefined;
  1117. function recursive_remove(dirpath) {
  1118. FS.readdir(dirpath).forEach((filename) => {
  1119. const p = `${dirpath}/${filename}`;
  1120. const p_s = FS.stat(p);
  1121. if (FS.isFile(p_s.mode)) {
  1122. FS.unlink(p);
  1123. } else if (FS.isDir(p)) {
  1124. recursive_remove(p);
  1125. }
  1126. });
  1127. FS.rmdir(dirpath);
  1128. }("/tmp/filedrop");
  1129. FS.rmdir("/tmp/filedrop");
  1130. target.removeEventListener("dragover", SDL3.eventHandlerDropDragover);
  1131. SDL3.eventHandlerDropDragover = undefined;
  1132. SDL3.eventHandlerDropDrop = undefined;
  1133. SDL3.eventHandlerDropDragend = undefined;
  1134. }
  1135. }, data->canvas_id);
  1136. }
  1137. static const char *Emscripten_GetKeyboardTargetElement(const char *target)
  1138. {
  1139. if (SDL_strcmp(target, "#none") == 0) {
  1140. return NULL;
  1141. } else if (SDL_strcmp(target, "#window") == 0) {
  1142. return EMSCRIPTEN_EVENT_TARGET_WINDOW;
  1143. } else if (SDL_strcmp(target, "#document") == 0) {
  1144. return EMSCRIPTEN_EVENT_TARGET_DOCUMENT;
  1145. } else if (SDL_strcmp(target, "#screen") == 0) {
  1146. return EMSCRIPTEN_EVENT_TARGET_SCREEN;
  1147. }
  1148. return target;
  1149. }
  1150. void Emscripten_RegisterGlobalEventHandlers(SDL_VideoDevice *device)
  1151. {
  1152. Emscripten_set_global_mouseup_callback(device);
  1153. emscripten_set_focus_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, device, 0, Emscripten_HandleFocus);
  1154. emscripten_set_blur_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, device, 0, Emscripten_HandleFocus);
  1155. emscripten_set_pointerlockchange_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, device, 0, Emscripten_HandlePointerLockChangeGlobal);
  1156. emscripten_set_fullscreenchange_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, device, 0, Emscripten_HandleFullscreenChangeGlobal);
  1157. emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, device, 0, Emscripten_HandleResizeGlobal);
  1158. }
  1159. void Emscripten_UnregisterGlobalEventHandlers(SDL_VideoDevice *device)
  1160. {
  1161. Emscripten_unset_global_mouseup_callback(device);
  1162. emscripten_set_focus_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 0, NULL);
  1163. emscripten_set_blur_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 0, NULL);
  1164. emscripten_set_pointerlockchange_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, NULL, 0, NULL);
  1165. emscripten_set_fullscreenchange_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, NULL, 0, NULL);
  1166. emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 0, NULL);
  1167. }
  1168. EMSCRIPTEN_KEEPALIVE void Emscripten_HandleLockKeysCheck(SDL_WindowData *window_data, EM_BOOL capslock, EM_BOOL numlock, EM_BOOL scrolllock)
  1169. {
  1170. const SDL_Keymod new_mods = (capslock ? SDL_KMOD_CAPS : 0) | (numlock ? SDL_KMOD_NUM : 0) | (scrolllock ? SDL_KMOD_SCROLL : 0);
  1171. SDL_Keymod modstate = SDL_GetModState();
  1172. if ((modstate & (SDL_KMOD_CAPS|SDL_KMOD_NUM|SDL_KMOD_SCROLL)) != new_mods) {
  1173. modstate &= ~(SDL_KMOD_CAPS|SDL_KMOD_NUM|SDL_KMOD_SCROLL);
  1174. modstate |= new_mods;
  1175. SDL_SetModState(modstate);
  1176. }
  1177. }
  1178. void Emscripten_RegisterEventHandlers(SDL_WindowData *data)
  1179. {
  1180. const char *keyElement;
  1181. // There is only one window and that window is the canvas
  1182. emscripten_set_wheel_callback(data->canvas_id, data, 0, Emscripten_HandleWheel);
  1183. emscripten_set_orientationchange_callback(data, 0, Emscripten_HandleOrientationChange);
  1184. keyElement = Emscripten_GetKeyboardTargetElement(data->keyboard_element);
  1185. if (keyElement) {
  1186. // Emscripten's HTML5 helpers do not deduplicate `addEventListener` calls:
  1187. // see `registerOrRemoveHandler` in `emscripten/src/lib/libhtml5.js`.
  1188. //
  1189. // If a previous SDL window already registered keyboard handlers on the same
  1190. // target, the new registration would *stack* a second listener, causing every
  1191. // browser keydown to fire `Emscripten_HandleKey` twice.
  1192. //
  1193. // The duplicate calls then produce two `SDL_EVENT_KEY_DOWN` events per physical
  1194. // keypress (the second one with `repeat=true`, due to the keystate-based repeat
  1195. // detection in `SDL_SendKeyboardKeyInternal`).
  1196. //
  1197. // We must clear any prior handler on this target before installing ours:
  1198. emscripten_set_keydown_callback(keyElement, NULL, 0, NULL);
  1199. emscripten_set_keyup_callback(keyElement, NULL, 0, NULL);
  1200. emscripten_set_keypress_callback(keyElement, NULL, 0, NULL);
  1201. MAIN_THREAD_EM_ASM_INT({
  1202. var data = $0;
  1203. // our keymod state can get confused in various ways (changed capslock when browser didn't have focus, etc), and you can't query the current
  1204. // state from the DOM, outside of a keyboard event, so catch keypresses globally and reset mod state if it's unexpectedly wrong. Best we can do.
  1205. // Note that this thing _only_ adjusts the lock keys if necessary; the real SDL keypress handling happens elsewhere.
  1206. // Remove any prior listener first -- `addEventListener` does not deduplicate either.
  1207. if (document.sdlEventHandlerLockKeysCheck) {
  1208. document.removeEventListener("keydown", document.sdlEventHandlerLockKeysCheck);
  1209. }
  1210. document.sdlEventHandlerLockKeysCheck = function(event) {
  1211. // don't try to adjust the state on the actual lock key presses; the normal key handler will catch that and adjust.
  1212. if ((event.key != "CapsLock") && (event.key != "NumLock") && (event.key != "ScrollLock"))
  1213. {
  1214. _Emscripten_HandleLockKeysCheck(Module['SDL3'].JSVarToCPtr(data), event.getModifierState("CapsLock"), event.getModifierState("NumLock"), event.getModifierState("ScrollLock"));
  1215. }
  1216. };
  1217. document.addEventListener("keydown", document.sdlEventHandlerLockKeysCheck);
  1218. }, data);
  1219. emscripten_set_keydown_callback(keyElement, data, 0, Emscripten_HandleKey);
  1220. emscripten_set_keyup_callback(keyElement, data, 0, Emscripten_HandleKey);
  1221. emscripten_set_keypress_callback(keyElement, data, 0, Emscripten_HandleKeyPress);
  1222. }
  1223. emscripten_set_visibilitychange_callback(data, 0, Emscripten_HandleVisibilityChange);
  1224. emscripten_set_beforeunload_callback(data, Emscripten_HandleBeforeUnload);
  1225. // !!! FIXME: currently Emscripten doesn't have a Pointer Events functions like emscripten_set_*_callback, but we should use those when they do:
  1226. // !!! FIXME: https://github.com/emscripten-core/emscripten/issues/7278#issuecomment-2280024621
  1227. Emscripten_set_pointer_event_callbacks(data);
  1228. // !!! FIXME: currently Emscripten doesn't have a Drop Events functions like emscripten_set_*_callback, but we should use those when they do:
  1229. Emscripten_set_drag_event_callbacks(data);
  1230. }
  1231. void Emscripten_UnregisterEventHandlers(SDL_WindowData *data)
  1232. {
  1233. const char *keyElement;
  1234. // !!! FIXME: currently Emscripten doesn't have a Drop Events functions like emscripten_set_*_callback, but we should use those when they do:
  1235. Emscripten_unset_drag_event_callbacks(data);
  1236. // !!! FIXME: currently Emscripten doesn't have a Pointer Events functions like emscripten_set_*_callback, but we should use those when they do:
  1237. // !!! FIXME: https://github.com/emscripten-core/emscripten/issues/7278#issuecomment-2280024621
  1238. Emscripten_unset_pointer_event_callbacks(data);
  1239. // only works due to having one window
  1240. emscripten_set_wheel_callback(data->canvas_id, NULL, 0, NULL);
  1241. emscripten_set_orientationchange_callback(NULL, 0, NULL);
  1242. keyElement = Emscripten_GetKeyboardTargetElement(data->keyboard_element);
  1243. if (keyElement) {
  1244. emscripten_set_keydown_callback(keyElement, NULL, 0, NULL);
  1245. emscripten_set_keyup_callback(keyElement, NULL, 0, NULL);
  1246. emscripten_set_keypress_callback(keyElement, NULL, 0, NULL);
  1247. MAIN_THREAD_EM_ASM_INT({
  1248. document.removeEventListener("keydown", document.sdlEventHandlerLockKeysCheck);
  1249. });
  1250. }
  1251. emscripten_set_visibilitychange_callback(NULL, 0, NULL);
  1252. emscripten_set_beforeunload_callback(NULL, NULL);
  1253. }
  1254. #endif // SDL_VIDEO_DRIVER_EMSCRIPTEN