1 /* See LICENSE for license details. */
7 #include <sys/select.h>
11 #include <X11/Xatom.h>
13 #include <X11/cursorfont.h>
14 #include <X11/keysym.h>
15 #include <X11/Xft/Xft.h>
16 #include <X11/XKBlib.h>
23 /* types used in config.h */
27 void (*func
)(const Arg
*);
34 void (*func
)(const Arg
*);
43 /* three-valued logic variables: 0 indifferent, 1 on, -1 off */
44 signed char appkey
; /* application keypad */
45 signed char appcursor
; /* application cursor */
49 #define XK_ANY_MOD UINT_MAX
51 #define XK_SWITCH_MOD (1<<13)
53 /* function definitions used in config.h */
54 static void clipcopy(const Arg
*);
55 static void clippaste(const Arg
*);
56 static void numlock(const Arg
*);
57 static void selpaste(const Arg
*);
58 static void zoom(const Arg
*);
59 static void zoomabs(const Arg
*);
60 static void zoomreset(const Arg
*);
61 static void ttysend(const Arg
*);
63 /* config.h for applying patches and the configuration. */
67 #define XEMBED_FOCUS_IN 4
68 #define XEMBED_FOCUS_OUT 5
71 #define IS_SET(flag) ((win.mode & (flag)) != 0)
72 #define TRUERED(x) (((x) & 0xff0000) >> 8)
73 #define TRUEGREEN(x) (((x) & 0xff00))
74 #define TRUEBLUE(x) (((x) & 0xff) << 8)
76 typedef XftDraw
*Draw
;
77 typedef XftColor Color
;
78 typedef XftGlyphFontSpec GlyphFontSpec
;
80 /* Purely graphic info */
82 int tw
, th
; /* tty width and height */
83 int w
, h
; /* window width and height */
84 int ch
; /* char height */
85 int cw
; /* char width */
86 int mode
; /* window state/mode flags */
87 int cursor
; /* cursor style */
95 GlyphFontSpec
*specbuf
; /* font spec buffer used for rendering */
96 Atom xembed
, wmdeletewin
, netwmname
, netwmpid
;
101 XSetWindowAttributes attrs
;
103 int isfixed
; /* is fixed geometry? */
104 int l
, t
; /* left and top offset */
105 int gm
; /* geometry mask */
110 char *primary
, *clipboard
;
111 struct timespec tclick1
;
112 struct timespec tclick2
;
131 /* Drawing Context */
135 Font font
, bfont
, ifont
, ibfont
;
139 static inline ushort
sixd_to_16bit(int);
140 static int xmakeglyphfontspecs(XftGlyphFontSpec
*, const Glyph
*, int, int, int);
141 static void xdrawglyphfontspecs(const XftGlyphFontSpec
*, Glyph
, int, int, int);
142 static void xdrawglyph(Glyph
, int, int);
143 static void xclear(int, int, int, int);
144 static int xgeommasktogravity(int);
145 static void ximopen(Display
*);
146 static void ximinstantiate(Display
*, XPointer
, XPointer
);
147 static void ximdestroy(XIM
, XPointer
, XPointer
);
148 static void xinit(int, int);
149 static void cresize(int, int);
150 static void xresize(int, int);
151 static void xhints(void);
152 static int xloadcolor(int, const char *, Color
*);
153 static int xloadfont(Font
*, FcPattern
*);
154 static void xloadfonts(char *, double);
155 static void xunloadfont(Font
*);
156 static void xunloadfonts(void);
157 static void xsetenv(void);
158 static void xseturgency(int);
159 static int evcol(XEvent
*);
160 static int evrow(XEvent
*);
162 static void expose(XEvent
*);
163 static void visibility(XEvent
*);
164 static void unmap(XEvent
*);
165 static void kpress(XEvent
*);
166 static void cmessage(XEvent
*);
167 static void resize(XEvent
*);
168 static void focus(XEvent
*);
169 static int mouseaction(XEvent
*, uint
);
170 static void brelease(XEvent
*);
171 static void bpress(XEvent
*);
172 static void bmotion(XEvent
*);
173 static void propnotify(XEvent
*);
174 static void selnotify(XEvent
*);
175 static void selclear_(XEvent
*);
176 static void selrequest(XEvent
*);
177 static void setsel(char *, Time
);
178 static void mousesel(XEvent
*, int);
179 static void mousereport(XEvent
*);
180 static char *kmap(KeySym
, uint
);
181 static int match(uint
, uint
);
183 static void run(void);
184 static void usage(void);
186 static void (*handler
[LASTEvent
])(XEvent
*) = {
188 [ClientMessage
] = cmessage
,
189 [ConfigureNotify
] = resize
,
190 [VisibilityNotify
] = visibility
,
191 [UnmapNotify
] = unmap
,
195 [MotionNotify
] = bmotion
,
196 [ButtonPress
] = bpress
,
197 [ButtonRelease
] = brelease
,
199 * Uncomment if you want the selection to disappear when you select something
200 * different in another window.
202 /* [SelectionClear] = selclear_, */
203 [SelectionNotify
] = selnotify
,
205 * PropertyNotify is only turned on when there is some INCR transfer happening
206 * for the selection retrieval.
208 [PropertyNotify
] = propnotify
,
209 [SelectionRequest
] = selrequest
,
215 static XSelection xsel
;
216 static TermWindow win
;
218 /* Font Ring Cache */
232 /* Fontcache is an array now. A new font will be appended to the array. */
233 static Fontcache
*frc
= NULL
;
234 static int frclen
= 0;
235 static int frccap
= 0;
236 static char *usedfont
= NULL
;
237 static double usedfontsize
= 0;
238 static double defaultfontsize
= 0;
240 static char *opt_class
= NULL
;
241 static char **opt_cmd
= NULL
;
242 static char *opt_embed
= NULL
;
243 static char *opt_font
= NULL
;
244 static char *opt_io
= NULL
;
245 static char *opt_line
= NULL
;
246 static char *opt_name
= NULL
;
247 static char *opt_title
= NULL
;
249 static int oldbutton
= 3; /* button event on startup: 3 = release */
252 clipcopy(const Arg
*dummy
)
256 free(xsel
.clipboard
);
257 xsel
.clipboard
= NULL
;
259 if (xsel
.primary
!= NULL
) {
260 xsel
.clipboard
= xstrdup(xsel
.primary
);
261 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
262 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
267 clippaste(const Arg
*dummy
)
271 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
272 XConvertSelection(xw
.dpy
, clipboard
, xsel
.xtarget
, clipboard
,
273 xw
.win
, CurrentTime
);
277 selpaste(const Arg
*dummy
)
279 XConvertSelection(xw
.dpy
, XA_PRIMARY
, xsel
.xtarget
, XA_PRIMARY
,
280 xw
.win
, CurrentTime
);
284 numlock(const Arg
*dummy
)
286 win
.mode
^= MODE_NUMLOCK
;
294 larg
.f
= usedfontsize
+ arg
->f
;
299 zoomabs(const Arg
*arg
)
302 xloadfonts(usedfont
, arg
->f
);
309 zoomreset(const Arg
*arg
)
313 if (defaultfontsize
> 0) {
314 larg
.f
= defaultfontsize
;
320 ttysend(const Arg
*arg
)
322 ttywrite(arg
->s
, strlen(arg
->s
), 1);
328 int x
= e
->xbutton
.x
- borderpx
;
329 LIMIT(x
, 0, win
.tw
- 1);
336 int y
= e
->xbutton
.y
- borderpx
;
337 LIMIT(y
, 0, win
.th
- 1);
342 mousesel(XEvent
*e
, int done
)
344 int type
, seltype
= SEL_REGULAR
;
345 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forcemousemod
);
347 for (type
= 1; type
< LEN(selmasks
); ++type
) {
348 if (match(selmasks
[type
], state
)) {
353 selextend(evcol(e
), evrow(e
), seltype
, done
);
355 setsel(getsel(), e
->xbutton
.time
);
359 mousereport(XEvent
*e
)
361 int len
, x
= evcol(e
), y
= evrow(e
),
362 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
;
367 if (e
->xbutton
.type
== MotionNotify
) {
368 if (x
== ox
&& y
== oy
)
370 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
372 /* MOUSE_MOTION: no reporting if no button is pressed */
373 if (IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
376 button
= oldbutton
+ 32;
380 if (!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
387 if (e
->xbutton
.type
== ButtonPress
) {
391 } else if (e
->xbutton
.type
== ButtonRelease
) {
393 /* MODE_MOUSEX10: no button release reporting */
394 if (IS_SET(MODE_MOUSEX10
))
396 if (button
== 64 || button
== 65)
401 if (!IS_SET(MODE_MOUSEX10
)) {
402 button
+= ((state
& ShiftMask
) ? 4 : 0)
403 + ((state
& Mod4Mask
) ? 8 : 0)
404 + ((state
& ControlMask
) ? 16 : 0);
407 if (IS_SET(MODE_MOUSESGR
)) {
408 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
410 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
411 } else if (x
< 223 && y
< 223) {
412 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
413 32+button
, 32+x
+1, 32+y
+1);
418 ttywrite(buf
, len
, 0);
422 mouseaction(XEvent
*e
, uint release
)
426 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
427 if (ms
->release
== release
&&
428 ms
->button
== e
->xbutton
.button
&&
429 match(ms
->mod
, e
->xbutton
.state
& ~forcemousemod
)) {
430 ms
->func(&(ms
->arg
));
444 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
449 if (mouseaction(e
, 0))
452 if (e
->xbutton
.button
== Button1
) {
454 * If the user clicks below predefined timeouts specific
455 * snapping behaviour is exposed.
457 clock_gettime(CLOCK_MONOTONIC
, &now
);
458 if (TIMEDIFF(now
, xsel
.tclick2
) <= tripleclicktimeout
) {
460 } else if (TIMEDIFF(now
, xsel
.tclick1
) <= doubleclicktimeout
) {
465 xsel
.tclick2
= xsel
.tclick1
;
468 selstart(evcol(e
), evrow(e
), snap
);
473 propnotify(XEvent
*e
)
475 XPropertyEvent
*xpev
;
476 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
478 xpev
= &e
->xproperty
;
479 if (xpev
->state
== PropertyNewValue
&&
480 (xpev
->atom
== XA_PRIMARY
||
481 xpev
->atom
== clipboard
)) {
489 ulong nitems
, ofs
, rem
;
491 uchar
*data
, *last
, *repl
;
492 Atom type
, incratom
, property
= None
;
494 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
497 if (e
->type
== SelectionNotify
)
498 property
= e
->xselection
.property
;
499 else if (e
->type
== PropertyNotify
)
500 property
= e
->xproperty
.atom
;
502 if (property
== None
)
506 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
507 BUFSIZ
/4, False
, AnyPropertyType
,
508 &type
, &format
, &nitems
, &rem
,
510 fprintf(stderr
, "Clipboard allocation failed\n");
514 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
516 * If there is some PropertyNotify with no data, then
517 * this is the signal of the selection owner that all
518 * data has been transferred. We won't need to receive
519 * PropertyNotify events anymore.
521 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
522 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
526 if (type
== incratom
) {
528 * Activate the PropertyNotify events so we receive
529 * when the selection owner does send us the next
532 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
533 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
537 * Deleting the property is the transfer start signal.
539 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
545 * Line endings are inconsistent in the terminal and GUI world
546 * copy and pasting. When receiving some selection data,
547 * replace all '\n' with '\r'.
548 * FIXME: Fix the computer world.
551 last
= data
+ nitems
* format
/ 8;
552 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
556 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
557 ttywrite("\033[200~", 6, 0);
558 ttywrite((char *)data
, nitems
* format
/ 8, 1);
559 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
560 ttywrite("\033[201~", 6, 0);
562 /* number of 32-bit chunks returned */
563 ofs
+= nitems
* format
/ 32;
567 * Deleting the property again tells the selection owner to send the
568 * next data chunk in the property.
570 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
586 selrequest(XEvent
*e
)
588 XSelectionRequestEvent
*xsre
;
590 Atom xa_targets
, string
, clipboard
;
593 xsre
= (XSelectionRequestEvent
*) e
;
594 xev
.type
= SelectionNotify
;
595 xev
.requestor
= xsre
->requestor
;
596 xev
.selection
= xsre
->selection
;
597 xev
.target
= xsre
->target
;
598 xev
.time
= xsre
->time
;
599 if (xsre
->property
== None
)
600 xsre
->property
= xsre
->target
;
605 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
606 if (xsre
->target
== xa_targets
) {
607 /* respond with the supported type */
608 string
= xsel
.xtarget
;
609 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
610 XA_ATOM
, 32, PropModeReplace
,
611 (uchar
*) &string
, 1);
612 xev
.property
= xsre
->property
;
613 } else if (xsre
->target
== xsel
.xtarget
|| xsre
->target
== XA_STRING
) {
615 * xith XA_STRING non ascii characters may be incorrect in the
616 * requestor. It is not our problem, use utf8.
618 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
619 if (xsre
->selection
== XA_PRIMARY
) {
620 seltext
= xsel
.primary
;
621 } else if (xsre
->selection
== clipboard
) {
622 seltext
= xsel
.clipboard
;
625 "Unhandled clipboard selection 0x%lx\n",
629 if (seltext
!= NULL
) {
630 XChangeProperty(xsre
->display
, xsre
->requestor
,
631 xsre
->property
, xsre
->target
,
633 (uchar
*)seltext
, strlen(seltext
));
634 xev
.property
= xsre
->property
;
638 /* all done, send a notification to the listener */
639 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
640 fprintf(stderr
, "Error sending SelectionNotify event\n");
644 setsel(char *str
, Time t
)
652 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
653 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
660 setsel(str
, CurrentTime
);
666 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
671 if (mouseaction(e
, 1))
673 if (e
->xbutton
.button
== Button1
)
680 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
689 cresize(int width
, int height
)
698 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
699 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
705 ttyresize(win
.tw
, win
.th
);
709 xresize(int col
, int row
)
711 win
.tw
= col
* win
.cw
;
712 win
.th
= row
* win
.ch
;
714 XFreePixmap(xw
.dpy
, xw
.buf
);
715 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
716 DefaultDepth(xw
.dpy
, xw
.scr
));
717 XftDrawChange(xw
.draw
, xw
.buf
);
718 xclear(0, 0, win
.w
, win
.h
);
720 /* resize to new width */
721 xw
.specbuf
= xrealloc(xw
.specbuf
, col
* sizeof(GlyphFontSpec
));
727 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
731 xloadcolor(int i
, const char *name
, Color
*ncolor
)
733 XRenderColor color
= { .alpha
= 0xffff };
736 if (BETWEEN(i
, 16, 255)) { /* 256 color */
737 if (i
< 6*6*6+16) { /* same colors as xterm */
738 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
739 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
740 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
741 } else { /* greyscale */
742 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
743 color
.green
= color
.blue
= color
.red
;
745 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
746 xw
.cmap
, &color
, ncolor
);
751 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
762 for (cp
= dc
.col
; cp
< &dc
.col
[dc
.collen
]; ++cp
)
763 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
765 dc
.collen
= MAX(LEN(colorname
), 256);
766 dc
.col
= xmalloc(dc
.collen
* sizeof(Color
));
769 for (i
= 0; i
< dc
.collen
; i
++)
770 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
772 die("could not allocate color '%s'\n", colorname
[i
]);
774 die("could not allocate color %d\n", i
);
780 xsetcolorname(int x
, const char *name
)
784 if (!BETWEEN(x
, 0, dc
.collen
))
787 if (!xloadcolor(x
, name
, &ncolor
))
790 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
797 * Absolute coordinates.
800 xclear(int x1
, int y1
, int x2
, int y2
)
803 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
804 x1
, y1
, x2
-x1
, y2
-y1
);
810 XClassHint
class = {opt_name
? opt_name
: termname
,
811 opt_class
? opt_class
: termname
};
812 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
815 sizeh
= XAllocSizeHints();
817 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
| PMinSize
;
818 sizeh
->height
= win
.h
;
819 sizeh
->width
= win
.w
;
820 sizeh
->height_inc
= win
.ch
;
821 sizeh
->width_inc
= win
.cw
;
822 sizeh
->base_height
= 2 * borderpx
;
823 sizeh
->base_width
= 2 * borderpx
;
824 sizeh
->min_height
= win
.ch
+ 2 * borderpx
;
825 sizeh
->min_width
= win
.cw
+ 2 * borderpx
;
827 sizeh
->flags
|= PMaxSize
;
828 sizeh
->min_width
= sizeh
->max_width
= win
.w
;
829 sizeh
->min_height
= sizeh
->max_height
= win
.h
;
831 if (xw
.gm
& (XValue
|YValue
)) {
832 sizeh
->flags
|= USPosition
| PWinGravity
;
835 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
838 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
844 xgeommasktogravity(int mask
)
846 switch (mask
& (XNegative
|YNegative
)) {
848 return NorthWestGravity
;
850 return NorthEastGravity
;
852 return SouthWestGravity
;
855 return SouthEastGravity
;
859 xloadfont(Font
*f
, FcPattern
*pattern
)
861 FcPattern
*configured
;
865 int wantattr
, haveattr
;
868 * Manually configure instead of calling XftMatchFont
869 * so that we can use the configured pattern for
870 * "missing glyph" lookups.
872 configured
= FcPatternDuplicate(pattern
);
876 FcConfigSubstitute(NULL
, configured
, FcMatchPattern
);
877 XftDefaultSubstitute(xw
.dpy
, xw
.scr
, configured
);
879 match
= FcFontMatch(NULL
, configured
, &result
);
881 FcPatternDestroy(configured
);
885 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
886 FcPatternDestroy(configured
);
887 FcPatternDestroy(match
);
891 if ((XftPatternGetInteger(pattern
, "slant", 0, &wantattr
) ==
894 * Check if xft was unable to find a font with the appropriate
895 * slant but gave us one anyway. Try to mitigate.
897 if ((XftPatternGetInteger(f
->match
->pattern
, "slant", 0,
898 &haveattr
) != XftResultMatch
) || haveattr
< wantattr
) {
900 fputs("font slant does not match\n", stderr
);
904 if ((XftPatternGetInteger(pattern
, "weight", 0, &wantattr
) ==
906 if ((XftPatternGetInteger(f
->match
->pattern
, "weight", 0,
907 &haveattr
) != XftResultMatch
) || haveattr
!= wantattr
) {
909 fputs("font weight does not match\n", stderr
);
913 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
914 (const FcChar8
*) ascii_printable
,
915 strlen(ascii_printable
), &extents
);
918 f
->pattern
= configured
;
920 f
->ascent
= f
->match
->ascent
;
921 f
->descent
= f
->match
->descent
;
923 f
->rbearing
= f
->match
->max_advance_width
;
925 f
->height
= f
->ascent
+ f
->descent
;
926 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
932 xloadfonts(char *fontstr
, double fontsize
)
937 if (fontstr
[0] == '-')
938 pattern
= XftXlfdParse(fontstr
, False
, False
);
940 pattern
= FcNameParse((FcChar8
*)fontstr
);
943 die("can't open font %s\n", fontstr
);
946 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
947 FcPatternDel(pattern
, FC_SIZE
);
948 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
949 usedfontsize
= fontsize
;
951 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
953 usedfontsize
= fontval
;
954 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
959 * Default font size is 12, if none given. This is to
960 * have a known usedfontsize value.
962 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
965 defaultfontsize
= usedfontsize
;
968 if (xloadfont(&dc
.font
, pattern
))
969 die("can't open font %s\n", fontstr
);
971 if (usedfontsize
< 0) {
972 FcPatternGetDouble(dc
.font
.match
->pattern
,
973 FC_PIXEL_SIZE
, 0, &fontval
);
974 usedfontsize
= fontval
;
976 defaultfontsize
= fontval
;
979 /* Setting character width and height. */
980 win
.cw
= ceilf(dc
.font
.width
* cwscale
);
981 win
.ch
= ceilf(dc
.font
.height
* chscale
);
983 FcPatternDel(pattern
, FC_SLANT
);
984 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
985 if (xloadfont(&dc
.ifont
, pattern
))
986 die("can't open font %s\n", fontstr
);
988 FcPatternDel(pattern
, FC_WEIGHT
);
989 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
990 if (xloadfont(&dc
.ibfont
, pattern
))
991 die("can't open font %s\n", fontstr
);
993 FcPatternDel(pattern
, FC_SLANT
);
994 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
995 if (xloadfont(&dc
.bfont
, pattern
))
996 die("can't open font %s\n", fontstr
);
998 FcPatternDestroy(pattern
);
1002 xunloadfont(Font
*f
)
1004 XftFontClose(xw
.dpy
, f
->match
);
1005 FcPatternDestroy(f
->pattern
);
1007 FcFontSetDestroy(f
->set
);
1013 /* Free the loaded fonts in the font cache. */
1015 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
1017 xunloadfont(&dc
.font
);
1018 xunloadfont(&dc
.bfont
);
1019 xunloadfont(&dc
.ifont
);
1020 xunloadfont(&dc
.ibfont
);
1024 ximopen(Display
*dpy
)
1026 XIMCallback destroy
= { .client_data
= NULL
, .callback
= ximdestroy
};
1028 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1029 XSetLocaleModifiers("@im=local");
1030 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1031 XSetLocaleModifiers("@im=");
1032 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
)
1033 die("XOpenIM failed. Could not open input device.\n");
1036 if (XSetIMValues(xw
.xim
, XNDestroyCallback
, &destroy
, NULL
) != NULL
)
1037 die("XSetIMValues failed. Could not set input method value.\n");
1038 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
| XIMStatusNothing
,
1039 XNClientWindow
, xw
.win
, XNFocusWindow
, xw
.win
, NULL
);
1041 die("XCreateIC failed. Could not obtain input method.\n");
1045 ximinstantiate(Display
*dpy
, XPointer client
, XPointer call
)
1048 XUnregisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1049 ximinstantiate
, NULL
);
1053 ximdestroy(XIM xim
, XPointer client
, XPointer call
)
1056 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1057 ximinstantiate
, NULL
);
1061 xinit(int cols
, int rows
)
1066 pid_t thispid
= getpid();
1067 XColor xmousefg
, xmousebg
;
1069 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
1070 die("can't open display\n");
1071 xw
.scr
= XDefaultScreen(xw
.dpy
);
1072 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
1076 die("could not init fontconfig.\n");
1078 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
1079 xloadfonts(usedfont
, 0);
1082 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1085 /* adjust fixed window geometry */
1086 win
.w
= 2 * borderpx
+ cols
* win
.cw
;
1087 win
.h
= 2 * borderpx
+ rows
* win
.ch
;
1088 if (xw
.gm
& XNegative
)
1089 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - win
.w
- 2;
1090 if (xw
.gm
& YNegative
)
1091 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - win
.h
- 2;
1094 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
1095 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
1096 xw
.attrs
.bit_gravity
= NorthWestGravity
;
1097 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
| KeyReleaseMask
1098 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1099 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1100 xw
.attrs
.colormap
= xw
.cmap
;
1102 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
1103 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
1104 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
1105 win
.w
, win
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1106 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
1107 | CWEventMask
| CWColormap
, &xw
.attrs
);
1109 memset(&gcvalues
, 0, sizeof(gcvalues
));
1110 gcvalues
.graphics_exposures
= False
;
1111 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
1113 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
1114 DefaultDepth(xw
.dpy
, xw
.scr
));
1115 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
1116 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, win
.w
, win
.h
);
1118 /* font spec buffer */
1119 xw
.specbuf
= xmalloc(cols
* sizeof(GlyphFontSpec
));
1121 /* Xft rendering context */
1122 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
1127 /* white cursor, black outline */
1128 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
1129 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1131 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
1132 xmousefg
.red
= 0xffff;
1133 xmousefg
.green
= 0xffff;
1134 xmousefg
.blue
= 0xffff;
1137 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
1138 xmousebg
.red
= 0x0000;
1139 xmousebg
.green
= 0x0000;
1140 xmousebg
.blue
= 0x0000;
1143 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
1145 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1146 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
1147 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
1148 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
1150 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
1151 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
1152 PropModeReplace
, (uchar
*)&thispid
, 1);
1154 win
.mode
= MODE_NUMLOCK
;
1156 XMapWindow(xw
.dpy
, xw
.win
);
1158 XSync(xw
.dpy
, False
);
1160 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick1
);
1161 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick2
);
1162 xsel
.primary
= NULL
;
1163 xsel
.clipboard
= NULL
;
1164 xsel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
1165 if (xsel
.xtarget
== None
)
1166 xsel
.xtarget
= XA_STRING
;
1170 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
1172 float winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
, xp
, yp
;
1173 ushort mode
, prevmode
= USHRT_MAX
;
1174 Font
*font
= &dc
.font
;
1175 int frcflags
= FRC_NORMAL
;
1176 float runewidth
= win
.cw
;
1180 FcPattern
*fcpattern
, *fontpattern
;
1181 FcFontSet
*fcsets
[] = { NULL
};
1182 FcCharSet
*fccharset
;
1183 int i
, f
, numspecs
= 0;
1185 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
1186 /* Fetch rune and mode for current glyph. */
1188 mode
= glyphs
[i
].mode
;
1190 /* Skip dummy wide-character spacing. */
1191 if (mode
== ATTR_WDUMMY
)
1194 /* Determine font for glyph if different from previous glyph. */
1195 if (prevmode
!= mode
) {
1198 frcflags
= FRC_NORMAL
;
1199 runewidth
= win
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
1200 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
1202 frcflags
= FRC_ITALICBOLD
;
1203 } else if (mode
& ATTR_ITALIC
) {
1205 frcflags
= FRC_ITALIC
;
1206 } else if (mode
& ATTR_BOLD
) {
1208 frcflags
= FRC_BOLD
;
1210 yp
= winy
+ font
->ascent
;
1213 /* Lookup character index with default font. */
1214 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
1216 specs
[numspecs
].font
= font
->match
;
1217 specs
[numspecs
].glyph
= glyphidx
;
1218 specs
[numspecs
].x
= (short)xp
;
1219 specs
[numspecs
].y
= (short)yp
;
1225 /* Fallback on font cache, search the font cache for match. */
1226 for (f
= 0; f
< frclen
; f
++) {
1227 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
1228 /* Everything correct. */
1229 if (glyphidx
&& frc
[f
].flags
== frcflags
)
1231 /* We got a default font for a not found glyph. */
1232 if (!glyphidx
&& frc
[f
].flags
== frcflags
1233 && frc
[f
].unicodep
== rune
) {
1238 /* Nothing was found. Use fontconfig to find matching font. */
1241 font
->set
= FcFontSort(0, font
->pattern
,
1243 fcsets
[0] = font
->set
;
1246 * Nothing was found in the cache. Now use
1247 * some dozen of Fontconfig calls to get the
1248 * font for one single character.
1250 * Xft and fontconfig are design failures.
1252 fcpattern
= FcPatternDuplicate(font
->pattern
);
1253 fccharset
= FcCharSetCreate();
1255 FcCharSetAddChar(fccharset
, rune
);
1256 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
1258 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
1260 FcConfigSubstitute(0, fcpattern
,
1262 FcDefaultSubstitute(fcpattern
);
1264 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
1267 /* Allocate memory for the new cache entry. */
1268 if (frclen
>= frccap
) {
1270 frc
= xrealloc(frc
, frccap
* sizeof(Fontcache
));
1273 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
1275 if (!frc
[frclen
].font
)
1276 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1278 frc
[frclen
].flags
= frcflags
;
1279 frc
[frclen
].unicodep
= rune
;
1281 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
1286 FcPatternDestroy(fcpattern
);
1287 FcCharSetDestroy(fccharset
);
1290 specs
[numspecs
].font
= frc
[f
].font
;
1291 specs
[numspecs
].glyph
= glyphidx
;
1292 specs
[numspecs
].x
= (short)xp
;
1293 specs
[numspecs
].y
= (short)yp
;
1302 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
1304 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
1305 int winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
,
1306 width
= charlen
* win
.cw
;
1307 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
1308 XRenderColor colfg
, colbg
;
1311 /* Fallback on color display for attributes not supported by the font */
1312 if (base
.mode
& ATTR_ITALIC
&& base
.mode
& ATTR_BOLD
) {
1313 if (dc
.ibfont
.badslant
|| dc
.ibfont
.badweight
)
1314 base
.fg
= defaultattr
;
1315 } else if ((base
.mode
& ATTR_ITALIC
&& dc
.ifont
.badslant
) ||
1316 (base
.mode
& ATTR_BOLD
&& dc
.bfont
.badweight
)) {
1317 base
.fg
= defaultattr
;
1320 if (IS_TRUECOL(base
.fg
)) {
1321 colfg
.alpha
= 0xffff;
1322 colfg
.red
= TRUERED(base
.fg
);
1323 colfg
.green
= TRUEGREEN(base
.fg
);
1324 colfg
.blue
= TRUEBLUE(base
.fg
);
1325 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
1328 fg
= &dc
.col
[base
.fg
];
1331 if (IS_TRUECOL(base
.bg
)) {
1332 colbg
.alpha
= 0xffff;
1333 colbg
.green
= TRUEGREEN(base
.bg
);
1334 colbg
.red
= TRUERED(base
.bg
);
1335 colbg
.blue
= TRUEBLUE(base
.bg
);
1336 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
1339 bg
= &dc
.col
[base
.bg
];
1342 /* Change basic system colors [0-7] to bright system colors [8-15] */
1343 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
1344 fg
= &dc
.col
[base
.fg
+ 8];
1346 if (IS_SET(MODE_REVERSE
)) {
1347 if (fg
== &dc
.col
[defaultfg
]) {
1348 fg
= &dc
.col
[defaultbg
];
1350 colfg
.red
= ~fg
->color
.red
;
1351 colfg
.green
= ~fg
->color
.green
;
1352 colfg
.blue
= ~fg
->color
.blue
;
1353 colfg
.alpha
= fg
->color
.alpha
;
1354 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
1359 if (bg
== &dc
.col
[defaultbg
]) {
1360 bg
= &dc
.col
[defaultfg
];
1362 colbg
.red
= ~bg
->color
.red
;
1363 colbg
.green
= ~bg
->color
.green
;
1364 colbg
.blue
= ~bg
->color
.blue
;
1365 colbg
.alpha
= bg
->color
.alpha
;
1366 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
1372 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
1373 colfg
.red
= fg
->color
.red
/ 2;
1374 colfg
.green
= fg
->color
.green
/ 2;
1375 colfg
.blue
= fg
->color
.blue
/ 2;
1376 colfg
.alpha
= fg
->color
.alpha
;
1377 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
1381 if (base
.mode
& ATTR_REVERSE
) {
1387 if (base
.mode
& ATTR_BLINK
&& win
.mode
& MODE_BLINK
)
1390 if (base
.mode
& ATTR_INVISIBLE
)
1393 /* Intelligent cleaning up of the borders. */
1395 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
1397 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: 0));
1399 if (winx
+ width
>= borderpx
+ win
.tw
) {
1400 xclear(winx
+ width
, (y
== 0)? 0 : winy
, win
.w
,
1401 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: (winy
+ win
.ch
)));
1404 xclear(winx
, 0, winx
+ width
, borderpx
);
1405 if (winy
+ win
.ch
>= borderpx
+ win
.th
)
1406 xclear(winx
, winy
+ win
.ch
, winx
+ width
, win
.h
);
1408 /* Clean up the region we want to draw to. */
1409 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, win
.ch
);
1411 /* Set the clip region because Xft is sometimes dirty. */
1416 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
1418 /* Render the glyphs. */
1419 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
1421 /* Render underline and strikethrough. */
1422 if (base
.mode
& ATTR_UNDERLINE
) {
1423 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
1427 if (base
.mode
& ATTR_STRUCK
) {
1428 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
1432 /* Reset clip to none. */
1433 XftDrawSetClip(xw
.draw
, 0);
1437 xdrawglyph(Glyph g
, int x
, int y
)
1440 XftGlyphFontSpec spec
;
1442 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
1443 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
1447 xdrawcursor(int cx
, int cy
, Glyph g
, int ox
, int oy
, Glyph og
)
1451 /* remove the old cursor */
1452 if (selected(ox
, oy
))
1453 og
.mode
^= ATTR_REVERSE
;
1454 xdrawglyph(og
, ox
, oy
);
1456 if (IS_SET(MODE_HIDE
))
1460 * Select the right color for the right mode.
1462 g
.mode
&= ATTR_BOLD
|ATTR_ITALIC
|ATTR_UNDERLINE
|ATTR_STRUCK
|ATTR_WIDE
;
1464 if (IS_SET(MODE_REVERSE
)) {
1465 g
.mode
|= ATTR_REVERSE
;
1467 if (selected(cx
, cy
)) {
1468 drawcol
= dc
.col
[defaultcs
];
1471 drawcol
= dc
.col
[defaultrcs
];
1475 if (selected(cx
, cy
)) {
1482 drawcol
= dc
.col
[g
.bg
];
1485 /* draw the new one */
1486 if (IS_SET(MODE_FOCUSED
)) {
1487 switch (win
.cursor
) {
1488 case 7: /* st extension: snowman (U+2603) */
1490 case 0: /* Blinking Block */
1491 case 1: /* Blinking Block (Default) */
1492 case 2: /* Steady Block */
1493 xdrawglyph(g
, cx
, cy
);
1495 case 3: /* Blinking Underline */
1496 case 4: /* Steady Underline */
1497 XftDrawRect(xw
.draw
, &drawcol
,
1498 borderpx
+ cx
* win
.cw
,
1499 borderpx
+ (cy
+ 1) * win
.ch
- \
1501 win
.cw
, cursorthickness
);
1503 case 5: /* Blinking bar */
1504 case 6: /* Steady bar */
1505 XftDrawRect(xw
.draw
, &drawcol
,
1506 borderpx
+ cx
* win
.cw
,
1507 borderpx
+ cy
* win
.ch
,
1508 cursorthickness
, win
.ch
);
1512 XftDrawRect(xw
.draw
, &drawcol
,
1513 borderpx
+ cx
* win
.cw
,
1514 borderpx
+ cy
* win
.ch
,
1516 XftDrawRect(xw
.draw
, &drawcol
,
1517 borderpx
+ cx
* win
.cw
,
1518 borderpx
+ cy
* win
.ch
,
1520 XftDrawRect(xw
.draw
, &drawcol
,
1521 borderpx
+ (cx
+ 1) * win
.cw
- 1,
1522 borderpx
+ cy
* win
.ch
,
1524 XftDrawRect(xw
.draw
, &drawcol
,
1525 borderpx
+ cx
* win
.cw
,
1526 borderpx
+ (cy
+ 1) * win
.ch
- 1,
1534 char buf
[sizeof(long) * 8 + 1];
1536 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1537 setenv("WINDOWID", buf
, 1);
1544 DEFAULT(p
, opt_title
);
1546 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1548 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
1549 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
1556 return IS_SET(MODE_VISIBLE
);
1560 xdrawline(Line line
, int x1
, int y1
, int x2
)
1562 int i
, x
, ox
, numspecs
;
1564 XftGlyphFontSpec
*specs
= xw
.specbuf
;
1566 numspecs
= xmakeglyphfontspecs(specs
, &line
[x1
], x2
- x1
, x1
, y1
);
1568 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
1570 if (new.mode
== ATTR_WDUMMY
)
1572 if (selected(x
, y1
))
1573 new.mode
^= ATTR_REVERSE
;
1574 if (i
> 0 && ATTRCMP(base
, new)) {
1575 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1587 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1593 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, win
.w
,
1595 XSetForeground(xw
.dpy
, dc
.gc
,
1596 dc
.col
[IS_SET(MODE_REVERSE
)?
1597 defaultfg
: defaultbg
].pixel
);
1601 xximspot(int x
, int y
)
1603 XPoint spot
= { borderpx
+ x
* win
.cw
, borderpx
+ (y
+ 1) * win
.ch
};
1604 XVaNestedList attr
= XVaCreateNestedList(0, XNSpotLocation
, &spot
, NULL
);
1606 XSetICValues(xw
.xic
, XNPreeditAttributes
, attr
, NULL
);
1617 visibility(XEvent
*ev
)
1619 XVisibilityEvent
*e
= &ev
->xvisibility
;
1621 MODBIT(win
.mode
, e
->state
!= VisibilityFullyObscured
, MODE_VISIBLE
);
1627 win
.mode
&= ~MODE_VISIBLE
;
1631 xsetpointermotion(int set
)
1633 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
1634 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
1638 xsetmode(int set
, unsigned int flags
)
1640 int mode
= win
.mode
;
1641 MODBIT(win
.mode
, set
, flags
);
1642 if ((win
.mode
& MODE_REVERSE
) != (mode
& MODE_REVERSE
))
1647 xsetcursor(int cursor
)
1650 if (!BETWEEN(cursor
, 0, 6))
1652 win
.cursor
= cursor
;
1657 xseturgency(int add
)
1659 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1661 MODBIT(h
->flags
, add
, XUrgencyHint
);
1662 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1669 if (!(IS_SET(MODE_FOCUSED
)))
1672 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
1678 XFocusChangeEvent
*e
= &ev
->xfocus
;
1680 if (e
->mode
== NotifyGrab
)
1683 if (ev
->type
== FocusIn
) {
1684 XSetICFocus(xw
.xic
);
1685 win
.mode
|= MODE_FOCUSED
;
1687 if (IS_SET(MODE_FOCUS
))
1688 ttywrite("\033[I", 3, 0);
1690 XUnsetICFocus(xw
.xic
);
1691 win
.mode
&= ~MODE_FOCUSED
;
1692 if (IS_SET(MODE_FOCUS
))
1693 ttywrite("\033[O", 3, 0);
1698 match(uint mask
, uint state
)
1700 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
1704 kmap(KeySym k
, uint state
)
1709 /* Check for mapped keys out of X11 function keys. */
1710 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
1711 if (mappedkeys
[i
] == k
)
1714 if (i
== LEN(mappedkeys
)) {
1715 if ((k
& 0xFFFF) < 0xFD00)
1719 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
1723 if (!match(kp
->mask
, state
))
1726 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
1728 if (IS_SET(MODE_NUMLOCK
) && kp
->appkey
== 2)
1731 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
1743 XKeyEvent
*e
= &ev
->xkey
;
1745 char buf
[32], *customkey
;
1751 if (IS_SET(MODE_KBDLOCK
))
1754 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
1756 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
1757 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
1758 bp
->func(&(bp
->arg
));
1763 /* 2. custom keys from config.h */
1764 if ((customkey
= kmap(ksym
, e
->state
))) {
1765 ttywrite(customkey
, strlen(customkey
), 1);
1769 /* 3. composed string from input method */
1772 if (len
== 1 && e
->state
& Mod1Mask
) {
1773 if (IS_SET(MODE_8BIT
)) {
1776 len
= utf8encode(c
, buf
);
1784 ttywrite(buf
, len
, 1);
1792 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1794 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1795 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1796 win
.mode
|= MODE_FOCUSED
;
1798 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1799 win
.mode
&= ~MODE_FOCUSED
;
1801 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
1810 if (e
->xconfigure
.width
== win
.w
&& e
->xconfigure
.height
== win
.h
)
1813 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
1820 int w
= win
.w
, h
= win
.h
;
1822 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
1824 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
1827 /* Waiting for window mapping */
1829 XNextEvent(xw
.dpy
, &ev
);
1831 * This XFilterEvent call is required because of XOpenIM. It
1832 * does filter out the key event and some client message for
1833 * the input method too.
1835 if (XFilterEvent(&ev
, None
))
1837 if (ev
.type
== ConfigureNotify
) {
1838 w
= ev
.xconfigure
.width
;
1839 h
= ev
.xconfigure
.height
;
1841 } while (ev
.type
!= MapNotify
);
1843 ttyfd
= ttynew(opt_line
, shell
, opt_io
, opt_cmd
);
1846 clock_gettime(CLOCK_MONOTONIC
, &last
);
1849 for (xev
= actionfps
;;) {
1851 FD_SET(ttyfd
, &rfd
);
1854 if (pselect(MAX(xfd
, ttyfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
1857 die("select failed: %s\n", strerror(errno
));
1859 if (FD_ISSET(ttyfd
, &rfd
)) {
1862 blinkset
= tattrset(ATTR_BLINK
);
1864 MODBIT(win
.mode
, 0, MODE_BLINK
);
1868 if (FD_ISSET(xfd
, &rfd
))
1871 clock_gettime(CLOCK_MONOTONIC
, &now
);
1872 drawtimeout
.tv_sec
= 0;
1873 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
1877 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
1878 tsetdirtattr(ATTR_BLINK
);
1879 win
.mode
^= MODE_BLINK
;
1883 deltatime
= TIMEDIFF(now
, last
);
1884 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
1890 while (XPending(xw
.dpy
)) {
1891 XNextEvent(xw
.dpy
, &ev
);
1892 if (XFilterEvent(&ev
, None
))
1894 if (handler
[ev
.type
])
1895 (handler
[ev
.type
])(&ev
);
1901 if (xev
&& !FD_ISSET(xfd
, &rfd
))
1903 if (!FD_ISSET(ttyfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
1905 if (TIMEDIFF(now
, lastblink
) \
1907 drawtimeout
.tv_nsec
= 1000;
1909 drawtimeout
.tv_nsec
= (1E6
* \
1914 drawtimeout
.tv_sec
= \
1915 drawtimeout
.tv_nsec
/ 1E9
;
1916 drawtimeout
.tv_nsec
%= (long)1E9
;
1928 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
1929 " [-n name] [-o file]\n"
1930 " [-T title] [-t title] [-w windowid]"
1931 " [[-e] command [args ...]]\n"
1932 " %s [-aiv] [-c class] [-f font] [-g geometry]"
1933 " [-n name] [-o file]\n"
1934 " [-T title] [-t title] [-w windowid] -l line"
1935 " [stty_args ...]\n", argv0
, argv0
);
1939 main(int argc
, char *argv
[])
1943 win
.cursor
= cursorshape
;
1950 opt_class
= EARGF(usage());
1957 opt_font
= EARGF(usage());
1960 xw
.gm
= XParseGeometry(EARGF(usage()),
1961 &xw
.l
, &xw
.t
, &cols
, &rows
);
1967 opt_io
= EARGF(usage());
1970 opt_line
= EARGF(usage());
1973 opt_name
= EARGF(usage());
1977 opt_title
= EARGF(usage());
1980 opt_embed
= EARGF(usage());
1983 die("%s " VERSION
"\n", argv0
);
1990 if (argc
> 0) /* eat all remaining arguments */
1994 opt_title
= (opt_line
|| !opt_cmd
) ? "st" : opt_cmd
[0];
1996 setlocale(LC_CTYPE
, "");
1997 XSetLocaleModifiers("");
1998 cols
= MAX(cols
, 1);
1999 rows
= MAX(rows
, 1);