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