From 33154f4ff9527463ccba6986ea9851af168080a0 Mon Sep 17 00:00:00 2001 From: ANSI TV Admin Date: Wed, 11 Nov 2026 12:04:09 -0800 Subject: [PATCH 07/24] SyncTERM: Exclusive Mouse Mode for every session; Ctrl+Alt releases Applies on top of `gpu-toggle.patch` (same tree). Adds an **Exclusive Mouse Mode** row to Program Settings (`ExclusiveMouse` under `[SyncTERM]` in syncterm.ini, off by default) that captures the pointer in every session, not just the entries that ask for it. The capture is a visible, window-confined pointer (`SDL_SetWindowGrab`) rather than hidden relative mode: a report is clamped to the screen on the way out, so relative mode never carried more than this. With either this or the per-entry setting on, **Ctrl+Alt pressed together** hands the pointer back to the OS and a click in the window takes it again (the click is swallowed, not reported). The chord fires on release and only if no other key came between, so Ctrl+Alt+key still reaches the host. The in-session Alt-E menu drops the capture while it is open and re-applies it on close, so a change made there takes effect at once. Verified under Xvfb with the SDL backend, measuring confinement with XTest relative motion: held in session, free after Ctrl+Alt (either order), held again after a click, still held after Ctrl+Alt+X, free with the setting off; the XFixes cursor image stays visible throughout. --- src/conio/ciolib.h | 8 +- src/conio/sdl_con.c | 91 +++++++++++++--------- src/conio/sdlfuncs.c | 1 + src/conio/sdlfuncs.h | 3 + src/syncterm/menu_settings.c | 2 + src/syncterm/scripts/menu_settings_ui.wren | 5 ++ src/syncterm/scripts/syncterm_menu.wren | 2 + src/syncterm/syncterm.c | 1 + src/syncterm/syncterm.h | 1 + src/syncterm/term.c | 14 ++-- src/syncterm/wren_bind_menu_settings.c | 2 + 11 files changed, 86 insertions(+), 44 deletions(-) diff --git a/src/conio/ciolib.h b/src/conio/ciolib.h index f3b13c66..7ffd36f1 100644 --- a/src/conio/ciolib.h +++ b/src/conio/ciolib.h @@ -915,9 +915,11 @@ CIOLIBEXPORT int ciolib_set_key_events(int on); /* Take the next key event. Answers 0 when there is none waiting. */ CIOLIBEXPORT int ciolib_getkeyevent(struct cio_key_event *ev); -/* Exclusive mouse: capture the pointer to the window and report motion - * rather than position. Answers non-zero if the backend could do it. - * Set per BBS entry -- see "Exclusive Mouse" in the entry editor. */ +/* Exclusive mouse: hold the (visible) pointer inside the window. + * Answers non-zero if the backend could do it. + * Set per BBS entry ("Exclusive Mouse" in the entry editor) or for every + * session ("Exclusive Mouse Mode" in Program Settings). While it is on, + * Ctrl+Alt hands the pointer back to the OS and a click takes it again. */ CIOLIBEXPORT int ciolib_set_mouse_exclusive(int on); #endif /* Do not add anything after this line */ diff --git a/src/conio/sdl_con.c b/src/conio/sdl_con.c index 3f294331..87930461 100644 --- a/src/conio/sdl_con.c +++ b/src/conio/sdl_con.c @@ -45,19 +45,18 @@ unsigned char sdl_keybuf[256]; /* Keyboard buffer */ /* * Exclusive mouse -- see ciolib_set_mouse_exclusive(). * - * Off, the pointer is the desktop's: it reports where it is, and it can - * walk out of the window in the middle of a turn. On, SDL captures it, - * hides it, and reports MOTION instead of position -- which is what a game - * wants and what a terminal has never been able to give one. + * Off, the pointer is the desktop's and can walk out of the window in the + * middle of a turn. On, SDL grabs it: it stays visible but is held inside + * the window until the session ends or the user lets it go. * - * The reported position is then this accumulator rather than the pointer, - * and it is deliberately NOT clamped to the window: a caller who keeps - * turning must keep being told so, and a position pinned at the edge would - * silently swallow the rest of the movement. + * Ctrl+Alt pressed together (and let go without another key in between) + * hands the pointer back to the OS, as a VM window does; a click in the + * window takes it again. The click only recaptures -- it is not reported. */ -static int sdl_mouse_exclusive = 0; -static int sdl_rel_x = 0, sdl_rel_y = 0; -static int sdl_rel_centred = 0; +static int sdl_mouse_exclusive = 0; /* captured right now */ +static int sdl_mouse_exclusive_want = 0; /* the session asked for it */ +static int sdl_ctrlalt_armed = 0; /* Ctrl+Alt down, nothing else yet */ +static int sdl_swallow_buttons = 0; /* buttons whose press recaptured */ /* * Key events -- see ciolib.h. A ring the size of the key buffer, holding @@ -863,17 +862,30 @@ static void setup_surfaces(struct video_stats *vs) update_cvstat(vs); } +static int sdl_mouse_grab(int on); + /* Called from event thread only */ CIOLIBEXPORT int ciolib_set_mouse_exclusive(int on) { - if (sdl.SetRelativeMouseMode == NULL) + sdl_mouse_exclusive_want = on ? 1 : 0; + sdl_ctrlalt_armed = 0; + return sdl_mouse_grab(on); +} + +static int +sdl_mouse_grab(int on) +{ + /* A window grab, not relative mode: relative mode hides the pointer, + * and the caller has to see where it is. Held inside the window the + * pointer reports its real position, which is all a report can carry + * anyway -- positions are clamped to the screen on the way out. */ + if (sdl.SetWindowGrab == NULL || win == NULL) return 0; /* SDL too old; caller carries on without */ - if (sdl.SetRelativeMouseMode(on ? SDL_TRUE : SDL_FALSE) != 0) - return 0; + assert_pthread_mutex_lock(&win_mutex); + sdl.SetWindowGrab(win, on ? SDL_TRUE : SDL_FALSE); + assert_pthread_mutex_unlock(&win_mutex); sdl_mouse_exclusive = on ? 1 : 0; - /* Centred on the first motion, where the screen size is in scope. */ - sdl_rel_centred = 0; return 1; } @@ -1253,6 +1265,17 @@ void sdl_video_event_thread(void *data) continue; switch (ev.type) { case SDL_KEYDOWN: /* Keypress */ + if (sdl_mouse_exclusive_want) { + SDL_Keycode ks = ev.key.keysym.sym; + if (ks == SDLK_LCTRL || ks == SDLK_RCTRL + || ks == SDLK_LALT || ks == SDLK_RALT) { + SDL_Keymod km2 = sdl.GetModState(); + if ((km2 & KMOD_CTRL) && (km2 & KMOD_ALT)) + sdl_ctrlalt_armed = 1; + } + else /* Ctrl+Alt+ is a key, not the release chord */ + sdl_ctrlalt_armed = 0; + } evdev = sdl_evdev_key(&ev.key.keysym); if (evdev != 0) ciokey_gotevent(evdev, true); @@ -1361,6 +1384,11 @@ void sdl_video_event_thread(void *data) } break; case SDL_KEYUP: + if (sdl_ctrlalt_armed) { + sdl_ctrlalt_armed = 0; + if (sdl_mouse_exclusive) + sdl_mouse_grab(0); /* back to the OS */ + } evdev = sdl_evdev_key(&ev.key.keysym); if (evdev != 0) ciokey_gotevent(evdev, false); @@ -1383,26 +1411,6 @@ void sdl_video_event_thread(void *data) ((km & KMOD_CTRL) ? CIOLIB_KMOD_CTRL : 0) | ((km & KMOD_ALT) ? CIOLIB_KMOD_ALT : 0); pthread_once(&ciolib_mouse_initialized, init_mouse); - if (sdl_mouse_exclusive) { - if (!sdl_rel_centred) { - /* Start from the middle so the - * first report is not a jump. */ - sdl_rel_x = cvstat.scrnwidth / 2; - sdl_rel_y = cvstat.scrnheight / 2; - sdl_rel_centred = 1; - } - /* Captured: report where the movement has - * carried us, not where the pointer is -- - * the pointer is not going anywhere. */ - sdl_rel_x += ev.motion.xrel; - sdl_rel_y += ev.motion.yrel; - ciomouse_gotevent(CIOLIB_MOUSE_MOVE, - win_to_text_xpos(sdl_rel_x, &cvstat), - win_to_text_ypos(sdl_rel_y, &cvstat), - win_to_res_xpos(sdl_rel_x, &cvstat), - win_to_res_ypos(sdl_rel_y, &cvstat), mods); - break; - } ciomouse_gotevent(CIOLIB_MOUSE_MOVE,win_to_text_xpos(ev.motion.x, &cvstat),win_to_text_ypos(ev.motion.y, &cvstat), win_to_res_xpos(ev.motion.x, &cvstat), win_to_res_ypos(ev.motion.y, &cvstat), mods); break; case SDL_MOUSEBUTTONDOWN: @@ -1411,6 +1419,13 @@ void sdl_video_event_thread(void *data) ((km & KMOD_CTRL) ? CIOLIB_KMOD_CTRL : 0) | ((km & KMOD_ALT) ? CIOLIB_KMOD_ALT : 0); pthread_once(&ciolib_mouse_initialized, init_mouse); + if (sdl_mouse_exclusive_want && !sdl_mouse_exclusive) { + /* Released with Ctrl+Alt: this click takes it back. */ + if (sdl_mouse_grab(1)) { + sdl_swallow_buttons |= 1 << (ev.button.button & 31); + break; + } + } switch(ev.button.button) { case SDL_BUTTON_LEFT: ciomouse_gotevent(CIOLIB_BUTTON_PRESS(1),win_to_text_xpos(ev.button.x, &cvstat),win_to_text_ypos(ev.button.y, &cvstat), win_to_res_xpos(ev.button.x, &cvstat), win_to_res_ypos(ev.button.y, &cvstat), mods); @@ -1446,6 +1461,10 @@ void sdl_video_event_thread(void *data) ((km & KMOD_CTRL) ? CIOLIB_KMOD_CTRL : 0) | ((km & KMOD_ALT) ? CIOLIB_KMOD_ALT : 0); pthread_once(&ciolib_mouse_initialized, init_mouse); + if (sdl_swallow_buttons & (1 << (ev.button.button & 31))) { + sdl_swallow_buttons &= ~(1 << (ev.button.button & 31)); + break; + } switch(ev.button.button) { case SDL_BUTTON_LEFT: ciomouse_gotevent(CIOLIB_BUTTON_RELEASE(1),win_to_text_xpos(ev.button.x, &cvstat),win_to_text_ypos(ev.button.y, &cvstat), win_to_res_xpos(ev.button.x, &cvstat), win_to_res_ypos(ev.button.y, &cvstat), mods); diff --git a/src/conio/sdlfuncs.c b/src/conio/sdlfuncs.c index 4b06946a..7b7a4115 100644 --- a/src/conio/sdlfuncs.c +++ b/src/conio/sdlfuncs.c @@ -98,6 +98,7 @@ int load_sdl_funcs(struct sdlfuncs *sdlf) * run at all, but a terminal without pointer capture is still a * terminal. The caller checks for NULL and does without. */ sdlf->SetRelativeMouseMode=xp_dlsym(sdl_dll, SDL_SetRelativeMouseMode); + sdlf->SetWindowGrab=xp_dlsym(sdl_dll, SDL_SetWindowGrab); /* Not fatal if missing: without it the upscale filter stays whatever * the texture-creation hint selected (SDL older than 2.0.12). */ sdlf->SetTextureScaleMode=xp_dlsym(sdl_dll, SDL_SetTextureScaleMode); diff --git a/src/conio/sdlfuncs.h b/src/conio/sdlfuncs.h index 10d767b8..31e63088 100644 --- a/src/conio/sdlfuncs.h +++ b/src/conio/sdlfuncs.h @@ -32,6 +32,9 @@ struct sdlfuncs { * the cursor is hidden, locked to the window, and motion arrives * as deltas that do not stop at the edge of the screen. */ int (HACK_HACK_HACK *SetRelativeMouseMode) (SDL_bool enabled); + /* Exclusive mouse proper: the pointer stays VISIBLE and is held + * inside the window. Relative mode above hides it. */ + void (HACK_HACK_HACK *SetWindowGrab) (SDL_Window *window, SDL_bool grabbed); Uint32 (HACK_HACK_HACK *WasInit) (Uint32 flags); SDL_bool (HACK_HACK_HACK *GetWindowWMInfo) (SDL_Window *window, SDL_SysWMinfo *info); const char *(HACK_HACK_HACK *GetError) (void); diff --git a/src/syncterm/menu_settings.c b/src/syncterm/menu_settings.c index 01a634f4..bedb8e87 100644 --- a/src/syncterm/menu_settings.c +++ b/src/syncterm/menu_settings.c @@ -101,6 +101,8 @@ set_ini_values(str_list_t *ini, const struct syncterm_settings *set) &ini_style); iniSetBool(ini, "SyncTERM", "GPUAccelerated", set->gpu_accel, &ini_style); + iniSetBool(ini, "SyncTERM", "ExclusiveMouse", set->exclusive_mouse, + &ini_style); iniSetBool(ini, "SyncTERM", "InvertMouseWheel", set->invert_wheel, &ini_style); iniSetString(ini, "SyncTERM", "KeyDerivationIterations", diff --git a/src/syncterm/scripts/menu_settings_ui.wren b/src/syncterm/scripts/menu_settings_ui.wren index 3e62ab37..40a4e8b7 100644 --- a/src/syncterm/scripts/menu_settings_ui.wren +++ b/src/syncterm/scripts/menu_settings_ui.wren @@ -499,6 +499,8 @@ class SettingsMenu { helpItem_("Invert Mouse Wheel", "Reverse wheel-up and wheel-down") + helpItem_("GPU Accelerated", "Use SDL/OpenGL; Pointy runs its edge-aware scaler on the GPU. Restart when enabling") + + helpItem_("Exclusive Mouse Mode", + "Capture the mouse in every session; press Ctrl+Alt together to release it to the OS, click to take it back") + helpItem_("Key Derivation Shift", "The scrypt cost used for list encryption") + helpItem_("Classic Theme Colours", "The colours used by the shipped menu and file picker") @@ -741,6 +743,7 @@ class SettingsMenu { rows.add([14, settingsLine_("Scaling", Menu.scalingModes[s.scalingMode])]) rows.add([15, settingsLine_("Invert Mouse Wheel", yesNo_(s.invertWheel))]) rows.add([16, settingsLine_("GPU Accelerated", yesNo_(s.gpuAccelerated))]) + rows.add([23, settingsLine_("Exclusive Mouse Mode", yesNo_(s.exclusiveMouse))]) rows.add([17, settingsLine_("Key Derivation Shift", s.kdfShift)]) rows.add([18, "Classic Theme Colours"]) if (!connected) rows.add([19, "Custom Screen Mode"]) @@ -814,6 +817,8 @@ class SettingsMenu { s.invertWheel = !s.invertWheel } else if (value == 16) { s.gpuAccelerated = !s.gpuAccelerated + } else if (value == 23) { + s.exclusiveMouse = !s.exclusiveMouse } else if (value == 17) { var next = MenuUi.integer(app, "Key Derivation Work Factor", "Exponent N for scrypt-Nn", s.kdfShift, 8, 20, kdfHelp_()) diff --git a/src/syncterm/scripts/syncterm_menu.wren b/src/syncterm/scripts/syncterm_menu.wren index b834a102..4f766d14 100644 --- a/src/syncterm/scripts/syncterm_menu.wren +++ b/src/syncterm/scripts/syncterm_menu.wren @@ -198,6 +198,8 @@ foreign class Settings { foreign invertWheel=(value) foreign gpuAccelerated foreign gpuAccelerated=(value) + foreign exclusiveMouse + foreign exclusiveMouse=(value) foreign modemDevice foreign modemDevice=(value) foreign modemInit diff --git a/src/syncterm/syncterm.c b/src/syncterm/syncterm.c index 82a8c8f8..6e67be4f 100644 --- a/src/syncterm/syncterm.c +++ b/src/syncterm/syncterm.c @@ -1938,6 +1938,7 @@ load_settings(struct syncterm_settings *set) set->blocky = iniReadBool(inifile, "SyncTERM", "BlockyScaling", true); set->extern_scale = iniReadBool(inifile, "SyncTERM", "ExternalScaling", false); set->gpu_accel = iniReadBool(inifile, "SyncTERM", "GPUAccelerated", false); + set->exclusive_mouse = iniReadBool(inifile, "SyncTERM", "ExclusiveMouse", false); set->invert_wheel = iniReadBool(inifile, "SyncTERM", "InvertMouseWheel", false); set->defaultCursor = iniReadEnum(inifile, "SyncTERM", "DefaultCursor", cursor_enum, ST_CT_DEFAULT); diff --git a/src/syncterm/syncterm.h b/src/syncterm/syncterm.h index d28337d6..f494e5ab 100644 --- a/src/syncterm/syncterm.h +++ b/src/syncterm/syncterm.h @@ -92,6 +92,7 @@ struct syncterm_settings { int blocky; int extern_scale; int gpu_accel; + bool exclusive_mouse; uint audio_output_modes; bool invert_wheel; bool webgetUserList; diff --git a/src/syncterm/term.c b/src/syncterm/term.c index c26625d7..66c8191d 100644 --- a/src/syncterm/term.c +++ b/src/syncterm/term.c @@ -5037,11 +5037,10 @@ doterm(struct bbslist *bbs) int ooii_mode = 0; bool ret = false; - /* Exclusive mouse, if this entry asked for it. The pointer is - * captured to the window and reports motion instead of position -- - * which is what a game needs and what a terminal has never given - * one, and a nuisance everywhere else, so it is per entry. */ - if (bbs != NULL && bbs->exclusive_mouse) + /* Exclusive mouse, if this entry or Program Settings asked for it. + * The pointer is held inside the window, so a game's mouse cannot + * wander onto the desktop mid-turn. Ctrl+Alt hands it back to the OS. */ + if ((bbs != NULL && bbs->exclusive_mouse) || settings.exclusive_mouse) ciolib_set_mouse_exclusive(1); recv_byte_buffer_len = recv_byte_buffer_pos = 0; wire_buffer_len = wire_buffer_pos = 0; @@ -5539,8 +5538,13 @@ doterm(struct bbslist *bbs) savscrn = cp437_savescrn(); bool menu_was_suspended = cterm_suspended; cterm_suspended = true; + /* The menu wants a pointer it can see. */ + ciolib_set_mouse_exclusive(0); wren_menu_host_run(bbs->name, true); cterm_suspended = menu_was_suspended; + /* ...and the setting may have changed in it. */ + if (bbs->exclusive_mouse || settings.exclusive_mouse) + ciolib_set_mouse_exclusive(1); sprintf(title, "SyncTERM - %s\n", bbs->name); settitle(title); setup_mouse_events(&ms); diff --git a/src/syncterm/wren_bind_menu_settings.c b/src/syncterm/wren_bind_menu_settings.c index 1b5ddc41..3970f23d 100644 --- a/src/syncterm/wren_bind_menu_settings.c +++ b/src/syncterm/wren_bind_menu_settings.c @@ -213,6 +213,7 @@ SETTINGS_BOOL_PROPERTY(confirmClose, confirm_close) SETTINGS_BOOL_PROPERTY(promptSave, prompt_save) SETTINGS_BOOL_PROPERTY(invertWheel, invert_wheel) SETTINGS_BOOL_PROPERTY(gpuAccelerated, gpu_accel) +SETTINGS_BOOL_PROPERTY(exclusiveMouse, exclusive_mouse) SETTINGS_STRING_PROPERTY(modemDevice, mdm.device_name) SETTINGS_STRING_PROPERTY(modemInit, mdm.init_string) SETTINGS_STRING_PROPERTY(modemDial, mdm.dial_string) @@ -1679,6 +1680,7 @@ static const struct binding bindings[] = { SETTINGS_GETSET(promptSave), SETTINGS_GETSET(invertWheel), SETTINGS_GETSET(gpuAccelerated), + SETTINGS_GETSET(exclusiveMouse), SETTINGS_GETSET(modemDevice), SETTINGS_GETSET(modemInit), SETTINGS_GETSET(modemDial), -- 2.47.3