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