From b418265b31cbc7710df7d46177426783734b7b32 Mon Sep 17 00:00:00 2001 From: ANSI TV Admin Date: Wed, 11 Nov 2026 12:52:06 -0800 Subject: [PATCH 18/24] Exclusive mouse: a coloured frame round the screen while captured While the pointer is captured (Exclusive Mouse Mode, per entry or global, or a game's CSI = 31 h) and the window has focus, the SDL output draws a frame round the picture, so the user can see the mouse is held. It goes the moment the capture ends (Ctrl+Alt, focus loss, session end) and comes back with it (click, focus). The frame never covers the terminal: the picture keeps its size when the window's margin has room (maximised, fullscreen, letterboxed), and otherwise shrinks just enough to clear it. Drawing and pointer mapping share one helper (sdl_picture_size), so clicks still land on the cell under the pointer. With the frame off the layout is the old one. Program Settings gains "Exclusive Mouse Border" (Off/2/4/6/8 px, default 4; ExclusiveMouseBorder) and "Exclusive Border Colour" (eight colours, default Cyan; ExclusiveMouseBorderColour). GDI output has no exclusive mode, so it draws no frame. Co-Authored-By: Claude Opus 5.5 --- src/conio/ciolib.h | 6 + src/conio/sdl_con.c | 145 ++++++++++++++++++++- src/conio/sdlfuncs.c | 3 + src/conio/sdlfuncs.h | 4 + src/syncterm/menu_settings.c | 5 + src/syncterm/scripts/menu_settings_ui.wren | 18 +++ src/syncterm/scripts/syncterm_menu.wren | 4 + src/syncterm/syncterm.c | 31 +++++ src/syncterm/syncterm.h | 4 + src/syncterm/wren_bind_menu_settings.c | 4 + 10 files changed, 220 insertions(+), 4 deletions(-) diff --git a/src/conio/ciolib.h b/src/conio/ciolib.h index cfc2365a..a4e9fa70 100644 --- a/src/conio/ciolib.h +++ b/src/conio/ciolib.h @@ -924,5 +924,11 @@ 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); +/* A coloured frame round the picture while the mouse is captured + * (exclusive mode) and the window has focus: width in window pixels + * (0 = off) and colour as 0xRRGGBB. Program Settings "Exclusive Mouse + * Border". The picture is never covered: it keeps its size when the + * window margin has room, else shrinks just enough to clear the frame. */ +CIOLIBEXPORT void ciolib_set_exclusive_border(int px, uint32_t rgb); #endif /* Do not add anything after this line */ diff --git a/src/conio/sdl_con.c b/src/conio/sdl_con.c index 81788c33..48335b0d 100644 --- a/src/conio/sdl_con.c +++ b/src/conio/sdl_con.c @@ -87,6 +87,17 @@ 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 */ +/* + * Exclusive-mode border -- see ciolib_set_exclusive_border(). Shown while + * the pointer is captured AND the window has focus; sdl_xborder_cur is the + * width the layout (picture size, pointer mapping, frame) uses right now. + * Event thread only, except the settings, which are plain ints. + */ +static int sdl_xborder_px = 4; /* configured width, 0 = off */ +static uint32_t sdl_xborder_rgb = 0x00FFFF; /* configured colour */ +static int sdl_focused = 1; /* window has keyboard focus */ +static int sdl_xborder_cur = 0; /* width in force */ + unsigned char sdl_key=0; /* Index into keybuf for next key in buffer */ unsigned char sdl_keynext=0; /* Index into keybuf for next free position */ @@ -941,6 +952,122 @@ ciolib_set_mouse_edge_motion(int on) sdl_update_relative(); } +/* Recompute whether the frame shows; on a change, repaint everything so + * the picture re-lays out (and the pointer mapping follows). */ +static void +sdl_xborder_refresh(void) +{ + int b = 0; + + if (sdl_xborder_px > 0 && sdl_mouse_exclusive && sdl_focused) + b = sdl_xborder_px; + if (b != sdl_xborder_cur) { + sdl_xborder_cur = b; + bitmap_drv_request_pixels(); + } +} + +CIOLIBEXPORT void +ciolib_set_exclusive_border(int px, uint32_t rgb) +{ + int recolour; + + if (px < 0) + px = 0; + if (px > 32) + px = 32; + recolour = (sdl_xborder_rgb != (rgb & 0xFFFFFF)); + sdl_xborder_px = px; + sdl_xborder_rgb = rgb & 0xFFFFFF; + sdl_xborder_refresh(); + if (recolour && sdl_xborder_cur > 0) + bitmap_drv_request_pixels(); +} + +/* + * The picture's size in the window. With no frame this is exactly the + * old computation (fit: sized to the window, for drawing; !fit: the + * nominal scaled size, for pointer mapping). With the frame, the picture + * keeps its size when the window margin already has room for it (a + * letterboxed, maximised or fullscreen window), and otherwise shrinks + * just enough to leave it, so the frame never covers terminal pixels. + * Drawing and pointer mapping both use this, so they always agree. + */ +static void +sdl_picture_size(struct video_stats *vs, int *w, int *h, bool fit) +{ + int b = sdl_xborder_cur; + int pw, ph, aw, ah; + double f; + + if (b <= 0) { + bitmap_get_scaled_win_size(vs->scaling, w, h, + fit ? vs->winwidth : 0, fit ? vs->winheight : 0); + return; + } + bitmap_get_scaled_win_size(vs->scaling, &pw, &ph, vs->winwidth, vs->winheight); + aw = vs->winwidth - 2 * b; + ah = vs->winheight - 2 * b; + if (aw < 1 || ah < 1 || pw <= 0 || ph <= 0 || (pw <= aw && ph <= ah)) { + *w = pw; + *h = ph; + return; + } + f = fmin((double)aw / pw, (double)ah / ph); + *w = (int)floor(pw * f); + *h = (int)floor(ph * f); + if (*w < 1) + *w = 1; + if (*h < 1) + *h = 1; +} + +/* Draw the frame round a centred pw x ph picture. Called right after + * RenderClear, so the picture (drawn after) is never under it. */ +static void +sdl_draw_xborder(int winw, int winh, int pw, int ph) +{ + int b = sdl_xborder_cur; + int x, y, w, h; + Uint8 or, og, ob, oa; + SDL_Rect r[4]; + + if (b <= 0 || renderer == NULL || sdl.SetRenderDrawColor == NULL + || sdl.GetRenderDrawColor == NULL || sdl.RenderFillRects == NULL) + return; + x = (winw - pw) / 2 - b; + y = (winh - ph) / 2 - b; + w = pw + 2 * b; + h = ph + 2 * b; + if (x < 0) { + w += x; + x = 0; + } + if (y < 0) { + h += y; + y = 0; + } + if (x + w > winw) + w = winw - x; + if (y + h > winh) + h = winh - y; + if (w <= 0 || h <= 0) + return; + r[0] = (SDL_Rect){x, y, w, b}; + r[1] = (SDL_Rect){x, y + h - b, w, b}; + r[2] = (SDL_Rect){x, y, b, h}; + r[3] = (SDL_Rect){x + w - b, y, b, h}; + if (sdl.GetRenderDrawColor(renderer, &or, &og, &ob, &oa) != 0) { + or = og = ob = 0; + oa = 255; + } + sdl.SetRenderDrawColor(renderer, (sdl_xborder_rgb >> 16) & 0xFF, + (sdl_xborder_rgb >> 8) & 0xFF, sdl_xborder_rgb & 0xFF, 255); + sdl.RenderFillRects(renderer, r, 4); + /* RenderClear uses the draw colour: put it back for the next frame. */ + sdl.SetRenderDrawColor(renderer, or, og, ob, oa); +} + static int sdl_mouse_grab(int on) { @@ -957,6 +1084,7 @@ sdl_mouse_grab(int on) assert_pthread_mutex_unlock(&win_mutex); sdl_mouse_exclusive = on ? 1 : 0; sdl_update_relative(); + sdl_xborder_refresh(); return 1; } @@ -1076,7 +1204,7 @@ sdl_virt_move(int xrel, int yrel, struct video_stats *vs) { int w, h, xoff, yoff, x; - bitmap_get_scaled_win_size(vs->scaling, &w, &h, 0, 0); + sdl_picture_size(vs, &w, &h, false); if (w <= 0 || h <= 0) return; xoff = (vs->winwidth - w) / 2; @@ -1269,7 +1397,7 @@ static int win_to_res_xpos(int winpos, struct video_stats *vs) int ret; int w, h; - bitmap_get_scaled_win_size(vs->scaling, &w, &h, 0, 0); + sdl_picture_size(vs, &w, &h, false); #ifdef __DARWIN__ winpos = winpos * vs->winwidth / mac_width; #endif @@ -1291,7 +1419,7 @@ static int win_to_res_ypos(int winpos, struct video_stats *vs) int ret; int w, h; - bitmap_get_scaled_win_size(vs->scaling, &w, &h, 0, 0); + sdl_picture_size(vs, &w, &h, false); #ifdef __DARWIN__ winpos = winpos * vs->winheight / mac_height; #endif @@ -1624,6 +1752,12 @@ void sdl_video_event_thread(void *data) break; case SDL_WINDOWEVENT_FOCUS_LOST: ciokey_focus_lost(); + sdl_focused = 0; + sdl_xborder_refresh(); + break; + case SDL_WINDOWEVENT_FOCUS_GAINED: + sdl_focused = 1; + sdl_xborder_refresh(); break; } break; @@ -1646,7 +1780,7 @@ void sdl_video_event_thread(void *data) // Get correct aspect ratio for dst... int sw = cvstat.scrnwidth; int sh = cvstat.scrnheight; - bitmap_get_scaled_win_size(cvstat.scaling, &dst.w, &dst.h, cvstat.winwidth, cvstat.winheight); + sdl_picture_size(&cvstat, &dst.w, &dst.h, true); assert_rwlock_unlock(&vstatlock); assert_pthread_mutex_lock(&win_mutex); if (win != NULL) { @@ -1677,6 +1811,7 @@ void sdl_video_event_thread(void *data) if (sdl.GetRendererInfo && sdl.RenderFlush && sdl.GL_GetProcAddress && sdl.GetRendererInfo(renderer,&info)==0 && strcmp(info.name,"opengl")==0) { sdl.RenderClear(renderer); + sdl_draw_xborder(cvstat.winwidth, cvstat.winheight, dst.w, dst.h); if (sdl.RenderFlush(renderer)==0) { ok=gpu_pointy_init(sdl.GL_GetProcAddress) && gpu_pointy_scale(list->data,sw,sh,dst.w,dst.h) @@ -1712,6 +1847,7 @@ void sdl_video_event_thread(void *data) if (sdl.QueryTexture(texture, NULL, NULL, &tw, &th) != 0) fprintf(stderr, "Unable to query texture (%s)\n", sdl.GetError()); sdl.RenderClear(renderer); + sdl_draw_xborder(cvstat.winwidth, cvstat.winheight, gb->w, gb->h); if (sdl.LockTexture(texture, &src, &pixels, &pitch) != 0) fprintf(stderr, "Unable to lock texture (%s)\n", sdl.GetError()); if (pitch != gb->w * sizeof(gb->data[0])) { @@ -1766,6 +1902,7 @@ void sdl_video_event_thread(void *data) if (src.h > th) src.h = th; sdl.RenderClear(renderer); + sdl_draw_xborder(cvstat.winwidth, cvstat.winheight, dst.w, dst.h); if (sdl.LockTexture(texture, &src, &pixels, &pitch) != 0) fprintf(stderr, "Unable to lock texture (%s)\n", sdl.GetError()); if (ts > 1) { diff --git a/src/conio/sdlfuncs.c b/src/conio/sdlfuncs.c index bda30894..a973f699 100644 --- a/src/conio/sdlfuncs.c +++ b/src/conio/sdlfuncs.c @@ -107,6 +107,9 @@ int load_sdl_funcs(struct sdlfuncs *sdlf) sdlf->GetRendererInfo=xp_dlsym(sdl_dll, SDL_GetRendererInfo); sdlf->ShowSimpleMessageBox=xp_dlsym(sdl_dll, SDL_ShowSimpleMessageBox); sdlf->ShowWindow=xp_dlsym(sdl_dll, SDL_ShowWindow); + sdlf->SetRenderDrawColor=xp_dlsym(sdl_dll, SDL_SetRenderDrawColor); + sdlf->GetRenderDrawColor=xp_dlsym(sdl_dll, SDL_GetRenderDrawColor); + sdlf->RenderFillRects=xp_dlsym(sdl_dll, SDL_RenderFillRects); if((sdlf->WasInit=xp_dlsym(sdl_dll, SDL_WasInit))==NULL) { xp_dlclose(sdl_dll); return(-1); diff --git a/src/conio/sdlfuncs.h b/src/conio/sdlfuncs.h index d84b0728..f8d4f037 100644 --- a/src/conio/sdlfuncs.h +++ b/src/conio/sdlfuncs.h @@ -56,6 +56,10 @@ struct sdlfuncs { /* NULL-tolerant: the window is created hidden and shown once it is * final; without this the caller creates it visible as before. */ void (HACK_HACK_HACK *ShowWindow)(SDL_Window *window); + /* NULL-tolerant: the exclusive-mode border is skipped without them. */ + int (HACK_HACK_HACK *SetRenderDrawColor)(SDL_Renderer *renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + int (HACK_HACK_HACK *GetRenderDrawColor)(SDL_Renderer *renderer, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a); + int (HACK_HACK_HACK *RenderFillRects)(SDL_Renderer *renderer, const SDL_Rect *rects, int count); SDL_bool (HACK_HACK_HACK *SetHint) (const char *name, const char *value); const char * (HACK_HACK_HACK *GetHint) (const char *name); SDL_Keymod (HACK_HACK_HACK *GetModState) (void); diff --git a/src/syncterm/menu_settings.c b/src/syncterm/menu_settings.c index 3e10d3e7..a54cef1e 100644 --- a/src/syncterm/menu_settings.c +++ b/src/syncterm/menu_settings.c @@ -105,6 +105,10 @@ set_ini_values(str_list_t *ini, const struct syncterm_settings *set) &ini_style); iniSetBool(ini, "SyncTERM", "MouseEdgeMotion", set->mouse_edge_motion, &ini_style); + iniSetInteger(ini, "SyncTERM", "ExclusiveMouseBorder", set->exclusive_border, + &ini_style); + iniSetInteger(ini, "SyncTERM", "ExclusiveMouseBorderColour", + set->exclusive_border_colour, &ini_style); iniSetBool(ini, "SyncTERM", "InvertMouseWheel", set->invert_wheel, &ini_style); iniSetString(ini, "SyncTERM", "KeyDerivationIterations", @@ -153,6 +157,7 @@ apply_settings(const struct syncterm_settings *set) resolve_list_path(&settings); xpbeep_sound_devices_enabled = settings.audio_output_modes; ciolib_swap_mouse_butt45 = false; /* applied to host reports in term.c */ + apply_exclusive_border(); if (settings.blocky) cio_api.options |= CONIO_OPT_BLOCKY_SCALING; else diff --git a/src/syncterm/scripts/menu_settings_ui.wren b/src/syncterm/scripts/menu_settings_ui.wren index 8b3359d6..a146c769 100644 --- a/src/syncterm/scripts/menu_settings_ui.wren +++ b/src/syncterm/scripts/menu_settings_ui.wren @@ -460,6 +460,11 @@ class SettingsMenu { static yesNo_(value) { value ? "Yes" : "No" } + // Same order as exclusive_border_colours[] in syncterm.c. + static borderColours_ { + return ["Cyan", "Magenta", "Yellow", "Green", "Orange", "Red", "White", "Blue"] + } + static audioSummary_(s) { var names = [] for (row in Menu.audioModes) { @@ -501,6 +506,9 @@ class SettingsMenu { "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 Border", + "A coloured frame round the screen while the mouse is captured; Off, 2, 4, 6 or 8 pixels") + + helpItem_("Exclusive Border Colour", "The colour of that frame") + 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") + @@ -747,6 +755,10 @@ class SettingsMenu { 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([25, settingsLine_("Exclusive Mouse Border", + s.exclusiveBorder == 0 ? "Off" : "%(s.exclusiveBorder) px")]) + rows.add([26, settingsLine_("Exclusive Border Colour", + borderColours_[s.exclusiveBorderColour])]) rows.add([17, settingsLine_("Key Derivation Shift", s.kdfShift)]) rows.add([18, "Classic Theme Colours"]) if (!connected) rows.add([19, "Custom Screen Mode"]) @@ -824,6 +836,12 @@ class SettingsMenu { s.exclusiveMouse = !s.exclusiveMouse } else if (value == 24) { s.mouseEdgeMotion = !s.mouseEdgeMotion + } else if (value == 25) { + var widths = [0, 2, 4, 6, 8] + var i = widths.indexOf(s.exclusiveBorder) + s.exclusiveBorder = widths[(i + 1) % widths.count] + } else if (value == 26) { + s.exclusiveBorderColour = (s.exclusiveBorderColour + 1) % borderColours_.count } 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 b8366ff3..8c6fd562 100644 --- a/src/syncterm/scripts/syncterm_menu.wren +++ b/src/syncterm/scripts/syncterm_menu.wren @@ -202,6 +202,10 @@ foreign class Settings { foreign exclusiveMouse=(value) foreign mouseEdgeMotion foreign mouseEdgeMotion=(value) + foreign exclusiveBorder + foreign exclusiveBorder=(value) + foreign exclusiveBorderColour + foreign exclusiveBorderColour=(value) foreign modemDevice foreign modemDevice=(value) foreign modemInit diff --git a/src/syncterm/syncterm.c b/src/syncterm/syncterm.c index c1765453..e2096558 100644 --- a/src/syncterm/syncterm.c +++ b/src/syncterm/syncterm.c @@ -1940,6 +1940,12 @@ load_settings(struct syncterm_settings *set) 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->exclusive_border = iniReadInteger(inifile, "SyncTERM", "ExclusiveMouseBorder", 4); + if (set->exclusive_border < 0 || set->exclusive_border > 16) + set->exclusive_border = 4; + set->exclusive_border_colour = iniReadInteger(inifile, "SyncTERM", "ExclusiveMouseBorderColour", 0); + if (set->exclusive_border_colour < 0 || set->exclusive_border_colour > 7) + set->exclusive_border_colour = 0; set->invert_wheel = iniReadBool(inifile, "SyncTERM", "InvertMouseWheel", false); set->defaultCursor = iniReadEnum(inifile, "SyncTERM", "DefaultCursor", cursor_enum, ST_CT_DEFAULT); @@ -2571,6 +2577,7 @@ main(int argc, char **argv) } if (initciolib(ciolib_mode)) return 1; + apply_exclusive_border(); setname(settings.gpu_accel ? "SyncTERM [GPU]" : "SyncTERM"); if (settings.blocky) cio_api.options |= CONIO_OPT_BLOCKY_SCALING; @@ -2922,6 +2929,30 @@ USAGE: return 0; } +/* Program Settings "Exclusive Mouse Border" colours, in the order the + * settings menu lists them (menu_settings_ui.wren borderColours_). */ +static const uint32_t exclusive_border_colours[8] = { + 0x00FFFF, /* Cyan */ + 0xFF00FF, /* Magenta */ + 0xFFFF00, /* Yellow */ + 0x00FF00, /* Green */ + 0xFF8000, /* Orange */ + 0xFF0000, /* Red */ + 0xFFFFFF, /* White */ + 0x3070FF, /* Blue */ +}; + +void +apply_exclusive_border(void) +{ + int c = settings.exclusive_border_colour; + + if (c < 0 || c > 7) + c = 0; + ciolib_set_exclusive_border(settings.exclusive_border, + exclusive_border_colours[c]); +} + int screen_to_ciolib(int screen) { diff --git a/src/syncterm/syncterm.h b/src/syncterm/syncterm.h index 1d4b7d2b..7daefba4 100644 --- a/src/syncterm/syncterm.h +++ b/src/syncterm/syncterm.h @@ -94,6 +94,8 @@ struct syncterm_settings { int gpu_accel; bool exclusive_mouse; bool mouse_edge_motion; + int exclusive_border; /* frame px while mouse captured, 0 = off */ + int exclusive_border_colour; /* index into exclusive_border_rgb() */ uint audio_output_modes; bool invert_wheel; bool webgetUserList; @@ -152,5 +154,7 @@ void set_default_cursor(void); * drains the queue and displays them on the UI thread. */ void popup_queue_post(const char *title, const char *body); bool popup_queue_drain(void); +/* Push the Exclusive Mouse Border setting to ciolib. */ +void apply_exclusive_border(void); #endif // ifndef _SYNCTERM_H_ diff --git a/src/syncterm/wren_bind_menu_settings.c b/src/syncterm/wren_bind_menu_settings.c index acadaead..3449310d 100644 --- a/src/syncterm/wren_bind_menu_settings.c +++ b/src/syncterm/wren_bind_menu_settings.c @@ -215,6 +215,8 @@ 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_NUM_PROPERTY(exclusiveBorder, exclusive_border, 0, 16) +SETTINGS_NUM_PROPERTY(exclusiveBorderColour, exclusive_border_colour, 0, 7) SETTINGS_STRING_PROPERTY(modemDevice, mdm.device_name) SETTINGS_STRING_PROPERTY(modemInit, mdm.init_string) SETTINGS_STRING_PROPERTY(modemDial, mdm.dial_string) @@ -1683,6 +1685,8 @@ static const struct binding bindings[] = { SETTINGS_GETSET(gpuAccelerated), SETTINGS_GETSET(exclusiveMouse), SETTINGS_GETSET(mouseEdgeMotion), + SETTINGS_GETSET(exclusiveBorder), + SETTINGS_GETSET(exclusiveBorderColour), SETTINGS_GETSET(modemDevice), SETTINGS_GETSET(modemInit), SETTINGS_GETSET(modemDial), -- 2.47.3