JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Remove call to draw in resize
[st.git] / st.c
diff --git a/st.c b/st.c
index c2ee3e2..6371167 100644 (file)
--- a/st.c
+++ b/st.c
 #define ESC_ARG_SIZ   16
 #define STR_BUF_SIZ   256
 #define STR_ARG_SIZ   16
-#define DRAW_BUF_SIZ  1024
+#define DRAW_BUF_SIZ  20*1024
 #define UTF_SIZ       4
 #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 {
@@ -240,7 +237,7 @@ typedef struct {
                short lbearing;
                short rbearing;
                XFontSet set;
-       } font, bfont, ifont;
+       } font, bfont, ifont, ibfont;
 } DC;
 
 static void die(const char*, ...);
@@ -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);
@@ -566,8 +561,11 @@ bpress(XEvent *e) {
        if(IS_SET(MODE_MOUSE))
                mousereport(e);
        else if(e->xbutton.button == Button1) {
-               if(sel.bx != -1)
+               if(sel.bx != -1) {
+                       sel.bx = -1;
                        tsetdirt(sel.b.y, sel.e.y);
+                       draw();
+               }
                sel.mode = 1;
                sel.ex = sel.bx = X2COL(e->xbutton.x);
                sel.ey = sel.by = Y2ROW(e->xbutton.y);
@@ -639,7 +637,6 @@ void selclear(XEvent *e) {
                return;
        sel.bx = -1;
        tsetdirt(sel.b.y, sel.e.y);
-       draw();
 }
 
 void
@@ -689,8 +686,6 @@ xsetsel(char *str) {
 
        clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
        XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
-
-       XFlush(xw.dpy);
 }
 
 void
@@ -733,7 +728,6 @@ brelease(XEvent *e) {
        }
        memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
        gettimeofday(&sel.tclick1, NULL);
-       draw();
 }
 
 void
@@ -750,7 +744,6 @@ bmotion(XEvent *e) {
                        int starty = MIN(oldey, sel.ey);
                        int endy = MAX(oldey, sel.ey);
                        tsetdirt(starty, endy);
-                       draw();
                }
        }
 }
@@ -1723,6 +1716,8 @@ tputc(char *c) {
                if(sel.bx != -1 && BETWEEN(term.c.y, sel.by, sel.ey))
                        sel.bx = -1;
                switch(ascii) {
+               case '\0': /* padding character, do nothing */
+                       break;
                case '\t':
                        tputtab(1);
                        break;
@@ -1943,13 +1938,15 @@ xgetfontinfo(XFontSet set, int *ascent, int *descent, short *lbearing, short *rb
 }
 
 void
-initfonts(char *fontstr, char *bfontstr, char *ifontstr) {
+initfonts(char *fontstr, char *bfontstr, char *ifontstr, char *ibfontstr) {
        if((dc.font.set = xinitfont(fontstr)) == NULL)
                die("Can't load font %s\n", fontstr);
        if((dc.bfont.set = xinitfont(bfontstr)) == NULL)
                die("Can't load bfont %s\n", bfontstr);
        if((dc.ifont.set = xinitfont(ifontstr)) == NULL)
                die("Can't load ifont %s\n", ifontstr);
+       if((dc.ibfont.set = xinitfont(ibfontstr)) == NULL)
+               die("Can't load ibfont %s\n", ibfontstr);
 
        xgetfontinfo(dc.font.set, &dc.font.ascent, &dc.font.descent,
            &dc.font.lbearing, &dc.font.rbearing);
@@ -1957,6 +1954,8 @@ initfonts(char *fontstr, char *bfontstr, char *ifontstr) {
            &dc.bfont.lbearing, &dc.bfont.rbearing);
        xgetfontinfo(dc.ifont.set, &dc.ifont.ascent, &dc.ifont.descent,
            &dc.ifont.lbearing, &dc.ifont.rbearing);
+       xgetfontinfo(dc.ibfont.set, &dc.ibfont.ascent, &dc.ibfont.descent,
+           &dc.ibfont.lbearing, &dc.ibfont.rbearing);
 }
 
 void
@@ -1964,14 +1963,14 @@ xinit(void) {
        XSetWindowAttributes attrs;
        Cursor cursor;
        Window parent;
-       int sw, sh;
+       int sw, sh, major, minor;
 
        if(!(xw.dpy = XOpenDisplay(NULL)))
                die("Can't open display\n");
        xw.scr = XDefaultScreen(xw.dpy);
 
        /* font */
-       initfonts(FONT, BOLDFONT, ITALICFONT);
+       initfonts(FONT, BOLDFONT, ITALICFONT, ITALICBOLDFONT);
 
        /* XXX: Assuming same size for bold font */
        xw.cw = dc.font.rbearing - dc.font.lbearing;
@@ -2015,9 +2014,10 @@ xinit(void) {
                        CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
                        | CWColormap,
                        &attrs);
+       if(!XdbeQueryExtension(xw.dpy, &major, &minor))
+               die("Xdbe extension is not present\n");
        xw.buf = XdbeAllocateBackBufferName(xw.dpy, xw.win, XdbeCopied);
 
-
        /* input methods */
        xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
        xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
@@ -2066,6 +2066,8 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
 
        if(base.mode & ATTR_ITALIC)
                fontset = dc.ifont.set;
+       if(base.mode & (ATTR_ITALIC|ATTR_ITALIC))
+               fontset = dc.ibfont.set;
 
        XSetBackground(xw.dpy, dc.gc, dc.col[bg]);
        XSetForeground(xw.dpy, dc.gc, dc.col[fg]);
@@ -2086,13 +2088,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;
@@ -2113,8 +2108,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))
@@ -2127,8 +2120,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
@@ -2141,14 +2132,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
@@ -2203,7 +2196,6 @@ expose(XEvent *ev) {
                if(!e->count)
                        xw.state &= ~WIN_REDRAW;
        }
-       xcopy();
 }
 
 void
@@ -2236,7 +2228,6 @@ focus(XEvent *ev) {
                xseturgency(0);
        } else
                xw.state &= ~WIN_FOCUSED;
-       draw();
 }
 
 char*
@@ -2312,7 +2303,6 @@ cmessage(XEvent *e) {
                } else if(e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
                        xw.state &= ~WIN_FOCUSED;
                }
-               draw();
        }
 }
 
@@ -2329,46 +2319,48 @@ resize(XEvent *e) {
        row = (xw.h - 2*BORDER) / xw.ch;
        if(col == term.col && row == term.row)
                return;
-       if(tresize(col, row))
-               draw();
+       tresize(col, row);
        xresize(col, row);
        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;
+       int xfd = XConnectionNumber(xw.dpy), i;
+       struct timeval drawtimeout;
 
        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)) {
+
+               /*
+                * Stop after a certain number of reads so the user does not
+                * feel like the system is stuttering.
+                */
+               for(i = 0; i < 1000 && FD_ISSET(cmdfd, &rfd); i++) {
                        ttyread();
-                       stuff_to_print = 1;
-               }
 
-               if(stuff_to_print && last_draw_too_old()) {
-                       stuff_to_print = 0;
-                       draw();
+                       FD_ZERO(&rfd);
+                       FD_SET(cmdfd, &rfd);
+                       /*
+                        * Just wait a bit so it isn't disturbing the
+                        * user and the system is able to write something.
+                        */
+                       drawtimeout.tv_sec = 0;
+                       drawtimeout.tv_usec = 5;
+                       if(select(cmdfd+1, &rfd, NULL, NULL, &drawtimeout) < 0) {
+                               if(errno == EINTR)
+                                       continue;
+                               die("select failed: %s\n", SERRNO);
+                       }
                }
 
                while(XPending(xw.dpy)) {
@@ -2378,6 +2370,9 @@ run(void) {
                        if(handler[ev.type])
                                (handler[ev.type])(&ev);
                }
+
+               draw();
+               XFlush(xw.dpy);
        }
 }