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
) || /* exact or forced */
430 match(ms
->mod
, e
->xbutton
.state
& ~forcemousemod
))) {
431 ms
->func(&(ms
->arg
));
445 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
450 if (mouseaction(e
, 0))
453 if (e
->xbutton
.button
== Button1
) {
455 * If the user clicks below predefined timeouts specific
456 * snapping behaviour is exposed.
458 clock_gettime(CLOCK_MONOTONIC
, &now
);
459 if (TIMEDIFF(now
, xsel
.tclick2
) <= tripleclicktimeout
) {
461 } else if (TIMEDIFF(now
, xsel
.tclick1
) <= doubleclicktimeout
) {
466 xsel
.tclick2
= xsel
.tclick1
;
469 selstart(evcol(e
), evrow(e
), snap
);
474 propnotify(XEvent
*e
)
476 XPropertyEvent
*xpev
;
477 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
479 xpev
= &e
->xproperty
;
480 if (xpev
->state
== PropertyNewValue
&&
481 (xpev
->atom
== XA_PRIMARY
||
482 xpev
->atom
== clipboard
)) {
490 ulong nitems
, ofs
, rem
;
492 uchar
*data
, *last
, *repl
;
493 Atom type
, incratom
, property
= None
;
495 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
498 if (e
->type
== SelectionNotify
)
499 property
= e
->xselection
.property
;
500 else if (e
->type
== PropertyNotify
)
501 property
= e
->xproperty
.atom
;
503 if (property
== None
)
507 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
508 BUFSIZ
/4, False
, AnyPropertyType
,
509 &type
, &format
, &nitems
, &rem
,
511 fprintf(stderr
, "Clipboard allocation failed\n");
515 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
517 * If there is some PropertyNotify with no data, then
518 * this is the signal of the selection owner that all
519 * data has been transferred. We won't need to receive
520 * PropertyNotify events anymore.
522 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
523 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
527 if (type
== incratom
) {
529 * Activate the PropertyNotify events so we receive
530 * when the selection owner does send us the next
533 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
534 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
538 * Deleting the property is the transfer start signal.
540 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
546 * Line endings are inconsistent in the terminal and GUI world
547 * copy and pasting. When receiving some selection data,
548 * replace all '\n' with '\r'.
549 * FIXME: Fix the computer world.
552 last
= data
+ nitems
* format
/ 8;
553 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
557 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
558 ttywrite("\033[200~", 6, 0);
559 ttywrite((char *)data
, nitems
* format
/ 8, 1);
560 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
561 ttywrite("\033[201~", 6, 0);
563 /* number of 32-bit chunks returned */
564 ofs
+= nitems
* format
/ 32;
568 * Deleting the property again tells the selection owner to send the
569 * next data chunk in the property.
571 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
587 selrequest(XEvent
*e
)
589 XSelectionRequestEvent
*xsre
;
591 Atom xa_targets
, string
, clipboard
;
594 xsre
= (XSelectionRequestEvent
*) e
;
595 xev
.type
= SelectionNotify
;
596 xev
.requestor
= xsre
->requestor
;
597 xev
.selection
= xsre
->selection
;
598 xev
.target
= xsre
->target
;
599 xev
.time
= xsre
->time
;
600 if (xsre
->property
== None
)
601 xsre
->property
= xsre
->target
;
606 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
607 if (xsre
->target
== xa_targets
) {
608 /* respond with the supported type */
609 string
= xsel
.xtarget
;
610 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
611 XA_ATOM
, 32, PropModeReplace
,
612 (uchar
*) &string
, 1);
613 xev
.property
= xsre
->property
;
614 } else if (xsre
->target
== xsel
.xtarget
|| xsre
->target
== XA_STRING
) {
616 * xith XA_STRING non ascii characters may be incorrect in the
617 * requestor. It is not our problem, use utf8.
619 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
620 if (xsre
->selection
== XA_PRIMARY
) {
621 seltext
= xsel
.primary
;
622 } else if (xsre
->selection
== clipboard
) {
623 seltext
= xsel
.clipboard
;
626 "Unhandled clipboard selection 0x%lx\n",
630 if (seltext
!= NULL
) {
631 XChangeProperty(xsre
->display
, xsre
->requestor
,
632 xsre
->property
, xsre
->target
,
634 (uchar
*)seltext
, strlen(seltext
));
635 xev
.property
= xsre
->property
;
639 /* all done, send a notification to the listener */
640 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
641 fprintf(stderr
, "Error sending SelectionNotify event\n");
645 setsel(char *str
, Time t
)
653 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
654 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
661 setsel(str
, CurrentTime
);
667 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
672 if (mouseaction(e
, 1))
674 if (e
->xbutton
.button
== Button1
)
681 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
690 cresize(int width
, int height
)
699 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
700 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
706 ttyresize(win
.tw
, win
.th
);
710 xresize(int col
, int row
)
712 win
.tw
= col
* win
.cw
;
713 win
.th
= row
* win
.ch
;
715 XFreePixmap(xw
.dpy
, xw
.buf
);
716 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
717 DefaultDepth(xw
.dpy
, xw
.scr
));
718 XftDrawChange(xw
.draw
, xw
.buf
);
719 xclear(0, 0, win
.w
, win
.h
);
721 /* resize to new width */
722 xw
.specbuf
= xrealloc(xw
.specbuf
, col
* sizeof(GlyphFontSpec
));
728 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
732 xloadcolor(int i
, const char *name
, Color
*ncolor
)
734 XRenderColor color
= { .alpha
= 0xffff };
737 if (BETWEEN(i
, 16, 255)) { /* 256 color */
738 if (i
< 6*6*6+16) { /* same colors as xterm */
739 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
740 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
741 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
742 } else { /* greyscale */
743 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
744 color
.green
= color
.blue
= color
.red
;
746 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
747 xw
.cmap
, &color
, ncolor
);
752 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
763 for (cp
= dc
.col
; cp
< &dc
.col
[dc
.collen
]; ++cp
)
764 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
766 dc
.collen
= MAX(LEN(colorname
), 256);
767 dc
.col
= xmalloc(dc
.collen
* sizeof(Color
));
770 for (i
= 0; i
< dc
.collen
; i
++)
771 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
773 die("could not allocate color '%s'\n", colorname
[i
]);
775 die("could not allocate color %d\n", i
);
781 xsetcolorname(int x
, const char *name
)
785 if (!BETWEEN(x
, 0, dc
.collen
))
788 if (!xloadcolor(x
, name
, &ncolor
))
791 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
798 * Absolute coordinates.
801 xclear(int x1
, int y1
, int x2
, int y2
)
804 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
805 x1
, y1
, x2
-x1
, y2
-y1
);
811 XClassHint
class = {opt_name
? opt_name
: termname
,
812 opt_class
? opt_class
: termname
};
813 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
816 sizeh
= XAllocSizeHints();
818 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
| PMinSize
;
819 sizeh
->height
= win
.h
;
820 sizeh
->width
= win
.w
;
821 sizeh
->height_inc
= win
.ch
;
822 sizeh
->width_inc
= win
.cw
;
823 sizeh
->base_height
= 2 * borderpx
;
824 sizeh
->base_width
= 2 * borderpx
;
825 sizeh
->min_height
= win
.ch
+ 2 * borderpx
;
826 sizeh
->min_width
= win
.cw
+ 2 * borderpx
;
828 sizeh
->flags
|= PMaxSize
;
829 sizeh
->min_width
= sizeh
->max_width
= win
.w
;
830 sizeh
->min_height
= sizeh
->max_height
= win
.h
;
832 if (xw
.gm
& (XValue
|YValue
)) {
833 sizeh
->flags
|= USPosition
| PWinGravity
;
836 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
839 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
845 xgeommasktogravity(int mask
)
847 switch (mask
& (XNegative
|YNegative
)) {
849 return NorthWestGravity
;
851 return NorthEastGravity
;
853 return SouthWestGravity
;
856 return SouthEastGravity
;
860 xloadfont(Font
*f
, FcPattern
*pattern
)
862 FcPattern
*configured
;
866 int wantattr
, haveattr
;
869 * Manually configure instead of calling XftMatchFont
870 * so that we can use the configured pattern for
871 * "missing glyph" lookups.
873 configured
= FcPatternDuplicate(pattern
);
877 FcConfigSubstitute(NULL
, configured
, FcMatchPattern
);
878 XftDefaultSubstitute(xw
.dpy
, xw
.scr
, configured
);
880 match
= FcFontMatch(NULL
, configured
, &result
);
882 FcPatternDestroy(configured
);
886 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
887 FcPatternDestroy(configured
);
888 FcPatternDestroy(match
);
892 if ((XftPatternGetInteger(pattern
, "slant", 0, &wantattr
) ==
895 * Check if xft was unable to find a font with the appropriate
896 * slant but gave us one anyway. Try to mitigate.
898 if ((XftPatternGetInteger(f
->match
->pattern
, "slant", 0,
899 &haveattr
) != XftResultMatch
) || haveattr
< wantattr
) {
901 fputs("font slant does not match\n", stderr
);
905 if ((XftPatternGetInteger(pattern
, "weight", 0, &wantattr
) ==
907 if ((XftPatternGetInteger(f
->match
->pattern
, "weight", 0,
908 &haveattr
) != XftResultMatch
) || haveattr
!= wantattr
) {
910 fputs("font weight does not match\n", stderr
);
914 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
915 (const FcChar8
*) ascii_printable
,
916 strlen(ascii_printable
), &extents
);
919 f
->pattern
= configured
;
921 f
->ascent
= f
->match
->ascent
;
922 f
->descent
= f
->match
->descent
;
924 f
->rbearing
= f
->match
->max_advance_width
;
926 f
->height
= f
->ascent
+ f
->descent
;
927 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
933 xloadfonts(char *fontstr
, double fontsize
)
938 if (fontstr
[0] == '-')
939 pattern
= XftXlfdParse(fontstr
, False
, False
);
941 pattern
= FcNameParse((FcChar8
*)fontstr
);
944 die("can't open font %s\n", fontstr
);
947 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
948 FcPatternDel(pattern
, FC_SIZE
);
949 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
950 usedfontsize
= fontsize
;
952 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
954 usedfontsize
= fontval
;
955 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
960 * Default font size is 12, if none given. This is to
961 * have a known usedfontsize value.
963 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
966 defaultfontsize
= usedfontsize
;
969 if (xloadfont(&dc
.font
, pattern
))
970 die("can't open font %s\n", fontstr
);
972 if (usedfontsize
< 0) {
973 FcPatternGetDouble(dc
.font
.match
->pattern
,
974 FC_PIXEL_SIZE
, 0, &fontval
);
975 usedfontsize
= fontval
;
977 defaultfontsize
= fontval
;
980 /* Setting character width and height. */
981 win
.cw
= ceilf(dc
.font
.width
* cwscale
);
982 win
.ch
= ceilf(dc
.font
.height
* chscale
);
984 FcPatternDel(pattern
, FC_SLANT
);
985 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
986 if (xloadfont(&dc
.ifont
, pattern
))
987 die("can't open font %s\n", fontstr
);
989 FcPatternDel(pattern
, FC_WEIGHT
);
990 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
991 if (xloadfont(&dc
.ibfont
, pattern
))
992 die("can't open font %s\n", fontstr
);
994 FcPatternDel(pattern
, FC_SLANT
);
995 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
996 if (xloadfont(&dc
.bfont
, pattern
))
997 die("can't open font %s\n", fontstr
);
999 FcPatternDestroy(pattern
);
1003 xunloadfont(Font
*f
)
1005 XftFontClose(xw
.dpy
, f
->match
);
1006 FcPatternDestroy(f
->pattern
);
1008 FcFontSetDestroy(f
->set
);
1014 /* Free the loaded fonts in the font cache. */
1016 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
1018 xunloadfont(&dc
.font
);
1019 xunloadfont(&dc
.bfont
);
1020 xunloadfont(&dc
.ifont
);
1021 xunloadfont(&dc
.ibfont
);
1025 ximopen(Display
*dpy
)
1027 XIMCallback destroy
= { .client_data
= NULL
, .callback
= ximdestroy
};
1029 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1030 XSetLocaleModifiers("@im=local");
1031 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1032 XSetLocaleModifiers("@im=");
1033 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
)
1034 die("XOpenIM failed. Could not open input device.\n");
1037 if (XSetIMValues(xw
.xim
, XNDestroyCallback
, &destroy
, NULL
) != NULL
)
1038 die("XSetIMValues failed. Could not set input method value.\n");
1039 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
| XIMStatusNothing
,
1040 XNClientWindow
, xw
.win
, XNFocusWindow
, xw
.win
, NULL
);
1042 die("XCreateIC failed. Could not obtain input method.\n");
1046 ximinstantiate(Display
*dpy
, XPointer client
, XPointer call
)
1049 XUnregisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1050 ximinstantiate
, NULL
);
1054 ximdestroy(XIM xim
, XPointer client
, XPointer call
)
1057 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1058 ximinstantiate
, NULL
);
1062 xinit(int cols
, int rows
)
1067 pid_t thispid
= getpid();
1068 XColor xmousefg
, xmousebg
;
1070 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
1071 die("can't open display\n");
1072 xw
.scr
= XDefaultScreen(xw
.dpy
);
1073 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
1077 die("could not init fontconfig.\n");
1079 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
1080 xloadfonts(usedfont
, 0);
1083 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1086 /* adjust fixed window geometry */
1087 win
.w
= 2 * borderpx
+ cols
* win
.cw
;
1088 win
.h
= 2 * borderpx
+ rows
* win
.ch
;
1089 if (xw
.gm
& XNegative
)
1090 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - win
.w
- 2;
1091 if (xw
.gm
& YNegative
)
1092 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - win
.h
- 2;
1095 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
1096 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
1097 xw
.attrs
.bit_gravity
= NorthWestGravity
;
1098 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
| KeyReleaseMask
1099 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1100 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1101 xw
.attrs
.colormap
= xw
.cmap
;
1103 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
1104 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
1105 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
1106 win
.w
, win
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1107 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
1108 | CWEventMask
| CWColormap
, &xw
.attrs
);
1110 memset(&gcvalues
, 0, sizeof(gcvalues
));
1111 gcvalues
.graphics_exposures
= False
;
1112 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
1114 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
1115 DefaultDepth(xw
.dpy
, xw
.scr
));
1116 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
1117 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, win
.w
, win
.h
);
1119 /* font spec buffer */
1120 xw
.specbuf
= xmalloc(cols
* sizeof(GlyphFontSpec
));
1122 /* Xft rendering context */
1123 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
1128 /* white cursor, black outline */
1129 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
1130 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1132 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
1133 xmousefg
.red
= 0xffff;
1134 xmousefg
.green
= 0xffff;
1135 xmousefg
.blue
= 0xffff;
1138 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
1139 xmousebg
.red
= 0x0000;
1140 xmousebg
.green
= 0x0000;
1141 xmousebg
.blue
= 0x0000;
1144 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
1146 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1147 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
1148 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
1149 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
1151 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
1152 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
1153 PropModeReplace
, (uchar
*)&thispid
, 1);
1155 win
.mode
= MODE_NUMLOCK
;
1158 XMapWindow(xw
.dpy
, xw
.win
);
1159 XSync(xw
.dpy
, False
);
1161 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick1
);
1162 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick2
);
1163 xsel
.primary
= NULL
;
1164 xsel
.clipboard
= NULL
;
1165 xsel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
1166 if (xsel
.xtarget
== None
)
1167 xsel
.xtarget
= XA_STRING
;
1171 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
1173 float winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
, xp
, yp
;
1174 ushort mode
, prevmode
= USHRT_MAX
;
1175 Font
*font
= &dc
.font
;
1176 int frcflags
= FRC_NORMAL
;
1177 float runewidth
= win
.cw
;
1181 FcPattern
*fcpattern
, *fontpattern
;
1182 FcFontSet
*fcsets
[] = { NULL
};
1183 FcCharSet
*fccharset
;
1184 int i
, f
, numspecs
= 0;
1186 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
1187 /* Fetch rune and mode for current glyph. */
1189 mode
= glyphs
[i
].mode
;
1191 /* Skip dummy wide-character spacing. */
1192 if (mode
== ATTR_WDUMMY
)
1195 /* Determine font for glyph if different from previous glyph. */
1196 if (prevmode
!= mode
) {
1199 frcflags
= FRC_NORMAL
;
1200 runewidth
= win
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
1201 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
1203 frcflags
= FRC_ITALICBOLD
;
1204 } else if (mode
& ATTR_ITALIC
) {
1206 frcflags
= FRC_ITALIC
;
1207 } else if (mode
& ATTR_BOLD
) {
1209 frcflags
= FRC_BOLD
;
1211 yp
= winy
+ font
->ascent
;
1214 /* Lookup character index with default font. */
1215 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
1217 specs
[numspecs
].font
= font
->match
;
1218 specs
[numspecs
].glyph
= glyphidx
;
1219 specs
[numspecs
].x
= (short)xp
;
1220 specs
[numspecs
].y
= (short)yp
;
1226 /* Fallback on font cache, search the font cache for match. */
1227 for (f
= 0; f
< frclen
; f
++) {
1228 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
1229 /* Everything correct. */
1230 if (glyphidx
&& frc
[f
].flags
== frcflags
)
1232 /* We got a default font for a not found glyph. */
1233 if (!glyphidx
&& frc
[f
].flags
== frcflags
1234 && frc
[f
].unicodep
== rune
) {
1239 /* Nothing was found. Use fontconfig to find matching font. */
1242 font
->set
= FcFontSort(0, font
->pattern
,
1244 fcsets
[0] = font
->set
;
1247 * Nothing was found in the cache. Now use
1248 * some dozen of Fontconfig calls to get the
1249 * font for one single character.
1251 * Xft and fontconfig are design failures.
1253 fcpattern
= FcPatternDuplicate(font
->pattern
);
1254 fccharset
= FcCharSetCreate();
1256 FcCharSetAddChar(fccharset
, rune
);
1257 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
1259 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
1261 FcConfigSubstitute(0, fcpattern
,
1263 FcDefaultSubstitute(fcpattern
);
1265 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
1268 /* Allocate memory for the new cache entry. */
1269 if (frclen
>= frccap
) {
1271 frc
= xrealloc(frc
, frccap
* sizeof(Fontcache
));
1274 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
1276 if (!frc
[frclen
].font
)
1277 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1279 frc
[frclen
].flags
= frcflags
;
1280 frc
[frclen
].unicodep
= rune
;
1282 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
1287 FcPatternDestroy(fcpattern
);
1288 FcCharSetDestroy(fccharset
);
1291 specs
[numspecs
].font
= frc
[f
].font
;
1292 specs
[numspecs
].glyph
= glyphidx
;
1293 specs
[numspecs
].x
= (short)xp
;
1294 specs
[numspecs
].y
= (short)yp
;
1303 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
1305 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
1306 int winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
,
1307 width
= charlen
* win
.cw
;
1308 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
1309 XRenderColor colfg
, colbg
;
1312 /* Fallback on color display for attributes not supported by the font */
1313 if (base
.mode
& ATTR_ITALIC
&& base
.mode
& ATTR_BOLD
) {
1314 if (dc
.ibfont
.badslant
|| dc
.ibfont
.badweight
)
1315 base
.fg
= defaultattr
;
1316 } else if ((base
.mode
& ATTR_ITALIC
&& dc
.ifont
.badslant
) ||
1317 (base
.mode
& ATTR_BOLD
&& dc
.bfont
.badweight
)) {
1318 base
.fg
= defaultattr
;
1321 if (IS_TRUECOL(base
.fg
)) {
1322 colfg
.alpha
= 0xffff;
1323 colfg
.red
= TRUERED(base
.fg
);
1324 colfg
.green
= TRUEGREEN(base
.fg
);
1325 colfg
.blue
= TRUEBLUE(base
.fg
);
1326 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
1329 fg
= &dc
.col
[base
.fg
];
1332 if (IS_TRUECOL(base
.bg
)) {
1333 colbg
.alpha
= 0xffff;
1334 colbg
.green
= TRUEGREEN(base
.bg
);
1335 colbg
.red
= TRUERED(base
.bg
);
1336 colbg
.blue
= TRUEBLUE(base
.bg
);
1337 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
1340 bg
= &dc
.col
[base
.bg
];
1343 /* Change basic system colors [0-7] to bright system colors [8-15] */
1344 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
1345 fg
= &dc
.col
[base
.fg
+ 8];
1347 if (IS_SET(MODE_REVERSE
)) {
1348 if (fg
== &dc
.col
[defaultfg
]) {
1349 fg
= &dc
.col
[defaultbg
];
1351 colfg
.red
= ~fg
->color
.red
;
1352 colfg
.green
= ~fg
->color
.green
;
1353 colfg
.blue
= ~fg
->color
.blue
;
1354 colfg
.alpha
= fg
->color
.alpha
;
1355 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
1360 if (bg
== &dc
.col
[defaultbg
]) {
1361 bg
= &dc
.col
[defaultfg
];
1363 colbg
.red
= ~bg
->color
.red
;
1364 colbg
.green
= ~bg
->color
.green
;
1365 colbg
.blue
= ~bg
->color
.blue
;
1366 colbg
.alpha
= bg
->color
.alpha
;
1367 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
1373 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
1374 colfg
.red
= fg
->color
.red
/ 2;
1375 colfg
.green
= fg
->color
.green
/ 2;
1376 colfg
.blue
= fg
->color
.blue
/ 2;
1377 colfg
.alpha
= fg
->color
.alpha
;
1378 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
1382 if (base
.mode
& ATTR_REVERSE
) {
1388 if (base
.mode
& ATTR_BLINK
&& win
.mode
& MODE_BLINK
)
1391 if (base
.mode
& ATTR_INVISIBLE
)
1394 /* Intelligent cleaning up of the borders. */
1396 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
1398 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: 0));
1400 if (winx
+ width
>= borderpx
+ win
.tw
) {
1401 xclear(winx
+ width
, (y
== 0)? 0 : winy
, win
.w
,
1402 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: (winy
+ win
.ch
)));
1405 xclear(winx
, 0, winx
+ width
, borderpx
);
1406 if (winy
+ win
.ch
>= borderpx
+ win
.th
)
1407 xclear(winx
, winy
+ win
.ch
, winx
+ width
, win
.h
);
1409 /* Clean up the region we want to draw to. */
1410 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, win
.ch
);
1412 /* Set the clip region because Xft is sometimes dirty. */
1417 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
1419 /* Render the glyphs. */
1420 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
1422 /* Render underline and strikethrough. */
1423 if (base
.mode
& ATTR_UNDERLINE
) {
1424 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
1428 if (base
.mode
& ATTR_STRUCK
) {
1429 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
1433 /* Reset clip to none. */
1434 XftDrawSetClip(xw
.draw
, 0);
1438 xdrawglyph(Glyph g
, int x
, int y
)
1441 XftGlyphFontSpec spec
;
1443 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
1444 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
1448 xdrawcursor(int cx
, int cy
, Glyph g
, int ox
, int oy
, Glyph og
)
1452 /* remove the old cursor */
1453 if (selected(ox
, oy
))
1454 og
.mode
^= ATTR_REVERSE
;
1455 xdrawglyph(og
, ox
, oy
);
1457 if (IS_SET(MODE_HIDE
))
1461 * Select the right color for the right mode.
1463 g
.mode
&= ATTR_BOLD
|ATTR_ITALIC
|ATTR_UNDERLINE
|ATTR_STRUCK
|ATTR_WIDE
;
1465 if (IS_SET(MODE_REVERSE
)) {
1466 g
.mode
|= ATTR_REVERSE
;
1468 if (selected(cx
, cy
)) {
1469 drawcol
= dc
.col
[defaultcs
];
1472 drawcol
= dc
.col
[defaultrcs
];
1476 if (selected(cx
, cy
)) {
1483 drawcol
= dc
.col
[g
.bg
];
1486 /* draw the new one */
1487 if (IS_SET(MODE_FOCUSED
)) {
1488 switch (win
.cursor
) {
1489 case 7: /* st extension: snowman (U+2603) */
1491 case 0: /* Blinking Block */
1492 case 1: /* Blinking Block (Default) */
1493 case 2: /* Steady Block */
1494 xdrawglyph(g
, cx
, cy
);
1496 case 3: /* Blinking Underline */
1497 case 4: /* Steady Underline */
1498 XftDrawRect(xw
.draw
, &drawcol
,
1499 borderpx
+ cx
* win
.cw
,
1500 borderpx
+ (cy
+ 1) * win
.ch
- \
1502 win
.cw
, cursorthickness
);
1504 case 5: /* Blinking bar */
1505 case 6: /* Steady bar */
1506 XftDrawRect(xw
.draw
, &drawcol
,
1507 borderpx
+ cx
* win
.cw
,
1508 borderpx
+ cy
* win
.ch
,
1509 cursorthickness
, win
.ch
);
1513 XftDrawRect(xw
.draw
, &drawcol
,
1514 borderpx
+ cx
* win
.cw
,
1515 borderpx
+ cy
* win
.ch
,
1517 XftDrawRect(xw
.draw
, &drawcol
,
1518 borderpx
+ cx
* win
.cw
,
1519 borderpx
+ cy
* win
.ch
,
1521 XftDrawRect(xw
.draw
, &drawcol
,
1522 borderpx
+ (cx
+ 1) * win
.cw
- 1,
1523 borderpx
+ cy
* win
.ch
,
1525 XftDrawRect(xw
.draw
, &drawcol
,
1526 borderpx
+ cx
* win
.cw
,
1527 borderpx
+ (cy
+ 1) * win
.ch
- 1,
1535 char buf
[sizeof(long) * 8 + 1];
1537 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1538 setenv("WINDOWID", buf
, 1);
1545 DEFAULT(p
, opt_title
);
1547 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1549 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
1550 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
1557 return IS_SET(MODE_VISIBLE
);
1561 xdrawline(Line line
, int x1
, int y1
, int x2
)
1563 int i
, x
, ox
, numspecs
;
1565 XftGlyphFontSpec
*specs
= xw
.specbuf
;
1567 numspecs
= xmakeglyphfontspecs(specs
, &line
[x1
], x2
- x1
, x1
, y1
);
1569 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
1571 if (new.mode
== ATTR_WDUMMY
)
1573 if (selected(x
, y1
))
1574 new.mode
^= ATTR_REVERSE
;
1575 if (i
> 0 && ATTRCMP(base
, new)) {
1576 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1588 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1594 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, win
.w
,
1596 XSetForeground(xw
.dpy
, dc
.gc
,
1597 dc
.col
[IS_SET(MODE_REVERSE
)?
1598 defaultfg
: defaultbg
].pixel
);
1602 xximspot(int x
, int y
)
1604 XPoint spot
= { borderpx
+ x
* win
.cw
, borderpx
+ (y
+ 1) * win
.ch
};
1605 XVaNestedList attr
= XVaCreateNestedList(0, XNSpotLocation
, &spot
, NULL
);
1607 XSetICValues(xw
.xic
, XNPreeditAttributes
, attr
, NULL
);
1618 visibility(XEvent
*ev
)
1620 XVisibilityEvent
*e
= &ev
->xvisibility
;
1622 MODBIT(win
.mode
, e
->state
!= VisibilityFullyObscured
, MODE_VISIBLE
);
1628 win
.mode
&= ~MODE_VISIBLE
;
1632 xsetpointermotion(int set
)
1634 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
1635 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
1639 xsetmode(int set
, unsigned int flags
)
1641 int mode
= win
.mode
;
1642 MODBIT(win
.mode
, set
, flags
);
1643 if ((win
.mode
& MODE_REVERSE
) != (mode
& MODE_REVERSE
))
1648 xsetcursor(int cursor
)
1651 if (!BETWEEN(cursor
, 0, 6))
1653 win
.cursor
= cursor
;
1658 xseturgency(int add
)
1660 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1662 MODBIT(h
->flags
, add
, XUrgencyHint
);
1663 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1670 if (!(IS_SET(MODE_FOCUSED
)))
1673 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
1679 XFocusChangeEvent
*e
= &ev
->xfocus
;
1681 if (e
->mode
== NotifyGrab
)
1684 if (ev
->type
== FocusIn
) {
1685 XSetICFocus(xw
.xic
);
1686 win
.mode
|= MODE_FOCUSED
;
1688 if (IS_SET(MODE_FOCUS
))
1689 ttywrite("\033[I", 3, 0);
1691 XUnsetICFocus(xw
.xic
);
1692 win
.mode
&= ~MODE_FOCUSED
;
1693 if (IS_SET(MODE_FOCUS
))
1694 ttywrite("\033[O", 3, 0);
1699 match(uint mask
, uint state
)
1701 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
1705 kmap(KeySym k
, uint state
)
1710 /* Check for mapped keys out of X11 function keys. */
1711 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
1712 if (mappedkeys
[i
] == k
)
1715 if (i
== LEN(mappedkeys
)) {
1716 if ((k
& 0xFFFF) < 0xFD00)
1720 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
1724 if (!match(kp
->mask
, state
))
1727 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
1729 if (IS_SET(MODE_NUMLOCK
) && kp
->appkey
== 2)
1732 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
1744 XKeyEvent
*e
= &ev
->xkey
;
1746 char buf
[32], *customkey
;
1752 if (IS_SET(MODE_KBDLOCK
))
1755 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
1757 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
1758 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
1759 bp
->func(&(bp
->arg
));
1764 /* 2. custom keys from config.h */
1765 if ((customkey
= kmap(ksym
, e
->state
))) {
1766 ttywrite(customkey
, strlen(customkey
), 1);
1770 /* 3. composed string from input method */
1773 if (len
== 1 && e
->state
& Mod1Mask
) {
1774 if (IS_SET(MODE_8BIT
)) {
1777 len
= utf8encode(c
, buf
);
1785 ttywrite(buf
, len
, 1);
1793 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1795 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1796 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1797 win
.mode
|= MODE_FOCUSED
;
1799 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1800 win
.mode
&= ~MODE_FOCUSED
;
1802 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
1811 if (e
->xconfigure
.width
== win
.w
&& e
->xconfigure
.height
== win
.h
)
1814 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
1821 int w
= win
.w
, h
= win
.h
;
1823 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
1825 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
1828 /* Waiting for window mapping */
1830 XNextEvent(xw
.dpy
, &ev
);
1832 * This XFilterEvent call is required because of XOpenIM. It
1833 * does filter out the key event and some client message for
1834 * the input method too.
1836 if (XFilterEvent(&ev
, None
))
1838 if (ev
.type
== ConfigureNotify
) {
1839 w
= ev
.xconfigure
.width
;
1840 h
= ev
.xconfigure
.height
;
1842 } while (ev
.type
!= MapNotify
);
1844 ttyfd
= ttynew(opt_line
, shell
, opt_io
, opt_cmd
);
1847 clock_gettime(CLOCK_MONOTONIC
, &last
);
1850 for (xev
= actionfps
;;) {
1852 FD_SET(ttyfd
, &rfd
);
1855 if (pselect(MAX(xfd
, ttyfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
1858 die("select failed: %s\n", strerror(errno
));
1860 if (FD_ISSET(ttyfd
, &rfd
)) {
1863 blinkset
= tattrset(ATTR_BLINK
);
1865 MODBIT(win
.mode
, 0, MODE_BLINK
);
1869 if (FD_ISSET(xfd
, &rfd
))
1872 clock_gettime(CLOCK_MONOTONIC
, &now
);
1873 drawtimeout
.tv_sec
= 0;
1874 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
1878 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
1879 tsetdirtattr(ATTR_BLINK
);
1880 win
.mode
^= MODE_BLINK
;
1884 deltatime
= TIMEDIFF(now
, last
);
1885 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
1891 while (XPending(xw
.dpy
)) {
1892 XNextEvent(xw
.dpy
, &ev
);
1893 if (XFilterEvent(&ev
, None
))
1895 if (handler
[ev
.type
])
1896 (handler
[ev
.type
])(&ev
);
1902 if (xev
&& !FD_ISSET(xfd
, &rfd
))
1904 if (!FD_ISSET(ttyfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
1906 if (TIMEDIFF(now
, lastblink
) \
1908 drawtimeout
.tv_nsec
= 1000;
1910 drawtimeout
.tv_nsec
= (1E6
* \
1915 drawtimeout
.tv_sec
= \
1916 drawtimeout
.tv_nsec
/ 1E9
;
1917 drawtimeout
.tv_nsec
%= (long)1E9
;
1929 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
1930 " [-n name] [-o file]\n"
1931 " [-T title] [-t title] [-w windowid]"
1932 " [[-e] command [args ...]]\n"
1933 " %s [-aiv] [-c class] [-f font] [-g geometry]"
1934 " [-n name] [-o file]\n"
1935 " [-T title] [-t title] [-w windowid] -l line"
1936 " [stty_args ...]\n", argv0
, argv0
);
1940 main(int argc
, char *argv
[])
1944 win
.cursor
= cursorshape
;
1951 opt_class
= EARGF(usage());
1958 opt_font
= EARGF(usage());
1961 xw
.gm
= XParseGeometry(EARGF(usage()),
1962 &xw
.l
, &xw
.t
, &cols
, &rows
);
1968 opt_io
= EARGF(usage());
1971 opt_line
= EARGF(usage());
1974 opt_name
= EARGF(usage());
1978 opt_title
= EARGF(usage());
1981 opt_embed
= EARGF(usage());
1984 die("%s " VERSION
"\n", argv0
);
1991 if (argc
> 0) /* eat all remaining arguments */
1995 opt_title
= (opt_line
|| !opt_cmd
) ? "st" : opt_cmd
[0];
1997 setlocale(LC_CTYPE
, "");
1998 XSetLocaleModifiers("");
1999 cols
= MAX(cols
, 1);
2000 rows
= MAX(rows
, 1);