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 */
44 signed char crlf
; /* crlf mode */
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 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 TRUERED(x) (((x) & 0xff0000) >> 8)
69 #define TRUEGREEN(x) (((x) & 0xff00))
70 #define TRUEBLUE(x) (((x) & 0xff) << 8)
72 typedef XftDraw
*Draw
;
73 typedef XftColor Color
;
74 typedef XftGlyphFontSpec GlyphFontSpec
;
76 /* Purely graphic info */
82 GlyphFontSpec
*specbuf
; /* font spec buffer used for rendering */
83 Atom xembed
, wmdeletewin
, netwmname
, netwmpid
;
88 XSetWindowAttributes attrs
;
90 int isfixed
; /* is fixed geometry? */
91 int l
, t
; /* left and top offset */
92 int gm
; /* geometry mask */
115 /* Drawing Context */
119 Font font
, bfont
, ifont
, ibfont
;
123 static inline ushort
sixd_to_16bit(int);
124 static int xmakeglyphfontspecs(XftGlyphFontSpec
*, const Glyph
*, int, int, int);
125 static void xdrawglyphfontspecs(const XftGlyphFontSpec
*, Glyph
, int, int, int);
126 static void xdrawglyph(Glyph
, int, int);
127 static void xclear(int, int, int, int);
128 static void xdrawcursor(void);
129 static int xgeommasktogravity(int);
130 static void xinit(void);
131 static void cresize(int, int);
132 static void xresize(int, int);
133 static int xloadfont(Font
*, FcPattern
*);
134 static void xloadfonts(char *, double);
135 static void xunloadfont(Font
*);
136 static void xunloadfonts(void);
137 static void xsetenv(void);
138 static void xseturgency(int);
139 static int x2col(int);
140 static int y2row(int);
142 static void expose(XEvent
*);
143 static void visibility(XEvent
*);
144 static void unmap(XEvent
*);
145 static void kpress(XEvent
*);
146 static void cmessage(XEvent
*);
147 static void resize(XEvent
*);
148 static void focus(XEvent
*);
149 static void brelease(XEvent
*);
150 static void bpress(XEvent
*);
151 static void bmotion(XEvent
*);
152 static void propnotify(XEvent
*);
153 static void selnotify(XEvent
*);
154 static void selclear_(XEvent
*);
155 static void selrequest(XEvent
*);
156 static void setsel(char *, Time
);
157 static void getbuttoninfo(XEvent
*);
158 static void mousereport(XEvent
*);
159 static char *kmap(KeySym
, uint
);
160 static int match(uint
, uint
);
162 static void run(void);
163 static void usage(void);
165 static void (*handler
[LASTEvent
])(XEvent
*) = {
167 [ClientMessage
] = cmessage
,
168 [ConfigureNotify
] = resize
,
169 [VisibilityNotify
] = visibility
,
170 [UnmapNotify
] = unmap
,
174 [MotionNotify
] = bmotion
,
175 [ButtonPress
] = bpress
,
176 [ButtonRelease
] = brelease
,
178 * Uncomment if you want the selection to disappear when you select something
179 * different in another window.
181 /* [SelectionClear] = selclear_, */
182 [SelectionNotify
] = selnotify
,
184 * PropertyNotify is only turned on when there is some INCR transfer happening
185 * for the selection retrieval.
187 [PropertyNotify
] = propnotify
,
188 [SelectionRequest
] = selrequest
,
194 static XSelection xsel
;
195 static TermWindow win
;
202 /* Font Ring Cache */
216 /* Fontcache is an array now. A new font will be appended to the array. */
217 static Fontcache frc
[16];
218 static int frclen
= 0;
219 static char *usedfont
= NULL
;
220 static double usedfontsize
= 0;
221 static double defaultfontsize
= 0;
223 static char *opt_class
= NULL
;
224 static char **opt_cmd
= NULL
;
225 static char *opt_embed
= NULL
;
226 static char *opt_font
= NULL
;
227 static char *opt_io
= NULL
;
228 static char *opt_line
= NULL
;
229 static char *opt_name
= NULL
;
230 static char *opt_title
= NULL
;
233 clipcopy(const Arg
*dummy
)
237 if (sel
.clipboard
!= NULL
)
240 if (sel
.primary
!= NULL
) {
241 sel
.clipboard
= xstrdup(sel
.primary
);
242 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
243 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
248 clippaste(const Arg
*dummy
)
252 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
253 XConvertSelection(xw
.dpy
, clipboard
, xsel
.xtarget
, clipboard
,
254 xw
.win
, CurrentTime
);
258 selpaste(const Arg
*dummy
)
260 XConvertSelection(xw
.dpy
, XA_PRIMARY
, xsel
.xtarget
, XA_PRIMARY
,
261 xw
.win
, CurrentTime
);
269 larg
.f
= usedfontsize
+ arg
->f
;
274 zoomabs(const Arg
*arg
)
277 xloadfonts(usedfont
, arg
->f
);
284 zoomreset(const Arg
*arg
)
288 if (defaultfontsize
> 0) {
289 larg
.f
= defaultfontsize
;
300 return LIMIT(x
, 0, term
.col
-1);
309 return LIMIT(y
, 0, term
.row
-1);
313 getbuttoninfo(XEvent
*e
)
316 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forceselmod
);
318 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
320 sel
.oe
.x
= x2col(e
->xbutton
.x
);
321 sel
.oe
.y
= y2row(e
->xbutton
.y
);
324 sel
.type
= SEL_REGULAR
;
325 for (type
= 1; type
< LEN(selmasks
); ++type
) {
326 if (match(selmasks
[type
], state
)) {
334 mousereport(XEvent
*e
)
336 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
337 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
343 if (e
->xbutton
.type
== MotionNotify
) {
344 if (x
== ox
&& y
== oy
)
346 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
348 /* MOUSE_MOTION: no reporting if no button is pressed */
349 if (IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
352 button
= oldbutton
+ 32;
356 if (!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
363 if (e
->xbutton
.type
== ButtonPress
) {
367 } else if (e
->xbutton
.type
== ButtonRelease
) {
369 /* MODE_MOUSEX10: no button release reporting */
370 if (IS_SET(MODE_MOUSEX10
))
372 if (button
== 64 || button
== 65)
377 if (!IS_SET(MODE_MOUSEX10
)) {
378 button
+= ((state
& ShiftMask
) ? 4 : 0)
379 + ((state
& Mod4Mask
) ? 8 : 0)
380 + ((state
& ControlMask
) ? 16 : 0);
383 if (IS_SET(MODE_MOUSESGR
)) {
384 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
386 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
387 } else if (x
< 223 && y
< 223) {
388 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
389 32+button
, 32+x
+1, 32+y
+1);
403 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
408 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
409 if (e
->xbutton
.button
== ms
->b
410 && match(ms
->mask
, e
->xbutton
.state
)) {
411 ttysend(ms
->s
, strlen(ms
->s
));
416 if (e
->xbutton
.button
== Button1
) {
417 clock_gettime(CLOCK_MONOTONIC
, &now
);
419 /* Clear previous selection, logically and visually. */
421 sel
.mode
= SEL_EMPTY
;
422 sel
.type
= SEL_REGULAR
;
423 sel
.oe
.x
= sel
.ob
.x
= x2col(e
->xbutton
.x
);
424 sel
.oe
.y
= sel
.ob
.y
= y2row(e
->xbutton
.y
);
427 * If the user clicks below predefined timeouts specific
428 * snapping behaviour is exposed.
430 if (TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
431 sel
.snap
= SNAP_LINE
;
432 } else if (TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
433 sel
.snap
= SNAP_WORD
;
440 sel
.mode
= SEL_READY
;
441 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
442 sel
.tclick2
= sel
.tclick1
;
448 propnotify(XEvent
*e
)
450 XPropertyEvent
*xpev
;
451 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
453 xpev
= &e
->xproperty
;
454 if (xpev
->state
== PropertyNewValue
&&
455 (xpev
->atom
== XA_PRIMARY
||
456 xpev
->atom
== clipboard
)) {
464 ulong nitems
, ofs
, rem
;
466 uchar
*data
, *last
, *repl
;
467 Atom type
, incratom
, property
;
469 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
472 if (e
->type
== SelectionNotify
) {
473 property
= e
->xselection
.property
;
474 } else if(e
->type
== PropertyNotify
) {
475 property
= e
->xproperty
.atom
;
479 if (property
== None
)
483 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
484 BUFSIZ
/4, False
, AnyPropertyType
,
485 &type
, &format
, &nitems
, &rem
,
487 fprintf(stderr
, "Clipboard allocation failed\n");
491 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
493 * If there is some PropertyNotify with no data, then
494 * this is the signal of the selection owner that all
495 * data has been transferred. We won't need to receive
496 * PropertyNotify events anymore.
498 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
499 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
503 if (type
== incratom
) {
505 * Activate the PropertyNotify events so we receive
506 * when the selection owner does send us the next
509 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
510 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
514 * Deleting the property is the transfer start signal.
516 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
522 * Line endings are inconsistent in the terminal and GUI world
523 * copy and pasting. When receiving some selection data,
524 * replace all '\n' with '\r'.
525 * FIXME: Fix the computer world.
528 last
= data
+ nitems
* format
/ 8;
529 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
533 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
534 ttywrite("\033[200~", 6);
535 ttysend((char *)data
, nitems
* format
/ 8);
536 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
537 ttywrite("\033[201~", 6);
539 /* number of 32-bit chunks returned */
540 ofs
+= nitems
* format
/ 32;
544 * Deleting the property again tells the selection owner to send the
545 * next data chunk in the property.
547 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
563 selrequest(XEvent
*e
)
565 XSelectionRequestEvent
*xsre
;
567 Atom xa_targets
, string
, clipboard
;
570 xsre
= (XSelectionRequestEvent
*) e
;
571 xev
.type
= SelectionNotify
;
572 xev
.requestor
= xsre
->requestor
;
573 xev
.selection
= xsre
->selection
;
574 xev
.target
= xsre
->target
;
575 xev
.time
= xsre
->time
;
576 if (xsre
->property
== None
)
577 xsre
->property
= xsre
->target
;
582 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
583 if (xsre
->target
== xa_targets
) {
584 /* respond with the supported type */
585 string
= xsel
.xtarget
;
586 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
587 XA_ATOM
, 32, PropModeReplace
,
588 (uchar
*) &string
, 1);
589 xev
.property
= xsre
->property
;
590 } else if (xsre
->target
== xsel
.xtarget
|| xsre
->target
== XA_STRING
) {
592 * xith XA_STRING non ascii characters may be incorrect in the
593 * requestor. It is not our problem, use utf8.
595 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
596 if (xsre
->selection
== XA_PRIMARY
) {
597 seltext
= sel
.primary
;
598 } else if (xsre
->selection
== clipboard
) {
599 seltext
= sel
.clipboard
;
602 "Unhandled clipboard selection 0x%lx\n",
606 if (seltext
!= NULL
) {
607 XChangeProperty(xsre
->display
, xsre
->requestor
,
608 xsre
->property
, xsre
->target
,
610 (uchar
*)seltext
, strlen(seltext
));
611 xev
.property
= xsre
->property
;
615 /* all done, send a notification to the listener */
616 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
617 fprintf(stderr
, "Error sending SelectionNotify event\n");
621 setsel(char *str
, Time t
)
626 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
627 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
634 setsel(str
, CurrentTime
);
640 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
645 if (e
->xbutton
.button
== Button2
) {
647 } else if (e
->xbutton
.button
== Button1
) {
648 if (sel
.mode
== SEL_READY
) {
650 setsel(getsel(), e
->xbutton
.time
);
654 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
661 int oldey
, oldex
, oldsby
, oldsey
;
663 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
671 sel
.mode
= SEL_READY
;
678 if (oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
679 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
683 cresize(int width
, int height
)
692 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
693 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
697 ttyresize(win
.tw
, win
.th
);
701 xresize(int col
, int row
)
703 win
.tw
= MAX(1, col
* win
.cw
);
704 win
.th
= MAX(1, row
* win
.ch
);
706 XFreePixmap(xw
.dpy
, xw
.buf
);
707 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
708 DefaultDepth(xw
.dpy
, xw
.scr
));
709 XftDrawChange(xw
.draw
, xw
.buf
);
710 xclear(0, 0, win
.w
, win
.h
);
712 /* resize to new width */
713 xw
.specbuf
= xrealloc(xw
.specbuf
, col
* sizeof(GlyphFontSpec
));
719 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
723 xloadcolor(int i
, const char *name
, Color
*ncolor
)
725 XRenderColor color
= { .alpha
= 0xffff };
728 if (BETWEEN(i
, 16, 255)) { /* 256 color */
729 if (i
< 6*6*6+16) { /* same colors as xterm */
730 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
731 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
732 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
733 } else { /* greyscale */
734 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
735 color
.green
= color
.blue
= color
.red
;
737 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
738 xw
.cmap
, &color
, ncolor
);
743 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
753 dc
.collen
= MAX(LEN(colorname
), 256);
754 dc
.col
= xmalloc(dc
.collen
* sizeof(Color
));
757 for (cp
= dc
.col
; cp
< &dc
.col
[dc
.collen
]; ++cp
)
758 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
761 for (i
= 0; i
< dc
.collen
; i
++)
762 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
764 die("Could not allocate color '%s'\n", colorname
[i
]);
766 die("Could not allocate color %d\n", i
);
772 xsetcolorname(int x
, const char *name
)
776 if (!BETWEEN(x
, 0, dc
.collen
))
780 if (!xloadcolor(x
, name
, &ncolor
))
783 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
790 * Absolute coordinates.
793 xclear(int x1
, int y1
, int x2
, int y2
)
796 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
797 x1
, y1
, x2
-x1
, y2
-y1
);
803 XClassHint
class = {opt_name
? opt_name
: termname
,
804 opt_class
? opt_class
: termname
};
805 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
806 XSizeHints
*sizeh
= NULL
;
808 sizeh
= XAllocSizeHints();
810 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
811 sizeh
->height
= win
.h
;
812 sizeh
->width
= win
.w
;
813 sizeh
->height_inc
= win
.ch
;
814 sizeh
->width_inc
= win
.cw
;
815 sizeh
->base_height
= 2 * borderpx
;
816 sizeh
->base_width
= 2 * borderpx
;
818 sizeh
->flags
|= PMaxSize
| PMinSize
;
819 sizeh
->min_width
= sizeh
->max_width
= win
.w
;
820 sizeh
->min_height
= sizeh
->max_height
= win
.h
;
822 if (xw
.gm
& (XValue
|YValue
)) {
823 sizeh
->flags
|= USPosition
| PWinGravity
;
826 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
829 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
835 xgeommasktogravity(int mask
)
837 switch (mask
& (XNegative
|YNegative
)) {
839 return NorthWestGravity
;
841 return NorthEastGravity
;
843 return SouthWestGravity
;
846 return SouthEastGravity
;
850 xloadfont(Font
*f
, FcPattern
*pattern
)
852 FcPattern
*configured
;
856 int wantattr
, haveattr
;
859 * Manually configure instead of calling XftMatchFont
860 * so that we can use the configured pattern for
861 * "missing glyph" lookups.
863 configured
= FcPatternDuplicate(pattern
);
867 FcConfigSubstitute(NULL
, configured
, FcMatchPattern
);
868 XftDefaultSubstitute(xw
.dpy
, xw
.scr
, configured
);
870 match
= FcFontMatch(NULL
, configured
, &result
);
872 FcPatternDestroy(configured
);
876 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
877 FcPatternDestroy(configured
);
878 FcPatternDestroy(match
);
882 if ((XftPatternGetInteger(pattern
, "slant", 0, &wantattr
) ==
885 * Check if xft was unable to find a font with the appropriate
886 * slant but gave us one anyway. Try to mitigate.
888 if ((XftPatternGetInteger(f
->match
->pattern
, "slant", 0,
889 &haveattr
) != XftResultMatch
) || haveattr
< wantattr
) {
891 fputs("st: font slant does not match\n", stderr
);
895 if ((XftPatternGetInteger(pattern
, "weight", 0, &wantattr
) ==
897 if ((XftPatternGetInteger(f
->match
->pattern
, "weight", 0,
898 &haveattr
) != XftResultMatch
) || haveattr
!= wantattr
) {
900 fputs("st: font weight does not match\n", stderr
);
904 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
905 (const FcChar8
*) ascii_printable
,
906 strlen(ascii_printable
), &extents
);
909 f
->pattern
= configured
;
911 f
->ascent
= f
->match
->ascent
;
912 f
->descent
= f
->match
->descent
;
914 f
->rbearing
= f
->match
->max_advance_width
;
916 f
->height
= f
->ascent
+ f
->descent
;
917 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
923 xloadfonts(char *fontstr
, double fontsize
)
929 if (fontstr
[0] == '-') {
930 pattern
= XftXlfdParse(fontstr
, False
, False
);
932 pattern
= FcNameParse((FcChar8
*)fontstr
);
936 die("st: can't open font %s\n", fontstr
);
939 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
940 FcPatternDel(pattern
, FC_SIZE
);
941 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
942 usedfontsize
= fontsize
;
944 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
946 usedfontsize
= fontval
;
947 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
952 * Default font size is 12, if none given. This is to
953 * have a known usedfontsize value.
955 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
958 defaultfontsize
= usedfontsize
;
961 if (xloadfont(&dc
.font
, pattern
))
962 die("st: can't open font %s\n", fontstr
);
964 if (usedfontsize
< 0) {
965 FcPatternGetDouble(dc
.font
.match
->pattern
,
966 FC_PIXEL_SIZE
, 0, &fontval
);
967 usedfontsize
= fontval
;
969 defaultfontsize
= fontval
;
972 /* Setting character width and height. */
973 win
.cw
= ceilf(dc
.font
.width
* cwscale
);
974 win
.ch
= ceilf(dc
.font
.height
* chscale
);
976 FcPatternDel(pattern
, FC_SLANT
);
977 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
978 if (xloadfont(&dc
.ifont
, pattern
))
979 die("st: can't open font %s\n", fontstr
);
981 FcPatternDel(pattern
, FC_WEIGHT
);
982 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
983 if (xloadfont(&dc
.ibfont
, pattern
))
984 die("st: can't open font %s\n", fontstr
);
986 FcPatternDel(pattern
, FC_SLANT
);
987 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
988 if (xloadfont(&dc
.bfont
, pattern
))
989 die("st: can't open font %s\n", fontstr
);
991 FcPatternDestroy(pattern
);
997 XftFontClose(xw
.dpy
, f
->match
);
998 FcPatternDestroy(f
->pattern
);
1000 FcFontSetDestroy(f
->set
);
1006 /* Free the loaded fonts in the font cache. */
1008 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
1010 xunloadfont(&dc
.font
);
1011 xunloadfont(&dc
.bfont
);
1012 xunloadfont(&dc
.ifont
);
1013 xunloadfont(&dc
.ibfont
);
1022 pid_t thispid
= getpid();
1023 XColor xmousefg
, xmousebg
;
1025 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
1026 die("Can't open display\n");
1027 xw
.scr
= XDefaultScreen(xw
.dpy
);
1028 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
1032 die("Could not init fontconfig.\n");
1034 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
1035 xloadfonts(usedfont
, 0);
1038 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1041 /* adjust fixed window geometry */
1042 win
.w
= 2 * borderpx
+ term
.col
* win
.cw
;
1043 win
.h
= 2 * borderpx
+ term
.row
* win
.ch
;
1044 if (xw
.gm
& XNegative
)
1045 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - win
.w
- 2;
1046 if (xw
.gm
& YNegative
)
1047 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - win
.h
- 2;
1050 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
1051 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
1052 xw
.attrs
.bit_gravity
= NorthWestGravity
;
1053 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1054 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1055 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1056 xw
.attrs
.colormap
= xw
.cmap
;
1058 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
1059 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
1060 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
1061 win
.w
, win
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1062 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
1063 | CWEventMask
| CWColormap
, &xw
.attrs
);
1065 memset(&gcvalues
, 0, sizeof(gcvalues
));
1066 gcvalues
.graphics_exposures
= False
;
1067 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
1069 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
1070 DefaultDepth(xw
.dpy
, xw
.scr
));
1071 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
1072 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, win
.w
, win
.h
);
1074 /* font spec buffer */
1075 xw
.specbuf
= xmalloc(term
.col
* sizeof(GlyphFontSpec
));
1077 /* Xft rendering context */
1078 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
1081 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1082 XSetLocaleModifiers("@im=local");
1083 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1084 XSetLocaleModifiers("@im=");
1085 if ((xw
.xim
= XOpenIM(xw
.dpy
,
1086 NULL
, NULL
, NULL
)) == NULL
) {
1087 die("XOpenIM failed. Could not open input"
1092 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1093 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1094 XNFocusWindow
, xw
.win
, NULL
);
1096 die("XCreateIC failed. Could not obtain input method.\n");
1098 /* white cursor, black outline */
1099 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
1100 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1102 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
1103 xmousefg
.red
= 0xffff;
1104 xmousefg
.green
= 0xffff;
1105 xmousefg
.blue
= 0xffff;
1108 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
1109 xmousebg
.red
= 0x0000;
1110 xmousebg
.green
= 0x0000;
1111 xmousebg
.blue
= 0x0000;
1114 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
1116 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1117 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
1118 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
1119 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
1121 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
1122 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
1123 PropModeReplace
, (uchar
*)&thispid
, 1);
1126 XMapWindow(xw
.dpy
, xw
.win
);
1128 XSync(xw
.dpy
, False
);
1130 xsel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
1131 if (xsel
.xtarget
== None
)
1132 xsel
.xtarget
= XA_STRING
;
1136 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
1138 float winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
, xp
, yp
;
1139 ushort mode
, prevmode
= USHRT_MAX
;
1140 Font
*font
= &dc
.font
;
1141 int frcflags
= FRC_NORMAL
;
1142 float runewidth
= win
.cw
;
1146 FcPattern
*fcpattern
, *fontpattern
;
1147 FcFontSet
*fcsets
[] = { NULL
};
1148 FcCharSet
*fccharset
;
1149 int i
, f
, numspecs
= 0;
1151 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
1152 /* Fetch rune and mode for current glyph. */
1154 mode
= glyphs
[i
].mode
;
1156 /* Skip dummy wide-character spacing. */
1157 if (mode
== ATTR_WDUMMY
)
1160 /* Determine font for glyph if different from previous glyph. */
1161 if (prevmode
!= mode
) {
1164 frcflags
= FRC_NORMAL
;
1165 runewidth
= win
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
1166 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
1168 frcflags
= FRC_ITALICBOLD
;
1169 } else if (mode
& ATTR_ITALIC
) {
1171 frcflags
= FRC_ITALIC
;
1172 } else if (mode
& ATTR_BOLD
) {
1174 frcflags
= FRC_BOLD
;
1176 yp
= winy
+ font
->ascent
;
1179 /* Lookup character index with default font. */
1180 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
1182 specs
[numspecs
].font
= font
->match
;
1183 specs
[numspecs
].glyph
= glyphidx
;
1184 specs
[numspecs
].x
= (short)xp
;
1185 specs
[numspecs
].y
= (short)yp
;
1191 /* Fallback on font cache, search the font cache for match. */
1192 for (f
= 0; f
< frclen
; f
++) {
1193 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
1194 /* Everything correct. */
1195 if (glyphidx
&& frc
[f
].flags
== frcflags
)
1197 /* We got a default font for a not found glyph. */
1198 if (!glyphidx
&& frc
[f
].flags
== frcflags
1199 && frc
[f
].unicodep
== rune
) {
1204 /* Nothing was found. Use fontconfig to find matching font. */
1207 font
->set
= FcFontSort(0, font
->pattern
,
1209 fcsets
[0] = font
->set
;
1212 * Nothing was found in the cache. Now use
1213 * some dozen of Fontconfig calls to get the
1214 * font for one single character.
1216 * Xft and fontconfig are design failures.
1218 fcpattern
= FcPatternDuplicate(font
->pattern
);
1219 fccharset
= FcCharSetCreate();
1221 FcCharSetAddChar(fccharset
, rune
);
1222 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
1224 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
1226 FcConfigSubstitute(0, fcpattern
,
1228 FcDefaultSubstitute(fcpattern
);
1230 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
1234 * Overwrite or create the new cache entry.
1236 if (frclen
>= LEN(frc
)) {
1237 frclen
= LEN(frc
) - 1;
1238 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
1239 frc
[frclen
].unicodep
= 0;
1242 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
1244 if (!frc
[frclen
].font
)
1245 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1247 frc
[frclen
].flags
= frcflags
;
1248 frc
[frclen
].unicodep
= rune
;
1250 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
1255 FcPatternDestroy(fcpattern
);
1256 FcCharSetDestroy(fccharset
);
1259 specs
[numspecs
].font
= frc
[f
].font
;
1260 specs
[numspecs
].glyph
= glyphidx
;
1261 specs
[numspecs
].x
= (short)xp
;
1262 specs
[numspecs
].y
= (short)yp
;
1271 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
1273 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
1274 int winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
,
1275 width
= charlen
* win
.cw
;
1276 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
1277 XRenderColor colfg
, colbg
;
1280 /* Fallback on color display for attributes not supported by the font */
1281 if (base
.mode
& ATTR_ITALIC
&& base
.mode
& ATTR_BOLD
) {
1282 if (dc
.ibfont
.badslant
|| dc
.ibfont
.badweight
)
1283 base
.fg
= defaultattr
;
1284 } else if ((base
.mode
& ATTR_ITALIC
&& dc
.ifont
.badslant
) ||
1285 (base
.mode
& ATTR_BOLD
&& dc
.bfont
.badweight
)) {
1286 base
.fg
= defaultattr
;
1289 if (IS_TRUECOL(base
.fg
)) {
1290 colfg
.alpha
= 0xffff;
1291 colfg
.red
= TRUERED(base
.fg
);
1292 colfg
.green
= TRUEGREEN(base
.fg
);
1293 colfg
.blue
= TRUEBLUE(base
.fg
);
1294 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
1297 fg
= &dc
.col
[base
.fg
];
1300 if (IS_TRUECOL(base
.bg
)) {
1301 colbg
.alpha
= 0xffff;
1302 colbg
.green
= TRUEGREEN(base
.bg
);
1303 colbg
.red
= TRUERED(base
.bg
);
1304 colbg
.blue
= TRUEBLUE(base
.bg
);
1305 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
1308 bg
= &dc
.col
[base
.bg
];
1311 /* Change basic system colors [0-7] to bright system colors [8-15] */
1312 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
1313 fg
= &dc
.col
[base
.fg
+ 8];
1315 if (IS_SET(MODE_REVERSE
)) {
1316 if (fg
== &dc
.col
[defaultfg
]) {
1317 fg
= &dc
.col
[defaultbg
];
1319 colfg
.red
= ~fg
->color
.red
;
1320 colfg
.green
= ~fg
->color
.green
;
1321 colfg
.blue
= ~fg
->color
.blue
;
1322 colfg
.alpha
= fg
->color
.alpha
;
1323 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
1328 if (bg
== &dc
.col
[defaultbg
]) {
1329 bg
= &dc
.col
[defaultfg
];
1331 colbg
.red
= ~bg
->color
.red
;
1332 colbg
.green
= ~bg
->color
.green
;
1333 colbg
.blue
= ~bg
->color
.blue
;
1334 colbg
.alpha
= bg
->color
.alpha
;
1335 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
1341 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
1342 colfg
.red
= fg
->color
.red
/ 2;
1343 colfg
.green
= fg
->color
.green
/ 2;
1344 colfg
.blue
= fg
->color
.blue
/ 2;
1345 colfg
.alpha
= fg
->color
.alpha
;
1346 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
1351 if (base
.mode
& ATTR_REVERSE
) {
1357 if (base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
1360 if (base
.mode
& ATTR_INVISIBLE
)
1363 /* Intelligent cleaning up of the borders. */
1365 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
1366 winy
+ win
.ch
+ ((y
>= term
.row
-1)? win
.h
: 0));
1368 if (x
+ charlen
>= term
.col
) {
1369 xclear(winx
+ width
, (y
== 0)? 0 : winy
, win
.w
,
1370 ((y
>= term
.row
-1)? win
.h
: (winy
+ win
.ch
)));
1373 xclear(winx
, 0, winx
+ width
, borderpx
);
1374 if (y
== term
.row
-1)
1375 xclear(winx
, winy
+ win
.ch
, winx
+ width
, win
.h
);
1377 /* Clean up the region we want to draw to. */
1378 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, win
.ch
);
1380 /* Set the clip region because Xft is sometimes dirty. */
1385 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
1387 /* Render the glyphs. */
1388 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
1390 /* Render underline and strikethrough. */
1391 if (base
.mode
& ATTR_UNDERLINE
) {
1392 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
1396 if (base
.mode
& ATTR_STRUCK
) {
1397 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
1401 /* Reset clip to none. */
1402 XftDrawSetClip(xw
.draw
, 0);
1406 xdrawglyph(Glyph g
, int x
, int y
)
1409 XftGlyphFontSpec spec
;
1411 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
1412 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
1418 static int oldx
= 0, oldy
= 0;
1420 Glyph g
= {' ', ATTR_NULL
, defaultbg
, defaultcs
}, og
;
1421 int ena_sel
= sel
.ob
.x
!= -1 && sel
.alt
== IS_SET(MODE_ALTSCREEN
);
1424 LIMIT(oldx
, 0, term
.col
-1);
1425 LIMIT(oldy
, 0, term
.row
-1);
1429 /* adjust position if in dummy */
1430 if (term
.line
[oldy
][oldx
].mode
& ATTR_WDUMMY
)
1432 if (term
.line
[term
.c
.y
][curx
].mode
& ATTR_WDUMMY
)
1435 /* remove the old cursor */
1436 og
= term
.line
[oldy
][oldx
];
1437 if (ena_sel
&& selected(oldx
, oldy
))
1438 og
.mode
^= ATTR_REVERSE
;
1439 xdrawglyph(og
, oldx
, oldy
);
1441 g
.u
= term
.line
[term
.c
.y
][term
.c
.x
].u
;
1442 g
.mode
|= term
.line
[term
.c
.y
][term
.c
.x
].mode
&
1443 (ATTR_BOLD
| ATTR_ITALIC
| ATTR_UNDERLINE
| ATTR_STRUCK
);
1446 * Select the right color for the right mode.
1448 if (IS_SET(MODE_REVERSE
)) {
1449 g
.mode
|= ATTR_REVERSE
;
1451 if (ena_sel
&& selected(term
.c
.x
, term
.c
.y
)) {
1452 drawcol
= dc
.col
[defaultcs
];
1455 drawcol
= dc
.col
[defaultrcs
];
1459 if (ena_sel
&& selected(term
.c
.x
, term
.c
.y
)) {
1460 drawcol
= dc
.col
[defaultrcs
];
1464 drawcol
= dc
.col
[defaultcs
];
1468 if (IS_SET(MODE_HIDE
))
1471 /* draw the new one */
1472 if (win
.state
& WIN_FOCUSED
) {
1473 switch (win
.cursor
) {
1474 case 7: /* st extension: snowman */
1475 utf8decode("☃", &g
.u
, UTF_SIZ
);
1476 case 0: /* Blinking Block */
1477 case 1: /* Blinking Block (Default) */
1478 case 2: /* Steady Block */
1479 g
.mode
|= term
.line
[term
.c
.y
][curx
].mode
& ATTR_WIDE
;
1480 xdrawglyph(g
, term
.c
.x
, term
.c
.y
);
1482 case 3: /* Blinking Underline */
1483 case 4: /* Steady Underline */
1484 XftDrawRect(xw
.draw
, &drawcol
,
1485 borderpx
+ curx
* win
.cw
,
1486 borderpx
+ (term
.c
.y
+ 1) * win
.ch
- \
1488 win
.cw
, cursorthickness
);
1490 case 5: /* Blinking bar */
1491 case 6: /* Steady bar */
1492 XftDrawRect(xw
.draw
, &drawcol
,
1493 borderpx
+ curx
* win
.cw
,
1494 borderpx
+ term
.c
.y
* win
.ch
,
1495 cursorthickness
, win
.ch
);
1499 XftDrawRect(xw
.draw
, &drawcol
,
1500 borderpx
+ curx
* win
.cw
,
1501 borderpx
+ term
.c
.y
* win
.ch
,
1503 XftDrawRect(xw
.draw
, &drawcol
,
1504 borderpx
+ curx
* win
.cw
,
1505 borderpx
+ term
.c
.y
* win
.ch
,
1507 XftDrawRect(xw
.draw
, &drawcol
,
1508 borderpx
+ (curx
+ 1) * win
.cw
- 1,
1509 borderpx
+ term
.c
.y
* win
.ch
,
1511 XftDrawRect(xw
.draw
, &drawcol
,
1512 borderpx
+ curx
* win
.cw
,
1513 borderpx
+ (term
.c
.y
+ 1) * win
.ch
- 1,
1516 oldx
= curx
, oldy
= term
.c
.y
;
1522 char buf
[sizeof(long) * 8 + 1];
1524 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1525 setenv("WINDOWID", buf
, 1);
1534 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1536 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
1537 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
1544 drawregion(0, 0, term
.col
, term
.row
);
1545 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, win
.w
,
1547 XSetForeground(xw
.dpy
, dc
.gc
,
1548 dc
.col
[IS_SET(MODE_REVERSE
)?
1549 defaultfg
: defaultbg
].pixel
);
1553 drawregion(int x1
, int y1
, int x2
, int y2
)
1555 int i
, x
, y
, ox
, numspecs
;
1557 XftGlyphFontSpec
*specs
;
1558 int ena_sel
= sel
.ob
.x
!= -1 && sel
.alt
== IS_SET(MODE_ALTSCREEN
);
1560 if (!(win
.state
& WIN_VISIBLE
))
1563 for (y
= y1
; y
< y2
; y
++) {
1570 numspecs
= xmakeglyphfontspecs(specs
, &term
.line
[y
][x1
], x2
- x1
, x1
, y
);
1573 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
1574 new = term
.line
[y
][x
];
1575 if (new.mode
== ATTR_WDUMMY
)
1577 if (ena_sel
&& selected(x
, y
))
1578 new.mode
^= ATTR_REVERSE
;
1579 if (i
> 0 && ATTRCMP(base
, new)) {
1580 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
1592 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
1604 visibility(XEvent
*ev
)
1606 XVisibilityEvent
*e
= &ev
->xvisibility
;
1608 MODBIT(win
.state
, e
->state
!= VisibilityFullyObscured
, WIN_VISIBLE
);
1614 win
.state
&= ~WIN_VISIBLE
;
1618 xsetpointermotion(int set
)
1620 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
1621 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
1625 xsetcursor(int cursor
)
1628 if (!BETWEEN(cursor
, 0, 6))
1630 win
.cursor
= cursor
;
1635 xseturgency(int add
)
1637 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1639 MODBIT(h
->flags
, add
, XUrgencyHint
);
1640 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1647 if (!(win
.state
& WIN_FOCUSED
))
1650 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
1656 XFocusChangeEvent
*e
= &ev
->xfocus
;
1658 if (e
->mode
== NotifyGrab
)
1661 if (ev
->type
== FocusIn
) {
1662 XSetICFocus(xw
.xic
);
1663 win
.state
|= WIN_FOCUSED
;
1665 if (IS_SET(MODE_FOCUS
))
1666 ttywrite("\033[I", 3);
1668 XUnsetICFocus(xw
.xic
);
1669 win
.state
&= ~WIN_FOCUSED
;
1670 if (IS_SET(MODE_FOCUS
))
1671 ttywrite("\033[O", 3);
1676 match(uint mask
, uint state
)
1678 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
1682 kmap(KeySym k
, uint state
)
1687 /* Check for mapped keys out of X11 function keys. */
1688 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
1689 if (mappedkeys
[i
] == k
)
1692 if (i
== LEN(mappedkeys
)) {
1693 if ((k
& 0xFFFF) < 0xFD00)
1697 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
1701 if (!match(kp
->mask
, state
))
1704 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
1706 if (term
.numlock
&& kp
->appkey
== 2)
1709 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
1712 if (IS_SET(MODE_CRLF
) ? kp
->crlf
< 0 : kp
->crlf
> 0)
1724 XKeyEvent
*e
= &ev
->xkey
;
1726 char buf
[32], *customkey
;
1732 if (IS_SET(MODE_KBDLOCK
))
1735 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
1737 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
1738 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
1739 bp
->func(&(bp
->arg
));
1744 /* 2. custom keys from config.h */
1745 if ((customkey
= kmap(ksym
, e
->state
))) {
1746 ttysend(customkey
, strlen(customkey
));
1750 /* 3. composed string from input method */
1753 if (len
== 1 && e
->state
& Mod1Mask
) {
1754 if (IS_SET(MODE_8BIT
)) {
1757 len
= utf8encode(c
, buf
);
1774 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1776 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1777 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1778 win
.state
|= WIN_FOCUSED
;
1780 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1781 win
.state
&= ~WIN_FOCUSED
;
1783 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
1784 /* Send SIGHUP to shell */
1793 if (e
->xconfigure
.width
== win
.w
&& e
->xconfigure
.height
== win
.h
)
1796 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
1803 int w
= win
.w
, h
= win
.h
;
1805 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
1806 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
1809 /* Waiting for window mapping */
1811 XNextEvent(xw
.dpy
, &ev
);
1813 * This XFilterEvent call is required because of XOpenIM. It
1814 * does filter out the key event and some client message for
1815 * the input method too.
1817 if (XFilterEvent(&ev
, None
))
1819 if (ev
.type
== ConfigureNotify
) {
1820 w
= ev
.xconfigure
.width
;
1821 h
= ev
.xconfigure
.height
;
1823 } while (ev
.type
!= MapNotify
);
1825 ttynew(opt_line
, opt_io
, opt_cmd
);
1828 clock_gettime(CLOCK_MONOTONIC
, &last
);
1831 for (xev
= actionfps
;;) {
1833 FD_SET(cmdfd
, &rfd
);
1836 if (pselect(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
1839 die("select failed: %s\n", strerror(errno
));
1841 if (FD_ISSET(cmdfd
, &rfd
)) {
1844 blinkset
= tattrset(ATTR_BLINK
);
1846 MODBIT(term
.mode
, 0, MODE_BLINK
);
1850 if (FD_ISSET(xfd
, &rfd
))
1853 clock_gettime(CLOCK_MONOTONIC
, &now
);
1854 drawtimeout
.tv_sec
= 0;
1855 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
1859 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
1860 tsetdirtattr(ATTR_BLINK
);
1861 term
.mode
^= MODE_BLINK
;
1865 deltatime
= TIMEDIFF(now
, last
);
1866 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
1872 while (XPending(xw
.dpy
)) {
1873 XNextEvent(xw
.dpy
, &ev
);
1874 if (XFilterEvent(&ev
, None
))
1876 if (handler
[ev
.type
])
1877 (handler
[ev
.type
])(&ev
);
1883 if (xev
&& !FD_ISSET(xfd
, &rfd
))
1885 if (!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
1887 if (TIMEDIFF(now
, lastblink
) \
1889 drawtimeout
.tv_nsec
= 1000;
1891 drawtimeout
.tv_nsec
= (1E6
* \
1896 drawtimeout
.tv_sec
= \
1897 drawtimeout
.tv_nsec
/ 1E9
;
1898 drawtimeout
.tv_nsec
%= (long)1E9
;
1910 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
1911 " [-n name] [-o file]\n"
1912 " [-T title] [-t title] [-w windowid]"
1913 " [[-e] command [args ...]]\n"
1914 " %s [-aiv] [-c class] [-f font] [-g geometry]"
1915 " [-n name] [-o file]\n"
1916 " [-T title] [-t title] [-w windowid] -l line"
1917 " [stty_args ...]\n", argv0
, argv0
);
1921 main(int argc
, char *argv
[])
1925 win
.cursor
= cursorshape
;
1932 opt_class
= EARGF(usage());
1939 opt_font
= EARGF(usage());
1942 xw
.gm
= XParseGeometry(EARGF(usage()),
1943 &xw
.l
, &xw
.t
, &cols
, &rows
);
1949 opt_io
= EARGF(usage());
1952 opt_line
= EARGF(usage());
1955 opt_name
= EARGF(usage());
1959 opt_title
= EARGF(usage());
1962 opt_embed
= EARGF(usage());
1965 die("%s " VERSION
" (c) 2010-2016 st engineers\n", argv0
);
1973 /* eat all remaining arguments */
1975 if (!opt_title
&& !opt_line
)
1976 opt_title
= basename(xstrdup(argv
[0]));
1978 setlocale(LC_CTYPE
, "");
1979 XSetLocaleModifiers("");
1980 tnew(MAX(cols
, 1), MAX(rows
, 1));