SDL_emscriptenmouse.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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, SDL_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. return Emscripten_CreateCursorFromString("default", SDL_FALSE);
  54. }
  55. EM_JS_DEPS(sdlmouse, "$stringToUTF8,$UTF8ToString");
  56. static SDL_Cursor *Emscripten_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y)
  57. {
  58. const char *cursor_url = NULL;
  59. SDL_Surface *conv_surf;
  60. conv_surf = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_ABGR8888);
  61. if (!conv_surf) {
  62. return NULL;
  63. }
  64. /* *INDENT-OFF* */ /* clang-format off */
  65. cursor_url = (const char *)MAIN_THREAD_EM_ASM_PTR({
  66. var w = $0;
  67. var h = $1;
  68. var hot_x = $2;
  69. var hot_y = $3;
  70. var pixels = $4;
  71. var canvas = document.createElement("canvas");
  72. canvas.width = w;
  73. canvas.height = h;
  74. var ctx = canvas.getContext("2d");
  75. var image = ctx.createImageData(w, h);
  76. var data = image.data;
  77. var src = pixels >> 2;
  78. var data32 = new Int32Array(data.buffer);
  79. data32.set(HEAP32.subarray(src, src + data32.length));
  80. ctx.putImageData(image, 0, 0);
  81. var url = hot_x === 0 && hot_y === 0
  82. ? "url(" + canvas.toDataURL() + "), auto"
  83. : "url(" + canvas.toDataURL() + ") " + hot_x + " " + hot_y + ", auto";
  84. var urlBuf = _malloc(url.length + 1);
  85. stringToUTF8(url, urlBuf, url.length + 1);
  86. return urlBuf;
  87. }, surface->w, surface->h, hot_x, hot_y, conv_surf->pixels);
  88. /* *INDENT-ON* */ /* clang-format on */
  89. SDL_DestroySurface(conv_surf);
  90. return Emscripten_CreateCursorFromString(cursor_url, SDL_TRUE);
  91. }
  92. static SDL_Cursor *Emscripten_CreateSystemCursor(SDL_SystemCursor id)
  93. {
  94. const char *cursor_name = SDL_GetCSSCursorName(id, NULL);
  95. return Emscripten_CreateCursorFromString(cursor_name, SDL_FALSE);
  96. }
  97. static void Emscripten_FreeCursor(SDL_Cursor *cursor)
  98. {
  99. SDL_CursorData *curdata;
  100. if (cursor) {
  101. curdata = cursor->internal;
  102. if (curdata) {
  103. if (curdata->is_custom) {
  104. SDL_free((char *)curdata->system_cursor);
  105. }
  106. SDL_free(cursor->internal);
  107. }
  108. SDL_free(cursor);
  109. }
  110. }
  111. static int Emscripten_ShowCursor(SDL_Cursor *cursor)
  112. {
  113. SDL_CursorData *curdata;
  114. if (SDL_GetMouseFocus() != NULL) {
  115. if (cursor && cursor->internal) {
  116. curdata = cursor->internal;
  117. if (curdata->system_cursor) {
  118. /* *INDENT-OFF* */ /* clang-format off */
  119. MAIN_THREAD_EM_ASM({
  120. if (Module['canvas']) {
  121. Module['canvas'].style['cursor'] = UTF8ToString($0);
  122. }
  123. }, curdata->system_cursor);
  124. /* *INDENT-ON* */ /* clang-format on */
  125. }
  126. } else {
  127. /* *INDENT-OFF* */ /* clang-format off */
  128. MAIN_THREAD_EM_ASM(
  129. if (Module['canvas']) {
  130. Module['canvas'].style['cursor'] = 'none';
  131. }
  132. );
  133. /* *INDENT-ON* */ /* clang-format on */
  134. }
  135. }
  136. return 0;
  137. }
  138. static int Emscripten_SetRelativeMouseMode(SDL_bool enabled)
  139. {
  140. SDL_Window *window;
  141. SDL_WindowData *window_data;
  142. /* TODO: pointer lock isn't actually enabled yet */
  143. if (enabled) {
  144. window = SDL_GetMouseFocus();
  145. if (!window) {
  146. return -1;
  147. }
  148. window_data = window->internal;
  149. if (emscripten_request_pointerlock(window_data->canvas_id, 1) >= EMSCRIPTEN_RESULT_SUCCESS) {
  150. return 0;
  151. }
  152. } else {
  153. if (emscripten_exit_pointerlock() >= EMSCRIPTEN_RESULT_SUCCESS) {
  154. return 0;
  155. }
  156. }
  157. return -1;
  158. }
  159. void Emscripten_InitMouse(void)
  160. {
  161. SDL_Mouse *mouse = SDL_GetMouse();
  162. mouse->CreateCursor = Emscripten_CreateCursor;
  163. mouse->ShowCursor = Emscripten_ShowCursor;
  164. mouse->FreeCursor = Emscripten_FreeCursor;
  165. mouse->CreateSystemCursor = Emscripten_CreateSystemCursor;
  166. mouse->SetRelativeMouseMode = Emscripten_SetRelativeMouseMode;
  167. SDL_SetDefaultCursor(Emscripten_CreateDefaultCursor());
  168. }
  169. void Emscripten_QuitMouse(void)
  170. {
  171. }
  172. #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */