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 */
97 char *primary
, *clipboard
;
98 struct timespec tclick1
;
99 struct timespec tclick2
;
118 /* Drawing Context */
122 Font font
, bfont
, ifont
, ibfont
;
126 static inline ushort
sixd_to_16bit(int);
127 static int xmakeglyphfontspecs(XftGlyphFontSpec
*, const Glyph
*, int, int, int);
128 static void xdrawglyphfontspecs(const XftGlyphFontSpec
*, Glyph
, int, int, int);
129 static void xdrawglyph(Glyph
, int, int);
130 static void xclear(int, int, int, int);
131 static void xdrawcursor(void);
132 static int xgeommasktogravity(int);
133 static void xinit(void);
134 static void cresize(int, int);
135 static void xresize(int, int);
136 static int xloadfont(Font
*, FcPattern
*);
137 static void xloadfonts(char *, double);
138 static void xunloadfont(Font
*);
139 static void xunloadfonts(void);
140 static void xsetenv(void);
141 static void xseturgency(int);
142 static int x2col(int);
143 static int y2row(int);
145 static void expose(XEvent
*);
146 static void visibility(XEvent
*);
147 static void unmap(XEvent
*);
148 static void kpress(XEvent
*);
149 static void cmessage(XEvent
*);
150 static void resize(XEvent
*);
151 static void focus(XEvent
*);
152 static void brelease(XEvent
*);
153 static void bpress(XEvent
*);
154 static void bmotion(XEvent
*);
155 static void propnotify(XEvent
*);
156 static void selnotify(XEvent
*);
157 static void selclear_(XEvent
*);
158 static void selrequest(XEvent
*);
159 static void setsel(char *, Time
);
160 static void mousesel(XEvent
*, int);
161 static void mousereport(XEvent
*);
162 static char *kmap(KeySym
, uint
);
163 static int match(uint
, uint
);
165 static void run(void);
166 static void usage(void);
168 static void (*handler
[LASTEvent
])(XEvent
*) = {
170 [ClientMessage
] = cmessage
,
171 [ConfigureNotify
] = resize
,
172 [VisibilityNotify
] = visibility
,
173 [UnmapNotify
] = unmap
,
177 [MotionNotify
] = bmotion
,
178 [ButtonPress
] = bpress
,
179 [ButtonRelease
] = brelease
,
181 * Uncomment if you want the selection to disappear when you select something
182 * different in another window.
184 /* [SelectionClear] = selclear_, */
185 [SelectionNotify
] = selnotify
,
187 * PropertyNotify is only turned on when there is some INCR transfer happening
188 * for the selection retrieval.
190 [PropertyNotify
] = propnotify
,
191 [SelectionRequest
] = selrequest
,
197 static XSelection xsel
;
198 static TermWindow win
;
205 /* Font Ring Cache */
219 /* Fontcache is an array now. A new font will be appended to the array. */
220 static Fontcache frc
[16];
221 static int frclen
= 0;
222 static char *usedfont
= NULL
;
223 static double usedfontsize
= 0;
224 static double defaultfontsize
= 0;
226 static char *opt_class
= NULL
;
227 static char **opt_cmd
= NULL
;
228 static char *opt_embed
= NULL
;
229 static char *opt_font
= NULL
;
230 static char *opt_io
= NULL
;
231 static char *opt_line
= NULL
;
232 static char *opt_name
= NULL
;
233 static char *opt_title
= NULL
;
236 clipcopy(const Arg
*dummy
)
240 if (xsel
.clipboard
!= NULL
)
241 free(xsel
.clipboard
);
243 if (xsel
.primary
!= NULL
) {
244 xsel
.clipboard
= xstrdup(xsel
.primary
);
245 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
246 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
251 clippaste(const Arg
*dummy
)
255 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
256 XConvertSelection(xw
.dpy
, clipboard
, xsel
.xtarget
, clipboard
,
257 xw
.win
, CurrentTime
);
261 selpaste(const Arg
*dummy
)
263 XConvertSelection(xw
.dpy
, XA_PRIMARY
, xsel
.xtarget
, XA_PRIMARY
,
264 xw
.win
, CurrentTime
);
272 larg
.f
= usedfontsize
+ arg
->f
;
277 zoomabs(const Arg
*arg
)
280 xloadfonts(usedfont
, arg
->f
);
287 zoomreset(const Arg
*arg
)
291 if (defaultfontsize
> 0) {
292 larg
.f
= defaultfontsize
;
303 return LIMIT(x
, 0, term
.col
-1);
312 return LIMIT(y
, 0, term
.row
-1);
316 mousesel(XEvent
*e
, int done
)
318 int type
, seltype
= SEL_REGULAR
;
319 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forceselmod
);
321 for (type
= 1; type
< LEN(selmasks
); ++type
) {
322 if (match(selmasks
[type
], state
)) {
327 selextend(x2col(e
->xbutton
.x
), y2row(e
->xbutton
.y
), seltype
, done
);
329 setsel(getsel(), e
->xbutton
.time
);
333 mousereport(XEvent
*e
)
335 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
336 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
342 if (e
->xbutton
.type
== MotionNotify
) {
343 if (x
== ox
&& y
== oy
)
345 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
347 /* MOUSE_MOTION: no reporting if no button is pressed */
348 if (IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
351 button
= oldbutton
+ 32;
355 if (!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
362 if (e
->xbutton
.type
== ButtonPress
) {
366 } else if (e
->xbutton
.type
== ButtonRelease
) {
368 /* MODE_MOUSEX10: no button release reporting */
369 if (IS_SET(MODE_MOUSEX10
))
371 if (button
== 64 || button
== 65)
376 if (!IS_SET(MODE_MOUSEX10
)) {
377 button
+= ((state
& ShiftMask
) ? 4 : 0)
378 + ((state
& Mod4Mask
) ? 8 : 0)
379 + ((state
& ControlMask
) ? 16 : 0);
382 if (IS_SET(MODE_MOUSESGR
)) {
383 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
385 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
386 } else if (x
< 223 && y
< 223) {
387 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
388 32+button
, 32+x
+1, 32+y
+1);
393 ttywrite(buf
, len
, 0);
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 ttywrite(ms
->s
, strlen(ms
->s
), 1);
416 if (e
->xbutton
.button
== Button1
) {
418 * If the user clicks below predefined timeouts specific
419 * snapping behaviour is exposed.
421 clock_gettime(CLOCK_MONOTONIC
, &now
);
422 if (TIMEDIFF(now
, xsel
.tclick2
) <= tripleclicktimeout
) {
424 } else if (TIMEDIFF(now
, xsel
.tclick1
) <= doubleclicktimeout
) {
429 xsel
.tclick2
= xsel
.tclick1
;
432 selstart(x2col(e
->xbutton
.x
), y2row(e
->xbutton
.y
), snap
);
437 propnotify(XEvent
*e
)
439 XPropertyEvent
*xpev
;
440 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
442 xpev
= &e
->xproperty
;
443 if (xpev
->state
== PropertyNewValue
&&
444 (xpev
->atom
== XA_PRIMARY
||
445 xpev
->atom
== clipboard
)) {
453 ulong nitems
, ofs
, rem
;
455 uchar
*data
, *last
, *repl
;
456 Atom type
, incratom
, property
;
458 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
461 if (e
->type
== SelectionNotify
) {
462 property
= e
->xselection
.property
;
463 } else if(e
->type
== PropertyNotify
) {
464 property
= e
->xproperty
.atom
;
468 if (property
== None
)
472 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
473 BUFSIZ
/4, False
, AnyPropertyType
,
474 &type
, &format
, &nitems
, &rem
,
476 fprintf(stderr
, "Clipboard allocation failed\n");
480 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
482 * If there is some PropertyNotify with no data, then
483 * this is the signal of the selection owner that all
484 * data has been transferred. We won't need to receive
485 * PropertyNotify events anymore.
487 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
488 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
492 if (type
== incratom
) {
494 * Activate the PropertyNotify events so we receive
495 * when the selection owner does send us the next
498 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
499 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
503 * Deleting the property is the transfer start signal.
505 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
511 * Line endings are inconsistent in the terminal and GUI world
512 * copy and pasting. When receiving some selection data,
513 * replace all '\n' with '\r'.
514 * FIXME: Fix the computer world.
517 last
= data
+ nitems
* format
/ 8;
518 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
522 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
523 ttywrite("\033[200~", 6, 0);
524 ttywrite((char *)data
, nitems
* format
/ 8, 1);
525 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
526 ttywrite("\033[201~", 6, 0);
528 /* number of 32-bit chunks returned */
529 ofs
+= nitems
* format
/ 32;
533 * Deleting the property again tells the selection owner to send the
534 * next data chunk in the property.
536 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
552 selrequest(XEvent
*e
)
554 XSelectionRequestEvent
*xsre
;
556 Atom xa_targets
, string
, clipboard
;
559 xsre
= (XSelectionRequestEvent
*) e
;
560 xev
.type
= SelectionNotify
;
561 xev
.requestor
= xsre
->requestor
;
562 xev
.selection
= xsre
->selection
;
563 xev
.target
= xsre
->target
;
564 xev
.time
= xsre
->time
;
565 if (xsre
->property
== None
)
566 xsre
->property
= xsre
->target
;
571 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
572 if (xsre
->target
== xa_targets
) {
573 /* respond with the supported type */
574 string
= xsel
.xtarget
;
575 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
576 XA_ATOM
, 32, PropModeReplace
,
577 (uchar
*) &string
, 1);
578 xev
.property
= xsre
->property
;
579 } else if (xsre
->target
== xsel
.xtarget
|| xsre
->target
== XA_STRING
) {
581 * xith XA_STRING non ascii characters may be incorrect in the
582 * requestor. It is not our problem, use utf8.
584 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
585 if (xsre
->selection
== XA_PRIMARY
) {
586 seltext
= xsel
.primary
;
587 } else if (xsre
->selection
== clipboard
) {
588 seltext
= xsel
.clipboard
;
591 "Unhandled clipboard selection 0x%lx\n",
595 if (seltext
!= NULL
) {
596 XChangeProperty(xsre
->display
, xsre
->requestor
,
597 xsre
->property
, xsre
->target
,
599 (uchar
*)seltext
, strlen(seltext
));
600 xev
.property
= xsre
->property
;
604 /* all done, send a notification to the listener */
605 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
606 fprintf(stderr
, "Error sending SelectionNotify event\n");
610 setsel(char *str
, Time t
)
615 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
616 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
623 setsel(str
, CurrentTime
);
629 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
634 if (e
->xbutton
.button
== Button2
)
636 else if (e
->xbutton
.button
== Button1
)
643 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
652 cresize(int width
, int height
)
661 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
662 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
666 ttyresize(win
.tw
, win
.th
);
670 xresize(int col
, int row
)
672 win
.tw
= MAX(1, col
* win
.cw
);
673 win
.th
= MAX(1, row
* win
.ch
);
675 XFreePixmap(xw
.dpy
, xw
.buf
);
676 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
677 DefaultDepth(xw
.dpy
, xw
.scr
));
678 XftDrawChange(xw
.draw
, xw
.buf
);
679 xclear(0, 0, win
.w
, win
.h
);
681 /* resize to new width */
682 xw
.specbuf
= xrealloc(xw
.specbuf
, col
* sizeof(GlyphFontSpec
));
688 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
692 xloadcolor(int i
, const char *name
, Color
*ncolor
)
694 XRenderColor color
= { .alpha
= 0xffff };
697 if (BETWEEN(i
, 16, 255)) { /* 256 color */
698 if (i
< 6*6*6+16) { /* same colors as xterm */
699 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
700 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
701 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
702 } else { /* greyscale */
703 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
704 color
.green
= color
.blue
= color
.red
;
706 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
707 xw
.cmap
, &color
, ncolor
);
712 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
722 dc
.collen
= MAX(LEN(colorname
), 256);
723 dc
.col
= xmalloc(dc
.collen
* sizeof(Color
));
726 for (cp
= dc
.col
; cp
< &dc
.col
[dc
.collen
]; ++cp
)
727 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
730 for (i
= 0; i
< dc
.collen
; i
++)
731 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
733 die("Could not allocate color '%s'\n", colorname
[i
]);
735 die("Could not allocate color %d\n", i
);
741 xsetcolorname(int x
, const char *name
)
745 if (!BETWEEN(x
, 0, dc
.collen
))
749 if (!xloadcolor(x
, name
, &ncolor
))
752 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
759 * Absolute coordinates.
762 xclear(int x1
, int y1
, int x2
, int y2
)
765 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
766 x1
, y1
, x2
-x1
, y2
-y1
);
772 XClassHint
class = {opt_name
? opt_name
: termname
,
773 opt_class
? opt_class
: termname
};
774 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
775 XSizeHints
*sizeh
= NULL
;
777 sizeh
= XAllocSizeHints();
779 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
780 sizeh
->height
= win
.h
;
781 sizeh
->width
= win
.w
;
782 sizeh
->height_inc
= win
.ch
;
783 sizeh
->width_inc
= win
.cw
;
784 sizeh
->base_height
= 2 * borderpx
;
785 sizeh
->base_width
= 2 * borderpx
;
787 sizeh
->flags
|= PMaxSize
| PMinSize
;
788 sizeh
->min_width
= sizeh
->max_width
= win
.w
;
789 sizeh
->min_height
= sizeh
->max_height
= win
.h
;
791 if (xw
.gm
& (XValue
|YValue
)) {
792 sizeh
->flags
|= USPosition
| PWinGravity
;
795 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
798 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
804 xgeommasktogravity(int mask
)
806 switch (mask
& (XNegative
|YNegative
)) {
808 return NorthWestGravity
;
810 return NorthEastGravity
;
812 return SouthWestGravity
;
815 return SouthEastGravity
;
819 xloadfont(Font
*f
, FcPattern
*pattern
)
821 FcPattern
*configured
;
825 int wantattr
, haveattr
;
828 * Manually configure instead of calling XftMatchFont
829 * so that we can use the configured pattern for
830 * "missing glyph" lookups.
832 configured
= FcPatternDuplicate(pattern
);
836 FcConfigSubstitute(NULL
, configured
, FcMatchPattern
);
837 XftDefaultSubstitute(xw
.dpy
, xw
.scr
, configured
);
839 match
= FcFontMatch(NULL
, configured
, &result
);
841 FcPatternDestroy(configured
);
845 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
846 FcPatternDestroy(configured
);
847 FcPatternDestroy(match
);
851 if ((XftPatternGetInteger(pattern
, "slant", 0, &wantattr
) ==
854 * Check if xft was unable to find a font with the appropriate
855 * slant but gave us one anyway. Try to mitigate.
857 if ((XftPatternGetInteger(f
->match
->pattern
, "slant", 0,
858 &haveattr
) != XftResultMatch
) || haveattr
< wantattr
) {
860 fputs("st: font slant does not match\n", stderr
);
864 if ((XftPatternGetInteger(pattern
, "weight", 0, &wantattr
) ==
866 if ((XftPatternGetInteger(f
->match
->pattern
, "weight", 0,
867 &haveattr
) != XftResultMatch
) || haveattr
!= wantattr
) {
869 fputs("st: font weight does not match\n", stderr
);
873 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
874 (const FcChar8
*) ascii_printable
,
875 strlen(ascii_printable
), &extents
);
878 f
->pattern
= configured
;
880 f
->ascent
= f
->match
->ascent
;
881 f
->descent
= f
->match
->descent
;
883 f
->rbearing
= f
->match
->max_advance_width
;
885 f
->height
= f
->ascent
+ f
->descent
;
886 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
892 xloadfonts(char *fontstr
, double fontsize
)
898 if (fontstr
[0] == '-') {
899 pattern
= XftXlfdParse(fontstr
, False
, False
);
901 pattern
= FcNameParse((FcChar8
*)fontstr
);
905 die("st: can't open font %s\n", fontstr
);
908 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
909 FcPatternDel(pattern
, FC_SIZE
);
910 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
911 usedfontsize
= fontsize
;
913 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
915 usedfontsize
= fontval
;
916 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
921 * Default font size is 12, if none given. This is to
922 * have a known usedfontsize value.
924 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
927 defaultfontsize
= usedfontsize
;
930 if (xloadfont(&dc
.font
, pattern
))
931 die("st: can't open font %s\n", fontstr
);
933 if (usedfontsize
< 0) {
934 FcPatternGetDouble(dc
.font
.match
->pattern
,
935 FC_PIXEL_SIZE
, 0, &fontval
);
936 usedfontsize
= fontval
;
938 defaultfontsize
= fontval
;
941 /* Setting character width and height. */
942 win
.cw
= ceilf(dc
.font
.width
* cwscale
);
943 win
.ch
= ceilf(dc
.font
.height
* chscale
);
945 FcPatternDel(pattern
, FC_SLANT
);
946 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
947 if (xloadfont(&dc
.ifont
, pattern
))
948 die("st: can't open font %s\n", fontstr
);
950 FcPatternDel(pattern
, FC_WEIGHT
);
951 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
952 if (xloadfont(&dc
.ibfont
, pattern
))
953 die("st: can't open font %s\n", fontstr
);
955 FcPatternDel(pattern
, FC_SLANT
);
956 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
957 if (xloadfont(&dc
.bfont
, pattern
))
958 die("st: can't open font %s\n", fontstr
);
960 FcPatternDestroy(pattern
);
966 XftFontClose(xw
.dpy
, f
->match
);
967 FcPatternDestroy(f
->pattern
);
969 FcFontSetDestroy(f
->set
);
975 /* Free the loaded fonts in the font cache. */
977 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
979 xunloadfont(&dc
.font
);
980 xunloadfont(&dc
.bfont
);
981 xunloadfont(&dc
.ifont
);
982 xunloadfont(&dc
.ibfont
);
991 pid_t thispid
= getpid();
992 XColor xmousefg
, xmousebg
;
994 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
995 die("Can't open display\n");
996 xw
.scr
= XDefaultScreen(xw
.dpy
);
997 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
1001 die("Could not init fontconfig.\n");
1003 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
1004 xloadfonts(usedfont
, 0);
1007 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
1010 /* adjust fixed window geometry */
1011 win
.w
= 2 * borderpx
+ term
.col
* win
.cw
;
1012 win
.h
= 2 * borderpx
+ term
.row
* win
.ch
;
1013 if (xw
.gm
& XNegative
)
1014 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - win
.w
- 2;
1015 if (xw
.gm
& YNegative
)
1016 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - win
.h
- 2;
1019 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
1020 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
1021 xw
.attrs
.bit_gravity
= NorthWestGravity
;
1022 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
1023 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
1024 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
1025 xw
.attrs
.colormap
= xw
.cmap
;
1027 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
1028 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
1029 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
1030 win
.w
, win
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
1031 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
1032 | CWEventMask
| CWColormap
, &xw
.attrs
);
1034 memset(&gcvalues
, 0, sizeof(gcvalues
));
1035 gcvalues
.graphics_exposures
= False
;
1036 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
1038 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, win
.w
, win
.h
,
1039 DefaultDepth(xw
.dpy
, xw
.scr
));
1040 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
1041 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, win
.w
, win
.h
);
1043 /* font spec buffer */
1044 xw
.specbuf
= xmalloc(term
.col
* sizeof(GlyphFontSpec
));
1046 /* Xft rendering context */
1047 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
1050 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1051 XSetLocaleModifiers("@im=local");
1052 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
1053 XSetLocaleModifiers("@im=");
1054 if ((xw
.xim
= XOpenIM(xw
.dpy
,
1055 NULL
, NULL
, NULL
)) == NULL
) {
1056 die("XOpenIM failed. Could not open input"
1061 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
1062 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
1063 XNFocusWindow
, xw
.win
, NULL
);
1065 die("XCreateIC failed. Could not obtain input method.\n");
1067 /* white cursor, black outline */
1068 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
1069 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
1071 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
1072 xmousefg
.red
= 0xffff;
1073 xmousefg
.green
= 0xffff;
1074 xmousefg
.blue
= 0xffff;
1077 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
1078 xmousebg
.red
= 0x0000;
1079 xmousebg
.green
= 0x0000;
1080 xmousebg
.blue
= 0x0000;
1083 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
1085 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
1086 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
1087 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
1088 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
1090 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
1091 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
1092 PropModeReplace
, (uchar
*)&thispid
, 1);
1095 XMapWindow(xw
.dpy
, xw
.win
);
1097 XSync(xw
.dpy
, False
);
1099 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick1
);
1100 clock_gettime(CLOCK_MONOTONIC
, &xsel
.tclick2
);
1101 xsel
.primary
= NULL
;
1102 xsel
.clipboard
= NULL
;
1103 xsel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
1104 if (xsel
.xtarget
== None
)
1105 xsel
.xtarget
= XA_STRING
;
1109 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
1111 float winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
, xp
, yp
;
1112 ushort mode
, prevmode
= USHRT_MAX
;
1113 Font
*font
= &dc
.font
;
1114 int frcflags
= FRC_NORMAL
;
1115 float runewidth
= win
.cw
;
1119 FcPattern
*fcpattern
, *fontpattern
;
1120 FcFontSet
*fcsets
[] = { NULL
};
1121 FcCharSet
*fccharset
;
1122 int i
, f
, numspecs
= 0;
1124 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
1125 /* Fetch rune and mode for current glyph. */
1127 mode
= glyphs
[i
].mode
;
1129 /* Skip dummy wide-character spacing. */
1130 if (mode
== ATTR_WDUMMY
)
1133 /* Determine font for glyph if different from previous glyph. */
1134 if (prevmode
!= mode
) {
1137 frcflags
= FRC_NORMAL
;
1138 runewidth
= win
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
1139 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
1141 frcflags
= FRC_ITALICBOLD
;
1142 } else if (mode
& ATTR_ITALIC
) {
1144 frcflags
= FRC_ITALIC
;
1145 } else if (mode
& ATTR_BOLD
) {
1147 frcflags
= FRC_BOLD
;
1149 yp
= winy
+ font
->ascent
;
1152 /* Lookup character index with default font. */
1153 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
1155 specs
[numspecs
].font
= font
->match
;
1156 specs
[numspecs
].glyph
= glyphidx
;
1157 specs
[numspecs
].x
= (short)xp
;
1158 specs
[numspecs
].y
= (short)yp
;
1164 /* Fallback on font cache, search the font cache for match. */
1165 for (f
= 0; f
< frclen
; f
++) {
1166 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
1167 /* Everything correct. */
1168 if (glyphidx
&& frc
[f
].flags
== frcflags
)
1170 /* We got a default font for a not found glyph. */
1171 if (!glyphidx
&& frc
[f
].flags
== frcflags
1172 && frc
[f
].unicodep
== rune
) {
1177 /* Nothing was found. Use fontconfig to find matching font. */
1180 font
->set
= FcFontSort(0, font
->pattern
,
1182 fcsets
[0] = font
->set
;
1185 * Nothing was found in the cache. Now use
1186 * some dozen of Fontconfig calls to get the
1187 * font for one single character.
1189 * Xft and fontconfig are design failures.
1191 fcpattern
= FcPatternDuplicate(font
->pattern
);
1192 fccharset
= FcCharSetCreate();
1194 FcCharSetAddChar(fccharset
, rune
);
1195 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
1197 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
1199 FcConfigSubstitute(0, fcpattern
,
1201 FcDefaultSubstitute(fcpattern
);
1203 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
1207 * Overwrite or create the new cache entry.
1209 if (frclen
>= LEN(frc
)) {
1210 frclen
= LEN(frc
) - 1;
1211 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
1212 frc
[frclen
].unicodep
= 0;
1215 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
1217 if (!frc
[frclen
].font
)
1218 die("XftFontOpenPattern failed seeking fallback font: %s\n",
1220 frc
[frclen
].flags
= frcflags
;
1221 frc
[frclen
].unicodep
= rune
;
1223 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
1228 FcPatternDestroy(fcpattern
);
1229 FcCharSetDestroy(fccharset
);
1232 specs
[numspecs
].font
= frc
[f
].font
;
1233 specs
[numspecs
].glyph
= glyphidx
;
1234 specs
[numspecs
].x
= (short)xp
;
1235 specs
[numspecs
].y
= (short)yp
;
1244 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
1246 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
1247 int winx
= borderpx
+ x
* win
.cw
, winy
= borderpx
+ y
* win
.ch
,
1248 width
= charlen
* win
.cw
;
1249 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
1250 XRenderColor colfg
, colbg
;
1253 /* Fallback on color display for attributes not supported by the font */
1254 if (base
.mode
& ATTR_ITALIC
&& base
.mode
& ATTR_BOLD
) {
1255 if (dc
.ibfont
.badslant
|| dc
.ibfont
.badweight
)
1256 base
.fg
= defaultattr
;
1257 } else if ((base
.mode
& ATTR_ITALIC
&& dc
.ifont
.badslant
) ||
1258 (base
.mode
& ATTR_BOLD
&& dc
.bfont
.badweight
)) {
1259 base
.fg
= defaultattr
;
1262 if (IS_TRUECOL(base
.fg
)) {
1263 colfg
.alpha
= 0xffff;
1264 colfg
.red
= TRUERED(base
.fg
);
1265 colfg
.green
= TRUEGREEN(base
.fg
);
1266 colfg
.blue
= TRUEBLUE(base
.fg
);
1267 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
1270 fg
= &dc
.col
[base
.fg
];
1273 if (IS_TRUECOL(base
.bg
)) {
1274 colbg
.alpha
= 0xffff;
1275 colbg
.green
= TRUEGREEN(base
.bg
);
1276 colbg
.red
= TRUERED(base
.bg
);
1277 colbg
.blue
= TRUEBLUE(base
.bg
);
1278 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
1281 bg
= &dc
.col
[base
.bg
];
1284 /* Change basic system colors [0-7] to bright system colors [8-15] */
1285 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
1286 fg
= &dc
.col
[base
.fg
+ 8];
1288 if (IS_SET(MODE_REVERSE
)) {
1289 if (fg
== &dc
.col
[defaultfg
]) {
1290 fg
= &dc
.col
[defaultbg
];
1292 colfg
.red
= ~fg
->color
.red
;
1293 colfg
.green
= ~fg
->color
.green
;
1294 colfg
.blue
= ~fg
->color
.blue
;
1295 colfg
.alpha
= fg
->color
.alpha
;
1296 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
1301 if (bg
== &dc
.col
[defaultbg
]) {
1302 bg
= &dc
.col
[defaultfg
];
1304 colbg
.red
= ~bg
->color
.red
;
1305 colbg
.green
= ~bg
->color
.green
;
1306 colbg
.blue
= ~bg
->color
.blue
;
1307 colbg
.alpha
= bg
->color
.alpha
;
1308 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
1314 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
1315 colfg
.red
= fg
->color
.red
/ 2;
1316 colfg
.green
= fg
->color
.green
/ 2;
1317 colfg
.blue
= fg
->color
.blue
/ 2;
1318 colfg
.alpha
= fg
->color
.alpha
;
1319 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
1324 if (base
.mode
& ATTR_REVERSE
) {
1330 if (base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
1333 if (base
.mode
& ATTR_INVISIBLE
)
1336 /* Intelligent cleaning up of the borders. */
1338 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
1339 winy
+ win
.ch
+ ((y
>= term
.row
-1)? win
.h
: 0));
1341 if (x
+ charlen
>= term
.col
) {
1342 xclear(winx
+ width
, (y
== 0)? 0 : winy
, win
.w
,
1343 ((y
>= term
.row
-1)? win
.h
: (winy
+ win
.ch
)));
1346 xclear(winx
, 0, winx
+ width
, borderpx
);
1347 if (y
== term
.row
-1)
1348 xclear(winx
, winy
+ win
.ch
, winx
+ width
, win
.h
);
1350 /* Clean up the region we want to draw to. */
1351 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, win
.ch
);
1353 /* Set the clip region because Xft is sometimes dirty. */
1358 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
1360 /* Render the glyphs. */
1361 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
1363 /* Render underline and strikethrough. */
1364 if (base
.mode
& ATTR_UNDERLINE
) {
1365 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
1369 if (base
.mode
& ATTR_STRUCK
) {
1370 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
1374 /* Reset clip to none. */
1375 XftDrawSetClip(xw
.draw
, 0);
1379 xdrawglyph(Glyph g
, int x
, int y
)
1382 XftGlyphFontSpec spec
;
1384 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
1385 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
1391 static int oldx
= 0, oldy
= 0;
1393 Glyph g
= {' ', ATTR_NULL
, defaultbg
, defaultcs
}, og
;
1396 LIMIT(oldx
, 0, term
.col
-1);
1397 LIMIT(oldy
, 0, term
.row
-1);
1401 /* adjust position if in dummy */
1402 if (term
.line
[oldy
][oldx
].mode
& ATTR_WDUMMY
)
1404 if (term
.line
[term
.c
.y
][curx
].mode
& ATTR_WDUMMY
)
1407 /* remove the old cursor */
1408 og
= term
.line
[oldy
][oldx
];
1409 if (selected(oldx
, oldy
))
1410 og
.mode
^= ATTR_REVERSE
;
1411 xdrawglyph(og
, oldx
, oldy
);
1413 g
.u
= term
.line
[term
.c
.y
][term
.c
.x
].u
;
1414 g
.mode
|= term
.line
[term
.c
.y
][term
.c
.x
].mode
&
1415 (ATTR_BOLD
| ATTR_ITALIC
| ATTR_UNDERLINE
| ATTR_STRUCK
);
1418 * Select the right color for the right mode.
1420 if (IS_SET(MODE_REVERSE
)) {
1421 g
.mode
|= ATTR_REVERSE
;
1423 if (selected(term
.c
.x
, term
.c
.y
)) {
1424 drawcol
= dc
.col
[defaultcs
];
1427 drawcol
= dc
.col
[defaultrcs
];
1431 if (selected(term
.c
.x
, term
.c
.y
)) {
1432 drawcol
= dc
.col
[defaultrcs
];
1436 drawcol
= dc
.col
[defaultcs
];
1440 if (IS_SET(MODE_HIDE
))
1443 /* draw the new one */
1444 if (win
.state
& WIN_FOCUSED
) {
1445 switch (win
.cursor
) {
1446 case 7: /* st extension: snowman */
1447 utf8decode("☃", &g
.u
, UTF_SIZ
);
1448 case 0: /* Blinking Block */
1449 case 1: /* Blinking Block (Default) */
1450 case 2: /* Steady Block */
1451 g
.mode
|= term
.line
[term
.c
.y
][curx
].mode
& ATTR_WIDE
;
1452 xdrawglyph(g
, term
.c
.x
, term
.c
.y
);
1454 case 3: /* Blinking Underline */
1455 case 4: /* Steady Underline */
1456 XftDrawRect(xw
.draw
, &drawcol
,
1457 borderpx
+ curx
* win
.cw
,
1458 borderpx
+ (term
.c
.y
+ 1) * win
.ch
- \
1460 win
.cw
, cursorthickness
);
1462 case 5: /* Blinking bar */
1463 case 6: /* Steady bar */
1464 XftDrawRect(xw
.draw
, &drawcol
,
1465 borderpx
+ curx
* win
.cw
,
1466 borderpx
+ term
.c
.y
* win
.ch
,
1467 cursorthickness
, win
.ch
);
1471 XftDrawRect(xw
.draw
, &drawcol
,
1472 borderpx
+ curx
* win
.cw
,
1473 borderpx
+ term
.c
.y
* win
.ch
,
1475 XftDrawRect(xw
.draw
, &drawcol
,
1476 borderpx
+ curx
* win
.cw
,
1477 borderpx
+ term
.c
.y
* win
.ch
,
1479 XftDrawRect(xw
.draw
, &drawcol
,
1480 borderpx
+ (curx
+ 1) * win
.cw
- 1,
1481 borderpx
+ term
.c
.y
* win
.ch
,
1483 XftDrawRect(xw
.draw
, &drawcol
,
1484 borderpx
+ curx
* win
.cw
,
1485 borderpx
+ (term
.c
.y
+ 1) * win
.ch
- 1,
1488 oldx
= curx
, oldy
= term
.c
.y
;
1494 char buf
[sizeof(long) * 8 + 1];
1496 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1497 setenv("WINDOWID", buf
, 1);
1506 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
1508 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
1509 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
1516 drawregion(0, 0, term
.col
, term
.row
);
1517 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, win
.w
,
1519 XSetForeground(xw
.dpy
, dc
.gc
,
1520 dc
.col
[IS_SET(MODE_REVERSE
)?
1521 defaultfg
: defaultbg
].pixel
);
1525 drawregion(int x1
, int y1
, int x2
, int y2
)
1527 int i
, x
, y
, ox
, numspecs
;
1529 XftGlyphFontSpec
*specs
;
1531 if (!(win
.state
& WIN_VISIBLE
))
1534 for (y
= y1
; y
< y2
; y
++) {
1541 numspecs
= xmakeglyphfontspecs(specs
, &term
.line
[y
][x1
], x2
- x1
, x1
, y
);
1544 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
1545 new = term
.line
[y
][x
];
1546 if (new.mode
== ATTR_WDUMMY
)
1549 new.mode
^= ATTR_REVERSE
;
1550 if (i
> 0 && ATTRCMP(base
, new)) {
1551 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
1563 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
1575 visibility(XEvent
*ev
)
1577 XVisibilityEvent
*e
= &ev
->xvisibility
;
1579 MODBIT(win
.state
, e
->state
!= VisibilityFullyObscured
, WIN_VISIBLE
);
1585 win
.state
&= ~WIN_VISIBLE
;
1589 xsetpointermotion(int set
)
1591 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
1592 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
1596 xsetcursor(int cursor
)
1599 if (!BETWEEN(cursor
, 0, 6))
1601 win
.cursor
= cursor
;
1606 xseturgency(int add
)
1608 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
1610 MODBIT(h
->flags
, add
, XUrgencyHint
);
1611 XSetWMHints(xw
.dpy
, xw
.win
, h
);
1618 if (!(win
.state
& WIN_FOCUSED
))
1621 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
1627 XFocusChangeEvent
*e
= &ev
->xfocus
;
1629 if (e
->mode
== NotifyGrab
)
1632 if (ev
->type
== FocusIn
) {
1633 XSetICFocus(xw
.xic
);
1634 win
.state
|= WIN_FOCUSED
;
1636 if (IS_SET(MODE_FOCUS
))
1637 ttywrite("\033[I", 3, 0);
1639 XUnsetICFocus(xw
.xic
);
1640 win
.state
&= ~WIN_FOCUSED
;
1641 if (IS_SET(MODE_FOCUS
))
1642 ttywrite("\033[O", 3, 0);
1647 match(uint mask
, uint state
)
1649 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
1653 kmap(KeySym k
, uint state
)
1658 /* Check for mapped keys out of X11 function keys. */
1659 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
1660 if (mappedkeys
[i
] == k
)
1663 if (i
== LEN(mappedkeys
)) {
1664 if ((k
& 0xFFFF) < 0xFD00)
1668 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
1672 if (!match(kp
->mask
, state
))
1675 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
1677 if (term
.numlock
&& kp
->appkey
== 2)
1680 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
1683 if (IS_SET(MODE_CRLF
) ? kp
->crlf
< 0 : kp
->crlf
> 0)
1695 XKeyEvent
*e
= &ev
->xkey
;
1697 char buf
[32], *customkey
;
1703 if (IS_SET(MODE_KBDLOCK
))
1706 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
1708 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
1709 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
1710 bp
->func(&(bp
->arg
));
1715 /* 2. custom keys from config.h */
1716 if ((customkey
= kmap(ksym
, e
->state
))) {
1717 ttywrite(customkey
, strlen(customkey
), 1);
1721 /* 3. composed string from input method */
1724 if (len
== 1 && e
->state
& Mod1Mask
) {
1725 if (IS_SET(MODE_8BIT
)) {
1728 len
= utf8encode(c
, buf
);
1736 ttywrite(buf
, len
, 1);
1745 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
1747 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
1748 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
1749 win
.state
|= WIN_FOCUSED
;
1751 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
1752 win
.state
&= ~WIN_FOCUSED
;
1754 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
1755 /* Send SIGHUP to shell */
1764 if (e
->xconfigure
.width
== win
.w
&& e
->xconfigure
.height
== win
.h
)
1767 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
1774 int w
= win
.w
, h
= win
.h
;
1776 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
1777 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
1780 /* Waiting for window mapping */
1782 XNextEvent(xw
.dpy
, &ev
);
1784 * This XFilterEvent call is required because of XOpenIM. It
1785 * does filter out the key event and some client message for
1786 * the input method too.
1788 if (XFilterEvent(&ev
, None
))
1790 if (ev
.type
== ConfigureNotify
) {
1791 w
= ev
.xconfigure
.width
;
1792 h
= ev
.xconfigure
.height
;
1794 } while (ev
.type
!= MapNotify
);
1796 ttynew(opt_line
, opt_io
, opt_cmd
);
1799 clock_gettime(CLOCK_MONOTONIC
, &last
);
1802 for (xev
= actionfps
;;) {
1804 FD_SET(cmdfd
, &rfd
);
1807 if (pselect(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
1810 die("select failed: %s\n", strerror(errno
));
1812 if (FD_ISSET(cmdfd
, &rfd
)) {
1815 blinkset
= tattrset(ATTR_BLINK
);
1817 MODBIT(term
.mode
, 0, MODE_BLINK
);
1821 if (FD_ISSET(xfd
, &rfd
))
1824 clock_gettime(CLOCK_MONOTONIC
, &now
);
1825 drawtimeout
.tv_sec
= 0;
1826 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
1830 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
1831 tsetdirtattr(ATTR_BLINK
);
1832 term
.mode
^= MODE_BLINK
;
1836 deltatime
= TIMEDIFF(now
, last
);
1837 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
1843 while (XPending(xw
.dpy
)) {
1844 XNextEvent(xw
.dpy
, &ev
);
1845 if (XFilterEvent(&ev
, None
))
1847 if (handler
[ev
.type
])
1848 (handler
[ev
.type
])(&ev
);
1854 if (xev
&& !FD_ISSET(xfd
, &rfd
))
1856 if (!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
1858 if (TIMEDIFF(now
, lastblink
) \
1860 drawtimeout
.tv_nsec
= 1000;
1862 drawtimeout
.tv_nsec
= (1E6
* \
1867 drawtimeout
.tv_sec
= \
1868 drawtimeout
.tv_nsec
/ 1E9
;
1869 drawtimeout
.tv_nsec
%= (long)1E9
;
1881 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
1882 " [-n name] [-o file]\n"
1883 " [-T title] [-t title] [-w windowid]"
1884 " [[-e] command [args ...]]\n"
1885 " %s [-aiv] [-c class] [-f font] [-g geometry]"
1886 " [-n name] [-o file]\n"
1887 " [-T title] [-t title] [-w windowid] -l line"
1888 " [stty_args ...]\n", argv0
, argv0
);
1892 main(int argc
, char *argv
[])
1896 win
.cursor
= cursorshape
;
1903 opt_class
= EARGF(usage());
1910 opt_font
= EARGF(usage());
1913 xw
.gm
= XParseGeometry(EARGF(usage()),
1914 &xw
.l
, &xw
.t
, &cols
, &rows
);
1920 opt_io
= EARGF(usage());
1923 opt_line
= EARGF(usage());
1926 opt_name
= EARGF(usage());
1930 opt_title
= EARGF(usage());
1933 opt_embed
= EARGF(usage());
1936 die("%s " VERSION
" (c) 2010-2016 st engineers\n", argv0
);
1944 /* eat all remaining arguments */
1946 if (!opt_title
&& !opt_line
)
1947 opt_title
= basename(xstrdup(argv
[0]));
1949 setlocale(LC_CTYPE
, "");
1950 XSetLocaleModifiers("");
1951 tnew(MAX(cols
, 1), MAX(rows
, 1));