Make strdump(), csidump(), print to stderr
[st.git] / st.c
diff --git a/st.c b/st.c
index ebbd7ca..0980082 100644 (file)
--- a/st.c
+++ b/st.c
@@ -66,6 +66,7 @@ char *argv0;
 #define MIN(a, b)              ((a) < (b) ? (a) : (b))
 #define MAX(a, b)              ((a) < (b) ? (b) : (a))
 #define LEN(a)                 (sizeof(a) / sizeof(a)[0])
+#define NUMMAXLEN(x)           ((int)(sizeof(x) * 2.56 + 0.5) + 1)
 #define DEFAULT(a, b)          (a) = (a) ? (a) : (b)
 #define BETWEEN(x, a, b)       ((a) <= (x) && (x) <= (b))
 #define DIVCEIL(n, d)          (((n) + ((d) - 1)) / (d))
@@ -87,6 +88,8 @@ char *argv0;
 #define TRUEGREEN(x)           (((x) & 0xff00))
 #define TRUEBLUE(x)            (((x) & 0xff) << 8)
 
+/* constants */
+#define ISO14755CMD            "dmenu -w %lu -p codepoint: </dev/null"
 
 enum glyph_attribute {
        ATTR_NULL       = 0,
@@ -138,6 +141,7 @@ enum term_mode {
        MODE_BRCKTPASTE  = 1 << 19,
        MODE_PRINT       = 1 << 20,
        MODE_UTF8        = 1 << 21,
+       MODE_SIXEL       = 1 << 22,
        MODE_MOUSE       = MODE_MOUSEBTN|MODE_MOUSEMOTION|MODE_MOUSEX10\
                          |MODE_MOUSEMANY,
 };
@@ -155,11 +159,12 @@ enum charset {
 enum escape_state {
        ESC_START      = 1,
        ESC_CSI        = 2,
-       ESC_STR        = 4,  /* DCS, OSC, PM, APC */
+       ESC_STR        = 4,  /* OSC, PM, APC */
        ESC_ALTCHARSET = 8,
        ESC_STR_END    = 16, /* a final string was encountered */
        ESC_TEST       = 32, /* Enter in test mode */
        ESC_UTF8       = 64,
+       ESC_DCS        =128,
 };
 
 enum window_state {
@@ -336,6 +341,7 @@ static void xzoomabs(const Arg *);
 static void xzoomreset(const Arg *);
 static void printsel(const Arg *);
 static void printscreen(const Arg *) ;
+static void iso14755(const Arg *);
 static void toggleprinter(const Arg *);
 static void sendbreak(const Arg *);
 
@@ -348,6 +354,8 @@ typedef struct {
        int width;
        int ascent;
        int descent;
+       int badslant;
+       int badweight;
        short lbearing;
        short rbearing;
        XftFont *match;
@@ -1485,7 +1493,7 @@ ttyread(void)
        ptr = buf;
 
        for (;;) {
-               if (IS_SET(MODE_UTF8)) {
+               if (IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
                        /* process a complete utf8 char */
                        charsize = utf8decode(ptr, &unicodep, buflen);
                        if (charsize == 0)
@@ -1578,7 +1586,7 @@ ttysend(char *s, size_t n)
 
        lim = &s[n];
        for (t = s; t < lim; t += len) {
-               if (IS_SET(MODE_UTF8)) {
+               if (IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
                        len = utf8decode(t, &u, n);
                } else {
                        u = *t & 0xFF;
@@ -2482,22 +2490,22 @@ csidump(void)
        int i;
        uint c;
 
-       printf("ESC[");
+       fprintf(stderr, "ESC[");
        for (i = 0; i < csiescseq.len; i++) {
                c = csiescseq.buf[i] & 0xff;
                if (isprint(c)) {
-                       putchar(c);
+                       putc(c, stderr);
                } else if (c == '\n') {
-                       printf("(\\n)");
+                       fprintf(stderr, "(\\n)");
                } else if (c == '\r') {
-                       printf("(\\r)");
+                       fprintf(stderr, "(\\r)");
                } else if (c == 0x1b) {
-                       printf("(\\e)");
+                       fprintf(stderr, "(\\e)");
                } else {
-                       printf("(%02x)", c);
+                       fprintf(stderr, "(%02x)", c);
                }
        }
-       putchar('\n');
+       putc('\n', stderr);
 }
 
 void
@@ -2548,6 +2556,7 @@ strhandle(void)
                xsettitle(strescseq.args[0]);
                return;
        case 'P': /* DCS -- Device Control String */
+               term.mode |= ESC_DCS;
        case '_': /* APC -- Application Program Command */
        case '^': /* PM -- Privacy Message */
                return;
@@ -2585,24 +2594,25 @@ strdump(void)
        int i;
        uint c;
 
-       printf("ESC%c", strescseq.type);
+       fprintf(stderr, "ESC%c", strescseq.type);
        for (i = 0; i < strescseq.len; i++) {
                c = strescseq.buf[i] & 0xff;
                if (c == '\0') {
+                       putc('\n', stderr);
                        return;
                } else if (isprint(c)) {
-                       putchar(c);
+                       putc(c, stderr);
                } else if (c == '\n') {
-                       printf("(\\n)");
+                       fprintf(stderr, "(\\n)");
                } else if (c == '\r') {
-                       printf("(\\r)");
+                       fprintf(stderr, "(\\r)");
                } else if (c == 0x1b) {
-                       printf("(\\e)");
+                       fprintf(stderr, "(\\e)");
                } else {
-                       printf("(%02x)", c);
+                       fprintf(stderr, "(%02x)", c);
                }
        }
-       printf("ESC\\\n");
+       fprintf(stderr, "ESC\\\n");
 }
 
 void
@@ -2629,6 +2639,30 @@ tprinter(char *s, size_t len)
        }
 }
 
+void
+iso14755(const Arg *arg)
+{
+       char cmd[sizeof(ISO14755CMD) + NUMMAXLEN(xw.win)];
+       FILE *p;
+       char *us, *e, codepoint[9], uc[UTF_SIZ];
+       unsigned long utf32;
+
+       snprintf(cmd, sizeof(cmd), ISO14755CMD, xw.win);
+       if (!(p = popen(cmd, "r")))
+               return;
+
+       us = fgets(codepoint, sizeof(codepoint), p);
+       pclose(p);
+
+       if (!us || *us == '\0' || *us == '-' || strlen(us) > 7)
+               return;
+       if ((utf32 = strtoul(us, &e, 16)) == ULONG_MAX ||
+           (*e != '\n' && *e != '\0'))
+               return;
+
+       ttysend(uc, utf8encode(utf32, uc));
+}
+
 void
 toggleprinter(const Arg *arg)
 {
@@ -2754,9 +2788,12 @@ tdectest(char c)
 void
 tstrsequence(uchar c)
 {
+       strreset();
+
        switch (c) {
        case 0x90:   /* DCS -- Device Control String */
                c = 'P';
+               term.esc |= ESC_DCS;
                break;
        case 0x9f:   /* APC -- Application Program Command */
                c = '_';
@@ -2768,7 +2805,6 @@ tstrsequence(uchar c)
                c = ']';
                break;
        }
-       strreset();
        strescseq.type = c;
        term.esc |= ESC_STR;
 }
@@ -2968,7 +3004,7 @@ tputc(Rune u)
        Glyph *gp;
 
        control = ISCONTROL(u);
-       if (!IS_SET(MODE_UTF8)) {
+       if (!IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
                c[0] = u;
                width = len = 1;
        } else {
@@ -2991,30 +3027,47 @@ tputc(Rune u)
        if (term.esc & ESC_STR) {
                if (u == '\a' || u == 030 || u == 032 || u == 033 ||
                   ISCONTROLC1(u)) {
-                       term.esc &= ~(ESC_START|ESC_STR);
+                       term.esc &= ~(ESC_START|ESC_STR|ESC_DCS);
+                       if (IS_SET(MODE_SIXEL)) {
+                               /* TODO: render sixel */;
+                               term.mode &= ~MODE_SIXEL;
+                               return;
+                       }
                        term.esc |= ESC_STR_END;
-               } else if (strescseq.len + len < sizeof(strescseq.buf) - 1) {
-                       memmove(&strescseq.buf[strescseq.len], c, len);
-                       strescseq.len += len;
+                       goto check_control_code;
+               }
+
+
+               if (IS_SET(MODE_SIXEL)) {
+                       /* TODO: implement sixel mode */
                        return;
-               } else {
-               /*
-                * Here is a bug in terminals. If the user never sends
-                * some code to stop the str or esc command, then st
-                * will stop responding. But this is better than
-                * silently failing with unknown characters. At least
-                * then users will report back.
-                *
-                * In the case users ever get fixed, here is the code:
-                */
-               /*
-                * term.esc = 0;
-                * strhandle();
-                */
+               }
+               if (term.esc&ESC_DCS && strescseq.len == 0 && u == 'q')
+                       term.mode |= MODE_SIXEL;
+
+               if (strescseq.len+len >= sizeof(strescseq.buf)-1) {
+                       /*
+                        * Here is a bug in terminals. If the user never sends
+                        * some code to stop the str or esc command, then st
+                        * will stop responding. But this is better than
+                        * silently failing with unknown characters. At least
+                        * then users will report back.
+                        *
+                        * In the case users ever get fixed, here is the code:
+                        */
+                       /*
+                        * term.esc = 0;
+                        * strhandle();
+                        */
                        return;
                }
+
+               memmove(&strescseq.buf[strescseq.len], c, len);
+               strescseq.len += len;
+               return;
        }
 
+check_control_code:
        /*
         * Actions of control codes must be performed as soon they arrive
         * because they can be embedded inside a control sequence, and
@@ -3323,6 +3376,7 @@ xloadfont(Font *f, FcPattern *pattern)
        FcPattern *match;
        FcResult result;
        XGlyphInfo extents;
+       int wantattr, haveattr;
 
        match = XftFontMatch(xw.dpy, xw.scr, pattern, &result);
        if (!match)
@@ -3333,6 +3387,28 @@ xloadfont(Font *f, FcPattern *pattern)
                return 1;
        }
 
+       if ((XftPatternGetInteger(pattern, "slant", 0, &wantattr) ==
+           XftResultMatch)) {
+               /*
+                * Check if xft was unable to find a font with the appropriate
+                * slant but gave us one anyway. Try to mitigate.
+                */
+               if ((XftPatternGetInteger(f->match->pattern, "slant", 0,
+                   &haveattr) != XftResultMatch) || haveattr < wantattr) {
+                       f->badslant = 1;
+                       fputs("st: font slant does not match\n", stderr);
+               }
+       }
+
+       if ((XftPatternGetInteger(pattern, "weight", 0, &wantattr) ==
+           XftResultMatch)) {
+               if ((XftPatternGetInteger(f->match->pattern, "weight", 0,
+                   &haveattr) != XftResultMatch) || haveattr != wantattr) {
+                       f->badweight = 1;
+                       fputs("st: font weight does not match\n", stderr);
+               }
+       }
+
        XftTextExtentsUtf8(xw.dpy, f->match,
                (const FcChar8 *) ascii_printable,
                strlen(ascii_printable), &extents);
@@ -3730,14 +3806,13 @@ xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, i
        XRenderColor colfg, colbg;
        XRectangle r;
 
-       /* Determine foreground and background colors based on mode. */
-       if (base.fg == defaultfg) {
-               if (base.mode & ATTR_ITALIC)
-                       base.fg = defaultitalic;
-               else if ((base.mode & ATTR_ITALIC) && (base.mode & ATTR_BOLD))
-                       base.fg = defaultitalic;
-               else if (base.mode & ATTR_UNDERLINE)
-                       base.fg = defaultunderline;
+       /* Fallback on color display for attributes not supported by the font */
+       if (base.mode & ATTR_ITALIC && base.mode & ATTR_BOLD) {
+               if (dc.ibfont.badslant || dc.ibfont.badweight)
+                       base.fg = defaultattr;
+       } else if ((base.mode & ATTR_ITALIC && dc.ifont.badslant) ||
+           (base.mode & ATTR_BOLD && dc.bfont.badweight)) {
+               base.fg = defaultattr;
        }
 
        if (IS_TRUECOL(base.fg)) {