From 81e6b289ceb954628b74359b8cfad5989b413084 Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 30 May 2022 13:02:55 +0000 Subject: [PATCH] If a mouse position was above the maximum supported by the normal mouse protocol (223), tmux was allowing it to wrap around. However, since tmux was not correctly handling this on input, other programs also do not handle it correctly, and the alternative SGR mouse mode is now widespread, this seems unnecessary, so remove this feature. Also define some constants to make it clearer what the numbers mean. Mostly from Leonid S Usov in GitHub issue 3165. --- usr.bin/tmux/input-keys.c | 33 ++++++++++++++++++++++++--------- usr.bin/tmux/tmux.h | 8 +++++++- usr.bin/tmux/tty-keys.c | 18 +++++++----------- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/usr.bin/tmux/input-keys.c b/usr.bin/tmux/input-keys.c index 0d099decb4b..54f347007b0 100644 --- a/usr.bin/tmux/input-keys.c +++ b/usr.bin/tmux/input-keys.c @@ -1,4 +1,4 @@ -/* $OpenBSD: input-keys.c,v 1.89 2022/03/01 15:20:22 nicm Exp $ */ +/* $OpenBSD: input-keys.c,v 1.90 2022/05/30 13:02:55 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -607,19 +607,34 @@ input_key_get_mouse(struct screen *s, struct mouse_event *m, u_int x, u_int y, len = xsnprintf(buf, sizeof buf, "\033[<%u;%u;%u%c", m->sgr_b, x + 1, y + 1, m->sgr_type); } else if (s->mode & MODE_MOUSE_UTF8) { - if (m->b > 0x7ff - 32 || x > 0x7ff - 33 || y > 0x7ff - 33) + if (m->b > MOUSE_PARAM_UTF8_MAX - MOUSE_PARAM_BTN_OFF || + x > MOUSE_PARAM_UTF8_MAX - MOUSE_PARAM_POS_OFF || + y > MOUSE_PARAM_UTF8_MAX - MOUSE_PARAM_POS_OFF) return (0); len = xsnprintf(buf, sizeof buf, "\033[M"); - len += input_key_split2(m->b + 32, &buf[len]); - len += input_key_split2(x + 33, &buf[len]); - len += input_key_split2(y + 33, &buf[len]); + len += input_key_split2(m->b + MOUSE_PARAM_BTN_OFF, &buf[len]); + len += input_key_split2(x + MOUSE_PARAM_POS_OFF, &buf[len]); + len += input_key_split2(y + MOUSE_PARAM_POS_OFF, &buf[len]); } else { - if (m->b > 223) + if (m->b + MOUSE_PARAM_BTN_OFF > MOUSE_PARAM_MAX) return (0); + len = xsnprintf(buf, sizeof buf, "\033[M"); - buf[len++] = m->b + 32; - buf[len++] = x + 33; - buf[len++] = y + 33; + buf[len++] = m->b + MOUSE_PARAM_BTN_OFF; + + /* + * The incoming x and y may be out of the range which can be + * supported by the "normal" mouse protocol. Clamp the + * coordinates to the supported range. + */ + if (x + MOUSE_PARAM_POS_OFF > MOUSE_PARAM_MAX) + buf[len++] = MOUSE_PARAM_MAX; + else + buf[len++] = x + MOUSE_PARAM_POS_OFF; + if (y + MOUSE_PARAM_POS_OFF > MOUSE_PARAM_MAX) + buf[len++] = MOUSE_PARAM_MAX; + else + buf[len++] = y + MOUSE_PARAM_POS_OFF; } *rbuf = buf; diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 1bbc4a466e8..7636d28bd8c 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.1169 2022/05/30 13:00:18 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.1170 2022/05/30 13:02:55 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -580,6 +580,12 @@ enum tty_code_code { #define MOTION_MOUSE_MODES (MODE_MOUSE_BUTTON|MODE_MOUSE_ALL) #define CURSOR_MODES (MODE_CURSOR|MODE_CURSOR_BLINKING|MODE_CURSOR_VERY_VISIBLE) +/* Mouse protocol constants. */ +#define MOUSE_PARAM_MAX 0xff +#define MOUSE_PARAM_UTF8_MAX 0x7ff +#define MOUSE_PARAM_BTN_OFF 0x20 +#define MOUSE_PARAM_POS_OFF 0x21 + /* A single UTF-8 character. */ typedef u_int utf8_char; diff --git a/usr.bin/tmux/tty-keys.c b/usr.bin/tmux/tty-keys.c index 70ac1b4a2be..e1b6f4cc2c6 100644 --- a/usr.bin/tmux/tty-keys.c +++ b/usr.bin/tmux/tty-keys.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tty-keys.c,v 1.154 2022/03/08 12:01:19 nicm Exp $ */ +/* $OpenBSD: tty-keys.c,v 1.155 2022/05/30 13:02:55 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -1061,17 +1061,13 @@ tty_keys_mouse(struct tty *tty, const char *buf, size_t len, size_t *size, log_debug("%s: mouse input: %.*s", c->name, (int)*size, buf); /* Check and return the mouse input. */ - if (b < 32) + if (b < MOUSE_PARAM_BTN_OFF || + x < MOUSE_PARAM_POS_OFF || + y < MOUSE_PARAM_POS_OFF) return (-1); - b -= 32; - if (x >= 33) - x -= 33; - else - x = 256 - x; - if (y >= 33) - y -= 33; - else - y = 256 - y; + b -= MOUSE_PARAM_BTN_OFF; + x -= MOUSE_PARAM_POS_OFF; + y -= MOUSE_PARAM_POS_OFF; } else if (buf[2] == '<') { /* Read the three inputs. */ *size = 3; -- 2.20.1