1 /* See LICENSE for license details. */
6 #include <sys/select.h>
10 #include <X11/Xatom.h>
12 #include <X11/cursorfont.h>
13 #include <X11/keysym.h>
14 #include <X11/Xft/Xft.h>
15 #include <X11/XKBlib.h>
22 /* types used in config.h */
26 void (*func)(const Arg *);
40 /* three-valued logic variables: 0 indifferent, 1 on, -1 off */
41 signed char appkey; /* application keypad */
42 signed char appcursor; /* application cursor */
46 #define XK_ANY_MOD UINT_MAX
48 #define XK_SWITCH_MOD (1<<13)
50 /* function definitions used in config.h */
51 static void clipcopy(const Arg *);
52 static void clippaste(const Arg *);
53 static void numlock(const Arg *);
54 static void selpaste(const Arg *);
55 static void zoom(const Arg *);
56 static void zoomabs(const Arg *);
57 static void zoomreset(const Arg *);
59 /* config.h for applying patches and the configuration. */
63 #define XEMBED_FOCUS_IN 4
64 #define XEMBED_FOCUS_OUT 5
67 #define IS_SET(flag) ((win.mode & (flag)) != 0)
68 #define TRUERED(x) (((x) & 0xff0000) >> 8)
69 #define TRUEGREEN(x) (((x) & 0xff00))
70 #define TRUEBLUE(x) (((x) & 0xff) << 8)
72 typedef XftDraw *Draw;
73 typedef XftColor Color;
74 typedef XftGlyphFontSpec GlyphFontSpec;
76 /* Purely graphic info */
78 int tw, th; /* tty width and height */
79 int w, h; /* window width and height */
80 int ch; /* char height */
81 int cw; /* char width */
82 int mode; /* window state/mode flags */
83 int cursor; /* cursor style */
91 GlyphFontSpec *specbuf; /* font spec buffer used for rendering */
92 Atom xembed, wmdeletewin, netwmname, netwmpid;
97 XSetWindowAttributes attrs;
99 int isfixed; /* is fixed geometry? */
100 int l, t; /* left and top offset */
101 int gm; /* geometry mask */
106 char *primary, *clipboard;
107 struct timespec tclick1;
108 struct timespec tclick2;
127 /* Drawing Context */
131 Font font, bfont, ifont, ibfont;
135 static inline ushort sixd_to_16bit(int);
136 static int xmakeglyphfontspecs(XftGlyphFontSpec *, const Glyph *, int, int, int);
137 static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int);
138 static void xdrawglyph(Glyph, int, int);
139 static void xclear(int, int, int, int);
140 static int xgeommasktogravity(int);
141 static void xinit(int, int);
142 static void cresize(int, int);
143 static void xresize(int, int);
144 static void xhints(void);
145 static int xloadcolor(int, const char *, Color *);
146 static int xloadfont(Font *, FcPattern *);
147 static void xloadfonts(char *, double);
148 static void xunloadfont(Font *);
149 static void xunloadfonts(void);
150 static void xsetenv(void);
151 static void xseturgency(int);
152 static int x2col(int);
153 static int y2row(int);
155 static void expose(XEvent *);
156 static void visibility(XEvent *);
157 static void unmap(XEvent *);
158 static void kpress(XEvent *);
159 static void cmessage(XEvent *);
160 static void resize(XEvent *);
161 static void focus(XEvent *);
162 static void brelease(XEvent *);
163 static void bpress(XEvent *);
164 static void bmotion(XEvent *);
165 static void propnotify(XEvent *);
166 static void selnotify(XEvent *);
167 static void selclear_(XEvent *);
168 static void selrequest(XEvent *);
169 static void setsel(char *, Time);
170 static void mousesel(XEvent *, int);
171 static void mousereport(XEvent *);
172 static char *kmap(KeySym, uint);
173 static int match(uint, uint);
175 static void run(void);
176 static void usage(void);
178 static void (*handler[LASTEvent])(XEvent *) = {
180 [ClientMessage] = cmessage,
181 [ConfigureNotify] = resize,
182 [VisibilityNotify] = visibility,
183 [UnmapNotify] = unmap,
187 [MotionNotify] = bmotion,
188 [ButtonPress] = bpress,
189 [ButtonRelease] = brelease,
191 * Uncomment if you want the selection to disappear when you select something
192 * different in another window.
194 /* [SelectionClear] = selclear_, */
195 [SelectionNotify] = selnotify,
197 * PropertyNotify is only turned on when there is some INCR transfer happening
198 * for the selection retrieval.
200 [PropertyNotify] = propnotify,
201 [SelectionRequest] = selrequest,
207 static XSelection xsel;
208 static TermWindow win;
210 /* Font Ring Cache */
224 /* Fontcache is an array now. A new font will be appended to the array. */
225 static Fontcache frc[16];
226 static int frclen = 0;
227 static char *usedfont = NULL;
228 static double usedfontsize = 0;
229 static double defaultfontsize = 0;
231 static char *opt_class = NULL;
232 static char **opt_cmd = NULL;
233 static char *opt_embed = NULL;
234 static char *opt_font = NULL;
235 static char *opt_io = NULL;
236 static char *opt_line = NULL;
237 static char *opt_name = NULL;
238 static char *opt_title = NULL;
240 static int oldbutton = 3; /* button event on startup: 3 = release */
243 clipcopy(const Arg *dummy)
247 if (xsel.clipboard != NULL)
248 free(xsel.clipboard);
250 if (xsel.primary != NULL) {
251 xsel.clipboard = xstrdup(xsel.primary);
252 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
253 XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
258 clippaste(const Arg *dummy)
262 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
263 XConvertSelection(xw.dpy, clipboard, xsel.xtarget, clipboard,
264 xw.win, CurrentTime);
268 selpaste(const Arg *dummy)
270 XConvertSelection(xw.dpy, XA_PRIMARY, xsel.xtarget, XA_PRIMARY,
271 xw.win, CurrentTime);
275 numlock(const Arg *dummy)
277 win.mode ^= MODE_NUMLOCK;
285 larg.f = usedfontsize + arg->f;
290 zoomabs(const Arg *arg)
293 xloadfonts(usedfont, arg->f);
300 zoomreset(const Arg *arg)
304 if (defaultfontsize > 0) {
305 larg.f = defaultfontsize;
314 LIMIT(x, 0, win.tw - 1);
322 LIMIT(y, 0, win.th - 1);
327 mousesel(XEvent *e, int done)
329 int type, seltype = SEL_REGULAR;
330 uint state = e->xbutton.state & ~(Button1Mask | forceselmod);
332 for (type = 1; type < LEN(selmasks); ++type) {
333 if (match(selmasks[type], state)) {
338 selextend(x2col(e->xbutton.x), y2row(e->xbutton.y), seltype, done);
340 setsel(getsel(), e->xbutton.time);
344 mousereport(XEvent *e)
346 int x = x2col(e->xbutton.x), y = y2row(e->xbutton.y),
347 button = e->xbutton.button, state = e->xbutton.state,
353 if (e->xbutton.type == MotionNotify) {
354 if (x == ox && y == oy)
356 if (!IS_SET(MODE_MOUSEMOTION) && !IS_SET(MODE_MOUSEMANY))
358 /* MOUSE_MOTION: no reporting if no button is pressed */
359 if (IS_SET(MODE_MOUSEMOTION) && oldbutton == 3)
362 button = oldbutton + 32;
366 if (!IS_SET(MODE_MOUSESGR) && e->xbutton.type == ButtonRelease) {
373 if (e->xbutton.type == ButtonPress) {
377 } else if (e->xbutton.type == ButtonRelease) {
379 /* MODE_MOUSEX10: no button release reporting */
380 if (IS_SET(MODE_MOUSEX10))
382 if (button == 64 || button == 65)
387 if (!IS_SET(MODE_MOUSEX10)) {
388 button += ((state & ShiftMask ) ? 4 : 0)
389 + ((state & Mod4Mask ) ? 8 : 0)
390 + ((state & ControlMask) ? 16 : 0);
393 if (IS_SET(MODE_MOUSESGR)) {
394 len = snprintf(buf, sizeof(buf), "\033[<%d;%d;%d%c",
396 e->xbutton.type == ButtonRelease ? 'm' : 'M');
397 } else if (x < 223 && y < 223) {
398 len = snprintf(buf, sizeof(buf), "\033[M%c%c%c",
399 32+button, 32+x+1, 32+y+1);
404 ttywrite(buf, len, 0);
414 if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
419 for (ms = mshortcuts; ms < mshortcuts + LEN(mshortcuts); ms++) {
420 if (e->xbutton.button == ms->b
421 && match(ms->mask, e->xbutton.state)) {
422 ttywrite(ms->s, strlen(ms->s), 1);
427 if (e->xbutton.button == Button1) {
429 * If the user clicks below predefined timeouts specific
430 * snapping behaviour is exposed.
432 clock_gettime(CLOCK_MONOTONIC, &now);
433 if (TIMEDIFF(now, xsel.tclick2) <= tripleclicktimeout) {
435 } else if (TIMEDIFF(now, xsel.tclick1) <= doubleclicktimeout) {
440 xsel.tclick2 = xsel.tclick1;
443 selstart(x2col(e->xbutton.x), y2row(e->xbutton.y), snap);
448 propnotify(XEvent *e)
450 XPropertyEvent *xpev;
451 Atom clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
453 xpev = &e->xproperty;
454 if (xpev->state == PropertyNewValue &&
455 (xpev->atom == XA_PRIMARY ||
456 xpev->atom == clipboard)) {
464 ulong nitems, ofs, rem;
466 uchar *data, *last, *repl;
467 Atom type, incratom, property;
469 incratom = XInternAtom(xw.dpy, "INCR", 0);
472 if (e->type == SelectionNotify) {
473 property = e->xselection.property;
474 } else if(e->type == PropertyNotify) {
475 property = e->xproperty.atom;
479 if (property == None)
483 if (XGetWindowProperty(xw.dpy, xw.win, property, ofs,
484 BUFSIZ/4, False, AnyPropertyType,
485 &type, &format, &nitems, &rem,
487 fprintf(stderr, "Clipboard allocation failed\n");
491 if (e->type == PropertyNotify && nitems == 0 && rem == 0) {
493 * If there is some PropertyNotify with no data, then
494 * this is the signal of the selection owner that all
495 * data has been transferred. We won't need to receive
496 * PropertyNotify events anymore.
498 MODBIT(xw.attrs.event_mask, 0, PropertyChangeMask);
499 XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
503 if (type == incratom) {
505 * Activate the PropertyNotify events so we receive
506 * when the selection owner does send us the next
509 MODBIT(xw.attrs.event_mask, 1, PropertyChangeMask);
510 XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
514 * Deleting the property is the transfer start signal.
516 XDeleteProperty(xw.dpy, xw.win, (int)property);
522 * Line endings are inconsistent in the terminal and GUI world
523 * copy and pasting. When receiving some selection data,
524 * replace all '\n' with '\r'.
525 * FIXME: Fix the computer world.
528 last = data + nitems * format / 8;
529 while ((repl = memchr(repl, '\n', last - repl))) {
533 if (IS_SET(MODE_BRCKTPASTE) && ofs == 0)
534 ttywrite("\033[200~", 6, 0);
535 ttywrite((char *)data, nitems * format / 8, 1);
536 if (IS_SET(MODE_BRCKTPASTE) && rem == 0)
537 ttywrite("\033[201~", 6, 0);
539 /* number of 32-bit chunks returned */
540 ofs += nitems * format / 32;
544 * Deleting the property again tells the selection owner to send the
545 * next data chunk in the property.
547 XDeleteProperty(xw.dpy, xw.win, (int)property);
563 selrequest(XEvent *e)
565 XSelectionRequestEvent *xsre;
567 Atom xa_targets, string, clipboard;
570 xsre = (XSelectionRequestEvent *) e;
571 xev.type = SelectionNotify;
572 xev.requestor = xsre->requestor;
573 xev.selection = xsre->selection;
574 xev.target = xsre->target;
575 xev.time = xsre->time;
576 if (xsre->property == None)
577 xsre->property = xsre->target;
582 xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
583 if (xsre->target == xa_targets) {
584 /* respond with the supported type */
585 string = xsel.xtarget;
586 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
587 XA_ATOM, 32, PropModeReplace,
588 (uchar *) &string, 1);
589 xev.property = xsre->property;
590 } else if (xsre->target == xsel.xtarget || xsre->target == XA_STRING) {
592 * xith XA_STRING non ascii characters may be incorrect in the
593 * requestor. It is not our problem, use utf8.
595 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
596 if (xsre->selection == XA_PRIMARY) {
597 seltext = xsel.primary;
598 } else if (xsre->selection == clipboard) {
599 seltext = xsel.clipboard;
602 "Unhandled clipboard selection 0x%lx\n",
606 if (seltext != NULL) {
607 XChangeProperty(xsre->display, xsre->requestor,
608 xsre->property, xsre->target,
610 (uchar *)seltext, strlen(seltext));
611 xev.property = xsre->property;
615 /* all done, send a notification to the listener */
616 if (!XSendEvent(xsre->display, xsre->requestor, 1, 0, (XEvent *) &xev))
617 fprintf(stderr, "Error sending SelectionNotify event\n");
621 setsel(char *str, Time t)
626 XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, t);
627 if (XGetSelectionOwner(xw.dpy, XA_PRIMARY) != xw.win)
634 setsel(str, CurrentTime);
640 if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
645 if (e->xbutton.button == Button2)
647 else if (e->xbutton.button == Button1)
654 if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
663 cresize(int width, int height)
672 col = (win.w - 2 * borderpx) / win.cw;
673 row = (win.h - 2 * borderpx) / win.ch;
677 ttyresize(win.tw, win.th);
681 xresize(int col, int row)
683 win.tw = MAX(1, col * win.cw);
684 win.th = MAX(1, row * win.ch);
686 XFreePixmap(xw.dpy, xw.buf);
687 xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
688 DefaultDepth(xw.dpy, xw.scr));
689 XftDrawChange(xw.draw, xw.buf);
690 xclear(0, 0, win.w, win.h);
692 /* resize to new width */
693 xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec));
699 return x == 0 ? 0 : 0x3737 + 0x2828 * x;
703 xloadcolor(int i, const char *name, Color *ncolor)
705 XRenderColor color = { .alpha = 0xffff };
708 if (BETWEEN(i, 16, 255)) { /* 256 color */
709 if (i < 6*6*6+16) { /* same colors as xterm */
710 color.red = sixd_to_16bit( ((i-16)/36)%6 );
711 color.green = sixd_to_16bit( ((i-16)/6) %6 );
712 color.blue = sixd_to_16bit( ((i-16)/1) %6 );
713 } else { /* greyscale */
714 color.red = 0x0808 + 0x0a0a * (i - (6*6*6+16));
715 color.green = color.blue = color.red;
717 return XftColorAllocValue(xw.dpy, xw.vis,
718 xw.cmap, &color, ncolor);
723 return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
733 dc.collen = MAX(LEN(colorname), 256);
734 dc.col = xmalloc(dc.collen * sizeof(Color));
737 for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp)
738 XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
741 for (i = 0; i < dc.collen; i++)
742 if (!xloadcolor(i, NULL, &dc.col[i])) {
744 die("Could not allocate color '%s'\n", colorname[i]);
746 die("Could not allocate color %d\n", i);
752 xsetcolorname(int x, const char *name)
756 if (!BETWEEN(x, 0, dc.collen))
760 if (!xloadcolor(x, name, &ncolor))
763 XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
770 * Absolute coordinates.
773 xclear(int x1, int y1, int x2, int y2)
776 &dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
777 x1, y1, x2-x1, y2-y1);
783 XClassHint class = {opt_name ? opt_name : termname,
784 opt_class ? opt_class : termname};
785 XWMHints wm = {.flags = InputHint, .input = 1};
786 XSizeHints *sizeh = NULL;
788 sizeh = XAllocSizeHints();
790 sizeh->flags = PSize | PResizeInc | PBaseSize;
791 sizeh->height = win.h;
792 sizeh->width = win.w;
793 sizeh->height_inc = win.ch;
794 sizeh->width_inc = win.cw;
795 sizeh->base_height = 2 * borderpx;
796 sizeh->base_width = 2 * borderpx;
798 sizeh->flags |= PMaxSize | PMinSize;
799 sizeh->min_width = sizeh->max_width = win.w;
800 sizeh->min_height = sizeh->max_height = win.h;
802 if (xw.gm & (XValue|YValue)) {
803 sizeh->flags |= USPosition | PWinGravity;
806 sizeh->win_gravity = xgeommasktogravity(xw.gm);
809 XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm,
815 xgeommasktogravity(int mask)
817 switch (mask & (XNegative|YNegative)) {
819 return NorthWestGravity;
821 return NorthEastGravity;
823 return SouthWestGravity;
826 return SouthEastGravity;
830 xloadfont(Font *f, FcPattern *pattern)
832 FcPattern *configured;
836 int wantattr, haveattr;
839 * Manually configure instead of calling XftMatchFont
840 * so that we can use the configured pattern for
841 * "missing glyph" lookups.
843 configured = FcPatternDuplicate(pattern);
847 FcConfigSubstitute(NULL, configured, FcMatchPattern);
848 XftDefaultSubstitute(xw.dpy, xw.scr, configured);
850 match = FcFontMatch(NULL, configured, &result);
852 FcPatternDestroy(configured);
856 if (!(f->match = XftFontOpenPattern(xw.dpy, match))) {
857 FcPatternDestroy(configured);
858 FcPatternDestroy(match);
862 if ((XftPatternGetInteger(pattern, "slant", 0, &wantattr) ==
865 * Check if xft was unable to find a font with the appropriate
866 * slant but gave us one anyway. Try to mitigate.
868 if ((XftPatternGetInteger(f->match->pattern, "slant", 0,
869 &haveattr) != XftResultMatch) || haveattr < wantattr) {
871 fputs("st: font slant does not match\n", stderr);
875 if ((XftPatternGetInteger(pattern, "weight", 0, &wantattr) ==
877 if ((XftPatternGetInteger(f->match->pattern, "weight", 0,
878 &haveattr) != XftResultMatch) || haveattr != wantattr) {
880 fputs("st: font weight does not match\n", stderr);
884 XftTextExtentsUtf8(xw.dpy, f->match,
885 (const FcChar8 *) ascii_printable,
886 strlen(ascii_printable), &extents);
889 f->pattern = configured;
891 f->ascent = f->match->ascent;
892 f->descent = f->match->descent;
894 f->rbearing = f->match->max_advance_width;
896 f->height = f->ascent + f->descent;
897 f->width = DIVCEIL(extents.xOff, strlen(ascii_printable));
903 xloadfonts(char *fontstr, double fontsize)
909 if (fontstr[0] == '-') {
910 pattern = XftXlfdParse(fontstr, False, False);
912 pattern = FcNameParse((FcChar8 *)fontstr);
916 die("st: can't open font %s\n", fontstr);
919 FcPatternDel(pattern, FC_PIXEL_SIZE);
920 FcPatternDel(pattern, FC_SIZE);
921 FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
922 usedfontsize = fontsize;
924 if (FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) ==
926 usedfontsize = fontval;
927 } else if (FcPatternGetDouble(pattern, FC_SIZE, 0, &fontval) ==
932 * Default font size is 12, if none given. This is to
933 * have a known usedfontsize value.
935 FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
938 defaultfontsize = usedfontsize;
941 if (xloadfont(&dc.font, pattern))
942 die("st: can't open font %s\n", fontstr);
944 if (usedfontsize < 0) {
945 FcPatternGetDouble(dc.font.match->pattern,
946 FC_PIXEL_SIZE, 0, &fontval);
947 usedfontsize = fontval;
949 defaultfontsize = fontval;
952 /* Setting character width and height. */
953 win.cw = ceilf(dc.font.width * cwscale);
954 win.ch = ceilf(dc.font.height * chscale);
956 FcPatternDel(pattern, FC_SLANT);
957 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
958 if (xloadfont(&dc.ifont, pattern))
959 die("st: can't open font %s\n", fontstr);
961 FcPatternDel(pattern, FC_WEIGHT);
962 FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
963 if (xloadfont(&dc.ibfont, pattern))
964 die("st: can't open font %s\n", fontstr);
966 FcPatternDel(pattern, FC_SLANT);
967 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
968 if (xloadfont(&dc.bfont, pattern))
969 die("st: can't open font %s\n", fontstr);
971 FcPatternDestroy(pattern);
977 XftFontClose(xw.dpy, f->match);
978 FcPatternDestroy(f->pattern);
980 FcFontSetDestroy(f->set);
986 /* Free the loaded fonts in the font cache. */
988 XftFontClose(xw.dpy, frc[--frclen].font);
990 xunloadfont(&dc.font);
991 xunloadfont(&dc.bfont);
992 xunloadfont(&dc.ifont);
993 xunloadfont(&dc.ibfont);
997 xinit(int cols, int rows)
1002 pid_t thispid = getpid();
1003 XColor xmousefg, xmousebg;
1005 if (!(xw.dpy = XOpenDisplay(NULL)))
1006 die("Can't open display\n");
1007 xw.scr = XDefaultScreen(xw.dpy);
1008 xw.vis = XDefaultVisual(xw.dpy, xw.scr);
1012 die("Could not init fontconfig.\n");
1014 usedfont = (opt_font == NULL)? font : opt_font;
1015 xloadfonts(usedfont, 0);
1018 xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
1021 /* adjust fixed window geometry */
1022 win.w = 2 * borderpx + cols * win.cw;
1023 win.h = 2 * borderpx + rows * win.ch;
1024 if (xw.gm & XNegative)
1025 xw.l += DisplayWidth(xw.dpy, xw.scr) - win.w - 2;
1026 if (xw.gm & YNegative)
1027 xw.t += DisplayHeight(xw.dpy, xw.scr) - win.h - 2;
1030 xw.attrs.background_pixel = dc.col[defaultbg].pixel;
1031 xw.attrs.border_pixel = dc.col[defaultbg].pixel;
1032 xw.attrs.bit_gravity = NorthWestGravity;
1033 xw.attrs.event_mask = FocusChangeMask | KeyPressMask
1034 | ExposureMask | VisibilityChangeMask | StructureNotifyMask
1035 | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
1036 xw.attrs.colormap = xw.cmap;
1038 if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
1039 parent = XRootWindow(xw.dpy, xw.scr);
1040 xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
1041 win.w, win.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
1042 xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
1043 | CWEventMask | CWColormap, &xw.attrs);
1045 memset(&gcvalues, 0, sizeof(gcvalues));
1046 gcvalues.graphics_exposures = False;
1047 dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
1049 xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
1050 DefaultDepth(xw.dpy, xw.scr));
1051 XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
1052 XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
1054 /* font spec buffer */
1055 xw.specbuf = xmalloc(cols * sizeof(GlyphFontSpec));
1057 /* Xft rendering context */
1058 xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
1061 if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
1062 XSetLocaleModifiers("@im=local");
1063 if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
1064 XSetLocaleModifiers("@im=");
1065 if ((xw.xim = XOpenIM(xw.dpy,
1066 NULL, NULL, NULL)) == NULL) {
1067 die("XOpenIM failed. Could not open input"
1072 xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
1073 | XIMStatusNothing, XNClientWindow, xw.win,
1074 XNFocusWindow, xw.win, NULL);
1076 die("XCreateIC failed. Could not obtain input method.\n");
1078 /* white cursor, black outline */
1079 cursor = XCreateFontCursor(xw.dpy, mouseshape);
1080 XDefineCursor(xw.dpy, xw.win, cursor);
1082 if (XParseColor(xw.dpy, xw.cmap, colorname[mousefg], &xmousefg) == 0) {
1083 xmousefg.red = 0xffff;
1084 xmousefg.green = 0xffff;
1085 xmousefg.blue = 0xffff;
1088 if (XParseColor(xw.dpy, xw.cmap, colorname[mousebg], &xmousebg) == 0) {
1089 xmousebg.red = 0x0000;
1090 xmousebg.green = 0x0000;
1091 xmousebg.blue = 0x0000;
1094 XRecolorCursor(xw.dpy, cursor, &xmousefg, &xmousebg);
1096 xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
1097 xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
1098 xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False);
1099 XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
1101 xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False);
1102 XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL, 32,
1103 PropModeReplace, (uchar *)&thispid, 1);
1105 win.mode = MODE_NUMLOCK;
1107 XMapWindow(xw.dpy, xw.win);
1109 XSync(xw.dpy, False);
1111 clock_gettime(CLOCK_MONOTONIC, &xsel.tclick1);
1112 clock_gettime(CLOCK_MONOTONIC, &xsel.tclick2);
1113 xsel.primary = NULL;
1114 xsel.clipboard = NULL;
1115 xsel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
1116 if (xsel.xtarget == None)
1117 xsel.xtarget = XA_STRING;
1121 xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y)
1123 float winx = borderpx + x * win.cw, winy = borderpx + y * win.ch, xp, yp;
1124 ushort mode, prevmode = USHRT_MAX;
1125 Font *font = &dc.font;
1126 int frcflags = FRC_NORMAL;
1127 float runewidth = win.cw;
1131 FcPattern *fcpattern, *fontpattern;
1132 FcFontSet *fcsets[] = { NULL };
1133 FcCharSet *fccharset;
1134 int i, f, numspecs = 0;
1136 for (i = 0, xp = winx, yp = winy + font->ascent; i < len; ++i) {
1137 /* Fetch rune and mode for current glyph. */
1139 mode = glyphs[i].mode;
1141 /* Skip dummy wide-character spacing. */
1142 if (mode == ATTR_WDUMMY)
1145 /* Determine font for glyph if different from previous glyph. */
1146 if (prevmode != mode) {
1149 frcflags = FRC_NORMAL;
1150 runewidth = win.cw * ((mode & ATTR_WIDE) ? 2.0f : 1.0f);
1151 if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) {
1153 frcflags = FRC_ITALICBOLD;
1154 } else if (mode & ATTR_ITALIC) {
1156 frcflags = FRC_ITALIC;
1157 } else if (mode & ATTR_BOLD) {
1159 frcflags = FRC_BOLD;
1161 yp = winy + font->ascent;
1164 /* Lookup character index with default font. */
1165 glyphidx = XftCharIndex(xw.dpy, font->match, rune);
1167 specs[numspecs].font = font->match;
1168 specs[numspecs].glyph = glyphidx;
1169 specs[numspecs].x = (short)xp;
1170 specs[numspecs].y = (short)yp;
1176 /* Fallback on font cache, search the font cache for match. */
1177 for (f = 0; f < frclen; f++) {
1178 glyphidx = XftCharIndex(xw.dpy, frc[f].font, rune);
1179 /* Everything correct. */
1180 if (glyphidx && frc[f].flags == frcflags)
1182 /* We got a default font for a not found glyph. */
1183 if (!glyphidx && frc[f].flags == frcflags
1184 && frc[f].unicodep == rune) {
1189 /* Nothing was found. Use fontconfig to find matching font. */
1192 font->set = FcFontSort(0, font->pattern,
1194 fcsets[0] = font->set;
1197 * Nothing was found in the cache. Now use
1198 * some dozen of Fontconfig calls to get the
1199 * font for one single character.
1201 * Xft and fontconfig are design failures.
1203 fcpattern = FcPatternDuplicate(font->pattern);
1204 fccharset = FcCharSetCreate();
1206 FcCharSetAddChar(fccharset, rune);
1207 FcPatternAddCharSet(fcpattern, FC_CHARSET,
1209 FcPatternAddBool(fcpattern, FC_SCALABLE, 1);
1211 FcConfigSubstitute(0, fcpattern,
1213 FcDefaultSubstitute(fcpattern);
1215 fontpattern = FcFontSetMatch(0, fcsets, 1,
1219 * Overwrite or create the new cache entry.
1221 if (frclen >= LEN(frc)) {
1222 frclen = LEN(frc) - 1;
1223 XftFontClose(xw.dpy, frc[frclen].font);
1224 frc[frclen].unicodep = 0;
1227 frc[frclen].font = XftFontOpenPattern(xw.dpy,
1229 if (!frc[frclen].font)
1230 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1232 frc[frclen].flags = frcflags;
1233 frc[frclen].unicodep = rune;
1235 glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune);
1240 FcPatternDestroy(fcpattern);
1241 FcCharSetDestroy(fccharset);
1244 specs[numspecs].font = frc[f].font;
1245 specs[numspecs].glyph = glyphidx;
1246 specs[numspecs].x = (short)xp;
1247 specs[numspecs].y = (short)yp;
1256 xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, int y)
1258 int charlen = len * ((base.mode & ATTR_WIDE) ? 2 : 1);
1259 int winx = borderpx + x * win.cw, winy = borderpx + y * win.ch,
1260 width = charlen * win.cw;
1261 Color *fg, *bg, *temp, revfg, revbg, truefg, truebg;
1262 XRenderColor colfg, colbg;
1265 /* Fallback on color display for attributes not supported by the font */
1266 if (base.mode & ATTR_ITALIC && base.mode & ATTR_BOLD) {
1267 if (dc.ibfont.badslant || dc.ibfont.badweight)
1268 base.fg = defaultattr;
1269 } else if ((base.mode & ATTR_ITALIC && dc.ifont.badslant) ||
1270 (base.mode & ATTR_BOLD && dc.bfont.badweight)) {
1271 base.fg = defaultattr;
1274 if (IS_TRUECOL(base.fg)) {
1275 colfg.alpha = 0xffff;
1276 colfg.red = TRUERED(base.fg);
1277 colfg.green = TRUEGREEN(base.fg);
1278 colfg.blue = TRUEBLUE(base.fg);
1279 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &truefg);
1282 fg = &dc.col[base.fg];
1285 if (IS_TRUECOL(base.bg)) {
1286 colbg.alpha = 0xffff;
1287 colbg.green = TRUEGREEN(base.bg);
1288 colbg.red = TRUERED(base.bg);
1289 colbg.blue = TRUEBLUE(base.bg);
1290 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &truebg);
1293 bg = &dc.col[base.bg];
1296 /* Change basic system colors [0-7] to bright system colors [8-15] */
1297 if ((base.mode & ATTR_BOLD_FAINT) == ATTR_BOLD && BETWEEN(base.fg, 0, 7))
1298 fg = &dc.col[base.fg + 8];
1300 if (IS_SET(MODE_REVERSE)) {
1301 if (fg == &dc.col[defaultfg]) {
1302 fg = &dc.col[defaultbg];
1304 colfg.red = ~fg->color.red;
1305 colfg.green = ~fg->color.green;
1306 colfg.blue = ~fg->color.blue;
1307 colfg.alpha = fg->color.alpha;
1308 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg,
1313 if (bg == &dc.col[defaultbg]) {
1314 bg = &dc.col[defaultfg];
1316 colbg.red = ~bg->color.red;
1317 colbg.green = ~bg->color.green;
1318 colbg.blue = ~bg->color.blue;
1319 colbg.alpha = bg->color.alpha;
1320 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg,
1326 if ((base.mode & ATTR_BOLD_FAINT) == ATTR_FAINT) {
1327 colfg.red = fg->color.red / 2;
1328 colfg.green = fg->color.green / 2;
1329 colfg.blue = fg->color.blue / 2;
1330 colfg.alpha = fg->color.alpha;
1331 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
1335 if (base.mode & ATTR_REVERSE) {
1341 if (base.mode & ATTR_BLINK && win.mode & MODE_BLINK)
1344 if (base.mode & ATTR_INVISIBLE)
1347 /* Intelligent cleaning up of the borders. */
1349 xclear(0, (y == 0)? 0 : winy, borderpx,
1351 ((winy + win.ch >= borderpx + win.th)? win.h : 0));
1353 if (winx + width >= borderpx + win.tw) {
1354 xclear(winx + width, (y == 0)? 0 : winy, win.w,
1355 ((winy + win.ch >= borderpx + win.th)? win.h : (winy + win.ch)));
1358 xclear(winx, 0, winx + width, borderpx);
1359 if (winy + win.ch >= borderpx + win.th)
1360 xclear(winx, winy + win.ch, winx + width, win.h);
1362 /* Clean up the region we want to draw to. */
1363 XftDrawRect(xw.draw, bg, winx, winy, width, win.ch);
1365 /* Set the clip region because Xft is sometimes dirty. */
1370 XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1);
1372 /* Render the glyphs. */
1373 XftDrawGlyphFontSpec(xw.draw, fg, specs, len);
1375 /* Render underline and strikethrough. */
1376 if (base.mode & ATTR_UNDERLINE) {
1377 XftDrawRect(xw.draw, fg, winx, winy + dc.font.ascent + 1,
1381 if (base.mode & ATTR_STRUCK) {
1382 XftDrawRect(xw.draw, fg, winx, winy + 2 * dc.font.ascent / 3,
1386 /* Reset clip to none. */
1387 XftDrawSetClip(xw.draw, 0);
1391 xdrawglyph(Glyph g, int x, int y)
1394 XftGlyphFontSpec spec;
1396 numspecs = xmakeglyphfontspecs(&spec, &g, 1, x, y);
1397 xdrawglyphfontspecs(&spec, g, numspecs, x, y);
1401 xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og)
1405 /* remove the old cursor */
1406 if (selected(ox, oy))
1407 og.mode ^= ATTR_REVERSE;
1408 xdrawglyph(og, ox, oy);
1411 * Select the right color for the right mode.
1413 g.mode &= ATTR_BOLD|ATTR_ITALIC|ATTR_UNDERLINE|ATTR_STRUCK|ATTR_WIDE;
1417 if (IS_SET(MODE_REVERSE)) {
1418 g.mode |= ATTR_REVERSE;
1420 if (selected(cx, cy)) {
1421 drawcol = dc.col[defaultcs];
1424 drawcol = dc.col[defaultrcs];
1428 if (selected(cx, cy)) {
1429 drawcol = dc.col[defaultrcs];
1433 drawcol = dc.col[defaultcs];
1437 if (IS_SET(MODE_HIDE))
1440 /* draw the new one */
1441 if (IS_SET(MODE_FOCUSED)) {
1442 switch (win.cursor) {
1443 case 7: /* st extension: snowman (U+2603) */
1445 case 0: /* Blinking Block */
1446 case 1: /* Blinking Block (Default) */
1447 case 2: /* Steady Block */
1448 xdrawglyph(g, cx, cy);
1450 case 3: /* Blinking Underline */
1451 case 4: /* Steady Underline */
1452 XftDrawRect(xw.draw, &drawcol,
1453 borderpx + cx * win.cw,
1454 borderpx + (cy + 1) * win.ch - \
1456 win.cw, cursorthickness);
1458 case 5: /* Blinking bar */
1459 case 6: /* Steady bar */
1460 XftDrawRect(xw.draw, &drawcol,
1461 borderpx + cx * win.cw,
1462 borderpx + cy * win.ch,
1463 cursorthickness, win.ch);
1467 XftDrawRect(xw.draw, &drawcol,
1468 borderpx + cx * win.cw,
1469 borderpx + cy * win.ch,
1471 XftDrawRect(xw.draw, &drawcol,
1472 borderpx + cx * win.cw,
1473 borderpx + cy * win.ch,
1475 XftDrawRect(xw.draw, &drawcol,
1476 borderpx + (cx + 1) * win.cw - 1,
1477 borderpx + cy * win.ch,
1479 XftDrawRect(xw.draw, &drawcol,
1480 borderpx + cx * win.cw,
1481 borderpx + (cy + 1) * win.ch - 1,
1489 char buf[sizeof(long) * 8 + 1];
1491 snprintf(buf, sizeof(buf), "%lu", xw.win);
1492 setenv("WINDOWID", buf, 1);
1501 Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
1503 XSetWMName(xw.dpy, xw.win, &prop);
1504 XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmname);
1511 return IS_SET(MODE_VISIBLE);
1515 xdrawline(Line line, int x1, int y1, int x2)
1517 int i, x, ox, numspecs;
1519 XftGlyphFontSpec *specs = xw.specbuf;
1521 numspecs = xmakeglyphfontspecs(specs, &line[x1], x2 - x1, x1, y1);
1523 for (x = x1; x < x2 && i < numspecs; x++) {
1525 if (new.mode == ATTR_WDUMMY)
1527 if (selected(x, y1))
1528 new.mode ^= ATTR_REVERSE;
1529 if (i > 0 && ATTRCMP(base, new)) {
1530 xdrawglyphfontspecs(specs, base, i, ox, y1);
1542 xdrawglyphfontspecs(specs, base, i, ox, y1);
1548 XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, win.w,
1550 XSetForeground(xw.dpy, dc.gc,
1551 dc.col[IS_SET(MODE_REVERSE)?
1552 defaultfg : defaultbg].pixel);
1562 visibility(XEvent *ev)
1564 XVisibilityEvent *e = &ev->xvisibility;
1566 MODBIT(win.mode, e->state != VisibilityFullyObscured, MODE_VISIBLE);
1572 win.mode &= ~MODE_VISIBLE;
1576 xsetpointermotion(int set)
1578 MODBIT(xw.attrs.event_mask, set, PointerMotionMask);
1579 XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs);
1583 xsetmode(int set, unsigned int flags)
1585 int mode = win.mode;
1586 MODBIT(win.mode, set, flags);
1587 if ((win.mode & MODE_REVERSE) != (mode & MODE_REVERSE))
1592 xsetcursor(int cursor)
1595 if (!BETWEEN(cursor, 0, 6))
1597 win.cursor = cursor;
1602 xseturgency(int add)
1604 XWMHints *h = XGetWMHints(xw.dpy, xw.win);
1606 MODBIT(h->flags, add, XUrgencyHint);
1607 XSetWMHints(xw.dpy, xw.win, h);
1614 if (!(IS_SET(MODE_FOCUSED)))
1617 XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL);
1623 XFocusChangeEvent *e = &ev->xfocus;
1625 if (e->mode == NotifyGrab)
1628 if (ev->type == FocusIn) {
1629 XSetICFocus(xw.xic);
1630 win.mode |= MODE_FOCUSED;
1632 if (IS_SET(MODE_FOCUS))
1633 ttywrite("\033[I", 3, 0);
1635 XUnsetICFocus(xw.xic);
1636 win.mode &= ~MODE_FOCUSED;
1637 if (IS_SET(MODE_FOCUS))
1638 ttywrite("\033[O", 3, 0);
1643 match(uint mask, uint state)
1645 return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
1649 kmap(KeySym k, uint state)
1654 /* Check for mapped keys out of X11 function keys. */
1655 for (i = 0; i < LEN(mappedkeys); i++) {
1656 if (mappedkeys[i] == k)
1659 if (i == LEN(mappedkeys)) {
1660 if ((k & 0xFFFF) < 0xFD00)
1664 for (kp = key; kp < key + LEN(key); kp++) {
1668 if (!match(kp->mask, state))
1671 if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0)
1673 if (IS_SET(MODE_NUMLOCK) && kp->appkey == 2)
1676 if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0)
1688 XKeyEvent *e = &ev->xkey;
1690 char buf[32], *customkey;
1696 if (IS_SET(MODE_KBDLOCK))
1699 len = XmbLookupString(xw.xic, e, buf, sizeof buf, &ksym, &status);
1701 for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
1702 if (ksym == bp->keysym && match(bp->mod, e->state)) {
1703 bp->func(&(bp->arg));
1708 /* 2. custom keys from config.h */
1709 if ((customkey = kmap(ksym, e->state))) {
1710 ttywrite(customkey, strlen(customkey), 1);
1714 /* 3. composed string from input method */
1717 if (len == 1 && e->state & Mod1Mask) {
1718 if (IS_SET(MODE_8BIT)) {
1721 len = utf8encode(c, buf);
1729 ttywrite(buf, len, 1);
1738 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1740 if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
1741 if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
1742 win.mode |= MODE_FOCUSED;
1744 } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
1745 win.mode &= ~MODE_FOCUSED;
1747 } else if (e->xclient.data.l[0] == xw.wmdeletewin) {
1756 if (e->xconfigure.width == win.w && e->xconfigure.height == win.h)
1759 cresize(e->xconfigure.width, e->xconfigure.height);
1766 int w = win.w, h = win.h;
1768 int xfd = XConnectionNumber(xw.dpy), xev, blinkset = 0, dodraw = 0;
1770 struct timespec drawtimeout, *tv = NULL, now, last, lastblink;
1773 /* Waiting for window mapping */
1775 XNextEvent(xw.dpy, &ev);
1777 * This XFilterEvent call is required because of XOpenIM. It
1778 * does filter out the key event and some client message for
1779 * the input method too.
1781 if (XFilterEvent(&ev, None))
1783 if (ev.type == ConfigureNotify) {
1784 w = ev.xconfigure.width;
1785 h = ev.xconfigure.height;
1787 } while (ev.type != MapNotify);
1789 ttyfd = ttynew(opt_line, shell, opt_io, opt_cmd);
1792 clock_gettime(CLOCK_MONOTONIC, &last);
1795 for (xev = actionfps;;) {
1797 FD_SET(ttyfd, &rfd);
1800 if (pselect(MAX(xfd, ttyfd)+1, &rfd, NULL, NULL, tv, NULL) < 0) {
1803 die("select failed: %s\n", strerror(errno));
1805 if (FD_ISSET(ttyfd, &rfd)) {
1808 blinkset = tattrset(ATTR_BLINK);
1810 MODBIT(win.mode, 0, MODE_BLINK);
1814 if (FD_ISSET(xfd, &rfd))
1817 clock_gettime(CLOCK_MONOTONIC, &now);
1818 drawtimeout.tv_sec = 0;
1819 drawtimeout.tv_nsec = (1000 * 1E6)/ xfps;
1823 if (blinktimeout && TIMEDIFF(now, lastblink) > blinktimeout) {
1824 tsetdirtattr(ATTR_BLINK);
1825 win.mode ^= MODE_BLINK;
1829 deltatime = TIMEDIFF(now, last);
1830 if (deltatime > 1000 / (xev ? xfps : actionfps)) {
1836 while (XPending(xw.dpy)) {
1837 XNextEvent(xw.dpy, &ev);
1838 if (XFilterEvent(&ev, None))
1840 if (handler[ev.type])
1841 (handler[ev.type])(&ev);
1847 if (xev && !FD_ISSET(xfd, &rfd))
1849 if (!FD_ISSET(ttyfd, &rfd) && !FD_ISSET(xfd, &rfd)) {
1851 if (TIMEDIFF(now, lastblink) \
1853 drawtimeout.tv_nsec = 1000;
1855 drawtimeout.tv_nsec = (1E6 * \
1860 drawtimeout.tv_sec = \
1861 drawtimeout.tv_nsec / 1E9;
1862 drawtimeout.tv_nsec %= (long)1E9;
1874 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
1875 " [-n name] [-o file]\n"
1876 " [-T title] [-t title] [-w windowid]"
1877 " [[-e] command [args ...]]\n"
1878 " %s [-aiv] [-c class] [-f font] [-g geometry]"
1879 " [-n name] [-o file]\n"
1880 " [-T title] [-t title] [-w windowid] -l line"
1881 " [stty_args ...]\n", argv0, argv0);
1885 main(int argc, char *argv[])
1889 win.cursor = cursorshape;
1896 opt_class = EARGF(usage());
1903 opt_font = EARGF(usage());
1906 xw.gm = XParseGeometry(EARGF(usage()),
1907 &xw.l, &xw.t, &cols, &rows);
1913 opt_io = EARGF(usage());
1916 opt_line = EARGF(usage());
1919 opt_name = EARGF(usage());
1923 opt_title = EARGF(usage());
1926 opt_embed = EARGF(usage());
1929 die("%s " VERSION " (c) 2010-2016 st engineers\n", argv0);
1937 /* eat all remaining arguments */
1939 if (!opt_title && !opt_line)
1940 opt_title = basename(xstrdup(argv[0]));
1942 setlocale(LC_CTYPE, "");
1943 XSetLocaleModifiers("");
1944 cols = MAX(cols, 1);
1945 rows = MAX(rows, 1);