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)
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
, 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(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
) {
393 if (e
->xbutton
.type
== ButtonPress
) {
397 } else if (e
->xbutton
.type
== ButtonRelease
) {
399 /* MODE_MOUSEX10: no button release reporting */
400 if (IS_SET(MODE_MOUSEX10
))
402 if (button
== 64 || button
== 65)
407 if (!IS_SET(MODE_MOUSEX10
)) {
408 button
+= ((state
& ShiftMask
) ? 4 : 0)
409 + ((state
& Mod4Mask
) ? 8 : 0)
410 + ((state
& ControlMask
) ? 16 : 0);
413 if (IS_SET(MODE_MOUSESGR
)) {
414 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
416 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
417 } else if (x
< 223 && y
< 223) {
418 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
419 32+button
, 32+x
+1, 32+y
+1);
424 ttywrite(buf
, len
, 0);
428 buttonmask(uint button
)
430 return button
== Button1
? Button1Mask
431 : button
== Button2
? Button2Mask
432 : button
== Button3
? Button3Mask
433 : button
== Button4
? Button4Mask
434 : button
== Button5
? Button5Mask
439 mouseaction(XEvent
*e
, uint release
)
443 /* ignore Button<N>mask for Button<N> - it's set on release */
444 uint state
= e
->xbutton
.state
& ~buttonmask(e
->xbutton
.button
);
446 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
447 if (ms
->release
== release
&&
448 ms
->button
== e
->xbutton
.button
&&
449 (match(ms
->mod
, state
) || /* exact or forced */
450 match(ms
->mod
, state
& ~forcemousemod
))) {
451 ms
->func(&(ms
->arg
));
465 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
470 if (mouseaction(e
, 0))
473 if (e
->xbutton
.button
== Button1
) {
475 * If the user clicks below predefined timeouts specific
476 * snapping behaviour is exposed.
478 clock_gettime(CLOCK_MONOTONIC
, &now
);
479 if (TIMEDIFF(now
, xsel
.tclick2
) <= tripleclicktimeout
) {
481 } else if (TIMEDIFF(now
, xsel
.tclick1
) <= doubleclicktimeout
) {
486 xsel
.tclick2
= xsel
.tclick1
;
489 selstart(evcol(e
), evrow(e
), snap
);
494 propnotify(XEvent
*e
)
496 XPropertyEvent
*xpev
;
497 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
499 xpev
= &e
->xproperty
;
500 if (xpev
->state
== PropertyNewValue
&&
501 (xpev
->atom
== XA_PRIMARY
||
502 xpev
->atom
== clipboard
)) {
510 ulong nitems
, ofs
, rem
;
512 uchar
*data
, *last
, *repl
;
513 Atom type
, incratom
, property
= None
;
515 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
518 if (e
->type
== SelectionNotify
)
519 property
= e
->xselection
.property
;
520 else if (e
->type
== PropertyNotify
)
521 property
= e
->xproperty
.atom
;
523 if (property
== None
)
527 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
528 BUFSIZ
/4, False
, AnyPropertyType
,
529 &type
, &format
, &nitems
, &rem
,
531 fprintf(stderr
, "Clipboard allocation failed\n");
535 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
537 * If there is some PropertyNotify with no data, then
538 * this is the signal of the selection owner that all
539 * data has been transferred. We won't need to receive
540 * PropertyNotify events anymore.
542 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
543 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
547 if (type
== incratom
) {
549 * Activate the PropertyNotify events so we receive
550 * when the selection owner does send us the next
553 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
554 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
558 * Deleting the property is the transfer start signal.
560 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
566 * Line endings are inconsistent in the terminal and GUI world
567 * copy and pasting. When receiving some selection data,
568 * replace all '\n' with '\r'.
569 * FIXME: Fix the computer world.
572 last
= data
+ nitems
* format
/ 8;
573 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
577 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
578 ttywrite("\033[200~", 6, 0);
579 ttywrite((char *)data
, nitems
* format
/ 8, 1);
580 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
581 ttywrite("\033[201~", 6, 0);
583 /* number of 32-bit chunks returned */
584 ofs
+= nitems
* format
/ 32;
588 * Deleting the property again tells the selection owner to send the
589 * next data chunk in the property.
591 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
607 selrequest(XEvent
*e
)
609 XSelectionRequestEvent
*xsre
;
611 Atom xa_targets
, string
, clipboard
;
614 xsre
= (XSelectionRequestEvent
*) e
;
615 xev
.type
= SelectionNotify
;
616 xev
.requestor
= xsre
->requestor
;
617 xev
.selection
= xsre
->selection
;
618 xev
.target
= xsre
->target
;
619 xev
.time
= xsre
->time
;
620 if (xsre
->property
== None
)
621 xsre
->property
= xsre
->target
;
626 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
627 if (xsre
->target
== xa_targets
) {
628 /* respond with the supported type */
629 string
= xsel
.xtarget
;
630 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
631 XA_ATOM
, 32, PropModeReplace
,
632 (uchar
*) &string
, 1);
633 xev
.property
= xsre
->property
;
634 } else if (xsre
->target
== xsel
.xtarget
|| xsre
->target
== XA_STRING
) {
636 * xith XA_STRING non ascii characters may be incorrect in the
637 * requestor. It is not our problem, use utf8.
639 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
640 if (xsre
->selection
== XA_PRIMARY
) {
641 seltext
= xsel
.primary
;
642 } else if (xsre
->selection
== clipboard
) {
643 seltext
= xsel
.clipboard
;
646 "Unhandled clipboard selection 0x%lx\n",
650 if (seltext
!= NULL
) {
651 XChangeProperty(xsre
->display
, xsre
->requestor
,
652 xsre
->property
, xsre
->target
,
654 (uchar
*)seltext
, strlen(seltext
));
655 xev
.property
= xsre
->property
;
659 /* all done, send a notification to the listener */
660 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
661 fprintf(stderr
, "Error sending SelectionNotify event\n");
665 setsel(char *str
, Time t
)
673 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
674 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
681 setsel(str
, CurrentTime
);
687 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
692 if (mouseaction(e
, 1))
694 if (e
->xbutton
.button
== Button1
)
701 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
710 cresize(int width
, int height
)
719 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
720 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
726 ttyresize(win
.tw
, win
.th
);
730 xresize(int col
, int row
)
732 win
.tw
= col
* win
.cw
;
733 win
.th
= row
* win
.ch
;
735 XFreePixmap(xw
.dpy
, xw
.buf
);
736 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
737 DefaultDepth(xw
.dpy
, xw
.scr
));
738 XftDrawChange(xw
.draw
, xw
.buf
);
739 xclear(0, 0, win
.w
, win
.h
);
741 /* resize to new width */
742 xw
.specbuf
= xrealloc(xw
.specbuf
, col
* sizeof(GlyphFontSpec
));
748 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
752 xloadcolor(int i
, const char *name
, Color
*ncolor
)
754 XRenderColor color
= { .alpha
= 0xffff };
757 if (BETWEEN(i
, 16, 255)) { /* 256 color */
758 if (i
< 6*6*6+16) { /* same colors as xterm */
759 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
760 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
761 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
762 } else { /* greyscale */
763 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
764 color
.green
= color
.blue
= color
.red
;
766 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
767 xw
.cmap
, &color
, ncolor
);
772 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
783 for (cp
= dc
.col
; cp
< &dc
.col
[dc
.collen
]; ++cp
)
784 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
786 dc
.collen
= MAX(LEN(colorname
), 256);
787 dc
.col
= xmalloc(dc
.collen
* sizeof(Color
));
790 for (i
= 0; i
< dc
.collen
; i
++)
791 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
793 die("could not allocate color '%s'\n", colorname
[i
]);
795 die("could not allocate color %d\n", i
);
801 xsetcolorname(int x
, const char *name
)
805 if (!BETWEEN(x
, 0, dc
.collen
))
808 if (!xloadcolor(x
, name
, &ncolor
))
811 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
818 * Absolute coordinates.
821 xclear(int x1
, int y1
, int x2
, int y2
)
824 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
825 x1
, y1
, x2
-x1
, y2
-y1
);
831 XClassHint
class = {opt_name
? opt_name
: termname
,
832 opt_class
? opt_class
: termname
};
833 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
836 sizeh
= XAllocSizeHints();
838 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
| PMinSize
;
839 sizeh
->height
= win
.h
;
840 sizeh
->width
= win
.w
;
841 sizeh
->height_inc
= win
.ch
;
842 sizeh
->width_inc
= win
.cw
;
843 sizeh
->base_height
= 2 * borderpx
;
844 sizeh
->base_width
= 2 * borderpx
;
845 sizeh
->min_height
= win
.ch
+ 2 * borderpx
;
846 sizeh
->min_width
= win
.cw
+ 2 * borderpx
;
848 sizeh
->flags
|= PMaxSize
;
849 sizeh
->min_width
= sizeh
->max_width
= win
.w
;
850 sizeh
->min_height
= sizeh
->max_height
= win
.h
;
852 if (xw
.gm
& (XValue
|YValue
)) {
853 sizeh
->flags
|= USPosition
| PWinGravity
;
856 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
859 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
865 xgeommasktogravity(int mask
)
867 switch (mask
& (XNegative
|YNegative
)) {
869 return NorthWestGravity
;
871 return NorthEastGravity
;
873 return SouthWestGravity
;
876 return SouthEastGravity
;
880 xloadfont(Font
*f
, FcPattern
*pattern
)
882 FcPattern
*configured
;
886 int wantattr
, haveattr
;
889 * Manually configure instead of calling XftMatchFont
890 * so that we can use the configured pattern for
891 * "missing glyph" lookups.
893 configured
= FcPatternDuplicate(pattern
);
897 FcConfigSubstitute(NULL
, configured
, FcMatchPattern
);
898 XftDefaultSubstitute(xw
.dpy
, xw
.scr
, configured
);
900 match
= FcFontMatch(NULL
, configured
, &result
);
902 FcPatternDestroy(configured
);
906 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
907 FcPatternDestroy(configured
);
908 FcPatternDestroy(match
);
912 if ((XftPatternGetInteger(pattern
, "slant", 0, &wantattr
) ==
915 * Check if xft was unable to find a font with the appropriate
916 * slant but gave us one anyway. Try to mitigate.
918 if ((XftPatternGetInteger(f
->match
->pattern
, "slant", 0,
919 &haveattr
) != XftResultMatch
) || haveattr
< wantattr
) {
921 fputs("font slant does not match\n", stderr
);
925 if ((XftPatternGetInteger(pattern
, "weight", 0, &wantattr
) ==
927 if ((XftPatternGetInteger(f
->match
->pattern
, "weight", 0,
928 &haveattr
) != XftResultMatch
) || haveattr
!= wantattr
) {
930 fputs("font weight does not match\n", stderr
);
934 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
935 (const FcChar8
*) ascii_printable
,
936 strlen(ascii_printable
), &extents
);
939 f
->pattern
= configured
;
941 f
->ascent
= f
->match
->ascent
;
942 f
->descent
= f
->match
->descent
;
944 f
->rbearing
= f
->match
->max_advance_width
;
946 f
->height
= f
->ascent
+ f
->descent
;
947 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
953 xloadfonts(char *fontstr
, double fontsize
)
958 if (fontstr
[0] == '-')
959 pattern
= XftXlfdParse(fontstr
, False
, False
);
961 pattern
= FcNameParse((FcChar8
*)fontstr
);
964 die("can't open font %s\n", fontstr
);
967 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
968 FcPatternDel(pattern
, FC_SIZE
);
969 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
970 usedfontsize
= fontsize
;
972 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
974 usedfontsize
= fontval
;
975 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
980 * Default font size is 12, if none given. This is to
981 * have a known usedfontsize value.
983 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
986 defaultfontsize
= usedfontsize
;
989 if (xloadfont(&dc
.font
, pattern
))
990 die("can't open font %s\n", fontstr
);
992 if (usedfontsize
< 0) {
993 FcPatternGetDouble(dc
.font
.match
->pattern
,
994 FC_PIXEL_SIZE
, 0, &fontval
);
995 usedfontsize
= fontval
;
997 defaultfontsize
= fontval
;
1000 /* Setting character width and height. */
1001 win
.cw
= ceilf(dc
.font
.width
* cwscale
);
1002 win
.ch
= ceilf(dc
.font
.height
* chscale
);
1004 FcPatternDel(pattern
, FC_SLANT
);
1005 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
1006 if (xloadfont(&dc
.ifont
, pattern
))
1007 die("can't open font %s\n", fontstr
);
1009 FcPatternDel(pattern
, FC_WEIGHT
);
1010 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
1011 if (xloadfont(&dc
.ibfont
, pattern
))
1012 die("can't open font %s\n", fontstr
);
1014 FcPatternDel(pattern
, FC_SLANT
);
1015 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
1016 if (xloadfont(&dc
.bfont
, pattern
))
1017 die("can't open font %s\n", fontstr
);
1019 FcPatternDestroy(pattern
);
1023 xunloadfont(Font
*f
)
1025 XftFontClose(xw
.dpy
, f
->match
);
1026 FcPatternDestroy(f
->pattern
);
1028 FcFontSetDestroy(f
->set
);
1034 /* Free the loaded fonts in the font cache. */
1036 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
1038 xunloadfont(&dc
.font
);
1039 xunloadfont(&dc
.bfont
);
1040 xunloadfont(&dc
.ifont
);
1041 xunloadfont(&dc
.ibfont
);
1045 ximopen(Display
*dpy
)
1047 XIMCallback imdestroy
= { .client_data
= NULL
, .callback
= ximdestroy
};
1048 XICCallback icdestroy
= { .client_data
= NULL
, .callback
= xicdestroy
};
1050 xw
.ime
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
1051 if (xw
.ime
.xim
== NULL
)
1054 if (XSetIMValues(xw
.ime
.xim
, XNDestroyCallback
, &imdestroy
, NULL
))
1055 fprintf(stderr
, "XSetIMValues: "
1056 "Could not set XNDestroyCallback.\n");
1058 xw
.ime
.spotlist
= XVaCreateNestedList(0, XNSpotLocation
, &xw
.ime
.spot
,
1061 if (xw
.ime
.xic
== NULL
) {
1062 xw
.ime
.xic
= XCreateIC(xw
.ime
.xim
, XNInputStyle
,
1063 XIMPreeditNothing
| XIMStatusNothing
,
1064 XNClientWindow
, xw
.win
,
1065 XNDestroyCallback
, &icdestroy
,
1068 if (xw
.ime
.xic
== NULL
)
1069 fprintf(stderr
, "XCreateIC: Could not create input context.\n");
1075 ximinstantiate(Display
*dpy
, XPointer client
, XPointer call
)
1078 XUnregisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1079 ximinstantiate
, NULL
);
1083 ximdestroy(XIM xim
, XPointer client
, XPointer call
)
1086 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1087 ximinstantiate
, NULL
);
1088 XFree(xw
.ime
.spotlist
);
1092 xicdestroy(XIC xim
, XPointer client
, XPointer call
)
1099 xinit(int cols
, int rows
)
1104 pid_t thispid
= getpid();
1105 XColor xmousefg
, xmousebg
;
1107 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
1108 die("can't open display\n");
1109 xw
.scr
= XDefaultScreen(xw
.dpy
);
1110 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
1114 die("could not init fontconfig.\n");
1116 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
1117 xloadfonts(usedfont
, 0);
1120 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1123 /* adjust fixed window geometry */
1124 win
.w
= 2 * borderpx
+ cols
* win
.cw
;
1125 win
.h
= 2 * borderpx
+ rows
* win
.ch
;
1126 if (xw
.gm
& XNegative
)
1127 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - win
.w
- 2;
1128 if (xw
.gm
& YNegative
)
1129 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - win
.h
- 2;
1132 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
1133 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
1134 xw
.attrs
.bit_gravity
= NorthWestGravity
;
1135 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
| KeyReleaseMask
1136 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1137 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1138 xw
.attrs
.colormap
= xw
.cmap
;
1140 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
1141 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
1142 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
1143 win
.w
, win
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1144 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
1145 | CWEventMask
| CWColormap
, &xw
.attrs
);
1147 memset(&gcvalues
, 0, sizeof(gcvalues
));
1148 gcvalues
.graphics_exposures
= False
;
1149 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
1151 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
1152 DefaultDepth(xw
.dpy
, xw
.scr
));
1153 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
1154 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, win
.w
, win
.h
);
1156 /* font spec buffer */
1157 xw
.specbuf
= xmalloc(cols
* sizeof(GlyphFontSpec
));
1159 /* Xft rendering context */
1160 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
1163 if (!ximopen(xw
.dpy
)) {
1164 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1165 ximinstantiate
, NULL
);
1168 /* white cursor, black outline */
1169 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
1170 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1172 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
1173 xmousefg
.red
= 0xffff;
1174 xmousefg
.green
= 0xffff;
1175 xmousefg
.blue
= 0xffff;
1178 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
1179 xmousebg
.red
= 0x0000;
1180 xmousebg
.green
= 0x0000;
1181 xmousebg
.blue
= 0x0000;
1184 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
1186 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1187 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
1188 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
1189 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
1191 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
1192 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
1193 PropModeReplace
, (uchar
*)&thispid
, 1);
1195 win
.mode
= MODE_NUMLOCK
;
1198 XMapWindow(xw
.dpy
, xw
.win
);
1199 XSync(xw
.dpy
, False
);
1201 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick1
);
1202 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick2
);
1203 xsel
.primary
= NULL
;
1204 xsel
.clipboard
= NULL
;
1205 xsel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
1206 if (xsel
.xtarget
== None
)
1207 xsel
.xtarget
= XA_STRING
;
1211 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
1213 float winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
, xp
, yp
;
1214 ushort mode
, prevmode
= USHRT_MAX
;
1215 Font
*font
= &dc
.font
;
1216 int frcflags
= FRC_NORMAL
;
1217 float runewidth
= win
.cw
;
1221 FcPattern
*fcpattern
, *fontpattern
;
1222 FcFontSet
*fcsets
[] = { NULL
};
1223 FcCharSet
*fccharset
;
1224 int i
, f
, numspecs
= 0;
1226 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
1227 /* Fetch rune and mode for current glyph. */
1229 mode
= glyphs
[i
].mode
;
1231 /* Skip dummy wide-character spacing. */
1232 if (mode
== ATTR_WDUMMY
)
1235 /* Determine font for glyph if different from previous glyph. */
1236 if (prevmode
!= mode
) {
1239 frcflags
= FRC_NORMAL
;
1240 runewidth
= win
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
1241 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
1243 frcflags
= FRC_ITALICBOLD
;
1244 } else if (mode
& ATTR_ITALIC
) {
1246 frcflags
= FRC_ITALIC
;
1247 } else if (mode
& ATTR_BOLD
) {
1249 frcflags
= FRC_BOLD
;
1251 yp
= winy
+ font
->ascent
;
1254 /* Lookup character index with default font. */
1255 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
1257 specs
[numspecs
].font
= font
->match
;
1258 specs
[numspecs
].glyph
= glyphidx
;
1259 specs
[numspecs
].x
= (short)xp
;
1260 specs
[numspecs
].y
= (short)yp
;
1266 /* Fallback on font cache, search the font cache for match. */
1267 for (f
= 0; f
< frclen
; f
++) {
1268 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
1269 /* Everything correct. */
1270 if (glyphidx
&& frc
[f
].flags
== frcflags
)
1272 /* We got a default font for a not found glyph. */
1273 if (!glyphidx
&& frc
[f
].flags
== frcflags
1274 && frc
[f
].unicodep
== rune
) {
1279 /* Nothing was found. Use fontconfig to find matching font. */
1282 font
->set
= FcFontSort(0, font
->pattern
,
1284 fcsets
[0] = font
->set
;
1287 * Nothing was found in the cache. Now use
1288 * some dozen of Fontconfig calls to get the
1289 * font for one single character.
1291 * Xft and fontconfig are design failures.
1293 fcpattern
= FcPatternDuplicate(font
->pattern
);
1294 fccharset
= FcCharSetCreate();
1296 FcCharSetAddChar(fccharset
, rune
);
1297 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
1299 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
1301 FcConfigSubstitute(0, fcpattern
,
1303 FcDefaultSubstitute(fcpattern
);
1305 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
1308 /* Allocate memory for the new cache entry. */
1309 if (frclen
>= frccap
) {
1311 frc
= xrealloc(frc
, frccap
* sizeof(Fontcache
));
1314 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
1316 if (!frc
[frclen
].font
)
1317 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1319 frc
[frclen
].flags
= frcflags
;
1320 frc
[frclen
].unicodep
= rune
;
1322 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
1327 FcPatternDestroy(fcpattern
);
1328 FcCharSetDestroy(fccharset
);
1331 specs
[numspecs
].font
= frc
[f
].font
;
1332 specs
[numspecs
].glyph
= glyphidx
;
1333 specs
[numspecs
].x
= (short)xp
;
1334 specs
[numspecs
].y
= (short)yp
;
1343 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
1345 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
1346 int winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
,
1347 width
= charlen
* win
.cw
;
1348 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
1349 XRenderColor colfg
, colbg
;
1352 /* Fallback on color display for attributes not supported by the font */
1353 if (base
.mode
& ATTR_ITALIC
&& base
.mode
& ATTR_BOLD
) {
1354 if (dc
.ibfont
.badslant
|| dc
.ibfont
.badweight
)
1355 base
.fg
= defaultattr
;
1356 } else if ((base
.mode
& ATTR_ITALIC
&& dc
.ifont
.badslant
) ||
1357 (base
.mode
& ATTR_BOLD
&& dc
.bfont
.badweight
)) {
1358 base
.fg
= defaultattr
;
1361 if (IS_TRUECOL(base
.fg
)) {
1362 colfg
.alpha
= 0xffff;
1363 colfg
.red
= TRUERED(base
.fg
);
1364 colfg
.green
= TRUEGREEN(base
.fg
);
1365 colfg
.blue
= TRUEBLUE(base
.fg
);
1366 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
1369 fg
= &dc
.col
[base
.fg
];
1372 if (IS_TRUECOL(base
.bg
)) {
1373 colbg
.alpha
= 0xffff;
1374 colbg
.green
= TRUEGREEN(base
.bg
);
1375 colbg
.red
= TRUERED(base
.bg
);
1376 colbg
.blue
= TRUEBLUE(base
.bg
);
1377 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
1380 bg
= &dc
.col
[base
.bg
];
1383 /* Change basic system colors [0-7] to bright system colors [8-15] */
1384 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
1385 fg
= &dc
.col
[base
.fg
+ 8];
1387 if (IS_SET(MODE_REVERSE
)) {
1388 if (fg
== &dc
.col
[defaultfg
]) {
1389 fg
= &dc
.col
[defaultbg
];
1391 colfg
.red
= ~fg
->color
.red
;
1392 colfg
.green
= ~fg
->color
.green
;
1393 colfg
.blue
= ~fg
->color
.blue
;
1394 colfg
.alpha
= fg
->color
.alpha
;
1395 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
1400 if (bg
== &dc
.col
[defaultbg
]) {
1401 bg
= &dc
.col
[defaultfg
];
1403 colbg
.red
= ~bg
->color
.red
;
1404 colbg
.green
= ~bg
->color
.green
;
1405 colbg
.blue
= ~bg
->color
.blue
;
1406 colbg
.alpha
= bg
->color
.alpha
;
1407 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
1413 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
1414 colfg
.red
= fg
->color
.red
/ 2;
1415 colfg
.green
= fg
->color
.green
/ 2;
1416 colfg
.blue
= fg
->color
.blue
/ 2;
1417 colfg
.alpha
= fg
->color
.alpha
;
1418 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
1422 if (base
.mode
& ATTR_REVERSE
) {
1428 if (base
.mode
& ATTR_BLINK
&& win
.mode
& MODE_BLINK
)
1431 if (base
.mode
& ATTR_INVISIBLE
)
1434 /* Intelligent cleaning up of the borders. */
1436 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
1438 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: 0));
1440 if (winx
+ width
>= borderpx
+ win
.tw
) {
1441 xclear(winx
+ width
, (y
== 0)? 0 : winy
, win
.w
,
1442 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: (winy
+ win
.ch
)));
1445 xclear(winx
, 0, winx
+ width
, borderpx
);
1446 if (winy
+ win
.ch
>= borderpx
+ win
.th
)
1447 xclear(winx
, winy
+ win
.ch
, winx
+ width
, win
.h
);
1449 /* Clean up the region we want to draw to. */
1450 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, win
.ch
);
1452 /* Set the clip region because Xft is sometimes dirty. */
1457 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
1459 /* Render the glyphs. */
1460 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
1462 /* Render underline and strikethrough. */
1463 if (base
.mode
& ATTR_UNDERLINE
) {
1464 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
1468 if (base
.mode
& ATTR_STRUCK
) {
1469 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
1473 /* Reset clip to none. */
1474 XftDrawSetClip(xw
.draw
, 0);
1478 xdrawglyph(Glyph g
, int x
, int y
)
1481 XftGlyphFontSpec spec
;
1483 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
1484 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
1488 xdrawcursor(int cx
, int cy
, Glyph g
, int ox
, int oy
, Glyph og
)
1492 /* remove the old cursor */
1493 if (selected(ox
, oy
))
1494 og
.mode
^= ATTR_REVERSE
;
1495 xdrawglyph(og
, ox
, oy
);
1497 if (IS_SET(MODE_HIDE
))
1501 * Select the right color for the right mode.
1503 g
.mode
&= ATTR_BOLD
|ATTR_ITALIC
|ATTR_UNDERLINE
|ATTR_STRUCK
|ATTR_WIDE
;
1505 if (IS_SET(MODE_REVERSE
)) {
1506 g
.mode
|= ATTR_REVERSE
;
1508 if (selected(cx
, cy
)) {
1509 drawcol
= dc
.col
[defaultcs
];
1512 drawcol
= dc
.col
[defaultrcs
];
1516 if (selected(cx
, cy
)) {
1523 drawcol
= dc
.col
[g
.bg
];
1526 /* draw the new one */
1527 if (IS_SET(MODE_FOCUSED
)) {
1528 switch (win
.cursor
) {
1529 case 7: /* st extension: snowman (U+2603) */
1531 case 0: /* Blinking Block */
1532 case 1: /* Blinking Block (Default) */
1533 case 2: /* Steady Block */
1534 xdrawglyph(g
, cx
, cy
);
1536 case 3: /* Blinking Underline */
1537 case 4: /* Steady Underline */
1538 XftDrawRect(xw
.draw
, &drawcol
,
1539 borderpx
+ cx
* win
.cw
,
1540 borderpx
+ (cy
+ 1) * win
.ch
- \
1542 win
.cw
, cursorthickness
);
1544 case 5: /* Blinking bar */
1545 case 6: /* Steady bar */
1546 XftDrawRect(xw
.draw
, &drawcol
,
1547 borderpx
+ cx
* win
.cw
,
1548 borderpx
+ cy
* win
.ch
,
1549 cursorthickness
, win
.ch
);
1553 XftDrawRect(xw
.draw
, &drawcol
,
1554 borderpx
+ cx
* win
.cw
,
1555 borderpx
+ cy
* 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
+ 1) * win
.cw
- 1,
1563 borderpx
+ cy
* win
.ch
,
1565 XftDrawRect(xw
.draw
, &drawcol
,
1566 borderpx
+ cx
* win
.cw
,
1567 borderpx
+ (cy
+ 1) * win
.ch
- 1,
1575 char buf
[sizeof(long) * 8 + 1];
1577 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1578 setenv("WINDOWID", buf
, 1);
1585 DEFAULT(p
, opt_title
);
1587 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1589 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
1590 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
1597 return IS_SET(MODE_VISIBLE
);
1601 xdrawline(Line line
, int x1
, int y1
, int x2
)
1603 int i
, x
, ox
, numspecs
;
1605 XftGlyphFontSpec
*specs
= xw
.specbuf
;
1607 numspecs
= xmakeglyphfontspecs(specs
, &line
[x1
], x2
- x1
, x1
, y1
);
1609 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
1611 if (new.mode
== ATTR_WDUMMY
)
1613 if (selected(x
, y1
))
1614 new.mode
^= ATTR_REVERSE
;
1615 if (i
> 0 && ATTRCMP(base
, new)) {
1616 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1628 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1634 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, win
.w
,
1636 XSetForeground(xw
.dpy
, dc
.gc
,
1637 dc
.col
[IS_SET(MODE_REVERSE
)?
1638 defaultfg
: defaultbg
].pixel
);
1642 xximspot(int x
, int y
)
1644 if (xw
.ime
.xic
== NULL
)
1647 xw
.ime
.spot
.x
= borderpx
+ x
* win
.cw
;
1648 xw
.ime
.spot
.y
= borderpx
+ (y
+ 1) * win
.ch
;
1650 XSetICValues(xw
.ime
.xic
, XNPreeditAttributes
, xw
.ime
.spotlist
, NULL
);
1660 visibility(XEvent
*ev
)
1662 XVisibilityEvent
*e
= &ev
->xvisibility
;
1664 MODBIT(win
.mode
, e
->state
!= VisibilityFullyObscured
, MODE_VISIBLE
);
1670 win
.mode
&= ~MODE_VISIBLE
;
1674 xsetpointermotion(int set
)
1676 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
1677 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
1681 xsetmode(int set
, unsigned int flags
)
1683 int mode
= win
.mode
;
1684 MODBIT(win
.mode
, set
, flags
);
1685 if ((win
.mode
& MODE_REVERSE
) != (mode
& MODE_REVERSE
))
1690 xsetcursor(int cursor
)
1693 if (!BETWEEN(cursor
, 0, 6))
1695 win
.cursor
= cursor
;
1700 xseturgency(int add
)
1702 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1704 MODBIT(h
->flags
, add
, XUrgencyHint
);
1705 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1712 if (!(IS_SET(MODE_FOCUSED
)))
1715 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
1721 XFocusChangeEvent
*e
= &ev
->xfocus
;
1723 if (e
->mode
== NotifyGrab
)
1726 if (ev
->type
== FocusIn
) {
1728 XSetICFocus(xw
.ime
.xic
);
1729 win
.mode
|= MODE_FOCUSED
;
1731 if (IS_SET(MODE_FOCUS
))
1732 ttywrite("\033[I", 3, 0);
1735 XUnsetICFocus(xw
.ime
.xic
);
1736 win
.mode
&= ~MODE_FOCUSED
;
1737 if (IS_SET(MODE_FOCUS
))
1738 ttywrite("\033[O", 3, 0);
1743 match(uint mask
, uint state
)
1745 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
1749 kmap(KeySym k
, uint state
)
1754 /* Check for mapped keys out of X11 function keys. */
1755 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
1756 if (mappedkeys
[i
] == k
)
1759 if (i
== LEN(mappedkeys
)) {
1760 if ((k
& 0xFFFF) < 0xFD00)
1764 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
1768 if (!match(kp
->mask
, state
))
1771 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
1773 if (IS_SET(MODE_NUMLOCK
) && kp
->appkey
== 2)
1776 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
1788 XKeyEvent
*e
= &ev
->xkey
;
1790 char buf
[64], *customkey
;
1796 if (IS_SET(MODE_KBDLOCK
))
1800 len
= XmbLookupString(xw
.ime
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
1802 len
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
1804 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
1805 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
1806 bp
->func(&(bp
->arg
));
1811 /* 2. custom keys from config.h */
1812 if ((customkey
= kmap(ksym
, e
->state
))) {
1813 ttywrite(customkey
, strlen(customkey
), 1);
1817 /* 3. composed string from input method */
1820 if (len
== 1 && e
->state
& Mod1Mask
) {
1821 if (IS_SET(MODE_8BIT
)) {
1824 len
= utf8encode(c
, buf
);
1832 ttywrite(buf
, len
, 1);
1840 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1842 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1843 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1844 win
.mode
|= MODE_FOCUSED
;
1846 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1847 win
.mode
&= ~MODE_FOCUSED
;
1849 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
1858 if (e
->xconfigure
.width
== win
.w
&& e
->xconfigure
.height
== win
.h
)
1861 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
1868 int w
= win
.w
, h
= win
.h
;
1870 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
1872 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
1875 /* Waiting for window mapping */
1877 XNextEvent(xw
.dpy
, &ev
);
1879 * This XFilterEvent call is required because of XOpenIM. It
1880 * does filter out the key event and some client message for
1881 * the input method too.
1883 if (XFilterEvent(&ev
, None
))
1885 if (ev
.type
== ConfigureNotify
) {
1886 w
= ev
.xconfigure
.width
;
1887 h
= ev
.xconfigure
.height
;
1889 } while (ev
.type
!= MapNotify
);
1891 ttyfd
= ttynew(opt_line
, shell
, opt_io
, opt_cmd
);
1894 clock_gettime(CLOCK_MONOTONIC
, &last
);
1897 for (xev
= actionfps
;;) {
1899 FD_SET(ttyfd
, &rfd
);
1902 if (pselect(MAX(xfd
, ttyfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
1905 die("select failed: %s\n", strerror(errno
));
1907 if (FD_ISSET(ttyfd
, &rfd
)) {
1910 blinkset
= tattrset(ATTR_BLINK
);
1912 MODBIT(win
.mode
, 0, MODE_BLINK
);
1916 if (FD_ISSET(xfd
, &rfd
))
1919 clock_gettime(CLOCK_MONOTONIC
, &now
);
1920 drawtimeout
.tv_sec
= 0;
1921 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
1925 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
1926 tsetdirtattr(ATTR_BLINK
);
1927 win
.mode
^= MODE_BLINK
;
1931 deltatime
= TIMEDIFF(now
, last
);
1932 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
1938 while (XPending(xw
.dpy
)) {
1939 XNextEvent(xw
.dpy
, &ev
);
1940 if (XFilterEvent(&ev
, None
))
1942 if (handler
[ev
.type
])
1943 (handler
[ev
.type
])(&ev
);
1949 if (xev
&& !FD_ISSET(xfd
, &rfd
))
1951 if (!FD_ISSET(ttyfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
1953 if (TIMEDIFF(now
, lastblink
) \
1955 drawtimeout
.tv_nsec
= 1000;
1957 drawtimeout
.tv_nsec
= (1E6
* \
1962 drawtimeout
.tv_sec
= \
1963 drawtimeout
.tv_nsec
/ 1E9
;
1964 drawtimeout
.tv_nsec
%= (long)1E9
;
1976 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
1977 " [-n name] [-o file]\n"
1978 " [-T title] [-t title] [-w windowid]"
1979 " [[-e] command [args ...]]\n"
1980 " %s [-aiv] [-c class] [-f font] [-g geometry]"
1981 " [-n name] [-o file]\n"
1982 " [-T title] [-t title] [-w windowid] -l line"
1983 " [stty_args ...]\n", argv0
, argv0
);
1987 main(int argc
, char *argv
[])
1991 win
.cursor
= cursorshape
;
1998 opt_class
= EARGF(usage());
2005 opt_font
= EARGF(usage());
2008 xw
.gm
= XParseGeometry(EARGF(usage()),
2009 &xw
.l
, &xw
.t
, &cols
, &rows
);
2015 opt_io
= EARGF(usage());
2018 opt_line
= EARGF(usage());
2021 opt_name
= EARGF(usage());
2025 opt_title
= EARGF(usage());
2028 opt_embed
= EARGF(usage());
2031 die("%s " VERSION
"\n", argv0
);
2038 if (argc
> 0) /* eat all remaining arguments */
2042 opt_title
= (opt_line
|| !opt_cmd
) ? "st" : opt_cmd
[0];
2044 setlocale(LC_CTYPE
, "");
2045 XSetLocaleModifiers("");
2046 cols
= MAX(cols
, 1);
2047 rows
= MAX(rows
, 1);