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