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