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>
23 /* types used in config.h */
27 void (*func
)(const Arg
*);
34 void (*func
)(const Arg
*);
43 /* three-valued logic variables: 0 indifferent, 1 on, -1 off */
44 signed char appkey
; /* application keypad */
45 signed char appcursor
; /* application cursor */
49 #define XK_ANY_MOD UINT_MAX
51 #define XK_SWITCH_MOD (1<<13|1<<14)
53 /* function definitions used in config.h */
54 static void clipcopy(const Arg
*);
55 static void clippaste(const Arg
*);
56 static void numlock(const Arg
*);
57 static void selpaste(const Arg
*);
58 static void zoom(const Arg
*);
59 static void zoomabs(const Arg
*);
60 static void zoomreset(const Arg
*);
61 static void ttysend(const Arg
*);
63 /* config.h for applying patches and the configuration. */
67 #define XEMBED_FOCUS_IN 4
68 #define XEMBED_FOCUS_OUT 5
71 #define IS_SET(flag) ((win.mode & (flag)) != 0)
72 #define TRUERED(x) (((x) & 0xff0000) >> 8)
73 #define TRUEGREEN(x) (((x) & 0xff00))
74 #define TRUEBLUE(x) (((x) & 0xff) << 8)
76 typedef XftDraw
*Draw
;
77 typedef XftColor Color
;
78 typedef XftGlyphFontSpec GlyphFontSpec
;
80 /* Purely graphic info */
82 int tw
, th
; /* tty width and height */
83 int w
, h
; /* window width and height */
84 int ch
; /* char height */
85 int cw
; /* char width */
86 int mode
; /* window state/mode flags */
87 int cursor
; /* cursor style */
95 GlyphFontSpec
*specbuf
; /* font spec buffer used for rendering */
96 Atom xembed
, wmdeletewin
, netwmname
, netwmiconname
, netwmpid
;
101 XVaNestedList spotlist
;
105 XSetWindowAttributes attrs
;
107 int isfixed
; /* is fixed geometry? */
108 int l
, t
; /* left and top offset */
109 int gm
; /* geometry mask */
114 char *primary
, *clipboard
;
115 struct timespec tclick1
;
116 struct timespec tclick2
;
135 /* Drawing Context */
139 Font font
, bfont
, ifont
, ibfont
;
143 static inline ushort
sixd_to_16bit(int);
144 static int xmakeglyphfontspecs(XftGlyphFontSpec
*, const Glyph
*, int, int, int);
145 static void xdrawglyphfontspecs(const XftGlyphFontSpec
*, Glyph
, int, int, int);
146 static void xdrawglyph(Glyph
, int, int);
147 static void xclear(int, int, int, int);
148 static int xgeommasktogravity(int);
149 static int ximopen(Display
*);
150 static void ximinstantiate(Display
*, XPointer
, XPointer
);
151 static void ximdestroy(XIM
, XPointer
, XPointer
);
152 static int xicdestroy(XIC
, XPointer
, XPointer
);
153 static void xinit(int, int);
154 static void cresize(int, int);
155 static void xresize(int, int);
156 static void xhints(void);
157 static int xloadcolor(int, const char *, Color
*);
158 static int xloadfont(Font
*, FcPattern
*);
159 static void xloadfonts(const char *, double);
160 static void xunloadfont(Font
*);
161 static void xunloadfonts(void);
162 static void xsetenv(void);
163 static void xseturgency(int);
164 static int evcol(XEvent
*);
165 static int evrow(XEvent
*);
167 static void expose(XEvent
*);
168 static void visibility(XEvent
*);
169 static void unmap(XEvent
*);
170 static void kpress(XEvent
*);
171 static void cmessage(XEvent
*);
172 static void resize(XEvent
*);
173 static void focus(XEvent
*);
174 static uint
buttonmask(uint
);
175 static int mouseaction(XEvent
*, uint
);
176 static void brelease(XEvent
*);
177 static void bpress(XEvent
*);
178 static void bmotion(XEvent
*);
179 static void propnotify(XEvent
*);
180 static void selnotify(XEvent
*);
181 static void selclear_(XEvent
*);
182 static void selrequest(XEvent
*);
183 static void setsel(char *, Time
);
184 static void mousesel(XEvent
*, int);
185 static void mousereport(XEvent
*);
186 static char *kmap(KeySym
, uint
);
187 static int match(uint
, uint
);
189 static void run(void);
190 static void usage(void);
192 static void (*handler
[LASTEvent
])(XEvent
*) = {
194 [ClientMessage
] = cmessage
,
195 [ConfigureNotify
] = resize
,
196 [VisibilityNotify
] = visibility
,
197 [UnmapNotify
] = unmap
,
201 [MotionNotify
] = bmotion
,
202 [ButtonPress
] = bpress
,
203 [ButtonRelease
] = brelease
,
205 * Uncomment if you want the selection to disappear when you select something
206 * different in another window.
208 /* [SelectionClear] = selclear_, */
209 [SelectionNotify
] = selnotify
,
211 * PropertyNotify is only turned on when there is some INCR transfer happening
212 * for the selection retrieval.
214 [PropertyNotify
] = propnotify
,
215 [SelectionRequest
] = selrequest
,
221 static XSelection xsel
;
222 static TermWindow win
;
224 /* Font Ring Cache */
238 /* Fontcache is an array now. A new font will be appended to the array. */
239 static Fontcache
*frc
= NULL
;
240 static int frclen
= 0;
241 static int frccap
= 0;
242 static char *usedfont
= NULL
;
243 static double usedfontsize
= 0;
244 static double defaultfontsize
= 0;
246 static char *opt_class
= NULL
;
247 static char **opt_cmd
= NULL
;
248 static char *opt_embed
= NULL
;
249 static char *opt_font
= NULL
;
250 static char *opt_io
= NULL
;
251 static char *opt_line
= NULL
;
252 static char *opt_name
= NULL
;
253 static char *opt_title
= NULL
;
255 static uint buttons
; /* bit field of pressed buttons */
258 clipcopy(const Arg
*dummy
)
262 free(xsel
.clipboard
);
263 xsel
.clipboard
= NULL
;
265 if (xsel
.primary
!= NULL
) {
266 xsel
.clipboard
= xstrdup(xsel
.primary
);
267 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
268 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
273 clippaste(const Arg
*dummy
)
277 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
278 XConvertSelection(xw
.dpy
, clipboard
, xsel
.xtarget
, clipboard
,
279 xw
.win
, CurrentTime
);
283 selpaste(const Arg
*dummy
)
285 XConvertSelection(xw
.dpy
, XA_PRIMARY
, xsel
.xtarget
, XA_PRIMARY
,
286 xw
.win
, CurrentTime
);
290 numlock(const Arg
*dummy
)
292 win
.mode
^= MODE_NUMLOCK
;
300 larg
.f
= usedfontsize
+ arg
->f
;
305 zoomabs(const Arg
*arg
)
308 xloadfonts(usedfont
, arg
->f
);
315 zoomreset(const Arg
*arg
)
319 if (defaultfontsize
> 0) {
320 larg
.f
= defaultfontsize
;
326 ttysend(const Arg
*arg
)
328 ttywrite(arg
->s
, strlen(arg
->s
), 1);
334 int x
= e
->xbutton
.x
- borderpx
;
335 LIMIT(x
, 0, win
.tw
- 1);
342 int y
= e
->xbutton
.y
- borderpx
;
343 LIMIT(y
, 0, win
.th
- 1);
348 mousesel(XEvent
*e
, int done
)
350 int type
, seltype
= SEL_REGULAR
;
351 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forcemousemod
);
353 for (type
= 1; type
< LEN(selmasks
); ++type
) {
354 if (match(selmasks
[type
], state
)) {
359 selextend(evcol(e
), evrow(e
), seltype
, done
);
361 setsel(getsel(), e
->xbutton
.time
);
365 mousereport(XEvent
*e
)
368 int x
= evcol(e
), y
= evrow(e
);
369 int state
= e
->xbutton
.state
;
373 if (e
->type
== MotionNotify
) {
374 if (x
== ox
&& y
== oy
)
376 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
378 /* MODE_MOUSEMOTION: no reporting if no button is pressed */
379 if (IS_SET(MODE_MOUSEMOTION
) && buttons
== 0)
381 /* Set btn to lowest-numbered pressed button, or 12 if no
382 * buttons are pressed. */
383 for (btn
= 1; btn
<= 11 && !(buttons
& (1<<(btn
-1))); btn
++)
387 btn
= e
->xbutton
.button
;
388 /* Only buttons 1 through 11 can be encoded */
389 if (btn
< 1 || btn
> 11)
391 if (e
->type
== ButtonRelease
) {
392 /* MODE_MOUSEX10: no button release reporting */
393 if (IS_SET(MODE_MOUSEX10
))
395 /* Don't send release events for the scroll wheel */
396 if (btn
== 4 || btn
== 5)
405 /* Encode btn into code. If no button is pressed for a motion event in
406 * MODE_MOUSEMANY, then encode it as a release. */
407 if ((!IS_SET(MODE_MOUSESGR
) && e
->type
== ButtonRelease
) || btn
== 12)
410 code
+= 128 + btn
- 8;
412 code
+= 64 + btn
- 4;
416 if (!IS_SET(MODE_MOUSEX10
)) {
417 code
+= ((state
& ShiftMask
) ? 4 : 0)
418 + ((state
& Mod1Mask
) ? 8 : 0) /* meta key: alt */
419 + ((state
& ControlMask
) ? 16 : 0);
422 if (IS_SET(MODE_MOUSESGR
)) {
423 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
425 e
->type
== ButtonRelease
? 'm' : 'M');
426 } else if (x
< 223 && y
< 223) {
427 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
428 32+code
, 32+x
+1, 32+y
+1);
433 ttywrite(buf
, len
, 0);
437 buttonmask(uint button
)
439 return button
== Button1
? Button1Mask
440 : button
== Button2
? Button2Mask
441 : button
== Button3
? Button3Mask
442 : button
== Button4
? Button4Mask
443 : button
== Button5
? Button5Mask
448 mouseaction(XEvent
*e
, uint release
)
452 /* ignore Button<N>mask for Button<N> - it's set on release */
453 uint state
= e
->xbutton
.state
& ~buttonmask(e
->xbutton
.button
);
455 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
456 if (ms
->release
== release
&&
457 ms
->button
== e
->xbutton
.button
&&
458 (match(ms
->mod
, state
) || /* exact or forced */
459 match(ms
->mod
, state
& ~forcemousemod
))) {
460 ms
->func(&(ms
->arg
));
471 int btn
= e
->xbutton
.button
;
475 if (1 <= btn
&& btn
<= 11)
476 buttons
|= 1 << (btn
-1);
478 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
483 if (mouseaction(e
, 0))
486 if (btn
== Button1
) {
488 * If the user clicks below predefined timeouts specific
489 * snapping behaviour is exposed.
491 clock_gettime(CLOCK_MONOTONIC
, &now
);
492 if (TIMEDIFF(now
, xsel
.tclick2
) <= tripleclicktimeout
) {
494 } else if (TIMEDIFF(now
, xsel
.tclick1
) <= doubleclicktimeout
) {
499 xsel
.tclick2
= xsel
.tclick1
;
502 selstart(evcol(e
), evrow(e
), snap
);
507 propnotify(XEvent
*e
)
509 XPropertyEvent
*xpev
;
510 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
512 xpev
= &e
->xproperty
;
513 if (xpev
->state
== PropertyNewValue
&&
514 (xpev
->atom
== XA_PRIMARY
||
515 xpev
->atom
== clipboard
)) {
523 ulong nitems
, ofs
, rem
;
525 uchar
*data
, *last
, *repl
;
526 Atom type
, incratom
, property
= None
;
528 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
531 if (e
->type
== SelectionNotify
)
532 property
= e
->xselection
.property
;
533 else if (e
->type
== PropertyNotify
)
534 property
= e
->xproperty
.atom
;
536 if (property
== None
)
540 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
541 BUFSIZ
/4, False
, AnyPropertyType
,
542 &type
, &format
, &nitems
, &rem
,
544 fprintf(stderr
, "Clipboard allocation failed\n");
548 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
550 * If there is some PropertyNotify with no data, then
551 * this is the signal of the selection owner that all
552 * data has been transferred. We won't need to receive
553 * PropertyNotify events anymore.
555 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
556 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
560 if (type
== incratom
) {
562 * Activate the PropertyNotify events so we receive
563 * when the selection owner does send us the next
566 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
567 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
571 * Deleting the property is the transfer start signal.
573 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
579 * Line endings are inconsistent in the terminal and GUI world
580 * copy and pasting. When receiving some selection data,
581 * replace all '\n' with '\r'.
582 * FIXME: Fix the computer world.
585 last
= data
+ nitems
* format
/ 8;
586 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
590 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
591 ttywrite("\033[200~", 6, 0);
592 ttywrite((char *)data
, nitems
* format
/ 8, 1);
593 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
594 ttywrite("\033[201~", 6, 0);
596 /* number of 32-bit chunks returned */
597 ofs
+= nitems
* format
/ 32;
601 * Deleting the property again tells the selection owner to send the
602 * next data chunk in the property.
604 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
620 selrequest(XEvent
*e
)
622 XSelectionRequestEvent
*xsre
;
624 Atom xa_targets
, string
, clipboard
;
627 xsre
= (XSelectionRequestEvent
*) e
;
628 xev
.type
= SelectionNotify
;
629 xev
.requestor
= xsre
->requestor
;
630 xev
.selection
= xsre
->selection
;
631 xev
.target
= xsre
->target
;
632 xev
.time
= xsre
->time
;
633 if (xsre
->property
== None
)
634 xsre
->property
= xsre
->target
;
639 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
640 if (xsre
->target
== xa_targets
) {
641 /* respond with the supported type */
642 string
= xsel
.xtarget
;
643 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
644 XA_ATOM
, 32, PropModeReplace
,
645 (uchar
*) &string
, 1);
646 xev
.property
= xsre
->property
;
647 } else if (xsre
->target
== xsel
.xtarget
|| xsre
->target
== XA_STRING
) {
649 * xith XA_STRING non ascii characters may be incorrect in the
650 * requestor. It is not our problem, use utf8.
652 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
653 if (xsre
->selection
== XA_PRIMARY
) {
654 seltext
= xsel
.primary
;
655 } else if (xsre
->selection
== clipboard
) {
656 seltext
= xsel
.clipboard
;
659 "Unhandled clipboard selection 0x%lx\n",
663 if (seltext
!= NULL
) {
664 XChangeProperty(xsre
->display
, xsre
->requestor
,
665 xsre
->property
, xsre
->target
,
667 (uchar
*)seltext
, strlen(seltext
));
668 xev
.property
= xsre
->property
;
672 /* all done, send a notification to the listener */
673 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
674 fprintf(stderr
, "Error sending SelectionNotify event\n");
678 setsel(char *str
, Time t
)
686 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
687 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
694 setsel(str
, CurrentTime
);
700 int btn
= e
->xbutton
.button
;
702 if (1 <= btn
&& btn
<= 11)
703 buttons
&= ~(1 << (btn
-1));
705 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
710 if (mouseaction(e
, 1))
719 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
728 cresize(int width
, int height
)
737 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
738 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
744 ttyresize(win
.tw
, win
.th
);
748 xresize(int col
, int row
)
750 win
.tw
= col
* win
.cw
;
751 win
.th
= row
* win
.ch
;
753 XFreePixmap(xw
.dpy
, xw
.buf
);
754 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
755 DefaultDepth(xw
.dpy
, xw
.scr
));
756 XftDrawChange(xw
.draw
, xw
.buf
);
757 xclear(0, 0, win
.w
, win
.h
);
759 /* resize to new width */
760 xw
.specbuf
= xrealloc(xw
.specbuf
, col
* sizeof(GlyphFontSpec
));
766 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
770 xloadcolor(int i
, const char *name
, Color
*ncolor
)
772 XRenderColor color
= { .alpha
= 0xffff };
775 if (BETWEEN(i
, 16, 255)) { /* 256 color */
776 if (i
< 6*6*6+16) { /* same colors as xterm */
777 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
778 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
779 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
780 } else { /* greyscale */
781 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
782 color
.green
= color
.blue
= color
.red
;
784 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
785 xw
.cmap
, &color
, ncolor
);
790 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
801 for (cp
= dc
.col
; cp
< &dc
.col
[dc
.collen
]; ++cp
)
802 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
804 dc
.collen
= MAX(LEN(colorname
), 256);
805 dc
.col
= xmalloc(dc
.collen
* sizeof(Color
));
808 for (i
= 0; i
< dc
.collen
; i
++)
809 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
811 die("could not allocate color '%s'\n", colorname
[i
]);
813 die("could not allocate color %d\n", i
);
819 xgetcolor(int x
, unsigned char *r
, unsigned char *g
, unsigned char *b
)
821 if (!BETWEEN(x
, 0, dc
.collen
))
824 *r
= dc
.col
[x
].color
.red
>> 8;
825 *g
= dc
.col
[x
].color
.green
>> 8;
826 *b
= dc
.col
[x
].color
.blue
>> 8;
832 xsetcolorname(int x
, const char *name
)
836 if (!BETWEEN(x
, 0, dc
.collen
))
839 if (!xloadcolor(x
, name
, &ncolor
))
842 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
849 * Absolute coordinates.
852 xclear(int x1
, int y1
, int x2
, int y2
)
855 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
856 x1
, y1
, x2
-x1
, y2
-y1
);
862 XClassHint
class = {opt_name
? opt_name
: termname
,
863 opt_class
? opt_class
: termname
};
864 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
867 sizeh
= XAllocSizeHints();
869 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
| PMinSize
;
870 sizeh
->height
= win
.h
;
871 sizeh
->width
= win
.w
;
872 sizeh
->height_inc
= win
.ch
;
873 sizeh
->width_inc
= win
.cw
;
874 sizeh
->base_height
= 2 * borderpx
;
875 sizeh
->base_width
= 2 * borderpx
;
876 sizeh
->min_height
= win
.ch
+ 2 * borderpx
;
877 sizeh
->min_width
= win
.cw
+ 2 * borderpx
;
879 sizeh
->flags
|= PMaxSize
;
880 sizeh
->min_width
= sizeh
->max_width
= win
.w
;
881 sizeh
->min_height
= sizeh
->max_height
= win
.h
;
883 if (xw
.gm
& (XValue
|YValue
)) {
884 sizeh
->flags
|= USPosition
| PWinGravity
;
887 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
890 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
896 xgeommasktogravity(int mask
)
898 switch (mask
& (XNegative
|YNegative
)) {
900 return NorthWestGravity
;
902 return NorthEastGravity
;
904 return SouthWestGravity
;
907 return SouthEastGravity
;
911 xloadfont(Font
*f
, FcPattern
*pattern
)
913 FcPattern
*configured
;
917 int wantattr
, haveattr
;
920 * Manually configure instead of calling XftMatchFont
921 * so that we can use the configured pattern for
922 * "missing glyph" lookups.
924 configured
= FcPatternDuplicate(pattern
);
928 FcConfigSubstitute(NULL
, configured
, FcMatchPattern
);
929 XftDefaultSubstitute(xw
.dpy
, xw
.scr
, configured
);
931 match
= FcFontMatch(NULL
, configured
, &result
);
933 FcPatternDestroy(configured
);
937 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
938 FcPatternDestroy(configured
);
939 FcPatternDestroy(match
);
943 if ((XftPatternGetInteger(pattern
, "slant", 0, &wantattr
) ==
946 * Check if xft was unable to find a font with the appropriate
947 * slant but gave us one anyway. Try to mitigate.
949 if ((XftPatternGetInteger(f
->match
->pattern
, "slant", 0,
950 &haveattr
) != XftResultMatch
) || haveattr
< wantattr
) {
952 fputs("font slant does not match\n", stderr
);
956 if ((XftPatternGetInteger(pattern
, "weight", 0, &wantattr
) ==
958 if ((XftPatternGetInteger(f
->match
->pattern
, "weight", 0,
959 &haveattr
) != XftResultMatch
) || haveattr
!= wantattr
) {
961 fputs("font weight does not match\n", stderr
);
965 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
966 (const FcChar8
*) ascii_printable
,
967 strlen(ascii_printable
), &extents
);
970 f
->pattern
= configured
;
972 f
->ascent
= f
->match
->ascent
;
973 f
->descent
= f
->match
->descent
;
975 f
->rbearing
= f
->match
->max_advance_width
;
977 f
->height
= f
->ascent
+ f
->descent
;
978 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
984 xloadfonts(const char *fontstr
, double fontsize
)
989 if (fontstr
[0] == '-')
990 pattern
= XftXlfdParse(fontstr
, False
, False
);
992 pattern
= FcNameParse((const FcChar8
*)fontstr
);
995 die("can't open font %s\n", fontstr
);
998 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
999 FcPatternDel(pattern
, FC_SIZE
);
1000 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
1001 usedfontsize
= fontsize
;
1003 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
1005 usedfontsize
= fontval
;
1006 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
1011 * Default font size is 12, if none given. This is to
1012 * have a known usedfontsize value.
1014 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
1017 defaultfontsize
= usedfontsize
;
1020 if (xloadfont(&dc
.font
, pattern
))
1021 die("can't open font %s\n", fontstr
);
1023 if (usedfontsize
< 0) {
1024 FcPatternGetDouble(dc
.font
.match
->pattern
,
1025 FC_PIXEL_SIZE
, 0, &fontval
);
1026 usedfontsize
= fontval
;
1028 defaultfontsize
= fontval
;
1031 /* Setting character width and height. */
1032 win
.cw
= ceilf(dc
.font
.width
* cwscale
);
1033 win
.ch
= ceilf(dc
.font
.height
* chscale
);
1035 FcPatternDel(pattern
, FC_SLANT
);
1036 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
1037 if (xloadfont(&dc
.ifont
, pattern
))
1038 die("can't open font %s\n", fontstr
);
1040 FcPatternDel(pattern
, FC_WEIGHT
);
1041 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
1042 if (xloadfont(&dc
.ibfont
, pattern
))
1043 die("can't open font %s\n", fontstr
);
1045 FcPatternDel(pattern
, FC_SLANT
);
1046 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
1047 if (xloadfont(&dc
.bfont
, pattern
))
1048 die("can't open font %s\n", fontstr
);
1050 FcPatternDestroy(pattern
);
1054 xunloadfont(Font
*f
)
1056 XftFontClose(xw
.dpy
, f
->match
);
1057 FcPatternDestroy(f
->pattern
);
1059 FcFontSetDestroy(f
->set
);
1065 /* Free the loaded fonts in the font cache. */
1067 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
1069 xunloadfont(&dc
.font
);
1070 xunloadfont(&dc
.bfont
);
1071 xunloadfont(&dc
.ifont
);
1072 xunloadfont(&dc
.ibfont
);
1076 ximopen(Display
*dpy
)
1078 XIMCallback imdestroy
= { .client_data
= NULL
, .callback
= ximdestroy
};
1079 XICCallback icdestroy
= { .client_data
= NULL
, .callback
= xicdestroy
};
1081 xw
.ime
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
1082 if (xw
.ime
.xim
== NULL
)
1085 if (XSetIMValues(xw
.ime
.xim
, XNDestroyCallback
, &imdestroy
, NULL
))
1086 fprintf(stderr
, "XSetIMValues: "
1087 "Could not set XNDestroyCallback.\n");
1089 xw
.ime
.spotlist
= XVaCreateNestedList(0, XNSpotLocation
, &xw
.ime
.spot
,
1092 if (xw
.ime
.xic
== NULL
) {
1093 xw
.ime
.xic
= XCreateIC(xw
.ime
.xim
, XNInputStyle
,
1094 XIMPreeditNothing
| XIMStatusNothing
,
1095 XNClientWindow
, xw
.win
,
1096 XNDestroyCallback
, &icdestroy
,
1099 if (xw
.ime
.xic
== NULL
)
1100 fprintf(stderr
, "XCreateIC: Could not create input context.\n");
1106 ximinstantiate(Display
*dpy
, XPointer client
, XPointer call
)
1109 XUnregisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1110 ximinstantiate
, NULL
);
1114 ximdestroy(XIM xim
, XPointer client
, XPointer call
)
1117 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1118 ximinstantiate
, NULL
);
1119 XFree(xw
.ime
.spotlist
);
1123 xicdestroy(XIC xim
, XPointer client
, XPointer call
)
1130 xinit(int cols
, int rows
)
1135 pid_t thispid
= getpid();
1136 XColor xmousefg
, xmousebg
;
1138 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
1139 die("can't open display\n");
1140 xw
.scr
= XDefaultScreen(xw
.dpy
);
1141 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
1145 die("could not init fontconfig.\n");
1147 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
1148 xloadfonts(usedfont
, 0);
1151 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1154 /* adjust fixed window geometry */
1155 win
.w
= 2 * borderpx
+ cols
* win
.cw
;
1156 win
.h
= 2 * borderpx
+ rows
* win
.ch
;
1157 if (xw
.gm
& XNegative
)
1158 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - win
.w
- 2;
1159 if (xw
.gm
& YNegative
)
1160 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - win
.h
- 2;
1163 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
1164 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
1165 xw
.attrs
.bit_gravity
= NorthWestGravity
;
1166 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
| KeyReleaseMask
1167 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1168 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1169 xw
.attrs
.colormap
= xw
.cmap
;
1171 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
1172 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
1173 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
1174 win
.w
, win
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1175 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
1176 | CWEventMask
| CWColormap
, &xw
.attrs
);
1178 memset(&gcvalues
, 0, sizeof(gcvalues
));
1179 gcvalues
.graphics_exposures
= False
;
1180 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
1182 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
1183 DefaultDepth(xw
.dpy
, xw
.scr
));
1184 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
1185 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, win
.w
, win
.h
);
1187 /* font spec buffer */
1188 xw
.specbuf
= xmalloc(cols
* sizeof(GlyphFontSpec
));
1190 /* Xft rendering context */
1191 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
1194 if (!ximopen(xw
.dpy
)) {
1195 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1196 ximinstantiate
, NULL
);
1199 /* white cursor, black outline */
1200 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
1201 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1203 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
1204 xmousefg
.red
= 0xffff;
1205 xmousefg
.green
= 0xffff;
1206 xmousefg
.blue
= 0xffff;
1209 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
1210 xmousebg
.red
= 0x0000;
1211 xmousebg
.green
= 0x0000;
1212 xmousebg
.blue
= 0x0000;
1215 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
1217 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1218 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
1219 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
1220 xw
.netwmiconname
= XInternAtom(xw
.dpy
, "_NET_WM_ICON_NAME", False
);
1221 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
1223 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
1224 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
1225 PropModeReplace
, (uchar
*)&thispid
, 1);
1227 win
.mode
= MODE_NUMLOCK
;
1230 XMapWindow(xw
.dpy
, xw
.win
);
1231 XSync(xw
.dpy
, False
);
1233 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick1
);
1234 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick2
);
1235 xsel
.primary
= NULL
;
1236 xsel
.clipboard
= NULL
;
1237 xsel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
1238 if (xsel
.xtarget
== None
)
1239 xsel
.xtarget
= XA_STRING
;
1243 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
1245 float winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
, xp
, yp
;
1246 ushort mode
, prevmode
= USHRT_MAX
;
1247 Font
*font
= &dc
.font
;
1248 int frcflags
= FRC_NORMAL
;
1249 float runewidth
= win
.cw
;
1253 FcPattern
*fcpattern
, *fontpattern
;
1254 FcFontSet
*fcsets
[] = { NULL
};
1255 FcCharSet
*fccharset
;
1256 int i
, f
, numspecs
= 0;
1258 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
1259 /* Fetch rune and mode for current glyph. */
1261 mode
= glyphs
[i
].mode
;
1263 /* Skip dummy wide-character spacing. */
1264 if (mode
== ATTR_WDUMMY
)
1267 /* Determine font for glyph if different from previous glyph. */
1268 if (prevmode
!= mode
) {
1271 frcflags
= FRC_NORMAL
;
1272 runewidth
= win
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
1273 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
1275 frcflags
= FRC_ITALICBOLD
;
1276 } else if (mode
& ATTR_ITALIC
) {
1278 frcflags
= FRC_ITALIC
;
1279 } else if (mode
& ATTR_BOLD
) {
1281 frcflags
= FRC_BOLD
;
1283 yp
= winy
+ font
->ascent
;
1286 /* Lookup character index with default font. */
1287 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
1289 specs
[numspecs
].font
= font
->match
;
1290 specs
[numspecs
].glyph
= glyphidx
;
1291 specs
[numspecs
].x
= (short)xp
;
1292 specs
[numspecs
].y
= (short)yp
;
1298 /* Fallback on font cache, search the font cache for match. */
1299 for (f
= 0; f
< frclen
; f
++) {
1300 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
1301 /* Everything correct. */
1302 if (glyphidx
&& frc
[f
].flags
== frcflags
)
1304 /* We got a default font for a not found glyph. */
1305 if (!glyphidx
&& frc
[f
].flags
== frcflags
1306 && frc
[f
].unicodep
== rune
) {
1311 /* Nothing was found. Use fontconfig to find matching font. */
1314 font
->set
= FcFontSort(0, font
->pattern
,
1316 fcsets
[0] = font
->set
;
1319 * Nothing was found in the cache. Now use
1320 * some dozen of Fontconfig calls to get the
1321 * font for one single character.
1323 * Xft and fontconfig are design failures.
1325 fcpattern
= FcPatternDuplicate(font
->pattern
);
1326 fccharset
= FcCharSetCreate();
1328 FcCharSetAddChar(fccharset
, rune
);
1329 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
1331 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
1333 FcConfigSubstitute(0, fcpattern
,
1335 FcDefaultSubstitute(fcpattern
);
1337 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
1340 /* Allocate memory for the new cache entry. */
1341 if (frclen
>= frccap
) {
1343 frc
= xrealloc(frc
, frccap
* sizeof(Fontcache
));
1346 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
1348 if (!frc
[frclen
].font
)
1349 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1351 frc
[frclen
].flags
= frcflags
;
1352 frc
[frclen
].unicodep
= rune
;
1354 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
1359 FcPatternDestroy(fcpattern
);
1360 FcCharSetDestroy(fccharset
);
1363 specs
[numspecs
].font
= frc
[f
].font
;
1364 specs
[numspecs
].glyph
= glyphidx
;
1365 specs
[numspecs
].x
= (short)xp
;
1366 specs
[numspecs
].y
= (short)yp
;
1375 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
1377 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
1378 int winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
,
1379 width
= charlen
* win
.cw
;
1380 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
1381 XRenderColor colfg
, colbg
;
1384 /* Fallback on color display for attributes not supported by the font */
1385 if (base
.mode
& ATTR_ITALIC
&& base
.mode
& ATTR_BOLD
) {
1386 if (dc
.ibfont
.badslant
|| dc
.ibfont
.badweight
)
1387 base
.fg
= defaultattr
;
1388 } else if ((base
.mode
& ATTR_ITALIC
&& dc
.ifont
.badslant
) ||
1389 (base
.mode
& ATTR_BOLD
&& dc
.bfont
.badweight
)) {
1390 base
.fg
= defaultattr
;
1393 if (IS_TRUECOL(base
.fg
)) {
1394 colfg
.alpha
= 0xffff;
1395 colfg
.red
= TRUERED(base
.fg
);
1396 colfg
.green
= TRUEGREEN(base
.fg
);
1397 colfg
.blue
= TRUEBLUE(base
.fg
);
1398 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
1401 fg
= &dc
.col
[base
.fg
];
1404 if (IS_TRUECOL(base
.bg
)) {
1405 colbg
.alpha
= 0xffff;
1406 colbg
.green
= TRUEGREEN(base
.bg
);
1407 colbg
.red
= TRUERED(base
.bg
);
1408 colbg
.blue
= TRUEBLUE(base
.bg
);
1409 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
1412 bg
= &dc
.col
[base
.bg
];
1415 /* Change basic system colors [0-7] to bright system colors [8-15] */
1416 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
1417 fg
= &dc
.col
[base
.fg
+ 8];
1419 if (IS_SET(MODE_REVERSE
)) {
1420 if (fg
== &dc
.col
[defaultfg
]) {
1421 fg
= &dc
.col
[defaultbg
];
1423 colfg
.red
= ~fg
->color
.red
;
1424 colfg
.green
= ~fg
->color
.green
;
1425 colfg
.blue
= ~fg
->color
.blue
;
1426 colfg
.alpha
= fg
->color
.alpha
;
1427 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
1432 if (bg
== &dc
.col
[defaultbg
]) {
1433 bg
= &dc
.col
[defaultfg
];
1435 colbg
.red
= ~bg
->color
.red
;
1436 colbg
.green
= ~bg
->color
.green
;
1437 colbg
.blue
= ~bg
->color
.blue
;
1438 colbg
.alpha
= bg
->color
.alpha
;
1439 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
1445 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
1446 colfg
.red
= fg
->color
.red
/ 2;
1447 colfg
.green
= fg
->color
.green
/ 2;
1448 colfg
.blue
= fg
->color
.blue
/ 2;
1449 colfg
.alpha
= fg
->color
.alpha
;
1450 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
1454 if (base
.mode
& ATTR_REVERSE
) {
1460 if (base
.mode
& ATTR_BLINK
&& win
.mode
& MODE_BLINK
)
1463 if (base
.mode
& ATTR_INVISIBLE
)
1466 /* Intelligent cleaning up of the borders. */
1468 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
1470 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: 0));
1472 if (winx
+ width
>= borderpx
+ win
.tw
) {
1473 xclear(winx
+ width
, (y
== 0)? 0 : winy
, win
.w
,
1474 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: (winy
+ win
.ch
)));
1477 xclear(winx
, 0, winx
+ width
, borderpx
);
1478 if (winy
+ win
.ch
>= borderpx
+ win
.th
)
1479 xclear(winx
, winy
+ win
.ch
, winx
+ width
, win
.h
);
1481 /* Clean up the region we want to draw to. */
1482 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, win
.ch
);
1484 /* Set the clip region because Xft is sometimes dirty. */
1489 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
1491 /* Render the glyphs. */
1492 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
1494 /* Render underline and strikethrough. */
1495 if (base
.mode
& ATTR_UNDERLINE
) {
1496 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
* chscale
+ 1,
1500 if (base
.mode
& ATTR_STRUCK
) {
1501 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
* chscale
/ 3,
1505 /* Reset clip to none. */
1506 XftDrawSetClip(xw
.draw
, 0);
1510 xdrawglyph(Glyph g
, int x
, int y
)
1513 XftGlyphFontSpec spec
;
1515 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
1516 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
1520 xdrawcursor(int cx
, int cy
, Glyph g
, int ox
, int oy
, Glyph og
)
1524 /* remove the old cursor */
1525 if (selected(ox
, oy
))
1526 og
.mode
^= ATTR_REVERSE
;
1527 xdrawglyph(og
, ox
, oy
);
1529 if (IS_SET(MODE_HIDE
))
1533 * Select the right color for the right mode.
1535 g
.mode
&= ATTR_BOLD
|ATTR_ITALIC
|ATTR_UNDERLINE
|ATTR_STRUCK
|ATTR_WIDE
;
1537 if (IS_SET(MODE_REVERSE
)) {
1538 g
.mode
|= ATTR_REVERSE
;
1540 if (selected(cx
, cy
)) {
1541 drawcol
= dc
.col
[defaultcs
];
1544 drawcol
= dc
.col
[defaultrcs
];
1548 if (selected(cx
, cy
)) {
1555 drawcol
= dc
.col
[g
.bg
];
1558 /* draw the new one */
1559 if (IS_SET(MODE_FOCUSED
)) {
1560 switch (win
.cursor
) {
1561 case 7: /* st extension */
1562 g
.u
= 0x2603; /* snowman (U+2603) */
1564 case 0: /* Blinking Block */
1565 case 1: /* Blinking Block (Default) */
1566 case 2: /* Steady Block */
1567 xdrawglyph(g
, cx
, cy
);
1569 case 3: /* Blinking Underline */
1570 case 4: /* Steady Underline */
1571 XftDrawRect(xw
.draw
, &drawcol
,
1572 borderpx
+ cx
* win
.cw
,
1573 borderpx
+ (cy
+ 1) * win
.ch
- \
1575 win
.cw
, cursorthickness
);
1577 case 5: /* Blinking bar */
1578 case 6: /* Steady bar */
1579 XftDrawRect(xw
.draw
, &drawcol
,
1580 borderpx
+ cx
* win
.cw
,
1581 borderpx
+ cy
* win
.ch
,
1582 cursorthickness
, win
.ch
);
1586 XftDrawRect(xw
.draw
, &drawcol
,
1587 borderpx
+ cx
* win
.cw
,
1588 borderpx
+ cy
* win
.ch
,
1590 XftDrawRect(xw
.draw
, &drawcol
,
1591 borderpx
+ cx
* win
.cw
,
1592 borderpx
+ cy
* win
.ch
,
1594 XftDrawRect(xw
.draw
, &drawcol
,
1595 borderpx
+ (cx
+ 1) * win
.cw
- 1,
1596 borderpx
+ cy
* win
.ch
,
1598 XftDrawRect(xw
.draw
, &drawcol
,
1599 borderpx
+ cx
* win
.cw
,
1600 borderpx
+ (cy
+ 1) * win
.ch
- 1,
1608 char buf
[sizeof(long) * 8 + 1];
1610 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1611 setenv("WINDOWID", buf
, 1);
1615 xseticontitle(char *p
)
1618 DEFAULT(p
, opt_title
);
1620 if (Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1623 XSetWMIconName(xw
.dpy
, xw
.win
, &prop
);
1624 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmiconname
);
1632 DEFAULT(p
, opt_title
);
1634 if (Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1637 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
1638 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
1645 return IS_SET(MODE_VISIBLE
);
1649 xdrawline(Line line
, int x1
, int y1
, int x2
)
1651 int i
, x
, ox
, numspecs
;
1653 XftGlyphFontSpec
*specs
= xw
.specbuf
;
1655 numspecs
= xmakeglyphfontspecs(specs
, &line
[x1
], x2
- x1
, x1
, y1
);
1657 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
1659 if (new.mode
== ATTR_WDUMMY
)
1661 if (selected(x
, y1
))
1662 new.mode
^= ATTR_REVERSE
;
1663 if (i
> 0 && ATTRCMP(base
, new)) {
1664 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1676 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1682 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, win
.w
,
1684 XSetForeground(xw
.dpy
, dc
.gc
,
1685 dc
.col
[IS_SET(MODE_REVERSE
)?
1686 defaultfg
: defaultbg
].pixel
);
1690 xximspot(int x
, int y
)
1692 if (xw
.ime
.xic
== NULL
)
1695 xw
.ime
.spot
.x
= borderpx
+ x
* win
.cw
;
1696 xw
.ime
.spot
.y
= borderpx
+ (y
+ 1) * win
.ch
;
1698 XSetICValues(xw
.ime
.xic
, XNPreeditAttributes
, xw
.ime
.spotlist
, NULL
);
1708 visibility(XEvent
*ev
)
1710 XVisibilityEvent
*e
= &ev
->xvisibility
;
1712 MODBIT(win
.mode
, e
->state
!= VisibilityFullyObscured
, MODE_VISIBLE
);
1718 win
.mode
&= ~MODE_VISIBLE
;
1722 xsetpointermotion(int set
)
1724 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
1725 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
1729 xsetmode(int set
, unsigned int flags
)
1731 int mode
= win
.mode
;
1732 MODBIT(win
.mode
, set
, flags
);
1733 if ((win
.mode
& MODE_REVERSE
) != (mode
& MODE_REVERSE
))
1738 xsetcursor(int cursor
)
1740 if (!BETWEEN(cursor
, 0, 7)) /* 7: st extension */
1742 win
.cursor
= cursor
;
1747 xseturgency(int add
)
1749 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1751 MODBIT(h
->flags
, add
, XUrgencyHint
);
1752 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1759 if (!(IS_SET(MODE_FOCUSED
)))
1762 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
1768 XFocusChangeEvent
*e
= &ev
->xfocus
;
1770 if (e
->mode
== NotifyGrab
)
1773 if (ev
->type
== FocusIn
) {
1775 XSetICFocus(xw
.ime
.xic
);
1776 win
.mode
|= MODE_FOCUSED
;
1778 if (IS_SET(MODE_FOCUS
))
1779 ttywrite("\033[I", 3, 0);
1782 XUnsetICFocus(xw
.ime
.xic
);
1783 win
.mode
&= ~MODE_FOCUSED
;
1784 if (IS_SET(MODE_FOCUS
))
1785 ttywrite("\033[O", 3, 0);
1790 match(uint mask
, uint state
)
1792 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
1796 kmap(KeySym k
, uint state
)
1801 /* Check for mapped keys out of X11 function keys. */
1802 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
1803 if (mappedkeys
[i
] == k
)
1806 if (i
== LEN(mappedkeys
)) {
1807 if ((k
& 0xFFFF) < 0xFD00)
1811 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
1815 if (!match(kp
->mask
, state
))
1818 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
1820 if (IS_SET(MODE_NUMLOCK
) && kp
->appkey
== 2)
1823 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
1835 XKeyEvent
*e
= &ev
->xkey
;
1836 KeySym ksym
= NoSymbol
;
1837 char buf
[64], *customkey
;
1843 if (IS_SET(MODE_KBDLOCK
))
1847 len
= XmbLookupString(xw
.ime
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
1848 if (status
== XBufferOverflow
)
1851 len
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
1854 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
1855 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
1856 bp
->func(&(bp
->arg
));
1861 /* 2. custom keys from config.h */
1862 if ((customkey
= kmap(ksym
, e
->state
))) {
1863 ttywrite(customkey
, strlen(customkey
), 1);
1867 /* 3. composed string from input method */
1870 if (len
== 1 && e
->state
& Mod1Mask
) {
1871 if (IS_SET(MODE_8BIT
)) {
1874 len
= utf8encode(c
, buf
);
1882 ttywrite(buf
, len
, 1);
1890 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1892 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1893 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1894 win
.mode
|= MODE_FOCUSED
;
1896 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1897 win
.mode
&= ~MODE_FOCUSED
;
1899 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
1908 if (e
->xconfigure
.width
== win
.w
&& e
->xconfigure
.height
== win
.h
)
1911 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
1918 int w
= win
.w
, h
= win
.h
;
1920 int xfd
= XConnectionNumber(xw
.dpy
), ttyfd
, xev
, drawing
;
1921 struct timespec seltv
, *tv
, now
, lastblink
, trigger
;
1924 /* Waiting for window mapping */
1926 XNextEvent(xw
.dpy
, &ev
);
1928 * This XFilterEvent call is required because of XOpenIM. It
1929 * does filter out the key event and some client message for
1930 * the input method too.
1932 if (XFilterEvent(&ev
, None
))
1934 if (ev
.type
== ConfigureNotify
) {
1935 w
= ev
.xconfigure
.width
;
1936 h
= ev
.xconfigure
.height
;
1938 } while (ev
.type
!= MapNotify
);
1940 ttyfd
= ttynew(opt_line
, shell
, opt_io
, opt_cmd
);
1943 for (timeout
= -1, drawing
= 0, lastblink
= (struct timespec
){0};;) {
1945 FD_SET(ttyfd
, &rfd
);
1948 if (XPending(xw
.dpy
))
1949 timeout
= 0; /* existing events might not set xfd */
1951 seltv
.tv_sec
= timeout
/ 1E3
;
1952 seltv
.tv_nsec
= 1E6
* (timeout
- 1E3
* seltv
.tv_sec
);
1953 tv
= timeout
>= 0 ? &seltv
: NULL
;
1955 if (pselect(MAX(xfd
, ttyfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
1958 die("select failed: %s\n", strerror(errno
));
1960 clock_gettime(CLOCK_MONOTONIC
, &now
);
1962 if (FD_ISSET(ttyfd
, &rfd
))
1966 while (XPending(xw
.dpy
)) {
1968 XNextEvent(xw
.dpy
, &ev
);
1969 if (XFilterEvent(&ev
, None
))
1971 if (handler
[ev
.type
])
1972 (handler
[ev
.type
])(&ev
);
1976 * To reduce flicker and tearing, when new content or event
1977 * triggers drawing, we first wait a bit to ensure we got
1978 * everything, and if nothing new arrives - we draw.
1979 * We start with trying to wait minlatency ms. If more content
1980 * arrives sooner, we retry with shorter and shorter periods,
1981 * and eventually draw even without idle after maxlatency ms.
1982 * Typically this results in low latency while interacting,
1983 * maximum latency intervals during `cat huge.txt`, and perfect
1984 * sync with periodic updates from animations/key-repeats/etc.
1986 if (FD_ISSET(ttyfd
, &rfd
) || xev
) {
1991 timeout
= (maxlatency
- TIMEDIFF(now
, trigger
)) \
1992 / maxlatency
* minlatency
;
1994 continue; /* we have time, try to find idle */
1997 /* idle detected or maxlatency exhausted -> draw */
1999 if (blinktimeout
&& tattrset(ATTR_BLINK
)) {
2000 timeout
= blinktimeout
- TIMEDIFF(now
, lastblink
);
2002 if (-timeout
> blinktimeout
) /* start visible */
2003 win
.mode
|= MODE_BLINK
;
2004 win
.mode
^= MODE_BLINK
;
2005 tsetdirtattr(ATTR_BLINK
);
2007 timeout
= blinktimeout
;
2020 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
2021 " [-n name] [-o file]\n"
2022 " [-T title] [-t title] [-w windowid]"
2023 " [[-e] command [args ...]]\n"
2024 " %s [-aiv] [-c class] [-f font] [-g geometry]"
2025 " [-n name] [-o file]\n"
2026 " [-T title] [-t title] [-w windowid] -l line"
2027 " [stty_args ...]\n", argv0
, argv0
);
2031 main(int argc
, char *argv
[])
2035 xsetcursor(cursorshape
);
2042 opt_class
= EARGF(usage());
2049 opt_font
= EARGF(usage());
2052 xw
.gm
= XParseGeometry(EARGF(usage()),
2053 &xw
.l
, &xw
.t
, &cols
, &rows
);
2059 opt_io
= EARGF(usage());
2062 opt_line
= EARGF(usage());
2065 opt_name
= EARGF(usage());
2069 opt_title
= EARGF(usage());
2072 opt_embed
= EARGF(usage());
2075 die("%s " VERSION
"\n", argv0
);
2082 if (argc
> 0) /* eat all remaining arguments */
2086 opt_title
= (opt_line
|| !opt_cmd
) ? "st" : opt_cmd
[0];
2088 setlocale(LC_CTYPE
, "");
2089 XSetLocaleModifiers("");
2090 cols
= MAX(cols
, 1);
2091 rows
= MAX(rows
, 1);