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
*);
44 /* three-valued logic variables: 0 indifferent, 1 on, -1 off */
45 signed char appkey
; /* application keypad */
46 signed char appcursor
; /* application cursor */
49 /* Xresources preferences */
58 enum resource_type type
;
63 #define XK_ANY_MOD UINT_MAX
65 #define XK_SWITCH_MOD (1<<13|1<<14)
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
*);
77 /* config.h for applying patches and the configuration. */
81 #define XEMBED_FOCUS_IN 4
82 #define XEMBED_FOCUS_OUT 5
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)
90 typedef XftDraw
*Draw
;
91 typedef XftColor Color
;
92 typedef XftGlyphFontSpec GlyphFontSpec
;
94 /* Purely graphic info */
96 int tw
, th
; /* tty width and height */
97 int w
, h
; /* window width and height */
98 int hborderpx
, vborderpx
;
99 int ch
; /* char height */
100 int cw
; /* char width */
101 int mode
; /* window state/mode flags */
102 int cursor
; /* cursor style */
110 GlyphFontSpec
*specbuf
; /* font spec buffer used for rendering */
111 Atom xembed
, wmdeletewin
, netwmname
, netwmiconname
, netwmpid
;
116 XVaNestedList spotlist
;
120 XSetWindowAttributes attrs
;
122 int isfixed
; /* is fixed geometry? */
123 int depth
; /* bit depth */
124 int l
, t
; /* left and top offset */
125 int gm
; /* geometry mask */
130 char *primary
, *clipboard
;
131 struct timespec tclick1
;
132 struct timespec tclick2
;
151 /* Drawing Context */
155 Font font
, bfont
, ifont
, ibfont
;
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(const char *, double);
176 static void xunloadfont(Font
*);
177 static void xunloadfonts(void);
178 static void xsetenv(void);
179 static void xseturgency(int);
180 static int evcol(XEvent
*);
181 static int evrow(XEvent
*);
183 static void expose(XEvent
*);
184 static void visibility(XEvent
*);
185 static void unmap(XEvent
*);
186 static void kpress(XEvent
*);
187 static void cmessage(XEvent
*);
188 static void resize(XEvent
*);
189 static void focus(XEvent
*);
190 static uint
buttonmask(uint
);
191 static int mouseaction(XEvent
*, uint
);
192 static void brelease(XEvent
*);
193 static void bpress(XEvent
*);
194 static void bmotion(XEvent
*);
195 static void propnotify(XEvent
*);
196 static void selnotify(XEvent
*);
197 static void selclear_(XEvent
*);
198 static void selrequest(XEvent
*);
199 static void setsel(char *, Time
);
200 static void mousesel(XEvent
*, int);
201 static void mousereport(XEvent
*);
202 static char *kmap(KeySym
, uint
);
203 static int match(uint
, uint
);
205 static void run(void);
206 static void usage(void);
208 static void (*handler
[LASTEvent
])(XEvent
*) = {
210 [ClientMessage
] = cmessage
,
211 [ConfigureNotify
] = resize
,
212 [VisibilityNotify
] = visibility
,
213 [UnmapNotify
] = unmap
,
217 [MotionNotify
] = bmotion
,
218 [ButtonPress
] = bpress
,
219 [ButtonRelease
] = brelease
,
221 * Uncomment if you want the selection to disappear when you select something
222 * different in another window.
224 /* [SelectionClear] = selclear_, */
225 [SelectionNotify
] = selnotify
,
227 * PropertyNotify is only turned on when there is some INCR transfer happening
228 * for the selection retrieval.
230 [PropertyNotify
] = propnotify
,
231 [SelectionRequest
] = selrequest
,
237 static XSelection xsel
;
238 static TermWindow win
;
240 /* Font Ring Cache */
254 /* Fontcache is an array now. A new font will be appended to the array. */
255 static Fontcache
*frc
= NULL
;
256 static int frclen
= 0;
257 static int frccap
= 0;
258 static char *usedfont
= NULL
;
259 static double usedfontsize
= 0;
260 static double defaultfontsize
= 0;
262 static char *opt_alpha
= NULL
;
263 static char *opt_class
= NULL
;
264 static char **opt_cmd
= NULL
;
265 static char *opt_embed
= NULL
;
266 static char *opt_font
= NULL
;
267 static char *opt_io
= NULL
;
268 static char *opt_line
= NULL
;
269 static char *opt_name
= NULL
;
270 static char *opt_title
= NULL
;
272 static uint buttons
; /* bit field of pressed buttons */
275 clipcopy(const Arg
*dummy
)
279 free(xsel
.clipboard
);
280 xsel
.clipboard
= NULL
;
282 if (xsel
.primary
!= NULL
) {
283 xsel
.clipboard
= xstrdup(xsel
.primary
);
284 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
285 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
290 clippaste(const Arg
*dummy
)
294 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
295 XConvertSelection(xw
.dpy
, clipboard
, xsel
.xtarget
, clipboard
,
296 xw
.win
, CurrentTime
);
300 selpaste(const Arg
*dummy
)
302 XConvertSelection(xw
.dpy
, XA_PRIMARY
, xsel
.xtarget
, XA_PRIMARY
,
303 xw
.win
, CurrentTime
);
307 numlock(const Arg
*dummy
)
309 win
.mode
^= MODE_NUMLOCK
;
317 larg
.f
= usedfontsize
+ arg
->f
;
322 zoomabs(const Arg
*arg
)
325 xloadfonts(usedfont
, arg
->f
);
332 zoomreset(const Arg
*arg
)
336 if (defaultfontsize
> 0) {
337 larg
.f
= defaultfontsize
;
343 ttysend(const Arg
*arg
)
345 ttywrite(arg
->s
, strlen(arg
->s
), 1);
351 int x
= e
->xbutton
.x
- win
.hborderpx
;
352 LIMIT(x
, 0, win
.tw
- 1);
359 int y
= e
->xbutton
.y
- win
.vborderpx
;
360 LIMIT(y
, 0, win
.th
- 1);
365 mousesel(XEvent
*e
, int done
)
367 int type
, seltype
= SEL_REGULAR
;
368 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forcemousemod
);
370 for (type
= 1; type
< LEN(selmasks
); ++type
) {
371 if (match(selmasks
[type
], state
)) {
376 selextend(evcol(e
), evrow(e
), seltype
, done
);
378 setsel(getsel(), e
->xbutton
.time
);
382 mousereport(XEvent
*e
)
385 int x
= evcol(e
), y
= evrow(e
);
386 int state
= e
->xbutton
.state
;
390 if (e
->type
== MotionNotify
) {
391 if (x
== ox
&& y
== oy
)
393 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
395 /* MODE_MOUSEMOTION: no reporting if no button is pressed */
396 if (IS_SET(MODE_MOUSEMOTION
) && buttons
== 0)
398 /* Set btn to lowest-numbered pressed button, or 12 if no
399 * buttons are pressed. */
400 for (btn
= 1; btn
<= 11 && !(buttons
& (1<<(btn
-1))); btn
++)
404 btn
= e
->xbutton
.button
;
405 /* Only buttons 1 through 11 can be encoded */
406 if (btn
< 1 || btn
> 11)
408 if (e
->type
== ButtonRelease
) {
409 /* MODE_MOUSEX10: no button release reporting */
410 if (IS_SET(MODE_MOUSEX10
))
412 /* Don't send release events for the scroll wheel */
413 if (btn
== 4 || btn
== 5)
422 /* Encode btn into code. If no button is pressed for a motion event in
423 * MODE_MOUSEMANY, then encode it as a release. */
424 if ((!IS_SET(MODE_MOUSESGR
) && e
->type
== ButtonRelease
) || btn
== 12)
427 code
+= 128 + btn
- 8;
429 code
+= 64 + btn
- 4;
433 if (!IS_SET(MODE_MOUSEX10
)) {
434 code
+= ((state
& ShiftMask
) ? 4 : 0)
435 + ((state
& Mod1Mask
) ? 8 : 0) /* meta key: alt */
436 + ((state
& ControlMask
) ? 16 : 0);
439 if (IS_SET(MODE_MOUSESGR
)) {
440 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
442 e
->type
== ButtonRelease
? 'm' : 'M');
443 } else if (x
< 223 && y
< 223) {
444 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
445 32+code
, 32+x
+1, 32+y
+1);
450 ttywrite(buf
, len
, 0);
454 buttonmask(uint button
)
456 return button
== Button1
? Button1Mask
457 : button
== Button2
? Button2Mask
458 : button
== Button3
? Button3Mask
459 : button
== Button4
? Button4Mask
460 : button
== Button5
? Button5Mask
465 mouseaction(XEvent
*e
, uint release
)
469 /* ignore Button<N>mask for Button<N> - it's set on release */
470 uint state
= e
->xbutton
.state
& ~buttonmask(e
->xbutton
.button
);
472 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
473 if (ms
->release
== release
&&
474 ms
->button
== e
->xbutton
.button
&&
475 (match(ms
->mod
, state
) || /* exact or forced */
476 match(ms
->mod
, state
& ~forcemousemod
))) {
477 ms
->func(&(ms
->arg
));
488 int btn
= e
->xbutton
.button
;
492 if (1 <= btn
&& btn
<= 11)
493 buttons
|= 1 << (btn
-1);
495 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
500 if (mouseaction(e
, 0))
503 if (btn
== Button1
) {
505 * If the user clicks below predefined timeouts specific
506 * snapping behaviour is exposed.
508 clock_gettime(CLOCK_MONOTONIC
, &now
);
509 if (TIMEDIFF(now
, xsel
.tclick2
) <= tripleclicktimeout
) {
511 } else if (TIMEDIFF(now
, xsel
.tclick1
) <= doubleclicktimeout
) {
516 xsel
.tclick2
= xsel
.tclick1
;
519 selstart(evcol(e
), evrow(e
), snap
);
524 propnotify(XEvent
*e
)
526 XPropertyEvent
*xpev
;
527 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
529 xpev
= &e
->xproperty
;
530 if (xpev
->state
== PropertyNewValue
&&
531 (xpev
->atom
== XA_PRIMARY
||
532 xpev
->atom
== clipboard
)) {
540 ulong nitems
, ofs
, rem
;
542 uchar
*data
, *last
, *repl
;
543 Atom type
, incratom
, property
= None
;
545 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
548 if (e
->type
== SelectionNotify
)
549 property
= e
->xselection
.property
;
550 else if (e
->type
== PropertyNotify
)
551 property
= e
->xproperty
.atom
;
553 if (property
== None
)
557 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
558 BUFSIZ
/4, False
, AnyPropertyType
,
559 &type
, &format
, &nitems
, &rem
,
561 fprintf(stderr
, "Clipboard allocation failed\n");
565 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
567 * If there is some PropertyNotify with no data, then
568 * this is the signal of the selection owner that all
569 * data has been transferred. We won't need to receive
570 * PropertyNotify events anymore.
572 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
573 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
577 if (type
== incratom
) {
579 * Activate the PropertyNotify events so we receive
580 * when the selection owner does send us the next
583 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
584 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
588 * Deleting the property is the transfer start signal.
590 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
596 * Line endings are inconsistent in the terminal and GUI world
597 * copy and pasting. When receiving some selection data,
598 * replace all '\n' with '\r'.
599 * FIXME: Fix the computer world.
602 last
= data
+ nitems
* format
/ 8;
603 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
607 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
608 ttywrite("\033[200~", 6, 0);
609 ttywrite((char *)data
, nitems
* format
/ 8, 1);
610 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
611 ttywrite("\033[201~", 6, 0);
613 /* number of 32-bit chunks returned */
614 ofs
+= nitems
* format
/ 32;
618 * Deleting the property again tells the selection owner to send the
619 * next data chunk in the property.
621 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
637 selrequest(XEvent
*e
)
639 XSelectionRequestEvent
*xsre
;
641 Atom xa_targets
, string
, clipboard
;
644 xsre
= (XSelectionRequestEvent
*) e
;
645 xev
.type
= SelectionNotify
;
646 xev
.requestor
= xsre
->requestor
;
647 xev
.selection
= xsre
->selection
;
648 xev
.target
= xsre
->target
;
649 xev
.time
= xsre
->time
;
650 if (xsre
->property
== None
)
651 xsre
->property
= xsre
->target
;
656 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
657 if (xsre
->target
== xa_targets
) {
658 /* respond with the supported type */
659 string
= xsel
.xtarget
;
660 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
661 XA_ATOM
, 32, PropModeReplace
,
662 (uchar
*) &string
, 1);
663 xev
.property
= xsre
->property
;
664 } else if (xsre
->target
== xsel
.xtarget
|| xsre
->target
== XA_STRING
) {
666 * xith XA_STRING non ascii characters may be incorrect in the
667 * requestor. It is not our problem, use utf8.
669 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
670 if (xsre
->selection
== XA_PRIMARY
) {
671 seltext
= xsel
.primary
;
672 } else if (xsre
->selection
== clipboard
) {
673 seltext
= xsel
.clipboard
;
676 "Unhandled clipboard selection 0x%lx\n",
680 if (seltext
!= NULL
) {
681 XChangeProperty(xsre
->display
, xsre
->requestor
,
682 xsre
->property
, xsre
->target
,
684 (uchar
*)seltext
, strlen(seltext
));
685 xev
.property
= xsre
->property
;
689 /* all done, send a notification to the listener */
690 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
691 fprintf(stderr
, "Error sending SelectionNotify event\n");
695 setsel(char *str
, Time t
)
703 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
704 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
711 setsel(str
, CurrentTime
);
717 int btn
= e
->xbutton
.button
;
719 if (1 <= btn
&& btn
<= 11)
720 buttons
&= ~(1 << (btn
-1));
722 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
727 if (mouseaction(e
, 1))
736 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
745 cresize(int width
, int height
)
754 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
755 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
759 win
.hborderpx
= (win
.w
- col
* win
.cw
) / 2;
760 win
.vborderpx
= (win
.h
- row
* win
.ch
) / 2;
764 ttyresize(win
.tw
, win
.th
);
768 xresize(int col
, int row
)
770 win
.tw
= col
* win
.cw
;
771 win
.th
= row
* win
.ch
;
773 XFreePixmap(xw
.dpy
, xw
.buf
);
774 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
776 XftDrawChange(xw
.draw
, xw
.buf
);
777 xclear(0, 0, win
.w
, win
.h
);
779 /* resize to new width */
780 xw
.specbuf
= xrealloc(xw
.specbuf
, col
* sizeof(GlyphFontSpec
));
786 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
790 xloadcolor(int i
, const char *name
, Color
*ncolor
)
792 XRenderColor color
= { .alpha
= 0xffff };
795 if (BETWEEN(i
, 16, 255)) { /* 256 color */
796 if (i
< 6*6*6+16) { /* same colors as xterm */
797 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
798 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
799 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
800 } else { /* greyscale */
801 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
802 color
.green
= color
.blue
= color
.red
;
804 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
805 xw
.cmap
, &color
, ncolor
);
810 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
821 for (cp
= dc
.col
; cp
< &dc
.col
[dc
.collen
]; ++cp
)
822 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
824 dc
.collen
= MAX(LEN(colorname
), 256);
825 dc
.col
= xmalloc(dc
.collen
* sizeof(Color
));
828 for (i
= 0; i
< dc
.collen
; i
++)
829 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
831 die("could not allocate color '%s'\n", colorname
[i
]);
833 die("could not allocate color %d\n", i
);
836 /* set alpha value of bg color */
838 alpha
= strtof(opt_alpha
, NULL
);
839 dc
.col
[defaultbg
].color
.alpha
= (unsigned short)(0xffff * alpha
);
840 dc
.col
[defaultbg
].pixel
&= 0x00FFFFFF;
841 dc
.col
[defaultbg
].pixel
|= (unsigned char)(0xff * alpha
) << 24;
846 xgetcolor(int x
, unsigned char *r
, unsigned char *g
, unsigned char *b
)
848 if (!BETWEEN(x
, 0, dc
.collen
))
851 *r
= dc
.col
[x
].color
.red
>> 8;
852 *g
= dc
.col
[x
].color
.green
>> 8;
853 *b
= dc
.col
[x
].color
.blue
>> 8;
859 xsetcolorname(int x
, const char *name
)
863 if (!BETWEEN(x
, 0, dc
.collen
))
866 if (!xloadcolor(x
, name
, &ncolor
))
869 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
876 * Absolute coordinates.
879 xclear(int x1
, int y1
, int x2
, int y2
)
882 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
883 x1
, y1
, x2
-x1
, y2
-y1
);
889 XClassHint
class = {opt_name
? opt_name
: "st",
890 opt_class
? opt_class
: "St"};
891 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
894 sizeh
= XAllocSizeHints();
896 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
| PMinSize
;
897 sizeh
->height
= win
.h
;
898 sizeh
->width
= win
.w
;
899 sizeh
->height_inc
= 1;
900 sizeh
->width_inc
= 1;
901 sizeh
->base_height
= 2 * borderpx
;
902 sizeh
->base_width
= 2 * borderpx
;
903 sizeh
->min_height
= win
.ch
+ 2 * borderpx
;
904 sizeh
->min_width
= win
.cw
+ 2 * borderpx
;
906 sizeh
->flags
|= PMaxSize
;
907 sizeh
->min_width
= sizeh
->max_width
= win
.w
;
908 sizeh
->min_height
= sizeh
->max_height
= win
.h
;
910 if (xw
.gm
& (XValue
|YValue
)) {
911 sizeh
->flags
|= USPosition
| PWinGravity
;
914 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
917 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
923 xgeommasktogravity(int mask
)
925 switch (mask
& (XNegative
|YNegative
)) {
927 return NorthWestGravity
;
929 return NorthEastGravity
;
931 return SouthWestGravity
;
934 return SouthEastGravity
;
938 xloadfont(Font
*f
, FcPattern
*pattern
)
940 FcPattern
*configured
;
944 int wantattr
, haveattr
;
947 * Manually configure instead of calling XftMatchFont
948 * so that we can use the configured pattern for
949 * "missing glyph" lookups.
951 configured
= FcPatternDuplicate(pattern
);
955 FcConfigSubstitute(NULL
, configured
, FcMatchPattern
);
956 XftDefaultSubstitute(xw
.dpy
, xw
.scr
, configured
);
958 match
= FcFontMatch(NULL
, configured
, &result
);
960 FcPatternDestroy(configured
);
964 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
965 FcPatternDestroy(configured
);
966 FcPatternDestroy(match
);
970 if ((XftPatternGetInteger(pattern
, "slant", 0, &wantattr
) ==
973 * Check if xft was unable to find a font with the appropriate
974 * slant but gave us one anyway. Try to mitigate.
976 if ((XftPatternGetInteger(f
->match
->pattern
, "slant", 0,
977 &haveattr
) != XftResultMatch
) || haveattr
< wantattr
) {
979 fputs("font slant does not match\n", stderr
);
983 if ((XftPatternGetInteger(pattern
, "weight", 0, &wantattr
) ==
985 if ((XftPatternGetInteger(f
->match
->pattern
, "weight", 0,
986 &haveattr
) != XftResultMatch
) || haveattr
!= wantattr
) {
988 fputs("font weight does not match\n", stderr
);
992 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
993 (const FcChar8
*) ascii_printable
,
994 strlen(ascii_printable
), &extents
);
997 f
->pattern
= configured
;
999 f
->ascent
= f
->match
->ascent
;
1000 f
->descent
= f
->match
->descent
;
1002 f
->rbearing
= f
->match
->max_advance_width
;
1004 f
->height
= f
->ascent
+ f
->descent
;
1005 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
1011 xloadfonts(const char *fontstr
, double fontsize
)
1016 if (fontstr
[0] == '-')
1017 pattern
= XftXlfdParse(fontstr
, False
, False
);
1019 pattern
= FcNameParse((const FcChar8
*)fontstr
);
1022 die("can't open font %s\n", fontstr
);
1025 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
1026 FcPatternDel(pattern
, FC_SIZE
);
1027 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
1028 usedfontsize
= fontsize
;
1030 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
1032 usedfontsize
= fontval
;
1033 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
1038 * Default font size is 12, if none given. This is to
1039 * have a known usedfontsize value.
1041 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
1044 defaultfontsize
= usedfontsize
;
1047 if (xloadfont(&dc
.font
, pattern
))
1048 die("can't open font %s\n", fontstr
);
1050 if (usedfontsize
< 0) {
1051 FcPatternGetDouble(dc
.font
.match
->pattern
,
1052 FC_PIXEL_SIZE
, 0, &fontval
);
1053 usedfontsize
= fontval
;
1055 defaultfontsize
= fontval
;
1058 /* Setting character width and height. */
1059 win
.cw
= ceilf(dc
.font
.width
* cwscale
);
1060 win
.ch
= ceilf(dc
.font
.height
* chscale
);
1062 FcPatternDel(pattern
, FC_SLANT
);
1063 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
1064 if (xloadfont(&dc
.ifont
, pattern
))
1065 die("can't open font %s\n", fontstr
);
1067 FcPatternDel(pattern
, FC_WEIGHT
);
1068 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
1069 if (xloadfont(&dc
.ibfont
, pattern
))
1070 die("can't open font %s\n", fontstr
);
1072 FcPatternDel(pattern
, FC_SLANT
);
1073 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
1074 if (xloadfont(&dc
.bfont
, pattern
))
1075 die("can't open font %s\n", fontstr
);
1077 FcPatternDestroy(pattern
);
1081 xunloadfont(Font
*f
)
1083 XftFontClose(xw
.dpy
, f
->match
);
1084 FcPatternDestroy(f
->pattern
);
1086 FcFontSetDestroy(f
->set
);
1092 /* Free the loaded fonts in the font cache. */
1094 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
1096 xunloadfont(&dc
.font
);
1097 xunloadfont(&dc
.bfont
);
1098 xunloadfont(&dc
.ifont
);
1099 xunloadfont(&dc
.ibfont
);
1103 ximopen(Display
*dpy
)
1105 XIMCallback imdestroy
= { .client_data
= NULL
, .callback
= ximdestroy
};
1106 XICCallback icdestroy
= { .client_data
= NULL
, .callback
= xicdestroy
};
1108 xw
.ime
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
1109 if (xw
.ime
.xim
== NULL
)
1112 if (XSetIMValues(xw
.ime
.xim
, XNDestroyCallback
, &imdestroy
, NULL
))
1113 fprintf(stderr
, "XSetIMValues: "
1114 "Could not set XNDestroyCallback.\n");
1116 xw
.ime
.spotlist
= XVaCreateNestedList(0, XNSpotLocation
, &xw
.ime
.spot
,
1119 if (xw
.ime
.xic
== NULL
) {
1120 xw
.ime
.xic
= XCreateIC(xw
.ime
.xim
, XNInputStyle
,
1121 XIMPreeditNothing
| XIMStatusNothing
,
1122 XNClientWindow
, xw
.win
,
1123 XNDestroyCallback
, &icdestroy
,
1126 if (xw
.ime
.xic
== NULL
)
1127 fprintf(stderr
, "XCreateIC: Could not create input context.\n");
1133 ximinstantiate(Display
*dpy
, XPointer client
, XPointer call
)
1136 XUnregisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1137 ximinstantiate
, NULL
);
1141 ximdestroy(XIM xim
, XPointer client
, XPointer call
)
1144 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1145 ximinstantiate
, NULL
);
1146 XFree(xw
.ime
.spotlist
);
1150 xicdestroy(XIC xim
, XPointer client
, XPointer call
)
1157 xinit(int cols
, int rows
)
1162 pid_t thispid
= getpid();
1163 XColor xmousefg
, xmousebg
;
1164 XWindowAttributes attr
;
1167 xw
.scr
= XDefaultScreen(xw
.dpy
);
1169 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0)))) {
1170 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
1173 XGetWindowAttributes(xw
.dpy
, parent
, &attr
);
1174 xw
.depth
= attr
.depth
;
1177 XMatchVisualInfo(xw
.dpy
, xw
.scr
, xw
.depth
, TrueColor
, &vis
);
1178 xw
.vis
= vis
.visual
;
1182 die("could not init fontconfig.\n");
1184 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
1185 xloadfonts(usedfont
, 0);
1188 xw
.cmap
= XCreateColormap(xw
.dpy
, parent
, xw
.vis
, None
);
1191 /* adjust fixed window geometry */
1192 win
.w
= 2 * win
.hborderpx
+ 2 * borderpx
+ cols
* win
.cw
;
1193 win
.h
= 2 * win
.vborderpx
+ 2 * borderpx
+ rows
* win
.ch
;
1194 if (xw
.gm
& XNegative
)
1195 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - win
.w
- 2;
1196 if (xw
.gm
& YNegative
)
1197 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - win
.h
- 2;
1200 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
1201 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
1202 xw
.attrs
.bit_gravity
= NorthWestGravity
;
1203 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
| KeyReleaseMask
1204 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1205 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1206 xw
.attrs
.colormap
= xw
.cmap
;
1208 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
1209 win
.w
, win
.h
, 0, xw
.depth
, InputOutput
,
1210 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
1211 | CWEventMask
| CWColormap
, &xw
.attrs
);
1213 memset(&gcvalues
, 0, sizeof(gcvalues
));
1214 gcvalues
.graphics_exposures
= False
;
1215 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
, xw
.depth
);
1216 dc
.gc
= XCreateGC(xw
.dpy
, xw
.buf
, GCGraphicsExposures
, &gcvalues
);
1217 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
1218 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, win
.w
, win
.h
);
1220 /* font spec buffer */
1221 xw
.specbuf
= xmalloc(cols
* sizeof(GlyphFontSpec
));
1223 /* Xft rendering context */
1224 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
1227 if (!ximopen(xw
.dpy
)) {
1228 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1229 ximinstantiate
, NULL
);
1232 /* white cursor, black outline */
1233 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
1234 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1236 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
1237 xmousefg
.red
= 0xffff;
1238 xmousefg
.green
= 0xffff;
1239 xmousefg
.blue
= 0xffff;
1242 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
1243 xmousebg
.red
= 0x0000;
1244 xmousebg
.green
= 0x0000;
1245 xmousebg
.blue
= 0x0000;
1248 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
1250 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1251 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
1252 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
1253 xw
.netwmiconname
= XInternAtom(xw
.dpy
, "_NET_WM_ICON_NAME", False
);
1254 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
1256 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
1257 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
1258 PropModeReplace
, (uchar
*)&thispid
, 1);
1260 win
.mode
= MODE_NUMLOCK
;
1263 XMapWindow(xw
.dpy
, xw
.win
);
1264 XSync(xw
.dpy
, False
);
1266 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick1
);
1267 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick2
);
1268 xsel
.primary
= NULL
;
1269 xsel
.clipboard
= NULL
;
1270 xsel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
1271 if (xsel
.xtarget
== None
)
1272 xsel
.xtarget
= XA_STRING
;
1276 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
1278 float winx
= win
.hborderpx
+ x
* win
.cw
, winy
= win
.vborderpx
+ y
* win
.ch
, xp
, yp
;
1279 ushort mode
, prevmode
= USHRT_MAX
;
1280 Font
*font
= &dc
.font
;
1281 int frcflags
= FRC_NORMAL
;
1282 float runewidth
= win
.cw
;
1286 FcPattern
*fcpattern
, *fontpattern
;
1287 FcFontSet
*fcsets
[] = { NULL
};
1288 FcCharSet
*fccharset
;
1289 int i
, f
, numspecs
= 0;
1291 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
1292 /* Fetch rune and mode for current glyph. */
1294 mode
= glyphs
[i
].mode
;
1296 /* Skip dummy wide-character spacing. */
1297 if (mode
== ATTR_WDUMMY
)
1300 /* Determine font for glyph if different from previous glyph. */
1301 if (prevmode
!= mode
) {
1304 frcflags
= FRC_NORMAL
;
1305 runewidth
= win
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
1306 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
1308 frcflags
= FRC_ITALICBOLD
;
1309 } else if (mode
& ATTR_ITALIC
) {
1311 frcflags
= FRC_ITALIC
;
1312 } else if (mode
& ATTR_BOLD
) {
1314 frcflags
= FRC_BOLD
;
1316 yp
= winy
+ font
->ascent
;
1319 /* Lookup character index with default font. */
1320 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
1322 specs
[numspecs
].font
= font
->match
;
1323 specs
[numspecs
].glyph
= glyphidx
;
1324 specs
[numspecs
].x
= (short)xp
;
1325 specs
[numspecs
].y
= (short)yp
;
1331 /* Fallback on font cache, search the font cache for match. */
1332 for (f
= 0; f
< frclen
; f
++) {
1333 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
1334 /* Everything correct. */
1335 if (glyphidx
&& frc
[f
].flags
== frcflags
)
1337 /* We got a default font for a not found glyph. */
1338 if (!glyphidx
&& frc
[f
].flags
== frcflags
1339 && frc
[f
].unicodep
== rune
) {
1344 /* Nothing was found. Use fontconfig to find matching font. */
1347 font
->set
= FcFontSort(0, font
->pattern
,
1349 fcsets
[0] = font
->set
;
1352 * Nothing was found in the cache. Now use
1353 * some dozen of Fontconfig calls to get the
1354 * font for one single character.
1356 * Xft and fontconfig are design failures.
1358 fcpattern
= FcPatternDuplicate(font
->pattern
);
1359 fccharset
= FcCharSetCreate();
1361 FcCharSetAddChar(fccharset
, rune
);
1362 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
1364 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
1366 FcConfigSubstitute(0, fcpattern
,
1368 FcDefaultSubstitute(fcpattern
);
1370 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
1373 /* Allocate memory for the new cache entry. */
1374 if (frclen
>= frccap
) {
1376 frc
= xrealloc(frc
, frccap
* sizeof(Fontcache
));
1379 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
1381 if (!frc
[frclen
].font
)
1382 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1384 frc
[frclen
].flags
= frcflags
;
1385 frc
[frclen
].unicodep
= rune
;
1387 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
1392 FcPatternDestroy(fcpattern
);
1393 FcCharSetDestroy(fccharset
);
1396 specs
[numspecs
].font
= frc
[f
].font
;
1397 specs
[numspecs
].glyph
= glyphidx
;
1398 specs
[numspecs
].x
= (short)xp
;
1399 specs
[numspecs
].y
= (short)yp
;
1408 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
1410 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
1411 int winx
= win
.hborderpx
+ x
* win
.cw
, winy
= win
.vborderpx
+ y
* win
.ch
,
1412 width
= charlen
* win
.cw
;
1413 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
1414 XRenderColor colfg
, colbg
;
1417 /* Fallback on color display for attributes not supported by the font */
1418 if (base
.mode
& ATTR_ITALIC
&& base
.mode
& ATTR_BOLD
) {
1419 if (dc
.ibfont
.badslant
|| dc
.ibfont
.badweight
)
1420 base
.fg
= defaultattr
;
1421 } else if ((base
.mode
& ATTR_ITALIC
&& dc
.ifont
.badslant
) ||
1422 (base
.mode
& ATTR_BOLD
&& dc
.bfont
.badweight
)) {
1423 base
.fg
= defaultattr
;
1426 if (IS_TRUECOL(base
.fg
)) {
1427 colfg
.alpha
= 0xffff;
1428 colfg
.red
= TRUERED(base
.fg
);
1429 colfg
.green
= TRUEGREEN(base
.fg
);
1430 colfg
.blue
= TRUEBLUE(base
.fg
);
1431 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
1434 fg
= &dc
.col
[base
.fg
];
1437 if (IS_TRUECOL(base
.bg
)) {
1438 colbg
.alpha
= 0xffff;
1439 colbg
.green
= TRUEGREEN(base
.bg
);
1440 colbg
.red
= TRUERED(base
.bg
);
1441 colbg
.blue
= TRUEBLUE(base
.bg
);
1442 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
1445 bg
= &dc
.col
[base
.bg
];
1448 /* Change basic system colors [0-7] to bright system colors [8-15] */
1449 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
1450 fg
= &dc
.col
[base
.fg
+ 8];
1452 if (IS_SET(MODE_REVERSE
)) {
1453 if (fg
== &dc
.col
[defaultfg
]) {
1454 fg
= &dc
.col
[defaultbg
];
1456 colfg
.red
= ~fg
->color
.red
;
1457 colfg
.green
= ~fg
->color
.green
;
1458 colfg
.blue
= ~fg
->color
.blue
;
1459 colfg
.alpha
= fg
->color
.alpha
;
1460 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
1465 if (bg
== &dc
.col
[defaultbg
]) {
1466 bg
= &dc
.col
[defaultfg
];
1468 colbg
.red
= ~bg
->color
.red
;
1469 colbg
.green
= ~bg
->color
.green
;
1470 colbg
.blue
= ~bg
->color
.blue
;
1471 colbg
.alpha
= bg
->color
.alpha
;
1472 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
1478 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
1479 colfg
.red
= fg
->color
.red
/ 2;
1480 colfg
.green
= fg
->color
.green
/ 2;
1481 colfg
.blue
= fg
->color
.blue
/ 2;
1482 colfg
.alpha
= fg
->color
.alpha
;
1483 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
1487 if (base
.mode
& ATTR_REVERSE
) {
1493 if (base
.mode
& ATTR_BLINK
&& win
.mode
& MODE_BLINK
)
1496 if (base
.mode
& ATTR_INVISIBLE
)
1499 /* Intelligent cleaning up of the borders. */
1501 xclear(0, (y
== 0)? 0 : winy
, win
.vborderpx
,
1503 ((winy
+ win
.ch
>= win
.vborderpx
+ win
.th
)? win
.h
: 0));
1505 if (winx
+ width
>= win
.hborderpx
+ win
.tw
) {
1506 xclear(winx
+ width
, (y
== 0)? 0 : winy
, win
.w
,
1507 ((winy
+ win
.ch
>= win
.vborderpx
+ win
.th
)? win
.h
: (winy
+ win
.ch
)));
1510 xclear(winx
, 0, winx
+ width
, win
.vborderpx
);
1511 if (winy
+ win
.ch
>= win
.vborderpx
+ win
.th
)
1512 xclear(winx
, winy
+ win
.ch
, winx
+ width
, win
.h
);
1514 /* Clean up the region we want to draw to. */
1515 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, win
.ch
);
1517 /* Set the clip region because Xft is sometimes dirty. */
1522 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
1524 /* Render the glyphs. */
1525 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
1527 /* Render underline and strikethrough. */
1528 if (base
.mode
& ATTR_UNDERLINE
) {
1529 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
* chscale
+ 1,
1533 if (base
.mode
& ATTR_STRUCK
) {
1534 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
* chscale
/ 3,
1538 /* Reset clip to none. */
1539 XftDrawSetClip(xw
.draw
, 0);
1543 xdrawglyph(Glyph g
, int x
, int y
)
1546 XftGlyphFontSpec spec
;
1548 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
1549 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
1553 xdrawcursor(int cx
, int cy
, Glyph g
, int ox
, int oy
, Glyph og
)
1557 /* remove the old cursor */
1558 if (selected(ox
, oy
))
1559 og
.mode
^= ATTR_REVERSE
;
1560 xdrawglyph(og
, ox
, oy
);
1562 if (IS_SET(MODE_HIDE
))
1566 * Select the right color for the right mode.
1568 g
.mode
&= ATTR_BOLD
|ATTR_ITALIC
|ATTR_UNDERLINE
|ATTR_STRUCK
|ATTR_WIDE
;
1570 if (IS_SET(MODE_REVERSE
)) {
1571 g
.mode
|= ATTR_REVERSE
;
1573 if (selected(cx
, cy
)) {
1574 drawcol
= dc
.col
[defaultcs
];
1577 drawcol
= dc
.col
[defaultrcs
];
1581 if (selected(cx
, cy
)) {
1588 drawcol
= dc
.col
[g
.bg
];
1591 /* draw the new one */
1592 if (IS_SET(MODE_FOCUSED
)) {
1593 switch (win
.cursor
) {
1594 case 7: /* st extension */
1595 g
.u
= 0x2603; /* snowman (U+2603) */
1597 case 0: /* Blinking Block */
1598 case 1: /* Blinking Block (Default) */
1599 case 2: /* Steady Block */
1600 xdrawglyph(g
, cx
, cy
);
1602 case 3: /* Blinking Underline */
1603 case 4: /* Steady Underline */
1604 XftDrawRect(xw
.draw
, &drawcol
,
1605 win
.hborderpx
+ cx
* win
.cw
,
1606 win
.vborderpx
+ (cy
+ 1) * win
.ch
- \
1608 win
.cw
, cursorthickness
);
1610 case 5: /* Blinking bar */
1611 case 6: /* Steady bar */
1612 XftDrawRect(xw
.draw
, &drawcol
,
1613 win
.hborderpx
+ cx
* win
.cw
,
1614 win
.vborderpx
+ cy
* win
.ch
,
1615 cursorthickness
, win
.ch
);
1619 XftDrawRect(xw
.draw
, &drawcol
,
1620 win
.hborderpx
+ cx
* win
.cw
,
1621 win
.vborderpx
+ cy
* win
.ch
,
1623 XftDrawRect(xw
.draw
, &drawcol
,
1624 win
.hborderpx
+ cx
* win
.cw
,
1625 win
.vborderpx
+ cy
* win
.ch
,
1627 XftDrawRect(xw
.draw
, &drawcol
,
1628 win
.hborderpx
+ (cx
+ 1) * win
.cw
- 1,
1629 win
.vborderpx
+ cy
* win
.ch
,
1631 XftDrawRect(xw
.draw
, &drawcol
,
1632 win
.hborderpx
+ cx
* win
.cw
,
1633 win
.vborderpx
+ (cy
+ 1) * win
.ch
- 1,
1641 char buf
[sizeof(long) * 8 + 1];
1643 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1644 setenv("WINDOWID", buf
, 1);
1648 xseticontitle(char *p
)
1651 DEFAULT(p
, opt_title
);
1653 if (Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1656 XSetWMIconName(xw
.dpy
, xw
.win
, &prop
);
1657 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmiconname
);
1665 DEFAULT(p
, opt_title
);
1667 if (Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1670 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
1671 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
1678 return IS_SET(MODE_VISIBLE
);
1682 xdrawline(Line line
, int x1
, int y1
, int x2
)
1684 int i
, x
, ox
, numspecs
;
1686 XftGlyphFontSpec
*specs
= xw
.specbuf
;
1688 numspecs
= xmakeglyphfontspecs(specs
, &line
[x1
], x2
- x1
, x1
, y1
);
1690 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
1692 if (new.mode
== ATTR_WDUMMY
)
1694 if (selected(x
, y1
))
1695 new.mode
^= ATTR_REVERSE
;
1696 if (i
> 0 && ATTRCMP(base
, new)) {
1697 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1709 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1715 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, win
.w
,
1717 XSetForeground(xw
.dpy
, dc
.gc
,
1718 dc
.col
[IS_SET(MODE_REVERSE
)?
1719 defaultfg
: defaultbg
].pixel
);
1723 xximspot(int x
, int y
)
1725 if (xw
.ime
.xic
== NULL
)
1728 xw
.ime
.spot
.x
= borderpx
+ x
* win
.cw
;
1729 xw
.ime
.spot
.y
= borderpx
+ (y
+ 1) * win
.ch
;
1731 XSetICValues(xw
.ime
.xic
, XNPreeditAttributes
, xw
.ime
.spotlist
, NULL
);
1741 visibility(XEvent
*ev
)
1743 XVisibilityEvent
*e
= &ev
->xvisibility
;
1745 MODBIT(win
.mode
, e
->state
!= VisibilityFullyObscured
, MODE_VISIBLE
);
1751 win
.mode
&= ~MODE_VISIBLE
;
1755 xsetpointermotion(int set
)
1757 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
1758 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
1762 xsetmode(int set
, unsigned int flags
)
1764 int mode
= win
.mode
;
1765 MODBIT(win
.mode
, set
, flags
);
1766 if ((win
.mode
& MODE_REVERSE
) != (mode
& MODE_REVERSE
))
1771 xsetcursor(int cursor
)
1773 if (!BETWEEN(cursor
, 0, 7)) /* 7: st extension */
1775 win
.cursor
= cursor
;
1780 xseturgency(int add
)
1782 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1784 MODBIT(h
->flags
, add
, XUrgencyHint
);
1785 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1792 if (!(IS_SET(MODE_FOCUSED
)))
1795 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
1801 XFocusChangeEvent
*e
= &ev
->xfocus
;
1803 if (e
->mode
== NotifyGrab
)
1806 if (ev
->type
== FocusIn
) {
1808 XSetICFocus(xw
.ime
.xic
);
1809 win
.mode
|= MODE_FOCUSED
;
1811 if (IS_SET(MODE_FOCUS
))
1812 ttywrite("\033[I", 3, 0);
1815 XUnsetICFocus(xw
.ime
.xic
);
1816 win
.mode
&= ~MODE_FOCUSED
;
1817 if (IS_SET(MODE_FOCUS
))
1818 ttywrite("\033[O", 3, 0);
1823 match(uint mask
, uint state
)
1825 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
1829 kmap(KeySym k
, uint state
)
1834 /* Check for mapped keys out of X11 function keys. */
1835 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
1836 if (mappedkeys
[i
] == k
)
1839 if (i
== LEN(mappedkeys
)) {
1840 if ((k
& 0xFFFF) < 0xFD00)
1844 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
1848 if (!match(kp
->mask
, state
))
1851 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
1853 if (IS_SET(MODE_NUMLOCK
) && kp
->appkey
== 2)
1856 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
1868 XKeyEvent
*e
= &ev
->xkey
;
1870 char buf
[64], *customkey
;
1876 if (IS_SET(MODE_KBDLOCK
))
1880 len
= XmbLookupString(xw
.ime
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
1882 len
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
1884 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
1885 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
1886 bp
->func(&(bp
->arg
));
1891 /* 2. custom keys from config.h */
1892 if ((customkey
= kmap(ksym
, e
->state
))) {
1893 ttywrite(customkey
, strlen(customkey
), 1);
1897 /* 3. composed string from input method */
1900 if (len
== 1 && e
->state
& Mod1Mask
) {
1901 if (IS_SET(MODE_8BIT
)) {
1904 len
= utf8encode(c
, buf
);
1912 ttywrite(buf
, len
, 1);
1920 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1922 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1923 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1924 win
.mode
|= MODE_FOCUSED
;
1926 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1927 win
.mode
&= ~MODE_FOCUSED
;
1929 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
1938 if (e
->xconfigure
.width
== win
.w
&& e
->xconfigure
.height
== win
.h
)
1941 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
1948 int w
= win
.w
, h
= win
.h
;
1950 int xfd
= XConnectionNumber(xw
.dpy
), ttyfd
, xev
, drawing
;
1951 struct timespec seltv
, *tv
, now
, lastblink
, trigger
;
1954 /* Waiting for window mapping */
1956 XNextEvent(xw
.dpy
, &ev
);
1958 * This XFilterEvent call is required because of XOpenIM. It
1959 * does filter out the key event and some client message for
1960 * the input method too.
1962 if (XFilterEvent(&ev
, None
))
1964 if (ev
.type
== ConfigureNotify
) {
1965 w
= ev
.xconfigure
.width
;
1966 h
= ev
.xconfigure
.height
;
1968 } while (ev
.type
!= MapNotify
);
1970 ttyfd
= ttynew(opt_line
, shell
, opt_io
, opt_cmd
);
1973 for (timeout
= -1, drawing
= 0, lastblink
= (struct timespec
){0};;) {
1975 FD_SET(ttyfd
, &rfd
);
1978 if (XPending(xw
.dpy
))
1979 timeout
= 0; /* existing events might not set xfd */
1981 seltv
.tv_sec
= timeout
/ 1E3
;
1982 seltv
.tv_nsec
= 1E6
* (timeout
- 1E3
* seltv
.tv_sec
);
1983 tv
= timeout
>= 0 ? &seltv
: NULL
;
1985 if (pselect(MAX(xfd
, ttyfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
1988 die("select failed: %s\n", strerror(errno
));
1990 clock_gettime(CLOCK_MONOTONIC
, &now
);
1992 if (FD_ISSET(ttyfd
, &rfd
))
1996 while (XPending(xw
.dpy
)) {
1998 XNextEvent(xw
.dpy
, &ev
);
1999 if (XFilterEvent(&ev
, None
))
2001 if (handler
[ev
.type
])
2002 (handler
[ev
.type
])(&ev
);
2006 * To reduce flicker and tearing, when new content or event
2007 * triggers drawing, we first wait a bit to ensure we got
2008 * everything, and if nothing new arrives - we draw.
2009 * We start with trying to wait minlatency ms. If more content
2010 * arrives sooner, we retry with shorter and shorter periods,
2011 * and eventually draw even without idle after maxlatency ms.
2012 * Typically this results in low latency while interacting,
2013 * maximum latency intervals during `cat huge.txt`, and perfect
2014 * sync with periodic updates from animations/key-repeats/etc.
2016 if (FD_ISSET(ttyfd
, &rfd
) || xev
) {
2021 timeout
= (maxlatency
- TIMEDIFF(now
, trigger
)) \
2022 / maxlatency
* minlatency
;
2024 continue; /* we have time, try to find idle */
2027 /* idle detected or maxlatency exhausted -> draw */
2029 if (blinktimeout
&& tattrset(ATTR_BLINK
)) {
2030 timeout
= blinktimeout
- TIMEDIFF(now
, lastblink
);
2032 if (-timeout
> blinktimeout
) /* start visible */
2033 win
.mode
|= MODE_BLINK
;
2034 win
.mode
^= MODE_BLINK
;
2035 tsetdirtattr(ATTR_BLINK
);
2037 timeout
= blinktimeout
;
2048 resource_load(XrmDatabase db
, char *name
, enum resource_type rtype
, void *dst
)
2055 char fullclass
[256];
2059 snprintf(fullname
, sizeof(fullname
), "%s.%s",
2060 opt_name
? opt_name
: "st", name
);
2061 snprintf(fullclass
, sizeof(fullclass
), "%s.%s",
2062 opt_class
? opt_class
: "St", name
);
2063 fullname
[sizeof(fullname
) - 1] = fullclass
[sizeof(fullclass
) - 1] = '\0';
2065 XrmGetResource(db
, fullname
, fullclass
, &type
, &ret
);
2066 if (ret
.addr
== NULL
|| strncmp("String", type
, 64))
2074 *idst
= strtoul(ret
.addr
, NULL
, 10);
2077 *fdst
= strtof(ret
.addr
, NULL
);
2091 resm
= XResourceManagerString(xw
.dpy
);
2095 db
= XrmGetStringDatabase(resm
);
2096 for (p
= resources
; p
< resources
+ LEN(resources
); p
++)
2097 resource_load(db
, p
->name
, p
->type
, p
->dst
);
2103 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
2104 " [-n name] [-o file]\n"
2105 " [-T title] [-t title] [-w windowid]"
2106 " [[-e] command [args ...]]\n"
2107 " %s [-aiv] [-c class] [-f font] [-g geometry]"
2108 " [-n name] [-o file]\n"
2109 " [-T title] [-t title] [-w windowid] -l line"
2110 " [stty_args ...]\n", argv0
, argv0
);
2114 main(int argc
, char *argv
[])
2118 xsetcursor(cursorshape
);
2125 opt_alpha
= EARGF(usage());
2128 opt_class
= EARGF(usage());
2135 opt_font
= EARGF(usage());
2138 xw
.gm
= XParseGeometry(EARGF(usage()),
2139 &xw
.l
, &xw
.t
, &cols
, &rows
);
2145 opt_io
= EARGF(usage());
2148 opt_line
= EARGF(usage());
2151 opt_name
= EARGF(usage());
2155 opt_title
= EARGF(usage());
2158 opt_embed
= EARGF(usage());
2161 die("%s " VERSION
"\n", argv0
);
2168 if (argc
> 0) /* eat all remaining arguments */
2172 opt_title
= (opt_line
|| !opt_cmd
) ? "st" : opt_cmd
[0];
2174 setlocale(LC_CTYPE
, "");
2175 XSetLocaleModifiers("");
2177 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2178 die("Can't open display\n");
2181 cols
= MAX(cols
, 1);
2182 rows
= MAX(rows
, 1);