X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=st.c;h=89c59b4145f9fdebe5b523c6129188fc0645a2ee;hb=50a619cee3093ad00a5672075b1f8914467d37e0;hp=da6f17ef351cc87ae0f57c1cd2887301c42dd51e;hpb=9fbafe55c996324a73a0be3af80edcd39e5a393b;p=st.git diff --git a/st.c b/st.c index da6f17e..89c59b4 100644 --- a/st.c +++ b/st.c @@ -198,6 +198,7 @@ typedef struct { int scr; Bool isfixed; /* is fixed geometry? */ int fx, fy, fw, fh; /* fixed geometry */ + int tw, th; /* tty width and height */ int w; /* window width */ int h; /* window height */ int ch; /* char height */ @@ -324,6 +325,7 @@ static int isfullutf8(char *, int); static void *xmalloc(size_t); static void *xrealloc(void *, size_t); +static void *xcalloc(size_t nmemb, size_t size); static void (*handler[LASTEvent])(XEvent *) = { [KeyPress] = kpress, @@ -362,14 +364,22 @@ void * xmalloc(size_t len) { void *p = malloc(len); if(!p) - die("Out of memory"); + die("Out of memory\n"); return p; } void * xrealloc(void *p, size_t len) { if((p = realloc(p, len)) == NULL) - die("Out of memory"); + die("Out of memory\n"); + return p; +} + +void * +xcalloc(size_t nmemb, size_t size) { + void *p = calloc(nmemb, size); + if(!p) + die("Out of memory\n"); return p; } @@ -587,14 +597,17 @@ selcopy(void) { /* append every set & selected glyph to the selection */ for(y = 0; y < term.row; y++) { for(x = 0; x < term.col; x++) { - is_selected = selected(x, y); - if((term.line[y][x].state & GLYPH_SET) && is_selected) { - int size = utf8size(term.line[y][x].c); - memcpy(ptr, term.line[y][x].c, size); - ptr += size; - } - } + int size; + char *p; + Glyph *gp = &term.line[y][x]; + if(!(is_selected = selected(x, y))) + continue; + p = (gp->state & GLYPH_SET) ? gp->c : " "; + size = utf8size(p); + memcpy(ptr, p, size); + ptr += size; + } /* \n at the end of every selected line except for the last one */ if(is_selected && y < sel.e.y) *ptr++ = '\n'; @@ -882,8 +895,8 @@ ttyresize(int x, int y) { w.ws_row = term.row; w.ws_col = term.col; - w.ws_xpixel = xw.w; - w.ws_ypixel = xw.h; + w.ws_xpixel = xw.tw; + w.ws_ypixel = xw.th; if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0) fprintf(stderr, "Couldn't set window size: %s\n", SERRNO); } @@ -1801,8 +1814,8 @@ tresize(int col, int row) { /* allocate any new rows */ for(/* i == minrow */; i < row; i++) { term.dirty[i] = 1; - term.line[i] = calloc(col, sizeof(Glyph)); - term.alt [i] = calloc(col, sizeof(Glyph)); + term.line[i] = xcalloc(col, sizeof(Glyph)); + term.alt [i] = xcalloc(col, sizeof(Glyph)); } if(col > term.col) { bool *bp = term.tabs + term.col; @@ -1825,8 +1838,8 @@ tresize(int col, int row) { void xresize(int col, int row) { - xw.w = MAX(1, 2*BORDER + col * xw.cw); - xw.h = MAX(1, 2*BORDER + row * xw.ch); + xw.tw = MAX(1, 2*BORDER + col * xw.cw); + xw.th = MAX(1, 2*BORDER + row * xw.ch); } void @@ -2130,6 +2143,8 @@ xresettitle(void) { void redraw(void) { struct timespec tv = {0, REDRAW_TIMEOUT * 1000}; + + xclear(0, 0, xw.w, xw.h); tfulldirt(); draw(); XSync(xw.dpy, False); /* necessary for a good tput flash */ @@ -2319,8 +2334,9 @@ resize(XEvent *e) { row = (xw.h - 2*BORDER) / xw.ch; if(col == term.col && row == term.row) return; - if(tresize(col, row)) - draw(); + + xclear(0, 0, xw.w, xw.h); + tresize(col, row); xresize(col, row); ttyresize(col, row); } @@ -2330,13 +2346,13 @@ run(void) { XEvent ev; fd_set rfd; int xfd = XConnectionNumber(xw.dpy), i; - struct timeval drawtimeout; + struct timeval drawtimeout, *tv = NULL; - for(;;) { + for(i = 0;; i++) { FD_ZERO(&rfd); FD_SET(cmdfd, &rfd); FD_SET(xfd, &rfd); - if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) { + if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv) < 0) { if(errno == EINTR) continue; die("select failed: %s\n", SERRNO); @@ -2346,23 +2362,20 @@ run(void) { * 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++) { + if(i < 1000 && FD_ISSET(cmdfd, &rfd)) { ttyread(); - 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); - } + tv = &drawtimeout; + continue; } + i = 0; + tv = NULL; while(XPending(xw.dpy)) { XNextEvent(xw.dpy, &ev);