SDL_emscriptenmouse.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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/emscripten.h>
  21. #include <emscripten/html5.h>
  22. #include <emscripten/threading.h>
  23. #include "SDL_emscriptenmouse.h"
  24. #include "SDL_emscriptenvideo.h"
  25. #include "../../events/SDL_mouse_c.h"
  26. /* older Emscriptens don't have this, but we need to for wasm64 compatibility. */
  27. #ifndef MAIN_THREAD_EM_ASM_PTR
  28. #ifdef __wasm64__
  29. #error You need to upgrade your Emscripten compiler to support wasm64
  30. #else
  31. #define MAIN_THREAD_EM_ASM_PTR MAIN_THREAD_EM_ASM_INT
  32. #endif
  33. #endif
  34. static SDL_Cursor *Emscripten_CreateCursorFromString(const char *cursor_str, SDL_bool is_custom)
  35. {
  36. SDL_Cursor *cursor;
  37. Emscripten_CursorData *curdata;
  38. cursor = SDL_calloc(1, sizeof(SDL_Cursor));
  39. if (cursor) {
  40. curdata = (Emscripten_CursorData *)SDL_calloc(1, sizeof(*curdata));
  41. if (curdata == NULL) {
  42. SDL_OutOfMemory();
  43. SDL_free(cursor);
  44. return NULL;
  45. }
  46. curdata->system_cursor = cursor_str;
  47. curdata->is_custom = is_custom;
  48. cursor->driverdata = curdata;
  49. } else {
  50. SDL_OutOfMemory();
  51. }
  52. return cursor;
  53. }
  54. static SDL_Cursor *Emscripten_CreateDefaultCursor()
  55. {
  56. return Emscripten_CreateCursorFromString("default", SDL_FALSE);
  57. }
  58. EM_JS_DEPS(sdlmouse, "$stringToUTF8,$UTF8ToString");
  59. static SDL_Cursor *Emscripten_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y)
  60. {
  61. const char *cursor_url = NULL;
  62. SDL_Surface *conv_surf;
  63. conv_surf = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ABGR8888, 0);
  64. if (conv_surf == NULL) {
  65. return NULL;
  66. }
  67. /* *INDENT-OFF* */ /* clang-format off */
  68. cursor_url = (const char *)MAIN_THREAD_EM_ASM_PTR({
  69. var w = $0;
  70. var h = $1;
  71. var hot_x = $2;
  72. var hot_y = $3;
  73. var pixels = $4;
  74. var canvas = document.createElement("canvas");
  75. canvas.width = w;
  76. canvas.height = h;
  77. var ctx = canvas.getContext("2d");
  78. var image = ctx.createImageData(w, h);
  79. var data = image.data;
  80. var src = pixels >> 2;
  81. var dst = 0;
  82. var num;
  83. if (typeof CanvasPixelArray !== 'undefined' && data instanceof CanvasPixelArray) {
  84. // IE10/IE11: ImageData objects are backed by the deprecated CanvasPixelArray,
  85. // not UInt8ClampedArray. These don't have buffers, so we need to revert
  86. // to copying a byte at a time. We do the undefined check because modern
  87. // browsers do not define CanvasPixelArray anymore.
  88. num = data.length;
  89. while (dst < num) {
  90. var val = HEAP32[src]; // This is optimized. Instead, we could do {{{ makeGetValue('buffer', 'dst', 'i32') }}};
  91. data[dst ] = val & 0xff;
  92. data[dst+1] = (val >> 8) & 0xff;
  93. data[dst+2] = (val >> 16) & 0xff;
  94. data[dst+3] = (val >> 24) & 0xff;
  95. src++;
  96. dst += 4;
  97. }
  98. } else {
  99. var data32 = new Int32Array(data.buffer);
  100. num = data32.length;
  101. data32.set(HEAP32.subarray(src, src + num));
  102. }
  103. ctx.putImageData(image, 0, 0);
  104. var url = hot_x === 0 && hot_y === 0
  105. ? "url(" + canvas.toDataURL() + "), auto"
  106. : "url(" + canvas.toDataURL() + ") " + hot_x + " " + hot_y + ", auto";
  107. var urlBuf = _malloc(url.length + 1);
  108. stringToUTF8(url, urlBuf, url.length + 1);
  109. return urlBuf;
  110. }, surface->w, surface->h, hot_x, hot_y, conv_surf->pixels);
  111. /* *INDENT-ON* */ /* clang-format on */
  112. SDL_FreeSurface(conv_surf);
  113. return Emscripten_CreateCursorFromString(cursor_url, SDL_TRUE);
  114. }
  115. static SDL_Cursor *Emscripten_CreateSystemCursor(SDL_SystemCursor id)
  116. {
  117. const char *cursor_name = NULL;
  118. switch (id) {
  119. case SDL_SYSTEM_CURSOR_ARROW:
  120. cursor_name = "default";
  121. break;
  122. case SDL_SYSTEM_CURSOR_IBEAM:
  123. cursor_name = "text";
  124. break;
  125. case SDL_SYSTEM_CURSOR_WAIT:
  126. cursor_name = "wait";
  127. break;
  128. case SDL_SYSTEM_CURSOR_CROSSHAIR:
  129. cursor_name = "crosshair";
  130. break;
  131. case SDL_SYSTEM_CURSOR_WAITARROW:
  132. cursor_name = "progress";
  133. break;
  134. case SDL_SYSTEM_CURSOR_SIZENWSE:
  135. cursor_name = "nwse-resize";
  136. break;
  137. case SDL_SYSTEM_CURSOR_SIZENESW:
  138. cursor_name = "nesw-resize";
  139. break;
  140. case SDL_SYSTEM_CURSOR_SIZEWE:
  141. cursor_name = "ew-resize";
  142. break;
  143. case SDL_SYSTEM_CURSOR_SIZENS:
  144. cursor_name = "ns-resize";
  145. break;
  146. case SDL_SYSTEM_CURSOR_SIZEALL:
  147. cursor_name = "move";
  148. break;
  149. case SDL_SYSTEM_CURSOR_NO:
  150. cursor_name = "not-allowed";
  151. break;
  152. case SDL_SYSTEM_CURSOR_HAND:
  153. cursor_name = "pointer";
  154. break;
  155. default:
  156. SDL_assert(0);
  157. return NULL;
  158. }
  159. return Emscripten_CreateCursorFromString(cursor_name, SDL_FALSE);
  160. }
  161. static void Emscripten_FreeCursor(SDL_Cursor *cursor)
  162. {
  163. Emscripten_CursorData *curdata;
  164. if (cursor) {
  165. curdata = (Emscripten_CursorData *)cursor->driverdata;
  166. if (curdata != NULL) {
  167. if (curdata->is_custom) {
  168. SDL_free((char *)curdata->system_cursor);
  169. }
  170. SDL_free(cursor->driverdata);
  171. }
  172. SDL_free(cursor);
  173. }
  174. }
  175. static int Emscripten_ShowCursor(SDL_Cursor *cursor)
  176. {
  177. Emscripten_CursorData *curdata;
  178. if (SDL_GetMouseFocus() != NULL) {
  179. if (cursor && cursor->driverdata) {
  180. curdata = (Emscripten_CursorData *)cursor->driverdata;
  181. if (curdata->system_cursor) {
  182. /* *INDENT-OFF* */ /* clang-format off */
  183. MAIN_THREAD_EM_ASM({
  184. if (Module['canvas']) {
  185. Module['canvas'].style['cursor'] = UTF8ToString($0);
  186. }
  187. }, curdata->system_cursor);
  188. /* *INDENT-ON* */ /* clang-format on */
  189. }
  190. } else {
  191. /* *INDENT-OFF* */ /* clang-format off */
  192. MAIN_THREAD_EM_ASM(
  193. if (Module['canvas']) {
  194. Module['canvas'].style['cursor'] = 'none';
  195. }
  196. );
  197. /* *INDENT-ON* */ /* clang-format on */
  198. }
  199. }
  200. return 0;
  201. }
  202. static void Emscripten_WarpMouse(SDL_Window *window, int x, int y)
  203. {
  204. SDL_Unsupported();
  205. }
  206. static int Emscripten_SetRelativeMouseMode(SDL_bool enabled)
  207. {
  208. SDL_Window *window;
  209. SDL_WindowData *window_data;
  210. /* TODO: pointer lock isn't actually enabled yet */
  211. if (enabled) {
  212. window = SDL_GetMouseFocus();
  213. if (window == NULL) {
  214. return -1;
  215. }
  216. window_data = (SDL_WindowData *)window->driverdata;
  217. if (emscripten_request_pointerlock(window_data->canvas_id, 1) >= EMSCRIPTEN_RESULT_SUCCESS) {
  218. return 0;
  219. }
  220. } else {
  221. if (emscripten_exit_pointerlock() >= EMSCRIPTEN_RESULT_SUCCESS) {
  222. return 0;
  223. }
  224. }
  225. return -1;
  226. }
  227. void Emscripten_InitMouse()
  228. {
  229. SDL_Mouse *mouse = SDL_GetMouse();
  230. mouse->CreateCursor = Emscripten_CreateCursor;
  231. mouse->ShowCursor = Emscripten_ShowCursor;
  232. mouse->FreeCursor = Emscripten_FreeCursor;
  233. mouse->WarpMouse = Emscripten_WarpMouse;
  234. mouse->CreateSystemCursor = Emscripten_CreateSystemCursor;
  235. mouse->SetRelativeMouseMode = Emscripten_SetRelativeMouseMode;
  236. SDL_SetDefaultCursor(Emscripten_CreateDefaultCursor());
  237. }
  238. void Emscripten_FiniMouse()
  239. {
  240. }
  241. #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
  242. /* vi: set ts=4 sw=4 expandtab: */