1 /* See LICENSE for license details. */
7 #include <sys/select.h>
11 #include <X11/Xatom.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>
24 /* types used in config.h */
28 void (*func
)(const Arg
*);
35 void (*func
)(const Arg
*);
38 int altscrn
; /* 0: don't care, -1: not alt screen, 1: alt screen */
45 /* three-valued logic variables: 0 indifferent, 1 on, -1 off */
46 signed char appkey
; /* application keypad */
47 signed char appcursor
; /* application cursor */
50 /* Xresources preferences */
59 enum resource_type type
;
64 #define XK_ANY_MOD UINT_MAX
66 #define XK_SWITCH_MOD (1<<13|1<<14)
68 /* function definitions used in config.h */
69 static void clipcopy(const Arg
*);
70 static void clippaste(const Arg
*);
71 static void numlock(const Arg
*);
72 static void selpaste(const Arg
*);
73 static void zoom(const Arg
*);
74 static void zoomabs(const Arg
*);
75 static void zoomreset(const Arg
*);
76 static void ttysend(const Arg
*);
78 /* config.h for applying patches and the configuration. */
82 #define XEMBED_FOCUS_IN 4
83 #define XEMBED_FOCUS_OUT 5
86 #define IS_SET(flag) ((win.mode & (flag)) != 0)
87 #define TRUERED(x) (((x) & 0xff0000) >> 8)
88 #define TRUEGREEN(x) (((x) & 0xff00))
89 #define TRUEBLUE(x) (((x) & 0xff) << 8)
91 typedef XftDraw
*Draw
;
92 typedef XftColor Color
;
93 typedef XftGlyphFontSpec GlyphFontSpec
;
95 /* Purely graphic info */
97 int tw
, th
; /* tty width and height */
98 int w
, h
; /* window width and height */
99 int hborderpx
, vborderpx
;
100 int ch
; /* char height */
101 int cw
; /* char width */
102 int mode
; /* window state/mode flags */
103 int cursor
; /* cursor style */
111 GlyphFontSpec
*specbuf
; /* font spec buffer used for rendering */
112 Atom xembed
, wmdeletewin
, netwmname
, netwmiconname
, netwmpid
;
117 XVaNestedList spotlist
;
121 XSetWindowAttributes attrs
;
123 int isfixed
; /* is fixed geometry? */
124 int depth
; /* bit depth */
125 int l
, t
; /* left and top offset */
126 int gm
; /* geometry mask */
131 char *primary
, *clipboard
;
132 struct timespec tclick1
;
133 struct timespec tclick2
;
152 /* Drawing Context */
156 Font font
, bfont
, ifont
, ibfont
;
160 static inline ushort
sixd_to_16bit(int);
161 static int xmakeglyphfontspecs(XftGlyphFontSpec
*, const Glyph
*, int, int, int);
162 static void xdrawglyphfontspecs(const XftGlyphFontSpec
*, Glyph
, int, int, int);
163 static void xdrawglyph(Glyph
, int, int);
164 static void xclear(int, int, int, int);
165 static int xgeommasktogravity(int);
166 static int ximopen(Display
*);
167 static void ximinstantiate(Display
*, XPointer
, XPointer
);
168 static void ximdestroy(XIM
, XPointer
, XPointer
);
169 static int xicdestroy(XIC
, XPointer
, XPointer
);
170 static void xinit(int, int);
171 static void cresize(int, int);
172 static void xresize(int, int);
173 static void xhints(void);
174 static int xloadcolor(int, const char *, Color
*);
175 static int xloadfont(Font
*, FcPattern
*);
176 static void xloadfonts(const char *, double);
177 static void xunloadfont(Font
*);
178 static void xunloadfonts(void);
179 static void xsetenv(void);
180 static void xseturgency(int);
181 static int evcol(XEvent
*);
182 static int evrow(XEvent
*);
184 static void expose(XEvent
*);
185 static void visibility(XEvent
*);
186 static void unmap(XEvent
*);
187 static void kpress(XEvent
*);
188 static void cmessage(XEvent
*);
189 static void resize(XEvent
*);
190 static void focus(XEvent
*);
191 static uint
buttonmask(uint
);
192 static int mouseaction(XEvent
*, uint
);
193 static void brelease(XEvent
*);
194 static void bpress(XEvent
*);
195 static void bmotion(XEvent
*);
196 static void propnotify(XEvent
*);
197 static void selnotify(XEvent
*);
198 static void selclear_(XEvent
*);
199 static void selrequest(XEvent
*);
200 static void setsel(char *, Time
);
201 static void mousesel(XEvent
*, int);
202 static void mousereport(XEvent
*);
203 static char *kmap(KeySym
, uint
);
204 static int match(uint
, uint
);
206 static void run(void);
207 static void usage(void);
209 static void (*handler
[LASTEvent
])(XEvent
*) = {
211 [ClientMessage
] = cmessage
,
212 [ConfigureNotify
] = resize
,
213 [VisibilityNotify
] = visibility
,
214 [UnmapNotify
] = unmap
,
218 [MotionNotify
] = bmotion
,
219 [ButtonPress
] = bpress
,
220 [ButtonRelease
] = brelease
,
222 * Uncomment if you want the selection to disappear when you select something
223 * different in another window.
225 /* [SelectionClear] = selclear_, */
226 [SelectionNotify
] = selnotify
,
228 * PropertyNotify is only turned on when there is some INCR transfer happening
229 * for the selection retrieval.
231 [PropertyNotify
] = propnotify
,
232 [SelectionRequest
] = selrequest
,
238 static XSelection xsel
;
239 static TermWindow win
;
241 /* Font Ring Cache */
255 /* Fontcache is an array now. A new font will be appended to the array. */
256 static Fontcache
*frc
= NULL
;
257 static int frclen
= 0;
258 static int frccap
= 0;
259 static char *usedfont
= NULL
;
260 static double usedfontsize
= 0;
261 static double defaultfontsize
= 0;
263 static char *opt_alpha
= NULL
;
264 static char *opt_class
= NULL
;
265 static char **opt_cmd
= NULL
;
266 static char *opt_embed
= NULL
;
267 static char *opt_font
= NULL
;
268 static char *opt_io
= NULL
;
269 static char *opt_line
= NULL
;
270 static char *opt_name
= NULL
;
271 static char *opt_title
= NULL
;
273 static uint buttons
; /* bit field of pressed buttons */
276 clipcopy(const Arg
*dummy
)
280 free(xsel
.clipboard
);
281 xsel
.clipboard
= NULL
;
283 if (xsel
.primary
!= NULL
) {
284 xsel
.clipboard
= xstrdup(xsel
.primary
);
285 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
286 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
291 clippaste(const Arg
*dummy
)
295 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
296 XConvertSelection(xw
.dpy
, clipboard
, xsel
.xtarget
, clipboard
,
297 xw
.win
, CurrentTime
);
301 selpaste(const Arg
*dummy
)
303 XConvertSelection(xw
.dpy
, XA_PRIMARY
, xsel
.xtarget
, XA_PRIMARY
,
304 xw
.win
, CurrentTime
);
308 numlock(const Arg
*dummy
)
310 win
.mode
^= MODE_NUMLOCK
;
318 larg
.f
= usedfontsize
+ arg
->f
;
323 zoomabs(const Arg
*arg
)
326 xloadfonts(usedfont
, arg
->f
);
333 zoomreset(const Arg
*arg
)
337 if (defaultfontsize
> 0) {
338 larg
.f
= defaultfontsize
;
344 ttysend(const Arg
*arg
)
346 ttywrite(arg
->s
, strlen(arg
->s
), 1);
352 int x
= e
->xbutton
.x
- win
.hborderpx
;
353 LIMIT(x
, 0, win
.tw
- 1);
360 int y
= e
->xbutton
.y
- win
.vborderpx
;
361 LIMIT(y
, 0, win
.th
- 1);
366 mousesel(XEvent
*e
, int done
)
368 int type
, seltype
= SEL_REGULAR
;
369 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forcemousemod
);
371 for (type
= 1; type
< LEN(selmasks
); ++type
) {
372 if (match(selmasks
[type
], state
)) {
377 selextend(evcol(e
), evrow(e
), seltype
, done
);
379 setsel(getsel(), e
->xbutton
.time
);
383 mousereport(XEvent
*e
)
386 int x
= evcol(e
), y
= evrow(e
);
387 int state
= e
->xbutton
.state
;
391 if (e
->type
== MotionNotify
) {
392 if (x
== ox
&& y
== oy
)
394 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
396 /* MODE_MOUSEMOTION: no reporting if no button is pressed */
397 if (IS_SET(MODE_MOUSEMOTION
) && buttons
== 0)
399 /* Set btn to lowest-numbered pressed button, or 12 if no
400 * buttons are pressed. */
401 for (btn
= 1; btn
<= 11 && !(buttons
& (1<<(btn
-1))); btn
++)
405 btn
= e
->xbutton
.button
;
406 /* Only buttons 1 through 11 can be encoded */
407 if (btn
< 1 || btn
> 11)
409 if (e
->type
== ButtonRelease
) {
410 /* MODE_MOUSEX10: no button release reporting */
411 if (IS_SET(MODE_MOUSEX10
))
413 /* Don't send release events for the scroll wheel */
414 if (btn
== 4 || btn
== 5)
423 /* Encode btn into code. If no button is pressed for a motion event in
424 * MODE_MOUSEMANY, then encode it as a release. */
425 if ((!IS_SET(MODE_MOUSESGR
) && e
->type
== ButtonRelease
) || btn
== 12)
428 code
+= 128 + btn
- 8;
430 code
+= 64 + btn
- 4;
434 if (!IS_SET(MODE_MOUSEX10
)) {
435 code
+= ((state
& ShiftMask
) ? 4 : 0)
436 + ((state
& Mod1Mask
) ? 8 : 0) /* meta key: alt */
437 + ((state
& ControlMask
) ? 16 : 0);
440 if (IS_SET(MODE_MOUSESGR
)) {
441 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
443 e
->type
== ButtonRelease
? 'm' : 'M');
444 } else if (x
< 223 && y
< 223) {
445 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
446 32+code
, 32+x
+1, 32+y
+1);
451 ttywrite(buf
, len
, 0);
455 buttonmask(uint button
)
457 return button
== Button1
? Button1Mask
458 : button
== Button2
? Button2Mask
459 : button
== Button3
? Button3Mask
460 : button
== Button4
? Button4Mask
461 : button
== Button5
? Button5Mask
466 mouseaction(XEvent
*e
, uint release
)
470 /* ignore Button<N>mask for Button<N> - it's set on release */
471 uint state
= e
->xbutton
.state
& ~buttonmask(e
->xbutton
.button
);
473 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
474 if (ms
->release
== release
&&
475 ms
->button
== e
->xbutton
.button
&&
476 (!ms
->altscrn
|| (ms
->altscrn
== (tisaltscr() ? 1 : -1))) &&
477 (match(ms
->mod
, state
) || /* exact or forced */
478 match(ms
->mod
, state
& ~forcemousemod
))) {
479 ms
->func(&(ms
->arg
));
490 int btn
= e
->xbutton
.button
;
494 if (1 <= btn
&& btn
<= 11)
495 buttons
|= 1 << (btn
-1);
497 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
502 if (mouseaction(e
, 0))
505 if (btn
== Button1
) {
507 * If the user clicks below predefined timeouts specific
508 * snapping behaviour is exposed.
510 clock_gettime(CLOCK_MONOTONIC
, &now
);
511 if (TIMEDIFF(now
, xsel
.tclick2
) <= tripleclicktimeout
) {
513 } else if (TIMEDIFF(now
, xsel
.tclick1
) <= doubleclicktimeout
) {
518 xsel
.tclick2
= xsel
.tclick1
;
521 selstart(evcol(e
), evrow(e
), snap
);
526 propnotify(XEvent
*e
)
528 XPropertyEvent
*xpev
;
529 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
531 xpev
= &e
->xproperty
;
532 if (xpev
->state
== PropertyNewValue
&&
533 (xpev
->atom
== XA_PRIMARY
||
534 xpev
->atom
== clipboard
)) {
542 ulong nitems
, ofs
, rem
;
544 uchar
*data
, *last
, *repl
;
545 Atom type
, incratom
, property
= None
;
547 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
550 if (e
->type
== SelectionNotify
)
551 property
= e
->xselection
.property
;
552 else if (e
->type
== PropertyNotify
)
553 property
= e
->xproperty
.atom
;
555 if (property
== None
)
559 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
560 BUFSIZ
/4, False
, AnyPropertyType
,
561 &type
, &format
, &nitems
, &rem
,
563 fprintf(stderr
, "Clipboard allocation failed\n");
567 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
569 * If there is some PropertyNotify with no data, then
570 * this is the signal of the selection owner that all
571 * data has been transferred. We won't need to receive
572 * PropertyNotify events anymore.
574 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
575 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
579 if (type
== incratom
) {
581 * Activate the PropertyNotify events so we receive
582 * when the selection owner does send us the next
585 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
586 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
590 * Deleting the property is the transfer start signal.
592 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
598 * Line endings are inconsistent in the terminal and GUI world
599 * copy and pasting. When receiving some selection data,
600 * replace all '\n' with '\r'.
601 * FIXME: Fix the computer world.
604 last
= data
+ nitems
* format
/ 8;
605 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
609 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
610 ttywrite("\033[200~", 6, 0);
611 ttywrite((char *)data
, nitems
* format
/ 8, 1);
612 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
613 ttywrite("\033[201~", 6, 0);
615 /* number of 32-bit chunks returned */
616 ofs
+= nitems
* format
/ 32;
620 * Deleting the property again tells the selection owner to send the
621 * next data chunk in the property.
623 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
639 selrequest(XEvent
*e
)
641 XSelectionRequestEvent
*xsre
;
643 Atom xa_targets
, string
, clipboard
;
646 xsre
= (XSelectionRequestEvent
*) e
;
647 xev
.type
= SelectionNotify
;
648 xev
.requestor
= xsre
->requestor
;
649 xev
.selection
= xsre
->selection
;
650 xev
.target
= xsre
->target
;
651 xev
.time
= xsre
->time
;
652 if (xsre
->property
== None
)
653 xsre
->property
= xsre
->target
;
658 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
659 if (xsre
->target
== xa_targets
) {
660 /* respond with the supported type */
661 string
= xsel
.xtarget
;
662 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
663 XA_ATOM
, 32, PropModeReplace
,
664 (uchar
*) &string
, 1);
665 xev
.property
= xsre
->property
;
666 } else if (xsre
->target
== xsel
.xtarget
|| xsre
->target
== XA_STRING
) {
668 * xith XA_STRING non ascii characters may be incorrect in the
669 * requestor. It is not our problem, use utf8.
671 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
672 if (xsre
->selection
== XA_PRIMARY
) {
673 seltext
= xsel
.primary
;
674 } else if (xsre
->selection
== clipboard
) {
675 seltext
= xsel
.clipboard
;
678 "Unhandled clipboard selection 0x%lx\n",
682 if (seltext
!= NULL
) {
683 XChangeProperty(xsre
->display
, xsre
->requestor
,
684 xsre
->property
, xsre
->target
,
686 (uchar
*)seltext
, strlen(seltext
));
687 xev
.property
= xsre
->property
;
691 /* all done, send a notification to the listener */
692 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
693 fprintf(stderr
, "Error sending SelectionNotify event\n");
697 setsel(char *str
, Time t
)
705 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
706 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
713 setsel(str
, CurrentTime
);
719 int btn
= e
->xbutton
.button
;
721 if (1 <= btn
&& btn
<= 11)
722 buttons
&= ~(1 << (btn
-1));
724 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
729 if (mouseaction(e
, 1))
738 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
747 cresize(int width
, int height
)
756 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
757 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
761 win
.hborderpx
= (win
.w
- col
* win
.cw
) / 2;
762 win
.vborderpx
= (win
.h
- row
* win
.ch
) / 2;
766 ttyresize(win
.tw
, win
.th
);
770 xresize(int col
, int row
)
772 win
.tw
= col
* win
.cw
;
773 win
.th
= row
* win
.ch
;
775 XFreePixmap(xw
.dpy
, xw
.buf
);
776 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
778 XftDrawChange(xw
.draw
, xw
.buf
);
779 xclear(0, 0, win
.w
, win
.h
);
781 /* resize to new width */
782 xw
.specbuf
= xrealloc(xw
.specbuf
, col
* sizeof(GlyphFontSpec
));
788 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
792 xloadcolor(int i
, const char *name
, Color
*ncolor
)
794 XRenderColor color
= { .alpha
= 0xffff };
797 if (BETWEEN(i
, 16, 255)) { /* 256 color */
798 if (i
< 6*6*6+16) { /* same colors as xterm */
799 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
800 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
801 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
802 } else { /* greyscale */
803 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
804 color
.green
= color
.blue
= color
.red
;
806 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
807 xw
.cmap
, &color
, ncolor
);
812 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
823 for (cp
= dc
.col
; cp
< &dc
.col
[dc
.collen
]; ++cp
)
824 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
826 dc
.collen
= MAX(LEN(colorname
), 256);
827 dc
.col
= xmalloc(dc
.collen
* sizeof(Color
));
830 for (i
= 0; i
< dc
.collen
; i
++)
831 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
833 die("could not allocate color '%s'\n", colorname
[i
]);
835 die("could not allocate color %d\n", i
);
838 /* set alpha value of bg color */
840 alpha
= strtof(opt_alpha
, NULL
);
841 dc
.col
[defaultbg
].color
.alpha
= (unsigned short)(0xffff * alpha
);
842 dc
.col
[defaultbg
].pixel
&= 0x00FFFFFF;
843 dc
.col
[defaultbg
].pixel
|= (unsigned char)(0xff * alpha
) << 24;
848 xgetcolor(int x
, unsigned char *r
, unsigned char *g
, unsigned char *b
)
850 if (!BETWEEN(x
, 0, dc
.collen
))
853 *r
= dc
.col
[x
].color
.red
>> 8;
854 *g
= dc
.col
[x
].color
.green
>> 8;
855 *b
= dc
.col
[x
].color
.blue
>> 8;
861 xsetcolorname(int x
, const char *name
)
865 if (!BETWEEN(x
, 0, dc
.collen
))
868 if (!xloadcolor(x
, name
, &ncolor
))
871 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
878 * Absolute coordinates.
881 xclear(int x1
, int y1
, int x2
, int y2
)
884 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
885 x1
, y1
, x2
-x1
, y2
-y1
);
891 XClassHint
class = {opt_name
? opt_name
: "st",
892 opt_class
? opt_class
: "St"};
893 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
896 sizeh
= XAllocSizeHints();
898 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
| PMinSize
;
899 sizeh
->height
= win
.h
;
900 sizeh
->width
= win
.w
;
901 sizeh
->height_inc
= 1;
902 sizeh
->width_inc
= 1;
903 sizeh
->base_height
= 2 * borderpx
;
904 sizeh
->base_width
= 2 * borderpx
;
905 sizeh
->min_height
= win
.ch
+ 2 * borderpx
;
906 sizeh
->min_width
= win
.cw
+ 2 * borderpx
;
908 sizeh
->flags
|= PMaxSize
;
909 sizeh
->min_width
= sizeh
->max_width
= win
.w
;
910 sizeh
->min_height
= sizeh
->max_height
= win
.h
;
912 if (xw
.gm
& (XValue
|YValue
)) {
913 sizeh
->flags
|= USPosition
| PWinGravity
;
916 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
919 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
925 xgeommasktogravity(int mask
)
927 switch (mask
& (XNegative
|YNegative
)) {
929 return NorthWestGravity
;
931 return NorthEastGravity
;
933 return SouthWestGravity
;
936 return SouthEastGravity
;
940 xloadfont(Font
*f
, FcPattern
*pattern
)
942 FcPattern
*configured
;
946 int wantattr
, haveattr
;
949 * Manually configure instead of calling XftMatchFont
950 * so that we can use the configured pattern for
951 * "missing glyph" lookups.
953 configured
= FcPatternDuplicate(pattern
);
957 FcConfigSubstitute(NULL
, configured
, FcMatchPattern
);
958 XftDefaultSubstitute(xw
.dpy
, xw
.scr
, configured
);
960 match
= FcFontMatch(NULL
, configured
, &result
);
962 FcPatternDestroy(configured
);
966 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
967 FcPatternDestroy(configured
);
968 FcPatternDestroy(match
);
972 if ((XftPatternGetInteger(pattern
, "slant", 0, &wantattr
) ==
975 * Check if xft was unable to find a font with the appropriate
976 * slant but gave us one anyway. Try to mitigate.
978 if ((XftPatternGetInteger(f
->match
->pattern
, "slant", 0,
979 &haveattr
) != XftResultMatch
) || haveattr
< wantattr
) {
981 fputs("font slant does not match\n", stderr
);
985 if ((XftPatternGetInteger(pattern
, "weight", 0, &wantattr
) ==
987 if ((XftPatternGetInteger(f
->match
->pattern
, "weight", 0,
988 &haveattr
) != XftResultMatch
) || haveattr
!= wantattr
) {
990 fputs("font weight does not match\n", stderr
);
994 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
995 (const FcChar8
*) ascii_printable
,
996 strlen(ascii_printable
), &extents
);
999 f
->pattern
= configured
;
1001 f
->ascent
= f
->match
->ascent
;
1002 f
->descent
= f
->match
->descent
;
1004 f
->rbearing
= f
->match
->max_advance_width
;
1006 f
->height
= f
->ascent
+ f
->descent
;
1007 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
1013 xloadfonts(const char *fontstr
, double fontsize
)
1018 if (fontstr
[0] == '-')
1019 pattern
= XftXlfdParse(fontstr
, False
, False
);
1021 pattern
= FcNameParse((const FcChar8
*)fontstr
);
1024 die("can't open font %s\n", fontstr
);
1027 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
1028 FcPatternDel(pattern
, FC_SIZE
);
1029 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
1030 usedfontsize
= fontsize
;
1032 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
1034 usedfontsize
= fontval
;
1035 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
1040 * Default font size is 12, if none given. This is to
1041 * have a known usedfontsize value.
1043 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
1046 defaultfontsize
= usedfontsize
;
1049 if (xloadfont(&dc
.font
, pattern
))
1050 die("can't open font %s\n", fontstr
);
1052 if (usedfontsize
< 0) {
1053 FcPatternGetDouble(dc
.font
.match
->pattern
,
1054 FC_PIXEL_SIZE
, 0, &fontval
);
1055 usedfontsize
= fontval
;
1057 defaultfontsize
= fontval
;
1060 /* Setting character width and height. */
1061 win
.cw
= ceilf(dc
.font
.width
* cwscale
);
1062 win
.ch
= ceilf(dc
.font
.height
* chscale
);
1064 FcPatternDel(pattern
, FC_SLANT
);
1065 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
1066 if (xloadfont(&dc
.ifont
, pattern
))
1067 die("can't open font %s\n", fontstr
);
1069 FcPatternDel(pattern
, FC_WEIGHT
);
1070 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
1071 if (xloadfont(&dc
.ibfont
, pattern
))
1072 die("can't open font %s\n", fontstr
);
1074 FcPatternDel(pattern
, FC_SLANT
);
1075 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
1076 if (xloadfont(&dc
.bfont
, pattern
))
1077 die("can't open font %s\n", fontstr
);
1079 FcPatternDestroy(pattern
);
1083 xunloadfont(Font
*f
)
1085 XftFontClose(xw
.dpy
, f
->match
);
1086 FcPatternDestroy(f
->pattern
);
1088 FcFontSetDestroy(f
->set
);
1094 /* Free the loaded fonts in the font cache. */
1096 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
1098 xunloadfont(&dc
.font
);
1099 xunloadfont(&dc
.bfont
);
1100 xunloadfont(&dc
.ifont
);
1101 xunloadfont(&dc
.ibfont
);
1105 ximopen(Display
*dpy
)
1107 XIMCallback imdestroy
= { .client_data
= NULL
, .callback
= ximdestroy
};
1108 XICCallback icdestroy
= { .client_data
= NULL
, .callback
= xicdestroy
};
1110 xw
.ime
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
1111 if (xw
.ime
.xim
== NULL
)
1114 if (XSetIMValues(xw
.ime
.xim
, XNDestroyCallback
, &imdestroy
, NULL
))
1115 fprintf(stderr
, "XSetIMValues: "
1116 "Could not set XNDestroyCallback.\n");
1118 xw
.ime
.spotlist
= XVaCreateNestedList(0, XNSpotLocation
, &xw
.ime
.spot
,
1121 if (xw
.ime
.xic
== NULL
) {
1122 xw
.ime
.xic
= XCreateIC(xw
.ime
.xim
, XNInputStyle
,
1123 XIMPreeditNothing
| XIMStatusNothing
,
1124 XNClientWindow
, xw
.win
,
1125 XNDestroyCallback
, &icdestroy
,
1128 if (xw
.ime
.xic
== NULL
)
1129 fprintf(stderr
, "XCreateIC: Could not create input context.\n");
1135 ximinstantiate(Display
*dpy
, XPointer client
, XPointer call
)
1138 XUnregisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1139 ximinstantiate
, NULL
);
1143 ximdestroy(XIM xim
, XPointer client
, XPointer call
)
1146 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1147 ximinstantiate
, NULL
);
1148 XFree(xw
.ime
.spotlist
);
1152 xicdestroy(XIC xim
, XPointer client
, XPointer call
)
1159 xinit(int cols
, int rows
)
1164 pid_t thispid
= getpid();
1165 XColor xmousefg
, xmousebg
;
1166 XWindowAttributes attr
;
1169 xw
.scr
= XDefaultScreen(xw
.dpy
);
1171 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0)))) {
1172 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
1175 XGetWindowAttributes(xw
.dpy
, parent
, &attr
);
1176 xw
.depth
= attr
.depth
;
1179 XMatchVisualInfo(xw
.dpy
, xw
.scr
, xw
.depth
, TrueColor
, &vis
);
1180 xw
.vis
= vis
.visual
;
1184 die("could not init fontconfig.\n");
1186 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
1187 xloadfonts(usedfont
, 0);
1190 xw
.cmap
= XCreateColormap(xw
.dpy
, parent
, xw
.vis
, None
);
1193 /* adjust fixed window geometry */
1194 win
.w
= 2 * win
.hborderpx
+ 2 * borderpx
+ cols
* win
.cw
;
1195 win
.h
= 2 * win
.vborderpx
+ 2 * borderpx
+ rows
* win
.ch
;
1196 if (xw
.gm
& XNegative
)
1197 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - win
.w
- 2;
1198 if (xw
.gm
& YNegative
)
1199 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - win
.h
- 2;
1202 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
1203 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
1204 xw
.attrs
.bit_gravity
= NorthWestGravity
;
1205 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
| KeyReleaseMask
1206 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1207 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1208 xw
.attrs
.colormap
= xw
.cmap
;
1210 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
1211 win
.w
, win
.h
, 0, xw
.depth
, InputOutput
,
1212 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
1213 | CWEventMask
| CWColormap
, &xw
.attrs
);
1215 memset(&gcvalues
, 0, sizeof(gcvalues
));
1216 gcvalues
.graphics_exposures
= False
;
1217 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
, xw
.depth
);
1218 dc
.gc
= XCreateGC(xw
.dpy
, xw
.buf
, GCGraphicsExposures
, &gcvalues
);
1219 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
1220 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, win
.w
, win
.h
);
1222 /* font spec buffer */
1223 xw
.specbuf
= xmalloc(cols
* sizeof(GlyphFontSpec
));
1225 /* Xft rendering context */
1226 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
1229 if (!ximopen(xw
.dpy
)) {
1230 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1231 ximinstantiate
, NULL
);
1234 /* white cursor, black outline */
1235 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
1236 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1238 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
1239 xmousefg
.red
= 0xffff;
1240 xmousefg
.green
= 0xffff;
1241 xmousefg
.blue
= 0xffff;
1244 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
1245 xmousebg
.red
= 0x0000;
1246 xmousebg
.green
= 0x0000;
1247 xmousebg
.blue
= 0x0000;
1250 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
1252 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1253 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
1254 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
1255 xw
.netwmiconname
= XInternAtom(xw
.dpy
, "_NET_WM_ICON_NAME", False
);
1256 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
1258 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
1259 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
1260 PropModeReplace
, (uchar
*)&thispid
, 1);
1262 win
.mode
= MODE_NUMLOCK
;
1265 XMapWindow(xw
.dpy
, xw
.win
);
1266 XSync(xw
.dpy
, False
);
1268 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick1
);
1269 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick2
);
1270 xsel
.primary
= NULL
;
1271 xsel
.clipboard
= NULL
;
1272 xsel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
1273 if (xsel
.xtarget
== None
)
1274 xsel
.xtarget
= XA_STRING
;
1278 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
1280 float winx
= win
.hborderpx
+ x
* win
.cw
, winy
= win
.vborderpx
+ y
* win
.ch
, xp
, yp
;
1281 ushort mode
, prevmode
= USHRT_MAX
;
1282 Font
*font
= &dc
.font
;
1283 int frcflags
= FRC_NORMAL
;
1284 float runewidth
= win
.cw
;
1288 FcPattern
*fcpattern
, *fontpattern
;
1289 FcFontSet
*fcsets
[] = { NULL
};
1290 FcCharSet
*fccharset
;
1291 int i
, f
, numspecs
= 0;
1293 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
1294 /* Fetch rune and mode for current glyph. */
1296 mode
= glyphs
[i
].mode
;
1298 /* Skip dummy wide-character spacing. */
1299 if (mode
== ATTR_WDUMMY
)
1302 /* Determine font for glyph if different from previous glyph. */
1303 if (prevmode
!= mode
) {
1306 frcflags
= FRC_NORMAL
;
1307 runewidth
= win
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
1308 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
1310 frcflags
= FRC_ITALICBOLD
;
1311 } else if (mode
& ATTR_ITALIC
) {
1313 frcflags
= FRC_ITALIC
;
1314 } else if (mode
& ATTR_BOLD
) {
1316 frcflags
= FRC_BOLD
;
1318 yp
= winy
+ font
->ascent
;
1321 /* Lookup character index with default font. */
1322 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
1324 specs
[numspecs
].font
= font
->match
;
1325 specs
[numspecs
].glyph
= glyphidx
;
1326 specs
[numspecs
].x
= (short)xp
;
1327 specs
[numspecs
].y
= (short)yp
;
1333 /* Fallback on font cache, search the font cache for match. */
1334 for (f
= 0; f
< frclen
; f
++) {
1335 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
1336 /* Everything correct. */
1337 if (glyphidx
&& frc
[f
].flags
== frcflags
)
1339 /* We got a default font for a not found glyph. */
1340 if (!glyphidx
&& frc
[f
].flags
== frcflags
1341 && frc
[f
].unicodep
== rune
) {
1346 /* Nothing was found. Use fontconfig to find matching font. */
1349 font
->set
= FcFontSort(0, font
->pattern
,
1351 fcsets
[0] = font
->set
;
1354 * Nothing was found in the cache. Now use
1355 * some dozen of Fontconfig calls to get the
1356 * font for one single character.
1358 * Xft and fontconfig are design failures.
1360 fcpattern
= FcPatternDuplicate(font
->pattern
);
1361 fccharset
= FcCharSetCreate();
1363 FcCharSetAddChar(fccharset
, rune
);
1364 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
1366 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
1368 FcConfigSubstitute(0, fcpattern
,
1370 FcDefaultSubstitute(fcpattern
);
1372 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
1375 /* Allocate memory for the new cache entry. */
1376 if (frclen
>= frccap
) {
1378 frc
= xrealloc(frc
, frccap
* sizeof(Fontcache
));
1381 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
1383 if (!frc
[frclen
].font
)
1384 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1386 frc
[frclen
].flags
= frcflags
;
1387 frc
[frclen
].unicodep
= rune
;
1389 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
1394 FcPatternDestroy(fcpattern
);
1395 FcCharSetDestroy(fccharset
);
1398 specs
[numspecs
].font
= frc
[f
].font
;
1399 specs
[numspecs
].glyph
= glyphidx
;
1400 specs
[numspecs
].x
= (short)xp
;
1401 specs
[numspecs
].y
= (short)yp
;
1410 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
1412 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
1413 int winx
= win
.hborderpx
+ x
* win
.cw
, winy
= win
.vborderpx
+ y
* win
.ch
,
1414 width
= charlen
* win
.cw
;
1415 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
1416 XRenderColor colfg
, colbg
;
1419 /* Fallback on color display for attributes not supported by the font */
1420 if (base
.mode
& ATTR_ITALIC
&& base
.mode
& ATTR_BOLD
) {
1421 if (dc
.ibfont
.badslant
|| dc
.ibfont
.badweight
)
1422 base
.fg
= defaultattr
;
1423 } else if ((base
.mode
& ATTR_ITALIC
&& dc
.ifont
.badslant
) ||
1424 (base
.mode
& ATTR_BOLD
&& dc
.bfont
.badweight
)) {
1425 base
.fg
= defaultattr
;
1428 if (IS_TRUECOL(base
.fg
)) {
1429 colfg
.alpha
= 0xffff;
1430 colfg
.red
= TRUERED(base
.fg
);
1431 colfg
.green
= TRUEGREEN(base
.fg
);
1432 colfg
.blue
= TRUEBLUE(base
.fg
);
1433 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
1436 fg
= &dc
.col
[base
.fg
];
1439 if (IS_TRUECOL(base
.bg
)) {
1440 colbg
.alpha
= 0xffff;
1441 colbg
.green
= TRUEGREEN(base
.bg
);
1442 colbg
.red
= TRUERED(base
.bg
);
1443 colbg
.blue
= TRUEBLUE(base
.bg
);
1444 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
1447 bg
= &dc
.col
[base
.bg
];
1450 /* Change basic system colors [0-7] to bright system colors [8-15] */
1451 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
1452 fg
= &dc
.col
[base
.fg
+ 8];
1454 if (IS_SET(MODE_REVERSE
)) {
1455 if (fg
== &dc
.col
[defaultfg
]) {
1456 fg
= &dc
.col
[defaultbg
];
1458 colfg
.red
= ~fg
->color
.red
;
1459 colfg
.green
= ~fg
->color
.green
;
1460 colfg
.blue
= ~fg
->color
.blue
;
1461 colfg
.alpha
= fg
->color
.alpha
;
1462 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
1467 if (bg
== &dc
.col
[defaultbg
]) {
1468 bg
= &dc
.col
[defaultfg
];
1470 colbg
.red
= ~bg
->color
.red
;
1471 colbg
.green
= ~bg
->color
.green
;
1472 colbg
.blue
= ~bg
->color
.blue
;
1473 colbg
.alpha
= bg
->color
.alpha
;
1474 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
1480 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
1481 colfg
.red
= fg
->color
.red
/ 2;
1482 colfg
.green
= fg
->color
.green
/ 2;
1483 colfg
.blue
= fg
->color
.blue
/ 2;
1484 colfg
.alpha
= fg
->color
.alpha
;
1485 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
1489 if (base
.mode
& ATTR_REVERSE
) {
1495 if (base
.mode
& ATTR_BLINK
&& win
.mode
& MODE_BLINK
)
1498 if (base
.mode
& ATTR_INVISIBLE
)
1501 /* Intelligent cleaning up of the borders. */
1503 xclear(0, (y
== 0)? 0 : winy
, win
.vborderpx
,
1505 ((winy
+ win
.ch
>= win
.vborderpx
+ win
.th
)? win
.h
: 0));
1507 if (winx
+ width
>= win
.hborderpx
+ win
.tw
) {
1508 xclear(winx
+ width
, (y
== 0)? 0 : winy
, win
.w
,
1509 ((winy
+ win
.ch
>= win
.vborderpx
+ win
.th
)? win
.h
: (winy
+ win
.ch
)));
1512 xclear(winx
, 0, winx
+ width
, win
.vborderpx
);
1513 if (winy
+ win
.ch
>= win
.vborderpx
+ win
.th
)
1514 xclear(winx
, winy
+ win
.ch
, winx
+ width
, win
.h
);
1516 /* Clean up the region we want to draw to. */
1517 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, win
.ch
);
1519 /* Set the clip region because Xft is sometimes dirty. */
1524 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
1526 /* Render the glyphs. */
1527 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
1529 /* Render underline and strikethrough. */
1530 if (base
.mode
& ATTR_UNDERLINE
) {
1531 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
* chscale
+ 1,
1535 if (base
.mode
& ATTR_STRUCK
) {
1536 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
* chscale
/ 3,
1540 /* Reset clip to none. */
1541 XftDrawSetClip(xw
.draw
, 0);
1545 xdrawglyph(Glyph g
, int x
, int y
)
1548 XftGlyphFontSpec spec
;
1550 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
1551 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
1555 xdrawcursor(int cx
, int cy
, Glyph g
, int ox
, int oy
, Glyph og
)
1559 /* remove the old cursor */
1560 if (selected(ox
, oy
))
1561 og
.mode
^= ATTR_REVERSE
;
1562 xdrawglyph(og
, ox
, oy
);
1564 if (IS_SET(MODE_HIDE
))
1568 * Select the right color for the right mode.
1570 g
.mode
&= ATTR_BOLD
|ATTR_ITALIC
|ATTR_UNDERLINE
|ATTR_STRUCK
|ATTR_WIDE
;
1572 if (IS_SET(MODE_REVERSE
)) {
1573 g
.mode
|= ATTR_REVERSE
;
1575 if (selected(cx
, cy
)) {
1576 drawcol
= dc
.col
[defaultcs
];
1579 drawcol
= dc
.col
[defaultrcs
];
1583 if (selected(cx
, cy
)) {
1590 drawcol
= dc
.col
[g
.bg
];
1593 /* draw the new one */
1594 if (IS_SET(MODE_FOCUSED
)) {
1595 switch (win
.cursor
) {
1596 case 7: /* st extension */
1597 g
.u
= 0x2603; /* snowman (U+2603) */
1599 case 0: /* Blinking Block */
1600 case 1: /* Blinking Block (Default) */
1601 case 2: /* Steady Block */
1602 xdrawglyph(g
, cx
, cy
);
1604 case 3: /* Blinking Underline */
1605 case 4: /* Steady Underline */
1606 XftDrawRect(xw
.draw
, &drawcol
,
1607 win
.hborderpx
+ cx
* win
.cw
,
1608 win
.vborderpx
+ (cy
+ 1) * win
.ch
- \
1610 win
.cw
, cursorthickness
);
1612 case 5: /* Blinking bar */
1613 case 6: /* Steady bar */
1614 XftDrawRect(xw
.draw
, &drawcol
,
1615 win
.hborderpx
+ cx
* win
.cw
,
1616 win
.vborderpx
+ cy
* win
.ch
,
1617 cursorthickness
, win
.ch
);
1621 XftDrawRect(xw
.draw
, &drawcol
,
1622 win
.hborderpx
+ cx
* win
.cw
,
1623 win
.vborderpx
+ cy
* win
.ch
,
1625 XftDrawRect(xw
.draw
, &drawcol
,
1626 win
.hborderpx
+ cx
* win
.cw
,
1627 win
.vborderpx
+ cy
* win
.ch
,
1629 XftDrawRect(xw
.draw
, &drawcol
,
1630 win
.hborderpx
+ (cx
+ 1) * win
.cw
- 1,
1631 win
.vborderpx
+ cy
* win
.ch
,
1633 XftDrawRect(xw
.draw
, &drawcol
,
1634 win
.hborderpx
+ cx
* win
.cw
,
1635 win
.vborderpx
+ (cy
+ 1) * win
.ch
- 1,
1643 char buf
[sizeof(long) * 8 + 1];
1645 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1646 setenv("WINDOWID", buf
, 1);
1650 xseticontitle(char *p
)
1653 DEFAULT(p
, opt_title
);
1655 if (Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1658 XSetWMIconName(xw
.dpy
, xw
.win
, &prop
);
1659 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmiconname
);
1667 DEFAULT(p
, opt_title
);
1669 if (Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1672 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
1673 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
1680 return IS_SET(MODE_VISIBLE
);
1684 xdrawline(Line line
, int x1
, int y1
, int x2
)
1686 int i
, x
, ox
, numspecs
;
1688 XftGlyphFontSpec
*specs
= xw
.specbuf
;
1690 numspecs
= xmakeglyphfontspecs(specs
, &line
[x1
], x2
- x1
, x1
, y1
);
1692 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
1694 if (new.mode
== ATTR_WDUMMY
)
1696 if (selected(x
, y1
))
1697 new.mode
^= ATTR_REVERSE
;
1698 if (i
> 0 && ATTRCMP(base
, new)) {
1699 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1711 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1717 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, win
.w
,
1719 XSetForeground(xw
.dpy
, dc
.gc
,
1720 dc
.col
[IS_SET(MODE_REVERSE
)?
1721 defaultfg
: defaultbg
].pixel
);
1725 xximspot(int x
, int y
)
1727 if (xw
.ime
.xic
== NULL
)
1730 xw
.ime
.spot
.x
= borderpx
+ x
* win
.cw
;
1731 xw
.ime
.spot
.y
= borderpx
+ (y
+ 1) * win
.ch
;
1733 XSetICValues(xw
.ime
.xic
, XNPreeditAttributes
, xw
.ime
.spotlist
, NULL
);
1743 visibility(XEvent
*ev
)
1745 XVisibilityEvent
*e
= &ev
->xvisibility
;
1747 MODBIT(win
.mode
, e
->state
!= VisibilityFullyObscured
, MODE_VISIBLE
);
1753 win
.mode
&= ~MODE_VISIBLE
;
1757 xsetpointermotion(int set
)
1759 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
1760 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
1764 xsetmode(int set
, unsigned int flags
)
1766 int mode
= win
.mode
;
1767 MODBIT(win
.mode
, set
, flags
);
1768 if ((win
.mode
& MODE_REVERSE
) != (mode
& MODE_REVERSE
))
1773 xsetcursor(int cursor
)
1775 if (!BETWEEN(cursor
, 0, 7)) /* 7: st extension */
1777 win
.cursor
= cursor
;
1782 xseturgency(int add
)
1784 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1786 MODBIT(h
->flags
, add
, XUrgencyHint
);
1787 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1794 if (!(IS_SET(MODE_FOCUSED
)))
1797 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
1803 XFocusChangeEvent
*e
= &ev
->xfocus
;
1805 if (e
->mode
== NotifyGrab
)
1808 if (ev
->type
== FocusIn
) {
1810 XSetICFocus(xw
.ime
.xic
);
1811 win
.mode
|= MODE_FOCUSED
;
1813 if (IS_SET(MODE_FOCUS
))
1814 ttywrite("\033[I", 3, 0);
1817 XUnsetICFocus(xw
.ime
.xic
);
1818 win
.mode
&= ~MODE_FOCUSED
;
1819 if (IS_SET(MODE_FOCUS
))
1820 ttywrite("\033[O", 3, 0);
1825 match(uint mask
, uint state
)
1827 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
1831 kmap(KeySym k
, uint state
)
1836 /* Check for mapped keys out of X11 function keys. */
1837 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
1838 if (mappedkeys
[i
] == k
)
1841 if (i
== LEN(mappedkeys
)) {
1842 if ((k
& 0xFFFF) < 0xFD00)
1846 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
1850 if (!match(kp
->mask
, state
))
1853 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
1855 if (IS_SET(MODE_NUMLOCK
) && kp
->appkey
== 2)
1858 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
1870 XKeyEvent
*e
= &ev
->xkey
;
1872 char buf
[64], *customkey
;
1878 if (IS_SET(MODE_KBDLOCK
))
1882 len
= XmbLookupString(xw
.ime
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
1884 len
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
1886 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
1887 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
1888 bp
->func(&(bp
->arg
));
1893 /* 2. custom keys from config.h */
1894 if ((customkey
= kmap(ksym
, e
->state
))) {
1895 ttywrite(customkey
, strlen(customkey
), 1);
1899 /* 3. composed string from input method */
1902 if (len
== 1 && e
->state
& Mod1Mask
) {
1903 if (IS_SET(MODE_8BIT
)) {
1906 len
= utf8encode(c
, buf
);
1914 ttywrite(buf
, len
, 1);
1922 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1924 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1925 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1926 win
.mode
|= MODE_FOCUSED
;
1928 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1929 win
.mode
&= ~MODE_FOCUSED
;
1931 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
1940 if (e
->xconfigure
.width
== win
.w
&& e
->xconfigure
.height
== win
.h
)
1943 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
1950 int w
= win
.w
, h
= win
.h
;
1952 int xfd
= XConnectionNumber(xw
.dpy
), ttyfd
, xev
, drawing
;
1953 struct timespec seltv
, *tv
, now
, lastblink
, trigger
;
1956 /* Waiting for window mapping */
1958 XNextEvent(xw
.dpy
, &ev
);
1960 * This XFilterEvent call is required because of XOpenIM. It
1961 * does filter out the key event and some client message for
1962 * the input method too.
1964 if (XFilterEvent(&ev
, None
))
1966 if (ev
.type
== ConfigureNotify
) {
1967 w
= ev
.xconfigure
.width
;
1968 h
= ev
.xconfigure
.height
;
1970 } while (ev
.type
!= MapNotify
);
1972 ttyfd
= ttynew(opt_line
, shell
, opt_io
, opt_cmd
);
1975 for (timeout
= -1, drawing
= 0, lastblink
= (struct timespec
){0};;) {
1977 FD_SET(ttyfd
, &rfd
);
1980 if (XPending(xw
.dpy
))
1981 timeout
= 0; /* existing events might not set xfd */
1983 seltv
.tv_sec
= timeout
/ 1E3
;
1984 seltv
.tv_nsec
= 1E6
* (timeout
- 1E3
* seltv
.tv_sec
);
1985 tv
= timeout
>= 0 ? &seltv
: NULL
;
1987 if (pselect(MAX(xfd
, ttyfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
1990 die("select failed: %s\n", strerror(errno
));
1992 clock_gettime(CLOCK_MONOTONIC
, &now
);
1994 if (FD_ISSET(ttyfd
, &rfd
))
1998 while (XPending(xw
.dpy
)) {
2000 XNextEvent(xw
.dpy
, &ev
);
2001 if (XFilterEvent(&ev
, None
))
2003 if (handler
[ev
.type
])
2004 (handler
[ev
.type
])(&ev
);
2008 * To reduce flicker and tearing, when new content or event
2009 * triggers drawing, we first wait a bit to ensure we got
2010 * everything, and if nothing new arrives - we draw.
2011 * We start with trying to wait minlatency ms. If more content
2012 * arrives sooner, we retry with shorter and shorter periods,
2013 * and eventually draw even without idle after maxlatency ms.
2014 * Typically this results in low latency while interacting,
2015 * maximum latency intervals during `cat huge.txt`, and perfect
2016 * sync with periodic updates from animations/key-repeats/etc.
2018 if (FD_ISSET(ttyfd
, &rfd
) || xev
) {
2023 timeout
= (maxlatency
- TIMEDIFF(now
, trigger
)) \
2024 / maxlatency
* minlatency
;
2026 continue; /* we have time, try to find idle */
2029 /* idle detected or maxlatency exhausted -> draw */
2031 if (blinktimeout
&& tattrset(ATTR_BLINK
)) {
2032 timeout
= blinktimeout
- TIMEDIFF(now
, lastblink
);
2034 if (-timeout
> blinktimeout
) /* start visible */
2035 win
.mode
|= MODE_BLINK
;
2036 win
.mode
^= MODE_BLINK
;
2037 tsetdirtattr(ATTR_BLINK
);
2039 timeout
= blinktimeout
;
2050 resource_load(XrmDatabase db
, char *name
, enum resource_type rtype
, void *dst
)
2057 char fullclass
[256];
2061 snprintf(fullname
, sizeof(fullname
), "%s.%s",
2062 opt_name
? opt_name
: "st", name
);
2063 snprintf(fullclass
, sizeof(fullclass
), "%s.%s",
2064 opt_class
? opt_class
: "St", name
);
2065 fullname
[sizeof(fullname
) - 1] = fullclass
[sizeof(fullclass
) - 1] = '\0';
2067 XrmGetResource(db
, fullname
, fullclass
, &type
, &ret
);
2068 if (ret
.addr
== NULL
|| strncmp("String", type
, 64))
2076 *idst
= strtoul(ret
.addr
, NULL
, 10);
2079 *fdst
= strtof(ret
.addr
, NULL
);
2093 resm
= XResourceManagerString(xw
.dpy
);
2097 db
= XrmGetStringDatabase(resm
);
2098 for (p
= resources
; p
< resources
+ LEN(resources
); p
++)
2099 resource_load(db
, p
->name
, p
->type
, p
->dst
);
2105 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
2106 " [-n name] [-o file]\n"
2107 " [-T title] [-t title] [-w windowid]"
2108 " [[-e] command [args ...]]\n"
2109 " %s [-aiv] [-c class] [-f font] [-g geometry]"
2110 " [-n name] [-o file]\n"
2111 " [-T title] [-t title] [-w windowid] -l line"
2112 " [stty_args ...]\n", argv0
, argv0
);
2116 main(int argc
, char *argv
[])
2120 xsetcursor(cursorshape
);
2127 opt_alpha
= EARGF(usage());
2130 opt_class
= EARGF(usage());
2137 opt_font
= EARGF(usage());
2140 xw
.gm
= XParseGeometry(EARGF(usage()),
2141 &xw
.l
, &xw
.t
, &cols
, &rows
);
2147 opt_io
= EARGF(usage());
2150 opt_line
= EARGF(usage());
2153 opt_name
= EARGF(usage());
2157 opt_title
= EARGF(usage());
2160 opt_embed
= EARGF(usage());
2163 die("%s " VERSION
"\n", argv0
);
2170 if (argc
> 0) /* eat all remaining arguments */
2174 opt_title
= (opt_line
|| !opt_cmd
) ? "st" : opt_cmd
[0];
2176 setlocale(LC_CTYPE
, "");
2177 XSetLocaleModifiers("");
2179 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2180 die("Can't open display\n");
2183 cols
= MAX(cols
, 1);
2184 rows
= MAX(rows
, 1);