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_class
= NULL
;
528 static char *opt_embed
= NULL
;
529 static char *opt_font
= NULL
;
530 static char *opt_io
= NULL
;
531 static char *opt_line
= NULL
;
532 static char *opt_name
= NULL
;
533 static char *opt_title
= NULL
;
534 static int oldbutton
= 3; /* button event on startup: 3 = release */
536 static char *usedfont
= NULL
;
537 static double usedfontsize
= 0;
538 static double defaultfontsize
= 0;
540 static uchar utfbyte
[UTF_SIZ
+ 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
541 static uchar utfmask
[UTF_SIZ
+ 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
542 static Rune utfmin
[UTF_SIZ
+ 1] = { 0, 0, 0x80, 0x800, 0x10000};
543 static Rune utfmax
[UTF_SIZ
+ 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
545 /* Font Ring Cache */
559 /* Fontcache is an array now. A new font will be appended to the array. */
560 static Fontcache frc
[16];
561 static int frclen
= 0;
564 xwrite(int fd
, const char *s
, size_t len
)
570 r
= write(fd
, s
, len
);
583 void *p
= malloc(len
);
586 die("Out of memory\n");
592 xrealloc(void *p
, size_t len
)
594 if ((p
= realloc(p
, len
)) == NULL
)
595 die("Out of memory\n");
603 if ((s
= strdup(s
)) == NULL
)
604 die("Out of memory\n");
610 utf8decode(char *c
, Rune
*u
, size_t clen
)
612 size_t i
, j
, len
, type
;
618 udecoded
= utf8decodebyte(c
[0], &len
);
619 if (!BETWEEN(len
, 1, UTF_SIZ
))
621 for (i
= 1, j
= 1; i
< clen
&& j
< len
; ++i
, ++j
) {
622 udecoded
= (udecoded
<< 6) | utf8decodebyte(c
[i
], &type
);
629 utf8validate(u
, len
);
635 utf8decodebyte(char c
, size_t *i
)
637 for (*i
= 0; *i
< LEN(utfmask
); ++(*i
))
638 if (((uchar
)c
& utfmask
[*i
]) == utfbyte
[*i
])
639 return (uchar
)c
& ~utfmask
[*i
];
645 utf8encode(Rune u
, char *c
)
649 len
= utf8validate(&u
, 0);
653 for (i
= len
- 1; i
!= 0; --i
) {
654 c
[i
] = utf8encodebyte(u
, 0);
657 c
[0] = utf8encodebyte(u
, len
);
663 utf8encodebyte(Rune u
, size_t i
)
665 return utfbyte
[i
] | (u
& ~utfmask
[i
]);
669 utf8strchr(char *s
, Rune u
)
675 for (i
= 0, j
= 0; i
< len
; i
+= j
) {
676 if (!(j
= utf8decode(&s
[i
], &r
, len
- i
)))
686 utf8validate(Rune
*u
, size_t i
)
688 if (!BETWEEN(*u
, utfmin
[i
], utfmax
[i
]) || BETWEEN(*u
, 0xD800, 0xDFFF))
690 for (i
= 1; *u
> utfmax
[i
]; ++i
)
699 clock_gettime(CLOCK_MONOTONIC
, &sel
.tclick1
);
700 clock_gettime(CLOCK_MONOTONIC
, &sel
.tclick2
);
705 sel
.clipboard
= NULL
;
706 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
707 if (sel
.xtarget
== None
)
708 sel
.xtarget
= XA_STRING
;
717 return LIMIT(x
, 0, term
.col
-1);
726 return LIMIT(y
, 0, term
.row
-1);
734 if (term
.line
[y
][i
- 1].mode
& ATTR_WRAP
)
737 while (i
> 0 && term
.line
[y
][i
- 1].u
== ' ')
748 if (sel
.type
== SEL_REGULAR
&& sel
.ob
.y
!= sel
.oe
.y
) {
749 sel
.nb
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.ob
.x
: sel
.oe
.x
;
750 sel
.ne
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.oe
.x
: sel
.ob
.x
;
752 sel
.nb
.x
= MIN(sel
.ob
.x
, sel
.oe
.x
);
753 sel
.ne
.x
= MAX(sel
.ob
.x
, sel
.oe
.x
);
755 sel
.nb
.y
= MIN(sel
.ob
.y
, sel
.oe
.y
);
756 sel
.ne
.y
= MAX(sel
.ob
.y
, sel
.oe
.y
);
758 selsnap(&sel
.nb
.x
, &sel
.nb
.y
, -1);
759 selsnap(&sel
.ne
.x
, &sel
.ne
.y
, +1);
761 /* expand selection over line breaks */
762 if (sel
.type
== SEL_RECTANGULAR
)
764 i
= tlinelen(sel
.nb
.y
);
767 if (tlinelen(sel
.ne
.y
) <= sel
.ne
.x
)
768 sel
.ne
.x
= term
.col
- 1;
772 selected(int x
, int y
)
774 if (sel
.mode
== SEL_EMPTY
)
777 if (sel
.type
== SEL_RECTANGULAR
)
778 return BETWEEN(y
, sel
.nb
.y
, sel
.ne
.y
)
779 && BETWEEN(x
, sel
.nb
.x
, sel
.ne
.x
);
781 return BETWEEN(y
, sel
.nb
.y
, sel
.ne
.y
)
782 && (y
!= sel
.nb
.y
|| x
>= sel
.nb
.x
)
783 && (y
!= sel
.ne
.y
|| x
<= sel
.ne
.x
);
787 selsnap(int *x
, int *y
, int direction
)
789 int newx
, newy
, xt
, yt
;
790 int delim
, prevdelim
;
796 * Snap around if the word wraps around at the end or
797 * beginning of a line.
799 prevgp
= &term
.line
[*y
][*x
];
800 prevdelim
= ISDELIM(prevgp
->u
);
802 newx
= *x
+ direction
;
804 if (!BETWEEN(newx
, 0, term
.col
- 1)) {
806 newx
= (newx
+ term
.col
) % term
.col
;
807 if (!BETWEEN(newy
, 0, term
.row
- 1))
813 yt
= newy
, xt
= newx
;
814 if (!(term
.line
[yt
][xt
].mode
& ATTR_WRAP
))
818 if (newx
>= tlinelen(newy
))
821 gp
= &term
.line
[newy
][newx
];
822 delim
= ISDELIM(gp
->u
);
823 if (!(gp
->mode
& ATTR_WDUMMY
) && (delim
!= prevdelim
824 || (delim
&& gp
->u
!= prevgp
->u
)))
835 * Snap around if the the previous line or the current one
836 * has set ATTR_WRAP at its end. Then the whole next or
837 * previous line will be selected.
839 *x
= (direction
< 0) ? 0 : term
.col
- 1;
841 for (; *y
> 0; *y
+= direction
) {
842 if (!(term
.line
[*y
-1][term
.col
-1].mode
847 } else if (direction
> 0) {
848 for (; *y
< term
.row
-1; *y
+= direction
) {
849 if (!(term
.line
[*y
][term
.col
-1].mode
860 getbuttoninfo(XEvent
*e
)
863 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forceselmod
);
865 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
867 sel
.oe
.x
= x2col(e
->xbutton
.x
);
868 sel
.oe
.y
= y2row(e
->xbutton
.y
);
871 sel
.type
= SEL_REGULAR
;
872 for (type
= 1; type
< LEN(selmasks
); ++type
) {
873 if (match(selmasks
[type
], state
)) {
881 mousereport(XEvent
*e
)
883 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
884 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
890 if (e
->xbutton
.type
== MotionNotify
) {
891 if (x
== ox
&& y
== oy
)
893 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
895 /* MOUSE_MOTION: no reporting if no button is pressed */
896 if (IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
899 button
= oldbutton
+ 32;
903 if (!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
910 if (e
->xbutton
.type
== ButtonPress
) {
914 } else if (e
->xbutton
.type
== ButtonRelease
) {
916 /* MODE_MOUSEX10: no button release reporting */
917 if (IS_SET(MODE_MOUSEX10
))
919 if (button
== 64 || button
== 65)
924 if (!IS_SET(MODE_MOUSEX10
)) {
925 button
+= ((state
& ShiftMask
) ? 4 : 0)
926 + ((state
& Mod4Mask
) ? 8 : 0)
927 + ((state
& ControlMask
) ? 16 : 0);
930 if (IS_SET(MODE_MOUSESGR
)) {
931 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
933 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
934 } else if (x
< 223 && y
< 223) {
935 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
936 32+button
, 32+x
+1, 32+y
+1);
950 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
955 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
956 if (e
->xbutton
.button
== ms
->b
957 && match(ms
->mask
, e
->xbutton
.state
)) {
958 ttysend(ms
->s
, strlen(ms
->s
));
963 if (e
->xbutton
.button
== Button1
) {
964 clock_gettime(CLOCK_MONOTONIC
, &now
);
966 /* Clear previous selection, logically and visually. */
968 sel
.mode
= SEL_EMPTY
;
969 sel
.type
= SEL_REGULAR
;
970 sel
.oe
.x
= sel
.ob
.x
= x2col(e
->xbutton
.x
);
971 sel
.oe
.y
= sel
.ob
.y
= y2row(e
->xbutton
.y
);
974 * If the user clicks below predefined timeouts specific
975 * snapping behaviour is exposed.
977 if (TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
978 sel
.snap
= SNAP_LINE
;
979 } else if (TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
980 sel
.snap
= SNAP_WORD
;
987 sel
.mode
= SEL_READY
;
988 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
989 sel
.tclick2
= sel
.tclick1
;
998 int y
, bufsize
, lastx
, linelen
;
1004 bufsize
= (term
.col
+1) * (sel
.ne
.y
-sel
.nb
.y
+1) * UTF_SIZ
;
1005 ptr
= str
= xmalloc(bufsize
);
1007 /* append every set & selected glyph to the selection */
1008 for (y
= sel
.nb
.y
; y
<= sel
.ne
.y
; y
++) {
1009 if ((linelen
= tlinelen(y
)) == 0) {
1014 if (sel
.type
== SEL_RECTANGULAR
) {
1015 gp
= &term
.line
[y
][sel
.nb
.x
];
1018 gp
= &term
.line
[y
][sel
.nb
.y
== y
? sel
.nb
.x
: 0];
1019 lastx
= (sel
.ne
.y
== y
) ? sel
.ne
.x
: term
.col
-1;
1021 last
= &term
.line
[y
][MIN(lastx
, linelen
-1)];
1022 while (last
>= gp
&& last
->u
== ' ')
1025 for ( ; gp
<= last
; ++gp
) {
1026 if (gp
->mode
& ATTR_WDUMMY
)
1029 ptr
+= utf8encode(gp
->u
, ptr
);
1033 * Copy and pasting of line endings is inconsistent
1034 * in the inconsistent terminal and GUI world.
1035 * The best solution seems like to produce '\n' when
1036 * something is copied from st and convert '\n' to
1037 * '\r', when something to be pasted is received by
1039 * FIXME: Fix the computer world.
1041 if ((y
< sel
.ne
.y
|| lastx
>= linelen
) && !(last
->mode
& ATTR_WRAP
))
1051 xsetsel(getsel(), t
);
1055 propnotify(XEvent
*e
)
1057 XPropertyEvent
*xpev
;
1058 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1060 xpev
= &e
->xproperty
;
1061 if (xpev
->state
== PropertyNewValue
&&
1062 (xpev
->atom
== XA_PRIMARY
||
1063 xpev
->atom
== clipboard
)) {
1069 selnotify(XEvent
*e
)
1071 ulong nitems
, ofs
, rem
;
1073 uchar
*data
, *last
, *repl
;
1074 Atom type
, incratom
, property
;
1076 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
1079 if (e
->type
== SelectionNotify
) {
1080 property
= e
->xselection
.property
;
1081 } else if(e
->type
== PropertyNotify
) {
1082 property
= e
->xproperty
.atom
;
1086 if (property
== None
)
1090 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
1091 BUFSIZ
/4, False
, AnyPropertyType
,
1092 &type
, &format
, &nitems
, &rem
,
1094 fprintf(stderr
, "Clipboard allocation failed\n");
1098 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
1100 * If there is some PropertyNotify with no data, then
1101 * this is the signal of the selection owner that all
1102 * data has been transferred. We won't need to receive
1103 * PropertyNotify events anymore.
1105 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
1106 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
1110 if (type
== incratom
) {
1112 * Activate the PropertyNotify events so we receive
1113 * when the selection owner does send us the next
1116 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
1117 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
1121 * Deleting the property is the transfer start signal.
1123 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
1128 * As seen in getsel:
1129 * Line endings are inconsistent in the terminal and GUI world
1130 * copy and pasting. When receiving some selection data,
1131 * replace all '\n' with '\r'.
1132 * FIXME: Fix the computer world.
1135 last
= data
+ nitems
* format
/ 8;
1136 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
1140 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
1141 ttywrite("\033[200~", 6);
1142 ttysend((char *)data
, nitems
* format
/ 8);
1143 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
1144 ttywrite("\033[201~", 6);
1146 /* number of 32-bit chunks returned */
1147 ofs
+= nitems
* format
/ 32;
1151 * Deleting the property again tells the selection owner to send the
1152 * next data chunk in the property.
1154 if (e
->type
== PropertyNotify
)
1155 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
1159 selpaste(const Arg
*dummy
)
1161 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1162 xw
.win
, CurrentTime
);
1166 clipcopy(const Arg
*dummy
)
1170 if (sel
.clipboard
!= NULL
)
1171 free(sel
.clipboard
);
1173 if (sel
.primary
!= NULL
) {
1174 sel
.clipboard
= xstrdup(sel
.primary
);
1175 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1176 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1181 clippaste(const Arg
*dummy
)
1185 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1186 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, clipboard
,
1187 xw
.win
, CurrentTime
);
1195 sel
.mode
= SEL_IDLE
;
1197 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1201 selrequest(XEvent
*e
)
1203 XSelectionRequestEvent
*xsre
;
1204 XSelectionEvent xev
;
1205 Atom xa_targets
, string
, clipboard
;
1208 xsre
= (XSelectionRequestEvent
*) e
;
1209 xev
.type
= SelectionNotify
;
1210 xev
.requestor
= xsre
->requestor
;
1211 xev
.selection
= xsre
->selection
;
1212 xev
.target
= xsre
->target
;
1213 xev
.time
= xsre
->time
;
1214 if (xsre
->property
== None
)
1215 xsre
->property
= xsre
->target
;
1218 xev
.property
= None
;
1220 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1221 if (xsre
->target
== xa_targets
) {
1222 /* respond with the supported type */
1223 string
= sel
.xtarget
;
1224 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1225 XA_ATOM
, 32, PropModeReplace
,
1226 (uchar
*) &string
, 1);
1227 xev
.property
= xsre
->property
;
1228 } else if (xsre
->target
== sel
.xtarget
|| xsre
->target
== XA_STRING
) {
1230 * xith XA_STRING non ascii characters may be incorrect in the
1231 * requestor. It is not our problem, use utf8.
1233 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1234 if (xsre
->selection
== XA_PRIMARY
) {
1235 seltext
= sel
.primary
;
1236 } else if (xsre
->selection
== clipboard
) {
1237 seltext
= sel
.clipboard
;
1240 "Unhandled clipboard selection 0x%lx\n",
1244 if (seltext
!= NULL
) {
1245 XChangeProperty(xsre
->display
, xsre
->requestor
,
1246 xsre
->property
, xsre
->target
,
1248 (uchar
*)seltext
, strlen(seltext
));
1249 xev
.property
= xsre
->property
;
1253 /* all done, send a notification to the listener */
1254 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
1255 fprintf(stderr
, "Error sending SelectionNotify event\n");
1259 xsetsel(char *str
, Time t
)
1264 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
1265 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
1272 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
1277 if (e
->xbutton
.button
== Button2
) {
1279 } else if (e
->xbutton
.button
== Button1
) {
1280 if (sel
.mode
== SEL_READY
) {
1282 selcopy(e
->xbutton
.time
);
1285 sel
.mode
= SEL_IDLE
;
1286 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1293 int oldey
, oldex
, oldsby
, oldsey
;
1295 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
1303 sel
.mode
= SEL_READY
;
1310 if (oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
1311 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
1315 die(const char *errstr
, ...)
1319 va_start(ap
, errstr
);
1320 vfprintf(stderr
, errstr
, ap
);
1328 char **args
, *sh
, *prog
;
1329 const struct passwd
*pw
;
1330 char buf
[sizeof(long) * 8 + 1];
1333 if ((pw
= getpwuid(getuid())) == NULL
) {
1335 die("getpwuid:%s\n", strerror(errno
));
1337 die("who are you?\n");
1340 if ((sh
= getenv("SHELL")) == NULL
)
1341 sh
= (pw
->pw_shell
[0]) ? pw
->pw_shell
: shell
;
1349 args
= (opt_cmd
) ? opt_cmd
: (char *[]) {prog
, NULL
};
1351 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1353 unsetenv("COLUMNS");
1355 unsetenv("TERMCAP");
1356 setenv("LOGNAME", pw
->pw_name
, 1);
1357 setenv("USER", pw
->pw_name
, 1);
1358 setenv("SHELL", sh
, 1);
1359 setenv("HOME", pw
->pw_dir
, 1);
1360 setenv("TERM", termname
, 1);
1361 setenv("WINDOWID", buf
, 1);
1363 signal(SIGCHLD
, SIG_DFL
);
1364 signal(SIGHUP
, SIG_DFL
);
1365 signal(SIGINT
, SIG_DFL
);
1366 signal(SIGQUIT
, SIG_DFL
);
1367 signal(SIGTERM
, SIG_DFL
);
1368 signal(SIGALRM
, SIG_DFL
);
1380 if ((p
= waitpid(pid
, &stat
, WNOHANG
)) < 0)
1381 die("Waiting for pid %hd failed: %s\n", pid
, strerror(errno
));
1386 if (!WIFEXITED(stat
) || WEXITSTATUS(stat
))
1387 die("child finished with error '%d'\n", stat
);
1395 char cmd
[_POSIX_ARG_MAX
], **p
, *q
, *s
;
1398 if ((n
= strlen(stty_args
)) > sizeof(cmd
)-1)
1399 die("incorrect stty parameters\n");
1400 memcpy(cmd
, stty_args
, n
);
1402 siz
= sizeof(cmd
) - n
;
1403 for (p
= opt_cmd
; p
&& (s
= *p
); ++p
) {
1404 if ((n
= strlen(s
)) > siz
-1)
1405 die("stty parameter length too long\n");
1407 q
= memcpy(q
, s
, n
);
1412 if (system(cmd
) != 0)
1413 perror("Couldn't call stty");
1420 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1423 term
.mode
|= MODE_PRINT
;
1424 iofd
= (!strcmp(opt_io
, "-")) ?
1425 1 : open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1427 fprintf(stderr
, "Error opening %s:%s\n",
1428 opt_io
, strerror(errno
));
1433 if ((cmdfd
= open(opt_line
, O_RDWR
)) < 0)
1434 die("open line failed: %s\n", strerror(errno
));
1440 /* seems to work fine on linux, openbsd and freebsd */
1441 if (openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1442 die("openpty failed: %s\n", strerror(errno
));
1444 switch (pid
= fork()) {
1446 die("fork failed\n");
1450 setsid(); /* create a new process group */
1454 if (ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1455 die("ioctl TIOCSCTTY failed: %s\n", strerror(errno
));
1463 signal(SIGCHLD
, sigchld
);
1471 static char buf
[BUFSIZ
];
1472 static int buflen
= 0;
1474 int charsize
; /* size of utf8 char in bytes */
1478 /* append read bytes to unprocessed bytes */
1479 if ((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1480 die("Couldn't read from shell: %s\n", strerror(errno
));
1482 /* process every complete utf8 char */
1485 while ((charsize
= utf8decode(ptr
, &unicodep
, buflen
))) {
1491 /* keep any uncomplete utf8 char for the next call */
1492 memmove(buf
, ptr
, buflen
);
1498 ttywrite(const char *s
, size_t n
)
1505 * Remember that we are using a pty, which might be a modem line.
1506 * Writing too much will clog the line. That's why we are doing this
1508 * FIXME: Migrate the world to Plan 9.
1513 FD_SET(cmdfd
, &wfd
);
1514 FD_SET(cmdfd
, &rfd
);
1516 /* Check if we can write. */
1517 if (pselect(cmdfd
+1, &rfd
, &wfd
, NULL
, NULL
, NULL
) < 0) {
1520 die("select failed: %s\n", strerror(errno
));
1522 if (FD_ISSET(cmdfd
, &wfd
)) {
1524 * Only write the bytes written by ttywrite() or the
1525 * default of 256. This seems to be a reasonable value
1526 * for a serial line. Bigger values might clog the I/O.
1528 if ((r
= write(cmdfd
, s
, (n
< lim
)? n
: lim
)) < 0)
1532 * We weren't able to write out everything.
1533 * This means the buffer is getting full
1541 /* All bytes have been written. */
1545 if (FD_ISSET(cmdfd
, &rfd
))
1551 die("write error on tty: %s\n", strerror(errno
));
1555 ttysend(char *s
, size_t n
)
1561 if (IS_SET(MODE_ECHO
))
1562 while ((len
= utf8decode(s
, &u
, n
)) > 0) {
1574 w
.ws_row
= term
.row
;
1575 w
.ws_col
= term
.col
;
1576 w
.ws_xpixel
= xw
.tw
;
1577 w
.ws_ypixel
= xw
.th
;
1578 if (ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1579 fprintf(stderr
, "Couldn't set window size: %s\n", strerror(errno
));
1587 for (i
= 0; i
< term
.row
-1; i
++) {
1588 for (j
= 0; j
< term
.col
-1; j
++) {
1589 if (term
.line
[i
][j
].mode
& attr
)
1598 tsetdirt(int top
, int bot
)
1602 LIMIT(top
, 0, term
.row
-1);
1603 LIMIT(bot
, 0, term
.row
-1);
1605 for (i
= top
; i
<= bot
; i
++)
1610 tsetdirtattr(int attr
)
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
) {
1627 tsetdirt(0, term
.row
-1);
1633 static TCursor c
[2];
1634 int alt
= IS_SET(MODE_ALTSCREEN
);
1636 if (mode
== CURSOR_SAVE
) {
1638 } else if (mode
== CURSOR_LOAD
) {
1640 tmoveto(c
[alt
].x
, c
[alt
].y
);
1649 term
.c
= (TCursor
){{
1653 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1655 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1656 for (i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1659 term
.bot
= term
.row
- 1;
1660 term
.mode
= MODE_WRAP
;
1661 memset(term
.trantbl
, CS_USA
, sizeof(term
.trantbl
));
1664 for (i
= 0; i
< 2; i
++) {
1666 tcursor(CURSOR_SAVE
);
1667 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1673 tnew(int col
, int row
)
1675 term
= (Term
){ .c
= { .attr
= { .fg
= defaultfg
, .bg
= defaultbg
} } };
1685 Line
*tmp
= term
.line
;
1687 term
.line
= term
.alt
;
1689 term
.mode
^= MODE_ALTSCREEN
;
1694 tscrolldown(int orig
, int n
)
1699 LIMIT(n
, 0, term
.bot
-orig
+1);
1701 tsetdirt(orig
, term
.bot
-n
);
1702 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1704 for (i
= term
.bot
; i
>= orig
+n
; i
--) {
1705 temp
= term
.line
[i
];
1706 term
.line
[i
] = term
.line
[i
-n
];
1707 term
.line
[i
-n
] = temp
;
1714 tscrollup(int orig
, int n
)
1719 LIMIT(n
, 0, term
.bot
-orig
+1);
1721 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1722 tsetdirt(orig
+n
, term
.bot
);
1724 for (i
= orig
; i
<= term
.bot
-n
; i
++) {
1725 temp
= term
.line
[i
];
1726 term
.line
[i
] = term
.line
[i
+n
];
1727 term
.line
[i
+n
] = temp
;
1730 selscroll(orig
, -n
);
1734 selscroll(int orig
, int n
)
1739 if (BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1740 if ((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1744 if (sel
.type
== SEL_RECTANGULAR
) {
1745 if (sel
.ob
.y
< term
.top
)
1746 sel
.ob
.y
= term
.top
;
1747 if (sel
.oe
.y
> term
.bot
)
1748 sel
.oe
.y
= term
.bot
;
1750 if (sel
.ob
.y
< term
.top
) {
1751 sel
.ob
.y
= term
.top
;
1754 if (sel
.oe
.y
> term
.bot
) {
1755 sel
.oe
.y
= term
.bot
;
1756 sel
.oe
.x
= term
.col
;
1764 tnewline(int first_col
)
1768 if (y
== term
.bot
) {
1769 tscrollup(term
.top
, 1);
1773 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1779 char *p
= csiescseq
.buf
, *np
;
1788 csiescseq
.buf
[csiescseq
.len
] = '\0';
1789 while (p
< csiescseq
.buf
+csiescseq
.len
) {
1791 v
= strtol(p
, &np
, 10);
1794 if (v
== LONG_MAX
|| v
== LONG_MIN
)
1796 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1798 if (*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1802 csiescseq
.mode
[0] = *p
++;
1803 csiescseq
.mode
[1] = (p
< csiescseq
.buf
+csiescseq
.len
) ? *p
: '\0';
1806 /* for absolute user moves, when decom is set */
1808 tmoveato(int x
, int y
)
1810 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1814 tmoveto(int x
, int y
)
1818 if (term
.c
.state
& CURSOR_ORIGIN
) {
1823 maxy
= term
.row
- 1;
1825 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1826 term
.c
.x
= LIMIT(x
, 0, term
.col
-1);
1827 term
.c
.y
= LIMIT(y
, miny
, maxy
);
1831 tsetchar(Rune u
, Glyph
*attr
, int x
, int y
)
1833 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1834 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1835 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1836 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1837 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1838 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1839 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1840 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1841 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1845 * The table is proudly stolen from rxvt.
1847 if (term
.trantbl
[term
.charset
] == CS_GRAPHIC0
&&
1848 BETWEEN(u
, 0x41, 0x7e) && vt100_0
[u
- 0x41])
1849 utf8decode(vt100_0
[u
- 0x41], &u
, UTF_SIZ
);
1851 if (term
.line
[y
][x
].mode
& ATTR_WIDE
) {
1852 if (x
+1 < term
.col
) {
1853 term
.line
[y
][x
+1].u
= ' ';
1854 term
.line
[y
][x
+1].mode
&= ~ATTR_WDUMMY
;
1856 } else if (term
.line
[y
][x
].mode
& ATTR_WDUMMY
) {
1857 term
.line
[y
][x
-1].u
= ' ';
1858 term
.line
[y
][x
-1].mode
&= ~ATTR_WIDE
;
1862 term
.line
[y
][x
] = *attr
;
1863 term
.line
[y
][x
].u
= u
;
1867 tclearregion(int x1
, int y1
, int x2
, int y2
)
1873 temp
= x1
, x1
= x2
, x2
= temp
;
1875 temp
= y1
, y1
= y2
, y2
= temp
;
1877 LIMIT(x1
, 0, term
.col
-1);
1878 LIMIT(x2
, 0, term
.col
-1);
1879 LIMIT(y1
, 0, term
.row
-1);
1880 LIMIT(y2
, 0, term
.row
-1);
1882 for (y
= y1
; y
<= y2
; y
++) {
1884 for (x
= x1
; x
<= x2
; x
++) {
1885 gp
= &term
.line
[y
][x
];
1888 gp
->fg
= term
.c
.attr
.fg
;
1889 gp
->bg
= term
.c
.attr
.bg
;
1902 LIMIT(n
, 0, term
.col
- term
.c
.x
);
1906 size
= term
.col
- src
;
1907 line
= term
.line
[term
.c
.y
];
1909 memmove(&line
[dst
], &line
[src
], size
* sizeof(Glyph
));
1910 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1919 LIMIT(n
, 0, term
.col
- term
.c
.x
);
1923 size
= term
.col
- dst
;
1924 line
= term
.line
[term
.c
.y
];
1926 memmove(&line
[dst
], &line
[src
], size
* sizeof(Glyph
));
1927 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1931 tinsertblankline(int n
)
1933 if (BETWEEN(term
.c
.y
, term
.top
, term
.bot
))
1934 tscrolldown(term
.c
.y
, n
);
1940 if (BETWEEN(term
.c
.y
, term
.top
, term
.bot
))
1941 tscrollup(term
.c
.y
, n
);
1945 tdefcolor(int *attr
, int *npar
, int l
)
1950 switch (attr
[*npar
+ 1]) {
1951 case 2: /* direct color in RGB space */
1952 if (*npar
+ 4 >= l
) {
1954 "erresc(38): Incorrect number of parameters (%d)\n",
1958 r
= attr
[*npar
+ 2];
1959 g
= attr
[*npar
+ 3];
1960 b
= attr
[*npar
+ 4];
1962 if (!BETWEEN(r
, 0, 255) || !BETWEEN(g
, 0, 255) || !BETWEEN(b
, 0, 255))
1963 fprintf(stderr
, "erresc: bad rgb color (%u,%u,%u)\n",
1966 idx
= TRUECOLOR(r
, g
, b
);
1968 case 5: /* indexed color */
1969 if (*npar
+ 2 >= l
) {
1971 "erresc(38): Incorrect number of parameters (%d)\n",
1976 if (!BETWEEN(attr
[*npar
], 0, 255))
1977 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[*npar
]);
1981 case 0: /* implemented defined (only foreground) */
1982 case 1: /* transparent */
1983 case 3: /* direct color in CMY space */
1984 case 4: /* direct color in CMYK space */
1987 "erresc(38): gfx attr %d unknown\n", attr
[*npar
]);
1995 tsetattr(int *attr
, int l
)
2000 for (i
= 0; i
< l
; i
++) {
2003 term
.c
.attr
.mode
&= ~(
2012 term
.c
.attr
.fg
= defaultfg
;
2013 term
.c
.attr
.bg
= defaultbg
;
2016 term
.c
.attr
.mode
|= ATTR_BOLD
;
2019 term
.c
.attr
.mode
|= ATTR_FAINT
;
2022 term
.c
.attr
.mode
|= ATTR_ITALIC
;
2025 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
2027 case 5: /* slow blink */
2029 case 6: /* rapid blink */
2030 term
.c
.attr
.mode
|= ATTR_BLINK
;
2033 term
.c
.attr
.mode
|= ATTR_REVERSE
;
2036 term
.c
.attr
.mode
|= ATTR_INVISIBLE
;
2039 term
.c
.attr
.mode
|= ATTR_STRUCK
;
2042 term
.c
.attr
.mode
&= ~(ATTR_BOLD
| ATTR_FAINT
);
2045 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
2048 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
2051 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
2054 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
2057 term
.c
.attr
.mode
&= ~ATTR_INVISIBLE
;
2060 term
.c
.attr
.mode
&= ~ATTR_STRUCK
;
2063 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
2064 term
.c
.attr
.fg
= idx
;
2067 term
.c
.attr
.fg
= defaultfg
;
2070 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
2071 term
.c
.attr
.bg
= idx
;
2074 term
.c
.attr
.bg
= defaultbg
;
2077 if (BETWEEN(attr
[i
], 30, 37)) {
2078 term
.c
.attr
.fg
= attr
[i
] - 30;
2079 } else if (BETWEEN(attr
[i
], 40, 47)) {
2080 term
.c
.attr
.bg
= attr
[i
] - 40;
2081 } else if (BETWEEN(attr
[i
], 90, 97)) {
2082 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
2083 } else if (BETWEEN(attr
[i
], 100, 107)) {
2084 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
2087 "erresc(default): gfx attr %d unknown\n",
2088 attr
[i
]), csidump();
2096 tsetscroll(int t
, int b
)
2100 LIMIT(t
, 0, term
.row
-1);
2101 LIMIT(b
, 0, term
.row
-1);
2112 tsetmode(int priv
, int set
, int *args
, int narg
)
2117 for (lim
= args
+ narg
; args
< lim
; ++args
) {
2120 case 1: /* DECCKM -- Cursor key */
2121 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
2123 case 5: /* DECSCNM -- Reverse video */
2125 MODBIT(term
.mode
, set
, MODE_REVERSE
);
2126 if (mode
!= term
.mode
)
2129 case 6: /* DECOM -- Origin */
2130 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
2133 case 7: /* DECAWM -- Auto wrap */
2134 MODBIT(term
.mode
, set
, MODE_WRAP
);
2136 case 0: /* Error (IGNORED) */
2137 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
2138 case 3: /* DECCOLM -- Column (IGNORED) */
2139 case 4: /* DECSCLM -- Scroll (IGNORED) */
2140 case 8: /* DECARM -- Auto repeat (IGNORED) */
2141 case 18: /* DECPFF -- Printer feed (IGNORED) */
2142 case 19: /* DECPEX -- Printer extent (IGNORED) */
2143 case 42: /* DECNRCM -- National characters (IGNORED) */
2144 case 12: /* att610 -- Start blinking cursor (IGNORED) */
2146 case 25: /* DECTCEM -- Text Cursor Enable Mode */
2147 MODBIT(term
.mode
, !set
, MODE_HIDE
);
2149 case 9: /* X10 mouse compatibility mode */
2150 xsetpointermotion(0);
2151 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2152 MODBIT(term
.mode
, set
, MODE_MOUSEX10
);
2154 case 1000: /* 1000: report button press */
2155 xsetpointermotion(0);
2156 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2157 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
2159 case 1002: /* 1002: report motion on button press */
2160 xsetpointermotion(0);
2161 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2162 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
2164 case 1003: /* 1003: enable all mouse motions */
2165 xsetpointermotion(set
);
2166 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2167 MODBIT(term
.mode
, set
, MODE_MOUSEMANY
);
2169 case 1004: /* 1004: send focus events to tty */
2170 MODBIT(term
.mode
, set
, MODE_FOCUS
);
2172 case 1006: /* 1006: extended reporting mode */
2173 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
2176 MODBIT(term
.mode
, set
, MODE_8BIT
);
2178 case 1049: /* swap screen & set/restore cursor as xterm */
2179 if (!allowaltscreen
)
2181 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
2183 case 47: /* swap screen */
2185 if (!allowaltscreen
)
2187 alt
= IS_SET(MODE_ALTSCREEN
);
2189 tclearregion(0, 0, term
.col
-1,
2192 if (set
^ alt
) /* set is always 1 or 0 */
2198 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
2200 case 2004: /* 2004: bracketed paste mode */
2201 MODBIT(term
.mode
, set
, MODE_BRCKTPASTE
);
2203 /* Not implemented mouse modes. See comments there. */
2204 case 1001: /* mouse highlight mode; can hang the
2205 terminal by design when implemented. */
2206 case 1005: /* UTF-8 mouse mode; will confuse
2207 applications not supporting UTF-8
2209 case 1015: /* urxvt mangled mouse mode; incompatible
2210 and can be mistaken for other control
2214 "erresc: unknown private set/reset mode %d\n",
2220 case 0: /* Error (IGNORED) */
2222 case 2: /* KAM -- keyboard action */
2223 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
2225 case 4: /* IRM -- Insertion-replacement */
2226 MODBIT(term
.mode
, set
, MODE_INSERT
);
2228 case 12: /* SRM -- Send/Receive */
2229 MODBIT(term
.mode
, !set
, MODE_ECHO
);
2231 case 20: /* LNM -- Linefeed/new line */
2232 MODBIT(term
.mode
, set
, MODE_CRLF
);
2236 "erresc: unknown set/reset mode %d\n",
2250 switch (csiescseq
.mode
[0]) {
2253 fprintf(stderr
, "erresc: unknown csi ");
2257 case '@': /* ICH -- Insert <n> blank char */
2258 DEFAULT(csiescseq
.arg
[0], 1);
2259 tinsertblank(csiescseq
.arg
[0]);
2261 case 'A': /* CUU -- Cursor <n> Up */
2262 DEFAULT(csiescseq
.arg
[0], 1);
2263 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
2265 case 'B': /* CUD -- Cursor <n> Down */
2266 case 'e': /* VPR --Cursor <n> Down */
2267 DEFAULT(csiescseq
.arg
[0], 1);
2268 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
2270 case 'i': /* MC -- Media Copy */
2271 switch (csiescseq
.arg
[0]) {
2276 tdumpline(term
.c
.y
);
2282 term
.mode
&= ~MODE_PRINT
;
2285 term
.mode
|= MODE_PRINT
;
2289 case 'c': /* DA -- Device Attributes */
2290 if (csiescseq
.arg
[0] == 0)
2291 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2293 case 'C': /* CUF -- Cursor <n> Forward */
2294 case 'a': /* HPR -- Cursor <n> Forward */
2295 DEFAULT(csiescseq
.arg
[0], 1);
2296 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
2298 case 'D': /* CUB -- Cursor <n> Backward */
2299 DEFAULT(csiescseq
.arg
[0], 1);
2300 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
2302 case 'E': /* CNL -- Cursor <n> Down and first col */
2303 DEFAULT(csiescseq
.arg
[0], 1);
2304 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
2306 case 'F': /* CPL -- Cursor <n> Up and first col */
2307 DEFAULT(csiescseq
.arg
[0], 1);
2308 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
2310 case 'g': /* TBC -- Tabulation clear */
2311 switch (csiescseq
.arg
[0]) {
2312 case 0: /* clear current tab stop */
2313 term
.tabs
[term
.c
.x
] = 0;
2315 case 3: /* clear all the tabs */
2316 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
2322 case 'G': /* CHA -- Move to <col> */
2324 DEFAULT(csiescseq
.arg
[0], 1);
2325 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
2327 case 'H': /* CUP -- Move to <row> <col> */
2329 DEFAULT(csiescseq
.arg
[0], 1);
2330 DEFAULT(csiescseq
.arg
[1], 1);
2331 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
2333 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
2334 DEFAULT(csiescseq
.arg
[0], 1);
2335 tputtab(csiescseq
.arg
[0]);
2337 case 'J': /* ED -- Clear screen */
2339 switch (csiescseq
.arg
[0]) {
2341 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
2342 if (term
.c
.y
< term
.row
-1) {
2343 tclearregion(0, term
.c
.y
+1, term
.col
-1,
2349 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
2350 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2353 tclearregion(0, 0, term
.col
-1, term
.row
-1);
2359 case 'K': /* EL -- Clear line */
2360 switch (csiescseq
.arg
[0]) {
2362 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
2366 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2369 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
2373 case 'S': /* SU -- Scroll <n> line up */
2374 DEFAULT(csiescseq
.arg
[0], 1);
2375 tscrollup(term
.top
, csiescseq
.arg
[0]);
2377 case 'T': /* SD -- Scroll <n> line down */
2378 DEFAULT(csiescseq
.arg
[0], 1);
2379 tscrolldown(term
.top
, csiescseq
.arg
[0]);
2381 case 'L': /* IL -- Insert <n> blank lines */
2382 DEFAULT(csiescseq
.arg
[0], 1);
2383 tinsertblankline(csiescseq
.arg
[0]);
2385 case 'l': /* RM -- Reset Mode */
2386 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
2388 case 'M': /* DL -- Delete <n> lines */
2389 DEFAULT(csiescseq
.arg
[0], 1);
2390 tdeleteline(csiescseq
.arg
[0]);
2392 case 'X': /* ECH -- Erase <n> char */
2393 DEFAULT(csiescseq
.arg
[0], 1);
2394 tclearregion(term
.c
.x
, term
.c
.y
,
2395 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
2397 case 'P': /* DCH -- Delete <n> char */
2398 DEFAULT(csiescseq
.arg
[0], 1);
2399 tdeletechar(csiescseq
.arg
[0]);
2401 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
2402 DEFAULT(csiescseq
.arg
[0], 1);
2403 tputtab(-csiescseq
.arg
[0]);
2405 case 'd': /* VPA -- Move to <row> */
2406 DEFAULT(csiescseq
.arg
[0], 1);
2407 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
2409 case 'h': /* SM -- Set terminal mode */
2410 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
2412 case 'm': /* SGR -- Terminal attribute (color) */
2413 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
2415 case 'n': /* DSR – Device Status Report (cursor position) */
2416 if (csiescseq
.arg
[0] == 6) {
2417 len
= snprintf(buf
, sizeof(buf
),"\033[%i;%iR",
2418 term
.c
.y
+1, term
.c
.x
+1);
2422 case 'r': /* DECSTBM -- Set Scrolling Region */
2423 if (csiescseq
.priv
) {
2426 DEFAULT(csiescseq
.arg
[0], 1);
2427 DEFAULT(csiescseq
.arg
[1], term
.row
);
2428 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
2432 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2433 tcursor(CURSOR_SAVE
);
2435 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2436 tcursor(CURSOR_LOAD
);
2439 switch (csiescseq
.mode
[1]) {
2440 case 'q': /* DECSCUSR -- Set Cursor Style */
2441 DEFAULT(csiescseq
.arg
[0], 1);
2442 if (!BETWEEN(csiescseq
.arg
[0], 0, 6)) {
2445 xw
.cursor
= csiescseq
.arg
[0];
2461 for (i
= 0; i
< csiescseq
.len
; i
++) {
2462 c
= csiescseq
.buf
[i
] & 0xff;
2465 } else if (c
== '\n') {
2467 } else if (c
== '\r') {
2469 } else if (c
== 0x1b) {
2472 printf("(%02x)", c
);
2481 memset(&csiescseq
, 0, sizeof(csiescseq
));
2490 term
.esc
&= ~(ESC_STR_END
|ESC_STR
);
2492 par
= (narg
= strescseq
.narg
) ? atoi(strescseq
.args
[0]) : 0;
2494 switch (strescseq
.type
) {
2495 case ']': /* OSC -- Operating System Command */
2501 xsettitle(strescseq
.args
[1]);
2503 case 4: /* color set */
2506 p
= strescseq
.args
[2];
2508 case 104: /* color reset, here p = NULL */
2509 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2510 if (xsetcolorname(j
, p
)) {
2511 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2514 * TODO if defaultbg color is changed, borders
2522 case 'k': /* old title set compatibility */
2523 xsettitle(strescseq
.args
[0]);
2525 case 'P': /* DCS -- Device Control String */
2526 case '_': /* APC -- Application Program Command */
2527 case '^': /* PM -- Privacy Message */
2531 fprintf(stderr
, "erresc: unknown str ");
2539 char *p
= strescseq
.buf
;
2542 strescseq
.buf
[strescseq
.len
] = '\0';
2547 while (strescseq
.narg
< STR_ARG_SIZ
) {
2548 strescseq
.args
[strescseq
.narg
++] = p
;
2549 while ((c
= *p
) != ';' && c
!= '\0')
2563 printf("ESC%c", strescseq
.type
);
2564 for (i
= 0; i
< strescseq
.len
; i
++) {
2565 c
= strescseq
.buf
[i
] & 0xff;
2568 } else if (isprint(c
)) {
2570 } else if (c
== '\n') {
2572 } else if (c
== '\r') {
2574 } else if (c
== 0x1b) {
2577 printf("(%02x)", c
);
2586 memset(&strescseq
, 0, sizeof(strescseq
));
2590 sendbreak(const Arg
*arg
)
2592 if (tcsendbreak(cmdfd
, 0))
2593 perror("Error sending break");
2597 tprinter(char *s
, size_t len
)
2599 if (iofd
!= -1 && xwrite(iofd
, s
, len
) < 0) {
2600 fprintf(stderr
, "Error writing in %s:%s\n",
2601 opt_io
, strerror(errno
));
2608 toggleprinter(const Arg
*arg
)
2610 term
.mode
^= MODE_PRINT
;
2614 printscreen(const Arg
*arg
)
2620 printsel(const Arg
*arg
)
2630 if ((ptr
= getsel())) {
2631 tprinter(ptr
, strlen(ptr
));
2642 bp
= &term
.line
[n
][0];
2643 end
= &bp
[MIN(tlinelen(n
), term
.col
) - 1];
2644 if (bp
!= end
|| bp
->u
!= ' ') {
2645 for ( ;bp
<= end
; ++bp
)
2646 tprinter(buf
, utf8encode(bp
->u
, buf
));
2656 for (i
= 0; i
< term
.row
; ++i
)
2666 while (x
< term
.col
&& n
--)
2667 for (++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2670 while (x
> 0 && n
++)
2671 for (--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2674 term
.c
.x
= LIMIT(x
, 0, term
.col
-1);
2680 if (ISCONTROL(u
)) { /* control code */
2685 } else if (u
!= '\n' && u
!= '\r' && u
!= '\t') {
2694 tdeftran(char ascii
)
2696 static char cs
[] = "0B";
2697 static int vcs
[] = {CS_GRAPHIC0
, CS_USA
};
2700 if ((p
= strchr(cs
, ascii
)) == NULL
) {
2701 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2703 term
.trantbl
[term
.icharset
] = vcs
[p
- cs
];
2712 if (c
== '8') { /* DEC screen alignment test. */
2713 for (x
= 0; x
< term
.col
; ++x
) {
2714 for (y
= 0; y
< term
.row
; ++y
)
2715 tsetchar('E', &term
.c
.attr
, x
, y
);
2721 tstrsequence(uchar c
)
2724 case 0x90: /* DCS -- Device Control String */
2727 case 0x9f: /* APC -- Application Program Command */
2730 case 0x9e: /* PM -- Privacy Message */
2733 case 0x9d: /* OSC -- Operating System Command */
2739 term
.esc
|= ESC_STR
;
2743 tcontrolcode(uchar ascii
)
2750 tmoveto(term
.c
.x
-1, term
.c
.y
);
2753 tmoveto(0, term
.c
.y
);
2758 /* go to first col if the mode is set */
2759 tnewline(IS_SET(MODE_CRLF
));
2761 case '\a': /* BEL */
2762 if (term
.esc
& ESC_STR_END
) {
2763 /* backwards compatibility to xterm */
2766 if (!(xw
.state
& WIN_FOCUSED
))
2769 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
2772 case '\033': /* ESC */
2774 term
.esc
&= ~(ESC_CSI
|ESC_ALTCHARSET
|ESC_TEST
);
2775 term
.esc
|= ESC_START
;
2777 case '\016': /* SO (LS1 -- Locking shift 1) */
2778 case '\017': /* SI (LS0 -- Locking shift 0) */
2779 term
.charset
= 1 - (ascii
- '\016');
2781 case '\032': /* SUB */
2782 tsetchar('?', &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2783 case '\030': /* CAN */
2786 case '\005': /* ENQ (IGNORED) */
2787 case '\000': /* NUL (IGNORED) */
2788 case '\021': /* XON (IGNORED) */
2789 case '\023': /* XOFF (IGNORED) */
2790 case 0177: /* DEL (IGNORED) */
2792 case 0x80: /* TODO: PAD */
2793 case 0x81: /* TODO: HOP */
2794 case 0x82: /* TODO: BPH */
2795 case 0x83: /* TODO: NBH */
2796 case 0x84: /* TODO: IND */
2798 case 0x85: /* NEL -- Next line */
2799 tnewline(1); /* always go to first col */
2801 case 0x86: /* TODO: SSA */
2802 case 0x87: /* TODO: ESA */
2804 case 0x88: /* HTS -- Horizontal tab stop */
2805 term
.tabs
[term
.c
.x
] = 1;
2807 case 0x89: /* TODO: HTJ */
2808 case 0x8a: /* TODO: VTS */
2809 case 0x8b: /* TODO: PLD */
2810 case 0x8c: /* TODO: PLU */
2811 case 0x8d: /* TODO: RI */
2812 case 0x8e: /* TODO: SS2 */
2813 case 0x8f: /* TODO: SS3 */
2814 case 0x91: /* TODO: PU1 */
2815 case 0x92: /* TODO: PU2 */
2816 case 0x93: /* TODO: STS */
2817 case 0x94: /* TODO: CCH */
2818 case 0x95: /* TODO: MW */
2819 case 0x96: /* TODO: SPA */
2820 case 0x97: /* TODO: EPA */
2821 case 0x98: /* TODO: SOS */
2822 case 0x99: /* TODO: SGCI */
2824 case 0x9a: /* DECID -- Identify Terminal */
2825 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2827 case 0x9b: /* TODO: CSI */
2828 case 0x9c: /* TODO: ST */
2830 case 0x90: /* DCS -- Device Control String */
2831 case 0x9d: /* OSC -- Operating System Command */
2832 case 0x9e: /* PM -- Privacy Message */
2833 case 0x9f: /* APC -- Application Program Command */
2834 tstrsequence(ascii
);
2837 /* only CAN, SUB, \a and C1 chars interrupt a sequence */
2838 term
.esc
&= ~(ESC_STR_END
|ESC_STR
);
2842 * returns 1 when the sequence is finished and it hasn't to read
2843 * more characters for this sequence, otherwise 0
2846 eschandle(uchar ascii
)
2850 term
.esc
|= ESC_CSI
;
2853 term
.esc
|= ESC_TEST
;
2855 case 'P': /* DCS -- Device Control String */
2856 case '_': /* APC -- Application Program Command */
2857 case '^': /* PM -- Privacy Message */
2858 case ']': /* OSC -- Operating System Command */
2859 case 'k': /* old title set compatibility */
2860 tstrsequence(ascii
);
2862 case 'n': /* LS2 -- Locking shift 2 */
2863 case 'o': /* LS3 -- Locking shift 3 */
2864 term
.charset
= 2 + (ascii
- 'n');
2866 case '(': /* GZD4 -- set primary charset G0 */
2867 case ')': /* G1D4 -- set secondary charset G1 */
2868 case '*': /* G2D4 -- set tertiary charset G2 */
2869 case '+': /* G3D4 -- set quaternary charset G3 */
2870 term
.icharset
= ascii
- '(';
2871 term
.esc
|= ESC_ALTCHARSET
;
2873 case 'D': /* IND -- Linefeed */
2874 if (term
.c
.y
== term
.bot
) {
2875 tscrollup(term
.top
, 1);
2877 tmoveto(term
.c
.x
, term
.c
.y
+1);
2880 case 'E': /* NEL -- Next line */
2881 tnewline(1); /* always go to first col */
2883 case 'H': /* HTS -- Horizontal tab stop */
2884 term
.tabs
[term
.c
.x
] = 1;
2886 case 'M': /* RI -- Reverse index */
2887 if (term
.c
.y
== term
.top
) {
2888 tscrolldown(term
.top
, 1);
2890 tmoveto(term
.c
.x
, term
.c
.y
-1);
2893 case 'Z': /* DECID -- Identify Terminal */
2894 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2896 case 'c': /* RIS -- Reset to inital state */
2901 case '=': /* DECPAM -- Application keypad */
2902 term
.mode
|= MODE_APPKEYPAD
;
2904 case '>': /* DECPNM -- Normal keypad */
2905 term
.mode
&= ~MODE_APPKEYPAD
;
2907 case '7': /* DECSC -- Save Cursor */
2908 tcursor(CURSOR_SAVE
);
2910 case '8': /* DECRC -- Restore Cursor */
2911 tcursor(CURSOR_LOAD
);
2913 case '\\': /* ST -- String Terminator */
2914 if (term
.esc
& ESC_STR_END
)
2918 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2919 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2933 control
= ISCONTROL(u
);
2934 len
= utf8encode(u
, c
);
2935 if (!control
&& (width
= wcwidth(u
)) == -1) {
2936 memcpy(c
, "\357\277\275", 4); /* UTF_INVALID */
2940 if (IS_SET(MODE_PRINT
))
2944 * STR sequence must be checked before anything else
2945 * because it uses all following characters until it
2946 * receives a ESC, a SUB, a ST or any other C1 control
2949 if (term
.esc
& ESC_STR
) {
2950 if (u
== '\a' || u
== 030 || u
== 032 || u
== 033 ||
2952 term
.esc
&= ~(ESC_START
|ESC_STR
);
2953 term
.esc
|= ESC_STR_END
;
2954 } else if (strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2955 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2956 strescseq
.len
+= len
;
2960 * Here is a bug in terminals. If the user never sends
2961 * some code to stop the str or esc command, then st
2962 * will stop responding. But this is better than
2963 * silently failing with unknown characters. At least
2964 * then users will report back.
2966 * In the case users ever get fixed, here is the code:
2977 * Actions of control codes must be performed as soon they arrive
2978 * because they can be embedded inside a control sequence, and
2979 * they must not cause conflicts with sequences.
2984 * control codes are not shown ever
2987 } else if (term
.esc
& ESC_START
) {
2988 if (term
.esc
& ESC_CSI
) {
2989 csiescseq
.buf
[csiescseq
.len
++] = u
;
2990 if (BETWEEN(u
, 0x40, 0x7E)
2991 || csiescseq
.len
>= \
2992 sizeof(csiescseq
.buf
)-1) {
2998 } else if (term
.esc
& ESC_ALTCHARSET
) {
3000 } else if (term
.esc
& ESC_TEST
) {
3005 /* sequence already finished */
3009 * All characters which form part of a sequence are not
3014 if (sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
3017 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
3018 if (IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
3019 gp
->mode
|= ATTR_WRAP
;
3021 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
3024 if (IS_SET(MODE_INSERT
) && term
.c
.x
+width
< term
.col
)
3025 memmove(gp
+width
, gp
, (term
.col
- term
.c
.x
- width
) * sizeof(Glyph
));
3027 if (term
.c
.x
+width
> term
.col
) {
3029 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
3032 tsetchar(u
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
3035 gp
->mode
|= ATTR_WIDE
;
3036 if (term
.c
.x
+1 < term
.col
) {
3038 gp
[1].mode
= ATTR_WDUMMY
;
3041 if (term
.c
.x
+width
< term
.col
) {
3042 tmoveto(term
.c
.x
+width
, term
.c
.y
);
3044 term
.c
.state
|= CURSOR_WRAPNEXT
;
3049 tresize(int col
, int row
)
3052 int minrow
= MIN(row
, term
.row
);
3053 int mincol
= MIN(col
, term
.col
);
3057 if (col
< 1 || row
< 1) {
3059 "tresize: error resizing to %dx%d\n", col
, row
);
3064 * slide screen to keep cursor where we expect it -
3065 * tscrollup would work here, but we can optimize to
3066 * memmove because we're freeing the earlier lines
3068 for (i
= 0; i
<= term
.c
.y
- row
; i
++) {
3072 /* ensure that both src and dst are not NULL */
3074 memmove(term
.line
, term
.line
+ i
, row
* sizeof(Line
));
3075 memmove(term
.alt
, term
.alt
+ i
, row
* sizeof(Line
));
3077 for (i
+= row
; i
< term
.row
; i
++) {
3082 /* resize to new width */
3083 term
.specbuf
= xrealloc(term
.specbuf
, col
* sizeof(XftGlyphFontSpec
));
3085 /* resize to new height */
3086 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
3087 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
3088 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
3089 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
3091 /* resize each row to new width, zero-pad if needed */
3092 for (i
= 0; i
< minrow
; i
++) {
3093 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
3094 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
3097 /* allocate any new rows */
3098 for (/* i == minrow */; i
< row
; i
++) {
3099 term
.line
[i
] = xmalloc(col
* sizeof(Glyph
));
3100 term
.alt
[i
] = xmalloc(col
* sizeof(Glyph
));
3102 if (col
> term
.col
) {
3103 bp
= term
.tabs
+ term
.col
;
3105 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
3106 while (--bp
> term
.tabs
&& !*bp
)
3108 for (bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
3111 /* update terminal size */
3114 /* reset scrolling region */
3115 tsetscroll(0, row
-1);
3116 /* make use of the LIMIT in tmoveto */
3117 tmoveto(term
.c
.x
, term
.c
.y
);
3118 /* Clearing both screens (it makes dirty all lines) */
3120 for (i
= 0; i
< 2; i
++) {
3121 if (mincol
< col
&& 0 < minrow
) {
3122 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
3124 if (0 < col
&& minrow
< row
) {
3125 tclearregion(0, minrow
, col
- 1, row
- 1);
3128 tcursor(CURSOR_LOAD
);
3134 xresize(int col
, int row
)
3136 xw
.tw
= MAX(1, col
* xw
.cw
);
3137 xw
.th
= MAX(1, row
* xw
.ch
);
3139 XFreePixmap(xw
.dpy
, xw
.buf
);
3140 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
3141 DefaultDepth(xw
.dpy
, xw
.scr
));
3142 XftDrawChange(xw
.draw
, xw
.buf
);
3143 xclear(0, 0, xw
.w
, xw
.h
);
3147 sixd_to_16bit(int x
)
3149 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
3153 xloadcolor(int i
, const char *name
, Color
*ncolor
)
3155 XRenderColor color
= { .alpha
= 0xffff };
3158 if (BETWEEN(i
, 16, 255)) { /* 256 color */
3159 if (i
< 6*6*6+16) { /* same colors as xterm */
3160 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
3161 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
3162 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
3163 } else { /* greyscale */
3164 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
3165 color
.green
= color
.blue
= color
.red
;
3167 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
3168 xw
.cmap
, &color
, ncolor
);
3170 name
= colorname
[i
];
3173 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
3184 for (cp
= dc
.col
; cp
< &dc
.col
[LEN(dc
.col
)]; ++cp
)
3185 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
3188 for (i
= 0; i
< LEN(dc
.col
); i
++)
3189 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
3191 die("Could not allocate color '%s'\n", colorname
[i
]);
3193 die("Could not allocate color %d\n", i
);
3199 xsetcolorname(int x
, const char *name
)
3203 if (!BETWEEN(x
, 0, LEN(dc
.col
)))
3207 if (!xloadcolor(x
, name
, &ncolor
))
3210 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
3217 xtermclear(int col1
, int row1
, int col2
, int row2
)
3219 XftDrawRect(xw
.draw
,
3220 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
3221 borderpx
+ col1
* xw
.cw
,
3222 borderpx
+ row1
* xw
.ch
,
3223 (col2
-col1
+1) * xw
.cw
,
3224 (row2
-row1
+1) * xw
.ch
);
3228 * Absolute coordinates.
3231 xclear(int x1
, int y1
, int x2
, int y2
)
3233 XftDrawRect(xw
.draw
,
3234 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
3235 x1
, y1
, x2
-x1
, y2
-y1
);
3241 XClassHint
class = {opt_name
? opt_name
: termname
,
3242 opt_class
? opt_class
: termname
};
3243 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
3244 XSizeHints
*sizeh
= NULL
;
3246 sizeh
= XAllocSizeHints();
3248 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
3249 sizeh
->height
= xw
.h
;
3250 sizeh
->width
= xw
.w
;
3251 sizeh
->height_inc
= xw
.ch
;
3252 sizeh
->width_inc
= xw
.cw
;
3253 sizeh
->base_height
= 2 * borderpx
;
3254 sizeh
->base_width
= 2 * borderpx
;
3256 sizeh
->flags
|= PMaxSize
| PMinSize
;
3257 sizeh
->min_width
= sizeh
->max_width
= xw
.w
;
3258 sizeh
->min_height
= sizeh
->max_height
= xw
.h
;
3260 if (xw
.gm
& (XValue
|YValue
)) {
3261 sizeh
->flags
|= USPosition
| PWinGravity
;
3264 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
3267 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
3273 xgeommasktogravity(int mask
)
3275 switch (mask
& (XNegative
|YNegative
)) {
3277 return NorthWestGravity
;
3279 return NorthEastGravity
;
3281 return SouthWestGravity
;
3284 return SouthEastGravity
;
3288 xloadfont(Font
*f
, FcPattern
*pattern
)
3293 match
= FcFontMatch(NULL
, pattern
, &result
);
3297 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
3298 FcPatternDestroy(match
);
3303 f
->pattern
= FcPatternDuplicate(pattern
);
3305 f
->ascent
= f
->match
->ascent
;
3306 f
->descent
= f
->match
->descent
;
3308 f
->rbearing
= f
->match
->max_advance_width
;
3310 f
->height
= f
->ascent
+ f
->descent
;
3311 f
->width
= f
->lbearing
+ f
->rbearing
;
3317 xloadfonts(char *fontstr
, double fontsize
)
3323 if (fontstr
[0] == '-') {
3324 pattern
= XftXlfdParse(fontstr
, False
, False
);
3326 pattern
= FcNameParse((FcChar8
*)fontstr
);
3330 die("st: can't open font %s\n", fontstr
);
3333 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
3334 FcPatternDel(pattern
, FC_SIZE
);
3335 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
3336 usedfontsize
= fontsize
;
3338 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
3340 usedfontsize
= fontval
;
3341 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
3346 * Default font size is 12, if none given. This is to
3347 * have a known usedfontsize value.
3349 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
3352 defaultfontsize
= usedfontsize
;
3355 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
3356 FcDefaultSubstitute(pattern
);
3358 if (xloadfont(&dc
.font
, pattern
))
3359 die("st: can't open font %s\n", fontstr
);
3361 if (usedfontsize
< 0) {
3362 FcPatternGetDouble(dc
.font
.match
->pattern
,
3363 FC_PIXEL_SIZE
, 0, &fontval
);
3364 usedfontsize
= fontval
;
3366 defaultfontsize
= fontval
;
3369 /* Setting character width and height. */
3370 xw
.cw
= ceilf(dc
.font
.width
* cwscale
);
3371 xw
.ch
= ceilf(dc
.font
.height
* chscale
);
3373 FcPatternDel(pattern
, FC_SLANT
);
3374 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
3375 if (xloadfont(&dc
.ifont
, pattern
))
3376 die("st: can't open font %s\n", fontstr
);
3378 FcPatternDel(pattern
, FC_WEIGHT
);
3379 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
3380 if (xloadfont(&dc
.ibfont
, pattern
))
3381 die("st: can't open font %s\n", fontstr
);
3383 FcPatternDel(pattern
, FC_SLANT
);
3384 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
3385 if (xloadfont(&dc
.bfont
, pattern
))
3386 die("st: can't open font %s\n", fontstr
);
3388 FcPatternDestroy(pattern
);
3392 xunloadfont(Font
*f
)
3394 XftFontClose(xw
.dpy
, f
->match
);
3395 FcPatternDestroy(f
->pattern
);
3397 FcFontSetDestroy(f
->set
);
3403 /* Free the loaded fonts in the font cache. */
3405 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
3407 xunloadfont(&dc
.font
);
3408 xunloadfont(&dc
.bfont
);
3409 xunloadfont(&dc
.ifont
);
3410 xunloadfont(&dc
.ibfont
);
3414 xzoom(const Arg
*arg
)
3418 larg
.f
= usedfontsize
+ arg
->f
;
3423 xzoomabs(const Arg
*arg
)
3426 xloadfonts(usedfont
, arg
->f
);
3434 xzoomreset(const Arg
*arg
)
3438 if (defaultfontsize
> 0) {
3439 larg
.f
= defaultfontsize
;
3450 pid_t thispid
= getpid();
3451 XColor xmousefg
, xmousebg
;
3453 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
3454 die("Can't open display\n");
3455 xw
.scr
= XDefaultScreen(xw
.dpy
);
3456 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
3460 die("Could not init fontconfig.\n");
3462 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
3463 xloadfonts(usedfont
, 0);
3466 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
3469 /* adjust fixed window geometry */
3470 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
3471 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
3472 if (xw
.gm
& XNegative
)
3473 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - xw
.w
- 2;
3474 if (xw
.gm
& YNegative
)
3475 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - xw
.h
- 2;
3478 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
3479 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
3480 xw
.attrs
.bit_gravity
= NorthWestGravity
;
3481 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
3482 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
3483 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
3484 xw
.attrs
.colormap
= xw
.cmap
;
3486 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
3487 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
3488 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
3489 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
3490 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
3491 | CWEventMask
| CWColormap
, &xw
.attrs
);
3493 memset(&gcvalues
, 0, sizeof(gcvalues
));
3494 gcvalues
.graphics_exposures
= False
;
3495 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
3497 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
3498 DefaultDepth(xw
.dpy
, xw
.scr
));
3499 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
3500 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
3502 /* Xft rendering context */
3503 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
3506 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3507 XSetLocaleModifiers("@im=local");
3508 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3509 XSetLocaleModifiers("@im=");
3510 if ((xw
.xim
= XOpenIM(xw
.dpy
,
3511 NULL
, NULL
, NULL
)) == NULL
) {
3512 die("XOpenIM failed. Could not open input"
3517 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
3518 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
3519 XNFocusWindow
, xw
.win
, NULL
);
3521 die("XCreateIC failed. Could not obtain input method.\n");
3523 /* white cursor, black outline */
3524 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
3525 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
3527 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
3528 xmousefg
.red
= 0xffff;
3529 xmousefg
.green
= 0xffff;
3530 xmousefg
.blue
= 0xffff;
3533 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
3534 xmousebg
.red
= 0x0000;
3535 xmousebg
.green
= 0x0000;
3536 xmousebg
.blue
= 0x0000;
3539 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
3541 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
3542 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
3543 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
3544 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
3546 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
3547 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
3548 PropModeReplace
, (uchar
*)&thispid
, 1);
3551 XMapWindow(xw
.dpy
, xw
.win
);
3553 XSync(xw
.dpy
, False
);
3557 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
3559 float winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
, xp
, yp
;
3560 ushort mode
, prevmode
= USHRT_MAX
;
3561 Font
*font
= &dc
.font
;
3562 int frcflags
= FRC_NORMAL
;
3563 float runewidth
= xw
.cw
;
3567 FcPattern
*fcpattern
, *fontpattern
;
3568 FcFontSet
*fcsets
[] = { NULL
};
3569 FcCharSet
*fccharset
;
3570 int i
, f
, numspecs
= 0;
3572 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
3573 /* Fetch rune and mode for current glyph. */
3575 mode
= glyphs
[i
].mode
;
3577 /* Skip dummy wide-character spacing. */
3578 if (mode
== ATTR_WDUMMY
)
3581 /* Determine font for glyph if different from previous glyph. */
3582 if (prevmode
!= mode
) {
3585 frcflags
= FRC_NORMAL
;
3586 runewidth
= xw
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
3587 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
3589 frcflags
= FRC_ITALICBOLD
;
3590 } else if (mode
& ATTR_ITALIC
) {
3592 frcflags
= FRC_ITALIC
;
3593 } else if (mode
& ATTR_BOLD
) {
3595 frcflags
= FRC_BOLD
;
3597 yp
= winy
+ font
->ascent
;
3600 /* Lookup character index with default font. */
3601 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
3603 specs
[numspecs
].font
= font
->match
;
3604 specs
[numspecs
].glyph
= glyphidx
;
3605 specs
[numspecs
].x
= (short)xp
;
3606 specs
[numspecs
].y
= (short)yp
;
3612 /* Fallback on font cache, search the font cache for match. */
3613 for (f
= 0; f
< frclen
; f
++) {
3614 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
3615 /* Everything correct. */
3616 if (glyphidx
&& frc
[f
].flags
== frcflags
)
3618 /* We got a default font for a not found glyph. */
3619 if (!glyphidx
&& frc
[f
].flags
== frcflags
3620 && frc
[f
].unicodep
== rune
) {
3625 /* Nothing was found. Use fontconfig to find matching font. */
3628 font
->set
= FcFontSort(0, font
->pattern
,
3630 fcsets
[0] = font
->set
;
3633 * Nothing was found in the cache. Now use
3634 * some dozen of Fontconfig calls to get the
3635 * font for one single character.
3637 * Xft and fontconfig are design failures.
3639 fcpattern
= FcPatternDuplicate(font
->pattern
);
3640 fccharset
= FcCharSetCreate();
3642 FcCharSetAddChar(fccharset
, rune
);
3643 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3645 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
3647 FcConfigSubstitute(0, fcpattern
,
3649 FcDefaultSubstitute(fcpattern
);
3651 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
3655 * Overwrite or create the new cache entry.
3657 if (frclen
>= LEN(frc
)) {
3658 frclen
= LEN(frc
) - 1;
3659 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
3660 frc
[frclen
].unicodep
= 0;
3663 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
3665 frc
[frclen
].flags
= frcflags
;
3666 frc
[frclen
].unicodep
= rune
;
3668 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
3673 FcPatternDestroy(fcpattern
);
3674 FcCharSetDestroy(fccharset
);
3677 specs
[numspecs
].font
= frc
[f
].font
;
3678 specs
[numspecs
].glyph
= glyphidx
;
3679 specs
[numspecs
].x
= (short)xp
;
3680 specs
[numspecs
].y
= (short)(winy
+ frc
[f
].font
->ascent
);
3689 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
3691 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
3692 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
3693 width
= charlen
* xw
.cw
;
3694 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
3695 XRenderColor colfg
, colbg
;
3698 /* Determine foreground and background colors based on mode. */
3699 if (base
.fg
== defaultfg
) {
3700 if (base
.mode
& ATTR_ITALIC
)
3701 base
.fg
= defaultitalic
;
3702 else if ((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
))
3703 base
.fg
= defaultitalic
;
3704 else if (base
.mode
& ATTR_UNDERLINE
)
3705 base
.fg
= defaultunderline
;
3708 if (IS_TRUECOL(base
.fg
)) {
3709 colfg
.alpha
= 0xffff;
3710 colfg
.red
= TRUERED(base
.fg
);
3711 colfg
.green
= TRUEGREEN(base
.fg
);
3712 colfg
.blue
= TRUEBLUE(base
.fg
);
3713 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
3716 fg
= &dc
.col
[base
.fg
];
3719 if (IS_TRUECOL(base
.bg
)) {
3720 colbg
.alpha
= 0xffff;
3721 colbg
.green
= TRUEGREEN(base
.bg
);
3722 colbg
.red
= TRUERED(base
.bg
);
3723 colbg
.blue
= TRUEBLUE(base
.bg
);
3724 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
3727 bg
= &dc
.col
[base
.bg
];
3730 /* Change basic system colors [0-7] to bright system colors [8-15] */
3731 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
3732 fg
= &dc
.col
[base
.fg
+ 8];
3734 if (IS_SET(MODE_REVERSE
)) {
3735 if (fg
== &dc
.col
[defaultfg
]) {
3736 fg
= &dc
.col
[defaultbg
];
3738 colfg
.red
= ~fg
->color
.red
;
3739 colfg
.green
= ~fg
->color
.green
;
3740 colfg
.blue
= ~fg
->color
.blue
;
3741 colfg
.alpha
= fg
->color
.alpha
;
3742 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
3747 if (bg
== &dc
.col
[defaultbg
]) {
3748 bg
= &dc
.col
[defaultfg
];
3750 colbg
.red
= ~bg
->color
.red
;
3751 colbg
.green
= ~bg
->color
.green
;
3752 colbg
.blue
= ~bg
->color
.blue
;
3753 colbg
.alpha
= bg
->color
.alpha
;
3754 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
3760 if (base
.mode
& ATTR_REVERSE
) {
3766 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
3767 colfg
.red
= fg
->color
.red
/ 2;
3768 colfg
.green
= fg
->color
.green
/ 2;
3769 colfg
.blue
= fg
->color
.blue
/ 2;
3770 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
3774 if (base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
3777 if (base
.mode
& ATTR_INVISIBLE
)
3780 /* Intelligent cleaning up of the borders. */
3782 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
3783 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
3785 if (x
+ charlen
>= term
.col
) {
3786 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
3787 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
3790 xclear(winx
, 0, winx
+ width
, borderpx
);
3791 if (y
== term
.row
-1)
3792 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
3794 /* Clean up the region we want to draw to. */
3795 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
3797 /* Set the clip region because Xft is sometimes dirty. */
3802 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3804 /* Render the glyphs. */
3805 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
3807 /* Render underline and strikethrough. */
3808 if (base
.mode
& ATTR_UNDERLINE
) {
3809 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
3813 if (base
.mode
& ATTR_STRUCK
) {
3814 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
3818 /* Reset clip to none. */
3819 XftDrawSetClip(xw
.draw
, 0);
3823 xdrawglyph(Glyph g
, int x
, int y
)
3826 XftGlyphFontSpec spec
;
3828 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
3829 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
3835 static int oldx
= 0, oldy
= 0;
3837 Glyph g
= {' ', ATTR_NULL
, defaultbg
, defaultcs
}, og
;
3838 int ena_sel
= sel
.ob
.x
!= -1 && sel
.alt
== IS_SET(MODE_ALTSCREEN
);
3841 LIMIT(oldx
, 0, term
.col
-1);
3842 LIMIT(oldy
, 0, term
.row
-1);
3846 /* adjust position if in dummy */
3847 if (term
.line
[oldy
][oldx
].mode
& ATTR_WDUMMY
)
3849 if (term
.line
[term
.c
.y
][curx
].mode
& ATTR_WDUMMY
)
3852 /* remove the old cursor */
3853 og
= term
.line
[oldy
][oldx
];
3854 if (ena_sel
&& selected(oldx
, oldy
))
3855 og
.mode
^= ATTR_REVERSE
;
3856 xdrawglyph(og
, oldx
, oldy
);
3858 g
.u
= term
.line
[term
.c
.y
][term
.c
.x
].u
;
3861 * Select the right color for the right mode.
3863 if (IS_SET(MODE_REVERSE
)) {
3864 g
.mode
|= ATTR_REVERSE
;
3866 if (ena_sel
&& selected(term
.c
.x
, term
.c
.y
)) {
3867 drawcol
= dc
.col
[defaultcs
];
3870 drawcol
= dc
.col
[defaultrcs
];
3874 if (ena_sel
&& selected(term
.c
.x
, term
.c
.y
)) {
3875 drawcol
= dc
.col
[defaultrcs
];
3879 drawcol
= dc
.col
[defaultcs
];
3883 if (IS_SET(MODE_HIDE
))
3886 /* draw the new one */
3887 if (xw
.state
& WIN_FOCUSED
) {
3888 switch (xw
.cursor
) {
3889 case 7: /* st extension: snowman */
3890 utf8decode("☃", &g
.u
, UTF_SIZ
);
3891 case 0: /* Blinking Block */
3892 case 1: /* Blinking Block (Default) */
3893 case 2: /* Steady Block */
3894 g
.mode
|= term
.line
[term
.c
.y
][curx
].mode
& ATTR_WIDE
;
3895 xdrawglyph(g
, term
.c
.x
, term
.c
.y
);
3897 case 3: /* Blinking Underline */
3898 case 4: /* Steady Underline */
3899 XftDrawRect(xw
.draw
, &drawcol
,
3900 borderpx
+ curx
* xw
.cw
,
3901 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- \
3903 xw
.cw
, cursorthickness
);
3905 case 5: /* Blinking bar */
3906 case 6: /* Steady bar */
3907 XftDrawRect(xw
.draw
, &drawcol
,
3908 borderpx
+ curx
* xw
.cw
,
3909 borderpx
+ term
.c
.y
* xw
.ch
,
3910 cursorthickness
, xw
.ch
);
3914 XftDrawRect(xw
.draw
, &drawcol
,
3915 borderpx
+ curx
* xw
.cw
,
3916 borderpx
+ term
.c
.y
* xw
.ch
,
3918 XftDrawRect(xw
.draw
, &drawcol
,
3919 borderpx
+ curx
* xw
.cw
,
3920 borderpx
+ term
.c
.y
* xw
.ch
,
3922 XftDrawRect(xw
.draw
, &drawcol
,
3923 borderpx
+ (curx
+ 1) * xw
.cw
- 1,
3924 borderpx
+ term
.c
.y
* xw
.ch
,
3926 XftDrawRect(xw
.draw
, &drawcol
,
3927 borderpx
+ curx
* xw
.cw
,
3928 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3931 oldx
= curx
, oldy
= term
.c
.y
;
3940 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3942 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3943 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
3950 xsettitle(opt_title
? opt_title
: "st");
3963 drawregion(0, 0, term
.col
, term
.row
);
3964 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3966 XSetForeground(xw
.dpy
, dc
.gc
,
3967 dc
.col
[IS_SET(MODE_REVERSE
)?
3968 defaultfg
: defaultbg
].pixel
);
3972 drawregion(int x1
, int y1
, int x2
, int y2
)
3974 int i
, x
, y
, ox
, numspecs
;
3976 XftGlyphFontSpec
*specs
;
3977 int ena_sel
= sel
.ob
.x
!= -1 && sel
.alt
== IS_SET(MODE_ALTSCREEN
);
3979 if (!(xw
.state
& WIN_VISIBLE
))
3982 for (y
= y1
; y
< y2
; y
++) {
3986 xtermclear(0, y
, term
.col
, y
);
3989 specs
= term
.specbuf
;
3990 numspecs
= xmakeglyphfontspecs(specs
, &term
.line
[y
][x1
], x2
- x1
, x1
, y
);
3993 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
3994 new = term
.line
[y
][x
];
3995 if (new.mode
== ATTR_WDUMMY
)
3997 if (ena_sel
&& selected(x
, y
))
3998 new.mode
^= ATTR_REVERSE
;
3999 if (i
> 0 && ATTRCMP(base
, new)) {
4000 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
4012 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
4024 visibility(XEvent
*ev
)
4026 XVisibilityEvent
*e
= &ev
->xvisibility
;
4028 MODBIT(xw
.state
, e
->state
!= VisibilityFullyObscured
, WIN_VISIBLE
);
4034 xw
.state
&= ~WIN_VISIBLE
;
4038 xsetpointermotion(int set
)
4040 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
4041 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
4045 xseturgency(int add
)
4047 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
4049 MODBIT(h
->flags
, add
, XUrgencyHint
);
4050 XSetWMHints(xw
.dpy
, xw
.win
, h
);
4057 XFocusChangeEvent
*e
= &ev
->xfocus
;
4059 if (e
->mode
== NotifyGrab
)
4062 if (ev
->type
== FocusIn
) {
4063 XSetICFocus(xw
.xic
);
4064 xw
.state
|= WIN_FOCUSED
;
4066 if (IS_SET(MODE_FOCUS
))
4067 ttywrite("\033[I", 3);
4069 XUnsetICFocus(xw
.xic
);
4070 xw
.state
&= ~WIN_FOCUSED
;
4071 if (IS_SET(MODE_FOCUS
))
4072 ttywrite("\033[O", 3);
4077 match(uint mask
, uint state
)
4079 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
4083 numlock(const Arg
*dummy
)
4089 kmap(KeySym k
, uint state
)
4094 /* Check for mapped keys out of X11 function keys. */
4095 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
4096 if (mappedkeys
[i
] == k
)
4099 if (i
== LEN(mappedkeys
)) {
4100 if ((k
& 0xFFFF) < 0xFD00)
4104 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
4108 if (!match(kp
->mask
, state
))
4111 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
4113 if (term
.numlock
&& kp
->appkey
== 2)
4116 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
4119 if (IS_SET(MODE_CRLF
) ? kp
->crlf
< 0 : kp
->crlf
> 0)
4131 XKeyEvent
*e
= &ev
->xkey
;
4133 char buf
[32], *customkey
;
4139 if (IS_SET(MODE_KBDLOCK
))
4142 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
4144 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
4145 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
4146 bp
->func(&(bp
->arg
));
4151 /* 2. custom keys from config.h */
4152 if ((customkey
= kmap(ksym
, e
->state
))) {
4153 ttysend(customkey
, strlen(customkey
));
4157 /* 3. composed string from input method */
4160 if (len
== 1 && e
->state
& Mod1Mask
) {
4161 if (IS_SET(MODE_8BIT
)) {
4164 len
= utf8encode(c
, buf
);
4181 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
4183 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
4184 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
4185 xw
.state
|= WIN_FOCUSED
;
4187 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
4188 xw
.state
&= ~WIN_FOCUSED
;
4190 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
4191 /* Send SIGHUP to shell */
4198 cresize(int width
, int height
)
4207 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
4208 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
4217 if (e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
4220 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
4228 int w
= xw
.w
, h
= xw
.h
;
4230 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
4231 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
4234 /* Waiting for window mapping */
4236 XNextEvent(xw
.dpy
, &ev
);
4238 * This XFilterEvent call is required because of XOpenIM. It
4239 * does filter out the key event and some client message for
4240 * the input method too.
4242 if (XFilterEvent(&ev
, None
))
4244 if (ev
.type
== ConfigureNotify
) {
4245 w
= ev
.xconfigure
.width
;
4246 h
= ev
.xconfigure
.height
;
4248 } while (ev
.type
!= MapNotify
);
4254 clock_gettime(CLOCK_MONOTONIC
, &last
);
4257 for (xev
= actionfps
;;) {
4259 FD_SET(cmdfd
, &rfd
);
4262 if (pselect(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
4265 die("select failed: %s\n", strerror(errno
));
4267 if (FD_ISSET(cmdfd
, &rfd
)) {
4270 blinkset
= tattrset(ATTR_BLINK
);
4272 MODBIT(term
.mode
, 0, MODE_BLINK
);
4276 if (FD_ISSET(xfd
, &rfd
))
4279 clock_gettime(CLOCK_MONOTONIC
, &now
);
4280 drawtimeout
.tv_sec
= 0;
4281 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
4285 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
4286 tsetdirtattr(ATTR_BLINK
);
4287 term
.mode
^= MODE_BLINK
;
4291 deltatime
= TIMEDIFF(now
, last
);
4292 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
4298 while (XPending(xw
.dpy
)) {
4299 XNextEvent(xw
.dpy
, &ev
);
4300 if (XFilterEvent(&ev
, None
))
4302 if (handler
[ev
.type
])
4303 (handler
[ev
.type
])(&ev
);
4309 if (xev
&& !FD_ISSET(xfd
, &rfd
))
4311 if (!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
4313 if (TIMEDIFF(now
, lastblink
) \
4315 drawtimeout
.tv_nsec
= 1000;
4317 drawtimeout
.tv_nsec
= (1E6
* \
4322 drawtimeout
.tv_sec
= \
4323 drawtimeout
.tv_nsec
/ 1E9
;
4324 drawtimeout
.tv_nsec
%= (long)1E9
;
4336 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
4337 " [-n name] [-o file]\n"
4338 " [-T title] [-t title] [-w windowid]"
4339 " [[-e] command [args ...]]\n"
4340 " %s [-aiv] [-c class] [-f font] [-g geometry]"
4341 " [-n name] [-o file]\n"
4342 " [-T title] [-t title] [-w windowid] -l line"
4343 " [stty_args ...]\n", argv0
, argv0
);
4347 main(int argc
, char *argv
[])
4349 uint cols
= 80, rows
= 24;
4353 xw
.cursor
= cursorshape
;
4360 opt_class
= EARGF(usage());
4367 opt_font
= EARGF(usage());
4370 xw
.gm
= XParseGeometry(EARGF(usage()),
4371 &xw
.l
, &xw
.t
, &cols
, &rows
);
4377 opt_io
= EARGF(usage());
4380 opt_line
= EARGF(usage());
4383 opt_name
= EARGF(usage());
4387 opt_title
= EARGF(usage());
4390 opt_embed
= EARGF(usage());
4393 die("%s " VERSION
" (c) 2010-2016 st engineers\n", argv0
);
4401 /* eat all remaining arguments */
4403 if (!opt_title
&& !opt_line
)
4404 opt_title
= basename(xstrdup(argv
[0]));
4406 setlocale(LC_CTYPE
, "");
4407 XSetLocaleModifiers("");
4408 tnew(MAX(cols
, 1), MAX(rows
, 1));