Add a way to associate custom data with a madtty term.
[apps/madtty.git] / madtty / madtty.c
index e7f26f8..c35959a 100644 (file)
@@ -24,6 +24,7 @@
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <langinfo.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -36,7 +37,7 @@
 # include <pty.h>
 #elif defined(__FreeBSD__)
 # include <libutil.h>
-#elif defined(__OpenBSD__)
+#elif defined(__OpenBSD__) || defined(__NetBSD__)
 # include <util.h>
 #endif
 #include "madtty.h"
@@ -47,7 +48,7 @@
 
 #define IS_CONTROL(ch) !((ch) & 0xffffff60UL)
 
-static int has_default = 0;
+static int has_default, is_utf8;
 
 enum {
     C0_NUL = 0x00,
@@ -105,6 +106,8 @@ struct madtty_t {
     char rbuf[BUFSIZ];
     char ebuf[BUFSIZ];
     int  rlen, elen;
+    madtty_handler_t handler;
+    void *data;
 };
 
 typedef struct t_row_t {
@@ -127,16 +130,26 @@ static char const * const keytable[KEY_MAX+1] = {
     [KEY_PPAGE]     = "\e[5~",
     [KEY_NPAGE]     = "\e[6~",
     [KEY_SUSPEND]   = "\x1A",  /* Ctrl+Z gets mapped to this */
-    [KEY_F(1)]      = "\e[[A",
-    [KEY_F(2)]      = "\e[[B",
-    [KEY_F(3)]      = "\e[[C",
-    [KEY_F(4)]      = "\e[[D",
-    [KEY_F(5)]      = "\e[[E",
+    [KEY_F(1)]      = "\e[11~",
+    [KEY_F(2)]      = "\e[12~",
+    [KEY_F(3)]      = "\e[13~",
+    [KEY_F(4)]      = "\e[14~",
+    [KEY_F(5)]      = "\e[15~",
     [KEY_F(6)]      = "\e[17~",
     [KEY_F(7)]      = "\e[18~",
     [KEY_F(8)]      = "\e[19~",
     [KEY_F(9)]      = "\e[20~",
     [KEY_F(10)]     = "\e[21~",
+    [KEY_F(11)]     = "\e[23~",
+    [KEY_F(12)]     = "\e[24~",
+    [KEY_F(13)]     = "\e[25~",
+    [KEY_F(14)]     = "\e[26~",
+    [KEY_F(15)]     = "\e[28~",
+    [KEY_F(16)]     = "\e[29~",
+    [KEY_F(17)]     = "\e[31~",
+    [KEY_F(18)]     = "\e[32~",
+    [KEY_F(19)]     = "\e[33~",
+    [KEY_F(20)]     = "\e[34~",
 };
 
 static void t_row_set(t_row_t *row, int start, int len, uint16_t attr)
@@ -586,11 +599,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);
@@ -677,13 +696,10 @@ static void madtty_process_nonprinting(madtty_t *t, wchar_t wc)
     }
 }
 
-bool is_utf8 = true;
-
 static void is_utf8_locale(void)
 {
-    const char *l = getenv("LANG");
-    if (l)
-        is_utf8 = (strstr(l, "UTF-8") != NULL);
+    const char *cset = nl_langinfo(CODESET) ?: "ANSI_X3.4-1968";
+    is_utf8 = !strcmp(cset, "UTF-8");
 }
 
 // vt100 special graphics and line drawing
@@ -761,10 +777,9 @@ static void madtty_putc(madtty_t *t, wchar_t wc)
 
         if (t->graphmode) {
             if (wc >= 0x41 && wc <= 0x7e) {
-                if(is_utf8 && vt100_utf8[wc - 0x41])
-                    wc = vt100_utf8[wc - 0x41];
-                else if(!is_utf8 && vt100[wc - 0x41])
-                    wc = vt100[wc - 0x41];
+                wchar_t gc = is_utf8 ? vt100_utf8[wc - 0x41] : vt100[wc - 0x41];
+                if (gc)
+                    wc = gc;
             }
             width = 1;
         } else {
@@ -958,7 +973,7 @@ void madtty_draw(madtty_t *t, WINDOW *win, int srow, int scol)
         for (int j = 0; j < t->cols; j++) {
             if (!j || row->attr[j] != row->attr[j - 1])
                 wattrset(win, (attr_t)row->attr[j] << NCURSES_ATTR_SHIFT);
-            if (is_utf8 && row->text[j] >= 128) {
+            if (row->text[j] >= 128) {
                 char buf[MB_CUR_MAX + 1];
                 int len;
 
@@ -1038,7 +1053,6 @@ void madtty_init_colors(void)
 {
     if (COLOR_PAIRS > 64) {
         use_default_colors();
-        assume_default_colors(-1, -1);
         has_default = 1;
 
         for (int bg = -1; bg < 8; bg++) {
@@ -1047,9 +1061,16 @@ void madtty_init_colors(void)
             }
         }
     } else {
+        int use_default = use_default_colors() == OK;
         for (int bg = 0; bg < 8; bg++) {
             for (int fg = 0; fg < 8; fg++) {
-                init_pair((7 - fg) * 8 + bg, fg, bg);
+                if (use_default) {
+                    init_pair((7 - fg) * 8 + bg,
+                              fg == COLOR_WHITE ? -1 : fg,
+                              bg == COLOR_BLACK ? -1 : bg);
+                } else {
+                    init_pair((7 - fg) * 8 + bg, fg, bg);
+                }
             }
         }
     }
@@ -1070,3 +1091,18 @@ 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;
+}
+
+void madtty_set_data(madtty_t *t, void *data)
+{
+    t->data = data;
+}
+
+void *madtty_get_data(madtty_t *t)
+{
+    return t->data;
+}