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 xinit(int, int);
143 static void cresize(int, int);
144 static void xresize(int, int);
145 static void xhints(void);
146 static int xloadcolor(int, const char *, Color
*);
147 static int xloadfont(Font
*, FcPattern
*);
148 static void xloadfonts(char *, double);
149 static void xunloadfont(Font
*);
150 static void xunloadfonts(void);
151 static void xsetenv(void);
152 static void xseturgency(int);
153 static int evcol(XEvent
*);
154 static int evrow(XEvent
*);
156 static void expose(XEvent
*);
157 static void visibility(XEvent
*);
158 static void unmap(XEvent
*);
159 static void kpress(XEvent
*);
160 static void cmessage(XEvent
*);
161 static void resize(XEvent
*);
162 static void focus(XEvent
*);
163 static void brelease(XEvent
*);
164 static void bpress(XEvent
*);
165 static void bmotion(XEvent
*);
166 static void propnotify(XEvent
*);
167 static void selnotify(XEvent
*);
168 static void selclear_(XEvent
*);
169 static void selrequest(XEvent
*);
170 static void setsel(char *, Time
);
171 static void mousesel(XEvent
*, int);
172 static void mousereport(XEvent
*);
173 static char *kmap(KeySym
, uint
);
174 static int match(uint
, uint
);
176 static void run(void);
177 static void usage(void);
179 static void (*handler
[LASTEvent
])(XEvent
*) = {
181 [ClientMessage
] = cmessage
,
182 [ConfigureNotify
] = resize
,
183 [VisibilityNotify
] = visibility
,
184 [UnmapNotify
] = unmap
,
188 [MotionNotify
] = bmotion
,
189 [ButtonPress
] = bpress
,
190 [ButtonRelease
] = brelease
,
192 * Uncomment if you want the selection to disappear when you select something
193 * different in another window.
195 /* [SelectionClear] = selclear_, */
196 [SelectionNotify
] = selnotify
,
198 * PropertyNotify is only turned on when there is some INCR transfer happening
199 * for the selection retrieval.
201 [PropertyNotify
] = propnotify
,
202 [SelectionRequest
] = selrequest
,
208 static XSelection xsel
;
209 static TermWindow win
;
211 /* Font Ring Cache */
225 /* Fontcache is an array now. A new font will be appended to the array. */
226 static Fontcache frc
[16];
227 static int frclen
= 0;
228 static char *usedfont
= NULL
;
229 static double usedfontsize
= 0;
230 static double defaultfontsize
= 0;
232 static char *opt_class
= NULL
;
233 static char **opt_cmd
= NULL
;
234 static char *opt_embed
= NULL
;
235 static char *opt_font
= NULL
;
236 static char *opt_io
= NULL
;
237 static char *opt_line
= NULL
;
238 static char *opt_name
= NULL
;
239 static char *opt_title
= NULL
;
241 static int oldbutton
= 3; /* button event on startup: 3 = release */
244 clipcopy(const Arg
*dummy
)
248 if (xsel
.clipboard
!= NULL
)
249 free(xsel
.clipboard
);
251 if (xsel
.primary
!= NULL
) {
252 xsel
.clipboard
= xstrdup(xsel
.primary
);
253 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
254 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
259 clippaste(const Arg
*dummy
)
263 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
264 XConvertSelection(xw
.dpy
, clipboard
, xsel
.xtarget
, clipboard
,
265 xw
.win
, CurrentTime
);
269 selpaste(const Arg
*dummy
)
271 XConvertSelection(xw
.dpy
, XA_PRIMARY
, xsel
.xtarget
, XA_PRIMARY
,
272 xw
.win
, CurrentTime
);
276 numlock(const Arg
*dummy
)
278 win
.mode
^= MODE_NUMLOCK
;
286 larg
.f
= usedfontsize
+ arg
->f
;
291 zoomabs(const Arg
*arg
)
294 xloadfonts(usedfont
, arg
->f
);
301 zoomreset(const Arg
*arg
)
305 if (defaultfontsize
> 0) {
306 larg
.f
= defaultfontsize
;
314 int x
= e
->xbutton
.x
- borderpx
;
315 LIMIT(x
, 0, win
.tw
- 1);
322 int y
= e
->xbutton
.y
- borderpx
;
323 LIMIT(y
, 0, win
.th
- 1);
328 mousesel(XEvent
*e
, int done
)
330 int type
, seltype
= SEL_REGULAR
;
331 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forceselmod
);
333 for (type
= 1; type
< LEN(selmasks
); ++type
) {
334 if (match(selmasks
[type
], state
)) {
339 selextend(evcol(e
), evrow(e
), seltype
, done
);
341 setsel(getsel(), e
->xbutton
.time
);
345 mousereport(XEvent
*e
)
347 int len
, x
= evcol(e
), y
= evrow(e
),
348 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
;
353 if (e
->xbutton
.type
== MotionNotify
) {
354 if (x
== ox
&& y
== oy
)
356 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
358 /* MOUSE_MOTION: no reporting if no button is pressed */
359 if (IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
362 button
= oldbutton
+ 32;
366 if (!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
373 if (e
->xbutton
.type
== ButtonPress
) {
377 } else if (e
->xbutton
.type
== ButtonRelease
) {
379 /* MODE_MOUSEX10: no button release reporting */
380 if (IS_SET(MODE_MOUSEX10
))
382 if (button
== 64 || button
== 65)
387 if (!IS_SET(MODE_MOUSEX10
)) {
388 button
+= ((state
& ShiftMask
) ? 4 : 0)
389 + ((state
& Mod4Mask
) ? 8 : 0)
390 + ((state
& ControlMask
) ? 16 : 0);
393 if (IS_SET(MODE_MOUSESGR
)) {
394 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
396 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
397 } else if (x
< 223 && y
< 223) {
398 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
399 32+button
, 32+x
+1, 32+y
+1);
404 ttywrite(buf
, len
, 0);
414 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
419 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
420 if (e
->xbutton
.button
== ms
->b
421 && match(ms
->mask
, e
->xbutton
.state
)) {
422 ttywrite(ms
->s
, strlen(ms
->s
), 1);
427 if (e
->xbutton
.button
== Button1
) {
429 * If the user clicks below predefined timeouts specific
430 * snapping behaviour is exposed.
432 clock_gettime(CLOCK_MONOTONIC
, &now
);
433 if (TIMEDIFF(now
, xsel
.tclick2
) <= tripleclicktimeout
) {
435 } else if (TIMEDIFF(now
, xsel
.tclick1
) <= doubleclicktimeout
) {
440 xsel
.tclick2
= xsel
.tclick1
;
443 selstart(evcol(e
), evrow(e
), snap
);
448 propnotify(XEvent
*e
)
450 XPropertyEvent
*xpev
;
451 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
453 xpev
= &e
->xproperty
;
454 if (xpev
->state
== PropertyNewValue
&&
455 (xpev
->atom
== XA_PRIMARY
||
456 xpev
->atom
== clipboard
)) {
464 ulong nitems
, ofs
, rem
;
466 uchar
*data
, *last
, *repl
;
467 Atom type
, incratom
, property
= None
;
469 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
472 if (e
->type
== SelectionNotify
)
473 property
= e
->xselection
.property
;
474 else if (e
->type
== PropertyNotify
)
475 property
= e
->xproperty
.atom
;
477 if (property
== None
)
481 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
482 BUFSIZ
/4, False
, AnyPropertyType
,
483 &type
, &format
, &nitems
, &rem
,
485 fprintf(stderr
, "Clipboard allocation failed\n");
489 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
491 * If there is some PropertyNotify with no data, then
492 * this is the signal of the selection owner that all
493 * data has been transferred. We won't need to receive
494 * PropertyNotify events anymore.
496 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
497 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
501 if (type
== incratom
) {
503 * Activate the PropertyNotify events so we receive
504 * when the selection owner does send us the next
507 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
508 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
512 * Deleting the property is the transfer start signal.
514 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
520 * Line endings are inconsistent in the terminal and GUI world
521 * copy and pasting. When receiving some selection data,
522 * replace all '\n' with '\r'.
523 * FIXME: Fix the computer world.
526 last
= data
+ nitems
* format
/ 8;
527 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
531 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
532 ttywrite("\033[200~", 6, 0);
533 ttywrite((char *)data
, nitems
* format
/ 8, 1);
534 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
535 ttywrite("\033[201~", 6, 0);
537 /* number of 32-bit chunks returned */
538 ofs
+= nitems
* format
/ 32;
542 * Deleting the property again tells the selection owner to send the
543 * next data chunk in the property.
545 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
561 selrequest(XEvent
*e
)
563 XSelectionRequestEvent
*xsre
;
565 Atom xa_targets
, string
, clipboard
;
568 xsre
= (XSelectionRequestEvent
*) e
;
569 xev
.type
= SelectionNotify
;
570 xev
.requestor
= xsre
->requestor
;
571 xev
.selection
= xsre
->selection
;
572 xev
.target
= xsre
->target
;
573 xev
.time
= xsre
->time
;
574 if (xsre
->property
== None
)
575 xsre
->property
= xsre
->target
;
580 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
581 if (xsre
->target
== xa_targets
) {
582 /* respond with the supported type */
583 string
= xsel
.xtarget
;
584 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
585 XA_ATOM
, 32, PropModeReplace
,
586 (uchar
*) &string
, 1);
587 xev
.property
= xsre
->property
;
588 } else if (xsre
->target
== xsel
.xtarget
|| xsre
->target
== XA_STRING
) {
590 * xith XA_STRING non ascii characters may be incorrect in the
591 * requestor. It is not our problem, use utf8.
593 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
594 if (xsre
->selection
== XA_PRIMARY
) {
595 seltext
= xsel
.primary
;
596 } else if (xsre
->selection
== clipboard
) {
597 seltext
= xsel
.clipboard
;
600 "Unhandled clipboard selection 0x%lx\n",
604 if (seltext
!= NULL
) {
605 XChangeProperty(xsre
->display
, xsre
->requestor
,
606 xsre
->property
, xsre
->target
,
608 (uchar
*)seltext
, strlen(seltext
));
609 xev
.property
= xsre
->property
;
613 /* all done, send a notification to the listener */
614 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
615 fprintf(stderr
, "Error sending SelectionNotify event\n");
619 setsel(char *str
, Time t
)
624 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
625 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
632 setsel(str
, CurrentTime
);
638 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
643 if (e
->xbutton
.button
== Button2
)
645 else if (e
->xbutton
.button
== Button1
)
652 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
661 cresize(int width
, int height
)
670 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
671 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
675 ttyresize(win
.tw
, win
.th
);
679 xresize(int col
, int row
)
681 win
.tw
= MAX(1, col
* win
.cw
);
682 win
.th
= MAX(1, row
* win
.ch
);
684 XFreePixmap(xw
.dpy
, xw
.buf
);
685 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
686 DefaultDepth(xw
.dpy
, xw
.scr
));
687 XftDrawChange(xw
.draw
, xw
.buf
);
688 xclear(0, 0, win
.w
, win
.h
);
690 /* resize to new width */
691 xw
.specbuf
= xrealloc(xw
.specbuf
, col
* sizeof(GlyphFontSpec
));
697 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
701 xloadcolor(int i
, const char *name
, Color
*ncolor
)
703 XRenderColor color
= { .alpha
= 0xffff };
706 if (BETWEEN(i
, 16, 255)) { /* 256 color */
707 if (i
< 6*6*6+16) { /* same colors as xterm */
708 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
709 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
710 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
711 } else { /* greyscale */
712 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
713 color
.green
= color
.blue
= color
.red
;
715 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
716 xw
.cmap
, &color
, ncolor
);
721 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
731 dc
.collen
= MAX(LEN(colorname
), 256);
732 dc
.col
= xmalloc(dc
.collen
* sizeof(Color
));
735 for (cp
= dc
.col
; cp
< &dc
.col
[dc
.collen
]; ++cp
)
736 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
739 for (i
= 0; i
< dc
.collen
; i
++)
740 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
742 die("Could not allocate color '%s'\n", colorname
[i
]);
744 die("Could not allocate color %d\n", i
);
750 xsetcolorname(int x
, const char *name
)
754 if (!BETWEEN(x
, 0, dc
.collen
))
758 if (!xloadcolor(x
, name
, &ncolor
))
761 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
768 * Absolute coordinates.
771 xclear(int x1
, int y1
, int x2
, int y2
)
774 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
775 x1
, y1
, x2
-x1
, y2
-y1
);
781 XClassHint
class = {opt_name
? opt_name
: termname
,
782 opt_class
? opt_class
: termname
};
783 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
786 sizeh
= XAllocSizeHints();
788 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
789 sizeh
->height
= win
.h
;
790 sizeh
->width
= win
.w
;
791 sizeh
->height_inc
= win
.ch
;
792 sizeh
->width_inc
= win
.cw
;
793 sizeh
->base_height
= 2 * borderpx
;
794 sizeh
->base_width
= 2 * borderpx
;
796 sizeh
->flags
|= PMaxSize
| PMinSize
;
797 sizeh
->min_width
= sizeh
->max_width
= win
.w
;
798 sizeh
->min_height
= sizeh
->max_height
= win
.h
;
800 if (xw
.gm
& (XValue
|YValue
)) {
801 sizeh
->flags
|= USPosition
| PWinGravity
;
804 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
807 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
813 xgeommasktogravity(int mask
)
815 switch (mask
& (XNegative
|YNegative
)) {
817 return NorthWestGravity
;
819 return NorthEastGravity
;
821 return SouthWestGravity
;
824 return SouthEastGravity
;
828 xloadfont(Font
*f
, FcPattern
*pattern
)
830 FcPattern
*configured
;
834 int wantattr
, haveattr
;
837 * Manually configure instead of calling XftMatchFont
838 * so that we can use the configured pattern for
839 * "missing glyph" lookups.
841 configured
= FcPatternDuplicate(pattern
);
845 FcConfigSubstitute(NULL
, configured
, FcMatchPattern
);
846 XftDefaultSubstitute(xw
.dpy
, xw
.scr
, configured
);
848 match
= FcFontMatch(NULL
, configured
, &result
);
850 FcPatternDestroy(configured
);
854 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
855 FcPatternDestroy(configured
);
856 FcPatternDestroy(match
);
860 if ((XftPatternGetInteger(pattern
, "slant", 0, &wantattr
) ==
863 * Check if xft was unable to find a font with the appropriate
864 * slant but gave us one anyway. Try to mitigate.
866 if ((XftPatternGetInteger(f
->match
->pattern
, "slant", 0,
867 &haveattr
) != XftResultMatch
) || haveattr
< wantattr
) {
869 fputs("st: font slant does not match\n", stderr
);
873 if ((XftPatternGetInteger(pattern
, "weight", 0, &wantattr
) ==
875 if ((XftPatternGetInteger(f
->match
->pattern
, "weight", 0,
876 &haveattr
) != XftResultMatch
) || haveattr
!= wantattr
) {
878 fputs("st: font weight does not match\n", stderr
);
882 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
883 (const FcChar8
*) ascii_printable
,
884 strlen(ascii_printable
), &extents
);
887 f
->pattern
= configured
;
889 f
->ascent
= f
->match
->ascent
;
890 f
->descent
= f
->match
->descent
;
892 f
->rbearing
= f
->match
->max_advance_width
;
894 f
->height
= f
->ascent
+ f
->descent
;
895 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
901 xloadfonts(char *fontstr
, double fontsize
)
906 if (fontstr
[0] == '-') {
907 pattern
= XftXlfdParse(fontstr
, False
, False
);
909 pattern
= FcNameParse((FcChar8
*)fontstr
);
913 die("st: can't open font %s\n", fontstr
);
916 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
917 FcPatternDel(pattern
, FC_SIZE
);
918 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
919 usedfontsize
= fontsize
;
921 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
923 usedfontsize
= fontval
;
924 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
929 * Default font size is 12, if none given. This is to
930 * have a known usedfontsize value.
932 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
935 defaultfontsize
= usedfontsize
;
938 if (xloadfont(&dc
.font
, pattern
))
939 die("st: can't open font %s\n", fontstr
);
941 if (usedfontsize
< 0) {
942 FcPatternGetDouble(dc
.font
.match
->pattern
,
943 FC_PIXEL_SIZE
, 0, &fontval
);
944 usedfontsize
= fontval
;
946 defaultfontsize
= fontval
;
949 /* Setting character width and height. */
950 win
.cw
= ceilf(dc
.font
.width
* cwscale
);
951 win
.ch
= ceilf(dc
.font
.height
* chscale
);
953 FcPatternDel(pattern
, FC_SLANT
);
954 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
955 if (xloadfont(&dc
.ifont
, pattern
))
956 die("st: can't open font %s\n", fontstr
);
958 FcPatternDel(pattern
, FC_WEIGHT
);
959 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
960 if (xloadfont(&dc
.ibfont
, pattern
))
961 die("st: can't open font %s\n", fontstr
);
963 FcPatternDel(pattern
, FC_SLANT
);
964 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
965 if (xloadfont(&dc
.bfont
, pattern
))
966 die("st: can't open font %s\n", fontstr
);
968 FcPatternDestroy(pattern
);
974 XftFontClose(xw
.dpy
, f
->match
);
975 FcPatternDestroy(f
->pattern
);
977 FcFontSetDestroy(f
->set
);
983 /* Free the loaded fonts in the font cache. */
985 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
987 xunloadfont(&dc
.font
);
988 xunloadfont(&dc
.bfont
);
989 xunloadfont(&dc
.ifont
);
990 xunloadfont(&dc
.ibfont
);
994 xinit(int cols
, int rows
)
999 pid_t thispid
= getpid();
1000 XColor xmousefg
, xmousebg
;
1002 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
1003 die("Can't open display\n");
1004 xw
.scr
= XDefaultScreen(xw
.dpy
);
1005 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
1009 die("Could not init fontconfig.\n");
1011 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
1012 xloadfonts(usedfont
, 0);
1015 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1018 /* adjust fixed window geometry */
1019 win
.w
= 2 * borderpx
+ cols
* win
.cw
;
1020 win
.h
= 2 * borderpx
+ rows
* win
.ch
;
1021 if (xw
.gm
& XNegative
)
1022 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - win
.w
- 2;
1023 if (xw
.gm
& YNegative
)
1024 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - win
.h
- 2;
1027 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
1028 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
1029 xw
.attrs
.bit_gravity
= NorthWestGravity
;
1030 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1031 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1032 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1033 xw
.attrs
.colormap
= xw
.cmap
;
1035 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
1036 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
1037 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
1038 win
.w
, win
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1039 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
1040 | CWEventMask
| CWColormap
, &xw
.attrs
);
1042 memset(&gcvalues
, 0, sizeof(gcvalues
));
1043 gcvalues
.graphics_exposures
= False
;
1044 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
1046 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
1047 DefaultDepth(xw
.dpy
, xw
.scr
));
1048 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
1049 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, win
.w
, win
.h
);
1051 /* font spec buffer */
1052 xw
.specbuf
= xmalloc(cols
* sizeof(GlyphFontSpec
));
1054 /* Xft rendering context */
1055 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
1058 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1059 XSetLocaleModifiers("@im=local");
1060 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1061 XSetLocaleModifiers("@im=");
1062 if ((xw
.xim
= XOpenIM(xw
.dpy
,
1063 NULL
, NULL
, NULL
)) == NULL
) {
1064 die("XOpenIM failed. Could not open input"
1069 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1070 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1071 XNFocusWindow
, xw
.win
, NULL
);
1073 die("XCreateIC failed. Could not obtain input method.\n");
1075 /* white cursor, black outline */
1076 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
1077 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1079 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
1080 xmousefg
.red
= 0xffff;
1081 xmousefg
.green
= 0xffff;
1082 xmousefg
.blue
= 0xffff;
1085 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
1086 xmousebg
.red
= 0x0000;
1087 xmousebg
.green
= 0x0000;
1088 xmousebg
.blue
= 0x0000;
1091 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
1093 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1094 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
1095 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
1096 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
1098 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
1099 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
1100 PropModeReplace
, (uchar
*)&thispid
, 1);
1102 win
.mode
= MODE_NUMLOCK
;
1104 XMapWindow(xw
.dpy
, xw
.win
);
1106 XSync(xw
.dpy
, False
);
1108 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick1
);
1109 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick2
);
1110 xsel
.primary
= NULL
;
1111 xsel
.clipboard
= NULL
;
1112 xsel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
1113 if (xsel
.xtarget
== None
)
1114 xsel
.xtarget
= XA_STRING
;
1118 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
1120 float winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
, xp
, yp
;
1121 ushort mode
, prevmode
= USHRT_MAX
;
1122 Font
*font
= &dc
.font
;
1123 int frcflags
= FRC_NORMAL
;
1124 float runewidth
= win
.cw
;
1128 FcPattern
*fcpattern
, *fontpattern
;
1129 FcFontSet
*fcsets
[] = { NULL
};
1130 FcCharSet
*fccharset
;
1131 int i
, f
, numspecs
= 0;
1133 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
1134 /* Fetch rune and mode for current glyph. */
1136 mode
= glyphs
[i
].mode
;
1138 /* Skip dummy wide-character spacing. */
1139 if (mode
== ATTR_WDUMMY
)
1142 /* Determine font for glyph if different from previous glyph. */
1143 if (prevmode
!= mode
) {
1146 frcflags
= FRC_NORMAL
;
1147 runewidth
= win
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
1148 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
1150 frcflags
= FRC_ITALICBOLD
;
1151 } else if (mode
& ATTR_ITALIC
) {
1153 frcflags
= FRC_ITALIC
;
1154 } else if (mode
& ATTR_BOLD
) {
1156 frcflags
= FRC_BOLD
;
1158 yp
= winy
+ font
->ascent
;
1161 /* Lookup character index with default font. */
1162 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
1164 specs
[numspecs
].font
= font
->match
;
1165 specs
[numspecs
].glyph
= glyphidx
;
1166 specs
[numspecs
].x
= (short)xp
;
1167 specs
[numspecs
].y
= (short)yp
;
1173 /* Fallback on font cache, search the font cache for match. */
1174 for (f
= 0; f
< frclen
; f
++) {
1175 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
1176 /* Everything correct. */
1177 if (glyphidx
&& frc
[f
].flags
== frcflags
)
1179 /* We got a default font for a not found glyph. */
1180 if (!glyphidx
&& frc
[f
].flags
== frcflags
1181 && frc
[f
].unicodep
== rune
) {
1186 /* Nothing was found. Use fontconfig to find matching font. */
1189 font
->set
= FcFontSort(0, font
->pattern
,
1191 fcsets
[0] = font
->set
;
1194 * Nothing was found in the cache. Now use
1195 * some dozen of Fontconfig calls to get the
1196 * font for one single character.
1198 * Xft and fontconfig are design failures.
1200 fcpattern
= FcPatternDuplicate(font
->pattern
);
1201 fccharset
= FcCharSetCreate();
1203 FcCharSetAddChar(fccharset
, rune
);
1204 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
1206 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
1208 FcConfigSubstitute(0, fcpattern
,
1210 FcDefaultSubstitute(fcpattern
);
1212 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
1216 * Overwrite or create the new cache entry.
1218 if (frclen
>= LEN(frc
)) {
1219 frclen
= LEN(frc
) - 1;
1220 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
1221 frc
[frclen
].unicodep
= 0;
1224 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
1226 if (!frc
[frclen
].font
)
1227 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1229 frc
[frclen
].flags
= frcflags
;
1230 frc
[frclen
].unicodep
= rune
;
1232 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
1237 FcPatternDestroy(fcpattern
);
1238 FcCharSetDestroy(fccharset
);
1241 specs
[numspecs
].font
= frc
[f
].font
;
1242 specs
[numspecs
].glyph
= glyphidx
;
1243 specs
[numspecs
].x
= (short)xp
;
1244 specs
[numspecs
].y
= (short)yp
;
1253 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
1255 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
1256 int winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
,
1257 width
= charlen
* win
.cw
;
1258 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
1259 XRenderColor colfg
, colbg
;
1262 /* Fallback on color display for attributes not supported by the font */
1263 if (base
.mode
& ATTR_ITALIC
&& base
.mode
& ATTR_BOLD
) {
1264 if (dc
.ibfont
.badslant
|| dc
.ibfont
.badweight
)
1265 base
.fg
= defaultattr
;
1266 } else if ((base
.mode
& ATTR_ITALIC
&& dc
.ifont
.badslant
) ||
1267 (base
.mode
& ATTR_BOLD
&& dc
.bfont
.badweight
)) {
1268 base
.fg
= defaultattr
;
1271 if (IS_TRUECOL(base
.fg
)) {
1272 colfg
.alpha
= 0xffff;
1273 colfg
.red
= TRUERED(base
.fg
);
1274 colfg
.green
= TRUEGREEN(base
.fg
);
1275 colfg
.blue
= TRUEBLUE(base
.fg
);
1276 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
1279 fg
= &dc
.col
[base
.fg
];
1282 if (IS_TRUECOL(base
.bg
)) {
1283 colbg
.alpha
= 0xffff;
1284 colbg
.green
= TRUEGREEN(base
.bg
);
1285 colbg
.red
= TRUERED(base
.bg
);
1286 colbg
.blue
= TRUEBLUE(base
.bg
);
1287 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
1290 bg
= &dc
.col
[base
.bg
];
1293 /* Change basic system colors [0-7] to bright system colors [8-15] */
1294 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
1295 fg
= &dc
.col
[base
.fg
+ 8];
1297 if (IS_SET(MODE_REVERSE
)) {
1298 if (fg
== &dc
.col
[defaultfg
]) {
1299 fg
= &dc
.col
[defaultbg
];
1301 colfg
.red
= ~fg
->color
.red
;
1302 colfg
.green
= ~fg
->color
.green
;
1303 colfg
.blue
= ~fg
->color
.blue
;
1304 colfg
.alpha
= fg
->color
.alpha
;
1305 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
1310 if (bg
== &dc
.col
[defaultbg
]) {
1311 bg
= &dc
.col
[defaultfg
];
1313 colbg
.red
= ~bg
->color
.red
;
1314 colbg
.green
= ~bg
->color
.green
;
1315 colbg
.blue
= ~bg
->color
.blue
;
1316 colbg
.alpha
= bg
->color
.alpha
;
1317 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
1323 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
1324 colfg
.red
= fg
->color
.red
/ 2;
1325 colfg
.green
= fg
->color
.green
/ 2;
1326 colfg
.blue
= fg
->color
.blue
/ 2;
1327 colfg
.alpha
= fg
->color
.alpha
;
1328 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
1332 if (base
.mode
& ATTR_REVERSE
) {
1338 if (base
.mode
& ATTR_BLINK
&& win
.mode
& MODE_BLINK
)
1341 if (base
.mode
& ATTR_INVISIBLE
)
1344 /* Intelligent cleaning up of the borders. */
1346 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
1348 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: 0));
1350 if (winx
+ width
>= borderpx
+ win
.tw
) {
1351 xclear(winx
+ width
, (y
== 0)? 0 : winy
, win
.w
,
1352 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: (winy
+ win
.ch
)));
1355 xclear(winx
, 0, winx
+ width
, borderpx
);
1356 if (winy
+ win
.ch
>= borderpx
+ win
.th
)
1357 xclear(winx
, winy
+ win
.ch
, winx
+ width
, win
.h
);
1359 /* Clean up the region we want to draw to. */
1360 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, win
.ch
);
1362 /* Set the clip region because Xft is sometimes dirty. */
1367 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
1369 /* Render the glyphs. */
1370 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
1372 /* Render underline and strikethrough. */
1373 if (base
.mode
& ATTR_UNDERLINE
) {
1374 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
1378 if (base
.mode
& ATTR_STRUCK
) {
1379 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
1383 /* Reset clip to none. */
1384 XftDrawSetClip(xw
.draw
, 0);
1388 xdrawglyph(Glyph g
, int x
, int y
)
1391 XftGlyphFontSpec spec
;
1393 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
1394 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
1398 xdrawcursor(int cx
, int cy
, Glyph g
, int ox
, int oy
, Glyph og
)
1402 /* remove the old cursor */
1403 if (selected(ox
, oy
))
1404 og
.mode
^= ATTR_REVERSE
;
1405 xdrawglyph(og
, ox
, oy
);
1407 if (IS_SET(MODE_HIDE
))
1411 * Select the right color for the right mode.
1413 g
.mode
&= ATTR_BOLD
|ATTR_ITALIC
|ATTR_UNDERLINE
|ATTR_STRUCK
|ATTR_WIDE
;
1415 if (IS_SET(MODE_REVERSE
)) {
1416 g
.mode
|= ATTR_REVERSE
;
1418 if (selected(cx
, cy
)) {
1419 drawcol
= dc
.col
[defaultcs
];
1422 drawcol
= dc
.col
[defaultrcs
];
1426 if (selected(cx
, cy
)) {
1433 drawcol
= dc
.col
[g
.bg
];
1436 /* draw the new one */
1437 if (IS_SET(MODE_FOCUSED
)) {
1438 switch (win
.cursor
) {
1439 case 7: /* st extension: snowman (U+2603) */
1441 case 0: /* Blinking Block */
1442 case 1: /* Blinking Block (Default) */
1443 case 2: /* Steady Block */
1444 xdrawglyph(g
, cx
, cy
);
1446 case 3: /* Blinking Underline */
1447 case 4: /* Steady Underline */
1448 XftDrawRect(xw
.draw
, &drawcol
,
1449 borderpx
+ cx
* win
.cw
,
1450 borderpx
+ (cy
+ 1) * win
.ch
- \
1452 win
.cw
, cursorthickness
);
1454 case 5: /* Blinking bar */
1455 case 6: /* Steady bar */
1456 XftDrawRect(xw
.draw
, &drawcol
,
1457 borderpx
+ cx
* win
.cw
,
1458 borderpx
+ cy
* win
.ch
,
1459 cursorthickness
, win
.ch
);
1463 XftDrawRect(xw
.draw
, &drawcol
,
1464 borderpx
+ cx
* win
.cw
,
1465 borderpx
+ cy
* win
.ch
,
1467 XftDrawRect(xw
.draw
, &drawcol
,
1468 borderpx
+ cx
* win
.cw
,
1469 borderpx
+ cy
* win
.ch
,
1471 XftDrawRect(xw
.draw
, &drawcol
,
1472 borderpx
+ (cx
+ 1) * win
.cw
- 1,
1473 borderpx
+ cy
* win
.ch
,
1475 XftDrawRect(xw
.draw
, &drawcol
,
1476 borderpx
+ cx
* win
.cw
,
1477 borderpx
+ (cy
+ 1) * win
.ch
- 1,
1485 char buf
[sizeof(long) * 8 + 1];
1487 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1488 setenv("WINDOWID", buf
, 1);
1495 DEFAULT(p
, opt_title
);
1497 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1499 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
1500 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
1507 return IS_SET(MODE_VISIBLE
);
1511 xdrawline(Line line
, int x1
, int y1
, int x2
)
1513 int i
, x
, ox
, numspecs
;
1515 XftGlyphFontSpec
*specs
= xw
.specbuf
;
1517 numspecs
= xmakeglyphfontspecs(specs
, &line
[x1
], x2
- x1
, x1
, y1
);
1519 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
1521 if (new.mode
== ATTR_WDUMMY
)
1523 if (selected(x
, y1
))
1524 new.mode
^= ATTR_REVERSE
;
1525 if (i
> 0 && ATTRCMP(base
, new)) {
1526 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1538 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1544 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, win
.w
,
1546 XSetForeground(xw
.dpy
, dc
.gc
,
1547 dc
.col
[IS_SET(MODE_REVERSE
)?
1548 defaultfg
: defaultbg
].pixel
);
1558 visibility(XEvent
*ev
)
1560 XVisibilityEvent
*e
= &ev
->xvisibility
;
1562 MODBIT(win
.mode
, e
->state
!= VisibilityFullyObscured
, MODE_VISIBLE
);
1568 win
.mode
&= ~MODE_VISIBLE
;
1572 xsetpointermotion(int set
)
1574 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
1575 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
1579 xsetmode(int set
, unsigned int flags
)
1581 int mode
= win
.mode
;
1582 MODBIT(win
.mode
, set
, flags
);
1583 if ((win
.mode
& MODE_REVERSE
) != (mode
& MODE_REVERSE
))
1588 xsetcursor(int cursor
)
1591 if (!BETWEEN(cursor
, 0, 6))
1593 win
.cursor
= cursor
;
1598 xseturgency(int add
)
1600 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1602 MODBIT(h
->flags
, add
, XUrgencyHint
);
1603 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1610 if (!(IS_SET(MODE_FOCUSED
)))
1613 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
1619 XFocusChangeEvent
*e
= &ev
->xfocus
;
1621 if (e
->mode
== NotifyGrab
)
1624 if (ev
->type
== FocusIn
) {
1625 XSetICFocus(xw
.xic
);
1626 win
.mode
|= MODE_FOCUSED
;
1628 if (IS_SET(MODE_FOCUS
))
1629 ttywrite("\033[I", 3, 0);
1631 XUnsetICFocus(xw
.xic
);
1632 win
.mode
&= ~MODE_FOCUSED
;
1633 if (IS_SET(MODE_FOCUS
))
1634 ttywrite("\033[O", 3, 0);
1639 match(uint mask
, uint state
)
1641 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
1645 kmap(KeySym k
, uint state
)
1650 /* Check for mapped keys out of X11 function keys. */
1651 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
1652 if (mappedkeys
[i
] == k
)
1655 if (i
== LEN(mappedkeys
)) {
1656 if ((k
& 0xFFFF) < 0xFD00)
1660 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
1664 if (!match(kp
->mask
, state
))
1667 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
1669 if (IS_SET(MODE_NUMLOCK
) && kp
->appkey
== 2)
1672 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
1684 XKeyEvent
*e
= &ev
->xkey
;
1686 char buf
[32], *customkey
;
1692 if (IS_SET(MODE_KBDLOCK
))
1695 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
1697 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
1698 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
1699 bp
->func(&(bp
->arg
));
1704 /* 2. custom keys from config.h */
1705 if ((customkey
= kmap(ksym
, e
->state
))) {
1706 ttywrite(customkey
, strlen(customkey
), 1);
1710 /* 3. composed string from input method */
1713 if (len
== 1 && e
->state
& Mod1Mask
) {
1714 if (IS_SET(MODE_8BIT
)) {
1717 len
= utf8encode(c
, buf
);
1725 ttywrite(buf
, len
, 1);
1734 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1736 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1737 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1738 win
.mode
|= MODE_FOCUSED
;
1740 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1741 win
.mode
&= ~MODE_FOCUSED
;
1743 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
1752 if (e
->xconfigure
.width
== win
.w
&& e
->xconfigure
.height
== win
.h
)
1755 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
1762 int w
= win
.w
, h
= win
.h
;
1764 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
1766 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
1769 /* Waiting for window mapping */
1771 XNextEvent(xw
.dpy
, &ev
);
1773 * This XFilterEvent call is required because of XOpenIM. It
1774 * does filter out the key event and some client message for
1775 * the input method too.
1777 if (XFilterEvent(&ev
, None
))
1779 if (ev
.type
== ConfigureNotify
) {
1780 w
= ev
.xconfigure
.width
;
1781 h
= ev
.xconfigure
.height
;
1783 } while (ev
.type
!= MapNotify
);
1785 ttyfd
= ttynew(opt_line
, shell
, opt_io
, opt_cmd
);
1788 clock_gettime(CLOCK_MONOTONIC
, &last
);
1791 for (xev
= actionfps
;;) {
1793 FD_SET(ttyfd
, &rfd
);
1796 if (pselect(MAX(xfd
, ttyfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
1799 die("select failed: %s\n", strerror(errno
));
1801 if (FD_ISSET(ttyfd
, &rfd
)) {
1804 blinkset
= tattrset(ATTR_BLINK
);
1806 MODBIT(win
.mode
, 0, MODE_BLINK
);
1810 if (FD_ISSET(xfd
, &rfd
))
1813 clock_gettime(CLOCK_MONOTONIC
, &now
);
1814 drawtimeout
.tv_sec
= 0;
1815 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
1819 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
1820 tsetdirtattr(ATTR_BLINK
);
1821 win
.mode
^= MODE_BLINK
;
1825 deltatime
= TIMEDIFF(now
, last
);
1826 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
1832 while (XPending(xw
.dpy
)) {
1833 XNextEvent(xw
.dpy
, &ev
);
1834 if (XFilterEvent(&ev
, None
))
1836 if (handler
[ev
.type
])
1837 (handler
[ev
.type
])(&ev
);
1843 if (xev
&& !FD_ISSET(xfd
, &rfd
))
1845 if (!FD_ISSET(ttyfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
1847 if (TIMEDIFF(now
, lastblink
) \
1849 drawtimeout
.tv_nsec
= 1000;
1851 drawtimeout
.tv_nsec
= (1E6
* \
1856 drawtimeout
.tv_sec
= \
1857 drawtimeout
.tv_nsec
/ 1E9
;
1858 drawtimeout
.tv_nsec
%= (long)1E9
;
1870 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
1871 " [-n name] [-o file]\n"
1872 " [-T title] [-t title] [-w windowid]"
1873 " [[-e] command [args ...]]\n"
1874 " %s [-aiv] [-c class] [-f font] [-g geometry]"
1875 " [-n name] [-o file]\n"
1876 " [-T title] [-t title] [-w windowid] -l line"
1877 " [stty_args ...]\n", argv0
, argv0
);
1881 main(int argc
, char *argv
[])
1885 win
.cursor
= cursorshape
;
1892 opt_class
= EARGF(usage());
1899 opt_font
= EARGF(usage());
1902 xw
.gm
= XParseGeometry(EARGF(usage()),
1903 &xw
.l
, &xw
.t
, &cols
, &rows
);
1909 opt_io
= EARGF(usage());
1912 opt_line
= EARGF(usage());
1915 opt_name
= EARGF(usage());
1919 opt_title
= EARGF(usage());
1922 opt_embed
= EARGF(usage());
1925 die("%s " VERSION
" (c) 2010-2016 st engineers\n", argv0
);
1933 /* eat all remaining arguments */
1935 if (!opt_title
&& !opt_line
)
1936 opt_title
= basename(xstrdup(argv
[0]));
1938 setlocale(LC_CTYPE
, "");
1939 XSetLocaleModifiers("");
1940 cols
= MAX(cols
, 1);
1941 rows
= MAX(rows
, 1);