1 /* See LICENSE for license details. */
14 #include <sys/ioctl.h>
15 #include <sys/select.h>
18 #include <sys/types.h>
24 #include <X11/Xatom.h>
26 #include <X11/Xutil.h>
27 #include <X11/cursorfont.h>
28 #include <X11/keysym.h>
29 #include <X11/Xft/Xft.h>
30 #include <X11/XKBlib.h>
31 #include <fontconfig/fontconfig.h>
43 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
45 #elif defined(__FreeBSD__) || defined(__DragonFly__)
51 #define XEMBED_FOCUS_IN 4
52 #define XEMBED_FOCUS_OUT 5
55 #define UTF_INVALID 0xFFFD
57 #define ESC_BUF_SIZ (128*UTF_SIZ)
58 #define ESC_ARG_SIZ 16
59 #define STR_BUF_SIZ ESC_BUF_SIZ
60 #define STR_ARG_SIZ ESC_ARG_SIZ
61 #define XK_ANY_MOD UINT_MAX
63 #define XK_SWITCH_MOD (1<<13)
66 #define MIN(a, b) ((a) < (b) ? (a) : (b))
67 #define MAX(a, b) ((a) < (b) ? (b) : (a))
68 #define LEN(a) (sizeof(a) / sizeof(a)[0])
69 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
70 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
71 #define DIVCEIL(n, d) (((n) + ((d) - 1)) / (d))
72 #define ISCONTROLC0(c) (BETWEEN(c, 0, 0x1f) || (c) == '\177')
73 #define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f))
74 #define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c))
75 #define ISDELIM(u) (utf8strchr(worddelimiters, u) != NULL)
76 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
77 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || \
79 #define IS_SET(flag) ((term.mode & (flag)) != 0)
80 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + \
81 (t1.tv_nsec-t2.tv_nsec)/1E6)
82 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
84 #define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
85 #define IS_TRUECOL(x) (1 << 24 & (x))
86 #define TRUERED(x) (((x) & 0xff0000) >> 8)
87 #define TRUEGREEN(x) (((x) & 0xff00))
88 #define TRUEBLUE(x) (((x) & 0xff) << 8)
91 enum glyph_attribute
{
96 ATTR_UNDERLINE
= 1 << 3,
98 ATTR_REVERSE
= 1 << 5,
99 ATTR_INVISIBLE
= 1 << 6,
100 ATTR_STRUCK
= 1 << 7,
103 ATTR_WDUMMY
= 1 << 10,
104 ATTR_BOLD_FAINT
= ATTR_BOLD
| ATTR_FAINT
,
107 enum cursor_movement
{
120 MODE_INSERT
= 1 << 1,
121 MODE_APPKEYPAD
= 1 << 2,
122 MODE_ALTSCREEN
= 1 << 3,
124 MODE_MOUSEBTN
= 1 << 5,
125 MODE_MOUSEMOTION
= 1 << 6,
126 MODE_REVERSE
= 1 << 7,
127 MODE_KBDLOCK
= 1 << 8,
130 MODE_APPCURSOR
= 1 << 11,
131 MODE_MOUSESGR
= 1 << 12,
133 MODE_BLINK
= 1 << 14,
134 MODE_FBLINK
= 1 << 15,
135 MODE_FOCUS
= 1 << 16,
136 MODE_MOUSEX10
= 1 << 17,
137 MODE_MOUSEMANY
= 1 << 18,
138 MODE_BRCKTPASTE
= 1 << 19,
139 MODE_PRINT
= 1 << 20,
141 MODE_SIXEL
= 1 << 22,
142 MODE_MOUSE
= MODE_MOUSEBTN
|MODE_MOUSEMOTION
|MODE_MOUSEX10\
159 ESC_STR
= 4, /* OSC, PM, APC */
161 ESC_STR_END
= 16, /* a final string was encountered */
162 ESC_TEST
= 32, /* Enter in test mode */
172 enum selection_mode
{
178 enum selection_type
{
183 enum selection_snap
{
188 typedef unsigned char uchar
;
189 typedef unsigned int uint
;
190 typedef unsigned long ulong
;
191 typedef unsigned short ushort
;
193 typedef uint_least32_t Rune
;
195 typedef XftDraw
*Draw
;
196 typedef XftColor Color
;
199 Rune u
; /* character code */
200 ushort mode
; /* attribute flags */
201 uint32_t fg
; /* foreground */
202 uint32_t bg
; /* background */
208 Glyph attr
; /* current char attributes */
214 /* CSI Escape sequence structs */
215 /* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
217 char buf
[ESC_BUF_SIZ
]; /* raw string */
218 int len
; /* raw string length */
220 int arg
[ESC_ARG_SIZ
];
221 int narg
; /* nb of args */
225 /* STR Escape sequence structs */
226 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
228 char type
; /* ESC type ... */
229 char buf
[STR_BUF_SIZ
]; /* raw string */
230 int len
; /* raw string length */
231 char *args
[STR_ARG_SIZ
];
232 int narg
; /* nb of args */
235 /* Internal representation of the screen */
237 int row
; /* nb row */
238 int col
; /* nb col */
239 Line
*line
; /* screen */
240 Line
*alt
; /* alternate screen */
241 int *dirty
; /* dirtyness of lines */
242 XftGlyphFontSpec
*specbuf
; /* font spec buffer used for rendering */
243 TCursor c
; /* cursor */
244 int top
; /* top scroll limit */
245 int bot
; /* bottom scroll limit */
246 int mode
; /* terminal mode flags */
247 int esc
; /* escape state flags */
248 char trantbl
[4]; /* charset table translation */
249 int charset
; /* current charset */
250 int icharset
; /* selected charset for sequence */
251 int numlock
; /* lock numbers in keyboard */
255 /* Purely graphic info */
261 Atom xembed
, wmdeletewin
, netwmname
, netwmpid
;
266 XSetWindowAttributes attrs
;
268 int isfixed
; /* is fixed geometry? */
269 int l
, t
; /* left and top offset */
270 int gm
; /* geometry mask */
271 int tw
, th
; /* tty width and height */
272 int w
, h
; /* window width and height */
273 int ch
; /* char height */
274 int cw
; /* char width */
275 char state
; /* focus, redraw, visible */
276 int cursor
; /* cursor style */
289 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
290 signed char appkey
; /* application keypad */
291 signed char appcursor
; /* application cursor */
292 signed char crlf
; /* crlf mode */
300 * Selection variables:
301 * nb – normalized coordinates of the beginning of the selection
302 * ne – normalized coordinates of the end of the selection
303 * ob – original coordinates of the beginning of the selection
304 * oe – original coordinates of the end of the selection
310 char *primary
, *clipboard
;
313 struct timespec tclick1
;
314 struct timespec tclick2
;
327 void (*func
)(const Arg
*);
331 /* function definitions used in config.h */
332 static void clipcopy(const Arg
*);
333 static void clippaste(const Arg
*);
334 static void numlock(const Arg
*);
335 static void selpaste(const Arg
*);
336 static void xzoom(const Arg
*);
337 static void xzoomabs(const Arg
*);
338 static void xzoomreset(const Arg
*);
339 static void printsel(const Arg
*);
340 static void printscreen(const Arg
*) ;
341 static void toggleprinter(const Arg
*);
342 static void sendbreak(const Arg
*);
344 /* Config.h for applying patches and the configuration. */
360 /* Drawing Context */
362 Color col
[MAX(LEN(colorname
), 256)];
363 Font font
, bfont
, ifont
, ibfont
;
367 static void die(const char *, ...);
368 static void draw(void);
369 static void redraw(void);
370 static void drawregion(int, int, int, int);
371 static void execsh(void);
372 static void stty(void);
373 static void sigchld(int);
374 static void run(void);
376 static void csidump(void);
377 static void csihandle(void);
378 static void csiparse(void);
379 static void csireset(void);
380 static int eschandle(uchar
);
381 static void strdump(void);
382 static void strhandle(void);
383 static void strparse(void);
384 static void strreset(void);
386 static int tattrset(int);
387 static void tprinter(char *, size_t);
388 static void tdumpsel(void);
389 static void tdumpline(int);
390 static void tdump(void);
391 static void tclearregion(int, int, int, int);
392 static void tcursor(int);
393 static void tdeletechar(int);
394 static void tdeleteline(int);
395 static void tinsertblank(int);
396 static void tinsertblankline(int);
397 static int tlinelen(int);
398 static void tmoveto(int, int);
399 static void tmoveato(int, int);
400 static void tnew(int, int);
401 static void tnewline(int);
402 static void tputtab(int);
403 static void tputc(Rune
);
404 static void treset(void);
405 static void tresize(int, int);
406 static void tscrollup(int, int);
407 static void tscrolldown(int, int);
408 static void tsetattr(int *, int);
409 static void tsetchar(Rune
, Glyph
*, int, int);
410 static void tsetscroll(int, int);
411 static void tswapscreen(void);
412 static void tsetdirt(int, int);
413 static void tsetdirtattr(int);
414 static void tsetmode(int, int, int *, int);
415 static void tfulldirt(void);
416 static void techo(Rune
);
417 static void tcontrolcode(uchar
);
418 static void tdectest(char );
419 static void tdefutf8(char);
420 static int32_t tdefcolor(int *, int *, int);
421 static void tdeftran(char);
422 static inline int match(uint
, uint
);
423 static void ttynew(void);
424 static size_t ttyread(void);
425 static void ttyresize(void);
426 static void ttysend(char *, size_t);
427 static void ttywrite(const char *, size_t);
428 static void tstrsequence(uchar
);
430 static inline ushort
sixd_to_16bit(int);
431 static int xmakeglyphfontspecs(XftGlyphFontSpec
*, const Glyph
*, int, int, int);
432 static void xdrawglyphfontspecs(const XftGlyphFontSpec
*, Glyph
, int, int, int);
433 static void xdrawglyph(Glyph
, int, int);
434 static void xhints(void);
435 static void xclear(int, int, int, int);
436 static void xdrawcursor(void);
437 static void xinit(void);
438 static void xloadcols(void);
439 static int xsetcolorname(int, const char *);
440 static int xgeommasktogravity(int);
441 static int xloadfont(Font
*, FcPattern
*);
442 static void xloadfonts(char *, double);
443 static void xsettitle(char *);
444 static void xresettitle(void);
445 static void xsetpointermotion(int);
446 static void xseturgency(int);
447 static void xsetsel(char *, Time
);
448 static void xunloadfont(Font
*);
449 static void xunloadfonts(void);
450 static void xresize(int, int);
452 static void expose(XEvent
*);
453 static void visibility(XEvent
*);
454 static void unmap(XEvent
*);
455 static char *kmap(KeySym
, uint
);
456 static void kpress(XEvent
*);
457 static void cmessage(XEvent
*);
458 static void cresize(int, int);
459 static void resize(XEvent
*);
460 static void focus(XEvent
*);
461 static void brelease(XEvent
*);
462 static void bpress(XEvent
*);
463 static void bmotion(XEvent
*);
464 static void propnotify(XEvent
*);
465 static void selnotify(XEvent
*);
466 static void selclear(XEvent
*);
467 static void selrequest(XEvent
*);
469 static void selinit(void);
470 static void selnormalize(void);
471 static inline int selected(int, int);
472 static char *getsel(void);
473 static void selcopy(Time
);
474 static void selscroll(int, int);
475 static void selsnap(int *, int *, int);
476 static int x2col(int);
477 static int y2row(int);
478 static void getbuttoninfo(XEvent
*);
479 static void mousereport(XEvent
*);
481 static size_t utf8decode(char *, Rune
*, size_t);
482 static Rune
utf8decodebyte(char, size_t *);
483 static size_t utf8encode(Rune
, char *);
484 static char utf8encodebyte(Rune
, size_t);
485 static char *utf8strchr(char *s
, Rune u
);
486 static size_t utf8validate(Rune
*, size_t);
488 static ssize_t
xwrite(int, const char *, size_t);
489 static void *xmalloc(size_t);
490 static void *xrealloc(void *, size_t);
491 static char *xstrdup(char *);
493 static void usage(void);
495 static void (*handler
[LASTEvent
])(XEvent
*) = {
497 [ClientMessage
] = cmessage
,
498 [ConfigureNotify
] = resize
,
499 [VisibilityNotify
] = visibility
,
500 [UnmapNotify
] = unmap
,
504 [MotionNotify
] = bmotion
,
505 [ButtonPress
] = bpress
,
506 [ButtonRelease
] = brelease
,
508 * Uncomment if you want the selection to disappear when you select something
509 * different in another window.
511 /* [SelectionClear] = selclear, */
512 [SelectionNotify
] = selnotify
,
514 * PropertyNotify is only turned on when there is some INCR transfer happening
515 * for the selection retrieval.
517 [PropertyNotify
] = propnotify
,
518 [SelectionRequest
] = selrequest
,
525 static CSIEscape csiescseq
;
526 static STREscape strescseq
;
529 static Selection sel
;
531 static char **opt_cmd
= NULL
;
532 static char *opt_class
= NULL
;
533 static char *opt_embed
= NULL
;
534 static char *opt_font
= NULL
;
535 static char *opt_io
= NULL
;
536 static char *opt_line
= NULL
;
537 static char *opt_name
= NULL
;
538 static char *opt_title
= NULL
;
539 static int oldbutton
= 3; /* button event on startup: 3 = release */
541 static char *usedfont
= NULL
;
542 static double usedfontsize
= 0;
543 static double defaultfontsize
= 0;
545 static uchar utfbyte
[UTF_SIZ
+ 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
546 static uchar utfmask
[UTF_SIZ
+ 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
547 static Rune utfmin
[UTF_SIZ
+ 1] = { 0, 0, 0x80, 0x800, 0x10000};
548 static Rune utfmax
[UTF_SIZ
+ 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
550 /* Font Ring Cache */
564 /* Fontcache is an array now. A new font will be appended to the array. */
565 static Fontcache frc
[16];
566 static int frclen
= 0;
569 xwrite(int fd
, const char *s
, size_t len
)
575 r
= write(fd
, s
, len
);
588 void *p
= malloc(len
);
591 die("Out of memory\n");
597 xrealloc(void *p
, size_t len
)
599 if ((p
= realloc(p
, len
)) == NULL
)
600 die("Out of memory\n");
608 if ((s
= strdup(s
)) == NULL
)
609 die("Out of memory\n");
615 utf8decode(char *c
, Rune
*u
, size_t clen
)
617 size_t i
, j
, len
, type
;
623 udecoded
= utf8decodebyte(c
[0], &len
);
624 if (!BETWEEN(len
, 1, UTF_SIZ
))
626 for (i
= 1, j
= 1; i
< clen
&& j
< len
; ++i
, ++j
) {
627 udecoded
= (udecoded
<< 6) | utf8decodebyte(c
[i
], &type
);
634 utf8validate(u
, len
);
640 utf8decodebyte(char c
, size_t *i
)
642 for (*i
= 0; *i
< LEN(utfmask
); ++(*i
))
643 if (((uchar
)c
& utfmask
[*i
]) == utfbyte
[*i
])
644 return (uchar
)c
& ~utfmask
[*i
];
650 utf8encode(Rune u
, char *c
)
654 len
= utf8validate(&u
, 0);
658 for (i
= len
- 1; i
!= 0; --i
) {
659 c
[i
] = utf8encodebyte(u
, 0);
662 c
[0] = utf8encodebyte(u
, len
);
668 utf8encodebyte(Rune u
, size_t i
)
670 return utfbyte
[i
] | (u
& ~utfmask
[i
]);
674 utf8strchr(char *s
, Rune u
)
680 for (i
= 0, j
= 0; i
< len
; i
+= j
) {
681 if (!(j
= utf8decode(&s
[i
], &r
, len
- i
)))
691 utf8validate(Rune
*u
, size_t i
)
693 if (!BETWEEN(*u
, utfmin
[i
], utfmax
[i
]) || BETWEEN(*u
, 0xD800, 0xDFFF))
695 for (i
= 1; *u
> utfmax
[i
]; ++i
)
704 clock_gettime(CLOCK_MONOTONIC
, &sel
.tclick1
);
705 clock_gettime(CLOCK_MONOTONIC
, &sel
.tclick2
);
710 sel
.clipboard
= NULL
;
711 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
712 if (sel
.xtarget
== None
)
713 sel
.xtarget
= XA_STRING
;
722 return LIMIT(x
, 0, term
.col
-1);
731 return LIMIT(y
, 0, term
.row
-1);
739 if (term
.line
[y
][i
- 1].mode
& ATTR_WRAP
)
742 while (i
> 0 && term
.line
[y
][i
- 1].u
== ' ')
753 if (sel
.type
== SEL_REGULAR
&& sel
.ob
.y
!= sel
.oe
.y
) {
754 sel
.nb
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.ob
.x
: sel
.oe
.x
;
755 sel
.ne
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.oe
.x
: sel
.ob
.x
;
757 sel
.nb
.x
= MIN(sel
.ob
.x
, sel
.oe
.x
);
758 sel
.ne
.x
= MAX(sel
.ob
.x
, sel
.oe
.x
);
760 sel
.nb
.y
= MIN(sel
.ob
.y
, sel
.oe
.y
);
761 sel
.ne
.y
= MAX(sel
.ob
.y
, sel
.oe
.y
);
763 selsnap(&sel
.nb
.x
, &sel
.nb
.y
, -1);
764 selsnap(&sel
.ne
.x
, &sel
.ne
.y
, +1);
766 /* expand selection over line breaks */
767 if (sel
.type
== SEL_RECTANGULAR
)
769 i
= tlinelen(sel
.nb
.y
);
772 if (tlinelen(sel
.ne
.y
) <= sel
.ne
.x
)
773 sel
.ne
.x
= term
.col
- 1;
777 selected(int x
, int y
)
779 if (sel
.mode
== SEL_EMPTY
)
782 if (sel
.type
== SEL_RECTANGULAR
)
783 return BETWEEN(y
, sel
.nb
.y
, sel
.ne
.y
)
784 && BETWEEN(x
, sel
.nb
.x
, sel
.ne
.x
);
786 return BETWEEN(y
, sel
.nb
.y
, sel
.ne
.y
)
787 && (y
!= sel
.nb
.y
|| x
>= sel
.nb
.x
)
788 && (y
!= sel
.ne
.y
|| x
<= sel
.ne
.x
);
792 selsnap(int *x
, int *y
, int direction
)
794 int newx
, newy
, xt
, yt
;
795 int delim
, prevdelim
;
801 * Snap around if the word wraps around at the end or
802 * beginning of a line.
804 prevgp
= &term
.line
[*y
][*x
];
805 prevdelim
= ISDELIM(prevgp
->u
);
807 newx
= *x
+ direction
;
809 if (!BETWEEN(newx
, 0, term
.col
- 1)) {
811 newx
= (newx
+ term
.col
) % term
.col
;
812 if (!BETWEEN(newy
, 0, term
.row
- 1))
818 yt
= newy
, xt
= newx
;
819 if (!(term
.line
[yt
][xt
].mode
& ATTR_WRAP
))
823 if (newx
>= tlinelen(newy
))
826 gp
= &term
.line
[newy
][newx
];
827 delim
= ISDELIM(gp
->u
);
828 if (!(gp
->mode
& ATTR_WDUMMY
) && (delim
!= prevdelim
829 || (delim
&& gp
->u
!= prevgp
->u
)))
840 * Snap around if the the previous line or the current one
841 * has set ATTR_WRAP at its end. Then the whole next or
842 * previous line will be selected.
844 *x
= (direction
< 0) ? 0 : term
.col
- 1;
846 for (; *y
> 0; *y
+= direction
) {
847 if (!(term
.line
[*y
-1][term
.col
-1].mode
852 } else if (direction
> 0) {
853 for (; *y
< term
.row
-1; *y
+= direction
) {
854 if (!(term
.line
[*y
][term
.col
-1].mode
865 getbuttoninfo(XEvent
*e
)
868 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forceselmod
);
870 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
872 sel
.oe
.x
= x2col(e
->xbutton
.x
);
873 sel
.oe
.y
= y2row(e
->xbutton
.y
);
876 sel
.type
= SEL_REGULAR
;
877 for (type
= 1; type
< LEN(selmasks
); ++type
) {
878 if (match(selmasks
[type
], state
)) {
886 mousereport(XEvent
*e
)
888 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
889 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
895 if (e
->xbutton
.type
== MotionNotify
) {
896 if (x
== ox
&& y
== oy
)
898 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
900 /* MOUSE_MOTION: no reporting if no button is pressed */
901 if (IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
904 button
= oldbutton
+ 32;
908 if (!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
915 if (e
->xbutton
.type
== ButtonPress
) {
919 } else if (e
->xbutton
.type
== ButtonRelease
) {
921 /* MODE_MOUSEX10: no button release reporting */
922 if (IS_SET(MODE_MOUSEX10
))
924 if (button
== 64 || button
== 65)
929 if (!IS_SET(MODE_MOUSEX10
)) {
930 button
+= ((state
& ShiftMask
) ? 4 : 0)
931 + ((state
& Mod4Mask
) ? 8 : 0)
932 + ((state
& ControlMask
) ? 16 : 0);
935 if (IS_SET(MODE_MOUSESGR
)) {
936 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
938 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
939 } else if (x
< 223 && y
< 223) {
940 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
941 32+button
, 32+x
+1, 32+y
+1);
955 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
960 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
961 if (e
->xbutton
.button
== ms
->b
962 && match(ms
->mask
, e
->xbutton
.state
)) {
963 ttysend(ms
->s
, strlen(ms
->s
));
968 if (e
->xbutton
.button
== Button1
) {
969 clock_gettime(CLOCK_MONOTONIC
, &now
);
971 /* Clear previous selection, logically and visually. */
973 sel
.mode
= SEL_EMPTY
;
974 sel
.type
= SEL_REGULAR
;
975 sel
.oe
.x
= sel
.ob
.x
= x2col(e
->xbutton
.x
);
976 sel
.oe
.y
= sel
.ob
.y
= y2row(e
->xbutton
.y
);
979 * If the user clicks below predefined timeouts specific
980 * snapping behaviour is exposed.
982 if (TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
983 sel
.snap
= SNAP_LINE
;
984 } else if (TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
985 sel
.snap
= SNAP_WORD
;
992 sel
.mode
= SEL_READY
;
993 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
994 sel
.tclick2
= sel
.tclick1
;
1003 int y
, bufsize
, lastx
, linelen
;
1009 bufsize
= (term
.col
+1) * (sel
.ne
.y
-sel
.nb
.y
+1) * UTF_SIZ
;
1010 ptr
= str
= xmalloc(bufsize
);
1012 /* append every set & selected glyph to the selection */
1013 for (y
= sel
.nb
.y
; y
<= sel
.ne
.y
; y
++) {
1014 if ((linelen
= tlinelen(y
)) == 0) {
1019 if (sel
.type
== SEL_RECTANGULAR
) {
1020 gp
= &term
.line
[y
][sel
.nb
.x
];
1023 gp
= &term
.line
[y
][sel
.nb
.y
== y
? sel
.nb
.x
: 0];
1024 lastx
= (sel
.ne
.y
== y
) ? sel
.ne
.x
: term
.col
-1;
1026 last
= &term
.line
[y
][MIN(lastx
, linelen
-1)];
1027 while (last
>= gp
&& last
->u
== ' ')
1030 for ( ; gp
<= last
; ++gp
) {
1031 if (gp
->mode
& ATTR_WDUMMY
)
1034 ptr
+= utf8encode(gp
->u
, ptr
);
1038 * Copy and pasting of line endings is inconsistent
1039 * in the inconsistent terminal and GUI world.
1040 * The best solution seems like to produce '\n' when
1041 * something is copied from st and convert '\n' to
1042 * '\r', when something to be pasted is received by
1044 * FIXME: Fix the computer world.
1046 if ((y
< sel
.ne
.y
|| lastx
>= linelen
) && !(last
->mode
& ATTR_WRAP
))
1056 xsetsel(getsel(), t
);
1060 propnotify(XEvent
*e
)
1062 XPropertyEvent
*xpev
;
1063 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1065 xpev
= &e
->xproperty
;
1066 if (xpev
->state
== PropertyNewValue
&&
1067 (xpev
->atom
== XA_PRIMARY
||
1068 xpev
->atom
== clipboard
)) {
1074 selnotify(XEvent
*e
)
1076 ulong nitems
, ofs
, rem
;
1078 uchar
*data
, *last
, *repl
;
1079 Atom type
, incratom
, property
;
1081 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
1084 if (e
->type
== SelectionNotify
) {
1085 property
= e
->xselection
.property
;
1086 } else if(e
->type
== PropertyNotify
) {
1087 property
= e
->xproperty
.atom
;
1091 if (property
== None
)
1095 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
1096 BUFSIZ
/4, False
, AnyPropertyType
,
1097 &type
, &format
, &nitems
, &rem
,
1099 fprintf(stderr
, "Clipboard allocation failed\n");
1103 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
1105 * If there is some PropertyNotify with no data, then
1106 * this is the signal of the selection owner that all
1107 * data has been transferred. We won't need to receive
1108 * PropertyNotify events anymore.
1110 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
1111 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
1115 if (type
== incratom
) {
1117 * Activate the PropertyNotify events so we receive
1118 * when the selection owner does send us the next
1121 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
1122 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
1126 * Deleting the property is the transfer start signal.
1128 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
1133 * As seen in getsel:
1134 * Line endings are inconsistent in the terminal and GUI world
1135 * copy and pasting. When receiving some selection data,
1136 * replace all '\n' with '\r'.
1137 * FIXME: Fix the computer world.
1140 last
= data
+ nitems
* format
/ 8;
1141 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
1145 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
1146 ttywrite("\033[200~", 6);
1147 ttysend((char *)data
, nitems
* format
/ 8);
1148 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
1149 ttywrite("\033[201~", 6);
1151 /* number of 32-bit chunks returned */
1152 ofs
+= nitems
* format
/ 32;
1156 * Deleting the property again tells the selection owner to send the
1157 * next data chunk in the property.
1159 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
1163 selpaste(const Arg
*dummy
)
1165 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1166 xw
.win
, CurrentTime
);
1170 clipcopy(const Arg
*dummy
)
1174 if (sel
.clipboard
!= NULL
)
1175 free(sel
.clipboard
);
1177 if (sel
.primary
!= NULL
) {
1178 sel
.clipboard
= xstrdup(sel
.primary
);
1179 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1180 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1185 clippaste(const Arg
*dummy
)
1189 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1190 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, clipboard
,
1191 xw
.win
, CurrentTime
);
1199 sel
.mode
= SEL_IDLE
;
1201 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1205 selrequest(XEvent
*e
)
1207 XSelectionRequestEvent
*xsre
;
1208 XSelectionEvent xev
;
1209 Atom xa_targets
, string
, clipboard
;
1212 xsre
= (XSelectionRequestEvent
*) e
;
1213 xev
.type
= SelectionNotify
;
1214 xev
.requestor
= xsre
->requestor
;
1215 xev
.selection
= xsre
->selection
;
1216 xev
.target
= xsre
->target
;
1217 xev
.time
= xsre
->time
;
1218 if (xsre
->property
== None
)
1219 xsre
->property
= xsre
->target
;
1222 xev
.property
= None
;
1224 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1225 if (xsre
->target
== xa_targets
) {
1226 /* respond with the supported type */
1227 string
= sel
.xtarget
;
1228 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1229 XA_ATOM
, 32, PropModeReplace
,
1230 (uchar
*) &string
, 1);
1231 xev
.property
= xsre
->property
;
1232 } else if (xsre
->target
== sel
.xtarget
|| xsre
->target
== XA_STRING
) {
1234 * xith XA_STRING non ascii characters may be incorrect in the
1235 * requestor. It is not our problem, use utf8.
1237 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1238 if (xsre
->selection
== XA_PRIMARY
) {
1239 seltext
= sel
.primary
;
1240 } else if (xsre
->selection
== clipboard
) {
1241 seltext
= sel
.clipboard
;
1244 "Unhandled clipboard selection 0x%lx\n",
1248 if (seltext
!= NULL
) {
1249 XChangeProperty(xsre
->display
, xsre
->requestor
,
1250 xsre
->property
, xsre
->target
,
1252 (uchar
*)seltext
, strlen(seltext
));
1253 xev
.property
= xsre
->property
;
1257 /* all done, send a notification to the listener */
1258 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
1259 fprintf(stderr
, "Error sending SelectionNotify event\n");
1263 xsetsel(char *str
, Time t
)
1268 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
1269 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
1276 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
1281 if (e
->xbutton
.button
== Button2
) {
1283 } else if (e
->xbutton
.button
== Button1
) {
1284 if (sel
.mode
== SEL_READY
) {
1286 selcopy(e
->xbutton
.time
);
1289 sel
.mode
= SEL_IDLE
;
1290 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1297 int oldey
, oldex
, oldsby
, oldsey
;
1299 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
1307 sel
.mode
= SEL_READY
;
1314 if (oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
1315 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
1319 die(const char *errstr
, ...)
1323 va_start(ap
, errstr
);
1324 vfprintf(stderr
, errstr
, ap
);
1332 char **args
, *sh
, *prog
;
1333 const struct passwd
*pw
;
1334 char buf
[sizeof(long) * 8 + 1];
1337 if ((pw
= getpwuid(getuid())) == NULL
) {
1339 die("getpwuid:%s\n", strerror(errno
));
1341 die("who are you?\n");
1344 if ((sh
= getenv("SHELL")) == NULL
)
1345 sh
= (pw
->pw_shell
[0]) ? pw
->pw_shell
: shell
;
1353 args
= (opt_cmd
) ? opt_cmd
: (char *[]) {prog
, NULL
};
1355 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1357 unsetenv("COLUMNS");
1359 unsetenv("TERMCAP");
1360 setenv("LOGNAME", pw
->pw_name
, 1);
1361 setenv("USER", pw
->pw_name
, 1);
1362 setenv("SHELL", sh
, 1);
1363 setenv("HOME", pw
->pw_dir
, 1);
1364 setenv("TERM", termname
, 1);
1365 setenv("WINDOWID", buf
, 1);
1367 signal(SIGCHLD
, SIG_DFL
);
1368 signal(SIGHUP
, SIG_DFL
);
1369 signal(SIGINT
, SIG_DFL
);
1370 signal(SIGQUIT
, SIG_DFL
);
1371 signal(SIGTERM
, SIG_DFL
);
1372 signal(SIGALRM
, SIG_DFL
);
1384 if ((p
= waitpid(pid
, &stat
, WNOHANG
)) < 0)
1385 die("Waiting for pid %hd failed: %s\n", pid
, strerror(errno
));
1390 if (!WIFEXITED(stat
) || WEXITSTATUS(stat
))
1391 die("child finished with error '%d'\n", stat
);
1399 char cmd
[_POSIX_ARG_MAX
], **p
, *q
, *s
;
1402 if ((n
= strlen(stty_args
)) > sizeof(cmd
)-1)
1403 die("incorrect stty parameters\n");
1404 memcpy(cmd
, stty_args
, n
);
1406 siz
= sizeof(cmd
) - n
;
1407 for (p
= opt_cmd
; p
&& (s
= *p
); ++p
) {
1408 if ((n
= strlen(s
)) > siz
-1)
1409 die("stty parameter length too long\n");
1416 if (system(cmd
) != 0)
1417 perror("Couldn't call stty");
1424 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1427 term
.mode
|= MODE_PRINT
;
1428 iofd
= (!strcmp(opt_io
, "-")) ?
1429 1 : open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1431 fprintf(stderr
, "Error opening %s:%s\n",
1432 opt_io
, strerror(errno
));
1437 if ((cmdfd
= open(opt_line
, O_RDWR
)) < 0)
1438 die("open line failed: %s\n", strerror(errno
));
1444 /* seems to work fine on linux, openbsd and freebsd */
1445 if (openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1446 die("openpty failed: %s\n", strerror(errno
));
1448 switch (pid
= fork()) {
1450 die("fork failed\n");
1454 setsid(); /* create a new process group */
1458 if (ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1459 die("ioctl TIOCSCTTY failed: %s\n", strerror(errno
));
1467 signal(SIGCHLD
, sigchld
);
1475 static char buf
[BUFSIZ
];
1476 static int buflen
= 0;
1478 int charsize
; /* size of utf8 char in bytes */
1482 /* append read bytes to unprocessed bytes */
1483 if ((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1484 die("Couldn't read from shell: %s\n", strerror(errno
));
1490 if (IS_SET(MODE_UTF8
) && !IS_SET(MODE_SIXEL
)) {
1491 /* process a complete utf8 char */
1492 charsize
= utf8decode(ptr
, &unicodep
, buflen
);
1502 tputc(*ptr
++ & 0xFF);
1506 /* keep any uncomplete utf8 char for the next call */
1508 memmove(buf
, ptr
, buflen
);
1514 ttywrite(const char *s
, size_t n
)
1521 * Remember that we are using a pty, which might be a modem line.
1522 * Writing too much will clog the line. That's why we are doing this
1524 * FIXME: Migrate the world to Plan 9.
1529 FD_SET(cmdfd
, &wfd
);
1530 FD_SET(cmdfd
, &rfd
);
1532 /* Check if we can write. */
1533 if (pselect(cmdfd
+1, &rfd
, &wfd
, NULL
, NULL
, NULL
) < 0) {
1536 die("select failed: %s\n", strerror(errno
));
1538 if (FD_ISSET(cmdfd
, &wfd
)) {
1540 * Only write the bytes written by ttywrite() or the
1541 * default of 256. This seems to be a reasonable value
1542 * for a serial line. Bigger values might clog the I/O.
1544 if ((r
= write(cmdfd
, s
, (n
< lim
)? n
: lim
)) < 0)
1548 * We weren't able to write out everything.
1549 * This means the buffer is getting full
1557 /* All bytes have been written. */
1561 if (FD_ISSET(cmdfd
, &rfd
))
1567 die("write error on tty: %s\n", strerror(errno
));
1571 ttysend(char *s
, size_t n
)
1578 if (!IS_SET(MODE_ECHO
))
1582 for (t
= s
; t
< lim
; t
+= len
) {
1583 if (IS_SET(MODE_UTF8
) && !IS_SET(MODE_SIXEL
)) {
1584 len
= utf8decode(t
, &u
, n
);
1601 w
.ws_row
= term
.row
;
1602 w
.ws_col
= term
.col
;
1603 w
.ws_xpixel
= xw
.tw
;
1604 w
.ws_ypixel
= xw
.th
;
1605 if (ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1606 fprintf(stderr
, "Couldn't set window size: %s\n", strerror(errno
));
1614 for (i
= 0; i
< term
.row
-1; i
++) {
1615 for (j
= 0; j
< term
.col
-1; j
++) {
1616 if (term
.line
[i
][j
].mode
& attr
)
1625 tsetdirt(int top
, int bot
)
1629 LIMIT(top
, 0, term
.row
-1);
1630 LIMIT(bot
, 0, term
.row
-1);
1632 for (i
= top
; i
<= bot
; i
++)
1637 tsetdirtattr(int attr
)
1641 for (i
= 0; i
< term
.row
-1; i
++) {
1642 for (j
= 0; j
< term
.col
-1; j
++) {
1643 if (term
.line
[i
][j
].mode
& attr
) {
1654 tsetdirt(0, term
.row
-1);
1660 static TCursor c
[2];
1661 int alt
= IS_SET(MODE_ALTSCREEN
);
1663 if (mode
== CURSOR_SAVE
) {
1665 } else if (mode
== CURSOR_LOAD
) {
1667 tmoveto(c
[alt
].x
, c
[alt
].y
);
1676 term
.c
= (TCursor
){{
1680 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1682 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1683 for (i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1686 term
.bot
= term
.row
- 1;
1687 term
.mode
= MODE_WRAP
|MODE_UTF8
;
1688 memset(term
.trantbl
, CS_USA
, sizeof(term
.trantbl
));
1691 for (i
= 0; i
< 2; i
++) {
1693 tcursor(CURSOR_SAVE
);
1694 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1700 tnew(int col
, int row
)
1702 term
= (Term
){ .c
= { .attr
= { .fg
= defaultfg
, .bg
= defaultbg
} } };
1712 Line
*tmp
= term
.line
;
1714 term
.line
= term
.alt
;
1716 term
.mode
^= MODE_ALTSCREEN
;
1721 tscrolldown(int orig
, int n
)
1726 LIMIT(n
, 0, term
.bot
-orig
+1);
1728 tsetdirt(orig
, term
.bot
-n
);
1729 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1731 for (i
= term
.bot
; i
>= orig
+n
; i
--) {
1732 temp
= term
.line
[i
];
1733 term
.line
[i
] = term
.line
[i
-n
];
1734 term
.line
[i
-n
] = temp
;
1741 tscrollup(int orig
, int n
)
1746 LIMIT(n
, 0, term
.bot
-orig
+1);
1748 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1749 tsetdirt(orig
+n
, term
.bot
);
1751 for (i
= orig
; i
<= term
.bot
-n
; i
++) {
1752 temp
= term
.line
[i
];
1753 term
.line
[i
] = term
.line
[i
+n
];
1754 term
.line
[i
+n
] = temp
;
1757 selscroll(orig
, -n
);
1761 selscroll(int orig
, int n
)
1766 if (BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1767 if ((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1771 if (sel
.type
== SEL_RECTANGULAR
) {
1772 if (sel
.ob
.y
< term
.top
)
1773 sel
.ob
.y
= term
.top
;
1774 if (sel
.oe
.y
> term
.bot
)
1775 sel
.oe
.y
= term
.bot
;
1777 if (sel
.ob
.y
< term
.top
) {
1778 sel
.ob
.y
= term
.top
;
1781 if (sel
.oe
.y
> term
.bot
) {
1782 sel
.oe
.y
= term
.bot
;
1783 sel
.oe
.x
= term
.col
;
1791 tnewline(int first_col
)
1795 if (y
== term
.bot
) {
1796 tscrollup(term
.top
, 1);
1800 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1806 char *p
= csiescseq
.buf
, *np
;
1815 csiescseq
.buf
[csiescseq
.len
] = '\0';
1816 while (p
< csiescseq
.buf
+csiescseq
.len
) {
1818 v
= strtol(p
, &np
, 10);
1821 if (v
== LONG_MAX
|| v
== LONG_MIN
)
1823 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1825 if (*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1829 csiescseq
.mode
[0] = *p
++;
1830 csiescseq
.mode
[1] = (p
< csiescseq
.buf
+csiescseq
.len
) ? *p
: '\0';
1833 /* for absolute user moves, when decom is set */
1835 tmoveato(int x
, int y
)
1837 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1841 tmoveto(int x
, int y
)
1845 if (term
.c
.state
& CURSOR_ORIGIN
) {
1850 maxy
= term
.row
- 1;
1852 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1853 term
.c
.x
= LIMIT(x
, 0, term
.col
-1);
1854 term
.c
.y
= LIMIT(y
, miny
, maxy
);
1858 tsetchar(Rune u
, Glyph
*attr
, int x
, int y
)
1860 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1861 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1862 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1863 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1864 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1865 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1866 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1867 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1868 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1872 * The table is proudly stolen from rxvt.
1874 if (term
.trantbl
[term
.charset
] == CS_GRAPHIC0
&&
1875 BETWEEN(u
, 0x41, 0x7e) && vt100_0
[u
- 0x41])
1876 utf8decode(vt100_0
[u
- 0x41], &u
, UTF_SIZ
);
1878 if (term
.line
[y
][x
].mode
& ATTR_WIDE
) {
1879 if (x
+1 < term
.col
) {
1880 term
.line
[y
][x
+1].u
= ' ';
1881 term
.line
[y
][x
+1].mode
&= ~ATTR_WDUMMY
;
1883 } else if (term
.line
[y
][x
].mode
& ATTR_WDUMMY
) {
1884 term
.line
[y
][x
-1].u
= ' ';
1885 term
.line
[y
][x
-1].mode
&= ~ATTR_WIDE
;
1889 term
.line
[y
][x
] = *attr
;
1890 term
.line
[y
][x
].u
= u
;
1894 tclearregion(int x1
, int y1
, int x2
, int y2
)
1900 temp
= x1
, x1
= x2
, x2
= temp
;
1902 temp
= y1
, y1
= y2
, y2
= temp
;
1904 LIMIT(x1
, 0, term
.col
-1);
1905 LIMIT(x2
, 0, term
.col
-1);
1906 LIMIT(y1
, 0, term
.row
-1);
1907 LIMIT(y2
, 0, term
.row
-1);
1909 for (y
= y1
; y
<= y2
; y
++) {
1911 for (x
= x1
; x
<= x2
; x
++) {
1912 gp
= &term
.line
[y
][x
];
1915 gp
->fg
= term
.c
.attr
.fg
;
1916 gp
->bg
= term
.c
.attr
.bg
;
1929 LIMIT(n
, 0, term
.col
- term
.c
.x
);
1933 size
= term
.col
- src
;
1934 line
= term
.line
[term
.c
.y
];
1936 memmove(&line
[dst
], &line
[src
], size
* sizeof(Glyph
));
1937 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1946 LIMIT(n
, 0, term
.col
- term
.c
.x
);
1950 size
= term
.col
- dst
;
1951 line
= term
.line
[term
.c
.y
];
1953 memmove(&line
[dst
], &line
[src
], size
* sizeof(Glyph
));
1954 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1958 tinsertblankline(int n
)
1960 if (BETWEEN(term
.c
.y
, term
.top
, term
.bot
))
1961 tscrolldown(term
.c
.y
, n
);
1967 if (BETWEEN(term
.c
.y
, term
.top
, term
.bot
))
1968 tscrollup(term
.c
.y
, n
);
1972 tdefcolor(int *attr
, int *npar
, int l
)
1977 switch (attr
[*npar
+ 1]) {
1978 case 2: /* direct color in RGB space */
1979 if (*npar
+ 4 >= l
) {
1981 "erresc(38): Incorrect number of parameters (%d)\n",
1985 r
= attr
[*npar
+ 2];
1986 g
= attr
[*npar
+ 3];
1987 b
= attr
[*npar
+ 4];
1989 if (!BETWEEN(r
, 0, 255) || !BETWEEN(g
, 0, 255) || !BETWEEN(b
, 0, 255))
1990 fprintf(stderr
, "erresc: bad rgb color (%u,%u,%u)\n",
1993 idx
= TRUECOLOR(r
, g
, b
);
1995 case 5: /* indexed color */
1996 if (*npar
+ 2 >= l
) {
1998 "erresc(38): Incorrect number of parameters (%d)\n",
2003 if (!BETWEEN(attr
[*npar
], 0, 255))
2004 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[*npar
]);
2008 case 0: /* implemented defined (only foreground) */
2009 case 1: /* transparent */
2010 case 3: /* direct color in CMY space */
2011 case 4: /* direct color in CMYK space */
2014 "erresc(38): gfx attr %d unknown\n", attr
[*npar
]);
2022 tsetattr(int *attr
, int l
)
2027 for (i
= 0; i
< l
; i
++) {
2030 term
.c
.attr
.mode
&= ~(
2039 term
.c
.attr
.fg
= defaultfg
;
2040 term
.c
.attr
.bg
= defaultbg
;
2043 term
.c
.attr
.mode
|= ATTR_BOLD
;
2046 term
.c
.attr
.mode
|= ATTR_FAINT
;
2049 term
.c
.attr
.mode
|= ATTR_ITALIC
;
2052 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
2054 case 5: /* slow blink */
2056 case 6: /* rapid blink */
2057 term
.c
.attr
.mode
|= ATTR_BLINK
;
2060 term
.c
.attr
.mode
|= ATTR_REVERSE
;
2063 term
.c
.attr
.mode
|= ATTR_INVISIBLE
;
2066 term
.c
.attr
.mode
|= ATTR_STRUCK
;
2069 term
.c
.attr
.mode
&= ~(ATTR_BOLD
| ATTR_FAINT
);
2072 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
2075 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
2078 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
2081 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
2084 term
.c
.attr
.mode
&= ~ATTR_INVISIBLE
;
2087 term
.c
.attr
.mode
&= ~ATTR_STRUCK
;
2090 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
2091 term
.c
.attr
.fg
= idx
;
2094 term
.c
.attr
.fg
= defaultfg
;
2097 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
2098 term
.c
.attr
.bg
= idx
;
2101 term
.c
.attr
.bg
= defaultbg
;
2104 if (BETWEEN(attr
[i
], 30, 37)) {
2105 term
.c
.attr
.fg
= attr
[i
] - 30;
2106 } else if (BETWEEN(attr
[i
], 40, 47)) {
2107 term
.c
.attr
.bg
= attr
[i
] - 40;
2108 } else if (BETWEEN(attr
[i
], 90, 97)) {
2109 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
2110 } else if (BETWEEN(attr
[i
], 100, 107)) {
2111 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
2114 "erresc(default): gfx attr %d unknown\n",
2115 attr
[i
]), csidump();
2123 tsetscroll(int t
, int b
)
2127 LIMIT(t
, 0, term
.row
-1);
2128 LIMIT(b
, 0, term
.row
-1);
2139 tsetmode(int priv
, int set
, int *args
, int narg
)
2144 for (lim
= args
+ narg
; args
< lim
; ++args
) {
2147 case 1: /* DECCKM -- Cursor key */
2148 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
2150 case 5: /* DECSCNM -- Reverse video */
2152 MODBIT(term
.mode
, set
, MODE_REVERSE
);
2153 if (mode
!= term
.mode
)
2156 case 6: /* DECOM -- Origin */
2157 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
2160 case 7: /* DECAWM -- Auto wrap */
2161 MODBIT(term
.mode
, set
, MODE_WRAP
);
2163 case 0: /* Error (IGNORED) */
2164 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
2165 case 3: /* DECCOLM -- Column (IGNORED) */
2166 case 4: /* DECSCLM -- Scroll (IGNORED) */
2167 case 8: /* DECARM -- Auto repeat (IGNORED) */
2168 case 18: /* DECPFF -- Printer feed (IGNORED) */
2169 case 19: /* DECPEX -- Printer extent (IGNORED) */
2170 case 42: /* DECNRCM -- National characters (IGNORED) */
2171 case 12: /* att610 -- Start blinking cursor (IGNORED) */
2173 case 25: /* DECTCEM -- Text Cursor Enable Mode */
2174 MODBIT(term
.mode
, !set
, MODE_HIDE
);
2176 case 9: /* X10 mouse compatibility mode */
2177 xsetpointermotion(0);
2178 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2179 MODBIT(term
.mode
, set
, MODE_MOUSEX10
);
2181 case 1000: /* 1000: report button press */
2182 xsetpointermotion(0);
2183 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2184 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
2186 case 1002: /* 1002: report motion on button press */
2187 xsetpointermotion(0);
2188 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2189 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
2191 case 1003: /* 1003: enable all mouse motions */
2192 xsetpointermotion(set
);
2193 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2194 MODBIT(term
.mode
, set
, MODE_MOUSEMANY
);
2196 case 1004: /* 1004: send focus events to tty */
2197 MODBIT(term
.mode
, set
, MODE_FOCUS
);
2199 case 1006: /* 1006: extended reporting mode */
2200 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
2203 MODBIT(term
.mode
, set
, MODE_8BIT
);
2205 case 1049: /* swap screen & set/restore cursor as xterm */
2206 if (!allowaltscreen
)
2208 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
2210 case 47: /* swap screen */
2212 if (!allowaltscreen
)
2214 alt
= IS_SET(MODE_ALTSCREEN
);
2216 tclearregion(0, 0, term
.col
-1,
2219 if (set
^ alt
) /* set is always 1 or 0 */
2225 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
2227 case 2004: /* 2004: bracketed paste mode */
2228 MODBIT(term
.mode
, set
, MODE_BRCKTPASTE
);
2230 /* Not implemented mouse modes. See comments there. */
2231 case 1001: /* mouse highlight mode; can hang the
2232 terminal by design when implemented. */
2233 case 1005: /* UTF-8 mouse mode; will confuse
2234 applications not supporting UTF-8
2236 case 1015: /* urxvt mangled mouse mode; incompatible
2237 and can be mistaken for other control
2241 "erresc: unknown private set/reset mode %d\n",
2247 case 0: /* Error (IGNORED) */
2249 case 2: /* KAM -- keyboard action */
2250 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
2252 case 4: /* IRM -- Insertion-replacement */
2253 MODBIT(term
.mode
, set
, MODE_INSERT
);
2255 case 12: /* SRM -- Send/Receive */
2256 MODBIT(term
.mode
, !set
, MODE_ECHO
);
2258 case 20: /* LNM -- Linefeed/new line */
2259 MODBIT(term
.mode
, set
, MODE_CRLF
);
2263 "erresc: unknown set/reset mode %d\n",
2277 switch (csiescseq
.mode
[0]) {
2280 fprintf(stderr
, "erresc: unknown csi ");
2284 case '@': /* ICH -- Insert <n> blank char */
2285 DEFAULT(csiescseq
.arg
[0], 1);
2286 tinsertblank(csiescseq
.arg
[0]);
2288 case 'A': /* CUU -- Cursor <n> Up */
2289 DEFAULT(csiescseq
.arg
[0], 1);
2290 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
2292 case 'B': /* CUD -- Cursor <n> Down */
2293 case 'e': /* VPR --Cursor <n> Down */
2294 DEFAULT(csiescseq
.arg
[0], 1);
2295 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
2297 case 'i': /* MC -- Media Copy */
2298 switch (csiescseq
.arg
[0]) {
2303 tdumpline(term
.c
.y
);
2309 term
.mode
&= ~MODE_PRINT
;
2312 term
.mode
|= MODE_PRINT
;
2316 case 'c': /* DA -- Device Attributes */
2317 if (csiescseq
.arg
[0] == 0)
2318 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2320 case 'C': /* CUF -- Cursor <n> Forward */
2321 case 'a': /* HPR -- Cursor <n> Forward */
2322 DEFAULT(csiescseq
.arg
[0], 1);
2323 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
2325 case 'D': /* CUB -- Cursor <n> Backward */
2326 DEFAULT(csiescseq
.arg
[0], 1);
2327 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
2329 case 'E': /* CNL -- Cursor <n> Down and first col */
2330 DEFAULT(csiescseq
.arg
[0], 1);
2331 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
2333 case 'F': /* CPL -- Cursor <n> Up and first col */
2334 DEFAULT(csiescseq
.arg
[0], 1);
2335 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
2337 case 'g': /* TBC -- Tabulation clear */
2338 switch (csiescseq
.arg
[0]) {
2339 case 0: /* clear current tab stop */
2340 term
.tabs
[term
.c
.x
] = 0;
2342 case 3: /* clear all the tabs */
2343 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
2349 case 'G': /* CHA -- Move to <col> */
2351 DEFAULT(csiescseq
.arg
[0], 1);
2352 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
2354 case 'H': /* CUP -- Move to <row> <col> */
2356 DEFAULT(csiescseq
.arg
[0], 1);
2357 DEFAULT(csiescseq
.arg
[1], 1);
2358 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
2360 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
2361 DEFAULT(csiescseq
.arg
[0], 1);
2362 tputtab(csiescseq
.arg
[0]);
2364 case 'J': /* ED -- Clear screen */
2366 switch (csiescseq
.arg
[0]) {
2368 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
2369 if (term
.c
.y
< term
.row
-1) {
2370 tclearregion(0, term
.c
.y
+1, term
.col
-1,
2376 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
2377 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2380 tclearregion(0, 0, term
.col
-1, term
.row
-1);
2386 case 'K': /* EL -- Clear line */
2387 switch (csiescseq
.arg
[0]) {
2389 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
2393 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2396 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
2400 case 'S': /* SU -- Scroll <n> line up */
2401 DEFAULT(csiescseq
.arg
[0], 1);
2402 tscrollup(term
.top
, csiescseq
.arg
[0]);
2404 case 'T': /* SD -- Scroll <n> line down */
2405 DEFAULT(csiescseq
.arg
[0], 1);
2406 tscrolldown(term
.top
, csiescseq
.arg
[0]);
2408 case 'L': /* IL -- Insert <n> blank lines */
2409 DEFAULT(csiescseq
.arg
[0], 1);
2410 tinsertblankline(csiescseq
.arg
[0]);
2412 case 'l': /* RM -- Reset Mode */
2413 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
2415 case 'M': /* DL -- Delete <n> lines */
2416 DEFAULT(csiescseq
.arg
[0], 1);
2417 tdeleteline(csiescseq
.arg
[0]);
2419 case 'X': /* ECH -- Erase <n> char */
2420 DEFAULT(csiescseq
.arg
[0], 1);
2421 tclearregion(term
.c
.x
, term
.c
.y
,
2422 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
2424 case 'P': /* DCH -- Delete <n> char */
2425 DEFAULT(csiescseq
.arg
[0], 1);
2426 tdeletechar(csiescseq
.arg
[0]);
2428 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
2429 DEFAULT(csiescseq
.arg
[0], 1);
2430 tputtab(-csiescseq
.arg
[0]);
2432 case 'd': /* VPA -- Move to <row> */
2433 DEFAULT(csiescseq
.arg
[0], 1);
2434 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
2436 case 'h': /* SM -- Set terminal mode */
2437 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
2439 case 'm': /* SGR -- Terminal attribute (color) */
2440 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
2442 case 'n': /* DSR – Device Status Report (cursor position) */
2443 if (csiescseq
.arg
[0] == 6) {
2444 len
= snprintf(buf
, sizeof(buf
),"\033[%i;%iR",
2445 term
.c
.y
+1, term
.c
.x
+1);
2449 case 'r': /* DECSTBM -- Set Scrolling Region */
2450 if (csiescseq
.priv
) {
2453 DEFAULT(csiescseq
.arg
[0], 1);
2454 DEFAULT(csiescseq
.arg
[1], term
.row
);
2455 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
2459 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2460 tcursor(CURSOR_SAVE
);
2462 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2463 tcursor(CURSOR_LOAD
);
2466 switch (csiescseq
.mode
[1]) {
2467 case 'q': /* DECSCUSR -- Set Cursor Style */
2468 DEFAULT(csiescseq
.arg
[0], 1);
2469 if (!BETWEEN(csiescseq
.arg
[0], 0, 6)) {
2472 xw
.cursor
= csiescseq
.arg
[0];
2488 for (i
= 0; i
< csiescseq
.len
; i
++) {
2489 c
= csiescseq
.buf
[i
] & 0xff;
2492 } else if (c
== '\n') {
2494 } else if (c
== '\r') {
2496 } else if (c
== 0x1b) {
2499 printf("(%02x)", c
);
2508 memset(&csiescseq
, 0, sizeof(csiescseq
));
2517 term
.esc
&= ~(ESC_STR_END
|ESC_STR
);
2519 par
= (narg
= strescseq
.narg
) ? atoi(strescseq
.args
[0]) : 0;
2521 switch (strescseq
.type
) {
2522 case ']': /* OSC -- Operating System Command */
2528 xsettitle(strescseq
.args
[1]);
2530 case 4: /* color set */
2533 p
= strescseq
.args
[2];
2535 case 104: /* color reset, here p = NULL */
2536 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2537 if (xsetcolorname(j
, p
)) {
2538 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2541 * TODO if defaultbg color is changed, borders
2549 case 'k': /* old title set compatibility */
2550 xsettitle(strescseq
.args
[0]);
2552 case 'P': /* DCS -- Device Control String */
2553 term
.mode
|= ESC_DCS
;
2554 case '_': /* APC -- Application Program Command */
2555 case '^': /* PM -- Privacy Message */
2559 fprintf(stderr
, "erresc: unknown str ");
2567 char *p
= strescseq
.buf
;
2570 strescseq
.buf
[strescseq
.len
] = '\0';
2575 while (strescseq
.narg
< STR_ARG_SIZ
) {
2576 strescseq
.args
[strescseq
.narg
++] = p
;
2577 while ((c
= *p
) != ';' && c
!= '\0')
2591 printf("ESC%c", strescseq
.type
);
2592 for (i
= 0; i
< strescseq
.len
; i
++) {
2593 c
= strescseq
.buf
[i
] & 0xff;
2596 } else if (isprint(c
)) {
2598 } else if (c
== '\n') {
2600 } else if (c
== '\r') {
2602 } else if (c
== 0x1b) {
2605 printf("(%02x)", c
);
2614 memset(&strescseq
, 0, sizeof(strescseq
));
2618 sendbreak(const Arg
*arg
)
2620 if (tcsendbreak(cmdfd
, 0))
2621 perror("Error sending break");
2625 tprinter(char *s
, size_t len
)
2627 if (iofd
!= -1 && xwrite(iofd
, s
, len
) < 0) {
2628 fprintf(stderr
, "Error writing in %s:%s\n",
2629 opt_io
, strerror(errno
));
2636 toggleprinter(const Arg
*arg
)
2638 term
.mode
^= MODE_PRINT
;
2642 printscreen(const Arg
*arg
)
2648 printsel(const Arg
*arg
)
2658 if ((ptr
= getsel())) {
2659 tprinter(ptr
, strlen(ptr
));
2670 bp
= &term
.line
[n
][0];
2671 end
= &bp
[MIN(tlinelen(n
), term
.col
) - 1];
2672 if (bp
!= end
|| bp
->u
!= ' ') {
2673 for ( ;bp
<= end
; ++bp
)
2674 tprinter(buf
, utf8encode(bp
->u
, buf
));
2684 for (i
= 0; i
< term
.row
; ++i
)
2694 while (x
< term
.col
&& n
--)
2695 for (++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2698 while (x
> 0 && n
++)
2699 for (--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2702 term
.c
.x
= LIMIT(x
, 0, term
.col
-1);
2708 if (ISCONTROL(u
)) { /* control code */
2713 } else if (u
!= '\n' && u
!= '\r' && u
!= '\t') {
2722 tdefutf8(char ascii
)
2725 term
.mode
|= MODE_UTF8
;
2726 else if (ascii
== '@')
2727 term
.mode
&= ~MODE_UTF8
;
2731 tdeftran(char ascii
)
2733 static char cs
[] = "0B";
2734 static int vcs
[] = {CS_GRAPHIC0
, CS_USA
};
2737 if ((p
= strchr(cs
, ascii
)) == NULL
) {
2738 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2740 term
.trantbl
[term
.icharset
] = vcs
[p
- cs
];
2749 if (c
== '8') { /* DEC screen alignment test. */
2750 for (x
= 0; x
< term
.col
; ++x
) {
2751 for (y
= 0; y
< term
.row
; ++y
)
2752 tsetchar('E', &term
.c
.attr
, x
, y
);
2758 tstrsequence(uchar c
)
2763 case 0x90: /* DCS -- Device Control String */
2765 term
.esc
|= ESC_DCS
;
2767 case 0x9f: /* APC -- Application Program Command */
2770 case 0x9e: /* PM -- Privacy Message */
2773 case 0x9d: /* OSC -- Operating System Command */
2778 term
.esc
|= ESC_STR
;
2782 tcontrolcode(uchar ascii
)
2789 tmoveto(term
.c
.x
-1, term
.c
.y
);
2792 tmoveto(0, term
.c
.y
);
2797 /* go to first col if the mode is set */
2798 tnewline(IS_SET(MODE_CRLF
));
2800 case '\a': /* BEL */
2801 if (term
.esc
& ESC_STR_END
) {
2802 /* backwards compatibility to xterm */
2805 if (!(xw
.state
& WIN_FOCUSED
))
2808 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
2811 case '\033': /* ESC */
2813 term
.esc
&= ~(ESC_CSI
|ESC_ALTCHARSET
|ESC_TEST
);
2814 term
.esc
|= ESC_START
;
2816 case '\016': /* SO (LS1 -- Locking shift 1) */
2817 case '\017': /* SI (LS0 -- Locking shift 0) */
2818 term
.charset
= 1 - (ascii
- '\016');
2820 case '\032': /* SUB */
2821 tsetchar('?', &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2822 case '\030': /* CAN */
2825 case '\005': /* ENQ (IGNORED) */
2826 case '\000': /* NUL (IGNORED) */
2827 case '\021': /* XON (IGNORED) */
2828 case '\023': /* XOFF (IGNORED) */
2829 case 0177: /* DEL (IGNORED) */
2831 case 0x80: /* TODO: PAD */
2832 case 0x81: /* TODO: HOP */
2833 case 0x82: /* TODO: BPH */
2834 case 0x83: /* TODO: NBH */
2835 case 0x84: /* TODO: IND */
2837 case 0x85: /* NEL -- Next line */
2838 tnewline(1); /* always go to first col */
2840 case 0x86: /* TODO: SSA */
2841 case 0x87: /* TODO: ESA */
2843 case 0x88: /* HTS -- Horizontal tab stop */
2844 term
.tabs
[term
.c
.x
] = 1;
2846 case 0x89: /* TODO: HTJ */
2847 case 0x8a: /* TODO: VTS */
2848 case 0x8b: /* TODO: PLD */
2849 case 0x8c: /* TODO: PLU */
2850 case 0x8d: /* TODO: RI */
2851 case 0x8e: /* TODO: SS2 */
2852 case 0x8f: /* TODO: SS3 */
2853 case 0x91: /* TODO: PU1 */
2854 case 0x92: /* TODO: PU2 */
2855 case 0x93: /* TODO: STS */
2856 case 0x94: /* TODO: CCH */
2857 case 0x95: /* TODO: MW */
2858 case 0x96: /* TODO: SPA */
2859 case 0x97: /* TODO: EPA */
2860 case 0x98: /* TODO: SOS */
2861 case 0x99: /* TODO: SGCI */
2863 case 0x9a: /* DECID -- Identify Terminal */
2864 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2866 case 0x9b: /* TODO: CSI */
2867 case 0x9c: /* TODO: ST */
2869 case 0x90: /* DCS -- Device Control String */
2870 case 0x9d: /* OSC -- Operating System Command */
2871 case 0x9e: /* PM -- Privacy Message */
2872 case 0x9f: /* APC -- Application Program Command */
2873 tstrsequence(ascii
);
2876 /* only CAN, SUB, \a and C1 chars interrupt a sequence */
2877 term
.esc
&= ~(ESC_STR_END
|ESC_STR
);
2881 * returns 1 when the sequence is finished and it hasn't to read
2882 * more characters for this sequence, otherwise 0
2885 eschandle(uchar ascii
)
2889 term
.esc
|= ESC_CSI
;
2892 term
.esc
|= ESC_TEST
;
2895 term
.esc
|= ESC_UTF8
;
2897 case 'P': /* DCS -- Device Control String */
2898 case '_': /* APC -- Application Program Command */
2899 case '^': /* PM -- Privacy Message */
2900 case ']': /* OSC -- Operating System Command */
2901 case 'k': /* old title set compatibility */
2902 tstrsequence(ascii
);
2904 case 'n': /* LS2 -- Locking shift 2 */
2905 case 'o': /* LS3 -- Locking shift 3 */
2906 term
.charset
= 2 + (ascii
- 'n');
2908 case '(': /* GZD4 -- set primary charset G0 */
2909 case ')': /* G1D4 -- set secondary charset G1 */
2910 case '*': /* G2D4 -- set tertiary charset G2 */
2911 case '+': /* G3D4 -- set quaternary charset G3 */
2912 term
.icharset
= ascii
- '(';
2913 term
.esc
|= ESC_ALTCHARSET
;
2915 case 'D': /* IND -- Linefeed */
2916 if (term
.c
.y
== term
.bot
) {
2917 tscrollup(term
.top
, 1);
2919 tmoveto(term
.c
.x
, term
.c
.y
+1);
2922 case 'E': /* NEL -- Next line */
2923 tnewline(1); /* always go to first col */
2925 case 'H': /* HTS -- Horizontal tab stop */
2926 term
.tabs
[term
.c
.x
] = 1;
2928 case 'M': /* RI -- Reverse index */
2929 if (term
.c
.y
== term
.top
) {
2930 tscrolldown(term
.top
, 1);
2932 tmoveto(term
.c
.x
, term
.c
.y
-1);
2935 case 'Z': /* DECID -- Identify Terminal */
2936 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2938 case 'c': /* RIS -- Reset to inital state */
2943 case '=': /* DECPAM -- Application keypad */
2944 term
.mode
|= MODE_APPKEYPAD
;
2946 case '>': /* DECPNM -- Normal keypad */
2947 term
.mode
&= ~MODE_APPKEYPAD
;
2949 case '7': /* DECSC -- Save Cursor */
2950 tcursor(CURSOR_SAVE
);
2952 case '8': /* DECRC -- Restore Cursor */
2953 tcursor(CURSOR_LOAD
);
2955 case '\\': /* ST -- String Terminator */
2956 if (term
.esc
& ESC_STR_END
)
2960 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2961 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2975 control
= ISCONTROL(u
);
2976 if (!IS_SET(MODE_UTF8
) && !IS_SET(MODE_SIXEL
)) {
2980 len
= utf8encode(u
, c
);
2981 if (!control
&& (width
= wcwidth(u
)) == -1) {
2982 memcpy(c
, "\357\277\275", 4); /* UTF_INVALID */
2987 if (IS_SET(MODE_PRINT
))
2991 * STR sequence must be checked before anything else
2992 * because it uses all following characters until it
2993 * receives a ESC, a SUB, a ST or any other C1 control
2996 if (term
.esc
& ESC_STR
) {
2997 if (u
== '\a' || u
== 030 || u
== 032 || u
== 033 ||
2999 term
.esc
&= ~(ESC_START
|ESC_STR
|ESC_DCS
);
3000 if (IS_SET(MODE_SIXEL
)) {
3001 /* TODO: render sixel */;
3002 term
.mode
&= ~MODE_SIXEL
;
3005 term
.esc
|= ESC_STR_END
;
3006 goto check_control_code
;
3010 if (IS_SET(MODE_SIXEL
)) {
3011 /* TODO: implement sixel mode */
3014 if (term
.esc
&ESC_DCS
&& strescseq
.len
== 0 && u
== 'q')
3015 term
.mode
|= MODE_SIXEL
;
3017 if (strescseq
.len
+len
>= sizeof(strescseq
.buf
)-1) {
3019 * Here is a bug in terminals. If the user never sends
3020 * some code to stop the str or esc command, then st
3021 * will stop responding. But this is better than
3022 * silently failing with unknown characters. At least
3023 * then users will report back.
3025 * In the case users ever get fixed, here is the code:
3034 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
3035 strescseq
.len
+= len
;
3041 * Actions of control codes must be performed as soon they arrive
3042 * because they can be embedded inside a control sequence, and
3043 * they must not cause conflicts with sequences.
3048 * control codes are not shown ever
3051 } else if (term
.esc
& ESC_START
) {
3052 if (term
.esc
& ESC_CSI
) {
3053 csiescseq
.buf
[csiescseq
.len
++] = u
;
3054 if (BETWEEN(u
, 0x40, 0x7E)
3055 || csiescseq
.len
>= \
3056 sizeof(csiescseq
.buf
)-1) {
3062 } else if (term
.esc
& ESC_UTF8
) {
3064 } else if (term
.esc
& ESC_ALTCHARSET
) {
3066 } else if (term
.esc
& ESC_TEST
) {
3071 /* sequence already finished */
3075 * All characters which form part of a sequence are not
3080 if (sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
3083 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
3084 if (IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
3085 gp
->mode
|= ATTR_WRAP
;
3087 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
3090 if (IS_SET(MODE_INSERT
) && term
.c
.x
+width
< term
.col
)
3091 memmove(gp
+width
, gp
, (term
.col
- term
.c
.x
- width
) * sizeof(Glyph
));
3093 if (term
.c
.x
+width
> term
.col
) {
3095 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
3098 tsetchar(u
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
3101 gp
->mode
|= ATTR_WIDE
;
3102 if (term
.c
.x
+1 < term
.col
) {
3104 gp
[1].mode
= ATTR_WDUMMY
;
3107 if (term
.c
.x
+width
< term
.col
) {
3108 tmoveto(term
.c
.x
+width
, term
.c
.y
);
3110 term
.c
.state
|= CURSOR_WRAPNEXT
;
3115 tresize(int col
, int row
)
3118 int minrow
= MIN(row
, term
.row
);
3119 int mincol
= MIN(col
, term
.col
);
3123 if (col
< 1 || row
< 1) {
3125 "tresize: error resizing to %dx%d\n", col
, row
);
3130 * slide screen to keep cursor where we expect it -
3131 * tscrollup would work here, but we can optimize to
3132 * memmove because we're freeing the earlier lines
3134 for (i
= 0; i
<= term
.c
.y
- row
; i
++) {
3138 /* ensure that both src and dst are not NULL */
3140 memmove(term
.line
, term
.line
+ i
, row
* sizeof(Line
));
3141 memmove(term
.alt
, term
.alt
+ i
, row
* sizeof(Line
));
3143 for (i
+= row
; i
< term
.row
; i
++) {
3148 /* resize to new width */
3149 term
.specbuf
= xrealloc(term
.specbuf
, col
* sizeof(XftGlyphFontSpec
));
3151 /* resize to new height */
3152 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
3153 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
3154 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
3155 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
3157 /* resize each row to new width, zero-pad if needed */
3158 for (i
= 0; i
< minrow
; i
++) {
3159 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
3160 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
3163 /* allocate any new rows */
3164 for (/* i == minrow */; i
< row
; i
++) {
3165 term
.line
[i
] = xmalloc(col
* sizeof(Glyph
));
3166 term
.alt
[i
] = xmalloc(col
* sizeof(Glyph
));
3168 if (col
> term
.col
) {
3169 bp
= term
.tabs
+ term
.col
;
3171 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
3172 while (--bp
> term
.tabs
&& !*bp
)
3174 for (bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
3177 /* update terminal size */
3180 /* reset scrolling region */
3181 tsetscroll(0, row
-1);
3182 /* make use of the LIMIT in tmoveto */
3183 tmoveto(term
.c
.x
, term
.c
.y
);
3184 /* Clearing both screens (it makes dirty all lines) */
3186 for (i
= 0; i
< 2; i
++) {
3187 if (mincol
< col
&& 0 < minrow
) {
3188 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
3190 if (0 < col
&& minrow
< row
) {
3191 tclearregion(0, minrow
, col
- 1, row
- 1);
3194 tcursor(CURSOR_LOAD
);
3200 xresize(int col
, int row
)
3202 xw
.tw
= MAX(1, col
* xw
.cw
);
3203 xw
.th
= MAX(1, row
* xw
.ch
);
3205 XFreePixmap(xw
.dpy
, xw
.buf
);
3206 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
3207 DefaultDepth(xw
.dpy
, xw
.scr
));
3208 XftDrawChange(xw
.draw
, xw
.buf
);
3209 xclear(0, 0, xw
.w
, xw
.h
);
3213 sixd_to_16bit(int x
)
3215 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
3219 xloadcolor(int i
, const char *name
, Color
*ncolor
)
3221 XRenderColor color
= { .alpha
= 0xffff };
3224 if (BETWEEN(i
, 16, 255)) { /* 256 color */
3225 if (i
< 6*6*6+16) { /* same colors as xterm */
3226 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
3227 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
3228 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
3229 } else { /* greyscale */
3230 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
3231 color
.green
= color
.blue
= color
.red
;
3233 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
3234 xw
.cmap
, &color
, ncolor
);
3236 name
= colorname
[i
];
3239 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
3250 for (cp
= dc
.col
; cp
< &dc
.col
[LEN(dc
.col
)]; ++cp
)
3251 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
3254 for (i
= 0; i
< LEN(dc
.col
); i
++)
3255 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
3257 die("Could not allocate color '%s'\n", colorname
[i
]);
3259 die("Could not allocate color %d\n", i
);
3265 xsetcolorname(int x
, const char *name
)
3269 if (!BETWEEN(x
, 0, LEN(dc
.col
)))
3273 if (!xloadcolor(x
, name
, &ncolor
))
3276 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
3283 * Absolute coordinates.
3286 xclear(int x1
, int y1
, int x2
, int y2
)
3288 XftDrawRect(xw
.draw
,
3289 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
3290 x1
, y1
, x2
-x1
, y2
-y1
);
3296 XClassHint
class = {opt_name
? opt_name
: termname
,
3297 opt_class
? opt_class
: termname
};
3298 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
3299 XSizeHints
*sizeh
= NULL
;
3301 sizeh
= XAllocSizeHints();
3303 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
3304 sizeh
->height
= xw
.h
;
3305 sizeh
->width
= xw
.w
;
3306 sizeh
->height_inc
= xw
.ch
;
3307 sizeh
->width_inc
= xw
.cw
;
3308 sizeh
->base_height
= 2 * borderpx
;
3309 sizeh
->base_width
= 2 * borderpx
;
3311 sizeh
->flags
|= PMaxSize
| PMinSize
;
3312 sizeh
->min_width
= sizeh
->max_width
= xw
.w
;
3313 sizeh
->min_height
= sizeh
->max_height
= xw
.h
;
3315 if (xw
.gm
& (XValue
|YValue
)) {
3316 sizeh
->flags
|= USPosition
| PWinGravity
;
3319 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
3322 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
3328 xgeommasktogravity(int mask
)
3330 switch (mask
& (XNegative
|YNegative
)) {
3332 return NorthWestGravity
;
3334 return NorthEastGravity
;
3336 return SouthWestGravity
;
3339 return SouthEastGravity
;
3343 xloadfont(Font
*f
, FcPattern
*pattern
)
3349 match
= XftFontMatch(xw
.dpy
, xw
.scr
, pattern
, &result
);
3353 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
3354 FcPatternDestroy(match
);
3358 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
3359 (const FcChar8
*) ascii_printable
,
3360 strlen(ascii_printable
), &extents
);
3363 f
->pattern
= FcPatternDuplicate(pattern
);
3365 f
->ascent
= f
->match
->ascent
;
3366 f
->descent
= f
->match
->descent
;
3368 f
->rbearing
= f
->match
->max_advance_width
;
3370 f
->height
= f
->ascent
+ f
->descent
;
3371 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
3377 xloadfonts(char *fontstr
, double fontsize
)
3383 if (fontstr
[0] == '-') {
3384 pattern
= XftXlfdParse(fontstr
, False
, False
);
3386 pattern
= FcNameParse((FcChar8
*)fontstr
);
3390 die("st: can't open font %s\n", fontstr
);
3393 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
3394 FcPatternDel(pattern
, FC_SIZE
);
3395 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
3396 usedfontsize
= fontsize
;
3398 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
3400 usedfontsize
= fontval
;
3401 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
3406 * Default font size is 12, if none given. This is to
3407 * have a known usedfontsize value.
3409 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
3412 defaultfontsize
= usedfontsize
;
3415 if (xloadfont(&dc
.font
, pattern
))
3416 die("st: can't open font %s\n", fontstr
);
3418 if (usedfontsize
< 0) {
3419 FcPatternGetDouble(dc
.font
.match
->pattern
,
3420 FC_PIXEL_SIZE
, 0, &fontval
);
3421 usedfontsize
= fontval
;
3423 defaultfontsize
= fontval
;
3426 /* Setting character width and height. */
3427 xw
.cw
= ceilf(dc
.font
.width
* cwscale
);
3428 xw
.ch
= ceilf(dc
.font
.height
* chscale
);
3430 FcPatternDel(pattern
, FC_SLANT
);
3431 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
3432 if (xloadfont(&dc
.ifont
, pattern
))
3433 die("st: can't open font %s\n", fontstr
);
3435 FcPatternDel(pattern
, FC_WEIGHT
);
3436 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
3437 if (xloadfont(&dc
.ibfont
, pattern
))
3438 die("st: can't open font %s\n", fontstr
);
3440 FcPatternDel(pattern
, FC_SLANT
);
3441 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
3442 if (xloadfont(&dc
.bfont
, pattern
))
3443 die("st: can't open font %s\n", fontstr
);
3445 FcPatternDestroy(pattern
);
3449 xunloadfont(Font
*f
)
3451 XftFontClose(xw
.dpy
, f
->match
);
3452 FcPatternDestroy(f
->pattern
);
3454 FcFontSetDestroy(f
->set
);
3460 /* Free the loaded fonts in the font cache. */
3462 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
3464 xunloadfont(&dc
.font
);
3465 xunloadfont(&dc
.bfont
);
3466 xunloadfont(&dc
.ifont
);
3467 xunloadfont(&dc
.ibfont
);
3471 xzoom(const Arg
*arg
)
3475 larg
.f
= usedfontsize
+ arg
->f
;
3480 xzoomabs(const Arg
*arg
)
3483 xloadfonts(usedfont
, arg
->f
);
3491 xzoomreset(const Arg
*arg
)
3495 if (defaultfontsize
> 0) {
3496 larg
.f
= defaultfontsize
;
3507 pid_t thispid
= getpid();
3508 XColor xmousefg
, xmousebg
;
3510 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
3511 die("Can't open display\n");
3512 xw
.scr
= XDefaultScreen(xw
.dpy
);
3513 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
3517 die("Could not init fontconfig.\n");
3519 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
3520 xloadfonts(usedfont
, 0);
3523 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
3526 /* adjust fixed window geometry */
3527 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
3528 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
3529 if (xw
.gm
& XNegative
)
3530 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - xw
.w
- 2;
3531 if (xw
.gm
& YNegative
)
3532 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - xw
.h
- 2;
3535 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
3536 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
3537 xw
.attrs
.bit_gravity
= NorthWestGravity
;
3538 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
3539 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
3540 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
3541 xw
.attrs
.colormap
= xw
.cmap
;
3543 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
3544 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
3545 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
3546 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
3547 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
3548 | CWEventMask
| CWColormap
, &xw
.attrs
);
3550 memset(&gcvalues
, 0, sizeof(gcvalues
));
3551 gcvalues
.graphics_exposures
= False
;
3552 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
3554 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
3555 DefaultDepth(xw
.dpy
, xw
.scr
));
3556 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
3557 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
3559 /* Xft rendering context */
3560 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
3563 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3564 XSetLocaleModifiers("@im=local");
3565 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3566 XSetLocaleModifiers("@im=");
3567 if ((xw
.xim
= XOpenIM(xw
.dpy
,
3568 NULL
, NULL
, NULL
)) == NULL
) {
3569 die("XOpenIM failed. Could not open input"
3574 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
3575 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
3576 XNFocusWindow
, xw
.win
, NULL
);
3578 die("XCreateIC failed. Could not obtain input method.\n");
3580 /* white cursor, black outline */
3581 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
3582 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
3584 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
3585 xmousefg
.red
= 0xffff;
3586 xmousefg
.green
= 0xffff;
3587 xmousefg
.blue
= 0xffff;
3590 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
3591 xmousebg
.red
= 0x0000;
3592 xmousebg
.green
= 0x0000;
3593 xmousebg
.blue
= 0x0000;
3596 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
3598 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
3599 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
3600 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
3601 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
3603 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
3604 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
3605 PropModeReplace
, (uchar
*)&thispid
, 1);
3608 XMapWindow(xw
.dpy
, xw
.win
);
3610 XSync(xw
.dpy
, False
);
3614 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
3616 float winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
, xp
, yp
;
3617 ushort mode
, prevmode
= USHRT_MAX
;
3618 Font
*font
= &dc
.font
;
3619 int frcflags
= FRC_NORMAL
;
3620 float runewidth
= xw
.cw
;
3624 FcPattern
*fcpattern
, *fontpattern
;
3625 FcFontSet
*fcsets
[] = { NULL
};
3626 FcCharSet
*fccharset
;
3627 int i
, f
, numspecs
= 0;
3629 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
3630 /* Fetch rune and mode for current glyph. */
3632 mode
= glyphs
[i
].mode
;
3634 /* Skip dummy wide-character spacing. */
3635 if (mode
== ATTR_WDUMMY
)
3638 /* Determine font for glyph if different from previous glyph. */
3639 if (prevmode
!= mode
) {
3642 frcflags
= FRC_NORMAL
;
3643 runewidth
= xw
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
3644 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
3646 frcflags
= FRC_ITALICBOLD
;
3647 } else if (mode
& ATTR_ITALIC
) {
3649 frcflags
= FRC_ITALIC
;
3650 } else if (mode
& ATTR_BOLD
) {
3652 frcflags
= FRC_BOLD
;
3654 yp
= winy
+ font
->ascent
;
3657 /* Lookup character index with default font. */
3658 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
3660 specs
[numspecs
].font
= font
->match
;
3661 specs
[numspecs
].glyph
= glyphidx
;
3662 specs
[numspecs
].x
= (short)xp
;
3663 specs
[numspecs
].y
= (short)yp
;
3669 /* Fallback on font cache, search the font cache for match. */
3670 for (f
= 0; f
< frclen
; f
++) {
3671 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
3672 /* Everything correct. */
3673 if (glyphidx
&& frc
[f
].flags
== frcflags
)
3675 /* We got a default font for a not found glyph. */
3676 if (!glyphidx
&& frc
[f
].flags
== frcflags
3677 && frc
[f
].unicodep
== rune
) {
3682 /* Nothing was found. Use fontconfig to find matching font. */
3685 font
->set
= FcFontSort(0, font
->pattern
,
3687 fcsets
[0] = font
->set
;
3690 * Nothing was found in the cache. Now use
3691 * some dozen of Fontconfig calls to get the
3692 * font for one single character.
3694 * Xft and fontconfig are design failures.
3696 fcpattern
= FcPatternDuplicate(font
->pattern
);
3697 fccharset
= FcCharSetCreate();
3699 FcCharSetAddChar(fccharset
, rune
);
3700 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3702 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
3704 FcConfigSubstitute(0, fcpattern
,
3706 FcDefaultSubstitute(fcpattern
);
3708 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
3712 * Overwrite or create the new cache entry.
3714 if (frclen
>= LEN(frc
)) {
3715 frclen
= LEN(frc
) - 1;
3716 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
3717 frc
[frclen
].unicodep
= 0;
3720 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
3722 frc
[frclen
].flags
= frcflags
;
3723 frc
[frclen
].unicodep
= rune
;
3725 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
3730 FcPatternDestroy(fcpattern
);
3731 FcCharSetDestroy(fccharset
);
3734 specs
[numspecs
].font
= frc
[f
].font
;
3735 specs
[numspecs
].glyph
= glyphidx
;
3736 specs
[numspecs
].x
= (short)xp
;
3737 specs
[numspecs
].y
= (short)yp
;
3746 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
3748 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
3749 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
3750 width
= charlen
* xw
.cw
;
3751 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
3752 XRenderColor colfg
, colbg
;
3755 /* Determine foreground and background colors based on mode. */
3756 if (base
.fg
== defaultfg
) {
3757 if (base
.mode
& ATTR_ITALIC
)
3758 base
.fg
= defaultitalic
;
3759 else if ((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
))
3760 base
.fg
= defaultitalic
;
3761 else if (base
.mode
& ATTR_UNDERLINE
)
3762 base
.fg
= defaultunderline
;
3765 if (IS_TRUECOL(base
.fg
)) {
3766 colfg
.alpha
= 0xffff;
3767 colfg
.red
= TRUERED(base
.fg
);
3768 colfg
.green
= TRUEGREEN(base
.fg
);
3769 colfg
.blue
= TRUEBLUE(base
.fg
);
3770 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
3773 fg
= &dc
.col
[base
.fg
];
3776 if (IS_TRUECOL(base
.bg
)) {
3777 colbg
.alpha
= 0xffff;
3778 colbg
.green
= TRUEGREEN(base
.bg
);
3779 colbg
.red
= TRUERED(base
.bg
);
3780 colbg
.blue
= TRUEBLUE(base
.bg
);
3781 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
3784 bg
= &dc
.col
[base
.bg
];
3787 /* Change basic system colors [0-7] to bright system colors [8-15] */
3788 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
3789 fg
= &dc
.col
[base
.fg
+ 8];
3791 if (IS_SET(MODE_REVERSE
)) {
3792 if (fg
== &dc
.col
[defaultfg
]) {
3793 fg
= &dc
.col
[defaultbg
];
3795 colfg
.red
= ~fg
->color
.red
;
3796 colfg
.green
= ~fg
->color
.green
;
3797 colfg
.blue
= ~fg
->color
.blue
;
3798 colfg
.alpha
= fg
->color
.alpha
;
3799 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
3804 if (bg
== &dc
.col
[defaultbg
]) {
3805 bg
= &dc
.col
[defaultfg
];
3807 colbg
.red
= ~bg
->color
.red
;
3808 colbg
.green
= ~bg
->color
.green
;
3809 colbg
.blue
= ~bg
->color
.blue
;
3810 colbg
.alpha
= bg
->color
.alpha
;
3811 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
3817 if (base
.mode
& ATTR_REVERSE
) {
3823 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
3824 colfg
.red
= fg
->color
.red
/ 2;
3825 colfg
.green
= fg
->color
.green
/ 2;
3826 colfg
.blue
= fg
->color
.blue
/ 2;
3827 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
3831 if (base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
3834 if (base
.mode
& ATTR_INVISIBLE
)
3837 /* Intelligent cleaning up of the borders. */
3839 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
3840 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
3842 if (x
+ charlen
>= term
.col
) {
3843 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
3844 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
3847 xclear(winx
, 0, winx
+ width
, borderpx
);
3848 if (y
== term
.row
-1)
3849 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
3851 /* Clean up the region we want to draw to. */
3852 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
3854 /* Set the clip region because Xft is sometimes dirty. */
3859 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3861 /* Render the glyphs. */
3862 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
3864 /* Render underline and strikethrough. */
3865 if (base
.mode
& ATTR_UNDERLINE
) {
3866 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
3870 if (base
.mode
& ATTR_STRUCK
) {
3871 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
3875 /* Reset clip to none. */
3876 XftDrawSetClip(xw
.draw
, 0);
3880 xdrawglyph(Glyph g
, int x
, int y
)
3883 XftGlyphFontSpec spec
;
3885 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
3886 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
3892 static int oldx
= 0, oldy
= 0;
3894 Glyph g
= {' ', ATTR_NULL
, defaultbg
, defaultcs
}, og
;
3895 int ena_sel
= sel
.ob
.x
!= -1 && sel
.alt
== IS_SET(MODE_ALTSCREEN
);
3898 LIMIT(oldx
, 0, term
.col
-1);
3899 LIMIT(oldy
, 0, term
.row
-1);
3903 /* adjust position if in dummy */
3904 if (term
.line
[oldy
][oldx
].mode
& ATTR_WDUMMY
)
3906 if (term
.line
[term
.c
.y
][curx
].mode
& ATTR_WDUMMY
)
3909 /* remove the old cursor */
3910 og
= term
.line
[oldy
][oldx
];
3911 if (ena_sel
&& selected(oldx
, oldy
))
3912 og
.mode
^= ATTR_REVERSE
;
3913 xdrawglyph(og
, oldx
, oldy
);
3915 g
.u
= term
.line
[term
.c
.y
][term
.c
.x
].u
;
3918 * Select the right color for the right mode.
3920 if (IS_SET(MODE_REVERSE
)) {
3921 g
.mode
|= ATTR_REVERSE
;
3923 if (ena_sel
&& selected(term
.c
.x
, term
.c
.y
)) {
3924 drawcol
= dc
.col
[defaultcs
];
3927 drawcol
= dc
.col
[defaultrcs
];
3931 if (ena_sel
&& selected(term
.c
.x
, term
.c
.y
)) {
3932 drawcol
= dc
.col
[defaultrcs
];
3936 drawcol
= dc
.col
[defaultcs
];
3940 if (IS_SET(MODE_HIDE
))
3943 /* draw the new one */
3944 if (xw
.state
& WIN_FOCUSED
) {
3945 switch (xw
.cursor
) {
3946 case 7: /* st extension: snowman */
3947 utf8decode("☃", &g
.u
, UTF_SIZ
);
3948 case 0: /* Blinking Block */
3949 case 1: /* Blinking Block (Default) */
3950 case 2: /* Steady Block */
3951 g
.mode
|= term
.line
[term
.c
.y
][curx
].mode
& ATTR_WIDE
;
3952 xdrawglyph(g
, term
.c
.x
, term
.c
.y
);
3954 case 3: /* Blinking Underline */
3955 case 4: /* Steady Underline */
3956 XftDrawRect(xw
.draw
, &drawcol
,
3957 borderpx
+ curx
* xw
.cw
,
3958 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- \
3960 xw
.cw
, cursorthickness
);
3962 case 5: /* Blinking bar */
3963 case 6: /* Steady bar */
3964 XftDrawRect(xw
.draw
, &drawcol
,
3965 borderpx
+ curx
* xw
.cw
,
3966 borderpx
+ term
.c
.y
* xw
.ch
,
3967 cursorthickness
, xw
.ch
);
3971 XftDrawRect(xw
.draw
, &drawcol
,
3972 borderpx
+ curx
* xw
.cw
,
3973 borderpx
+ term
.c
.y
* xw
.ch
,
3975 XftDrawRect(xw
.draw
, &drawcol
,
3976 borderpx
+ curx
* xw
.cw
,
3977 borderpx
+ term
.c
.y
* xw
.ch
,
3979 XftDrawRect(xw
.draw
, &drawcol
,
3980 borderpx
+ (curx
+ 1) * xw
.cw
- 1,
3981 borderpx
+ term
.c
.y
* xw
.ch
,
3983 XftDrawRect(xw
.draw
, &drawcol
,
3984 borderpx
+ curx
* xw
.cw
,
3985 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3988 oldx
= curx
, oldy
= term
.c
.y
;
3997 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3999 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
4000 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
4007 xsettitle(opt_title
? opt_title
: "st");
4020 drawregion(0, 0, term
.col
, term
.row
);
4021 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
4023 XSetForeground(xw
.dpy
, dc
.gc
,
4024 dc
.col
[IS_SET(MODE_REVERSE
)?
4025 defaultfg
: defaultbg
].pixel
);
4029 drawregion(int x1
, int y1
, int x2
, int y2
)
4031 int i
, x
, y
, ox
, numspecs
;
4033 XftGlyphFontSpec
*specs
;
4034 int ena_sel
= sel
.ob
.x
!= -1 && sel
.alt
== IS_SET(MODE_ALTSCREEN
);
4036 if (!(xw
.state
& WIN_VISIBLE
))
4039 for (y
= y1
; y
< y2
; y
++) {
4045 specs
= term
.specbuf
;
4046 numspecs
= xmakeglyphfontspecs(specs
, &term
.line
[y
][x1
], x2
- x1
, x1
, y
);
4049 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
4050 new = term
.line
[y
][x
];
4051 if (new.mode
== ATTR_WDUMMY
)
4053 if (ena_sel
&& selected(x
, y
))
4054 new.mode
^= ATTR_REVERSE
;
4055 if (i
> 0 && ATTRCMP(base
, new)) {
4056 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
4068 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
4080 visibility(XEvent
*ev
)
4082 XVisibilityEvent
*e
= &ev
->xvisibility
;
4084 MODBIT(xw
.state
, e
->state
!= VisibilityFullyObscured
, WIN_VISIBLE
);
4090 xw
.state
&= ~WIN_VISIBLE
;
4094 xsetpointermotion(int set
)
4096 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
4097 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
4101 xseturgency(int add
)
4103 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
4105 MODBIT(h
->flags
, add
, XUrgencyHint
);
4106 XSetWMHints(xw
.dpy
, xw
.win
, h
);
4113 XFocusChangeEvent
*e
= &ev
->xfocus
;
4115 if (e
->mode
== NotifyGrab
)
4118 if (ev
->type
== FocusIn
) {
4119 XSetICFocus(xw
.xic
);
4120 xw
.state
|= WIN_FOCUSED
;
4122 if (IS_SET(MODE_FOCUS
))
4123 ttywrite("\033[I", 3);
4125 XUnsetICFocus(xw
.xic
);
4126 xw
.state
&= ~WIN_FOCUSED
;
4127 if (IS_SET(MODE_FOCUS
))
4128 ttywrite("\033[O", 3);
4133 match(uint mask
, uint state
)
4135 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
4139 numlock(const Arg
*dummy
)
4145 kmap(KeySym k
, uint state
)
4150 /* Check for mapped keys out of X11 function keys. */
4151 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
4152 if (mappedkeys
[i
] == k
)
4155 if (i
== LEN(mappedkeys
)) {
4156 if ((k
& 0xFFFF) < 0xFD00)
4160 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
4164 if (!match(kp
->mask
, state
))
4167 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
4169 if (term
.numlock
&& kp
->appkey
== 2)
4172 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
4175 if (IS_SET(MODE_CRLF
) ? kp
->crlf
< 0 : kp
->crlf
> 0)
4187 XKeyEvent
*e
= &ev
->xkey
;
4189 char buf
[32], *customkey
;
4195 if (IS_SET(MODE_KBDLOCK
))
4198 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
4200 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
4201 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
4202 bp
->func(&(bp
->arg
));
4207 /* 2. custom keys from config.h */
4208 if ((customkey
= kmap(ksym
, e
->state
))) {
4209 ttysend(customkey
, strlen(customkey
));
4213 /* 3. composed string from input method */
4216 if (len
== 1 && e
->state
& Mod1Mask
) {
4217 if (IS_SET(MODE_8BIT
)) {
4220 len
= utf8encode(c
, buf
);
4237 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
4239 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
4240 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
4241 xw
.state
|= WIN_FOCUSED
;
4243 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
4244 xw
.state
&= ~WIN_FOCUSED
;
4246 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
4247 /* Send SIGHUP to shell */
4254 cresize(int width
, int height
)
4263 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
4264 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
4273 if (e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
4276 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
4284 int w
= xw
.w
, h
= xw
.h
;
4286 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
4287 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
4290 /* Waiting for window mapping */
4292 XNextEvent(xw
.dpy
, &ev
);
4294 * This XFilterEvent call is required because of XOpenIM. It
4295 * does filter out the key event and some client message for
4296 * the input method too.
4298 if (XFilterEvent(&ev
, None
))
4300 if (ev
.type
== ConfigureNotify
) {
4301 w
= ev
.xconfigure
.width
;
4302 h
= ev
.xconfigure
.height
;
4304 } while (ev
.type
!= MapNotify
);
4310 clock_gettime(CLOCK_MONOTONIC
, &last
);
4313 for (xev
= actionfps
;;) {
4315 FD_SET(cmdfd
, &rfd
);
4318 if (pselect(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
4321 die("select failed: %s\n", strerror(errno
));
4323 if (FD_ISSET(cmdfd
, &rfd
)) {
4326 blinkset
= tattrset(ATTR_BLINK
);
4328 MODBIT(term
.mode
, 0, MODE_BLINK
);
4332 if (FD_ISSET(xfd
, &rfd
))
4335 clock_gettime(CLOCK_MONOTONIC
, &now
);
4336 drawtimeout
.tv_sec
= 0;
4337 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
4341 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
4342 tsetdirtattr(ATTR_BLINK
);
4343 term
.mode
^= MODE_BLINK
;
4347 deltatime
= TIMEDIFF(now
, last
);
4348 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
4354 while (XPending(xw
.dpy
)) {
4355 XNextEvent(xw
.dpy
, &ev
);
4356 if (XFilterEvent(&ev
, None
))
4358 if (handler
[ev
.type
])
4359 (handler
[ev
.type
])(&ev
);
4365 if (xev
&& !FD_ISSET(xfd
, &rfd
))
4367 if (!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
4369 if (TIMEDIFF(now
, lastblink
) \
4371 drawtimeout
.tv_nsec
= 1000;
4373 drawtimeout
.tv_nsec
= (1E6
* \
4378 drawtimeout
.tv_sec
= \
4379 drawtimeout
.tv_nsec
/ 1E9
;
4380 drawtimeout
.tv_nsec
%= (long)1E9
;
4392 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
4393 " [-n name] [-o file]\n"
4394 " [-T title] [-t title] [-w windowid]"
4395 " [[-e] command [args ...]]\n"
4396 " %s [-aiv] [-c class] [-f font] [-g geometry]"
4397 " [-n name] [-o file]\n"
4398 " [-T title] [-t title] [-w windowid] -l line"
4399 " [stty_args ...]\n", argv0
, argv0
);
4403 main(int argc
, char *argv
[])
4405 uint cols
= 80, rows
= 24;
4409 xw
.cursor
= cursorshape
;
4416 opt_class
= EARGF(usage());
4423 opt_font
= EARGF(usage());
4426 xw
.gm
= XParseGeometry(EARGF(usage()),
4427 &xw
.l
, &xw
.t
, &cols
, &rows
);
4433 opt_io
= EARGF(usage());
4436 opt_line
= EARGF(usage());
4439 opt_name
= EARGF(usage());
4443 opt_title
= EARGF(usage());
4446 opt_embed
= EARGF(usage());
4449 die("%s " VERSION
" (c) 2010-2016 st engineers\n", argv0
);
4457 /* eat all remaining arguments */
4459 if (!opt_title
&& !opt_line
)
4460 opt_title
= basename(xstrdup(argv
[0]));
4462 setlocale(LC_CTYPE
, "");
4463 XSetLocaleModifiers("");
4464 tnew(MAX(cols
, 1), MAX(rows
, 1));