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
*);
42 /* three-valued logic variables: 0 indifferent, 1 on, -1 off */
43 signed char appkey
; /* application keypad */
44 signed char appcursor
; /* application cursor */
48 #define XK_ANY_MOD UINT_MAX
50 #define XK_SWITCH_MOD (1<<13)
52 /* function definitions used in config.h */
53 static void clipcopy(const Arg
*);
54 static void clippaste(const Arg
*);
55 static void numlock(const Arg
*);
56 static void selpaste(const Arg
*);
57 static void zoom(const Arg
*);
58 static void zoomabs(const Arg
*);
59 static void zoomreset(const Arg
*);
60 static void ttysend(const Arg
*);
62 /* config.h for applying patches and the configuration. */
66 #define XEMBED_FOCUS_IN 4
67 #define XEMBED_FOCUS_OUT 5
70 #define IS_SET(flag) ((win.mode & (flag)) != 0)
71 #define TRUERED(x) (((x) & 0xff0000) >> 8)
72 #define TRUEGREEN(x) (((x) & 0xff00))
73 #define TRUEBLUE(x) (((x) & 0xff) << 8)
75 typedef XftDraw
*Draw
;
76 typedef XftColor Color
;
77 typedef XftGlyphFontSpec GlyphFontSpec
;
79 /* Purely graphic info */
81 int tw
, th
; /* tty width and height */
82 int w
, h
; /* window width and height */
83 int ch
; /* char height */
84 int cw
; /* char width */
85 int mode
; /* window state/mode flags */
86 int cursor
; /* cursor style */
94 GlyphFontSpec
*specbuf
; /* font spec buffer used for rendering */
95 Atom xembed
, wmdeletewin
, netwmname
, netwmpid
;
100 XSetWindowAttributes attrs
;
102 int isfixed
; /* is fixed geometry? */
103 int l
, t
; /* left and top offset */
104 int gm
; /* geometry mask */
109 char *primary
, *clipboard
;
110 struct timespec tclick1
;
111 struct timespec tclick2
;
130 /* Drawing Context */
134 Font font
, bfont
, ifont
, ibfont
;
138 static inline ushort
sixd_to_16bit(int);
139 static int xmakeglyphfontspecs(XftGlyphFontSpec
*, const Glyph
*, int, int, int);
140 static void xdrawglyphfontspecs(const XftGlyphFontSpec
*, Glyph
, int, int, int);
141 static void xdrawglyph(Glyph
, int, int);
142 static void xclear(int, int, int, int);
143 static int xgeommasktogravity(int);
144 static void ximopen(Display
*);
145 static void ximinstantiate(Display
*, XPointer
, XPointer
);
146 static void ximdestroy(XIM
, XPointer
, XPointer
);
147 static void xinit(int, int);
148 static void cresize(int, int);
149 static void xresize(int, int);
150 static void xhints(void);
151 static int xloadcolor(int, const char *, Color
*);
152 static int xloadfont(Font
*, FcPattern
*);
153 static void xloadfonts(char *, double);
154 static void xunloadfont(Font
*);
155 static void xunloadfonts(void);
156 static void xsetenv(void);
157 static void xseturgency(int);
158 static int evcol(XEvent
*);
159 static int evrow(XEvent
*);
161 static void expose(XEvent
*);
162 static void visibility(XEvent
*);
163 static void unmap(XEvent
*);
164 static void kpress(XEvent
*);
165 static void cmessage(XEvent
*);
166 static void resize(XEvent
*);
167 static void focus(XEvent
*);
168 static void brelease(XEvent
*);
169 static void bpress(XEvent
*);
170 static void bmotion(XEvent
*);
171 static void propnotify(XEvent
*);
172 static void selnotify(XEvent
*);
173 static void selclear_(XEvent
*);
174 static void selrequest(XEvent
*);
175 static void setsel(char *, Time
);
176 static void mousesel(XEvent
*, int);
177 static void mousereport(XEvent
*);
178 static char *kmap(KeySym
, uint
);
179 static int match(uint
, uint
);
181 static void run(void);
182 static void usage(void);
184 static void (*handler
[LASTEvent
])(XEvent
*) = {
186 [ClientMessage
] = cmessage
,
187 [ConfigureNotify
] = resize
,
188 [VisibilityNotify
] = visibility
,
189 [UnmapNotify
] = unmap
,
193 [MotionNotify
] = bmotion
,
194 [ButtonPress
] = bpress
,
195 [ButtonRelease
] = brelease
,
197 * Uncomment if you want the selection to disappear when you select something
198 * different in another window.
200 /* [SelectionClear] = selclear_, */
201 [SelectionNotify
] = selnotify
,
203 * PropertyNotify is only turned on when there is some INCR transfer happening
204 * for the selection retrieval.
206 [PropertyNotify
] = propnotify
,
207 [SelectionRequest
] = selrequest
,
213 static XSelection xsel
;
214 static TermWindow win
;
216 /* Font Ring Cache */
230 /* Fontcache is an array now. A new font will be appended to the array. */
231 static Fontcache
*frc
= NULL
;
232 static int frclen
= 0;
233 static int frccap
= 0;
234 static char *usedfont
= NULL
;
235 static double usedfontsize
= 0;
236 static double defaultfontsize
= 0;
238 static char *opt_class
= NULL
;
239 static char **opt_cmd
= NULL
;
240 static char *opt_embed
= NULL
;
241 static char *opt_font
= NULL
;
242 static char *opt_io
= NULL
;
243 static char *opt_line
= NULL
;
244 static char *opt_name
= NULL
;
245 static char *opt_title
= NULL
;
247 static int oldbutton
= 3; /* button event on startup: 3 = release */
250 clipcopy(const Arg
*dummy
)
254 free(xsel
.clipboard
);
255 xsel
.clipboard
= NULL
;
257 if (xsel
.primary
!= NULL
) {
258 xsel
.clipboard
= xstrdup(xsel
.primary
);
259 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
260 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
265 clippaste(const Arg
*dummy
)
269 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
270 XConvertSelection(xw
.dpy
, clipboard
, xsel
.xtarget
, clipboard
,
271 xw
.win
, CurrentTime
);
275 selpaste(const Arg
*dummy
)
277 XConvertSelection(xw
.dpy
, XA_PRIMARY
, xsel
.xtarget
, XA_PRIMARY
,
278 xw
.win
, CurrentTime
);
282 numlock(const Arg
*dummy
)
284 win
.mode
^= MODE_NUMLOCK
;
292 larg
.f
= usedfontsize
+ arg
->f
;
297 zoomabs(const Arg
*arg
)
300 xloadfonts(usedfont
, arg
->f
);
307 zoomreset(const Arg
*arg
)
311 if (defaultfontsize
> 0) {
312 larg
.f
= defaultfontsize
;
318 ttysend(const Arg
*arg
)
320 ttywrite(arg
->s
, strlen(arg
->s
), 1);
326 int x
= e
->xbutton
.x
- borderpx
;
327 LIMIT(x
, 0, win
.tw
- 1);
334 int y
= e
->xbutton
.y
- borderpx
;
335 LIMIT(y
, 0, win
.th
- 1);
340 mousesel(XEvent
*e
, int done
)
342 int type
, seltype
= SEL_REGULAR
;
343 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forcemousemod
);
345 for (type
= 1; type
< LEN(selmasks
); ++type
) {
346 if (match(selmasks
[type
], state
)) {
351 selextend(evcol(e
), evrow(e
), seltype
, done
);
353 setsel(getsel(), e
->xbutton
.time
);
357 mousereport(XEvent
*e
)
359 int len
, x
= evcol(e
), y
= evrow(e
),
360 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
;
365 if (e
->xbutton
.type
== MotionNotify
) {
366 if (x
== ox
&& y
== oy
)
368 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
370 /* MOUSE_MOTION: no reporting if no button is pressed */
371 if (IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
374 button
= oldbutton
+ 32;
378 if (!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
385 if (e
->xbutton
.type
== ButtonPress
) {
389 } else if (e
->xbutton
.type
== ButtonRelease
) {
391 /* MODE_MOUSEX10: no button release reporting */
392 if (IS_SET(MODE_MOUSEX10
))
394 if (button
== 64 || button
== 65)
399 if (!IS_SET(MODE_MOUSEX10
)) {
400 button
+= ((state
& ShiftMask
) ? 4 : 0)
401 + ((state
& Mod4Mask
) ? 8 : 0)
402 + ((state
& ControlMask
) ? 16 : 0);
405 if (IS_SET(MODE_MOUSESGR
)) {
406 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
408 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
409 } else if (x
< 223 && y
< 223) {
410 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
411 32+button
, 32+x
+1, 32+y
+1);
416 ttywrite(buf
, len
, 0);
426 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
431 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
432 if (e
->xbutton
.button
== ms
->button
&&
433 match(ms
->mod
, e
->xbutton
.state
& ~forcemousemod
)) {
434 ms
->func(&(ms
->arg
));
439 if (e
->xbutton
.button
== Button1
) {
441 * If the user clicks below predefined timeouts specific
442 * snapping behaviour is exposed.
444 clock_gettime(CLOCK_MONOTONIC
, &now
);
445 if (TIMEDIFF(now
, xsel
.tclick2
) <= tripleclicktimeout
) {
447 } else if (TIMEDIFF(now
, xsel
.tclick1
) <= doubleclicktimeout
) {
452 xsel
.tclick2
= xsel
.tclick1
;
455 selstart(evcol(e
), evrow(e
), snap
);
460 propnotify(XEvent
*e
)
462 XPropertyEvent
*xpev
;
463 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
465 xpev
= &e
->xproperty
;
466 if (xpev
->state
== PropertyNewValue
&&
467 (xpev
->atom
== XA_PRIMARY
||
468 xpev
->atom
== clipboard
)) {
476 ulong nitems
, ofs
, rem
;
478 uchar
*data
, *last
, *repl
;
479 Atom type
, incratom
, property
= None
;
481 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
484 if (e
->type
== SelectionNotify
)
485 property
= e
->xselection
.property
;
486 else if (e
->type
== PropertyNotify
)
487 property
= e
->xproperty
.atom
;
489 if (property
== None
)
493 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
494 BUFSIZ
/4, False
, AnyPropertyType
,
495 &type
, &format
, &nitems
, &rem
,
497 fprintf(stderr
, "Clipboard allocation failed\n");
501 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
503 * If there is some PropertyNotify with no data, then
504 * this is the signal of the selection owner that all
505 * data has been transferred. We won't need to receive
506 * PropertyNotify events anymore.
508 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
509 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
513 if (type
== incratom
) {
515 * Activate the PropertyNotify events so we receive
516 * when the selection owner does send us the next
519 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
520 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
524 * Deleting the property is the transfer start signal.
526 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
532 * Line endings are inconsistent in the terminal and GUI world
533 * copy and pasting. When receiving some selection data,
534 * replace all '\n' with '\r'.
535 * FIXME: Fix the computer world.
538 last
= data
+ nitems
* format
/ 8;
539 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
543 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
544 ttywrite("\033[200~", 6, 0);
545 ttywrite((char *)data
, nitems
* format
/ 8, 1);
546 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
547 ttywrite("\033[201~", 6, 0);
549 /* number of 32-bit chunks returned */
550 ofs
+= nitems
* format
/ 32;
554 * Deleting the property again tells the selection owner to send the
555 * next data chunk in the property.
557 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
573 selrequest(XEvent
*e
)
575 XSelectionRequestEvent
*xsre
;
577 Atom xa_targets
, string
, clipboard
;
580 xsre
= (XSelectionRequestEvent
*) e
;
581 xev
.type
= SelectionNotify
;
582 xev
.requestor
= xsre
->requestor
;
583 xev
.selection
= xsre
->selection
;
584 xev
.target
= xsre
->target
;
585 xev
.time
= xsre
->time
;
586 if (xsre
->property
== None
)
587 xsre
->property
= xsre
->target
;
592 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
593 if (xsre
->target
== xa_targets
) {
594 /* respond with the supported type */
595 string
= xsel
.xtarget
;
596 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
597 XA_ATOM
, 32, PropModeReplace
,
598 (uchar
*) &string
, 1);
599 xev
.property
= xsre
->property
;
600 } else if (xsre
->target
== xsel
.xtarget
|| xsre
->target
== XA_STRING
) {
602 * xith XA_STRING non ascii characters may be incorrect in the
603 * requestor. It is not our problem, use utf8.
605 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
606 if (xsre
->selection
== XA_PRIMARY
) {
607 seltext
= xsel
.primary
;
608 } else if (xsre
->selection
== clipboard
) {
609 seltext
= xsel
.clipboard
;
612 "Unhandled clipboard selection 0x%lx\n",
616 if (seltext
!= NULL
) {
617 XChangeProperty(xsre
->display
, xsre
->requestor
,
618 xsre
->property
, xsre
->target
,
620 (uchar
*)seltext
, strlen(seltext
));
621 xev
.property
= xsre
->property
;
625 /* all done, send a notification to the listener */
626 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
627 fprintf(stderr
, "Error sending SelectionNotify event\n");
631 setsel(char *str
, Time t
)
639 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
640 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
647 setsel(str
, CurrentTime
);
653 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
658 if (e
->xbutton
.button
== Button2
)
660 else if (e
->xbutton
.button
== Button1
)
667 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forcemousemod
)) {
676 cresize(int width
, int height
)
685 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
686 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
692 ttyresize(win
.tw
, win
.th
);
696 xresize(int col
, int row
)
698 win
.tw
= col
* win
.cw
;
699 win
.th
= row
* win
.ch
;
701 XFreePixmap(xw
.dpy
, xw
.buf
);
702 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
703 DefaultDepth(xw
.dpy
, xw
.scr
));
704 XftDrawChange(xw
.draw
, xw
.buf
);
705 xclear(0, 0, win
.w
, win
.h
);
707 /* resize to new width */
708 xw
.specbuf
= xrealloc(xw
.specbuf
, col
* sizeof(GlyphFontSpec
));
714 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
718 xloadcolor(int i
, const char *name
, Color
*ncolor
)
720 XRenderColor color
= { .alpha
= 0xffff };
723 if (BETWEEN(i
, 16, 255)) { /* 256 color */
724 if (i
< 6*6*6+16) { /* same colors as xterm */
725 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
726 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
727 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
728 } else { /* greyscale */
729 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
730 color
.green
= color
.blue
= color
.red
;
732 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
733 xw
.cmap
, &color
, ncolor
);
738 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
749 for (cp
= dc
.col
; cp
< &dc
.col
[dc
.collen
]; ++cp
)
750 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
752 dc
.collen
= MAX(LEN(colorname
), 256);
753 dc
.col
= xmalloc(dc
.collen
* sizeof(Color
));
756 for (i
= 0; i
< dc
.collen
; i
++)
757 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
759 die("could not allocate color '%s'\n", colorname
[i
]);
761 die("could not allocate color %d\n", i
);
767 xsetcolorname(int x
, const char *name
)
771 if (!BETWEEN(x
, 0, dc
.collen
))
774 if (!xloadcolor(x
, name
, &ncolor
))
777 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
784 * Absolute coordinates.
787 xclear(int x1
, int y1
, int x2
, int y2
)
790 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
791 x1
, y1
, x2
-x1
, y2
-y1
);
797 XClassHint
class = {opt_name
? opt_name
: termname
,
798 opt_class
? opt_class
: termname
};
799 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
802 sizeh
= XAllocSizeHints();
804 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
| PMinSize
;
805 sizeh
->height
= win
.h
;
806 sizeh
->width
= win
.w
;
807 sizeh
->height_inc
= win
.ch
;
808 sizeh
->width_inc
= win
.cw
;
809 sizeh
->base_height
= 2 * borderpx
;
810 sizeh
->base_width
= 2 * borderpx
;
811 sizeh
->min_height
= win
.ch
+ 2 * borderpx
;
812 sizeh
->min_width
= win
.cw
+ 2 * borderpx
;
814 sizeh
->flags
|= PMaxSize
;
815 sizeh
->min_width
= sizeh
->max_width
= win
.w
;
816 sizeh
->min_height
= sizeh
->max_height
= win
.h
;
818 if (xw
.gm
& (XValue
|YValue
)) {
819 sizeh
->flags
|= USPosition
| PWinGravity
;
822 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
825 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
831 xgeommasktogravity(int mask
)
833 switch (mask
& (XNegative
|YNegative
)) {
835 return NorthWestGravity
;
837 return NorthEastGravity
;
839 return SouthWestGravity
;
842 return SouthEastGravity
;
846 xloadfont(Font
*f
, FcPattern
*pattern
)
848 FcPattern
*configured
;
852 int wantattr
, haveattr
;
855 * Manually configure instead of calling XftMatchFont
856 * so that we can use the configured pattern for
857 * "missing glyph" lookups.
859 configured
= FcPatternDuplicate(pattern
);
863 FcConfigSubstitute(NULL
, configured
, FcMatchPattern
);
864 XftDefaultSubstitute(xw
.dpy
, xw
.scr
, configured
);
866 match
= FcFontMatch(NULL
, configured
, &result
);
868 FcPatternDestroy(configured
);
872 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
873 FcPatternDestroy(configured
);
874 FcPatternDestroy(match
);
878 if ((XftPatternGetInteger(pattern
, "slant", 0, &wantattr
) ==
881 * Check if xft was unable to find a font with the appropriate
882 * slant but gave us one anyway. Try to mitigate.
884 if ((XftPatternGetInteger(f
->match
->pattern
, "slant", 0,
885 &haveattr
) != XftResultMatch
) || haveattr
< wantattr
) {
887 fputs("font slant does not match\n", stderr
);
891 if ((XftPatternGetInteger(pattern
, "weight", 0, &wantattr
) ==
893 if ((XftPatternGetInteger(f
->match
->pattern
, "weight", 0,
894 &haveattr
) != XftResultMatch
) || haveattr
!= wantattr
) {
896 fputs("font weight does not match\n", stderr
);
900 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
901 (const FcChar8
*) ascii_printable
,
902 strlen(ascii_printable
), &extents
);
905 f
->pattern
= configured
;
907 f
->ascent
= f
->match
->ascent
;
908 f
->descent
= f
->match
->descent
;
910 f
->rbearing
= f
->match
->max_advance_width
;
912 f
->height
= f
->ascent
+ f
->descent
;
913 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
919 xloadfonts(char *fontstr
, double fontsize
)
924 if (fontstr
[0] == '-')
925 pattern
= XftXlfdParse(fontstr
, False
, False
);
927 pattern
= FcNameParse((FcChar8
*)fontstr
);
930 die("can't open font %s\n", fontstr
);
933 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
934 FcPatternDel(pattern
, FC_SIZE
);
935 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
936 usedfontsize
= fontsize
;
938 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
940 usedfontsize
= fontval
;
941 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
946 * Default font size is 12, if none given. This is to
947 * have a known usedfontsize value.
949 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
952 defaultfontsize
= usedfontsize
;
955 if (xloadfont(&dc
.font
, pattern
))
956 die("can't open font %s\n", fontstr
);
958 if (usedfontsize
< 0) {
959 FcPatternGetDouble(dc
.font
.match
->pattern
,
960 FC_PIXEL_SIZE
, 0, &fontval
);
961 usedfontsize
= fontval
;
963 defaultfontsize
= fontval
;
966 /* Setting character width and height. */
967 win
.cw
= ceilf(dc
.font
.width
* cwscale
);
968 win
.ch
= ceilf(dc
.font
.height
* chscale
);
970 FcPatternDel(pattern
, FC_SLANT
);
971 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
972 if (xloadfont(&dc
.ifont
, pattern
))
973 die("can't open font %s\n", fontstr
);
975 FcPatternDel(pattern
, FC_WEIGHT
);
976 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
977 if (xloadfont(&dc
.ibfont
, pattern
))
978 die("can't open font %s\n", fontstr
);
980 FcPatternDel(pattern
, FC_SLANT
);
981 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
982 if (xloadfont(&dc
.bfont
, pattern
))
983 die("can't open font %s\n", fontstr
);
985 FcPatternDestroy(pattern
);
991 XftFontClose(xw
.dpy
, f
->match
);
992 FcPatternDestroy(f
->pattern
);
994 FcFontSetDestroy(f
->set
);
1000 /* Free the loaded fonts in the font cache. */
1002 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
1004 xunloadfont(&dc
.font
);
1005 xunloadfont(&dc
.bfont
);
1006 xunloadfont(&dc
.ifont
);
1007 xunloadfont(&dc
.ibfont
);
1011 ximopen(Display
*dpy
)
1013 XIMCallback destroy
= { .client_data
= NULL
, .callback
= ximdestroy
};
1015 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1016 XSetLocaleModifiers("@im=local");
1017 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1018 XSetLocaleModifiers("@im=");
1019 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
)
1020 die("XOpenIM failed. Could not open input device.\n");
1023 if (XSetIMValues(xw
.xim
, XNDestroyCallback
, &destroy
, NULL
) != NULL
)
1024 die("XSetIMValues failed. Could not set input method value.\n");
1025 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
| XIMStatusNothing
,
1026 XNClientWindow
, xw
.win
, XNFocusWindow
, xw
.win
, NULL
);
1028 die("XCreateIC failed. Could not obtain input method.\n");
1032 ximinstantiate(Display
*dpy
, XPointer client
, XPointer call
)
1035 XUnregisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1036 ximinstantiate
, NULL
);
1040 ximdestroy(XIM xim
, XPointer client
, XPointer call
)
1043 XRegisterIMInstantiateCallback(xw
.dpy
, NULL
, NULL
, NULL
,
1044 ximinstantiate
, NULL
);
1048 xinit(int cols
, int rows
)
1053 pid_t thispid
= getpid();
1054 XColor xmousefg
, xmousebg
;
1056 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
1057 die("can't open display\n");
1058 xw
.scr
= XDefaultScreen(xw
.dpy
);
1059 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
1063 die("could not init fontconfig.\n");
1065 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
1066 xloadfonts(usedfont
, 0);
1069 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1072 /* adjust fixed window geometry */
1073 win
.w
= 2 * borderpx
+ cols
* win
.cw
;
1074 win
.h
= 2 * borderpx
+ rows
* win
.ch
;
1075 if (xw
.gm
& XNegative
)
1076 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - win
.w
- 2;
1077 if (xw
.gm
& YNegative
)
1078 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - win
.h
- 2;
1081 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
1082 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
1083 xw
.attrs
.bit_gravity
= NorthWestGravity
;
1084 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
| KeyReleaseMask
1085 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1086 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1087 xw
.attrs
.colormap
= xw
.cmap
;
1089 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
1090 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
1091 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
1092 win
.w
, win
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1093 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
1094 | CWEventMask
| CWColormap
, &xw
.attrs
);
1096 memset(&gcvalues
, 0, sizeof(gcvalues
));
1097 gcvalues
.graphics_exposures
= False
;
1098 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
1100 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
1101 DefaultDepth(xw
.dpy
, xw
.scr
));
1102 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
1103 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, win
.w
, win
.h
);
1105 /* font spec buffer */
1106 xw
.specbuf
= xmalloc(cols
* sizeof(GlyphFontSpec
));
1108 /* Xft rendering context */
1109 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
1114 /* white cursor, black outline */
1115 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
1116 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1118 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
1119 xmousefg
.red
= 0xffff;
1120 xmousefg
.green
= 0xffff;
1121 xmousefg
.blue
= 0xffff;
1124 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
1125 xmousebg
.red
= 0x0000;
1126 xmousebg
.green
= 0x0000;
1127 xmousebg
.blue
= 0x0000;
1130 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
1132 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1133 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
1134 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
1135 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
1137 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
1138 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
1139 PropModeReplace
, (uchar
*)&thispid
, 1);
1141 win
.mode
= MODE_NUMLOCK
;
1143 XMapWindow(xw
.dpy
, xw
.win
);
1145 XSync(xw
.dpy
, False
);
1147 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick1
);
1148 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick2
);
1149 xsel
.primary
= NULL
;
1150 xsel
.clipboard
= NULL
;
1151 xsel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
1152 if (xsel
.xtarget
== None
)
1153 xsel
.xtarget
= XA_STRING
;
1157 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
1159 float winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
, xp
, yp
;
1160 ushort mode
, prevmode
= USHRT_MAX
;
1161 Font
*font
= &dc
.font
;
1162 int frcflags
= FRC_NORMAL
;
1163 float runewidth
= win
.cw
;
1167 FcPattern
*fcpattern
, *fontpattern
;
1168 FcFontSet
*fcsets
[] = { NULL
};
1169 FcCharSet
*fccharset
;
1170 int i
, f
, numspecs
= 0;
1172 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
1173 /* Fetch rune and mode for current glyph. */
1175 mode
= glyphs
[i
].mode
;
1177 /* Skip dummy wide-character spacing. */
1178 if (mode
== ATTR_WDUMMY
)
1181 /* Determine font for glyph if different from previous glyph. */
1182 if (prevmode
!= mode
) {
1185 frcflags
= FRC_NORMAL
;
1186 runewidth
= win
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
1187 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
1189 frcflags
= FRC_ITALICBOLD
;
1190 } else if (mode
& ATTR_ITALIC
) {
1192 frcflags
= FRC_ITALIC
;
1193 } else if (mode
& ATTR_BOLD
) {
1195 frcflags
= FRC_BOLD
;
1197 yp
= winy
+ font
->ascent
;
1200 /* Lookup character index with default font. */
1201 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
1203 specs
[numspecs
].font
= font
->match
;
1204 specs
[numspecs
].glyph
= glyphidx
;
1205 specs
[numspecs
].x
= (short)xp
;
1206 specs
[numspecs
].y
= (short)yp
;
1212 /* Fallback on font cache, search the font cache for match. */
1213 for (f
= 0; f
< frclen
; f
++) {
1214 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
1215 /* Everything correct. */
1216 if (glyphidx
&& frc
[f
].flags
== frcflags
)
1218 /* We got a default font for a not found glyph. */
1219 if (!glyphidx
&& frc
[f
].flags
== frcflags
1220 && frc
[f
].unicodep
== rune
) {
1225 /* Nothing was found. Use fontconfig to find matching font. */
1228 font
->set
= FcFontSort(0, font
->pattern
,
1230 fcsets
[0] = font
->set
;
1233 * Nothing was found in the cache. Now use
1234 * some dozen of Fontconfig calls to get the
1235 * font for one single character.
1237 * Xft and fontconfig are design failures.
1239 fcpattern
= FcPatternDuplicate(font
->pattern
);
1240 fccharset
= FcCharSetCreate();
1242 FcCharSetAddChar(fccharset
, rune
);
1243 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
1245 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
1247 FcConfigSubstitute(0, fcpattern
,
1249 FcDefaultSubstitute(fcpattern
);
1251 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
1254 /* Allocate memory for the new cache entry. */
1255 if (frclen
>= frccap
) {
1257 frc
= xrealloc(frc
, frccap
* sizeof(Fontcache
));
1260 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
1262 if (!frc
[frclen
].font
)
1263 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1265 frc
[frclen
].flags
= frcflags
;
1266 frc
[frclen
].unicodep
= rune
;
1268 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
1273 FcPatternDestroy(fcpattern
);
1274 FcCharSetDestroy(fccharset
);
1277 specs
[numspecs
].font
= frc
[f
].font
;
1278 specs
[numspecs
].glyph
= glyphidx
;
1279 specs
[numspecs
].x
= (short)xp
;
1280 specs
[numspecs
].y
= (short)yp
;
1289 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
1291 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
1292 int winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
,
1293 width
= charlen
* win
.cw
;
1294 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
1295 XRenderColor colfg
, colbg
;
1298 /* Fallback on color display for attributes not supported by the font */
1299 if (base
.mode
& ATTR_ITALIC
&& base
.mode
& ATTR_BOLD
) {
1300 if (dc
.ibfont
.badslant
|| dc
.ibfont
.badweight
)
1301 base
.fg
= defaultattr
;
1302 } else if ((base
.mode
& ATTR_ITALIC
&& dc
.ifont
.badslant
) ||
1303 (base
.mode
& ATTR_BOLD
&& dc
.bfont
.badweight
)) {
1304 base
.fg
= defaultattr
;
1307 if (IS_TRUECOL(base
.fg
)) {
1308 colfg
.alpha
= 0xffff;
1309 colfg
.red
= TRUERED(base
.fg
);
1310 colfg
.green
= TRUEGREEN(base
.fg
);
1311 colfg
.blue
= TRUEBLUE(base
.fg
);
1312 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
1315 fg
= &dc
.col
[base
.fg
];
1318 if (IS_TRUECOL(base
.bg
)) {
1319 colbg
.alpha
= 0xffff;
1320 colbg
.green
= TRUEGREEN(base
.bg
);
1321 colbg
.red
= TRUERED(base
.bg
);
1322 colbg
.blue
= TRUEBLUE(base
.bg
);
1323 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
1326 bg
= &dc
.col
[base
.bg
];
1329 /* Change basic system colors [0-7] to bright system colors [8-15] */
1330 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
1331 fg
= &dc
.col
[base
.fg
+ 8];
1333 if (IS_SET(MODE_REVERSE
)) {
1334 if (fg
== &dc
.col
[defaultfg
]) {
1335 fg
= &dc
.col
[defaultbg
];
1337 colfg
.red
= ~fg
->color
.red
;
1338 colfg
.green
= ~fg
->color
.green
;
1339 colfg
.blue
= ~fg
->color
.blue
;
1340 colfg
.alpha
= fg
->color
.alpha
;
1341 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
1346 if (bg
== &dc
.col
[defaultbg
]) {
1347 bg
= &dc
.col
[defaultfg
];
1349 colbg
.red
= ~bg
->color
.red
;
1350 colbg
.green
= ~bg
->color
.green
;
1351 colbg
.blue
= ~bg
->color
.blue
;
1352 colbg
.alpha
= bg
->color
.alpha
;
1353 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
1359 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
1360 colfg
.red
= fg
->color
.red
/ 2;
1361 colfg
.green
= fg
->color
.green
/ 2;
1362 colfg
.blue
= fg
->color
.blue
/ 2;
1363 colfg
.alpha
= fg
->color
.alpha
;
1364 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
1368 if (base
.mode
& ATTR_REVERSE
) {
1374 if (base
.mode
& ATTR_BLINK
&& win
.mode
& MODE_BLINK
)
1377 if (base
.mode
& ATTR_INVISIBLE
)
1380 /* Intelligent cleaning up of the borders. */
1382 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
1384 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: 0));
1386 if (winx
+ width
>= borderpx
+ win
.tw
) {
1387 xclear(winx
+ width
, (y
== 0)? 0 : winy
, win
.w
,
1388 ((winy
+ win
.ch
>= borderpx
+ win
.th
)? win
.h
: (winy
+ win
.ch
)));
1391 xclear(winx
, 0, winx
+ width
, borderpx
);
1392 if (winy
+ win
.ch
>= borderpx
+ win
.th
)
1393 xclear(winx
, winy
+ win
.ch
, winx
+ width
, win
.h
);
1395 /* Clean up the region we want to draw to. */
1396 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, win
.ch
);
1398 /* Set the clip region because Xft is sometimes dirty. */
1403 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
1405 /* Render the glyphs. */
1406 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
1408 /* Render underline and strikethrough. */
1409 if (base
.mode
& ATTR_UNDERLINE
) {
1410 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
1414 if (base
.mode
& ATTR_STRUCK
) {
1415 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
1419 /* Reset clip to none. */
1420 XftDrawSetClip(xw
.draw
, 0);
1424 xdrawglyph(Glyph g
, int x
, int y
)
1427 XftGlyphFontSpec spec
;
1429 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
1430 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
1434 xdrawcursor(int cx
, int cy
, Glyph g
, int ox
, int oy
, Glyph og
)
1438 /* remove the old cursor */
1439 if (selected(ox
, oy
))
1440 og
.mode
^= ATTR_REVERSE
;
1441 xdrawglyph(og
, ox
, oy
);
1443 if (IS_SET(MODE_HIDE
))
1447 * Select the right color for the right mode.
1449 g
.mode
&= ATTR_BOLD
|ATTR_ITALIC
|ATTR_UNDERLINE
|ATTR_STRUCK
|ATTR_WIDE
;
1451 if (IS_SET(MODE_REVERSE
)) {
1452 g
.mode
|= ATTR_REVERSE
;
1454 if (selected(cx
, cy
)) {
1455 drawcol
= dc
.col
[defaultcs
];
1458 drawcol
= dc
.col
[defaultrcs
];
1462 if (selected(cx
, cy
)) {
1469 drawcol
= dc
.col
[g
.bg
];
1472 /* draw the new one */
1473 if (IS_SET(MODE_FOCUSED
)) {
1474 switch (win
.cursor
) {
1475 case 7: /* st extension: snowman (U+2603) */
1477 case 0: /* Blinking Block */
1478 case 1: /* Blinking Block (Default) */
1479 case 2: /* Steady Block */
1480 xdrawglyph(g
, cx
, cy
);
1482 case 3: /* Blinking Underline */
1483 case 4: /* Steady Underline */
1484 XftDrawRect(xw
.draw
, &drawcol
,
1485 borderpx
+ cx
* win
.cw
,
1486 borderpx
+ (cy
+ 1) * win
.ch
- \
1488 win
.cw
, cursorthickness
);
1490 case 5: /* Blinking bar */
1491 case 6: /* Steady bar */
1492 XftDrawRect(xw
.draw
, &drawcol
,
1493 borderpx
+ cx
* win
.cw
,
1494 borderpx
+ cy
* win
.ch
,
1495 cursorthickness
, win
.ch
);
1499 XftDrawRect(xw
.draw
, &drawcol
,
1500 borderpx
+ cx
* win
.cw
,
1501 borderpx
+ cy
* win
.ch
,
1503 XftDrawRect(xw
.draw
, &drawcol
,
1504 borderpx
+ cx
* win
.cw
,
1505 borderpx
+ cy
* win
.ch
,
1507 XftDrawRect(xw
.draw
, &drawcol
,
1508 borderpx
+ (cx
+ 1) * win
.cw
- 1,
1509 borderpx
+ cy
* win
.ch
,
1511 XftDrawRect(xw
.draw
, &drawcol
,
1512 borderpx
+ cx
* win
.cw
,
1513 borderpx
+ (cy
+ 1) * win
.ch
- 1,
1521 char buf
[sizeof(long) * 8 + 1];
1523 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1524 setenv("WINDOWID", buf
, 1);
1531 DEFAULT(p
, opt_title
);
1533 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1535 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
1536 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
1543 return IS_SET(MODE_VISIBLE
);
1547 xdrawline(Line line
, int x1
, int y1
, int x2
)
1549 int i
, x
, ox
, numspecs
;
1551 XftGlyphFontSpec
*specs
= xw
.specbuf
;
1553 numspecs
= xmakeglyphfontspecs(specs
, &line
[x1
], x2
- x1
, x1
, y1
);
1555 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
1557 if (new.mode
== ATTR_WDUMMY
)
1559 if (selected(x
, y1
))
1560 new.mode
^= ATTR_REVERSE
;
1561 if (i
> 0 && ATTRCMP(base
, new)) {
1562 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1574 xdrawglyphfontspecs(specs
, base
, i
, ox
, y1
);
1580 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, win
.w
,
1582 XSetForeground(xw
.dpy
, dc
.gc
,
1583 dc
.col
[IS_SET(MODE_REVERSE
)?
1584 defaultfg
: defaultbg
].pixel
);
1588 xximspot(int x
, int y
)
1590 XPoint spot
= { borderpx
+ x
* win
.cw
, borderpx
+ (y
+ 1) * win
.ch
};
1591 XVaNestedList attr
= XVaCreateNestedList(0, XNSpotLocation
, &spot
, NULL
);
1593 XSetICValues(xw
.xic
, XNPreeditAttributes
, attr
, NULL
);
1604 visibility(XEvent
*ev
)
1606 XVisibilityEvent
*e
= &ev
->xvisibility
;
1608 MODBIT(win
.mode
, e
->state
!= VisibilityFullyObscured
, MODE_VISIBLE
);
1614 win
.mode
&= ~MODE_VISIBLE
;
1618 xsetpointermotion(int set
)
1620 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
1621 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
1625 xsetmode(int set
, unsigned int flags
)
1627 int mode
= win
.mode
;
1628 MODBIT(win
.mode
, set
, flags
);
1629 if ((win
.mode
& MODE_REVERSE
) != (mode
& MODE_REVERSE
))
1634 xsetcursor(int cursor
)
1637 if (!BETWEEN(cursor
, 0, 6))
1639 win
.cursor
= cursor
;
1644 xseturgency(int add
)
1646 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1648 MODBIT(h
->flags
, add
, XUrgencyHint
);
1649 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1656 if (!(IS_SET(MODE_FOCUSED
)))
1659 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
1665 XFocusChangeEvent
*e
= &ev
->xfocus
;
1667 if (e
->mode
== NotifyGrab
)
1670 if (ev
->type
== FocusIn
) {
1671 XSetICFocus(xw
.xic
);
1672 win
.mode
|= MODE_FOCUSED
;
1674 if (IS_SET(MODE_FOCUS
))
1675 ttywrite("\033[I", 3, 0);
1677 XUnsetICFocus(xw
.xic
);
1678 win
.mode
&= ~MODE_FOCUSED
;
1679 if (IS_SET(MODE_FOCUS
))
1680 ttywrite("\033[O", 3, 0);
1685 match(uint mask
, uint state
)
1687 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
1691 kmap(KeySym k
, uint state
)
1696 /* Check for mapped keys out of X11 function keys. */
1697 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
1698 if (mappedkeys
[i
] == k
)
1701 if (i
== LEN(mappedkeys
)) {
1702 if ((k
& 0xFFFF) < 0xFD00)
1706 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
1710 if (!match(kp
->mask
, state
))
1713 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
1715 if (IS_SET(MODE_NUMLOCK
) && kp
->appkey
== 2)
1718 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
1730 XKeyEvent
*e
= &ev
->xkey
;
1732 char buf
[32], *customkey
;
1738 if (IS_SET(MODE_KBDLOCK
))
1741 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
1743 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
1744 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
1745 bp
->func(&(bp
->arg
));
1750 /* 2. custom keys from config.h */
1751 if ((customkey
= kmap(ksym
, e
->state
))) {
1752 ttywrite(customkey
, strlen(customkey
), 1);
1756 /* 3. composed string from input method */
1759 if (len
== 1 && e
->state
& Mod1Mask
) {
1760 if (IS_SET(MODE_8BIT
)) {
1763 len
= utf8encode(c
, buf
);
1771 ttywrite(buf
, len
, 1);
1779 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1781 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1782 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1783 win
.mode
|= MODE_FOCUSED
;
1785 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1786 win
.mode
&= ~MODE_FOCUSED
;
1788 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
1797 if (e
->xconfigure
.width
== win
.w
&& e
->xconfigure
.height
== win
.h
)
1800 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
1807 int w
= win
.w
, h
= win
.h
;
1809 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
1811 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
1814 /* Waiting for window mapping */
1816 XNextEvent(xw
.dpy
, &ev
);
1818 * This XFilterEvent call is required because of XOpenIM. It
1819 * does filter out the key event and some client message for
1820 * the input method too.
1822 if (XFilterEvent(&ev
, None
))
1824 if (ev
.type
== ConfigureNotify
) {
1825 w
= ev
.xconfigure
.width
;
1826 h
= ev
.xconfigure
.height
;
1828 } while (ev
.type
!= MapNotify
);
1830 ttyfd
= ttynew(opt_line
, shell
, opt_io
, opt_cmd
);
1833 clock_gettime(CLOCK_MONOTONIC
, &last
);
1836 for (xev
= actionfps
;;) {
1838 FD_SET(ttyfd
, &rfd
);
1841 if (pselect(MAX(xfd
, ttyfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
1844 die("select failed: %s\n", strerror(errno
));
1846 if (FD_ISSET(ttyfd
, &rfd
)) {
1849 blinkset
= tattrset(ATTR_BLINK
);
1851 MODBIT(win
.mode
, 0, MODE_BLINK
);
1855 if (FD_ISSET(xfd
, &rfd
))
1858 clock_gettime(CLOCK_MONOTONIC
, &now
);
1859 drawtimeout
.tv_sec
= 0;
1860 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
1864 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
1865 tsetdirtattr(ATTR_BLINK
);
1866 win
.mode
^= MODE_BLINK
;
1870 deltatime
= TIMEDIFF(now
, last
);
1871 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
1877 while (XPending(xw
.dpy
)) {
1878 XNextEvent(xw
.dpy
, &ev
);
1879 if (XFilterEvent(&ev
, None
))
1881 if (handler
[ev
.type
])
1882 (handler
[ev
.type
])(&ev
);
1888 if (xev
&& !FD_ISSET(xfd
, &rfd
))
1890 if (!FD_ISSET(ttyfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
1892 if (TIMEDIFF(now
, lastblink
) \
1894 drawtimeout
.tv_nsec
= 1000;
1896 drawtimeout
.tv_nsec
= (1E6
* \
1901 drawtimeout
.tv_sec
= \
1902 drawtimeout
.tv_nsec
/ 1E9
;
1903 drawtimeout
.tv_nsec
%= (long)1E9
;
1915 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
1916 " [-n name] [-o file]\n"
1917 " [-T title] [-t title] [-w windowid]"
1918 " [[-e] command [args ...]]\n"
1919 " %s [-aiv] [-c class] [-f font] [-g geometry]"
1920 " [-n name] [-o file]\n"
1921 " [-T title] [-t title] [-w windowid] -l line"
1922 " [stty_args ...]\n", argv0
, argv0
);
1926 main(int argc
, char *argv
[])
1930 win
.cursor
= cursorshape
;
1937 opt_class
= EARGF(usage());
1944 opt_font
= EARGF(usage());
1947 xw
.gm
= XParseGeometry(EARGF(usage()),
1948 &xw
.l
, &xw
.t
, &cols
, &rows
);
1954 opt_io
= EARGF(usage());
1957 opt_line
= EARGF(usage());
1960 opt_name
= EARGF(usage());
1964 opt_title
= EARGF(usage());
1967 opt_embed
= EARGF(usage());
1970 die("%s " VERSION
"\n", argv0
);
1977 if (argc
> 0) /* eat all remaining arguments */
1981 opt_title
= (opt_line
|| !opt_cmd
) ? "st" : opt_cmd
[0];
1983 setlocale(LC_CTYPE
, "");
1984 XSetLocaleModifiers("");
1985 cols
= MAX(cols
, 1);
1986 rows
= MAX(rows
, 1);