SDL_emscriptenmouse.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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. static SDL_Cursor*
  27. Emscripten_CreateCursorFromString(const char* cursor_str, SDL_bool is_custom)
  28. {
  29. SDL_Cursor* cursor;
  30. Emscripten_CursorData *curdata;
  31. cursor = SDL_calloc(1, sizeof(SDL_Cursor));
  32. if (cursor) {
  33. curdata = (Emscripten_CursorData *) SDL_calloc(1, sizeof(*curdata));
  34. if (curdata == NULL) {
  35. SDL_OutOfMemory();
  36. SDL_free(cursor);
  37. return NULL;
  38. }
  39. curdata->system_cursor = cursor_str;
  40. curdata->is_custom = is_custom;
  41. cursor->driverdata = curdata;
  42. } else {
  43. SDL_OutOfMemory();
  44. }
  45. return cursor;
  46. }
  47. static SDL_Cursor*
  48. Emscripten_CreateDefaultCursor()
  49. {
  50. return Emscripten_CreateCursorFromString("default", SDL_FALSE);
  51. }
  52. static SDL_Cursor*
  53. Emscripten_CreateCursor(SDL_Surface* surface, int hot_x, int hot_y)
  54. {
  55. const char *cursor_url = NULL;
  56. SDL_Surface *conv_surf;
  57. conv_surf = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ABGR8888);
  58. if (conv_surf == NULL) {
  59. return NULL;
  60. }
  61. cursor_url = (const char *)MAIN_THREAD_EM_ASM_INT({
  62. var w = $0;
  63. var h = $1;
  64. var hot_x = $2;
  65. var hot_y = $3;
  66. var pixels = $4;
  67. var canvas = document.createElement("canvas");
  68. canvas.width = w;
  69. canvas.height = h;
  70. var ctx = canvas.getContext("2d");
  71. var image = ctx.createImageData(w, h);
  72. var data = image.data;
  73. var src = pixels >> 2;
  74. var data32 = new Int32Array(data.buffer);
  75. data32.set(HEAP32.subarray(src, src + data32.length));
  76. ctx.putImageData(image, 0, 0);
  77. var url = hot_x === 0 && hot_y === 0
  78. ? "url(" + canvas.toDataURL() + "), auto"
  79. : "url(" + canvas.toDataURL() + ") " + hot_x + " " + hot_y + ", auto";
  80. var urlBuf = _malloc(url.length + 1);
  81. stringToUTF8(url, urlBuf, url.length + 1);
  82. return urlBuf;
  83. }, surface->w, surface->h, hot_x, hot_y, conv_surf->pixels);
  84. SDL_FreeSurface(conv_surf);
  85. return Emscripten_CreateCursorFromString(cursor_url, SDL_TRUE);
  86. }
  87. static SDL_Cursor*
  88. Emscripten_CreateSystemCursor(SDL_SystemCursor id)
  89. {
  90. const char *cursor_name = NULL;
  91. switch(id) {
  92. case SDL_SYSTEM_CURSOR_ARROW:
  93. cursor_name = "default";
  94. break;
  95. case SDL_SYSTEM_CURSOR_IBEAM:
  96. cursor_name = "text";
  97. break;
  98. case SDL_SYSTEM_CURSOR_WAIT:
  99. cursor_name = "wait";
  100. break;
  101. case SDL_SYSTEM_CURSOR_CROSSHAIR:
  102. cursor_name = "crosshair";
  103. break;
  104. case SDL_SYSTEM_CURSOR_WAITARROW:
  105. cursor_name = "progress";
  106. break;
  107. case SDL_SYSTEM_CURSOR_SIZENWSE:
  108. cursor_name = "nwse-resize";
  109. break;
  110. case SDL_SYSTEM_CURSOR_SIZENESW:
  111. cursor_name = "nesw-resize";
  112. break;
  113. case SDL_SYSTEM_CURSOR_SIZEWE:
  114. cursor_name = "ew-resize";
  115. break;
  116. case SDL_SYSTEM_CURSOR_SIZENS:
  117. cursor_name = "ns-resize";
  118. break;
  119. case SDL_SYSTEM_CURSOR_SIZEALL:
  120. cursor_name = "move";
  121. break;
  122. case SDL_SYSTEM_CURSOR_NO:
  123. cursor_name = "not-allowed";
  124. break;
  125. case SDL_SYSTEM_CURSOR_HAND:
  126. cursor_name = "pointer";
  127. break;
  128. default:
  129. SDL_assert(0);
  130. return NULL;
  131. }
  132. return Emscripten_CreateCursorFromString(cursor_name, SDL_FALSE);
  133. }
  134. static void
  135. Emscripten_FreeCursor(SDL_Cursor* cursor)
  136. {
  137. Emscripten_CursorData *curdata;
  138. if (cursor) {
  139. curdata = (Emscripten_CursorData *) cursor->driverdata;
  140. if (curdata != NULL) {
  141. if (curdata->is_custom) {
  142. SDL_free((char *)curdata->system_cursor);
  143. }
  144. SDL_free(cursor->driverdata);
  145. }
  146. SDL_free(cursor);
  147. }
  148. }
  149. static int
  150. Emscripten_ShowCursor(SDL_Cursor* cursor)
  151. {
  152. Emscripten_CursorData *curdata;
  153. if (SDL_GetMouseFocus() != NULL) {
  154. if (cursor && cursor->driverdata) {
  155. curdata = (Emscripten_CursorData *) cursor->driverdata;
  156. if (curdata->system_cursor) {
  157. MAIN_THREAD_EM_ASM({
  158. if (Module['canvas']) {
  159. Module['canvas'].style['cursor'] = UTF8ToString($0);
  160. }
  161. }, curdata->system_cursor);
  162. }
  163. } else {
  164. MAIN_THREAD_EM_ASM(
  165. if (Module['canvas']) {
  166. Module['canvas'].style['cursor'] = 'none';
  167. }
  168. );
  169. }
  170. }
  171. return 0;
  172. }
  173. static void
  174. Emscripten_WarpMouse(SDL_Window* window, int x, int y)
  175. {
  176. SDL_Unsupported();
  177. }
  178. static int
  179. Emscripten_SetRelativeMouseMode(SDL_bool enabled)
  180. {
  181. SDL_Window *window;
  182. SDL_WindowData *window_data;
  183. /* TODO: pointer lock isn't actually enabled yet */
  184. if (enabled) {
  185. window = SDL_GetMouseFocus();
  186. if (window == NULL) {
  187. return -1;
  188. }
  189. window_data = (SDL_WindowData *) window->driverdata;
  190. if (emscripten_request_pointerlock(window_data->canvas_id, 1) >= EMSCRIPTEN_RESULT_SUCCESS) {
  191. return 0;
  192. }
  193. } else {
  194. if (emscripten_exit_pointerlock() >= EMSCRIPTEN_RESULT_SUCCESS) {
  195. return 0;
  196. }
  197. }
  198. return -1;
  199. }
  200. void
  201. Emscripten_InitMouse()
  202. {
  203. SDL_Mouse* mouse = SDL_GetMouse();
  204. mouse->CreateCursor = Emscripten_CreateCursor;
  205. mouse->ShowCursor = Emscripten_ShowCursor;
  206. mouse->FreeCursor = Emscripten_FreeCursor;
  207. mouse->WarpMouse = Emscripten_WarpMouse;
  208. mouse->CreateSystemCursor = Emscripten_CreateSystemCursor;
  209. mouse->SetRelativeMouseMode = Emscripten_SetRelativeMouseMode;
  210. SDL_SetDefaultCursor(Emscripten_CreateDefaultCursor());
  211. }
  212. void
  213. Emscripten_FiniMouse()
  214. {
  215. }
  216. #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
  217. /* vi: set ts=4 sw=4 expandtab: */