Skip to content
This repository was archived by the owner on Feb 17, 2022. It is now read-only.

Commit 7469c14

Browse files
committed
emscripten: mouse: proxy canvas access to main thread
1 parent 2ee1b48 commit 7469c14

1 file changed

Lines changed: 70 additions & 31 deletions

File tree

src/video/emscripten/SDL_emscriptenmouse.c

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <emscripten/emscripten.h>
2828
#include <emscripten/html5.h>
29+
#include <emscripten/threading.h>
2930

3031
#include "SDL_emscriptenmouse.h"
3132

@@ -64,19 +65,9 @@ Emscripten_CreateDefaultCursor()
6465
return Emscripten_CreateCursorFromString("default", SDL_FALSE);
6566
}
6667

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)
6969
{
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({
8071
var w = $0;
8172
var h = $1;
8273
var hot_x = $2;
@@ -124,7 +115,40 @@ Emscripten_CreateCursor(SDL_Surface* surface, int hot_x, int hot_y)
124115
stringToUTF8(url, urlBuf, url.length + 1);
125116

126117
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+
}
128152

129153
SDL_FreeSurface(conv_surf);
130154

@@ -199,28 +223,43 @@ Emscripten_FreeCursor(SDL_Cursor* cursor)
199223
}
200224
}
201225

202-
static int
203-
Emscripten_ShowCursor(SDL_Cursor* cursor)
226+
static void
227+
Emscripten_ShowCursorWorker(SDL_Cursor *cursor)
204228
{
205229
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({
221235
if (Module['canvas']) {
222-
Module['canvas'].style['cursor'] = 'none';
236+
Module['canvas'].style['cursor'] = UTF8ToString($0);
223237
}
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
224263
);
225264
}
226265
}

0 commit comments

Comments
 (0)