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 */
47 #define XK_ANY_MOD UINT_MAX
49 #define XK_SWITCH_MOD (1<<13)
51 /* function definitions used in config.h */
52 static void clipcopy(const Arg *);
53 static void clippaste(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 TRUERED(x) (((x) & 0xff0000) >> 8)
68 #define TRUEGREEN(x) (((x) & 0xff00))
69 #define TRUEBLUE(x) (((x) & 0xff) << 8)
71 typedef XftDraw *Draw;
72 typedef XftColor Color;
73 typedef XftGlyphFontSpec GlyphFontSpec;
75 /* Purely graphic info */
81 GlyphFontSpec *specbuf; /* font spec buffer used for rendering */
82 Atom xembed, wmdeletewin, netwmname, netwmpid;
87 XSetWindowAttributes attrs;
89 int isfixed; /* is fixed geometry? */
90 int l, t; /* left and top offset */
91 int gm; /* geometry mask */
96 char *primary, *clipboard;
97 struct timespec tclick1;
98 struct timespec tclick2;
117 /* Drawing Context */
121 Font font, bfont, ifont, ibfont;
125 static inline ushort sixd_to_16bit(int);
126 static int xmakeglyphfontspecs(XftGlyphFontSpec *, const Glyph *, int, int, int);
127 static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int);
128 static void xdrawglyph(Glyph, int, int);
129 static void xclear(int, int, int, int);
130 static void xdrawcursor(void);
131 static int xgeommasktogravity(int);
132 static void xinit(void);
133 static void cresize(int, int);
134 static void xresize(int, int);
135 static int xloadfont(Font *, FcPattern *);
136 static void xloadfonts(char *, double);
137 static void xunloadfont(Font *);
138 static void xunloadfonts(void);
139 static void xsetenv(void);
140 static void xseturgency(int);
141 static int x2col(int);
142 static int y2row(int);
144 static void expose(XEvent *);
145 static void visibility(XEvent *);
146 static void unmap(XEvent *);
147 static void kpress(XEvent *);
148 static void cmessage(XEvent *);
149 static void resize(XEvent *);
150 static void focus(XEvent *);
151 static void brelease(XEvent *);
152 static void bpress(XEvent *);
153 static void bmotion(XEvent *);
154 static void propnotify(XEvent *);
155 static void selnotify(XEvent *);
156 static void selclear_(XEvent *);
157 static void selrequest(XEvent *);
158 static void setsel(char *, Time);
159 static void mousesel(XEvent *, int);
160 static void mousereport(XEvent *);
161 static char *kmap(KeySym, uint);
162 static int match(uint, uint);
164 static void run(void);
165 static void usage(void);
167 static void (*handler[LASTEvent])(XEvent *) = {
169 [ClientMessage] = cmessage,
170 [ConfigureNotify] = resize,
171 [VisibilityNotify] = visibility,
172 [UnmapNotify] = unmap,
176 [MotionNotify] = bmotion,
177 [ButtonPress] = bpress,
178 [ButtonRelease] = brelease,
180 * Uncomment if you want the selection to disappear when you select something
181 * different in another window.
183 /* [SelectionClear] = selclear_, */
184 [SelectionNotify] = selnotify,
186 * PropertyNotify is only turned on when there is some INCR transfer happening
187 * for the selection retrieval.
189 [PropertyNotify] = propnotify,
190 [SelectionRequest] = selrequest,
196 static XSelection xsel;
197 static TermWindow win;
204 /* Font Ring Cache */
218 /* Fontcache is an array now. A new font will be appended to the array. */
219 static Fontcache frc[16];
220 static int frclen = 0;
221 static char *usedfont = NULL;
222 static double usedfontsize = 0;
223 static double defaultfontsize = 0;
225 static char *opt_class = NULL;
226 static char **opt_cmd = NULL;
227 static char *opt_embed = NULL;
228 static char *opt_font = NULL;
229 static char *opt_io = NULL;
230 static char *opt_line = NULL;
231 static char *opt_name = NULL;
232 static char *opt_title = NULL;
235 clipcopy(const Arg *dummy)
239 if (xsel.clipboard != NULL)
240 free(xsel.clipboard);
242 if (xsel.primary != NULL) {
243 xsel.clipboard = xstrdup(xsel.primary);
244 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
245 XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
250 clippaste(const Arg *dummy)
254 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
255 XConvertSelection(xw.dpy, clipboard, xsel.xtarget, clipboard,
256 xw.win, CurrentTime);
260 selpaste(const Arg *dummy)
262 XConvertSelection(xw.dpy, XA_PRIMARY, xsel.xtarget, XA_PRIMARY,
263 xw.win, CurrentTime);
271 larg.f = usedfontsize + arg->f;
276 zoomabs(const Arg *arg)
279 xloadfonts(usedfont, arg->f);
286 zoomreset(const Arg *arg)
290 if (defaultfontsize > 0) {
291 larg.f = defaultfontsize;
302 return LIMIT(x, 0, term.col-1);
311 return LIMIT(y, 0, term.row-1);
315 mousesel(XEvent *e, int done)
317 int type, seltype = SEL_REGULAR;
318 uint state = e->xbutton.state & ~(Button1Mask | forceselmod);
320 for (type = 1; type < LEN(selmasks); ++type) {
321 if (match(selmasks[type], state)) {
326 selextend(x2col(e->xbutton.x), y2row(e->xbutton.y), seltype, done);
328 setsel(getsel(), e->xbutton.time);
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);
392 ttywrite(buf, len, 0);
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 ttywrite(ms->s, strlen(ms->s), 1);
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, 0);
523 ttywrite((char *)data, nitems * format / 8, 1);
524 if (IS_SET(MODE_BRCKTPASTE) && rem == 0)
525 ttywrite("\033[201~", 6, 0);
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)
642 if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
651 cresize(int width, int height)
660 col = (win.w - 2 * borderpx) / win.cw;
661 row = (win.h - 2 * borderpx) / win.ch;
665 ttyresize(win.tw, win.th);
669 xresize(int col, int row)
671 win.tw = MAX(1, col * win.cw);
672 win.th = MAX(1, row * win.ch);
674 XFreePixmap(xw.dpy, xw.buf);
675 xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
676 DefaultDepth(xw.dpy, xw.scr));
677 XftDrawChange(xw.draw, xw.buf);
678 xclear(0, 0, win.w, win.h);
680 /* resize to new width */
681 xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec));
687 return x == 0 ? 0 : 0x3737 + 0x2828 * x;
691 xloadcolor(int i, const char *name, Color *ncolor)
693 XRenderColor color = { .alpha = 0xffff };
696 if (BETWEEN(i, 16, 255)) { /* 256 color */
697 if (i < 6*6*6+16) { /* same colors as xterm */
698 color.red = sixd_to_16bit( ((i-16)/36)%6 );
699 color.green = sixd_to_16bit( ((i-16)/6) %6 );
700 color.blue = sixd_to_16bit( ((i-16)/1) %6 );
701 } else { /* greyscale */
702 color.red = 0x0808 + 0x0a0a * (i - (6*6*6+16));
703 color.green = color.blue = color.red;
705 return XftColorAllocValue(xw.dpy, xw.vis,
706 xw.cmap, &color, ncolor);
711 return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
721 dc.collen = MAX(LEN(colorname), 256);
722 dc.col = xmalloc(dc.collen * sizeof(Color));
725 for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp)
726 XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
729 for (i = 0; i < dc.collen; i++)
730 if (!xloadcolor(i, NULL, &dc.col[i])) {
732 die("Could not allocate color '%s'\n", colorname[i]);
734 die("Could not allocate color %d\n", i);
740 xsetcolorname(int x, const char *name)
744 if (!BETWEEN(x, 0, dc.collen))
748 if (!xloadcolor(x, name, &ncolor))
751 XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
758 * Absolute coordinates.
761 xclear(int x1, int y1, int x2, int y2)
764 &dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
765 x1, y1, x2-x1, y2-y1);
771 XClassHint class = {opt_name ? opt_name : termname,
772 opt_class ? opt_class : termname};
773 XWMHints wm = {.flags = InputHint, .input = 1};
774 XSizeHints *sizeh = NULL;
776 sizeh = XAllocSizeHints();
778 sizeh->flags = PSize | PResizeInc | PBaseSize;
779 sizeh->height = win.h;
780 sizeh->width = win.w;
781 sizeh->height_inc = win.ch;
782 sizeh->width_inc = win.cw;
783 sizeh->base_height = 2 * borderpx;
784 sizeh->base_width = 2 * borderpx;
786 sizeh->flags |= PMaxSize | PMinSize;
787 sizeh->min_width = sizeh->max_width = win.w;
788 sizeh->min_height = sizeh->max_height = win.h;
790 if (xw.gm & (XValue|YValue)) {
791 sizeh->flags |= USPosition | PWinGravity;
794 sizeh->win_gravity = xgeommasktogravity(xw.gm);
797 XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm,
803 xgeommasktogravity(int mask)
805 switch (mask & (XNegative|YNegative)) {
807 return NorthWestGravity;
809 return NorthEastGravity;
811 return SouthWestGravity;
814 return SouthEastGravity;
818 xloadfont(Font *f, FcPattern *pattern)
820 FcPattern *configured;
824 int wantattr, haveattr;
827 * Manually configure instead of calling XftMatchFont
828 * so that we can use the configured pattern for
829 * "missing glyph" lookups.
831 configured = FcPatternDuplicate(pattern);
835 FcConfigSubstitute(NULL, configured, FcMatchPattern);
836 XftDefaultSubstitute(xw.dpy, xw.scr, configured);
838 match = FcFontMatch(NULL, configured, &result);
840 FcPatternDestroy(configured);
844 if (!(f->match = XftFontOpenPattern(xw.dpy, match))) {
845 FcPatternDestroy(configured);
846 FcPatternDestroy(match);
850 if ((XftPatternGetInteger(pattern, "slant", 0, &wantattr) ==
853 * Check if xft was unable to find a font with the appropriate
854 * slant but gave us one anyway. Try to mitigate.
856 if ((XftPatternGetInteger(f->match->pattern, "slant", 0,
857 &haveattr) != XftResultMatch) || haveattr < wantattr) {
859 fputs("st: font slant does not match\n", stderr);
863 if ((XftPatternGetInteger(pattern, "weight", 0, &wantattr) ==
865 if ((XftPatternGetInteger(f->match->pattern, "weight", 0,
866 &haveattr) != XftResultMatch) || haveattr != wantattr) {
868 fputs("st: font weight does not match\n", stderr);
872 XftTextExtentsUtf8(xw.dpy, f->match,
873 (const FcChar8 *) ascii_printable,
874 strlen(ascii_printable), &extents);
877 f->pattern = configured;
879 f->ascent = f->match->ascent;
880 f->descent = f->match->descent;
882 f->rbearing = f->match->max_advance_width;
884 f->height = f->ascent + f->descent;
885 f->width = DIVCEIL(extents.xOff, strlen(ascii_printable));
891 xloadfonts(char *fontstr, double fontsize)
897 if (fontstr[0] == '-') {
898 pattern = XftXlfdParse(fontstr, False, False);
900 pattern = FcNameParse((FcChar8 *)fontstr);
904 die("st: can't open font %s\n", fontstr);
907 FcPatternDel(pattern, FC_PIXEL_SIZE);
908 FcPatternDel(pattern, FC_SIZE);
909 FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
910 usedfontsize = fontsize;
912 if (FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) ==
914 usedfontsize = fontval;
915 } else if (FcPatternGetDouble(pattern, FC_SIZE, 0, &fontval) ==
920 * Default font size is 12, if none given. This is to
921 * have a known usedfontsize value.
923 FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
926 defaultfontsize = usedfontsize;
929 if (xloadfont(&dc.font, pattern))
930 die("st: can't open font %s\n", fontstr);
932 if (usedfontsize < 0) {
933 FcPatternGetDouble(dc.font.match->pattern,
934 FC_PIXEL_SIZE, 0, &fontval);
935 usedfontsize = fontval;
937 defaultfontsize = fontval;
940 /* Setting character width and height. */
941 win.cw = ceilf(dc.font.width * cwscale);
942 win.ch = ceilf(dc.font.height * chscale);
944 FcPatternDel(pattern, FC_SLANT);
945 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
946 if (xloadfont(&dc.ifont, pattern))
947 die("st: can't open font %s\n", fontstr);
949 FcPatternDel(pattern, FC_WEIGHT);
950 FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
951 if (xloadfont(&dc.ibfont, pattern))
952 die("st: can't open font %s\n", fontstr);
954 FcPatternDel(pattern, FC_SLANT);
955 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
956 if (xloadfont(&dc.bfont, pattern))
957 die("st: can't open font %s\n", fontstr);
959 FcPatternDestroy(pattern);
965 XftFontClose(xw.dpy, f->match);
966 FcPatternDestroy(f->pattern);
968 FcFontSetDestroy(f->set);
974 /* Free the loaded fonts in the font cache. */
976 XftFontClose(xw.dpy, frc[--frclen].font);
978 xunloadfont(&dc.font);
979 xunloadfont(&dc.bfont);
980 xunloadfont(&dc.ifont);
981 xunloadfont(&dc.ibfont);
990 pid_t thispid = getpid();
991 XColor xmousefg, xmousebg;
993 if (!(xw.dpy = XOpenDisplay(NULL)))
994 die("Can't open display\n");
995 xw.scr = XDefaultScreen(xw.dpy);
996 xw.vis = XDefaultVisual(xw.dpy, xw.scr);
1000 die("Could not init fontconfig.\n");
1002 usedfont = (opt_font == NULL)? font : opt_font;
1003 xloadfonts(usedfont, 0);
1006 xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
1009 /* adjust fixed window geometry */
1010 win.w = 2 * borderpx + term.col * win.cw;
1011 win.h = 2 * borderpx + term.row * win.ch;
1012 if (xw.gm & XNegative)
1013 xw.l += DisplayWidth(xw.dpy, xw.scr) - win.w - 2;
1014 if (xw.gm & YNegative)
1015 xw.t += DisplayHeight(xw.dpy, xw.scr) - win.h - 2;
1018 xw.attrs.background_pixel = dc.col[defaultbg].pixel;
1019 xw.attrs.border_pixel = dc.col[defaultbg].pixel;
1020 xw.attrs.bit_gravity = NorthWestGravity;
1021 xw.attrs.event_mask = FocusChangeMask | KeyPressMask
1022 | ExposureMask | VisibilityChangeMask | StructureNotifyMask
1023 | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
1024 xw.attrs.colormap = xw.cmap;
1026 if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
1027 parent = XRootWindow(xw.dpy, xw.scr);
1028 xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
1029 win.w, win.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
1030 xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
1031 | CWEventMask | CWColormap, &xw.attrs);
1033 memset(&gcvalues, 0, sizeof(gcvalues));
1034 gcvalues.graphics_exposures = False;
1035 dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
1037 xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
1038 DefaultDepth(xw.dpy, xw.scr));
1039 XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
1040 XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
1042 /* font spec buffer */
1043 xw.specbuf = xmalloc(term.col * sizeof(GlyphFontSpec));
1045 /* Xft rendering context */
1046 xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
1049 if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
1050 XSetLocaleModifiers("@im=local");
1051 if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
1052 XSetLocaleModifiers("@im=");
1053 if ((xw.xim = XOpenIM(xw.dpy,
1054 NULL, NULL, NULL)) == NULL) {
1055 die("XOpenIM failed. Could not open input"
1060 xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
1061 | XIMStatusNothing, XNClientWindow, xw.win,
1062 XNFocusWindow, xw.win, NULL);
1064 die("XCreateIC failed. Could not obtain input method.\n");
1066 /* white cursor, black outline */
1067 cursor = XCreateFontCursor(xw.dpy, mouseshape);
1068 XDefineCursor(xw.dpy, xw.win, cursor);
1070 if (XParseColor(xw.dpy, xw.cmap, colorname[mousefg], &xmousefg) == 0) {
1071 xmousefg.red = 0xffff;
1072 xmousefg.green = 0xffff;
1073 xmousefg.blue = 0xffff;
1076 if (XParseColor(xw.dpy, xw.cmap, colorname[mousebg], &xmousebg) == 0) {
1077 xmousebg.red = 0x0000;
1078 xmousebg.green = 0x0000;
1079 xmousebg.blue = 0x0000;
1082 XRecolorCursor(xw.dpy, cursor, &xmousefg, &xmousebg);
1084 xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
1085 xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
1086 xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False);
1087 XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
1089 xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False);
1090 XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL, 32,
1091 PropModeReplace, (uchar *)&thispid, 1);
1094 XMapWindow(xw.dpy, xw.win);
1096 XSync(xw.dpy, False);
1098 clock_gettime(CLOCK_MONOTONIC, &xsel.tclick1);
1099 clock_gettime(CLOCK_MONOTONIC, &xsel.tclick2);
1100 xsel.primary = NULL;
1101 xsel.clipboard = NULL;
1102 xsel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
1103 if (xsel.xtarget == None)
1104 xsel.xtarget = XA_STRING;
1108 xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y)
1110 float winx = borderpx + x * win.cw, winy = borderpx + y * win.ch, xp, yp;
1111 ushort mode, prevmode = USHRT_MAX;
1112 Font *font = &dc.font;
1113 int frcflags = FRC_NORMAL;
1114 float runewidth = win.cw;
1118 FcPattern *fcpattern, *fontpattern;
1119 FcFontSet *fcsets[] = { NULL };
1120 FcCharSet *fccharset;
1121 int i, f, numspecs = 0;
1123 for (i = 0, xp = winx, yp = winy + font->ascent; i < len; ++i) {
1124 /* Fetch rune and mode for current glyph. */
1126 mode = glyphs[i].mode;
1128 /* Skip dummy wide-character spacing. */
1129 if (mode == ATTR_WDUMMY)
1132 /* Determine font for glyph if different from previous glyph. */
1133 if (prevmode != mode) {
1136 frcflags = FRC_NORMAL;
1137 runewidth = win.cw * ((mode & ATTR_WIDE) ? 2.0f : 1.0f);
1138 if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) {
1140 frcflags = FRC_ITALICBOLD;
1141 } else if (mode & ATTR_ITALIC) {
1143 frcflags = FRC_ITALIC;
1144 } else if (mode & ATTR_BOLD) {
1146 frcflags = FRC_BOLD;
1148 yp = winy + font->ascent;
1151 /* Lookup character index with default font. */
1152 glyphidx = XftCharIndex(xw.dpy, font->match, rune);
1154 specs[numspecs].font = font->match;
1155 specs[numspecs].glyph = glyphidx;
1156 specs[numspecs].x = (short)xp;
1157 specs[numspecs].y = (short)yp;
1163 /* Fallback on font cache, search the font cache for match. */
1164 for (f = 0; f < frclen; f++) {
1165 glyphidx = XftCharIndex(xw.dpy, frc[f].font, rune);
1166 /* Everything correct. */
1167 if (glyphidx && frc[f].flags == frcflags)
1169 /* We got a default font for a not found glyph. */
1170 if (!glyphidx && frc[f].flags == frcflags
1171 && frc[f].unicodep == rune) {
1176 /* Nothing was found. Use fontconfig to find matching font. */
1179 font->set = FcFontSort(0, font->pattern,
1181 fcsets[0] = font->set;
1184 * Nothing was found in the cache. Now use
1185 * some dozen of Fontconfig calls to get the
1186 * font for one single character.
1188 * Xft and fontconfig are design failures.
1190 fcpattern = FcPatternDuplicate(font->pattern);
1191 fccharset = FcCharSetCreate();
1193 FcCharSetAddChar(fccharset, rune);
1194 FcPatternAddCharSet(fcpattern, FC_CHARSET,
1196 FcPatternAddBool(fcpattern, FC_SCALABLE, 1);
1198 FcConfigSubstitute(0, fcpattern,
1200 FcDefaultSubstitute(fcpattern);
1202 fontpattern = FcFontSetMatch(0, fcsets, 1,
1206 * Overwrite or create the new cache entry.
1208 if (frclen >= LEN(frc)) {
1209 frclen = LEN(frc) - 1;
1210 XftFontClose(xw.dpy, frc[frclen].font);
1211 frc[frclen].unicodep = 0;
1214 frc[frclen].font = XftFontOpenPattern(xw.dpy,
1216 if (!frc[frclen].font)
1217 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1219 frc[frclen].flags = frcflags;
1220 frc[frclen].unicodep = rune;
1222 glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune);
1227 FcPatternDestroy(fcpattern);
1228 FcCharSetDestroy(fccharset);
1231 specs[numspecs].font = frc[f].font;
1232 specs[numspecs].glyph = glyphidx;
1233 specs[numspecs].x = (short)xp;
1234 specs[numspecs].y = (short)yp;
1243 xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, int y)
1245 int charlen = len * ((base.mode & ATTR_WIDE) ? 2 : 1);
1246 int winx = borderpx + x * win.cw, winy = borderpx + y * win.ch,
1247 width = charlen * win.cw;
1248 Color *fg, *bg, *temp, revfg, revbg, truefg, truebg;
1249 XRenderColor colfg, colbg;
1252 /* Fallback on color display for attributes not supported by the font */
1253 if (base.mode & ATTR_ITALIC && base.mode & ATTR_BOLD) {
1254 if (dc.ibfont.badslant || dc.ibfont.badweight)
1255 base.fg = defaultattr;
1256 } else if ((base.mode & ATTR_ITALIC && dc.ifont.badslant) ||
1257 (base.mode & ATTR_BOLD && dc.bfont.badweight)) {
1258 base.fg = defaultattr;
1261 if (IS_TRUECOL(base.fg)) {
1262 colfg.alpha = 0xffff;
1263 colfg.red = TRUERED(base.fg);
1264 colfg.green = TRUEGREEN(base.fg);
1265 colfg.blue = TRUEBLUE(base.fg);
1266 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &truefg);
1269 fg = &dc.col[base.fg];
1272 if (IS_TRUECOL(base.bg)) {
1273 colbg.alpha = 0xffff;
1274 colbg.green = TRUEGREEN(base.bg);
1275 colbg.red = TRUERED(base.bg);
1276 colbg.blue = TRUEBLUE(base.bg);
1277 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &truebg);
1280 bg = &dc.col[base.bg];
1283 /* Change basic system colors [0-7] to bright system colors [8-15] */
1284 if ((base.mode & ATTR_BOLD_FAINT) == ATTR_BOLD && BETWEEN(base.fg, 0, 7))
1285 fg = &dc.col[base.fg + 8];
1287 if (IS_SET(MODE_REVERSE)) {
1288 if (fg == &dc.col[defaultfg]) {
1289 fg = &dc.col[defaultbg];
1291 colfg.red = ~fg->color.red;
1292 colfg.green = ~fg->color.green;
1293 colfg.blue = ~fg->color.blue;
1294 colfg.alpha = fg->color.alpha;
1295 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg,
1300 if (bg == &dc.col[defaultbg]) {
1301 bg = &dc.col[defaultfg];
1303 colbg.red = ~bg->color.red;
1304 colbg.green = ~bg->color.green;
1305 colbg.blue = ~bg->color.blue;
1306 colbg.alpha = bg->color.alpha;
1307 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg,
1313 if ((base.mode & ATTR_BOLD_FAINT) == ATTR_FAINT) {
1314 colfg.red = fg->color.red / 2;
1315 colfg.green = fg->color.green / 2;
1316 colfg.blue = fg->color.blue / 2;
1317 colfg.alpha = fg->color.alpha;
1318 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
1323 if (base.mode & ATTR_REVERSE) {
1329 if (base.mode & ATTR_BLINK && term.mode & MODE_BLINK)
1332 if (base.mode & ATTR_INVISIBLE)
1335 /* Intelligent cleaning up of the borders. */
1337 xclear(0, (y == 0)? 0 : winy, borderpx,
1338 winy + win.ch + ((y >= term.row-1)? win.h : 0));
1340 if (x + charlen >= term.col) {
1341 xclear(winx + width, (y == 0)? 0 : winy, win.w,
1342 ((y >= term.row-1)? win.h : (winy + win.ch)));
1345 xclear(winx, 0, winx + width, borderpx);
1346 if (y == term.row-1)
1347 xclear(winx, winy + win.ch, winx + width, win.h);
1349 /* Clean up the region we want to draw to. */
1350 XftDrawRect(xw.draw, bg, winx, winy, width, win.ch);
1352 /* Set the clip region because Xft is sometimes dirty. */
1357 XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1);
1359 /* Render the glyphs. */
1360 XftDrawGlyphFontSpec(xw.draw, fg, specs, len);
1362 /* Render underline and strikethrough. */
1363 if (base.mode & ATTR_UNDERLINE) {
1364 XftDrawRect(xw.draw, fg, winx, winy + dc.font.ascent + 1,
1368 if (base.mode & ATTR_STRUCK) {
1369 XftDrawRect(xw.draw, fg, winx, winy + 2 * dc.font.ascent / 3,
1373 /* Reset clip to none. */
1374 XftDrawSetClip(xw.draw, 0);
1378 xdrawglyph(Glyph g, int x, int y)
1381 XftGlyphFontSpec spec;
1383 numspecs = xmakeglyphfontspecs(&spec, &g, 1, x, y);
1384 xdrawglyphfontspecs(&spec, g, numspecs, x, y);
1390 static int oldx = 0, oldy = 0;
1392 Glyph g = {' ', ATTR_NULL, defaultbg, defaultcs}, og;
1395 LIMIT(oldx, 0, term.col-1);
1396 LIMIT(oldy, 0, term.row-1);
1400 /* adjust position if in dummy */
1401 if (term.line[oldy][oldx].mode & ATTR_WDUMMY)
1403 if (term.line[term.c.y][curx].mode & ATTR_WDUMMY)
1406 /* remove the old cursor */
1407 og = term.line[oldy][oldx];
1408 if (selected(oldx, oldy))
1409 og.mode ^= ATTR_REVERSE;
1410 xdrawglyph(og, oldx, oldy);
1412 g.u = term.line[term.c.y][term.c.x].u;
1413 g.mode |= term.line[term.c.y][term.c.x].mode &
1414 (ATTR_BOLD | ATTR_ITALIC | ATTR_UNDERLINE | ATTR_STRUCK);
1417 * Select the right color for the right mode.
1419 if (IS_SET(MODE_REVERSE)) {
1420 g.mode |= ATTR_REVERSE;
1422 if (selected(term.c.x, term.c.y)) {
1423 drawcol = dc.col[defaultcs];
1426 drawcol = dc.col[defaultrcs];
1430 if (selected(term.c.x, term.c.y)) {
1431 drawcol = dc.col[defaultrcs];
1435 drawcol = dc.col[defaultcs];
1439 if (IS_SET(MODE_HIDE))
1442 /* draw the new one */
1443 if (win.state & WIN_FOCUSED) {
1444 switch (win.cursor) {
1445 case 7: /* st extension: snowman */
1446 utf8decode("☃", &g.u, UTF_SIZ);
1447 case 0: /* Blinking Block */
1448 case 1: /* Blinking Block (Default) */
1449 case 2: /* Steady Block */
1450 g.mode |= term.line[term.c.y][curx].mode & ATTR_WIDE;
1451 xdrawglyph(g, term.c.x, term.c.y);
1453 case 3: /* Blinking Underline */
1454 case 4: /* Steady Underline */
1455 XftDrawRect(xw.draw, &drawcol,
1456 borderpx + curx * win.cw,
1457 borderpx + (term.c.y + 1) * win.ch - \
1459 win.cw, cursorthickness);
1461 case 5: /* Blinking bar */
1462 case 6: /* Steady bar */
1463 XftDrawRect(xw.draw, &drawcol,
1464 borderpx + curx * win.cw,
1465 borderpx + term.c.y * win.ch,
1466 cursorthickness, win.ch);
1470 XftDrawRect(xw.draw, &drawcol,
1471 borderpx + curx * win.cw,
1472 borderpx + term.c.y * win.ch,
1474 XftDrawRect(xw.draw, &drawcol,
1475 borderpx + curx * win.cw,
1476 borderpx + term.c.y * win.ch,
1478 XftDrawRect(xw.draw, &drawcol,
1479 borderpx + (curx + 1) * win.cw - 1,
1480 borderpx + term.c.y * win.ch,
1482 XftDrawRect(xw.draw, &drawcol,
1483 borderpx + curx * win.cw,
1484 borderpx + (term.c.y + 1) * win.ch - 1,
1487 oldx = curx, oldy = term.c.y;
1493 char buf[sizeof(long) * 8 + 1];
1495 snprintf(buf, sizeof(buf), "%lu", xw.win);
1496 setenv("WINDOWID", buf, 1);
1505 Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
1507 XSetWMName(xw.dpy, xw.win, &prop);
1508 XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmname);
1515 drawregion(0, 0, term.col, term.row);
1516 XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, win.w,
1518 XSetForeground(xw.dpy, dc.gc,
1519 dc.col[IS_SET(MODE_REVERSE)?
1520 defaultfg : defaultbg].pixel);
1524 drawregion(int x1, int y1, int x2, int y2)
1526 int i, x, y, ox, numspecs;
1528 XftGlyphFontSpec *specs;
1530 if (!(win.state & WIN_VISIBLE))
1533 for (y = y1; y < y2; y++) {
1540 numspecs = xmakeglyphfontspecs(specs, &term.line[y][x1], x2 - x1, x1, y);
1543 for (x = x1; x < x2 && i < numspecs; x++) {
1544 new = term.line[y][x];
1545 if (new.mode == ATTR_WDUMMY)
1548 new.mode ^= ATTR_REVERSE;
1549 if (i > 0 && ATTRCMP(base, new)) {
1550 xdrawglyphfontspecs(specs, base, i, ox, y);
1562 xdrawglyphfontspecs(specs, base, i, ox, y);
1574 visibility(XEvent *ev)
1576 XVisibilityEvent *e = &ev->xvisibility;
1578 MODBIT(win.state, e->state != VisibilityFullyObscured, WIN_VISIBLE);
1584 win.state &= ~WIN_VISIBLE;
1588 xsetpointermotion(int set)
1590 MODBIT(xw.attrs.event_mask, set, PointerMotionMask);
1591 XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs);
1595 xsetcursor(int cursor)
1598 if (!BETWEEN(cursor, 0, 6))
1600 win.cursor = cursor;
1605 xseturgency(int add)
1607 XWMHints *h = XGetWMHints(xw.dpy, xw.win);
1609 MODBIT(h->flags, add, XUrgencyHint);
1610 XSetWMHints(xw.dpy, xw.win, h);
1617 if (!(win.state & WIN_FOCUSED))
1620 XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL);
1626 XFocusChangeEvent *e = &ev->xfocus;
1628 if (e->mode == NotifyGrab)
1631 if (ev->type == FocusIn) {
1632 XSetICFocus(xw.xic);
1633 win.state |= WIN_FOCUSED;
1635 if (IS_SET(MODE_FOCUS))
1636 ttywrite("\033[I", 3, 0);
1638 XUnsetICFocus(xw.xic);
1639 win.state &= ~WIN_FOCUSED;
1640 if (IS_SET(MODE_FOCUS))
1641 ttywrite("\033[O", 3, 0);
1646 match(uint mask, uint state)
1648 return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
1652 kmap(KeySym k, uint state)
1657 /* Check for mapped keys out of X11 function keys. */
1658 for (i = 0; i < LEN(mappedkeys); i++) {
1659 if (mappedkeys[i] == k)
1662 if (i == LEN(mappedkeys)) {
1663 if ((k & 0xFFFF) < 0xFD00)
1667 for (kp = key; kp < key + LEN(key); kp++) {
1671 if (!match(kp->mask, state))
1674 if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0)
1676 if (term.numlock && kp->appkey == 2)
1679 if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0)
1691 XKeyEvent *e = &ev->xkey;
1693 char buf[32], *customkey;
1699 if (IS_SET(MODE_KBDLOCK))
1702 len = XmbLookupString(xw.xic, e, buf, sizeof buf, &ksym, &status);
1704 for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
1705 if (ksym == bp->keysym && match(bp->mod, e->state)) {
1706 bp->func(&(bp->arg));
1711 /* 2. custom keys from config.h */
1712 if ((customkey = kmap(ksym, e->state))) {
1713 ttywrite(customkey, strlen(customkey), 1);
1717 /* 3. composed string from input method */
1720 if (len == 1 && e->state & Mod1Mask) {
1721 if (IS_SET(MODE_8BIT)) {
1724 len = utf8encode(c, buf);
1732 ttywrite(buf, len, 1);
1741 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1743 if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
1744 if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
1745 win.state |= WIN_FOCUSED;
1747 } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
1748 win.state &= ~WIN_FOCUSED;
1750 } else if (e->xclient.data.l[0] == xw.wmdeletewin) {
1751 /* Send SIGHUP to shell */
1760 if (e->xconfigure.width == win.w && e->xconfigure.height == win.h)
1763 cresize(e->xconfigure.width, e->xconfigure.height);
1770 int w = win.w, h = win.h;
1772 int xfd = XConnectionNumber(xw.dpy), xev, blinkset = 0, dodraw = 0;
1773 struct timespec drawtimeout, *tv = NULL, now, last, lastblink;
1776 /* Waiting for window mapping */
1778 XNextEvent(xw.dpy, &ev);
1780 * This XFilterEvent call is required because of XOpenIM. It
1781 * does filter out the key event and some client message for
1782 * the input method too.
1784 if (XFilterEvent(&ev, None))
1786 if (ev.type == ConfigureNotify) {
1787 w = ev.xconfigure.width;
1788 h = ev.xconfigure.height;
1790 } while (ev.type != MapNotify);
1792 ttynew(opt_line, opt_io, opt_cmd);
1795 clock_gettime(CLOCK_MONOTONIC, &last);
1798 for (xev = actionfps;;) {
1800 FD_SET(cmdfd, &rfd);
1803 if (pselect(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv, NULL) < 0) {
1806 die("select failed: %s\n", strerror(errno));
1808 if (FD_ISSET(cmdfd, &rfd)) {
1811 blinkset = tattrset(ATTR_BLINK);
1813 MODBIT(term.mode, 0, MODE_BLINK);
1817 if (FD_ISSET(xfd, &rfd))
1820 clock_gettime(CLOCK_MONOTONIC, &now);
1821 drawtimeout.tv_sec = 0;
1822 drawtimeout.tv_nsec = (1000 * 1E6)/ xfps;
1826 if (blinktimeout && TIMEDIFF(now, lastblink) > blinktimeout) {
1827 tsetdirtattr(ATTR_BLINK);
1828 term.mode ^= MODE_BLINK;
1832 deltatime = TIMEDIFF(now, last);
1833 if (deltatime > 1000 / (xev ? xfps : actionfps)) {
1839 while (XPending(xw.dpy)) {
1840 XNextEvent(xw.dpy, &ev);
1841 if (XFilterEvent(&ev, None))
1843 if (handler[ev.type])
1844 (handler[ev.type])(&ev);
1850 if (xev && !FD_ISSET(xfd, &rfd))
1852 if (!FD_ISSET(cmdfd, &rfd) && !FD_ISSET(xfd, &rfd)) {
1854 if (TIMEDIFF(now, lastblink) \
1856 drawtimeout.tv_nsec = 1000;
1858 drawtimeout.tv_nsec = (1E6 * \
1863 drawtimeout.tv_sec = \
1864 drawtimeout.tv_nsec / 1E9;
1865 drawtimeout.tv_nsec %= (long)1E9;
1877 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
1878 " [-n name] [-o file]\n"
1879 " [-T title] [-t title] [-w windowid]"
1880 " [[-e] command [args ...]]\n"
1881 " %s [-aiv] [-c class] [-f font] [-g geometry]"
1882 " [-n name] [-o file]\n"
1883 " [-T title] [-t title] [-w windowid] -l line"
1884 " [stty_args ...]\n", argv0, argv0);
1888 main(int argc, char *argv[])
1892 win.cursor = cursorshape;
1899 opt_class = EARGF(usage());
1906 opt_font = EARGF(usage());
1909 xw.gm = XParseGeometry(EARGF(usage()),
1910 &xw.l, &xw.t, &cols, &rows);
1916 opt_io = EARGF(usage());
1919 opt_line = EARGF(usage());
1922 opt_name = EARGF(usage());
1926 opt_title = EARGF(usage());
1929 opt_embed = EARGF(usage());
1932 die("%s " VERSION " (c) 2010-2016 st engineers\n", argv0);
1940 /* eat all remaining arguments */
1942 if (!opt_title && !opt_line)
1943 opt_title = basename(xstrdup(argv[0]));
1945 setlocale(LC_CTYPE, "");
1946 XSetLocaleModifiers("");
1947 tnew(MAX(cols, 1), MAX(rows, 1));