1 /* See LICENSE for license details. */
6 #include <sys/select.h>
10 #include <X11/Xatom.h>
12 #include <X11/Xutil.h>
13 #include <X11/cursorfont.h>
14 #include <X11/keysym.h>
15 #include <X11/Xft/Xft.h>
16 #include <X11/XKBlib.h>
23 /* types used in config.h */
27 void (*func)(const Arg *);
41 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
42 signed char appkey; /* application keypad */
43 signed char appcursor; /* application cursor */
44 signed char crlf; /* crlf mode */
48 #define XK_ANY_MOD UINT_MAX
50 #define XK_SWITCH_MOD (1<<13)
52 /* function definitions used in config.h */
53 static void clipcopy(const Arg *);
54 static void clippaste(const Arg *);
55 static void selpaste(const Arg *);
56 static void zoom(const Arg *);
57 static void zoomabs(const Arg *);
58 static void zoomreset(const Arg *);
60 /* config.h for applying patches and the configuration. */
64 #define XEMBED_FOCUS_IN 4
65 #define XEMBED_FOCUS_OUT 5
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 */
82 GlyphFontSpec *specbuf; /* font spec buffer used for rendering */
83 Atom xembed, wmdeletewin, netwmname, netwmpid;
88 XSetWindowAttributes attrs;
90 int isfixed; /* is fixed geometry? */
91 int l, t; /* left and top offset */
92 int gm; /* geometry mask */
97 char *primary, *clipboard;
98 struct timespec tclick1;
99 struct timespec tclick2;
118 /* Drawing Context */
122 Font font, bfont, ifont, ibfont;
126 static inline ushort sixd_to_16bit(int);
127 static int xmakeglyphfontspecs(XftGlyphFontSpec *, const Glyph *, int, int, int);
128 static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int);
129 static void xdrawglyph(Glyph, int, int);
130 static void xclear(int, int, int, int);
131 static void xdrawcursor(void);
132 static int xgeommasktogravity(int);
133 static void xinit(void);
134 static void cresize(int, int);
135 static void xresize(int, int);
136 static int xloadfont(Font *, FcPattern *);
137 static void xloadfonts(char *, double);
138 static void xunloadfont(Font *);
139 static void xunloadfonts(void);
140 static void xsetenv(void);
141 static void xseturgency(int);
142 static int x2col(int);
143 static int y2row(int);
145 static void expose(XEvent *);
146 static void visibility(XEvent *);
147 static void unmap(XEvent *);
148 static void kpress(XEvent *);
149 static void cmessage(XEvent *);
150 static void resize(XEvent *);
151 static void focus(XEvent *);
152 static void brelease(XEvent *);
153 static void bpress(XEvent *);
154 static void bmotion(XEvent *);
155 static void propnotify(XEvent *);
156 static void selnotify(XEvent *);
157 static void selclear_(XEvent *);
158 static void selrequest(XEvent *);
159 static void setsel(char *, Time);
160 static void mousesel(XEvent *);
161 static void mousereport(XEvent *);
162 static char *kmap(KeySym, uint);
163 static int match(uint, uint);
165 static void run(void);
166 static void usage(void);
168 static void (*handler[LASTEvent])(XEvent *) = {
170 [ClientMessage] = cmessage,
171 [ConfigureNotify] = resize,
172 [VisibilityNotify] = visibility,
173 [UnmapNotify] = unmap,
177 [MotionNotify] = bmotion,
178 [ButtonPress] = bpress,
179 [ButtonRelease] = brelease,
181 * Uncomment if you want the selection to disappear when you select something
182 * different in another window.
184 /* [SelectionClear] = selclear_, */
185 [SelectionNotify] = selnotify,
187 * PropertyNotify is only turned on when there is some INCR transfer happening
188 * for the selection retrieval.
190 [PropertyNotify] = propnotify,
191 [SelectionRequest] = selrequest,
197 static XSelection xsel;
198 static TermWindow win;
205 /* Font Ring Cache */
219 /* Fontcache is an array now. A new font will be appended to the array. */
220 static Fontcache frc[16];
221 static int frclen = 0;
222 static char *usedfont = NULL;
223 static double usedfontsize = 0;
224 static double defaultfontsize = 0;
226 static char *opt_class = NULL;
227 static char **opt_cmd = NULL;
228 static char *opt_embed = NULL;
229 static char *opt_font = NULL;
230 static char *opt_io = NULL;
231 static char *opt_line = NULL;
232 static char *opt_name = NULL;
233 static char *opt_title = NULL;
236 clipcopy(const Arg *dummy)
240 if (xsel.clipboard != NULL)
241 free(xsel.clipboard);
243 if (xsel.primary != NULL) {
244 xsel.clipboard = xstrdup(xsel.primary);
245 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
246 XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
251 clippaste(const Arg *dummy)
255 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
256 XConvertSelection(xw.dpy, clipboard, xsel.xtarget, clipboard,
257 xw.win, CurrentTime);
261 selpaste(const Arg *dummy)
263 XConvertSelection(xw.dpy, XA_PRIMARY, xsel.xtarget, XA_PRIMARY,
264 xw.win, CurrentTime);
272 larg.f = usedfontsize + arg->f;
277 zoomabs(const Arg *arg)
280 xloadfonts(usedfont, arg->f);
287 zoomreset(const Arg *arg)
291 if (defaultfontsize > 0) {
292 larg.f = defaultfontsize;
303 return LIMIT(x, 0, term.col-1);
312 return LIMIT(y, 0, term.row-1);
318 int type, seltype = SEL_REGULAR;
319 uint state = e->xbutton.state & ~(Button1Mask | forceselmod);
321 for (type = 1; type < LEN(selmasks); ++type) {
322 if (match(selmasks[type], state)) {
328 selextend(x2col(e->xbutton.x), y2row(e->xbutton.y), seltype);
332 mousereport(XEvent *e)
334 int x = x2col(e->xbutton.x), y = y2row(e->xbutton.y),
335 button = e->xbutton.button, state = e->xbutton.state,
341 if (e->xbutton.type == MotionNotify) {
342 if (x == ox && y == oy)
344 if (!IS_SET(MODE_MOUSEMOTION) && !IS_SET(MODE_MOUSEMANY))
346 /* MOUSE_MOTION: no reporting if no button is pressed */
347 if (IS_SET(MODE_MOUSEMOTION) && oldbutton == 3)
350 button = oldbutton + 32;
354 if (!IS_SET(MODE_MOUSESGR) && e->xbutton.type == ButtonRelease) {
361 if (e->xbutton.type == ButtonPress) {
365 } else if (e->xbutton.type == ButtonRelease) {
367 /* MODE_MOUSEX10: no button release reporting */
368 if (IS_SET(MODE_MOUSEX10))
370 if (button == 64 || button == 65)
375 if (!IS_SET(MODE_MOUSEX10)) {
376 button += ((state & ShiftMask ) ? 4 : 0)
377 + ((state & Mod4Mask ) ? 8 : 0)
378 + ((state & ControlMask) ? 16 : 0);
381 if (IS_SET(MODE_MOUSESGR)) {
382 len = snprintf(buf, sizeof(buf), "\033[<%d;%d;%d%c",
384 e->xbutton.type == ButtonRelease ? 'm' : 'M');
385 } else if (x < 223 && y < 223) {
386 len = snprintf(buf, sizeof(buf), "\033[M%c%c%c",
387 32+button, 32+x+1, 32+y+1);
402 if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
407 for (ms = mshortcuts; ms < mshortcuts + LEN(mshortcuts); ms++) {
408 if (e->xbutton.button == ms->b
409 && match(ms->mask, e->xbutton.state)) {
410 ttysend(ms->s, strlen(ms->s));
415 if (e->xbutton.button == Button1) {
417 * If the user clicks below predefined timeouts specific
418 * snapping behaviour is exposed.
420 clock_gettime(CLOCK_MONOTONIC, &now);
421 if (TIMEDIFF(now, xsel.tclick2) <= tripleclicktimeout) {
423 } else if (TIMEDIFF(now, xsel.tclick1) <= doubleclicktimeout) {
428 xsel.tclick2 = xsel.tclick1;
431 selstart(x2col(e->xbutton.x), y2row(e->xbutton.y), snap);
436 propnotify(XEvent *e)
438 XPropertyEvent *xpev;
439 Atom clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
441 xpev = &e->xproperty;
442 if (xpev->state == PropertyNewValue &&
443 (xpev->atom == XA_PRIMARY ||
444 xpev->atom == clipboard)) {
452 ulong nitems, ofs, rem;
454 uchar *data, *last, *repl;
455 Atom type, incratom, property;
457 incratom = XInternAtom(xw.dpy, "INCR", 0);
460 if (e->type == SelectionNotify) {
461 property = e->xselection.property;
462 } else if(e->type == PropertyNotify) {
463 property = e->xproperty.atom;
467 if (property == None)
471 if (XGetWindowProperty(xw.dpy, xw.win, property, ofs,
472 BUFSIZ/4, False, AnyPropertyType,
473 &type, &format, &nitems, &rem,
475 fprintf(stderr, "Clipboard allocation failed\n");
479 if (e->type == PropertyNotify && nitems == 0 && rem == 0) {
481 * If there is some PropertyNotify with no data, then
482 * this is the signal of the selection owner that all
483 * data has been transferred. We won't need to receive
484 * PropertyNotify events anymore.
486 MODBIT(xw.attrs.event_mask, 0, PropertyChangeMask);
487 XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
491 if (type == incratom) {
493 * Activate the PropertyNotify events so we receive
494 * when the selection owner does send us the next
497 MODBIT(xw.attrs.event_mask, 1, PropertyChangeMask);
498 XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
502 * Deleting the property is the transfer start signal.
504 XDeleteProperty(xw.dpy, xw.win, (int)property);
510 * Line endings are inconsistent in the terminal and GUI world
511 * copy and pasting. When receiving some selection data,
512 * replace all '\n' with '\r'.
513 * FIXME: Fix the computer world.
516 last = data + nitems * format / 8;
517 while ((repl = memchr(repl, '\n', last - repl))) {
521 if (IS_SET(MODE_BRCKTPASTE) && ofs == 0)
522 ttywrite("\033[200~", 6);
523 ttysend((char *)data, nitems * format / 8);
524 if (IS_SET(MODE_BRCKTPASTE) && rem == 0)
525 ttywrite("\033[201~", 6);
527 /* number of 32-bit chunks returned */
528 ofs += nitems * format / 32;
532 * Deleting the property again tells the selection owner to send the
533 * next data chunk in the property.
535 XDeleteProperty(xw.dpy, xw.win, (int)property);
551 selrequest(XEvent *e)
553 XSelectionRequestEvent *xsre;
555 Atom xa_targets, string, clipboard;
558 xsre = (XSelectionRequestEvent *) e;
559 xev.type = SelectionNotify;
560 xev.requestor = xsre->requestor;
561 xev.selection = xsre->selection;
562 xev.target = xsre->target;
563 xev.time = xsre->time;
564 if (xsre->property == None)
565 xsre->property = xsre->target;
570 xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
571 if (xsre->target == xa_targets) {
572 /* respond with the supported type */
573 string = xsel.xtarget;
574 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
575 XA_ATOM, 32, PropModeReplace,
576 (uchar *) &string, 1);
577 xev.property = xsre->property;
578 } else if (xsre->target == xsel.xtarget || xsre->target == XA_STRING) {
580 * xith XA_STRING non ascii characters may be incorrect in the
581 * requestor. It is not our problem, use utf8.
583 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
584 if (xsre->selection == XA_PRIMARY) {
585 seltext = xsel.primary;
586 } else if (xsre->selection == clipboard) {
587 seltext = xsel.clipboard;
590 "Unhandled clipboard selection 0x%lx\n",
594 if (seltext != NULL) {
595 XChangeProperty(xsre->display, xsre->requestor,
596 xsre->property, xsre->target,
598 (uchar *)seltext, strlen(seltext));
599 xev.property = xsre->property;
603 /* all done, send a notification to the listener */
604 if (!XSendEvent(xsre->display, xsre->requestor, 1, 0, (XEvent *) &xev))
605 fprintf(stderr, "Error sending SelectionNotify event\n");
609 setsel(char *str, Time t)
614 XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, t);
615 if (XGetSelectionOwner(xw.dpy, XA_PRIMARY) != xw.win)
622 setsel(str, CurrentTime);
628 if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
633 if (e->xbutton.button == Button2) {
635 } else if (e->xbutton.button == Button1) {
636 if (sel.mode == SEL_READY) {
638 setsel(getsel(), e->xbutton.time);
648 if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
656 sel.mode = SEL_READY;
661 cresize(int width, int height)
670 col = (win.w - 2 * borderpx) / win.cw;
671 row = (win.h - 2 * borderpx) / win.ch;
675 ttyresize(win.tw, win.th);
679 xresize(int col, int row)
681 win.tw = MAX(1, col * win.cw);
682 win.th = MAX(1, row * win.ch);
684 XFreePixmap(xw.dpy, xw.buf);
685 xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
686 DefaultDepth(xw.dpy, xw.scr));
687 XftDrawChange(xw.draw, xw.buf);
688 xclear(0, 0, win.w, win.h);
690 /* resize to new width */
691 xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec));
697 return x == 0 ? 0 : 0x3737 + 0x2828 * x;
701 xloadcolor(int i, const char *name, Color *ncolor)
703 XRenderColor color = { .alpha = 0xffff };
706 if (BETWEEN(i, 16, 255)) { /* 256 color */
707 if (i < 6*6*6+16) { /* same colors as xterm */
708 color.red = sixd_to_16bit( ((i-16)/36)%6 );
709 color.green = sixd_to_16bit( ((i-16)/6) %6 );
710 color.blue = sixd_to_16bit( ((i-16)/1) %6 );
711 } else { /* greyscale */
712 color.red = 0x0808 + 0x0a0a * (i - (6*6*6+16));
713 color.green = color.blue = color.red;
715 return XftColorAllocValue(xw.dpy, xw.vis,
716 xw.cmap, &color, ncolor);
721 return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
731 dc.collen = MAX(LEN(colorname), 256);
732 dc.col = xmalloc(dc.collen * sizeof(Color));
735 for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp)
736 XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
739 for (i = 0; i < dc.collen; i++)
740 if (!xloadcolor(i, NULL, &dc.col[i])) {
742 die("Could not allocate color '%s'\n", colorname[i]);
744 die("Could not allocate color %d\n", i);
750 xsetcolorname(int x, const char *name)
754 if (!BETWEEN(x, 0, dc.collen))
758 if (!xloadcolor(x, name, &ncolor))
761 XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
768 * Absolute coordinates.
771 xclear(int x1, int y1, int x2, int y2)
774 &dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
775 x1, y1, x2-x1, y2-y1);
781 XClassHint class = {opt_name ? opt_name : termname,
782 opt_class ? opt_class : termname};
783 XWMHints wm = {.flags = InputHint, .input = 1};
784 XSizeHints *sizeh = NULL;
786 sizeh = XAllocSizeHints();
788 sizeh->flags = PSize | PResizeInc | PBaseSize;
789 sizeh->height = win.h;
790 sizeh->width = win.w;
791 sizeh->height_inc = win.ch;
792 sizeh->width_inc = win.cw;
793 sizeh->base_height = 2 * borderpx;
794 sizeh->base_width = 2 * borderpx;
796 sizeh->flags |= PMaxSize | PMinSize;
797 sizeh->min_width = sizeh->max_width = win.w;
798 sizeh->min_height = sizeh->max_height = win.h;
800 if (xw.gm & (XValue|YValue)) {
801 sizeh->flags |= USPosition | PWinGravity;
804 sizeh->win_gravity = xgeommasktogravity(xw.gm);
807 XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm,
813 xgeommasktogravity(int mask)
815 switch (mask & (XNegative|YNegative)) {
817 return NorthWestGravity;
819 return NorthEastGravity;
821 return SouthWestGravity;
824 return SouthEastGravity;
828 xloadfont(Font *f, FcPattern *pattern)
830 FcPattern *configured;
834 int wantattr, haveattr;
837 * Manually configure instead of calling XftMatchFont
838 * so that we can use the configured pattern for
839 * "missing glyph" lookups.
841 configured = FcPatternDuplicate(pattern);
845 FcConfigSubstitute(NULL, configured, FcMatchPattern);
846 XftDefaultSubstitute(xw.dpy, xw.scr, configured);
848 match = FcFontMatch(NULL, configured, &result);
850 FcPatternDestroy(configured);
854 if (!(f->match = XftFontOpenPattern(xw.dpy, match))) {
855 FcPatternDestroy(configured);
856 FcPatternDestroy(match);
860 if ((XftPatternGetInteger(pattern, "slant", 0, &wantattr) ==
863 * Check if xft was unable to find a font with the appropriate
864 * slant but gave us one anyway. Try to mitigate.
866 if ((XftPatternGetInteger(f->match->pattern, "slant", 0,
867 &haveattr) != XftResultMatch) || haveattr < wantattr) {
869 fputs("st: font slant does not match\n", stderr);
873 if ((XftPatternGetInteger(pattern, "weight", 0, &wantattr) ==
875 if ((XftPatternGetInteger(f->match->pattern, "weight", 0,
876 &haveattr) != XftResultMatch) || haveattr != wantattr) {
878 fputs("st: font weight does not match\n", stderr);
882 XftTextExtentsUtf8(xw.dpy, f->match,
883 (const FcChar8 *) ascii_printable,
884 strlen(ascii_printable), &extents);
887 f->pattern = configured;
889 f->ascent = f->match->ascent;
890 f->descent = f->match->descent;
892 f->rbearing = f->match->max_advance_width;
894 f->height = f->ascent + f->descent;
895 f->width = DIVCEIL(extents.xOff, strlen(ascii_printable));
901 xloadfonts(char *fontstr, double fontsize)
907 if (fontstr[0] == '-') {
908 pattern = XftXlfdParse(fontstr, False, False);
910 pattern = FcNameParse((FcChar8 *)fontstr);
914 die("st: can't open font %s\n", fontstr);
917 FcPatternDel(pattern, FC_PIXEL_SIZE);
918 FcPatternDel(pattern, FC_SIZE);
919 FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
920 usedfontsize = fontsize;
922 if (FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) ==
924 usedfontsize = fontval;
925 } else if (FcPatternGetDouble(pattern, FC_SIZE, 0, &fontval) ==
930 * Default font size is 12, if none given. This is to
931 * have a known usedfontsize value.
933 FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
936 defaultfontsize = usedfontsize;
939 if (xloadfont(&dc.font, pattern))
940 die("st: can't open font %s\n", fontstr);
942 if (usedfontsize < 0) {
943 FcPatternGetDouble(dc.font.match->pattern,
944 FC_PIXEL_SIZE, 0, &fontval);
945 usedfontsize = fontval;
947 defaultfontsize = fontval;
950 /* Setting character width and height. */
951 win.cw = ceilf(dc.font.width * cwscale);
952 win.ch = ceilf(dc.font.height * chscale);
954 FcPatternDel(pattern, FC_SLANT);
955 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
956 if (xloadfont(&dc.ifont, pattern))
957 die("st: can't open font %s\n", fontstr);
959 FcPatternDel(pattern, FC_WEIGHT);
960 FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
961 if (xloadfont(&dc.ibfont, pattern))
962 die("st: can't open font %s\n", fontstr);
964 FcPatternDel(pattern, FC_SLANT);
965 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
966 if (xloadfont(&dc.bfont, pattern))
967 die("st: can't open font %s\n", fontstr);
969 FcPatternDestroy(pattern);
975 XftFontClose(xw.dpy, f->match);
976 FcPatternDestroy(f->pattern);
978 FcFontSetDestroy(f->set);
984 /* Free the loaded fonts in the font cache. */
986 XftFontClose(xw.dpy, frc[--frclen].font);
988 xunloadfont(&dc.font);
989 xunloadfont(&dc.bfont);
990 xunloadfont(&dc.ifont);
991 xunloadfont(&dc.ibfont);
1000 pid_t thispid = getpid();
1001 XColor xmousefg, xmousebg;
1003 if (!(xw.dpy = XOpenDisplay(NULL)))
1004 die("Can't open display\n");
1005 xw.scr = XDefaultScreen(xw.dpy);
1006 xw.vis = XDefaultVisual(xw.dpy, xw.scr);
1010 die("Could not init fontconfig.\n");
1012 usedfont = (opt_font == NULL)? font : opt_font;
1013 xloadfonts(usedfont, 0);
1016 xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
1019 /* adjust fixed window geometry */
1020 win.w = 2 * borderpx + term.col * win.cw;
1021 win.h = 2 * borderpx + term.row * win.ch;
1022 if (xw.gm & XNegative)
1023 xw.l += DisplayWidth(xw.dpy, xw.scr) - win.w - 2;
1024 if (xw.gm & YNegative)
1025 xw.t += DisplayHeight(xw.dpy, xw.scr) - win.h - 2;
1028 xw.attrs.background_pixel = dc.col[defaultbg].pixel;
1029 xw.attrs.border_pixel = dc.col[defaultbg].pixel;
1030 xw.attrs.bit_gravity = NorthWestGravity;
1031 xw.attrs.event_mask = FocusChangeMask | KeyPressMask
1032 | ExposureMask | VisibilityChangeMask | StructureNotifyMask
1033 | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
1034 xw.attrs.colormap = xw.cmap;
1036 if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
1037 parent = XRootWindow(xw.dpy, xw.scr);
1038 xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
1039 win.w, win.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
1040 xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
1041 | CWEventMask | CWColormap, &xw.attrs);
1043 memset(&gcvalues, 0, sizeof(gcvalues));
1044 gcvalues.graphics_exposures = False;
1045 dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
1047 xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
1048 DefaultDepth(xw.dpy, xw.scr));
1049 XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
1050 XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
1052 /* font spec buffer */
1053 xw.specbuf = xmalloc(term.col * sizeof(GlyphFontSpec));
1055 /* Xft rendering context */
1056 xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
1059 if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
1060 XSetLocaleModifiers("@im=local");
1061 if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
1062 XSetLocaleModifiers("@im=");
1063 if ((xw.xim = XOpenIM(xw.dpy,
1064 NULL, NULL, NULL)) == NULL) {
1065 die("XOpenIM failed. Could not open input"
1070 xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
1071 | XIMStatusNothing, XNClientWindow, xw.win,
1072 XNFocusWindow, xw.win, NULL);
1074 die("XCreateIC failed. Could not obtain input method.\n");
1076 /* white cursor, black outline */
1077 cursor = XCreateFontCursor(xw.dpy, mouseshape);
1078 XDefineCursor(xw.dpy, xw.win, cursor);
1080 if (XParseColor(xw.dpy, xw.cmap, colorname[mousefg], &xmousefg) == 0) {
1081 xmousefg.red = 0xffff;
1082 xmousefg.green = 0xffff;
1083 xmousefg.blue = 0xffff;
1086 if (XParseColor(xw.dpy, xw.cmap, colorname[mousebg], &xmousebg) == 0) {
1087 xmousebg.red = 0x0000;
1088 xmousebg.green = 0x0000;
1089 xmousebg.blue = 0x0000;
1092 XRecolorCursor(xw.dpy, cursor, &xmousefg, &xmousebg);
1094 xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
1095 xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
1096 xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False);
1097 XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
1099 xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False);
1100 XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL, 32,
1101 PropModeReplace, (uchar *)&thispid, 1);
1104 XMapWindow(xw.dpy, xw.win);
1106 XSync(xw.dpy, False);
1108 clock_gettime(CLOCK_MONOTONIC, &xsel.tclick1);
1109 clock_gettime(CLOCK_MONOTONIC, &xsel.tclick2);
1110 xsel.primary = NULL;
1111 xsel.clipboard = NULL;
1112 xsel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
1113 if (xsel.xtarget == None)
1114 xsel.xtarget = XA_STRING;
1118 xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y)
1120 float winx = borderpx + x * win.cw, winy = borderpx + y * win.ch, xp, yp;
1121 ushort mode, prevmode = USHRT_MAX;
1122 Font *font = &dc.font;
1123 int frcflags = FRC_NORMAL;
1124 float runewidth = win.cw;
1128 FcPattern *fcpattern, *fontpattern;
1129 FcFontSet *fcsets[] = { NULL };
1130 FcCharSet *fccharset;
1131 int i, f, numspecs = 0;
1133 for (i = 0, xp = winx, yp = winy + font->ascent; i < len; ++i) {
1134 /* Fetch rune and mode for current glyph. */
1136 mode = glyphs[i].mode;
1138 /* Skip dummy wide-character spacing. */
1139 if (mode == ATTR_WDUMMY)
1142 /* Determine font for glyph if different from previous glyph. */
1143 if (prevmode != mode) {
1146 frcflags = FRC_NORMAL;
1147 runewidth = win.cw * ((mode & ATTR_WIDE) ? 2.0f : 1.0f);
1148 if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) {
1150 frcflags = FRC_ITALICBOLD;
1151 } else if (mode & ATTR_ITALIC) {
1153 frcflags = FRC_ITALIC;
1154 } else if (mode & ATTR_BOLD) {
1156 frcflags = FRC_BOLD;
1158 yp = winy + font->ascent;
1161 /* Lookup character index with default font. */
1162 glyphidx = XftCharIndex(xw.dpy, font->match, rune);
1164 specs[numspecs].font = font->match;
1165 specs[numspecs].glyph = glyphidx;
1166 specs[numspecs].x = (short)xp;
1167 specs[numspecs].y = (short)yp;
1173 /* Fallback on font cache, search the font cache for match. */
1174 for (f = 0; f < frclen; f++) {
1175 glyphidx = XftCharIndex(xw.dpy, frc[f].font, rune);
1176 /* Everything correct. */
1177 if (glyphidx && frc[f].flags == frcflags)
1179 /* We got a default font for a not found glyph. */
1180 if (!glyphidx && frc[f].flags == frcflags
1181 && frc[f].unicodep == rune) {
1186 /* Nothing was found. Use fontconfig to find matching font. */
1189 font->set = FcFontSort(0, font->pattern,
1191 fcsets[0] = font->set;
1194 * Nothing was found in the cache. Now use
1195 * some dozen of Fontconfig calls to get the
1196 * font for one single character.
1198 * Xft and fontconfig are design failures.
1200 fcpattern = FcPatternDuplicate(font->pattern);
1201 fccharset = FcCharSetCreate();
1203 FcCharSetAddChar(fccharset, rune);
1204 FcPatternAddCharSet(fcpattern, FC_CHARSET,
1206 FcPatternAddBool(fcpattern, FC_SCALABLE, 1);
1208 FcConfigSubstitute(0, fcpattern,
1210 FcDefaultSubstitute(fcpattern);
1212 fontpattern = FcFontSetMatch(0, fcsets, 1,
1216 * Overwrite or create the new cache entry.
1218 if (frclen >= LEN(frc)) {
1219 frclen = LEN(frc) - 1;
1220 XftFontClose(xw.dpy, frc[frclen].font);
1221 frc[frclen].unicodep = 0;
1224 frc[frclen].font = XftFontOpenPattern(xw.dpy,
1226 if (!frc[frclen].font)
1227 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1229 frc[frclen].flags = frcflags;
1230 frc[frclen].unicodep = rune;
1232 glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune);
1237 FcPatternDestroy(fcpattern);
1238 FcCharSetDestroy(fccharset);
1241 specs[numspecs].font = frc[f].font;
1242 specs[numspecs].glyph = glyphidx;
1243 specs[numspecs].x = (short)xp;
1244 specs[numspecs].y = (short)yp;
1253 xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, int y)
1255 int charlen = len * ((base.mode & ATTR_WIDE) ? 2 : 1);
1256 int winx = borderpx + x * win.cw, winy = borderpx + y * win.ch,
1257 width = charlen * win.cw;
1258 Color *fg, *bg, *temp, revfg, revbg, truefg, truebg;
1259 XRenderColor colfg, colbg;
1262 /* Fallback on color display for attributes not supported by the font */
1263 if (base.mode & ATTR_ITALIC && base.mode & ATTR_BOLD) {
1264 if (dc.ibfont.badslant || dc.ibfont.badweight)
1265 base.fg = defaultattr;
1266 } else if ((base.mode & ATTR_ITALIC && dc.ifont.badslant) ||
1267 (base.mode & ATTR_BOLD && dc.bfont.badweight)) {
1268 base.fg = defaultattr;
1271 if (IS_TRUECOL(base.fg)) {
1272 colfg.alpha = 0xffff;
1273 colfg.red = TRUERED(base.fg);
1274 colfg.green = TRUEGREEN(base.fg);
1275 colfg.blue = TRUEBLUE(base.fg);
1276 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &truefg);
1279 fg = &dc.col[base.fg];
1282 if (IS_TRUECOL(base.bg)) {
1283 colbg.alpha = 0xffff;
1284 colbg.green = TRUEGREEN(base.bg);
1285 colbg.red = TRUERED(base.bg);
1286 colbg.blue = TRUEBLUE(base.bg);
1287 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &truebg);
1290 bg = &dc.col[base.bg];
1293 /* Change basic system colors [0-7] to bright system colors [8-15] */
1294 if ((base.mode & ATTR_BOLD_FAINT) == ATTR_BOLD && BETWEEN(base.fg, 0, 7))
1295 fg = &dc.col[base.fg + 8];
1297 if (IS_SET(MODE_REVERSE)) {
1298 if (fg == &dc.col[defaultfg]) {
1299 fg = &dc.col[defaultbg];
1301 colfg.red = ~fg->color.red;
1302 colfg.green = ~fg->color.green;
1303 colfg.blue = ~fg->color.blue;
1304 colfg.alpha = fg->color.alpha;
1305 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg,
1310 if (bg == &dc.col[defaultbg]) {
1311 bg = &dc.col[defaultfg];
1313 colbg.red = ~bg->color.red;
1314 colbg.green = ~bg->color.green;
1315 colbg.blue = ~bg->color.blue;
1316 colbg.alpha = bg->color.alpha;
1317 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg,
1323 if ((base.mode & ATTR_BOLD_FAINT) == ATTR_FAINT) {
1324 colfg.red = fg->color.red / 2;
1325 colfg.green = fg->color.green / 2;
1326 colfg.blue = fg->color.blue / 2;
1327 colfg.alpha = fg->color.alpha;
1328 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
1333 if (base.mode & ATTR_REVERSE) {
1339 if (base.mode & ATTR_BLINK && term.mode & MODE_BLINK)
1342 if (base.mode & ATTR_INVISIBLE)
1345 /* Intelligent cleaning up of the borders. */
1347 xclear(0, (y == 0)? 0 : winy, borderpx,
1348 winy + win.ch + ((y >= term.row-1)? win.h : 0));
1350 if (x + charlen >= term.col) {
1351 xclear(winx + width, (y == 0)? 0 : winy, win.w,
1352 ((y >= term.row-1)? win.h : (winy + win.ch)));
1355 xclear(winx, 0, winx + width, borderpx);
1356 if (y == term.row-1)
1357 xclear(winx, winy + win.ch, winx + width, win.h);
1359 /* Clean up the region we want to draw to. */
1360 XftDrawRect(xw.draw, bg, winx, winy, width, win.ch);
1362 /* Set the clip region because Xft is sometimes dirty. */
1367 XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1);
1369 /* Render the glyphs. */
1370 XftDrawGlyphFontSpec(xw.draw, fg, specs, len);
1372 /* Render underline and strikethrough. */
1373 if (base.mode & ATTR_UNDERLINE) {
1374 XftDrawRect(xw.draw, fg, winx, winy + dc.font.ascent + 1,
1378 if (base.mode & ATTR_STRUCK) {
1379 XftDrawRect(xw.draw, fg, winx, winy + 2 * dc.font.ascent / 3,
1383 /* Reset clip to none. */
1384 XftDrawSetClip(xw.draw, 0);
1388 xdrawglyph(Glyph g, int x, int y)
1391 XftGlyphFontSpec spec;
1393 numspecs = xmakeglyphfontspecs(&spec, &g, 1, x, y);
1394 xdrawglyphfontspecs(&spec, g, numspecs, x, y);
1400 static int oldx = 0, oldy = 0;
1402 Glyph g = {' ', ATTR_NULL, defaultbg, defaultcs}, og;
1405 LIMIT(oldx, 0, term.col-1);
1406 LIMIT(oldy, 0, term.row-1);
1410 /* adjust position if in dummy */
1411 if (term.line[oldy][oldx].mode & ATTR_WDUMMY)
1413 if (term.line[term.c.y][curx].mode & ATTR_WDUMMY)
1416 /* remove the old cursor */
1417 og = term.line[oldy][oldx];
1418 if (selected(oldx, oldy))
1419 og.mode ^= ATTR_REVERSE;
1420 xdrawglyph(og, oldx, oldy);
1422 g.u = term.line[term.c.y][term.c.x].u;
1423 g.mode |= term.line[term.c.y][term.c.x].mode &
1424 (ATTR_BOLD | ATTR_ITALIC | ATTR_UNDERLINE | ATTR_STRUCK);
1427 * Select the right color for the right mode.
1429 if (IS_SET(MODE_REVERSE)) {
1430 g.mode |= ATTR_REVERSE;
1432 if (selected(term.c.x, term.c.y)) {
1433 drawcol = dc.col[defaultcs];
1436 drawcol = dc.col[defaultrcs];
1440 if (selected(term.c.x, term.c.y)) {
1441 drawcol = dc.col[defaultrcs];
1445 drawcol = dc.col[defaultcs];
1449 if (IS_SET(MODE_HIDE))
1452 /* draw the new one */
1453 if (win.state & WIN_FOCUSED) {
1454 switch (win.cursor) {
1455 case 7: /* st extension: snowman */
1456 utf8decode("☃", &g.u, UTF_SIZ);
1457 case 0: /* Blinking Block */
1458 case 1: /* Blinking Block (Default) */
1459 case 2: /* Steady Block */
1460 g.mode |= term.line[term.c.y][curx].mode & ATTR_WIDE;
1461 xdrawglyph(g, term.c.x, term.c.y);
1463 case 3: /* Blinking Underline */
1464 case 4: /* Steady Underline */
1465 XftDrawRect(xw.draw, &drawcol,
1466 borderpx + curx * win.cw,
1467 borderpx + (term.c.y + 1) * win.ch - \
1469 win.cw, cursorthickness);
1471 case 5: /* Blinking bar */
1472 case 6: /* Steady bar */
1473 XftDrawRect(xw.draw, &drawcol,
1474 borderpx + curx * win.cw,
1475 borderpx + term.c.y * win.ch,
1476 cursorthickness, win.ch);
1480 XftDrawRect(xw.draw, &drawcol,
1481 borderpx + curx * win.cw,
1482 borderpx + term.c.y * win.ch,
1484 XftDrawRect(xw.draw, &drawcol,
1485 borderpx + curx * win.cw,
1486 borderpx + term.c.y * win.ch,
1488 XftDrawRect(xw.draw, &drawcol,
1489 borderpx + (curx + 1) * win.cw - 1,
1490 borderpx + term.c.y * win.ch,
1492 XftDrawRect(xw.draw, &drawcol,
1493 borderpx + curx * win.cw,
1494 borderpx + (term.c.y + 1) * win.ch - 1,
1497 oldx = curx, oldy = term.c.y;
1503 char buf[sizeof(long) * 8 + 1];
1505 snprintf(buf, sizeof(buf), "%lu", xw.win);
1506 setenv("WINDOWID", buf, 1);
1515 Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
1517 XSetWMName(xw.dpy, xw.win, &prop);
1518 XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmname);
1525 drawregion(0, 0, term.col, term.row);
1526 XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, win.w,
1528 XSetForeground(xw.dpy, dc.gc,
1529 dc.col[IS_SET(MODE_REVERSE)?
1530 defaultfg : defaultbg].pixel);
1534 drawregion(int x1, int y1, int x2, int y2)
1536 int i, x, y, ox, numspecs;
1538 XftGlyphFontSpec *specs;
1540 if (!(win.state & WIN_VISIBLE))
1543 for (y = y1; y < y2; y++) {
1550 numspecs = xmakeglyphfontspecs(specs, &term.line[y][x1], x2 - x1, x1, y);
1553 for (x = x1; x < x2 && i < numspecs; x++) {
1554 new = term.line[y][x];
1555 if (new.mode == ATTR_WDUMMY)
1558 new.mode ^= ATTR_REVERSE;
1559 if (i > 0 && ATTRCMP(base, new)) {
1560 xdrawglyphfontspecs(specs, base, i, ox, y);
1572 xdrawglyphfontspecs(specs, base, i, ox, y);
1584 visibility(XEvent *ev)
1586 XVisibilityEvent *e = &ev->xvisibility;
1588 MODBIT(win.state, e->state != VisibilityFullyObscured, WIN_VISIBLE);
1594 win.state &= ~WIN_VISIBLE;
1598 xsetpointermotion(int set)
1600 MODBIT(xw.attrs.event_mask, set, PointerMotionMask);
1601 XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs);
1605 xsetcursor(int cursor)
1608 if (!BETWEEN(cursor, 0, 6))
1610 win.cursor = cursor;
1615 xseturgency(int add)
1617 XWMHints *h = XGetWMHints(xw.dpy, xw.win);
1619 MODBIT(h->flags, add, XUrgencyHint);
1620 XSetWMHints(xw.dpy, xw.win, h);
1627 if (!(win.state & WIN_FOCUSED))
1630 XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL);
1636 XFocusChangeEvent *e = &ev->xfocus;
1638 if (e->mode == NotifyGrab)
1641 if (ev->type == FocusIn) {
1642 XSetICFocus(xw.xic);
1643 win.state |= WIN_FOCUSED;
1645 if (IS_SET(MODE_FOCUS))
1646 ttywrite("\033[I", 3);
1648 XUnsetICFocus(xw.xic);
1649 win.state &= ~WIN_FOCUSED;
1650 if (IS_SET(MODE_FOCUS))
1651 ttywrite("\033[O", 3);
1656 match(uint mask, uint state)
1658 return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
1662 kmap(KeySym k, uint state)
1667 /* Check for mapped keys out of X11 function keys. */
1668 for (i = 0; i < LEN(mappedkeys); i++) {
1669 if (mappedkeys[i] == k)
1672 if (i == LEN(mappedkeys)) {
1673 if ((k & 0xFFFF) < 0xFD00)
1677 for (kp = key; kp < key + LEN(key); kp++) {
1681 if (!match(kp->mask, state))
1684 if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0)
1686 if (term.numlock && kp->appkey == 2)
1689 if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0)
1692 if (IS_SET(MODE_CRLF) ? kp->crlf < 0 : kp->crlf > 0)
1704 XKeyEvent *e = &ev->xkey;
1706 char buf[32], *customkey;
1712 if (IS_SET(MODE_KBDLOCK))
1715 len = XmbLookupString(xw.xic, e, buf, sizeof buf, &ksym, &status);
1717 for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
1718 if (ksym == bp->keysym && match(bp->mod, e->state)) {
1719 bp->func(&(bp->arg));
1724 /* 2. custom keys from config.h */
1725 if ((customkey = kmap(ksym, e->state))) {
1726 ttysend(customkey, strlen(customkey));
1730 /* 3. composed string from input method */
1733 if (len == 1 && e->state & Mod1Mask) {
1734 if (IS_SET(MODE_8BIT)) {
1737 len = utf8encode(c, buf);
1754 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1756 if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
1757 if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
1758 win.state |= WIN_FOCUSED;
1760 } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
1761 win.state &= ~WIN_FOCUSED;
1763 } else if (e->xclient.data.l[0] == xw.wmdeletewin) {
1764 /* Send SIGHUP to shell */
1773 if (e->xconfigure.width == win.w && e->xconfigure.height == win.h)
1776 cresize(e->xconfigure.width, e->xconfigure.height);
1783 int w = win.w, h = win.h;
1785 int xfd = XConnectionNumber(xw.dpy), xev, blinkset = 0, dodraw = 0;
1786 struct timespec drawtimeout, *tv = NULL, now, last, lastblink;
1789 /* Waiting for window mapping */
1791 XNextEvent(xw.dpy, &ev);
1793 * This XFilterEvent call is required because of XOpenIM. It
1794 * does filter out the key event and some client message for
1795 * the input method too.
1797 if (XFilterEvent(&ev, None))
1799 if (ev.type == ConfigureNotify) {
1800 w = ev.xconfigure.width;
1801 h = ev.xconfigure.height;
1803 } while (ev.type != MapNotify);
1805 ttynew(opt_line, opt_io, opt_cmd);
1808 clock_gettime(CLOCK_MONOTONIC, &last);
1811 for (xev = actionfps;;) {
1813 FD_SET(cmdfd, &rfd);
1816 if (pselect(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv, NULL) < 0) {
1819 die("select failed: %s\n", strerror(errno));
1821 if (FD_ISSET(cmdfd, &rfd)) {
1824 blinkset = tattrset(ATTR_BLINK);
1826 MODBIT(term.mode, 0, MODE_BLINK);
1830 if (FD_ISSET(xfd, &rfd))
1833 clock_gettime(CLOCK_MONOTONIC, &now);
1834 drawtimeout.tv_sec = 0;
1835 drawtimeout.tv_nsec = (1000 * 1E6)/ xfps;
1839 if (blinktimeout && TIMEDIFF(now, lastblink) > blinktimeout) {
1840 tsetdirtattr(ATTR_BLINK);
1841 term.mode ^= MODE_BLINK;
1845 deltatime = TIMEDIFF(now, last);
1846 if (deltatime > 1000 / (xev ? xfps : actionfps)) {
1852 while (XPending(xw.dpy)) {
1853 XNextEvent(xw.dpy, &ev);
1854 if (XFilterEvent(&ev, None))
1856 if (handler[ev.type])
1857 (handler[ev.type])(&ev);
1863 if (xev && !FD_ISSET(xfd, &rfd))
1865 if (!FD_ISSET(cmdfd, &rfd) && !FD_ISSET(xfd, &rfd)) {
1867 if (TIMEDIFF(now, lastblink) \
1869 drawtimeout.tv_nsec = 1000;
1871 drawtimeout.tv_nsec = (1E6 * \
1876 drawtimeout.tv_sec = \
1877 drawtimeout.tv_nsec / 1E9;
1878 drawtimeout.tv_nsec %= (long)1E9;
1890 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
1891 " [-n name] [-o file]\n"
1892 " [-T title] [-t title] [-w windowid]"
1893 " [[-e] command [args ...]]\n"
1894 " %s [-aiv] [-c class] [-f font] [-g geometry]"
1895 " [-n name] [-o file]\n"
1896 " [-T title] [-t title] [-w windowid] -l line"
1897 " [stty_args ...]\n", argv0, argv0);
1901 main(int argc, char *argv[])
1905 win.cursor = cursorshape;
1912 opt_class = EARGF(usage());
1919 opt_font = EARGF(usage());
1922 xw.gm = XParseGeometry(EARGF(usage()),
1923 &xw.l, &xw.t, &cols, &rows);
1929 opt_io = EARGF(usage());
1932 opt_line = EARGF(usage());
1935 opt_name = EARGF(usage());
1939 opt_title = EARGF(usage());
1942 opt_embed = EARGF(usage());
1945 die("%s " VERSION " (c) 2010-2016 st engineers\n", argv0);
1953 /* eat all remaining arguments */
1955 if (!opt_title && !opt_line)
1956 opt_title = basename(xstrdup(argv[0]));
1958 setlocale(LC_CTYPE, "");
1959 XSetLocaleModifiers("");
1960 tnew(MAX(cols, 1), MAX(rows, 1));