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 depth
; /* bit depth */
109 int l
, t
; /* left and top offset */
110 int gm
; /* geometry mask */
115 char *primary
, *clipboard
;
116 struct timespec tclick1
;
117 struct timespec tclick2
;
136 /* Drawing Context */
140 Font font
, bfont
, ifont
, ibfont
;
144 static inline ushort
sixd_to_16bit(int);
145 static int xmakeglyphfontspecs(XftGlyphFontSpec
*, const Glyph
*, int, int, int);
146 static void xdrawglyphfontspecs(const XftGlyphFontSpec
*, Glyph
, int, int, int);
147 static void xdrawglyph(Glyph
, int, int);
148 static void xclear(int, int, int, int);
149 static int xgeommasktogravity(int);
150 static int ximopen(Display
*);
151 static void ximinstantiate(Display
*, XPointer
, XPointer
);
152 static void ximdestroy(XIM
, XPointer
, XPointer
);
153 static int xicdestroy(XIC
, XPointer
, XPointer
);
154 static void xinit(int, int);
155 static void cresize(int, int);
156 static void xresize(int, int);
157 static void xhints(void);
158 static int xloadcolor(int, const char *, Color
*);
159 static int xloadfont(Font
*, FcPattern
*);
160 static void xloadfonts(const char *, double);
161 static void xunloadfont(Font
*);
162 static void xunloadfonts(void);
163 static void xsetenv(void);
164 static void xseturgency(int);
165 static int evcol(XEvent
*);
166 static int evrow(XEvent
*);
168 static void expose(XEvent
*);
169 static void visibility(XEvent
*);
170 static void unmap(XEvent
*);
171 static void kpress(XEvent
*);
172 static void cmessage(XEvent
*);
173 static void resize(XEvent
*);
174 static void focus(XEvent
*);
175 static uint
buttonmask(uint
);
176 static int mouseaction(XEvent
*, uint
);
177 static void brelease(XEvent
*);
178 static void bpress(XEvent
*);
179 static void bmotion(XEvent
*);
180 static void propnotify(XEvent
*);
181 static void selnotify(XEvent
*);
182 static void selclear_(XEvent
*);
183 static void selrequest(XEvent
*);
184 static void setsel(char *, Time
);
185 static void mousesel(XEvent
*, int);
186 static void mousereport(XEvent
*);
187 static char *kmap(KeySym
, uint
);
188 static int match(uint
, uint
);
190 static void run(void);
191 static void usage(void);
193 static void (*handler
[LASTEvent
])(XEvent
*) = {
195 [ClientMessage
] = cmessage
,
196 [ConfigureNotify
] = resize
,
197 [VisibilityNotify
] = visibility
,
198 [UnmapNotify
] = unmap
,
202 [MotionNotify
] = bmotion
,
203 [ButtonPress
] = bpress
,
204 [ButtonRelease
] = brelease
,
206 * Uncomment if you want the selection to disappear when you select something
207 * different in another window.
209 /* [SelectionClear] = selclear_, */
210 [SelectionNotify
] = selnotify
,
212 * PropertyNotify is only turned on when there is some INCR transfer happening
213 * for the selection retrieval.
215 [PropertyNotify
] = propnotify
,
216 [SelectionRequest
] = selrequest
,
222 static XSelection xsel
;
223 static TermWindow win
;
225 /* Font Ring Cache */
239 /* Fontcache is an array now. A new font will be appended to the array. */
240 static Fontcache
*frc
= NULL
;
241 static int frclen
= 0;
242 static int frccap
= 0;
243 static char *usedfont
= NULL
;
244 static double usedfontsize
= 0;
245 static double defaultfontsize
= 0;
247 static char *opt_alpha
= NULL
;
248 static char *opt_class
= NULL
;
249 static char **opt_cmd
= NULL
;
250 static char *opt_embed
= NULL
;
251 static char *opt_font
= NULL
;
252 static char *opt_io
= NULL
;
253 static char *opt_line
= NULL
;
254 static char *opt_name
= NULL
;
255 static char *opt_title
= NULL
;
257 static uint buttons
; /* bit field of pressed buttons */
260 clipcopy(const Arg
*dummy
)
264 free(xsel
.clipboard
);
265 xsel
.clipboard
= NULL
;
267 if (xsel
.primary
!= NULL
) {
268 xsel
.clipboard
= xstrdup(xsel
.primary
);
269 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
270 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
275 clippaste(const Arg
*dummy
)
279 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
280 XConvertSelection(xw
.dpy
, clipboard
, xsel
.xtarget
, clipboard
,
281 xw
.win
, CurrentTime
);
285 selpaste(const Arg
*dummy
)
287 XConvertSelection(xw
.dpy
, XA_PRIMARY
, xsel
.xtarget
, XA_PRIMARY
,
288 xw
.win
, CurrentTime
);
292 numlock(const Arg
*dummy
)
294 win
.mode
^= MODE_NUMLOCK
;
302 larg
.f
= usedfontsize
+ arg
->f
;
307 zoomabs(const Arg
*arg
)
310 xloadfonts(usedfont
, arg
->f
);
317 zoomreset(const Arg
*arg
)
321 if (defaultfontsize
> 0) {
322 larg
.f
= defaultfontsize
;
328 ttysend(const Arg
*arg
)
330 ttywrite(arg
->s
, strlen(arg
->s
), 1);
336 int x
= e
->xbutton
.x
- borderpx
;
337 LIMIT(x
, 0, win
.tw
- 1);
344 int y
= e
->xbutton
.y
- borderpx
;
345 LIMIT(y
, 0, win
.th
- 1);
350 mousesel(XEvent
*e
, int done
)
352 int type
, seltype
= SEL_REGULAR
;
353 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forcemousemod
);
355 for (type
= 1; type
< LEN(selmasks
); ++type
) {
356 if (match(selmasks
[type
], state
)) {
361 selextend(evcol(e
), evrow(e
), seltype
, done
);
363 setsel(getsel(), e
->xbutton
.time
);
367 mousereport(XEvent
*e
)
370 int x
= evcol(e
), y
= evrow(e
);
371 int state
= e
->xbutton
.state
;
375 if (e
->type
== MotionNotify
) {
376 if (x
== ox
&& y
== oy
)
378 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
380 /* MODE_MOUSEMOTION: no reporting if no button is pressed */
381 if (IS_SET(MODE_MOUSEMOTION
) && buttons
== 0)
383 /* Set btn to lowest-numbered pressed button, or 12 if no
384 * buttons are pressed. */
385 for (btn
= 1; btn
<= 11 && !(buttons
& (1<<(btn
-1))); btn
++)
389 btn
= e
->xbutton
.button
;
390 /* Only buttons 1 through 11 can be encoded */
391 if (btn
< 1 || btn
> 11)
393 if (e
->type
== ButtonRelease
) {
394 /* MODE_MOUSEX10: no button release reporting */
395 if (IS_SET(MODE_MOUSEX10
))
397 /* Don't send release events for the scroll wheel */
398 if (btn
== 4 || btn
== 5)
407 /* Encode btn into code. If no button is pressed for a motion event in
408 * MODE_MOUSEMANY, then encode it as a release. */
409 if ((!IS_SET(MODE_MOUSESGR
) && e
->type
== ButtonRelease
) || btn
== 12)
412 code
+= 128 + btn
- 8;
414 code
+= 64 + btn
- 4;
418 if (!IS_SET(MODE_MOUSEX10
)) {
419 code
+= ((state
& ShiftMask
) ? 4 : 0)
420 + ((state
& Mod1Mask
) ? 8 : 0) /* meta key: alt */
421 + ((state
& ControlMask
) ? 16 : 0);
424 if (IS_SET(MODE_MOUSESGR
)) {
425 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
427 e
->type
== ButtonRelease
? 'm' : 'M');
428 } else if (x
< 223 && y
< 223) {
429 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
430 32+code
, 32+x
+1, 32+y
+1);
435 ttywrite(buf
, len
, 0);
439 buttonmask(uint button
)
441 return button
== Button1
? Button1Mask
442 : button
== Button2
? Button2Mask
443 : button
== Button3
? Button3Mask
444 : button
== Button4
? Button4Mask
445 : button
== Button5
? Button5Mask
450 mouseaction(XEvent
*e
, uint release
)
454 /* ignore Button<N>mask for Button<N> - it's set on release */
455 uint state
= e
->xbutton
.state
& ~buttonmask(e
->xbutton
.button
);
457 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
458 if (ms
->release
== release
&&
459 ms
->button
== e
->xbutton
.button
&&
460 (match(ms
->mod
, state
) || /* exact or forced */
461 match(ms
->mod
, state
& ~forcemousemod
))) {
462 ms
->func(&(ms
->arg
));
473 int btn
= e
->xbutton
.button
;
477 if (1 <= btn
&& btn
<= 11)
478 buttons
|= 1 << (btn
-1);
480 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
485 if (mouseaction(e
, 0))
488 if (btn
== Button1
) {
490 * If the user clicks below predefined timeouts specific
491 * snapping behaviour is exposed.
493 clock_gettime(CLOCK_MONOTONIC
, &now
);
494 if (TIMEDIFF(now
, xsel
.tclick2
) <= tripleclicktimeout
) {
496 } else if (TIMEDIFF(now
, xsel
.tclick1
) <= doubleclicktimeout
) {
501 xsel
.tclick2
= xsel
.tclick1
;
504 selstart(evcol(e
), evrow(e
), snap
);
509 propnotify(XEvent
*e
)
511 XPropertyEvent
*xpev
;
512 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
514 xpev
= &e
->xproperty
;
515 if (xpev
->state
== PropertyNewValue
&&
516 (xpev
->atom
== XA_PRIMARY
||
517 xpev
->atom
== clipboard
)) {
525 ulong nitems
, ofs
, rem
;
527 uchar
*data
, *last
, *repl
;
528 Atom type
, incratom
, property
= None
;
530 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
533 if (e
->type
== SelectionNotify
)
534 property
= e
->xselection
.property
;
535 else if (e
->type
== PropertyNotify
)
536 property
= e
->xproperty
.atom
;
538 if (property
== None
)
542 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
543 BUFSIZ
/4, False
, AnyPropertyType
,
544 &type
, &format
, &nitems
, &rem
,
546 fprintf(stderr
, "Clipboard allocation failed\n");
550 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
552 * If there is some PropertyNotify with no data, then
553 * this is the signal of the selection owner that all
554 * data has been transferred. We won't need to receive
555 * PropertyNotify events anymore.
557 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
558 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
562 if (type
== incratom
) {
564 * Activate the PropertyNotify events so we receive
565 * when the selection owner does send us the next
568 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
569 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
573 * Deleting the property is the transfer start signal.
575 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
581 * Line endings are inconsistent in the terminal and GUI world
582 * copy and pasting. When receiving some selection data,
583 * replace all '\n' with '\r'.
584 * FIXME: Fix the computer world.
587 last
= data
+ nitems
* format
/ 8;
588 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
592 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
593 ttywrite("\033[200~", 6, 0);
594 ttywrite((char *)data
, nitems
* format
/ 8, 1);
595 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
596 ttywrite("\033[201~", 6, 0);
598 /* number of 32-bit chunks returned */
599 ofs
+= nitems
* format
/ 32;
603 * Deleting the property again tells the selection owner to send the
604 * next data chunk in the property.
606 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
622 selrequest(XEvent
*e
)
624 XSelectionRequestEvent
*xsre
;
626 Atom xa_targets
, string
, clipboard
;
629 xsre
= (XSelectionRequestEvent
*) e
;
630 xev
.type
= SelectionNotify
;
631 xev
.requestor
= xsre
->requestor
;
632 xev
.selection
= xsre
->selection
;
633 xev
.target
= xsre
->target
;
634 xev
.time
= xsre
->time
;
635 if (xsre
->property
== None
)
636 xsre
->property
= xsre
->target
;
641 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
642 if (xsre
->target
== xa_targets
) {
643 /* respond with the supported type */
644 string
= xsel
.xtarget
;
645 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
646 XA_ATOM
, 32, PropModeReplace
,
647 (uchar
*) &string
, 1);
648 xev
.property
= xsre
->property
;
649 } else if (xsre
->target
== xsel
.xtarget
|| xsre
->target
== XA_STRING
) {
651 * xith XA_STRING non ascii characters may be incorrect in the
652 * requestor. It is not our problem, use utf8.
654 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
655 if (xsre
->selection
== XA_PRIMARY
) {
656 seltext
= xsel
.primary
;
657 } else if (xsre
->selection
== clipboard
) {
658 seltext
= xsel
.clipboard
;
661 "Unhandled clipboard selection 0x%lx\n",
665 if (seltext
!= NULL
) {
666 XChangeProperty(xsre
->display
, xsre
->requestor
,
667 xsre
->property
, xsre
->target
,
669 (uchar
*)seltext
, strlen(seltext
));
670 xev
.property
= xsre
->property
;
674 /* all done, send a notification to the listener */
675 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
676 fprintf(stderr
, "Error sending SelectionNotify event\n");
680 setsel(char *str
, Time t
)
688 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
689 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
696 setsel(str
, CurrentTime
);
702 int btn
= e
->xbutton
.button
;
704 if (1 <= btn
&& btn
<= 11)
705 buttons
&= ~(1 << (btn
-1));
707 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
712 if (mouseaction(e
, 1))
721 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
730 cresize(int width
, int height
)
739 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
740 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
746 ttyresize(win
.tw
, win
.th
);
750 xresize(int col
, int row
)
752 win
.tw
= col
* win
.cw
;
753 win
.th
= row
* win
.ch
;
755 XFreePixmap(xw
.dpy
, xw
.buf
);
756 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
758 XftDrawChange(xw
.draw
, xw
.buf
);
759 xclear(0, 0, win
.w
, win
.h
);
761 /* resize to new width */
762 xw
.specbuf
= xrealloc(xw
.specbuf
, col
* sizeof(GlyphFontSpec
));
768 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
772 xloadcolor(int i
, const char *name
, Color
*ncolor
)
774 XRenderColor color
= { .alpha
= 0xffff };
777 if (BETWEEN(i
, 16, 255)) { /* 256 color */
778 if (i
< 6*6*6+16) { /* same colors as xterm */
779 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
780 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
781 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
782 } else { /* greyscale */
783 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
784 color
.green
= color
.blue
= color
.red
;
786 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
787 xw
.cmap
, &color
, ncolor
);
792 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
803 for (cp
= dc
.col
; cp
< &dc
.col
[dc
.collen
]; ++cp
)
804 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
806 dc
.collen
= MAX(LEN(colorname
), 256);
807 dc
.col
= xmalloc(dc
.collen
* sizeof(Color
));
810 for (i
= 0; i
< dc
.collen
; i
++)
811 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
813 die("could not allocate color '%s'\n", colorname
[i
]);
815 die("could not allocate color %d\n", i
);
818 /* set alpha value of bg color */
820 alpha
= strtof(opt_alpha
, NULL
);
821 dc
.col
[defaultbg
].color
.alpha
= (unsigned short)(0xffff * alpha
);
822 dc
.col
[defaultbg
].pixel
&= 0x00FFFFFF;
823 dc
.col
[defaultbg
].pixel
|= (unsigned char)(0xff * alpha
) << 24;
828 xgetcolor(int x
, unsigned char *r
, unsigned char *g
, unsigned char *b
)
830 if (!BETWEEN(x
, 0, dc
.collen
))
833 *r
= dc
.col
[x
].color
.red
>> 8;
834 *g
= dc
.col
[x
].color
.green
>> 8;
835 *b
= dc
.col
[x
].color
.blue
>> 8;
841 xsetcolorname(int x
, const char *name
)
845 if (!BETWEEN(x
, 0, dc
.collen
))
848 if (!xloadcolor(x
, name
, &ncolor
))
851 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
858 * Absolute coordinates.
861 xclear(int x1
, int y1
, int x2
, int y2
)
864 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
865 x1
, y1
, x2
-x1
, y2
-y1
);
871 XClassHint
class = {opt_name
? opt_name
: termname
,
872 opt_class
? opt_class
: termname
};
873 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
876 sizeh
= XAllocSizeHints();
878 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
| PMinSize
;
879 sizeh
->height
= win
.h
;
880 sizeh
->width
= win
.w
;
881 sizeh
->height_inc
= win
.ch
;
882 sizeh
->width_inc
= win
.cw
;
883 sizeh
->base_height
= 2 * borderpx
;
884 sizeh
->base_width
= 2 * borderpx
;
885 sizeh
->min_height
= win
.ch
+ 2 * borderpx
;
886 sizeh
->min_width
= win
.cw
+ 2 * borderpx
;
888 sizeh
->flags
|= PMaxSize
;
889 sizeh
->min_width
= sizeh
->max_width
= win
.w
;
890 sizeh
->min_height
= sizeh
->max_height
= win
.h
;
892 if (xw
.gm
& (XValue
|YValue
)) {
893 sizeh
->flags
|= USPosition
| PWinGravity
;
896 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
899 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
905 xgeommasktogravity(int mask
)
907 switch (mask
& (XNegative
|YNegative
)) {
909 return NorthWestGravity
;
911 return NorthEastGravity
;
913 return SouthWestGravity
;
916 return SouthEastGravity
;
920 xloadfont(Font
*f
, FcPattern
*pattern
)
922 FcPattern
*configured
;
926 int wantattr
, haveattr
;
929 * Manually configure instead of calling XftMatchFont
930 * so that we can use the configured pattern for
931 * "missing glyph" lookups.
933 configured
= FcPatternDuplicate(pattern
);
937 FcConfigSubstitute(NULL
, configured
, FcMatchPattern
);
938 XftDefaultSubstitute(xw
.dpy
, xw
.scr
, configured
);
940 match
= FcFontMatch(NULL
, configured
, &result
);
942 FcPatternDestroy(configured
);
946 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
947 FcPatternDestroy(configured
);
948 FcPatternDestroy(match
);
952 if ((XftPatternGetInteger(pattern
, "slant", 0, &wantattr
) ==
955 * Check if xft was unable to find a font with the appropriate
956 * slant but gave us one anyway. Try to mitigate.
958 if ((XftPatternGetInteger(f
->match
->pattern
, "slant", 0,
959 &haveattr
) != XftResultMatch
) || haveattr
< wantattr
) {
961 fputs("font slant does not match\n", stderr
);
965 if ((XftPatternGetInteger(pattern
, "weight", 0, &wantattr
) ==
967 if ((XftPatternGetInteger(f
->match
->pattern
, "weight", 0,
968 &haveattr
) != XftResultMatch
) || haveattr
!= wantattr
) {
970 fputs("font weight does not match\n", stderr
);
974 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
975 (const FcChar8
*) ascii_printable
,
976 strlen(ascii_printable
), &extents
);
979 f
->pattern
= configured
;
981 f
->ascent
= f
->match
->ascent
;
982 f
->descent
= f
->match
->descent
;
984 f
->rbearing
= f
->match
->max_advance_width
;
986 f
->height
= f
->ascent
+ f
->descent
;
987 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
993 xloadfonts(const char *fontstr
, double fontsize
)
998 if (fontstr
[0] == '-')
999 pattern
= XftXlfdParse(fontstr
, False
, False
);
1001 pattern
= FcNameParse((const FcChar8
*)fontstr
);
1004 die("can't open font %s\n", fontstr
);
1007 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
1008 FcPatternDel(pattern
, FC_SIZE
);
1009 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
1010 usedfontsize
= fontsize
;
1012 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
1014 usedfontsize
= fontval
;
1015 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
1020 * Default font size is 12, if none given. This is to
1021 * have a known usedfontsize value.
1023 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
1026 defaultfontsize
= usedfontsize
;
1029 if (xloadfont(&dc
.font
, pattern
))
1030 die("can't open font %s\n", fontstr
);
1032 if (usedfontsize
< 0) {
1033 FcPatternGetDouble(dc
.font
.match
->pattern
,
1034 FC_PIXEL_SIZE
, 0, &fontval
);
1035 usedfontsize
= fontval
;
1037 defaultfontsize
= fontval
;
1040 /* Setting character width and height. */
1041 win
.cw
= ceilf(dc
.font
.width
* cwscale
);
1042 win
.ch
= ceilf(dc
.font
.height
* chscale
);
1044 FcPatternDel(pattern
, FC_SLANT
);
1045 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
1046 if (xloadfont(&dc
.ifont
, pattern
))
1047 die("can't open font %s\n", fontstr
);
1049 FcPatternDel(pattern
, FC_WEIGHT
);
1050 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
1051 if (xloadfont(&dc
.ibfont
, pattern
))
1052 die("can't open font %s\n", fontstr
);
1054 FcPatternDel(pattern
, FC_SLANT
);
1055 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
1056 if (xloadfont(&dc
.bfont
, pattern
))
1057 die("can't open font %s\n", fontstr
);
1059 FcPatternDestroy(pattern
);
1063 xunloadfont(Font
*f
)
1065 XftFontClose(xw
.dpy
, f
->match
);
1066 FcPatternDestroy(f
->pattern
);
1068 FcFontSetDestroy(f
->set
);
1074 /* Free the loaded fonts in the font cache. */
1076 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
1078 xunloadfont(&dc
.font
);
1079 xunloadfont(&dc
.bfont
);
1080 xunloadfont(&dc
.ifont
);
1081 xunloadfont(&dc
.ibfont
);
1085 ximopen(Display
*dpy
)
1087 XIMCallback imdestroy
= { .client_data
= NULL
, .callback
= ximdestroy
};
1088 XICCallback icdestroy
= { .client_data
= NULL
, .callback
= xicdestroy
};
1090 xw
.ime
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
1091 if (xw
.ime
.xim
== NULL
)
1094 if (XSetIMValues(xw
.ime
.xim
, XNDestroyCallback
, &imdestroy
, NULL
))
1095 fprintf(stderr
, "XSetIMValues: "
1096 "Could not set XNDestroyCallback.\n");
1098 xw
.ime
.spotlist
= XVaCreateNestedList(0, XNSpotLocation
, &xw
.ime
.spot
,
1101 if (xw
.ime
.xic
== NULL
) {
1102 xw
.ime
.xic
= XCreateIC(xw
.ime
.xim
, XNInputStyle
,
1103 XIMPreeditNothing
| XIMStatusNothing
,
1104 XNClientWindow
, xw
.win
,
1105 XNDestroyCallback
, &icdestroy
,
1108 if (xw
.ime
.xic
== NULL
)
1109 fprintf(stderr
, "XCreateIC: Could not create input context.\n");
1115 ximinstantiate(Display
*dpy
, XPointer client
, XPointer call
)
1118 XUnregisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1119 ximinstantiate
, NULL
);
1123 ximdestroy(XIM xim
, XPointer client
, XPointer call
)
1126 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1127 ximinstantiate
, NULL
);
1128 XFree(xw
.ime
.spotlist
);
1132 xicdestroy(XIC xim
, XPointer client
, XPointer call
)
1139 xinit(int cols
, int rows
)
1144 pid_t thispid
= getpid();
1145 XColor xmousefg
, xmousebg
;
1146 XWindowAttributes attr
;
1149 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
1150 die("can't open display\n");
1151 xw
.scr
= XDefaultScreen(xw
.dpy
);
1153 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0)))) {
1154 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
1157 XGetWindowAttributes(xw
.dpy
, parent
, &attr
);
1158 xw
.depth
= attr
.depth
;
1161 XMatchVisualInfo(xw
.dpy
, xw
.scr
, xw
.depth
, TrueColor
, &vis
);
1162 xw
.vis
= vis
.visual
;
1166 die("could not init fontconfig.\n");
1168 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
1169 xloadfonts(usedfont
, 0);
1172 xw
.cmap
= XCreateColormap(xw
.dpy
, parent
, xw
.vis
, None
);
1175 /* adjust fixed window geometry */
1176 win
.w
= 2 * borderpx
+ cols
* win
.cw
;
1177 win
.h
= 2 * borderpx
+ rows
* win
.ch
;
1178 if (xw
.gm
& XNegative
)
1179 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - win
.w
- 2;
1180 if (xw
.gm
& YNegative
)
1181 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - win
.h
- 2;
1184 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
1185 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
1186 xw
.attrs
.bit_gravity
= NorthWestGravity
;
1187 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
| KeyReleaseMask
1188 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1189 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1190 xw
.attrs
.colormap
= xw
.cmap
;
1192 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
1193 win
.w
, win
.h
, 0, xw
.depth
, InputOutput
,
1194 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
1195 | CWEventMask
| CWColormap
, &xw
.attrs
);
1197 memset(&gcvalues
, 0, sizeof(gcvalues
));
1198 gcvalues
.graphics_exposures
= False
;
1199 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
, xw
.depth
);
1200 dc
.gc
= XCreateGC(xw
.dpy
, xw
.buf
, GCGraphicsExposures
, &gcvalues
);
1201 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
1202 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, win
.w
, win
.h
);
1204 /* font spec buffer */
1205 xw
.specbuf
= xmalloc(cols
* sizeof(GlyphFontSpec
));
1207 /* Xft rendering context */
1208 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
1211 if (!ximopen(xw
.dpy
)) {
1212 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1213 ximinstantiate
, NULL
);
1216 /* white cursor, black outline */
1217 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
1218 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1220 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
1221 xmousefg
.red
= 0xffff;
1222 xmousefg
.green
= 0xffff;
1223 xmousefg
.blue
= 0xffff;
1226 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
1227 xmousebg
.red
= 0x0000;
1228 xmousebg
.green
= 0x0000;
1229 xmousebg
.blue
= 0x0000;
1232 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
1234 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1235 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
1236 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
1237 xw
.netwmiconname
= XInternAtom(xw
.dpy
, "_NET_WM_ICON_NAME", False
);
1238 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
1240 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
1241 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
1242 PropModeReplace
, (uchar
*)&thispid
, 1);
1244 win
.mode
= MODE_NUMLOCK
;
1247 XMapWindow(xw
.dpy
, xw
.win
);
1248 XSync(xw
.dpy
, False
);
1250 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick1
);
1251 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick2
);
1252 xsel
.primary
= NULL
;
1253 xsel
.clipboard
= NULL
;
1254 xsel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
1255 if (xsel
.xtarget
== None
)
1256 xsel
.xtarget
= XA_STRING
;
1260 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
1262 float winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
, xp
, yp
;
1263 ushort mode
, prevmode
= USHRT_MAX
;
1264 Font
*font
= &dc
.font
;
1265 int frcflags
= FRC_NORMAL
;
1266 float runewidth
= win
.cw
;
1270 FcPattern
*fcpattern
, *fontpattern
;
1271 FcFontSet
*fcsets
[] = { NULL
};
1272 FcCharSet
*fccharset
;
1273 int i
, f
, numspecs
= 0;
1275 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
1276 /* Fetch rune and mode for current glyph. */
1278 mode
= glyphs
[i
].mode
;
1280 /* Skip dummy wide-character spacing. */
1281 if (mode
== ATTR_WDUMMY
)
1284 /* Determine font for glyph if different from previous glyph. */
1285 if (prevmode
!= mode
) {
1288 frcflags
= FRC_NORMAL
;
1289 runewidth
= win
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
1290 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
1292 frcflags
= FRC_ITALICBOLD
;
1293 } else if (mode
& ATTR_ITALIC
) {
1295 frcflags
= FRC_ITALIC
;
1296 } else if (mode
& ATTR_BOLD
) {
1298 frcflags
= FRC_BOLD
;
1300 yp
= winy
+ font
->ascent
;
1303 /* Lookup character index with default font. */
1304 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
1306 specs
[numspecs
].font
= font
->match
;
1307 specs
[numspecs
].glyph
= glyphidx
;
1308 specs
[numspecs
].x
= (short)xp
;
1309 specs
[numspecs
].y
= (short)yp
;
1315 /* Fallback on font cache, search the font cache for match. */
1316 for (f
= 0; f
< frclen
; f
++) {
1317 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
1318 /* Everything correct. */
1319 if (glyphidx
&& frc
[f
].flags
== frcflags
)
1321 /* We got a default font for a not found glyph. */
1322 if (!glyphidx
&& frc
[f
].flags
== frcflags
1323 && frc
[f
].unicodep
== rune
) {
1328 /* Nothing was found. Use fontconfig to find matching font. */
1331 font
->set
= FcFontSort(0, font
->pattern
,
1333 fcsets
[0] = font
->set
;
1336 * Nothing was found in the cache. Now use
1337 * some dozen of Fontconfig calls to get the
1338 * font for one single character.
1340 * Xft and fontconfig are design failures.
1342 fcpattern
= FcPatternDuplicate(font
->pattern
);
1343 fccharset
= FcCharSetCreate();
1345 FcCharSetAddChar(fccharset
, rune
);
1346 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
1348 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
1350 FcConfigSubstitute(0, fcpattern
,
1352 FcDefaultSubstitute(fcpattern
);
1354 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
1357 /* Allocate memory for the new cache entry. */
1358 if (frclen
>= frccap
) {
1360 frc
= xrealloc(frc
, frccap
* sizeof(Fontcache
));
1363 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
1365 if (!frc
[frclen
].font
)
1366 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1368 frc
[frclen
].flags
= frcflags
;
1369 frc
[frclen
].unicodep
= rune
;
1371 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
1376 FcPatternDestroy(fcpattern
);
1377 FcCharSetDestroy(fccharset
);
1380 specs
[numspecs
].font
= frc
[f
].font
;
1381 specs
[numspecs
].glyph
= glyphidx
;
1382 specs
[numspecs
].x
= (short)xp
;
1383 specs
[numspecs
].y
= (short)yp
;
1392 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
1394 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
1395 int winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
,
1396 width
= charlen
* win
.cw
;
1397 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
1398 XRenderColor colfg
, colbg
;
1401 /* Fallback on color display for attributes not supported by the font */
1402 if (base
.mode
& ATTR_ITALIC
&& base
.mode
& ATTR_BOLD
) {
1403 if (dc
.ibfont
.badslant
|| dc
.ibfont
.badweight
)
1404 base
.fg
= defaultattr
;
1405 } else if ((base
.mode
& ATTR_ITALIC
&& dc
.ifont
.badslant
) ||
1406 (base
.mode
& ATTR_BOLD
&& dc
.bfont
.badweight
)) {
1407 base
.fg
= defaultattr
;
1410 if (IS_TRUECOL(base
.fg
)) {
1411 colfg
.alpha
= 0xffff;
1412 colfg
.red
= TRUERED(base
.fg
);
1413 colfg
.green
= TRUEGREEN(base
.fg
);
1414 colfg
.blue
= TRUEBLUE(base
.fg
);
1415 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
1418 fg
= &dc
.col
[base
.fg
];
1421 if (IS_TRUECOL(base
.bg
)) {
1422 colbg
.alpha
= 0xffff;
1423 colbg
.green
= TRUEGREEN(base
.bg
);
1424 colbg
.red
= TRUERED(base
.bg
);
1425 colbg
.blue
= TRUEBLUE(base
.bg
);
1426 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
1429 bg
= &dc
.col
[base
.bg
];
1432 /* Change basic system colors [0-7] to bright system colors [8-15] */
1433 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
1434 fg
= &dc
.col
[base
.fg
+ 8];
1436 if (IS_SET(MODE_REVERSE
)) {
1437 if (fg
== &dc
.col
[defaultfg
]) {
1438 fg
= &dc
.col
[defaultbg
];
1440 colfg
.red
= ~fg
->color
.red
;
1441 colfg
.green
= ~fg
->color
.green
;
1442 colfg
.blue
= ~fg
->color
.blue
;
1443 colfg
.alpha
= fg
->color
.alpha
;
1444 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
1449 if (bg
== &dc
.col
[defaultbg
]) {
1450 bg
= &dc
.col
[defaultfg
];
1452 colbg
.red
= ~bg
->color
.red
;
1453 colbg
.green
= ~bg
->color
.green
;
1454 colbg
.blue
= ~bg
->color
.blue
;
1455 colbg
.alpha
= bg
->color
.alpha
;
1456 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
1462 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
1463 colfg
.red
= fg
->color
.red
/ 2;
1464 colfg
.green
= fg
->color
.green
/ 2;
1465 colfg
.blue
= fg
->color
.blue
/ 2;
1466 colfg
.alpha
= fg
->color
.alpha
;
1467 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
1471 if (base
.mode
& ATTR_REVERSE
) {
1477 if (base
.mode
& ATTR_BLINK
&& win
.mode
& MODE_BLINK
)
1480 if (base
.mode
& ATTR_INVISIBLE
)
1483 /* Intelligent cleaning up of the borders. */
1485 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
1487 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: 0));
1489 if (winx
+ width
>= borderpx
+ win
.tw
) {
1490 xclear(winx
+ width
, (y
== 0)? 0 : winy
, win
.w
,
1491 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: (winy
+ win
.ch
)));
1494 xclear(winx
, 0, winx
+ width
, borderpx
);
1495 if (winy
+ win
.ch
>= borderpx
+ win
.th
)
1496 xclear(winx
, winy
+ win
.ch
, winx
+ width
, win
.h
);
1498 /* Clean up the region we want to draw to. */
1499 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, win
.ch
);
1501 /* Set the clip region because Xft is sometimes dirty. */
1506 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
1508 /* Render the glyphs. */
1509 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
1511 /* Render underline and strikethrough. */
1512 if (base
.mode
& ATTR_UNDERLINE
) {
1513 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
* chscale
+ 1,
1517 if (base
.mode
& ATTR_STRUCK
) {
1518 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
* chscale
/ 3,
1522 /* Reset clip to none. */
1523 XftDrawSetClip(xw
.draw
, 0);
1527 xdrawglyph(Glyph g
, int x
, int y
)
1530 XftGlyphFontSpec spec
;
1532 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
1533 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
1537 xdrawcursor(int cx
, int cy
, Glyph g
, int ox
, int oy
, Glyph og
)
1541 /* remove the old cursor */
1542 if (selected(ox
, oy
))
1543 og
.mode
^= ATTR_REVERSE
;
1544 xdrawglyph(og
, ox
, oy
);
1546 if (IS_SET(MODE_HIDE
))
1550 * Select the right color for the right mode.
1552 g
.mode
&= ATTR_BOLD
|ATTR_ITALIC
|ATTR_UNDERLINE
|ATTR_STRUCK
|ATTR_WIDE
;
1554 if (IS_SET(MODE_REVERSE
)) {
1555 g
.mode
|= ATTR_REVERSE
;
1557 if (selected(cx
, cy
)) {
1558 drawcol
= dc
.col
[defaultcs
];
1561 drawcol
= dc
.col
[defaultrcs
];
1565 if (selected(cx
, cy
)) {
1572 drawcol
= dc
.col
[g
.bg
];
1575 /* draw the new one */
1576 if (IS_SET(MODE_FOCUSED
)) {
1577 switch (win
.cursor
) {
1578 case 7: /* st extension */
1579 g
.u
= 0x2603; /* snowman (U+2603) */
1581 case 0: /* Blinking Block */
1582 case 1: /* Blinking Block (Default) */
1583 case 2: /* Steady Block */
1584 xdrawglyph(g
, cx
, cy
);
1586 case 3: /* Blinking Underline */
1587 case 4: /* Steady Underline */
1588 XftDrawRect(xw
.draw
, &drawcol
,
1589 borderpx
+ cx
* win
.cw
,
1590 borderpx
+ (cy
+ 1) * win
.ch
- \
1592 win
.cw
, cursorthickness
);
1594 case 5: /* Blinking bar */
1595 case 6: /* Steady bar */
1596 XftDrawRect(xw
.draw
, &drawcol
,
1597 borderpx
+ cx
* win
.cw
,
1598 borderpx
+ cy
* win
.ch
,
1599 cursorthickness
, win
.ch
);
1603 XftDrawRect(xw
.draw
, &drawcol
,
1604 borderpx
+ cx
* win
.cw
,
1605 borderpx
+ cy
* win
.ch
,
1607 XftDrawRect(xw
.draw
, &drawcol
,
1608 borderpx
+ cx
* win
.cw
,
1609 borderpx
+ cy
* win
.ch
,
1611 XftDrawRect(xw
.draw
, &drawcol
,
1612 borderpx
+ (cx
+ 1) * win
.cw
- 1,
1613 borderpx
+ cy
* win
.ch
,
1615 XftDrawRect(xw
.draw
, &drawcol
,
1616 borderpx
+ cx
* win
.cw
,
1617 borderpx
+ (cy
+ 1) * win
.ch
- 1,
1625 char buf
[sizeof(long) * 8 + 1];
1627 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1628 setenv("WINDOWID", buf
, 1);
1632 xseticontitle(char *p
)
1635 DEFAULT(p
, opt_title
);
1637 if (Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1640 XSetWMIconName(xw
.dpy
, xw
.win
, &prop
);
1641 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmiconname
);
1649 DEFAULT(p
, opt_title
);
1651 if (Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1654 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
1655 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
1662 return IS_SET(MODE_VISIBLE
);
1666 xdrawline(Line line
, int x1
, int y1
, int x2
)
1668 int i
, x
, ox
, numspecs
;
1670 XftGlyphFontSpec
*specs
= xw
.specbuf
;
1672 numspecs
= xmakeglyphfontspecs(specs
, &line
[x1
], x2
- x1
, x1
, y1
);
1674 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
1676 if (new.mode
== ATTR_WDUMMY
)
1678 if (selected(x
, y1
))
1679 new.mode
^= ATTR_REVERSE
;
1680 if (i
> 0 && ATTRCMP(base
, new)) {
1681 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1693 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1699 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, win
.w
,
1701 XSetForeground(xw
.dpy
, dc
.gc
,
1702 dc
.col
[IS_SET(MODE_REVERSE
)?
1703 defaultfg
: defaultbg
].pixel
);
1707 xximspot(int x
, int y
)
1709 if (xw
.ime
.xic
== NULL
)
1712 xw
.ime
.spot
.x
= borderpx
+ x
* win
.cw
;
1713 xw
.ime
.spot
.y
= borderpx
+ (y
+ 1) * win
.ch
;
1715 XSetICValues(xw
.ime
.xic
, XNPreeditAttributes
, xw
.ime
.spotlist
, NULL
);
1725 visibility(XEvent
*ev
)
1727 XVisibilityEvent
*e
= &ev
->xvisibility
;
1729 MODBIT(win
.mode
, e
->state
!= VisibilityFullyObscured
, MODE_VISIBLE
);
1735 win
.mode
&= ~MODE_VISIBLE
;
1739 xsetpointermotion(int set
)
1741 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
1742 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
1746 xsetmode(int set
, unsigned int flags
)
1748 int mode
= win
.mode
;
1749 MODBIT(win
.mode
, set
, flags
);
1750 if ((win
.mode
& MODE_REVERSE
) != (mode
& MODE_REVERSE
))
1755 xsetcursor(int cursor
)
1757 if (!BETWEEN(cursor
, 0, 7)) /* 7: st extension */
1759 win
.cursor
= cursor
;
1764 xseturgency(int add
)
1766 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1768 MODBIT(h
->flags
, add
, XUrgencyHint
);
1769 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1776 if (!(IS_SET(MODE_FOCUSED
)))
1779 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
1785 XFocusChangeEvent
*e
= &ev
->xfocus
;
1787 if (e
->mode
== NotifyGrab
)
1790 if (ev
->type
== FocusIn
) {
1792 XSetICFocus(xw
.ime
.xic
);
1793 win
.mode
|= MODE_FOCUSED
;
1795 if (IS_SET(MODE_FOCUS
))
1796 ttywrite("\033[I", 3, 0);
1799 XUnsetICFocus(xw
.ime
.xic
);
1800 win
.mode
&= ~MODE_FOCUSED
;
1801 if (IS_SET(MODE_FOCUS
))
1802 ttywrite("\033[O", 3, 0);
1807 match(uint mask
, uint state
)
1809 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
1813 kmap(KeySym k
, uint state
)
1818 /* Check for mapped keys out of X11 function keys. */
1819 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
1820 if (mappedkeys
[i
] == k
)
1823 if (i
== LEN(mappedkeys
)) {
1824 if ((k
& 0xFFFF) < 0xFD00)
1828 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
1832 if (!match(kp
->mask
, state
))
1835 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
1837 if (IS_SET(MODE_NUMLOCK
) && kp
->appkey
== 2)
1840 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
1852 XKeyEvent
*e
= &ev
->xkey
;
1854 char buf
[64], *customkey
;
1860 if (IS_SET(MODE_KBDLOCK
))
1864 len
= XmbLookupString(xw
.ime
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
1866 len
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
1868 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
1869 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
1870 bp
->func(&(bp
->arg
));
1875 /* 2. custom keys from config.h */
1876 if ((customkey
= kmap(ksym
, e
->state
))) {
1877 ttywrite(customkey
, strlen(customkey
), 1);
1881 /* 3. composed string from input method */
1884 if (len
== 1 && e
->state
& Mod1Mask
) {
1885 if (IS_SET(MODE_8BIT
)) {
1888 len
= utf8encode(c
, buf
);
1896 ttywrite(buf
, len
, 1);
1904 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1906 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1907 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1908 win
.mode
|= MODE_FOCUSED
;
1910 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1911 win
.mode
&= ~MODE_FOCUSED
;
1913 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
1922 if (e
->xconfigure
.width
== win
.w
&& e
->xconfigure
.height
== win
.h
)
1925 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
1932 int w
= win
.w
, h
= win
.h
;
1934 int xfd
= XConnectionNumber(xw
.dpy
), ttyfd
, xev
, drawing
;
1935 struct timespec seltv
, *tv
, now
, lastblink
, trigger
;
1938 /* Waiting for window mapping */
1940 XNextEvent(xw
.dpy
, &ev
);
1942 * This XFilterEvent call is required because of XOpenIM. It
1943 * does filter out the key event and some client message for
1944 * the input method too.
1946 if (XFilterEvent(&ev
, None
))
1948 if (ev
.type
== ConfigureNotify
) {
1949 w
= ev
.xconfigure
.width
;
1950 h
= ev
.xconfigure
.height
;
1952 } while (ev
.type
!= MapNotify
);
1954 ttyfd
= ttynew(opt_line
, shell
, opt_io
, opt_cmd
);
1957 for (timeout
= -1, drawing
= 0, lastblink
= (struct timespec
){0};;) {
1959 FD_SET(ttyfd
, &rfd
);
1962 if (XPending(xw
.dpy
))
1963 timeout
= 0; /* existing events might not set xfd */
1965 seltv
.tv_sec
= timeout
/ 1E3
;
1966 seltv
.tv_nsec
= 1E6
* (timeout
- 1E3
* seltv
.tv_sec
);
1967 tv
= timeout
>= 0 ? &seltv
: NULL
;
1969 if (pselect(MAX(xfd
, ttyfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
1972 die("select failed: %s\n", strerror(errno
));
1974 clock_gettime(CLOCK_MONOTONIC
, &now
);
1976 if (FD_ISSET(ttyfd
, &rfd
))
1980 while (XPending(xw
.dpy
)) {
1982 XNextEvent(xw
.dpy
, &ev
);
1983 if (XFilterEvent(&ev
, None
))
1985 if (handler
[ev
.type
])
1986 (handler
[ev
.type
])(&ev
);
1990 * To reduce flicker and tearing, when new content or event
1991 * triggers drawing, we first wait a bit to ensure we got
1992 * everything, and if nothing new arrives - we draw.
1993 * We start with trying to wait minlatency ms. If more content
1994 * arrives sooner, we retry with shorter and shorter periods,
1995 * and eventually draw even without idle after maxlatency ms.
1996 * Typically this results in low latency while interacting,
1997 * maximum latency intervals during `cat huge.txt`, and perfect
1998 * sync with periodic updates from animations/key-repeats/etc.
2000 if (FD_ISSET(ttyfd
, &rfd
) || xev
) {
2005 timeout
= (maxlatency
- TIMEDIFF(now
, trigger
)) \
2006 / maxlatency
* minlatency
;
2008 continue; /* we have time, try to find idle */
2011 /* idle detected or maxlatency exhausted -> draw */
2013 if (blinktimeout
&& tattrset(ATTR_BLINK
)) {
2014 timeout
= blinktimeout
- TIMEDIFF(now
, lastblink
);
2016 if (-timeout
> blinktimeout
) /* start visible */
2017 win
.mode
|= MODE_BLINK
;
2018 win
.mode
^= MODE_BLINK
;
2019 tsetdirtattr(ATTR_BLINK
);
2021 timeout
= blinktimeout
;
2034 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
2035 " [-n name] [-o file]\n"
2036 " [-T title] [-t title] [-w windowid]"
2037 " [[-e] command [args ...]]\n"
2038 " %s [-aiv] [-c class] [-f font] [-g geometry]"
2039 " [-n name] [-o file]\n"
2040 " [-T title] [-t title] [-w windowid] -l line"
2041 " [stty_args ...]\n", argv0
, argv0
);
2045 main(int argc
, char *argv
[])
2049 xsetcursor(cursorshape
);
2056 opt_alpha
= EARGF(usage());
2059 opt_class
= EARGF(usage());
2066 opt_font
= EARGF(usage());
2069 xw
.gm
= XParseGeometry(EARGF(usage()),
2070 &xw
.l
, &xw
.t
, &cols
, &rows
);
2076 opt_io
= EARGF(usage());
2079 opt_line
= EARGF(usage());
2082 opt_name
= EARGF(usage());
2086 opt_title
= EARGF(usage());
2089 opt_embed
= EARGF(usage());
2092 die("%s " VERSION
"\n", argv0
);
2099 if (argc
> 0) /* eat all remaining arguments */
2103 opt_title
= (opt_line
|| !opt_cmd
) ? "st" : opt_cmd
[0];
2105 setlocale(LC_CTYPE
, "");
2106 XSetLocaleModifiers("");
2107 cols
= MAX(cols
, 1);
2108 rows
= MAX(rows
, 1);