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
;
231 clipcopy(const Arg
*dummy
)
235 if (xsel
.clipboard
!= NULL
)
236 free(xsel
.clipboard
);
238 if (xsel
.primary
!= NULL
) {
239 xsel
.clipboard
= xstrdup(xsel
.primary
);
240 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
241 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
246 clippaste(const Arg
*dummy
)
250 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
251 XConvertSelection(xw
.dpy
, clipboard
, xsel
.xtarget
, clipboard
,
252 xw
.win
, CurrentTime
);
256 selpaste(const Arg
*dummy
)
258 XConvertSelection(xw
.dpy
, XA_PRIMARY
, xsel
.xtarget
, XA_PRIMARY
,
259 xw
.win
, CurrentTime
);
263 numlock(const Arg
*dummy
)
265 win
.mode
^= MODE_NUMLOCK
;
273 larg
.f
= usedfontsize
+ arg
->f
;
278 zoomabs(const Arg
*arg
)
281 xloadfonts(usedfont
, arg
->f
);
288 zoomreset(const Arg
*arg
)
292 if (defaultfontsize
> 0) {
293 larg
.f
= defaultfontsize
;
302 LIMIT(x
, 0, win
.tw
- 1);
310 LIMIT(y
, 0, win
.th
- 1);
315 mousesel(XEvent
*e
, int done
)
317 int type
, seltype
= SEL_REGULAR
;
318 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forceselmod
);
320 for (type
= 1; type
< LEN(selmasks
); ++type
) {
321 if (match(selmasks
[type
], state
)) {
326 selextend(x2col(e
->xbutton
.x
), y2row(e
->xbutton
.y
), seltype
, done
);
328 setsel(getsel(), e
->xbutton
.time
);
332 mousereport(XEvent
*e
)
334 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
335 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
341 if (e
->xbutton
.type
== MotionNotify
) {
342 if (x
== ox
&& y
== oy
)
344 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
346 /* MOUSE_MOTION: no reporting if no button is pressed */
347 if (IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
350 button
= oldbutton
+ 32;
354 if (!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
361 if (e
->xbutton
.type
== ButtonPress
) {
365 } else if (e
->xbutton
.type
== ButtonRelease
) {
367 /* MODE_MOUSEX10: no button release reporting */
368 if (IS_SET(MODE_MOUSEX10
))
370 if (button
== 64 || button
== 65)
375 if (!IS_SET(MODE_MOUSEX10
)) {
376 button
+= ((state
& ShiftMask
) ? 4 : 0)
377 + ((state
& Mod4Mask
) ? 8 : 0)
378 + ((state
& ControlMask
) ? 16 : 0);
381 if (IS_SET(MODE_MOUSESGR
)) {
382 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
384 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
385 } else if (x
< 223 && y
< 223) {
386 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
387 32+button
, 32+x
+1, 32+y
+1);
392 ttywrite(buf
, len
, 0);
402 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
407 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
408 if (e
->xbutton
.button
== ms
->b
409 && match(ms
->mask
, e
->xbutton
.state
)) {
410 ttywrite(ms
->s
, strlen(ms
->s
), 1);
415 if (e
->xbutton
.button
== Button1
) {
417 * If the user clicks below predefined timeouts specific
418 * snapping behaviour is exposed.
420 clock_gettime(CLOCK_MONOTONIC
, &now
);
421 if (TIMEDIFF(now
, xsel
.tclick2
) <= tripleclicktimeout
) {
423 } else if (TIMEDIFF(now
, xsel
.tclick1
) <= doubleclicktimeout
) {
428 xsel
.tclick2
= xsel
.tclick1
;
431 selstart(x2col(e
->xbutton
.x
), y2row(e
->xbutton
.y
), snap
);
436 propnotify(XEvent
*e
)
438 XPropertyEvent
*xpev
;
439 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
441 xpev
= &e
->xproperty
;
442 if (xpev
->state
== PropertyNewValue
&&
443 (xpev
->atom
== XA_PRIMARY
||
444 xpev
->atom
== clipboard
)) {
452 ulong nitems
, ofs
, rem
;
454 uchar
*data
, *last
, *repl
;
455 Atom type
, incratom
, property
;
457 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
460 if (e
->type
== SelectionNotify
) {
461 property
= e
->xselection
.property
;
462 } else if(e
->type
== PropertyNotify
) {
463 property
= e
->xproperty
.atom
;
467 if (property
== None
)
471 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
472 BUFSIZ
/4, False
, AnyPropertyType
,
473 &type
, &format
, &nitems
, &rem
,
475 fprintf(stderr
, "Clipboard allocation failed\n");
479 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
481 * If there is some PropertyNotify with no data, then
482 * this is the signal of the selection owner that all
483 * data has been transferred. We won't need to receive
484 * PropertyNotify events anymore.
486 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
487 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
491 if (type
== incratom
) {
493 * Activate the PropertyNotify events so we receive
494 * when the selection owner does send us the next
497 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
498 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
502 * Deleting the property is the transfer start signal.
504 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
510 * Line endings are inconsistent in the terminal and GUI world
511 * copy and pasting. When receiving some selection data,
512 * replace all '\n' with '\r'.
513 * FIXME: Fix the computer world.
516 last
= data
+ nitems
* format
/ 8;
517 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
521 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
522 ttywrite("\033[200~", 6, 0);
523 ttywrite((char *)data
, nitems
* format
/ 8, 1);
524 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
525 ttywrite("\033[201~", 6, 0);
527 /* number of 32-bit chunks returned */
528 ofs
+= nitems
* format
/ 32;
532 * Deleting the property again tells the selection owner to send the
533 * next data chunk in the property.
535 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
551 selrequest(XEvent
*e
)
553 XSelectionRequestEvent
*xsre
;
555 Atom xa_targets
, string
, clipboard
;
558 xsre
= (XSelectionRequestEvent
*) e
;
559 xev
.type
= SelectionNotify
;
560 xev
.requestor
= xsre
->requestor
;
561 xev
.selection
= xsre
->selection
;
562 xev
.target
= xsre
->target
;
563 xev
.time
= xsre
->time
;
564 if (xsre
->property
== None
)
565 xsre
->property
= xsre
->target
;
570 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
571 if (xsre
->target
== xa_targets
) {
572 /* respond with the supported type */
573 string
= xsel
.xtarget
;
574 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
575 XA_ATOM
, 32, PropModeReplace
,
576 (uchar
*) &string
, 1);
577 xev
.property
= xsre
->property
;
578 } else if (xsre
->target
== xsel
.xtarget
|| xsre
->target
== XA_STRING
) {
580 * xith XA_STRING non ascii characters may be incorrect in the
581 * requestor. It is not our problem, use utf8.
583 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
584 if (xsre
->selection
== XA_PRIMARY
) {
585 seltext
= xsel
.primary
;
586 } else if (xsre
->selection
== clipboard
) {
587 seltext
= xsel
.clipboard
;
590 "Unhandled clipboard selection 0x%lx\n",
594 if (seltext
!= NULL
) {
595 XChangeProperty(xsre
->display
, xsre
->requestor
,
596 xsre
->property
, xsre
->target
,
598 (uchar
*)seltext
, strlen(seltext
));
599 xev
.property
= xsre
->property
;
603 /* all done, send a notification to the listener */
604 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
605 fprintf(stderr
, "Error sending SelectionNotify event\n");
609 setsel(char *str
, Time t
)
614 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
615 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
622 setsel(str
, CurrentTime
);
628 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
633 if (e
->xbutton
.button
== Button2
)
635 else if (e
->xbutton
.button
== Button1
)
642 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
651 cresize(int width
, int height
)
660 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
661 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
665 ttyresize(win
.tw
, win
.th
);
669 xresize(int col
, int row
)
671 win
.tw
= MAX(1, col
* win
.cw
);
672 win
.th
= MAX(1, row
* win
.ch
);
674 XFreePixmap(xw
.dpy
, xw
.buf
);
675 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
676 DefaultDepth(xw
.dpy
, xw
.scr
));
677 XftDrawChange(xw
.draw
, xw
.buf
);
678 xclear(0, 0, win
.w
, win
.h
);
680 /* resize to new width */
681 xw
.specbuf
= xrealloc(xw
.specbuf
, col
* sizeof(GlyphFontSpec
));
687 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
691 xloadcolor(int i
, const char *name
, Color
*ncolor
)
693 XRenderColor color
= { .alpha
= 0xffff };
696 if (BETWEEN(i
, 16, 255)) { /* 256 color */
697 if (i
< 6*6*6+16) { /* same colors as xterm */
698 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
699 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
700 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
701 } else { /* greyscale */
702 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
703 color
.green
= color
.blue
= color
.red
;
705 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
706 xw
.cmap
, &color
, ncolor
);
711 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
721 dc
.collen
= MAX(LEN(colorname
), 256);
722 dc
.col
= xmalloc(dc
.collen
* sizeof(Color
));
725 for (cp
= dc
.col
; cp
< &dc
.col
[dc
.collen
]; ++cp
)
726 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
729 for (i
= 0; i
< dc
.collen
; i
++)
730 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
732 die("Could not allocate color '%s'\n", colorname
[i
]);
734 die("Could not allocate color %d\n", i
);
740 xsetcolorname(int x
, const char *name
)
744 if (!BETWEEN(x
, 0, dc
.collen
))
748 if (!xloadcolor(x
, name
, &ncolor
))
751 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
758 * Absolute coordinates.
761 xclear(int x1
, int y1
, int x2
, int y2
)
764 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
765 x1
, y1
, x2
-x1
, y2
-y1
);
771 XClassHint
class = {opt_name
? opt_name
: termname
,
772 opt_class
? opt_class
: termname
};
773 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
774 XSizeHints
*sizeh
= NULL
;
776 sizeh
= XAllocSizeHints();
778 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
779 sizeh
->height
= win
.h
;
780 sizeh
->width
= win
.w
;
781 sizeh
->height_inc
= win
.ch
;
782 sizeh
->width_inc
= win
.cw
;
783 sizeh
->base_height
= 2 * borderpx
;
784 sizeh
->base_width
= 2 * borderpx
;
786 sizeh
->flags
|= PMaxSize
| PMinSize
;
787 sizeh
->min_width
= sizeh
->max_width
= win
.w
;
788 sizeh
->min_height
= sizeh
->max_height
= win
.h
;
790 if (xw
.gm
& (XValue
|YValue
)) {
791 sizeh
->flags
|= USPosition
| PWinGravity
;
794 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
797 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
803 xgeommasktogravity(int mask
)
805 switch (mask
& (XNegative
|YNegative
)) {
807 return NorthWestGravity
;
809 return NorthEastGravity
;
811 return SouthWestGravity
;
814 return SouthEastGravity
;
818 xloadfont(Font
*f
, FcPattern
*pattern
)
820 FcPattern
*configured
;
824 int wantattr
, haveattr
;
827 * Manually configure instead of calling XftMatchFont
828 * so that we can use the configured pattern for
829 * "missing glyph" lookups.
831 configured
= FcPatternDuplicate(pattern
);
835 FcConfigSubstitute(NULL
, configured
, FcMatchPattern
);
836 XftDefaultSubstitute(xw
.dpy
, xw
.scr
, configured
);
838 match
= FcFontMatch(NULL
, configured
, &result
);
840 FcPatternDestroy(configured
);
844 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
845 FcPatternDestroy(configured
);
846 FcPatternDestroy(match
);
850 if ((XftPatternGetInteger(pattern
, "slant", 0, &wantattr
) ==
853 * Check if xft was unable to find a font with the appropriate
854 * slant but gave us one anyway. Try to mitigate.
856 if ((XftPatternGetInteger(f
->match
->pattern
, "slant", 0,
857 &haveattr
) != XftResultMatch
) || haveattr
< wantattr
) {
859 fputs("st: font slant does not match\n", stderr
);
863 if ((XftPatternGetInteger(pattern
, "weight", 0, &wantattr
) ==
865 if ((XftPatternGetInteger(f
->match
->pattern
, "weight", 0,
866 &haveattr
) != XftResultMatch
) || haveattr
!= wantattr
) {
868 fputs("st: font weight does not match\n", stderr
);
872 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
873 (const FcChar8
*) ascii_printable
,
874 strlen(ascii_printable
), &extents
);
877 f
->pattern
= configured
;
879 f
->ascent
= f
->match
->ascent
;
880 f
->descent
= f
->match
->descent
;
882 f
->rbearing
= f
->match
->max_advance_width
;
884 f
->height
= f
->ascent
+ f
->descent
;
885 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
891 xloadfonts(char *fontstr
, double fontsize
)
897 if (fontstr
[0] == '-') {
898 pattern
= XftXlfdParse(fontstr
, False
, False
);
900 pattern
= FcNameParse((FcChar8
*)fontstr
);
904 die("st: can't open font %s\n", fontstr
);
907 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
908 FcPatternDel(pattern
, FC_SIZE
);
909 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
910 usedfontsize
= fontsize
;
912 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
914 usedfontsize
= fontval
;
915 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
920 * Default font size is 12, if none given. This is to
921 * have a known usedfontsize value.
923 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
926 defaultfontsize
= usedfontsize
;
929 if (xloadfont(&dc
.font
, pattern
))
930 die("st: can't open font %s\n", fontstr
);
932 if (usedfontsize
< 0) {
933 FcPatternGetDouble(dc
.font
.match
->pattern
,
934 FC_PIXEL_SIZE
, 0, &fontval
);
935 usedfontsize
= fontval
;
937 defaultfontsize
= fontval
;
940 /* Setting character width and height. */
941 win
.cw
= ceilf(dc
.font
.width
* cwscale
);
942 win
.ch
= ceilf(dc
.font
.height
* chscale
);
944 FcPatternDel(pattern
, FC_SLANT
);
945 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
946 if (xloadfont(&dc
.ifont
, pattern
))
947 die("st: can't open font %s\n", fontstr
);
949 FcPatternDel(pattern
, FC_WEIGHT
);
950 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
951 if (xloadfont(&dc
.ibfont
, pattern
))
952 die("st: can't open font %s\n", fontstr
);
954 FcPatternDel(pattern
, FC_SLANT
);
955 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
956 if (xloadfont(&dc
.bfont
, pattern
))
957 die("st: can't open font %s\n", fontstr
);
959 FcPatternDestroy(pattern
);
965 XftFontClose(xw
.dpy
, f
->match
);
966 FcPatternDestroy(f
->pattern
);
968 FcFontSetDestroy(f
->set
);
974 /* Free the loaded fonts in the font cache. */
976 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
978 xunloadfont(&dc
.font
);
979 xunloadfont(&dc
.bfont
);
980 xunloadfont(&dc
.ifont
);
981 xunloadfont(&dc
.ibfont
);
985 xinit(int cols
, int rows
)
990 pid_t thispid
= getpid();
991 XColor xmousefg
, xmousebg
;
993 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
994 die("Can't open display\n");
995 xw
.scr
= XDefaultScreen(xw
.dpy
);
996 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
1000 die("Could not init fontconfig.\n");
1002 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
1003 xloadfonts(usedfont
, 0);
1006 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1009 /* adjust fixed window geometry */
1010 win
.w
= 2 * borderpx
+ cols
* win
.cw
;
1011 win
.h
= 2 * borderpx
+ rows
* win
.ch
;
1012 if (xw
.gm
& XNegative
)
1013 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - win
.w
- 2;
1014 if (xw
.gm
& YNegative
)
1015 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - win
.h
- 2;
1018 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
1019 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
1020 xw
.attrs
.bit_gravity
= NorthWestGravity
;
1021 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1022 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1023 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1024 xw
.attrs
.colormap
= xw
.cmap
;
1026 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
1027 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
1028 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
1029 win
.w
, win
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1030 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
1031 | CWEventMask
| CWColormap
, &xw
.attrs
);
1033 memset(&gcvalues
, 0, sizeof(gcvalues
));
1034 gcvalues
.graphics_exposures
= False
;
1035 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
1037 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
1038 DefaultDepth(xw
.dpy
, xw
.scr
));
1039 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
1040 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, win
.w
, win
.h
);
1042 /* font spec buffer */
1043 xw
.specbuf
= xmalloc(cols
* sizeof(GlyphFontSpec
));
1045 /* Xft rendering context */
1046 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
1049 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1050 XSetLocaleModifiers("@im=local");
1051 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1052 XSetLocaleModifiers("@im=");
1053 if ((xw
.xim
= XOpenIM(xw
.dpy
,
1054 NULL
, NULL
, NULL
)) == NULL
) {
1055 die("XOpenIM failed. Could not open input"
1060 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1061 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1062 XNFocusWindow
, xw
.win
, NULL
);
1064 die("XCreateIC failed. Could not obtain input method.\n");
1066 /* white cursor, black outline */
1067 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
1068 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1070 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
1071 xmousefg
.red
= 0xffff;
1072 xmousefg
.green
= 0xffff;
1073 xmousefg
.blue
= 0xffff;
1076 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
1077 xmousebg
.red
= 0x0000;
1078 xmousebg
.green
= 0x0000;
1079 xmousebg
.blue
= 0x0000;
1082 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
1084 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1085 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
1086 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
1087 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
1089 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
1090 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
1091 PropModeReplace
, (uchar
*)&thispid
, 1);
1093 win
.mode
= MODE_NUMLOCK
;
1095 XMapWindow(xw
.dpy
, xw
.win
);
1097 XSync(xw
.dpy
, False
);
1099 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick1
);
1100 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick2
);
1101 xsel
.primary
= NULL
;
1102 xsel
.clipboard
= NULL
;
1103 xsel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
1104 if (xsel
.xtarget
== None
)
1105 xsel
.xtarget
= XA_STRING
;
1109 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
1111 float winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
, xp
, yp
;
1112 ushort mode
, prevmode
= USHRT_MAX
;
1113 Font
*font
= &dc
.font
;
1114 int frcflags
= FRC_NORMAL
;
1115 float runewidth
= win
.cw
;
1119 FcPattern
*fcpattern
, *fontpattern
;
1120 FcFontSet
*fcsets
[] = { NULL
};
1121 FcCharSet
*fccharset
;
1122 int i
, f
, numspecs
= 0;
1124 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
1125 /* Fetch rune and mode for current glyph. */
1127 mode
= glyphs
[i
].mode
;
1129 /* Skip dummy wide-character spacing. */
1130 if (mode
== ATTR_WDUMMY
)
1133 /* Determine font for glyph if different from previous glyph. */
1134 if (prevmode
!= mode
) {
1137 frcflags
= FRC_NORMAL
;
1138 runewidth
= win
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
1139 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
1141 frcflags
= FRC_ITALICBOLD
;
1142 } else if (mode
& ATTR_ITALIC
) {
1144 frcflags
= FRC_ITALIC
;
1145 } else if (mode
& ATTR_BOLD
) {
1147 frcflags
= FRC_BOLD
;
1149 yp
= winy
+ font
->ascent
;
1152 /* Lookup character index with default font. */
1153 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
1155 specs
[numspecs
].font
= font
->match
;
1156 specs
[numspecs
].glyph
= glyphidx
;
1157 specs
[numspecs
].x
= (short)xp
;
1158 specs
[numspecs
].y
= (short)yp
;
1164 /* Fallback on font cache, search the font cache for match. */
1165 for (f
= 0; f
< frclen
; f
++) {
1166 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
1167 /* Everything correct. */
1168 if (glyphidx
&& frc
[f
].flags
== frcflags
)
1170 /* We got a default font for a not found glyph. */
1171 if (!glyphidx
&& frc
[f
].flags
== frcflags
1172 && frc
[f
].unicodep
== rune
) {
1177 /* Nothing was found. Use fontconfig to find matching font. */
1180 font
->set
= FcFontSort(0, font
->pattern
,
1182 fcsets
[0] = font
->set
;
1185 * Nothing was found in the cache. Now use
1186 * some dozen of Fontconfig calls to get the
1187 * font for one single character.
1189 * Xft and fontconfig are design failures.
1191 fcpattern
= FcPatternDuplicate(font
->pattern
);
1192 fccharset
= FcCharSetCreate();
1194 FcCharSetAddChar(fccharset
, rune
);
1195 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
1197 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
1199 FcConfigSubstitute(0, fcpattern
,
1201 FcDefaultSubstitute(fcpattern
);
1203 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
1207 * Overwrite or create the new cache entry.
1209 if (frclen
>= LEN(frc
)) {
1210 frclen
= LEN(frc
) - 1;
1211 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
1212 frc
[frclen
].unicodep
= 0;
1215 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
1217 if (!frc
[frclen
].font
)
1218 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1220 frc
[frclen
].flags
= frcflags
;
1221 frc
[frclen
].unicodep
= rune
;
1223 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
1228 FcPatternDestroy(fcpattern
);
1229 FcCharSetDestroy(fccharset
);
1232 specs
[numspecs
].font
= frc
[f
].font
;
1233 specs
[numspecs
].glyph
= glyphidx
;
1234 specs
[numspecs
].x
= (short)xp
;
1235 specs
[numspecs
].y
= (short)yp
;
1244 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
1246 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
1247 int winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
,
1248 width
= charlen
* win
.cw
;
1249 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
1250 XRenderColor colfg
, colbg
;
1253 /* Fallback on color display for attributes not supported by the font */
1254 if (base
.mode
& ATTR_ITALIC
&& base
.mode
& ATTR_BOLD
) {
1255 if (dc
.ibfont
.badslant
|| dc
.ibfont
.badweight
)
1256 base
.fg
= defaultattr
;
1257 } else if ((base
.mode
& ATTR_ITALIC
&& dc
.ifont
.badslant
) ||
1258 (base
.mode
& ATTR_BOLD
&& dc
.bfont
.badweight
)) {
1259 base
.fg
= defaultattr
;
1262 if (IS_TRUECOL(base
.fg
)) {
1263 colfg
.alpha
= 0xffff;
1264 colfg
.red
= TRUERED(base
.fg
);
1265 colfg
.green
= TRUEGREEN(base
.fg
);
1266 colfg
.blue
= TRUEBLUE(base
.fg
);
1267 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
1270 fg
= &dc
.col
[base
.fg
];
1273 if (IS_TRUECOL(base
.bg
)) {
1274 colbg
.alpha
= 0xffff;
1275 colbg
.green
= TRUEGREEN(base
.bg
);
1276 colbg
.red
= TRUERED(base
.bg
);
1277 colbg
.blue
= TRUEBLUE(base
.bg
);
1278 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
1281 bg
= &dc
.col
[base
.bg
];
1284 /* Change basic system colors [0-7] to bright system colors [8-15] */
1285 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
1286 fg
= &dc
.col
[base
.fg
+ 8];
1288 if (IS_SET(MODE_REVERSE
)) {
1289 if (fg
== &dc
.col
[defaultfg
]) {
1290 fg
= &dc
.col
[defaultbg
];
1292 colfg
.red
= ~fg
->color
.red
;
1293 colfg
.green
= ~fg
->color
.green
;
1294 colfg
.blue
= ~fg
->color
.blue
;
1295 colfg
.alpha
= fg
->color
.alpha
;
1296 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
1301 if (bg
== &dc
.col
[defaultbg
]) {
1302 bg
= &dc
.col
[defaultfg
];
1304 colbg
.red
= ~bg
->color
.red
;
1305 colbg
.green
= ~bg
->color
.green
;
1306 colbg
.blue
= ~bg
->color
.blue
;
1307 colbg
.alpha
= bg
->color
.alpha
;
1308 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
1314 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
1315 colfg
.red
= fg
->color
.red
/ 2;
1316 colfg
.green
= fg
->color
.green
/ 2;
1317 colfg
.blue
= fg
->color
.blue
/ 2;
1318 colfg
.alpha
= fg
->color
.alpha
;
1319 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
1323 if (base
.mode
& ATTR_REVERSE
) {
1329 if (base
.mode
& ATTR_BLINK
&& win
.mode
& MODE_BLINK
)
1332 if (base
.mode
& ATTR_INVISIBLE
)
1335 /* Intelligent cleaning up of the borders. */
1337 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
1339 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: 0));
1341 if (winx
+ width
>= borderpx
+ win
.tw
) {
1342 xclear(winx
+ width
, (y
== 0)? 0 : winy
, win
.w
,
1343 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: (winy
+ win
.ch
)));
1346 xclear(winx
, 0, winx
+ width
, borderpx
);
1347 if (winy
+ win
.ch
>= borderpx
+ win
.th
)
1348 xclear(winx
, winy
+ win
.ch
, winx
+ width
, win
.h
);
1350 /* Clean up the region we want to draw to. */
1351 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, win
.ch
);
1353 /* Set the clip region because Xft is sometimes dirty. */
1358 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
1360 /* Render the glyphs. */
1361 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
1363 /* Render underline and strikethrough. */
1364 if (base
.mode
& ATTR_UNDERLINE
) {
1365 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
1369 if (base
.mode
& ATTR_STRUCK
) {
1370 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
1374 /* Reset clip to none. */
1375 XftDrawSetClip(xw
.draw
, 0);
1379 xdrawglyph(Glyph g
, int x
, int y
)
1382 XftGlyphFontSpec spec
;
1384 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
1385 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
1389 xdrawcursor(int cx
, int cy
, Glyph g
, int ox
, int oy
, Glyph og
)
1393 /* remove the old cursor */
1394 if (selected(ox
, oy
))
1395 og
.mode
^= ATTR_REVERSE
;
1396 xdrawglyph(og
, ox
, oy
);
1399 * Select the right color for the right mode.
1401 g
.mode
&= ATTR_BOLD
|ATTR_ITALIC
|ATTR_UNDERLINE
|ATTR_STRUCK
|ATTR_WIDE
;
1405 if (IS_SET(MODE_REVERSE
)) {
1406 g
.mode
|= ATTR_REVERSE
;
1408 if (selected(cx
, cy
)) {
1409 drawcol
= dc
.col
[defaultcs
];
1412 drawcol
= dc
.col
[defaultrcs
];
1416 if (selected(cx
, cy
)) {
1417 drawcol
= dc
.col
[defaultrcs
];
1421 drawcol
= dc
.col
[defaultcs
];
1425 if (IS_SET(MODE_HIDE
))
1428 /* draw the new one */
1429 if (IS_SET(MODE_FOCUSED
)) {
1430 switch (win
.cursor
) {
1431 case 7: /* st extension: snowman */
1432 utf8decode("☃", &g
.u
, UTF_SIZ
);
1433 case 0: /* Blinking Block */
1434 case 1: /* Blinking Block (Default) */
1435 case 2: /* Steady Block */
1436 xdrawglyph(g
, cx
, cy
);
1438 case 3: /* Blinking Underline */
1439 case 4: /* Steady Underline */
1440 XftDrawRect(xw
.draw
, &drawcol
,
1441 borderpx
+ cx
* win
.cw
,
1442 borderpx
+ (cy
+ 1) * win
.ch
- \
1444 win
.cw
, cursorthickness
);
1446 case 5: /* Blinking bar */
1447 case 6: /* Steady bar */
1448 XftDrawRect(xw
.draw
, &drawcol
,
1449 borderpx
+ cx
* win
.cw
,
1450 borderpx
+ cy
* win
.ch
,
1451 cursorthickness
, win
.ch
);
1455 XftDrawRect(xw
.draw
, &drawcol
,
1456 borderpx
+ cx
* win
.cw
,
1457 borderpx
+ cy
* win
.ch
,
1459 XftDrawRect(xw
.draw
, &drawcol
,
1460 borderpx
+ cx
* win
.cw
,
1461 borderpx
+ cy
* win
.ch
,
1463 XftDrawRect(xw
.draw
, &drawcol
,
1464 borderpx
+ (cx
+ 1) * win
.cw
- 1,
1465 borderpx
+ cy
* win
.ch
,
1467 XftDrawRect(xw
.draw
, &drawcol
,
1468 borderpx
+ cx
* win
.cw
,
1469 borderpx
+ (cy
+ 1) * win
.ch
- 1,
1477 char buf
[sizeof(long) * 8 + 1];
1479 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1480 setenv("WINDOWID", buf
, 1);
1489 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1491 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
1492 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
1499 return IS_SET(MODE_VISIBLE
);
1503 xdrawline(Line line
, int x1
, int y1
, int x2
)
1505 int i
, x
, ox
, numspecs
;
1507 XftGlyphFontSpec
*specs
= xw
.specbuf
;
1509 numspecs
= xmakeglyphfontspecs(specs
, &line
[x1
], x2
- x1
, x1
, y1
);
1511 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
1513 if (new.mode
== ATTR_WDUMMY
)
1515 if (selected(x
, y1
))
1516 new.mode
^= ATTR_REVERSE
;
1517 if (i
> 0 && ATTRCMP(base
, new)) {
1518 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1530 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1536 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, win
.w
,
1538 XSetForeground(xw
.dpy
, dc
.gc
,
1539 dc
.col
[IS_SET(MODE_REVERSE
)?
1540 defaultfg
: defaultbg
].pixel
);
1550 visibility(XEvent
*ev
)
1552 XVisibilityEvent
*e
= &ev
->xvisibility
;
1554 MODBIT(win
.mode
, e
->state
!= VisibilityFullyObscured
, MODE_VISIBLE
);
1560 win
.mode
&= ~MODE_VISIBLE
;
1564 xsetpointermotion(int set
)
1566 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
1567 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
1571 xsetmode(int set
, unsigned int flags
)
1573 int mode
= win
.mode
;
1574 MODBIT(win
.mode
, set
, flags
);
1575 if ((win
.mode
& MODE_REVERSE
) != (mode
& MODE_REVERSE
))
1580 xsetcursor(int cursor
)
1583 if (!BETWEEN(cursor
, 0, 6))
1585 win
.cursor
= cursor
;
1590 xseturgency(int add
)
1592 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1594 MODBIT(h
->flags
, add
, XUrgencyHint
);
1595 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1602 if (!(IS_SET(MODE_FOCUSED
)))
1605 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
1611 XFocusChangeEvent
*e
= &ev
->xfocus
;
1613 if (e
->mode
== NotifyGrab
)
1616 if (ev
->type
== FocusIn
) {
1617 XSetICFocus(xw
.xic
);
1618 win
.mode
|= MODE_FOCUSED
;
1620 if (IS_SET(MODE_FOCUS
))
1621 ttywrite("\033[I", 3, 0);
1623 XUnsetICFocus(xw
.xic
);
1624 win
.mode
&= ~MODE_FOCUSED
;
1625 if (IS_SET(MODE_FOCUS
))
1626 ttywrite("\033[O", 3, 0);
1631 match(uint mask
, uint state
)
1633 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
1637 kmap(KeySym k
, uint state
)
1642 /* Check for mapped keys out of X11 function keys. */
1643 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
1644 if (mappedkeys
[i
] == k
)
1647 if (i
== LEN(mappedkeys
)) {
1648 if ((k
& 0xFFFF) < 0xFD00)
1652 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
1656 if (!match(kp
->mask
, state
))
1659 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
1661 if (IS_SET(MODE_NUMLOCK
) && kp
->appkey
== 2)
1664 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
1676 XKeyEvent
*e
= &ev
->xkey
;
1678 char buf
[32], *customkey
;
1684 if (IS_SET(MODE_KBDLOCK
))
1687 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
1689 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
1690 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
1691 bp
->func(&(bp
->arg
));
1696 /* 2. custom keys from config.h */
1697 if ((customkey
= kmap(ksym
, e
->state
))) {
1698 ttywrite(customkey
, strlen(customkey
), 1);
1702 /* 3. composed string from input method */
1705 if (len
== 1 && e
->state
& Mod1Mask
) {
1706 if (IS_SET(MODE_8BIT
)) {
1709 len
= utf8encode(c
, buf
);
1717 ttywrite(buf
, len
, 1);
1726 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1728 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1729 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1730 win
.mode
|= MODE_FOCUSED
;
1732 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1733 win
.mode
&= ~MODE_FOCUSED
;
1735 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
1736 /* Send SIGHUP to shell */
1745 if (e
->xconfigure
.width
== win
.w
&& e
->xconfigure
.height
== win
.h
)
1748 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
1755 int w
= win
.w
, h
= win
.h
;
1757 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
1758 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
1761 /* Waiting for window mapping */
1763 XNextEvent(xw
.dpy
, &ev
);
1765 * This XFilterEvent call is required because of XOpenIM. It
1766 * does filter out the key event and some client message for
1767 * the input method too.
1769 if (XFilterEvent(&ev
, None
))
1771 if (ev
.type
== ConfigureNotify
) {
1772 w
= ev
.xconfigure
.width
;
1773 h
= ev
.xconfigure
.height
;
1775 } while (ev
.type
!= MapNotify
);
1777 ttynew(opt_line
, opt_io
, opt_cmd
);
1780 clock_gettime(CLOCK_MONOTONIC
, &last
);
1783 for (xev
= actionfps
;;) {
1785 FD_SET(cmdfd
, &rfd
);
1788 if (pselect(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
1791 die("select failed: %s\n", strerror(errno
));
1793 if (FD_ISSET(cmdfd
, &rfd
)) {
1796 blinkset
= tattrset(ATTR_BLINK
);
1798 MODBIT(win
.mode
, 0, MODE_BLINK
);
1802 if (FD_ISSET(xfd
, &rfd
))
1805 clock_gettime(CLOCK_MONOTONIC
, &now
);
1806 drawtimeout
.tv_sec
= 0;
1807 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
1811 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
1812 tsetdirtattr(ATTR_BLINK
);
1813 win
.mode
^= MODE_BLINK
;
1817 deltatime
= TIMEDIFF(now
, last
);
1818 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
1824 while (XPending(xw
.dpy
)) {
1825 XNextEvent(xw
.dpy
, &ev
);
1826 if (XFilterEvent(&ev
, None
))
1828 if (handler
[ev
.type
])
1829 (handler
[ev
.type
])(&ev
);
1835 if (xev
&& !FD_ISSET(xfd
, &rfd
))
1837 if (!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
1839 if (TIMEDIFF(now
, lastblink
) \
1841 drawtimeout
.tv_nsec
= 1000;
1843 drawtimeout
.tv_nsec
= (1E6
* \
1848 drawtimeout
.tv_sec
= \
1849 drawtimeout
.tv_nsec
/ 1E9
;
1850 drawtimeout
.tv_nsec
%= (long)1E9
;
1862 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
1863 " [-n name] [-o file]\n"
1864 " [-T title] [-t title] [-w windowid]"
1865 " [[-e] command [args ...]]\n"
1866 " %s [-aiv] [-c class] [-f font] [-g geometry]"
1867 " [-n name] [-o file]\n"
1868 " [-T title] [-t title] [-w windowid] -l line"
1869 " [stty_args ...]\n", argv0
, argv0
);
1873 main(int argc
, char *argv
[])
1877 win
.cursor
= cursorshape
;
1884 opt_class
= EARGF(usage());
1891 opt_font
= EARGF(usage());
1894 xw
.gm
= XParseGeometry(EARGF(usage()),
1895 &xw
.l
, &xw
.t
, &cols
, &rows
);
1901 opt_io
= EARGF(usage());
1904 opt_line
= EARGF(usage());
1907 opt_name
= EARGF(usage());
1911 opt_title
= EARGF(usage());
1914 opt_embed
= EARGF(usage());
1917 die("%s " VERSION
" (c) 2010-2016 st engineers\n", argv0
);
1925 /* eat all remaining arguments */
1927 if (!opt_title
&& !opt_line
)
1928 opt_title
= basename(xstrdup(argv
[0]));
1930 setlocale(LC_CTYPE
, "");
1931 XSetLocaleModifiers("");
1932 cols
= MAX(cols
, 1);
1933 rows
= MAX(rows
, 1);