1 /* See LICENSE for license details. */
14 #include <sys/ioctl.h>
15 #include <sys/select.h>
18 #include <sys/types.h>
23 #include <X11/Xatom.h>
25 #include <X11/Xutil.h>
26 #include <X11/cursorfont.h>
27 #include <X11/keysym.h>
28 #include <X11/Xft/Xft.h>
29 #include <X11/XKBlib.h>
30 #include <fontconfig/fontconfig.h>
42 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
44 #elif defined(__FreeBSD__) || defined(__DragonFly__)
50 #define XEMBED_FOCUS_IN 4
51 #define XEMBED_FOCUS_OUT 5
54 #define UTF_INVALID 0xFFFD
56 #define ESC_BUF_SIZ (128*UTF_SIZ)
57 #define ESC_ARG_SIZ 16
58 #define STR_BUF_SIZ ESC_BUF_SIZ
59 #define STR_ARG_SIZ ESC_ARG_SIZ
60 #define XK_ANY_MOD UINT_MAX
62 #define XK_SWITCH_MOD (1<<13)
65 #define MIN(a, b) ((a) < (b) ? (a) : (b))
66 #define MAX(a, b) ((a) < (b) ? (b) : (a))
67 #define LEN(a) (sizeof(a) / sizeof(a)[0])
68 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
69 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
70 #define ISCONTROLC0(c) (BETWEEN(c, 0, 0x1f) || (c) == '\177')
71 #define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f))
72 #define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c))
73 #define ISDELIM(u) (utf8strchr(worddelimiters, u) != NULL)
74 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
75 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || \
77 #define IS_SET(flag) ((term.mode & (flag)) != 0)
78 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + \
79 (t1.tv_nsec-t2.tv_nsec)/1E6)
80 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
82 #define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
83 #define IS_TRUECOL(x) (1 << 24 & (x))
84 #define TRUERED(x) (((x) & 0xff0000) >> 8)
85 #define TRUEGREEN(x) (((x) & 0xff00))
86 #define TRUEBLUE(x) (((x) & 0xff) << 8)
89 enum glyph_attribute
{
94 ATTR_UNDERLINE
= 1 << 3,
96 ATTR_REVERSE
= 1 << 5,
97 ATTR_INVISIBLE
= 1 << 6,
101 ATTR_WDUMMY
= 1 << 10,
102 ATTR_BOLD_FAINT
= ATTR_BOLD
| ATTR_FAINT
,
105 enum cursor_movement
{
118 MODE_INSERT
= 1 << 1,
119 MODE_APPKEYPAD
= 1 << 2,
120 MODE_ALTSCREEN
= 1 << 3,
122 MODE_MOUSEBTN
= 1 << 5,
123 MODE_MOUSEMOTION
= 1 << 6,
124 MODE_REVERSE
= 1 << 7,
125 MODE_KBDLOCK
= 1 << 8,
128 MODE_APPCURSOR
= 1 << 11,
129 MODE_MOUSESGR
= 1 << 12,
131 MODE_BLINK
= 1 << 14,
132 MODE_FBLINK
= 1 << 15,
133 MODE_FOCUS
= 1 << 16,
134 MODE_MOUSEX10
= 1 << 17,
135 MODE_MOUSEMANY
= 1 << 18,
136 MODE_BRCKTPASTE
= 1 << 19,
137 MODE_PRINT
= 1 << 20,
138 MODE_MOUSE
= MODE_MOUSEBTN
|MODE_MOUSEMOTION
|MODE_MOUSEX10\
155 ESC_STR
= 4, /* DCS, OSC, PM, APC */
157 ESC_STR_END
= 16, /* a final string was encountered */
158 ESC_TEST
= 32, /* Enter in test mode */
166 enum selection_mode
{
172 enum selection_type
{
177 enum selection_snap
{
182 typedef unsigned char uchar
;
183 typedef unsigned int uint
;
184 typedef unsigned long ulong
;
185 typedef unsigned short ushort
;
187 typedef uint_least32_t Rune
;
189 typedef XftDraw
*Draw
;
190 typedef XftColor Color
;
193 Rune u
; /* character code */
194 ushort mode
; /* attribute flags */
195 uint32_t fg
; /* foreground */
196 uint32_t bg
; /* background */
202 Glyph attr
; /* current char attributes */
208 /* CSI Escape sequence structs */
209 /* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
211 char buf
[ESC_BUF_SIZ
]; /* raw string */
212 int len
; /* raw string length */
214 int arg
[ESC_ARG_SIZ
];
215 int narg
; /* nb of args */
219 /* STR Escape sequence structs */
220 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
222 char type
; /* ESC type ... */
223 char buf
[STR_BUF_SIZ
]; /* raw string */
224 int len
; /* raw string length */
225 char *args
[STR_ARG_SIZ
];
226 int narg
; /* nb of args */
229 /* Internal representation of the screen */
231 int row
; /* nb row */
232 int col
; /* nb col */
233 Line
*line
; /* screen */
234 Line
*alt
; /* alternate screen */
235 int *dirty
; /* dirtyness of lines */
236 XftGlyphFontSpec
*specbuf
; /* font spec buffer used for rendering */
237 TCursor c
; /* cursor */
238 int top
; /* top scroll limit */
239 int bot
; /* bottom scroll limit */
240 int mode
; /* terminal mode flags */
241 int esc
; /* escape state flags */
242 char trantbl
[4]; /* charset table translation */
243 int charset
; /* current charset */
244 int icharset
; /* selected charset for sequence */
245 int numlock
; /* lock numbers in keyboard */
249 /* Purely graphic info */
255 Atom xembed
, wmdeletewin
, netwmname
, netwmpid
;
260 XSetWindowAttributes attrs
;
262 int isfixed
; /* is fixed geometry? */
263 int l
, t
; /* left and top offset */
264 int gm
; /* geometry mask */
265 int tw
, th
; /* tty width and height */
266 int w
, h
; /* window width and height */
267 int ch
; /* char height */
268 int cw
; /* char width */
269 char state
; /* focus, redraw, visible */
270 int cursor
; /* cursor style */
283 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
284 signed char appkey
; /* application keypad */
285 signed char appcursor
; /* application cursor */
286 signed char crlf
; /* crlf mode */
294 * Selection variables:
295 * nb – normalized coordinates of the beginning of the selection
296 * ne – normalized coordinates of the end of the selection
297 * ob – original coordinates of the beginning of the selection
298 * oe – original coordinates of the end of the selection
304 char *primary
, *clipboard
;
307 struct timespec tclick1
;
308 struct timespec tclick2
;
321 void (*func
)(const Arg
*);
325 /* function definitions used in config.h */
326 static void clipcopy(const Arg
*);
327 static void clippaste(const Arg
*);
328 static void numlock(const Arg
*);
329 static void selpaste(const Arg
*);
330 static void xzoom(const Arg
*);
331 static void xzoomabs(const Arg
*);
332 static void xzoomreset(const Arg
*);
333 static void printsel(const Arg
*);
334 static void printscreen(const Arg
*) ;
335 static void toggleprinter(const Arg
*);
337 /* Config.h for applying patches and the configuration. */
353 /* Drawing Context */
355 Color col
[MAX(LEN(colorname
), 256)];
356 Font font
, bfont
, ifont
, ibfont
;
360 static void die(const char *, ...);
361 static void draw(void);
362 static void redraw(void);
363 static void drawregion(int, int, int, int);
364 static void execsh(void);
365 static void stty(void);
366 static void sigchld(int);
367 static void run(void);
369 static void csidump(void);
370 static void csihandle(void);
371 static void csiparse(void);
372 static void csireset(void);
373 static int eschandle(uchar
);
374 static void strdump(void);
375 static void strhandle(void);
376 static void strparse(void);
377 static void strreset(void);
379 static int tattrset(int);
380 static void tprinter(char *, size_t);
381 static void tdumpsel(void);
382 static void tdumpline(int);
383 static void tdump(void);
384 static void tclearregion(int, int, int, int);
385 static void tcursor(int);
386 static void tdeletechar(int);
387 static void tdeleteline(int);
388 static void tinsertblank(int);
389 static void tinsertblankline(int);
390 static int tlinelen(int);
391 static void tmoveto(int, int);
392 static void tmoveato(int, int);
393 static void tnew(int, int);
394 static void tnewline(int);
395 static void tputtab(int);
396 static void tputc(Rune
);
397 static void treset(void);
398 static void tresize(int, int);
399 static void tscrollup(int, int);
400 static void tscrolldown(int, int);
401 static void tsetattr(int *, int);
402 static void tsetchar(Rune
, Glyph
*, int, int);
403 static void tsetscroll(int, int);
404 static void tswapscreen(void);
405 static void tsetdirt(int, int);
406 static void tsetdirtattr(int);
407 static void tsetmode(int, int, int *, int);
408 static void tfulldirt(void);
409 static void techo(Rune
);
410 static void tcontrolcode(uchar
);
411 static void tdectest(char );
412 static int32_t tdefcolor(int *, int *, int);
413 static void tdeftran(char);
414 static inline int match(uint
, uint
);
415 static void ttynew(void);
416 static void ttyread(void);
417 static void ttyresize(void);
418 static void ttysend(char *, size_t);
419 static void ttywrite(const char *, size_t);
420 static void tstrsequence(uchar
);
422 static inline ushort
sixd_to_16bit(int);
423 static int xmakeglyphfontspecs(XftGlyphFontSpec
*, const Glyph
*, int, int, int);
424 static void xdrawglyphfontspecs(const XftGlyphFontSpec
*, Glyph
, int, int, int);
425 static void xdrawglyph(Glyph
, int, int);
426 static void xhints(void);
427 static void xclear(int, int, int, int);
428 static void xdrawcursor(void);
429 static void xinit(void);
430 static void xloadcols(void);
431 static int xsetcolorname(int, const char *);
432 static int xgeommasktogravity(int);
433 static int xloadfont(Font
*, FcPattern
*);
434 static void xloadfonts(char *, double);
435 static void xsettitle(char *);
436 static void xresettitle(void);
437 static void xsetpointermotion(int);
438 static void xseturgency(int);
439 static void xsetsel(char *, Time
);
440 static void xtermclear(int, int, int, int);
441 static void xunloadfont(Font
*);
442 static void xunloadfonts(void);
443 static void xresize(int, int);
445 static void expose(XEvent
*);
446 static void visibility(XEvent
*);
447 static void unmap(XEvent
*);
448 static char *kmap(KeySym
, uint
);
449 static void kpress(XEvent
*);
450 static void cmessage(XEvent
*);
451 static void cresize(int, int);
452 static void resize(XEvent
*);
453 static void focus(XEvent
*);
454 static void brelease(XEvent
*);
455 static void bpress(XEvent
*);
456 static void bmotion(XEvent
*);
457 static void propnotify(XEvent
*);
458 static void selnotify(XEvent
*);
459 static void selclear(XEvent
*);
460 static void selrequest(XEvent
*);
462 static void selinit(void);
463 static void selnormalize(void);
464 static inline int selected(int, int);
465 static char *getsel(void);
466 static void selcopy(Time
);
467 static void selscroll(int, int);
468 static void selsnap(int *, int *, int);
469 static int x2col(int);
470 static int y2row(int);
471 static void getbuttoninfo(XEvent
*);
472 static void mousereport(XEvent
*);
474 static size_t utf8decode(char *, Rune
*, size_t);
475 static Rune
utf8decodebyte(char, size_t *);
476 static size_t utf8encode(Rune
, char *);
477 static char utf8encodebyte(Rune
, size_t);
478 static char *utf8strchr(char *s
, Rune u
);
479 static size_t utf8validate(Rune
*, size_t);
481 static ssize_t
xwrite(int, const char *, size_t);
482 static void *xmalloc(size_t);
483 static void *xrealloc(void *, size_t);
484 static char *xstrdup(char *);
486 static void usage(void);
488 static void (*handler
[LASTEvent
])(XEvent
*) = {
490 [ClientMessage
] = cmessage
,
491 [ConfigureNotify
] = resize
,
492 [VisibilityNotify
] = visibility
,
493 [UnmapNotify
] = unmap
,
497 [MotionNotify
] = bmotion
,
498 [ButtonPress
] = bpress
,
499 [ButtonRelease
] = brelease
,
501 * Uncomment if you want the selection to disappear when you select something
502 * different in another window.
504 /* [SelectionClear] = selclear, */
505 [SelectionNotify
] = selnotify
,
507 * PropertyNotify is only turned on when there is some INCR transfer happening
508 * for the selection retrieval.
510 [PropertyNotify
] = propnotify
,
511 [SelectionRequest
] = selrequest
,
518 static CSIEscape csiescseq
;
519 static STREscape strescseq
;
522 static Selection sel
;
524 static char **opt_cmd
= NULL
;
525 static char *opt_io
= NULL
;
526 static char *opt_title
= NULL
;
527 static char *opt_embed
= NULL
;
528 static char *opt_class
= NULL
;
529 static char *opt_font
= NULL
;
530 static char *opt_line
= NULL
;
531 static int oldbutton
= 3; /* button event on startup: 3 = release */
533 static char *usedfont
= NULL
;
534 static double usedfontsize
= 0;
535 static double defaultfontsize
= 0;
537 static uchar utfbyte
[UTF_SIZ
+ 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
538 static uchar utfmask
[UTF_SIZ
+ 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
539 static Rune utfmin
[UTF_SIZ
+ 1] = { 0, 0, 0x80, 0x800, 0x10000};
540 static Rune utfmax
[UTF_SIZ
+ 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
542 /* Font Ring Cache */
556 /* Fontcache is an array now. A new font will be appended to the array. */
557 static Fontcache frc
[16];
558 static int frclen
= 0;
561 xwrite(int fd
, const char *s
, size_t len
)
567 r
= write(fd
, s
, len
);
580 void *p
= malloc(len
);
583 die("Out of memory\n");
589 xrealloc(void *p
, size_t len
)
591 if ((p
= realloc(p
, len
)) == NULL
)
592 die("Out of memory\n");
600 if ((s
= strdup(s
)) == NULL
)
601 die("Out of memory\n");
607 utf8decode(char *c
, Rune
*u
, size_t clen
)
609 size_t i
, j
, len
, type
;
615 udecoded
= utf8decodebyte(c
[0], &len
);
616 if (!BETWEEN(len
, 1, UTF_SIZ
))
618 for (i
= 1, j
= 1; i
< clen
&& j
< len
; ++i
, ++j
) {
619 udecoded
= (udecoded
<< 6) | utf8decodebyte(c
[i
], &type
);
626 utf8validate(u
, len
);
632 utf8decodebyte(char c
, size_t *i
)
634 for (*i
= 0; *i
< LEN(utfmask
); ++(*i
))
635 if (((uchar
)c
& utfmask
[*i
]) == utfbyte
[*i
])
636 return (uchar
)c
& ~utfmask
[*i
];
642 utf8encode(Rune u
, char *c
)
646 len
= utf8validate(&u
, 0);
650 for (i
= len
- 1; i
!= 0; --i
) {
651 c
[i
] = utf8encodebyte(u
, 0);
654 c
[0] = utf8encodebyte(u
, len
);
660 utf8encodebyte(Rune u
, size_t i
)
662 return utfbyte
[i
] | (u
& ~utfmask
[i
]);
666 utf8strchr(char *s
, Rune u
)
672 for (i
= 0, j
= 0; i
< len
; i
+= j
) {
673 if (!(j
= utf8decode(&s
[i
], &r
, len
- i
)))
683 utf8validate(Rune
*u
, size_t i
)
685 if (!BETWEEN(*u
, utfmin
[i
], utfmax
[i
]) || BETWEEN(*u
, 0xD800, 0xDFFF))
687 for (i
= 1; *u
> utfmax
[i
]; ++i
)
696 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
697 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
701 sel
.clipboard
= NULL
;
702 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
703 if (sel
.xtarget
== None
)
704 sel
.xtarget
= XA_STRING
;
713 return LIMIT(x
, 0, term
.col
-1);
722 return LIMIT(y
, 0, term
.row
-1);
730 if (term
.line
[y
][i
- 1].mode
& ATTR_WRAP
)
733 while (i
> 0 && term
.line
[y
][i
- 1].u
== ' ')
744 if (sel
.type
== SEL_REGULAR
&& sel
.ob
.y
!= sel
.oe
.y
) {
745 sel
.nb
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.ob
.x
: sel
.oe
.x
;
746 sel
.ne
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.oe
.x
: sel
.ob
.x
;
748 sel
.nb
.x
= MIN(sel
.ob
.x
, sel
.oe
.x
);
749 sel
.ne
.x
= MAX(sel
.ob
.x
, sel
.oe
.x
);
751 sel
.nb
.y
= MIN(sel
.ob
.y
, sel
.oe
.y
);
752 sel
.ne
.y
= MAX(sel
.ob
.y
, sel
.oe
.y
);
754 selsnap(&sel
.nb
.x
, &sel
.nb
.y
, -1);
755 selsnap(&sel
.ne
.x
, &sel
.ne
.y
, +1);
757 /* expand selection over line breaks */
758 if (sel
.type
== SEL_RECTANGULAR
)
760 i
= tlinelen(sel
.nb
.y
);
763 if (tlinelen(sel
.ne
.y
) <= sel
.ne
.x
)
764 sel
.ne
.x
= term
.col
- 1;
768 selected(int x
, int y
)
770 if (sel
.mode
== SEL_EMPTY
)
773 if (sel
.type
== SEL_RECTANGULAR
)
774 return BETWEEN(y
, sel
.nb
.y
, sel
.ne
.y
)
775 && BETWEEN(x
, sel
.nb
.x
, sel
.ne
.x
);
777 return BETWEEN(y
, sel
.nb
.y
, sel
.ne
.y
)
778 && (y
!= sel
.nb
.y
|| x
>= sel
.nb
.x
)
779 && (y
!= sel
.ne
.y
|| x
<= sel
.ne
.x
);
783 selsnap(int *x
, int *y
, int direction
)
785 int newx
, newy
, xt
, yt
;
786 int delim
, prevdelim
;
792 * Snap around if the word wraps around at the end or
793 * beginning of a line.
795 prevgp
= &term
.line
[*y
][*x
];
796 prevdelim
= ISDELIM(prevgp
->u
);
798 newx
= *x
+ direction
;
800 if (!BETWEEN(newx
, 0, term
.col
- 1)) {
802 newx
= (newx
+ term
.col
) % term
.col
;
803 if (!BETWEEN(newy
, 0, term
.row
- 1))
809 yt
= newy
, xt
= newx
;
810 if (!(term
.line
[yt
][xt
].mode
& ATTR_WRAP
))
814 if (newx
>= tlinelen(newy
))
817 gp
= &term
.line
[newy
][newx
];
818 delim
= ISDELIM(gp
->u
);
819 if (!(gp
->mode
& ATTR_WDUMMY
) && (delim
!= prevdelim
820 || (delim
&& gp
->u
!= prevgp
->u
)))
831 * Snap around if the the previous line or the current one
832 * has set ATTR_WRAP at its end. Then the whole next or
833 * previous line will be selected.
835 *x
= (direction
< 0) ? 0 : term
.col
- 1;
837 for (; *y
> 0; *y
+= direction
) {
838 if (!(term
.line
[*y
-1][term
.col
-1].mode
843 } else if (direction
> 0) {
844 for (; *y
< term
.row
-1; *y
+= direction
) {
845 if (!(term
.line
[*y
][term
.col
-1].mode
856 getbuttoninfo(XEvent
*e
)
859 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forceselmod
);
861 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
863 sel
.oe
.x
= x2col(e
->xbutton
.x
);
864 sel
.oe
.y
= y2row(e
->xbutton
.y
);
867 sel
.type
= SEL_REGULAR
;
868 for (type
= 1; type
< LEN(selmasks
); ++type
) {
869 if (match(selmasks
[type
], state
)) {
877 mousereport(XEvent
*e
)
879 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
880 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
886 if (e
->xbutton
.type
== MotionNotify
) {
887 if (x
== ox
&& y
== oy
)
889 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
891 /* MOUSE_MOTION: no reporting if no button is pressed */
892 if (IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
895 button
= oldbutton
+ 32;
899 if (!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
906 if (e
->xbutton
.type
== ButtonPress
) {
910 } else if (e
->xbutton
.type
== ButtonRelease
) {
912 /* MODE_MOUSEX10: no button release reporting */
913 if (IS_SET(MODE_MOUSEX10
))
915 if (button
== 64 || button
== 65)
920 if (!IS_SET(MODE_MOUSEX10
)) {
921 button
+= ((state
& ShiftMask
) ? 4 : 0)
922 + ((state
& Mod4Mask
) ? 8 : 0)
923 + ((state
& ControlMask
) ? 16 : 0);
926 if (IS_SET(MODE_MOUSESGR
)) {
927 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
929 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
930 } else if (x
< 223 && y
< 223) {
931 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
932 32+button
, 32+x
+1, 32+y
+1);
946 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
951 for (mk
= mshortcuts
; mk
< mshortcuts
+ LEN(mshortcuts
); mk
++) {
952 if (e
->xbutton
.button
== mk
->b
953 && match(mk
->mask
, e
->xbutton
.state
)) {
954 ttysend(mk
->s
, strlen(mk
->s
));
959 if (e
->xbutton
.button
== Button1
) {
960 clock_gettime(CLOCK_MONOTONIC
, &now
);
962 /* Clear previous selection, logically and visually. */
964 sel
.mode
= SEL_EMPTY
;
965 sel
.type
= SEL_REGULAR
;
966 sel
.oe
.x
= sel
.ob
.x
= x2col(e
->xbutton
.x
);
967 sel
.oe
.y
= sel
.ob
.y
= y2row(e
->xbutton
.y
);
970 * If the user clicks below predefined timeouts specific
971 * snapping behaviour is exposed.
973 if (TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
974 sel
.snap
= SNAP_LINE
;
975 } else if (TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
976 sel
.snap
= SNAP_WORD
;
983 sel
.mode
= SEL_READY
;
984 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
985 sel
.tclick2
= sel
.tclick1
;
994 int y
, bufsize
, lastx
, linelen
;
1000 bufsize
= (term
.col
+1) * (sel
.ne
.y
-sel
.nb
.y
+1) * UTF_SIZ
;
1001 ptr
= str
= xmalloc(bufsize
);
1003 /* append every set & selected glyph to the selection */
1004 for (y
= sel
.nb
.y
; y
<= sel
.ne
.y
; y
++) {
1005 linelen
= tlinelen(y
);
1007 if (sel
.type
== SEL_RECTANGULAR
) {
1008 gp
= &term
.line
[y
][sel
.nb
.x
];
1011 gp
= &term
.line
[y
][sel
.nb
.y
== y
? sel
.nb
.x
: 0];
1012 lastx
= (sel
.ne
.y
== y
) ? sel
.ne
.x
: term
.col
-1;
1014 last
= &term
.line
[y
][MIN(lastx
, linelen
-1)];
1015 while (last
>= gp
&& last
->u
== ' ')
1018 for ( ; gp
<= last
; ++gp
) {
1019 if (gp
->mode
& ATTR_WDUMMY
)
1022 ptr
+= utf8encode(gp
->u
, ptr
);
1026 * Copy and pasting of line endings is inconsistent
1027 * in the inconsistent terminal and GUI world.
1028 * The best solution seems like to produce '\n' when
1029 * something is copied from st and convert '\n' to
1030 * '\r', when something to be pasted is received by
1032 * FIXME: Fix the computer world.
1034 if ((y
< sel
.ne
.y
|| lastx
>= linelen
) && !(last
->mode
& ATTR_WRAP
))
1044 xsetsel(getsel(), t
);
1048 propnotify(XEvent
*e
)
1050 XPropertyEvent
*xpev
;
1051 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1053 xpev
= &e
->xproperty
;
1054 if (xpev
->state
== PropertyNewValue
&&
1055 (xpev
->atom
== XA_PRIMARY
||
1056 xpev
->atom
== clipboard
)) {
1062 selnotify(XEvent
*e
)
1064 ulong nitems
, ofs
, rem
;
1066 uchar
*data
, *last
, *repl
;
1067 Atom type
, incratom
, property
;
1069 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
1072 if (e
->type
== SelectionNotify
) {
1073 property
= e
->xselection
.property
;
1074 } else if(e
->type
== PropertyNotify
) {
1075 property
= e
->xproperty
.atom
;
1079 if (property
== None
)
1083 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
1084 BUFSIZ
/4, False
, AnyPropertyType
,
1085 &type
, &format
, &nitems
, &rem
,
1087 fprintf(stderr
, "Clipboard allocation failed\n");
1091 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
1093 * If there is some PropertyNotify with no data, then
1094 * this is the signal of the selection owner that all
1095 * data has been transferred. We won't need to receive
1096 * PropertyNotify events anymore.
1098 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
1099 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
1103 if (type
== incratom
) {
1105 * Activate the PropertyNotify events so we receive
1106 * when the selection owner does send us the next
1109 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
1110 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
1114 * Deleting the property is the transfer start signal.
1116 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
1121 * As seen in getsel:
1122 * Line endings are inconsistent in the terminal and GUI world
1123 * copy and pasting. When receiving some selection data,
1124 * replace all '\n' with '\r'.
1125 * FIXME: Fix the computer world.
1128 last
= data
+ nitems
* format
/ 8;
1129 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
1133 if (IS_SET(MODE_BRCKTPASTE
))
1134 ttywrite("\033[200~", 6);
1135 ttysend((char *)data
, nitems
* format
/ 8);
1136 if (IS_SET(MODE_BRCKTPASTE
))
1137 ttywrite("\033[201~", 6);
1139 /* number of 32-bit chunks returned */
1140 ofs
+= nitems
* format
/ 32;
1144 * Deleting the property again tells the selection owner to send the
1145 * next data chunk in the property.
1147 if (e
->type
== PropertyNotify
)
1148 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
1152 selpaste(const Arg
*dummy
)
1154 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1155 xw
.win
, CurrentTime
);
1159 clipcopy(const Arg
*dummy
)
1163 if (sel
.clipboard
!= NULL
)
1164 free(sel
.clipboard
);
1166 if (sel
.primary
!= NULL
) {
1167 sel
.clipboard
= xstrdup(sel
.primary
);
1168 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1169 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1174 clippaste(const Arg
*dummy
)
1178 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1179 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, clipboard
,
1180 xw
.win
, CurrentTime
);
1188 sel
.mode
= SEL_IDLE
;
1190 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1194 selrequest(XEvent
*e
)
1196 XSelectionRequestEvent
*xsre
;
1197 XSelectionEvent xev
;
1198 Atom xa_targets
, string
, clipboard
;
1201 xsre
= (XSelectionRequestEvent
*) e
;
1202 xev
.type
= SelectionNotify
;
1203 xev
.requestor
= xsre
->requestor
;
1204 xev
.selection
= xsre
->selection
;
1205 xev
.target
= xsre
->target
;
1206 xev
.time
= xsre
->time
;
1207 if (xsre
->property
== None
)
1208 xsre
->property
= xsre
->target
;
1211 xev
.property
= None
;
1213 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1214 if (xsre
->target
== xa_targets
) {
1215 /* respond with the supported type */
1216 string
= sel
.xtarget
;
1217 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1218 XA_ATOM
, 32, PropModeReplace
,
1219 (uchar
*) &string
, 1);
1220 xev
.property
= xsre
->property
;
1221 } else if (xsre
->target
== sel
.xtarget
|| xsre
->target
== XA_STRING
) {
1223 * xith XA_STRING non ascii characters may be incorrect in the
1224 * requestor. It is not our problem, use utf8.
1226 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1227 if (xsre
->selection
== XA_PRIMARY
) {
1228 seltext
= sel
.primary
;
1229 } else if (xsre
->selection
== clipboard
) {
1230 seltext
= sel
.clipboard
;
1233 "Unhandled clipboard selection 0x%lx\n",
1237 if (seltext
!= NULL
) {
1238 XChangeProperty(xsre
->display
, xsre
->requestor
,
1239 xsre
->property
, xsre
->target
,
1241 (uchar
*)seltext
, strlen(seltext
));
1242 xev
.property
= xsre
->property
;
1246 /* all done, send a notification to the listener */
1247 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
1248 fprintf(stderr
, "Error sending SelectionNotify event\n");
1252 xsetsel(char *str
, Time t
)
1257 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
1258 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
1265 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
1270 if (e
->xbutton
.button
== Button2
) {
1272 } else if (e
->xbutton
.button
== Button1
) {
1273 if (sel
.mode
== SEL_READY
) {
1275 selcopy(e
->xbutton
.time
);
1278 sel
.mode
= SEL_IDLE
;
1279 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1286 int oldey
, oldex
, oldsby
, oldsey
;
1288 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
1296 sel
.mode
= SEL_READY
;
1303 if (oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
1304 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
1308 die(const char *errstr
, ...)
1312 va_start(ap
, errstr
);
1313 vfprintf(stderr
, errstr
, ap
);
1321 char **args
, *sh
, *prog
;
1322 const struct passwd
*pw
;
1323 char buf
[sizeof(long) * 8 + 1];
1326 if ((pw
= getpwuid(getuid())) == NULL
) {
1328 die("getpwuid:%s\n", strerror(errno
));
1330 die("who are you?\n");
1333 if ((sh
= getenv("SHELL")) == NULL
)
1334 sh
= (pw
->pw_shell
[0]) ? pw
->pw_shell
: shell
;
1342 args
= (opt_cmd
) ? opt_cmd
: (char *[]) {prog
, NULL
};
1344 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1346 unsetenv("COLUMNS");
1348 unsetenv("TERMCAP");
1349 setenv("LOGNAME", pw
->pw_name
, 1);
1350 setenv("USER", pw
->pw_name
, 1);
1351 setenv("SHELL", sh
, 1);
1352 setenv("HOME", pw
->pw_dir
, 1);
1353 setenv("TERM", termname
, 1);
1354 setenv("WINDOWID", buf
, 1);
1356 signal(SIGCHLD
, SIG_DFL
);
1357 signal(SIGHUP
, SIG_DFL
);
1358 signal(SIGINT
, SIG_DFL
);
1359 signal(SIGQUIT
, SIG_DFL
);
1360 signal(SIGTERM
, SIG_DFL
);
1361 signal(SIGALRM
, SIG_DFL
);
1373 if ((p
= waitpid(pid
, &stat
, WNOHANG
)) < 0)
1374 die("Waiting for pid %hd failed: %s\n", pid
, strerror(errno
));
1379 if (!WIFEXITED(stat
) || WEXITSTATUS(stat
))
1380 die("child finished with error '%d'\n", stat
);
1388 char cmd
[_POSIX_ARG_MAX
], **p
, *q
, *s
;
1391 if ((n
= strlen(stty_args
)) > sizeof(cmd
)-1)
1392 die("incorrect stty parameters\n");
1393 memcpy(cmd
, stty_args
, n
);
1395 siz
= sizeof(cmd
) - n
;
1396 for (p
= opt_cmd
; p
&& (s
= *p
); ++p
) {
1397 if ((n
= strlen(s
)) > siz
-1)
1398 die("stty parameter length too long\n");
1400 q
= memcpy(q
, s
, n
);
1405 if (system(cmd
) != 0)
1406 perror("Couldn't call stty");
1413 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1416 term
.mode
|= MODE_PRINT
;
1417 iofd
= (!strcmp(opt_io
, "-")) ?
1418 1 : open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1420 fprintf(stderr
, "Error opening %s:%s\n",
1421 opt_io
, strerror(errno
));
1426 if ((cmdfd
= open(opt_line
, O_RDWR
)) < 0)
1427 die("open line failed: %s\n", strerror(errno
));
1434 /* seems to work fine on linux, openbsd and freebsd */
1435 if (openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1436 die("openpty failed: %s\n", strerror(errno
));
1438 switch (pid
= fork()) {
1440 die("fork failed\n");
1444 setsid(); /* create a new process group */
1448 if (ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1449 die("ioctl TIOCSCTTY failed: %s\n", strerror(errno
));
1457 signal(SIGCHLD
, sigchld
);
1465 static char buf
[BUFSIZ
];
1466 static int buflen
= 0;
1468 int charsize
; /* size of utf8 char in bytes */
1472 /* append read bytes to unprocessed bytes */
1473 if ((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1474 die("Couldn't read from shell: %s\n", strerror(errno
));
1476 /* process every complete utf8 char */
1479 while ((charsize
= utf8decode(ptr
, &unicodep
, buflen
))) {
1485 /* keep any uncomplete utf8 char for the next call */
1486 memmove(buf
, ptr
, buflen
);
1490 ttywrite(const char *s
, size_t n
)
1497 * Remember that we are using a pty, which might be a modem line.
1498 * Writing too much will clog the line. That's why we are doing this
1500 * FIXME: Migrate the world to Plan 9.
1504 FD_SET(cmdfd
, &wfd
);
1508 /* Check if we can write. */
1509 if (pselect(cmdfd
+1, NULL
, &wfd
, NULL
, &tv
, NULL
) < 0) {
1512 die("select failed: %s\n", strerror(errno
));
1514 if(!FD_ISSET(cmdfd
, &wfd
)) {
1515 /* No, then free some buffer space. */
1519 * Only write 256 bytes at maximum. This seems to be a
1520 * reasonable value for a serial line. Bigger values
1521 * might clog the I/O.
1523 r
= write(cmdfd
, s
, (n
< 256)? n
: 256);
1525 die("write error on tty: %s\n",
1530 * We weren't able to write out everything.
1531 * This means the buffer is getting full
1539 /* All bytes have been written. */
1547 ttysend(char *s
, size_t n
)
1553 if (IS_SET(MODE_ECHO
))
1554 while ((len
= utf8decode(s
, &u
, n
)) > 0) {
1566 w
.ws_row
= term
.row
;
1567 w
.ws_col
= term
.col
;
1568 w
.ws_xpixel
= xw
.tw
;
1569 w
.ws_ypixel
= xw
.th
;
1570 if (ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1571 fprintf(stderr
, "Couldn't set window size: %s\n", strerror(errno
));
1579 for (i
= 0; i
< term
.row
-1; i
++) {
1580 for (j
= 0; j
< term
.col
-1; j
++) {
1581 if (term
.line
[i
][j
].mode
& attr
)
1590 tsetdirt(int top
, int bot
)
1594 LIMIT(top
, 0, term
.row
-1);
1595 LIMIT(bot
, 0, term
.row
-1);
1597 for (i
= top
; i
<= bot
; i
++)
1602 tsetdirtattr(int attr
)
1606 for (i
= 0; i
< term
.row
-1; i
++) {
1607 for (j
= 0; j
< term
.col
-1; j
++) {
1608 if (term
.line
[i
][j
].mode
& attr
) {
1619 tsetdirt(0, term
.row
-1);
1625 static TCursor c
[2];
1626 int alt
= IS_SET(MODE_ALTSCREEN
);
1628 if (mode
== CURSOR_SAVE
) {
1630 } else if (mode
== CURSOR_LOAD
) {
1632 tmoveto(c
[alt
].x
, c
[alt
].y
);
1641 term
.c
= (TCursor
){{
1645 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1647 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1648 for (i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1651 term
.bot
= term
.row
- 1;
1652 term
.mode
= MODE_WRAP
;
1653 memset(term
.trantbl
, CS_USA
, sizeof(term
.trantbl
));
1656 for (i
= 0; i
< 2; i
++) {
1658 tcursor(CURSOR_SAVE
);
1659 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1665 tnew(int col
, int row
)
1667 term
= (Term
){ .c
= { .attr
= { .fg
= defaultfg
, .bg
= defaultbg
} } };
1677 Line
*tmp
= term
.line
;
1679 term
.line
= term
.alt
;
1681 term
.mode
^= MODE_ALTSCREEN
;
1686 tscrolldown(int orig
, int n
)
1691 LIMIT(n
, 0, term
.bot
-orig
+1);
1693 tsetdirt(orig
, term
.bot
-n
);
1694 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1696 for (i
= term
.bot
; i
>= orig
+n
; i
--) {
1697 temp
= term
.line
[i
];
1698 term
.line
[i
] = term
.line
[i
-n
];
1699 term
.line
[i
-n
] = temp
;
1706 tscrollup(int orig
, int n
)
1711 LIMIT(n
, 0, term
.bot
-orig
+1);
1713 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1714 tsetdirt(orig
+n
, term
.bot
);
1716 for (i
= orig
; i
<= term
.bot
-n
; i
++) {
1717 temp
= term
.line
[i
];
1718 term
.line
[i
] = term
.line
[i
+n
];
1719 term
.line
[i
+n
] = temp
;
1722 selscroll(orig
, -n
);
1726 selscroll(int orig
, int n
)
1731 if (BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1732 if ((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1736 if (sel
.type
== SEL_RECTANGULAR
) {
1737 if (sel
.ob
.y
< term
.top
)
1738 sel
.ob
.y
= term
.top
;
1739 if (sel
.oe
.y
> term
.bot
)
1740 sel
.oe
.y
= term
.bot
;
1742 if (sel
.ob
.y
< term
.top
) {
1743 sel
.ob
.y
= term
.top
;
1746 if (sel
.oe
.y
> term
.bot
) {
1747 sel
.oe
.y
= term
.bot
;
1748 sel
.oe
.x
= term
.col
;
1756 tnewline(int first_col
)
1760 if (y
== term
.bot
) {
1761 tscrollup(term
.top
, 1);
1765 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1771 char *p
= csiescseq
.buf
, *np
;
1780 csiescseq
.buf
[csiescseq
.len
] = '\0';
1781 while (p
< csiescseq
.buf
+csiescseq
.len
) {
1783 v
= strtol(p
, &np
, 10);
1786 if (v
== LONG_MAX
|| v
== LONG_MIN
)
1788 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1790 if (*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1794 csiescseq
.mode
[0] = *p
++;
1795 csiescseq
.mode
[1] = (p
< csiescseq
.buf
+csiescseq
.len
) ? *p
: '\0';
1798 /* for absolute user moves, when decom is set */
1800 tmoveato(int x
, int y
)
1802 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1806 tmoveto(int x
, int y
)
1810 if (term
.c
.state
& CURSOR_ORIGIN
) {
1815 maxy
= term
.row
- 1;
1817 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1818 term
.c
.x
= LIMIT(x
, 0, term
.col
-1);
1819 term
.c
.y
= LIMIT(y
, miny
, maxy
);
1823 tsetchar(Rune u
, Glyph
*attr
, int x
, int y
)
1825 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1826 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1827 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1828 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1829 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1830 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1831 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1832 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1833 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1837 * The table is proudly stolen from rxvt.
1839 if (term
.trantbl
[term
.charset
] == CS_GRAPHIC0
&&
1840 BETWEEN(u
, 0x41, 0x7e) && vt100_0
[u
- 0x41])
1841 utf8decode(vt100_0
[u
- 0x41], &u
, UTF_SIZ
);
1843 if (term
.line
[y
][x
].mode
& ATTR_WIDE
) {
1844 if (x
+1 < term
.col
) {
1845 term
.line
[y
][x
+1].u
= ' ';
1846 term
.line
[y
][x
+1].mode
&= ~ATTR_WDUMMY
;
1848 } else if (term
.line
[y
][x
].mode
& ATTR_WDUMMY
) {
1849 term
.line
[y
][x
-1].u
= ' ';
1850 term
.line
[y
][x
-1].mode
&= ~ATTR_WIDE
;
1854 term
.line
[y
][x
] = *attr
;
1855 term
.line
[y
][x
].u
= u
;
1859 tclearregion(int x1
, int y1
, int x2
, int y2
)
1865 temp
= x1
, x1
= x2
, x2
= temp
;
1867 temp
= y1
, y1
= y2
, y2
= temp
;
1869 LIMIT(x1
, 0, term
.col
-1);
1870 LIMIT(x2
, 0, term
.col
-1);
1871 LIMIT(y1
, 0, term
.row
-1);
1872 LIMIT(y2
, 0, term
.row
-1);
1874 for (y
= y1
; y
<= y2
; y
++) {
1876 for (x
= x1
; x
<= x2
; x
++) {
1877 gp
= &term
.line
[y
][x
];
1880 gp
->fg
= term
.c
.attr
.fg
;
1881 gp
->bg
= term
.c
.attr
.bg
;
1894 LIMIT(n
, 0, term
.col
- term
.c
.x
);
1898 size
= term
.col
- src
;
1899 line
= term
.line
[term
.c
.y
];
1901 memmove(&line
[dst
], &line
[src
], size
* sizeof(Glyph
));
1902 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1911 LIMIT(n
, 0, term
.col
- term
.c
.x
);
1915 size
= term
.col
- dst
;
1916 line
= term
.line
[term
.c
.y
];
1918 memmove(&line
[dst
], &line
[src
], size
* sizeof(Glyph
));
1919 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1923 tinsertblankline(int n
)
1925 if (BETWEEN(term
.c
.y
, term
.top
, term
.bot
))
1926 tscrolldown(term
.c
.y
, n
);
1932 if (BETWEEN(term
.c
.y
, term
.top
, term
.bot
))
1933 tscrollup(term
.c
.y
, n
);
1937 tdefcolor(int *attr
, int *npar
, int l
)
1942 switch (attr
[*npar
+ 1]) {
1943 case 2: /* direct color in RGB space */
1944 if (*npar
+ 4 >= l
) {
1946 "erresc(38): Incorrect number of parameters (%d)\n",
1950 r
= attr
[*npar
+ 2];
1951 g
= attr
[*npar
+ 3];
1952 b
= attr
[*npar
+ 4];
1954 if (!BETWEEN(r
, 0, 255) || !BETWEEN(g
, 0, 255) || !BETWEEN(b
, 0, 255))
1955 fprintf(stderr
, "erresc: bad rgb color (%u,%u,%u)\n",
1958 idx
= TRUECOLOR(r
, g
, b
);
1960 case 5: /* indexed color */
1961 if (*npar
+ 2 >= l
) {
1963 "erresc(38): Incorrect number of parameters (%d)\n",
1968 if (!BETWEEN(attr
[*npar
], 0, 255))
1969 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[*npar
]);
1973 case 0: /* implemented defined (only foreground) */
1974 case 1: /* transparent */
1975 case 3: /* direct color in CMY space */
1976 case 4: /* direct color in CMYK space */
1979 "erresc(38): gfx attr %d unknown\n", attr
[*npar
]);
1987 tsetattr(int *attr
, int l
)
1992 for (i
= 0; i
< l
; i
++) {
1995 term
.c
.attr
.mode
&= ~(
2004 term
.c
.attr
.fg
= defaultfg
;
2005 term
.c
.attr
.bg
= defaultbg
;
2008 term
.c
.attr
.mode
|= ATTR_BOLD
;
2011 term
.c
.attr
.mode
|= ATTR_FAINT
;
2014 term
.c
.attr
.mode
|= ATTR_ITALIC
;
2017 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
2019 case 5: /* slow blink */
2021 case 6: /* rapid blink */
2022 term
.c
.attr
.mode
|= ATTR_BLINK
;
2025 term
.c
.attr
.mode
|= ATTR_REVERSE
;
2028 term
.c
.attr
.mode
|= ATTR_INVISIBLE
;
2031 term
.c
.attr
.mode
|= ATTR_STRUCK
;
2034 term
.c
.attr
.mode
&= ~(ATTR_BOLD
| ATTR_FAINT
);
2037 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
2040 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
2043 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
2046 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
2049 term
.c
.attr
.mode
&= ~ATTR_INVISIBLE
;
2052 term
.c
.attr
.mode
&= ~ATTR_STRUCK
;
2055 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
2056 term
.c
.attr
.fg
= idx
;
2059 term
.c
.attr
.fg
= defaultfg
;
2062 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
2063 term
.c
.attr
.bg
= idx
;
2066 term
.c
.attr
.bg
= defaultbg
;
2069 if (BETWEEN(attr
[i
], 30, 37)) {
2070 term
.c
.attr
.fg
= attr
[i
] - 30;
2071 } else if (BETWEEN(attr
[i
], 40, 47)) {
2072 term
.c
.attr
.bg
= attr
[i
] - 40;
2073 } else if (BETWEEN(attr
[i
], 90, 97)) {
2074 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
2075 } else if (BETWEEN(attr
[i
], 100, 107)) {
2076 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
2079 "erresc(default): gfx attr %d unknown\n",
2080 attr
[i
]), csidump();
2088 tsetscroll(int t
, int b
)
2092 LIMIT(t
, 0, term
.row
-1);
2093 LIMIT(b
, 0, term
.row
-1);
2104 tsetmode(int priv
, int set
, int *args
, int narg
)
2109 for (lim
= args
+ narg
; args
< lim
; ++args
) {
2112 case 1: /* DECCKM -- Cursor key */
2113 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
2115 case 5: /* DECSCNM -- Reverse video */
2117 MODBIT(term
.mode
, set
, MODE_REVERSE
);
2118 if (mode
!= term
.mode
)
2121 case 6: /* DECOM -- Origin */
2122 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
2125 case 7: /* DECAWM -- Auto wrap */
2126 MODBIT(term
.mode
, set
, MODE_WRAP
);
2128 case 0: /* Error (IGNORED) */
2129 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
2130 case 3: /* DECCOLM -- Column (IGNORED) */
2131 case 4: /* DECSCLM -- Scroll (IGNORED) */
2132 case 8: /* DECARM -- Auto repeat (IGNORED) */
2133 case 18: /* DECPFF -- Printer feed (IGNORED) */
2134 case 19: /* DECPEX -- Printer extent (IGNORED) */
2135 case 42: /* DECNRCM -- National characters (IGNORED) */
2136 case 12: /* att610 -- Start blinking cursor (IGNORED) */
2138 case 25: /* DECTCEM -- Text Cursor Enable Mode */
2139 MODBIT(term
.mode
, !set
, MODE_HIDE
);
2141 case 9: /* X10 mouse compatibility mode */
2142 xsetpointermotion(0);
2143 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2144 MODBIT(term
.mode
, set
, MODE_MOUSEX10
);
2146 case 1000: /* 1000: report button press */
2147 xsetpointermotion(0);
2148 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2149 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
2151 case 1002: /* 1002: report motion on button press */
2152 xsetpointermotion(0);
2153 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2154 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
2156 case 1003: /* 1003: enable all mouse motions */
2157 xsetpointermotion(set
);
2158 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2159 MODBIT(term
.mode
, set
, MODE_MOUSEMANY
);
2161 case 1004: /* 1004: send focus events to tty */
2162 MODBIT(term
.mode
, set
, MODE_FOCUS
);
2164 case 1006: /* 1006: extended reporting mode */
2165 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
2168 MODBIT(term
.mode
, set
, MODE_8BIT
);
2170 case 1049: /* swap screen & set/restore cursor as xterm */
2171 if (!allowaltscreen
)
2173 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
2175 case 47: /* swap screen */
2177 if (!allowaltscreen
)
2179 alt
= IS_SET(MODE_ALTSCREEN
);
2181 tclearregion(0, 0, term
.col
-1,
2184 if (set
^ alt
) /* set is always 1 or 0 */
2190 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
2192 case 2004: /* 2004: bracketed paste mode */
2193 MODBIT(term
.mode
, set
, MODE_BRCKTPASTE
);
2195 /* Not implemented mouse modes. See comments there. */
2196 case 1001: /* mouse highlight mode; can hang the
2197 terminal by design when implemented. */
2198 case 1005: /* UTF-8 mouse mode; will confuse
2199 applications not supporting UTF-8
2201 case 1015: /* urxvt mangled mouse mode; incompatible
2202 and can be mistaken for other control
2206 "erresc: unknown private set/reset mode %d\n",
2212 case 0: /* Error (IGNORED) */
2214 case 2: /* KAM -- keyboard action */
2215 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
2217 case 4: /* IRM -- Insertion-replacement */
2218 MODBIT(term
.mode
, set
, MODE_INSERT
);
2220 case 12: /* SRM -- Send/Receive */
2221 MODBIT(term
.mode
, !set
, MODE_ECHO
);
2223 case 20: /* LNM -- Linefeed/new line */
2224 MODBIT(term
.mode
, set
, MODE_CRLF
);
2228 "erresc: unknown set/reset mode %d\n",
2242 switch (csiescseq
.mode
[0]) {
2245 fprintf(stderr
, "erresc: unknown csi ");
2249 case '@': /* ICH -- Insert <n> blank char */
2250 DEFAULT(csiescseq
.arg
[0], 1);
2251 tinsertblank(csiescseq
.arg
[0]);
2253 case 'A': /* CUU -- Cursor <n> Up */
2254 DEFAULT(csiescseq
.arg
[0], 1);
2255 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
2257 case 'B': /* CUD -- Cursor <n> Down */
2258 case 'e': /* VPR --Cursor <n> Down */
2259 DEFAULT(csiescseq
.arg
[0], 1);
2260 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
2262 case 'i': /* MC -- Media Copy */
2263 switch (csiescseq
.arg
[0]) {
2268 tdumpline(term
.c
.y
);
2274 term
.mode
&= ~MODE_PRINT
;
2277 term
.mode
|= MODE_PRINT
;
2281 case 'c': /* DA -- Device Attributes */
2282 if (csiescseq
.arg
[0] == 0)
2283 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2285 case 'C': /* CUF -- Cursor <n> Forward */
2286 case 'a': /* HPR -- Cursor <n> Forward */
2287 DEFAULT(csiescseq
.arg
[0], 1);
2288 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
2290 case 'D': /* CUB -- Cursor <n> Backward */
2291 DEFAULT(csiescseq
.arg
[0], 1);
2292 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
2294 case 'E': /* CNL -- Cursor <n> Down and first col */
2295 DEFAULT(csiescseq
.arg
[0], 1);
2296 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
2298 case 'F': /* CPL -- Cursor <n> Up and first col */
2299 DEFAULT(csiescseq
.arg
[0], 1);
2300 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
2302 case 'g': /* TBC -- Tabulation clear */
2303 switch (csiescseq
.arg
[0]) {
2304 case 0: /* clear current tab stop */
2305 term
.tabs
[term
.c
.x
] = 0;
2307 case 3: /* clear all the tabs */
2308 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
2314 case 'G': /* CHA -- Move to <col> */
2316 DEFAULT(csiescseq
.arg
[0], 1);
2317 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
2319 case 'H': /* CUP -- Move to <row> <col> */
2321 DEFAULT(csiescseq
.arg
[0], 1);
2322 DEFAULT(csiescseq
.arg
[1], 1);
2323 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
2325 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
2326 DEFAULT(csiescseq
.arg
[0], 1);
2327 tputtab(csiescseq
.arg
[0]);
2329 case 'J': /* ED -- Clear screen */
2331 switch (csiescseq
.arg
[0]) {
2333 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
2334 if (term
.c
.y
< term
.row
-1) {
2335 tclearregion(0, term
.c
.y
+1, term
.col
-1,
2341 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
2342 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2345 tclearregion(0, 0, term
.col
-1, term
.row
-1);
2351 case 'K': /* EL -- Clear line */
2352 switch (csiescseq
.arg
[0]) {
2354 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
2358 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2361 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
2365 case 'S': /* SU -- Scroll <n> line up */
2366 DEFAULT(csiescseq
.arg
[0], 1);
2367 tscrollup(term
.top
, csiescseq
.arg
[0]);
2369 case 'T': /* SD -- Scroll <n> line down */
2370 DEFAULT(csiescseq
.arg
[0], 1);
2371 tscrolldown(term
.top
, csiescseq
.arg
[0]);
2373 case 'L': /* IL -- Insert <n> blank lines */
2374 DEFAULT(csiescseq
.arg
[0], 1);
2375 tinsertblankline(csiescseq
.arg
[0]);
2377 case 'l': /* RM -- Reset Mode */
2378 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
2380 case 'M': /* DL -- Delete <n> lines */
2381 DEFAULT(csiescseq
.arg
[0], 1);
2382 tdeleteline(csiescseq
.arg
[0]);
2384 case 'X': /* ECH -- Erase <n> char */
2385 DEFAULT(csiescseq
.arg
[0], 1);
2386 tclearregion(term
.c
.x
, term
.c
.y
,
2387 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
2389 case 'P': /* DCH -- Delete <n> char */
2390 DEFAULT(csiescseq
.arg
[0], 1);
2391 tdeletechar(csiescseq
.arg
[0]);
2393 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
2394 DEFAULT(csiescseq
.arg
[0], 1);
2395 tputtab(-csiescseq
.arg
[0]);
2397 case 'd': /* VPA -- Move to <row> */
2398 DEFAULT(csiescseq
.arg
[0], 1);
2399 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
2401 case 'h': /* SM -- Set terminal mode */
2402 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
2404 case 'm': /* SGR -- Terminal attribute (color) */
2405 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
2407 case 'n': /* DSR – Device Status Report (cursor position) */
2408 if (csiescseq
.arg
[0] == 6) {
2409 len
= snprintf(buf
, sizeof(buf
),"\033[%i;%iR",
2410 term
.c
.y
+1, term
.c
.x
+1);
2414 case 'r': /* DECSTBM -- Set Scrolling Region */
2415 if (csiescseq
.priv
) {
2418 DEFAULT(csiescseq
.arg
[0], 1);
2419 DEFAULT(csiescseq
.arg
[1], term
.row
);
2420 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
2424 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2425 tcursor(CURSOR_SAVE
);
2427 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2428 tcursor(CURSOR_LOAD
);
2431 switch (csiescseq
.mode
[1]) {
2432 case 'q': /* DECSCUSR -- Set Cursor Style */
2433 DEFAULT(csiescseq
.arg
[0], 1);
2434 if (!BETWEEN(csiescseq
.arg
[0], 0, 6)) {
2437 xw
.cursor
= csiescseq
.arg
[0];
2453 for (i
= 0; i
< csiescseq
.len
; i
++) {
2454 c
= csiescseq
.buf
[i
] & 0xff;
2457 } else if (c
== '\n') {
2459 } else if (c
== '\r') {
2461 } else if (c
== 0x1b) {
2464 printf("(%02x)", c
);
2473 memset(&csiescseq
, 0, sizeof(csiescseq
));
2482 term
.esc
&= ~(ESC_STR_END
|ESC_STR
);
2484 par
= (narg
= strescseq
.narg
) ? atoi(strescseq
.args
[0]) : 0;
2486 switch (strescseq
.type
) {
2487 case ']': /* OSC -- Operating System Command */
2493 xsettitle(strescseq
.args
[1]);
2495 case 4: /* color set */
2498 p
= strescseq
.args
[2];
2500 case 104: /* color reset, here p = NULL */
2501 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2502 if (xsetcolorname(j
, p
)) {
2503 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2506 * TODO if defaultbg color is changed, borders
2514 case 'k': /* old title set compatibility */
2515 xsettitle(strescseq
.args
[0]);
2517 case 'P': /* DCS -- Device Control String */
2518 case '_': /* APC -- Application Program Command */
2519 case '^': /* PM -- Privacy Message */
2523 fprintf(stderr
, "erresc: unknown str ");
2531 char *p
= strescseq
.buf
;
2534 strescseq
.buf
[strescseq
.len
] = '\0';
2539 while (strescseq
.narg
< STR_ARG_SIZ
) {
2540 strescseq
.args
[strescseq
.narg
++] = p
;
2541 while ((c
= *p
) != ';' && c
!= '\0')
2555 printf("ESC%c", strescseq
.type
);
2556 for (i
= 0; i
< strescseq
.len
; i
++) {
2557 c
= strescseq
.buf
[i
] & 0xff;
2560 } else if (isprint(c
)) {
2562 } else if (c
== '\n') {
2564 } else if (c
== '\r') {
2566 } else if (c
== 0x1b) {
2569 printf("(%02x)", c
);
2578 memset(&strescseq
, 0, sizeof(strescseq
));
2582 tprinter(char *s
, size_t len
)
2584 if (iofd
!= -1 && xwrite(iofd
, s
, len
) < 0) {
2585 fprintf(stderr
, "Error writing in %s:%s\n",
2586 opt_io
, strerror(errno
));
2593 toggleprinter(const Arg
*arg
)
2595 term
.mode
^= MODE_PRINT
;
2599 printscreen(const Arg
*arg
)
2605 printsel(const Arg
*arg
)
2615 if ((ptr
= getsel())) {
2616 tprinter(ptr
, strlen(ptr
));
2627 bp
= &term
.line
[n
][0];
2628 end
= &bp
[MIN(tlinelen(n
), term
.col
) - 1];
2629 if (bp
!= end
|| bp
->u
!= ' ') {
2630 for ( ;bp
<= end
; ++bp
)
2631 tprinter(buf
, utf8encode(bp
->u
, buf
));
2641 for (i
= 0; i
< term
.row
; ++i
)
2651 while (x
< term
.col
&& n
--)
2652 for (++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2655 while (x
> 0 && n
++)
2656 for (--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2659 term
.c
.x
= LIMIT(x
, 0, term
.col
-1);
2665 if (ISCONTROL(u
)) { /* control code */
2670 } else if (u
!= '\n' && u
!= '\r' && u
!= '\t') {
2679 tdeftran(char ascii
)
2681 static char cs
[] = "0B";
2682 static int vcs
[] = {CS_GRAPHIC0
, CS_USA
};
2685 if ((p
= strchr(cs
, ascii
)) == NULL
) {
2686 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2688 term
.trantbl
[term
.icharset
] = vcs
[p
- cs
];
2697 if (c
== '8') { /* DEC screen alignment test. */
2698 for (x
= 0; x
< term
.col
; ++x
) {
2699 for (y
= 0; y
< term
.row
; ++y
)
2700 tsetchar('E', &term
.c
.attr
, x
, y
);
2706 tstrsequence(uchar c
)
2709 case 0x90: /* DCS -- Device Control String */
2712 case 0x9f: /* APC -- Application Program Command */
2715 case 0x9e: /* PM -- Privacy Message */
2718 case 0x9d: /* OSC -- Operating System Command */
2724 term
.esc
|= ESC_STR
;
2728 tcontrolcode(uchar ascii
)
2735 tmoveto(term
.c
.x
-1, term
.c
.y
);
2738 tmoveto(0, term
.c
.y
);
2743 /* go to first col if the mode is set */
2744 tnewline(IS_SET(MODE_CRLF
));
2746 case '\a': /* BEL */
2747 if (term
.esc
& ESC_STR_END
) {
2748 /* backwards compatibility to xterm */
2751 if (!(xw
.state
& WIN_FOCUSED
))
2754 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
2757 case '\033': /* ESC */
2759 term
.esc
&= ~(ESC_CSI
|ESC_ALTCHARSET
|ESC_TEST
);
2760 term
.esc
|= ESC_START
;
2762 case '\016': /* SO (LS1 -- Locking shift 1) */
2763 case '\017': /* SI (LS0 -- Locking shift 0) */
2764 term
.charset
= 1 - (ascii
- '\016');
2766 case '\032': /* SUB */
2767 tsetchar('?', &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2768 case '\030': /* CAN */
2771 case '\005': /* ENQ (IGNORED) */
2772 case '\000': /* NUL (IGNORED) */
2773 case '\021': /* XON (IGNORED) */
2774 case '\023': /* XOFF (IGNORED) */
2775 case 0177: /* DEL (IGNORED) */
2777 case 0x84: /* TODO: IND */
2779 case 0x85: /* NEL -- Next line */
2780 tnewline(1); /* always go to first col */
2782 case 0x88: /* HTS -- Horizontal tab stop */
2783 term
.tabs
[term
.c
.x
] = 1;
2785 case 0x8d: /* TODO: RI */
2786 case 0x8e: /* TODO: SS2 */
2787 case 0x8f: /* TODO: SS3 */
2788 case 0x98: /* TODO: SOS */
2790 case 0x9a: /* DECID -- Identify Terminal */
2791 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2793 case 0x9b: /* TODO: CSI */
2794 case 0x9c: /* TODO: ST */
2796 case 0x90: /* DCS -- Device Control String */
2797 case 0x9f: /* APC -- Application Program Command */
2798 case 0x9e: /* PM -- Privacy Message */
2799 case 0x9d: /* OSC -- Operating System Command */
2800 tstrsequence(ascii
);
2803 /* only CAN, SUB, \a and C1 chars interrupt a sequence */
2804 term
.esc
&= ~(ESC_STR_END
|ESC_STR
);
2808 * returns 1 when the sequence is finished and it hasn't to read
2809 * more characters for this sequence, otherwise 0
2812 eschandle(uchar ascii
)
2816 term
.esc
|= ESC_CSI
;
2819 term
.esc
|= ESC_TEST
;
2821 case 'P': /* DCS -- Device Control String */
2822 case '_': /* APC -- Application Program Command */
2823 case '^': /* PM -- Privacy Message */
2824 case ']': /* OSC -- Operating System Command */
2825 case 'k': /* old title set compatibility */
2826 tstrsequence(ascii
);
2828 case 'n': /* LS2 -- Locking shift 2 */
2829 case 'o': /* LS3 -- Locking shift 3 */
2830 term
.charset
= 2 + (ascii
- 'n');
2832 case '(': /* GZD4 -- set primary charset G0 */
2833 case ')': /* G1D4 -- set secondary charset G1 */
2834 case '*': /* G2D4 -- set tertiary charset G2 */
2835 case '+': /* G3D4 -- set quaternary charset G3 */
2836 term
.icharset
= ascii
- '(';
2837 term
.esc
|= ESC_ALTCHARSET
;
2839 case 'D': /* IND -- Linefeed */
2840 if (term
.c
.y
== term
.bot
) {
2841 tscrollup(term
.top
, 1);
2843 tmoveto(term
.c
.x
, term
.c
.y
+1);
2846 case 'E': /* NEL -- Next line */
2847 tnewline(1); /* always go to first col */
2849 case 'H': /* HTS -- Horizontal tab stop */
2850 term
.tabs
[term
.c
.x
] = 1;
2852 case 'M': /* RI -- Reverse index */
2853 if (term
.c
.y
== term
.top
) {
2854 tscrolldown(term
.top
, 1);
2856 tmoveto(term
.c
.x
, term
.c
.y
-1);
2859 case 'Z': /* DECID -- Identify Terminal */
2860 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2862 case 'c': /* RIS -- Reset to inital state */
2867 case '=': /* DECPAM -- Application keypad */
2868 term
.mode
|= MODE_APPKEYPAD
;
2870 case '>': /* DECPNM -- Normal keypad */
2871 term
.mode
&= ~MODE_APPKEYPAD
;
2873 case '7': /* DECSC -- Save Cursor */
2874 tcursor(CURSOR_SAVE
);
2876 case '8': /* DECRC -- Restore Cursor */
2877 tcursor(CURSOR_LOAD
);
2879 case '\\': /* ST -- String Terminator */
2880 if (term
.esc
& ESC_STR_END
)
2884 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2885 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2899 len
= utf8encode(u
, c
);
2900 if ((width
= wcwidth(u
)) == -1) {
2901 memcpy(c
, "\357\277\275", 4); /* UTF_INVALID */
2905 if (IS_SET(MODE_PRINT
))
2907 control
= ISCONTROL(u
);
2910 * STR sequence must be checked before anything else
2911 * because it uses all following characters until it
2912 * receives a ESC, a SUB, a ST or any other C1 control
2915 if (term
.esc
& ESC_STR
) {
2916 if (u
== '\a' || u
== 030 || u
== 032 || u
== 033 ||
2918 term
.esc
&= ~(ESC_START
|ESC_STR
);
2919 term
.esc
|= ESC_STR_END
;
2920 } else if (strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2921 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2922 strescseq
.len
+= len
;
2926 * Here is a bug in terminals. If the user never sends
2927 * some code to stop the str or esc command, then st
2928 * will stop responding. But this is better than
2929 * silently failing with unknown characters. At least
2930 * then users will report back.
2932 * In the case users ever get fixed, here is the code:
2943 * Actions of control codes must be performed as soon they arrive
2944 * because they can be embedded inside a control sequence, and
2945 * they must not cause conflicts with sequences.
2950 * control codes are not shown ever
2953 } else if (term
.esc
& ESC_START
) {
2954 if (term
.esc
& ESC_CSI
) {
2955 csiescseq
.buf
[csiescseq
.len
++] = u
;
2956 if (BETWEEN(u
, 0x40, 0x7E)
2957 || csiescseq
.len
>= \
2958 sizeof(csiescseq
.buf
)-1) {
2964 } else if (term
.esc
& ESC_ALTCHARSET
) {
2966 } else if (term
.esc
& ESC_TEST
) {
2971 /* sequence already finished */
2975 * All characters which form part of a sequence are not
2980 if (sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
2983 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
2984 if (IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
2985 gp
->mode
|= ATTR_WRAP
;
2987 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
2990 if (IS_SET(MODE_INSERT
) && term
.c
.x
+width
< term
.col
)
2991 memmove(gp
+width
, gp
, (term
.col
- term
.c
.x
- width
) * sizeof(Glyph
));
2993 if (term
.c
.x
+width
> term
.col
) {
2995 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
2998 tsetchar(u
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
3001 gp
->mode
|= ATTR_WIDE
;
3002 if (term
.c
.x
+1 < term
.col
) {
3004 gp
[1].mode
= ATTR_WDUMMY
;
3007 if (term
.c
.x
+width
< term
.col
) {
3008 tmoveto(term
.c
.x
+width
, term
.c
.y
);
3010 term
.c
.state
|= CURSOR_WRAPNEXT
;
3015 tresize(int col
, int row
)
3018 int minrow
= MIN(row
, term
.row
);
3019 int mincol
= MIN(col
, term
.col
);
3023 if (col
< 1 || row
< 1) {
3025 "tresize: error resizing to %dx%d\n", col
, row
);
3030 * slide screen to keep cursor where we expect it -
3031 * tscrollup would work here, but we can optimize to
3032 * memmove because we're freeing the earlier lines
3034 for (i
= 0; i
<= term
.c
.y
- row
; i
++) {
3038 /* ensure that both src and dst are not NULL */
3040 memmove(term
.line
, term
.line
+ i
, row
* sizeof(Line
));
3041 memmove(term
.alt
, term
.alt
+ i
, row
* sizeof(Line
));
3043 for (i
+= row
; i
< term
.row
; i
++) {
3048 /* resize to new width */
3049 term
.specbuf
= xrealloc(term
.specbuf
, col
* sizeof(XftGlyphFontSpec
));
3051 /* resize to new height */
3052 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
3053 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
3054 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
3055 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
3057 /* resize each row to new width, zero-pad if needed */
3058 for (i
= 0; i
< minrow
; i
++) {
3059 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
3060 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
3063 /* allocate any new rows */
3064 for (/* i == minrow */; i
< row
; i
++) {
3065 term
.line
[i
] = xmalloc(col
* sizeof(Glyph
));
3066 term
.alt
[i
] = xmalloc(col
* sizeof(Glyph
));
3068 if (col
> term
.col
) {
3069 bp
= term
.tabs
+ term
.col
;
3071 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
3072 while (--bp
> term
.tabs
&& !*bp
)
3074 for (bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
3077 /* update terminal size */
3080 /* reset scrolling region */
3081 tsetscroll(0, row
-1);
3082 /* make use of the LIMIT in tmoveto */
3083 tmoveto(term
.c
.x
, term
.c
.y
);
3084 /* Clearing both screens (it makes dirty all lines) */
3086 for (i
= 0; i
< 2; i
++) {
3087 if (mincol
< col
&& 0 < minrow
) {
3088 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
3090 if (0 < col
&& minrow
< row
) {
3091 tclearregion(0, minrow
, col
- 1, row
- 1);
3094 tcursor(CURSOR_LOAD
);
3100 xresize(int col
, int row
)
3102 xw
.tw
= MAX(1, col
* xw
.cw
);
3103 xw
.th
= MAX(1, row
* xw
.ch
);
3105 XFreePixmap(xw
.dpy
, xw
.buf
);
3106 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
3107 DefaultDepth(xw
.dpy
, xw
.scr
));
3108 XftDrawChange(xw
.draw
, xw
.buf
);
3109 xclear(0, 0, xw
.w
, xw
.h
);
3113 sixd_to_16bit(int x
)
3115 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
3119 xloadcolor(int i
, const char *name
, Color
*ncolor
)
3121 XRenderColor color
= { .alpha
= 0xffff };
3124 if (BETWEEN(i
, 16, 255)) { /* 256 color */
3125 if (i
< 6*6*6+16) { /* same colors as xterm */
3126 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
3127 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
3128 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
3129 } else { /* greyscale */
3130 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
3131 color
.green
= color
.blue
= color
.red
;
3133 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
3134 xw
.cmap
, &color
, ncolor
);
3136 name
= colorname
[i
];
3139 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
3150 for (cp
= dc
.col
; cp
< &dc
.col
[LEN(dc
.col
)]; ++cp
)
3151 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
3154 for (i
= 0; i
< LEN(dc
.col
); i
++)
3155 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
3157 die("Could not allocate color '%s'\n", colorname
[i
]);
3159 die("Could not allocate color %d\n", i
);
3165 xsetcolorname(int x
, const char *name
)
3169 if (!BETWEEN(x
, 0, LEN(dc
.col
)))
3173 if (!xloadcolor(x
, name
, &ncolor
))
3176 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
3183 xtermclear(int col1
, int row1
, int col2
, int row2
)
3185 XftDrawRect(xw
.draw
,
3186 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
3187 borderpx
+ col1
* xw
.cw
,
3188 borderpx
+ row1
* xw
.ch
,
3189 (col2
-col1
+1) * xw
.cw
,
3190 (row2
-row1
+1) * xw
.ch
);
3194 * Absolute coordinates.
3197 xclear(int x1
, int y1
, int x2
, int y2
)
3199 XftDrawRect(xw
.draw
,
3200 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
3201 x1
, y1
, x2
-x1
, y2
-y1
);
3207 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
3208 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
3209 XSizeHints
*sizeh
= NULL
;
3211 sizeh
= XAllocSizeHints();
3213 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
3214 sizeh
->height
= xw
.h
;
3215 sizeh
->width
= xw
.w
;
3216 sizeh
->height_inc
= xw
.ch
;
3217 sizeh
->width_inc
= xw
.cw
;
3218 sizeh
->base_height
= 2 * borderpx
;
3219 sizeh
->base_width
= 2 * borderpx
;
3221 sizeh
->flags
|= PMaxSize
| PMinSize
;
3222 sizeh
->min_width
= sizeh
->max_width
= xw
.w
;
3223 sizeh
->min_height
= sizeh
->max_height
= xw
.h
;
3225 if (xw
.gm
& (XValue
|YValue
)) {
3226 sizeh
->flags
|= USPosition
| PWinGravity
;
3229 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
3232 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
3238 xgeommasktogravity(int mask
)
3240 switch (mask
& (XNegative
|YNegative
)) {
3242 return NorthWestGravity
;
3244 return NorthEastGravity
;
3246 return SouthWestGravity
;
3249 return SouthEastGravity
;
3253 xloadfont(Font
*f
, FcPattern
*pattern
)
3258 match
= FcFontMatch(NULL
, pattern
, &result
);
3262 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
3263 FcPatternDestroy(match
);
3268 f
->pattern
= FcPatternDuplicate(pattern
);
3270 f
->ascent
= f
->match
->ascent
;
3271 f
->descent
= f
->match
->descent
;
3273 f
->rbearing
= f
->match
->max_advance_width
;
3275 f
->height
= f
->ascent
+ f
->descent
;
3276 f
->width
= f
->lbearing
+ f
->rbearing
;
3282 xloadfonts(char *fontstr
, double fontsize
)
3288 if (fontstr
[0] == '-') {
3289 pattern
= XftXlfdParse(fontstr
, False
, False
);
3291 pattern
= FcNameParse((FcChar8
*)fontstr
);
3295 die("st: can't open font %s\n", fontstr
);
3298 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
3299 FcPatternDel(pattern
, FC_SIZE
);
3300 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
3301 usedfontsize
= fontsize
;
3303 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
3305 usedfontsize
= fontval
;
3306 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
3311 * Default font size is 12, if none given. This is to
3312 * have a known usedfontsize value.
3314 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
3317 defaultfontsize
= usedfontsize
;
3320 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
3321 FcDefaultSubstitute(pattern
);
3323 if (xloadfont(&dc
.font
, pattern
))
3324 die("st: can't open font %s\n", fontstr
);
3326 if (usedfontsize
< 0) {
3327 FcPatternGetDouble(dc
.font
.match
->pattern
,
3328 FC_PIXEL_SIZE
, 0, &fontval
);
3329 usedfontsize
= fontval
;
3331 defaultfontsize
= fontval
;
3334 /* Setting character width and height. */
3335 xw
.cw
= ceilf(dc
.font
.width
* cwscale
);
3336 xw
.ch
= ceilf(dc
.font
.height
* chscale
);
3338 FcPatternDel(pattern
, FC_SLANT
);
3339 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
3340 if (xloadfont(&dc
.ifont
, pattern
))
3341 die("st: can't open font %s\n", fontstr
);
3343 FcPatternDel(pattern
, FC_WEIGHT
);
3344 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
3345 if (xloadfont(&dc
.ibfont
, pattern
))
3346 die("st: can't open font %s\n", fontstr
);
3348 FcPatternDel(pattern
, FC_SLANT
);
3349 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
3350 if (xloadfont(&dc
.bfont
, pattern
))
3351 die("st: can't open font %s\n", fontstr
);
3353 FcPatternDestroy(pattern
);
3357 xunloadfont(Font
*f
)
3359 XftFontClose(xw
.dpy
, f
->match
);
3360 FcPatternDestroy(f
->pattern
);
3362 FcFontSetDestroy(f
->set
);
3368 /* Free the loaded fonts in the font cache. */
3370 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
3372 xunloadfont(&dc
.font
);
3373 xunloadfont(&dc
.bfont
);
3374 xunloadfont(&dc
.ifont
);
3375 xunloadfont(&dc
.ibfont
);
3379 xzoom(const Arg
*arg
)
3383 larg
.f
= usedfontsize
+ arg
->f
;
3388 xzoomabs(const Arg
*arg
)
3391 xloadfonts(usedfont
, arg
->f
);
3398 xzoomreset(const Arg
*arg
)
3402 if (defaultfontsize
> 0) {
3403 larg
.f
= defaultfontsize
;
3414 pid_t thispid
= getpid();
3415 XColor xmousefg
, xmousebg
;
3417 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
3418 die("Can't open display\n");
3419 xw
.scr
= XDefaultScreen(xw
.dpy
);
3420 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
3424 die("Could not init fontconfig.\n");
3426 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
3427 xloadfonts(usedfont
, 0);
3430 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
3433 /* adjust fixed window geometry */
3434 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
3435 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
3436 if (xw
.gm
& XNegative
)
3437 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - xw
.w
- 2;
3438 if (xw
.gm
& YNegative
)
3439 xw
.t
+= DisplayWidth(xw
.dpy
, xw
.scr
) - xw
.h
- 2;
3442 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
3443 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
3444 xw
.attrs
.bit_gravity
= NorthWestGravity
;
3445 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
3446 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
3447 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
3448 xw
.attrs
.colormap
= xw
.cmap
;
3450 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
3451 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
3452 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
3453 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
3454 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
3455 | CWEventMask
| CWColormap
, &xw
.attrs
);
3457 memset(&gcvalues
, 0, sizeof(gcvalues
));
3458 gcvalues
.graphics_exposures
= False
;
3459 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
3461 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
3462 DefaultDepth(xw
.dpy
, xw
.scr
));
3463 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
3464 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
3466 /* Xft rendering context */
3467 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
3470 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3471 XSetLocaleModifiers("@im=local");
3472 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3473 XSetLocaleModifiers("@im=");
3474 if ((xw
.xim
= XOpenIM(xw
.dpy
,
3475 NULL
, NULL
, NULL
)) == NULL
) {
3476 die("XOpenIM failed. Could not open input"
3481 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
3482 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
3483 XNFocusWindow
, xw
.win
, NULL
);
3485 die("XCreateIC failed. Could not obtain input method.\n");
3487 /* white cursor, black outline */
3488 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
3489 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
3491 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
3492 xmousefg
.red
= 0xffff;
3493 xmousefg
.green
= 0xffff;
3494 xmousefg
.blue
= 0xffff;
3497 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
3498 xmousebg
.red
= 0x0000;
3499 xmousebg
.green
= 0x0000;
3500 xmousebg
.blue
= 0x0000;
3503 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
3505 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
3506 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
3507 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
3508 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
3510 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
3511 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
3512 PropModeReplace
, (uchar
*)&thispid
, 1);
3515 XMapWindow(xw
.dpy
, xw
.win
);
3517 XSync(xw
.dpy
, False
);
3521 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
3523 float winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
, xp
, yp
;
3524 ushort mode
, prevmode
= USHRT_MAX
;
3525 Font
*font
= &dc
.font
;
3526 int frcflags
= FRC_NORMAL
;
3527 float runewidth
= xw
.cw
;
3531 FcPattern
*fcpattern
, *fontpattern
;
3532 FcFontSet
*fcsets
[] = { NULL
};
3533 FcCharSet
*fccharset
;
3534 int i
, f
, numspecs
= 0;
3536 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
3537 /* Fetch rune and mode for current glyph. */
3539 mode
= glyphs
[i
].mode
;
3541 /* Skip dummy wide-character spacing. */
3542 if (mode
== ATTR_WDUMMY
)
3545 /* Determine font for glyph if different from previous glyph. */
3546 if (prevmode
!= mode
) {
3549 frcflags
= FRC_NORMAL
;
3550 runewidth
= xw
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
3551 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
3553 frcflags
= FRC_ITALICBOLD
;
3554 } else if (mode
& ATTR_ITALIC
) {
3556 frcflags
= FRC_ITALIC
;
3557 } else if (mode
& ATTR_BOLD
) {
3559 frcflags
= FRC_BOLD
;
3561 yp
= winy
+ font
->ascent
;
3564 /* Lookup character index with default font. */
3565 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
3567 specs
[numspecs
].font
= font
->match
;
3568 specs
[numspecs
].glyph
= glyphidx
;
3569 specs
[numspecs
].x
= (short)xp
;
3570 specs
[numspecs
].y
= (short)yp
;
3576 /* Fallback on font cache, search the font cache for match. */
3577 for (f
= 0; f
< frclen
; f
++) {
3578 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
3579 /* Everything correct. */
3580 if (glyphidx
&& frc
[f
].flags
== frcflags
)
3582 /* We got a default font for a not found glyph. */
3583 if (!glyphidx
&& frc
[f
].flags
== frcflags
3584 && frc
[f
].unicodep
== rune
) {
3589 /* Nothing was found. Use fontconfig to find matching font. */
3592 font
->set
= FcFontSort(0, font
->pattern
,
3594 fcsets
[0] = font
->set
;
3597 * Nothing was found in the cache. Now use
3598 * some dozen of Fontconfig calls to get the
3599 * font for one single character.
3601 * Xft and fontconfig are design failures.
3603 fcpattern
= FcPatternDuplicate(font
->pattern
);
3604 fccharset
= FcCharSetCreate();
3606 FcCharSetAddChar(fccharset
, rune
);
3607 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3609 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
3611 FcConfigSubstitute(0, fcpattern
,
3613 FcDefaultSubstitute(fcpattern
);
3615 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
3619 * Overwrite or create the new cache entry.
3621 if (frclen
>= LEN(frc
)) {
3622 frclen
= LEN(frc
) - 1;
3623 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
3624 frc
[frclen
].unicodep
= 0;
3627 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
3629 frc
[frclen
].flags
= frcflags
;
3630 frc
[frclen
].unicodep
= rune
;
3632 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
3637 FcPatternDestroy(fcpattern
);
3638 FcCharSetDestroy(fccharset
);
3641 specs
[numspecs
].font
= frc
[f
].font
;
3642 specs
[numspecs
].glyph
= glyphidx
;
3643 specs
[numspecs
].x
= (short)xp
;
3644 specs
[numspecs
].y
= (short)(winy
+ frc
[f
].font
->ascent
);
3653 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
3655 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
3656 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
3657 width
= charlen
* xw
.cw
;
3658 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
3659 XRenderColor colfg
, colbg
;
3662 /* Determine foreground and background colors based on mode. */
3663 if (base
.fg
== defaultfg
) {
3664 if (base
.mode
& ATTR_ITALIC
)
3665 base
.fg
= defaultitalic
;
3666 else if ((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
))
3667 base
.fg
= defaultitalic
;
3668 else if (base
.mode
& ATTR_UNDERLINE
)
3669 base
.fg
= defaultunderline
;
3672 if (IS_TRUECOL(base
.fg
)) {
3673 colfg
.alpha
= 0xffff;
3674 colfg
.red
= TRUERED(base
.fg
);
3675 colfg
.green
= TRUEGREEN(base
.fg
);
3676 colfg
.blue
= TRUEBLUE(base
.fg
);
3677 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
3680 fg
= &dc
.col
[base
.fg
];
3683 if (IS_TRUECOL(base
.bg
)) {
3684 colbg
.alpha
= 0xffff;
3685 colbg
.green
= TRUEGREEN(base
.bg
);
3686 colbg
.red
= TRUERED(base
.bg
);
3687 colbg
.blue
= TRUEBLUE(base
.bg
);
3688 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
3691 bg
= &dc
.col
[base
.bg
];
3694 /* Change basic system colors [0-7] to bright system colors [8-15] */
3695 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
3696 fg
= &dc
.col
[base
.fg
+ 8];
3698 if (IS_SET(MODE_REVERSE
)) {
3699 if (fg
== &dc
.col
[defaultfg
]) {
3700 fg
= &dc
.col
[defaultbg
];
3702 colfg
.red
= ~fg
->color
.red
;
3703 colfg
.green
= ~fg
->color
.green
;
3704 colfg
.blue
= ~fg
->color
.blue
;
3705 colfg
.alpha
= fg
->color
.alpha
;
3706 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
3711 if (bg
== &dc
.col
[defaultbg
]) {
3712 bg
= &dc
.col
[defaultfg
];
3714 colbg
.red
= ~bg
->color
.red
;
3715 colbg
.green
= ~bg
->color
.green
;
3716 colbg
.blue
= ~bg
->color
.blue
;
3717 colbg
.alpha
= bg
->color
.alpha
;
3718 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
3724 if (base
.mode
& ATTR_REVERSE
) {
3730 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
3731 colfg
.red
= fg
->color
.red
/ 2;
3732 colfg
.green
= fg
->color
.green
/ 2;
3733 colfg
.blue
= fg
->color
.blue
/ 2;
3734 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
3738 if (base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
3741 if (base
.mode
& ATTR_INVISIBLE
)
3744 /* Intelligent cleaning up of the borders. */
3746 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
3747 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
3749 if (x
+ charlen
>= term
.col
) {
3750 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
3751 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
3754 xclear(winx
, 0, winx
+ width
, borderpx
);
3755 if (y
== term
.row
-1)
3756 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
3758 /* Clean up the region we want to draw to. */
3759 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
3761 /* Set the clip region because Xft is sometimes dirty. */
3766 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3768 /* Render the glyphs. */
3769 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
3771 /* Render underline and strikethrough. */
3772 if (base
.mode
& ATTR_UNDERLINE
) {
3773 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
3777 if (base
.mode
& ATTR_STRUCK
) {
3778 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
3782 /* Reset clip to none. */
3783 XftDrawSetClip(xw
.draw
, 0);
3787 xdrawglyph(Glyph g
, int x
, int y
)
3790 XftGlyphFontSpec spec
;
3791 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
3792 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
3798 static int oldx
= 0, oldy
= 0;
3800 Glyph g
= {' ', ATTR_NULL
, defaultbg
, defaultcs
};
3802 LIMIT(oldx
, 0, term
.col
-1);
3803 LIMIT(oldy
, 0, term
.row
-1);
3807 /* adjust position if in dummy */
3808 if (term
.line
[oldy
][oldx
].mode
& ATTR_WDUMMY
)
3810 if (term
.line
[term
.c
.y
][curx
].mode
& ATTR_WDUMMY
)
3813 g
.u
= term
.line
[term
.c
.y
][term
.c
.x
].u
;
3815 /* remove the old cursor */
3816 xdrawglyph(term
.line
[oldy
][oldx
], oldx
, oldy
);
3818 if (IS_SET(MODE_HIDE
))
3821 /* draw the new one */
3822 if (xw
.state
& WIN_FOCUSED
) {
3823 switch (xw
.cursor
) {
3824 case 0: /* Blinking Block */
3825 case 1: /* Blinking Block (Default) */
3826 case 2: /* Steady Block */
3827 if (IS_SET(MODE_REVERSE
)) {
3828 g
.mode
|= ATTR_REVERSE
;
3833 g
.mode
|= term
.line
[term
.c
.y
][curx
].mode
& ATTR_WIDE
;
3834 xdrawglyph(g
, term
.c
.x
, term
.c
.y
);
3836 case 3: /* Blinking Underline */
3837 case 4: /* Steady Underline */
3838 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3839 borderpx
+ curx
* xw
.cw
,
3840 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- cursorthickness
,
3841 xw
.cw
, cursorthickness
);
3843 case 5: /* Blinking bar */
3844 case 6: /* Steady bar */
3845 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3846 borderpx
+ curx
* xw
.cw
,
3847 borderpx
+ term
.c
.y
* xw
.ch
,
3848 cursorthickness
, xw
.ch
);
3852 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3853 borderpx
+ curx
* xw
.cw
,
3854 borderpx
+ term
.c
.y
* xw
.ch
,
3856 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3857 borderpx
+ curx
* xw
.cw
,
3858 borderpx
+ term
.c
.y
* xw
.ch
,
3860 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3861 borderpx
+ (curx
+ 1) * xw
.cw
- 1,
3862 borderpx
+ term
.c
.y
* xw
.ch
,
3864 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3865 borderpx
+ curx
* xw
.cw
,
3866 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3869 oldx
= curx
, oldy
= term
.c
.y
;
3878 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3880 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3881 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
3888 xsettitle(opt_title
? opt_title
: "st");
3901 drawregion(0, 0, term
.col
, term
.row
);
3902 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3904 XSetForeground(xw
.dpy
, dc
.gc
,
3905 dc
.col
[IS_SET(MODE_REVERSE
)?
3906 defaultfg
: defaultbg
].pixel
);
3910 drawregion(int x1
, int y1
, int x2
, int y2
)
3912 int i
, x
, y
, ox
, numspecs
;
3914 XftGlyphFontSpec
* specs
;
3915 int ena_sel
= sel
.ob
.x
!= -1 && sel
.alt
== IS_SET(MODE_ALTSCREEN
);
3917 if (!(xw
.state
& WIN_VISIBLE
))
3920 for (y
= y1
; y
< y2
; y
++) {
3924 xtermclear(0, y
, term
.col
, y
);
3927 specs
= term
.specbuf
;
3928 numspecs
= xmakeglyphfontspecs(specs
, &term
.line
[y
][x1
], x2
- x1
, x1
, y
);
3931 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
3932 new = term
.line
[y
][x
];
3933 if (new.mode
== ATTR_WDUMMY
)
3935 if (ena_sel
&& selected(x
, y
))
3936 new.mode
^= ATTR_REVERSE
;
3937 if (i
> 0 && ATTRCMP(base
, new)) {
3938 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
3950 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
3962 visibility(XEvent
*ev
)
3964 XVisibilityEvent
*e
= &ev
->xvisibility
;
3966 MODBIT(xw
.state
, e
->state
!= VisibilityFullyObscured
, WIN_VISIBLE
);
3972 xw
.state
&= ~WIN_VISIBLE
;
3976 xsetpointermotion(int set
)
3978 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
3979 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
3983 xseturgency(int add
)
3985 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3987 MODBIT(h
->flags
, add
, XUrgencyHint
);
3988 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3995 XFocusChangeEvent
*e
= &ev
->xfocus
;
3997 if (e
->mode
== NotifyGrab
)
4000 if (ev
->type
== FocusIn
) {
4001 XSetICFocus(xw
.xic
);
4002 xw
.state
|= WIN_FOCUSED
;
4004 if (IS_SET(MODE_FOCUS
))
4005 ttywrite("\033[I", 3);
4007 XUnsetICFocus(xw
.xic
);
4008 xw
.state
&= ~WIN_FOCUSED
;
4009 if (IS_SET(MODE_FOCUS
))
4010 ttywrite("\033[O", 3);
4015 match(uint mask
, uint state
)
4017 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
4021 numlock(const Arg
*dummy
)
4027 kmap(KeySym k
, uint state
)
4032 /* Check for mapped keys out of X11 function keys. */
4033 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
4034 if (mappedkeys
[i
] == k
)
4037 if (i
== LEN(mappedkeys
)) {
4038 if ((k
& 0xFFFF) < 0xFD00)
4042 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
4046 if (!match(kp
->mask
, state
))
4049 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
4051 if (term
.numlock
&& kp
->appkey
== 2)
4054 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
4057 if (IS_SET(MODE_CRLF
) ? kp
->crlf
< 0 : kp
->crlf
> 0)
4069 XKeyEvent
*e
= &ev
->xkey
;
4071 char buf
[32], *customkey
;
4077 if (IS_SET(MODE_KBDLOCK
))
4080 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
4082 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
4083 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
4084 bp
->func(&(bp
->arg
));
4089 /* 2. custom keys from config.h */
4090 if ((customkey
= kmap(ksym
, e
->state
))) {
4091 ttysend(customkey
, strlen(customkey
));
4095 /* 3. composed string from input method */
4098 if (len
== 1 && e
->state
& Mod1Mask
) {
4099 if (IS_SET(MODE_8BIT
)) {
4102 len
= utf8encode(c
, buf
);
4119 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
4121 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
4122 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
4123 xw
.state
|= WIN_FOCUSED
;
4125 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
4126 xw
.state
&= ~WIN_FOCUSED
;
4128 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
4129 /* Send SIGHUP to shell */
4136 cresize(int width
, int height
)
4145 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
4146 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
4156 if (e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
4159 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
4166 int w
= xw
.w
, h
= xw
.h
;
4168 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
4169 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
4172 /* Waiting for window mapping */
4174 XNextEvent(xw
.dpy
, &ev
);
4176 * This XFilterEvent call is required because of XOpenIM. It
4177 * does filter out the key event and some client message for
4178 * the input method too.
4180 if (XFilterEvent(&ev
, None
))
4182 if (ev
.type
== ConfigureNotify
) {
4183 w
= ev
.xconfigure
.width
;
4184 h
= ev
.xconfigure
.height
;
4186 } while (ev
.type
!= MapNotify
);
4191 clock_gettime(CLOCK_MONOTONIC
, &last
);
4194 for (xev
= actionfps
;;) {
4196 FD_SET(cmdfd
, &rfd
);
4199 if (pselect(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
4202 die("select failed: %s\n", strerror(errno
));
4204 if (FD_ISSET(cmdfd
, &rfd
)) {
4207 blinkset
= tattrset(ATTR_BLINK
);
4209 MODBIT(term
.mode
, 0, MODE_BLINK
);
4213 if (FD_ISSET(xfd
, &rfd
))
4216 clock_gettime(CLOCK_MONOTONIC
, &now
);
4217 drawtimeout
.tv_sec
= 0;
4218 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
4222 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
4223 tsetdirtattr(ATTR_BLINK
);
4224 term
.mode
^= MODE_BLINK
;
4228 deltatime
= TIMEDIFF(now
, last
);
4229 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
4235 while (XPending(xw
.dpy
)) {
4236 XNextEvent(xw
.dpy
, &ev
);
4237 if (XFilterEvent(&ev
, None
))
4239 if (handler
[ev
.type
])
4240 (handler
[ev
.type
])(&ev
);
4246 if (xev
&& !FD_ISSET(xfd
, &rfd
))
4248 if (!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
4250 if (TIMEDIFF(now
, lastblink
) \
4252 drawtimeout
.tv_nsec
= 1000;
4254 drawtimeout
.tv_nsec
= (1E6
* \
4259 drawtimeout
.tv_sec
= \
4260 drawtimeout
.tv_nsec
/ 1E9
;
4261 drawtimeout
.tv_nsec
%= (long)1E9
;
4273 die("%s " VERSION
" (c) 2010-2015 st engineers\n"
4274 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]\n"
4275 " [-i] [-t title] [-T title] [-w windowid] [-e command ...]"
4277 " st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]\n"
4278 " [-i] [-t title] [-T title] [-w windowid] [-l line]"
4279 " [stty_args ...]\n",
4284 main(int argc
, char *argv
[])
4286 uint cols
= 80, rows
= 24;
4297 opt_class
= EARGF(usage());
4304 opt_font
= EARGF(usage());
4307 xw
.gm
= XParseGeometry(EARGF(usage()),
4308 &xw
.l
, &xw
.t
, &cols
, &rows
);
4314 opt_io
= EARGF(usage());
4317 opt_line
= EARGF(usage());
4321 opt_title
= EARGF(usage());
4324 opt_embed
= EARGF(usage());
4333 /* eat all remaining arguments */
4335 if (!opt_title
&& !opt_line
)
4336 opt_title
= basename(xstrdup(argv
[0]));
4338 setlocale(LC_CTYPE
, "");
4339 XSetLocaleModifiers("");
4340 tnew(MAX(cols
, 1), MAX(rows
, 1));