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