JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Render only once in each main loop iteration
[st.git] / st.c
diff --git a/st.c b/st.c
index b013bca..397f6a6 100644 (file)
--- a/st.c
+++ b/st.c
@@ -53,8 +53,6 @@
 #define XK_NO_MOD     UINT_MAX
 #define XK_ANY_MOD    0
 
-#define SELECT_TIMEOUT (20*1000) /* 20 ms */
-#define DRAW_TIMEOUT  (20*1000) /* 20 ms */
 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
 
 #define SERRNO strerror(errno)
@@ -205,7 +203,6 @@ typedef struct {
        int ch; /* char height */
        int cw; /* char width  */
        char state; /* focus, redraw, visible */
-       struct timeval lastdraw;
 } XWindow;
 
 typedef struct {
@@ -250,7 +247,6 @@ static void drawregion(int, int, int, int);
 static void execsh(void);
 static void sigchld(int);
 static void run(void);
-static bool last_draw_too_old(void);
 
 static void csidump(void);
 static void csihandle(void);
@@ -292,7 +288,6 @@ static void ttywrite(const char *, size_t);
 static void xdraws(char *, Glyph, int, int, int, int);
 static void xhints(void);
 static void xclear(int, int, int, int);
-static void xcopy(void);
 static void xdrawcursor(void);
 static void xinit(void);
 static void xloadcols(void);
@@ -639,7 +634,6 @@ void selclear(XEvent *e) {
                return;
        sel.bx = -1;
        tsetdirt(sel.b.y, sel.e.y);
-       draw();
 }
 
 void
@@ -689,8 +683,6 @@ xsetsel(char *str) {
 
        clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
        XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
-
-       XFlush(xw.dpy);
 }
 
 void
@@ -733,7 +725,6 @@ brelease(XEvent *e) {
        }
        memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
        gettimeofday(&sel.tclick1, NULL);
-       draw();
 }
 
 void
@@ -750,7 +741,6 @@ bmotion(XEvent *e) {
                        int starty = MIN(oldey, sel.ey);
                        int endy = MAX(oldey, sel.ey);
                        tsetdirt(starty, endy);
-                       draw();
                }
        }
 }
@@ -2095,13 +2085,6 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
                XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
 }
 
-/* copy buffer pixmap to screen pixmap */
-void
-xcopy() {
-       XdbeSwapInfo swpinfo[1] = {{xw.win, XdbeCopied}};
-       XdbeSwapBuffers(xw.dpy, swpinfo, 1);
-}
-
 void
 xdrawcursor(void) {
        static int oldx = 0;
@@ -2122,8 +2105,6 @@ xdrawcursor(void) {
        } else
                xclear(oldx, oldy, oldx, oldy);
 
-       xcopy();
-
        /* draw the new one */
        if(!(term.c.state & CURSOR_HIDE)) {
                if(!(xw.state & WIN_FOCUSED))
@@ -2136,8 +2117,6 @@ xdrawcursor(void) {
                xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
                oldx = term.c.x, oldy = term.c.y;
        }
-
-       xcopy();
 }
 
 void
@@ -2150,14 +2129,16 @@ redraw(void) {
        struct timespec tv = {0, REDRAW_TIMEOUT * 1000};
        tfulldirt();
        draw();
+       XSync(xw.dpy, False); /* necessary for a good tput flash */
        nanosleep(&tv, NULL);
 }
 
 void
 draw() {
+       XdbeSwapInfo swpinfo[1] = {{xw.win, XdbeCopied}};
+
        drawregion(0, 0, term.col, term.row);
-       xcopy();
-       gettimeofday(&xw.lastdraw, NULL);
+       XdbeSwapBuffers(xw.dpy, swpinfo, 1);
 }
 
 void
@@ -2212,7 +2193,6 @@ expose(XEvent *ev) {
                if(!e->count)
                        xw.state &= ~WIN_REDRAW;
        }
-       xcopy();
 }
 
 void
@@ -2245,7 +2225,6 @@ focus(XEvent *ev) {
                xseturgency(0);
        } else
                xw.state &= ~WIN_FOCUSED;
-       draw();
 }
 
 char*
@@ -2321,7 +2300,6 @@ cmessage(XEvent *e) {
                } else if(e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
                        xw.state &= ~WIN_FOCUSED;
                }
-               draw();
        }
 }
 
@@ -2344,41 +2322,23 @@ resize(XEvent *e) {
        ttyresize(col, row);
 }
 
-bool
-last_draw_too_old(void) {
-       struct timeval now;
-       gettimeofday(&now, NULL);
-       return TIMEDIFF(now, xw.lastdraw) >= DRAW_TIMEOUT/1000;
-}
-
 void
 run(void) {
        XEvent ev;
        fd_set rfd;
        int xfd = XConnectionNumber(xw.dpy);
-       struct timeval timeout = {0};
-       bool stuff_to_print = 0;
 
        for(;;) {
                FD_ZERO(&rfd);
                FD_SET(cmdfd, &rfd);
                FD_SET(xfd, &rfd);
-               timeout.tv_sec  = 0;
-               timeout.tv_usec = SELECT_TIMEOUT;
-               if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, &timeout) < 0) {
+               if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
                        if(errno == EINTR)
                                continue;
                        die("select failed: %s\n", SERRNO);
                }
-               if(FD_ISSET(cmdfd, &rfd)) {
+               if(FD_ISSET(cmdfd, &rfd))
                        ttyread();
-                       stuff_to_print = 1;
-               }
-
-               if(stuff_to_print && last_draw_too_old()) {
-                       stuff_to_print = 0;
-                       draw();
-               }
 
                while(XPending(xw.dpy)) {
                        XNextEvent(xw.dpy, &ev);
@@ -2387,6 +2347,9 @@ run(void) {
                        if(handler[ev.type])
                                (handler[ev.type])(&ev);
                }
+
+               draw();
+               XFlush(xw.dpy);
        }
 }