1 /* See LICENSE for license details. */
6 #include <sys/select.h>
10 #include <X11/Xatom.h>
12 #include <X11/Xutil.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 */
83 GlyphFontSpec
*specbuf
; /* font spec buffer used for rendering */
84 Atom xembed
, wmdeletewin
, netwmname
, netwmpid
;
89 XSetWindowAttributes attrs
;
91 int isfixed
; /* is fixed geometry? */
92 int l
, t
; /* left and top offset */
93 int gm
; /* geometry mask */
98 char *primary
, *clipboard
;
99 struct timespec tclick1
;
100 struct timespec tclick2
;
119 /* Drawing Context */
123 Font font
, bfont
, ifont
, ibfont
;
127 static inline ushort
sixd_to_16bit(int);
128 static int xmakeglyphfontspecs(XftGlyphFontSpec
*, const Glyph
*, int, int, int);
129 static void xdrawglyphfontspecs(const XftGlyphFontSpec
*, Glyph
, int, int, int);
130 static void xdrawglyph(Glyph
, int, int);
131 static void xclear(int, int, int, int);
132 static int xgeommasktogravity(int);
133 static void xinit(int, int);
134 static void cresize(int, int);
135 static void xresize(int, int);
136 static int xloadfont(Font
*, FcPattern
*);
137 static void xloadfonts(char *, double);
138 static void xunloadfont(Font
*);
139 static void xunloadfonts(void);
140 static void xsetenv(void);
141 static void xseturgency(int);
142 static int x2col(int);
143 static int y2row(int);
145 static void expose(XEvent
*);
146 static void visibility(XEvent
*);
147 static void unmap(XEvent
*);
148 static void kpress(XEvent
*);
149 static void cmessage(XEvent
*);
150 static void resize(XEvent
*);
151 static void focus(XEvent
*);
152 static void brelease(XEvent
*);
153 static void bpress(XEvent
*);
154 static void bmotion(XEvent
*);
155 static void propnotify(XEvent
*);
156 static void selnotify(XEvent
*);
157 static void selclear_(XEvent
*);
158 static void selrequest(XEvent
*);
159 static void setsel(char *, Time
);
160 static void mousesel(XEvent
*, int);
161 static void mousereport(XEvent
*);
162 static char *kmap(KeySym
, uint
);
163 static int match(uint
, uint
);
165 static void run(void);
166 static void usage(void);
168 static void (*handler
[LASTEvent
])(XEvent
*) = {
170 [ClientMessage
] = cmessage
,
171 [ConfigureNotify
] = resize
,
172 [VisibilityNotify
] = visibility
,
173 [UnmapNotify
] = unmap
,
177 [MotionNotify
] = bmotion
,
178 [ButtonPress
] = bpress
,
179 [ButtonRelease
] = brelease
,
181 * Uncomment if you want the selection to disappear when you select something
182 * different in another window.
184 /* [SelectionClear] = selclear_, */
185 [SelectionNotify
] = selnotify
,
187 * PropertyNotify is only turned on when there is some INCR transfer happening
188 * for the selection retrieval.
190 [PropertyNotify
] = propnotify
,
191 [SelectionRequest
] = selrequest
,
197 static XSelection xsel
;
198 static TermWindow win
;
200 /* Font Ring Cache */
214 /* Fontcache is an array now. A new font will be appended to the array. */
215 static Fontcache frc
[16];
216 static int frclen
= 0;
217 static char *usedfont
= NULL
;
218 static double usedfontsize
= 0;
219 static double defaultfontsize
= 0;
221 static char *opt_class
= NULL
;
222 static char **opt_cmd
= NULL
;
223 static char *opt_embed
= NULL
;
224 static char *opt_font
= NULL
;
225 static char *opt_io
= NULL
;
226 static char *opt_line
= NULL
;
227 static char *opt_name
= NULL
;
228 static char *opt_title
= NULL
;
230 static int oldbutton
= 3; /* button event on startup: 3 = release */
233 clipcopy(const Arg
*dummy
)
237 if (xsel
.clipboard
!= NULL
)
238 free(xsel
.clipboard
);
240 if (xsel
.primary
!= NULL
) {
241 xsel
.clipboard
= xstrdup(xsel
.primary
);
242 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
243 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
248 clippaste(const Arg
*dummy
)
252 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
253 XConvertSelection(xw
.dpy
, clipboard
, xsel
.xtarget
, clipboard
,
254 xw
.win
, CurrentTime
);
258 selpaste(const Arg
*dummy
)
260 XConvertSelection(xw
.dpy
, XA_PRIMARY
, xsel
.xtarget
, XA_PRIMARY
,
261 xw
.win
, CurrentTime
);
265 numlock(const Arg
*dummy
)
267 win
.mode
^= MODE_NUMLOCK
;
275 larg
.f
= usedfontsize
+ arg
->f
;
280 zoomabs(const Arg
*arg
)
283 xloadfonts(usedfont
, arg
->f
);
290 zoomreset(const Arg
*arg
)
294 if (defaultfontsize
> 0) {
295 larg
.f
= defaultfontsize
;
304 LIMIT(x
, 0, win
.tw
- 1);
312 LIMIT(y
, 0, win
.th
- 1);
317 mousesel(XEvent
*e
, int done
)
319 int type
, seltype
= SEL_REGULAR
;
320 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forceselmod
);
322 for (type
= 1; type
< LEN(selmasks
); ++type
) {
323 if (match(selmasks
[type
], state
)) {
328 selextend(x2col(e
->xbutton
.x
), y2row(e
->xbutton
.y
), seltype
, done
);
330 setsel(getsel(), e
->xbutton
.time
);
334 mousereport(XEvent
*e
)
336 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
337 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
343 if (e
->xbutton
.type
== MotionNotify
) {
344 if (x
== ox
&& y
== oy
)
346 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
348 /* MOUSE_MOTION: no reporting if no button is pressed */
349 if (IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
352 button
= oldbutton
+ 32;
356 if (!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
363 if (e
->xbutton
.type
== ButtonPress
) {
367 } else if (e
->xbutton
.type
== ButtonRelease
) {
369 /* MODE_MOUSEX10: no button release reporting */
370 if (IS_SET(MODE_MOUSEX10
))
372 if (button
== 64 || button
== 65)
377 if (!IS_SET(MODE_MOUSEX10
)) {
378 button
+= ((state
& ShiftMask
) ? 4 : 0)
379 + ((state
& Mod4Mask
) ? 8 : 0)
380 + ((state
& ControlMask
) ? 16 : 0);
383 if (IS_SET(MODE_MOUSESGR
)) {
384 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
386 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
387 } else if (x
< 223 && y
< 223) {
388 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
389 32+button
, 32+x
+1, 32+y
+1);
394 ttywrite(buf
, len
, 0);
404 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
409 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
410 if (e
->xbutton
.button
== ms
->b
411 && match(ms
->mask
, e
->xbutton
.state
)) {
412 ttywrite(ms
->s
, strlen(ms
->s
), 1);
417 if (e
->xbutton
.button
== Button1
) {
419 * If the user clicks below predefined timeouts specific
420 * snapping behaviour is exposed.
422 clock_gettime(CLOCK_MONOTONIC
, &now
);
423 if (TIMEDIFF(now
, xsel
.tclick2
) <= tripleclicktimeout
) {
425 } else if (TIMEDIFF(now
, xsel
.tclick1
) <= doubleclicktimeout
) {
430 xsel
.tclick2
= xsel
.tclick1
;
433 selstart(x2col(e
->xbutton
.x
), y2row(e
->xbutton
.y
), snap
);
438 propnotify(XEvent
*e
)
440 XPropertyEvent
*xpev
;
441 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
443 xpev
= &e
->xproperty
;
444 if (xpev
->state
== PropertyNewValue
&&
445 (xpev
->atom
== XA_PRIMARY
||
446 xpev
->atom
== clipboard
)) {
454 ulong nitems
, ofs
, rem
;
456 uchar
*data
, *last
, *repl
;
457 Atom type
, incratom
, property
;
459 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
462 if (e
->type
== SelectionNotify
) {
463 property
= e
->xselection
.property
;
464 } else if(e
->type
== PropertyNotify
) {
465 property
= e
->xproperty
.atom
;
469 if (property
== None
)
473 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
474 BUFSIZ
/4, False
, AnyPropertyType
,
475 &type
, &format
, &nitems
, &rem
,
477 fprintf(stderr
, "Clipboard allocation failed\n");
481 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
483 * If there is some PropertyNotify with no data, then
484 * this is the signal of the selection owner that all
485 * data has been transferred. We won't need to receive
486 * PropertyNotify events anymore.
488 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
489 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
493 if (type
== incratom
) {
495 * Activate the PropertyNotify events so we receive
496 * when the selection owner does send us the next
499 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
500 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
504 * Deleting the property is the transfer start signal.
506 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
512 * Line endings are inconsistent in the terminal and GUI world
513 * copy and pasting. When receiving some selection data,
514 * replace all '\n' with '\r'.
515 * FIXME: Fix the computer world.
518 last
= data
+ nitems
* format
/ 8;
519 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
523 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
524 ttywrite("\033[200~", 6, 0);
525 ttywrite((char *)data
, nitems
* format
/ 8, 1);
526 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
527 ttywrite("\033[201~", 6, 0);
529 /* number of 32-bit chunks returned */
530 ofs
+= nitems
* format
/ 32;
534 * Deleting the property again tells the selection owner to send the
535 * next data chunk in the property.
537 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
553 selrequest(XEvent
*e
)
555 XSelectionRequestEvent
*xsre
;
557 Atom xa_targets
, string
, clipboard
;
560 xsre
= (XSelectionRequestEvent
*) e
;
561 xev
.type
= SelectionNotify
;
562 xev
.requestor
= xsre
->requestor
;
563 xev
.selection
= xsre
->selection
;
564 xev
.target
= xsre
->target
;
565 xev
.time
= xsre
->time
;
566 if (xsre
->property
== None
)
567 xsre
->property
= xsre
->target
;
572 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
573 if (xsre
->target
== xa_targets
) {
574 /* respond with the supported type */
575 string
= xsel
.xtarget
;
576 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
577 XA_ATOM
, 32, PropModeReplace
,
578 (uchar
*) &string
, 1);
579 xev
.property
= xsre
->property
;
580 } else if (xsre
->target
== xsel
.xtarget
|| xsre
->target
== XA_STRING
) {
582 * xith XA_STRING non ascii characters may be incorrect in the
583 * requestor. It is not our problem, use utf8.
585 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
586 if (xsre
->selection
== XA_PRIMARY
) {
587 seltext
= xsel
.primary
;
588 } else if (xsre
->selection
== clipboard
) {
589 seltext
= xsel
.clipboard
;
592 "Unhandled clipboard selection 0x%lx\n",
596 if (seltext
!= NULL
) {
597 XChangeProperty(xsre
->display
, xsre
->requestor
,
598 xsre
->property
, xsre
->target
,
600 (uchar
*)seltext
, strlen(seltext
));
601 xev
.property
= xsre
->property
;
605 /* all done, send a notification to the listener */
606 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
607 fprintf(stderr
, "Error sending SelectionNotify event\n");
611 setsel(char *str
, Time t
)
616 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
617 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
624 setsel(str
, CurrentTime
);
630 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
635 if (e
->xbutton
.button
== Button2
)
637 else if (e
->xbutton
.button
== Button1
)
644 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
653 cresize(int width
, int height
)
662 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
663 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
667 ttyresize(win
.tw
, win
.th
);
671 xresize(int col
, int row
)
673 win
.tw
= MAX(1, col
* win
.cw
);
674 win
.th
= MAX(1, row
* win
.ch
);
676 XFreePixmap(xw
.dpy
, xw
.buf
);
677 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
678 DefaultDepth(xw
.dpy
, xw
.scr
));
679 XftDrawChange(xw
.draw
, xw
.buf
);
680 xclear(0, 0, win
.w
, win
.h
);
682 /* resize to new width */
683 xw
.specbuf
= xrealloc(xw
.specbuf
, col
* sizeof(GlyphFontSpec
));
689 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
693 xloadcolor(int i
, const char *name
, Color
*ncolor
)
695 XRenderColor color
= { .alpha
= 0xffff };
698 if (BETWEEN(i
, 16, 255)) { /* 256 color */
699 if (i
< 6*6*6+16) { /* same colors as xterm */
700 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
701 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
702 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
703 } else { /* greyscale */
704 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
705 color
.green
= color
.blue
= color
.red
;
707 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
708 xw
.cmap
, &color
, ncolor
);
713 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
723 dc
.collen
= MAX(LEN(colorname
), 256);
724 dc
.col
= xmalloc(dc
.collen
* sizeof(Color
));
727 for (cp
= dc
.col
; cp
< &dc
.col
[dc
.collen
]; ++cp
)
728 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
731 for (i
= 0; i
< dc
.collen
; i
++)
732 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
734 die("Could not allocate color '%s'\n", colorname
[i
]);
736 die("Could not allocate color %d\n", i
);
742 xsetcolorname(int x
, const char *name
)
746 if (!BETWEEN(x
, 0, dc
.collen
))
750 if (!xloadcolor(x
, name
, &ncolor
))
753 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
760 * Absolute coordinates.
763 xclear(int x1
, int y1
, int x2
, int y2
)
766 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
767 x1
, y1
, x2
-x1
, y2
-y1
);
773 XClassHint
class = {opt_name
? opt_name
: termname
,
774 opt_class
? opt_class
: termname
};
775 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
776 XSizeHints
*sizeh
= NULL
;
778 sizeh
= XAllocSizeHints();
780 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
781 sizeh
->height
= win
.h
;
782 sizeh
->width
= win
.w
;
783 sizeh
->height_inc
= win
.ch
;
784 sizeh
->width_inc
= win
.cw
;
785 sizeh
->base_height
= 2 * borderpx
;
786 sizeh
->base_width
= 2 * borderpx
;
788 sizeh
->flags
|= PMaxSize
| PMinSize
;
789 sizeh
->min_width
= sizeh
->max_width
= win
.w
;
790 sizeh
->min_height
= sizeh
->max_height
= win
.h
;
792 if (xw
.gm
& (XValue
|YValue
)) {
793 sizeh
->flags
|= USPosition
| PWinGravity
;
796 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
799 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
805 xgeommasktogravity(int mask
)
807 switch (mask
& (XNegative
|YNegative
)) {
809 return NorthWestGravity
;
811 return NorthEastGravity
;
813 return SouthWestGravity
;
816 return SouthEastGravity
;
820 xloadfont(Font
*f
, FcPattern
*pattern
)
822 FcPattern
*configured
;
826 int wantattr
, haveattr
;
829 * Manually configure instead of calling XftMatchFont
830 * so that we can use the configured pattern for
831 * "missing glyph" lookups.
833 configured
= FcPatternDuplicate(pattern
);
837 FcConfigSubstitute(NULL
, configured
, FcMatchPattern
);
838 XftDefaultSubstitute(xw
.dpy
, xw
.scr
, configured
);
840 match
= FcFontMatch(NULL
, configured
, &result
);
842 FcPatternDestroy(configured
);
846 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
847 FcPatternDestroy(configured
);
848 FcPatternDestroy(match
);
852 if ((XftPatternGetInteger(pattern
, "slant", 0, &wantattr
) ==
855 * Check if xft was unable to find a font with the appropriate
856 * slant but gave us one anyway. Try to mitigate.
858 if ((XftPatternGetInteger(f
->match
->pattern
, "slant", 0,
859 &haveattr
) != XftResultMatch
) || haveattr
< wantattr
) {
861 fputs("st: font slant does not match\n", stderr
);
865 if ((XftPatternGetInteger(pattern
, "weight", 0, &wantattr
) ==
867 if ((XftPatternGetInteger(f
->match
->pattern
, "weight", 0,
868 &haveattr
) != XftResultMatch
) || haveattr
!= wantattr
) {
870 fputs("st: font weight does not match\n", stderr
);
874 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
875 (const FcChar8
*) ascii_printable
,
876 strlen(ascii_printable
), &extents
);
879 f
->pattern
= configured
;
881 f
->ascent
= f
->match
->ascent
;
882 f
->descent
= f
->match
->descent
;
884 f
->rbearing
= f
->match
->max_advance_width
;
886 f
->height
= f
->ascent
+ f
->descent
;
887 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
893 xloadfonts(char *fontstr
, double fontsize
)
899 if (fontstr
[0] == '-') {
900 pattern
= XftXlfdParse(fontstr
, False
, False
);
902 pattern
= FcNameParse((FcChar8
*)fontstr
);
906 die("st: can't open font %s\n", fontstr
);
909 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
910 FcPatternDel(pattern
, FC_SIZE
);
911 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
912 usedfontsize
= fontsize
;
914 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
916 usedfontsize
= fontval
;
917 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
922 * Default font size is 12, if none given. This is to
923 * have a known usedfontsize value.
925 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
928 defaultfontsize
= usedfontsize
;
931 if (xloadfont(&dc
.font
, pattern
))
932 die("st: can't open font %s\n", fontstr
);
934 if (usedfontsize
< 0) {
935 FcPatternGetDouble(dc
.font
.match
->pattern
,
936 FC_PIXEL_SIZE
, 0, &fontval
);
937 usedfontsize
= fontval
;
939 defaultfontsize
= fontval
;
942 /* Setting character width and height. */
943 win
.cw
= ceilf(dc
.font
.width
* cwscale
);
944 win
.ch
= ceilf(dc
.font
.height
* chscale
);
946 FcPatternDel(pattern
, FC_SLANT
);
947 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
948 if (xloadfont(&dc
.ifont
, pattern
))
949 die("st: can't open font %s\n", fontstr
);
951 FcPatternDel(pattern
, FC_WEIGHT
);
952 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
953 if (xloadfont(&dc
.ibfont
, pattern
))
954 die("st: can't open font %s\n", fontstr
);
956 FcPatternDel(pattern
, FC_SLANT
);
957 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
958 if (xloadfont(&dc
.bfont
, pattern
))
959 die("st: can't open font %s\n", fontstr
);
961 FcPatternDestroy(pattern
);
967 XftFontClose(xw
.dpy
, f
->match
);
968 FcPatternDestroy(f
->pattern
);
970 FcFontSetDestroy(f
->set
);
976 /* Free the loaded fonts in the font cache. */
978 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
980 xunloadfont(&dc
.font
);
981 xunloadfont(&dc
.bfont
);
982 xunloadfont(&dc
.ifont
);
983 xunloadfont(&dc
.ibfont
);
987 xinit(int cols
, int rows
)
992 pid_t thispid
= getpid();
993 XColor xmousefg
, xmousebg
;
995 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
996 die("Can't open display\n");
997 xw
.scr
= XDefaultScreen(xw
.dpy
);
998 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
1002 die("Could not init fontconfig.\n");
1004 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
1005 xloadfonts(usedfont
, 0);
1008 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1011 /* adjust fixed window geometry */
1012 win
.w
= 2 * borderpx
+ cols
* win
.cw
;
1013 win
.h
= 2 * borderpx
+ rows
* win
.ch
;
1014 if (xw
.gm
& XNegative
)
1015 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - win
.w
- 2;
1016 if (xw
.gm
& YNegative
)
1017 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - win
.h
- 2;
1020 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
1021 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
1022 xw
.attrs
.bit_gravity
= NorthWestGravity
;
1023 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1024 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1025 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1026 xw
.attrs
.colormap
= xw
.cmap
;
1028 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
1029 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
1030 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
1031 win
.w
, win
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1032 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
1033 | CWEventMask
| CWColormap
, &xw
.attrs
);
1035 memset(&gcvalues
, 0, sizeof(gcvalues
));
1036 gcvalues
.graphics_exposures
= False
;
1037 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
1039 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
1040 DefaultDepth(xw
.dpy
, xw
.scr
));
1041 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
1042 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, win
.w
, win
.h
);
1044 /* font spec buffer */
1045 xw
.specbuf
= xmalloc(cols
* sizeof(GlyphFontSpec
));
1047 /* Xft rendering context */
1048 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
1051 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1052 XSetLocaleModifiers("@im=local");
1053 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1054 XSetLocaleModifiers("@im=");
1055 if ((xw
.xim
= XOpenIM(xw
.dpy
,
1056 NULL
, NULL
, NULL
)) == NULL
) {
1057 die("XOpenIM failed. Could not open input"
1062 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1063 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1064 XNFocusWindow
, xw
.win
, NULL
);
1066 die("XCreateIC failed. Could not obtain input method.\n");
1068 /* white cursor, black outline */
1069 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
1070 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1072 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
1073 xmousefg
.red
= 0xffff;
1074 xmousefg
.green
= 0xffff;
1075 xmousefg
.blue
= 0xffff;
1078 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
1079 xmousebg
.red
= 0x0000;
1080 xmousebg
.green
= 0x0000;
1081 xmousebg
.blue
= 0x0000;
1084 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
1086 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1087 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
1088 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
1089 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
1091 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
1092 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
1093 PropModeReplace
, (uchar
*)&thispid
, 1);
1095 win
.mode
= MODE_NUMLOCK
;
1097 XMapWindow(xw
.dpy
, xw
.win
);
1099 XSync(xw
.dpy
, False
);
1101 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick1
);
1102 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick2
);
1103 xsel
.primary
= NULL
;
1104 xsel
.clipboard
= NULL
;
1105 xsel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
1106 if (xsel
.xtarget
== None
)
1107 xsel
.xtarget
= XA_STRING
;
1111 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
1113 float winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
, xp
, yp
;
1114 ushort mode
, prevmode
= USHRT_MAX
;
1115 Font
*font
= &dc
.font
;
1116 int frcflags
= FRC_NORMAL
;
1117 float runewidth
= win
.cw
;
1121 FcPattern
*fcpattern
, *fontpattern
;
1122 FcFontSet
*fcsets
[] = { NULL
};
1123 FcCharSet
*fccharset
;
1124 int i
, f
, numspecs
= 0;
1126 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
1127 /* Fetch rune and mode for current glyph. */
1129 mode
= glyphs
[i
].mode
;
1131 /* Skip dummy wide-character spacing. */
1132 if (mode
== ATTR_WDUMMY
)
1135 /* Determine font for glyph if different from previous glyph. */
1136 if (prevmode
!= mode
) {
1139 frcflags
= FRC_NORMAL
;
1140 runewidth
= win
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
1141 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
1143 frcflags
= FRC_ITALICBOLD
;
1144 } else if (mode
& ATTR_ITALIC
) {
1146 frcflags
= FRC_ITALIC
;
1147 } else if (mode
& ATTR_BOLD
) {
1149 frcflags
= FRC_BOLD
;
1151 yp
= winy
+ font
->ascent
;
1154 /* Lookup character index with default font. */
1155 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
1157 specs
[numspecs
].font
= font
->match
;
1158 specs
[numspecs
].glyph
= glyphidx
;
1159 specs
[numspecs
].x
= (short)xp
;
1160 specs
[numspecs
].y
= (short)yp
;
1166 /* Fallback on font cache, search the font cache for match. */
1167 for (f
= 0; f
< frclen
; f
++) {
1168 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
1169 /* Everything correct. */
1170 if (glyphidx
&& frc
[f
].flags
== frcflags
)
1172 /* We got a default font for a not found glyph. */
1173 if (!glyphidx
&& frc
[f
].flags
== frcflags
1174 && frc
[f
].unicodep
== rune
) {
1179 /* Nothing was found. Use fontconfig to find matching font. */
1182 font
->set
= FcFontSort(0, font
->pattern
,
1184 fcsets
[0] = font
->set
;
1187 * Nothing was found in the cache. Now use
1188 * some dozen of Fontconfig calls to get the
1189 * font for one single character.
1191 * Xft and fontconfig are design failures.
1193 fcpattern
= FcPatternDuplicate(font
->pattern
);
1194 fccharset
= FcCharSetCreate();
1196 FcCharSetAddChar(fccharset
, rune
);
1197 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
1199 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
1201 FcConfigSubstitute(0, fcpattern
,
1203 FcDefaultSubstitute(fcpattern
);
1205 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
1209 * Overwrite or create the new cache entry.
1211 if (frclen
>= LEN(frc
)) {
1212 frclen
= LEN(frc
) - 1;
1213 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
1214 frc
[frclen
].unicodep
= 0;
1217 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
1219 if (!frc
[frclen
].font
)
1220 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1222 frc
[frclen
].flags
= frcflags
;
1223 frc
[frclen
].unicodep
= rune
;
1225 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
1230 FcPatternDestroy(fcpattern
);
1231 FcCharSetDestroy(fccharset
);
1234 specs
[numspecs
].font
= frc
[f
].font
;
1235 specs
[numspecs
].glyph
= glyphidx
;
1236 specs
[numspecs
].x
= (short)xp
;
1237 specs
[numspecs
].y
= (short)yp
;
1246 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
1248 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
1249 int winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
,
1250 width
= charlen
* win
.cw
;
1251 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
1252 XRenderColor colfg
, colbg
;
1255 /* Fallback on color display for attributes not supported by the font */
1256 if (base
.mode
& ATTR_ITALIC
&& base
.mode
& ATTR_BOLD
) {
1257 if (dc
.ibfont
.badslant
|| dc
.ibfont
.badweight
)
1258 base
.fg
= defaultattr
;
1259 } else if ((base
.mode
& ATTR_ITALIC
&& dc
.ifont
.badslant
) ||
1260 (base
.mode
& ATTR_BOLD
&& dc
.bfont
.badweight
)) {
1261 base
.fg
= defaultattr
;
1264 if (IS_TRUECOL(base
.fg
)) {
1265 colfg
.alpha
= 0xffff;
1266 colfg
.red
= TRUERED(base
.fg
);
1267 colfg
.green
= TRUEGREEN(base
.fg
);
1268 colfg
.blue
= TRUEBLUE(base
.fg
);
1269 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
1272 fg
= &dc
.col
[base
.fg
];
1275 if (IS_TRUECOL(base
.bg
)) {
1276 colbg
.alpha
= 0xffff;
1277 colbg
.green
= TRUEGREEN(base
.bg
);
1278 colbg
.red
= TRUERED(base
.bg
);
1279 colbg
.blue
= TRUEBLUE(base
.bg
);
1280 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
1283 bg
= &dc
.col
[base
.bg
];
1286 /* Change basic system colors [0-7] to bright system colors [8-15] */
1287 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
1288 fg
= &dc
.col
[base
.fg
+ 8];
1290 if (IS_SET(MODE_REVERSE
)) {
1291 if (fg
== &dc
.col
[defaultfg
]) {
1292 fg
= &dc
.col
[defaultbg
];
1294 colfg
.red
= ~fg
->color
.red
;
1295 colfg
.green
= ~fg
->color
.green
;
1296 colfg
.blue
= ~fg
->color
.blue
;
1297 colfg
.alpha
= fg
->color
.alpha
;
1298 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
1303 if (bg
== &dc
.col
[defaultbg
]) {
1304 bg
= &dc
.col
[defaultfg
];
1306 colbg
.red
= ~bg
->color
.red
;
1307 colbg
.green
= ~bg
->color
.green
;
1308 colbg
.blue
= ~bg
->color
.blue
;
1309 colbg
.alpha
= bg
->color
.alpha
;
1310 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
1316 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
1317 colfg
.red
= fg
->color
.red
/ 2;
1318 colfg
.green
= fg
->color
.green
/ 2;
1319 colfg
.blue
= fg
->color
.blue
/ 2;
1320 colfg
.alpha
= fg
->color
.alpha
;
1321 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
1325 if (base
.mode
& ATTR_REVERSE
) {
1331 if (base
.mode
& ATTR_BLINK
&& win
.mode
& MODE_BLINK
)
1334 if (base
.mode
& ATTR_INVISIBLE
)
1337 /* Intelligent cleaning up of the borders. */
1339 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
1341 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: 0));
1343 if (winx
+ width
>= borderpx
+ win
.tw
) {
1344 xclear(winx
+ width
, (y
== 0)? 0 : winy
, win
.w
,
1345 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: (winy
+ win
.ch
)));
1348 xclear(winx
, 0, winx
+ width
, borderpx
);
1349 if (winy
+ win
.ch
>= borderpx
+ win
.th
)
1350 xclear(winx
, winy
+ win
.ch
, winx
+ width
, win
.h
);
1352 /* Clean up the region we want to draw to. */
1353 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, win
.ch
);
1355 /* Set the clip region because Xft is sometimes dirty. */
1360 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
1362 /* Render the glyphs. */
1363 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
1365 /* Render underline and strikethrough. */
1366 if (base
.mode
& ATTR_UNDERLINE
) {
1367 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
1371 if (base
.mode
& ATTR_STRUCK
) {
1372 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
1376 /* Reset clip to none. */
1377 XftDrawSetClip(xw
.draw
, 0);
1381 xdrawglyph(Glyph g
, int x
, int y
)
1384 XftGlyphFontSpec spec
;
1386 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
1387 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
1391 xdrawcursor(int cx
, int cy
, Glyph g
, int ox
, int oy
, Glyph og
)
1395 /* remove the old cursor */
1396 if (selected(ox
, oy
))
1397 og
.mode
^= ATTR_REVERSE
;
1398 xdrawglyph(og
, ox
, oy
);
1401 * Select the right color for the right mode.
1403 g
.mode
&= ATTR_BOLD
|ATTR_ITALIC
|ATTR_UNDERLINE
|ATTR_STRUCK
|ATTR_WIDE
;
1407 if (IS_SET(MODE_REVERSE
)) {
1408 g
.mode
|= ATTR_REVERSE
;
1410 if (selected(cx
, cy
)) {
1411 drawcol
= dc
.col
[defaultcs
];
1414 drawcol
= dc
.col
[defaultrcs
];
1418 if (selected(cx
, cy
)) {
1419 drawcol
= dc
.col
[defaultrcs
];
1423 drawcol
= dc
.col
[defaultcs
];
1427 if (IS_SET(MODE_HIDE
))
1430 /* draw the new one */
1431 if (IS_SET(MODE_FOCUSED
)) {
1432 switch (win
.cursor
) {
1433 case 7: /* st extension: snowman */
1434 utf8decode("☃", &g
.u
, UTF_SIZ
);
1435 case 0: /* Blinking Block */
1436 case 1: /* Blinking Block (Default) */
1437 case 2: /* Steady Block */
1438 xdrawglyph(g
, cx
, cy
);
1440 case 3: /* Blinking Underline */
1441 case 4: /* Steady Underline */
1442 XftDrawRect(xw
.draw
, &drawcol
,
1443 borderpx
+ cx
* win
.cw
,
1444 borderpx
+ (cy
+ 1) * win
.ch
- \
1446 win
.cw
, cursorthickness
);
1448 case 5: /* Blinking bar */
1449 case 6: /* Steady bar */
1450 XftDrawRect(xw
.draw
, &drawcol
,
1451 borderpx
+ cx
* win
.cw
,
1452 borderpx
+ cy
* win
.ch
,
1453 cursorthickness
, win
.ch
);
1457 XftDrawRect(xw
.draw
, &drawcol
,
1458 borderpx
+ cx
* win
.cw
,
1459 borderpx
+ cy
* win
.ch
,
1461 XftDrawRect(xw
.draw
, &drawcol
,
1462 borderpx
+ cx
* win
.cw
,
1463 borderpx
+ cy
* win
.ch
,
1465 XftDrawRect(xw
.draw
, &drawcol
,
1466 borderpx
+ (cx
+ 1) * win
.cw
- 1,
1467 borderpx
+ cy
* win
.ch
,
1469 XftDrawRect(xw
.draw
, &drawcol
,
1470 borderpx
+ cx
* win
.cw
,
1471 borderpx
+ (cy
+ 1) * win
.ch
- 1,
1479 char buf
[sizeof(long) * 8 + 1];
1481 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1482 setenv("WINDOWID", buf
, 1);
1491 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1493 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
1494 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
1501 return IS_SET(MODE_VISIBLE
);
1505 xdrawline(Line line
, int x1
, int y1
, int x2
)
1507 int i
, x
, ox
, numspecs
;
1509 XftGlyphFontSpec
*specs
= xw
.specbuf
;
1511 numspecs
= xmakeglyphfontspecs(specs
, &line
[x1
], x2
- x1
, x1
, y1
);
1513 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
1515 if (new.mode
== ATTR_WDUMMY
)
1517 if (selected(x
, y1
))
1518 new.mode
^= ATTR_REVERSE
;
1519 if (i
> 0 && ATTRCMP(base
, new)) {
1520 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1532 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1538 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, win
.w
,
1540 XSetForeground(xw
.dpy
, dc
.gc
,
1541 dc
.col
[IS_SET(MODE_REVERSE
)?
1542 defaultfg
: defaultbg
].pixel
);
1552 visibility(XEvent
*ev
)
1554 XVisibilityEvent
*e
= &ev
->xvisibility
;
1556 MODBIT(win
.mode
, e
->state
!= VisibilityFullyObscured
, MODE_VISIBLE
);
1562 win
.mode
&= ~MODE_VISIBLE
;
1566 xsetpointermotion(int set
)
1568 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
1569 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
1573 xsetmode(int set
, unsigned int flags
)
1575 int mode
= win
.mode
;
1576 MODBIT(win
.mode
, set
, flags
);
1577 if ((win
.mode
& MODE_REVERSE
) != (mode
& MODE_REVERSE
))
1582 xsetcursor(int cursor
)
1585 if (!BETWEEN(cursor
, 0, 6))
1587 win
.cursor
= cursor
;
1592 xseturgency(int add
)
1594 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1596 MODBIT(h
->flags
, add
, XUrgencyHint
);
1597 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1604 if (!(IS_SET(MODE_FOCUSED
)))
1607 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
1613 XFocusChangeEvent
*e
= &ev
->xfocus
;
1615 if (e
->mode
== NotifyGrab
)
1618 if (ev
->type
== FocusIn
) {
1619 XSetICFocus(xw
.xic
);
1620 win
.mode
|= MODE_FOCUSED
;
1622 if (IS_SET(MODE_FOCUS
))
1623 ttywrite("\033[I", 3, 0);
1625 XUnsetICFocus(xw
.xic
);
1626 win
.mode
&= ~MODE_FOCUSED
;
1627 if (IS_SET(MODE_FOCUS
))
1628 ttywrite("\033[O", 3, 0);
1633 match(uint mask
, uint state
)
1635 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
1639 kmap(KeySym k
, uint state
)
1644 /* Check for mapped keys out of X11 function keys. */
1645 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
1646 if (mappedkeys
[i
] == k
)
1649 if (i
== LEN(mappedkeys
)) {
1650 if ((k
& 0xFFFF) < 0xFD00)
1654 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
1658 if (!match(kp
->mask
, state
))
1661 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
1663 if (IS_SET(MODE_NUMLOCK
) && kp
->appkey
== 2)
1666 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
1678 XKeyEvent
*e
= &ev
->xkey
;
1680 char buf
[32], *customkey
;
1686 if (IS_SET(MODE_KBDLOCK
))
1689 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
1691 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
1692 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
1693 bp
->func(&(bp
->arg
));
1698 /* 2. custom keys from config.h */
1699 if ((customkey
= kmap(ksym
, e
->state
))) {
1700 ttywrite(customkey
, strlen(customkey
), 1);
1704 /* 3. composed string from input method */
1707 if (len
== 1 && e
->state
& Mod1Mask
) {
1708 if (IS_SET(MODE_8BIT
)) {
1711 len
= utf8encode(c
, buf
);
1719 ttywrite(buf
, len
, 1);
1728 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1730 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1731 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1732 win
.mode
|= MODE_FOCUSED
;
1734 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1735 win
.mode
&= ~MODE_FOCUSED
;
1737 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
1746 if (e
->xconfigure
.width
== win
.w
&& e
->xconfigure
.height
== win
.h
)
1749 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
1756 int w
= win
.w
, h
= win
.h
;
1758 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
1760 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
1763 /* Waiting for window mapping */
1765 XNextEvent(xw
.dpy
, &ev
);
1767 * This XFilterEvent call is required because of XOpenIM. It
1768 * does filter out the key event and some client message for
1769 * the input method too.
1771 if (XFilterEvent(&ev
, None
))
1773 if (ev
.type
== ConfigureNotify
) {
1774 w
= ev
.xconfigure
.width
;
1775 h
= ev
.xconfigure
.height
;
1777 } while (ev
.type
!= MapNotify
);
1779 ttyfd
= ttynew(opt_line
, shell
, opt_io
, opt_cmd
);
1782 clock_gettime(CLOCK_MONOTONIC
, &last
);
1785 for (xev
= actionfps
;;) {
1787 FD_SET(ttyfd
, &rfd
);
1790 if (pselect(MAX(xfd
, ttyfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
1793 die("select failed: %s\n", strerror(errno
));
1795 if (FD_ISSET(ttyfd
, &rfd
)) {
1798 blinkset
= tattrset(ATTR_BLINK
);
1800 MODBIT(win
.mode
, 0, MODE_BLINK
);
1804 if (FD_ISSET(xfd
, &rfd
))
1807 clock_gettime(CLOCK_MONOTONIC
, &now
);
1808 drawtimeout
.tv_sec
= 0;
1809 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
1813 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
1814 tsetdirtattr(ATTR_BLINK
);
1815 win
.mode
^= MODE_BLINK
;
1819 deltatime
= TIMEDIFF(now
, last
);
1820 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
1826 while (XPending(xw
.dpy
)) {
1827 XNextEvent(xw
.dpy
, &ev
);
1828 if (XFilterEvent(&ev
, None
))
1830 if (handler
[ev
.type
])
1831 (handler
[ev
.type
])(&ev
);
1837 if (xev
&& !FD_ISSET(xfd
, &rfd
))
1839 if (!FD_ISSET(ttyfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
1841 if (TIMEDIFF(now
, lastblink
) \
1843 drawtimeout
.tv_nsec
= 1000;
1845 drawtimeout
.tv_nsec
= (1E6
* \
1850 drawtimeout
.tv_sec
= \
1851 drawtimeout
.tv_nsec
/ 1E9
;
1852 drawtimeout
.tv_nsec
%= (long)1E9
;
1864 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
1865 " [-n name] [-o file]\n"
1866 " [-T title] [-t title] [-w windowid]"
1867 " [[-e] command [args ...]]\n"
1868 " %s [-aiv] [-c class] [-f font] [-g geometry]"
1869 " [-n name] [-o file]\n"
1870 " [-T title] [-t title] [-w windowid] -l line"
1871 " [stty_args ...]\n", argv0
, argv0
);
1875 main(int argc
, char *argv
[])
1879 win
.cursor
= cursorshape
;
1886 opt_class
= EARGF(usage());
1893 opt_font
= EARGF(usage());
1896 xw
.gm
= XParseGeometry(EARGF(usage()),
1897 &xw
.l
, &xw
.t
, &cols
, &rows
);
1903 opt_io
= EARGF(usage());
1906 opt_line
= EARGF(usage());
1909 opt_name
= EARGF(usage());
1913 opt_title
= EARGF(usage());
1916 opt_embed
= EARGF(usage());
1919 die("%s " VERSION
" (c) 2010-2016 st engineers\n", argv0
);
1927 /* eat all remaining arguments */
1929 if (!opt_title
&& !opt_line
)
1930 opt_title
= basename(xstrdup(argv
[0]));
1932 setlocale(LC_CTYPE
, "");
1933 XSetLocaleModifiers("");
1934 cols
= MAX(cols
, 1);
1935 rows
= MAX(rows
, 1);