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 XVaNestedList spotlist
;
105 XSetWindowAttributes attrs
;
107 int isfixed
; /* is fixed geometry? */
108 int l
, t
; /* left and top offset */
109 int gm
; /* geometry mask */
114 char *primary
, *clipboard
;
115 struct timespec tclick1
;
116 struct timespec tclick2
;
135 /* Drawing Context */
139 Font font
, bfont
, ifont
, ibfont
;
143 static inline ushort
sixd_to_16bit(int);
144 static int xmakeglyphfontspecs(XftGlyphFontSpec
*, const Glyph
*, int, int, int);
145 static void xdrawglyphfontspecs(const XftGlyphFontSpec
*, Glyph
, int, int, int);
146 static void xdrawglyph(Glyph
, int, int);
147 static void xclear(int, int, int, int);
148 static int xgeommasktogravity(int);
149 static int ximopen(Display
*);
150 static void ximinstantiate(Display
*, XPointer
, XPointer
);
151 static void ximdestroy(XIM
, XPointer
, XPointer
);
152 static int xicdestroy(XIC
, XPointer
, XPointer
);
153 static void xinit(int, int);
154 static void cresize(int, int);
155 static void xresize(int, int);
156 static void xhints(void);
157 static int xloadcolor(int, const char *, Color
*);
158 static int xloadfont(Font
*, FcPattern
*);
159 static void xloadfonts(char *, double);
160 static void xunloadfont(Font
*);
161 static void xunloadfonts(void);
162 static void xsetenv(void);
163 static void xseturgency(int);
164 static int evcol(XEvent
*);
165 static int evrow(XEvent
*);
167 static void expose(XEvent
*);
168 static void visibility(XEvent
*);
169 static void unmap(XEvent
*);
170 static void kpress(XEvent
*);
171 static void cmessage(XEvent
*);
172 static void resize(XEvent
*);
173 static void focus(XEvent
*);
174 static int mouseaction(XEvent
*, uint
);
175 static void brelease(XEvent
*);
176 static void bpress(XEvent
*);
177 static void bmotion(XEvent
*);
178 static void propnotify(XEvent
*);
179 static void selnotify(XEvent
*);
180 static void selclear_(XEvent
*);
181 static void selrequest(XEvent
*);
182 static void setsel(char *, Time
);
183 static void mousesel(XEvent
*, int);
184 static void mousereport(XEvent
*);
185 static char *kmap(KeySym
, uint
);
186 static int match(uint
, uint
);
188 static void run(void);
189 static void usage(void);
191 static void (*handler
[LASTEvent
])(XEvent
*) = {
193 [ClientMessage
] = cmessage
,
194 [ConfigureNotify
] = resize
,
195 [VisibilityNotify
] = visibility
,
196 [UnmapNotify
] = unmap
,
200 [MotionNotify
] = bmotion
,
201 [ButtonPress
] = bpress
,
202 [ButtonRelease
] = brelease
,
204 * Uncomment if you want the selection to disappear when you select something
205 * different in another window.
207 /* [SelectionClear] = selclear_, */
208 [SelectionNotify
] = selnotify
,
210 * PropertyNotify is only turned on when there is some INCR transfer happening
211 * for the selection retrieval.
213 [PropertyNotify
] = propnotify
,
214 [SelectionRequest
] = selrequest
,
220 static XSelection xsel
;
221 static TermWindow win
;
223 /* Font Ring Cache */
237 /* Fontcache is an array now. A new font will be appended to the array. */
238 static Fontcache
*frc
= NULL
;
239 static int frclen
= 0;
240 static int frccap
= 0;
241 static char *usedfont
= NULL
;
242 static double usedfontsize
= 0;
243 static double defaultfontsize
= 0;
245 static char *opt_class
= NULL
;
246 static char **opt_cmd
= NULL
;
247 static char *opt_embed
= NULL
;
248 static char *opt_font
= NULL
;
249 static char *opt_io
= NULL
;
250 static char *opt_line
= NULL
;
251 static char *opt_name
= NULL
;
252 static char *opt_title
= NULL
;
254 static int oldbutton
= 3; /* button event on startup: 3 = release */
257 clipcopy(const Arg
*dummy
)
261 free(xsel
.clipboard
);
262 xsel
.clipboard
= NULL
;
264 if (xsel
.primary
!= NULL
) {
265 xsel
.clipboard
= xstrdup(xsel
.primary
);
266 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
267 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
272 clippaste(const Arg
*dummy
)
276 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
277 XConvertSelection(xw
.dpy
, clipboard
, xsel
.xtarget
, clipboard
,
278 xw
.win
, CurrentTime
);
282 selpaste(const Arg
*dummy
)
284 XConvertSelection(xw
.dpy
, XA_PRIMARY
, xsel
.xtarget
, XA_PRIMARY
,
285 xw
.win
, CurrentTime
);
289 numlock(const Arg
*dummy
)
291 win
.mode
^= MODE_NUMLOCK
;
299 larg
.f
= usedfontsize
+ arg
->f
;
304 zoomabs(const Arg
*arg
)
307 xloadfonts(usedfont
, arg
->f
);
314 zoomreset(const Arg
*arg
)
318 if (defaultfontsize
> 0) {
319 larg
.f
= defaultfontsize
;
325 ttysend(const Arg
*arg
)
327 ttywrite(arg
->s
, strlen(arg
->s
), 1);
333 int x
= e
->xbutton
.x
- borderpx
;
334 LIMIT(x
, 0, win
.tw
- 1);
341 int y
= e
->xbutton
.y
- borderpx
;
342 LIMIT(y
, 0, win
.th
- 1);
347 mousesel(XEvent
*e
, int done
)
349 int type
, seltype
= SEL_REGULAR
;
350 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forcemousemod
);
352 for (type
= 1; type
< LEN(selmasks
); ++type
) {
353 if (match(selmasks
[type
], state
)) {
358 selextend(evcol(e
), evrow(e
), seltype
, done
);
360 setsel(getsel(), e
->xbutton
.time
);
364 mousereport(XEvent
*e
)
366 int len
, x
= evcol(e
), y
= evrow(e
),
367 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
;
372 if (e
->xbutton
.type
== MotionNotify
) {
373 if (x
== ox
&& y
== oy
)
375 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
377 /* MOUSE_MOTION: no reporting if no button is pressed */
378 if (IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
381 button
= oldbutton
+ 32;
385 if (!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
392 if (e
->xbutton
.type
== ButtonPress
) {
396 } else if (e
->xbutton
.type
== ButtonRelease
) {
398 /* MODE_MOUSEX10: no button release reporting */
399 if (IS_SET(MODE_MOUSEX10
))
401 if (button
== 64 || button
== 65)
406 if (!IS_SET(MODE_MOUSEX10
)) {
407 button
+= ((state
& ShiftMask
) ? 4 : 0)
408 + ((state
& Mod4Mask
) ? 8 : 0)
409 + ((state
& ControlMask
) ? 16 : 0);
412 if (IS_SET(MODE_MOUSESGR
)) {
413 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
415 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
416 } else if (x
< 223 && y
< 223) {
417 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
418 32+button
, 32+x
+1, 32+y
+1);
423 ttywrite(buf
, len
, 0);
427 mouseaction(XEvent
*e
, uint release
)
431 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
432 if (ms
->release
== release
&&
433 ms
->button
== e
->xbutton
.button
&&
434 (match(ms
->mod
, e
->xbutton
.state
) || /* exact or forced */
435 match(ms
->mod
, e
->xbutton
.state
& ~forcemousemod
))) {
436 ms
->func(&(ms
->arg
));
450 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
455 if (mouseaction(e
, 0))
458 if (e
->xbutton
.button
== Button1
) {
460 * If the user clicks below predefined timeouts specific
461 * snapping behaviour is exposed.
463 clock_gettime(CLOCK_MONOTONIC
, &now
);
464 if (TIMEDIFF(now
, xsel
.tclick2
) <= tripleclicktimeout
) {
466 } else if (TIMEDIFF(now
, xsel
.tclick1
) <= doubleclicktimeout
) {
471 xsel
.tclick2
= xsel
.tclick1
;
474 selstart(evcol(e
), evrow(e
), snap
);
479 propnotify(XEvent
*e
)
481 XPropertyEvent
*xpev
;
482 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
484 xpev
= &e
->xproperty
;
485 if (xpev
->state
== PropertyNewValue
&&
486 (xpev
->atom
== XA_PRIMARY
||
487 xpev
->atom
== clipboard
)) {
495 ulong nitems
, ofs
, rem
;
497 uchar
*data
, *last
, *repl
;
498 Atom type
, incratom
, property
= None
;
500 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
503 if (e
->type
== SelectionNotify
)
504 property
= e
->xselection
.property
;
505 else if (e
->type
== PropertyNotify
)
506 property
= e
->xproperty
.atom
;
508 if (property
== None
)
512 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
513 BUFSIZ
/4, False
, AnyPropertyType
,
514 &type
, &format
, &nitems
, &rem
,
516 fprintf(stderr
, "Clipboard allocation failed\n");
520 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
522 * If there is some PropertyNotify with no data, then
523 * this is the signal of the selection owner that all
524 * data has been transferred. We won't need to receive
525 * PropertyNotify events anymore.
527 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
528 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
532 if (type
== incratom
) {
534 * Activate the PropertyNotify events so we receive
535 * when the selection owner does send us the next
538 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
539 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
543 * Deleting the property is the transfer start signal.
545 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
551 * Line endings are inconsistent in the terminal and GUI world
552 * copy and pasting. When receiving some selection data,
553 * replace all '\n' with '\r'.
554 * FIXME: Fix the computer world.
557 last
= data
+ nitems
* format
/ 8;
558 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
562 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
563 ttywrite("\033[200~", 6, 0);
564 ttywrite((char *)data
, nitems
* format
/ 8, 1);
565 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
566 ttywrite("\033[201~", 6, 0);
568 /* number of 32-bit chunks returned */
569 ofs
+= nitems
* format
/ 32;
573 * Deleting the property again tells the selection owner to send the
574 * next data chunk in the property.
576 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
592 selrequest(XEvent
*e
)
594 XSelectionRequestEvent
*xsre
;
596 Atom xa_targets
, string
, clipboard
;
599 xsre
= (XSelectionRequestEvent
*) e
;
600 xev
.type
= SelectionNotify
;
601 xev
.requestor
= xsre
->requestor
;
602 xev
.selection
= xsre
->selection
;
603 xev
.target
= xsre
->target
;
604 xev
.time
= xsre
->time
;
605 if (xsre
->property
== None
)
606 xsre
->property
= xsre
->target
;
611 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
612 if (xsre
->target
== xa_targets
) {
613 /* respond with the supported type */
614 string
= xsel
.xtarget
;
615 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
616 XA_ATOM
, 32, PropModeReplace
,
617 (uchar
*) &string
, 1);
618 xev
.property
= xsre
->property
;
619 } else if (xsre
->target
== xsel
.xtarget
|| xsre
->target
== XA_STRING
) {
621 * xith XA_STRING non ascii characters may be incorrect in the
622 * requestor. It is not our problem, use utf8.
624 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
625 if (xsre
->selection
== XA_PRIMARY
) {
626 seltext
= xsel
.primary
;
627 } else if (xsre
->selection
== clipboard
) {
628 seltext
= xsel
.clipboard
;
631 "Unhandled clipboard selection 0x%lx\n",
635 if (seltext
!= NULL
) {
636 XChangeProperty(xsre
->display
, xsre
->requestor
,
637 xsre
->property
, xsre
->target
,
639 (uchar
*)seltext
, strlen(seltext
));
640 xev
.property
= xsre
->property
;
644 /* all done, send a notification to the listener */
645 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
646 fprintf(stderr
, "Error sending SelectionNotify event\n");
650 setsel(char *str
, Time t
)
658 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
659 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
666 setsel(str
, CurrentTime
);
672 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
677 if (mouseaction(e
, 1))
679 if (e
->xbutton
.button
== Button1
)
686 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
695 cresize(int width
, int height
)
704 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
705 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
711 ttyresize(win
.tw
, win
.th
);
715 xresize(int col
, int row
)
717 win
.tw
= col
* win
.cw
;
718 win
.th
= row
* win
.ch
;
720 XFreePixmap(xw
.dpy
, xw
.buf
);
721 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
722 DefaultDepth(xw
.dpy
, xw
.scr
));
723 XftDrawChange(xw
.draw
, xw
.buf
);
724 xclear(0, 0, win
.w
, win
.h
);
726 /* resize to new width */
727 xw
.specbuf
= xrealloc(xw
.specbuf
, col
* sizeof(GlyphFontSpec
));
733 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
737 xloadcolor(int i
, const char *name
, Color
*ncolor
)
739 XRenderColor color
= { .alpha
= 0xffff };
742 if (BETWEEN(i
, 16, 255)) { /* 256 color */
743 if (i
< 6*6*6+16) { /* same colors as xterm */
744 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
745 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
746 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
747 } else { /* greyscale */
748 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
749 color
.green
= color
.blue
= color
.red
;
751 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
752 xw
.cmap
, &color
, ncolor
);
757 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
768 for (cp
= dc
.col
; cp
< &dc
.col
[dc
.collen
]; ++cp
)
769 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
771 dc
.collen
= MAX(LEN(colorname
), 256);
772 dc
.col
= xmalloc(dc
.collen
* sizeof(Color
));
775 for (i
= 0; i
< dc
.collen
; i
++)
776 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
778 die("could not allocate color '%s'\n", colorname
[i
]);
780 die("could not allocate color %d\n", i
);
786 xsetcolorname(int x
, const char *name
)
790 if (!BETWEEN(x
, 0, dc
.collen
))
793 if (!xloadcolor(x
, name
, &ncolor
))
796 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
803 * Absolute coordinates.
806 xclear(int x1
, int y1
, int x2
, int y2
)
809 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
810 x1
, y1
, x2
-x1
, y2
-y1
);
816 XClassHint
class = {opt_name
? opt_name
: termname
,
817 opt_class
? opt_class
: termname
};
818 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
821 sizeh
= XAllocSizeHints();
823 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
| PMinSize
;
824 sizeh
->height
= win
.h
;
825 sizeh
->width
= win
.w
;
826 sizeh
->height_inc
= win
.ch
;
827 sizeh
->width_inc
= win
.cw
;
828 sizeh
->base_height
= 2 * borderpx
;
829 sizeh
->base_width
= 2 * borderpx
;
830 sizeh
->min_height
= win
.ch
+ 2 * borderpx
;
831 sizeh
->min_width
= win
.cw
+ 2 * borderpx
;
833 sizeh
->flags
|= PMaxSize
;
834 sizeh
->min_width
= sizeh
->max_width
= win
.w
;
835 sizeh
->min_height
= sizeh
->max_height
= win
.h
;
837 if (xw
.gm
& (XValue
|YValue
)) {
838 sizeh
->flags
|= USPosition
| PWinGravity
;
841 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
844 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
850 xgeommasktogravity(int mask
)
852 switch (mask
& (XNegative
|YNegative
)) {
854 return NorthWestGravity
;
856 return NorthEastGravity
;
858 return SouthWestGravity
;
861 return SouthEastGravity
;
865 xloadfont(Font
*f
, FcPattern
*pattern
)
867 FcPattern
*configured
;
871 int wantattr
, haveattr
;
874 * Manually configure instead of calling XftMatchFont
875 * so that we can use the configured pattern for
876 * "missing glyph" lookups.
878 configured
= FcPatternDuplicate(pattern
);
882 FcConfigSubstitute(NULL
, configured
, FcMatchPattern
);
883 XftDefaultSubstitute(xw
.dpy
, xw
.scr
, configured
);
885 match
= FcFontMatch(NULL
, configured
, &result
);
887 FcPatternDestroy(configured
);
891 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
892 FcPatternDestroy(configured
);
893 FcPatternDestroy(match
);
897 if ((XftPatternGetInteger(pattern
, "slant", 0, &wantattr
) ==
900 * Check if xft was unable to find a font with the appropriate
901 * slant but gave us one anyway. Try to mitigate.
903 if ((XftPatternGetInteger(f
->match
->pattern
, "slant", 0,
904 &haveattr
) != XftResultMatch
) || haveattr
< wantattr
) {
906 fputs("font slant does not match\n", stderr
);
910 if ((XftPatternGetInteger(pattern
, "weight", 0, &wantattr
) ==
912 if ((XftPatternGetInteger(f
->match
->pattern
, "weight", 0,
913 &haveattr
) != XftResultMatch
) || haveattr
!= wantattr
) {
915 fputs("font weight does not match\n", stderr
);
919 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
920 (const FcChar8
*) ascii_printable
,
921 strlen(ascii_printable
), &extents
);
924 f
->pattern
= configured
;
926 f
->ascent
= f
->match
->ascent
;
927 f
->descent
= f
->match
->descent
;
929 f
->rbearing
= f
->match
->max_advance_width
;
931 f
->height
= f
->ascent
+ f
->descent
;
932 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
938 xloadfonts(char *fontstr
, double fontsize
)
943 if (fontstr
[0] == '-')
944 pattern
= XftXlfdParse(fontstr
, False
, False
);
946 pattern
= FcNameParse((FcChar8
*)fontstr
);
949 die("can't open font %s\n", fontstr
);
952 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
953 FcPatternDel(pattern
, FC_SIZE
);
954 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
955 usedfontsize
= fontsize
;
957 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
959 usedfontsize
= fontval
;
960 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
965 * Default font size is 12, if none given. This is to
966 * have a known usedfontsize value.
968 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
971 defaultfontsize
= usedfontsize
;
974 if (xloadfont(&dc
.font
, pattern
))
975 die("can't open font %s\n", fontstr
);
977 if (usedfontsize
< 0) {
978 FcPatternGetDouble(dc
.font
.match
->pattern
,
979 FC_PIXEL_SIZE
, 0, &fontval
);
980 usedfontsize
= fontval
;
982 defaultfontsize
= fontval
;
985 /* Setting character width and height. */
986 win
.cw
= ceilf(dc
.font
.width
* cwscale
);
987 win
.ch
= ceilf(dc
.font
.height
* chscale
);
989 FcPatternDel(pattern
, FC_SLANT
);
990 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
991 if (xloadfont(&dc
.ifont
, pattern
))
992 die("can't open font %s\n", fontstr
);
994 FcPatternDel(pattern
, FC_WEIGHT
);
995 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
996 if (xloadfont(&dc
.ibfont
, pattern
))
997 die("can't open font %s\n", fontstr
);
999 FcPatternDel(pattern
, FC_SLANT
);
1000 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
1001 if (xloadfont(&dc
.bfont
, pattern
))
1002 die("can't open font %s\n", fontstr
);
1004 FcPatternDestroy(pattern
);
1008 xunloadfont(Font
*f
)
1010 XftFontClose(xw
.dpy
, f
->match
);
1011 FcPatternDestroy(f
->pattern
);
1013 FcFontSetDestroy(f
->set
);
1019 /* Free the loaded fonts in the font cache. */
1021 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
1023 xunloadfont(&dc
.font
);
1024 xunloadfont(&dc
.bfont
);
1025 xunloadfont(&dc
.ifont
);
1026 xunloadfont(&dc
.ibfont
);
1030 ximopen(Display
*dpy
)
1032 XIMCallback imdestroy
= { .client_data
= NULL
, .callback
= ximdestroy
};
1033 XICCallback icdestroy
= { .client_data
= NULL
, .callback
= xicdestroy
};
1035 xw
.ime
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
1036 if (xw
.ime
.xim
== NULL
)
1039 if (XSetIMValues(xw
.ime
.xim
, XNDestroyCallback
, &imdestroy
, NULL
))
1040 fprintf(stderr
, "XSetIMValues: "
1041 "Could not set XNDestroyCallback.\n");
1043 xw
.ime
.spotlist
= XVaCreateNestedList(0, XNSpotLocation
, &xw
.ime
.spot
,
1046 if (xw
.ime
.xic
== NULL
) {
1047 xw
.ime
.xic
= XCreateIC(xw
.ime
.xim
, XNInputStyle
,
1048 XIMPreeditNothing
| XIMStatusNothing
,
1049 XNClientWindow
, xw
.win
,
1050 XNDestroyCallback
, &icdestroy
,
1053 if (xw
.ime
.xic
== NULL
)
1054 fprintf(stderr
, "XCreateIC: Could not create input context.\n");
1060 ximinstantiate(Display
*dpy
, XPointer client
, XPointer call
)
1063 XUnregisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1064 ximinstantiate
, NULL
);
1068 ximdestroy(XIM xim
, XPointer client
, XPointer call
)
1071 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1072 ximinstantiate
, NULL
);
1073 XFree(xw
.ime
.spotlist
);
1077 xicdestroy(XIC xim
, XPointer client
, XPointer call
)
1084 xinit(int cols
, int rows
)
1089 pid_t thispid
= getpid();
1090 XColor xmousefg
, xmousebg
;
1092 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
1093 die("can't open display\n");
1094 xw
.scr
= XDefaultScreen(xw
.dpy
);
1095 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
1099 die("could not init fontconfig.\n");
1101 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
1102 xloadfonts(usedfont
, 0);
1105 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1108 /* adjust fixed window geometry */
1109 win
.w
= 2 * borderpx
+ cols
* win
.cw
;
1110 win
.h
= 2 * borderpx
+ rows
* win
.ch
;
1111 if (xw
.gm
& XNegative
)
1112 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - win
.w
- 2;
1113 if (xw
.gm
& YNegative
)
1114 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - win
.h
- 2;
1117 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
1118 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
1119 xw
.attrs
.bit_gravity
= NorthWestGravity
;
1120 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
| KeyReleaseMask
1121 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1122 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1123 xw
.attrs
.colormap
= xw
.cmap
;
1125 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
1126 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
1127 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
1128 win
.w
, win
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1129 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
1130 | CWEventMask
| CWColormap
, &xw
.attrs
);
1132 memset(&gcvalues
, 0, sizeof(gcvalues
));
1133 gcvalues
.graphics_exposures
= False
;
1134 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
1136 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
1137 DefaultDepth(xw
.dpy
, xw
.scr
));
1138 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
1139 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, win
.w
, win
.h
);
1141 /* font spec buffer */
1142 xw
.specbuf
= xmalloc(cols
* sizeof(GlyphFontSpec
));
1144 /* Xft rendering context */
1145 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
1148 if (!ximopen(xw
.dpy
)) {
1149 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1150 ximinstantiate
, NULL
);
1153 /* white cursor, black outline */
1154 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
1155 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1157 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
1158 xmousefg
.red
= 0xffff;
1159 xmousefg
.green
= 0xffff;
1160 xmousefg
.blue
= 0xffff;
1163 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
1164 xmousebg
.red
= 0x0000;
1165 xmousebg
.green
= 0x0000;
1166 xmousebg
.blue
= 0x0000;
1169 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
1171 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1172 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
1173 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
1174 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
1176 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
1177 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
1178 PropModeReplace
, (uchar
*)&thispid
, 1);
1180 win
.mode
= MODE_NUMLOCK
;
1183 XMapWindow(xw
.dpy
, xw
.win
);
1184 XSync(xw
.dpy
, False
);
1186 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick1
);
1187 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick2
);
1188 xsel
.primary
= NULL
;
1189 xsel
.clipboard
= NULL
;
1190 xsel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
1191 if (xsel
.xtarget
== None
)
1192 xsel
.xtarget
= XA_STRING
;
1196 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
1198 float winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
, xp
, yp
;
1199 ushort mode
, prevmode
= USHRT_MAX
;
1200 Font
*font
= &dc
.font
;
1201 int frcflags
= FRC_NORMAL
;
1202 float runewidth
= win
.cw
;
1206 FcPattern
*fcpattern
, *fontpattern
;
1207 FcFontSet
*fcsets
[] = { NULL
};
1208 FcCharSet
*fccharset
;
1209 int i
, f
, numspecs
= 0;
1211 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
1212 /* Fetch rune and mode for current glyph. */
1214 mode
= glyphs
[i
].mode
;
1216 /* Skip dummy wide-character spacing. */
1217 if (mode
== ATTR_WDUMMY
)
1220 /* Determine font for glyph if different from previous glyph. */
1221 if (prevmode
!= mode
) {
1224 frcflags
= FRC_NORMAL
;
1225 runewidth
= win
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
1226 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
1228 frcflags
= FRC_ITALICBOLD
;
1229 } else if (mode
& ATTR_ITALIC
) {
1231 frcflags
= FRC_ITALIC
;
1232 } else if (mode
& ATTR_BOLD
) {
1234 frcflags
= FRC_BOLD
;
1236 yp
= winy
+ font
->ascent
;
1239 /* Lookup character index with default font. */
1240 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
1242 specs
[numspecs
].font
= font
->match
;
1243 specs
[numspecs
].glyph
= glyphidx
;
1244 specs
[numspecs
].x
= (short)xp
;
1245 specs
[numspecs
].y
= (short)yp
;
1251 /* Fallback on font cache, search the font cache for match. */
1252 for (f
= 0; f
< frclen
; f
++) {
1253 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
1254 /* Everything correct. */
1255 if (glyphidx
&& frc
[f
].flags
== frcflags
)
1257 /* We got a default font for a not found glyph. */
1258 if (!glyphidx
&& frc
[f
].flags
== frcflags
1259 && frc
[f
].unicodep
== rune
) {
1264 /* Nothing was found. Use fontconfig to find matching font. */
1267 font
->set
= FcFontSort(0, font
->pattern
,
1269 fcsets
[0] = font
->set
;
1272 * Nothing was found in the cache. Now use
1273 * some dozen of Fontconfig calls to get the
1274 * font for one single character.
1276 * Xft and fontconfig are design failures.
1278 fcpattern
= FcPatternDuplicate(font
->pattern
);
1279 fccharset
= FcCharSetCreate();
1281 FcCharSetAddChar(fccharset
, rune
);
1282 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
1284 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
1286 FcConfigSubstitute(0, fcpattern
,
1288 FcDefaultSubstitute(fcpattern
);
1290 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
1293 /* Allocate memory for the new cache entry. */
1294 if (frclen
>= frccap
) {
1296 frc
= xrealloc(frc
, frccap
* sizeof(Fontcache
));
1299 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
1301 if (!frc
[frclen
].font
)
1302 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1304 frc
[frclen
].flags
= frcflags
;
1305 frc
[frclen
].unicodep
= rune
;
1307 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
1312 FcPatternDestroy(fcpattern
);
1313 FcCharSetDestroy(fccharset
);
1316 specs
[numspecs
].font
= frc
[f
].font
;
1317 specs
[numspecs
].glyph
= glyphidx
;
1318 specs
[numspecs
].x
= (short)xp
;
1319 specs
[numspecs
].y
= (short)yp
;
1328 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
1330 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
1331 int winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
,
1332 width
= charlen
* win
.cw
;
1333 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
1334 XRenderColor colfg
, colbg
;
1337 /* Fallback on color display for attributes not supported by the font */
1338 if (base
.mode
& ATTR_ITALIC
&& base
.mode
& ATTR_BOLD
) {
1339 if (dc
.ibfont
.badslant
|| dc
.ibfont
.badweight
)
1340 base
.fg
= defaultattr
;
1341 } else if ((base
.mode
& ATTR_ITALIC
&& dc
.ifont
.badslant
) ||
1342 (base
.mode
& ATTR_BOLD
&& dc
.bfont
.badweight
)) {
1343 base
.fg
= defaultattr
;
1346 if (IS_TRUECOL(base
.fg
)) {
1347 colfg
.alpha
= 0xffff;
1348 colfg
.red
= TRUERED(base
.fg
);
1349 colfg
.green
= TRUEGREEN(base
.fg
);
1350 colfg
.blue
= TRUEBLUE(base
.fg
);
1351 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
1354 fg
= &dc
.col
[base
.fg
];
1357 if (IS_TRUECOL(base
.bg
)) {
1358 colbg
.alpha
= 0xffff;
1359 colbg
.green
= TRUEGREEN(base
.bg
);
1360 colbg
.red
= TRUERED(base
.bg
);
1361 colbg
.blue
= TRUEBLUE(base
.bg
);
1362 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
1365 bg
= &dc
.col
[base
.bg
];
1368 /* Change basic system colors [0-7] to bright system colors [8-15] */
1369 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
1370 fg
= &dc
.col
[base
.fg
+ 8];
1372 if (IS_SET(MODE_REVERSE
)) {
1373 if (fg
== &dc
.col
[defaultfg
]) {
1374 fg
= &dc
.col
[defaultbg
];
1376 colfg
.red
= ~fg
->color
.red
;
1377 colfg
.green
= ~fg
->color
.green
;
1378 colfg
.blue
= ~fg
->color
.blue
;
1379 colfg
.alpha
= fg
->color
.alpha
;
1380 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
1385 if (bg
== &dc
.col
[defaultbg
]) {
1386 bg
= &dc
.col
[defaultfg
];
1388 colbg
.red
= ~bg
->color
.red
;
1389 colbg
.green
= ~bg
->color
.green
;
1390 colbg
.blue
= ~bg
->color
.blue
;
1391 colbg
.alpha
= bg
->color
.alpha
;
1392 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
1398 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
1399 colfg
.red
= fg
->color
.red
/ 2;
1400 colfg
.green
= fg
->color
.green
/ 2;
1401 colfg
.blue
= fg
->color
.blue
/ 2;
1402 colfg
.alpha
= fg
->color
.alpha
;
1403 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
1407 if (base
.mode
& ATTR_REVERSE
) {
1413 if (base
.mode
& ATTR_BLINK
&& win
.mode
& MODE_BLINK
)
1416 if (base
.mode
& ATTR_INVISIBLE
)
1419 /* Intelligent cleaning up of the borders. */
1421 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
1423 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: 0));
1425 if (winx
+ width
>= borderpx
+ win
.tw
) {
1426 xclear(winx
+ width
, (y
== 0)? 0 : winy
, win
.w
,
1427 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: (winy
+ win
.ch
)));
1430 xclear(winx
, 0, winx
+ width
, borderpx
);
1431 if (winy
+ win
.ch
>= borderpx
+ win
.th
)
1432 xclear(winx
, winy
+ win
.ch
, winx
+ width
, win
.h
);
1434 /* Clean up the region we want to draw to. */
1435 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, win
.ch
);
1437 /* Set the clip region because Xft is sometimes dirty. */
1442 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
1444 /* Render the glyphs. */
1445 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
1447 /* Render underline and strikethrough. */
1448 if (base
.mode
& ATTR_UNDERLINE
) {
1449 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
1453 if (base
.mode
& ATTR_STRUCK
) {
1454 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
1458 /* Reset clip to none. */
1459 XftDrawSetClip(xw
.draw
, 0);
1463 xdrawglyph(Glyph g
, int x
, int y
)
1466 XftGlyphFontSpec spec
;
1468 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
1469 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
1473 xdrawcursor(int cx
, int cy
, Glyph g
, int ox
, int oy
, Glyph og
)
1477 /* remove the old cursor */
1478 if (selected(ox
, oy
))
1479 og
.mode
^= ATTR_REVERSE
;
1480 xdrawglyph(og
, ox
, oy
);
1482 if (IS_SET(MODE_HIDE
))
1486 * Select the right color for the right mode.
1488 g
.mode
&= ATTR_BOLD
|ATTR_ITALIC
|ATTR_UNDERLINE
|ATTR_STRUCK
|ATTR_WIDE
;
1490 if (IS_SET(MODE_REVERSE
)) {
1491 g
.mode
|= ATTR_REVERSE
;
1493 if (selected(cx
, cy
)) {
1494 drawcol
= dc
.col
[defaultcs
];
1497 drawcol
= dc
.col
[defaultrcs
];
1501 if (selected(cx
, cy
)) {
1508 drawcol
= dc
.col
[g
.bg
];
1511 /* draw the new one */
1512 if (IS_SET(MODE_FOCUSED
)) {
1513 switch (win
.cursor
) {
1514 case 7: /* st extension: snowman (U+2603) */
1516 case 0: /* Blinking Block */
1517 case 1: /* Blinking Block (Default) */
1518 case 2: /* Steady Block */
1519 xdrawglyph(g
, cx
, cy
);
1521 case 3: /* Blinking Underline */
1522 case 4: /* Steady Underline */
1523 XftDrawRect(xw
.draw
, &drawcol
,
1524 borderpx
+ cx
* win
.cw
,
1525 borderpx
+ (cy
+ 1) * win
.ch
- \
1527 win
.cw
, cursorthickness
);
1529 case 5: /* Blinking bar */
1530 case 6: /* Steady bar */
1531 XftDrawRect(xw
.draw
, &drawcol
,
1532 borderpx
+ cx
* win
.cw
,
1533 borderpx
+ cy
* win
.ch
,
1534 cursorthickness
, win
.ch
);
1538 XftDrawRect(xw
.draw
, &drawcol
,
1539 borderpx
+ cx
* win
.cw
,
1540 borderpx
+ cy
* win
.ch
,
1542 XftDrawRect(xw
.draw
, &drawcol
,
1543 borderpx
+ cx
* win
.cw
,
1544 borderpx
+ cy
* win
.ch
,
1546 XftDrawRect(xw
.draw
, &drawcol
,
1547 borderpx
+ (cx
+ 1) * win
.cw
- 1,
1548 borderpx
+ cy
* win
.ch
,
1550 XftDrawRect(xw
.draw
, &drawcol
,
1551 borderpx
+ cx
* win
.cw
,
1552 borderpx
+ (cy
+ 1) * win
.ch
- 1,
1560 char buf
[sizeof(long) * 8 + 1];
1562 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1563 setenv("WINDOWID", buf
, 1);
1570 DEFAULT(p
, opt_title
);
1572 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1574 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
1575 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
1582 return IS_SET(MODE_VISIBLE
);
1586 xdrawline(Line line
, int x1
, int y1
, int x2
)
1588 int i
, x
, ox
, numspecs
;
1590 XftGlyphFontSpec
*specs
= xw
.specbuf
;
1592 numspecs
= xmakeglyphfontspecs(specs
, &line
[x1
], x2
- x1
, x1
, y1
);
1594 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
1596 if (new.mode
== ATTR_WDUMMY
)
1598 if (selected(x
, y1
))
1599 new.mode
^= ATTR_REVERSE
;
1600 if (i
> 0 && ATTRCMP(base
, new)) {
1601 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1613 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1619 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, win
.w
,
1621 XSetForeground(xw
.dpy
, dc
.gc
,
1622 dc
.col
[IS_SET(MODE_REVERSE
)?
1623 defaultfg
: defaultbg
].pixel
);
1627 xximspot(int x
, int y
)
1629 if (xw
.ime
.xic
== NULL
)
1632 xw
.ime
.spot
.x
= borderpx
+ x
* win
.cw
;
1633 xw
.ime
.spot
.y
= borderpx
+ (y
+ 1) * win
.ch
;
1635 XSetICValues(xw
.ime
.xic
, XNPreeditAttributes
, xw
.ime
.spotlist
, NULL
);
1645 visibility(XEvent
*ev
)
1647 XVisibilityEvent
*e
= &ev
->xvisibility
;
1649 MODBIT(win
.mode
, e
->state
!= VisibilityFullyObscured
, MODE_VISIBLE
);
1655 win
.mode
&= ~MODE_VISIBLE
;
1659 xsetpointermotion(int set
)
1661 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
1662 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
1666 xsetmode(int set
, unsigned int flags
)
1668 int mode
= win
.mode
;
1669 MODBIT(win
.mode
, set
, flags
);
1670 if ((win
.mode
& MODE_REVERSE
) != (mode
& MODE_REVERSE
))
1675 xsetcursor(int cursor
)
1678 if (!BETWEEN(cursor
, 0, 6))
1680 win
.cursor
= cursor
;
1685 xseturgency(int add
)
1687 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1689 MODBIT(h
->flags
, add
, XUrgencyHint
);
1690 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1697 if (!(IS_SET(MODE_FOCUSED
)))
1700 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
1706 XFocusChangeEvent
*e
= &ev
->xfocus
;
1708 if (e
->mode
== NotifyGrab
)
1711 if (ev
->type
== FocusIn
) {
1713 XSetICFocus(xw
.ime
.xic
);
1714 win
.mode
|= MODE_FOCUSED
;
1716 if (IS_SET(MODE_FOCUS
))
1717 ttywrite("\033[I", 3, 0);
1720 XUnsetICFocus(xw
.ime
.xic
);
1721 win
.mode
&= ~MODE_FOCUSED
;
1722 if (IS_SET(MODE_FOCUS
))
1723 ttywrite("\033[O", 3, 0);
1728 match(uint mask
, uint state
)
1730 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
1734 kmap(KeySym k
, uint state
)
1739 /* Check for mapped keys out of X11 function keys. */
1740 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
1741 if (mappedkeys
[i
] == k
)
1744 if (i
== LEN(mappedkeys
)) {
1745 if ((k
& 0xFFFF) < 0xFD00)
1749 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
1753 if (!match(kp
->mask
, state
))
1756 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
1758 if (IS_SET(MODE_NUMLOCK
) && kp
->appkey
== 2)
1761 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
1773 XKeyEvent
*e
= &ev
->xkey
;
1775 char buf
[64], *customkey
;
1781 if (IS_SET(MODE_KBDLOCK
))
1785 len
= XmbLookupString(xw
.ime
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
1787 len
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
1789 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
1790 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
1791 bp
->func(&(bp
->arg
));
1796 /* 2. custom keys from config.h */
1797 if ((customkey
= kmap(ksym
, e
->state
))) {
1798 ttywrite(customkey
, strlen(customkey
), 1);
1802 /* 3. composed string from input method */
1805 if (len
== 1 && e
->state
& Mod1Mask
) {
1806 if (IS_SET(MODE_8BIT
)) {
1809 len
= utf8encode(c
, buf
);
1817 ttywrite(buf
, len
, 1);
1825 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1827 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1828 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1829 win
.mode
|= MODE_FOCUSED
;
1831 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1832 win
.mode
&= ~MODE_FOCUSED
;
1834 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
1843 if (e
->xconfigure
.width
== win
.w
&& e
->xconfigure
.height
== win
.h
)
1846 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
1853 int w
= win
.w
, h
= win
.h
;
1855 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
1857 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
1860 /* Waiting for window mapping */
1862 XNextEvent(xw
.dpy
, &ev
);
1864 * This XFilterEvent call is required because of XOpenIM. It
1865 * does filter out the key event and some client message for
1866 * the input method too.
1868 if (XFilterEvent(&ev
, None
))
1870 if (ev
.type
== ConfigureNotify
) {
1871 w
= ev
.xconfigure
.width
;
1872 h
= ev
.xconfigure
.height
;
1874 } while (ev
.type
!= MapNotify
);
1876 ttyfd
= ttynew(opt_line
, shell
, opt_io
, opt_cmd
);
1879 clock_gettime(CLOCK_MONOTONIC
, &last
);
1882 for (xev
= actionfps
;;) {
1884 FD_SET(ttyfd
, &rfd
);
1887 if (pselect(MAX(xfd
, ttyfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
1890 die("select failed: %s\n", strerror(errno
));
1892 if (FD_ISSET(ttyfd
, &rfd
)) {
1895 blinkset
= tattrset(ATTR_BLINK
);
1897 MODBIT(win
.mode
, 0, MODE_BLINK
);
1901 if (FD_ISSET(xfd
, &rfd
))
1904 clock_gettime(CLOCK_MONOTONIC
, &now
);
1905 drawtimeout
.tv_sec
= 0;
1906 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
1910 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
1911 tsetdirtattr(ATTR_BLINK
);
1912 win
.mode
^= MODE_BLINK
;
1916 deltatime
= TIMEDIFF(now
, last
);
1917 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
1923 while (XPending(xw
.dpy
)) {
1924 XNextEvent(xw
.dpy
, &ev
);
1925 if (XFilterEvent(&ev
, None
))
1927 if (handler
[ev
.type
])
1928 (handler
[ev
.type
])(&ev
);
1934 if (xev
&& !FD_ISSET(xfd
, &rfd
))
1936 if (!FD_ISSET(ttyfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
1938 if (TIMEDIFF(now
, lastblink
) \
1940 drawtimeout
.tv_nsec
= 1000;
1942 drawtimeout
.tv_nsec
= (1E6
* \
1947 drawtimeout
.tv_sec
= \
1948 drawtimeout
.tv_nsec
/ 1E9
;
1949 drawtimeout
.tv_nsec
%= (long)1E9
;
1961 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
1962 " [-n name] [-o file]\n"
1963 " [-T title] [-t title] [-w windowid]"
1964 " [[-e] command [args ...]]\n"
1965 " %s [-aiv] [-c class] [-f font] [-g geometry]"
1966 " [-n name] [-o file]\n"
1967 " [-T title] [-t title] [-w windowid] -l line"
1968 " [stty_args ...]\n", argv0
, argv0
);
1972 main(int argc
, char *argv
[])
1976 win
.cursor
= cursorshape
;
1983 opt_class
= EARGF(usage());
1990 opt_font
= EARGF(usage());
1993 xw
.gm
= XParseGeometry(EARGF(usage()),
1994 &xw
.l
, &xw
.t
, &cols
, &rows
);
2000 opt_io
= EARGF(usage());
2003 opt_line
= EARGF(usage());
2006 opt_name
= EARGF(usage());
2010 opt_title
= EARGF(usage());
2013 opt_embed
= EARGF(usage());
2016 die("%s " VERSION
"\n", argv0
);
2023 if (argc
> 0) /* eat all remaining arguments */
2027 opt_title
= (opt_line
|| !opt_cmd
) ? "st" : opt_cmd
[0];
2029 setlocale(LC_CTYPE
, "");
2030 XSetLocaleModifiers("");
2031 cols
= MAX(cols
, 1);
2032 rows
= MAX(rows
, 1);