From 5312104e330f6424a14f14ec6508fcb7891a80db Mon Sep 17 00:00:00 2001 From: ANSI TV Admin Date: Wed, 11 Nov 2026 12:04:09 -0800 Subject: [PATCH 12/24] SyncTERM: relative look -- a captured, hidden pointer turns past the edge Captured AND hidden (CSI = 30 h, which a mouse-look game sends): a confined pointer stops at the window edge, and a stopped pointer sends nothing, so a turn ended where the window did. With the pointer hidden SDL is switched to relative motion and the reports come from a virtual pointer: it moves by the mouse's deltas, its column wraps at the edges (80 -> 1) and its row stops at them. Off again the moment the pointer is shown or let go. --- src/conio/sdl_con.c | 102 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 95 insertions(+), 7 deletions(-) diff --git a/src/conio/sdl_con.c b/src/conio/sdl_con.c index 8b67d1d4..a8afb9dc 100644 --- a/src/conio/sdl_con.c +++ b/src/conio/sdl_con.c @@ -69,6 +69,23 @@ static struct cio_key_event sdl_keyevents[256]; static unsigned char sdl_keyevent_first = 0; static unsigned char sdl_keyevent_next = 0; static int sdl_keyevents_wanted = 0; + +/* + * Relative look -- captured AND hidden (CSI = 30 h, which a mouse-look + * game sends). A confined pointer stops at the window edge, and a stopped + * pointer sends nothing, so a turn ended where the window did. With the + * pointer hidden there is nothing to see, so SDL is switched to relative + * motion and the reports come from a VIRTUAL pointer instead: it moves by + * the mouse's deltas, its column WRAPS at the edges (80 -> 1) and its row + * stops at them. The host sees a jump of most of the width and unwraps + * it; the turn carries on for as long as the hand moves. Off again the + * moment the pointer is shown or let go. + */ +static int sdl_mouse_hidden = 0; /* ShowCursor(SDL_DISABLE) in force */ +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 */ + 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 */ @@ -873,19 +890,46 @@ ciolib_set_mouse_exclusive(int on) return sdl_mouse_grab(on); } +/* Relative look wanted iff captured and hidden. Event thread only. */ +static void +sdl_update_relative(void) +{ + int want = (sdl_mouse_exclusive && sdl_mouse_hidden) ? 1 : 0; + + if (want == sdl_mouse_relative) + return; + if (sdl.SetRelativeMouseMode == NULL) + return; + if (want) { + sdl_virt_x = sdl_last_x; + sdl_virt_y = sdl_last_y; + } + if (sdl.SetRelativeMouseMode(want ? SDL_TRUE : SDL_FALSE) != 0) { + if (getenv("SYNCTERM_MOUSE_TRACE")) + fprintf(stderr, "relative look: SetRelativeMouseMode(%d) refused\n", want); + return; + } + sdl_mouse_relative = want; + if (getenv("SYNCTERM_MOUSE_TRACE")) + fprintf(stderr, "relative look: %s (virtual %d,%d)\n", want ? "on" : "off", sdl_virt_x, sdl_virt_y); +} + 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. */ + * anyway -- positions are clamped to the screen on the way out. + * (Relative motion is used only while the pointer is hidden as well; + * see sdl_update_relative.) */ if (sdl.SetWindowGrab == NULL || win == NULL) return 0; /* SDL too old; caller carries on without */ 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; + sdl_update_relative(); return 1; } @@ -998,6 +1042,34 @@ ciolib_getkeyevent(struct cio_key_event *ev) return got; } +/* The virtual pointer, moved by a relative-mode delta: the column wraps + * within the console's scaled width, the row stops at its edges. */ +static void +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); + if (w <= 0 || h <= 0) + return; + xoff = (vs->winwidth - w) / 2; + if (xoff < 0) + xoff = 0; + yoff = (vs->winheight - h) / 2; + if (yoff < 0) + yoff = 0; + x = sdl_virt_x - xoff + xrel; + x %= w; + if (x < 0) + x += w; + sdl_virt_x = xoff + x; + sdl_virt_y += yrel; + if (sdl_virt_y < yoff) + sdl_virt_y = yoff; + if (sdl_virt_y > yoff + h - 1) + sdl_virt_y = yoff + h - 1; +} + static void sdl_add_key(unsigned int keyval, struct video_stats *vs) { int w, h; @@ -1411,6 +1483,16 @@ 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 (getenv("SYNCTERM_MOUSE_TRACE")) + fprintf(stderr, "motion: %d,%d rel %d,%d relative=%d grabbed=%d hidden=%d\n", ev.motion.x, ev.motion.y, ev.motion.xrel, ev.motion.yrel, sdl_mouse_relative, sdl_mouse_exclusive, sdl_mouse_hidden); + if (sdl_mouse_relative) { + /* The virtual pointer moves; the real one is parked. */ + sdl_virt_move(ev.motion.xrel, ev.motion.yrel, &cvstat); + ciomouse_gotevent(CIOLIB_MOUSE_MOVE,win_to_text_xpos(sdl_virt_x, &cvstat),win_to_text_ypos(sdl_virt_y, &cvstat), win_to_res_xpos(sdl_virt_x, &cvstat), win_to_res_ypos(sdl_virt_y, &cvstat), mods); + break; + } + sdl_last_x = ev.motion.x; + sdl_last_y = ev.motion.y; 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: @@ -1431,13 +1513,13 @@ void sdl_video_event_thread(void *data) } 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); + ciomouse_gotevent(CIOLIB_BUTTON_PRESS(1),win_to_text_xpos((sdl_mouse_relative ? sdl_virt_x : ev.button.x), &cvstat),win_to_text_ypos((sdl_mouse_relative ? sdl_virt_y : ev.button.y), &cvstat), win_to_res_xpos((sdl_mouse_relative ? sdl_virt_x : ev.button.x), &cvstat), win_to_res_ypos((sdl_mouse_relative ? sdl_virt_y : ev.button.y), &cvstat), mods); break; case SDL_BUTTON_MIDDLE: - ciomouse_gotevent(CIOLIB_BUTTON_PRESS(2),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); + ciomouse_gotevent(CIOLIB_BUTTON_PRESS(2),win_to_text_xpos((sdl_mouse_relative ? sdl_virt_x : ev.button.x), &cvstat),win_to_text_ypos((sdl_mouse_relative ? sdl_virt_y : ev.button.y), &cvstat), win_to_res_xpos((sdl_mouse_relative ? sdl_virt_x : ev.button.x), &cvstat), win_to_res_ypos((sdl_mouse_relative ? sdl_virt_y : ev.button.y), &cvstat), mods); break; case SDL_BUTTON_RIGHT: - ciomouse_gotevent(CIOLIB_BUTTON_PRESS(3),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); + ciomouse_gotevent(CIOLIB_BUTTON_PRESS(3),win_to_text_xpos((sdl_mouse_relative ? sdl_virt_x : ev.button.x), &cvstat),win_to_text_ypos((sdl_mouse_relative ? sdl_virt_y : ev.button.y), &cvstat), win_to_res_xpos((sdl_mouse_relative ? sdl_virt_x : ev.button.x), &cvstat), win_to_res_ypos((sdl_mouse_relative ? sdl_virt_y : ev.button.y), &cvstat), mods); break; } break; @@ -1470,13 +1552,13 @@ void sdl_video_event_thread(void *data) } 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); + ciomouse_gotevent(CIOLIB_BUTTON_RELEASE(1),win_to_text_xpos((sdl_mouse_relative ? sdl_virt_x : ev.button.x), &cvstat),win_to_text_ypos((sdl_mouse_relative ? sdl_virt_y : ev.button.y), &cvstat), win_to_res_xpos((sdl_mouse_relative ? sdl_virt_x : ev.button.x), &cvstat), win_to_res_ypos((sdl_mouse_relative ? sdl_virt_y : ev.button.y), &cvstat), mods); break; case SDL_BUTTON_MIDDLE: - ciomouse_gotevent(CIOLIB_BUTTON_RELEASE(2),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); + ciomouse_gotevent(CIOLIB_BUTTON_RELEASE(2),win_to_text_xpos((sdl_mouse_relative ? sdl_virt_x : ev.button.x), &cvstat),win_to_text_ypos((sdl_mouse_relative ? sdl_virt_y : ev.button.y), &cvstat), win_to_res_xpos((sdl_mouse_relative ? sdl_virt_x : ev.button.x), &cvstat), win_to_res_ypos((sdl_mouse_relative ? sdl_virt_y : ev.button.y), &cvstat), mods); break; case SDL_BUTTON_RIGHT: - ciomouse_gotevent(CIOLIB_BUTTON_RELEASE(3),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); + ciomouse_gotevent(CIOLIB_BUTTON_RELEASE(3),win_to_text_xpos((sdl_mouse_relative ? sdl_virt_x : ev.button.x), &cvstat),win_to_text_ypos((sdl_mouse_relative ? sdl_virt_y : ev.button.y), &cvstat), win_to_res_xpos((sdl_mouse_relative ? sdl_virt_x : ev.button.x), &cvstat), win_to_res_ypos((sdl_mouse_relative ? sdl_virt_y : ev.button.y), &cvstat), mods); break; } break; @@ -1767,9 +1849,15 @@ void sdl_video_event_thread(void *data) sem_post(&sdl_ufunc_ret); break; case SDL_USEREVENT_HIDEMOUSE: + if (getenv("SYNCTERM_MOUSE_TRACE")) + fprintf(stderr, "hidemouse (grabbed=%d)\n", sdl_mouse_exclusive); sdl.ShowCursor(SDL_DISABLE); + sdl_mouse_hidden = 1; + sdl_update_relative(); break; case SDL_USEREVENT_SHOWMOUSE: + sdl_mouse_hidden = 0; + sdl_update_relative(); sdl.ShowCursor(SDL_ENABLE); break; case SDL_USEREVENT_INIT: -- 2.47.3