JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fixed wrong nanosecond factor 10E6.
[st.git] / st.c
diff --git a/st.c b/st.c
index 8b65450..f9c9f7a 100644 (file)
--- a/st.c
+++ b/st.c
@@ -76,7 +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 TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_nsec-t2.tv_nsec)/1E6)
 #define CEIL(x) (((x) != (int) (x)) ? (x) + 1 : (x))
 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
 
@@ -180,7 +180,6 @@ typedef unsigned short ushort;
 
 typedef XftDraw *Draw;
 typedef XftColor Color;
-typedef Colormap Colormap;
 
 typedef struct {
        char c[UTF_SIZ]; /* character code */
@@ -295,8 +294,8 @@ typedef struct {
        char *clip;
        Atom xtarget;
        bool alt;
-       struct timeval tclick1;
-       struct timeval tclick2;
+       struct timespec tclick1;
+       struct timespec tclick2;
 } Selection;
 
 typedef union {
@@ -703,6 +702,9 @@ selected(int x, int y) {
 
 void
 selsnap(int mode, int *x, int *y, int direction) {
+       int newx, newy, xt, yt;
+       Glyph *gp;
+
        switch(mode) {
        case SNAP_WORD:
                /*
@@ -710,36 +712,31 @@ selsnap(int mode, int *x, int *y, int direction) {
                 * beginning of a line.
                 */
                for(;;) {
-                       if(direction < 0 && *x <= 0) {
-                               if(*y > 0 && term.line[*y - 1][term.col-1].mode
-                                               & ATTR_WRAP) {
-                                       *y -= 1;
-                                       *x = term.col-1;
-                               } else {
+                       newx = *x + direction;
+                       newy = *y;
+                       if(!BETWEEN(newx, 0, term.col - 1)) {
+                               newy += direction;
+                               newx = (newx + term.col) % term.col;
+                               if (!BETWEEN(newy, 0, term.row - 1))
                                        break;
-                               }
-                       }
-                       if(direction > 0 && *x >= term.col-1) {
-                               if(*y < term.row-1 && term.line[*y][*x].mode
-                                               & ATTR_WRAP) {
-                                       *y += 1;
-                                       *x = 0;
-                               } else {
+
+                               if(direction > 0)
+                                       yt = *y, xt = *x;
+                               else
+                                       yt = newy, xt = newx;
+                               if(!(term.line[yt][xt].mode & ATTR_WRAP))
                                        break;
-                               }
                        }
 
-                       if(term.line[*y][*x+direction].mode & ATTR_WDUMMY) {
-                               *x += direction;
-                               continue;
-                       }
+                       if (newx >= tlinelen(newy))
+                               break;
 
-                       if(*x >= tlinelen(*y) || strchr(worddelimiters,
-                                       term.line[*y][*x+direction].c[0])) {
+                       gp = &term.line[newy][newx];
+                       if (!(gp->mode & ATTR_WDUMMY) && strchr(worddelimiters, gp->c[0]))
                                break;
-                       }
 
-                       *x += direction;
+                       *x = newx;
+                       *y = newy;
                }
                break;
        case SNAP_LINE:
@@ -863,7 +860,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)) {
@@ -880,7 +877,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);
@@ -2318,19 +2315,15 @@ techo(char *buf, int len) {
 
 void
 tdeftran(char ascii) {
-       char c, (*bp)[2];
-       static char tbl[][2] = {
-               {'0', CS_GRAPHIC0}, {'B', CS_USA},
-               {0, 0}
-       };
+       static char cs[] = "0B";
+       static int vcs[] = {CS_GRAPHIC0, CS_USA};
+       char *p;
 
-       for (bp = &tbl[0]; (c = (*bp)[0]) && c != ascii; ++bp)
-               /* nothing */;
-
-       if (c == 0)
+       if((p = strchr(cs, ascii)) == NULL) {
                fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
-       else
-               term.trantbl[term.icharset] = (*bp)[1];
+       } else {
+               term.trantbl[term.icharset] = vcs[p - cs];
+       }
 }
 
 void
@@ -3000,6 +2993,7 @@ xzoom(const Arg *arg) {
        xloadfonts(usedfont, usedfontsize + arg->i);
        cresize(0, 0);
        redraw(0);
+       xhints();
 }
 
 void
@@ -3252,28 +3246,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)
@@ -3721,7 +3709,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) {
@@ -3737,17 +3726,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));
@@ -3764,9 +3751,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;
@@ -3801,9 +3788,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)));