SDL_emscriptenmouse.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/emscripten.h>
  21. #include <emscripten/html5.h>
  22. #include <emscripten/threading.h>
  23. #include "SDL_emscriptenmouse.h"
  24. #include "SDL_emscriptenvideo.h"
  25. #include "../SDL_video_c.h"
  26. #include "../../events/SDL_mouse_c.h"
  27. // older Emscriptens don't have this, but we need to for wasm64 compatibility.
  28. #ifndef MAIN_THREAD_EM_ASM_PTR
  29. #ifdef __wasm64__
  30. #error You need to upgrade your Emscripten compiler to support wasm64
  31. #else
  32. #define MAIN_THREAD_EM_ASM_PTR MAIN_THREAD_EM_ASM_INT
  33. #endif
  34. #endif
  35. static SDL_Cursor *Emscripten_CreateCursorFromString(const char *cursor_str, bool is_custom)
  36. {
  37. SDL_CursorData *curdata;
  38. SDL_Cursor *cursor = SDL_calloc(1, sizeof(SDL_Cursor));
  39. if (cursor) {
  40. curdata = (SDL_CursorData *)SDL_calloc(1, sizeof(*curdata));
  41. if (!curdata) {
  42. SDL_free(cursor);
  43. return NULL;
  44. }
  45. curdata->system_cursor = cursor_str;
  46. curdata->is_custom = is_custom;
  47. cursor->internal = curdata;
  48. }
  49. return cursor;
  50. }
  51. static SDL_Cursor *Emscripten_CreateDefaultCursor(void)
  52. {
  53. SDL_SystemCursor id = SDL_GetDefaultSystemCursor();
  54. const char *cursor_name = SDL_GetCSSCursorName(id, NULL);
  55. return Emscripten_CreateCursorFromString(cursor_name, false);
  56. }
  57. EM_JS_DEPS(sdlmouse, "$stringToUTF8,$UTF8ToString");
  58. static SDL_Cursor *Emscripten_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y)
  59. {
  60. const char *cursor_url = NULL;
  61. SDL_Surface *conv_surf;
  62. conv_surf = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA32);
  63. if (!conv_surf) {
  64. return NULL;
  65. }
  66. /* *INDENT-OFF* */ // clang-format off
  67. cursor_url = (const char *)MAIN_THREAD_EM_ASM_PTR({
  68. var w = $0;
  69. var h = $1;
  70. var hot_x = $2;
  71. var hot_y = $3;
  72. var pixels = $4;
  73. var canvas = document.createElement("canvas");
  74. canvas.width = w;
  75. canvas.height = h;
  76. var ctx = canvas.getContext("2d");
  77. var image = ctx.createImageData(w, h);
  78. var data = image.data;
  79. var src = pixels / 4;
  80. var data32 = new Int32Array(data.buffer);
  81. data32.set(HEAP32.subarray(src, src + data32.length));
  82. ctx.putImageData(image, 0, 0);
  83. var url = hot_x === 0 && hot_y === 0
  84. ? "url(" + canvas.toDataURL() + "), auto"
  85. : "url(" + canvas.toDataURL() + ") " + hot_x + " " + hot_y + ", auto";
  86. var urlBuf = _SDL_malloc(url.length + 1);
  87. stringToUTF8(url, urlBuf, url.length + 1);
  88. return urlBuf;
  89. }, surface->w, surface->h, hot_x, hot_y, conv_surf->pixels);
  90. /* *INDENT-ON* */ // clang-format on
  91. SDL_DestroySurface(conv_surf);
  92. return Emscripten_CreateCursorFromString(cursor_url, true);
  93. }
  94. static SDL_Cursor *Emscripten_CreateSystemCursor(SDL_SystemCursor id)
  95. {
  96. const char *cursor_name = SDL_GetCSSCursorName(id, NULL);
  97. return Emscripten_CreateCursorFromString(cursor_name, false);
  98. }
  99. static void Emscripten_FreeCursor(SDL_Cursor *cursor)
  100. {
  101. SDL_CursorData *curdata;
  102. if (cursor) {
  103. curdata = cursor->internal;
  104. if (curdata) {
  105. if (curdata->is_custom) {
  106. SDL_free((char *)curdata->system_cursor);
  107. }
  108. SDL_free(cursor->internal);
  109. }
  110. SDL_free(cursor);
  111. }
  112. }
  113. static bool Emscripten_ShowCursor(SDL_Cursor *cursor)
  114. {
  115. SDL_CursorData *curdata;
  116. if (SDL_GetMouseFocus() != NULL) {
  117. if (cursor && cursor->internal) {
  118. curdata = cursor->internal;
  119. if (curdata->system_cursor) {
  120. /* *INDENT-OFF* */ // clang-format off
  121. MAIN_THREAD_EM_ASM({
  122. if (Module['canvas']) {
  123. Module['canvas'].style['cursor'] = UTF8ToString($0);
  124. }
  125. }, curdata->system_cursor);
  126. /* *INDENT-ON* */ // clang-format on
  127. }
  128. } else {
  129. /* *INDENT-OFF* */ // clang-format off
  130. MAIN_THREAD_EM_ASM(
  131. if (Module['canvas']) {
  132. Module['canvas'].style['cursor'] = 'none';
  133. }
  134. );
  135. /* *INDENT-ON* */ // clang-format on
  136. }
  137. }
  138. return true;
  139. }
  140. static bool Emscripten_SetRelativeMouseMode(bool enabled)
  141. {
  142. SDL_Window *window;
  143. SDL_WindowData *window_data;
  144. // TODO: pointer lock isn't actually enabled yet
  145. if (enabled) {
  146. window = SDL_GetMouseFocus();
  147. if (!window) {
  148. return false;
  149. }
  150. window_data = window->internal;
  151. if (emscripten_request_pointerlock(window_data->canvas_id, 1) >= EMSCRIPTEN_RESULT_SUCCESS) {
  152. return true;
  153. }
  154. } else {
  155. if (emscripten_exit_pointerlock() >= EMSCRIPTEN_RESULT_SUCCESS) {
  156. return true;
  157. }
  158. }
  159. return false;
  160. }
  161. static SDL_MouseButtonFlags Emscripten_GetGlobalMouseState(float *x, float *y)
  162. {
  163. *x = MAIN_THREAD_EM_ASM_DOUBLE({
  164. return Module['SDL3']['mouse_x'];
  165. });
  166. *y = MAIN_THREAD_EM_ASM_DOUBLE({
  167. return Module['SDL3']['mouse_y'];
  168. });
  169. SDL_MouseButtonFlags flags = 0;
  170. for (int i = 0; i < 5; ++i) {
  171. const bool button_down = MAIN_THREAD_EM_ASM_INT({
  172. return Module['SDL3']['mouse_buttons'][$0];
  173. }, i);
  174. if (button_down) {
  175. flags |= 1 << i;
  176. }
  177. }
  178. return flags;
  179. }
  180. void Emscripten_InitMouse(void)
  181. {
  182. SDL_Mouse *mouse = SDL_GetMouse();
  183. mouse->CreateCursor = Emscripten_CreateCursor;
  184. mouse->ShowCursor = Emscripten_ShowCursor;
  185. mouse->FreeCursor = Emscripten_FreeCursor;
  186. mouse->CreateSystemCursor = Emscripten_CreateSystemCursor;
  187. mouse->SetRelativeMouseMode = Emscripten_SetRelativeMouseMode;
  188. // Add event listeners to track mouse events on the document
  189. MAIN_THREAD_EM_ASM({
  190. var SDL3 = Module['SDL3'];
  191. SDL3['mouse_x'] = 0;
  192. SDL3['mouse_y'] = 0;
  193. /*
  194. Based on https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
  195. Possible value for button in the event object is [0, 5)
  196. NOTE: Some browsers do not allow handling the forwards and backwards buttons
  197. */
  198. SDL3['mouse_buttons'] = [];
  199. for (var i = 0; i < 5; ++i) {
  200. SDL3['mouse_buttons'][i] = false;
  201. }
  202. document.addEventListener('mousemove', function(e) {
  203. // Reacquire from object in case it changed for some reason
  204. var SDL3 = Module['SDL3'];
  205. SDL3['mouse_x'] = e.clientX;
  206. SDL3['mouse_y'] = e.clientY;
  207. });
  208. document.addEventListener('mousedown', function(e) {
  209. // Reacquire from object in case it changed for some reason
  210. var SDL3 = Module['SDL3'];
  211. if (0 <= e.button && e.button < SDL3['mouse_buttons'].length) {
  212. SDL3['mouse_buttons'][e.button] = true;
  213. }
  214. });
  215. document.addEventListener('mouseup', function(e) {
  216. // Reacquire from object in case it changed for some reason
  217. var SDL3 = Module['SDL3'];
  218. if (0 <= e.button && e.button < SDL3['mouse_buttons'].length) {
  219. SDL3['mouse_buttons'][e.button] = false;
  220. }
  221. });
  222. });
  223. mouse->GetGlobalMouseState = Emscripten_GetGlobalMouseState;
  224. SDL_SetDefaultCursor(Emscripten_CreateDefaultCursor());
  225. }
  226. void Emscripten_QuitMouse(void)
  227. {
  228. }
  229. #endif // SDL_VIDEO_DRIVER_EMSCRIPTEN