+ tmoveto(x, term.c.y);
+}
+
+void
+tputc(char *c) {
+ char ascii = *c;
+
+ if(fileio)
+ putc(ascii, fileio);
+
+ if(term.esc & ESC_START) {
+ if(term.esc & ESC_CSI) {
+ csiescseq.buf[csiescseq.len++] = ascii;
+ if(BETWEEN(ascii, 0x40, 0x7E) || csiescseq.len >= ESC_BUF_SIZ) {
+ term.esc = 0;
+ csiparse(), csihandle();
+ }
+ } else if(term.esc & ESC_STR) {
+ switch(ascii) {
+ case '\033':
+ term.esc = ESC_START | ESC_STR_END;
+ break;
+ case '\a': /* backwards compatibility to xterm */
+ term.esc = 0;
+ strhandle();
+ break;
+ default:
+ strescseq.buf[strescseq.len++] = ascii;
+ if(strescseq.len+1 >= STR_BUF_SIZ) {
+ term.esc = 0;
+ strhandle();
+ }
+ }
+ } else if(term.esc & ESC_STR_END) {
+ term.esc = 0;
+ if(ascii == '\\')
+ strhandle();
+ } else if(term.esc & ESC_ALTCHARSET) {
+ switch(ascii) {
+ case '0': /* Line drawing crap */
+ term.c.attr.mode |= ATTR_GFX;
+ break;
+ case 'B': /* Back to regular text */
+ term.c.attr.mode &= ~ATTR_GFX;
+ break;
+ default:
+ fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
+ }
+ term.esc = 0;
+ } else {
+ switch(ascii) {
+ case '[':
+ term.esc |= ESC_CSI;
+ break;
+ case 'P': /* DCS -- Device Control String */
+ case '_': /* APC -- Application Program Command */
+ case '^': /* PM -- Privacy Message */
+ case ']': /* OSC -- Operating System Command */
+ strreset();
+ strescseq.type = ascii;
+ term.esc |= ESC_STR;
+ break;
+ case '(':
+ term.esc |= ESC_ALTCHARSET;
+ break;
+ case 'D': /* IND -- Linefeed */
+ if(term.c.y == term.bot)
+ tscrollup(term.top, 1);
+ else
+ tmoveto(term.c.x, term.c.y+1);
+ term.esc = 0;
+ break;
+ case 'E': /* NEL -- Next line */
+ tnewline(1); /* always go to first col */
+ term.esc = 0;
+ break;
+ case 'H': /* HTS -- Horizontal tab stop */
+ term.tabs[term.c.x] = 1;
+ term.esc = 0;
+ break;
+ case 'M': /* RI -- Reverse index */
+ if(term.c.y == term.top)
+ tscrolldown(term.top, 1);
+ else
+ tmoveto(term.c.x, term.c.y-1);
+ term.esc = 0;
+ break;
+ case 'c': /* RIS -- Reset to inital state */
+ treset();
+ term.esc = 0;
+ break;
+ case '=': /* DECPAM -- Application keypad */
+ term.mode |= MODE_APPKEYPAD;
+ term.esc = 0;
+ break;
+ case '>': /* DECPNM -- Normal keypad */
+ term.mode &= ~MODE_APPKEYPAD;
+ term.esc = 0;
+ break;
+ case '7': /* DECSC -- Save Cursor */
+ tcursor(CURSOR_SAVE);
+ term.esc = 0;
+ break;
+ case '8': /* DECRC -- Restore Cursor */
+ tcursor(CURSOR_LOAD);
+ term.esc = 0;
+ break;
+ case '\\': /* ST -- Stop */
+ term.esc = 0;
+ break;
+ default:
+ fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
+ (uchar) ascii, isprint(ascii)?ascii:'.');
+ term.esc = 0;
+ }
+ }
+ } else {
+ if(sel.bx != -1 && BETWEEN(term.c.y, sel.by, sel.ey))
+ sel.bx = -1;
+ switch(ascii) {
+ case '\t':
+ tputtab(1);
+ break;
+ case '\b':
+ tmoveto(term.c.x-1, term.c.y);
+ break;
+ case '\r':
+ tmoveto(0, term.c.y);
+ break;
+ case '\f':
+ case '\v':
+ case '\n':
+ /* go to first col if the mode is set */
+ tnewline(IS_SET(MODE_CRLF));
+ break;
+ case '\a':
+ if(!(xw.state & WIN_FOCUSED))
+ xseturgency(1);
+ break;
+ case '\033':
+ csireset();
+ term.esc = ESC_START;
+ break;
+ default:
+ if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
+ tnewline(1); /* always go to first col */
+ tsetchar(c);
+ if(term.c.x+1 < term.col)
+ tmoveto(term.c.x+1, term.c.y);
+ else
+ term.c.state |= CURSOR_WRAPNEXT;
+ }
+ }
+}
+
+int
+tresize(int col, int row) {
+ int i, x;
+ int minrow = MIN(row, term.row);
+ int mincol = MIN(col, term.col);
+ int slide = term.c.y - row + 1;
+
+ if(col < 1 || row < 1)
+ return 0;
+
+ /* free unneeded rows */
+ i = 0;
+ if(slide > 0) {
+ /* slide screen to keep cursor where we expect it -
+ * tscrollup would work here, but we can optimize to
+ * memmove because we're freeing the earlier lines */
+ for(/* i = 0 */; i < slide; i++) {
+ free(term.line[i]);
+ free(term.alt[i]);
+ }
+ memmove(term.line, term.line + slide, row * sizeof(Line));
+ memmove(term.alt, term.alt + slide, row * sizeof(Line));
+ }
+ for(i += row; i < term.row; i++) {
+ free(term.line[i]);
+ free(term.alt[i]);
+ }
+
+ /* resize to new height */
+ term.line = realloc(term.line, row * sizeof(Line));
+ term.alt = realloc(term.alt, row * sizeof(Line));
+ term.dirty = realloc(term.dirty, row * sizeof(*term.dirty));
+ term.tabs = realloc(term.tabs, col * sizeof(*term.tabs));
+
+ /* resize each row to new width, zero-pad if needed */
+ for(i = 0; i < minrow; i++) {
+ term.dirty[i] = 1;
+ term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
+ term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
+ for(x = mincol; x < col; x++) {
+ term.line[i][x].state = 0;
+ term.alt[i][x].state = 0;
+ }
+ }
+
+ /* 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));
+ }
+ if(col > term.col) {
+ bool *bp = term.tabs + term.col;
+
+ memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
+ while(--bp > term.tabs && !*bp)
+ /* nothing */ ;
+ for(bp += TAB; bp < term.tabs + col; bp += TAB)
+ *bp = 1;
+ }
+ /* update terminal size */
+ term.col = col, term.row = row;
+ /* make use of the LIMIT in tmoveto */
+ tmoveto(term.c.x, term.c.y);
+ /* reset scrolling region */
+ tsetscroll(0, row-1);
+
+ return (slide > 0);
+}
+
+void
+xresize(int col, int row) {
+ xw.w = MAX(1, 2*BORDER + col * xw.cw);
+ xw.h = MAX(1, 2*BORDER + row * xw.ch);
+}
+
+void
+xloadcols(void) {
+ int i, r, g, b;
+ XColor color;
+ ulong white = WhitePixel(xw.dpy, xw.scr);
+
+ /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
+ for(i = 0; i < LEN(colorname); i++) {
+ if(!colorname[i])
+ continue;
+ if(!XAllocNamedColor(xw.dpy, xw.cmap, colorname[i], &color, &color)) {
+ dc.col[i] = white;
+ fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
+ } else
+ dc.col[i] = color.pixel;
+ }
+
+ /* load colors [16-255] ; same colors as xterm */
+ for(i = 16, r = 0; r < 6; r++)
+ for(g = 0; g < 6; g++)
+ for(b = 0; b < 6; b++) {
+ color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
+ color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
+ color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
+ if(!XAllocColor(xw.dpy, xw.cmap, &color)) {
+ dc.col[i] = white;
+ fprintf(stderr, "Could not allocate color %d\n", i);
+ } else
+ dc.col[i] = color.pixel;
+ i++;
+ }
+
+ for(r = 0; r < 24; r++, i++) {
+ color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
+ if(!XAllocColor(xw.dpy, xw.cmap, &color)) {
+ dc.col[i] = white;
+ fprintf(stderr, "Could not allocate color %d\n", i);
+ } else
+ dc.col[i] = color.pixel;
+ }
+}
+
+void
+xclear(int x1, int y1, int x2, int y2) {
+ XSetForeground(xw.dpy, dc.gc, dc.col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG]);
+ XFillRectangle(xw.dpy, xw.buf, dc.gc,
+ BORDER + x1 * xw.cw, BORDER + y1 * xw.ch,
+ (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
+}
+
+void
+xhints(void) {
+ XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
+ XWMHints wm = {.flags = InputHint, .input = 1};
+ XSizeHints *sizeh = NULL;
+
+ sizeh = XAllocSizeHints();
+ if(xw.isfixed == False) {
+ sizeh->flags = PSize | PResizeInc | PBaseSize;
+ sizeh->height = xw.h;
+ sizeh->width = xw.w;
+ sizeh->height_inc = xw.ch;
+ sizeh->width_inc = xw.cw;
+ sizeh->base_height = 2*BORDER;
+ sizeh->base_width = 2*BORDER;
+ } else {
+ sizeh->flags = PMaxSize | PMinSize;
+ sizeh->min_width = sizeh->max_width = xw.fw;
+ sizeh->min_height = sizeh->max_height = xw.fh;
+ }
+
+ XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm, &class);
+ XFree(sizeh);
+}
+
+XFontSet
+xinitfont(char *fontstr) {
+ XFontSet set;
+ char *def, **missing;
+ int n;
+
+ missing = NULL;
+ set = XCreateFontSet(xw.dpy, fontstr, &missing, &n, &def);
+ if(missing) {
+ while(n--)
+ fprintf(stderr, "st: missing fontset: %s\n", missing[n]);
+ XFreeStringList(missing);
+ }
+ return set;
+}
+
+void
+xgetfontinfo(XFontSet set, int *ascent, int *descent, short *lbearing, short *rbearing) {
+ XFontStruct **xfonts;
+ char **font_names;
+ int i, n;
+
+ *ascent = *descent = *lbearing = *rbearing = 0;
+ n = XFontsOfFontSet(set, &xfonts, &font_names);
+ for(i = 0; i < n; i++) {
+ *ascent = MAX(*ascent, (*xfonts)->ascent);
+ *descent = MAX(*descent, (*xfonts)->descent);
+ *lbearing = MAX(*lbearing, (*xfonts)->min_bounds.lbearing);
+ *rbearing = MAX(*rbearing, (*xfonts)->max_bounds.rbearing);
+ xfonts++;
+ }
+}
+
+void
+initfonts(char *fontstr, char *bfontstr) {
+ if((dc.font.set = xinitfont(fontstr)) == NULL ||
+ (dc.bfont.set = xinitfont(bfontstr)) == NULL)
+ die("Can't load font %s\n", dc.font.set ? BOLDFONT : FONT);
+ xgetfontinfo(dc.font.set, &dc.font.ascent, &dc.font.descent,
+ &dc.font.lbearing, &dc.font.rbearing);
+ xgetfontinfo(dc.bfont.set, &dc.bfont.ascent, &dc.bfont.descent,
+ &dc.bfont.lbearing, &dc.bfont.rbearing);
+}
+
+void
+xinit(void) {
+ XSetWindowAttributes attrs;
+ Cursor cursor;
+ Window parent;
+ int sw, sh;
+
+ if(!(xw.dpy = XOpenDisplay(NULL)))
+ die("Can't open display\n");
+ xw.scr = XDefaultScreen(xw.dpy);
+
+ /* adjust fixed window geometry */
+ if(xw.isfixed) {
+ sw = DisplayWidth(xw.dpy, xw.scr);
+ sh = DisplayHeight(xw.dpy, xw.scr);
+ if(xw.fx < 0)
+ xw.fx = sw + xw.fx - xw.fw - 1;
+ if(xw.fy < 0)
+ xw.fy = sh + xw.fy - xw.fh - 1;
+
+ xw.h = xw.fh;
+ xw.w = xw.fw;
+ } else {
+ /* window - default size */
+ xw.h = 2*BORDER + term.row * xw.ch;
+ xw.w = 2*BORDER + term.col * xw.cw;
+ xw.fx = 0;
+ xw.fy = 0;
+ }
+
+ /* font */
+ initfonts(FONT, BOLDFONT);
+
+ /* XXX: Assuming same size for bold font */
+ xw.cw = dc.font.rbearing - dc.font.lbearing;
+ xw.ch = dc.font.ascent + dc.font.descent;
+
+ /* colors */
+ xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
+ xloadcols();
+
+ attrs.background_pixel = dc.col[DefaultBG];
+ attrs.border_pixel = dc.col[DefaultBG];
+ attrs.bit_gravity = NorthWestGravity;
+ attrs.event_mask = FocusChangeMask | KeyPressMask
+ | ExposureMask | VisibilityChangeMask | StructureNotifyMask
+ | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask
+ | EnterWindowMask | LeaveWindowMask;
+ attrs.colormap = xw.cmap;
+
+ parent = opt_embed ? strtol(opt_embed, NULL, 0) : XRootWindow(xw.dpy, xw.scr);
+ xw.win = XCreateWindow(xw.dpy, parent, xw.fx, xw.fy,
+ xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
+ XDefaultVisual(xw.dpy, xw.scr),
+ CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
+ | CWColormap,
+ &attrs);
+ 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
+ | XIMStatusNothing, XNClientWindow, xw.win,
+ XNFocusWindow, xw.win, NULL);
+ /* gc */
+ dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
+
+ /* white cursor, black outline */
+ cursor = XCreateFontCursor(xw.dpy, XC_xterm);
+ XDefineCursor(xw.dpy, xw.win, cursor);
+ XRecolorCursor(xw.dpy, cursor,
+ &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
+ &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
+
+ xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
+
+ XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
+ XMapWindow(xw.dpy, xw.win);
+ xhints();
+ XSync(xw.dpy, 0);
+}
+
+void
+xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
+ int fg = base.fg, bg = base.bg, temp;
+ int winx = BORDER+x*xw.cw, winy = BORDER+y*xw.ch + dc.font.ascent, width = charlen*xw.cw;
+ XFontSet fontset = dc.font.set;
+ int i;
+
+ /* only switch default fg/bg if term is in RV mode */
+ if(IS_SET(MODE_REVERSE)) {
+ if(fg == DefaultFG)
+ fg = DefaultBG;
+ if(bg == DefaultBG)
+ bg = DefaultFG;
+ }
+
+ if(base.mode & ATTR_REVERSE)
+ temp = fg, fg = bg, bg = temp;
+
+ if(base.mode & ATTR_BOLD) {
+ fg += 8;
+ fontset = dc.bfont.set;
+ }
+
+ XSetBackground(xw.dpy, dc.gc, dc.col[bg]);
+ XSetForeground(xw.dpy, dc.gc, dc.col[fg]);
+
+ if(base.mode & ATTR_GFX) {
+ for(i = 0; i < bytelen; i++) {
+ char c = gfx[(uint)s[i] % 256];
+ if(c)
+ s[i] = c;
+ else if(s[i] > 0x5f)
+ s[i] -= 0x5f;
+ }
+ }
+
+ XmbDrawImageString(xw.dpy, xw.buf, fontset, dc.gc, winx, winy, s, bytelen);
+
+ if(base.mode & ATTR_UNDERLINE)
+ 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;
+ static int oldy = 0;
+ int sl;
+ Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
+
+ LIMIT(oldx, 0, term.col-1);
+ LIMIT(oldy, 0, term.row-1);
+
+ if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
+ memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
+
+ /* remove the old cursor */
+ if(term.line[oldy][oldx].state & GLYPH_SET) {
+ sl = utf8size(term.line[oldy][oldx].c);
+ xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1, sl);
+ } else
+ xclear(oldx, oldy, oldx, oldy);
+
+ xcopy(oldx, oldy, 1, 1);
+
+ /* draw the new one */
+ if(!(term.c.state & CURSOR_HIDE)) {
+ if(!(xw.state & WIN_FOCUSED))
+ g.bg = DefaultUCS;
+
+ if(IS_SET(MODE_REVERSE))
+ g.mode |= ATTR_REVERSE, g.fg = DefaultCS, g.bg = DefaultFG;
+
+ sl = utf8size(g.c);
+ xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
+ oldx = term.c.x, oldy = term.c.y;
+ }
+
+ xcopy(term.c.x, term.c.y, 1, 1);
+}
+
+void
+redraw(void) {
+ struct timespec tv = {0, REDRAW_TIMEOUT * 1000};
+ tfulldirt();
+ draw();
+ nanosleep(&tv, NULL);
+}
+
+void
+draw() {
+ drawregion(0, 0, term.col, term.row);
+ xcopy();
+ gettimeofday(&xw.lastdraw, NULL);
+}
+
+void
+drawregion(int x1, int y1, int x2, int y2) {
+ int ic, ib, x, y, ox, sl;
+ Glyph base, new;
+ char buf[DRAW_BUF_SIZ];
+ bool ena_sel = sel.bx != -1, alt = IS_SET(MODE_ALTSCREEN);
+
+ if((sel.alt && !alt) || (!sel.alt && alt))
+ ena_sel = 0;
+ if(!(xw.state & WIN_VISIBLE))
+ return;
+
+ for(y = y1; y < y2; y++) {
+ if(!term.dirty[y])
+ continue;
+ xclear(0, y, term.col, y);
+ term.dirty[y] = 0;
+ base = term.line[y][0];
+ ic = ib = ox = 0;
+ for(x = x1; x < x2; x++) {
+ new = term.line[y][x];
+ if(ena_sel && *(new.c) && selected(x, y))
+ new.mode ^= ATTR_REVERSE;
+ if(ib > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
+ ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
+ xdraws(buf, base, ox, y, ic, ib);
+ ic = ib = 0;
+ }
+ if(new.state & GLYPH_SET) {
+ if(ib == 0) {
+ ox = x;
+ base = new;
+ }
+ sl = utf8size(new.c);
+ memcpy(buf+ib, new.c, sl);
+ ib += sl;
+ ++ic;
+ }
+ }
+ if(ib > 0)
+ xdraws(buf, base, ox, y, ic, ib);
+ }
+ xdrawcursor();
+}
+
+void
+expose(XEvent *ev) {
+ XExposeEvent *e = &ev->xexpose;
+ if(xw.state & WIN_REDRAW) {
+ if(!e->count)
+ xw.state &= ~WIN_REDRAW;
+ }
+ xcopy();
+}
+
+void
+visibility(XEvent *ev) {
+ XVisibilityEvent *e = &ev->xvisibility;
+ if(e->state == VisibilityFullyObscured)
+ xw.state &= ~WIN_VISIBLE;
+ else if(!(xw.state & WIN_VISIBLE))
+ /* need a full redraw for next Expose, not just a buf copy */
+ xw.state |= WIN_VISIBLE | WIN_REDRAW;
+}
+
+void
+unmap(XEvent *ev) {
+ xw.state &= ~WIN_VISIBLE;
+}
+
+void
+xseturgency(int add) {
+ XWMHints *h = XGetWMHints(xw.dpy, xw.win);
+ h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
+ XSetWMHints(xw.dpy, xw.win, h);
+ XFree(h);
+}
+
+void
+focus(XEvent *ev) {
+ if(ev->type == FocusIn) {
+ xw.state |= WIN_FOCUSED;
+ xseturgency(0);
+ } else
+ xw.state &= ~WIN_FOCUSED;
+ draw();
+}
+
+char*
+kmap(KeySym k, uint state) {
+ int i;
+ state &= ~Mod2Mask;
+ for(i = 0; i < LEN(key); i++) {
+ uint mask = key[i].mask;
+ if(key[i].k == k && ((state & mask) == mask || (mask == XK_NO_MOD && !state)))
+ return (char*)key[i].s;
+ }
+ return NULL;
+}
+
+void
+kpress(XEvent *ev) {
+ XKeyEvent *e = &ev->xkey;
+ KeySym ksym;
+ char buf[32];
+ char *customkey;
+ int len;
+ int meta;
+ int shift;
+ Status status;
+
+ meta = e->state & Mod1Mask;
+ shift = e->state & ShiftMask;
+ len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
+
+ /* 1. custom keys from config.h */
+ if((customkey = kmap(ksym, e->state)))
+ ttywrite(customkey, strlen(customkey));
+ /* 2. hardcoded (overrides X lookup) */
+ else
+ switch(ksym) {
+ case XK_Up:
+ case XK_Down:
+ case XK_Left:
+ case XK_Right:
+ /* XXX: shift up/down doesn't work */
+ sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', (shift ? "dacb":"DACB")[ksym - XK_Left]);
+ ttywrite(buf, 3);
+ break;
+ case XK_Insert:
+ if(shift)
+ selpaste();
+ break;
+ case XK_Return:
+ if(IS_SET(MODE_CRLF))
+ ttywrite("\r\n", 2);
+ else
+ ttywrite("\r", 1);
+ break;
+ /* 3. X lookup */
+ default:
+ if(len > 0) {
+ if(meta && len == 1)
+ ttywrite("\033", 1);
+ ttywrite(buf, len);
+ }
+ break;
+ }
+}
+
+void
+cmessage(XEvent *e) {
+ /* See xembed specs
+ http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
+ if(e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
+ if(e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
+ xw.state |= WIN_FOCUSED;
+ xseturgency(0);
+ } else if(e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
+ xw.state &= ~WIN_FOCUSED;
+ }
+ draw();
+ }
+}
+
+void
+resize(XEvent *e) {
+ int col, row;
+
+ if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
+ return;
+
+ xw.w = e->xconfigure.width;
+ xw.h = e->xconfigure.height;
+ col = (xw.w - 2*BORDER) / xw.cw;
+ row = (xw.h - 2*BORDER) / xw.ch;
+ if(col == term.col && row == term.row)
+ return;
+ if(tresize(col, row))
+ draw();
+ 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;
+
+ 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(errno == EINTR)
+ continue;
+ die("select failed: %s\n", SERRNO);
+ }
+ 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);
+ if(XFilterEvent(&ev, xw.win))
+ continue;
+ if(handler[ev.type])
+ (handler[ev.type])(&ev);
+ }
+ }
+}
+
+int
+main(int argc, char *argv[]) {
+ int i, bitm, xr, yr;
+ unsigned int wr, hr;
+
+ xw.fw = xw.fh = xw.fx = xw.fy = 0;
+ xw.isfixed = False;
+
+ for(i = 1; i < argc; i++) {
+ switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
+ case 't':
+ if(++i < argc) opt_title = argv[i];
+ break;
+ case 'c':
+ if(++i < argc) opt_class = argv[i];
+ break;
+ case 'w':
+ if(++i < argc) opt_embed = argv[i];
+ break;
+ case 'f':
+ if(++i < argc) opt_io = argv[i];
+ break;
+ case 'e':
+ /* eat every remaining arguments */
+ if(++i < argc) opt_cmd = &argv[i];
+ goto run;
+ case 'g':
+ if(++i >= argc)
+ break;
+
+ bitm = XParseGeometry(argv[i], &xr, &yr, &wr, &hr);
+ if(bitm & XValue)
+ xw.fx = xr;
+ if(bitm & YValue)
+ xw.fy = yr;
+ if(bitm & WidthValue)
+ xw.fw = (int)wr;
+ if(bitm & HeightValue)
+ xw.fh = (int)hr;
+ if(bitm & XNegative && xw.fx == 0)
+ xw.fx = -1;
+ if(bitm & XNegative && xw.fy == 0)
+ xw.fy = -1;
+
+ if(xw.fh != 0 && xw.fw != 0)
+ xw.isfixed = True;
+ break;
+ case 'v':
+ default:
+ die(USAGE);
+ }
+ }
+
+ run:
+ setlocale(LC_CTYPE, "");
+ tnew(80, 24);
+ ttynew();
+ xinit();
+ selinit();
+ run();