dvtm

dynamic virtual terminal manager - with my changes
git clone https://pi.duncano.de/git/dvtm.git
Log | Files | Refs | README | LICENSE

commit d0818f982eec716bdb20e570b84c6cf02da95fe3
parent 7e3eb40e92f6ce3a81a06283fb305f4f7765559f
Author: Marc Andre Tanner <mat@brain-dump.org>
Date:   Sat, 24 Sep 2011 08:29:22 +0200

vt: provide an event mechanism

For now the only event is VT_EVENT_TITLE which is fired when
ever the window title is changed through the xterm specific
escape sequence:

 ESC]n;dataBEL

The event argument is the new title or NULL if none was given.

Signed-off-by: Marc Andre Tanner <mat@brain-dump.org>

Diffstat:
vt.c | 39++++++++++++++++++++++++++++++++++++---
vt.h | 7+++++++
2 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/vt.c b/vt.c @@ -58,6 +58,7 @@ #define MIN(x, y) ((x) < (y) ? (x) : (y)) #define COLOR_PALETTE_START 1 #define COLOR_PALETTE_END (unsigned)(MIN(512, COLOR_PAIRS)) +#define sstrlen(str) (sizeof(str) - 1) static bool is_utf8, has_default_colors, use_color_palette; static short *color2palette; @@ -132,6 +133,11 @@ struct Vt { char ebuf[BUFSIZ]; int rlen, elen; + /* xterm style window title */ + char title[256]; + + vt_event_handler_t event_handler; + /* custom escape sequence handler */ vt_handler_t handler; void *data; @@ -823,6 +829,24 @@ static void interpret_esc_SCS(Vt *t) t->graphmode = t->charsets[0]; } +/* Interpret xterm specific escape sequences */ +static void interpret_esc_xterm(Vt *t) +{ + /* ESC]n;dataBEL -- the ESC is not part of t->ebuf */ + char *title = NULL; + + switch (t->ebuf[1]) { + case '0': + case '2': + t->ebuf[t->elen - 1] = '\0'; + if (t->elen > sstrlen("]n;\a")) + title = t->ebuf + sstrlen("]n;"); + + if (t->event_handler) + t->event_handler(t, VT_EVENT_TITLE, title); + } +} + static void try_interpret_escape_seq(Vt *t) { char lastchar = t->ebuf[t->elen - 1]; @@ -833,7 +857,8 @@ static void try_interpret_escape_seq(Vt *t) if (t->handler) { switch ((*(t->handler)) (t, t->ebuf)) { case VT_HANDLER_OK: - goto cancel; + cancel_escape_sequence(t); + return; case VT_HANDLER_NOTYET: if (t->elen + 1 >= (int)sizeof(t->ebuf)) goto cancel; @@ -851,8 +876,11 @@ static void try_interpret_escape_seq(Vt *t) } break; case ']': /* xterm thing */ - if (lastchar == '\a') - goto cancel; + if (lastchar == '\a') { + interpret_esc_xterm(t); + cancel_escape_sequence(t); + return; + } break; case '[': if (is_valid_csi_ender(lastchar)) { @@ -1555,6 +1583,11 @@ void vt_set_handler(Vt *t, vt_handler_t handler) t->handler = handler; } +void vt_set_event_handler(Vt *t, vt_event_handler_t handler) +{ + t->event_handler = handler; +} + void vt_set_data(Vt *t, void *data) { t->data = data; diff --git a/vt.h b/vt.h @@ -53,8 +53,15 @@ enum { typedef struct Vt Vt; typedef int (*vt_handler_t)(Vt *, char *es); +enum { + VT_EVENT_TITLE, +}; + +typedef void (*vt_event_handler_t)(Vt *, int event, void *data); + void vt_init(void); void vt_set_handler(Vt *, vt_handler_t); +void vt_set_event_handler(Vt *, vt_event_handler_t); void vt_set_data(Vt *, void *); void *vt_get_data(Vt *); void vt_set_default_colors(Vt *, unsigned attrs, short fg, short bg);