diff options
Diffstat (limited to 'contrib/SDL-3.2.8/src/video/emscripten/SDL_emscriptenmouse.c')
| -rw-r--r-- | contrib/SDL-3.2.8/src/video/emscripten/SDL_emscriptenmouse.c | 216 |
1 files changed, 216 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/video/emscripten/SDL_emscriptenmouse.c b/contrib/SDL-3.2.8/src/video/emscripten/SDL_emscriptenmouse.c new file mode 100644 index 0000000..c959804 --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/emscripten/SDL_emscriptenmouse.c | |||
| @@ -0,0 +1,216 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 4 | |||
| 5 | This software is provided 'as-is', without any express or implied | ||
| 6 | warranty. In no event will the authors be held liable for any damages | ||
| 7 | arising from the use of this software. | ||
| 8 | |||
| 9 | Permission is granted to anyone to use this software for any purpose, | ||
| 10 | including commercial applications, and to alter it and redistribute it | ||
| 11 | freely, subject to the following restrictions: | ||
| 12 | |||
| 13 | 1. The origin of this software must not be misrepresented; you must not | ||
| 14 | claim that you wrote the original software. If you use this software | ||
| 15 | in a product, an acknowledgment in the product documentation would be | ||
| 16 | appreciated but is not required. | ||
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
| 18 | misrepresented as being the original software. | ||
| 19 | 3. This notice may not be removed or altered from any source distribution. | ||
| 20 | */ | ||
| 21 | #include "SDL_internal.h" | ||
| 22 | |||
| 23 | #ifdef SDL_VIDEO_DRIVER_EMSCRIPTEN | ||
| 24 | |||
| 25 | #include <emscripten/emscripten.h> | ||
| 26 | #include <emscripten/html5.h> | ||
| 27 | #include <emscripten/threading.h> | ||
| 28 | |||
| 29 | #include "SDL_emscriptenmouse.h" | ||
| 30 | #include "SDL_emscriptenvideo.h" | ||
| 31 | |||
| 32 | #include "../SDL_video_c.h" | ||
| 33 | #include "../../events/SDL_mouse_c.h" | ||
| 34 | |||
| 35 | // older Emscriptens don't have this, but we need to for wasm64 compatibility. | ||
| 36 | #ifndef MAIN_THREAD_EM_ASM_PTR | ||
| 37 | #ifdef __wasm64__ | ||
| 38 | #error You need to upgrade your Emscripten compiler to support wasm64 | ||
| 39 | #else | ||
| 40 | #define MAIN_THREAD_EM_ASM_PTR MAIN_THREAD_EM_ASM_INT | ||
| 41 | #endif | ||
| 42 | #endif | ||
| 43 | |||
| 44 | static SDL_Cursor *Emscripten_CreateCursorFromString(const char *cursor_str, bool is_custom) | ||
| 45 | { | ||
| 46 | SDL_CursorData *curdata; | ||
| 47 | SDL_Cursor *cursor = SDL_calloc(1, sizeof(SDL_Cursor)); | ||
| 48 | if (cursor) { | ||
| 49 | curdata = (SDL_CursorData *)SDL_calloc(1, sizeof(*curdata)); | ||
| 50 | if (!curdata) { | ||
| 51 | SDL_free(cursor); | ||
| 52 | return NULL; | ||
| 53 | } | ||
| 54 | |||
| 55 | curdata->system_cursor = cursor_str; | ||
| 56 | curdata->is_custom = is_custom; | ||
| 57 | cursor->internal = curdata; | ||
| 58 | } | ||
| 59 | |||
| 60 | return cursor; | ||
| 61 | } | ||
| 62 | |||
| 63 | static SDL_Cursor *Emscripten_CreateDefaultCursor(void) | ||
| 64 | { | ||
| 65 | SDL_SystemCursor id = SDL_GetDefaultSystemCursor(); | ||
| 66 | const char *cursor_name = SDL_GetCSSCursorName(id, NULL); | ||
| 67 | return Emscripten_CreateCursorFromString(cursor_name, false); | ||
| 68 | } | ||
| 69 | |||
| 70 | EM_JS_DEPS(sdlmouse, "$stringToUTF8,$UTF8ToString"); | ||
| 71 | |||
| 72 | static SDL_Cursor *Emscripten_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y) | ||
| 73 | { | ||
| 74 | const char *cursor_url = NULL; | ||
| 75 | SDL_Surface *conv_surf; | ||
| 76 | |||
| 77 | conv_surf = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_ABGR8888); | ||
| 78 | |||
| 79 | if (!conv_surf) { | ||
| 80 | return NULL; | ||
| 81 | } | ||
| 82 | |||
| 83 | /* *INDENT-OFF* */ // clang-format off | ||
| 84 | cursor_url = (const char *)MAIN_THREAD_EM_ASM_PTR({ | ||
| 85 | var w = $0; | ||
| 86 | var h = $1; | ||
| 87 | var hot_x = $2; | ||
| 88 | var hot_y = $3; | ||
| 89 | var pixels = $4; | ||
| 90 | |||
| 91 | var canvas = document.createElement("canvas"); | ||
| 92 | canvas.width = w; | ||
| 93 | canvas.height = h; | ||
| 94 | |||
| 95 | var ctx = canvas.getContext("2d"); | ||
| 96 | |||
| 97 | var image = ctx.createImageData(w, h); | ||
| 98 | var data = image.data; | ||
| 99 | var src = pixels / 4; | ||
| 100 | |||
| 101 | var data32 = new Int32Array(data.buffer); | ||
| 102 | data32.set(HEAP32.subarray(src, src + data32.length)); | ||
| 103 | |||
| 104 | ctx.putImageData(image, 0, 0); | ||
| 105 | var url = hot_x === 0 && hot_y === 0 | ||
| 106 | ? "url(" + canvas.toDataURL() + "), auto" | ||
| 107 | : "url(" + canvas.toDataURL() + ") " + hot_x + " " + hot_y + ", auto"; | ||
| 108 | |||
| 109 | var urlBuf = _SDL_malloc(url.length + 1); | ||
| 110 | stringToUTF8(url, urlBuf, url.length + 1); | ||
| 111 | |||
| 112 | return urlBuf; | ||
| 113 | }, surface->w, surface->h, hot_x, hot_y, conv_surf->pixels); | ||
| 114 | /* *INDENT-ON* */ // clang-format on | ||
| 115 | |||
| 116 | SDL_DestroySurface(conv_surf); | ||
| 117 | |||
| 118 | return Emscripten_CreateCursorFromString(cursor_url, true); | ||
| 119 | } | ||
| 120 | |||
| 121 | static SDL_Cursor *Emscripten_CreateSystemCursor(SDL_SystemCursor id) | ||
| 122 | { | ||
| 123 | const char *cursor_name = SDL_GetCSSCursorName(id, NULL); | ||
| 124 | |||
| 125 | return Emscripten_CreateCursorFromString(cursor_name, false); | ||
| 126 | } | ||
| 127 | |||
| 128 | static void Emscripten_FreeCursor(SDL_Cursor *cursor) | ||
| 129 | { | ||
| 130 | SDL_CursorData *curdata; | ||
| 131 | if (cursor) { | ||
| 132 | curdata = cursor->internal; | ||
| 133 | |||
| 134 | if (curdata) { | ||
| 135 | if (curdata->is_custom) { | ||
| 136 | SDL_free((char *)curdata->system_cursor); | ||
| 137 | } | ||
| 138 | SDL_free(cursor->internal); | ||
| 139 | } | ||
| 140 | |||
| 141 | SDL_free(cursor); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | static bool Emscripten_ShowCursor(SDL_Cursor *cursor) | ||
| 146 | { | ||
| 147 | SDL_CursorData *curdata; | ||
| 148 | if (SDL_GetMouseFocus() != NULL) { | ||
| 149 | if (cursor && cursor->internal) { | ||
| 150 | curdata = cursor->internal; | ||
| 151 | |||
| 152 | if (curdata->system_cursor) { | ||
| 153 | /* *INDENT-OFF* */ // clang-format off | ||
| 154 | MAIN_THREAD_EM_ASM({ | ||
| 155 | if (Module['canvas']) { | ||
| 156 | Module['canvas'].style['cursor'] = UTF8ToString($0); | ||
| 157 | } | ||
| 158 | }, curdata->system_cursor); | ||
| 159 | /* *INDENT-ON* */ // clang-format on | ||
| 160 | } | ||
| 161 | } else { | ||
| 162 | /* *INDENT-OFF* */ // clang-format off | ||
| 163 | MAIN_THREAD_EM_ASM( | ||
| 164 | if (Module['canvas']) { | ||
| 165 | Module['canvas'].style['cursor'] = 'none'; | ||
| 166 | } | ||
| 167 | ); | ||
| 168 | /* *INDENT-ON* */ // clang-format on | ||
| 169 | } | ||
| 170 | } | ||
| 171 | return true; | ||
| 172 | } | ||
| 173 | |||
| 174 | static bool Emscripten_SetRelativeMouseMode(bool enabled) | ||
| 175 | { | ||
| 176 | SDL_Window *window; | ||
| 177 | SDL_WindowData *window_data; | ||
| 178 | |||
| 179 | // TODO: pointer lock isn't actually enabled yet | ||
| 180 | if (enabled) { | ||
| 181 | window = SDL_GetMouseFocus(); | ||
| 182 | if (!window) { | ||
| 183 | return false; | ||
| 184 | } | ||
| 185 | |||
| 186 | window_data = window->internal; | ||
| 187 | |||
| 188 | if (emscripten_request_pointerlock(window_data->canvas_id, 1) >= EMSCRIPTEN_RESULT_SUCCESS) { | ||
| 189 | return true; | ||
| 190 | } | ||
| 191 | } else { | ||
| 192 | if (emscripten_exit_pointerlock() >= EMSCRIPTEN_RESULT_SUCCESS) { | ||
| 193 | return true; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | return false; | ||
| 197 | } | ||
| 198 | |||
| 199 | void Emscripten_InitMouse(void) | ||
| 200 | { | ||
| 201 | SDL_Mouse *mouse = SDL_GetMouse(); | ||
| 202 | |||
| 203 | mouse->CreateCursor = Emscripten_CreateCursor; | ||
| 204 | mouse->ShowCursor = Emscripten_ShowCursor; | ||
| 205 | mouse->FreeCursor = Emscripten_FreeCursor; | ||
| 206 | mouse->CreateSystemCursor = Emscripten_CreateSystemCursor; | ||
| 207 | mouse->SetRelativeMouseMode = Emscripten_SetRelativeMouseMode; | ||
| 208 | |||
| 209 | SDL_SetDefaultCursor(Emscripten_CreateDefaultCursor()); | ||
| 210 | } | ||
| 211 | |||
| 212 | void Emscripten_QuitMouse(void) | ||
| 213 | { | ||
| 214 | } | ||
| 215 | |||
| 216 | #endif // SDL_VIDEO_DRIVER_EMSCRIPTEN | ||
