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
*);
41 /* three-valued logic variables: 0 indifferent, 1 on, -1 off */
42 signed char appkey
; /* application keypad */
43 signed char appcursor
; /* application cursor */
47 #define XK_ANY_MOD UINT_MAX
49 #define XK_SWITCH_MOD (1<<13)
51 /* function definitions used in config.h */
52 static void clipcopy(const Arg
*);
53 static void clippaste(const Arg
*);
54 static void numlock(const Arg
*);
55 static void selpaste(const Arg
*);
56 static void zoom(const Arg
*);
57 static void zoomabs(const Arg
*);
58 static void zoomreset(const Arg
*);
60 /* config.h for applying patches and the configuration. */
64 #define XEMBED_FOCUS_IN 4
65 #define XEMBED_FOCUS_OUT 5
68 #define IS_SET(flag) ((win.mode & (flag)) != 0)
69 #define TRUERED(x) (((x) & 0xff0000) >> 8)
70 #define TRUEGREEN(x) (((x) & 0xff00))
71 #define TRUEBLUE(x) (((x) & 0xff) << 8)
73 typedef XftDraw
*Draw
;
74 typedef XftColor Color
;
75 typedef XftGlyphFontSpec GlyphFontSpec
;
77 /* Purely graphic info */
79 int tw
, th
; /* tty width and height */
80 int w
, h
; /* window width and height */
81 int ch
; /* char height */
82 int cw
; /* char width */
83 int mode
; /* window state/mode flags */
84 int cursor
; /* cursor style */
92 GlyphFontSpec
*specbuf
; /* font spec buffer used for rendering */
93 Atom xembed
, wmdeletewin
, netwmname
, netwmpid
;
98 XSetWindowAttributes attrs
;
100 int isfixed
; /* is fixed geometry? */
101 int l
, t
; /* left and top offset */
102 int gm
; /* geometry mask */
107 char *primary
, *clipboard
;
108 struct timespec tclick1
;
109 struct timespec tclick2
;
128 /* Drawing Context */
132 Font font
, bfont
, ifont
, ibfont
;
136 static inline ushort
sixd_to_16bit(int);
137 static int xmakeglyphfontspecs(XftGlyphFontSpec
*, const Glyph
*, int, int, int);
138 static void xdrawglyphfontspecs(const XftGlyphFontSpec
*, Glyph
, int, int, int);
139 static void xdrawglyph(Glyph
, int, int);
140 static void xclear(int, int, int, int);
141 static int xgeommasktogravity(int);
142 static void ximopen(Display
*);
143 static void ximinstantiate(Display
*, XPointer
, XPointer
);
144 static void ximdestroy(XIM
, XPointer
, XPointer
);
145 static void xinit(int, int);
146 static void cresize(int, int);
147 static void xresize(int, int);
148 static void xhints(void);
149 static int xloadcolor(int, const char *, Color
*);
150 static int xloadfont(Font
*, FcPattern
*);
151 static void xloadfonts(char *, double);
152 static void xunloadfont(Font
*);
153 static void xunloadfonts(void);
154 static void xsetenv(void);
155 static void xseturgency(int);
156 static int evcol(XEvent
*);
157 static int evrow(XEvent
*);
159 static void expose(XEvent
*);
160 static void visibility(XEvent
*);
161 static void unmap(XEvent
*);
162 static void kpress(XEvent
*);
163 static void cmessage(XEvent
*);
164 static void resize(XEvent
*);
165 static void focus(XEvent
*);
166 static void brelease(XEvent
*);
167 static void bpress(XEvent
*);
168 static void bmotion(XEvent
*);
169 static void propnotify(XEvent
*);
170 static void selnotify(XEvent
*);
171 static void selclear_(XEvent
*);
172 static void selrequest(XEvent
*);
173 static void setsel(char *, Time
);
174 static void mousesel(XEvent
*, int);
175 static void mousereport(XEvent
*);
176 static char *kmap(KeySym
, uint
);
177 static int match(uint
, uint
);
179 static void run(void);
180 static void usage(void);
182 static void (*handler
[LASTEvent
])(XEvent
*) = {
184 [ClientMessage
] = cmessage
,
185 [ConfigureNotify
] = resize
,
186 [VisibilityNotify
] = visibility
,
187 [UnmapNotify
] = unmap
,
191 [MotionNotify
] = bmotion
,
192 [ButtonPress
] = bpress
,
193 [ButtonRelease
] = brelease
,
195 * Uncomment if you want the selection to disappear when you select something
196 * different in another window.
198 /* [SelectionClear] = selclear_, */
199 [SelectionNotify
] = selnotify
,
201 * PropertyNotify is only turned on when there is some INCR transfer happening
202 * for the selection retrieval.
204 [PropertyNotify
] = propnotify
,
205 [SelectionRequest
] = selrequest
,
211 static XSelection xsel
;
212 static TermWindow win
;
214 /* Font Ring Cache */
228 /* Fontcache is an array now. A new font will be appended to the array. */
229 static Fontcache
*frc
= NULL
;
230 static int frclen
= 0;
231 static int frccap
= 0;
232 static char *usedfont
= NULL
;
233 static double usedfontsize
= 0;
234 static double defaultfontsize
= 0;
236 static char *opt_class
= NULL
;
237 static char **opt_cmd
= NULL
;
238 static char *opt_embed
= NULL
;
239 static char *opt_font
= NULL
;
240 static char *opt_io
= NULL
;
241 static char *opt_line
= NULL
;
242 static char *opt_name
= NULL
;
243 static char *opt_title
= NULL
;
245 static int oldbutton
= 3; /* button event on startup: 3 = release */
248 clipcopy(const Arg
*dummy
)
252 free(xsel
.clipboard
);
253 xsel
.clipboard
= NULL
;
255 if (xsel
.primary
!= NULL
) {
256 xsel
.clipboard
= xstrdup(xsel
.primary
);
257 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
258 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
263 clippaste(const Arg
*dummy
)
267 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
268 XConvertSelection(xw
.dpy
, clipboard
, xsel
.xtarget
, clipboard
,
269 xw
.win
, CurrentTime
);
273 selpaste(const Arg
*dummy
)
275 XConvertSelection(xw
.dpy
, XA_PRIMARY
, xsel
.xtarget
, XA_PRIMARY
,
276 xw
.win
, CurrentTime
);
280 numlock(const Arg
*dummy
)
282 win
.mode
^= MODE_NUMLOCK
;
290 larg
.f
= usedfontsize
+ arg
->f
;
295 zoomabs(const Arg
*arg
)
298 xloadfonts(usedfont
, arg
->f
);
305 zoomreset(const Arg
*arg
)
309 if (defaultfontsize
> 0) {
310 larg
.f
= defaultfontsize
;
318 int x
= e
->xbutton
.x
- borderpx
;
319 LIMIT(x
, 0, win
.tw
- 1);
326 int y
= e
->xbutton
.y
- borderpx
;
327 LIMIT(y
, 0, win
.th
- 1);
332 mousesel(XEvent
*e
, int done
)
334 int type
, seltype
= SEL_REGULAR
;
335 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forceselmod
);
337 for (type
= 1; type
< LEN(selmasks
); ++type
) {
338 if (match(selmasks
[type
], state
)) {
343 selextend(evcol(e
), evrow(e
), seltype
, done
);
345 setsel(getsel(), e
->xbutton
.time
);
349 mousereport(XEvent
*e
)
351 int len
, x
= evcol(e
), y
= evrow(e
),
352 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
;
357 if (e
->xbutton
.type
== MotionNotify
) {
358 if (x
== ox
&& y
== oy
)
360 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
362 /* MOUSE_MOTION: no reporting if no button is pressed */
363 if (IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
366 button
= oldbutton
+ 32;
370 if (!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
377 if (e
->xbutton
.type
== ButtonPress
) {
381 } else if (e
->xbutton
.type
== ButtonRelease
) {
383 /* MODE_MOUSEX10: no button release reporting */
384 if (IS_SET(MODE_MOUSEX10
))
386 if (button
== 64 || button
== 65)
391 if (!IS_SET(MODE_MOUSEX10
)) {
392 button
+= ((state
& ShiftMask
) ? 4 : 0)
393 + ((state
& Mod4Mask
) ? 8 : 0)
394 + ((state
& ControlMask
) ? 16 : 0);
397 if (IS_SET(MODE_MOUSESGR
)) {
398 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
400 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
401 } else if (x
< 223 && y
< 223) {
402 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
403 32+button
, 32+x
+1, 32+y
+1);
408 ttywrite(buf
, len
, 0);
418 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
423 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
424 if (e
->xbutton
.button
== ms
->b
425 && match(ms
->mask
, e
->xbutton
.state
)) {
426 ttywrite(ms
->s
, strlen(ms
->s
), 1);
431 if (e
->xbutton
.button
== Button1
) {
433 * If the user clicks below predefined timeouts specific
434 * snapping behaviour is exposed.
436 clock_gettime(CLOCK_MONOTONIC
, &now
);
437 if (TIMEDIFF(now
, xsel
.tclick2
) <= tripleclicktimeout
) {
439 } else if (TIMEDIFF(now
, xsel
.tclick1
) <= doubleclicktimeout
) {
444 xsel
.tclick2
= xsel
.tclick1
;
447 selstart(evcol(e
), evrow(e
), snap
);
452 propnotify(XEvent
*e
)
454 XPropertyEvent
*xpev
;
455 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
457 xpev
= &e
->xproperty
;
458 if (xpev
->state
== PropertyNewValue
&&
459 (xpev
->atom
== XA_PRIMARY
||
460 xpev
->atom
== clipboard
)) {
468 ulong nitems
, ofs
, rem
;
470 uchar
*data
, *last
, *repl
;
471 Atom type
, incratom
, property
= None
;
473 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
476 if (e
->type
== SelectionNotify
)
477 property
= e
->xselection
.property
;
478 else if (e
->type
== PropertyNotify
)
479 property
= e
->xproperty
.atom
;
481 if (property
== None
)
485 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
486 BUFSIZ
/4, False
, AnyPropertyType
,
487 &type
, &format
, &nitems
, &rem
,
489 fprintf(stderr
, "Clipboard allocation failed\n");
493 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
495 * If there is some PropertyNotify with no data, then
496 * this is the signal of the selection owner that all
497 * data has been transferred. We won't need to receive
498 * PropertyNotify events anymore.
500 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
501 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
505 if (type
== incratom
) {
507 * Activate the PropertyNotify events so we receive
508 * when the selection owner does send us the next
511 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
512 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
516 * Deleting the property is the transfer start signal.
518 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
524 * Line endings are inconsistent in the terminal and GUI world
525 * copy and pasting. When receiving some selection data,
526 * replace all '\n' with '\r'.
527 * FIXME: Fix the computer world.
530 last
= data
+ nitems
* format
/ 8;
531 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
535 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
536 ttywrite("\033[200~", 6, 0);
537 ttywrite((char *)data
, nitems
* format
/ 8, 1);
538 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
539 ttywrite("\033[201~", 6, 0);
541 /* number of 32-bit chunks returned */
542 ofs
+= nitems
* format
/ 32;
546 * Deleting the property again tells the selection owner to send the
547 * next data chunk in the property.
549 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
565 selrequest(XEvent
*e
)
567 XSelectionRequestEvent
*xsre
;
569 Atom xa_targets
, string
, clipboard
;
572 xsre
= (XSelectionRequestEvent
*) e
;
573 xev
.type
= SelectionNotify
;
574 xev
.requestor
= xsre
->requestor
;
575 xev
.selection
= xsre
->selection
;
576 xev
.target
= xsre
->target
;
577 xev
.time
= xsre
->time
;
578 if (xsre
->property
== None
)
579 xsre
->property
= xsre
->target
;
584 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
585 if (xsre
->target
== xa_targets
) {
586 /* respond with the supported type */
587 string
= xsel
.xtarget
;
588 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
589 XA_ATOM
, 32, PropModeReplace
,
590 (uchar
*) &string
, 1);
591 xev
.property
= xsre
->property
;
592 } else if (xsre
->target
== xsel
.xtarget
|| xsre
->target
== XA_STRING
) {
594 * xith XA_STRING non ascii characters may be incorrect in the
595 * requestor. It is not our problem, use utf8.
597 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
598 if (xsre
->selection
== XA_PRIMARY
) {
599 seltext
= xsel
.primary
;
600 } else if (xsre
->selection
== clipboard
) {
601 seltext
= xsel
.clipboard
;
604 "Unhandled clipboard selection 0x%lx\n",
608 if (seltext
!= NULL
) {
609 XChangeProperty(xsre
->display
, xsre
->requestor
,
610 xsre
->property
, xsre
->target
,
612 (uchar
*)seltext
, strlen(seltext
));
613 xev
.property
= xsre
->property
;
617 /* all done, send a notification to the listener */
618 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
619 fprintf(stderr
, "Error sending SelectionNotify event\n");
623 setsel(char *str
, Time t
)
631 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
632 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
639 setsel(str
, CurrentTime
);
645 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
650 if (e
->xbutton
.button
== Button2
)
652 else if (e
->xbutton
.button
== Button1
)
659 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
668 cresize(int width
, int height
)
677 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
678 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
684 ttyresize(win
.tw
, win
.th
);
688 xresize(int col
, int row
)
690 win
.tw
= col
* win
.cw
;
691 win
.th
= row
* win
.ch
;
693 XFreePixmap(xw
.dpy
, xw
.buf
);
694 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
695 DefaultDepth(xw
.dpy
, xw
.scr
));
696 XftDrawChange(xw
.draw
, xw
.buf
);
697 xclear(0, 0, win
.w
, win
.h
);
699 /* resize to new width */
700 xw
.specbuf
= xrealloc(xw
.specbuf
, col
* sizeof(GlyphFontSpec
));
706 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
710 xloadcolor(int i
, const char *name
, Color
*ncolor
)
712 XRenderColor color
= { .alpha
= 0xffff };
715 if (BETWEEN(i
, 16, 255)) { /* 256 color */
716 if (i
< 6*6*6+16) { /* same colors as xterm */
717 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
718 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
719 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
720 } else { /* greyscale */
721 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
722 color
.green
= color
.blue
= color
.red
;
724 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
725 xw
.cmap
, &color
, ncolor
);
730 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
741 for (cp
= dc
.col
; cp
< &dc
.col
[dc
.collen
]; ++cp
)
742 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
744 dc
.collen
= MAX(LEN(colorname
), 256);
745 dc
.col
= xmalloc(dc
.collen
* sizeof(Color
));
748 for (i
= 0; i
< dc
.collen
; i
++)
749 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
751 die("could not allocate color '%s'\n", colorname
[i
]);
753 die("could not allocate color %d\n", i
);
759 xsetcolorname(int x
, const char *name
)
763 if (!BETWEEN(x
, 0, dc
.collen
))
767 if (!xloadcolor(x
, name
, &ncolor
))
770 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
777 * Absolute coordinates.
780 xclear(int x1
, int y1
, int x2
, int y2
)
783 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
784 x1
, y1
, x2
-x1
, y2
-y1
);
790 XClassHint
class = {opt_name
? opt_name
: termname
,
791 opt_class
? opt_class
: termname
};
792 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
795 sizeh
= XAllocSizeHints();
797 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
| PMinSize
;
798 sizeh
->height
= win
.h
;
799 sizeh
->width
= win
.w
;
800 sizeh
->height_inc
= win
.ch
;
801 sizeh
->width_inc
= win
.cw
;
802 sizeh
->base_height
= 2 * borderpx
;
803 sizeh
->base_width
= 2 * borderpx
;
804 sizeh
->min_height
= win
.ch
+ 2 * borderpx
;
805 sizeh
->min_width
= win
.cw
+ 2 * borderpx
;
807 sizeh
->flags
|= PMaxSize
;
808 sizeh
->min_width
= sizeh
->max_width
= win
.w
;
809 sizeh
->min_height
= sizeh
->max_height
= win
.h
;
811 if (xw
.gm
& (XValue
|YValue
)) {
812 sizeh
->flags
|= USPosition
| PWinGravity
;
815 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
818 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
824 xgeommasktogravity(int mask
)
826 switch (mask
& (XNegative
|YNegative
)) {
828 return NorthWestGravity
;
830 return NorthEastGravity
;
832 return SouthWestGravity
;
835 return SouthEastGravity
;
839 xloadfont(Font
*f
, FcPattern
*pattern
)
841 FcPattern
*configured
;
845 int wantattr
, haveattr
;
848 * Manually configure instead of calling XftMatchFont
849 * so that we can use the configured pattern for
850 * "missing glyph" lookups.
852 configured
= FcPatternDuplicate(pattern
);
856 FcConfigSubstitute(NULL
, configured
, FcMatchPattern
);
857 XftDefaultSubstitute(xw
.dpy
, xw
.scr
, configured
);
859 match
= FcFontMatch(NULL
, configured
, &result
);
861 FcPatternDestroy(configured
);
865 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
866 FcPatternDestroy(configured
);
867 FcPatternDestroy(match
);
871 if ((XftPatternGetInteger(pattern
, "slant", 0, &wantattr
) ==
874 * Check if xft was unable to find a font with the appropriate
875 * slant but gave us one anyway. Try to mitigate.
877 if ((XftPatternGetInteger(f
->match
->pattern
, "slant", 0,
878 &haveattr
) != XftResultMatch
) || haveattr
< wantattr
) {
880 fputs("font slant does not match\n", stderr
);
884 if ((XftPatternGetInteger(pattern
, "weight", 0, &wantattr
) ==
886 if ((XftPatternGetInteger(f
->match
->pattern
, "weight", 0,
887 &haveattr
) != XftResultMatch
) || haveattr
!= wantattr
) {
889 fputs("font weight does not match\n", stderr
);
893 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
894 (const FcChar8
*) ascii_printable
,
895 strlen(ascii_printable
), &extents
);
898 f
->pattern
= configured
;
900 f
->ascent
= f
->match
->ascent
;
901 f
->descent
= f
->match
->descent
;
903 f
->rbearing
= f
->match
->max_advance_width
;
905 f
->height
= f
->ascent
+ f
->descent
;
906 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
912 xloadfonts(char *fontstr
, double fontsize
)
917 if (fontstr
[0] == '-')
918 pattern
= XftXlfdParse(fontstr
, False
, False
);
920 pattern
= FcNameParse((FcChar8
*)fontstr
);
923 die("can't open font %s\n", fontstr
);
926 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
927 FcPatternDel(pattern
, FC_SIZE
);
928 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
929 usedfontsize
= fontsize
;
931 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
933 usedfontsize
= fontval
;
934 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
939 * Default font size is 12, if none given. This is to
940 * have a known usedfontsize value.
942 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
945 defaultfontsize
= usedfontsize
;
948 if (xloadfont(&dc
.font
, pattern
))
949 die("can't open font %s\n", fontstr
);
951 if (usedfontsize
< 0) {
952 FcPatternGetDouble(dc
.font
.match
->pattern
,
953 FC_PIXEL_SIZE
, 0, &fontval
);
954 usedfontsize
= fontval
;
956 defaultfontsize
= fontval
;
959 /* Setting character width and height. */
960 win
.cw
= ceilf(dc
.font
.width
* cwscale
);
961 win
.ch
= ceilf(dc
.font
.height
* chscale
);
963 FcPatternDel(pattern
, FC_SLANT
);
964 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
965 if (xloadfont(&dc
.ifont
, pattern
))
966 die("can't open font %s\n", fontstr
);
968 FcPatternDel(pattern
, FC_WEIGHT
);
969 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
970 if (xloadfont(&dc
.ibfont
, pattern
))
971 die("can't open font %s\n", fontstr
);
973 FcPatternDel(pattern
, FC_SLANT
);
974 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
975 if (xloadfont(&dc
.bfont
, pattern
))
976 die("can't open font %s\n", fontstr
);
978 FcPatternDestroy(pattern
);
984 XftFontClose(xw
.dpy
, f
->match
);
985 FcPatternDestroy(f
->pattern
);
987 FcFontSetDestroy(f
->set
);
993 /* Free the loaded fonts in the font cache. */
995 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
997 xunloadfont(&dc
.font
);
998 xunloadfont(&dc
.bfont
);
999 xunloadfont(&dc
.ifont
);
1000 xunloadfont(&dc
.ibfont
);
1004 ximopen(Display
*dpy
)
1006 XIMCallback destroy
= { .client_data
= NULL
, .callback
= ximdestroy
};
1008 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1009 XSetLocaleModifiers("@im=local");
1010 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1011 XSetLocaleModifiers("@im=");
1012 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
)
1013 die("XOpenIM failed. Could not open input device.\n");
1016 if (XSetIMValues(xw
.xim
, XNDestroyCallback
, &destroy
, NULL
) != NULL
)
1017 die("XSetIMValues failed. Could not set input method value.\n");
1018 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
| XIMStatusNothing
,
1019 XNClientWindow
, xw
.win
, XNFocusWindow
, xw
.win
, NULL
);
1021 die("XCreateIC failed. Could not obtain input method.\n");
1025 ximinstantiate(Display
*dpy
, XPointer client
, XPointer call
)
1028 XUnregisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1029 ximinstantiate
, NULL
);
1033 ximdestroy(XIM xim
, XPointer client
, XPointer call
)
1036 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1037 ximinstantiate
, NULL
);
1041 xinit(int cols
, int rows
)
1046 pid_t thispid
= getpid();
1047 XColor xmousefg
, xmousebg
;
1049 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
1050 die("can't open display\n");
1051 xw
.scr
= XDefaultScreen(xw
.dpy
);
1052 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
1056 die("could not init fontconfig.\n");
1058 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
1059 xloadfonts(usedfont
, 0);
1062 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1065 /* adjust fixed window geometry */
1066 win
.w
= 2 * borderpx
+ cols
* win
.cw
;
1067 win
.h
= 2 * borderpx
+ rows
* win
.ch
;
1068 if (xw
.gm
& XNegative
)
1069 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - win
.w
- 2;
1070 if (xw
.gm
& YNegative
)
1071 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - win
.h
- 2;
1074 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
1075 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
1076 xw
.attrs
.bit_gravity
= NorthWestGravity
;
1077 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
| KeyReleaseMask
1078 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1079 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1080 xw
.attrs
.colormap
= xw
.cmap
;
1082 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
1083 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
1084 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
1085 win
.w
, win
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1086 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
1087 | CWEventMask
| CWColormap
, &xw
.attrs
);
1089 memset(&gcvalues
, 0, sizeof(gcvalues
));
1090 gcvalues
.graphics_exposures
= False
;
1091 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
1093 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
1094 DefaultDepth(xw
.dpy
, xw
.scr
));
1095 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
1096 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, win
.w
, win
.h
);
1098 /* font spec buffer */
1099 xw
.specbuf
= xmalloc(cols
* sizeof(GlyphFontSpec
));
1101 /* Xft rendering context */
1102 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
1107 /* white cursor, black outline */
1108 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
1109 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1111 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
1112 xmousefg
.red
= 0xffff;
1113 xmousefg
.green
= 0xffff;
1114 xmousefg
.blue
= 0xffff;
1117 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
1118 xmousebg
.red
= 0x0000;
1119 xmousebg
.green
= 0x0000;
1120 xmousebg
.blue
= 0x0000;
1123 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
1125 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1126 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
1127 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
1128 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
1130 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
1131 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
1132 PropModeReplace
, (uchar
*)&thispid
, 1);
1134 win
.mode
= MODE_NUMLOCK
;
1136 XMapWindow(xw
.dpy
, xw
.win
);
1138 XSync(xw
.dpy
, False
);
1140 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick1
);
1141 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick2
);
1142 xsel
.primary
= NULL
;
1143 xsel
.clipboard
= NULL
;
1144 xsel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
1145 if (xsel
.xtarget
== None
)
1146 xsel
.xtarget
= XA_STRING
;
1150 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
1152 float winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
, xp
, yp
;
1153 ushort mode
, prevmode
= USHRT_MAX
;
1154 Font
*font
= &dc
.font
;
1155 int frcflags
= FRC_NORMAL
;
1156 float runewidth
= win
.cw
;
1160 FcPattern
*fcpattern
, *fontpattern
;
1161 FcFontSet
*fcsets
[] = { NULL
};
1162 FcCharSet
*fccharset
;
1163 int i
, f
, numspecs
= 0;
1165 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
1166 /* Fetch rune and mode for current glyph. */
1168 mode
= glyphs
[i
].mode
;
1170 /* Skip dummy wide-character spacing. */
1171 if (mode
== ATTR_WDUMMY
)
1174 /* Determine font for glyph if different from previous glyph. */
1175 if (prevmode
!= mode
) {
1178 frcflags
= FRC_NORMAL
;
1179 runewidth
= win
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
1180 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
1182 frcflags
= FRC_ITALICBOLD
;
1183 } else if (mode
& ATTR_ITALIC
) {
1185 frcflags
= FRC_ITALIC
;
1186 } else if (mode
& ATTR_BOLD
) {
1188 frcflags
= FRC_BOLD
;
1190 yp
= winy
+ font
->ascent
;
1193 /* Lookup character index with default font. */
1194 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
1196 specs
[numspecs
].font
= font
->match
;
1197 specs
[numspecs
].glyph
= glyphidx
;
1198 specs
[numspecs
].x
= (short)xp
;
1199 specs
[numspecs
].y
= (short)yp
;
1205 /* Fallback on font cache, search the font cache for match. */
1206 for (f
= 0; f
< frclen
; f
++) {
1207 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
1208 /* Everything correct. */
1209 if (glyphidx
&& frc
[f
].flags
== frcflags
)
1211 /* We got a default font for a not found glyph. */
1212 if (!glyphidx
&& frc
[f
].flags
== frcflags
1213 && frc
[f
].unicodep
== rune
) {
1218 /* Nothing was found. Use fontconfig to find matching font. */
1221 font
->set
= FcFontSort(0, font
->pattern
,
1223 fcsets
[0] = font
->set
;
1226 * Nothing was found in the cache. Now use
1227 * some dozen of Fontconfig calls to get the
1228 * font for one single character.
1230 * Xft and fontconfig are design failures.
1232 fcpattern
= FcPatternDuplicate(font
->pattern
);
1233 fccharset
= FcCharSetCreate();
1235 FcCharSetAddChar(fccharset
, rune
);
1236 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
1238 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
1240 FcConfigSubstitute(0, fcpattern
,
1242 FcDefaultSubstitute(fcpattern
);
1244 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
1248 * Allocate memory for the new cache entry.
1250 if (frclen
>= frccap
) {
1253 frc
= xmalloc(frccap
* sizeof(Fontcache
));
1255 frc
= xrealloc(frc
, frccap
* sizeof(Fontcache
));
1258 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
1260 if (!frc
[frclen
].font
)
1261 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1263 frc
[frclen
].flags
= frcflags
;
1264 frc
[frclen
].unicodep
= rune
;
1266 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
1271 FcPatternDestroy(fcpattern
);
1272 FcCharSetDestroy(fccharset
);
1275 specs
[numspecs
].font
= frc
[f
].font
;
1276 specs
[numspecs
].glyph
= glyphidx
;
1277 specs
[numspecs
].x
= (short)xp
;
1278 specs
[numspecs
].y
= (short)yp
;
1287 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
1289 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
1290 int winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
,
1291 width
= charlen
* win
.cw
;
1292 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
1293 XRenderColor colfg
, colbg
;
1296 /* Fallback on color display for attributes not supported by the font */
1297 if (base
.mode
& ATTR_ITALIC
&& base
.mode
& ATTR_BOLD
) {
1298 if (dc
.ibfont
.badslant
|| dc
.ibfont
.badweight
)
1299 base
.fg
= defaultattr
;
1300 } else if ((base
.mode
& ATTR_ITALIC
&& dc
.ifont
.badslant
) ||
1301 (base
.mode
& ATTR_BOLD
&& dc
.bfont
.badweight
)) {
1302 base
.fg
= defaultattr
;
1305 if (IS_TRUECOL(base
.fg
)) {
1306 colfg
.alpha
= 0xffff;
1307 colfg
.red
= TRUERED(base
.fg
);
1308 colfg
.green
= TRUEGREEN(base
.fg
);
1309 colfg
.blue
= TRUEBLUE(base
.fg
);
1310 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
1313 fg
= &dc
.col
[base
.fg
];
1316 if (IS_TRUECOL(base
.bg
)) {
1317 colbg
.alpha
= 0xffff;
1318 colbg
.green
= TRUEGREEN(base
.bg
);
1319 colbg
.red
= TRUERED(base
.bg
);
1320 colbg
.blue
= TRUEBLUE(base
.bg
);
1321 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
1324 bg
= &dc
.col
[base
.bg
];
1327 /* Change basic system colors [0-7] to bright system colors [8-15] */
1328 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
1329 fg
= &dc
.col
[base
.fg
+ 8];
1331 if (IS_SET(MODE_REVERSE
)) {
1332 if (fg
== &dc
.col
[defaultfg
]) {
1333 fg
= &dc
.col
[defaultbg
];
1335 colfg
.red
= ~fg
->color
.red
;
1336 colfg
.green
= ~fg
->color
.green
;
1337 colfg
.blue
= ~fg
->color
.blue
;
1338 colfg
.alpha
= fg
->color
.alpha
;
1339 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
1344 if (bg
== &dc
.col
[defaultbg
]) {
1345 bg
= &dc
.col
[defaultfg
];
1347 colbg
.red
= ~bg
->color
.red
;
1348 colbg
.green
= ~bg
->color
.green
;
1349 colbg
.blue
= ~bg
->color
.blue
;
1350 colbg
.alpha
= bg
->color
.alpha
;
1351 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
1357 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
1358 colfg
.red
= fg
->color
.red
/ 2;
1359 colfg
.green
= fg
->color
.green
/ 2;
1360 colfg
.blue
= fg
->color
.blue
/ 2;
1361 colfg
.alpha
= fg
->color
.alpha
;
1362 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
1366 if (base
.mode
& ATTR_REVERSE
) {
1372 if (base
.mode
& ATTR_BLINK
&& win
.mode
& MODE_BLINK
)
1375 if (base
.mode
& ATTR_INVISIBLE
)
1378 /* Intelligent cleaning up of the borders. */
1380 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
1382 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: 0));
1384 if (winx
+ width
>= borderpx
+ win
.tw
) {
1385 xclear(winx
+ width
, (y
== 0)? 0 : winy
, win
.w
,
1386 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: (winy
+ win
.ch
)));
1389 xclear(winx
, 0, winx
+ width
, borderpx
);
1390 if (winy
+ win
.ch
>= borderpx
+ win
.th
)
1391 xclear(winx
, winy
+ win
.ch
, winx
+ width
, win
.h
);
1393 /* Clean up the region we want to draw to. */
1394 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, win
.ch
);
1396 /* Set the clip region because Xft is sometimes dirty. */
1401 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
1403 /* Render the glyphs. */
1404 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
1406 /* Render underline and strikethrough. */
1407 if (base
.mode
& ATTR_UNDERLINE
) {
1408 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
1412 if (base
.mode
& ATTR_STRUCK
) {
1413 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
1417 /* Reset clip to none. */
1418 XftDrawSetClip(xw
.draw
, 0);
1422 xdrawglyph(Glyph g
, int x
, int y
)
1425 XftGlyphFontSpec spec
;
1427 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
1428 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
1432 xdrawcursor(int cx
, int cy
, Glyph g
, int ox
, int oy
, Glyph og
)
1436 /* remove the old cursor */
1437 if (selected(ox
, oy
))
1438 og
.mode
^= ATTR_REVERSE
;
1439 xdrawglyph(og
, ox
, oy
);
1441 if (IS_SET(MODE_HIDE
))
1445 * Select the right color for the right mode.
1447 g
.mode
&= ATTR_BOLD
|ATTR_ITALIC
|ATTR_UNDERLINE
|ATTR_STRUCK
|ATTR_WIDE
;
1449 if (IS_SET(MODE_REVERSE
)) {
1450 g
.mode
|= ATTR_REVERSE
;
1452 if (selected(cx
, cy
)) {
1453 drawcol
= dc
.col
[defaultcs
];
1456 drawcol
= dc
.col
[defaultrcs
];
1460 if (selected(cx
, cy
)) {
1467 drawcol
= dc
.col
[g
.bg
];
1470 /* draw the new one */
1471 if (IS_SET(MODE_FOCUSED
)) {
1472 switch (win
.cursor
) {
1473 case 7: /* st extension: snowman (U+2603) */
1475 case 0: /* Blinking Block */
1476 case 1: /* Blinking Block (Default) */
1477 case 2: /* Steady Block */
1478 xdrawglyph(g
, cx
, cy
);
1480 case 3: /* Blinking Underline */
1481 case 4: /* Steady Underline */
1482 XftDrawRect(xw
.draw
, &drawcol
,
1483 borderpx
+ cx
* win
.cw
,
1484 borderpx
+ (cy
+ 1) * win
.ch
- \
1486 win
.cw
, cursorthickness
);
1488 case 5: /* Blinking bar */
1489 case 6: /* Steady bar */
1490 XftDrawRect(xw
.draw
, &drawcol
,
1491 borderpx
+ cx
* win
.cw
,
1492 borderpx
+ cy
* win
.ch
,
1493 cursorthickness
, win
.ch
);
1497 XftDrawRect(xw
.draw
, &drawcol
,
1498 borderpx
+ cx
* win
.cw
,
1499 borderpx
+ cy
* win
.ch
,
1501 XftDrawRect(xw
.draw
, &drawcol
,
1502 borderpx
+ cx
* win
.cw
,
1503 borderpx
+ cy
* win
.ch
,
1505 XftDrawRect(xw
.draw
, &drawcol
,
1506 borderpx
+ (cx
+ 1) * win
.cw
- 1,
1507 borderpx
+ cy
* win
.ch
,
1509 XftDrawRect(xw
.draw
, &drawcol
,
1510 borderpx
+ cx
* win
.cw
,
1511 borderpx
+ (cy
+ 1) * win
.ch
- 1,
1519 char buf
[sizeof(long) * 8 + 1];
1521 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1522 setenv("WINDOWID", buf
, 1);
1529 DEFAULT(p
, opt_title
);
1531 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1533 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
1534 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
1541 return IS_SET(MODE_VISIBLE
);
1545 xdrawline(Line line
, int x1
, int y1
, int x2
)
1547 int i
, x
, ox
, numspecs
;
1549 XftGlyphFontSpec
*specs
= xw
.specbuf
;
1551 numspecs
= xmakeglyphfontspecs(specs
, &line
[x1
], x2
- x1
, x1
, y1
);
1553 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
1555 if (new.mode
== ATTR_WDUMMY
)
1557 if (selected(x
, y1
))
1558 new.mode
^= ATTR_REVERSE
;
1559 if (i
> 0 && ATTRCMP(base
, new)) {
1560 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1572 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1578 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, win
.w
,
1580 XSetForeground(xw
.dpy
, dc
.gc
,
1581 dc
.col
[IS_SET(MODE_REVERSE
)?
1582 defaultfg
: defaultbg
].pixel
);
1586 xximspot(int x
, int y
)
1588 XPoint spot
= { borderpx
+ x
* win
.cw
, borderpx
+ (y
+ 1) * win
.ch
};
1589 XVaNestedList attr
= XVaCreateNestedList(0, XNSpotLocation
, &spot
, NULL
);
1591 XSetICValues(xw
.xic
, XNPreeditAttributes
, attr
, NULL
);
1602 visibility(XEvent
*ev
)
1604 XVisibilityEvent
*e
= &ev
->xvisibility
;
1606 MODBIT(win
.mode
, e
->state
!= VisibilityFullyObscured
, MODE_VISIBLE
);
1612 win
.mode
&= ~MODE_VISIBLE
;
1616 xsetpointermotion(int set
)
1618 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
1619 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
1623 xsetmode(int set
, unsigned int flags
)
1625 int mode
= win
.mode
;
1626 MODBIT(win
.mode
, set
, flags
);
1627 if ((win
.mode
& MODE_REVERSE
) != (mode
& MODE_REVERSE
))
1632 xsetcursor(int cursor
)
1635 if (!BETWEEN(cursor
, 0, 6))
1637 win
.cursor
= cursor
;
1642 xseturgency(int add
)
1644 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1646 MODBIT(h
->flags
, add
, XUrgencyHint
);
1647 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1654 if (!(IS_SET(MODE_FOCUSED
)))
1657 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
1663 XFocusChangeEvent
*e
= &ev
->xfocus
;
1665 if (e
->mode
== NotifyGrab
)
1668 if (ev
->type
== FocusIn
) {
1669 XSetICFocus(xw
.xic
);
1670 win
.mode
|= MODE_FOCUSED
;
1672 if (IS_SET(MODE_FOCUS
))
1673 ttywrite("\033[I", 3, 0);
1675 XUnsetICFocus(xw
.xic
);
1676 win
.mode
&= ~MODE_FOCUSED
;
1677 if (IS_SET(MODE_FOCUS
))
1678 ttywrite("\033[O", 3, 0);
1683 match(uint mask
, uint state
)
1685 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
1689 kmap(KeySym k
, uint state
)
1694 /* Check for mapped keys out of X11 function keys. */
1695 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
1696 if (mappedkeys
[i
] == k
)
1699 if (i
== LEN(mappedkeys
)) {
1700 if ((k
& 0xFFFF) < 0xFD00)
1704 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
1708 if (!match(kp
->mask
, state
))
1711 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
1713 if (IS_SET(MODE_NUMLOCK
) && kp
->appkey
== 2)
1716 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
1728 XKeyEvent
*e
= &ev
->xkey
;
1730 char buf
[32], *customkey
;
1736 if (IS_SET(MODE_KBDLOCK
))
1739 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
1741 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
1742 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
1743 bp
->func(&(bp
->arg
));
1748 /* 2. custom keys from config.h */
1749 if ((customkey
= kmap(ksym
, e
->state
))) {
1750 ttywrite(customkey
, strlen(customkey
), 1);
1754 /* 3. composed string from input method */
1757 if (len
== 1 && e
->state
& Mod1Mask
) {
1758 if (IS_SET(MODE_8BIT
)) {
1761 len
= utf8encode(c
, buf
);
1769 ttywrite(buf
, len
, 1);
1778 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1780 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1781 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1782 win
.mode
|= MODE_FOCUSED
;
1784 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1785 win
.mode
&= ~MODE_FOCUSED
;
1787 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
1796 if (e
->xconfigure
.width
== win
.w
&& e
->xconfigure
.height
== win
.h
)
1799 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
1806 int w
= win
.w
, h
= win
.h
;
1808 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
1810 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
1813 /* Waiting for window mapping */
1815 XNextEvent(xw
.dpy
, &ev
);
1817 * This XFilterEvent call is required because of XOpenIM. It
1818 * does filter out the key event and some client message for
1819 * the input method too.
1821 if (XFilterEvent(&ev
, None
))
1823 if (ev
.type
== ConfigureNotify
) {
1824 w
= ev
.xconfigure
.width
;
1825 h
= ev
.xconfigure
.height
;
1827 } while (ev
.type
!= MapNotify
);
1829 ttyfd
= ttynew(opt_line
, shell
, opt_io
, opt_cmd
);
1832 clock_gettime(CLOCK_MONOTONIC
, &last
);
1835 for (xev
= actionfps
;;) {
1837 FD_SET(ttyfd
, &rfd
);
1840 if (pselect(MAX(xfd
, ttyfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
1843 die("select failed: %s\n", strerror(errno
));
1845 if (FD_ISSET(ttyfd
, &rfd
)) {
1848 blinkset
= tattrset(ATTR_BLINK
);
1850 MODBIT(win
.mode
, 0, MODE_BLINK
);
1854 if (FD_ISSET(xfd
, &rfd
))
1857 clock_gettime(CLOCK_MONOTONIC
, &now
);
1858 drawtimeout
.tv_sec
= 0;
1859 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
1863 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
1864 tsetdirtattr(ATTR_BLINK
);
1865 win
.mode
^= MODE_BLINK
;
1869 deltatime
= TIMEDIFF(now
, last
);
1870 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
1876 while (XPending(xw
.dpy
)) {
1877 XNextEvent(xw
.dpy
, &ev
);
1878 if (XFilterEvent(&ev
, None
))
1880 if (handler
[ev
.type
])
1881 (handler
[ev
.type
])(&ev
);
1887 if (xev
&& !FD_ISSET(xfd
, &rfd
))
1889 if (!FD_ISSET(ttyfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
1891 if (TIMEDIFF(now
, lastblink
) \
1893 drawtimeout
.tv_nsec
= 1000;
1895 drawtimeout
.tv_nsec
= (1E6
* \
1900 drawtimeout
.tv_sec
= \
1901 drawtimeout
.tv_nsec
/ 1E9
;
1902 drawtimeout
.tv_nsec
%= (long)1E9
;
1914 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
1915 " [-n name] [-o file]\n"
1916 " [-T title] [-t title] [-w windowid]"
1917 " [[-e] command [args ...]]\n"
1918 " %s [-aiv] [-c class] [-f font] [-g geometry]"
1919 " [-n name] [-o file]\n"
1920 " [-T title] [-t title] [-w windowid] -l line"
1921 " [stty_args ...]\n", argv0
, argv0
);
1925 main(int argc
, char *argv
[])
1929 win
.cursor
= cursorshape
;
1936 opt_class
= EARGF(usage());
1943 opt_font
= EARGF(usage());
1946 xw
.gm
= XParseGeometry(EARGF(usage()),
1947 &xw
.l
, &xw
.t
, &cols
, &rows
);
1953 opt_io
= EARGF(usage());
1956 opt_line
= EARGF(usage());
1959 opt_name
= EARGF(usage());
1963 opt_title
= EARGF(usage());
1966 opt_embed
= EARGF(usage());
1969 die("%s " VERSION
"\n", argv0
);
1976 if (argc
> 0) /* eat all remaining arguments */
1980 opt_title
= (opt_line
|| !opt_cmd
) ? "st" : opt_cmd
[0];
1982 setlocale(LC_CTYPE
, "");
1983 XSetLocaleModifiers("");
1984 cols
= MAX(cols
, 1);
1985 rows
= MAX(rows
, 1);