Atlas - SDL_emscriptenmouse.c

Home / ext / SDL / src / video / emscripten Lines: 1 | Size: 8548 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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 44static 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 63static 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 70EM_JS_DEPS(sdlmouse, "$stringToUTF8,$UTF8ToString"); 71 72static 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_RGBA32); 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 121static 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 128static 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 145static 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 174static 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 199static SDL_MouseButtonFlags Emscripten_GetGlobalMouseState(float *x, float *y) 200{ 201 *x = MAIN_THREAD_EM_ASM_DOUBLE({ 202 return Module['SDL3']['mouse_x']; 203 }); 204 *y = MAIN_THREAD_EM_ASM_DOUBLE({ 205 return Module['SDL3']['mouse_y']; 206 }); 207 SDL_MouseButtonFlags flags = 0; 208 for (int i = 0; i < 5; ++i) { 209 const bool button_down = MAIN_THREAD_EM_ASM_INT({ 210 return Module['SDL3']['mouse_buttons'][$0]; 211 }, i); 212 if (button_down) { 213 flags |= 1 << i; 214 } 215 } 216 return flags; 217} 218 219void Emscripten_InitMouse(void) 220{ 221 SDL_Mouse *mouse = SDL_GetMouse(); 222 223 mouse->CreateCursor = Emscripten_CreateCursor; 224 mouse->ShowCursor = Emscripten_ShowCursor; 225 mouse->FreeCursor = Emscripten_FreeCursor; 226 mouse->CreateSystemCursor = Emscripten_CreateSystemCursor; 227 mouse->SetRelativeMouseMode = Emscripten_SetRelativeMouseMode; 228 229 // Add event listeners to track mouse events on the document 230 MAIN_THREAD_EM_ASM({ 231 if (!Module['SDL3']) { 232 Module['SDL3'] = {}; 233 } 234 var SDL3 = Module['SDL3']; 235 SDL3['mouse_x'] = 0; 236 SDL3['mouse_y'] = 0; 237 /* 238 Based on https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button 239 Possible value for button in the event object is [0, 5) 240 NOTE: Some browsers do not allow handling the forwards and backwards buttons 241 */ 242 SDL3['mouse_buttons'] = []; 243 for (var i = 0; i < 5; ++i) { 244 SDL3['mouse_buttons'][i] = false; 245 } 246 document.addEventListener('mousemove', function(e) { 247 // Reacquire from object in case it changed for some reason 248 var SDL3 = Module['SDL3']; 249 SDL3['mouse_x'] = e.clientX; 250 SDL3['mouse_y'] = e.clientY; 251 }); 252 document.addEventListener('mousedown', function(e) { 253 // Reacquire from object in case it changed for some reason 254 var SDL3 = Module['SDL3']; 255 if (0 <= e.button && e.button < SDL3['mouse_buttons'].length) { 256 SDL3['mouse_buttons'][e.button] = true; 257 } 258 }); 259 document.addEventListener('mouseup', function(e) { 260 // Reacquire from object in case it changed for some reason 261 var SDL3 = Module['SDL3']; 262 if (0 <= e.button && e.button < SDL3['mouse_buttons'].length) { 263 SDL3['mouse_buttons'][e.button] = false; 264 } 265 }); 266 }); 267 mouse->GetGlobalMouseState = Emscripten_GetGlobalMouseState; 268 269 SDL_SetDefaultCursor(Emscripten_CreateDefaultCursor()); 270} 271 272void Emscripten_QuitMouse(void) 273{ 274} 275 276#endif // SDL_VIDEO_DRIVER_EMSCRIPTEN 277
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.