x.c (49168B)
1 /* See LICENSE for license details. */ 2 #include <errno.h> 3 #include <math.h> 4 #include <limits.h> 5 #include <locale.h> 6 #include <signal.h> 7 #include <stdbool.h> 8 #include <sys/select.h> 9 #include <time.h> 10 #include <unistd.h> 11 #include <libgen.h> 12 #include <X11/Xatom.h> 13 #include <X11/Xlib.h> 14 #include <X11/cursorfont.h> 15 #include <X11/keysym.h> 16 #include <X11/Xft/Xft.h> 17 #include <X11/XKBlib.h> 18 #include <X11/Xresource.h> 19 20 char *argv0; 21 #include "arg.h" 22 #include "st.h" 23 #include "win.h" 24 25 /* types used in config.def.h */ 26 typedef struct { 27 uint mod; 28 KeySym keysym; 29 void (*func)(const Arg *); 30 const Arg arg; 31 } Shortcut; 32 33 typedef struct { 34 uint mod; 35 uint button; 36 void (*func)(const Arg *); 37 const Arg arg; 38 uint release; 39 int altscrn; /* 0: don't care, -1: not alt screen, 1: alt screen */ 40 } MouseShortcut; 41 42 typedef struct { 43 KeySym k; 44 uint mask; 45 char *s; 46 /* three-valued logic variables: 0 indifferent, 1 on, -1 off */ 47 signed char appkey; /* application keypad */ 48 signed char appcursor; /* application cursor */ 49 } Key; 50 51 /* Xresources preferences */ 52 enum resource_type { 53 STRING = 0, 54 INTEGER = 1, 55 FLOAT = 2 56 }; 57 58 typedef struct { 59 char *name; 60 enum resource_type type; 61 void *dst; 62 } ResourcePref; 63 64 /* X modifiers */ 65 #define XK_ANY_MOD UINT_MAX 66 #define XK_NO_MOD 0 67 #define XK_SWITCH_MOD (1<<13) 68 69 /* function definitions used in config.def.h */ 70 static void clipcopy(const Arg *); 71 static void clippaste(const Arg *); 72 static void numlock(const Arg *); 73 static void selpaste(const Arg *); 74 static void zoom(const Arg *); 75 static void zoomabs(const Arg *); 76 static void zoomreset(const Arg *); 77 static void ttysend(const Arg *); 78 79 /* config.def.h for applying patches and the configuration. */ 80 #include "config.def.h" 81 82 /* XEMBED messages */ 83 #define XEMBED_FOCUS_IN 4 84 #define XEMBED_FOCUS_OUT 5 85 86 /* macros */ 87 #define IS_SET(flag) ((win.mode & (flag)) != 0) 88 #define TRUERED(x) (((x) & 0xff0000) >> 8) 89 #define TRUEGREEN(x) (((x) & 0xff00)) 90 #define TRUEBLUE(x) (((x) & 0xff) << 8) 91 92 typedef XftDraw *Draw; 93 typedef XftColor Color; 94 typedef XftGlyphFontSpec GlyphFontSpec; 95 96 /* Purely graphic info */ 97 typedef struct { 98 int tw, th; /* tty width and height */ 99 int w, h; /* window width and height */ 100 int ch; /* char height */ 101 int cw; /* char width */ 102 int mode; /* window state/mode flags */ 103 int cursor; /* cursor style */ 104 } TermWindow; 105 106 typedef struct { 107 Display *dpy; 108 Colormap cmap; 109 Window win; 110 Drawable buf; 111 GlyphFontSpec *specbuf; /* font spec buffer used for rendering */ 112 Atom xembed, wmdeletewin, netwmname, netwmpid; 113 struct { 114 XIM xim; 115 XIC xic; 116 XPoint spot; 117 XVaNestedList spotlist; 118 } ime; 119 Draw draw; 120 Visual *vis; 121 XSetWindowAttributes attrs; 122 int scr; 123 int isfixed; /* is fixed geometry? */ 124 int depth; /* bit depth */ 125 int l, t; /* left and top offset */ 126 int gm; /* geometry mask */ 127 } XWindow; 128 129 typedef struct { 130 Atom xtarget; 131 char *primary, *clipboard; 132 struct timespec tclick1; 133 struct timespec tclick2; 134 } XSelection; 135 136 /* Font structure */ 137 #define Font Font_ 138 typedef struct { 139 int height; 140 int width; 141 int ascent; 142 int descent; 143 int badslant; 144 int badweight; 145 short lbearing; 146 short rbearing; 147 XftFont *match; 148 FcFontSet *set; 149 FcPattern *pattern; 150 } Font; 151 152 /* Drawing Context */ 153 typedef struct { 154 Color *col; 155 size_t collen; 156 Font font, bfont, ifont, ibfont; 157 GC gc; 158 } DC; 159 160 static inline ushort sixd_to_16bit(int); 161 static int xmakeglyphfontspecs(XftGlyphFontSpec *, const Glyph *, int, int, int); 162 static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int); 163 static void xdrawglyph(Glyph, int, int); 164 static void xclear(int, int, int, int); 165 static int xgeommasktogravity(int); 166 static int ximopen(Display *); 167 static void ximinstantiate(Display *, XPointer, XPointer); 168 static void ximdestroy(XIM, XPointer, XPointer); 169 static int xicdestroy(XIC, XPointer, XPointer); 170 static void xinit(int, int); 171 static void cresize(int, int); 172 static void xresize(int, int); 173 static void xhints(void); 174 static int xloadcolor(int, const char *, Color *); 175 static int xloadfont(Font *, FcPattern *); 176 static void xloadfonts(char *, double); 177 static void xunloadfont(Font *); 178 static void xunloadfonts(void); 179 static void xsetenv(void); 180 static void xseturgency(int); 181 static int evcol(XEvent *); 182 static int evrow(XEvent *); 183 184 static void expose(XEvent *); 185 static void visibility(XEvent *); 186 static void unmap(XEvent *); 187 static void kpress(XEvent *); 188 static void cmessage(XEvent *); 189 static void resize(XEvent *); 190 static void focus(XEvent *); 191 static uint buttonmask(uint); 192 static int mouseaction(XEvent *, uint); 193 static void brelease(XEvent *); 194 static void bpress(XEvent *); 195 static void bmotion(XEvent *); 196 static void propnotify(XEvent *); 197 static void selnotify(XEvent *); 198 static void selclear_(XEvent *); 199 static void selrequest(XEvent *); 200 static void setsel(char *, Time); 201 static void mousesel(XEvent *, int); 202 static void mousereport(XEvent *); 203 static char *kmap(KeySym, uint); 204 static int match(uint, uint); 205 206 static void run(void); 207 static void usage(void); 208 209 static void (*handler[LASTEvent])(XEvent *) = { 210 [KeyPress] = kpress, 211 [ClientMessage] = cmessage, 212 [ConfigureNotify] = resize, 213 [VisibilityNotify] = visibility, 214 [UnmapNotify] = unmap, 215 [Expose] = expose, 216 [FocusIn] = focus, 217 [FocusOut] = focus, 218 [MotionNotify] = bmotion, 219 [ButtonPress] = bpress, 220 [ButtonRelease] = brelease, 221 /* 222 * Uncomment if you want the selection to disappear when you select something 223 * different in another window. 224 */ 225 /* [SelectionClear] = selclear_, */ 226 [SelectionNotify] = selnotify, 227 /* 228 * PropertyNotify is only turned on when there is some INCR transfer happening 229 * for the selection retrieval. 230 */ 231 [PropertyNotify] = propnotify, 232 [SelectionRequest] = selrequest, 233 }; 234 235 /* Globals */ 236 static DC dc; 237 static XWindow xw; 238 static XSelection xsel; 239 static TermWindow win; 240 241 /* Font Ring Cache */ 242 enum { 243 FRC_NORMAL, 244 FRC_ITALIC, 245 FRC_BOLD, 246 FRC_ITALICBOLD 247 }; 248 249 typedef struct { 250 XftFont *font; 251 int flags; 252 Rune unicodep; 253 } Fontcache; 254 255 /* Fontcache is an array now. A new font will be appended to the array. */ 256 static Fontcache *frc = NULL; 257 static int frclen = 0; 258 static int frccap = 0; 259 static char *usedfont = NULL; 260 static double usedfontsize = 0; 261 static double defaultfontsize = 0; 262 263 static char *opt_alpha = NULL; 264 static char *opt_class = NULL; 265 static char **opt_cmd = NULL; 266 static char *opt_embed = NULL; 267 static char *opt_font = NULL; 268 static char *opt_io = NULL; 269 static char *opt_line = NULL; 270 static char *opt_name = NULL; 271 static char *opt_title = NULL; 272 static bool focused = true; 273 274 static int oldbutton = 3; /* button event on startup: 3 = release */ 275 276 void 277 clipcopy(const Arg *dummy) 278 { 279 Atom clipboard; 280 281 free(xsel.clipboard); 282 xsel.clipboard = NULL; 283 284 if (xsel.primary != NULL) { 285 xsel.clipboard = xstrdup(xsel.primary); 286 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0); 287 XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime); 288 } 289 } 290 291 void 292 clippaste(const Arg *dummy) 293 { 294 Atom clipboard; 295 296 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0); 297 XConvertSelection(xw.dpy, clipboard, xsel.xtarget, clipboard, 298 xw.win, CurrentTime); 299 } 300 301 void 302 selpaste(const Arg *dummy) 303 { 304 XConvertSelection(xw.dpy, XA_PRIMARY, xsel.xtarget, XA_PRIMARY, 305 xw.win, CurrentTime); 306 } 307 308 void 309 numlock(const Arg *dummy) 310 { 311 win.mode ^= MODE_NUMLOCK; 312 } 313 314 void 315 zoom(const Arg *arg) 316 { 317 Arg larg; 318 319 larg.f = usedfontsize + arg->f; 320 zoomabs(&larg); 321 } 322 323 void 324 zoomabs(const Arg *arg) 325 { 326 xunloadfonts(); 327 xloadfonts(usedfont, arg->f); 328 cresize(0, 0); 329 redraw(); 330 xhints(); 331 } 332 333 void 334 zoomreset(const Arg *arg) 335 { 336 Arg larg; 337 338 if (defaultfontsize > 0) { 339 larg.f = defaultfontsize; 340 zoomabs(&larg); 341 } 342 } 343 344 void 345 ttysend(const Arg *arg) 346 { 347 ttywrite(arg->s, strlen(arg->s), 1); 348 } 349 350 int 351 evcol(XEvent *e) 352 { 353 int x = e->xbutton.x - borderpx; 354 LIMIT(x, 0, win.tw - 1); 355 return x / win.cw; 356 } 357 358 int 359 evrow(XEvent *e) 360 { 361 int y = e->xbutton.y - borderpx; 362 LIMIT(y, 0, win.th - 1); 363 return y / win.ch; 364 } 365 366 void 367 mousesel(XEvent *e, int done) 368 { 369 int type, seltype = SEL_REGULAR; 370 uint state = e->xbutton.state & ~(Button1Mask | forcemousemod); 371 372 for (type = 1; type < LEN(selmasks); ++type) { 373 if (match(selmasks[type], state)) { 374 seltype = type; 375 break; 376 } 377 } 378 selextend(evcol(e), evrow(e), seltype, done); 379 if (done) 380 setsel(getsel(), e->xbutton.time); 381 } 382 383 void 384 mousereport(XEvent *e) 385 { 386 int len, x = evcol(e), y = evrow(e), 387 button = e->xbutton.button, state = e->xbutton.state; 388 char buf[40]; 389 static int ox, oy; 390 391 /* from urxvt */ 392 if (e->xbutton.type == MotionNotify) { 393 if (x == ox && y == oy) 394 return; 395 if (!IS_SET(MODE_MOUSEMOTION) && !IS_SET(MODE_MOUSEMANY)) 396 return; 397 /* MOUSE_MOTION: no reporting if no button is pressed */ 398 if (IS_SET(MODE_MOUSEMOTION) && oldbutton == 3) 399 return; 400 401 button = oldbutton + 32; 402 ox = x; 403 oy = y; 404 } else { 405 if (!IS_SET(MODE_MOUSESGR) && e->xbutton.type == ButtonRelease) { 406 button = 3; 407 } else { 408 button -= Button1; 409 if (button >= 3) 410 button += 64 - 3; 411 } 412 if (e->xbutton.type == ButtonPress) { 413 oldbutton = button; 414 ox = x; 415 oy = y; 416 } else if (e->xbutton.type == ButtonRelease) { 417 oldbutton = 3; 418 /* MODE_MOUSEX10: no button release reporting */ 419 if (IS_SET(MODE_MOUSEX10)) 420 return; 421 if (button == 64 || button == 65) 422 return; 423 } 424 } 425 426 if (!IS_SET(MODE_MOUSEX10)) { 427 button += ((state & ShiftMask ) ? 4 : 0) 428 + ((state & Mod4Mask ) ? 8 : 0) 429 + ((state & ControlMask) ? 16 : 0); 430 } 431 432 if (IS_SET(MODE_MOUSESGR)) { 433 len = snprintf(buf, sizeof(buf), "033[<%d;%d;%d%c", 434 button, x+1, y+1, 435 e->xbutton.type == ButtonRelease ? 'm' : 'M'); 436 } else if (x < 223 && y < 223) { 437 len = snprintf(buf, sizeof(buf), "033[M%c%c%c", 438 32+button, 32+x+1, 32+y+1); 439 } else { 440 return; 441 } 442 443 ttywrite(buf, len, 0); 444 } 445 446 uint 447 buttonmask(uint button) 448 { 449 return button == Button1 ? Button1Mask 450 : button == Button2 ? Button2Mask 451 : button == Button3 ? Button3Mask 452 : button == Button4 ? Button4Mask 453 : button == Button5 ? Button5Mask 454 : 0; 455 } 456 457 int 458 mouseaction(XEvent *e, uint release) 459 { 460 MouseShortcut *ms; 461 462 /* ignore Button<N>mask for Button<N> - it's set on release */ 463 uint state = e->xbutton.state & ~buttonmask(e->xbutton.button); 464 465 for (ms = mshortcuts; ms < mshortcuts + LEN(mshortcuts); ms++) { 466 if (ms->release == release && 467 ms->button == e->xbutton.button && 468 (!ms->altscrn || (ms->altscrn == (tisaltscr() ? 1 : -1))) && 469 (match(ms->mod, state) || /* exact or forced */ 470 match(ms->mod, state & ~forcemousemod))) { 471 ms->func(&(ms->arg)); 472 return 1; 473 } 474 } 475 476 return 0; 477 } 478 479 void 480 bpress(XEvent *e) 481 { 482 struct timespec now; 483 int snap; 484 485 if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) { 486 mousereport(e); 487 return; 488 } 489 490 if (mouseaction(e, 0)) 491 return; 492 493 if (e->xbutton.button == Button1) { 494 /* 495 * If the user clicks below predefined timeouts specific 496 * snapping behaviour is exposed. 497 */ 498 clock_gettime(CLOCK_MONOTONIC, &now); 499 if (TIMEDIFF(now, xsel.tclick2) <= tripleclicktimeout) { 500 snap = SNAP_LINE; 501 } else if (TIMEDIFF(now, xsel.tclick1) <= doubleclicktimeout) { 502 snap = SNAP_WORD; 503 } else { 504 snap = 0; 505 } 506 xsel.tclick2 = xsel.tclick1; 507 xsel.tclick1 = now; 508 509 selstart(evcol(e), evrow(e), snap); 510 } 511 } 512 513 void 514 propnotify(XEvent *e) 515 { 516 XPropertyEvent *xpev; 517 Atom clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0); 518 519 xpev = &e->xproperty; 520 if (xpev->state == PropertyNewValue && 521 (xpev->atom == XA_PRIMARY || 522 xpev->atom == clipboard)) { 523 selnotify(e); 524 } 525 } 526 527 void 528 selnotify(XEvent *e) 529 { 530 ulong nitems, ofs, rem; 531 int format; 532 uchar *data, *last, *repl; 533 Atom type, incratom, property = None; 534 535 incratom = XInternAtom(xw.dpy, "INCR", 0); 536 537 ofs = 0; 538 if (e->type == SelectionNotify) 539 property = e->xselection.property; 540 else if (e->type == PropertyNotify) 541 property = e->xproperty.atom; 542 543 if (property == None) 544 return; 545 546 do { 547 if (XGetWindowProperty(xw.dpy, xw.win, property, ofs, 548 BUFSIZ/4, False, AnyPropertyType, 549 &type, &format, &nitems, &rem, 550 &data)) { 551 fprintf(stderr, "Clipboard allocation failedn"); 552 return; 553 } 554 555 if (e->type == PropertyNotify && nitems == 0 && rem == 0) { 556 /* 557 * If there is some PropertyNotify with no data, then 558 * this is the signal of the selection owner that all 559 * data has been transferred. We won't need to receive 560 * PropertyNotify events anymore. 561 */ 562 MODBIT(xw.attrs.event_mask, 0, PropertyChangeMask); 563 XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, 564 &xw.attrs); 565 } 566 567 if (type == incratom) { 568 /* 569 * Activate the PropertyNotify events so we receive 570 * when the selection owner does send us the next 571 * chunk of data. 572 */ 573 MODBIT(xw.attrs.event_mask, 1, PropertyChangeMask); 574 XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, 575 &xw.attrs); 576 577 /* 578 * Deleting the property is the transfer start signal. 579 */ 580 XDeleteProperty(xw.dpy, xw.win, (int)property); 581 continue; 582 } 583 584 /* 585 * As seen in getsel: 586 * Line endings are inconsistent in the terminal and GUI world 587 * copy and pasting. When receiving some selection data, 588 * replace all 'n' with 'r'. 589 * FIXME: Fix the computer world. 590 */ 591 repl = data; 592 last = data + nitems * format / 8; 593 while ((repl = memchr(repl, 'n', last - repl))) { 594 *repl++ = 'r'; 595 } 596 597 if (IS_SET(MODE_BRCKTPASTE) && ofs == 0) 598 ttywrite("033[200~", 6, 0); 599 ttywrite((char *)data, nitems * format / 8, 1); 600 if (IS_SET(MODE_BRCKTPASTE) && rem == 0) 601 ttywrite("033[201~", 6, 0); 602 XFree(data); 603 /* number of 32-bit chunks returned */ 604 ofs += nitems * format / 32; 605 } while (rem > 0); 606 607 /* 608 * Deleting the property again tells the selection owner to send the 609 * next data chunk in the property. 610 */ 611 XDeleteProperty(xw.dpy, xw.win, (int)property); 612 } 613 614 void 615 xclipcopy(void) 616 { 617 clipcopy(NULL); 618 } 619 620 void 621 selclear_(XEvent *e) 622 { 623 selclear(); 624 } 625 626 void 627 selrequest(XEvent *e) 628 { 629 XSelectionRequestEvent *xsre; 630 XSelectionEvent xev; 631 Atom xa_targets, string, clipboard; 632 char *seltext; 633 634 xsre = (XSelectionRequestEvent *) e; 635 xev.type = SelectionNotify; 636 xev.requestor = xsre->requestor; 637 xev.selection = xsre->selection; 638 xev.target = xsre->target; 639 xev.time = xsre->time; 640 if (xsre->property == None) 641 xsre->property = xsre->target; 642 643 /* reject */ 644 xev.property = None; 645 646 xa_targets = XInternAtom(xw.dpy, "TARGETS", 0); 647 if (xsre->target == xa_targets) { 648 /* respond with the supported type */ 649 string = xsel.xtarget; 650 XChangeProperty(xsre->display, xsre->requestor, xsre->property, 651 XA_ATOM, 32, PropModeReplace, 652 (uchar *) &string, 1); 653 xev.property = xsre->property; 654 } else if (xsre->target == xsel.xtarget || xsre->target == XA_STRING) { 655 /* 656 * xith XA_STRING non ascii characters may be incorrect in the 657 * requestor. It is not our problem, use utf8. 658 */ 659 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0); 660 if (xsre->selection == XA_PRIMARY) { 661 seltext = xsel.primary; 662 } else if (xsre->selection == clipboard) { 663 seltext = xsel.clipboard; 664 } else { 665 fprintf(stderr, 666 "Unhandled clipboard selection 0x%lxn", 667 xsre->selection); 668 return; 669 } 670 if (seltext != NULL) { 671 XChangeProperty(xsre->display, xsre->requestor, 672 xsre->property, xsre->target, 673 8, PropModeReplace, 674 (uchar *)seltext, strlen(seltext)); 675 xev.property = xsre->property; 676 } 677 } 678 679 /* all done, send a notification to the listener */ 680 if (!XSendEvent(xsre->display, xsre->requestor, 1, 0, (XEvent *) &xev)) 681 fprintf(stderr, "Error sending SelectionNotify eventn"); 682 } 683 684 void 685 setsel(char *str, Time t) 686 { 687 if (!str) 688 return; 689 690 free(xsel.primary); 691 xsel.primary = str; 692 693 XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, t); 694 if (XGetSelectionOwner(xw.dpy, XA_PRIMARY) != xw.win) 695 selclear(); 696 } 697 698 void 699 xsetsel(char *str) 700 { 701 setsel(str, CurrentTime); 702 } 703 704 void 705 brelease(XEvent *e) 706 { 707 if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) { 708 mousereport(e); 709 return; 710 } 711 712 if (mouseaction(e, 1)) 713 return; 714 if (e->xbutton.button == Button1) 715 mousesel(e, 1); 716 } 717 718 void 719 bmotion(XEvent *e) 720 { 721 if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) { 722 mousereport(e); 723 return; 724 } 725 726 mousesel(e, 0); 727 } 728 729 void 730 cresize(int width, int height) 731 { 732 int col, row; 733 734 if (width != 0) 735 win.w = width; 736 if (height != 0) 737 win.h = height; 738 739 col = (win.w - 2 * borderpx) / win.cw; 740 row = (win.h - 2 * borderpx) / win.ch; 741 col = MAX(1, col); 742 row = MAX(1, row); 743 744 tresize(col, row); 745 xresize(col, row); 746 ttyresize(win.tw, win.th); 747 } 748 749 void 750 xresize(int col, int row) 751 { 752 win.tw = col * win.cw; 753 win.th = row * win.ch; 754 755 XFreePixmap(xw.dpy, xw.buf); 756 xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h, 757 xw.depth); 758 XftDrawChange(xw.draw, xw.buf); 759 xclear(0, 0, win.w, win.h); 760 761 /* resize to new width */ 762 xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec)); 763 } 764 765 ushort 766 sixd_to_16bit(int x) 767 { 768 return x == 0 ? 0 : 0x3737 + 0x2828 * x; 769 } 770 771 int 772 xloadcolor(int i, const char *name, Color *ncolor) 773 { 774 XRenderColor color = { .alpha = 0xffff }; 775 776 if (!name) { 777 if (BETWEEN(i, 16, 255)) { /* 256 color */ 778 if (i < 6*6*6+16) { /* same colors as xterm */ 779 color.red = sixd_to_16bit( ((i-16)/36)%6 ); 780 color.green = sixd_to_16bit( ((i-16)/6) %6 ); 781 color.blue = sixd_to_16bit( ((i-16)/1) %6 ); 782 } else { /* greyscale */ 783 color.red = 0x0808 + 0x0a0a * (i - (6*6*6+16)); 784 color.green = color.blue = color.red; 785 } 786 return XftColorAllocValue(xw.dpy, xw.vis, 787 xw.cmap, &color, ncolor); 788 } else 789 name = colorname[i]; 790 } 791 792 return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor); 793 } 794 795 void 796 xloadalpha(void) 797 { 798 /* set alpha value of bg color */ 799 if (opt_alpha) 800 alpha = strtof(opt_alpha, NULL); 801 802 float const usedAlpha = focused ? alpha : alphaUnfocussed; 803 804 dc.col[defaultbg].color.alpha = (unsigned short)(0xffff * usedAlpha); 805 dc.col[defaultbg].pixel &= 0x00FFFFFF; 806 dc.col[defaultbg].pixel |= (unsigned char)(0xff * usedAlpha) << 24; 807 } 808 809 void 810 xloadcols(void) 811 { 812 int i; 813 static int loaded; 814 Color *cp; 815 816 if (loaded) { 817 for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp) 818 XftColorFree(xw.dpy, xw.vis, xw.cmap, cp); 819 } else { 820 dc.collen = MAX(LEN(colorname), 256); 821 dc.col = xmalloc(dc.collen * sizeof(Color)); 822 } 823 824 for (i = 0; i < dc.collen; i++) 825 if (!xloadcolor(i, NULL, &dc.col[i])) { 826 if (colorname[i]) 827 die("could not allocate color '%s'n", colorname[i]); 828 else 829 die("could not allocate color %dn", i); 830 } 831 832 xloadalpha(); 833 loaded = 1; 834 } 835 836 int 837 xsetcolorname(int x, const char *name) 838 { 839 Color ncolor; 840 841 if (!BETWEEN(x, 0, dc.collen)) 842 return 1; 843 844 if (!xloadcolor(x, name, &ncolor)) 845 return 1; 846 847 XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]); 848 dc.col[x] = ncolor; 849 850 return 0; 851 } 852 853 /* 854 * Absolute coordinates. 855 */ 856 void 857 xclear(int x1, int y1, int x2, int y2) 858 { 859 XftDrawRect(xw.draw, 860 &dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg], 861 x1, y1, x2-x1, y2-y1); 862 } 863 864 void 865 xhints(void) 866 { 867 XClassHint class = {opt_name ? opt_name : "st", 868 opt_class ? opt_class : "St"}; 869 XWMHints wm = {.flags = InputHint, .input = 1}; 870 XSizeHints *sizeh; 871 872 sizeh = XAllocSizeHints(); 873 874 sizeh->flags = PSize | PResizeInc | PBaseSize | PMinSize; 875 sizeh->height = win.h; 876 sizeh->width = win.w; 877 sizeh->height_inc = win.ch; 878 sizeh->width_inc = win.cw; 879 sizeh->base_height = 2 * borderpx; 880 sizeh->base_width = 2 * borderpx; 881 sizeh->min_height = win.ch + 2 * borderpx; 882 sizeh->min_width = win.cw + 2 * borderpx; 883 if (xw.isfixed) { 884 sizeh->flags |= PMaxSize; 885 sizeh->min_width = sizeh->max_width = win.w; 886 sizeh->min_height = sizeh->max_height = win.h; 887 } 888 if (xw.gm & (XValue|YValue)) { 889 sizeh->flags |= USPosition | PWinGravity; 890 sizeh->x = xw.l; 891 sizeh->y = xw.t; 892 sizeh->win_gravity = xgeommasktogravity(xw.gm); 893 } 894 895 XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm, 896 &class); 897 XFree(sizeh); 898 } 899 900 int 901 xgeommasktogravity(int mask) 902 { 903 switch (mask & (XNegative|YNegative)) { 904 case 0: 905 return NorthWestGravity; 906 case XNegative: 907 return NorthEastGravity; 908 case YNegative: 909 return SouthWestGravity; 910 } 911 912 return SouthEastGravity; 913 } 914 915 int 916 xloadfont(Font *f, FcPattern *pattern) 917 { 918 FcPattern *configured; 919 FcPattern *match; 920 FcResult result; 921 XGlyphInfo extents; 922 int wantattr, haveattr; 923 924 /* 925 * Manually configure instead of calling XftMatchFont 926 * so that we can use the configured pattern for 927 * "missing glyph" lookups. 928 */ 929 configured = FcPatternDuplicate(pattern); 930 if (!configured) 931 return 1; 932 933 FcConfigSubstitute(NULL, configured, FcMatchPattern); 934 XftDefaultSubstitute(xw.dpy, xw.scr, configured); 935 936 match = FcFontMatch(NULL, configured, &result); 937 if (!match) { 938 FcPatternDestroy(configured); 939 return 1; 940 } 941 942 if (!(f->match = XftFontOpenPattern(xw.dpy, match))) { 943 FcPatternDestroy(configured); 944 FcPatternDestroy(match); 945 return 1; 946 } 947 948 if ((XftPatternGetInteger(pattern, "slant", 0, &wantattr) == 949 XftResultMatch)) { 950 /* 951 * Check if xft was unable to find a font with the appropriate 952 * slant but gave us one anyway. Try to mitigate. 953 */ 954 if ((XftPatternGetInteger(f->match->pattern, "slant", 0, 955 &haveattr) != XftResultMatch) || haveattr < wantattr) { 956 f->badslant = 1; 957 fputs("font slant does not matchn", stderr); 958 } 959 } 960 961 if ((XftPatternGetInteger(pattern, "weight", 0, &wantattr) == 962 XftResultMatch)) { 963 if ((XftPatternGetInteger(f->match->pattern, "weight", 0, 964 &haveattr) != XftResultMatch) || haveattr != wantattr) { 965 f->badweight = 1; 966 fputs("font weight does not matchn", stderr); 967 } 968 } 969 970 XftTextExtentsUtf8(xw.dpy, f->match, 971 (const FcChar8 *) ascii_printable, 972 strlen(ascii_printable), &extents); 973 974 f->set = NULL; 975 f->pattern = configured; 976 977 f->ascent = f->match->ascent; 978 f->descent = f->match->descent; 979 f->lbearing = 0; 980 f->rbearing = f->match->max_advance_width; 981 982 f->height = f->ascent + f->descent; 983 f->width = DIVCEIL(extents.xOff, strlen(ascii_printable)); 984 985 return 0; 986 } 987 988 void 989 xloadfonts(char *fontstr, double fontsize) 990 { 991 FcPattern *pattern; 992 double fontval; 993 994 if (fontstr[0] == '-') 995 pattern = XftXlfdParse(fontstr, False, False); 996 else 997 pattern = FcNameParse((FcChar8 *)fontstr); 998 999 if (!pattern) 1000 die("can't open font %sn", fontstr); 1001 1002 if (fontsize > 1) { 1003 FcPatternDel(pattern, FC_PIXEL_SIZE); 1004 FcPatternDel(pattern, FC_SIZE); 1005 FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize); 1006 usedfontsize = fontsize; 1007 } else { 1008 if (FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) == 1009 FcResultMatch) { 1010 usedfontsize = fontval; 1011 } else if (FcPatternGetDouble(pattern, FC_SIZE, 0, &fontval) == 1012 FcResultMatch) { 1013 usedfontsize = -1; 1014 } else { 1015 /* 1016 * Default font size is 12, if none given. This is to 1017 * have a known usedfontsize value. 1018 */ 1019 FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12); 1020 usedfontsize = 12; 1021 } 1022 defaultfontsize = usedfontsize; 1023 } 1024 1025 if (xloadfont(&dc.font, pattern)) 1026 die("can't open font %sn", fontstr); 1027 1028 if (usedfontsize < 0) { 1029 FcPatternGetDouble(dc.font.match->pattern, 1030 FC_PIXEL_SIZE, 0, &fontval); 1031 usedfontsize = fontval; 1032 if (fontsize == 0) 1033 defaultfontsize = fontval; 1034 } 1035 1036 /* Setting character width and height. */ 1037 win.cw = ceilf(dc.font.width * cwscale); 1038 win.ch = ceilf(dc.font.height * chscale); 1039 1040 FcPatternDel(pattern, FC_SLANT); 1041 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC); 1042 if (xloadfont(&dc.ifont, pattern)) 1043 die("can't open font %sn", fontstr); 1044 1045 FcPatternDel(pattern, FC_WEIGHT); 1046 FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD); 1047 if (xloadfont(&dc.ibfont, pattern)) 1048 die("can't open font %sn", fontstr); 1049 1050 FcPatternDel(pattern, FC_SLANT); 1051 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN); 1052 if (xloadfont(&dc.bfont, pattern)) 1053 die("can't open font %sn", fontstr); 1054 1055 FcPatternDestroy(pattern); 1056 } 1057 1058 void 1059 xunloadfont(Font *f) 1060 { 1061 XftFontClose(xw.dpy, f->match); 1062 FcPatternDestroy(f->pattern); 1063 if (f->set) 1064 FcFontSetDestroy(f->set); 1065 } 1066 1067 void 1068 xunloadfonts(void) 1069 { 1070 /* Free the loaded fonts in the font cache. */ 1071 while (frclen > 0) 1072 XftFontClose(xw.dpy, frc[--frclen].font); 1073 1074 xunloadfont(&dc.font); 1075 xunloadfont(&dc.bfont); 1076 xunloadfont(&dc.ifont); 1077 xunloadfont(&dc.ibfont); 1078 } 1079 1080 int 1081 ximopen(Display *dpy) 1082 { 1083 XIMCallback imdestroy = { .client_data = NULL, .callback = ximdestroy }; 1084 XICCallback icdestroy = { .client_data = NULL, .callback = xicdestroy }; 1085 1086 xw.ime.xim = XOpenIM(xw.dpy, NULL, NULL, NULL); 1087 if (xw.ime.xim == NULL) 1088 return 0; 1089 1090 if (XSetIMValues(xw.ime.xim, XNDestroyCallback, &imdestroy, NULL)) 1091 fprintf(stderr, "XSetIMValues: " 1092 "Could not set XNDestroyCallback.n"); 1093 1094 xw.ime.spotlist = XVaCreateNestedList(0, XNSpotLocation, &xw.ime.spot, 1095 NULL); 1096 1097 if (xw.ime.xic == NULL) { 1098 xw.ime.xic = XCreateIC(xw.ime.xim, XNInputStyle, 1099 XIMPreeditNothing | XIMStatusNothing, 1100 XNClientWindow, xw.win, 1101 XNDestroyCallback, &icdestroy, 1102 NULL); 1103 } 1104 if (xw.ime.xic == NULL) 1105 fprintf(stderr, "XCreateIC: Could not create input context.n"); 1106 1107 return 1; 1108 } 1109 1110 void 1111 ximinstantiate(Display *dpy, XPointer client, XPointer call) 1112 { 1113 if (ximopen(dpy)) 1114 XUnregisterIMInstantiateCallback(xw.dpy, NULL, NULL, NULL, 1115 ximinstantiate, NULL); 1116 } 1117 1118 void 1119 ximdestroy(XIM xim, XPointer client, XPointer call) 1120 { 1121 xw.ime.xim = NULL; 1122 XRegisterIMInstantiateCallback(xw.dpy, NULL, NULL, NULL, 1123 ximinstantiate, NULL); 1124 XFree(xw.ime.spotlist); 1125 } 1126 1127 int 1128 xicdestroy(XIC xim, XPointer client, XPointer call) 1129 { 1130 xw.ime.xic = NULL; 1131 return 1; 1132 } 1133 1134 void 1135 xinit(int cols, int rows) 1136 { 1137 XGCValues gcvalues; 1138 Cursor cursor; 1139 Window parent; 1140 pid_t thispid = getpid(); 1141 XColor xmousefg, xmousebg; 1142 XWindowAttributes attr; 1143 XVisualInfo vis; 1144 1145 xw.scr = XDefaultScreen(xw.dpy); 1146 1147 if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0)))) { 1148 parent = XRootWindow(xw.dpy, xw.scr); 1149 xw.depth = 32; 1150 } else { 1151 XGetWindowAttributes(xw.dpy, parent, &attr); 1152 xw.depth = attr.depth; 1153 } 1154 1155 XMatchVisualInfo(xw.dpy, xw.scr, xw.depth, TrueColor, &vis); 1156 xw.vis = vis.visual; 1157 1158 /* font */ 1159 if (!FcInit()) 1160 die("could not init fontconfig.n"); 1161 1162 usedfont = (opt_font == NULL)? font : opt_font; 1163 xloadfonts(usedfont, 0); 1164 1165 /* colors */ 1166 xw.cmap = XCreateColormap(xw.dpy, parent, xw.vis, None); 1167 xloadcols(); 1168 1169 /* adjust fixed window geometry */ 1170 win.w = 2 * borderpx + cols * win.cw; 1171 win.h = 2 * borderpx + rows * win.ch; 1172 if (xw.gm & XNegative) 1173 xw.l += DisplayWidth(xw.dpy, xw.scr) - win.w - 2; 1174 if (xw.gm & YNegative) 1175 xw.t += DisplayHeight(xw.dpy, xw.scr) - win.h - 2; 1176 1177 /* Events */ 1178 xw.attrs.background_pixel = dc.col[defaultbg].pixel; 1179 xw.attrs.border_pixel = dc.col[defaultbg].pixel; 1180 xw.attrs.bit_gravity = NorthWestGravity; 1181 xw.attrs.event_mask = FocusChangeMask | KeyPressMask | KeyReleaseMask 1182 | ExposureMask | VisibilityChangeMask | StructureNotifyMask 1183 | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask; 1184 xw.attrs.colormap = xw.cmap; 1185 1186 xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t, 1187 win.w, win.h, 0, xw.depth, InputOutput, 1188 xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity 1189 | CWEventMask | CWColormap, &xw.attrs); 1190 1191 memset(&gcvalues, 0, sizeof(gcvalues)); 1192 gcvalues.graphics_exposures = False; 1193 xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h, xw.depth); 1194 dc.gc = XCreateGC(xw.dpy, xw.buf, GCGraphicsExposures, &gcvalues); 1195 XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel); 1196 XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h); 1197 1198 /* font spec buffer */ 1199 xw.specbuf = xmalloc(cols * sizeof(GlyphFontSpec)); 1200 1201 /* Xft rendering context */ 1202 xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap); 1203 1204 /* input methods */ 1205 if (!ximopen(xw.dpy)) { 1206 XRegisterIMInstantiateCallback(xw.dpy, NULL, NULL, NULL, 1207 ximinstantiate, NULL); 1208 } 1209 1210 /* white cursor, black outline */ 1211 cursor = XCreateFontCursor(xw.dpy, mouseshape); 1212 XDefineCursor(xw.dpy, xw.win, cursor); 1213 1214 if (XParseColor(xw.dpy, xw.cmap, colorname[mousefg], &xmousefg) == 0) { 1215 xmousefg.red = 0xffff; 1216 xmousefg.green = 0xffff; 1217 xmousefg.blue = 0xffff; 1218 } 1219 1220 if (XParseColor(xw.dpy, xw.cmap, colorname[mousebg], &xmousebg) == 0) { 1221 xmousebg.red = 0x0000; 1222 xmousebg.green = 0x0000; 1223 xmousebg.blue = 0x0000; 1224 } 1225 1226 XRecolorCursor(xw.dpy, cursor, &xmousefg, &xmousebg); 1227 1228 xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False); 1229 xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False); 1230 xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False); 1231 XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1); 1232 1233 xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False); 1234 XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL, 32, 1235 PropModeReplace, (uchar *)&thispid, 1); 1236 1237 win.mode = MODE_NUMLOCK; 1238 resettitle(); 1239 xhints(); 1240 XMapWindow(xw.dpy, xw.win); 1241 XSync(xw.dpy, False); 1242 1243 clock_gettime(CLOCK_MONOTONIC, &xsel.tclick1); 1244 clock_gettime(CLOCK_MONOTONIC, &xsel.tclick2); 1245 xsel.primary = NULL; 1246 xsel.clipboard = NULL; 1247 xsel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0); 1248 if (xsel.xtarget == None) 1249 xsel.xtarget = XA_STRING; 1250 } 1251 1252 int 1253 xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y) 1254 { 1255 float winx = borderpx + x * win.cw, winy = borderpx + y * win.ch, xp, yp; 1256 ushort mode, prevmode = USHRT_MAX; 1257 Font *font = &dc.font; 1258 int frcflags = FRC_NORMAL; 1259 float runewidth = win.cw; 1260 Rune rune; 1261 FT_UInt glyphidx; 1262 FcResult fcres; 1263 FcPattern *fcpattern, *fontpattern; 1264 FcFontSet *fcsets[] = { NULL }; 1265 FcCharSet *fccharset; 1266 int i, f, numspecs = 0; 1267 1268 for (i = 0, xp = winx, yp = winy + font->ascent; i < len; ++i) { 1269 /* Fetch rune and mode for current glyph. */ 1270 rune = glyphs[i].u; 1271 mode = glyphs[i].mode; 1272 1273 /* Skip dummy wide-character spacing. */ 1274 if (mode == ATTR_WDUMMY) 1275 continue; 1276 1277 /* Determine font for glyph if different from previous glyph. */ 1278 if (prevmode != mode) { 1279 prevmode = mode; 1280 font = &dc.font; 1281 frcflags = FRC_NORMAL; 1282 runewidth = win.cw * ((mode & ATTR_WIDE) ? 2.0f : 1.0f); 1283 if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) { 1284 font = &dc.ibfont; 1285 frcflags = FRC_ITALICBOLD; 1286 } else if (mode & ATTR_ITALIC) { 1287 font = &dc.ifont; 1288 frcflags = FRC_ITALIC; 1289 } else if (mode & ATTR_BOLD) { 1290 font = &dc.bfont; 1291 frcflags = FRC_BOLD; 1292 } 1293 yp = winy + font->ascent; 1294 } 1295 1296 /* Lookup character index with default font. */ 1297 glyphidx = XftCharIndex(xw.dpy, font->match, rune); 1298 if (glyphidx) { 1299 specs[numspecs].font = font->match; 1300 specs[numspecs].glyph = glyphidx; 1301 specs[numspecs].x = (short)xp; 1302 specs[numspecs].y = (short)yp; 1303 xp += runewidth; 1304 numspecs++; 1305 continue; 1306 } 1307 1308 /* Fallback on font cache, search the font cache for match. */ 1309 for (f = 0; f < frclen; f++) { 1310 glyphidx = XftCharIndex(xw.dpy, frc[f].font, rune); 1311 /* Everything correct. */ 1312 if (glyphidx && frc[f].flags == frcflags) 1313 break; 1314 /* We got a default font for a not found glyph. */ 1315 if (!glyphidx && frc[f].flags == frcflags 1316 && frc[f].unicodep == rune) { 1317 break; 1318 } 1319 } 1320 1321 /* Nothing was found. Use fontconfig to find matching font. */ 1322 if (f >= frclen) { 1323 if (!font->set) 1324 font->set = FcFontSort(0, font->pattern, 1325 1, 0, &fcres); 1326 fcsets[0] = font->set; 1327 1328 /* 1329 * Nothing was found in the cache. Now use 1330 * some dozen of Fontconfig calls to get the 1331 * font for one single character. 1332 * 1333 * Xft and fontconfig are design failures. 1334 */ 1335 fcpattern = FcPatternDuplicate(font->pattern); 1336 fccharset = FcCharSetCreate(); 1337 1338 FcCharSetAddChar(fccharset, rune); 1339 FcPatternAddCharSet(fcpattern, FC_CHARSET, 1340 fccharset); 1341 FcPatternAddBool(fcpattern, FC_SCALABLE, 1); 1342 1343 FcConfigSubstitute(0, fcpattern, 1344 FcMatchPattern); 1345 FcDefaultSubstitute(fcpattern); 1346 1347 fontpattern = FcFontSetMatch(0, fcsets, 1, 1348 fcpattern, &fcres); 1349 1350 /* Allocate memory for the new cache entry. */ 1351 if (frclen >= frccap) { 1352 frccap += 16; 1353 frc = xrealloc(frc, frccap * sizeof(Fontcache)); 1354 } 1355 1356 frc[frclen].font = XftFontOpenPattern(xw.dpy, 1357 fontpattern); 1358 if (!frc[frclen].font) 1359 die("XftFontOpenPattern failed seeking fallback font: %sn", 1360 strerror(errno)); 1361 frc[frclen].flags = frcflags; 1362 frc[frclen].unicodep = rune; 1363 1364 glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune); 1365 1366 f = frclen; 1367 frclen++; 1368 1369 FcPatternDestroy(fcpattern); 1370 FcCharSetDestroy(fccharset); 1371 } 1372 1373 specs[numspecs].font = frc[f].font; 1374 specs[numspecs].glyph = glyphidx; 1375 specs[numspecs].x = (short)xp; 1376 specs[numspecs].y = (short)yp; 1377 xp += runewidth; 1378 numspecs++; 1379 } 1380 1381 return numspecs; 1382 } 1383 1384 void 1385 xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, int y) 1386 { 1387 int charlen = len * ((base.mode & ATTR_WIDE) ? 2 : 1); 1388 int winx = borderpx + x * win.cw, winy = borderpx + y * win.ch, 1389 width = charlen * win.cw; 1390 Color *fg, *bg, *temp, revfg, revbg, truefg, truebg; 1391 XRenderColor colfg, colbg; 1392 XRectangle r; 1393 1394 /* Fallback on color display for attributes not supported by the font */ 1395 if (base.mode & ATTR_ITALIC && base.mode & ATTR_BOLD) { 1396 if (dc.ibfont.badslant || dc.ibfont.badweight) 1397 base.fg = defaultattr; 1398 } else if ((base.mode & ATTR_ITALIC && dc.ifont.badslant) || 1399 (base.mode & ATTR_BOLD && dc.bfont.badweight)) { 1400 base.fg = defaultattr; 1401 } 1402 1403 if (IS_TRUECOL(base.fg)) { 1404 colfg.alpha = 0xffff; 1405 colfg.red = TRUERED(base.fg); 1406 colfg.green = TRUEGREEN(base.fg); 1407 colfg.blue = TRUEBLUE(base.fg); 1408 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &truefg); 1409 fg = &truefg; 1410 } else { 1411 fg = &dc.col[base.fg]; 1412 } 1413 1414 if (IS_TRUECOL(base.bg)) { 1415 colbg.alpha = 0xffff; 1416 colbg.green = TRUEGREEN(base.bg); 1417 colbg.red = TRUERED(base.bg); 1418 colbg.blue = TRUEBLUE(base.bg); 1419 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &truebg); 1420 bg = &truebg; 1421 } else { 1422 bg = &dc.col[base.bg]; 1423 } 1424 1425 /* Change basic system colors [0-7] to bright system colors [8-15] */ 1426 if ((base.mode & ATTR_BOLD_FAINT) == ATTR_BOLD && BETWEEN(base.fg, 0, 7)) 1427 fg = &dc.col[base.fg + 8]; 1428 1429 if (IS_SET(MODE_REVERSE)) { 1430 if (fg == &dc.col[defaultfg]) { 1431 fg = &dc.col[defaultbg]; 1432 } else { 1433 colfg.red = ~fg->color.red; 1434 colfg.green = ~fg->color.green; 1435 colfg.blue = ~fg->color.blue; 1436 colfg.alpha = fg->color.alpha; 1437 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, 1438 &revfg); 1439 fg = &revfg; 1440 } 1441 1442 if (bg == &dc.col[defaultbg]) { 1443 bg = &dc.col[defaultfg]; 1444 } else { 1445 colbg.red = ~bg->color.red; 1446 colbg.green = ~bg->color.green; 1447 colbg.blue = ~bg->color.blue; 1448 colbg.alpha = bg->color.alpha; 1449 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, 1450 &revbg); 1451 bg = &revbg; 1452 } 1453 } 1454 1455 if ((base.mode & ATTR_BOLD_FAINT) == ATTR_FAINT) { 1456 colfg.red = fg->color.red / 2; 1457 colfg.green = fg->color.green / 2; 1458 colfg.blue = fg->color.blue / 2; 1459 colfg.alpha = fg->color.alpha; 1460 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg); 1461 fg = &revfg; 1462 } 1463 1464 if (base.mode & ATTR_REVERSE) { 1465 temp = fg; 1466 fg = bg; 1467 bg = temp; 1468 } 1469 1470 if (base.mode & ATTR_BLINK && win.mode & MODE_BLINK) 1471 fg = bg; 1472 1473 if (base.mode & ATTR_INVISIBLE) 1474 fg = bg; 1475 1476 /* Intelligent cleaning up of the borders. */ 1477 if (x == 0) { 1478 xclear(0, (y == 0)? 0 : winy, borderpx, 1479 winy + win.ch + 1480 ((winy + win.ch >= borderpx + win.th)? win.h : 0)); 1481 } 1482 if (winx + width >= borderpx + win.tw) { 1483 xclear(winx + width, (y == 0)? 0 : winy, win.w, 1484 ((winy + win.ch >= borderpx + win.th)? win.h : (winy + win.ch))); 1485 } 1486 if (y == 0) 1487 xclear(winx, 0, winx + width, borderpx); 1488 if (winy + win.ch >= borderpx + win.th) 1489 xclear(winx, winy + win.ch, winx + width, win.h); 1490 1491 /* Clean up the region we want to draw to. */ 1492 XftDrawRect(xw.draw, bg, winx, winy, width, win.ch); 1493 1494 /* Set the clip region because Xft is sometimes dirty. */ 1495 r.x = 0; 1496 r.y = 0; 1497 r.height = win.ch; 1498 r.width = width; 1499 XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1); 1500 1501 /* Render the glyphs. */ 1502 XftDrawGlyphFontSpec(xw.draw, fg, specs, len); 1503 1504 /* Render underline and strikethrough. */ 1505 if (base.mode & ATTR_UNDERLINE) { 1506 XftDrawRect(xw.draw, fg, winx, winy + dc.font.ascent + 1, 1507 width, 1); 1508 } 1509 1510 if (base.mode & ATTR_STRUCK) { 1511 XftDrawRect(xw.draw, fg, winx, winy + 2 * dc.font.ascent / 3, 1512 width, 1); 1513 } 1514 1515 /* Reset clip to none. */ 1516 XftDrawSetClip(xw.draw, 0); 1517 } 1518 1519 void 1520 xdrawglyph(Glyph g, int x, int y) 1521 { 1522 int numspecs; 1523 XftGlyphFontSpec spec; 1524 1525 numspecs = xmakeglyphfontspecs(&spec, &g, 1, x, y); 1526 xdrawglyphfontspecs(&spec, g, numspecs, x, y); 1527 } 1528 1529 void 1530 xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og) 1531 { 1532 Color drawcol; 1533 1534 /* remove the old cursor */ 1535 if (selected(ox, oy)) 1536 og.mode ^= ATTR_REVERSE; 1537 xdrawglyph(og, ox, oy); 1538 1539 if (IS_SET(MODE_HIDE)) 1540 return; 1541 1542 /* 1543 * Select the right color for the right mode. 1544 */ 1545 g.mode &= ATTR_BOLD|ATTR_ITALIC|ATTR_UNDERLINE|ATTR_STRUCK|ATTR_WIDE; 1546 1547 if (IS_SET(MODE_REVERSE)) { 1548 g.mode |= ATTR_REVERSE; 1549 g.bg = defaultfg; 1550 if (selected(cx, cy)) { 1551 drawcol = dc.col[defaultcs]; 1552 g.fg = defaultrcs; 1553 } else { 1554 drawcol = dc.col[defaultrcs]; 1555 g.fg = defaultcs; 1556 } 1557 } else { 1558 if (selected(cx, cy)) { 1559 g.fg = defaultfg; 1560 g.bg = defaultrcs; 1561 } else { 1562 g.fg = defaultbg; 1563 g.bg = defaultcs; 1564 } 1565 drawcol = dc.col[g.bg]; 1566 } 1567 1568 /* draw the new one */ 1569 if (IS_SET(MODE_FOCUSED)) { 1570 switch (win.cursor) { 1571 case 7: /* st extension */ 1572 g.u = 0x2603; /* snowman (U+2603) */ 1573 /* FALLTHROUGH */ 1574 case 0: /* Blinking Block */ 1575 case 1: /* Blinking Block (Default) */ 1576 case 2: /* Steady Block */ 1577 xdrawglyph(g, cx, cy); 1578 break; 1579 case 3: /* Blinking Underline */ 1580 case 4: /* Steady Underline */ 1581 XftDrawRect(xw.draw, &drawcol, 1582 borderpx + cx * win.cw, 1583 borderpx + (cy + 1) * win.ch - 1584 cursorthickness, 1585 win.cw, cursorthickness); 1586 break; 1587 case 5: /* Blinking bar */ 1588 case 6: /* Steady bar */ 1589 XftDrawRect(xw.draw, &drawcol, 1590 borderpx + cx * win.cw, 1591 borderpx + cy * win.ch, 1592 cursorthickness, win.ch); 1593 break; 1594 } 1595 } else { 1596 XftDrawRect(xw.draw, &drawcol, 1597 borderpx + cx * win.cw, 1598 borderpx + cy * win.ch, 1599 win.cw - 1, 1); 1600 XftDrawRect(xw.draw, &drawcol, 1601 borderpx + cx * win.cw, 1602 borderpx + cy * win.ch, 1603 1, win.ch - 1); 1604 XftDrawRect(xw.draw, &drawcol, 1605 borderpx + (cx + 1) * win.cw - 1, 1606 borderpx + cy * win.ch, 1607 1, win.ch - 1); 1608 XftDrawRect(xw.draw, &drawcol, 1609 borderpx + cx * win.cw, 1610 borderpx + (cy + 1) * win.ch - 1, 1611 win.cw, 1); 1612 } 1613 } 1614 1615 void 1616 xsetenv(void) 1617 { 1618 char buf[sizeof(long) * 8 + 1]; 1619 1620 snprintf(buf, sizeof(buf), "%lu", xw.win); 1621 setenv("WINDOWID", buf, 1); 1622 } 1623 1624 void 1625 xsettitle(char *p) 1626 { 1627 XTextProperty prop; 1628 DEFAULT(p, opt_title); 1629 1630 Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle, 1631 &prop); 1632 XSetWMName(xw.dpy, xw.win, &prop); 1633 XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmname); 1634 XFree(prop.value); 1635 } 1636 1637 int 1638 xstartdraw(void) 1639 { 1640 return IS_SET(MODE_VISIBLE); 1641 } 1642 1643 void 1644 xdrawline(Line line, int x1, int y1, int x2) 1645 { 1646 int i, x, ox, numspecs; 1647 Glyph base, new; 1648 XftGlyphFontSpec *specs = xw.specbuf; 1649 1650 numspecs = xmakeglyphfontspecs(specs, &line[x1], x2 - x1, x1, y1); 1651 i = ox = 0; 1652 for (x = x1; x < x2 && i < numspecs; x++) { 1653 new = line[x]; 1654 if (new.mode == ATTR_WDUMMY) 1655 continue; 1656 if (selected(x, y1)) 1657 new.mode ^= ATTR_REVERSE; 1658 if (i > 0 && ATTRCMP(base, new)) { 1659 xdrawglyphfontspecs(specs, base, i, ox, y1); 1660 specs += i; 1661 numspecs -= i; 1662 i = 0; 1663 } 1664 if (i == 0) { 1665 ox = x; 1666 base = new; 1667 } 1668 i++; 1669 } 1670 if (i > 0) 1671 xdrawglyphfontspecs(specs, base, i, ox, y1); 1672 } 1673 1674 void 1675 xfinishdraw(void) 1676 { 1677 XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, win.w, 1678 win.h, 0, 0); 1679 XSetForeground(xw.dpy, dc.gc, 1680 dc.col[IS_SET(MODE_REVERSE)? 1681 defaultfg : defaultbg].pixel); 1682 } 1683 1684 void 1685 xximspot(int x, int y) 1686 { 1687 if (xw.ime.xic == NULL) 1688 return; 1689 1690 xw.ime.spot.x = borderpx + x * win.cw; 1691 xw.ime.spot.y = borderpx + (y + 1) * win.ch; 1692 1693 XSetICValues(xw.ime.xic, XNPreeditAttributes, xw.ime.spotlist, NULL); 1694 } 1695 1696 void 1697 expose(XEvent *ev) 1698 { 1699 redraw(); 1700 } 1701 1702 void 1703 visibility(XEvent *ev) 1704 { 1705 XVisibilityEvent *e = &ev->xvisibility; 1706 1707 MODBIT(win.mode, e->state != VisibilityFullyObscured, MODE_VISIBLE); 1708 } 1709 1710 void 1711 unmap(XEvent *ev) 1712 { 1713 win.mode &= ~MODE_VISIBLE; 1714 } 1715 1716 void 1717 xsetpointermotion(int set) 1718 { 1719 MODBIT(xw.attrs.event_mask, set, PointerMotionMask); 1720 XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs); 1721 } 1722 1723 void 1724 xsetmode(int set, unsigned int flags) 1725 { 1726 int mode = win.mode; 1727 MODBIT(win.mode, set, flags); 1728 if ((win.mode & MODE_REVERSE) != (mode & MODE_REVERSE)) 1729 redraw(); 1730 } 1731 1732 int 1733 xsetcursor(int cursor) 1734 { 1735 if (!BETWEEN(cursor, 0, 7)) /* 7: st extension */ 1736 return 1; 1737 win.cursor = cursor; 1738 return 0; 1739 } 1740 1741 void 1742 xseturgency(int add) 1743 { 1744 XWMHints *h = XGetWMHints(xw.dpy, xw.win); 1745 1746 MODBIT(h->flags, add, XUrgencyHint); 1747 XSetWMHints(xw.dpy, xw.win, h); 1748 XFree(h); 1749 } 1750 1751 void 1752 xbell(void) 1753 { 1754 if (!(IS_SET(MODE_FOCUSED))) 1755 xseturgency(1); 1756 if (bellvolume) 1757 XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL); 1758 } 1759 1760 void 1761 focus(XEvent *ev) 1762 { 1763 XFocusChangeEvent *e = &ev->xfocus; 1764 1765 if (e->mode == NotifyGrab) 1766 return; 1767 1768 if (ev->type == FocusIn) { 1769 if (xw.ime.xic) 1770 XSetICFocus(xw.ime.xic); 1771 win.mode |= MODE_FOCUSED; 1772 xseturgency(0); 1773 if (IS_SET(MODE_FOCUS)) 1774 ttywrite("033[I", 3, 0); 1775 if (!focused) { 1776 focused = true; 1777 xloadalpha(); 1778 redraw(); 1779 } 1780 } else { 1781 if (xw.ime.xic) 1782 XUnsetICFocus(xw.ime.xic); 1783 win.mode &= ~MODE_FOCUSED; 1784 if (IS_SET(MODE_FOCUS)) 1785 ttywrite("033[O", 3, 0); 1786 if (focused) { 1787 focused = false; 1788 xloadalpha(); 1789 redraw(); 1790 } 1791 } 1792 } 1793 1794 int 1795 match(uint mask, uint state) 1796 { 1797 return mask == XK_ANY_MOD || mask == (state & ~ignoremod); 1798 } 1799 1800 char* 1801 kmap(KeySym k, uint state) 1802 { 1803 Key *kp; 1804 int i; 1805 1806 /* Check for mapped keys out of X11 function keys. */ 1807 for (i = 0; i < LEN(mappedkeys); i++) { 1808 if (mappedkeys[i] == k) 1809 break; 1810 } 1811 if (i == LEN(mappedkeys)) { 1812 if ((k & 0xFFFF) < 0xFD00) 1813 return NULL; 1814 } 1815 1816 for (kp = key; kp < key + LEN(key); kp++) { 1817 if (kp->k != k) 1818 continue; 1819 1820 if (!match(kp->mask, state)) 1821 continue; 1822 1823 if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0) 1824 continue; 1825 if (IS_SET(MODE_NUMLOCK) && kp->appkey == 2) 1826 continue; 1827 1828 if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0) 1829 continue; 1830 1831 return kp->s; 1832 } 1833 1834 return NULL; 1835 } 1836 1837 void 1838 kpress(XEvent *ev) 1839 { 1840 XKeyEvent *e = &ev->xkey; 1841 KeySym ksym; 1842 char buf[64], *customkey; 1843 int len; 1844 Rune c; 1845 Status status; 1846 Shortcut *bp; 1847 1848 if (IS_SET(MODE_KBDLOCK)) 1849 return; 1850 1851 if (xw.ime.xic) 1852 len = XmbLookupString(xw.ime.xic, e, buf, sizeof buf, &ksym, &status); 1853 else 1854 len = XLookupString(e, buf, sizeof buf, &ksym, NULL); 1855 /* 1. shortcuts */ 1856 for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) { 1857 if (ksym == bp->keysym && match(bp->mod, e->state)) { 1858 bp->func(&(bp->arg)); 1859 return; 1860 } 1861 } 1862 1863 /* 2. custom keys from config.def.h */ 1864 if ((customkey = kmap(ksym, e->state))) { 1865 ttywrite(customkey, strlen(customkey), 1); 1866 return; 1867 } 1868 1869 /* 3. composed string from input method */ 1870 if (len == 0) 1871 return; 1872 if (len == 1 && e->state & Mod1Mask) { 1873 if (IS_SET(MODE_8BIT)) { 1874 if (*buf < 0177) { 1875 c = *buf | 0x80; 1876 len = utf8encode(c, buf); 1877 } 1878 } else { 1879 buf[1] = buf[0]; 1880 buf[0] = '033'; 1881 len = 2; 1882 } 1883 } 1884 ttywrite(buf, len, 1); 1885 } 1886 1887 void 1888 cmessage(XEvent *e) 1889 { 1890 /* 1891 * See xembed specs 1892 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html 1893 */ 1894 if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) { 1895 if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) { 1896 win.mode |= MODE_FOCUSED; 1897 xseturgency(0); 1898 } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) { 1899 win.mode &= ~MODE_FOCUSED; 1900 } 1901 } else if (e->xclient.data.l[0] == xw.wmdeletewin) { 1902 ttyhangup(); 1903 exit(0); 1904 } 1905 } 1906 1907 void 1908 resize(XEvent *e) 1909 { 1910 if (e->xconfigure.width == win.w && e->xconfigure.height == win.h) 1911 return; 1912 1913 cresize(e->xconfigure.width, e->xconfigure.height); 1914 } 1915 1916 void 1917 run(void) 1918 { 1919 XEvent ev; 1920 int w = win.w, h = win.h; 1921 fd_set rfd; 1922 int xfd = XConnectionNumber(xw.dpy), ttyfd, xev, drawing; 1923 struct timespec seltv, *tv, now, lastblink, trigger; 1924 double timeout; 1925 1926 /* Waiting for window mapping */ 1927 do { 1928 XNextEvent(xw.dpy, &ev); 1929 /* 1930 * This XFilterEvent call is required because of XOpenIM. It 1931 * does filter out the key event and some client message for 1932 * the input method too. 1933 */ 1934 if (XFilterEvent(&ev, None)) 1935 continue; 1936 if (ev.type == ConfigureNotify) { 1937 w = ev.xconfigure.width; 1938 h = ev.xconfigure.height; 1939 } 1940 } while (ev.type != MapNotify); 1941 1942 ttyfd = ttynew(opt_line, shell, opt_io, opt_cmd); 1943 cresize(w, h); 1944 1945 for (timeout = -1, drawing = 0, lastblink = (struct timespec){0};;) { 1946 FD_ZERO(&rfd); 1947 FD_SET(ttyfd, &rfd); 1948 FD_SET(xfd, &rfd); 1949 1950 if (XPending(xw.dpy)) 1951 timeout = 0; /* existing events might not set xfd */ 1952 1953 seltv.tv_sec = timeout / 1E3; 1954 seltv.tv_nsec = 1E6 * (timeout - 1E3 * seltv.tv_sec); 1955 tv = timeout >= 0 ? &seltv : NULL; 1956 1957 if (pselect(MAX(xfd, ttyfd)+1, &rfd, NULL, NULL, tv, NULL) < 0) { 1958 if (errno == EINTR) 1959 continue; 1960 die("select failed: %sn", strerror(errno)); 1961 } 1962 clock_gettime(CLOCK_MONOTONIC, &now); 1963 1964 if (FD_ISSET(ttyfd, &rfd)) 1965 ttyread(); 1966 1967 xev = 0; 1968 while (XPending(xw.dpy)) { 1969 xev = 1; 1970 XNextEvent(xw.dpy, &ev); 1971 if (XFilterEvent(&ev, None)) 1972 continue; 1973 if (handler[ev.type]) 1974 (handler[ev.type])(&ev); 1975 } 1976 1977 /* 1978 * To reduce flicker and tearing, when new content or event 1979 * triggers drawing, we first wait a bit to ensure we got 1980 * everything, and if nothing new arrives - we draw. 1981 * We start with trying to wait minlatency ms. If more content 1982 * arrives sooner, we retry with shorter and shorter periods, 1983 * and eventually draw even without idle after maxlatency ms. 1984 * Typically this results in low latency while interacting, 1985 * maximum latency intervals during `cat huge.txt`, and perfect 1986 * sync with periodic updates from animations/key-repeats/etc. 1987 */ 1988 if (FD_ISSET(ttyfd, &rfd) || xev) { 1989 if (!drawing) { 1990 trigger = now; 1991 drawing = 1; 1992 } 1993 timeout = (maxlatency - TIMEDIFF(now, trigger)) 1994 / maxlatency * minlatency; 1995 if (timeout > 0) 1996 continue; /* we have time, try to find idle */ 1997 } 1998 1999 /* idle detected or maxlatency exhausted -> draw */ 2000 timeout = -1; 2001 if (blinktimeout && tattrset(ATTR_BLINK)) { 2002 timeout = blinktimeout - TIMEDIFF(now, lastblink); 2003 if (timeout <= 0) { 2004 if (-timeout > blinktimeout) /* start visible */ 2005 win.mode |= MODE_BLINK; 2006 win.mode ^= MODE_BLINK; 2007 tsetdirtattr(ATTR_BLINK); 2008 lastblink = now; 2009 timeout = blinktimeout; 2010 } 2011 } 2012 2013 draw(); 2014 XFlush(xw.dpy); 2015 drawing = 0; 2016 } 2017 } 2018 2019 int 2020 resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst) 2021 { 2022 char **sdst = dst; 2023 int *idst = dst; 2024 float *fdst = dst; 2025 2026 char fullname[256]; 2027 char fullclass[256]; 2028 char *type; 2029 XrmValue ret; 2030 2031 snprintf(fullname, sizeof(fullname), "%s.%s", 2032 opt_name ? opt_name : "st", name); 2033 snprintf(fullclass, sizeof(fullclass), "%s.%s", 2034 opt_class ? opt_class : "St", name); 2035 fullname[sizeof(fullname) - 1] = fullclass[sizeof(fullclass) - 1] = '0'; 2036 2037 XrmGetResource(db, fullname, fullclass, &type, &ret); 2038 if (ret.addr == NULL || strncmp("String", type, 64)) 2039 return 1; 2040 2041 switch (rtype) { 2042 case STRING: 2043 *sdst = ret.addr; 2044 break; 2045 case INTEGER: 2046 *idst = strtoul(ret.addr, NULL, 10); 2047 break; 2048 case FLOAT: 2049 *fdst = strtof(ret.addr, NULL); 2050 break; 2051 } 2052 return 0; 2053 } 2054 2055 void 2056 config_init(void) 2057 { 2058 char *resm; 2059 XrmDatabase db; 2060 ResourcePref *p; 2061 2062 XrmInitialize(); 2063 resm = XResourceManagerString(xw.dpy); 2064 if (!resm) 2065 return; 2066 2067 db = XrmGetStringDatabase(resm); 2068 for (p = resources; p < resources + LEN(resources); p++) 2069 resource_load(db, p->name, p->type, p->dst); 2070 } 2071 2072 void 2073 usage(void) 2074 { 2075 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]" 2076 " [-n name] [-o file]n" 2077 " [-T title] [-t title] [-w windowid]" 2078 " [[-e] command [args ...]]n" 2079 " %s [-aiv] [-c class] [-f font] [-g geometry]" 2080 " [-n name] [-o file]n" 2081 " [-T title] [-t title] [-w windowid] -l line" 2082 " [stty_args ...]n", argv0, argv0); 2083 } 2084 2085 int 2086 main(int argc, char *argv[]) 2087 { 2088 xw.l = xw.t = 0; 2089 xw.isfixed = False; 2090 xsetcursor(cursorshape); 2091 2092 ARGBEGIN { 2093 case 'a': 2094 allowaltscreen = 0; 2095 break; 2096 case 'A': 2097 opt_alpha = EARGF(usage()); 2098 break; 2099 case 'c': 2100 opt_class = EARGF(usage()); 2101 break; 2102 case 'e': 2103 if (argc > 0) 2104 --argc, ++argv; 2105 goto run; 2106 case 'f': 2107 opt_font = EARGF(usage()); 2108 break; 2109 case 'g': 2110 xw.gm = XParseGeometry(EARGF(usage()), 2111 &xw.l, &xw.t, &cols, &rows); 2112 break; 2113 case 'i': 2114 xw.isfixed = 1; 2115 break; 2116 case 'o': 2117 opt_io = EARGF(usage()); 2118 break; 2119 case 'l': 2120 opt_line = EARGF(usage()); 2121 break; 2122 case 'n': 2123 opt_name = EARGF(usage()); 2124 break; 2125 case 't': 2126 case 'T': 2127 opt_title = EARGF(usage()); 2128 break; 2129 case 'w': 2130 opt_embed = EARGF(usage()); 2131 break; 2132 case 'v': 2133 die("%s " VERSION "n", argv0); 2134 break; 2135 default: 2136 usage(); 2137 } ARGEND; 2138 2139 run: 2140 if (argc > 0) /* eat all remaining arguments */ 2141 opt_cmd = argv; 2142 2143 if (!opt_title) 2144 opt_title = (opt_line || !opt_cmd) ? "st" : opt_cmd[0]; 2145 2146 setlocale(LC_CTYPE, ""); 2147 XSetLocaleModifiers(""); 2148 2149 if(!(xw.dpy = XOpenDisplay(NULL))) 2150 die("Can't open displayn"); 2151 2152 config_init(); 2153 cols = MAX(cols, 1); 2154 rows = MAX(rows, 1); 2155 tnew(cols, rows); 2156 xinit(cols, rows); 2157 xsetenv(); 2158 selinit(); 2159 run(); 2160 2161 return 0; 2162 }