JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Reorder and extend glyph attributes
[st.git] / st.c
diff --git a/st.c b/st.c
index 3681776..9637834 100644 (file)
--- a/st.c
+++ b/st.c
@@ -76,8 +76,7 @@ char *argv0;
 #define LIMIT(x, a, b)    (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
 #define IS_SET(flag) ((term.mode & (flag)) != 0)
-#define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
-#define CEIL(x) (((x) != (int) (x)) ? (x) + 1 : (x))
+#define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_nsec-t2.tv_nsec)/1E6)
 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
 
 #define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
@@ -90,15 +89,19 @@ char *argv0;
 #define VT102ID "\033[?6c"
 
 enum glyph_attribute {
-       ATTR_NULL      = 0,
-       ATTR_REVERSE   = 1,
-       ATTR_UNDERLINE = 2,
-       ATTR_BOLD      = 4,
-       ATTR_ITALIC    = 8,
+        ATTR_NULL      = 0,
+       ATTR_BOLD      = 1,
+       ATTR_FAINT     = 2,
+       ATTR_ITALIC    = 4,
+       ATTR_UNDERLINE = 8,
        ATTR_BLINK     = 16,
-       ATTR_WRAP      = 32,
-       ATTR_WIDE      = 64,
-       ATTR_WDUMMY    = 128,
+       ATTR_FASTBLINK = 32,
+       ATTR_REVERSE   = 64,
+       ATTR_INVISIBLE = 128,
+       ATTR_STRUCK    = 256,
+       ATTR_WRAP      = 512,
+       ATTR_WIDE      = 1024,
+       ATTR_WDUMMY    = 2048,
 };
 
 enum cursor_movement {
@@ -294,8 +297,8 @@ typedef struct {
        char *clip;
        Atom xtarget;
        bool alt;
-       struct timeval tclick1;
-       struct timeval tclick2;
+       struct timespec tclick1;
+       struct timespec tclick2;
 } Selection;
 
 typedef union {
@@ -860,7 +863,7 @@ mousereport(XEvent *e) {
 
 void
 bpress(XEvent *e) {
-       struct timeval now;
+       struct timespec now;
        Mousekey *mk;
 
        if(IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
@@ -877,7 +880,7 @@ bpress(XEvent *e) {
        }
 
        if(e->xbutton.button == Button1) {
-               gettimeofday(&now, NULL);
+               clock_gettime(CLOCK_MONOTONIC, &now);
 
                /* Clear previous selection, logically and visually. */
                selclear(NULL);
@@ -1682,15 +1685,25 @@ tsetattr(int *attr, int l) {
        for(i = 0; i < l; i++) {
                switch(attr[i]) {
                case 0:
-                       term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE \
-                                       | ATTR_BOLD | ATTR_ITALIC \
-                                       | ATTR_BLINK);
+                       term.c.attr.mode &= ~(
+                               ATTR_BOLD       |
+                               ATTR_FAINT      |
+                               ATTR_ITALIC     |
+                               ATTR_UNDERLINE  |
+                               ATTR_BLINK      |
+                               ATTR_FASTBLINK  |
+                               ATTR_REVERSE    |
+                               ATTR_INVISIBLE  |
+                               ATTR_STRUCK     );
                        term.c.attr.fg = defaultfg;
                        term.c.attr.bg = defaultbg;
                        break;
                case 1:
                        term.c.attr.mode |= ATTR_BOLD;
                        break;
+               case 2:
+                       term.c.attr.mode |= ATTR_FAINT;
+                       break;
                case 3:
                        term.c.attr.mode |= ATTR_ITALIC;
                        break;
@@ -1698,16 +1711,26 @@ tsetattr(int *attr, int l) {
                        term.c.attr.mode |= ATTR_UNDERLINE;
                        break;
                case 5: /* slow blink */
-               case 6: /* rapid blink */
                        term.c.attr.mode |= ATTR_BLINK;
                        break;
+               case 6: /* rapid blink */
+                       term.c.attr.mode |= ATTR_FASTBLINK;
+                       break;
                case 7:
                        term.c.attr.mode |= ATTR_REVERSE;
                        break;
+               case 8:
+                       term.c.attr.mode |= ATTR_INVISIBLE;
+                       break;
+               case 9:
+                       term.c.attr.mode |= ATTR_STRUCK;
+                       break;
                case 21:
-               case 22:
                        term.c.attr.mode &= ~ATTR_BOLD;
                        break;
+               case 22:
+                       term.c.attr.mode &= ~ATTR_FAINT;
+                       break;
                case 23:
                        term.c.attr.mode &= ~ATTR_ITALIC;
                        break;
@@ -1715,12 +1738,20 @@ tsetattr(int *attr, int l) {
                        term.c.attr.mode &= ~ATTR_UNDERLINE;
                        break;
                case 25:
-               case 26:
                        term.c.attr.mode &= ~ATTR_BLINK;
                        break;
+               case 26:
+                       term.c.attr.mode &= ~ATTR_FASTBLINK;
+                       break;
                case 27:
                        term.c.attr.mode &= ~ATTR_REVERSE;
                        break;
+               case 28:
+                       term.c.attr.mode &= ~ATTR_INVISIBLE;
+                       break;
+               case 29:
+                       term.c.attr.mode &= ~ATTR_STRUCK;
+                       break;
                case 38:
                        if ((idx = tdefcolor(attr, &i, l)) >= 0)
                                term.c.attr.fg = idx;
@@ -2892,6 +2923,7 @@ xloadfonts(char *fontstr, double fontsize) {
        FcPattern *pattern;
        FcResult r_sz, r_psz;
        double fontval;
+       float ceilf(float);
 
        if(fontstr[0] == '-') {
                pattern = XftXlfdParse(fontstr, False, False);
@@ -2937,8 +2969,8 @@ xloadfonts(char *fontstr, double fontsize) {
        }
 
        /* Setting character width and height. */
-       xw.cw = CEIL(dc.font.width * cwscale);
-       xw.ch = CEIL(dc.font.height * chscale);
+       xw.cw = ceilf(dc.font.width * cwscale);
+       xw.ch = ceilf(dc.font.height * chscale);
 
        FcPatternDel(pattern, FC_SLANT);
        FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
@@ -2993,6 +3025,7 @@ xzoom(const Arg *arg) {
        xloadfonts(usedfont, usedfontsize + arg->i);
        cresize(0, 0);
        redraw(0);
+       xhints();
 }
 
 void
@@ -3245,28 +3278,22 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
                        bytelen -= u8cblen;
 
                        doesexist = XftCharExists(xw.dpy, font->match, unicodep);
-                       if(oneatatime || !doesexist || bytelen <= 0) {
-                               if(oneatatime || bytelen <= 0) {
-                                       if(doesexist) {
-                                               u8fl++;
-                                               u8fblen += u8cblen;
-                                       }
-                               }
-
-                               if(u8fl > 0) {
-                                       XftDrawStringUtf8(xw.draw, fg,
-                                                       font->match, xp,
-                                                       winy + font->ascent,
-                                                       (FcChar8 *)u8fs,
-                                                       u8fblen);
-                                       xp += xw.cw * u8fl;
-
-                               }
-                               break;
+                       if(doesexist) {
+                                       u8fl++;
+                                       u8fblen += u8cblen;
+                                       if(!oneatatime && bytelen > 0)
+                                                       continue;
                        }
 
-                       u8fl++;
-                       u8fblen += u8cblen;
+                       if(u8fl > 0) {
+                               XftDrawStringUtf8(xw.draw, fg,
+                                               font->match, xp,
+                                               winy + font->ascent,
+                                               (FcChar8 *)u8fs,
+                                               u8fblen);
+                               xp += xw.cw * u8fl;
+                       }
+                       break;
                }
                if(doesexist) {
                        if(oneatatime)
@@ -3714,7 +3741,8 @@ run(void) {
        int w = xw.w, h = xw.h;
        fd_set rfd;
        int xfd = XConnectionNumber(xw.dpy), xev, blinkset = 0, dodraw = 0;
-       struct timeval drawtimeout, *tv = NULL, now, last, lastblink;
+       struct timespec drawtimeout, *tv = NULL, now, last, lastblink;
+       long deltatime;
 
        /* Waiting for window mapping */
        while(1) {
@@ -3730,17 +3758,15 @@ run(void) {
        ttynew();
        cresize(w, h);
 
-       gettimeofday(&last, NULL);
+       clock_gettime(CLOCK_MONOTONIC, &last);
        lastblink = last;
 
        for(xev = actionfps;;) {
-               long deltatime;
-
                FD_ZERO(&rfd);
                FD_SET(cmdfd, &rfd);
                FD_SET(xfd, &rfd);
 
-               if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv) < 0) {
+               if(pselect(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv, NULL) < 0) {
                        if(errno == EINTR)
                                continue;
                        die("select failed: %s\n", strerror(errno));
@@ -3757,9 +3783,9 @@ run(void) {
                if(FD_ISSET(xfd, &rfd))
                        xev = actionfps;
 
-               gettimeofday(&now, NULL);
+               clock_gettime(CLOCK_MONOTONIC, &now);
                drawtimeout.tv_sec = 0;
-               drawtimeout.tv_usec = (1000/xfps) * 1000;
+               drawtimeout.tv_nsec = (1000/xfps) * 1E6;
                tv = &drawtimeout;
 
                dodraw = 0;
@@ -3794,9 +3820,9 @@ run(void) {
                                if(blinkset) {
                                        if(TIMEDIFF(now, lastblink) \
                                                        > blinktimeout) {
-                                               drawtimeout.tv_usec = 1;
+                                               drawtimeout.tv_nsec = 1000;
                                        } else {
-                                               drawtimeout.tv_usec = (1000 * \
+                                               drawtimeout.tv_nsec = (1E6 * \
                                                        (blinktimeout - \
                                                        TIMEDIFF(now,
                                                                lastblink)));