SyncTERM 1.9: exclusive (captured) mouse, per BBS entry Capture the pointer to the window and report MOTION instead of position, so a game driven over the wire can keep turning: the pointer cannot walk out of the window mid-turn, and the reported position is an accumulator that is deliberately NOT clamped, because a position pinned at the edge of the screen silently swallows the rest of the movement. Off by default, set per entry: "Exclusive Mouse" in the entry editor, or ExclusiveMouse=true in syncterm.lst. DIFFERENCES FROM THE MASTER VERSION OF THIS PATCH: * 1.9's doterm() returns from six places and has no single exit, so capture is done in a small wrapper (doterm() -> doterm_session()) that releases the pointer on every path. A terminal that kept the pointer after the session would be one the user cannot get out of. * The Wren binding (bbs.exclusiveMouse) is omitted -- 1.9 has no Wren. * The row is added to 1.9's UIFC entry editor (BBSLIST_FIELD_* enum, optmap row, toggle case). 38 rows against EDIT_LIST_MAX 41, so it fits. SDL_SetRelativeMouseMode is resolved as an OPTIONAL symbol: a terminal built against an SDL that lacks it still runs, without capture. --- a/src/conio/ciolib.h +++ b/src/conio/ciolib.h @@ -830,4 +830,9 @@ #define CIO_KEY_ABORTED 0x01E0 // ESC key by scancode #define CIO_KEY_LITERAL_E0 0xE0E0 // Literal 0xe0 character +/* 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. */ +CIOLIBEXPORT int ciolib_set_mouse_exclusive(int on); + #endif /* Do not add anything after this line */ --- a/src/conio/sdlfuncs.h +++ b/src/conio/sdlfuncs.h @@ -28,6 +28,10 @@ void (HACK_HACK_HACK *GetWindowSizeInPixels) (SDL_Window *window, int *w, int *h); void (HACK_HACK_HACK *SetWindowIcon) (SDL_Window *win, SDL_Surface *icon); int (HACK_HACK_HACK *ShowCursor) (int toggle); + /* Pointer capture, for the exclusive mouse mode a game wants: + * 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); 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); --- a/src/conio/sdlfuncs.c +++ b/src/conio/sdlfuncs.c @@ -94,6 +94,10 @@ xp_dlclose(sdl_dll); return(-1); } + /* Not fatal if missing: every other entry point here is required to + * 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); if((sdlf->WasInit=xp_dlsym(sdl_dll, SDL_WasInit))==NULL) { xp_dlclose(sdl_dll); return(-1); --- a/src/conio/sdl_con.c +++ b/src/conio/sdl_con.c @@ -39,6 +39,24 @@ /* 256 bytes so I can cheat */ 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. + * + * 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. + */ +static int sdl_mouse_exclusive = 0; +static int sdl_rel_x = 0, sdl_rel_y = 0; +static int sdl_rel_centred = 0; 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 */ @@ -737,6 +755,19 @@ } /* Called from event thread only */ +CIOLIBEXPORT int +ciolib_set_mouse_exclusive(int on) +{ + if (sdl.SetRelativeMouseMode == NULL) + return 0; /* SDL too old; caller carries on without */ + if (sdl.SetRelativeMouseMode(on ? SDL_TRUE : SDL_FALSE) != 0) + return 0; + sdl_mouse_exclusive = on ? 1 : 0; + /* Centred on the first motion, where the screen size is in scope. */ + sdl_rel_centred = 0; + return 1; +} + static void sdl_add_key(unsigned int keyval, struct video_stats *vs) { int w, h; @@ -1109,6 +1140,26 @@ ((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: --- a/src/syncterm/bbslist.h +++ b/src/syncterm/bbslist.h @@ -151,6 +151,10 @@ char comment[1024]; bool force_lcf; bool yellow_is_yellow; + /* Capture the pointer to the window and report motion rather than + * position -- what a game needs, and a nuisance for a BBS, so it + * is per entry and off by default. */ + bool exclusive_mouse; bool has_fingerprint; uint8_t ssh_fingerprint[20]; bool sftp_public_key; --- a/src/syncterm/bbslist.c +++ b/src/syncterm/bbslist.c @@ -229,6 +229,12 @@ sizeof(((struct bbslist *)NULL)->yellow_is_yellow) }, { + "Exclusive Mouse", + 0, + offsetof(struct bbslist, exclusive_mouse), + sizeof(((struct bbslist *)NULL)->exclusive_mouse) + }, + { "Terminal Type", 0, offsetof(struct bbslist, term_name), @@ -1316,6 +1322,7 @@ entry->rip = iniGetEnum(section, NULL, "RIP", rip_versions, RIP_VERSION_NONE); entry->force_lcf = iniGetBool(section, NULL, "ForceLCF", false); entry->yellow_is_yellow = iniGetBool(section, NULL, "YellowIsYellow", false); + entry->exclusive_mouse = iniGetBool(section, NULL, "ExclusiveMouse", false); iniGetSString(section, NULL, "TerminalType", "", entry->term_name, sizeof(entry->term_name)); entry->lf_expand = iniGetBool(section, NULL, "LFExpand", false); if (iniKeyExists(section, NULL, "SSHFingerprint")) { @@ -1807,6 +1814,7 @@ BBSLIST_FIELD_COMMENT, BBSLIST_FIELD_FORCE_LCF, BBSLIST_FIELD_YELLOW_IS_YELLOW, + BBSLIST_FIELD_EXCLUSIVE_MOUSE, BBSLIST_FIELD_HAS_FINGERPRINT, BBSLIST_FIELD_SSH_FINGERPRINT, BBSLIST_FIELD_SFTP_PUBLIC_KEY, @@ -1942,6 +1950,8 @@ optmap[i] = BBSLIST_FIELD_YELLOW_IS_YELLOW; sprintf(opt[i++], "Yellow is Yellow %s", item->yellow_is_yellow ? "Yes" : "No"); } + optmap[i] = BBSLIST_FIELD_EXCLUSIVE_MOUSE; + sprintf(opt[i++], "Exclusive Mouse %s", item->exclusive_mouse ? "Yes" : "No"); optmap[i] = BBSLIST_FIELD_NOSTATUS; sprintf(opt[i++], "Hide Status Line %s", item->nostatus ? "Yes" : "No"); optmap[i] = BBSLIST_FIELD_DLDIR; @@ -3036,6 +3046,11 @@ changed = 1; iniSetBool(&inifile, itemname, "YellowIsYellow", item->yellow_is_yellow, &ini_style); break; + case BBSLIST_FIELD_EXCLUSIVE_MOUSE: + item->exclusive_mouse = !item->exclusive_mouse; + changed = 1; + iniSetBool(&inifile, itemname, "ExclusiveMouse", item->exclusive_mouse, &ini_style); + break; case BBSLIST_FIELD_SFTP_PUBLIC_KEY: item->sftp_public_key = !item->sftp_public_key; changed = 1; --- a/src/syncterm/term.c +++ b/src/syncterm/term.c @@ -5737,9 +5737,29 @@ } #endif /* !WITHOUT_OOII */ +/* + * Exclusive mouse, if this entry asked for it. doterm() leaves by any of + * several returns, so capture is done in a wrapper rather than at each one: + * a terminal that kept the pointer after the session would be one the user + * cannot get out of. + */ +static bool doterm_session(struct bbslist *bbs); + bool doterm(struct bbslist *bbs) { + bool ret; + + if (bbs != NULL && bbs->exclusive_mouse) + ciolib_set_mouse_exclusive(1); + ret = doterm_session(bbs); + ciolib_set_mouse_exclusive(0); + return ret; +} + +static bool +doterm_session(struct bbslist *bbs) +{ unsigned char ch[2]; unsigned char outbuf[OUTBUF_SIZE]; size_t outbuf_size = 0;