* st + patches and config
       
   URI git clone git://git.codevoid.de/st-sdk
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       x.c (47444B)
       ---
            1 /* See LICENSE for license details. */
            2 #include <errno.h>
            3 #include <math.h>
            4 #include <limits.h>
            5 #include <locale.h>
            6 #include <signal.h>
            7 #include <sys/select.h>
            8 #include <time.h>
            9 #include <unistd.h>
           10 #include <libgen.h>
           11 #include <X11/Xatom.h>
           12 #include <X11/Xlib.h>
           13 #include <X11/cursorfont.h>
           14 #include <X11/keysym.h>
           15 #include <X11/Xft/Xft.h>
           16 #include <X11/XKBlib.h>
           17 
           18 char *argv0;
           19 #include "arg.h"
           20 #include "st.h"
           21 #include "win.h"
           22 
           23 /* types used in config.h */
           24 typedef struct {
           25         uint mod;
           26         KeySym keysym;
           27         void (*func)(const Arg *);
           28         const Arg arg;
           29 } Shortcut;
           30 
           31 typedef struct {
           32         uint mod;
           33         uint button;
           34         void (*func)(const Arg *);
           35         const Arg arg;
           36         uint  release;
           37 } MouseShortcut;
           38 
           39 typedef struct {
           40         KeySym k;
           41         uint mask;
           42         char *s;
           43         /* three-valued logic variables: 0 indifferent, 1 on, -1 off */
           44         signed char appkey;    /* application keypad */
           45         signed char appcursor; /* application cursor */
           46 } Key;
           47 
           48 /* X modifiers */
           49 #define XK_ANY_MOD    UINT_MAX
           50 #define XK_NO_MOD     0
           51 #define XK_SWITCH_MOD (1<<13)
           52 
           53 /* function definitions used in config.h */
           54 static void clipcopy(const Arg *);
           55 static void clippaste(const Arg *);
           56 static void numlock(const Arg *);
           57 static void selpaste(const Arg *);
           58 static void zoom(const Arg *);
           59 static void zoomabs(const Arg *);
           60 static void zoomreset(const Arg *);
           61 static void ttysend(const Arg *);
           62 
           63 /* config.h for applying patches and the configuration. */
           64 #include "config.h"
           65 
           66 /* XEMBED messages */
           67 #define XEMBED_FOCUS_IN  4
           68 #define XEMBED_FOCUS_OUT 5
           69 
           70 /* macros */
           71 #define IS_SET(flag)                ((win.mode & (flag)) != 0)
           72 #define TRUERED(x)                (((x) & 0xff0000) >> 8)
           73 #define TRUEGREEN(x)                (((x) & 0xff00))
           74 #define TRUEBLUE(x)                (((x) & 0xff) << 8)
           75 
           76 typedef XftDraw *Draw;
           77 typedef XftColor Color;
           78 typedef XftGlyphFontSpec GlyphFontSpec;
           79 
           80 /* Purely graphic info */
           81 typedef struct {
           82         int tw, th; /* tty width and height */
           83         int w, h; /* window width and height */
           84         int ch; /* char height */
           85         int cw; /* char width  */
           86         int mode; /* window state/mode flags */
           87         int cursor; /* cursor style */
           88 } TermWindow;
           89 
           90 typedef struct {
           91         Display *dpy;
           92         Colormap cmap;
           93         Window win;
           94         Drawable buf;
           95         GlyphFontSpec *specbuf; /* font spec buffer used for rendering */
           96         Atom xembed, wmdeletewin, netwmname, netwmiconname, netwmpid;
           97         struct {
           98                 XIM xim;
           99                 XIC xic;
          100                 XPoint spot;
          101                 XVaNestedList spotlist;
          102         } ime;
          103         Draw draw;
          104         Visual *vis;
          105         XSetWindowAttributes attrs;
          106         int scr;
          107         int isfixed; /* is fixed geometry? */
          108         int l, t; /* left and top offset */
          109         int gm; /* geometry mask */
          110 } XWindow;
          111 
          112 typedef struct {
          113         Atom xtarget;
          114         char *primary, *clipboard;
          115         struct timespec tclick1;
          116         struct timespec tclick2;
          117 } XSelection;
          118 
          119 /* Font structure */
          120 #define Font Font_
          121 typedef struct {
          122         int height;
          123         int width;
          124         int ascent;
          125         int descent;
          126         int badslant;
          127         int badweight;
          128         short lbearing;
          129         short rbearing;
          130         XftFont *match;
          131         FcFontSet *set;
          132         FcPattern *pattern;
          133 } Font;
          134 
          135 /* Drawing Context */
          136 typedef struct {
          137         Color *col;
          138         size_t collen;
          139         Font font, bfont, ifont, ibfont;
          140         GC gc;
          141 } DC;
          142 
          143 static inline ushort sixd_to_16bit(int);
          144 static int xmakeglyphfontspecs(XftGlyphFontSpec *, const Glyph *, int, int, int);
          145 static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int);
          146 static void xdrawglyph(Glyph, int, int);
          147 static void xclear(int, int, int, int);
          148 static int xgeommasktogravity(int);
          149 static int ximopen(Display *);
          150 static void ximinstantiate(Display *, XPointer, XPointer);
          151 static void ximdestroy(XIM, XPointer, XPointer);
          152 static int xicdestroy(XIC, XPointer, XPointer);
          153 static void xinit(int, int);
          154 static void cresize(int, int);
          155 static void xresize(int, int);
          156 static void xhints(void);
          157 static int xloadcolor(int, const char *, Color *);
          158 static int xloadfont(Font *, FcPattern *);
          159 static void xloadfonts(char *, double);
          160 static void xunloadfont(Font *);
          161 static void xunloadfonts(void);
          162 static void xsetenv(void);
          163 static void xseturgency(int);
          164 static int evcol(XEvent *);
          165 static int evrow(XEvent *);
          166 
          167 static void expose(XEvent *);
          168 static void visibility(XEvent *);
          169 static void unmap(XEvent *);
          170 static void kpress(XEvent *);
          171 static void cmessage(XEvent *);
          172 static void resize(XEvent *);
          173 static void focus(XEvent *);
          174 static uint buttonmask(uint);
          175 static int mouseaction(XEvent *, uint);
          176 static void brelease(XEvent *);
          177 static void bpress(XEvent *);
          178 static void bmotion(XEvent *);
          179 static void propnotify(XEvent *);
          180 static void selnotify(XEvent *);
          181 static void selclear_(XEvent *);
          182 static void selrequest(XEvent *);
          183 static void setsel(char *, Time);
          184 static void mousesel(XEvent *, int);
          185 static void mousereport(XEvent *);
          186 static char *kmap(KeySym, uint);
          187 static int match(uint, uint);
          188 
          189 static void run(void);
          190 static void usage(void);
          191 
          192 static void (*handler[LASTEvent])(XEvent *) = {
          193         [KeyPress] = kpress,
          194         [ClientMessage] = cmessage,
          195         [ConfigureNotify] = resize,
          196         [VisibilityNotify] = visibility,
          197         [UnmapNotify] = unmap,
          198         [Expose] = expose,
          199         [FocusIn] = focus,
          200         [FocusOut] = focus,
          201         [MotionNotify] = bmotion,
          202         [ButtonPress] = bpress,
          203         [ButtonRelease] = brelease,
          204 /*
          205  * Uncomment if you want the selection to disappear when you select something
          206  * different in another window.
          207  */
          208 /*        [SelectionClear] = selclear_, */
          209         [SelectionNotify] = selnotify,
          210 /*
          211  * PropertyNotify is only turned on when there is some INCR transfer happening
          212  * for the selection retrieval.
          213  */
          214         [PropertyNotify] = propnotify,
          215         [SelectionRequest] = selrequest,
          216 };
          217 
          218 /* Globals */
          219 static DC dc;
          220 static XWindow xw;
          221 static XSelection xsel;
          222 static TermWindow win;
          223 
          224 /* Font Ring Cache */
          225 enum {
          226         FRC_NORMAL,
          227         FRC_ITALIC,
          228         FRC_BOLD,
          229         FRC_ITALICBOLD
          230 };
          231 
          232 typedef struct {
          233         XftFont *font;
          234         int flags;
          235         Rune unicodep;
          236 } Fontcache;
          237 
          238 /* Fontcache is an array now. A new font will be appended to the array. */
          239 static Fontcache *frc = NULL;
          240 static int frclen = 0;
          241 static int frccap = 0;
          242 static char *usedfont = NULL;
          243 static double usedfontsize = 0;
          244 static double defaultfontsize = 0;
          245 
          246 static char *opt_class = NULL;
          247 static char **opt_cmd  = NULL;
          248 static char *opt_embed = NULL;
          249 static char *opt_font  = NULL;
          250 static char *opt_io    = NULL;
          251 static char *opt_line  = NULL;
          252 static char *opt_name  = NULL;
          253 static char *opt_title = NULL;
          254 
          255 static int oldbutton = 3; /* button event on startup: 3 = release */
          256 
          257 void
          258 clipcopy(const Arg *dummy)
          259 {
          260         Atom clipboard;
          261 
          262         free(xsel.clipboard);
          263         xsel.clipboard = NULL;
          264 
          265         if (xsel.primary != NULL) {
          266                 xsel.clipboard = xstrdup(xsel.primary);
          267                 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
          268                 XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
          269         }
          270 }
          271 
          272 void
          273 clippaste(const Arg *dummy)
          274 {
          275         Atom clipboard;
          276 
          277         clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
          278         XConvertSelection(xw.dpy, clipboard, xsel.xtarget, clipboard,
          279                         xw.win, CurrentTime);
          280 }
          281 
          282 void
          283 selpaste(const Arg *dummy)
          284 {
          285         XConvertSelection(xw.dpy, XA_PRIMARY, xsel.xtarget, XA_PRIMARY,
          286                         xw.win, CurrentTime);
          287 }
          288 
          289 void
          290 numlock(const Arg *dummy)
          291 {
          292         win.mode ^= MODE_NUMLOCK;
          293 }
          294 
          295 void
          296 zoom(const Arg *arg)
          297 {
          298         Arg larg;
          299 
          300         larg.f = usedfontsize + arg->f;
          301         zoomabs(&larg);
          302 }
          303 
          304 void
          305 zoomabs(const Arg *arg)
          306 {
          307         xunloadfonts();
          308         xloadfonts(usedfont, arg->f);
          309         cresize(0, 0);
          310         redraw();
          311         xhints();
          312 }
          313 
          314 void
          315 zoomreset(const Arg *arg)
          316 {
          317         Arg larg;
          318 
          319         if (defaultfontsize > 0) {
          320                 larg.f = defaultfontsize;
          321                 zoomabs(&larg);
          322         }
          323 }
          324 
          325 void
          326 ttysend(const Arg *arg)
          327 {
          328         ttywrite(arg->s, strlen(arg->s), 1);
          329 }
          330 
          331 int
          332 evcol(XEvent *e)
          333 {
          334         int x = e->xbutton.x - borderpx;
          335         LIMIT(x, 0, win.tw - 1);
          336         return x / win.cw;
          337 }
          338 
          339 int
          340 evrow(XEvent *e)
          341 {
          342         int y = e->xbutton.y - borderpx;
          343         LIMIT(y, 0, win.th - 1);
          344         return y / win.ch;
          345 }
          346 
          347 void
          348 mousesel(XEvent *e, int done)
          349 {
          350         int type, seltype = SEL_REGULAR;
          351         uint state = e->xbutton.state & ~(Button1Mask | forcemousemod);
          352 
          353         for (type = 1; type < LEN(selmasks); ++type) {
          354                 if (match(selmasks[type], state)) {
          355                         seltype = type;
          356                         break;
          357                 }
          358         }
          359         selextend(evcol(e), evrow(e), seltype, done);
          360         if (done)
          361                 setsel(getsel(), e->xbutton.time);
          362 }
          363 
          364 void
          365 mousereport(XEvent *e)
          366 {
          367         int len, x = evcol(e), y = evrow(e),
          368             button = e->xbutton.button, state = e->xbutton.state;
          369         char buf[40];
          370         static int ox, oy;
          371 
          372         /* from urxvt */
          373         if (e->xbutton.type == MotionNotify) {
          374                 if (x == ox && y == oy)
          375                         return;
          376                 if (!IS_SET(MODE_MOUSEMOTION) && !IS_SET(MODE_MOUSEMANY))
          377                         return;
          378                 /* MOUSE_MOTION: no reporting if no button is pressed */
          379                 if (IS_SET(MODE_MOUSEMOTION) && oldbutton == 3)
          380                         return;
          381 
          382                 button = oldbutton + 32;
          383                 ox = x;
          384                 oy = y;
          385         } else {
          386                 if (!IS_SET(MODE_MOUSESGR) && e->xbutton.type == ButtonRelease) {
          387                         button = 3;
          388                 } else {
          389                         button -= Button1;
          390                         if (button >= 3)
          391                                 button += 64 - 3;
          392                 }
          393                 if (e->xbutton.type == ButtonPress) {
          394                         oldbutton = button;
          395                         ox = x;
          396                         oy = y;
          397                 } else if (e->xbutton.type == ButtonRelease) {
          398                         oldbutton = 3;
          399                         /* MODE_MOUSEX10: no button release reporting */
          400                         if (IS_SET(MODE_MOUSEX10))
          401                                 return;
          402                         if (button == 64 || button == 65)
          403                                 return;
          404                 }
          405         }
          406 
          407         if (!IS_SET(MODE_MOUSEX10)) {
          408                 button += ((state & ShiftMask  ) ? 4  : 0)
          409                         + ((state & Mod4Mask   ) ? 8  : 0)
          410                         + ((state & ControlMask) ? 16 : 0);
          411         }
          412 
          413         if (IS_SET(MODE_MOUSESGR)) {
          414                 len = snprintf(buf, sizeof(buf), "\033[<%d;%d;%d%c",
          415                                 button, x+1, y+1,
          416                                 e->xbutton.type == ButtonRelease ? 'm' : 'M');
          417         } else if (x < 223 && y < 223) {
          418                 len = snprintf(buf, sizeof(buf), "\033[M%c%c%c",
          419                                 32+button, 32+x+1, 32+y+1);
          420         } else {
          421                 return;
          422         }
          423 
          424         ttywrite(buf, len, 0);
          425 }
          426 
          427 uint
          428 buttonmask(uint button)
          429 {
          430         return button == Button1 ? Button1Mask
          431              : button == Button2 ? Button2Mask
          432              : button == Button3 ? Button3Mask
          433              : button == Button4 ? Button4Mask
          434              : button == Button5 ? Button5Mask
          435              : 0;
          436 }
          437 
          438 int
          439 mouseaction(XEvent *e, uint release)
          440 {
          441         MouseShortcut *ms;
          442 
          443         /* ignore Button<N>mask for Button<N> - it's set on release */
          444         uint state = e->xbutton.state & ~buttonmask(e->xbutton.button);
          445 
          446         for (ms = mshortcuts; ms < mshortcuts + LEN(mshortcuts); ms++) {
          447                 if (ms->release == release &&
          448                     ms->button == e->xbutton.button &&
          449                     (match(ms->mod, state) ||  /* exact or forced */
          450                      match(ms->mod, state & ~forcemousemod))) {
          451                         ms->func(&(ms->arg));
          452                         return 1;
          453                 }
          454         }
          455 
          456         return 0;
          457 }
          458 
          459 void
          460 bpress(XEvent *e)
          461 {
          462         struct timespec now;
          463         int snap;
          464 
          465         if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
          466                 mousereport(e);
          467                 return;
          468         }
          469 
          470         if (mouseaction(e, 0))
          471                 return;
          472 
          473         if (e->xbutton.button == Button1) {
          474                 /*
          475                  * If the user clicks below predefined timeouts specific
          476                  * snapping behaviour is exposed.
          477                  */
          478                 clock_gettime(CLOCK_MONOTONIC, &now);
          479                 if (TIMEDIFF(now, xsel.tclick2) <= tripleclicktimeout) {
          480                         snap = SNAP_LINE;
          481                 } else if (TIMEDIFF(now, xsel.tclick1) <= doubleclicktimeout) {
          482                         snap = SNAP_WORD;
          483                 } else {
          484                         snap = 0;
          485                 }
          486                 xsel.tclick2 = xsel.tclick1;
          487                 xsel.tclick1 = now;
          488 
          489                 selstart(evcol(e), evrow(e), snap);
          490         }
          491 }
          492 
          493 void
          494 propnotify(XEvent *e)
          495 {
          496         XPropertyEvent *xpev;
          497         Atom clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
          498 
          499         xpev = &e->xproperty;
          500         if (xpev->state == PropertyNewValue &&
          501                         (xpev->atom == XA_PRIMARY ||
          502                          xpev->atom == clipboard)) {
          503                 selnotify(e);
          504         }
          505 }
          506 
          507 void
          508 selnotify(XEvent *e)
          509 {
          510         ulong nitems, ofs, rem;
          511         int format;
          512         uchar *data, *last, *repl;
          513         Atom type, incratom, property = None;
          514 
          515         incratom = XInternAtom(xw.dpy, "INCR", 0);
          516 
          517         ofs = 0;
          518         if (e->type == SelectionNotify)
          519                 property = e->xselection.property;
          520         else if (e->type == PropertyNotify)
          521                 property = e->xproperty.atom;
          522 
          523         if (property == None)
          524                 return;
          525 
          526         do {
          527                 if (XGetWindowProperty(xw.dpy, xw.win, property, ofs,
          528                                         BUFSIZ/4, False, AnyPropertyType,
          529                                         &type, &format, &nitems, &rem,
          530                                         &data)) {
          531                         fprintf(stderr, "Clipboard allocation failed\n");
          532                         return;
          533                 }
          534 
          535                 if (e->type == PropertyNotify && nitems == 0 && rem == 0) {
          536                         /*
          537                          * If there is some PropertyNotify with no data, then
          538                          * this is the signal of the selection owner that all
          539                          * data has been transferred. We won't need to receive
          540                          * PropertyNotify events anymore.
          541                          */
          542                         MODBIT(xw.attrs.event_mask, 0, PropertyChangeMask);
          543                         XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
          544                                         &xw.attrs);
          545                 }
          546 
          547                 if (type == incratom) {
          548                         /*
          549                          * Activate the PropertyNotify events so we receive
          550                          * when the selection owner does send us the next
          551                          * chunk of data.
          552                          */
          553                         MODBIT(xw.attrs.event_mask, 1, PropertyChangeMask);
          554                         XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
          555                                         &xw.attrs);
          556 
          557                         /*
          558                          * Deleting the property is the transfer start signal.
          559                          */
          560                         XDeleteProperty(xw.dpy, xw.win, (int)property);
          561                         continue;
          562                 }
          563 
          564                 /*
          565                  * As seen in getsel:
          566                  * Line endings are inconsistent in the terminal and GUI world
          567                  * copy and pasting. When receiving some selection data,
          568                  * replace all '\n' with '\r'.
          569                  * FIXME: Fix the computer world.
          570                  */
          571                 repl = data;
          572                 last = data + nitems * format / 8;
          573                 while ((repl = memchr(repl, '\n', last - repl))) {
          574                         *repl++ = '\r';
          575                 }
          576 
          577                 if (IS_SET(MODE_BRCKTPASTE) && ofs == 0)
          578                         ttywrite("\033[200~", 6, 0);
          579                 ttywrite((char *)data, nitems * format / 8, 1);
          580                 if (IS_SET(MODE_BRCKTPASTE) && rem == 0)
          581                         ttywrite("\033[201~", 6, 0);
          582                 XFree(data);
          583                 /* number of 32-bit chunks returned */
          584                 ofs += nitems * format / 32;
          585         } while (rem > 0);
          586 
          587         /*
          588          * Deleting the property again tells the selection owner to send the
          589          * next data chunk in the property.
          590          */
          591         XDeleteProperty(xw.dpy, xw.win, (int)property);
          592 }
          593 
          594 void
          595 xclipcopy(void)
          596 {
          597         clipcopy(NULL);
          598 }
          599 
          600 void
          601 selclear_(XEvent *e)
          602 {
          603         selclear();
          604 }
          605 
          606 void
          607 selrequest(XEvent *e)
          608 {
          609         XSelectionRequestEvent *xsre;
          610         XSelectionEvent xev;
          611         Atom xa_targets, string, clipboard;
          612         char *seltext;
          613 
          614         xsre = (XSelectionRequestEvent *) e;
          615         xev.type = SelectionNotify;
          616         xev.requestor = xsre->requestor;
          617         xev.selection = xsre->selection;
          618         xev.target = xsre->target;
          619         xev.time = xsre->time;
          620         if (xsre->property == None)
          621                 xsre->property = xsre->target;
          622 
          623         /* reject */
          624         xev.property = None;
          625 
          626         xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
          627         if (xsre->target == xa_targets) {
          628                 /* respond with the supported type */
          629                 string = xsel.xtarget;
          630                 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
          631                                 XA_ATOM, 32, PropModeReplace,
          632                                 (uchar *) &string, 1);
          633                 xev.property = xsre->property;
          634         } else if (xsre->target == xsel.xtarget || xsre->target == XA_STRING) {
          635                 /*
          636                  * xith XA_STRING non ascii characters may be incorrect in the
          637                  * requestor. It is not our problem, use utf8.
          638                  */
          639                 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
          640                 if (xsre->selection == XA_PRIMARY) {
          641                         seltext = xsel.primary;
          642                 } else if (xsre->selection == clipboard) {
          643                         seltext = xsel.clipboard;
          644                 } else {
          645                         fprintf(stderr,
          646                                 "Unhandled clipboard selection 0x%lx\n",
          647                                 xsre->selection);
          648                         return;
          649                 }
          650                 if (seltext != NULL) {
          651                         XChangeProperty(xsre->display, xsre->requestor,
          652                                         xsre->property, xsre->target,
          653                                         8, PropModeReplace,
          654                                         (uchar *)seltext, strlen(seltext));
          655                         xev.property = xsre->property;
          656                 }
          657         }
          658 
          659         /* all done, send a notification to the listener */
          660         if (!XSendEvent(xsre->display, xsre->requestor, 1, 0, (XEvent *) &xev))
          661                 fprintf(stderr, "Error sending SelectionNotify event\n");
          662 }
          663 
          664 void
          665 setsel(char *str, Time t)
          666 {
          667         if (!str)
          668                 return;
          669 
          670         free(xsel.primary);
          671         xsel.primary = str;
          672 
          673         XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, t);
          674         if (XGetSelectionOwner(xw.dpy, XA_PRIMARY) != xw.win)
          675                 selclear();
          676 }
          677 
          678 void
          679 xsetsel(char *str)
          680 {
          681         setsel(str, CurrentTime);
          682 }
          683 
          684 void
          685 brelease(XEvent *e)
          686 {
          687         if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
          688                 mousereport(e);
          689                 return;
          690         }
          691 
          692         if (mouseaction(e, 1))
          693                 return;
          694         if (e->xbutton.button == Button1)
          695                 mousesel(e, 1);
          696 }
          697 
          698 void
          699 bmotion(XEvent *e)
          700 {
          701         if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
          702                 mousereport(e);
          703                 return;
          704         }
          705 
          706         mousesel(e, 0);
          707 }
          708 
          709 void
          710 cresize(int width, int height)
          711 {
          712         int col, row;
          713 
          714         if (width != 0)
          715                 win.w = width;
          716         if (height != 0)
          717                 win.h = height;
          718 
          719         col = (win.w - 2 * borderpx) / win.cw;
          720         row = (win.h - 2 * borderpx) / win.ch;
          721         col = MAX(1, col);
          722         row = MAX(1, row);
          723 
          724         tresize(col, row);
          725         xresize(col, row);
          726         ttyresize(win.tw, win.th);
          727 }
          728 
          729 void
          730 xresize(int col, int row)
          731 {
          732         win.tw = col * win.cw;
          733         win.th = row * win.ch;
          734 
          735         XFreePixmap(xw.dpy, xw.buf);
          736         xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
          737                         DefaultDepth(xw.dpy, xw.scr));
          738         XftDrawChange(xw.draw, xw.buf);
          739         xclear(0, 0, win.w, win.h);
          740 
          741         /* resize to new width */
          742         xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec));
          743 }
          744 
          745 ushort
          746 sixd_to_16bit(int x)
          747 {
          748         return x == 0 ? 0 : 0x3737 + 0x2828 * x;
          749 }
          750 
          751 int
          752 xloadcolor(int i, const char *name, Color *ncolor)
          753 {
          754         XRenderColor color = { .alpha = 0xffff };
          755 
          756         if (!name) {
          757                 if (BETWEEN(i, 16, 255)) { /* 256 color */
          758                         if (i < 6*6*6+16) { /* same colors as xterm */
          759                                 color.red   = sixd_to_16bit( ((i-16)/36)%6 );
          760                                 color.green = sixd_to_16bit( ((i-16)/6) %6 );
          761                                 color.blue  = sixd_to_16bit( ((i-16)/1) %6 );
          762                         } else { /* greyscale */
          763                                 color.red = 0x0808 + 0x0a0a * (i - (6*6*6+16));
          764                                 color.green = color.blue = color.red;
          765                         }
          766                         return XftColorAllocValue(xw.dpy, xw.vis,
          767                                                   xw.cmap, &color, ncolor);
          768                 } else
          769                         name = colorname[i];
          770         }
          771 
          772         return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
          773 }
          774 
          775 void
          776 xloadcols(void)
          777 {
          778         int i;
          779         static int loaded;
          780         Color *cp;
          781 
          782         if (loaded) {
          783                 for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp)
          784                         XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
          785         } else {
          786                 dc.collen = MAX(LEN(colorname), 256);
          787                 dc.col = xmalloc(dc.collen * sizeof(Color));
          788         }
          789 
          790         for (i = 0; i < dc.collen; i++)
          791                 if (!xloadcolor(i, NULL, &dc.col[i])) {
          792                         if (colorname[i])
          793                                 die("could not allocate color '%s'\n", colorname[i]);
          794                         else
          795                                 die("could not allocate color %d\n", i);
          796                 }
          797         loaded = 1;
          798 }
          799 
          800 int
          801 xsetcolorname(int x, const char *name)
          802 {
          803         Color ncolor;
          804 
          805         if (!BETWEEN(x, 0, dc.collen))
          806                 return 1;
          807 
          808         if (!xloadcolor(x, name, &ncolor))
          809                 return 1;
          810 
          811         XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
          812         dc.col[x] = ncolor;
          813 
          814         return 0;
          815 }
          816 
          817 /*
          818  * Absolute coordinates.
          819  */
          820 void
          821 xclear(int x1, int y1, int x2, int y2)
          822 {
          823         XftDrawRect(xw.draw,
          824                         &dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
          825                         x1, y1, x2-x1, y2-y1);
          826 }
          827 
          828 void
          829 xhints(void)
          830 {
          831         XClassHint class = {opt_name ? opt_name : termname,
          832                             opt_class ? opt_class : termname};
          833         XWMHints wm = {.flags = InputHint, .input = 1};
          834         XSizeHints *sizeh;
          835 
          836         sizeh = XAllocSizeHints();
          837 
          838         sizeh->flags = PSize | PResizeInc | PBaseSize | PMinSize;
          839         sizeh->height = win.h;
          840         sizeh->width = win.w;
          841         sizeh->height_inc = win.ch;
          842         sizeh->width_inc = win.cw;
          843         sizeh->base_height = 2 * borderpx;
          844         sizeh->base_width = 2 * borderpx;
          845         sizeh->min_height = win.ch + 2 * borderpx;
          846         sizeh->min_width = win.cw + 2 * borderpx;
          847         if (xw.isfixed) {
          848                 sizeh->flags |= PMaxSize;
          849                 sizeh->min_width = sizeh->max_width = win.w;
          850                 sizeh->min_height = sizeh->max_height = win.h;
          851         }
          852         if (xw.gm & (XValue|YValue)) {
          853                 sizeh->flags |= USPosition | PWinGravity;
          854                 sizeh->x = xw.l;
          855                 sizeh->y = xw.t;
          856                 sizeh->win_gravity = xgeommasktogravity(xw.gm);
          857         }
          858 
          859         XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm,
          860                         &class);
          861         XFree(sizeh);
          862 }
          863 
          864 int
          865 xgeommasktogravity(int mask)
          866 {
          867         switch (mask & (XNegative|YNegative)) {
          868         case 0:
          869                 return NorthWestGravity;
          870         case XNegative:
          871                 return NorthEastGravity;
          872         case YNegative:
          873                 return SouthWestGravity;
          874         }
          875 
          876         return SouthEastGravity;
          877 }
          878 
          879 int
          880 xloadfont(Font *f, FcPattern *pattern)
          881 {
          882         FcPattern *configured;
          883         FcPattern *match;
          884         FcResult result;
          885         XGlyphInfo extents;
          886         int wantattr, haveattr;
          887 
          888         /*
          889          * Manually configure instead of calling XftMatchFont
          890          * so that we can use the configured pattern for
          891          * "missing glyph" lookups.
          892          */
          893         configured = FcPatternDuplicate(pattern);
          894         if (!configured)
          895                 return 1;
          896 
          897         FcConfigSubstitute(NULL, configured, FcMatchPattern);
          898         XftDefaultSubstitute(xw.dpy, xw.scr, configured);
          899 
          900         match = FcFontMatch(NULL, configured, &result);
          901         if (!match) {
          902                 FcPatternDestroy(configured);
          903                 return 1;
          904         }
          905 
          906         if (!(f->match = XftFontOpenPattern(xw.dpy, match))) {
          907                 FcPatternDestroy(configured);
          908                 FcPatternDestroy(match);
          909                 return 1;
          910         }
          911 
          912         if ((XftPatternGetInteger(pattern, "slant", 0, &wantattr) ==
          913             XftRes