1 /* See LICENSE for license details. */
6 #include <sys/select.h>
10 #include <X11/Xatom.h>
12 #include <X11/Xutil.h>
13 #include <X11/cursorfont.h>
14 #include <X11/keysym.h>
15 #include <X11/Xft/Xft.h>
16 #include <X11/XKBlib.h>
23 /* types used in config.h */
27 void (*func
)(const Arg
*);
41 /* three-valued logic variables: 0 indifferent, 1 on, -1 off */
42 signed char appkey
; /* application keypad */
43 signed char appcursor
; /* application cursor */
47 #define XK_ANY_MOD UINT_MAX
49 #define XK_SWITCH_MOD (1<<13)
51 /* function definitions used in config.h */
52 static void clipcopy(const Arg
*);
53 static void clippaste(const Arg
*);
54 static void numlock(const Arg
*);
55 static void selpaste(const Arg
*);
56 static void zoom(const Arg
*);
57 static void zoomabs(const Arg
*);
58 static void zoomreset(const Arg
*);
60 /* config.h for applying patches and the configuration. */
64 #define XEMBED_FOCUS_IN 4
65 #define XEMBED_FOCUS_OUT 5
68 #define IS_SET(flag) ((win.mode & (flag)) != 0)
69 #define TRUERED(x) (((x) & 0xff0000) >> 8)
70 #define TRUEGREEN(x) (((x) & 0xff00))
71 #define TRUEBLUE(x) (((x) & 0xff) << 8)
73 typedef XftDraw
*Draw
;
74 typedef XftColor Color
;
75 typedef XftGlyphFontSpec GlyphFontSpec
;
77 /* Purely graphic info */
83 GlyphFontSpec
*specbuf
; /* font spec buffer used for rendering */
84 Atom xembed
, wmdeletewin
, netwmname
, netwmpid
;
89 XSetWindowAttributes attrs
;
91 int isfixed
; /* is fixed geometry? */
92 int l
, t
; /* left and top offset */
93 int gm
; /* geometry mask */
98 char *primary
, *clipboard
;
99 struct timespec tclick1
;
100 struct timespec tclick2
;
119 /* Drawing Context */
123 Font font
, bfont
, ifont
, ibfont
;
127 static inline ushort
sixd_to_16bit(int);
128 static int xmakeglyphfontspecs(XftGlyphFontSpec
*, const Glyph
*, int, int, int);
129 static void xdrawglyphfontspecs(const XftGlyphFontSpec
*, Glyph
, int, int, int);
130 static void xdrawglyph(Glyph
, int, int);
131 static void xclear(int, int, int, int);
132 static void xdrawcursor(void);
133 static int xgeommasktogravity(int);
134 static void xinit(void);
135 static void cresize(int, int);
136 static void xresize(int, int);
137 static int xloadfont(Font
*, FcPattern
*);
138 static void xloadfonts(char *, double);
139 static void xunloadfont(Font
*);
140 static void xunloadfonts(void);
141 static void xsetenv(void);
142 static void xseturgency(int);
143 static int x2col(int);
144 static int y2row(int);
146 static void expose(XEvent
*);
147 static void visibility(XEvent
*);
148 static void unmap(XEvent
*);
149 static void kpress(XEvent
*);
150 static void cmessage(XEvent
*);
151 static void resize(XEvent
*);
152 static void focus(XEvent
*);
153 static void brelease(XEvent
*);
154 static void bpress(XEvent
*);
155 static void bmotion(XEvent
*);
156 static void propnotify(XEvent
*);
157 static void selnotify(XEvent
*);
158 static void selclear_(XEvent
*);
159 static void selrequest(XEvent
*);
160 static void setsel(char *, Time
);
161 static void mousesel(XEvent
*, int);
162 static void mousereport(XEvent
*);
163 static char *kmap(KeySym
, uint
);
164 static int match(uint
, uint
);
166 static void run(void);
167 static void usage(void);
169 static void (*handler
[LASTEvent
])(XEvent
*) = {
171 [ClientMessage
] = cmessage
,
172 [ConfigureNotify
] = resize
,
173 [VisibilityNotify
] = visibility
,
174 [UnmapNotify
] = unmap
,
178 [MotionNotify
] = bmotion
,
179 [ButtonPress
] = bpress
,
180 [ButtonRelease
] = brelease
,
182 * Uncomment if you want the selection to disappear when you select something
183 * different in another window.
185 /* [SelectionClear] = selclear_, */
186 [SelectionNotify
] = selnotify
,
188 * PropertyNotify is only turned on when there is some INCR transfer happening
189 * for the selection retrieval.
191 [PropertyNotify
] = propnotify
,
192 [SelectionRequest
] = selrequest
,
198 static XSelection xsel
;
199 static TermWindow win
;
201 /* Font Ring Cache */
215 /* Fontcache is an array now. A new font will be appended to the array. */
216 static Fontcache frc
[16];
217 static int frclen
= 0;
218 static char *usedfont
= NULL
;
219 static double usedfontsize
= 0;
220 static double defaultfontsize
= 0;
222 static char *opt_class
= NULL
;
223 static char **opt_cmd
= NULL
;
224 static char *opt_embed
= NULL
;
225 static char *opt_font
= NULL
;
226 static char *opt_io
= NULL
;
227 static char *opt_line
= NULL
;
228 static char *opt_name
= NULL
;
229 static char *opt_title
= NULL
;
232 clipcopy(const Arg
*dummy
)
236 if (xsel
.clipboard
!= NULL
)
237 free(xsel
.clipboard
);
239 if (xsel
.primary
!= NULL
) {
240 xsel
.clipboard
= xstrdup(xsel
.primary
);
241 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
242 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
247 clippaste(const Arg
*dummy
)
251 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
252 XConvertSelection(xw
.dpy
, clipboard
, xsel
.xtarget
, clipboard
,
253 xw
.win
, CurrentTime
);
257 selpaste(const Arg
*dummy
)
259 XConvertSelection(xw
.dpy
, XA_PRIMARY
, xsel
.xtarget
, XA_PRIMARY
,
260 xw
.win
, CurrentTime
);
264 numlock(const Arg
*dummy
)
266 win
.mode
^= MODE_NUMLOCK
;
274 larg
.f
= usedfontsize
+ arg
->f
;
279 zoomabs(const Arg
*arg
)
282 xloadfonts(usedfont
, arg
->f
);
289 zoomreset(const Arg
*arg
)
293 if (defaultfontsize
> 0) {
294 larg
.f
= defaultfontsize
;
305 return LIMIT(x
, 0, term
.col
-1);
314 return LIMIT(y
, 0, term
.row
-1);
318 mousesel(XEvent
*e
, int done
)
320 int type
, seltype
= SEL_REGULAR
;
321 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forceselmod
);
323 for (type
= 1; type
< LEN(selmasks
); ++type
) {
324 if (match(selmasks
[type
], state
)) {
329 selextend(x2col(e
->xbutton
.x
), y2row(e
->xbutton
.y
), seltype
, done
);
331 setsel(getsel(), e
->xbutton
.time
);
335 mousereport(XEvent
*e
)
337 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
338 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
344 if (e
->xbutton
.type
== MotionNotify
) {
345 if (x
== ox
&& y
== oy
)
347 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
349 /* MOUSE_MOTION: no reporting if no button is pressed */
350 if (IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
353 button
= oldbutton
+ 32;
357 if (!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
364 if (e
->xbutton
.type
== ButtonPress
) {
368 } else if (e
->xbutton
.type
== ButtonRelease
) {
370 /* MODE_MOUSEX10: no button release reporting */
371 if (IS_SET(MODE_MOUSEX10
))
373 if (button
== 64 || button
== 65)
378 if (!IS_SET(MODE_MOUSEX10
)) {
379 button
+= ((state
& ShiftMask
) ? 4 : 0)
380 + ((state
& Mod4Mask
) ? 8 : 0)
381 + ((state
& ControlMask
) ? 16 : 0);
384 if (IS_SET(MODE_MOUSESGR
)) {
385 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
387 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
388 } else if (x
< 223 && y
< 223) {
389 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
390 32+button
, 32+x
+1, 32+y
+1);
395 ttywrite(buf
, len
, 0);
405 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
410 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
411 if (e
->xbutton
.button
== ms
->b
412 && match(ms
->mask
, e
->xbutton
.state
)) {
413 ttywrite(ms
->s
, strlen(ms
->s
), 1);
418 if (e
->xbutton
.button
== Button1
) {
420 * If the user clicks below predefined timeouts specific
421 * snapping behaviour is exposed.
423 clock_gettime(CLOCK_MONOTONIC
, &now
);
424 if (TIMEDIFF(now
, xsel
.tclick2
) <= tripleclicktimeout
) {
426 } else if (TIMEDIFF(now
, xsel
.tclick1
) <= doubleclicktimeout
) {
431 xsel
.tclick2
= xsel
.tclick1
;
434 selstart(x2col(e
->xbutton
.x
), y2row(e
->xbutton
.y
), snap
);
439 propnotify(XEvent
*e
)
441 XPropertyEvent
*xpev
;
442 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
444 xpev
= &e
->xproperty
;
445 if (xpev
->state
== PropertyNewValue
&&
446 (xpev
->atom
== XA_PRIMARY
||
447 xpev
->atom
== clipboard
)) {
455 ulong nitems
, ofs
, rem
;
457 uchar
*data
, *last
, *repl
;
458 Atom type
, incratom
, property
;
460 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
463 if (e
->type
== SelectionNotify
) {
464 property
= e
->xselection
.property
;
465 } else if(e
->type
== PropertyNotify
) {
466 property
= e
->xproperty
.atom
;
470 if (property
== None
)
474 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
475 BUFSIZ
/4, False
, AnyPropertyType
,
476 &type
, &format
, &nitems
, &rem
,
478 fprintf(stderr
, "Clipboard allocation failed\n");
482 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
484 * If there is some PropertyNotify with no data, then
485 * this is the signal of the selection owner that all
486 * data has been transferred. We won't need to receive
487 * PropertyNotify events anymore.
489 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
490 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
494 if (type
== incratom
) {
496 * Activate the PropertyNotify events so we receive
497 * when the selection owner does send us the next
500 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
501 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
505 * Deleting the property is the transfer start signal.
507 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
513 * Line endings are inconsistent in the terminal and GUI world
514 * copy and pasting. When receiving some selection data,
515 * replace all '\n' with '\r'.
516 * FIXME: Fix the computer world.
519 last
= data
+ nitems
* format
/ 8;
520 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
524 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
525 ttywrite("\033[200~", 6, 0);
526 ttywrite((char *)data
, nitems
* format
/ 8, 1);
527 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
528 ttywrite("\033[201~", 6, 0);
530 /* number of 32-bit chunks returned */
531 ofs
+= nitems
* format
/ 32;
535 * Deleting the property again tells the selection owner to send the
536 * next data chunk in the property.
538 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
554 selrequest(XEvent
*e
)
556 XSelectionRequestEvent
*xsre
;
558 Atom xa_targets
, string
, clipboard
;
561 xsre
= (XSelectionRequestEvent
*) e
;
562 xev
.type
= SelectionNotify
;
563 xev
.requestor
= xsre
->requestor
;
564 xev
.selection
= xsre
->selection
;
565 xev
.target
= xsre
->target
;
566 xev
.time
= xsre
->time
;
567 if (xsre
->property
== None
)
568 xsre
->property
= xsre
->target
;
573 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
574 if (xsre
->target
== xa_targets
) {
575 /* respond with the supported type */
576 string
= xsel
.xtarget
;
577 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
578 XA_ATOM
, 32, PropModeReplace
,
579 (uchar
*) &string
, 1);
580 xev
.property
= xsre
->property
;
581 } else if (xsre
->target
== xsel
.xtarget
|| xsre
->target
== XA_STRING
) {
583 * xith XA_STRING non ascii characters may be incorrect in the
584 * requestor. It is not our problem, use utf8.
586 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
587 if (xsre
->selection
== XA_PRIMARY
) {
588 seltext
= xsel
.primary
;
589 } else if (xsre
->selection
== clipboard
) {
590 seltext
= xsel
.clipboard
;
593 "Unhandled clipboard selection 0x%lx\n",
597 if (seltext
!= NULL
) {
598 XChangeProperty(xsre
->display
, xsre
->requestor
,
599 xsre
->property
, xsre
->target
,
601 (uchar
*)seltext
, strlen(seltext
));
602 xev
.property
= xsre
->property
;
606 /* all done, send a notification to the listener */
607 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
608 fprintf(stderr
, "Error sending SelectionNotify event\n");
612 setsel(char *str
, Time t
)
617 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
618 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
625 setsel(str
, CurrentTime
);
631 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
636 if (e
->xbutton
.button
== Button2
)
638 else if (e
->xbutton
.button
== Button1
)
645 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
654 cresize(int width
, int height
)
663 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
664 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
668 ttyresize(win
.tw
, win
.th
);
672 xresize(int col
, int row
)
674 win
.tw
= MAX(1, col
* win
.cw
);
675 win
.th
= MAX(1, row
* win
.ch
);
677 XFreePixmap(xw
.dpy
, xw
.buf
);
678 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
679 DefaultDepth(xw
.dpy
, xw
.scr
));
680 XftDrawChange(xw
.draw
, xw
.buf
);
681 xclear(0, 0, win
.w
, win
.h
);
683 /* resize to new width */
684 xw
.specbuf
= xrealloc(xw
.specbuf
, col
* sizeof(GlyphFontSpec
));
690 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
694 xloadcolor(int i
, const char *name
, Color
*ncolor
)
696 XRenderColor color
= { .alpha
= 0xffff };
699 if (BETWEEN(i
, 16, 255)) { /* 256 color */
700 if (i
< 6*6*6+16) { /* same colors as xterm */
701 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
702 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
703 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
704 } else { /* greyscale */
705 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
706 color
.green
= color
.blue
= color
.red
;
708 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
709 xw
.cmap
, &color
, ncolor
);
714 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
724 dc
.collen
= MAX(LEN(colorname
), 256);
725 dc
.col
= xmalloc(dc
.collen
* sizeof(Color
));
728 for (cp
= dc
.col
; cp
< &dc
.col
[dc
.collen
]; ++cp
)
729 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
732 for (i
= 0; i
< dc
.collen
; i
++)
733 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
735 die("Could not allocate color '%s'\n", colorname
[i
]);
737 die("Could not allocate color %d\n", i
);
743 xsetcolorname(int x
, const char *name
)
747 if (!BETWEEN(x
, 0, dc
.collen
))
751 if (!xloadcolor(x
, name
, &ncolor
))
754 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
761 * Absolute coordinates.
764 xclear(int x1
, int y1
, int x2
, int y2
)
767 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
768 x1
, y1
, x2
-x1
, y2
-y1
);
774 XClassHint
class = {opt_name
? opt_name
: termname
,
775 opt_class
? opt_class
: termname
};
776 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
777 XSizeHints
*sizeh
= NULL
;
779 sizeh
= XAllocSizeHints();
781 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
782 sizeh
->height
= win
.h
;
783 sizeh
->width
= win
.w
;
784 sizeh
->height_inc
= win
.ch
;
785 sizeh
->width_inc
= win
.cw
;
786 sizeh
->base_height
= 2 * borderpx
;
787 sizeh
->base_width
= 2 * borderpx
;
789 sizeh
->flags
|= PMaxSize
| PMinSize
;
790 sizeh
->min_width
= sizeh
->max_width
= win
.w
;
791 sizeh
->min_height
= sizeh
->max_height
= win
.h
;
793 if (xw
.gm
& (XValue
|YValue
)) {
794 sizeh
->flags
|= USPosition
| PWinGravity
;
797 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
800 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
806 xgeommasktogravity(int mask
)
808 switch (mask
& (XNegative
|YNegative
)) {
810 return NorthWestGravity
;
812 return NorthEastGravity
;
814 return SouthWestGravity
;
817 return SouthEastGravity
;
821 xloadfont(Font
*f
, FcPattern
*pattern
)
823 FcPattern
*configured
;
827 int wantattr
, haveattr
;
830 * Manually configure instead of calling XftMatchFont
831 * so that we can use the configured pattern for
832 * "missing glyph" lookups.
834 configured
= FcPatternDuplicate(pattern
);
838 FcConfigSubstitute(NULL
, configured
, FcMatchPattern
);
839 XftDefaultSubstitute(xw
.dpy
, xw
.scr
, configured
);
841 match
= FcFontMatch(NULL
, configured
, &result
);
843 FcPatternDestroy(configured
);
847 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
848 FcPatternDestroy(configured
);
849 FcPatternDestroy(match
);
853 if ((XftPatternGetInteger(pattern
, "slant", 0, &wantattr
) ==
856 * Check if xft was unable to find a font with the appropriate
857 * slant but gave us one anyway. Try to mitigate.
859 if ((XftPatternGetInteger(f
->match
->pattern
, "slant", 0,
860 &haveattr
) != XftResultMatch
) || haveattr
< wantattr
) {
862 fputs("st: font slant does not match\n", stderr
);
866 if ((XftPatternGetInteger(pattern
, "weight", 0, &wantattr
) ==
868 if ((XftPatternGetInteger(f
->match
->pattern
, "weight", 0,
869 &haveattr
) != XftResultMatch
) || haveattr
!= wantattr
) {
871 fputs("st: font weight does not match\n", stderr
);
875 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
876 (const FcChar8
*) ascii_printable
,
877 strlen(ascii_printable
), &extents
);
880 f
->pattern
= configured
;
882 f
->ascent
= f
->match
->ascent
;
883 f
->descent
= f
->match
->descent
;
885 f
->rbearing
= f
->match
->max_advance_width
;
887 f
->height
= f
->ascent
+ f
->descent
;
888 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
894 xloadfonts(char *fontstr
, double fontsize
)
900 if (fontstr
[0] == '-') {
901 pattern
= XftXlfdParse(fontstr
, False
, False
);
903 pattern
= FcNameParse((FcChar8
*)fontstr
);
907 die("st: can't open font %s\n", fontstr
);
910 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
911 FcPatternDel(pattern
, FC_SIZE
);
912 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
913 usedfontsize
= fontsize
;
915 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
917 usedfontsize
= fontval
;
918 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
923 * Default font size is 12, if none given. This is to
924 * have a known usedfontsize value.
926 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
929 defaultfontsize
= usedfontsize
;
932 if (xloadfont(&dc
.font
, pattern
))
933 die("st: can't open font %s\n", fontstr
);
935 if (usedfontsize
< 0) {
936 FcPatternGetDouble(dc
.font
.match
->pattern
,
937 FC_PIXEL_SIZE
, 0, &fontval
);
938 usedfontsize
= fontval
;
940 defaultfontsize
= fontval
;
943 /* Setting character width and height. */
944 win
.cw
= ceilf(dc
.font
.width
* cwscale
);
945 win
.ch
= ceilf(dc
.font
.height
* chscale
);
947 FcPatternDel(pattern
, FC_SLANT
);
948 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
949 if (xloadfont(&dc
.ifont
, pattern
))
950 die("st: can't open font %s\n", fontstr
);
952 FcPatternDel(pattern
, FC_WEIGHT
);
953 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
954 if (xloadfont(&dc
.ibfont
, pattern
))
955 die("st: can't open font %s\n", fontstr
);
957 FcPatternDel(pattern
, FC_SLANT
);
958 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
959 if (xloadfont(&dc
.bfont
, pattern
))
960 die("st: can't open font %s\n", fontstr
);
962 FcPatternDestroy(pattern
);
968 XftFontClose(xw
.dpy
, f
->match
);
969 FcPatternDestroy(f
->pattern
);
971 FcFontSetDestroy(f
->set
);
977 /* Free the loaded fonts in the font cache. */
979 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
981 xunloadfont(&dc
.font
);
982 xunloadfont(&dc
.bfont
);
983 xunloadfont(&dc
.ifont
);
984 xunloadfont(&dc
.ibfont
);
993 pid_t thispid
= getpid();
994 XColor xmousefg
, xmousebg
;
996 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
997 die("Can't open display\n");
998 xw
.scr
= XDefaultScreen(xw
.dpy
);
999 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
1003 die("Could not init fontconfig.\n");
1005 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
1006 xloadfonts(usedfont
, 0);
1009 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1012 /* adjust fixed window geometry */
1013 win
.w
= 2 * borderpx
+ term
.col
* win
.cw
;
1014 win
.h
= 2 * borderpx
+ term
.row
* win
.ch
;
1015 if (xw
.gm
& XNegative
)
1016 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - win
.w
- 2;
1017 if (xw
.gm
& YNegative
)
1018 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - win
.h
- 2;
1021 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
1022 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
1023 xw
.attrs
.bit_gravity
= NorthWestGravity
;
1024 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1025 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1026 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1027 xw
.attrs
.colormap
= xw
.cmap
;
1029 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
1030 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
1031 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
1032 win
.w
, win
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1033 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
1034 | CWEventMask
| CWColormap
, &xw
.attrs
);
1036 memset(&gcvalues
, 0, sizeof(gcvalues
));
1037 gcvalues
.graphics_exposures
= False
;
1038 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
1040 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
1041 DefaultDepth(xw
.dpy
, xw
.scr
));
1042 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
1043 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, win
.w
, win
.h
);
1045 /* font spec buffer */
1046 xw
.specbuf
= xmalloc(term
.col
* sizeof(GlyphFontSpec
));
1048 /* Xft rendering context */
1049 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
1052 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1053 XSetLocaleModifiers("@im=local");
1054 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1055 XSetLocaleModifiers("@im=");
1056 if ((xw
.xim
= XOpenIM(xw
.dpy
,
1057 NULL
, NULL
, NULL
)) == NULL
) {
1058 die("XOpenIM failed. Could not open input"
1063 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1064 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1065 XNFocusWindow
, xw
.win
, NULL
);
1067 die("XCreateIC failed. Could not obtain input method.\n");
1069 /* white cursor, black outline */
1070 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
1071 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1073 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
1074 xmousefg
.red
= 0xffff;
1075 xmousefg
.green
= 0xffff;
1076 xmousefg
.blue
= 0xffff;
1079 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
1080 xmousebg
.red
= 0x0000;
1081 xmousebg
.green
= 0x0000;
1082 xmousebg
.blue
= 0x0000;
1085 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
1087 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1088 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
1089 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
1090 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
1092 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
1093 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
1094 PropModeReplace
, (uchar
*)&thispid
, 1);
1096 win
.mode
= MODE_NUMLOCK
;
1098 XMapWindow(xw
.dpy
, xw
.win
);
1100 XSync(xw
.dpy
, False
);
1102 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick1
);
1103 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick2
);
1104 xsel
.primary
= NULL
;
1105 xsel
.clipboard
= NULL
;
1106 xsel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
1107 if (xsel
.xtarget
== None
)
1108 xsel
.xtarget
= XA_STRING
;
1112 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
1114 float winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
, xp
, yp
;
1115 ushort mode
, prevmode
= USHRT_MAX
;
1116 Font
*font
= &dc
.font
;
1117 int frcflags
= FRC_NORMAL
;
1118 float runewidth
= win
.cw
;
1122 FcPattern
*fcpattern
, *fontpattern
;
1123 FcFontSet
*fcsets
[] = { NULL
};
1124 FcCharSet
*fccharset
;
1125 int i
, f
, numspecs
= 0;
1127 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
1128 /* Fetch rune and mode for current glyph. */
1130 mode
= glyphs
[i
].mode
;
1132 /* Skip dummy wide-character spacing. */
1133 if (mode
== ATTR_WDUMMY
)
1136 /* Determine font for glyph if different from previous glyph. */
1137 if (prevmode
!= mode
) {
1140 frcflags
= FRC_NORMAL
;
1141 runewidth
= win
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
1142 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
1144 frcflags
= FRC_ITALICBOLD
;
1145 } else if (mode
& ATTR_ITALIC
) {
1147 frcflags
= FRC_ITALIC
;
1148 } else if (mode
& ATTR_BOLD
) {
1150 frcflags
= FRC_BOLD
;
1152 yp
= winy
+ font
->ascent
;
1155 /* Lookup character index with default font. */
1156 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
1158 specs
[numspecs
].font
= font
->match
;
1159 specs
[numspecs
].glyph
= glyphidx
;
1160 specs
[numspecs
].x
= (short)xp
;
1161 specs
[numspecs
].y
= (short)yp
;
1167 /* Fallback on font cache, search the font cache for match. */
1168 for (f
= 0; f
< frclen
; f
++) {
1169 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
1170 /* Everything correct. */
1171 if (glyphidx
&& frc
[f
].flags
== frcflags
)
1173 /* We got a default font for a not found glyph. */
1174 if (!glyphidx
&& frc
[f
].flags
== frcflags
1175 && frc
[f
].unicodep
== rune
) {
1180 /* Nothing was found. Use fontconfig to find matching font. */
1183 font
->set
= FcFontSort(0, font
->pattern
,
1185 fcsets
[0] = font
->set
;
1188 * Nothing was found in the cache. Now use
1189 * some dozen of Fontconfig calls to get the
1190 * font for one single character.
1192 * Xft and fontconfig are design failures.
1194 fcpattern
= FcPatternDuplicate(font
->pattern
);
1195 fccharset
= FcCharSetCreate();
1197 FcCharSetAddChar(fccharset
, rune
);
1198 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
1200 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
1202 FcConfigSubstitute(0, fcpattern
,
1204 FcDefaultSubstitute(fcpattern
);
1206 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
1210 * Overwrite or create the new cache entry.
1212 if (frclen
>= LEN(frc
)) {
1213 frclen
= LEN(frc
) - 1;
1214 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
1215 frc
[frclen
].unicodep
= 0;
1218 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
1220 if (!frc
[frclen
].font
)
1221 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1223 frc
[frclen
].flags
= frcflags
;
1224 frc
[frclen
].unicodep
= rune
;
1226 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
1231 FcPatternDestroy(fcpattern
);
1232 FcCharSetDestroy(fccharset
);
1235 specs
[numspecs
].font
= frc
[f
].font
;
1236 specs
[numspecs
].glyph
= glyphidx
;
1237 specs
[numspecs
].x
= (short)xp
;
1238 specs
[numspecs
].y
= (short)yp
;
1247 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
1249 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
1250 int winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
,
1251 width
= charlen
* win
.cw
;
1252 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
1253 XRenderColor colfg
, colbg
;
1256 /* Fallback on color display for attributes not supported by the font */
1257 if (base
.mode
& ATTR_ITALIC
&& base
.mode
& ATTR_BOLD
) {
1258 if (dc
.ibfont
.badslant
|| dc
.ibfont
.badweight
)
1259 base
.fg
= defaultattr
;
1260 } else if ((base
.mode
& ATTR_ITALIC
&& dc
.ifont
.badslant
) ||
1261 (base
.mode
& ATTR_BOLD
&& dc
.bfont
.badweight
)) {
1262 base
.fg
= defaultattr
;
1265 if (IS_TRUECOL(base
.fg
)) {
1266 colfg
.alpha
= 0xffff;
1267 colfg
.red
= TRUERED(base
.fg
);
1268 colfg
.green
= TRUEGREEN(base
.fg
);
1269 colfg
.blue
= TRUEBLUE(base
.fg
);
1270 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
1273 fg
= &dc
.col
[base
.fg
];
1276 if (IS_TRUECOL(base
.bg
)) {
1277 colbg
.alpha
= 0xffff;
1278 colbg
.green
= TRUEGREEN(base
.bg
);
1279 colbg
.red
= TRUERED(base
.bg
);
1280 colbg
.blue
= TRUEBLUE(base
.bg
);
1281 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
1284 bg
= &dc
.col
[base
.bg
];
1287 /* Change basic system colors [0-7] to bright system colors [8-15] */
1288 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
1289 fg
= &dc
.col
[base
.fg
+ 8];
1291 if (IS_SET(MODE_REVERSE
)) {
1292 if (fg
== &dc
.col
[defaultfg
]) {
1293 fg
= &dc
.col
[defaultbg
];
1295 colfg
.red
= ~fg
->color
.red
;
1296 colfg
.green
= ~fg
->color
.green
;
1297 colfg
.blue
= ~fg
->color
.blue
;
1298 colfg
.alpha
= fg
->color
.alpha
;
1299 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
1304 if (bg
== &dc
.col
[defaultbg
]) {
1305 bg
= &dc
.col
[defaultfg
];
1307 colbg
.red
= ~bg
->color
.red
;
1308 colbg
.green
= ~bg
->color
.green
;
1309 colbg
.blue
= ~bg
->color
.blue
;
1310 colbg
.alpha
= bg
->color
.alpha
;
1311 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
1317 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
1318 colfg
.red
= fg
->color
.red
/ 2;
1319 colfg
.green
= fg
->color
.green
/ 2;
1320 colfg
.blue
= fg
->color
.blue
/ 2;
1321 colfg
.alpha
= fg
->color
.alpha
;
1322 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
1326 if (base
.mode
& ATTR_REVERSE
) {
1332 if (base
.mode
& ATTR_BLINK
&& win
.mode
& MODE_BLINK
)
1335 if (base
.mode
& ATTR_INVISIBLE
)
1338 /* Intelligent cleaning up of the borders. */
1340 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
1341 winy
+ win
.ch
+ ((y
>= term
.row
-1)? win
.h
: 0));
1343 if (x
+ charlen
>= term
.col
) {
1344 xclear(winx
+ width
, (y
== 0)? 0 : winy
, win
.w
,
1345 ((y
>= term
.row
-1)? win
.h
: (winy
+ win
.ch
)));
1348 xclear(winx
, 0, winx
+ width
, borderpx
);
1349 if (y
== term
.row
-1)
1350 xclear(winx
, winy
+ win
.ch
, winx
+ width
, win
.h
);
1352 /* Clean up the region we want to draw to. */
1353 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, win
.ch
);
1355 /* Set the clip region because Xft is sometimes dirty. */
1360 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
1362 /* Render the glyphs. */
1363 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
1365 /* Render underline and strikethrough. */
1366 if (base
.mode
& ATTR_UNDERLINE
) {
1367 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
1371 if (base
.mode
& ATTR_STRUCK
) {
1372 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
1376 /* Reset clip to none. */
1377 XftDrawSetClip(xw
.draw
, 0);
1381 xdrawglyph(Glyph g
, int x
, int y
)
1384 XftGlyphFontSpec spec
;
1386 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
1387 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
1393 static int oldx
= 0, oldy
= 0;
1395 Glyph g
= {' ', ATTR_NULL
, defaultbg
, defaultcs
}, og
;
1398 LIMIT(oldx
, 0, term
.col
-1);
1399 LIMIT(oldy
, 0, term
.row
-1);
1403 /* adjust position if in dummy */
1404 if (term
.line
[oldy
][oldx
].mode
& ATTR_WDUMMY
)
1406 if (term
.line
[term
.c
.y
][curx
].mode
& ATTR_WDUMMY
)
1409 /* remove the old cursor */
1410 og
= term
.line
[oldy
][oldx
];
1411 if (selected(oldx
, oldy
))
1412 og
.mode
^= ATTR_REVERSE
;
1413 xdrawglyph(og
, oldx
, oldy
);
1415 g
.u
= term
.line
[term
.c
.y
][term
.c
.x
].u
;
1416 g
.mode
|= term
.line
[term
.c
.y
][term
.c
.x
].mode
&
1417 (ATTR_BOLD
| ATTR_ITALIC
| ATTR_UNDERLINE
| ATTR_STRUCK
);
1420 * Select the right color for the right mode.
1422 if (IS_SET(MODE_REVERSE
)) {
1423 g
.mode
|= ATTR_REVERSE
;
1425 if (selected(term
.c
.x
, term
.c
.y
)) {
1426 drawcol
= dc
.col
[defaultcs
];
1429 drawcol
= dc
.col
[defaultrcs
];
1433 if (selected(term
.c
.x
, term
.c
.y
)) {
1434 drawcol
= dc
.col
[defaultrcs
];
1438 drawcol
= dc
.col
[defaultcs
];
1442 if (IS_SET(MODE_HIDE
))
1445 /* draw the new one */
1446 if (IS_SET(MODE_FOCUSED
)) {
1447 switch (win
.cursor
) {
1448 case 7: /* st extension: snowman */
1449 utf8decode("☃", &g
.u
, UTF_SIZ
);
1450 case 0: /* Blinking Block */
1451 case 1: /* Blinking Block (Default) */
1452 case 2: /* Steady Block */
1453 g
.mode
|= term
.line
[term
.c
.y
][curx
].mode
& ATTR_WIDE
;
1454 xdrawglyph(g
, term
.c
.x
, term
.c
.y
);
1456 case 3: /* Blinking Underline */
1457 case 4: /* Steady Underline */
1458 XftDrawRect(xw
.draw
, &drawcol
,
1459 borderpx
+ curx
* win
.cw
,
1460 borderpx
+ (term
.c
.y
+ 1) * win
.ch
- \
1462 win
.cw
, cursorthickness
);
1464 case 5: /* Blinking bar */
1465 case 6: /* Steady bar */
1466 XftDrawRect(xw
.draw
, &drawcol
,
1467 borderpx
+ curx
* win
.cw
,
1468 borderpx
+ term
.c
.y
* win
.ch
,
1469 cursorthickness
, win
.ch
);
1473 XftDrawRect(xw
.draw
, &drawcol
,
1474 borderpx
+ curx
* win
.cw
,
1475 borderpx
+ term
.c
.y
* win
.ch
,
1477 XftDrawRect(xw
.draw
, &drawcol
,
1478 borderpx
+ curx
* win
.cw
,
1479 borderpx
+ term
.c
.y
* win
.ch
,
1481 XftDrawRect(xw
.draw
, &drawcol
,
1482 borderpx
+ (curx
+ 1) * win
.cw
- 1,
1483 borderpx
+ term
.c
.y
* win
.ch
,
1485 XftDrawRect(xw
.draw
, &drawcol
,
1486 borderpx
+ curx
* win
.cw
,
1487 borderpx
+ (term
.c
.y
+ 1) * win
.ch
- 1,
1490 oldx
= curx
, oldy
= term
.c
.y
;
1496 char buf
[sizeof(long) * 8 + 1];
1498 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1499 setenv("WINDOWID", buf
, 1);
1508 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1510 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
1511 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
1518 drawregion(0, 0, term
.col
, term
.row
);
1519 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, win
.w
,
1521 XSetForeground(xw
.dpy
, dc
.gc
,
1522 dc
.col
[IS_SET(MODE_REVERSE
)?
1523 defaultfg
: defaultbg
].pixel
);
1527 drawregion(int x1
, int y1
, int x2
, int y2
)
1529 int i
, x
, y
, ox
, numspecs
;
1531 XftGlyphFontSpec
*specs
;
1533 if (!(IS_SET(MODE_VISIBLE
)))
1536 for (y
= y1
; y
< y2
; y
++) {
1543 numspecs
= xmakeglyphfontspecs(specs
, &term
.line
[y
][x1
], x2
- x1
, x1
, y
);
1546 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
1547 new = term
.line
[y
][x
];
1548 if (new.mode
== ATTR_WDUMMY
)
1551 new.mode
^= ATTR_REVERSE
;
1552 if (i
> 0 && ATTRCMP(base
, new)) {
1553 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
1565 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
1577 visibility(XEvent
*ev
)
1579 XVisibilityEvent
*e
= &ev
->xvisibility
;
1581 MODBIT(win
.mode
, e
->state
!= VisibilityFullyObscured
, MODE_VISIBLE
);
1587 win
.mode
&= ~MODE_VISIBLE
;
1591 xsetpointermotion(int set
)
1593 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
1594 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
1598 xsetmode(int set
, unsigned int flags
)
1600 int mode
= win
.mode
;
1601 MODBIT(win
.mode
, set
, flags
);
1602 if ((win
.mode
& MODE_REVERSE
) != (mode
& MODE_REVERSE
))
1607 xsetcursor(int cursor
)
1610 if (!BETWEEN(cursor
, 0, 6))
1612 win
.cursor
= cursor
;
1617 xseturgency(int add
)
1619 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1621 MODBIT(h
->flags
, add
, XUrgencyHint
);
1622 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1629 if (!(IS_SET(MODE_FOCUSED
)))
1632 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
1638 XFocusChangeEvent
*e
= &ev
->xfocus
;
1640 if (e
->mode
== NotifyGrab
)
1643 if (ev
->type
== FocusIn
) {
1644 XSetICFocus(xw
.xic
);
1645 win
.mode
|= MODE_FOCUSED
;
1647 if (IS_SET(MODE_FOCUS
))
1648 ttywrite("\033[I", 3, 0);
1650 XUnsetICFocus(xw
.xic
);
1651 win
.mode
&= ~MODE_FOCUSED
;
1652 if (IS_SET(MODE_FOCUS
))
1653 ttywrite("\033[O", 3, 0);
1658 match(uint mask
, uint state
)
1660 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
1664 kmap(KeySym k
, uint state
)
1669 /* Check for mapped keys out of X11 function keys. */
1670 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
1671 if (mappedkeys
[i
] == k
)
1674 if (i
== LEN(mappedkeys
)) {
1675 if ((k
& 0xFFFF) < 0xFD00)
1679 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
1683 if (!match(kp
->mask
, state
))
1686 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
1688 if (IS_SET(MODE_NUMLOCK
) && kp
->appkey
== 2)
1691 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
1703 XKeyEvent
*e
= &ev
->xkey
;
1705 char buf
[32], *customkey
;
1711 if (IS_SET(MODE_KBDLOCK
))
1714 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
1716 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
1717 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
1718 bp
->func(&(bp
->arg
));
1723 /* 2. custom keys from config.h */
1724 if ((customkey
= kmap(ksym
, e
->state
))) {
1725 ttywrite(customkey
, strlen(customkey
), 1);
1729 /* 3. composed string from input method */
1732 if (len
== 1 && e
->state
& Mod1Mask
) {
1733 if (IS_SET(MODE_8BIT
)) {
1736 len
= utf8encode(c
, buf
);
1744 ttywrite(buf
, len
, 1);
1753 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1755 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1756 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1757 win
.mode
|= MODE_FOCUSED
;
1759 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1760 win
.mode
&= ~MODE_FOCUSED
;
1762 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
1763 /* Send SIGHUP to shell */
1772 if (e
->xconfigure
.width
== win
.w
&& e
->xconfigure
.height
== win
.h
)
1775 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
1782 int w
= win
.w
, h
= win
.h
;
1784 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
1785 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
1788 /* Waiting for window mapping */
1790 XNextEvent(xw
.dpy
, &ev
);
1792 * This XFilterEvent call is required because of XOpenIM. It
1793 * does filter out the key event and some client message for
1794 * the input method too.
1796 if (XFilterEvent(&ev
, None
))
1798 if (ev
.type
== ConfigureNotify
) {
1799 w
= ev
.xconfigure
.width
;
1800 h
= ev
.xconfigure
.height
;
1802 } while (ev
.type
!= MapNotify
);
1804 ttynew(opt_line
, opt_io
, opt_cmd
);
1807 clock_gettime(CLOCK_MONOTONIC
, &last
);
1810 for (xev
= actionfps
;;) {
1812 FD_SET(cmdfd
, &rfd
);
1815 if (pselect(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
1818 die("select failed: %s\n", strerror(errno
));
1820 if (FD_ISSET(cmdfd
, &rfd
)) {
1823 blinkset
= tattrset(ATTR_BLINK
);
1825 MODBIT(win
.mode
, 0, MODE_BLINK
);
1829 if (FD_ISSET(xfd
, &rfd
))
1832 clock_gettime(CLOCK_MONOTONIC
, &now
);
1833 drawtimeout
.tv_sec
= 0;
1834 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
1838 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
1839 tsetdirtattr(ATTR_BLINK
);
1840 win
.mode
^= MODE_BLINK
;
1844 deltatime
= TIMEDIFF(now
, last
);
1845 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
1851 while (XPending(xw
.dpy
)) {
1852 XNextEvent(xw
.dpy
, &ev
);
1853 if (XFilterEvent(&ev
, None
))
1855 if (handler
[ev
.type
])
1856 (handler
[ev
.type
])(&ev
);
1862 if (xev
&& !FD_ISSET(xfd
, &rfd
))
1864 if (!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
1866 if (TIMEDIFF(now
, lastblink
) \
1868 drawtimeout
.tv_nsec
= 1000;
1870 drawtimeout
.tv_nsec
= (1E6
* \
1875 drawtimeout
.tv_sec
= \
1876 drawtimeout
.tv_nsec
/ 1E9
;
1877 drawtimeout
.tv_nsec
%= (long)1E9
;
1889 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
1890 " [-n name] [-o file]\n"
1891 " [-T title] [-t title] [-w windowid]"
1892 " [[-e] command [args ...]]\n"
1893 " %s [-aiv] [-c class] [-f font] [-g geometry]"
1894 " [-n name] [-o file]\n"
1895 " [-T title] [-t title] [-w windowid] -l line"
1896 " [stty_args ...]\n", argv0
, argv0
);
1900 main(int argc
, char *argv
[])
1904 win
.cursor
= cursorshape
;
1911 opt_class
= EARGF(usage());
1918 opt_font
= EARGF(usage());
1921 xw
.gm
= XParseGeometry(EARGF(usage()),
1922 &xw
.l
, &xw
.t
, &cols
, &rows
);
1928 opt_io
= EARGF(usage());
1931 opt_line
= EARGF(usage());
1934 opt_name
= EARGF(usage());
1938 opt_title
= EARGF(usage());
1941 opt_embed
= EARGF(usage());
1944 die("%s " VERSION
" (c) 2010-2016 st engineers\n", argv0
);
1952 /* eat all remaining arguments */
1954 if (!opt_title
&& !opt_line
)
1955 opt_title
= basename(xstrdup(argv
[0]));
1957 setlocale(LC_CTYPE
, "");
1958 XSetLocaleModifiers("");
1959 tnew(MAX(cols
, 1), MAX(rows
, 1));