Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merged upstream; merged vt_bell_handler into vt_event_handler

  • Loading branch information...
commit 31851b4d38bdb39ed83b45bb95dcb9ad3089139f 2 parents 6a382ab + c16ea5c
@muennich authored
Showing with 98 additions and 70 deletions.
  1. +36 −42 dvtm.c
  2. +49 −22 vt.c
  3. +13 −6 vt.h
View
78 dvtm.c
@@ -250,10 +250,11 @@ drawbar() {
static void
draw_border(Client *c) {
- Client *cc;
+ char t = '\0';
+ int x, y, maxlen;
chtype linechar = ACS_HLINE;
- char *s, t = '\0';
- int x, y, o;
+ Client *cc;
+
if (sel == c) {
wattrset(c->window, SELECTED_ATTR);
wcolor_set(c->window, vt_color_get(SELECTED_FG, SELECTED_BG), NULL);
@@ -275,19 +276,20 @@ draw_border(Client *c) {
getyx(c->window, y, x);
curs_set(0);
mvwhline(c->window, 0, 0, linechar, c->w);
- o = c->w - (4 + sstrlen(TITLE) - 5 + sstrlen(SEPARATOR));
- if (o < 0)
- o = 0;
- if ((size_t)o < sizeof(c->title)) {
- t = *(s = &c->title[o]);
- *s = '\0';
+ maxlen = c->w - (2 + sstrlen(TITLE) - sstrlen("%s%sd") + sstrlen(SEPARATOR) + 2);
+ if (maxlen < 0)
+ maxlen = 0;
+ if ((size_t)maxlen < sizeof(c->title)) {
+ t = c->title[maxlen];
+ c->title[maxlen] = '\0';
}
+
mvwprintw(c->window, 0, 2, TITLE,
c->order,
*c->title ? SEPARATOR : "",
*c->title ? c->title : "");
if (t)
- *s = t;
+ c->title[maxlen] = t;
wmove(c->window, y, x);
if (!c->minimized)
curs_set(vt_cursor(c->term));
@@ -409,7 +411,7 @@ focus(Client *c) {
}
static void
-applycolorrules(Vt *term, char *title) {
+applycolorrules(Client *c) {
unsigned int i;
unsigned attrs = A_NORMAL;
short fg = -1, bg = -1;
@@ -417,45 +419,38 @@ applycolorrules(Vt *term, char *title) {
for (i = 0; i < countof(colorrules); i++) {
r = &colorrules[i];
- if (strstr(title, r->title)) {
+ if (strstr(c->title, r->title)) {
attrs = r->attrs;
fg = r->fg;
bg = r->bg;
break;
}
}
- vt_set_default_colors(term, attrs, fg, bg);
-}
-static int
-title_escape_seq_handler(Vt *term, char *es) {
- Client *c;
- unsigned int l;
- if (es[0] != ']' || (es[1] && (es[1] < '0' || es[1] > '9')) || (es[2] && es[2] != ';'))
- return VT_HANDLER_NOWAY;
- if ((l = strlen(es)) < 3 || es[l - 1] != '\07')
- return VT_HANDLER_NOTYET;
- es[l - 1] = '\0';
- c = (Client *)vt_get_data(term);
- strncpy(c->title, es + 3, sizeof(c->title));
- draw_border(c);
- debug("window title: %s\n", c->title);
- applycolorrules(term, c->title);
- return VT_HANDLER_OK;
+ vt_set_default_colors(c->term, attrs, fg, bg);
}
-static int
-bell_handler(Vt *term, char *ptr) {
- Client *c;
-
- if (bell)
- beep();
- c = (Client *)vt_get_data(term);
- if (c && c != sel && !c->bell_rung) {
- c->bell_rung = true;
+static void
+term_event_handler(Vt *term, int event, void *event_data) {
+ Client *c = (Client *)vt_get_data(term);
+ switch (event) {
+ case VT_EVENT_TITLE:
+ if (event_data)
+ strncpy(c->title, event_data, sizeof(c->title) - 1);
+ c->title[event_data ? sizeof(c->title) - 1 : 0] = '\0';
draw_border(c);
- if (isarrange(fullscreen))
- draw_border(sel);
+ applycolorrules(c);
+ break;
+ case VT_EVENT_BELL:
+ if (bell)
+ beep();
+ if (c && c != sel && !c->bell_rung) {
+ c->bell_rung = true;
+ draw_border(c);
+ if (isarrange(fullscreen))
+ draw_border(sel);
+ }
+ break;
}
}
@@ -738,8 +733,7 @@ create(const char *args[]) {
strncpy(c->title, args[1], sizeof(c->title));
c->pid = vt_forkpty(c->term, "/bin/sh", pargs, env, &c->pty);
vt_set_data(c->term, c);
- vt_set_handler(c->term, title_escape_seq_handler);
- vt_set_bell_handler(c->term, bell_handler);
+ vt_set_event_handler(c->term, term_event_handler);
c->w = screen.w;
c->h = screen.h;
c->x = wax;
View
71 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;
@@ -129,13 +130,16 @@ struct Vt {
mbstate_t ps;
char rbuf[BUFSIZ];
char ebuf[BUFSIZ];
- int rlen, elen;
+ unsigned int rlen, elen;
+
+ /* xterm style window title */
+ char title[256];
+
+ vt_event_handler_t event_handler;
/* custom escape sequence handler */
- vt_handler_t handler;
+ vt_escseq_handler_t escseq_handler;
void *data;
-
- vt_handler_t bell_handler;
};
typedef struct Row {
@@ -824,6 +828,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];
@@ -831,12 +853,13 @@ static void try_interpret_escape_seq(Vt *t)
if (!*t->ebuf)
return;
- if (t->handler) {
- switch ((*(t->handler)) (t, t->ebuf)) {
- case VT_HANDLER_OK:
- goto cancel;
- case VT_HANDLER_NOTYET:
- if (t->elen + 1 >= (int)sizeof(t->ebuf))
+ if (t->escseq_handler) {
+ switch ((*(t->escseq_handler)) (t, t->ebuf)) {
+ case VT_ESCSEQ_HANDLER_OK:
+ cancel_escape_sequence(t);
+ return;
+ case VT_ESCSEQ_HANDLER_NOTYET:
+ if (t->elen + 1 >= sizeof(t->ebuf))
goto cancel;
return;
}
@@ -852,8 +875,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)) {
@@ -888,11 +914,11 @@ static void try_interpret_escape_seq(Vt *t)
goto cancel;
}
- if (t->elen + 1 >= (int)sizeof(t->ebuf)) {
+ if (t->elen + 1 >= sizeof(t->ebuf)) {
cancel:
#ifndef NDEBUG
fprintf(stderr, "cancelled: \\033");
- for (int i = 0; i < (int)t->elen; i++) {
+ for (unsigned int i = 0; i < t->elen; i++) {
if (isprint(t->ebuf[i])) {
fputc(t->ebuf[i], stderr);
} else {
@@ -912,8 +938,8 @@ static void process_nonprinting(Vt *t, wchar_t wc)
new_escape_sequence(t);
break;
case C0_BEL:
- if (t->bell_handler)
- t->bell_handler(t, NULL);
+ if (t->event_handler)
+ t->event_handler(t, VT_EVENT_BELL, NULL);
break;
case C0_BS:
if (t->curs_col > 0)
@@ -983,7 +1009,7 @@ static void put_wc(Vt *t, wchar_t wc)
}
if (t->escaped) {
- if (t->elen + 1 < (int)sizeof(t->ebuf)) {
+ if (t->elen + 1 < sizeof(t->ebuf)) {
t->ebuf[t->elen] = wc;
t->ebuf[++t->elen] = '\0';
try_interpret_escape_seq(t);
@@ -1052,7 +1078,8 @@ static void put_wc(Vt *t, wchar_t wc)
int vt_process(Vt *t)
{
- int res, pos = 0;
+ int res;
+ unsigned int pos = 0;
if (t->pty < 0) {
errno = EINVAL;
@@ -1541,14 +1568,14 @@ void vt_init(void)
is_utf8_locale();
}
-void vt_set_handler(Vt *t, vt_handler_t handler)
+void vt_set_escseq_handler(Vt *t, vt_escseq_handler_t handler)
{
- t->handler = handler;
+ t->escseq_handler = handler;
}
-void vt_set_bell_handler(Vt *t, vt_handler_t handler)
+void vt_set_event_handler(Vt *t, vt_event_handler_t handler)
{
- t->bell_handler = handler;
+ t->event_handler = handler;
}
void vt_set_data(Vt *t, void *data)
View
19 vt.h
@@ -33,7 +33,7 @@
enum {
/* means escape sequence was handled */
- VT_HANDLER_OK,
+ VT_ESCSEQ_HANDLER_OK,
/* means the escape sequence was not recognized yet, but
* there is hope that it still will once more characters
* arrive (i.e. it is not yet complete).
@@ -42,20 +42,27 @@ enum {
* and calling the handler as each character arrives until
* either OK or NOWAY is returned.
*/
- VT_HANDLER_NOTYET,
+ VT_ESCSEQ_HANDLER_NOTYET,
/* means the escape sequence was not recognized, and there
* is no chance that it will even if more characters are
* added to it.
*/
- VT_HANDLER_NOWAY
+ VT_ESCSEQ_HANDLER_NOWAY
};
typedef struct Vt Vt;
-typedef int (*vt_handler_t)(Vt *, char *es);
+typedef int (*vt_escseq_handler_t)(Vt *, char *es);
+
+enum {
+ VT_EVENT_TITLE,
+ VT_EVENT_BELL,
+};
+
+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_bell_handler(Vt *, vt_handler_t);
+void vt_set_escseq_handler(Vt *, vt_escseq_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);
Please sign in to comment.
Something went wrong with that request. Please try again.