From 5e8bd7606f26abce289b94c905db65487926bc9c Mon Sep 17 00:00:00 2001 From: Marc Andre Tanner Date: Mon, 14 Jan 2008 22:27:47 +0100 Subject: [PATCH] Add the possibility to register custom escape sequence handlers. Signed-off-by: Marc Andre Tanner --- madtty/madtty.c | 20 ++++++++++++++++---- madtty/madtty.h | 22 +++++++++++++++++++++- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/madtty/madtty.c b/madtty/madtty.c index 317712c..1718b83 100644 --- a/madtty/madtty.c +++ b/madtty/madtty.c @@ -106,6 +106,7 @@ struct madtty_t { char rbuf[BUFSIZ]; char ebuf[BUFSIZ]; int rlen, elen; + madtty_handler_t handler; }; typedef struct t_row_t { @@ -597,11 +598,17 @@ static void es_interpret_csi(madtty_t *t) static void try_interpret_escape_seq(madtty_t *t) { char lastchar = t->ebuf[t->elen-1]; - + if(!*t->ebuf) + return; + if(t->handler){ + switch((*(t->handler))(t, t->ebuf)){ + case MADTTY_HANDLER_OK: + goto cancel; + case MADTTY_HANDLER_NOTYET: + return; + } + } switch (*t->ebuf) { - case '\0': - return; - case 'M': interpret_csi_SR(t); cancel_escape_sequence(t); @@ -1083,3 +1090,8 @@ int madtty_color_pair(int fg, int bg) bg = COLOR_BLACK; return COLOR_PAIR((7 - fg) * 8 + bg); } + +void madtty_set_handler(madtty_t *t, madtty_handler_t handler) +{ + t->handler = handler; +} diff --git a/madtty/madtty.h b/madtty/madtty.h index 8e8424a..0a1e451 100644 --- a/madtty/madtty.h +++ b/madtty/madtty.h @@ -30,11 +30,31 @@ #include #include +enum { + /* means escape sequence was handled */ + MADTTY_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). + * + * The library will thus continue collecting characters + * and calling the handler as each character arrives until + * either OK or NOWAY is returned. + */ + MADTTY_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. + */ + MADTTY_HANDLER_NOWAY +}; + typedef struct madtty_t madtty_t; +typedef int (*madtty_handler_t)(madtty_t *, char *es); void madtty_init_colors(void); void madtty_init_vt100_graphics(void); - +void madtty_set_handler(madtty_t *, madtty_handler_t); int madtty_color_pair(int fg, int bg); madtty_t *madtty_create(int rows, int cols); -- 2.20.1