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