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 int oldbutton
= 3; /* button event on startup: 3 = release */
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
)
367 int len
, x
= evcol(e
), y
= evrow(e
),
368 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
;
373 if (e
->xbutton
.type
== MotionNotify
) {
374 if (x
== ox
&& y
== oy
)
376 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
378 /* MOUSE_MOTION: no reporting if no button is pressed */
379 if (IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
382 button
= oldbutton
+ 32;
386 if (!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
392 else if (button
>= 3)
395 if (e
->xbutton
.type
== ButtonPress
) {
399 } else if (e
->xbutton
.type
== ButtonRelease
) {
401 /* MODE_MOUSEX10: no button release reporting */
402 if (IS_SET(MODE_MOUSEX10
))
404 if (button
== 64 || button
== 65)
409 if (!IS_SET(MODE_MOUSEX10
)) {
410 button
+= ((state
& ShiftMask
) ? 4 : 0)
411 + ((state
& Mod4Mask
) ? 8 : 0)
412 + ((state
& ControlMask
) ? 16 : 0);
415 if (IS_SET(MODE_MOUSESGR
)) {
416 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
418 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
419 } else if (x
< 223 && y
< 223) {
420 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
421 32+button
, 32+x
+1, 32+y
+1);
426 ttywrite(buf
, len
, 0);
430 buttonmask(uint button
)
432 return button
== Button1
? Button1Mask
433 : button
== Button2
? Button2Mask
434 : button
== Button3
? Button3Mask
435 : button
== Button4
? Button4Mask
436 : button
== Button5
? Button5Mask
441 mouseaction(XEvent
*e
, uint release
)
445 /* ignore Button<N>mask for Button<N> - it's set on release */
446 uint state
= e
->xbutton
.state
& ~buttonmask(e
->xbutton
.button
);
448 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
449 if (ms
->release
== release
&&
450 ms
->button
== e
->xbutton
.button
&&
451 (match(ms
->mod
, state
) || /* exact or forced */
452 match(ms
->mod
, state
& ~forcemousemod
))) {
453 ms
->func(&(ms
->arg
));
467 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
472 if (mouseaction(e
, 0))
475 if (e
->xbutton
.button
== Button1
) {
477 * If the user clicks below predefined timeouts specific
478 * snapping behaviour is exposed.
480 clock_gettime(CLOCK_MONOTONIC
, &now
);
481 if (TIMEDIFF(now
, xsel
.tclick2
) <= tripleclicktimeout
) {
483 } else if (TIMEDIFF(now
, xsel
.tclick1
) <= doubleclicktimeout
) {
488 xsel
.tclick2
= xsel
.tclick1
;
491 selstart(evcol(e
), evrow(e
), snap
);
496 propnotify(XEvent
*e
)
498 XPropertyEvent
*xpev
;
499 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
501 xpev
= &e
->xproperty
;
502 if (xpev
->state
== PropertyNewValue
&&
503 (xpev
->atom
== XA_PRIMARY
||
504 xpev
->atom
== clipboard
)) {
512 ulong nitems
, ofs
, rem
;
514 uchar
*data
, *last
, *repl
;
515 Atom type
, incratom
, property
= None
;
517 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
520 if (e
->type
== SelectionNotify
)
521 property
= e
->xselection
.property
;
522 else if (e
->type
== PropertyNotify
)
523 property
= e
->xproperty
.atom
;
525 if (property
== None
)
529 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
530 BUFSIZ
/4, False
, AnyPropertyType
,
531 &type
, &format
, &nitems
, &rem
,
533 fprintf(stderr
, "Clipboard allocation failed\n");
537 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
539 * If there is some PropertyNotify with no data, then
540 * this is the signal of the selection owner that all
541 * data has been transferred. We won't need to receive
542 * PropertyNotify events anymore.
544 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
545 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
549 if (type
== incratom
) {
551 * Activate the PropertyNotify events so we receive
552 * when the selection owner does send us the next
555 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
556 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
560 * Deleting the property is the transfer start signal.
562 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
568 * Line endings are inconsistent in the terminal and GUI world
569 * copy and pasting. When receiving some selection data,
570 * replace all '\n' with '\r'.
571 * FIXME: Fix the computer world.
574 last
= data
+ nitems
* format
/ 8;
575 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
579 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
580 ttywrite("\033[200~", 6, 0);
581 ttywrite((char *)data
, nitems
* format
/ 8, 1);
582 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
583 ttywrite("\033[201~", 6, 0);
585 /* number of 32-bit chunks returned */
586 ofs
+= nitems
* format
/ 32;
590 * Deleting the property again tells the selection owner to send the
591 * next data chunk in the property.
593 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
609 selrequest(XEvent
*e
)
611 XSelectionRequestEvent
*xsre
;
613 Atom xa_targets
, string
, clipboard
;
616 xsre
= (XSelectionRequestEvent
*) e
;
617 xev
.type
= SelectionNotify
;
618 xev
.requestor
= xsre
->requestor
;
619 xev
.selection
= xsre
->selection
;
620 xev
.target
= xsre
->target
;
621 xev
.time
= xsre
->time
;
622 if (xsre
->property
== None
)
623 xsre
->property
= xsre
->target
;
628 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
629 if (xsre
->target
== xa_targets
) {
630 /* respond with the supported type */
631 string
= xsel
.xtarget
;
632 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
633 XA_ATOM
, 32, PropModeReplace
,
634 (uchar
*) &string
, 1);
635 xev
.property
= xsre
->property
;
636 } else if (xsre
->target
== xsel
.xtarget
|| xsre
->target
== XA_STRING
) {
638 * xith XA_STRING non ascii characters may be incorrect in the
639 * requestor. It is not our problem, use utf8.
641 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
642 if (xsre
->selection
== XA_PRIMARY
) {
643 seltext
= xsel
.primary
;
644 } else if (xsre
->selection
== clipboard
) {
645 seltext
= xsel
.clipboard
;
648 "Unhandled clipboard selection 0x%lx\n",
652 if (seltext
!= NULL
) {
653 XChangeProperty(xsre
->display
, xsre
->requestor
,
654 xsre
->property
, xsre
->target
,
656 (uchar
*)seltext
, strlen(seltext
));
657 xev
.property
= xsre
->property
;
661 /* all done, send a notification to the listener */
662 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
663 fprintf(stderr
, "Error sending SelectionNotify event\n");
667 setsel(char *str
, Time t
)
675 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
676 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
683 setsel(str
, CurrentTime
);
689 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
694 if (mouseaction(e
, 1))
696 if (e
->xbutton
.button
== Button1
)
703 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
712 cresize(int width
, int height
)
721 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
722 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
728 ttyresize(win
.tw
, win
.th
);
732 xresize(int col
, int row
)
734 win
.tw
= col
* win
.cw
;
735 win
.th
= row
* win
.ch
;
737 XFreePixmap(xw
.dpy
, xw
.buf
);
738 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
739 DefaultDepth(xw
.dpy
, xw
.scr
));
740 XftDrawChange(xw
.draw
, xw
.buf
);
741 xclear(0, 0, win
.w
, win
.h
);
743 /* resize to new width */
744 xw
.specbuf
= xrealloc(xw
.specbuf
, col
* sizeof(GlyphFontSpec
));
750 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
754 xloadcolor(int i
, const char *name
, Color
*ncolor
)
756 XRenderColor color
= { .alpha
= 0xffff };
759 if (BETWEEN(i
, 16, 255)) { /* 256 color */
760 if (i
< 6*6*6+16) { /* same colors as xterm */
761 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
762 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
763 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
764 } else { /* greyscale */
765 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
766 color
.green
= color
.blue
= color
.red
;
768 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
769 xw
.cmap
, &color
, ncolor
);
774 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
785 for (cp
= dc
.col
; cp
< &dc
.col
[dc
.collen
]; ++cp
)
786 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
788 dc
.collen
= MAX(LEN(colorname
), 256);
789 dc
.col
= xmalloc(dc
.collen
* sizeof(Color
));
792 for (i
= 0; i
< dc
.collen
; i
++)
793 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
795 die("could not allocate color '%s'\n", colorname
[i
]);
797 die("could not allocate color %d\n", i
);
803 xsetcolorname(int x
, const char *name
)
807 if (!BETWEEN(x
, 0, dc
.collen
))
810 if (!xloadcolor(x
, name
, &ncolor
))
813 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
820 * Absolute coordinates.
823 xclear(int x1
, int y1
, int x2
, int y2
)
826 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
827 x1
, y1
, x2
-x1
, y2
-y1
);
833 XClassHint
class = {opt_name
? opt_name
: termname
,
834 opt_class
? opt_class
: termname
};
835 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
838 sizeh
= XAllocSizeHints();
840 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
| PMinSize
;
841 sizeh
->height
= win
.h
;
842 sizeh
->width
= win
.w
;
843 sizeh
->height_inc
= win
.ch
;
844 sizeh
->width_inc
= win
.cw
;
845 sizeh
->base_height
= 2 * borderpx
;
846 sizeh
->base_width
= 2 * borderpx
;
847 sizeh
->min_height
= win
.ch
+ 2 * borderpx
;
848 sizeh
->min_width
= win
.cw
+ 2 * borderpx
;
850 sizeh
->flags
|= PMaxSize
;
851 sizeh
->min_width
= sizeh
->max_width
= win
.w
;
852 sizeh
->min_height
= sizeh
->max_height
= win
.h
;
854 if (xw
.gm
& (XValue
|YValue
)) {
855 sizeh
->flags
|= USPosition
| PWinGravity
;
858 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
861 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
867 xgeommasktogravity(int mask
)
869 switch (mask
& (XNegative
|YNegative
)) {
871 return NorthWestGravity
;
873 return NorthEastGravity
;
875 return SouthWestGravity
;
878 return SouthEastGravity
;
882 xloadfont(Font
*f
, FcPattern
*pattern
)
884 FcPattern
*configured
;
888 int wantattr
, haveattr
;
891 * Manually configure instead of calling XftMatchFont
892 * so that we can use the configured pattern for
893 * "missing glyph" lookups.
895 configured
= FcPatternDuplicate(pattern
);
899 FcConfigSubstitute(NULL
, configured
, FcMatchPattern
);
900 XftDefaultSubstitute(xw
.dpy
, xw
.scr
, configured
);
902 match
= FcFontMatch(NULL
, configured
, &result
);
904 FcPatternDestroy(configured
);
908 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
909 FcPatternDestroy(configured
);
910 FcPatternDestroy(match
);
914 if ((XftPatternGetInteger(pattern
, "slant", 0, &wantattr
) ==
917 * Check if xft was unable to find a font with the appropriate
918 * slant but gave us one anyway. Try to mitigate.
920 if ((XftPatternGetInteger(f
->match
->pattern
, "slant", 0,
921 &haveattr
) != XftResultMatch
) || haveattr
< wantattr
) {
923 fputs("font slant does not match\n", stderr
);
927 if ((XftPatternGetInteger(pattern
, "weight", 0, &wantattr
) ==
929 if ((XftPatternGetInteger(f
->match
->pattern
, "weight", 0,
930 &haveattr
) != XftResultMatch
) || haveattr
!= wantattr
) {
932 fputs("font weight does not match\n", stderr
);
936 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
937 (const FcChar8
*) ascii_printable
,
938 strlen(ascii_printable
), &extents
);
941 f
->pattern
= configured
;
943 f
->ascent
= f
->match
->ascent
;
944 f
->descent
= f
->match
->descent
;
946 f
->rbearing
= f
->match
->max_advance_width
;
948 f
->height
= f
->ascent
+ f
->descent
;
949 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
955 xloadfonts(const char *fontstr
, double fontsize
)
960 if (fontstr
[0] == '-')
961 pattern
= XftXlfdParse(fontstr
, False
, False
);
963 pattern
= FcNameParse((const FcChar8
*)fontstr
);
966 die("can't open font %s\n", fontstr
);
969 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
970 FcPatternDel(pattern
, FC_SIZE
);
971 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
972 usedfontsize
= fontsize
;
974 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
976 usedfontsize
= fontval
;
977 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
982 * Default font size is 12, if none given. This is to
983 * have a known usedfontsize value.
985 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
988 defaultfontsize
= usedfontsize
;
991 if (xloadfont(&dc
.font
, pattern
))
992 die("can't open font %s\n", fontstr
);
994 if (usedfontsize
< 0) {
995 FcPatternGetDouble(dc
.font
.match
->pattern
,
996 FC_PIXEL_SIZE
, 0, &fontval
);
997 usedfontsize
= fontval
;
999 defaultfontsize
= fontval
;
1002 /* Setting character width and height. */
1003 win
.cw
= ceilf(dc
.font
.width
* cwscale
);
1004 win
.ch
= ceilf(dc
.font
.height
* chscale
);
1006 FcPatternDel(pattern
, FC_SLANT
);
1007 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
1008 if (xloadfont(&dc
.ifont
, pattern
))
1009 die("can't open font %s\n", fontstr
);
1011 FcPatternDel(pattern
, FC_WEIGHT
);
1012 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
1013 if (xloadfont(&dc
.ibfont
, pattern
))
1014 die("can't open font %s\n", fontstr
);
1016 FcPatternDel(pattern
, FC_SLANT
);
1017 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
1018 if (xloadfont(&dc
.bfont
, pattern
))
1019 die("can't open font %s\n", fontstr
);
1021 FcPatternDestroy(pattern
);
1025 xunloadfont(Font
*f
)
1027 XftFontClose(xw
.dpy
, f
->match
);
1028 FcPatternDestroy(f
->pattern
);
1030 FcFontSetDestroy(f
->set
);
1036 /* Free the loaded fonts in the font cache. */
1038 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
1040 xunloadfont(&dc
.font
);
1041 xunloadfont(&dc
.bfont
);
1042 xunloadfont(&dc
.ifont
);
1043 xunloadfont(&dc
.ibfont
);
1047 ximopen(Display
*dpy
)
1049 XIMCallback imdestroy
= { .client_data
= NULL
, .callback
= ximdestroy
};
1050 XICCallback icdestroy
= { .client_data
= NULL
, .callback
= xicdestroy
};
1052 xw
.ime
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
1053 if (xw
.ime
.xim
== NULL
)
1056 if (XSetIMValues(xw
.ime
.xim
, XNDestroyCallback
, &imdestroy
, NULL
))
1057 fprintf(stderr
, "XSetIMValues: "
1058 "Could not set XNDestroyCallback.\n");
1060 xw
.ime
.spotlist
= XVaCreateNestedList(0, XNSpotLocation
, &xw
.ime
.spot
,
1063 if (xw
.ime
.xic
== NULL
) {
1064 xw
.ime
.xic
= XCreateIC(xw
.ime
.xim
, XNInputStyle
,
1065 XIMPreeditNothing
| XIMStatusNothing
,
1066 XNClientWindow
, xw
.win
,
1067 XNDestroyCallback
, &icdestroy
,
1070 if (xw
.ime
.xic
== NULL
)
1071 fprintf(stderr
, "XCreateIC: Could not create input context.\n");
1077 ximinstantiate(Display
*dpy
, XPointer client
, XPointer call
)
1080 XUnregisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1081 ximinstantiate
, NULL
);
1085 ximdestroy(XIM xim
, XPointer client
, XPointer call
)
1088 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1089 ximinstantiate
, NULL
);
1090 XFree(xw
.ime
.spotlist
);
1094 xicdestroy(XIC xim
, XPointer client
, XPointer call
)
1101 xinit(int cols
, int rows
)
1106 pid_t thispid
= getpid();
1107 XColor xmousefg
, xmousebg
;
1109 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
1110 die("can't open display\n");
1111 xw
.scr
= XDefaultScreen(xw
.dpy
);
1112 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
1116 die("could not init fontconfig.\n");
1118 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
1119 xloadfonts(usedfont
, 0);
1122 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1125 /* adjust fixed window geometry */
1126 win
.w
= 2 * borderpx
+ cols
* win
.cw
;
1127 win
.h
= 2 * borderpx
+ rows
* win
.ch
;
1128 if (xw
.gm
& XNegative
)
1129 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - win
.w
- 2;
1130 if (xw
.gm
& YNegative
)
1131 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - win
.h
- 2;
1134 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
1135 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
1136 xw
.attrs
.bit_gravity
= NorthWestGravity
;
1137 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
| KeyReleaseMask
1138 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1139 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1140 xw
.attrs
.colormap
= xw
.cmap
;
1142 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
1143 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
1144 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
1145 win
.w
, win
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1146 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
1147 | CWEventMask
| CWColormap
, &xw
.attrs
);
1149 memset(&gcvalues
, 0, sizeof(gcvalues
));
1150 gcvalues
.graphics_exposures
= False
;
1151 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
1153 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
1154 DefaultDepth(xw
.dpy
, xw
.scr
));
1155 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
1156 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, win
.w
, win
.h
);
1158 /* font spec buffer */
1159 xw
.specbuf
= xmalloc(cols
* sizeof(GlyphFontSpec
));
1161 /* Xft rendering context */
1162 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
1165 if (!ximopen(xw
.dpy
)) {
1166 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1167 ximinstantiate
, NULL
);
1170 /* white cursor, black outline */
1171 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
1172 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1174 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
1175 xmousefg
.red
= 0xffff;
1176 xmousefg
.green
= 0xffff;
1177 xmousefg
.blue
= 0xffff;
1180 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
1181 xmousebg
.red
= 0x0000;
1182 xmousebg
.green
= 0x0000;
1183 xmousebg
.blue
= 0x0000;
1186 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
1188 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1189 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
1190 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
1191 xw
.netwmiconname
= XInternAtom(xw
.dpy
, "_NET_WM_ICON_NAME", False
);
1192 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
1194 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
1195 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
1196 PropModeReplace
, (uchar
*)&thispid
, 1);
1198 win
.mode
= MODE_NUMLOCK
;
1201 XMapWindow(xw
.dpy
, xw
.win
);
1202 XSync(xw
.dpy
, False
);
1204 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick1
);
1205 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick2
);
1206 xsel
.primary
= NULL
;
1207 xsel
.clipboard
= NULL
;
1208 xsel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
1209 if (xsel
.xtarget
== None
)
1210 xsel
.xtarget
= XA_STRING
;
1214 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
1216 float winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
, xp
, yp
;
1217 ushort mode
, prevmode
= USHRT_MAX
;
1218 Font
*font
= &dc
.font
;
1219 int frcflags
= FRC_NORMAL
;
1220 float runewidth
= win
.cw
;
1224 FcPattern
*fcpattern
, *fontpattern
;
1225 FcFontSet
*fcsets
[] = { NULL
};
1226 FcCharSet
*fccharset
;
1227 int i
, f
, numspecs
= 0;
1229 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
1230 /* Fetch rune and mode for current glyph. */
1232 mode
= glyphs
[i
].mode
;
1234 /* Skip dummy wide-character spacing. */
1235 if (mode
== ATTR_WDUMMY
)
1238 /* Determine font for glyph if different from previous glyph. */
1239 if (prevmode
!= mode
) {
1242 frcflags
= FRC_NORMAL
;
1243 runewidth
= win
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
1244 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
1246 frcflags
= FRC_ITALICBOLD
;
1247 } else if (mode
& ATTR_ITALIC
) {
1249 frcflags
= FRC_ITALIC
;
1250 } else if (mode
& ATTR_BOLD
) {
1252 frcflags
= FRC_BOLD
;
1254 yp
= winy
+ font
->ascent
;
1257 /* Lookup character index with default font. */
1258 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
1260 specs
[numspecs
].font
= font
->match
;
1261 specs
[numspecs
].glyph
= glyphidx
;
1262 specs
[numspecs
].x
= (short)xp
;
1263 specs
[numspecs
].y
= (short)yp
;
1269 /* Fallback on font cache, search the font cache for match. */
1270 for (f
= 0; f
< frclen
; f
++) {
1271 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
1272 /* Everything correct. */
1273 if (glyphidx
&& frc
[f
].flags
== frcflags
)
1275 /* We got a default font for a not found glyph. */
1276 if (!glyphidx
&& frc
[f
].flags
== frcflags
1277 && frc
[f
].unicodep
== rune
) {
1282 /* Nothing was found. Use fontconfig to find matching font. */
1285 font
->set
= FcFontSort(0, font
->pattern
,
1287 fcsets
[0] = font
->set
;
1290 * Nothing was found in the cache. Now use
1291 * some dozen of Fontconfig calls to get the
1292 * font for one single character.
1294 * Xft and fontconfig are design failures.
1296 fcpattern
= FcPatternDuplicate(font
->pattern
);
1297 fccharset
= FcCharSetCreate();
1299 FcCharSetAddChar(fccharset
, rune
);
1300 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
1302 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
1304 FcConfigSubstitute(0, fcpattern
,
1306 FcDefaultSubstitute(fcpattern
);
1308 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
1311 /* Allocate memory for the new cache entry. */
1312 if (frclen
>= frccap
) {
1314 frc
= xrealloc(frc
, frccap
* sizeof(Fontcache
));
1317 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
1319 if (!frc
[frclen
].font
)
1320 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1322 frc
[frclen
].flags
= frcflags
;
1323 frc
[frclen
].unicodep
= rune
;
1325 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
1330 FcPatternDestroy(fcpattern
);
1331 FcCharSetDestroy(fccharset
);
1334 specs
[numspecs
].font
= frc
[f
].font
;
1335 specs
[numspecs
].glyph
= glyphidx
;
1336 specs
[numspecs
].x
= (short)xp
;
1337 specs
[numspecs
].y
= (short)yp
;
1346 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
1348 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
1349 int winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
,
1350 width
= charlen
* win
.cw
;
1351 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
1352 XRenderColor colfg
, colbg
;
1355 /* Fallback on color display for attributes not supported by the font */
1356 if (base
.mode
& ATTR_ITALIC
&& base
.mode
& ATTR_BOLD
) {
1357 if (dc
.ibfont
.badslant
|| dc
.ibfont
.badweight
)
1358 base
.fg
= defaultattr
;
1359 } else if ((base
.mode
& ATTR_ITALIC
&& dc
.ifont
.badslant
) ||
1360 (base
.mode
& ATTR_BOLD
&& dc
.bfont
.badweight
)) {
1361 base
.fg
= defaultattr
;
1364 if (IS_TRUECOL(base
.fg
)) {
1365 colfg
.alpha
= 0xffff;
1366 colfg
.red
= TRUERED(base
.fg
);
1367 colfg
.green
= TRUEGREEN(base
.fg
);
1368 colfg
.blue
= TRUEBLUE(base
.fg
);
1369 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
1372 fg
= &dc
.col
[base
.fg
];
1375 if (IS_TRUECOL(base
.bg
)) {
1376 colbg
.alpha
= 0xffff;
1377 colbg
.green
= TRUEGREEN(base
.bg
);
1378 colbg
.red
= TRUERED(base
.bg
);
1379 colbg
.blue
= TRUEBLUE(base
.bg
);
1380 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
1383 bg
= &dc
.col
[base
.bg
];
1386 /* Change basic system colors [0-7] to bright system colors [8-15] */
1387 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
1388 fg
= &dc
.col
[base
.fg
+ 8];
1390 if (IS_SET(MODE_REVERSE
)) {
1391 if (fg
== &dc
.col
[defaultfg
]) {
1392 fg
= &dc
.col
[defaultbg
];
1394 colfg
.red
= ~fg
->color
.red
;
1395 colfg
.green
= ~fg
->color
.green
;
1396 colfg
.blue
= ~fg
->color
.blue
;
1397 colfg
.alpha
= fg
->color
.alpha
;
1398 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
1403 if (bg
== &dc
.col
[defaultbg
]) {
1404 bg
= &dc
.col
[defaultfg
];
1406 colbg
.red
= ~bg
->color
.red
;
1407 colbg
.green
= ~bg
->color
.green
;
1408 colbg
.blue
= ~bg
->color
.blue
;
1409 colbg
.alpha
= bg
->color
.alpha
;
1410 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
1416 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
1417 colfg
.red
= fg
->color
.red
/ 2;
1418 colfg
.green
= fg
->color
.green
/ 2;
1419 colfg
.blue
= fg
->color
.blue
/ 2;
1420 colfg
.alpha
= fg
->color
.alpha
;
1421 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
1425 if (base
.mode
& ATTR_REVERSE
) {
1431 if (base
.mode
& ATTR_BLINK
&& win
.mode
& MODE_BLINK
)
1434 if (base
.mode
& ATTR_INVISIBLE
)
1437 /* Intelligent cleaning up of the borders. */
1439 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
1441 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: 0));
1443 if (winx
+ width
>= borderpx
+ win
.tw
) {
1444 xclear(winx
+ width
, (y
== 0)? 0 : winy
, win
.w
,
1445 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: (winy
+ win
.ch
)));
1448 xclear(winx
, 0, winx
+ width
, borderpx
);
1449 if (winy
+ win
.ch
>= borderpx
+ win
.th
)
1450 xclear(winx
, winy
+ win
.ch
, winx
+ width
, win
.h
);
1452 /* Clean up the region we want to draw to. */
1453 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, win
.ch
);
1455 /* Set the clip region because Xft is sometimes dirty. */
1460 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
1462 /* Render the glyphs. */
1463 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
1465 /* Render underline and strikethrough. */
1466 if (base
.mode
& ATTR_UNDERLINE
) {
1467 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
1471 if (base
.mode
& ATTR_STRUCK
) {
1472 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
1476 /* Reset clip to none. */
1477 XftDrawSetClip(xw
.draw
, 0);
1481 xdrawglyph(Glyph g
, int x
, int y
)
1484 XftGlyphFontSpec spec
;
1486 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
1487 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
1491 xdrawcursor(int cx
, int cy
, Glyph g
, int ox
, int oy
, Glyph og
)
1495 /* remove the old cursor */
1496 if (selected(ox
, oy
))
1497 og
.mode
^= ATTR_REVERSE
;
1498 xdrawglyph(og
, ox
, oy
);
1500 if (IS_SET(MODE_HIDE
))
1504 * Select the right color for the right mode.
1506 g
.mode
&= ATTR_BOLD
|ATTR_ITALIC
|ATTR_UNDERLINE
|ATTR_STRUCK
|ATTR_WIDE
;
1508 if (IS_SET(MODE_REVERSE
)) {
1509 g
.mode
|= ATTR_REVERSE
;
1511 if (selected(cx
, cy
)) {
1512 drawcol
= dc
.col
[defaultcs
];
1515 drawcol
= dc
.col
[defaultrcs
];
1519 if (selected(cx
, cy
)) {
1526 drawcol
= dc
.col
[g
.bg
];
1529 /* draw the new one */
1530 if (IS_SET(MODE_FOCUSED
)) {
1531 switch (win
.cursor
) {
1532 case 7: /* st extension */
1533 g
.u
= 0x2603; /* snowman (U+2603) */
1535 case 0: /* Blinking Block */
1536 case 1: /* Blinking Block (Default) */
1537 case 2: /* Steady Block */
1538 xdrawglyph(g
, cx
, cy
);
1540 case 3: /* Blinking Underline */
1541 case 4: /* Steady Underline */
1542 XftDrawRect(xw
.draw
, &drawcol
,
1543 borderpx
+ cx
* win
.cw
,
1544 borderpx
+ (cy
+ 1) * win
.ch
- \
1546 win
.cw
, cursorthickness
);
1548 case 5: /* Blinking bar */
1549 case 6: /* Steady bar */
1550 XftDrawRect(xw
.draw
, &drawcol
,
1551 borderpx
+ cx
* win
.cw
,
1552 borderpx
+ cy
* win
.ch
,
1553 cursorthickness
, win
.ch
);
1557 XftDrawRect(xw
.draw
, &drawcol
,
1558 borderpx
+ cx
* win
.cw
,
1559 borderpx
+ cy
* win
.ch
,
1561 XftDrawRect(xw
.draw
, &drawcol
,
1562 borderpx
+ cx
* win
.cw
,
1563 borderpx
+ cy
* win
.ch
,
1565 XftDrawRect(xw
.draw
, &drawcol
,
1566 borderpx
+ (cx
+ 1) * win
.cw
- 1,
1567 borderpx
+ cy
* win
.ch
,
1569 XftDrawRect(xw
.draw
, &drawcol
,
1570 borderpx
+ cx
* win
.cw
,
1571 borderpx
+ (cy
+ 1) * win
.ch
- 1,
1579 char buf
[sizeof(long) * 8 + 1];
1581 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1582 setenv("WINDOWID", buf
, 1);
1586 xseticontitle(char *p
)
1589 DEFAULT(p
, opt_title
);
1591 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1593 XSetWMIconName(xw
.dpy
, xw
.win
, &prop
);
1594 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmiconname
);
1602 DEFAULT(p
, opt_title
);
1604 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1606 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
1607 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
1614 return IS_SET(MODE_VISIBLE
);
1618 xdrawline(Line line
, int x1
, int y1
, int x2
)
1620 int i
, x
, ox
, numspecs
;
1622 XftGlyphFontSpec
*specs
= xw
.specbuf
;
1624 numspecs
= xmakeglyphfontspecs(specs
, &line
[x1
], x2
- x1
, x1
, y1
);
1626 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
1628 if (new.mode
== ATTR_WDUMMY
)
1630 if (selected(x
, y1
))
1631 new.mode
^= ATTR_REVERSE
;
1632 if (i
> 0 && ATTRCMP(base
, new)) {
1633 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1645 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1651 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, win
.w
,
1653 XSetForeground(xw
.dpy
, dc
.gc
,
1654 dc
.col
[IS_SET(MODE_REVERSE
)?
1655 defaultfg
: defaultbg
].pixel
);
1659 xximspot(int x
, int y
)
1661 if (xw
.ime
.xic
== NULL
)
1664 xw
.ime
.spot
.x
= borderpx
+ x
* win
.cw
;
1665 xw
.ime
.spot
.y
= borderpx
+ (y
+ 1) * win
.ch
;
1667 XSetICValues(xw
.ime
.xic
, XNPreeditAttributes
, xw
.ime
.spotlist
, NULL
);
1677 visibility(XEvent
*ev
)
1679 XVisibilityEvent
*e
= &ev
->xvisibility
;
1681 MODBIT(win
.mode
, e
->state
!= VisibilityFullyObscured
, MODE_VISIBLE
);
1687 win
.mode
&= ~MODE_VISIBLE
;
1691 xsetpointermotion(int set
)
1693 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
1694 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
1698 xsetmode(int set
, unsigned int flags
)
1700 int mode
= win
.mode
;
1701 MODBIT(win
.mode
, set
, flags
);
1702 if ((win
.mode
& MODE_REVERSE
) != (mode
& MODE_REVERSE
))
1707 xsetcursor(int cursor
)
1709 if (!BETWEEN(cursor
, 0, 7)) /* 7: st extension */
1711 win
.cursor
= cursor
;
1716 xseturgency(int add
)
1718 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1720 MODBIT(h
->flags
, add
, XUrgencyHint
);
1721 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1728 if (!(IS_SET(MODE_FOCUSED
)))
1731 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
1737 XFocusChangeEvent
*e
= &ev
->xfocus
;
1739 if (e
->mode
== NotifyGrab
)
1742 if (ev
->type
== FocusIn
) {
1744 XSetICFocus(xw
.ime
.xic
);
1745 win
.mode
|= MODE_FOCUSED
;
1747 if (IS_SET(MODE_FOCUS
))
1748 ttywrite("\033[I", 3, 0);
1751 XUnsetICFocus(xw
.ime
.xic
);
1752 win
.mode
&= ~MODE_FOCUSED
;
1753 if (IS_SET(MODE_FOCUS
))
1754 ttywrite("\033[O", 3, 0);
1759 match(uint mask
, uint state
)
1761 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
1765 kmap(KeySym k
, uint state
)
1770 /* Check for mapped keys out of X11 function keys. */
1771 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
1772 if (mappedkeys
[i
] == k
)
1775 if (i
== LEN(mappedkeys
)) {
1776 if ((k
& 0xFFFF) < 0xFD00)
1780 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
1784 if (!match(kp
->mask
, state
))
1787 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
1789 if (IS_SET(MODE_NUMLOCK
) && kp
->appkey
== 2)
1792 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
1804 XKeyEvent
*e
= &ev
->xkey
;
1806 char buf
[64], *customkey
;
1812 if (IS_SET(MODE_KBDLOCK
))
1816 len
= XmbLookupString(xw
.ime
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
1818 len
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
1820 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
1821 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
1822 bp
->func(&(bp
->arg
));
1827 /* 2. custom keys from config.h */
1828 if ((customkey
= kmap(ksym
, e
->state
))) {
1829 ttywrite(customkey
, strlen(customkey
), 1);
1833 /* 3. composed string from input method */
1836 if (len
== 1 && e
->state
& Mod1Mask
) {
1837 if (IS_SET(MODE_8BIT
)) {
1840 len
= utf8encode(c
, buf
);
1848 ttywrite(buf
, len
, 1);
1856 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1858 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1859 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1860 win
.mode
|= MODE_FOCUSED
;
1862 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1863 win
.mode
&= ~MODE_FOCUSED
;
1865 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
1874 if (e
->xconfigure
.width
== win
.w
&& e
->xconfigure
.height
== win
.h
)
1877 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
1884 int w
= win
.w
, h
= win
.h
;
1886 int xfd
= XConnectionNumber(xw
.dpy
), ttyfd
, xev
, drawing
;
1887 struct timespec seltv
, *tv
, now
, lastblink
, trigger
;
1890 /* Waiting for window mapping */
1892 XNextEvent(xw
.dpy
, &ev
);
1894 * This XFilterEvent call is required because of XOpenIM. It
1895 * does filter out the key event and some client message for
1896 * the input method too.
1898 if (XFilterEvent(&ev
, None
))
1900 if (ev
.type
== ConfigureNotify
) {
1901 w
= ev
.xconfigure
.width
;
1902 h
= ev
.xconfigure
.height
;
1904 } while (ev
.type
!= MapNotify
);
1906 ttyfd
= ttynew(opt_line
, shell
, opt_io
, opt_cmd
);
1909 for (timeout
= -1, drawing
= 0, lastblink
= (struct timespec
){0};;) {
1911 FD_SET(ttyfd
, &rfd
);
1914 if (XPending(xw
.dpy
))
1915 timeout
= 0; /* existing events might not set xfd */
1917 seltv
.tv_sec
= timeout
/ 1E3
;
1918 seltv
.tv_nsec
= 1E6
* (timeout
- 1E3
* seltv
.tv_sec
);
1919 tv
= timeout
>= 0 ? &seltv
: NULL
;
1921 if (pselect(MAX(xfd
, ttyfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
1924 die("select failed: %s\n", strerror(errno
));
1926 clock_gettime(CLOCK_MONOTONIC
, &now
);
1928 if (FD_ISSET(ttyfd
, &rfd
))
1932 while (XPending(xw
.dpy
)) {
1934 XNextEvent(xw
.dpy
, &ev
);
1935 if (XFilterEvent(&ev
, None
))
1937 if (handler
[ev
.type
])
1938 (handler
[ev
.type
])(&ev
);
1942 * To reduce flicker and tearing, when new content or event
1943 * triggers drawing, we first wait a bit to ensure we got
1944 * everything, and if nothing new arrives - we draw.
1945 * We start with trying to wait minlatency ms. If more content
1946 * arrives sooner, we retry with shorter and shorter periods,
1947 * and eventually draw even without idle after maxlatency ms.
1948 * Typically this results in low latency while interacting,
1949 * maximum latency intervals during `cat huge.txt`, and perfect
1950 * sync with periodic updates from animations/key-repeats/etc.
1952 if (FD_ISSET(ttyfd
, &rfd
) || xev
) {
1957 timeout
= (maxlatency
- TIMEDIFF(now
, trigger
)) \
1958 / maxlatency
* minlatency
;
1960 continue; /* we have time, try to find idle */
1963 /* idle detected or maxlatency exhausted -> draw */
1965 if (blinktimeout
&& tattrset(ATTR_BLINK
)) {
1966 timeout
= blinktimeout
- TIMEDIFF(now
, lastblink
);
1968 if (-timeout
> blinktimeout
) /* start visible */
1969 win
.mode
|= MODE_BLINK
;
1970 win
.mode
^= MODE_BLINK
;
1971 tsetdirtattr(ATTR_BLINK
);
1973 timeout
= blinktimeout
;
1986 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
1987 " [-n name] [-o file]\n"
1988 " [-T title] [-t title] [-w windowid]"
1989 " [[-e] command [args ...]]\n"
1990 " %s [-aiv] [-c class] [-f font] [-g geometry]"
1991 " [-n name] [-o file]\n"
1992 " [-T title] [-t title] [-w windowid] -l line"
1993 " [stty_args ...]\n", argv0
, argv0
);
1997 main(int argc
, char *argv
[])
2001 xsetcursor(cursorshape
);
2008 opt_class
= EARGF(usage());
2015 opt_font
= EARGF(usage());
2018 xw
.gm
= XParseGeometry(EARGF(usage()),
2019 &xw
.l
, &xw
.t
, &cols
, &rows
);
2025 opt_io
= EARGF(usage());
2028 opt_line
= EARGF(usage());
2031 opt_name
= EARGF(usage());
2035 opt_title
= EARGF(usage());
2038 opt_embed
= EARGF(usage());
2041 die("%s " VERSION
"\n", argv0
);
2048 if (argc
> 0) /* eat all remaining arguments */
2052 opt_title
= (opt_line
|| !opt_cmd
) ? "st" : opt_cmd
[0];
2054 setlocale(LC_CTYPE
, "");
2055 XSetLocaleModifiers("");
2056 cols
= MAX(cols
, 1);
2057 rows
= MAX(rows
, 1);