commit 321b75bcdf92f3e66ad9a09d1440414b2462adaf
parent 83d91c4561ac88ad61615992cfc423769ab7c9d0
Author: anon <anon@example.com>
Date: Fri, 16 Oct 2026 22:53:52 +0000
Handle browser pointer capture and unsupported socket half-close
Diffstat:
4 files changed, 57 insertions(+), 4 deletions(-)
diff --git a/src/runtime/browser_host.js b/src/runtime/browser_host.js
@@ -10,5 +10,27 @@ addToLibrary({
},
ao_asset_base_url__deps: ['$stringToNewUTF8'],
ao_asset_base_url__proxy: 'sync',
- ao_asset_base_url: () => stringToNewUTF8(new URL('/browser-assets', location.href).href)
+ ao_asset_base_url: () => {
+ // SDL's browser backend has no CaptureMouse; use canvas pointer capture.
+ const canvas = Module.canvas;
+ canvas.addEventListener('pointerdown', event => {
+ if (!event.isPrimary) return;
+ Module.aoMousePointer = event.pointerId;
+ if (Module.aoMouseCaptured) canvas.setPointerCapture(event.pointerId);
+ });
+ const released = event => {
+ if (Module.aoMousePointer === event.pointerId) Module.aoMousePointer = undefined;
+ };
+ canvas.addEventListener('pointerup', released);
+ canvas.addEventListener('pointercancel', released);
+ return stringToNewUTF8(new URL('/browser-assets', location.href).href);
+ },
+ ao_mouse_capture__proxy: 'sync',
+ ao_mouse_capture: (enabled) => {
+ Module.aoMouseCaptured = enabled;
+ const pointer = Module.aoMousePointer;
+ if (pointer === undefined) return;
+ if (enabled) Module.canvas.setPointerCapture(pointer);
+ else if (Module.canvas.hasPointerCapture(pointer)) Module.canvas.releasePointerCapture(pointer);
+ }
});
diff --git a/src/runtime/browser_network.c b/src/runtime/browser_network.c
@@ -94,6 +94,18 @@ static void close_socket(void)
g_esp += 8;
}
+static void shutdown_socket(void)
+{
+ /* WebSocket has no TCP half-close. ACE handles this error and closes the
+ * socket normally; do not discard unread bytes or claim a half-close. */
+ pthread_mutex_lock(&mutex);
+ if (find_socket_locked(MEM32(g_esp + 4)))
+ MEM32(g_fs_base + 0x34) = MEM32(g_esp + 8) > 2 ? 10022 : 10045;
+ pthread_mutex_unlock(&mutex);
+ g_eax = UINT32_MAX;
+ g_esp += 12;
+}
+
static void connect_socket(void)
{
uint32_t handle = MEM32(g_esp + 4), address = MEM32(g_esp + 8), size = MEM32(g_esp + 12);
@@ -354,6 +366,7 @@ recomp_func_t linux_network_lookup(const char *name, uint32_t ordinal)
{"htonl", 8, network_long}, {"htons", 9, network_short},
{"ntohl", 14, network_long}, {"ntohs", 15, network_short},
{"socket", 23, create_socket}, {"closesocket", 3, close_socket},
+ {"shutdown", 22, shutdown_socket},
{"getsockname", 6, local_socket_address},
{"ioctlsocket", 10, control_socket}, {"connect", 4, connect_socket},
{"send", 19, send_socket}, {"recv", 16, receive_socket},
diff --git a/src/runtime/linux_window.c b/src/runtime/linux_window.c
@@ -209,11 +209,23 @@ static uint32_t send_message(uint32_t message, uint32_t wparam, uint32_t lparam)
return g_eax;
}
+/* The browser routes captured pointer events through the canvas. */
+static int capture_mouse(SDL_bool capture)
+{
+#ifdef __EMSCRIPTEN__
+ extern void ao_mouse_capture(int enabled);
+ ao_mouse_capture(capture);
+ return 0;
+#else
+ return SDL_CaptureMouse(capture);
+#endif
+}
+
static uint32_t change_capture(uint32_t handle)
{
uint32_t previous = client.capture;
SDL_bool capture = handle && SDL_GetKeyboardFocus() == linux_graphics_window();
- if (SDL_CaptureMouse(capture)) unsupported("mouse capture", handle);
+ if (capture_mouse(capture)) unsupported("mouse capture", handle);
client.capture = handle;
if (previous) send_message(0x215, 0, handle); /* WM_CAPTURECHANGED */
return previous;
@@ -360,7 +372,7 @@ static void default_procedure(void)
static void release_window(void)
{
if (client.capture) {
- if (SDL_CaptureMouse(SDL_FALSE)) unsupported("release mouse capture", client.capture);
+ if (capture_mouse(SDL_FALSE)) unsupported("release mouse capture", client.capture);
client.capture = 0;
}
if (pthread_mutex_lock(&message_mutex)) abort();
@@ -921,7 +933,7 @@ static void process_event(const SDL_Event *event)
switch (event->window.event) {
case SDL_WINDOWEVENT_CLOSE: queue_message(client.handle, 0x10, 0, 0, 0, 0); break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
- if (client.capture && SDL_CaptureMouse(SDL_TRUE)) unsupported("mouse capture", client.capture);
+ if (client.capture && capture_mouse(SDL_TRUE)) unsupported("mouse capture", client.capture);
if (!client.active) {
client.active = 1;
send_message(0x1c, 1, 0);
diff --git a/src/tests/browser_runtime.c b/src/tests/browser_runtime.c
@@ -133,6 +133,12 @@ int main(void)
MEM16(0x102000) = 2;
assert(call(linux_network_lookup("connect", 0), 3, (uint32_t[]){socket, 0x102000, 16}) == 0);
socket_address_queries(socket, 0);
+ // Unsupported half-close leaves the connection available for the test below.
+ assert(linux_network_lookup("shutdown", 0) == linux_network_lookup(NULL, 22));
+ assert(call(linux_network_lookup(NULL, 22), 2, (uint32_t[]){socket, 1}) == UINT32_MAX);
+
+ assert(MEM32(g_fs_base + 0x34) == 10045);
+
uint32_t event = call(linux_sync_lookup("CreateEventA"), 4, (uint32_t[]){0, 1, 0, 0});
assert(event);
assert(call(linux_network_lookup("WSAEventSelect", 0), 3, (uint32_t[]){socket, event, 33}) == 0);