diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-25 21:55:37 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-25 21:56:07 -0600 |
| commit | 8a85eb0a61c4deaa337f49f437e00710fc940556 (patch) | |
| tree | 5a29d916b67da9a73be318c90271cb5656a5431d /tui/tui.c | |
| parent | 431855189a87388c323787d668e0698cbfefef30 (diff) | |
| download | ztop-feature/mouse.tar.gz | |
tui: mouse messages; ztop: clickable tabs, wheel, better helpfeature/mouse
Enable xterm SGR mouse reporting and decode it into TUI_MSG_MOUSE. ztop
uses it for the view tabs, the storage sub-tabs, table rows and the
wheel. The help panel now lists navigation explicitly, and the status
line shows the keys for the current view.
Diffstat (limited to 'tui/tui.c')
| -rw-r--r-- | tui/tui.c | 54 |
1 files changed, 52 insertions, 2 deletions
@@ -24,6 +24,10 @@ #define ALT_ON ESCS "[?1049h" #define ALT_OFF ESCS "[?1049l" +/* 1000 reports button presses and releases, 1006 gives SGR coordinates + * (1-based, no 223 limit) so clicks beyond column 223 still work. */ +#define MOUSE_ON ESCS "[?1000h" ESCS "[?1006h" +#define MOUSE_OFF ESCS "[?1006l" ESCS "[?1000l" #define CUR_OFF ESCS "[?25l" #define CUR_ON ESCS "[?25h" #define CLEAR ESCS "[H" ESCS "[2J" @@ -49,7 +53,8 @@ static void wr(int fd, const void *buf, size_t n) { static void restore(struct tui *t) { if (!t->open) return; - wr(t->fd_out, RESET CUR_ON ALT_OFF, strlen(RESET CUR_ON ALT_OFF)); + wr(t->fd_out, RESET CUR_ON MOUSE_OFF ALT_OFF, + strlen(RESET CUR_ON MOUSE_OFF ALT_OFF)); (void)tcsetattr(t->fd_in, TCSANOW, &t->orig); } @@ -268,7 +273,7 @@ bool tui_open(struct tui *t) { #ifdef SIGWINCH (void)signal(SIGWINCH, on_winch); #endif - outs(t, ALT_ON CUR_OFF CLEAR); + outs(t, ALT_ON CUR_OFF CLEAR MOUSE_ON); out_flush(t); if (!tui_resize(t)) { restore(t); @@ -426,6 +431,19 @@ void tui_flush(struct tui *t) { out_flush(t); } +int tui_tabs_hit(int x, const char *const *labels, int n) { + int at = 0; + + for (int i = 0; i < n; i++) { + int len = (int)strlen(labels[i]) + 2; + + if (x >= at && x < at + len) + return i; + at += len + 1; + } + return -1; +} + int tui_tabs_draw(struct tui *t, int x, int y, const char *const *labels, int n, int active) { for (int i = 0; i < n; i++) { @@ -504,6 +522,35 @@ static int decode(struct tui *t) { shift_in(t, 3); return k; } + if (b[2] == '<') { + int v[3] = {0, 0, 0}; + int n = 0; + int i = 3; + + while (i < t->inlen && n < 3) { + if (b[i] >= '0' && b[i] <= '9') { + v[n] = v[n] * 10 + (b[i] - '0'); + i++; + } else if (b[i] == ';') { + n++; + i++; + } else { + break; + } + } + if (i >= t->inlen) + return TUI_KEY_NONE; /* wait for the final byte */ + if (b[i] != 'M' && b[i] != 'm') { + shift_in(t, i + 1); + return TUI_KEY_NONE; + } + t->mouse.button = v[0]; + t->mouse.x = v[1] > 0 ? v[1] - 1 : 0; + t->mouse.y = v[2] > 0 ? v[2] - 1 : 0; + t->mouse.release = b[i] == 'm'; + shift_in(t, i + 1); + return TUI_KEY_MOUSE; + } if (b[2] >= '1' && b[2] <= '9') { int v = 0, i = 2; while (i < t->inlen && b[i] >= '0' && b[i] <= '9') { @@ -620,6 +667,9 @@ void tui_run(struct tui *t, struct tui_app *app) { } else if (k == TUI_KEY_RESIZE) { (void)tui_resize(t); m.type = TUI_MSG_RESIZE; + } else if (k == TUI_KEY_MOUSE) { + m.type = TUI_MSG_MOUSE; + m.mouse = t->mouse; } else { m.type = TUI_MSG_KEY; m.key = k; |
