Skip to content

Handle global mouse state for Emscripten #12669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/video/emscripten/SDL_emscriptenmouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,26 @@ static bool Emscripten_SetRelativeMouseMode(bool enabled)
return false;
}

static SDL_MouseButtonFlags Emscripten_GetGlobalMouseState(float *x, float *y)
{
*x = MAIN_THREAD_EM_ASM_DOUBLE({
return Module['SDL3']['mouse_x'];
});
*y = MAIN_THREAD_EM_ASM_DOUBLE({
return Module['SDL3']['mouse_y'];
});
SDL_MouseButtonFlags flags = 0;
for (int i = 0; i < 5; ++i) {
const bool button_down = MAIN_THREAD_EM_ASM_INT({
return Module['SDL3']['mouse_buttons'][$0];
}, i);
if (button_down) {
flags |= 1 << i;
}
}
return flags;
}

void Emscripten_InitMouse(void)
{
SDL_Mouse *mouse = SDL_GetMouse();
Expand All @@ -206,6 +226,46 @@ void Emscripten_InitMouse(void)
mouse->CreateSystemCursor = Emscripten_CreateSystemCursor;
mouse->SetRelativeMouseMode = Emscripten_SetRelativeMouseMode;

// Add event listeners to track mouse events on the document
MAIN_THREAD_EM_ASM({
if (!Module['SDL3']) {
Module['SDL3'] = {};
}
var SDL3 = Module['SDL3'];
SDL3['mouse_x'] = 0;
SDL3['mouse_y'] = 0;
/*
Based on https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
Possible value for button in the event object is [0, 5)
NOTE: Some browsers do not allow handling the forwards and backwards buttons
*/
SDL3['mouse_buttons'] = [];
for (var i = 0; i < 5; ++i) {
SDL3['mouse_buttons'][i] = false;
}
document.addEventListener('mousemove', function(e) {
// Reacquire from object in case it changed for some reason
var SDL3 = Module['SDL3'];
SDL3['mouse_x'] = e.clientX;
SDL3['mouse_y'] = e.clientY;
});
document.addEventListener('mousedown', function(e) {
// Reacquire from object in case it changed for some reason
var SDL3 = Module['SDL3'];
if (0 <= e.button && e.button < SDL3['mouse_buttons'].length) {
SDL3['mouse_buttons'][e.button] = true;
}
});
document.addEventListener('mouseup', function(e) {
// Reacquire from object in case it changed for some reason
var SDL3 = Module['SDL3'];
if (0 <= e.button && e.button < SDL3['mouse_buttons'].length) {
SDL3['mouse_buttons'][e.button] = false;
}
});
});
mouse->GetGlobalMouseState = Emscripten_GetGlobalMouseState;

SDL_SetDefaultCursor(Emscripten_CreateDefaultCursor());
}

Expand Down
6 changes: 6 additions & 0 deletions test/testmouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ static void loop(void *arg)
struct mouse_loop_data *loop_data = (struct mouse_loop_data *)arg;
SDL_Event event;
SDL_Renderer *renderer = loop_data->renderer;
float fx, fy;
SDL_MouseButtonFlags flags;

/* Check for events */
while (SDL_PollEvent(&event)) {
Expand Down Expand Up @@ -265,6 +267,10 @@ static void loop(void *arg)
DrawObject(renderer, active);
}

flags = SDL_GetGlobalMouseState(&fx, &fy);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDebugTextFormat(renderer, 0, 0, "Global Mouse State: x=%f y=%f flags=%" SDL_PRIu32, fx, fy, flags);

SDL_RenderPresent(renderer);

#ifdef SDL_PLATFORM_EMSCRIPTEN
Expand Down
Loading