|
26 | 26 |
|
27 | 27 | #include <emscripten/emscripten.h> |
28 | 28 | #include <emscripten/html5.h> |
| 29 | +#include <emscripten/threading.h> |
29 | 30 |
|
30 | 31 | #include "SDL_emscriptenmouse.h" |
31 | 32 |
|
@@ -64,19 +65,9 @@ Emscripten_CreateDefaultCursor() |
64 | 65 | return Emscripten_CreateCursorFromString("default", SDL_FALSE); |
65 | 66 | } |
66 | 67 |
|
67 | | -static SDL_Cursor* |
68 | | -Emscripten_CreateCursor(SDL_Surface* surface, int hot_x, int hot_y) |
| 68 | +static const char *Emscripten_GetCursorUrl(int w, int h, int hot_x, int hot_y, int pixels) |
69 | 69 | { |
70 | | - const char *cursor_url = NULL; |
71 | | - SDL_Surface *conv_surf; |
72 | | - |
73 | | - conv_surf = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ABGR8888, 0); |
74 | | - |
75 | | - if (!conv_surf) { |
76 | | - return NULL; |
77 | | - } |
78 | | - |
79 | | - cursor_url = (const char *)EM_ASM_INT({ |
| 70 | + return (const char *)EM_ASM_INT({ |
80 | 71 | var w = $0; |
81 | 72 | var h = $1; |
82 | 73 | var hot_x = $2; |
@@ -124,7 +115,40 @@ Emscripten_CreateCursor(SDL_Surface* surface, int hot_x, int hot_y) |
124 | 115 | stringToUTF8(url, urlBuf, url.length + 1); |
125 | 116 |
|
126 | 117 | return urlBuf; |
127 | | - }, surface->w, surface->h, hot_x, hot_y, conv_surf->pixels); |
| 118 | + }, w, h, hot_x, hot_y, pixels); |
| 119 | +} |
| 120 | + |
| 121 | +static SDL_Cursor* |
| 122 | +Emscripten_CreateCursor(SDL_Surface* surface, int hot_x, int hot_y) |
| 123 | +{ |
| 124 | + const char *cursor_url = NULL; |
| 125 | + SDL_Surface *conv_surf; |
| 126 | + |
| 127 | + conv_surf = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ABGR8888, 0); |
| 128 | + |
| 129 | + if (!conv_surf) { |
| 130 | + return NULL; |
| 131 | + } |
| 132 | + |
| 133 | + if (emscripten_is_main_runtime_thread()) { |
| 134 | + cursor_url = Emscripten_GetCursorUrl( |
| 135 | + surface->w, |
| 136 | + surface->h, |
| 137 | + hot_x, |
| 138 | + hot_y, |
| 139 | + conv_surf->pixels |
| 140 | + ); |
| 141 | + } else { |
| 142 | + cursor_url = emscripten_sync_run_in_main_runtime_thread( |
| 143 | + EM_FUNC_SIG_IIIIIII, |
| 144 | + Emscripten_GetCursorUrl, |
| 145 | + surface->w, |
| 146 | + surface->h, |
| 147 | + hot_x, |
| 148 | + hot_y, |
| 149 | + conv_surf->pixels |
| 150 | + ); |
| 151 | + } |
128 | 152 |
|
129 | 153 | SDL_FreeSurface(conv_surf); |
130 | 154 |
|
@@ -199,28 +223,43 @@ Emscripten_FreeCursor(SDL_Cursor* cursor) |
199 | 223 | } |
200 | 224 | } |
201 | 225 |
|
202 | | -static int |
203 | | -Emscripten_ShowCursor(SDL_Cursor* cursor) |
| 226 | +static void |
| 227 | +Emscripten_ShowCursorWorker(SDL_Cursor *cursor) |
204 | 228 | { |
205 | 229 | Emscripten_CursorData *curdata; |
206 | | - if (SDL_GetMouseFocus() != NULL) { |
207 | | - if(cursor && cursor->driverdata) { |
208 | | - curdata = (Emscripten_CursorData *) cursor->driverdata; |
209 | | - |
210 | | - if(curdata->system_cursor) { |
211 | | - EM_ASM_INT({ |
212 | | - if (Module['canvas']) { |
213 | | - Module['canvas'].style['cursor'] = UTF8ToString($0); |
214 | | - } |
215 | | - return 0; |
216 | | - }, curdata->system_cursor); |
217 | | - } |
218 | | - } |
219 | | - else { |
220 | | - EM_ASM( |
| 230 | + if(cursor && cursor->driverdata) { |
| 231 | + curdata = (Emscripten_CursorData *) cursor->driverdata; |
| 232 | + |
| 233 | + if(curdata->system_cursor) { |
| 234 | + EM_ASM_INT({ |
221 | 235 | if (Module['canvas']) { |
222 | | - Module['canvas'].style['cursor'] = 'none'; |
| 236 | + Module['canvas'].style['cursor'] = UTF8ToString($0); |
223 | 237 | } |
| 238 | + return 0; |
| 239 | + }, curdata->system_cursor); |
| 240 | + } |
| 241 | + } |
| 242 | + else { |
| 243 | + EM_ASM( |
| 244 | + if (Module['canvas']) { |
| 245 | + Module['canvas'].style['cursor'] = 'none'; |
| 246 | + } |
| 247 | + ); |
| 248 | + } |
| 249 | + |
| 250 | +} |
| 251 | + |
| 252 | +static int |
| 253 | +Emscripten_ShowCursor(SDL_Cursor* cursor) |
| 254 | +{ |
| 255 | + if (SDL_GetMouseFocus() != NULL) { |
| 256 | + if (emscripten_is_main_runtime_thread()) { |
| 257 | + Emscripten_ShowCursorWorker(cursor); |
| 258 | + } else { |
| 259 | + emscripten_sync_run_in_main_runtime_thread( |
| 260 | + EM_FUNC_SIG_VI, |
| 261 | + Emscripten_ShowCursorWorker, |
| 262 | + (uint32_t)cursor |
224 | 263 | ); |
225 | 264 | } |
226 | 265 | } |
|
0 commit comments