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 ISCONTROLC0(c) (BETWEEN(c, 0, 0x1f) || (c) == '\177')
72 #define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f))
73 #define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c))
74 #define ISDELIM(u) (utf8strchr(worddelimiters, u) != NULL)
75 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
76 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || \
78 #define IS_SET(flag) ((term.mode & (flag)) != 0)
79 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + \
80 (t1.tv_nsec-t2.tv_nsec)/1E6)
81 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
83 #define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
84 #define IS_TRUECOL(x) (1 << 24 & (x))
85 #define TRUERED(x) (((x) & 0xff0000) >> 8)
86 #define TRUEGREEN(x) (((x) & 0xff00))
87 #define TRUEBLUE(x) (((x) & 0xff) << 8)
90 enum glyph_attribute
{
95 ATTR_UNDERLINE
= 1 << 3,
97 ATTR_REVERSE
= 1 << 5,
98 ATTR_INVISIBLE
= 1 << 6,
102 ATTR_WDUMMY
= 1 << 10,
103 ATTR_BOLD_FAINT
= ATTR_BOLD
| ATTR_FAINT
,
106 enum cursor_movement
{
119 MODE_INSERT
= 1 << 1,
120 MODE_APPKEYPAD
= 1 << 2,
121 MODE_ALTSCREEN
= 1 << 3,
123 MODE_MOUSEBTN
= 1 << 5,
124 MODE_MOUSEMOTION
= 1 << 6,
125 MODE_REVERSE
= 1 << 7,
126 MODE_KBDLOCK
= 1 << 8,
129 MODE_APPCURSOR
= 1 << 11,
130 MODE_MOUSESGR
= 1 << 12,
132 MODE_BLINK
= 1 << 14,
133 MODE_FBLINK
= 1 << 15,
134 MODE_FOCUS
= 1 << 16,
135 MODE_MOUSEX10
= 1 << 17,
136 MODE_MOUSEMANY
= 1 << 18,
137 MODE_BRCKTPASTE
= 1 << 19,
138 MODE_PRINT
= 1 << 20,
139 MODE_MOUSE
= MODE_MOUSEBTN
|MODE_MOUSEMOTION
|MODE_MOUSEX10\
156 ESC_STR
= 4, /* DCS, OSC, PM, APC */
158 ESC_STR_END
= 16, /* a final string was encountered */
159 ESC_TEST
= 32, /* Enter in test mode */
167 enum selection_mode
{
173 enum selection_type
{
178 enum selection_snap
{
183 typedef unsigned char uchar
;
184 typedef unsigned int uint
;
185 typedef unsigned long ulong
;
186 typedef unsigned short ushort
;
188 typedef uint_least32_t Rune
;
190 typedef XftDraw
*Draw
;
191 typedef XftColor Color
;
194 Rune u
; /* character code */
195 ushort mode
; /* attribute flags */
196 uint32_t fg
; /* foreground */
197 uint32_t bg
; /* background */
203 Glyph attr
; /* current char attributes */
209 /* CSI Escape sequence structs */
210 /* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
212 char buf
[ESC_BUF_SIZ
]; /* raw string */
213 int len
; /* raw string length */
215 int arg
[ESC_ARG_SIZ
];
216 int narg
; /* nb of args */
220 /* STR Escape sequence structs */
221 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
223 char type
; /* ESC type ... */
224 char buf
[STR_BUF_SIZ
]; /* raw string */
225 int len
; /* raw string length */
226 char *args
[STR_ARG_SIZ
];
227 int narg
; /* nb of args */
230 /* Internal representation of the screen */
232 int row
; /* nb row */
233 int col
; /* nb col */
234 Line
*line
; /* screen */
235 Line
*alt
; /* alternate screen */
236 int *dirty
; /* dirtyness of lines */
237 XftGlyphFontSpec
*specbuf
; /* font spec buffer used for rendering */
238 TCursor c
; /* cursor */
239 int top
; /* top scroll limit */
240 int bot
; /* bottom scroll limit */
241 int mode
; /* terminal mode flags */
242 int esc
; /* escape state flags */
243 char trantbl
[4]; /* charset table translation */
244 int charset
; /* current charset */
245 int icharset
; /* selected charset for sequence */
246 int numlock
; /* lock numbers in keyboard */
250 /* Purely graphic info */
256 Atom xembed
, wmdeletewin
, netwmname
, netwmpid
;
261 XSetWindowAttributes attrs
;
263 int isfixed
; /* is fixed geometry? */
264 int l
, t
; /* left and top offset */
265 int gm
; /* geometry mask */
266 int tw
, th
; /* tty width and height */
267 int w
, h
; /* window width and height */
268 int ch
; /* char height */
269 int cw
; /* char width */
270 char state
; /* focus, redraw, visible */
271 int cursor
; /* cursor style */
284 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
285 signed char appkey
; /* application keypad */
286 signed char appcursor
; /* application cursor */
287 signed char crlf
; /* crlf mode */
295 * Selection variables:
296 * nb – normalized coordinates of the beginning of the selection
297 * ne – normalized coordinates of the end of the selection
298 * ob – original coordinates of the beginning of the selection
299 * oe – original coordinates of the end of the selection
305 char *primary
, *clipboard
;
308 struct timespec tclick1
;
309 struct timespec tclick2
;
322 void (*func
)(const Arg
*);
326 /* function definitions used in config.h */
327 static void clipcopy(const Arg
*);
328 static void clippaste(const Arg
*);
329 static void numlock(const Arg
*);
330 static void selpaste(const Arg
*);
331 static void xzoom(const Arg
*);
332 static void xzoomabs(const Arg
*);
333 static void xzoomreset(const Arg
*);
334 static void printsel(const Arg
*);
335 static void printscreen(const Arg
*) ;
336 static void toggleprinter(const Arg
*);
337 static void sendbreak(const Arg
*);
339 /* Config.h for applying patches and the configuration. */
355 /* Drawing Context */
357 Color col
[MAX(LEN(colorname
), 256)];
358 Font font
, bfont
, ifont
, ibfont
;
362 static void die(const char *, ...);
363 static void draw(void);
364 static void redraw(void);
365 static void drawregion(int, int, int, int);
366 static void execsh(void);
367 static void stty(void);
368 static void sigchld(int);
369 static void run(void);
371 static void csidump(void);
372 static void csihandle(void);
373 static void csiparse(void);
374 static void csireset(void);
375 static int eschandle(uchar
);
376 static void strdump(void);
377 static void strhandle(void);
378 static void strparse(void);
379 static void strreset(void);
381 static int tattrset(int);
382 static void tprinter(char *, size_t);
383 static void tdumpsel(void);
384 static void tdumpline(int);
385 static void tdump(void);
386 static void tclearregion(int, int, int, int);
387 static void tcursor(int);
388 static void tdeletechar(int);
389 static void tdeleteline(int);
390 static void tinsertblank(int);
391 static void tinsertblankline(int);
392 static int tlinelen(int);
393 static void tmoveto(int, int);
394 static void tmoveato(int, int);
395 static void tnew(int, int);
396 static void tnewline(int);
397 static void tputtab(int);
398 static void tputc(Rune
);
399 static void treset(void);
400 static void tresize(int, int);
401 static void tscrollup(int, int);
402 static void tscrolldown(int, int);
403 static void tsetattr(int *, int);
404 static void tsetchar(Rune
, Glyph
*, int, int);
405 static void tsetscroll(int, int);
406 static void tswapscreen(void);
407 static void tsetdirt(int, int);
408 static void tsetdirtattr(int);
409 static void tsetmode(int, int, int *, int);
410 static void tfulldirt(void);
411 static void techo(Rune
);
412 static void tcontrolcode(uchar
);
413 static void tdectest(char );
414 static int32_t tdefcolor(int *, int *, int);
415 static void tdeftran(char);
416 static inline int match(uint
, uint
);
417 static void ttynew(void);
418 static size_t ttyread(void);
419 static void ttyresize(void);
420 static void ttysend(char *, size_t);
421 static void ttywrite(const char *, size_t);
422 static void tstrsequence(uchar
);
424 static inline ushort
sixd_to_16bit(int);
425 static int xmakeglyphfontspecs(XftGlyphFontSpec
*, const Glyph
*, int, int, int);
426 static void xdrawglyphfontspecs(const XftGlyphFontSpec
*, Glyph
, int, int, int);
427 static void xdrawglyph(Glyph
, int, int);
428 static void xhints(void);
429 static void xclear(int, int, int, int);
430 static void xdrawcursor(void);
431 static void xinit(void);
432 static void xloadcols(void);
433 static int xsetcolorname(int, const char *);
434 static int xgeommasktogravity(int);
435 static int xloadfont(Font
*, FcPattern
*);
436 static void xloadfonts(char *, double);
437 static void xsettitle(char *);
438 static void xresettitle(void);
439 static void xsetpointermotion(int);
440 static void xseturgency(int);
441 static void xsetsel(char *, Time
);
442 static void xtermclear(int, int, int, int);
443 static void xunloadfont(Font
*);
444 static void xunloadfonts(void);
445 static void xresize(int, int);
447 static void expose(XEvent
*);
448 static void visibility(XEvent
*);
449 static void unmap(XEvent
*);
450 static char *kmap(KeySym
, uint
);
451 static void kpress(XEvent
*);
452 static void cmessage(XEvent
*);
453 static void cresize(int, int);
454 static void resize(XEvent
*);
455 static void focus(XEvent
*);
456 static void brelease(XEvent
*);
457 static void bpress(XEvent
*);
458 static void bmotion(XEvent
*);
459 static void propnotify(XEvent
*);
460 static void selnotify(XEvent
*);
461 static void selclear(XEvent
*);
462 static void selrequest(XEvent
*);
464 static void selinit(void);
465 static void selnormalize(void);
466 static inline int selected(int, int);
467 static char *getsel(void);
468 static void selcopy(Time
);
469 static void selscroll(int, int);
470 static void selsnap(int *, int *, int);
471 static int x2col(int);
472 static int y2row(int);
473 static void getbuttoninfo(XEvent
*);
474 static void mousereport(XEvent
*);
476 static size_t utf8decode(char *, Rune
*, size_t);
477 static Rune
utf8decodebyte(char, size_t *);
478 static size_t utf8encode(Rune
, char *);
479 static char utf8encodebyte(Rune
, size_t);
480 static char *utf8strchr(char *s
, Rune u
);
481 static size_t utf8validate(Rune
*, size_t);
483 static ssize_t
xwrite(int, const char *, size_t);
484 static void *xmalloc(size_t);
485 static void *xrealloc(void *, size_t);
486 static char *xstrdup(char *);
488 static void usage(void);
490 static void (*handler
[LASTEvent
])(XEvent
*) = {
492 [ClientMessage
] = cmessage
,
493 [ConfigureNotify
] = resize
,
494 [VisibilityNotify
] = visibility
,
495 [UnmapNotify
] = unmap
,
499 [MotionNotify
] = bmotion
,
500 [ButtonPress
] = bpress
,
501 [ButtonRelease
] = brelease
,
503 * Uncomment if you want the selection to disappear when you select something
504 * different in another window.
506 /* [SelectionClear] = selclear, */
507 [SelectionNotify
] = selnotify
,
509 * PropertyNotify is only turned on when there is some INCR transfer happening
510 * for the selection retrieval.
512 [PropertyNotify
] = propnotify
,
513 [SelectionRequest
] = selrequest
,
520 static CSIEscape csiescseq
;
521 static STREscape strescseq
;
524 static Selection sel
;
526 static char **opt_cmd
= NULL
;
527 static char *opt_io
= NULL
;
528 static char *opt_title
= NULL
;
529 static char *opt_embed
= NULL
;
530 static char *opt_class
= NULL
;
531 static char *opt_font
= NULL
;
532 static char *opt_line
= NULL
;
533 static int oldbutton
= 3; /* button event on startup: 3 = release */
535 static char *usedfont
= NULL
;
536 static double usedfontsize
= 0;
537 static double defaultfontsize
= 0;
539 static uchar utfbyte
[UTF_SIZ
+ 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
540 static uchar utfmask
[UTF_SIZ
+ 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
541 static Rune utfmin
[UTF_SIZ
+ 1] = { 0, 0, 0x80, 0x800, 0x10000};
542 static Rune utfmax
[UTF_SIZ
+ 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
544 /* Font Ring Cache */
558 /* Fontcache is an array now. A new font will be appended to the array. */
559 static Fontcache frc
[16];
560 static int frclen
= 0;
563 xwrite(int fd
, const char *s
, size_t len
)
569 r
= write(fd
, s
, len
);
582 void *p
= malloc(len
);
585 die("Out of memory\n");
591 xrealloc(void *p
, size_t len
)
593 if ((p
= realloc(p
, len
)) == NULL
)
594 die("Out of memory\n");
602 if ((s
= strdup(s
)) == NULL
)
603 die("Out of memory\n");
609 utf8decode(char *c
, Rune
*u
, size_t clen
)
611 size_t i
, j
, len
, type
;
617 udecoded
= utf8decodebyte(c
[0], &len
);
618 if (!BETWEEN(len
, 1, UTF_SIZ
))
620 for (i
= 1, j
= 1; i
< clen
&& j
< len
; ++i
, ++j
) {
621 udecoded
= (udecoded
<< 6) | utf8decodebyte(c
[i
], &type
);
628 utf8validate(u
, len
);
634 utf8decodebyte(char c
, size_t *i
)
636 for (*i
= 0; *i
< LEN(utfmask
); ++(*i
))
637 if (((uchar
)c
& utfmask
[*i
]) == utfbyte
[*i
])
638 return (uchar
)c
& ~utfmask
[*i
];
644 utf8encode(Rune u
, char *c
)
648 len
= utf8validate(&u
, 0);
652 for (i
= len
- 1; i
!= 0; --i
) {
653 c
[i
] = utf8encodebyte(u
, 0);
656 c
[0] = utf8encodebyte(u
, len
);
662 utf8encodebyte(Rune u
, size_t i
)
664 return utfbyte
[i
] | (u
& ~utfmask
[i
]);
668 utf8strchr(char *s
, Rune u
)
674 for (i
= 0, j
= 0; i
< len
; i
+= j
) {
675 if (!(j
= utf8decode(&s
[i
], &r
, len
- i
)))
685 utf8validate(Rune
*u
, size_t i
)
687 if (!BETWEEN(*u
, utfmin
[i
], utfmax
[i
]) || BETWEEN(*u
, 0xD800, 0xDFFF))
689 for (i
= 1; *u
> utfmax
[i
]; ++i
)
698 clock_gettime(CLOCK_MONOTONIC
, &sel
.tclick1
);
699 clock_gettime(CLOCK_MONOTONIC
, &sel
.tclick2
);
704 sel
.clipboard
= NULL
;
705 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
706 if (sel
.xtarget
== None
)
707 sel
.xtarget
= XA_STRING
;
716 return LIMIT(x
, 0, term
.col
-1);
725 return LIMIT(y
, 0, term
.row
-1);
733 if (term
.line
[y
][i
- 1].mode
& ATTR_WRAP
)
736 while (i
> 0 && term
.line
[y
][i
- 1].u
== ' ')
747 if (sel
.type
== SEL_REGULAR
&& sel
.ob
.y
!= sel
.oe
.y
) {
748 sel
.nb
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.ob
.x
: sel
.oe
.x
;
749 sel
.ne
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.oe
.x
: sel
.ob
.x
;
751 sel
.nb
.x
= MIN(sel
.ob
.x
, sel
.oe
.x
);
752 sel
.ne
.x
= MAX(sel
.ob
.x
, sel
.oe
.x
);
754 sel
.nb
.y
= MIN(sel
.ob
.y
, sel
.oe
.y
);
755 sel
.ne
.y
= MAX(sel
.ob
.y
, sel
.oe
.y
);
757 selsnap(&sel
.nb
.x
, &sel
.nb
.y
, -1);
758 selsnap(&sel
.ne
.x
, &sel
.ne
.y
, +1);
760 /* expand selection over line breaks */
761 if (sel
.type
== SEL_RECTANGULAR
)
763 i
= tlinelen(sel
.nb
.y
);
766 if (tlinelen(sel
.ne
.y
) <= sel
.ne
.x
)
767 sel
.ne
.x
= term
.col
- 1;
771 selected(int x
, int y
)
773 if (sel
.mode
== SEL_EMPTY
)
776 if (sel
.type
== SEL_RECTANGULAR
)
777 return BETWEEN(y
, sel
.nb
.y
, sel
.ne
.y
)
778 && BETWEEN(x
, sel
.nb
.x
, sel
.ne
.x
);
780 return BETWEEN(y
, sel
.nb
.y
, sel
.ne
.y
)
781 && (y
!= sel
.nb
.y
|| x
>= sel
.nb
.x
)
782 && (y
!= sel
.ne
.y
|| x
<= sel
.ne
.x
);
786 selsnap(int *x
, int *y
, int direction
)
788 int newx
, newy
, xt
, yt
;
789 int delim
, prevdelim
;
795 * Snap around if the word wraps around at the end or
796 * beginning of a line.
798 prevgp
= &term
.line
[*y
][*x
];
799 prevdelim
= ISDELIM(prevgp
->u
);
801 newx
= *x
+ direction
;
803 if (!BETWEEN(newx
, 0, term
.col
- 1)) {
805 newx
= (newx
+ term
.col
) % term
.col
;
806 if (!BETWEEN(newy
, 0, term
.row
- 1))
812 yt
= newy
, xt
= newx
;
813 if (!(term
.line
[yt
][xt
].mode
& ATTR_WRAP
))
817 if (newx
>= tlinelen(newy
))
820 gp
= &term
.line
[newy
][newx
];
821 delim
= ISDELIM(gp
->u
);
822 if (!(gp
->mode
& ATTR_WDUMMY
) && (delim
!= prevdelim
823 || (delim
&& gp
->u
!= prevgp
->u
)))
834 * Snap around if the the previous line or the current one
835 * has set ATTR_WRAP at its end. Then the whole next or
836 * previous line will be selected.
838 *x
= (direction
< 0) ? 0 : term
.col
- 1;
840 for (; *y
> 0; *y
+= direction
) {
841 if (!(term
.line
[*y
-1][term
.col
-1].mode
846 } else if (direction
> 0) {
847 for (; *y
< term
.row
-1; *y
+= direction
) {
848 if (!(term
.line
[*y
][term
.col
-1].mode
859 getbuttoninfo(XEvent
*e
)
862 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forceselmod
);
864 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
866 sel
.oe
.x
= x2col(e
->xbutton
.x
);
867 sel
.oe
.y
= y2row(e
->xbutton
.y
);
870 sel
.type
= SEL_REGULAR
;
871 for (type
= 1; type
< LEN(selmasks
); ++type
) {
872 if (match(selmasks
[type
], state
)) {
880 mousereport(XEvent
*e
)
882 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
883 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
889 if (e
->xbutton
.type
== MotionNotify
) {
890 if (x
== ox
&& y
== oy
)
892 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
894 /* MOUSE_MOTION: no reporting if no button is pressed */
895 if (IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
898 button
= oldbutton
+ 32;
902 if (!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
909 if (e
->xbutton
.type
== ButtonPress
) {
913 } else if (e
->xbutton
.type
== ButtonRelease
) {
915 /* MODE_MOUSEX10: no button release reporting */
916 if (IS_SET(MODE_MOUSEX10
))
918 if (button
== 64 || button
== 65)
923 if (!IS_SET(MODE_MOUSEX10
)) {
924 button
+= ((state
& ShiftMask
) ? 4 : 0)
925 + ((state
& Mod4Mask
) ? 8 : 0)
926 + ((state
& ControlMask
) ? 16 : 0);
929 if (IS_SET(MODE_MOUSESGR
)) {
930 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
932 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
933 } else if (x
< 223 && y
< 223) {
934 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
935 32+button
, 32+x
+1, 32+y
+1);
949 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
954 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
955 if (e
->xbutton
.button
== ms
->b
956 && match(ms
->mask
, e
->xbutton
.state
)) {
957 ttysend(ms
->s
, strlen(ms
->s
));
962 if (e
->xbutton
.button
== Button1
) {
963 clock_gettime(CLOCK_MONOTONIC
, &now
);
965 /* Clear previous selection, logically and visually. */
967 sel
.mode
= SEL_EMPTY
;
968 sel
.type
= SEL_REGULAR
;
969 sel
.oe
.x
= sel
.ob
.x
= x2col(e
->xbutton
.x
);
970 sel
.oe
.y
= sel
.ob
.y
= y2row(e
->xbutton
.y
);
973 * If the user clicks below predefined timeouts specific
974 * snapping behaviour is exposed.
976 if (TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
977 sel
.snap
= SNAP_LINE
;
978 } else if (TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
979 sel
.snap
= SNAP_WORD
;
986 sel
.mode
= SEL_READY
;
987 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
988 sel
.tclick2
= sel
.tclick1
;
997 int y
, bufsize
, lastx
, linelen
;
1003 bufsize
= (term
.col
+1) * (sel
.ne
.y
-sel
.nb
.y
+1) * UTF_SIZ
;
1004 ptr
= str
= xmalloc(bufsize
);
1006 /* append every set & selected glyph to the selection */
1007 for (y
= sel
.nb
.y
; y
<= sel
.ne
.y
; y
++) {
1008 if ((linelen
= tlinelen(y
)) == 0) {
1013 if (sel
.type
== SEL_RECTANGULAR
) {
1014 gp
= &term
.line
[y
][sel
.nb
.x
];
1017 gp
= &term
.line
[y
][sel
.nb
.y
== y
? sel
.nb
.x
: 0];
1018 lastx
= (sel
.ne
.y
== y
) ? sel
.ne
.x
: term
.col
-1;
1020 last
= &term
.line
[y
][MIN(lastx
, linelen
-1)];
1021 while (last
>= gp
&& last
->u
== ' ')
1024 for ( ; gp
<= last
; ++gp
) {
1025 if (gp
->mode
& ATTR_WDUMMY
)
1028 ptr
+= utf8encode(gp
->u
, ptr
);
1032 * Copy and pasting of line endings is inconsistent
1033 * in the inconsistent terminal and GUI world.
1034 * The best solution seems like to produce '\n' when
1035 * something is copied from st and convert '\n' to
1036 * '\r', when something to be pasted is received by
1038 * FIXME: Fix the computer world.
1040 if ((y
< sel
.ne
.y
|| lastx
>= linelen
) && !(last
->mode
& ATTR_WRAP
))
1050 xsetsel(getsel(), t
);
1054 propnotify(XEvent
*e
)
1056 XPropertyEvent
*xpev
;
1057 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1059 xpev
= &e
->xproperty
;
1060 if (xpev
->state
== PropertyNewValue
&&
1061 (xpev
->atom
== XA_PRIMARY
||
1062 xpev
->atom
== clipboard
)) {
1068 selnotify(XEvent
*e
)
1070 ulong nitems
, ofs
, rem
;
1072 uchar
*data
, *last
, *repl
;
1073 Atom type
, incratom
, property
;
1075 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
1078 if (e
->type
== SelectionNotify
) {
1079 property
= e
->xselection
.property
;
1080 } else if(e
->type
== PropertyNotify
) {
1081 property
= e
->xproperty
.atom
;
1085 if (property
== None
)
1089 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
1090 BUFSIZ
/4, False
, AnyPropertyType
,
1091 &type
, &format
, &nitems
, &rem
,
1093 fprintf(stderr
, "Clipboard allocation failed\n");
1097 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
1099 * If there is some PropertyNotify with no data, then
1100 * this is the signal of the selection owner that all
1101 * data has been transferred. We won't need to receive
1102 * PropertyNotify events anymore.
1104 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
1105 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
1109 if (type
== incratom
) {
1111 * Activate the PropertyNotify events so we receive
1112 * when the selection owner does send us the next
1115 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
1116 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
1120 * Deleting the property is the transfer start signal.
1122 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
1127 * As seen in getsel:
1128 * Line endings are inconsistent in the terminal and GUI world
1129 * copy and pasting. When receiving some selection data,
1130 * replace all '\n' with '\r'.
1131 * FIXME: Fix the computer world.
1134 last
= data
+ nitems
* format
/ 8;
1135 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
1139 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
1140 ttywrite("\033[200~", 6);
1141 ttysend((char *)data
, nitems
* format
/ 8);
1142 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
1143 ttywrite("\033[201~", 6);
1145 /* number of 32-bit chunks returned */
1146 ofs
+= nitems
* format
/ 32;
1150 * Deleting the property again tells the selection owner to send the
1151 * next data chunk in the property.
1153 if (e
->type
== PropertyNotify
)
1154 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
1158 selpaste(const Arg
*dummy
)
1160 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1161 xw
.win
, CurrentTime
);
1165 clipcopy(const Arg
*dummy
)
1169 if (sel
.clipboard
!= NULL
)
1170 free(sel
.clipboard
);
1172 if (sel
.primary
!= NULL
) {
1173 sel
.clipboard
= xstrdup(sel
.primary
);
1174 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1175 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1180 clippaste(const Arg
*dummy
)
1184 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1185 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, clipboard
,
1186 xw
.win
, CurrentTime
);
1194 sel
.mode
= SEL_IDLE
;
1196 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1200 selrequest(XEvent
*e
)
1202 XSelectionRequestEvent
*xsre
;
1203 XSelectionEvent xev
;
1204 Atom xa_targets
, string
, clipboard
;
1207 xsre
= (XSelectionRequestEvent
*) e
;
1208 xev
.type
= SelectionNotify
;
1209 xev
.requestor
= xsre
->requestor
;
1210 xev
.selection
= xsre
->selection
;
1211 xev
.target
= xsre
->target
;
1212 xev
.time
= xsre
->time
;
1213 if (xsre
->property
== None
)
1214 xsre
->property
= xsre
->target
;
1217 xev
.property
= None
;
1219 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1220 if (xsre
->target
== xa_targets
) {
1221 /* respond with the supported type */
1222 string
= sel
.xtarget
;
1223 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1224 XA_ATOM
, 32, PropModeReplace
,
1225 (uchar
*) &string
, 1);
1226 xev
.property
= xsre
->property
;
1227 } else if (xsre
->target
== sel
.xtarget
|| xsre
->target
== XA_STRING
) {
1229 * xith XA_STRING non ascii characters may be incorrect in the
1230 * requestor. It is not our problem, use utf8.
1232 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1233 if (xsre
->selection
== XA_PRIMARY
) {
1234 seltext
= sel
.primary
;
1235 } else if (xsre
->selection
== clipboard
) {
1236 seltext
= sel
.clipboard
;
1239 "Unhandled clipboard selection 0x%lx\n",
1243 if (seltext
!= NULL
) {
1244 XChangeProperty(xsre
->display
, xsre
->requestor
,
1245 xsre
->property
, xsre
->target
,
1247 (uchar
*)seltext
, strlen(seltext
));
1248 xev
.property
= xsre
->property
;
1252 /* all done, send a notification to the listener */
1253 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
1254 fprintf(stderr
, "Error sending SelectionNotify event\n");
1258 xsetsel(char *str
, Time t
)
1263 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
1264 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
1271 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
1276 if (e
->xbutton
.button
== Button2
) {
1278 } else if (e
->xbutton
.button
== Button1
) {
1279 if (sel
.mode
== SEL_READY
) {
1281 selcopy(e
->xbutton
.time
);
1284 sel
.mode
= SEL_IDLE
;
1285 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1292 int oldey
, oldex
, oldsby
, oldsey
;
1294 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
1302 sel
.mode
= SEL_READY
;
1309 if (oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
1310 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
1314 die(const char *errstr
, ...)
1318 va_start(ap
, errstr
);
1319 vfprintf(stderr
, errstr
, ap
);
1327 char **args
, *sh
, *prog
;
1328 const struct passwd
*pw
;
1329 char buf
[sizeof(long) * 8 + 1];
1332 if ((pw
= getpwuid(getuid())) == NULL
) {
1334 die("getpwuid:%s\n", strerror(errno
));
1336 die("who are you?\n");
1339 if ((sh
= getenv("SHELL")) == NULL
)
1340 sh
= (pw
->pw_shell
[0]) ? pw
->pw_shell
: shell
;
1348 args
= (opt_cmd
) ? opt_cmd
: (char *[]) {prog
, NULL
};
1350 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1352 unsetenv("COLUMNS");
1354 unsetenv("TERMCAP");
1355 setenv("LOGNAME", pw
->pw_name
, 1);
1356 setenv("USER", pw
->pw_name
, 1);
1357 setenv("SHELL", sh
, 1);
1358 setenv("HOME", pw
->pw_dir
, 1);
1359 setenv("TERM", termname
, 1);
1360 setenv("WINDOWID", buf
, 1);
1362 signal(SIGCHLD
, SIG_DFL
);
1363 signal(SIGHUP
, SIG_DFL
);
1364 signal(SIGINT
, SIG_DFL
);
1365 signal(SIGQUIT
, SIG_DFL
);
1366 signal(SIGTERM
, SIG_DFL
);
1367 signal(SIGALRM
, SIG_DFL
);
1379 if ((p
= waitpid(pid
, &stat
, WNOHANG
)) < 0)
1380 die("Waiting for pid %hd failed: %s\n", pid
, strerror(errno
));
1385 if (!WIFEXITED(stat
) || WEXITSTATUS(stat
))
1386 die("child finished with error '%d'\n", stat
);
1394 char cmd
[_POSIX_ARG_MAX
], **p
, *q
, *s
;
1397 if ((n
= strlen(stty_args
)) > sizeof(cmd
)-1)
1398 die("incorrect stty parameters\n");
1399 memcpy(cmd
, stty_args
, n
);
1401 siz
= sizeof(cmd
) - n
;
1402 for (p
= opt_cmd
; p
&& (s
= *p
); ++p
) {
1403 if ((n
= strlen(s
)) > siz
-1)
1404 die("stty parameter length too long\n");
1406 q
= memcpy(q
, s
, n
);
1411 if (system(cmd
) != 0)
1412 perror("Couldn't call stty");
1419 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1422 term
.mode
|= MODE_PRINT
;
1423 iofd
= (!strcmp(opt_io
, "-")) ?
1424 1 : open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1426 fprintf(stderr
, "Error opening %s:%s\n",
1427 opt_io
, strerror(errno
));
1432 if ((cmdfd
= open(opt_line
, O_RDWR
)) < 0)
1433 die("open line failed: %s\n", strerror(errno
));
1439 /* seems to work fine on linux, openbsd and freebsd */
1440 if (openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1441 die("openpty failed: %s\n", strerror(errno
));
1443 switch (pid
= fork()) {
1445 die("fork failed\n");
1449 setsid(); /* create a new process group */
1453 if (ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1454 die("ioctl TIOCSCTTY failed: %s\n", strerror(errno
));
1462 signal(SIGCHLD
, sigchld
);
1470 static char buf
[BUFSIZ
];
1471 static int buflen
= 0;
1473 int charsize
; /* size of utf8 char in bytes */
1477 /* append read bytes to unprocessed bytes */
1478 if ((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1479 die("Couldn't read from shell: %s\n", strerror(errno
));
1481 /* process every complete utf8 char */
1484 while ((charsize
= utf8decode(ptr
, &unicodep
, buflen
))) {
1490 /* keep any uncomplete utf8 char for the next call */
1491 memmove(buf
, ptr
, buflen
);
1497 ttywrite(const char *s
, size_t n
)
1504 * Remember that we are using a pty, which might be a modem line.
1505 * Writing too much will clog the line. That's why we are doing this
1507 * FIXME: Migrate the world to Plan 9.
1512 FD_SET(cmdfd
, &wfd
);
1513 FD_SET(cmdfd
, &rfd
);
1515 /* Check if we can write. */
1516 if (pselect(cmdfd
+1, &rfd
, &wfd
, NULL
, NULL
, NULL
) < 0) {
1519 die("select failed: %s\n", strerror(errno
));
1521 if (FD_ISSET(cmdfd
, &wfd
)) {
1523 * Only write the bytes written by ttywrite() or the
1524 * default of 256. This seems to be a reasonable value
1525 * for a serial line. Bigger values might clog the I/O.
1527 if ((r
= write(cmdfd
, s
, (n
< lim
)? n
: lim
)) < 0)
1531 * We weren't able to write out everything.
1532 * This means the buffer is getting full
1540 /* All bytes have been written. */
1544 if (FD_ISSET(cmdfd
, &rfd
))
1550 die("write error on tty: %s\n", strerror(errno
));
1554 ttysend(char *s
, size_t n
)
1560 if (IS_SET(MODE_ECHO
))
1561 while ((len
= utf8decode(s
, &u
, n
)) > 0) {
1573 w
.ws_row
= term
.row
;
1574 w
.ws_col
= term
.col
;
1575 w
.ws_xpixel
= xw
.tw
;
1576 w
.ws_ypixel
= xw
.th
;
1577 if (ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1578 fprintf(stderr
, "Couldn't set window size: %s\n", strerror(errno
));
1586 for (i
= 0; i
< term
.row
-1; i
++) {
1587 for (j
= 0; j
< term
.col
-1; j
++) {
1588 if (term
.line
[i
][j
].mode
& attr
)
1597 tsetdirt(int top
, int bot
)
1601 LIMIT(top
, 0, term
.row
-1);
1602 LIMIT(bot
, 0, term
.row
-1);
1604 for (i
= top
; i
<= bot
; i
++)
1609 tsetdirtattr(int attr
)
1613 for (i
= 0; i
< term
.row
-1; i
++) {
1614 for (j
= 0; j
< term
.col
-1; j
++) {
1615 if (term
.line
[i
][j
].mode
& attr
) {
1626 tsetdirt(0, term
.row
-1);
1632 static TCursor c
[2];
1633 int alt
= IS_SET(MODE_ALTSCREEN
);
1635 if (mode
== CURSOR_SAVE
) {
1637 } else if (mode
== CURSOR_LOAD
) {
1639 tmoveto(c
[alt
].x
, c
[alt
].y
);
1648 term
.c
= (TCursor
){{
1652 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1654 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1655 for (i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1658 term
.bot
= term
.row
- 1;
1659 term
.mode
= MODE_WRAP
;
1660 memset(term
.trantbl
, CS_USA
, sizeof(term
.trantbl
));
1663 for (i
= 0; i
< 2; i
++) {
1665 tcursor(CURSOR_SAVE
);
1666 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1672 tnew(int col
, int row
)
1674 term
= (Term
){ .c
= { .attr
= { .fg
= defaultfg
, .bg
= defaultbg
} } };
1684 Line
*tmp
= term
.line
;
1686 term
.line
= term
.alt
;
1688 term
.mode
^= MODE_ALTSCREEN
;
1693 tscrolldown(int orig
, int n
)
1698 LIMIT(n
, 0, term
.bot
-orig
+1);
1700 tsetdirt(orig
, term
.bot
-n
);
1701 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1703 for (i
= term
.bot
; i
>= orig
+n
; i
--) {
1704 temp
= term
.line
[i
];
1705 term
.line
[i
] = term
.line
[i
-n
];
1706 term
.line
[i
-n
] = temp
;
1713 tscrollup(int orig
, int n
)
1718 LIMIT(n
, 0, term
.bot
-orig
+1);
1720 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1721 tsetdirt(orig
+n
, term
.bot
);
1723 for (i
= orig
; i
<= term
.bot
-n
; i
++) {
1724 temp
= term
.line
[i
];
1725 term
.line
[i
] = term
.line
[i
+n
];
1726 term
.line
[i
+n
] = temp
;
1729 selscroll(orig
, -n
);
1733 selscroll(int orig
, int n
)
1738 if (BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1739 if ((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1743 if (sel
.type
== SEL_RECTANGULAR
) {
1744 if (sel
.ob
.y
< term
.top
)
1745 sel
.ob
.y
= term
.top
;
1746 if (sel
.oe
.y
> term
.bot
)
1747 sel
.oe
.y
= term
.bot
;
1749 if (sel
.ob
.y
< term
.top
) {
1750 sel
.ob
.y
= term
.top
;
1753 if (sel
.oe
.y
> term
.bot
) {
1754 sel
.oe
.y
= term
.bot
;
1755 sel
.oe
.x
= term
.col
;
1763 tnewline(int first_col
)
1767 if (y
== term
.bot
) {
1768 tscrollup(term
.top
, 1);
1772 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1778 char *p
= csiescseq
.buf
, *np
;
1787 csiescseq
.buf
[csiescseq
.len
] = '\0';
1788 while (p
< csiescseq
.buf
+csiescseq
.len
) {
1790 v
= strtol(p
, &np
, 10);
1793 if (v
== LONG_MAX
|| v
== LONG_MIN
)
1795 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1797 if (*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1801 csiescseq
.mode
[0] = *p
++;
1802 csiescseq
.mode
[1] = (p
< csiescseq
.buf
+csiescseq
.len
) ? *p
: '\0';
1805 /* for absolute user moves, when decom is set */
1807 tmoveato(int x
, int y
)
1809 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1813 tmoveto(int x
, int y
)
1817 if (term
.c
.state
& CURSOR_ORIGIN
) {
1822 maxy
= term
.row
- 1;
1824 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1825 term
.c
.x
= LIMIT(x
, 0, term
.col
-1);
1826 term
.c
.y
= LIMIT(y
, miny
, maxy
);
1830 tsetchar(Rune u
, Glyph
*attr
, int x
, int y
)
1832 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1833 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1834 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1835 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1836 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1837 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1838 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1839 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1840 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1844 * The table is proudly stolen from rxvt.
1846 if (term
.trantbl
[term
.charset
] == CS_GRAPHIC0
&&
1847 BETWEEN(u
, 0x41, 0x7e) && vt100_0
[u
- 0x41])
1848 utf8decode(vt100_0
[u
- 0x41], &u
, UTF_SIZ
);
1850 if (term
.line
[y
][x
].mode
& ATTR_WIDE
) {
1851 if (x
+1 < term
.col
) {
1852 term
.line
[y
][x
+1].u
= ' ';
1853 term
.line
[y
][x
+1].mode
&= ~ATTR_WDUMMY
;
1855 } else if (term
.line
[y
][x
].mode
& ATTR_WDUMMY
) {
1856 term
.line
[y
][x
-1].u
= ' ';
1857 term
.line
[y
][x
-1].mode
&= ~ATTR_WIDE
;
1861 term
.line
[y
][x
] = *attr
;
1862 term
.line
[y
][x
].u
= u
;
1866 tclearregion(int x1
, int y1
, int x2
, int y2
)
1872 temp
= x1
, x1
= x2
, x2
= temp
;
1874 temp
= y1
, y1
= y2
, y2
= temp
;
1876 LIMIT(x1
, 0, term
.col
-1);
1877 LIMIT(x2
, 0, term
.col
-1);
1878 LIMIT(y1
, 0, term
.row
-1);
1879 LIMIT(y2
, 0, term
.row
-1);
1881 for (y
= y1
; y
<= y2
; y
++) {
1883 for (x
= x1
; x
<= x2
; x
++) {
1884 gp
= &term
.line
[y
][x
];
1887 gp
->fg
= term
.c
.attr
.fg
;
1888 gp
->bg
= term
.c
.attr
.bg
;
1901 LIMIT(n
, 0, term
.col
- term
.c
.x
);
1905 size
= term
.col
- src
;
1906 line
= term
.line
[term
.c
.y
];
1908 memmove(&line
[dst
], &line
[src
], size
* sizeof(Glyph
));
1909 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1918 LIMIT(n
, 0, term
.col
- term
.c
.x
);
1922 size
= term
.col
- dst
;
1923 line
= term
.line
[term
.c
.y
];
1925 memmove(&line
[dst
], &line
[src
], size
* sizeof(Glyph
));
1926 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1930 tinsertblankline(int n
)
1932 if (BETWEEN(term
.c
.y
, term
.top
, term
.bot
))
1933 tscrolldown(term
.c
.y
, n
);
1939 if (BETWEEN(term
.c
.y
, term
.top
, term
.bot
))
1940 tscrollup(term
.c
.y
, n
);
1944 tdefcolor(int *attr
, int *npar
, int l
)
1949 switch (attr
[*npar
+ 1]) {
1950 case 2: /* direct color in RGB space */
1951 if (*npar
+ 4 >= l
) {
1953 "erresc(38): Incorrect number of parameters (%d)\n",
1957 r
= attr
[*npar
+ 2];
1958 g
= attr
[*npar
+ 3];
1959 b
= attr
[*npar
+ 4];
1961 if (!BETWEEN(r
, 0, 255) || !BETWEEN(g
, 0, 255) || !BETWEEN(b
, 0, 255))
1962 fprintf(stderr
, "erresc: bad rgb color (%u,%u,%u)\n",
1965 idx
= TRUECOLOR(r
, g
, b
);
1967 case 5: /* indexed color */
1968 if (*npar
+ 2 >= l
) {
1970 "erresc(38): Incorrect number of parameters (%d)\n",
1975 if (!BETWEEN(attr
[*npar
], 0, 255))
1976 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[*npar
]);
1980 case 0: /* implemented defined (only foreground) */
1981 case 1: /* transparent */
1982 case 3: /* direct color in CMY space */
1983 case 4: /* direct color in CMYK space */
1986 "erresc(38): gfx attr %d unknown\n", attr
[*npar
]);
1994 tsetattr(int *attr
, int l
)
1999 for (i
= 0; i
< l
; i
++) {
2002 term
.c
.attr
.mode
&= ~(
2011 term
.c
.attr
.fg
= defaultfg
;
2012 term
.c
.attr
.bg
= defaultbg
;
2015 term
.c
.attr
.mode
|= ATTR_BOLD
;
2018 term
.c
.attr
.mode
|= ATTR_FAINT
;
2021 term
.c
.attr
.mode
|= ATTR_ITALIC
;
2024 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
2026 case 5: /* slow blink */
2028 case 6: /* rapid blink */
2029 term
.c
.attr
.mode
|= ATTR_BLINK
;
2032 term
.c
.attr
.mode
|= ATTR_REVERSE
;
2035 term
.c
.attr
.mode
|= ATTR_INVISIBLE
;
2038 term
.c
.attr
.mode
|= ATTR_STRUCK
;
2041 term
.c
.attr
.mode
&= ~(ATTR_BOLD
| ATTR_FAINT
);
2044 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
2047 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
2050 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
2053 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
2056 term
.c
.attr
.mode
&= ~ATTR_INVISIBLE
;
2059 term
.c
.attr
.mode
&= ~ATTR_STRUCK
;
2062 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
2063 term
.c
.attr
.fg
= idx
;
2066 term
.c
.attr
.fg
= defaultfg
;
2069 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
2070 term
.c
.attr
.bg
= idx
;
2073 term
.c
.attr
.bg
= defaultbg
;
2076 if (BETWEEN(attr
[i
], 30, 37)) {
2077 term
.c
.attr
.fg
= attr
[i
] - 30;
2078 } else if (BETWEEN(attr
[i
], 40, 47)) {
2079 term
.c
.attr
.bg
= attr
[i
] - 40;
2080 } else if (BETWEEN(attr
[i
], 90, 97)) {
2081 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
2082 } else if (BETWEEN(attr
[i
], 100, 107)) {
2083 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
2086 "erresc(default): gfx attr %d unknown\n",
2087 attr
[i
]), csidump();
2095 tsetscroll(int t
, int b
)
2099 LIMIT(t
, 0, term
.row
-1);
2100 LIMIT(b
, 0, term
.row
-1);
2111 tsetmode(int priv
, int set
, int *args
, int narg
)
2116 for (lim
= args
+ narg
; args
< lim
; ++args
) {
2119 case 1: /* DECCKM -- Cursor key */
2120 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
2122 case 5: /* DECSCNM -- Reverse video */
2124 MODBIT(term
.mode
, set
, MODE_REVERSE
);
2125 if (mode
!= term
.mode
)
2128 case 6: /* DECOM -- Origin */
2129 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
2132 case 7: /* DECAWM -- Auto wrap */
2133 MODBIT(term
.mode
, set
, MODE_WRAP
);
2135 case 0: /* Error (IGNORED) */
2136 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
2137 case 3: /* DECCOLM -- Column (IGNORED) */
2138 case 4: /* DECSCLM -- Scroll (IGNORED) */
2139 case 8: /* DECARM -- Auto repeat (IGNORED) */
2140 case 18: /* DECPFF -- Printer feed (IGNORED) */
2141 case 19: /* DECPEX -- Printer extent (IGNORED) */
2142 case 42: /* DECNRCM -- National characters (IGNORED) */
2143 case 12: /* att610 -- Start blinking cursor (IGNORED) */
2145 case 25: /* DECTCEM -- Text Cursor Enable Mode */
2146 MODBIT(term
.mode
, !set
, MODE_HIDE
);
2148 case 9: /* X10 mouse compatibility mode */
2149 xsetpointermotion(0);
2150 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2151 MODBIT(term
.mode
, set
, MODE_MOUSEX10
);
2153 case 1000: /* 1000: report button press */
2154 xsetpointermotion(0);
2155 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2156 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
2158 case 1002: /* 1002: report motion on button press */
2159 xsetpointermotion(0);
2160 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2161 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
2163 case 1003: /* 1003: enable all mouse motions */
2164 xsetpointermotion(set
);
2165 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2166 MODBIT(term
.mode
, set
, MODE_MOUSEMANY
);
2168 case 1004: /* 1004: send focus events to tty */
2169 MODBIT(term
.mode
, set
, MODE_FOCUS
);
2171 case 1006: /* 1006: extended reporting mode */
2172 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
2175 MODBIT(term
.mode
, set
, MODE_8BIT
);
2177 case 1049: /* swap screen & set/restore cursor as xterm */
2178 if (!allowaltscreen
)
2180 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
2182 case 47: /* swap screen */
2184 if (!allowaltscreen
)
2186 alt
= IS_SET(MODE_ALTSCREEN
);
2188 tclearregion(0, 0, term
.col
-1,
2191 if (set
^ alt
) /* set is always 1 or 0 */
2197 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
2199 case 2004: /* 2004: bracketed paste mode */
2200 MODBIT(term
.mode
, set
, MODE_BRCKTPASTE
);
2202 /* Not implemented mouse modes. See comments there. */
2203 case 1001: /* mouse highlight mode; can hang the
2204 terminal by design when implemented. */
2205 case 1005: /* UTF-8 mouse mode; will confuse
2206 applications not supporting UTF-8
2208 case 1015: /* urxvt mangled mouse mode; incompatible
2209 and can be mistaken for other control
2213 "erresc: unknown private set/reset mode %d\n",
2219 case 0: /* Error (IGNORED) */
2221 case 2: /* KAM -- keyboard action */
2222 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
2224 case 4: /* IRM -- Insertion-replacement */
2225 MODBIT(term
.mode
, set
, MODE_INSERT
);
2227 case 12: /* SRM -- Send/Receive */
2228 MODBIT(term
.mode
, !set
, MODE_ECHO
);
2230 case 20: /* LNM -- Linefeed/new line */
2231 MODBIT(term
.mode
, set
, MODE_CRLF
);
2235 "erresc: unknown set/reset mode %d\n",
2249 switch (csiescseq
.mode
[0]) {
2252 fprintf(stderr
, "erresc: unknown csi ");
2256 case '@': /* ICH -- Insert <n> blank char */
2257 DEFAULT(csiescseq
.arg
[0], 1);
2258 tinsertblank(csiescseq
.arg
[0]);
2260 case 'A': /* CUU -- Cursor <n> Up */
2261 DEFAULT(csiescseq
.arg
[0], 1);
2262 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
2264 case 'B': /* CUD -- Cursor <n> Down */
2265 case 'e': /* VPR --Cursor <n> Down */
2266 DEFAULT(csiescseq
.arg
[0], 1);
2267 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
2269 case 'i': /* MC -- Media Copy */
2270 switch (csiescseq
.arg
[0]) {
2275 tdumpline(term
.c
.y
);
2281 term
.mode
&= ~MODE_PRINT
;
2284 term
.mode
|= MODE_PRINT
;
2288 case 'c': /* DA -- Device Attributes */
2289 if (csiescseq
.arg
[0] == 0)
2290 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2292 case 'C': /* CUF -- Cursor <n> Forward */
2293 case 'a': /* HPR -- Cursor <n> Forward */
2294 DEFAULT(csiescseq
.arg
[0], 1);
2295 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
2297 case 'D': /* CUB -- Cursor <n> Backward */
2298 DEFAULT(csiescseq
.arg
[0], 1);
2299 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
2301 case 'E': /* CNL -- Cursor <n> Down and first col */
2302 DEFAULT(csiescseq
.arg
[0], 1);
2303 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
2305 case 'F': /* CPL -- Cursor <n> Up and first col */
2306 DEFAULT(csiescseq
.arg
[0], 1);
2307 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
2309 case 'g': /* TBC -- Tabulation clear */
2310 switch (csiescseq
.arg
[0]) {
2311 case 0: /* clear current tab stop */
2312 term
.tabs
[term
.c
.x
] = 0;
2314 case 3: /* clear all the tabs */
2315 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
2321 case 'G': /* CHA -- Move to <col> */
2323 DEFAULT(csiescseq
.arg
[0], 1);
2324 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
2326 case 'H': /* CUP -- Move to <row> <col> */
2328 DEFAULT(csiescseq
.arg
[0], 1);
2329 DEFAULT(csiescseq
.arg
[1], 1);
2330 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
2332 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
2333 DEFAULT(csiescseq
.arg
[0], 1);
2334 tputtab(csiescseq
.arg
[0]);
2336 case 'J': /* ED -- Clear screen */
2338 switch (csiescseq
.arg
[0]) {
2340 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
2341 if (term
.c
.y
< term
.row
-1) {
2342 tclearregion(0, term
.c
.y
+1, term
.col
-1,
2348 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
2349 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2352 tclearregion(0, 0, term
.col
-1, term
.row
-1);
2358 case 'K': /* EL -- Clear line */
2359 switch (csiescseq
.arg
[0]) {
2361 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
2365 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2368 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
2372 case 'S': /* SU -- Scroll <n> line up */
2373 DEFAULT(csiescseq
.arg
[0], 1);
2374 tscrollup(term
.top
, csiescseq
.arg
[0]);
2376 case 'T': /* SD -- Scroll <n> line down */
2377 DEFAULT(csiescseq
.arg
[0], 1);
2378 tscrolldown(term
.top
, csiescseq
.arg
[0]);
2380 case 'L': /* IL -- Insert <n> blank lines */
2381 DEFAULT(csiescseq
.arg
[0], 1);
2382 tinsertblankline(csiescseq
.arg
[0]);
2384 case 'l': /* RM -- Reset Mode */
2385 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
2387 case 'M': /* DL -- Delete <n> lines */
2388 DEFAULT(csiescseq
.arg
[0], 1);
2389 tdeleteline(csiescseq
.arg
[0]);
2391 case 'X': /* ECH -- Erase <n> char */
2392 DEFAULT(csiescseq
.arg
[0], 1);
2393 tclearregion(term
.c
.x
, term
.c
.y
,
2394 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
2396 case 'P': /* DCH -- Delete <n> char */
2397 DEFAULT(csiescseq
.arg
[0], 1);
2398 tdeletechar(csiescseq
.arg
[0]);
2400 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
2401 DEFAULT(csiescseq
.arg
[0], 1);
2402 tputtab(-csiescseq
.arg
[0]);
2404 case 'd': /* VPA -- Move to <row> */
2405 DEFAULT(csiescseq
.arg
[0], 1);
2406 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
2408 case 'h': /* SM -- Set terminal mode */
2409 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
2411 case 'm': /* SGR -- Terminal attribute (color) */
2412 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
2414 case 'n': /* DSR – Device Status Report (cursor position) */
2415 if (csiescseq
.arg
[0] == 6) {
2416 len
= snprintf(buf
, sizeof(buf
),"\033[%i;%iR",
2417 term
.c
.y
+1, term
.c
.x
+1);
2421 case 'r': /* DECSTBM -- Set Scrolling Region */
2422 if (csiescseq
.priv
) {
2425 DEFAULT(csiescseq
.arg
[0], 1);
2426 DEFAULT(csiescseq
.arg
[1], term
.row
);
2427 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
2431 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2432 tcursor(CURSOR_SAVE
);
2434 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2435 tcursor(CURSOR_LOAD
);
2438 switch (csiescseq
.mode
[1]) {
2439 case 'q': /* DECSCUSR -- Set Cursor Style */
2440 DEFAULT(csiescseq
.arg
[0], 1);
2441 if (!BETWEEN(csiescseq
.arg
[0], 0, 6)) {
2444 xw
.cursor
= csiescseq
.arg
[0];
2460 for (i
= 0; i
< csiescseq
.len
; i
++) {
2461 c
= csiescseq
.buf
[i
] & 0xff;
2464 } else if (c
== '\n') {
2466 } else if (c
== '\r') {
2468 } else if (c
== 0x1b) {
2471 printf("(%02x)", c
);
2480 memset(&csiescseq
, 0, sizeof(csiescseq
));
2489 term
.esc
&= ~(ESC_STR_END
|ESC_STR
);
2491 par
= (narg
= strescseq
.narg
) ? atoi(strescseq
.args
[0]) : 0;
2493 switch (strescseq
.type
) {
2494 case ']': /* OSC -- Operating System Command */
2500 xsettitle(strescseq
.args
[1]);
2502 case 4: /* color set */
2505 p
= strescseq
.args
[2];
2507 case 104: /* color reset, here p = NULL */
2508 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2509 if (xsetcolorname(j
, p
)) {
2510 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2513 * TODO if defaultbg color is changed, borders
2521 case 'k': /* old title set compatibility */
2522 xsettitle(strescseq
.args
[0]);
2524 case 'P': /* DCS -- Device Control String */
2525 case '_': /* APC -- Application Program Command */
2526 case '^': /* PM -- Privacy Message */
2530 fprintf(stderr
, "erresc: unknown str ");
2538 char *p
= strescseq
.buf
;
2541 strescseq
.buf
[strescseq
.len
] = '\0';
2546 while (strescseq
.narg
< STR_ARG_SIZ
) {
2547 strescseq
.args
[strescseq
.narg
++] = p
;
2548 while ((c
= *p
) != ';' && c
!= '\0')
2562 printf("ESC%c", strescseq
.type
);
2563 for (i
= 0; i
< strescseq
.len
; i
++) {
2564 c
= strescseq
.buf
[i
] & 0xff;
2567 } else if (isprint(c
)) {
2569 } else if (c
== '\n') {
2571 } else if (c
== '\r') {
2573 } else if (c
== 0x1b) {
2576 printf("(%02x)", c
);
2585 memset(&strescseq
, 0, sizeof(strescseq
));
2589 sendbreak(const Arg
*arg
)
2591 if (tcsendbreak(cmdfd
, 0))
2592 perror("Error sending break");
2596 tprinter(char *s
, size_t len
)
2598 if (iofd
!= -1 && xwrite(iofd
, s
, len
) < 0) {
2599 fprintf(stderr
, "Error writing in %s:%s\n",
2600 opt_io
, strerror(errno
));
2607 toggleprinter(const Arg
*arg
)
2609 term
.mode
^= MODE_PRINT
;
2613 printscreen(const Arg
*arg
)
2619 printsel(const Arg
*arg
)
2629 if ((ptr
= getsel())) {
2630 tprinter(ptr
, strlen(ptr
));
2641 bp
= &term
.line
[n
][0];
2642 end
= &bp
[MIN(tlinelen(n
), term
.col
) - 1];
2643 if (bp
!= end
|| bp
->u
!= ' ') {
2644 for ( ;bp
<= end
; ++bp
)
2645 tprinter(buf
, utf8encode(bp
->u
, buf
));
2655 for (i
= 0; i
< term
.row
; ++i
)
2665 while (x
< term
.col
&& n
--)
2666 for (++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2669 while (x
> 0 && n
++)
2670 for (--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2673 term
.c
.x
= LIMIT(x
, 0, term
.col
-1);
2679 if (ISCONTROL(u
)) { /* control code */
2684 } else if (u
!= '\n' && u
!= '\r' && u
!= '\t') {
2693 tdeftran(char ascii
)
2695 static char cs
[] = "0B";
2696 static int vcs
[] = {CS_GRAPHIC0
, CS_USA
};
2699 if ((p
= strchr(cs
, ascii
)) == NULL
) {
2700 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2702 term
.trantbl
[term
.icharset
] = vcs
[p
- cs
];
2711 if (c
== '8') { /* DEC screen alignment test. */
2712 for (x
= 0; x
< term
.col
; ++x
) {
2713 for (y
= 0; y
< term
.row
; ++y
)
2714 tsetchar('E', &term
.c
.attr
, x
, y
);
2720 tstrsequence(uchar c
)
2723 case 0x90: /* DCS -- Device Control String */
2726 case 0x9f: /* APC -- Application Program Command */
2729 case 0x9e: /* PM -- Privacy Message */
2732 case 0x9d: /* OSC -- Operating System Command */
2738 term
.esc
|= ESC_STR
;
2742 tcontrolcode(uchar ascii
)
2749 tmoveto(term
.c
.x
-1, term
.c
.y
);
2752 tmoveto(0, term
.c
.y
);
2757 /* go to first col if the mode is set */
2758 tnewline(IS_SET(MODE_CRLF
));
2760 case '\a': /* BEL */
2761 if (term
.esc
& ESC_STR_END
) {
2762 /* backwards compatibility to xterm */
2765 if (!(xw
.state
& WIN_FOCUSED
))
2768 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
2771 case '\033': /* ESC */
2773 term
.esc
&= ~(ESC_CSI
|ESC_ALTCHARSET
|ESC_TEST
);
2774 term
.esc
|= ESC_START
;
2776 case '\016': /* SO (LS1 -- Locking shift 1) */
2777 case '\017': /* SI (LS0 -- Locking shift 0) */
2778 term
.charset
= 1 - (ascii
- '\016');
2780 case '\032': /* SUB */
2781 tsetchar('?', &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2782 case '\030': /* CAN */
2785 case '\005': /* ENQ (IGNORED) */
2786 case '\000': /* NUL (IGNORED) */
2787 case '\021': /* XON (IGNORED) */
2788 case '\023': /* XOFF (IGNORED) */
2789 case 0177: /* DEL (IGNORED) */
2791 case 0x80: /* TODO: PAD */
2792 case 0x81: /* TODO: HOP */
2793 case 0x82: /* TODO: BPH */
2794 case 0x83: /* TODO: NBH */
2795 case 0x84: /* TODO: IND */
2797 case 0x85: /* NEL -- Next line */
2798 tnewline(1); /* always go to first col */
2800 case 0x86: /* TODO: SSA */
2801 case 0x87: /* TODO: ESA */
2803 case 0x88: /* HTS -- Horizontal tab stop */
2804 term
.tabs
[term
.c
.x
] = 1;
2806 case 0x89: /* TODO: HTJ */
2807 case 0x8a: /* TODO: VTS */
2808 case 0x8b: /* TODO: PLD */
2809 case 0x8c: /* TODO: PLU */
2810 case 0x8d: /* TODO: RI */
2811 case 0x8e: /* TODO: SS2 */
2812 case 0x8f: /* TODO: SS3 */
2813 case 0x91: /* TODO: PU1 */
2814 case 0x92: /* TODO: PU2 */
2815 case 0x93: /* TODO: STS */
2816 case 0x94: /* TODO: CCH */
2817 case 0x95: /* TODO: MW */
2818 case 0x96: /* TODO: SPA */
2819 case 0x97: /* TODO: EPA */
2820 case 0x98: /* TODO: SOS */
2821 case 0x99: /* TODO: SGCI */
2823 case 0x9a: /* DECID -- Identify Terminal */
2824 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2826 case 0x9b: /* TODO: CSI */
2827 case 0x9c: /* TODO: ST */
2829 case 0x90: /* DCS -- Device Control String */
2830 case 0x9d: /* OSC -- Operating System Command */
2831 case 0x9e: /* PM -- Privacy Message */
2832 case 0x9f: /* APC -- Application Program Command */
2833 tstrsequence(ascii
);
2836 /* only CAN, SUB, \a and C1 chars interrupt a sequence */
2837 term
.esc
&= ~(ESC_STR_END
|ESC_STR
);
2841 * returns 1 when the sequence is finished and it hasn't to read
2842 * more characters for this sequence, otherwise 0
2845 eschandle(uchar ascii
)
2849 term
.esc
|= ESC_CSI
;
2852 term
.esc
|= ESC_TEST
;
2854 case 'P': /* DCS -- Device Control String */
2855 case '_': /* APC -- Application Program Command */
2856 case '^': /* PM -- Privacy Message */
2857 case ']': /* OSC -- Operating System Command */
2858 case 'k': /* old title set compatibility */
2859 tstrsequence(ascii
);
2861 case 'n': /* LS2 -- Locking shift 2 */
2862 case 'o': /* LS3 -- Locking shift 3 */
2863 term
.charset
= 2 + (ascii
- 'n');
2865 case '(': /* GZD4 -- set primary charset G0 */
2866 case ')': /* G1D4 -- set secondary charset G1 */
2867 case '*': /* G2D4 -- set tertiary charset G2 */
2868 case '+': /* G3D4 -- set quaternary charset G3 */
2869 term
.icharset
= ascii
- '(';
2870 term
.esc
|= ESC_ALTCHARSET
;
2872 case 'D': /* IND -- Linefeed */
2873 if (term
.c
.y
== term
.bot
) {
2874 tscrollup(term
.top
, 1);
2876 tmoveto(term
.c
.x
, term
.c
.y
+1);
2879 case 'E': /* NEL -- Next line */
2880 tnewline(1); /* always go to first col */
2882 case 'H': /* HTS -- Horizontal tab stop */
2883 term
.tabs
[term
.c
.x
] = 1;
2885 case 'M': /* RI -- Reverse index */
2886 if (term
.c
.y
== term
.top
) {
2887 tscrolldown(term
.top
, 1);
2889 tmoveto(term
.c
.x
, term
.c
.y
-1);
2892 case 'Z': /* DECID -- Identify Terminal */
2893 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2895 case 'c': /* RIS -- Reset to inital state */
2900 case '=': /* DECPAM -- Application keypad */
2901 term
.mode
|= MODE_APPKEYPAD
;
2903 case '>': /* DECPNM -- Normal keypad */
2904 term
.mode
&= ~MODE_APPKEYPAD
;
2906 case '7': /* DECSC -- Save Cursor */
2907 tcursor(CURSOR_SAVE
);
2909 case '8': /* DECRC -- Restore Cursor */
2910 tcursor(CURSOR_LOAD
);
2912 case '\\': /* ST -- String Terminator */
2913 if (term
.esc
& ESC_STR_END
)
2917 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2918 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2932 control
= ISCONTROL(u
);
2933 len
= utf8encode(u
, c
);
2934 if (!control
&& (width
= wcwidth(u
)) == -1) {
2935 memcpy(c
, "\357\277\275", 4); /* UTF_INVALID */
2939 if (IS_SET(MODE_PRINT
))
2943 * STR sequence must be checked before anything else
2944 * because it uses all following characters until it
2945 * receives a ESC, a SUB, a ST or any other C1 control
2948 if (term
.esc
& ESC_STR
) {
2949 if (u
== '\a' || u
== 030 || u
== 032 || u
== 033 ||
2951 term
.esc
&= ~(ESC_START
|ESC_STR
);
2952 term
.esc
|= ESC_STR_END
;
2953 } else if (strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2954 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2955 strescseq
.len
+= len
;
2959 * Here is a bug in terminals. If the user never sends
2960 * some code to stop the str or esc command, then st
2961 * will stop responding. But this is better than
2962 * silently failing with unknown characters. At least
2963 * then users will report back.
2965 * In the case users ever get fixed, here is the code:
2976 * Actions of control codes must be performed as soon they arrive
2977 * because they can be embedded inside a control sequence, and
2978 * they must not cause conflicts with sequences.
2983 * control codes are not shown ever
2986 } else if (term
.esc
& ESC_START
) {
2987 if (term
.esc
& ESC_CSI
) {
2988 csiescseq
.buf
[csiescseq
.len
++] = u
;
2989 if (BETWEEN(u
, 0x40, 0x7E)
2990 || csiescseq
.len
>= \
2991 sizeof(csiescseq
.buf
)-1) {
2997 } else if (term
.esc
& ESC_ALTCHARSET
) {
2999 } else if (term
.esc
& ESC_TEST
) {
3004 /* sequence already finished */
3008 * All characters which form part of a sequence are not
3013 if (sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
3016 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
3017 if (IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
3018 gp
->mode
|= ATTR_WRAP
;
3020 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
3023 if (IS_SET(MODE_INSERT
) && term
.c
.x
+width
< term
.col
)
3024 memmove(gp
+width
, gp
, (term
.col
- term
.c
.x
- width
) * sizeof(Glyph
));
3026 if (term
.c
.x
+width
> term
.col
) {
3028 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
3031 tsetchar(u
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
3034 gp
->mode
|= ATTR_WIDE
;
3035 if (term
.c
.x
+1 < term
.col
) {
3037 gp
[1].mode
= ATTR_WDUMMY
;
3040 if (term
.c
.x
+width
< term
.col
) {
3041 tmoveto(term
.c
.x
+width
, term
.c
.y
);
3043 term
.c
.state
|= CURSOR_WRAPNEXT
;
3048 tresize(int col
, int row
)
3051 int minrow
= MIN(row
, term
.row
);
3052 int mincol
= MIN(col
, term
.col
);
3056 if (col
< 1 || row
< 1) {
3058 "tresize: error resizing to %dx%d\n", col
, row
);
3063 * slide screen to keep cursor where we expect it -
3064 * tscrollup would work here, but we can optimize to
3065 * memmove because we're freeing the earlier lines
3067 for (i
= 0; i
<= term
.c
.y
- row
; i
++) {
3071 /* ensure that both src and dst are not NULL */
3073 memmove(term
.line
, term
.line
+ i
, row
* sizeof(Line
));
3074 memmove(term
.alt
, term
.alt
+ i
, row
* sizeof(Line
));
3076 for (i
+= row
; i
< term
.row
; i
++) {
3081 /* resize to new width */
3082 term
.specbuf
= xrealloc(term
.specbuf
, col
* sizeof(XftGlyphFontSpec
));
3084 /* resize to new height */
3085 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
3086 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
3087 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
3088 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
3090 /* resize each row to new width, zero-pad if needed */
3091 for (i
= 0; i
< minrow
; i
++) {
3092 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
3093 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
3096 /* allocate any new rows */
3097 for (/* i == minrow */; i
< row
; i
++) {
3098 term
.line
[i
] = xmalloc(col
* sizeof(Glyph
));
3099 term
.alt
[i
] = xmalloc(col
* sizeof(Glyph
));
3101 if (col
> term
.col
) {
3102 bp
= term
.tabs
+ term
.col
;
3104 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
3105 while (--bp
> term
.tabs
&& !*bp
)
3107 for (bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
3110 /* update terminal size */
3113 /* reset scrolling region */
3114 tsetscroll(0, row
-1);
3115 /* make use of the LIMIT in tmoveto */
3116 tmoveto(term
.c
.x
, term
.c
.y
);
3117 /* Clearing both screens (it makes dirty all lines) */
3119 for (i
= 0; i
< 2; i
++) {
3120 if (mincol
< col
&& 0 < minrow
) {
3121 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
3123 if (0 < col
&& minrow
< row
) {
3124 tclearregion(0, minrow
, col
- 1, row
- 1);
3127 tcursor(CURSOR_LOAD
);
3133 xresize(int col
, int row
)
3135 xw
.tw
= MAX(1, col
* xw
.cw
);
3136 xw
.th
= MAX(1, row
* xw
.ch
);
3138 XFreePixmap(xw
.dpy
, xw
.buf
);
3139 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
3140 DefaultDepth(xw
.dpy
, xw
.scr
));
3141 XftDrawChange(xw
.draw
, xw
.buf
);
3142 xclear(0, 0, xw
.w
, xw
.h
);
3146 sixd_to_16bit(int x
)
3148 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
3152 xloadcolor(int i
, const char *name
, Color
*ncolor
)
3154 XRenderColor color
= { .alpha
= 0xffff };
3157 if (BETWEEN(i
, 16, 255)) { /* 256 color */
3158 if (i
< 6*6*6+16) { /* same colors as xterm */
3159 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
3160 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
3161 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
3162 } else { /* greyscale */
3163 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
3164 color
.green
= color
.blue
= color
.red
;
3166 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
3167 xw
.cmap
, &color
, ncolor
);
3169 name
= colorname
[i
];
3172 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
3183 for (cp
= dc
.col
; cp
< &dc
.col
[LEN(dc
.col
)]; ++cp
)
3184 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
3187 for (i
= 0; i
< LEN(dc
.col
); i
++)
3188 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
3190 die("Could not allocate color '%s'\n", colorname
[i
]);
3192 die("Could not allocate color %d\n", i
);
3198 xsetcolorname(int x
, const char *name
)
3202 if (!BETWEEN(x
, 0, LEN(dc
.col
)))
3206 if (!xloadcolor(x
, name
, &ncolor
))
3209 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
3216 xtermclear(int col1
, int row1
, int col2
, int row2
)
3218 XftDrawRect(xw
.draw
,
3219 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
3220 borderpx
+ col1
* xw
.cw
,
3221 borderpx
+ row1
* xw
.ch
,
3222 (col2
-col1
+1) * xw
.cw
,
3223 (row2
-row1
+1) * xw
.ch
);
3227 * Absolute coordinates.
3230 xclear(int x1
, int y1
, int x2
, int y2
)
3232 XftDrawRect(xw
.draw
,
3233 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
3234 x1
, y1
, x2
-x1
, y2
-y1
);
3240 XClassHint
class = {termname
, opt_class
? opt_class
: termname
};
3241 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
3242 XSizeHints
*sizeh
= NULL
;
3244 sizeh
= XAllocSizeHints();
3246 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
3247 sizeh
->height
= xw
.h
;
3248 sizeh
->width
= xw
.w
;
3249 sizeh
->height_inc
= xw
.ch
;
3250 sizeh
->width_inc
= xw
.cw
;
3251 sizeh
->base_height
= 2 * borderpx
;
3252 sizeh
->base_width
= 2 * borderpx
;
3254 sizeh
->flags
|= PMaxSize
| PMinSize
;
3255 sizeh
->min_width
= sizeh
->max_width
= xw
.w
;
3256 sizeh
->min_height
= sizeh
->max_height
= xw
.h
;
3258 if (xw
.gm
& (XValue
|YValue
)) {
3259 sizeh
->flags
|= USPosition
| PWinGravity
;
3262 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
3265 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
3271 xgeommasktogravity(int mask
)
3273 switch (mask
& (XNegative
|YNegative
)) {
3275 return NorthWestGravity
;
3277 return NorthEastGravity
;
3279 return SouthWestGravity
;
3282 return SouthEastGravity
;
3286 xloadfont(Font
*f
, FcPattern
*pattern
)
3291 match
= FcFontMatch(NULL
, pattern
, &result
);
3295 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
3296 FcPatternDestroy(match
);
3301 f
->pattern
= FcPatternDuplicate(pattern
);
3303 f
->ascent
= f
->match
->ascent
;
3304 f
->descent
= f
->match
->descent
;
3306 f
->rbearing
= f
->match
->max_advance_width
;
3308 f
->height
= f
->ascent
+ f
->descent
;
3309 f
->width
= f
->lbearing
+ f
->rbearing
;
3315 xloadfonts(char *fontstr
, double fontsize
)
3321 if (fontstr
[0] == '-') {
3322 pattern
= XftXlfdParse(fontstr
, False
, False
);
3324 pattern
= FcNameParse((FcChar8
*)fontstr
);
3328 die("st: can't open font %s\n", fontstr
);
3331 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
3332 FcPatternDel(pattern
, FC_SIZE
);
3333 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
3334 usedfontsize
= fontsize
;
3336 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
3338 usedfontsize
= fontval
;
3339 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
3344 * Default font size is 12, if none given. This is to
3345 * have a known usedfontsize value.
3347 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
3350 defaultfontsize
= usedfontsize
;
3353 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
3354 FcDefaultSubstitute(pattern
);
3356 if (xloadfont(&dc
.font
, pattern
))
3357 die("st: can't open font %s\n", fontstr
);
3359 if (usedfontsize
< 0) {
3360 FcPatternGetDouble(dc
.font
.match
->pattern
,
3361 FC_PIXEL_SIZE
, 0, &fontval
);
3362 usedfontsize
= fontval
;
3364 defaultfontsize
= fontval
;
3367 /* Setting character width and height. */
3368 xw
.cw
= ceilf(dc
.font
.width
* cwscale
);
3369 xw
.ch
= ceilf(dc
.font
.height
* chscale
);
3371 FcPatternDel(pattern
, FC_SLANT
);
3372 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
3373 if (xloadfont(&dc
.ifont
, pattern
))
3374 die("st: can't open font %s\n", fontstr
);
3376 FcPatternDel(pattern
, FC_WEIGHT
);
3377 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
3378 if (xloadfont(&dc
.ibfont
, pattern
))
3379 die("st: can't open font %s\n", fontstr
);
3381 FcPatternDel(pattern
, FC_SLANT
);
3382 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
3383 if (xloadfont(&dc
.bfont
, pattern
))
3384 die("st: can't open font %s\n", fontstr
);
3386 FcPatternDestroy(pattern
);
3390 xunloadfont(Font
*f
)
3392 XftFontClose(xw
.dpy
, f
->match
);
3393 FcPatternDestroy(f
->pattern
);
3395 FcFontSetDestroy(f
->set
);
3401 /* Free the loaded fonts in the font cache. */
3403 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
3405 xunloadfont(&dc
.font
);
3406 xunloadfont(&dc
.bfont
);
3407 xunloadfont(&dc
.ifont
);
3408 xunloadfont(&dc
.ibfont
);
3412 xzoom(const Arg
*arg
)
3416 larg
.f
= usedfontsize
+ arg
->f
;
3421 xzoomabs(const Arg
*arg
)
3424 xloadfonts(usedfont
, arg
->f
);
3431 xzoomreset(const Arg
*arg
)
3435 if (defaultfontsize
> 0) {
3436 larg
.f
= defaultfontsize
;
3447 pid_t thispid
= getpid();
3448 XColor xmousefg
, xmousebg
;
3450 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
3451 die("Can't open display\n");
3452 xw
.scr
= XDefaultScreen(xw
.dpy
);
3453 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
3457 die("Could not init fontconfig.\n");
3459 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
3460 xloadfonts(usedfont
, 0);
3463 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
3466 /* adjust fixed window geometry */
3467 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
3468 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
3469 if (xw
.gm
& XNegative
)
3470 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - xw
.w
- 2;
3471 if (xw
.gm
& YNegative
)
3472 xw
.t
+= DisplayWidth(xw
.dpy
, xw
.scr
) - xw
.h
- 2;
3475 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
3476 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
3477 xw
.attrs
.bit_gravity
= NorthWestGravity
;
3478 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
3479 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
3480 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
3481 xw
.attrs
.colormap
= xw
.cmap
;
3483 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
3484 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
3485 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
3486 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
3487 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
3488 | CWEventMask
| CWColormap
, &xw
.attrs
);
3490 memset(&gcvalues
, 0, sizeof(gcvalues
));
3491 gcvalues
.graphics_exposures
= False
;
3492 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
3494 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
3495 DefaultDepth(xw
.dpy
, xw
.scr
));
3496 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
3497 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
3499 /* Xft rendering context */
3500 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
3503 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3504 XSetLocaleModifiers("@im=local");
3505 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3506 XSetLocaleModifiers("@im=");
3507 if ((xw
.xim
= XOpenIM(xw
.dpy
,
3508 NULL
, NULL
, NULL
)) == NULL
) {
3509 die("XOpenIM failed. Could not open input"
3514 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
3515 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
3516 XNFocusWindow
, xw
.win
, NULL
);
3518 die("XCreateIC failed. Could not obtain input method.\n");
3520 /* white cursor, black outline */
3521 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
3522 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
3524 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
3525 xmousefg
.red
= 0xffff;
3526 xmousefg
.green
= 0xffff;
3527 xmousefg
.blue
= 0xffff;
3530 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
3531 xmousebg
.red
= 0x0000;
3532 xmousebg
.green
= 0x0000;
3533 xmousebg
.blue
= 0x0000;
3536 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
3538 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
3539 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
3540 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
3541 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
3543 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
3544 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
3545 PropModeReplace
, (uchar
*)&thispid
, 1);
3548 XMapWindow(xw
.dpy
, xw
.win
);
3550 XSync(xw
.dpy
, False
);
3554 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
3556 float winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
, xp
, yp
;
3557 ushort mode
, prevmode
= USHRT_MAX
;
3558 Font
*font
= &dc
.font
;
3559 int frcflags
= FRC_NORMAL
;
3560 float runewidth
= xw
.cw
;
3564 FcPattern
*fcpattern
, *fontpattern
;
3565 FcFontSet
*fcsets
[] = { NULL
};
3566 FcCharSet
*fccharset
;
3567 int i
, f
, numspecs
= 0;
3569 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
3570 /* Fetch rune and mode for current glyph. */
3572 mode
= glyphs
[i
].mode
;
3574 /* Skip dummy wide-character spacing. */
3575 if (mode
== ATTR_WDUMMY
)
3578 /* Determine font for glyph if different from previous glyph. */
3579 if (prevmode
!= mode
) {
3582 frcflags
= FRC_NORMAL
;
3583 runewidth
= xw
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
3584 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
3586 frcflags
= FRC_ITALICBOLD
;
3587 } else if (mode
& ATTR_ITALIC
) {
3589 frcflags
= FRC_ITALIC
;
3590 } else if (mode
& ATTR_BOLD
) {
3592 frcflags
= FRC_BOLD
;
3594 yp
= winy
+ font
->ascent
;
3597 /* Lookup character index with default font. */
3598 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
3600 specs
[numspecs
].font
= font
->match
;
3601 specs
[numspecs
].glyph
= glyphidx
;
3602 specs
[numspecs
].x
= (short)xp
;
3603 specs
[numspecs
].y
= (short)yp
;
3609 /* Fallback on font cache, search the font cache for match. */
3610 for (f
= 0; f
< frclen
; f
++) {
3611 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
3612 /* Everything correct. */
3613 if (glyphidx
&& frc
[f
].flags
== frcflags
)
3615 /* We got a default font for a not found glyph. */
3616 if (!glyphidx
&& frc
[f
].flags
== frcflags
3617 && frc
[f
].unicodep
== rune
) {
3622 /* Nothing was found. Use fontconfig to find matching font. */
3625 font
->set
= FcFontSort(0, font
->pattern
,
3627 fcsets
[0] = font
->set
;
3630 * Nothing was found in the cache. Now use
3631 * some dozen of Fontconfig calls to get the
3632 * font for one single character.
3634 * Xft and fontconfig are design failures.
3636 fcpattern
= FcPatternDuplicate(font
->pattern
);
3637 fccharset
= FcCharSetCreate();
3639 FcCharSetAddChar(fccharset
, rune
);
3640 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3642 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
3644 FcConfigSubstitute(0, fcpattern
,
3646 FcDefaultSubstitute(fcpattern
);
3648 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
3652 * Overwrite or create the new cache entry.
3654 if (frclen
>= LEN(frc
)) {
3655 frclen
= LEN(frc
) - 1;
3656 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
3657 frc
[frclen
].unicodep
= 0;
3660 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
3662 frc
[frclen
].flags
= frcflags
;
3663 frc
[frclen
].unicodep
= rune
;
3665 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
3670 FcPatternDestroy(fcpattern
);
3671 FcCharSetDestroy(fccharset
);
3674 specs
[numspecs
].font
= frc
[f
].font
;
3675 specs
[numspecs
].glyph
= glyphidx
;
3676 specs
[numspecs
].x
= (short)xp
;
3677 specs
[numspecs
].y
= (short)(winy
+ frc
[f
].font
->ascent
);
3686 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
3688 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
3689 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
3690 width
= charlen
* xw
.cw
;
3691 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
3692 XRenderColor colfg
, colbg
;
3695 /* Determine foreground and background colors based on mode. */
3696 if (base
.fg
== defaultfg
) {
3697 if (base
.mode
& ATTR_ITALIC
)
3698 base
.fg
= defaultitalic
;
3699 else if ((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
))
3700 base
.fg
= defaultitalic
;
3701 else if (base
.mode
& ATTR_UNDERLINE
)
3702 base
.fg
= defaultunderline
;
3705 if (IS_TRUECOL(base
.fg
)) {
3706 colfg
.alpha
= 0xffff;
3707 colfg
.red
= TRUERED(base
.fg
);
3708 colfg
.green
= TRUEGREEN(base
.fg
);
3709 colfg
.blue
= TRUEBLUE(base
.fg
);
3710 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
3713 fg
= &dc
.col
[base
.fg
];
3716 if (IS_TRUECOL(base
.bg
)) {
3717 colbg
.alpha
= 0xffff;
3718 colbg
.green
= TRUEGREEN(base
.bg
);
3719 colbg
.red
= TRUERED(base
.bg
);
3720 colbg
.blue
= TRUEBLUE(base
.bg
);
3721 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
3724 bg
= &dc
.col
[base
.bg
];
3727 /* Change basic system colors [0-7] to bright system colors [8-15] */
3728 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
3729 fg
= &dc
.col
[base
.fg
+ 8];
3731 if (IS_SET(MODE_REVERSE
)) {
3732 if (fg
== &dc
.col
[defaultfg
]) {
3733 fg
= &dc
.col
[defaultbg
];
3735 colfg
.red
= ~fg
->color
.red
;
3736 colfg
.green
= ~fg
->color
.green
;
3737 colfg
.blue
= ~fg
->color
.blue
;
3738 colfg
.alpha
= fg
->color
.alpha
;
3739 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
3744 if (bg
== &dc
.col
[defaultbg
]) {
3745 bg
= &dc
.col
[defaultfg
];
3747 colbg
.red
= ~bg
->color
.red
;
3748 colbg
.green
= ~bg
->color
.green
;
3749 colbg
.blue
= ~bg
->color
.blue
;
3750 colbg
.alpha
= bg
->color
.alpha
;
3751 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
3757 if (base
.mode
& ATTR_REVERSE
) {
3763 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
3764 colfg
.red
= fg
->color
.red
/ 2;
3765 colfg
.green
= fg
->color
.green
/ 2;
3766 colfg
.blue
= fg
->color
.blue
/ 2;
3767 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
3771 if (base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
3774 if (base
.mode
& ATTR_INVISIBLE
)
3777 /* Intelligent cleaning up of the borders. */
3779 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
3780 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
3782 if (x
+ charlen
>= term
.col
) {
3783 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
3784 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
3787 xclear(winx
, 0, winx
+ width
, borderpx
);
3788 if (y
== term
.row
-1)
3789 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
3791 /* Clean up the region we want to draw to. */
3792 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
3794 /* Set the clip region because Xft is sometimes dirty. */
3799 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3801 /* Render the glyphs. */
3802 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
3804 /* Render underline and strikethrough. */
3805 if (base
.mode
& ATTR_UNDERLINE
) {
3806 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
3810 if (base
.mode
& ATTR_STRUCK
) {
3811 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
3815 /* Reset clip to none. */
3816 XftDrawSetClip(xw
.draw
, 0);
3820 xdrawglyph(Glyph g
, int x
, int y
)
3823 XftGlyphFontSpec spec
;
3825 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
3826 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
3832 static int oldx
= 0, oldy
= 0;
3834 Glyph g
= {' ', ATTR_NULL
, defaultbg
, defaultcs
}, og
;
3835 int ena_sel
= sel
.ob
.x
!= -1 && sel
.alt
== IS_SET(MODE_ALTSCREEN
);
3838 LIMIT(oldx
, 0, term
.col
-1);
3839 LIMIT(oldy
, 0, term
.row
-1);
3843 /* adjust position if in dummy */
3844 if (term
.line
[oldy
][oldx
].mode
& ATTR_WDUMMY
)
3846 if (term
.line
[term
.c
.y
][curx
].mode
& ATTR_WDUMMY
)
3849 /* remove the old cursor */
3850 og
= term
.line
[oldy
][oldx
];
3851 if (ena_sel
&& selected(oldx
, oldy
))
3852 og
.mode
^= ATTR_REVERSE
;
3853 xdrawglyph(og
, oldx
, oldy
);
3855 g
.u
= term
.line
[term
.c
.y
][term
.c
.x
].u
;
3858 * Select the right color for the right mode.
3860 if (IS_SET(MODE_REVERSE
)) {
3861 g
.mode
|= ATTR_REVERSE
;
3863 if (ena_sel
&& selected(term
.c
.x
, term
.c
.y
)) {
3864 drawcol
= dc
.col
[defaultcs
];
3867 drawcol
= dc
.col
[defaultrcs
];
3871 if (ena_sel
&& selected(term
.c
.x
, term
.c
.y
)) {
3872 drawcol
= dc
.col
[defaultrcs
];
3876 drawcol
= dc
.col
[defaultcs
];
3880 if (IS_SET(MODE_HIDE
))
3883 /* draw the new one */
3884 if (xw
.state
& WIN_FOCUSED
) {
3885 switch (xw
.cursor
) {
3886 case 7: /* st extension: snowman */
3887 utf8decode("☃", &g
.u
, UTF_SIZ
);
3888 case 0: /* Blinking Block */
3889 case 1: /* Blinking Block (Default) */
3890 case 2: /* Steady Block */
3891 g
.mode
|= term
.line
[term
.c
.y
][curx
].mode
& ATTR_WIDE
;
3892 xdrawglyph(g
, term
.c
.x
, term
.c
.y
);
3894 case 3: /* Blinking Underline */
3895 case 4: /* Steady Underline */
3896 XftDrawRect(xw
.draw
, &drawcol
,
3897 borderpx
+ curx
* xw
.cw
,
3898 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- \
3900 xw
.cw
, cursorthickness
);
3902 case 5: /* Blinking bar */
3903 case 6: /* Steady bar */
3904 XftDrawRect(xw
.draw
, &drawcol
,
3905 borderpx
+ curx
* xw
.cw
,
3906 borderpx
+ term
.c
.y
* xw
.ch
,
3907 cursorthickness
, xw
.ch
);
3911 XftDrawRect(xw
.draw
, &drawcol
,
3912 borderpx
+ curx
* xw
.cw
,
3913 borderpx
+ term
.c
.y
* xw
.ch
,
3915 XftDrawRect(xw
.draw
, &drawcol
,
3916 borderpx
+ curx
* xw
.cw
,
3917 borderpx
+ term
.c
.y
* xw
.ch
,
3919 XftDrawRect(xw
.draw
, &drawcol
,
3920 borderpx
+ (curx
+ 1) * xw
.cw
- 1,
3921 borderpx
+ term
.c
.y
* xw
.ch
,
3923 XftDrawRect(xw
.draw
, &drawcol
,
3924 borderpx
+ curx
* xw
.cw
,
3925 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3928 oldx
= curx
, oldy
= term
.c
.y
;
3937 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3939 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3940 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
3947 xsettitle(opt_title
? opt_title
: "st");
3960 drawregion(0, 0, term
.col
, term
.row
);
3961 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3963 XSetForeground(xw
.dpy
, dc
.gc
,
3964 dc
.col
[IS_SET(MODE_REVERSE
)?
3965 defaultfg
: defaultbg
].pixel
);
3969 drawregion(int x1
, int y1
, int x2
, int y2
)
3971 int i
, x
, y
, ox
, numspecs
;
3973 XftGlyphFontSpec
*specs
;
3974 int ena_sel
= sel
.ob
.x
!= -1 && sel
.alt
== IS_SET(MODE_ALTSCREEN
);
3976 if (!(xw
.state
& WIN_VISIBLE
))
3979 for (y
= y1
; y
< y2
; y
++) {
3983 xtermclear(0, y
, term
.col
, y
);
3986 specs
= term
.specbuf
;
3987 numspecs
= xmakeglyphfontspecs(specs
, &term
.line
[y
][x1
], x2
- x1
, x1
, y
);
3990 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
3991 new = term
.line
[y
][x
];
3992 if (new.mode
== ATTR_WDUMMY
)
3994 if (ena_sel
&& selected(x
, y
))
3995 new.mode
^= ATTR_REVERSE
;
3996 if (i
> 0 && ATTRCMP(base
, new)) {
3997 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
4009 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
4021 visibility(XEvent
*ev
)
4023 XVisibilityEvent
*e
= &ev
->xvisibility
;
4025 MODBIT(xw
.state
, e
->state
!= VisibilityFullyObscured
, WIN_VISIBLE
);
4031 xw
.state
&= ~WIN_VISIBLE
;
4035 xsetpointermotion(int set
)
4037 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
4038 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
4042 xseturgency(int add
)
4044 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
4046 MODBIT(h
->flags
, add
, XUrgencyHint
);
4047 XSetWMHints(xw
.dpy
, xw
.win
, h
);
4054 XFocusChangeEvent
*e
= &ev
->xfocus
;
4056 if (e
->mode
== NotifyGrab
)
4059 if (ev
->type
== FocusIn
) {
4060 XSetICFocus(xw
.xic
);
4061 xw
.state
|= WIN_FOCUSED
;
4063 if (IS_SET(MODE_FOCUS
))
4064 ttywrite("\033[I", 3);
4066 XUnsetICFocus(xw
.xic
);
4067 xw
.state
&= ~WIN_FOCUSED
;
4068 if (IS_SET(MODE_FOCUS
))
4069 ttywrite("\033[O", 3);
4074 match(uint mask
, uint state
)
4076 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
4080 numlock(const Arg
*dummy
)
4086 kmap(KeySym k
, uint state
)
4091 /* Check for mapped keys out of X11 function keys. */
4092 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
4093 if (mappedkeys
[i
] == k
)
4096 if (i
== LEN(mappedkeys
)) {
4097 if ((k
& 0xFFFF) < 0xFD00)
4101 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
4105 if (!match(kp
->mask
, state
))
4108 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
4110 if (term
.numlock
&& kp
->appkey
== 2)
4113 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
4116 if (IS_SET(MODE_CRLF
) ? kp
->crlf
< 0 : kp
->crlf
> 0)
4128 XKeyEvent
*e
= &ev
->xkey
;
4130 char buf
[32], *customkey
;
4136 if (IS_SET(MODE_KBDLOCK
))
4139 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
4141 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
4142 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
4143 bp
->func(&(bp
->arg
));
4148 /* 2. custom keys from config.h */
4149 if ((customkey
= kmap(ksym
, e
->state
))) {
4150 ttysend(customkey
, strlen(customkey
));
4154 /* 3. composed string from input method */
4157 if (len
== 1 && e
->state
& Mod1Mask
) {
4158 if (IS_SET(MODE_8BIT
)) {
4161 len
= utf8encode(c
, buf
);
4178 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
4180 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
4181 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
4182 xw
.state
|= WIN_FOCUSED
;
4184 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
4185 xw
.state
&= ~WIN_FOCUSED
;
4187 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
4188 /* Send SIGHUP to shell */
4195 cresize(int width
, int height
)
4204 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
4205 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
4215 if (e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
4218 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
4225 int w
= xw
.w
, h
= xw
.h
;
4227 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
4228 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
4231 /* Waiting for window mapping */
4233 XNextEvent(xw
.dpy
, &ev
);
4235 * This XFilterEvent call is required because of XOpenIM. It
4236 * does filter out the key event and some client message for
4237 * the input method too.
4239 if (XFilterEvent(&ev
, None
))
4241 if (ev
.type
== ConfigureNotify
) {
4242 w
= ev
.xconfigure
.width
;
4243 h
= ev
.xconfigure
.height
;
4245 } while (ev
.type
!= MapNotify
);
4250 clock_gettime(CLOCK_MONOTONIC
, &last
);
4253 for (xev
= actionfps
;;) {
4255 FD_SET(cmdfd
, &rfd
);
4258 if (pselect(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
4261 die("select failed: %s\n", strerror(errno
));
4263 if (FD_ISSET(cmdfd
, &rfd
)) {
4266 blinkset
= tattrset(ATTR_BLINK
);
4268 MODBIT(term
.mode
, 0, MODE_BLINK
);
4272 if (FD_ISSET(xfd
, &rfd
))
4275 clock_gettime(CLOCK_MONOTONIC
, &now
);
4276 drawtimeout
.tv_sec
= 0;
4277 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
4281 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
4282 tsetdirtattr(ATTR_BLINK
);
4283 term
.mode
^= MODE_BLINK
;
4287 deltatime
= TIMEDIFF(now
, last
);
4288 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
4294 while (XPending(xw
.dpy
)) {
4295 XNextEvent(xw
.dpy
, &ev
);
4296 if (XFilterEvent(&ev
, None
))
4298 if (handler
[ev
.type
])
4299 (handler
[ev
.type
])(&ev
);
4305 if (xev
&& !FD_ISSET(xfd
, &rfd
))
4307 if (!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
4309 if (TIMEDIFF(now
, lastblink
) \
4311 drawtimeout
.tv_nsec
= 1000;
4313 drawtimeout
.tv_nsec
= (1E6
* \
4318 drawtimeout
.tv_sec
= \
4319 drawtimeout
.tv_nsec
/ 1E9
;
4320 drawtimeout
.tv_nsec
%= (long)1E9
;
4332 die("%s " VERSION
" (c) 2010-2016 st engineers\n"
4333 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]\n"
4334 " [-i] [-t title] [-T title] [-w windowid] [-e command ...]"
4336 " st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]\n"
4337 " [-i] [-t title] [-T title] [-w windowid] -l line"
4338 " [stty_args ...]\n",
4343 main(int argc
, char *argv
[])
4345 uint cols
= 80, rows
= 24;
4349 xw
.cursor
= cursorshape
;
4356 opt_class
= EARGF(usage());
4363 opt_font
= EARGF(usage());
4366 xw
.gm
= XParseGeometry(EARGF(usage()),
4367 &xw
.l
, &xw
.t
, &cols
, &rows
);
4373 opt_io
= EARGF(usage());
4376 opt_line
= EARGF(usage());
4380 opt_title
= EARGF(usage());
4383 opt_embed
= EARGF(usage());
4392 /* eat all remaining arguments */
4394 if (!opt_title
&& !opt_line
)
4395 opt_title
= basename(xstrdup(argv
[0]));
4397 setlocale(LC_CTYPE
, "");
4398 XSetLocaleModifiers("");
4399 tnew(MAX(cols
, 1), MAX(rows
, 1));