From 31659f1d209745de28dc629833c187a5da4f6e25 Mon Sep 17 00:00:00 2001 From: ANSI TV Admin Date: Wed, 11 Nov 2026 12:04:09 -0800 Subject: [PATCH 13/24] SyncTERM: CSI = 31 h/l game capture + Mouse Motion Past Edges Applies after `relative-look.patch`. `CSI = 31 h` lets a game capture the mouse, hide the pointer and get relative-look motion past the edges without the caller turning on Exclusive Mouse Mode; `CSI = 31 l` gives it back (DECRQM mode 31). Program Settings gains **Mouse Motion Past Edges** (`MouseEdgeMotion`, Yes by default): No ignores `CSI = 31 h` and also stops a captured, hidden pointer at the edge. One helper in term.c (`apply_mouse_capture`) decides grab, visibility and edge motion from the settings and both host modes; right-click paste and drag-select treat `=31h` as a game too. Verified under Xvfb with XTest relative motion into a host that logs its reports: game capture wraps the column (40 reports) with Exclusive off; setting No -> the pointer leaves the window; Exclusive on + setting No -> pinned at column 1; Ctrl+Alt releases, a click recaptures; `=31l` shows and frees the pointer. --- src/conio/ciolib.h | 3 ++ src/conio/cterm.adoc | 19 +++++++ src/conio/cterm.h | 8 +++ src/conio/cterm_cterm.c | 15 ++++++ src/conio/sdl_con.c | 15 +++++- 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 | 63 ++++++++++++++++------ src/syncterm/wren_bind_conn.c | 2 +- src/syncterm/wren_bind_menu_settings.c | 2 + 13 files changed, 119 insertions(+), 19 deletions(-) diff --git a/src/conio/ciolib.h b/src/conio/ciolib.h index 7ffd36f1..cfc2365a 100644 --- a/src/conio/ciolib.h +++ b/src/conio/ciolib.h @@ -921,5 +921,8 @@ CIOLIBEXPORT int ciolib_getkeyevent(struct cio_key_event *ev); * 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); +/* Whether a captured, hidden pointer reports motion past the window edges + * (relative look). Program Settings "Mouse Motion Past Edges". */ +CIOLIBEXPORT void ciolib_set_mouse_edge_motion(int on); #endif /* Do not add anything after this line */ diff --git a/src/conio/cterm.adoc b/src/conio/cterm.adoc index 44e8e140..b14448f9 100644 --- a/src/conio/cterm.adoc +++ b/src/conio/cterm.adoc @@ -936,6 +936,18 @@ while they are open. SOURCE: CTerm only. +=== `CSI = 31 h` Capture Mouse, Motion Past Edges +NON-STANDARD EXTENSION + +For a mouse-look game. Captures the mouse to the window, hides the +pointer and switches to relative motion: movement keeps being reported +past the window edges (the column wraps; the row stops at the edges). +Honoured only while the user allows it (Program Settings, "Mouse Motion +Past Edges", on by default). The user can always release the mouse with +Ctrl+Alt and take it back with a click. Undone by `CSI = 31 l` or when +the connection ends. + +SOURCE: CTerm only. + === `CSI = 4 h` Enable Last Column Flag (`CTELCF`) NON-STANDARD EXTENSION + Enable Last Column Flag mode @@ -1174,6 +1186,13 @@ Shows the terminal's mouse pointer again after `CSI = 30 h`. SOURCE: CTerm only. +=== `CSI = 31 l` Release Mouse Capture +NON-STANDARD EXTENSION + +Ends `CSI = 31 h`: the pointer is released and shown again (unless +`CSI = 30 h` still hides it or the user's own capture setting holds it). + +SOURCE: CTerm only. + === `CSI = 4 l` (`CTDLCF`) NON-STANDARD EXTENSION + Disable Last Column Flag mode diff --git a/src/conio/cterm.h b/src/conio/cterm.h index e30b87eb..59598358 100644 --- a/src/conio/cterm.h +++ b/src/conio/cterm.h @@ -215,6 +215,7 @@ struct cterminal { bool pk_mode; bool suppress_translated_keys; bool pointer_hidden; // CSI = 30 h: the host draws its own pointer, or none + bool mouse_capture; // CSI = 31 h: capture, hide, motion past the edges uint8_t pk_reported[CTERM_PK_MAX_EVDEV / 8]; int cursor; // Current cursor mode (Normal or None) char *fg_tc_str; @@ -267,6 +268,8 @@ struct cterminal { void *key_event_mode_change_cbdata; void (*pointer_hide_change)(int hide, void *cbdata); void *pointer_hide_change_cbdata; + void (*mouse_capture_change)(int on, void *cbdata); + void *mouse_capture_change_cbdata; int (*mouse_state_query)(int parameter, void *cbdata); void *mouse_state_query_cbdata; @@ -483,4 +486,9 @@ CIOLIBEXPORT void cterm_resize_rows(struct cterminal *cterm, int new_rows); } #endif +/* The host owns the pointer: it hid ours (CSI = 30 h) or captured the + * mouse for a game (CSI = 31 h). Such a host keeps every button. */ +#define CTERM_HOST_OWNS_POINTER(c) \ + ((c) != NULL && ((c)->pointer_hidden || (c)->mouse_capture)) + #endif diff --git a/src/conio/cterm_cterm.c b/src/conio/cterm_cterm.c index ccdebf4a..932e3dc3 100644 --- a/src/conio/cterm_cterm.c +++ b/src/conio/cterm_cterm.c @@ -159,6 +159,12 @@ cterm_handle_syncterm_sm_ext(struct cterminal *cterm, int *speed) /* CSI = h */ cterm->pointer_hide_change(1, cterm->pointer_hide_change_cbdata); break; + case 31: + cterm->mouse_capture = true; + if (cterm->mouse_capture_change != NULL) + cterm->mouse_capture_change(1, + cterm->mouse_capture_change_cbdata); + break; case 4: cterm->last_column_flag |= CTERM_LCF_ENABLED; cterm->last_column_flag &= ~CTERM_LCF_SET; @@ -198,6 +204,12 @@ cterm_handle_syncterm_rm_ext(struct cterminal *cterm, int *speed) /* CSI = l */ cterm->pointer_hide_change(0, cterm->pointer_hide_change_cbdata); break; + case 31: + cterm->mouse_capture = false; + if (cterm->mouse_capture_change != NULL) + cterm->mouse_capture_change(0, + cterm->mouse_capture_change_cbdata); + break; case 4: if ((cterm->last_column_flag & CTERM_LCF_FORCED) == 0) cterm->last_column_flag = 0; @@ -411,6 +423,9 @@ cterm_handle_decrqm_syncterm(struct cterminal *cterm, int *speed) /* CSI = $ p * case 30: pm = cterm->pointer_hidden ? 1 : 2; break; + case 31: + pm = cterm->mouse_capture ? 1 : 2; + break; case 4: pm = (cterm->last_column_flag & CTERM_LCF_ENABLED) ? 1 : 2; break; diff --git a/src/conio/sdl_con.c b/src/conio/sdl_con.c index a8afb9dc..bdcc7945 100644 --- a/src/conio/sdl_con.c +++ b/src/conio/sdl_con.c @@ -82,6 +82,7 @@ static int sdl_keyevents_wanted = 0; * moment the pointer is shown or let go. */ static int sdl_mouse_hidden = 0; /* ShowCursor(SDL_DISABLE) in force */ +static int sdl_mouse_edge = 1; /* "Mouse Motion Past Edges" allowed */ static int sdl_mouse_relative = 0; /* SDL relative mode on */ static int sdl_virt_x = -1, sdl_virt_y = -1; /* virtual pointer, window px */ static int sdl_last_x = 0, sdl_last_y = 0; /* last real pointer position */ @@ -890,11 +891,12 @@ ciolib_set_mouse_exclusive(int on) return sdl_mouse_grab(on); } -/* Relative look wanted iff captured and hidden. Event thread only. */ +/* Relative look wanted iff captured, hidden, and the user allows motion + * past the edges. Event thread only. */ static void sdl_update_relative(void) { - int want = (sdl_mouse_exclusive && sdl_mouse_hidden) ? 1 : 0; + int want = (sdl_mouse_exclusive && sdl_mouse_hidden && sdl_mouse_edge) ? 1 : 0; if (want == sdl_mouse_relative) return; @@ -914,6 +916,15 @@ sdl_update_relative(void) fprintf(stderr, "relative look: %s (virtual %d,%d)\n", want ? "on" : "off", sdl_virt_x, sdl_virt_y); } +/* Program Settings "Mouse Motion Past Edges": off, a captured pointer + * stops at the window edge even in a game. */ +CIOLIBEXPORT void +ciolib_set_mouse_edge_motion(int on) +{ + sdl_mouse_edge = on ? 1 : 0; + sdl_update_relative(); +} + static int sdl_mouse_grab(int on) { diff --git a/src/syncterm/menu_settings.c b/src/syncterm/menu_settings.c index bedb8e87..5aacb564 100644 --- a/src/syncterm/menu_settings.c +++ b/src/syncterm/menu_settings.c @@ -103,6 +103,8 @@ set_ini_values(str_list_t *ini, const struct syncterm_settings *set) &ini_style); iniSetBool(ini, "SyncTERM", "ExclusiveMouse", set->exclusive_mouse, &ini_style); + iniSetBool(ini, "SyncTERM", "MouseEdgeMotion", set->mouse_edge_motion, + &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 40a4e8b7..fb841275 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_("Mouse Motion Past Edges", + "Let a game (e.g. /quake) capture the mouse and keep turning past the window edges; Ctrl+Alt releases") + 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") + @@ -744,6 +746,7 @@ class SettingsMenu { 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([24, settingsLine_("Mouse Motion Past Edges", yesNo_(s.mouseEdgeMotion))]) rows.add([17, settingsLine_("Key Derivation Shift", s.kdfShift)]) rows.add([18, "Classic Theme Colours"]) if (!connected) rows.add([19, "Custom Screen Mode"]) @@ -819,6 +822,8 @@ class SettingsMenu { s.gpuAccelerated = !s.gpuAccelerated } else if (value == 23) { s.exclusiveMouse = !s.exclusiveMouse + } else if (value == 24) { + s.mouseEdgeMotion = !s.mouseEdgeMotion } 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 4f766d14..b8366ff3 100644 --- a/src/syncterm/scripts/syncterm_menu.wren +++ b/src/syncterm/scripts/syncterm_menu.wren @@ -200,6 +200,8 @@ foreign class Settings { foreign gpuAccelerated=(value) foreign exclusiveMouse foreign exclusiveMouse=(value) + foreign mouseEdgeMotion + foreign mouseEdgeMotion=(value) foreign modemDevice foreign modemDevice=(value) foreign modemInit diff --git a/src/syncterm/syncterm.c b/src/syncterm/syncterm.c index 6e67be4f..99440e4d 100644 --- a/src/syncterm/syncterm.c +++ b/src/syncterm/syncterm.c @@ -1939,6 +1939,7 @@ load_settings(struct syncterm_settings *set) 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->mouse_edge_motion = iniReadBool(inifile, "SyncTERM", "MouseEdgeMotion", true); 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 f494e5ab..1d4b7d2b 100644 --- a/src/syncterm/syncterm.h +++ b/src/syncterm/syncterm.h @@ -93,6 +93,7 @@ struct syncterm_settings { int extern_scale; int gpu_accel; bool exclusive_mouse; + bool mouse_edge_motion; uint audio_output_modes; bool invert_wheel; bool webgetUserList; diff --git a/src/syncterm/term.c b/src/syncterm/term.c index 5bde0344..4a36cc04 100644 --- a/src/syncterm/term.c +++ b/src/syncterm/term.c @@ -4155,17 +4155,46 @@ key_event_mode_change(int enable, void *cbdata) } #endif -/* CSI = 30 h / l: the host hides our pointer while it draws its own. */ +/* Who holds the mouse this session: the user's own capture (the entry's + * or Program Settings' Exclusive Mouse) or a game's CSI = 31 h, which the + * user allows with "Mouse Motion Past Edges". One place decides grab, + * pointer visibility and edge motion, so the host's two modes, the menu + * and the settings can never disagree. */ +static bool session_user_capture; + static void -pointer_hide_change(int hide, void *cbdata) +apply_mouse_capture(void) { - (void)cbdata; - if (hide) + bool game = cterm != NULL && cterm->mouse_capture + && settings.mouse_edge_motion; + + ciolib_set_mouse_edge_motion(settings.mouse_edge_motion); + ciolib_set_mouse_exclusive(session_user_capture || game); + if (cterm != NULL && (cterm->pointer_hidden || game)) hidemouse(); else showmouse(); } +/* CSI = 30 h / l: the host hides our pointer while it draws its own. + * CSI = 31 h / l: a game captures the mouse and wants motion past the + * edges. */ +static void +pointer_hide_change(int hide, void *cbdata) +{ + (void)hide; + (void)cbdata; + apply_mouse_capture(); +} + +static void +mouse_capture_change(int on, void *cbdata) +{ + (void)on; + (void)cbdata; + apply_mouse_capture(); +} + int mouse_state_query(int type, void *pms) { @@ -4789,7 +4818,7 @@ handle_mouse_event(struct mouse_state *ms) bool tracking = (ms->mode == MM_NORMAL_TRACKING || ms->mode == MM_BUTTON_EVENT_TRACKING || ms->mode == MM_ANY_EVENT_TRACKING) - && !(cterm != NULL && cterm->pointer_hidden); + && !CTERM_HOST_OWNS_POINTER(cterm); if (!tracking) sel_pending = 0; @@ -4837,7 +4866,7 @@ handle_mouse_event(struct mouse_state *ms) || ms->mode == MM_BUTTON_EVENT_TRACKING || ms->mode == MM_ANY_EVENT_TRACKING || ms->mode == MM_X10) - && !(cterm != NULL && cterm->pointer_hidden)) { + && !CTERM_HOST_OWNS_POINTER(cterm)) { switch (mevent.event) { case CIOLIB_BUTTON_3_PRESS: return; @@ -5123,7 +5152,10 @@ doterm(struct bbslist *bbs) /* 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) + session_user_capture = (bbs != NULL && bbs->exclusive_mouse) + || settings.exclusive_mouse; + ciolib_set_mouse_edge_motion(settings.mouse_edge_motion); + if (session_user_capture) ciolib_set_mouse_exclusive(1); recv_byte_buffer_len = recv_byte_buffer_pos = 0; wire_buffer_len = wire_buffer_pos = 0; @@ -5259,6 +5291,8 @@ doterm(struct bbslist *bbs) #endif cterm->pointer_hide_change = pointer_hide_change; cterm->pointer_hide_change_cbdata = cterm; + cterm->mouse_capture_change = mouse_capture_change; + cterm->mouse_capture_change_cbdata = cterm; cterm->status_display_type = bbs->nostatus ? 0 : 1; cterm->status_display_cb = on_status_display_change; cterm->status_display_cbdata = bbs; @@ -5627,9 +5661,9 @@ doterm(struct bbslist *bbs) 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); + /* ...and the settings may have changed in it. */ + session_user_capture = bbs->exclusive_mouse + || settings.exclusive_mouse; sprintf(title, "SyncTERM - %s\n", bbs->name); settitle(title); setup_mouse_events(&ms); @@ -5642,12 +5676,9 @@ doterm(struct bbslist *bbs) if (cterm->backpos > cterm->backlines) cterm->backpos = cterm->backlines; } - /* Back from the menu: the host may still be - * drawing its own pointer (CSI = 30 h). */ - if (cterm->pointer_hidden) - hidemouse(); - else - showmouse(); + /* Back from the menu: capture, pointer and + * edge motion as the host and settings say. */ + apply_mouse_capture(); _setcursortype(_NORMALCURSOR); #ifdef CIOLIB_KEY_EVENTS cterm_pk_resync_ciokey(cterm); diff --git a/src/syncterm/wren_bind_conn.c b/src/syncterm/wren_bind_conn.c index 186df592..1a9746cd 100644 --- a/src/syncterm/wren_bind_conn.c +++ b/src/syncterm/wren_bind_conn.c @@ -983,7 +983,7 @@ fn_Input_setupMouseEvents(WrenVM *vm) if (cterm == NULL || cterm->mouse_state_change_cbdata == NULL) return; setup_mouse_events(cterm->mouse_state_change_cbdata); - if (cterm->pointer_hidden) /* the host draws its own (CSI = 30 h) */ + if (CTERM_HOST_OWNS_POINTER(cterm)) /* CSI = 30 h / 31 h */ hidemouse(); else showmouse(); diff --git a/src/syncterm/wren_bind_menu_settings.c b/src/syncterm/wren_bind_menu_settings.c index 3970f23d..acadaead 100644 --- a/src/syncterm/wren_bind_menu_settings.c +++ b/src/syncterm/wren_bind_menu_settings.c @@ -214,6 +214,7 @@ 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_BOOL_PROPERTY(mouseEdgeMotion, mouse_edge_motion) SETTINGS_STRING_PROPERTY(modemDevice, mdm.device_name) SETTINGS_STRING_PROPERTY(modemInit, mdm.init_string) SETTINGS_STRING_PROPERTY(modemDial, mdm.dial_string) @@ -1681,6 +1682,7 @@ static const struct binding bindings[] = { SETTINGS_GETSET(invertWheel), SETTINGS_GETSET(gpuAccelerated), SETTINGS_GETSET(exclusiveMouse), + SETTINGS_GETSET(mouseEdgeMotion), SETTINGS_GETSET(modemDevice), SETTINGS_GETSET(modemInit), SETTINGS_GETSET(modemDial), -- 2.47.3