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