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