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 void 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 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
699 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
703 sel
.clipboard
= NULL
;
704 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
705 if (sel
.xtarget
== None
)
706 sel
.xtarget
= XA_STRING
;
715 return LIMIT(x
, 0, term
.col
-1);
724 return LIMIT(y
, 0, term
.row
-1);
732 if (term
.line
[y
][i
- 1].mode
& ATTR_WRAP
)
735 while (i
> 0 && term
.line
[y
][i
- 1].u
== ' ')
746 if (sel
.type
== SEL_REGULAR
&& sel
.ob
.y
!= sel
.oe
.y
) {
747 sel
.nb
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.ob
.x
: sel
.oe
.x
;
748 sel
.ne
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.oe
.x
: sel
.ob
.x
;
750 sel
.nb
.x
= MIN(sel
.ob
.x
, sel
.oe
.x
);
751 sel
.ne
.x
= MAX(sel
.ob
.x
, sel
.oe
.x
);
753 sel
.nb
.y
= MIN(sel
.ob
.y
, sel
.oe
.y
);
754 sel
.ne
.y
= MAX(sel
.ob
.y
, sel
.oe
.y
);
756 selsnap(&sel
.nb
.x
, &sel
.nb
.y
, -1);
757 selsnap(&sel
.ne
.x
, &sel
.ne
.y
, +1);
759 /* expand selection over line breaks */
760 if (sel
.type
== SEL_RECTANGULAR
)
762 i
= tlinelen(sel
.nb
.y
);
765 if (tlinelen(sel
.ne
.y
) <= sel
.ne
.x
)
766 sel
.ne
.x
= term
.col
- 1;
770 selected(int x
, int y
)
772 if (sel
.mode
== SEL_EMPTY
)
775 if (sel
.type
== SEL_RECTANGULAR
)
776 return BETWEEN(y
, sel
.nb
.y
, sel
.ne
.y
)
777 && BETWEEN(x
, sel
.nb
.x
, sel
.ne
.x
);
779 return BETWEEN(y
, sel
.nb
.y
, sel
.ne
.y
)
780 && (y
!= sel
.nb
.y
|| x
>= sel
.nb
.x
)
781 && (y
!= sel
.ne
.y
|| x
<= sel
.ne
.x
);
785 selsnap(int *x
, int *y
, int direction
)
787 int newx
, newy
, xt
, yt
;
788 int delim
, prevdelim
;
794 * Snap around if the word wraps around at the end or
795 * beginning of a line.
797 prevgp
= &term
.line
[*y
][*x
];
798 prevdelim
= ISDELIM(prevgp
->u
);
800 newx
= *x
+ direction
;
802 if (!BETWEEN(newx
, 0, term
.col
- 1)) {
804 newx
= (newx
+ term
.col
) % term
.col
;
805 if (!BETWEEN(newy
, 0, term
.row
- 1))
811 yt
= newy
, xt
= newx
;
812 if (!(term
.line
[yt
][xt
].mode
& ATTR_WRAP
))
816 if (newx
>= tlinelen(newy
))
819 gp
= &term
.line
[newy
][newx
];
820 delim
= ISDELIM(gp
->u
);
821 if (!(gp
->mode
& ATTR_WDUMMY
) && (delim
!= prevdelim
822 || (delim
&& gp
->u
!= prevgp
->u
)))
833 * Snap around if the the previous line or the current one
834 * has set ATTR_WRAP at its end. Then the whole next or
835 * previous line will be selected.
837 *x
= (direction
< 0) ? 0 : term
.col
- 1;
839 for (; *y
> 0; *y
+= direction
) {
840 if (!(term
.line
[*y
-1][term
.col
-1].mode
845 } else if (direction
> 0) {
846 for (; *y
< term
.row
-1; *y
+= direction
) {
847 if (!(term
.line
[*y
][term
.col
-1].mode
858 getbuttoninfo(XEvent
*e
)
861 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forceselmod
);
863 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
865 sel
.oe
.x
= x2col(e
->xbutton
.x
);
866 sel
.oe
.y
= y2row(e
->xbutton
.y
);
869 sel
.type
= SEL_REGULAR
;
870 for (type
= 1; type
< LEN(selmasks
); ++type
) {
871 if (match(selmasks
[type
], state
)) {
879 mousereport(XEvent
*e
)
881 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
882 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
888 if (e
->xbutton
.type
== MotionNotify
) {
889 if (x
== ox
&& y
== oy
)
891 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
893 /* MOUSE_MOTION: no reporting if no button is pressed */
894 if (IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
897 button
= oldbutton
+ 32;
901 if (!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
908 if (e
->xbutton
.type
== ButtonPress
) {
912 } else if (e
->xbutton
.type
== ButtonRelease
) {
914 /* MODE_MOUSEX10: no button release reporting */
915 if (IS_SET(MODE_MOUSEX10
))
917 if (button
== 64 || button
== 65)
922 if (!IS_SET(MODE_MOUSEX10
)) {
923 button
+= ((state
& ShiftMask
) ? 4 : 0)
924 + ((state
& Mod4Mask
) ? 8 : 0)
925 + ((state
& ControlMask
) ? 16 : 0);
928 if (IS_SET(MODE_MOUSESGR
)) {
929 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
931 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
932 } else if (x
< 223 && y
< 223) {
933 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
934 32+button
, 32+x
+1, 32+y
+1);
948 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
953 for (mk
= mshortcuts
; mk
< mshortcuts
+ LEN(mshortcuts
); mk
++) {
954 if (e
->xbutton
.button
== mk
->b
955 && match(mk
->mask
, e
->xbutton
.state
)) {
956 ttysend(mk
->s
, strlen(mk
->s
));
961 if (e
->xbutton
.button
== Button1
) {
962 clock_gettime(CLOCK_MONOTONIC
, &now
);
964 /* Clear previous selection, logically and visually. */
966 sel
.mode
= SEL_EMPTY
;
967 sel
.type
= SEL_REGULAR
;
968 sel
.oe
.x
= sel
.ob
.x
= x2col(e
->xbutton
.x
);
969 sel
.oe
.y
= sel
.ob
.y
= y2row(e
->xbutton
.y
);
972 * If the user clicks below predefined timeouts specific
973 * snapping behaviour is exposed.
975 if (TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
976 sel
.snap
= SNAP_LINE
;
977 } else if (TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
978 sel
.snap
= SNAP_WORD
;
985 sel
.mode
= SEL_READY
;
986 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
987 sel
.tclick2
= sel
.tclick1
;
996 int y
, bufsize
, lastx
, linelen
;
1002 bufsize
= (term
.col
+1) * (sel
.ne
.y
-sel
.nb
.y
+1) * UTF_SIZ
;
1003 ptr
= str
= xmalloc(bufsize
);
1005 /* append every set & selected glyph to the selection */
1006 for (y
= sel
.nb
.y
; y
<= sel
.ne
.y
; y
++) {
1007 linelen
= tlinelen(y
);
1009 if (sel
.type
== SEL_RECTANGULAR
) {
1010 gp
= &term
.line
[y
][sel
.nb
.x
];
1013 gp
= &term
.line
[y
][sel
.nb
.y
== y
? sel
.nb
.x
: 0];
1014 lastx
= (sel
.ne
.y
== y
) ? sel
.ne
.x
: term
.col
-1;
1016 last
= &term
.line
[y
][MIN(lastx
, linelen
-1)];
1017 while (last
>= gp
&& last
->u
== ' ')
1020 for ( ; gp
<= last
; ++gp
) {
1021 if (gp
->mode
& ATTR_WDUMMY
)
1024 ptr
+= utf8encode(gp
->u
, ptr
);
1028 * Copy and pasting of line endings is inconsistent
1029 * in the inconsistent terminal and GUI world.
1030 * The best solution seems like to produce '\n' when
1031 * something is copied from st and convert '\n' to
1032 * '\r', when something to be pasted is received by
1034 * FIXME: Fix the computer world.
1036 if ((y
< sel
.ne
.y
|| lastx
>= linelen
) && !(last
->mode
& ATTR_WRAP
))
1046 xsetsel(getsel(), t
);
1050 propnotify(XEvent
*e
)
1052 XPropertyEvent
*xpev
;
1053 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1055 xpev
= &e
->xproperty
;
1056 if (xpev
->state
== PropertyNewValue
&&
1057 (xpev
->atom
== XA_PRIMARY
||
1058 xpev
->atom
== clipboard
)) {
1064 selnotify(XEvent
*e
)
1066 ulong nitems
, ofs
, rem
;
1068 uchar
*data
, *last
, *repl
;
1069 Atom type
, incratom
, property
;
1071 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
1074 if (e
->type
== SelectionNotify
) {
1075 property
= e
->xselection
.property
;
1076 } else if(e
->type
== PropertyNotify
) {
1077 property
= e
->xproperty
.atom
;
1081 if (property
== None
)
1085 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
1086 BUFSIZ
/4, False
, AnyPropertyType
,
1087 &type
, &format
, &nitems
, &rem
,
1089 fprintf(stderr
, "Clipboard allocation failed\n");
1093 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
1095 * If there is some PropertyNotify with no data, then
1096 * this is the signal of the selection owner that all
1097 * data has been transferred. We won't need to receive
1098 * PropertyNotify events anymore.
1100 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
1101 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
1105 if (type
== incratom
) {
1107 * Activate the PropertyNotify events so we receive
1108 * when the selection owner does send us the next
1111 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
1112 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
1116 * Deleting the property is the transfer start signal.
1118 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
1123 * As seen in getsel:
1124 * Line endings are inconsistent in the terminal and GUI world
1125 * copy and pasting. When receiving some selection data,
1126 * replace all '\n' with '\r'.
1127 * FIXME: Fix the computer world.
1130 last
= data
+ nitems
* format
/ 8;
1131 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
1135 if (IS_SET(MODE_BRCKTPASTE
))
1136 ttywrite("\033[200~", 6);
1137 ttysend((char *)data
, nitems
* format
/ 8);
1138 if (IS_SET(MODE_BRCKTPASTE
))
1139 ttywrite("\033[201~", 6);
1141 /* number of 32-bit chunks returned */
1142 ofs
+= nitems
* format
/ 32;
1146 * Deleting the property again tells the selection owner to send the
1147 * next data chunk in the property.
1149 if (e
->type
== PropertyNotify
)
1150 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
1154 selpaste(const Arg
*dummy
)
1156 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1157 xw
.win
, CurrentTime
);
1161 clipcopy(const Arg
*dummy
)
1165 if (sel
.clipboard
!= NULL
)
1166 free(sel
.clipboard
);
1168 if (sel
.primary
!= NULL
) {
1169 sel
.clipboard
= xstrdup(sel
.primary
);
1170 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1171 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1176 clippaste(const Arg
*dummy
)
1180 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1181 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, clipboard
,
1182 xw
.win
, CurrentTime
);
1190 sel
.mode
= SEL_IDLE
;
1192 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1196 selrequest(XEvent
*e
)
1198 XSelectionRequestEvent
*xsre
;
1199 XSelectionEvent xev
;
1200 Atom xa_targets
, string
, clipboard
;
1203 xsre
= (XSelectionRequestEvent
*) e
;
1204 xev
.type
= SelectionNotify
;
1205 xev
.requestor
= xsre
->requestor
;
1206 xev
.selection
= xsre
->selection
;
1207 xev
.target
= xsre
->target
;
1208 xev
.time
= xsre
->time
;
1209 if (xsre
->property
== None
)
1210 xsre
->property
= xsre
->target
;
1213 xev
.property
= None
;
1215 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1216 if (xsre
->target
== xa_targets
) {
1217 /* respond with the supported type */
1218 string
= sel
.xtarget
;
1219 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1220 XA_ATOM
, 32, PropModeReplace
,
1221 (uchar
*) &string
, 1);
1222 xev
.property
= xsre
->property
;
1223 } else if (xsre
->target
== sel
.xtarget
|| xsre
->target
== XA_STRING
) {
1225 * xith XA_STRING non ascii characters may be incorrect in the
1226 * requestor. It is not our problem, use utf8.
1228 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1229 if (xsre
->selection
== XA_PRIMARY
) {
1230 seltext
= sel
.primary
;
1231 } else if (xsre
->selection
== clipboard
) {
1232 seltext
= sel
.clipboard
;
1235 "Unhandled clipboard selection 0x%lx\n",
1239 if (seltext
!= NULL
) {
1240 XChangeProperty(xsre
->display
, xsre
->requestor
,
1241 xsre
->property
, xsre
->target
,
1243 (uchar
*)seltext
, strlen(seltext
));
1244 xev
.property
= xsre
->property
;
1248 /* all done, send a notification to the listener */
1249 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
1250 fprintf(stderr
, "Error sending SelectionNotify event\n");
1254 xsetsel(char *str
, Time t
)
1259 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
1260 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
1267 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
1272 if (e
->xbutton
.button
== Button2
) {
1274 } else if (e
->xbutton
.button
== Button1
) {
1275 if (sel
.mode
== SEL_READY
) {
1277 selcopy(e
->xbutton
.time
);
1280 sel
.mode
= SEL_IDLE
;
1281 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1288 int oldey
, oldex
, oldsby
, oldsey
;
1290 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
1298 sel
.mode
= SEL_READY
;
1305 if (oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
1306 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
1310 die(const char *errstr
, ...)
1314 va_start(ap
, errstr
);
1315 vfprintf(stderr
, errstr
, ap
);
1323 char **args
, *sh
, *prog
;
1324 const struct passwd
*pw
;
1325 char buf
[sizeof(long) * 8 + 1];
1328 if ((pw
= getpwuid(getuid())) == NULL
) {
1330 die("getpwuid:%s\n", strerror(errno
));
1332 die("who are you?\n");
1335 if ((sh
= getenv("SHELL")) == NULL
)
1336 sh
= (pw
->pw_shell
[0]) ? pw
->pw_shell
: shell
;
1344 args
= (opt_cmd
) ? opt_cmd
: (char *[]) {prog
, NULL
};
1346 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1348 unsetenv("COLUMNS");
1350 unsetenv("TERMCAP");
1351 setenv("LOGNAME", pw
->pw_name
, 1);
1352 setenv("USER", pw
->pw_name
, 1);
1353 setenv("SHELL", sh
, 1);
1354 setenv("HOME", pw
->pw_dir
, 1);
1355 setenv("TERM", termname
, 1);
1356 setenv("WINDOWID", buf
, 1);
1358 signal(SIGCHLD
, SIG_DFL
);
1359 signal(SIGHUP
, SIG_DFL
);
1360 signal(SIGINT
, SIG_DFL
);
1361 signal(SIGQUIT
, SIG_DFL
);
1362 signal(SIGTERM
, SIG_DFL
);
1363 signal(SIGALRM
, SIG_DFL
);
1375 if ((p
= waitpid(pid
, &stat
, WNOHANG
)) < 0)
1376 die("Waiting for pid %hd failed: %s\n", pid
, strerror(errno
));
1381 if (!WIFEXITED(stat
) || WEXITSTATUS(stat
))
1382 die("child finished with error '%d'\n", stat
);
1390 char cmd
[_POSIX_ARG_MAX
], **p
, *q
, *s
;
1393 if ((n
= strlen(stty_args
)) > sizeof(cmd
)-1)
1394 die("incorrect stty parameters\n");
1395 memcpy(cmd
, stty_args
, n
);
1397 siz
= sizeof(cmd
) - n
;
1398 for (p
= opt_cmd
; p
&& (s
= *p
); ++p
) {
1399 if ((n
= strlen(s
)) > siz
-1)
1400 die("stty parameter length too long\n");
1402 q
= memcpy(q
, s
, n
);
1407 if (system(cmd
) != 0)
1408 perror("Couldn't call stty");
1415 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1418 term
.mode
|= MODE_PRINT
;
1419 iofd
= (!strcmp(opt_io
, "-")) ?
1420 1 : open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1422 fprintf(stderr
, "Error opening %s:%s\n",
1423 opt_io
, strerror(errno
));
1428 if ((cmdfd
= open(opt_line
, O_RDWR
)) < 0)
1429 die("open line failed: %s\n", strerror(errno
));
1436 /* seems to work fine on linux, openbsd and freebsd */
1437 if (openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1438 die("openpty failed: %s\n", strerror(errno
));
1440 switch (pid
= fork()) {
1442 die("fork failed\n");
1446 setsid(); /* create a new process group */
1450 if (ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1451 die("ioctl TIOCSCTTY failed: %s\n", strerror(errno
));
1459 signal(SIGCHLD
, sigchld
);
1467 static char buf
[BUFSIZ
];
1468 static int buflen
= 0;
1470 int charsize
; /* size of utf8 char in bytes */
1474 /* append read bytes to unprocessed bytes */
1475 if ((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1476 die("Couldn't read from shell: %s\n", strerror(errno
));
1478 /* process every complete utf8 char */
1481 while ((charsize
= utf8decode(ptr
, &unicodep
, buflen
))) {
1487 /* keep any uncomplete utf8 char for the next call */
1488 memmove(buf
, ptr
, buflen
);
1492 ttywrite(const char *s
, size_t n
)
1499 * Remember that we are using a pty, which might be a modem line.
1500 * Writing too much will clog the line. That's why we are doing this
1502 * FIXME: Migrate the world to Plan 9.
1506 FD_SET(cmdfd
, &wfd
);
1510 /* Check if we can write. */
1511 if (pselect(cmdfd
+1, NULL
, &wfd
, NULL
, &tv
, NULL
) < 0) {
1514 die("select failed: %s\n", strerror(errno
));
1516 if(!FD_ISSET(cmdfd
, &wfd
)) {
1517 /* No, then free some buffer space. */
1521 * Only write 256 bytes at maximum. This seems to be a
1522 * reasonable value for a serial line. Bigger values
1523 * might clog the I/O.
1525 r
= write(cmdfd
, s
, (n
< 256)? n
: 256);
1527 die("write error on tty: %s\n",
1532 * We weren't able to write out everything.
1533 * This means the buffer is getting full
1541 /* All bytes have been written. */
1549 ttysend(char *s
, size_t n
)
1555 if (IS_SET(MODE_ECHO
))
1556 while ((len
= utf8decode(s
, &u
, n
)) > 0) {
1568 w
.ws_row
= term
.row
;
1569 w
.ws_col
= term
.col
;
1570 w
.ws_xpixel
= xw
.tw
;
1571 w
.ws_ypixel
= xw
.th
;
1572 if (ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1573 fprintf(stderr
, "Couldn't set window size: %s\n", strerror(errno
));
1581 for (i
= 0; i
< term
.row
-1; i
++) {
1582 for (j
= 0; j
< term
.col
-1; j
++) {
1583 if (term
.line
[i
][j
].mode
& attr
)
1592 tsetdirt(int top
, int bot
)
1596 LIMIT(top
, 0, term
.row
-1);
1597 LIMIT(bot
, 0, term
.row
-1);
1599 for (i
= top
; i
<= bot
; i
++)
1604 tsetdirtattr(int attr
)
1608 for (i
= 0; i
< term
.row
-1; i
++) {
1609 for (j
= 0; j
< term
.col
-1; j
++) {
1610 if (term
.line
[i
][j
].mode
& attr
) {
1621 tsetdirt(0, term
.row
-1);
1627 static TCursor c
[2];
1628 int alt
= IS_SET(MODE_ALTSCREEN
);
1630 if (mode
== CURSOR_SAVE
) {
1632 } else if (mode
== CURSOR_LOAD
) {
1634 tmoveto(c
[alt
].x
, c
[alt
].y
);
1643 term
.c
= (TCursor
){{
1647 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1649 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1650 for (i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1653 term
.bot
= term
.row
- 1;
1654 term
.mode
= MODE_WRAP
;
1655 memset(term
.trantbl
, CS_USA
, sizeof(term
.trantbl
));
1658 for (i
= 0; i
< 2; i
++) {
1660 tcursor(CURSOR_SAVE
);
1661 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1667 tnew(int col
, int row
)
1669 term
= (Term
){ .c
= { .attr
= { .fg
= defaultfg
, .bg
= defaultbg
} } };
1679 Line
*tmp
= term
.line
;
1681 term
.line
= term
.alt
;
1683 term
.mode
^= MODE_ALTSCREEN
;
1688 tscrolldown(int orig
, int n
)
1693 LIMIT(n
, 0, term
.bot
-orig
+1);
1695 tsetdirt(orig
, term
.bot
-n
);
1696 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1698 for (i
= term
.bot
; i
>= orig
+n
; i
--) {
1699 temp
= term
.line
[i
];
1700 term
.line
[i
] = term
.line
[i
-n
];
1701 term
.line
[i
-n
] = temp
;
1708 tscrollup(int orig
, int n
)
1713 LIMIT(n
, 0, term
.bot
-orig
+1);
1715 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1716 tsetdirt(orig
+n
, term
.bot
);
1718 for (i
= orig
; i
<= term
.bot
-n
; i
++) {
1719 temp
= term
.line
[i
];
1720 term
.line
[i
] = term
.line
[i
+n
];
1721 term
.line
[i
+n
] = temp
;
1724 selscroll(orig
, -n
);
1728 selscroll(int orig
, int n
)
1733 if (BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1734 if ((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1738 if (sel
.type
== SEL_RECTANGULAR
) {
1739 if (sel
.ob
.y
< term
.top
)
1740 sel
.ob
.y
= term
.top
;
1741 if (sel
.oe
.y
> term
.bot
)
1742 sel
.oe
.y
= term
.bot
;
1744 if (sel
.ob
.y
< term
.top
) {
1745 sel
.ob
.y
= term
.top
;
1748 if (sel
.oe
.y
> term
.bot
) {
1749 sel
.oe
.y
= term
.bot
;
1750 sel
.oe
.x
= term
.col
;
1758 tnewline(int first_col
)
1762 if (y
== term
.bot
) {
1763 tscrollup(term
.top
, 1);
1767 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1773 char *p
= csiescseq
.buf
, *np
;
1782 csiescseq
.buf
[csiescseq
.len
] = '\0';
1783 while (p
< csiescseq
.buf
+csiescseq
.len
) {
1785 v
= strtol(p
, &np
, 10);
1788 if (v
== LONG_MAX
|| v
== LONG_MIN
)
1790 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1792 if (*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1796 csiescseq
.mode
[0] = *p
++;
1797 csiescseq
.mode
[1] = (p
< csiescseq
.buf
+csiescseq
.len
) ? *p
: '\0';
1800 /* for absolute user moves, when decom is set */
1802 tmoveato(int x
, int y
)
1804 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1808 tmoveto(int x
, int y
)
1812 if (term
.c
.state
& CURSOR_ORIGIN
) {
1817 maxy
= term
.row
- 1;
1819 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1820 term
.c
.x
= LIMIT(x
, 0, term
.col
-1);
1821 term
.c
.y
= LIMIT(y
, miny
, maxy
);
1825 tsetchar(Rune u
, Glyph
*attr
, int x
, int y
)
1827 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1828 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1829 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1830 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1831 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1832 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1833 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1834 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1835 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1839 * The table is proudly stolen from rxvt.
1841 if (term
.trantbl
[term
.charset
] == CS_GRAPHIC0
&&
1842 BETWEEN(u
, 0x41, 0x7e) && vt100_0
[u
- 0x41])
1843 utf8decode(vt100_0
[u
- 0x41], &u
, UTF_SIZ
);
1845 if (term
.line
[y
][x
].mode
& ATTR_WIDE
) {
1846 if (x
+1 < term
.col
) {
1847 term
.line
[y
][x
+1].u
= ' ';
1848 term
.line
[y
][x
+1].mode
&= ~ATTR_WDUMMY
;
1850 } else if (term
.line
[y
][x
].mode
& ATTR_WDUMMY
) {
1851 term
.line
[y
][x
-1].u
= ' ';
1852 term
.line
[y
][x
-1].mode
&= ~ATTR_WIDE
;
1856 term
.line
[y
][x
] = *attr
;
1857 term
.line
[y
][x
].u
= u
;
1861 tclearregion(int x1
, int y1
, int x2
, int y2
)
1867 temp
= x1
, x1
= x2
, x2
= temp
;
1869 temp
= y1
, y1
= y2
, y2
= temp
;
1871 LIMIT(x1
, 0, term
.col
-1);
1872 LIMIT(x2
, 0, term
.col
-1);
1873 LIMIT(y1
, 0, term
.row
-1);
1874 LIMIT(y2
, 0, term
.row
-1);
1876 for (y
= y1
; y
<= y2
; y
++) {
1878 for (x
= x1
; x
<= x2
; x
++) {
1879 gp
= &term
.line
[y
][x
];
1882 gp
->fg
= term
.c
.attr
.fg
;
1883 gp
->bg
= term
.c
.attr
.bg
;
1896 LIMIT(n
, 0, term
.col
- term
.c
.x
);
1900 size
= term
.col
- src
;
1901 line
= term
.line
[term
.c
.y
];
1903 memmove(&line
[dst
], &line
[src
], size
* sizeof(Glyph
));
1904 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1913 LIMIT(n
, 0, term
.col
- term
.c
.x
);
1917 size
= term
.col
- dst
;
1918 line
= term
.line
[term
.c
.y
];
1920 memmove(&line
[dst
], &line
[src
], size
* sizeof(Glyph
));
1921 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1925 tinsertblankline(int n
)
1927 if (BETWEEN(term
.c
.y
, term
.top
, term
.bot
))
1928 tscrolldown(term
.c
.y
, n
);
1934 if (BETWEEN(term
.c
.y
, term
.top
, term
.bot
))
1935 tscrollup(term
.c
.y
, n
);
1939 tdefcolor(int *attr
, int *npar
, int l
)
1944 switch (attr
[*npar
+ 1]) {
1945 case 2: /* direct color in RGB space */
1946 if (*npar
+ 4 >= l
) {
1948 "erresc(38): Incorrect number of parameters (%d)\n",
1952 r
= attr
[*npar
+ 2];
1953 g
= attr
[*npar
+ 3];
1954 b
= attr
[*npar
+ 4];
1956 if (!BETWEEN(r
, 0, 255) || !BETWEEN(g
, 0, 255) || !BETWEEN(b
, 0, 255))
1957 fprintf(stderr
, "erresc: bad rgb color (%u,%u,%u)\n",
1960 idx
= TRUECOLOR(r
, g
, b
);
1962 case 5: /* indexed color */
1963 if (*npar
+ 2 >= l
) {
1965 "erresc(38): Incorrect number of parameters (%d)\n",
1970 if (!BETWEEN(attr
[*npar
], 0, 255))
1971 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[*npar
]);
1975 case 0: /* implemented defined (only foreground) */
1976 case 1: /* transparent */
1977 case 3: /* direct color in CMY space */
1978 case 4: /* direct color in CMYK space */
1981 "erresc(38): gfx attr %d unknown\n", attr
[*npar
]);
1989 tsetattr(int *attr
, int l
)
1994 for (i
= 0; i
< l
; i
++) {
1997 term
.c
.attr
.mode
&= ~(
2006 term
.c
.attr
.fg
= defaultfg
;
2007 term
.c
.attr
.bg
= defaultbg
;
2010 term
.c
.attr
.mode
|= ATTR_BOLD
;
2013 term
.c
.attr
.mode
|= ATTR_FAINT
;
2016 term
.c
.attr
.mode
|= ATTR_ITALIC
;
2019 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
2021 case 5: /* slow blink */
2023 case 6: /* rapid blink */
2024 term
.c
.attr
.mode
|= ATTR_BLINK
;
2027 term
.c
.attr
.mode
|= ATTR_REVERSE
;
2030 term
.c
.attr
.mode
|= ATTR_INVISIBLE
;
2033 term
.c
.attr
.mode
|= ATTR_STRUCK
;
2036 term
.c
.attr
.mode
&= ~(ATTR_BOLD
| ATTR_FAINT
);
2039 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
2042 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
2045 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
2048 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
2051 term
.c
.attr
.mode
&= ~ATTR_INVISIBLE
;
2054 term
.c
.attr
.mode
&= ~ATTR_STRUCK
;
2057 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
2058 term
.c
.attr
.fg
= idx
;
2061 term
.c
.attr
.fg
= defaultfg
;
2064 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
2065 term
.c
.attr
.bg
= idx
;
2068 term
.c
.attr
.bg
= defaultbg
;
2071 if (BETWEEN(attr
[i
], 30, 37)) {
2072 term
.c
.attr
.fg
= attr
[i
] - 30;
2073 } else if (BETWEEN(attr
[i
], 40, 47)) {
2074 term
.c
.attr
.bg
= attr
[i
] - 40;
2075 } else if (BETWEEN(attr
[i
], 90, 97)) {
2076 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
2077 } else if (BETWEEN(attr
[i
], 100, 107)) {
2078 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
2081 "erresc(default): gfx attr %d unknown\n",
2082 attr
[i
]), csidump();
2090 tsetscroll(int t
, int b
)
2094 LIMIT(t
, 0, term
.row
-1);
2095 LIMIT(b
, 0, term
.row
-1);
2106 tsetmode(int priv
, int set
, int *args
, int narg
)
2111 for (lim
= args
+ narg
; args
< lim
; ++args
) {
2114 case 1: /* DECCKM -- Cursor key */
2115 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
2117 case 5: /* DECSCNM -- Reverse video */
2119 MODBIT(term
.mode
, set
, MODE_REVERSE
);
2120 if (mode
!= term
.mode
)
2123 case 6: /* DECOM -- Origin */
2124 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
2127 case 7: /* DECAWM -- Auto wrap */
2128 MODBIT(term
.mode
, set
, MODE_WRAP
);
2130 case 0: /* Error (IGNORED) */
2131 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
2132 case 3: /* DECCOLM -- Column (IGNORED) */
2133 case 4: /* DECSCLM -- Scroll (IGNORED) */
2134 case 8: /* DECARM -- Auto repeat (IGNORED) */
2135 case 18: /* DECPFF -- Printer feed (IGNORED) */
2136 case 19: /* DECPEX -- Printer extent (IGNORED) */
2137 case 42: /* DECNRCM -- National characters (IGNORED) */
2138 case 12: /* att610 -- Start blinking cursor (IGNORED) */
2140 case 25: /* DECTCEM -- Text Cursor Enable Mode */
2141 MODBIT(term
.mode
, !set
, MODE_HIDE
);
2143 case 9: /* X10 mouse compatibility mode */
2144 xsetpointermotion(0);
2145 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2146 MODBIT(term
.mode
, set
, MODE_MOUSEX10
);
2148 case 1000: /* 1000: report button press */
2149 xsetpointermotion(0);
2150 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2151 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
2153 case 1002: /* 1002: report motion on button press */
2154 xsetpointermotion(0);
2155 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2156 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
2158 case 1003: /* 1003: enable all mouse motions */
2159 xsetpointermotion(set
);
2160 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2161 MODBIT(term
.mode
, set
, MODE_MOUSEMANY
);
2163 case 1004: /* 1004: send focus events to tty */
2164 MODBIT(term
.mode
, set
, MODE_FOCUS
);
2166 case 1006: /* 1006: extended reporting mode */
2167 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
2170 MODBIT(term
.mode
, set
, MODE_8BIT
);
2172 case 1049: /* swap screen & set/restore cursor as xterm */
2173 if (!allowaltscreen
)
2175 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
2177 case 47: /* swap screen */
2179 if (!allowaltscreen
)
2181 alt
= IS_SET(MODE_ALTSCREEN
);
2183 tclearregion(0, 0, term
.col
-1,
2186 if (set
^ alt
) /* set is always 1 or 0 */
2192 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
2194 case 2004: /* 2004: bracketed paste mode */
2195 MODBIT(term
.mode
, set
, MODE_BRCKTPASTE
);
2197 /* Not implemented mouse modes. See comments there. */
2198 case 1001: /* mouse highlight mode; can hang the
2199 terminal by design when implemented. */
2200 case 1005: /* UTF-8 mouse mode; will confuse
2201 applications not supporting UTF-8
2203 case 1015: /* urxvt mangled mouse mode; incompatible
2204 and can be mistaken for other control
2208 "erresc: unknown private set/reset mode %d\n",
2214 case 0: /* Error (IGNORED) */
2216 case 2: /* KAM -- keyboard action */
2217 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
2219 case 4: /* IRM -- Insertion-replacement */
2220 MODBIT(term
.mode
, set
, MODE_INSERT
);
2222 case 12: /* SRM -- Send/Receive */
2223 MODBIT(term
.mode
, !set
, MODE_ECHO
);
2225 case 20: /* LNM -- Linefeed/new line */
2226 MODBIT(term
.mode
, set
, MODE_CRLF
);
2230 "erresc: unknown set/reset mode %d\n",
2244 switch (csiescseq
.mode
[0]) {
2247 fprintf(stderr
, "erresc: unknown csi ");
2251 case '@': /* ICH -- Insert <n> blank char */
2252 DEFAULT(csiescseq
.arg
[0], 1);
2253 tinsertblank(csiescseq
.arg
[0]);
2255 case 'A': /* CUU -- Cursor <n> Up */
2256 DEFAULT(csiescseq
.arg
[0], 1);
2257 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
2259 case 'B': /* CUD -- Cursor <n> Down */
2260 case 'e': /* VPR --Cursor <n> Down */
2261 DEFAULT(csiescseq
.arg
[0], 1);
2262 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
2264 case 'i': /* MC -- Media Copy */
2265 switch (csiescseq
.arg
[0]) {
2270 tdumpline(term
.c
.y
);
2276 term
.mode
&= ~MODE_PRINT
;
2279 term
.mode
|= MODE_PRINT
;
2283 case 'c': /* DA -- Device Attributes */
2284 if (csiescseq
.arg
[0] == 0)
2285 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2287 case 'C': /* CUF -- Cursor <n> Forward */
2288 case 'a': /* HPR -- Cursor <n> Forward */
2289 DEFAULT(csiescseq
.arg
[0], 1);
2290 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
2292 case 'D': /* CUB -- Cursor <n> Backward */
2293 DEFAULT(csiescseq
.arg
[0], 1);
2294 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
2296 case 'E': /* CNL -- Cursor <n> Down and first col */
2297 DEFAULT(csiescseq
.arg
[0], 1);
2298 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
2300 case 'F': /* CPL -- Cursor <n> Up and first col */
2301 DEFAULT(csiescseq
.arg
[0], 1);
2302 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
2304 case 'g': /* TBC -- Tabulation clear */
2305 switch (csiescseq
.arg
[0]) {
2306 case 0: /* clear current tab stop */
2307 term
.tabs
[term
.c
.x
] = 0;
2309 case 3: /* clear all the tabs */
2310 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
2316 case 'G': /* CHA -- Move to <col> */
2318 DEFAULT(csiescseq
.arg
[0], 1);
2319 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
2321 case 'H': /* CUP -- Move to <row> <col> */
2323 DEFAULT(csiescseq
.arg
[0], 1);
2324 DEFAULT(csiescseq
.arg
[1], 1);
2325 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
2327 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
2328 DEFAULT(csiescseq
.arg
[0], 1);
2329 tputtab(csiescseq
.arg
[0]);
2331 case 'J': /* ED -- Clear screen */
2333 switch (csiescseq
.arg
[0]) {
2335 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
2336 if (term
.c
.y
< term
.row
-1) {
2337 tclearregion(0, term
.c
.y
+1, term
.col
-1,
2343 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
2344 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2347 tclearregion(0, 0, term
.col
-1, term
.row
-1);
2353 case 'K': /* EL -- Clear line */
2354 switch (csiescseq
.arg
[0]) {
2356 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
2360 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2363 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
2367 case 'S': /* SU -- Scroll <n> line up */
2368 DEFAULT(csiescseq
.arg
[0], 1);
2369 tscrollup(term
.top
, csiescseq
.arg
[0]);
2371 case 'T': /* SD -- Scroll <n> line down */
2372 DEFAULT(csiescseq
.arg
[0], 1);
2373 tscrolldown(term
.top
, csiescseq
.arg
[0]);
2375 case 'L': /* IL -- Insert <n> blank lines */
2376 DEFAULT(csiescseq
.arg
[0], 1);
2377 tinsertblankline(csiescseq
.arg
[0]);
2379 case 'l': /* RM -- Reset Mode */
2380 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
2382 case 'M': /* DL -- Delete <n> lines */
2383 DEFAULT(csiescseq
.arg
[0], 1);
2384 tdeleteline(csiescseq
.arg
[0]);
2386 case 'X': /* ECH -- Erase <n> char */
2387 DEFAULT(csiescseq
.arg
[0], 1);
2388 tclearregion(term
.c
.x
, term
.c
.y
,
2389 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
2391 case 'P': /* DCH -- Delete <n> char */
2392 DEFAULT(csiescseq
.arg
[0], 1);
2393 tdeletechar(csiescseq
.arg
[0]);
2395 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
2396 DEFAULT(csiescseq
.arg
[0], 1);
2397 tputtab(-csiescseq
.arg
[0]);
2399 case 'd': /* VPA -- Move to <row> */
2400 DEFAULT(csiescseq
.arg
[0], 1);
2401 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
2403 case 'h': /* SM -- Set terminal mode */
2404 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
2406 case 'm': /* SGR -- Terminal attribute (color) */
2407 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
2409 case 'n': /* DSR – Device Status Report (cursor position) */
2410 if (csiescseq
.arg
[0] == 6) {
2411 len
= snprintf(buf
, sizeof(buf
),"\033[%i;%iR",
2412 term
.c
.y
+1, term
.c
.x
+1);
2416 case 'r': /* DECSTBM -- Set Scrolling Region */
2417 if (csiescseq
.priv
) {
2420 DEFAULT(csiescseq
.arg
[0], 1);
2421 DEFAULT(csiescseq
.arg
[1], term
.row
);
2422 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
2426 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2427 tcursor(CURSOR_SAVE
);
2429 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2430 tcursor(CURSOR_LOAD
);
2433 switch (csiescseq
.mode
[1]) {
2434 case 'q': /* DECSCUSR -- Set Cursor Style */
2435 DEFAULT(csiescseq
.arg
[0], 1);
2436 if (!BETWEEN(csiescseq
.arg
[0], 0, 6)) {
2439 xw
.cursor
= csiescseq
.arg
[0];
2455 for (i
= 0; i
< csiescseq
.len
; i
++) {
2456 c
= csiescseq
.buf
[i
] & 0xff;
2459 } else if (c
== '\n') {
2461 } else if (c
== '\r') {
2463 } else if (c
== 0x1b) {
2466 printf("(%02x)", c
);
2475 memset(&csiescseq
, 0, sizeof(csiescseq
));
2484 term
.esc
&= ~(ESC_STR_END
|ESC_STR
);
2486 par
= (narg
= strescseq
.narg
) ? atoi(strescseq
.args
[0]) : 0;
2488 switch (strescseq
.type
) {
2489 case ']': /* OSC -- Operating System Command */
2495 xsettitle(strescseq
.args
[1]);
2497 case 4: /* color set */
2500 p
= strescseq
.args
[2];
2502 case 104: /* color reset, here p = NULL */
2503 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2504 if (xsetcolorname(j
, p
)) {
2505 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2508 * TODO if defaultbg color is changed, borders
2516 case 'k': /* old title set compatibility */
2517 xsettitle(strescseq
.args
[0]);
2519 case 'P': /* DCS -- Device Control String */
2520 case '_': /* APC -- Application Program Command */
2521 case '^': /* PM -- Privacy Message */
2525 fprintf(stderr
, "erresc: unknown str ");
2533 char *p
= strescseq
.buf
;
2536 strescseq
.buf
[strescseq
.len
] = '\0';
2541 while (strescseq
.narg
< STR_ARG_SIZ
) {
2542 strescseq
.args
[strescseq
.narg
++] = p
;
2543 while ((c
= *p
) != ';' && c
!= '\0')
2557 printf("ESC%c", strescseq
.type
);
2558 for (i
= 0; i
< strescseq
.len
; i
++) {
2559 c
= strescseq
.buf
[i
] & 0xff;
2562 } else if (isprint(c
)) {
2564 } else if (c
== '\n') {
2566 } else if (c
== '\r') {
2568 } else if (c
== 0x1b) {
2571 printf("(%02x)", c
);
2580 memset(&strescseq
, 0, sizeof(strescseq
));
2584 sendbreak(const Arg
*arg
)
2586 if (tcsendbreak(cmdfd
, 0))
2587 perror("Error sending break");
2591 tprinter(char *s
, size_t len
)
2593 if (iofd
!= -1 && xwrite(iofd
, s
, len
) < 0) {
2594 fprintf(stderr
, "Error writing in %s:%s\n",
2595 opt_io
, strerror(errno
));
2602 toggleprinter(const Arg
*arg
)
2604 term
.mode
^= MODE_PRINT
;
2608 printscreen(const Arg
*arg
)
2614 printsel(const Arg
*arg
)
2624 if ((ptr
= getsel())) {
2625 tprinter(ptr
, strlen(ptr
));
2636 bp
= &term
.line
[n
][0];
2637 end
= &bp
[MIN(tlinelen(n
), term
.col
) - 1];
2638 if (bp
!= end
|| bp
->u
!= ' ') {
2639 for ( ;bp
<= end
; ++bp
)
2640 tprinter(buf
, utf8encode(bp
->u
, buf
));
2650 for (i
= 0; i
< term
.row
; ++i
)
2660 while (x
< term
.col
&& n
--)
2661 for (++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2664 while (x
> 0 && n
++)
2665 for (--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2668 term
.c
.x
= LIMIT(x
, 0, term
.col
-1);
2674 if (ISCONTROL(u
)) { /* control code */
2679 } else if (u
!= '\n' && u
!= '\r' && u
!= '\t') {
2688 tdeftran(char ascii
)
2690 static char cs
[] = "0B";
2691 static int vcs
[] = {CS_GRAPHIC0
, CS_USA
};
2694 if ((p
= strchr(cs
, ascii
)) == NULL
) {
2695 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2697 term
.trantbl
[term
.icharset
] = vcs
[p
- cs
];
2706 if (c
== '8') { /* DEC screen alignment test. */
2707 for (x
= 0; x
< term
.col
; ++x
) {
2708 for (y
= 0; y
< term
.row
; ++y
)
2709 tsetchar('E', &term
.c
.attr
, x
, y
);
2715 tstrsequence(uchar c
)
2718 case 0x90: /* DCS -- Device Control String */
2721 case 0x9f: /* APC -- Application Program Command */
2724 case 0x9e: /* PM -- Privacy Message */
2727 case 0x9d: /* OSC -- Operating System Command */
2733 term
.esc
|= ESC_STR
;
2737 tcontrolcode(uchar ascii
)
2744 tmoveto(term
.c
.x
-1, term
.c
.y
);
2747 tmoveto(0, term
.c
.y
);
2752 /* go to first col if the mode is set */
2753 tnewline(IS_SET(MODE_CRLF
));
2755 case '\a': /* BEL */
2756 if (term
.esc
& ESC_STR_END
) {
2757 /* backwards compatibility to xterm */
2760 if (!(xw
.state
& WIN_FOCUSED
))
2763 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
2766 case '\033': /* ESC */
2768 term
.esc
&= ~(ESC_CSI
|ESC_ALTCHARSET
|ESC_TEST
);
2769 term
.esc
|= ESC_START
;
2771 case '\016': /* SO (LS1 -- Locking shift 1) */
2772 case '\017': /* SI (LS0 -- Locking shift 0) */
2773 term
.charset
= 1 - (ascii
- '\016');
2775 case '\032': /* SUB */
2776 tsetchar('?', &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2777 case '\030': /* CAN */
2780 case '\005': /* ENQ (IGNORED) */
2781 case '\000': /* NUL (IGNORED) */
2782 case '\021': /* XON (IGNORED) */
2783 case '\023': /* XOFF (IGNORED) */
2784 case 0177: /* DEL (IGNORED) */
2786 case 0x80: /* TODO: PAD */
2787 case 0x81: /* TODO: HOP */
2788 case 0x82: /* TODO: BPH */
2789 case 0x83: /* TODO: NBH */
2790 case 0x84: /* TODO: IND */
2792 case 0x85: /* NEL -- Next line */
2793 tnewline(1); /* always go to first col */
2795 case 0x86: /* TODO: SSA */
2796 case 0x87: /* TODO: ESA */
2798 case 0x88: /* HTS -- Horizontal tab stop */
2799 term
.tabs
[term
.c
.x
] = 1;
2801 case 0x89: /* TODO: HTJ */
2802 case 0x8a: /* TODO: VTS */
2803 case 0x8b: /* TODO: PLD */
2804 case 0x8c: /* TODO: PLU */
2805 case 0x8d: /* TODO: RI */
2806 case 0x8e: /* TODO: SS2 */
2807 case 0x8f: /* TODO: SS3 */
2808 case 0x91: /* TODO: PU1 */
2809 case 0x92: /* TODO: PU2 */
2810 case 0x93: /* TODO: STS */
2811 case 0x94: /* TODO: CCH */
2812 case 0x95: /* TODO: MW */
2813 case 0x96: /* TODO: SPA */
2814 case 0x97: /* TODO: EPA */
2815 case 0x98: /* TODO: SOS */
2816 case 0x99: /* TODO: SGCI */
2818 case 0x9a: /* DECID -- Identify Terminal */
2819 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2821 case 0x9b: /* TODO: CSI */
2822 case 0x9c: /* TODO: ST */
2824 case 0x90: /* DCS -- Device Control String */
2825 case 0x9d: /* OSC -- Operating System Command */
2826 case 0x9e: /* PM -- Privacy Message */
2827 case 0x9f: /* APC -- Application Program Command */
2828 tstrsequence(ascii
);
2831 /* only CAN, SUB, \a and C1 chars interrupt a sequence */
2832 term
.esc
&= ~(ESC_STR_END
|ESC_STR
);
2836 * returns 1 when the sequence is finished and it hasn't to read
2837 * more characters for this sequence, otherwise 0
2840 eschandle(uchar ascii
)
2844 term
.esc
|= ESC_CSI
;
2847 term
.esc
|= ESC_TEST
;
2849 case 'P': /* DCS -- Device Control String */
2850 case '_': /* APC -- Application Program Command */
2851 case '^': /* PM -- Privacy Message */
2852 case ']': /* OSC -- Operating System Command */
2853 case 'k': /* old title set compatibility */
2854 tstrsequence(ascii
);
2856 case 'n': /* LS2 -- Locking shift 2 */
2857 case 'o': /* LS3 -- Locking shift 3 */
2858 term
.charset
= 2 + (ascii
- 'n');
2860 case '(': /* GZD4 -- set primary charset G0 */
2861 case ')': /* G1D4 -- set secondary charset G1 */
2862 case '*': /* G2D4 -- set tertiary charset G2 */
2863 case '+': /* G3D4 -- set quaternary charset G3 */
2864 term
.icharset
= ascii
- '(';
2865 term
.esc
|= ESC_ALTCHARSET
;
2867 case 'D': /* IND -- Linefeed */
2868 if (term
.c
.y
== term
.bot
) {
2869 tscrollup(term
.top
, 1);
2871 tmoveto(term
.c
.x
, term
.c
.y
+1);
2874 case 'E': /* NEL -- Next line */
2875 tnewline(1); /* always go to first col */
2877 case 'H': /* HTS -- Horizontal tab stop */
2878 term
.tabs
[term
.c
.x
] = 1;
2880 case 'M': /* RI -- Reverse index */
2881 if (term
.c
.y
== term
.top
) {
2882 tscrolldown(term
.top
, 1);
2884 tmoveto(term
.c
.x
, term
.c
.y
-1);
2887 case 'Z': /* DECID -- Identify Terminal */
2888 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2890 case 'c': /* RIS -- Reset to inital state */
2895 case '=': /* DECPAM -- Application keypad */
2896 term
.mode
|= MODE_APPKEYPAD
;
2898 case '>': /* DECPNM -- Normal keypad */
2899 term
.mode
&= ~MODE_APPKEYPAD
;
2901 case '7': /* DECSC -- Save Cursor */
2902 tcursor(CURSOR_SAVE
);
2904 case '8': /* DECRC -- Restore Cursor */
2905 tcursor(CURSOR_LOAD
);
2907 case '\\': /* ST -- String Terminator */
2908 if (term
.esc
& ESC_STR_END
)
2912 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2913 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2927 control
= ISCONTROL(u
);
2928 len
= utf8encode(u
, c
);
2929 if (!control
&& (width
= wcwidth(u
)) == -1) {
2930 memcpy(c
, "\357\277\275", 4); /* UTF_INVALID */
2934 if (IS_SET(MODE_PRINT
))
2938 * STR sequence must be checked before anything else
2939 * because it uses all following characters until it
2940 * receives a ESC, a SUB, a ST or any other C1 control
2943 if (term
.esc
& ESC_STR
) {
2944 if (u
== '\a' || u
== 030 || u
== 032 || u
== 033 ||
2946 term
.esc
&= ~(ESC_START
|ESC_STR
);
2947 term
.esc
|= ESC_STR_END
;
2948 } else if (strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2949 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2950 strescseq
.len
+= len
;
2954 * Here is a bug in terminals. If the user never sends
2955 * some code to stop the str or esc command, then st
2956 * will stop responding. But this is better than
2957 * silently failing with unknown characters. At least
2958 * then users will report back.
2960 * In the case users ever get fixed, here is the code:
2971 * Actions of control codes must be performed as soon they arrive
2972 * because they can be embedded inside a control sequence, and
2973 * they must not cause conflicts with sequences.
2978 * control codes are not shown ever
2981 } else if (term
.esc
& ESC_START
) {
2982 if (term
.esc
& ESC_CSI
) {
2983 csiescseq
.buf
[csiescseq
.len
++] = u
;
2984 if (BETWEEN(u
, 0x40, 0x7E)
2985 || csiescseq
.len
>= \
2986 sizeof(csiescseq
.buf
)-1) {
2992 } else if (term
.esc
& ESC_ALTCHARSET
) {
2994 } else if (term
.esc
& ESC_TEST
) {
2999 /* sequence already finished */
3003 * All characters which form part of a sequence are not
3008 if (sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
3011 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
3012 if (IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
3013 gp
->mode
|= ATTR_WRAP
;
3015 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
3018 if (IS_SET(MODE_INSERT
) && term
.c
.x
+width
< term
.col
)
3019 memmove(gp
+width
, gp
, (term
.col
- term
.c
.x
- width
) * sizeof(Glyph
));
3021 if (term
.c
.x
+width
> term
.col
) {
3023 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
3026 tsetchar(u
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
3029 gp
->mode
|= ATTR_WIDE
;
3030 if (term
.c
.x
+1 < term
.col
) {
3032 gp
[1].mode
= ATTR_WDUMMY
;
3035 if (term
.c
.x
+width
< term
.col
) {
3036 tmoveto(term
.c
.x
+width
, term
.c
.y
);
3038 term
.c
.state
|= CURSOR_WRAPNEXT
;
3043 tresize(int col
, int row
)
3046 int minrow
= MIN(row
, term
.row
);
3047 int mincol
= MIN(col
, term
.col
);
3051 if (col
< 1 || row
< 1) {
3053 "tresize: error resizing to %dx%d\n", col
, row
);
3058 * slide screen to keep cursor where we expect it -
3059 * tscrollup would work here, but we can optimize to
3060 * memmove because we're freeing the earlier lines
3062 for (i
= 0; i
<= term
.c
.y
- row
; i
++) {
3066 /* ensure that both src and dst are not NULL */
3068 memmove(term
.line
, term
.line
+ i
, row
* sizeof(Line
));
3069 memmove(term
.alt
, term
.alt
+ i
, row
* sizeof(Line
));
3071 for (i
+= row
; i
< term
.row
; i
++) {
3076 /* resize to new width */
3077 term
.specbuf
= xrealloc(term
.specbuf
, col
* sizeof(XftGlyphFontSpec
));
3079 /* resize to new height */
3080 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
3081 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
3082 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
3083 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
3085 /* resize each row to new width, zero-pad if needed */
3086 for (i
= 0; i
< minrow
; i
++) {
3087 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
3088 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
3091 /* allocate any new rows */
3092 for (/* i == minrow */; i
< row
; i
++) {
3093 term
.line
[i
] = xmalloc(col
* sizeof(Glyph
));
3094 term
.alt
[i
] = xmalloc(col
* sizeof(Glyph
));
3096 if (col
> term
.col
) {
3097 bp
= term
.tabs
+ term
.col
;
3099 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
3100 while (--bp
> term
.tabs
&& !*bp
)
3102 for (bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
3105 /* update terminal size */
3108 /* reset scrolling region */
3109 tsetscroll(0, row
-1);
3110 /* make use of the LIMIT in tmoveto */
3111 tmoveto(term
.c
.x
, term
.c
.y
);
3112 /* Clearing both screens (it makes dirty all lines) */
3114 for (i
= 0; i
< 2; i
++) {
3115 if (mincol
< col
&& 0 < minrow
) {
3116 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
3118 if (0 < col
&& minrow
< row
) {
3119 tclearregion(0, minrow
, col
- 1, row
- 1);
3122 tcursor(CURSOR_LOAD
);
3128 xresize(int col
, int row
)
3130 xw
.tw
= MAX(1, col
* xw
.cw
);
3131 xw
.th
= MAX(1, row
* xw
.ch
);
3133 XFreePixmap(xw
.dpy
, xw
.buf
);
3134 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
3135 DefaultDepth(xw
.dpy
, xw
.scr
));
3136 XftDrawChange(xw
.draw
, xw
.buf
);
3137 xclear(0, 0, xw
.w
, xw
.h
);
3141 sixd_to_16bit(int x
)
3143 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
3147 xloadcolor(int i
, const char *name
, Color
*ncolor
)
3149 XRenderColor color
= { .alpha
= 0xffff };
3152 if (BETWEEN(i
, 16, 255)) { /* 256 color */
3153 if (i
< 6*6*6+16) { /* same colors as xterm */
3154 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
3155 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
3156 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
3157 } else { /* greyscale */
3158 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
3159 color
.green
= color
.blue
= color
.red
;
3161 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
3162 xw
.cmap
, &color
, ncolor
);
3164 name
= colorname
[i
];
3167 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
3178 for (cp
= dc
.col
; cp
< &dc
.col
[LEN(dc
.col
)]; ++cp
)
3179 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
3182 for (i
= 0; i
< LEN(dc
.col
); i
++)
3183 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
3185 die("Could not allocate color '%s'\n", colorname
[i
]);
3187 die("Could not allocate color %d\n", i
);
3193 xsetcolorname(int x
, const char *name
)
3197 if (!BETWEEN(x
, 0, LEN(dc
.col
)))
3201 if (!xloadcolor(x
, name
, &ncolor
))
3204 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
3211 xtermclear(int col1
, int row1
, int col2
, int row2
)
3213 XftDrawRect(xw
.draw
,
3214 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
3215 borderpx
+ col1
* xw
.cw
,
3216 borderpx
+ row1
* xw
.ch
,
3217 (col2
-col1
+1) * xw
.cw
,
3218 (row2
-row1
+1) * xw
.ch
);
3222 * Absolute coordinates.
3225 xclear(int x1
, int y1
, int x2
, int y2
)
3227 XftDrawRect(xw
.draw
,
3228 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
3229 x1
, y1
, x2
-x1
, y2
-y1
);
3235 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
3236 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
3237 XSizeHints
*sizeh
= NULL
;
3239 sizeh
= XAllocSizeHints();
3241 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
3242 sizeh
->height
= xw
.h
;
3243 sizeh
->width
= xw
.w
;
3244 sizeh
->height_inc
= xw
.ch
;
3245 sizeh
->width_inc
= xw
.cw
;
3246 sizeh
->base_height
= 2 * borderpx
;
3247 sizeh
->base_width
= 2 * borderpx
;
3249 sizeh
->flags
|= PMaxSize
| PMinSize
;
3250 sizeh
->min_width
= sizeh
->max_width
= xw
.w
;
3251 sizeh
->min_height
= sizeh
->max_height
= xw
.h
;
3253 if (xw
.gm
& (XValue
|YValue
)) {
3254 sizeh
->flags
|= USPosition
| PWinGravity
;
3257 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
3260 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
3266 xgeommasktogravity(int mask
)
3268 switch (mask
& (XNegative
|YNegative
)) {
3270 return NorthWestGravity
;
3272 return NorthEastGravity
;
3274 return SouthWestGravity
;
3277 return SouthEastGravity
;
3281 xloadfont(Font
*f
, FcPattern
*pattern
)
3286 match
= FcFontMatch(NULL
, pattern
, &result
);
3290 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
3291 FcPatternDestroy(match
);
3296 f
->pattern
= FcPatternDuplicate(pattern
);
3298 f
->ascent
= f
->match
->ascent
;
3299 f
->descent
= f
->match
->descent
;
3301 f
->rbearing
= f
->match
->max_advance_width
;
3303 f
->height
= f
->ascent
+ f
->descent
;
3304 f
->width
= f
->lbearing
+ f
->rbearing
;
3310 xloadfonts(char *fontstr
, double fontsize
)
3316 if (fontstr
[0] == '-') {
3317 pattern
= XftXlfdParse(fontstr
, False
, False
);
3319 pattern
= FcNameParse((FcChar8
*)fontstr
);
3323 die("st: can't open font %s\n", fontstr
);
3326 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
3327 FcPatternDel(pattern
, FC_SIZE
);
3328 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
3329 usedfontsize
= fontsize
;
3331 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
3333 usedfontsize
= fontval
;
3334 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
3339 * Default font size is 12, if none given. This is to
3340 * have a known usedfontsize value.
3342 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
3345 defaultfontsize
= usedfontsize
;
3348 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
3349 FcDefaultSubstitute(pattern
);
3351 if (xloadfont(&dc
.font
, pattern
))
3352 die("st: can't open font %s\n", fontstr
);
3354 if (usedfontsize
< 0) {
3355 FcPatternGetDouble(dc
.font
.match
->pattern
,
3356 FC_PIXEL_SIZE
, 0, &fontval
);
3357 usedfontsize
= fontval
;
3359 defaultfontsize
= fontval
;
3362 /* Setting character width and height. */
3363 xw
.cw
= ceilf(dc
.font
.width
* cwscale
);
3364 xw
.ch
= ceilf(dc
.font
.height
* chscale
);
3366 FcPatternDel(pattern
, FC_SLANT
);
3367 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
3368 if (xloadfont(&dc
.ifont
, pattern
))
3369 die("st: can't open font %s\n", fontstr
);
3371 FcPatternDel(pattern
, FC_WEIGHT
);
3372 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
3373 if (xloadfont(&dc
.ibfont
, pattern
))
3374 die("st: can't open font %s\n", fontstr
);
3376 FcPatternDel(pattern
, FC_SLANT
);
3377 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
3378 if (xloadfont(&dc
.bfont
, pattern
))
3379 die("st: can't open font %s\n", fontstr
);
3381 FcPatternDestroy(pattern
);
3385 xunloadfont(Font
*f
)
3387 XftFontClose(xw
.dpy
, f
->match
);
3388 FcPatternDestroy(f
->pattern
);
3390 FcFontSetDestroy(f
->set
);
3396 /* Free the loaded fonts in the font cache. */
3398 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
3400 xunloadfont(&dc
.font
);
3401 xunloadfont(&dc
.bfont
);
3402 xunloadfont(&dc
.ifont
);
3403 xunloadfont(&dc
.ibfont
);
3407 xzoom(const Arg
*arg
)
3411 larg
.f
= usedfontsize
+ arg
->f
;
3416 xzoomabs(const Arg
*arg
)
3419 xloadfonts(usedfont
, arg
->f
);
3426 xzoomreset(const Arg
*arg
)
3430 if (defaultfontsize
> 0) {
3431 larg
.f
= defaultfontsize
;
3442 pid_t thispid
= getpid();
3443 XColor xmousefg
, xmousebg
;
3445 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
3446 die("Can't open display\n");
3447 xw
.scr
= XDefaultScreen(xw
.dpy
);
3448 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
3452 die("Could not init fontconfig.\n");
3454 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
3455 xloadfonts(usedfont
, 0);
3458 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
3461 /* adjust fixed window geometry */
3462 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
3463 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
3464 if (xw
.gm
& XNegative
)
3465 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - xw
.w
- 2;
3466 if (xw
.gm
& YNegative
)
3467 xw
.t
+= DisplayWidth(xw
.dpy
, xw
.scr
) - xw
.h
- 2;
3470 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
3471 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
3472 xw
.attrs
.bit_gravity
= NorthWestGravity
;
3473 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
3474 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
3475 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
3476 xw
.attrs
.colormap
= xw
.cmap
;
3478 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
3479 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
3480 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
3481 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
3482 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
3483 | CWEventMask
| CWColormap
, &xw
.attrs
);
3485 memset(&gcvalues
, 0, sizeof(gcvalues
));
3486 gcvalues
.graphics_exposures
= False
;
3487 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
3489 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
3490 DefaultDepth(xw
.dpy
, xw
.scr
));
3491 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
3492 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
3494 /* Xft rendering context */
3495 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
3498 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3499 XSetLocaleModifiers("@im=local");
3500 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3501 XSetLocaleModifiers("@im=");
3502 if ((xw
.xim
= XOpenIM(xw
.dpy
,
3503 NULL
, NULL
, NULL
)) == NULL
) {
3504 die("XOpenIM failed. Could not open input"
3509 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
3510 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
3511 XNFocusWindow
, xw
.win
, NULL
);
3513 die("XCreateIC failed. Could not obtain input method.\n");
3515 /* white cursor, black outline */
3516 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
3517 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
3519 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
3520 xmousefg
.red
= 0xffff;
3521 xmousefg
.green
= 0xffff;
3522 xmousefg
.blue
= 0xffff;
3525 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
3526 xmousebg
.red
= 0x0000;
3527 xmousebg
.green
= 0x0000;
3528 xmousebg
.blue
= 0x0000;
3531 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
3533 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
3534 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
3535 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
3536 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
3538 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
3539 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
3540 PropModeReplace
, (uchar
*)&thispid
, 1);
3543 XMapWindow(xw
.dpy
, xw
.win
);
3545 XSync(xw
.dpy
, False
);
3549 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
3551 float winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
, xp
, yp
;
3552 ushort mode
, prevmode
= USHRT_MAX
;
3553 Font
*font
= &dc
.font
;
3554 int frcflags
= FRC_NORMAL
;
3555 float runewidth
= xw
.cw
;
3559 FcPattern
*fcpattern
, *fontpattern
;
3560 FcFontSet
*fcsets
[] = { NULL
};
3561 FcCharSet
*fccharset
;
3562 int i
, f
, numspecs
= 0;
3564 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
3565 /* Fetch rune and mode for current glyph. */
3567 mode
= glyphs
[i
].mode
;
3569 /* Skip dummy wide-character spacing. */
3570 if (mode
== ATTR_WDUMMY
)
3573 /* Determine font for glyph if different from previous glyph. */
3574 if (prevmode
!= mode
) {
3577 frcflags
= FRC_NORMAL
;
3578 runewidth
= xw
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
3579 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
3581 frcflags
= FRC_ITALICBOLD
;
3582 } else if (mode
& ATTR_ITALIC
) {
3584 frcflags
= FRC_ITALIC
;
3585 } else if (mode
& ATTR_BOLD
) {
3587 frcflags
= FRC_BOLD
;
3589 yp
= winy
+ font
->ascent
;
3592 /* Lookup character index with default font. */
3593 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
3595 specs
[numspecs
].font
= font
->match
;
3596 specs
[numspecs
].glyph
= glyphidx
;
3597 specs
[numspecs
].x
= (short)xp
;
3598 specs
[numspecs
].y
= (short)yp
;
3604 /* Fallback on font cache, search the font cache for match. */
3605 for (f
= 0; f
< frclen
; f
++) {
3606 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
3607 /* Everything correct. */
3608 if (glyphidx
&& frc
[f
].flags
== frcflags
)
3610 /* We got a default font for a not found glyph. */
3611 if (!glyphidx
&& frc
[f
].flags
== frcflags
3612 && frc
[f
].unicodep
== rune
) {
3617 /* Nothing was found. Use fontconfig to find matching font. */
3620 font
->set
= FcFontSort(0, font
->pattern
,
3622 fcsets
[0] = font
->set
;
3625 * Nothing was found in the cache. Now use
3626 * some dozen of Fontconfig calls to get the
3627 * font for one single character.
3629 * Xft and fontconfig are design failures.
3631 fcpattern
= FcPatternDuplicate(font
->pattern
);
3632 fccharset
= FcCharSetCreate();
3634 FcCharSetAddChar(fccharset
, rune
);
3635 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3637 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
3639 FcConfigSubstitute(0, fcpattern
,
3641 FcDefaultSubstitute(fcpattern
);
3643 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
3647 * Overwrite or create the new cache entry.
3649 if (frclen
>= LEN(frc
)) {
3650 frclen
= LEN(frc
) - 1;
3651 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
3652 frc
[frclen
].unicodep
= 0;
3655 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
3657 frc
[frclen
].flags
= frcflags
;
3658 frc
[frclen
].unicodep
= rune
;
3660 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
3665 FcPatternDestroy(fcpattern
);
3666 FcCharSetDestroy(fccharset
);
3669 specs
[numspecs
].font
= frc
[f
].font
;
3670 specs
[numspecs
].glyph
= glyphidx
;
3671 specs
[numspecs
].x
= (short)xp
;
3672 specs
[numspecs
].y
= (short)(winy
+ frc
[f
].font
->ascent
);
3681 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
3683 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
3684 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
3685 width
= charlen
* xw
.cw
;
3686 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
3687 XRenderColor colfg
, colbg
;
3690 /* Determine foreground and background colors based on mode. */
3691 if (base
.fg
== defaultfg
) {
3692 if (base
.mode
& ATTR_ITALIC
)
3693 base
.fg
= defaultitalic
;
3694 else if ((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
))
3695 base
.fg
= defaultitalic
;
3696 else if (base
.mode
& ATTR_UNDERLINE
)
3697 base
.fg
= defaultunderline
;
3700 if (IS_TRUECOL(base
.fg
)) {
3701 colfg
.alpha
= 0xffff;
3702 colfg
.red
= TRUERED(base
.fg
);
3703 colfg
.green
= TRUEGREEN(base
.fg
);
3704 colfg
.blue
= TRUEBLUE(base
.fg
);
3705 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
3708 fg
= &dc
.col
[base
.fg
];
3711 if (IS_TRUECOL(base
.bg
)) {
3712 colbg
.alpha
= 0xffff;
3713 colbg
.green
= TRUEGREEN(base
.bg
);
3714 colbg
.red
= TRUERED(base
.bg
);
3715 colbg
.blue
= TRUEBLUE(base
.bg
);
3716 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
3719 bg
= &dc
.col
[base
.bg
];
3722 /* Change basic system colors [0-7] to bright system colors [8-15] */
3723 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
3724 fg
= &dc
.col
[base
.fg
+ 8];
3726 if (IS_SET(MODE_REVERSE
)) {
3727 if (fg
== &dc
.col
[defaultfg
]) {
3728 fg
= &dc
.col
[defaultbg
];
3730 colfg
.red
= ~fg
->color
.red
;
3731 colfg
.green
= ~fg
->color
.green
;
3732 colfg
.blue
= ~fg
->color
.blue
;
3733 colfg
.alpha
= fg
->color
.alpha
;
3734 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
3739 if (bg
== &dc
.col
[defaultbg
]) {
3740 bg
= &dc
.col
[defaultfg
];
3742 colbg
.red
= ~bg
->color
.red
;
3743 colbg
.green
= ~bg
->color
.green
;
3744 colbg
.blue
= ~bg
->color
.blue
;
3745 colbg
.alpha
= bg
->color
.alpha
;
3746 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
3752 if (base
.mode
& ATTR_REVERSE
) {
3758 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
3759 colfg
.red
= fg
->color
.red
/ 2;
3760 colfg
.green
= fg
->color
.green
/ 2;
3761 colfg
.blue
= fg
->color
.blue
/ 2;
3762 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
3766 if (base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
3769 if (base
.mode
& ATTR_INVISIBLE
)
3772 /* Intelligent cleaning up of the borders. */
3774 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
3775 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
3777 if (x
+ charlen
>= term
.col
) {
3778 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
3779 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
3782 xclear(winx
, 0, winx
+ width
, borderpx
);
3783 if (y
== term
.row
-1)
3784 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
3786 /* Clean up the region we want to draw to. */
3787 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
3789 /* Set the clip region because Xft is sometimes dirty. */
3794 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3796 /* Render the glyphs. */
3797 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
3799 /* Render underline and strikethrough. */
3800 if (base
.mode
& ATTR_UNDERLINE
) {
3801 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
3805 if (base
.mode
& ATTR_STRUCK
) {
3806 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
3810 /* Reset clip to none. */
3811 XftDrawSetClip(xw
.draw
, 0);
3815 xdrawglyph(Glyph g
, int x
, int y
)
3818 XftGlyphFontSpec spec
;
3819 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
3820 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
3826 static int oldx
= 0, oldy
= 0;
3828 Glyph g
= {' ', ATTR_NULL
, defaultbg
, defaultcs
};
3830 LIMIT(oldx
, 0, term
.col
-1);
3831 LIMIT(oldy
, 0, term
.row
-1);
3835 /* adjust position if in dummy */
3836 if (term
.line
[oldy
][oldx
].mode
& ATTR_WDUMMY
)
3838 if (term
.line
[term
.c
.y
][curx
].mode
& ATTR_WDUMMY
)
3841 g
.u
= term
.line
[term
.c
.y
][term
.c
.x
].u
;
3843 /* remove the old cursor */
3844 xdrawglyph(term
.line
[oldy
][oldx
], oldx
, oldy
);
3846 if (IS_SET(MODE_HIDE
))
3849 /* draw the new one */
3850 if (xw
.state
& WIN_FOCUSED
) {
3851 switch (xw
.cursor
) {
3852 case 0: /* Blinking Block */
3853 case 1: /* Blinking Block (Default) */
3854 case 2: /* Steady Block */
3855 if (IS_SET(MODE_REVERSE
)) {
3856 g
.mode
|= ATTR_REVERSE
;
3861 g
.mode
|= term
.line
[term
.c
.y
][curx
].mode
& ATTR_WIDE
;
3862 xdrawglyph(g
, term
.c
.x
, term
.c
.y
);
3864 case 3: /* Blinking Underline */
3865 case 4: /* Steady Underline */
3866 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3867 borderpx
+ curx
* xw
.cw
,
3868 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- cursorthickness
,
3869 xw
.cw
, cursorthickness
);
3871 case 5: /* Blinking bar */
3872 case 6: /* Steady bar */
3873 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3874 borderpx
+ curx
* xw
.cw
,
3875 borderpx
+ term
.c
.y
* xw
.ch
,
3876 cursorthickness
, xw
.ch
);
3880 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3881 borderpx
+ curx
* xw
.cw
,
3882 borderpx
+ term
.c
.y
* xw
.ch
,
3884 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3885 borderpx
+ curx
* xw
.cw
,
3886 borderpx
+ term
.c
.y
* xw
.ch
,
3888 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3889 borderpx
+ (curx
+ 1) * xw
.cw
- 1,
3890 borderpx
+ term
.c
.y
* xw
.ch
,
3892 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3893 borderpx
+ curx
* xw
.cw
,
3894 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3897 oldx
= curx
, oldy
= term
.c
.y
;
3906 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3908 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3909 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
3916 xsettitle(opt_title
? opt_title
: "st");
3929 drawregion(0, 0, term
.col
, term
.row
);
3930 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3932 XSetForeground(xw
.dpy
, dc
.gc
,
3933 dc
.col
[IS_SET(MODE_REVERSE
)?
3934 defaultfg
: defaultbg
].pixel
);
3938 drawregion(int x1
, int y1
, int x2
, int y2
)
3940 int i
, x
, y
, ox
, numspecs
;
3942 XftGlyphFontSpec
* specs
;
3943 int ena_sel
= sel
.ob
.x
!= -1 && sel
.alt
== IS_SET(MODE_ALTSCREEN
);
3945 if (!(xw
.state
& WIN_VISIBLE
))
3948 for (y
= y1
; y
< y2
; y
++) {
3952 xtermclear(0, y
, term
.col
, y
);
3955 specs
= term
.specbuf
;
3956 numspecs
= xmakeglyphfontspecs(specs
, &term
.line
[y
][x1
], x2
- x1
, x1
, y
);
3959 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
3960 new = term
.line
[y
][x
];
3961 if (new.mode
== ATTR_WDUMMY
)
3963 if (ena_sel
&& selected(x
, y
))
3964 new.mode
^= ATTR_REVERSE
;
3965 if (i
> 0 && ATTRCMP(base
, new)) {
3966 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
3978 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
3990 visibility(XEvent
*ev
)
3992 XVisibilityEvent
*e
= &ev
->xvisibility
;
3994 MODBIT(xw
.state
, e
->state
!= VisibilityFullyObscured
, WIN_VISIBLE
);
4000 xw
.state
&= ~WIN_VISIBLE
;
4004 xsetpointermotion(int set
)
4006 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
4007 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
4011 xseturgency(int add
)
4013 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
4015 MODBIT(h
->flags
, add
, XUrgencyHint
);
4016 XSetWMHints(xw
.dpy
, xw
.win
, h
);
4023 XFocusChangeEvent
*e
= &ev
->xfocus
;
4025 if (e
->mode
== NotifyGrab
)
4028 if (ev
->type
== FocusIn
) {
4029 XSetICFocus(xw
.xic
);
4030 xw
.state
|= WIN_FOCUSED
;
4032 if (IS_SET(MODE_FOCUS
))
4033 ttywrite("\033[I", 3);
4035 XUnsetICFocus(xw
.xic
);
4036 xw
.state
&= ~WIN_FOCUSED
;
4037 if (IS_SET(MODE_FOCUS
))
4038 ttywrite("\033[O", 3);
4043 match(uint mask
, uint state
)
4045 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
4049 numlock(const Arg
*dummy
)
4055 kmap(KeySym k
, uint state
)
4060 /* Check for mapped keys out of X11 function keys. */
4061 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
4062 if (mappedkeys
[i
] == k
)
4065 if (i
== LEN(mappedkeys
)) {
4066 if ((k
& 0xFFFF) < 0xFD00)
4070 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
4074 if (!match(kp
->mask
, state
))
4077 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
4079 if (term
.numlock
&& kp
->appkey
== 2)
4082 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
4085 if (IS_SET(MODE_CRLF
) ? kp
->crlf
< 0 : kp
->crlf
> 0)
4097 XKeyEvent
*e
= &ev
->xkey
;
4099 char buf
[32], *customkey
;
4105 if (IS_SET(MODE_KBDLOCK
))
4108 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
4110 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
4111 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
4112 bp
->func(&(bp
->arg
));
4117 /* 2. custom keys from config.h */
4118 if ((customkey
= kmap(ksym
, e
->state
))) {
4119 ttysend(customkey
, strlen(customkey
));
4123 /* 3. composed string from input method */
4126 if (len
== 1 && e
->state
& Mod1Mask
) {
4127 if (IS_SET(MODE_8BIT
)) {
4130 len
= utf8encode(c
, buf
);
4147 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
4149 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
4150 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
4151 xw
.state
|= WIN_FOCUSED
;
4153 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
4154 xw
.state
&= ~WIN_FOCUSED
;
4156 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
4157 /* Send SIGHUP to shell */
4164 cresize(int width
, int height
)
4173 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
4174 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
4184 if (e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
4187 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
4194 int w
= xw
.w
, h
= xw
.h
;
4196 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
4197 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
4200 /* Waiting for window mapping */
4202 XNextEvent(xw
.dpy
, &ev
);
4204 * This XFilterEvent call is required because of XOpenIM. It
4205 * does filter out the key event and some client message for
4206 * the input method too.
4208 if (XFilterEvent(&ev
, None
))
4210 if (ev
.type
== ConfigureNotify
) {
4211 w
= ev
.xconfigure
.width
;
4212 h
= ev
.xconfigure
.height
;
4214 } while (ev
.type
!= MapNotify
);
4219 clock_gettime(CLOCK_MONOTONIC
, &last
);
4222 for (xev
= actionfps
;;) {
4224 FD_SET(cmdfd
, &rfd
);
4227 if (pselect(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
4230 die("select failed: %s\n", strerror(errno
));
4232 if (FD_ISSET(cmdfd
, &rfd
)) {
4235 blinkset
= tattrset(ATTR_BLINK
);
4237 MODBIT(term
.mode
, 0, MODE_BLINK
);
4241 if (FD_ISSET(xfd
, &rfd
))
4244 clock_gettime(CLOCK_MONOTONIC
, &now
);
4245 drawtimeout
.tv_sec
= 0;
4246 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
4250 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
4251 tsetdirtattr(ATTR_BLINK
);
4252 term
.mode
^= MODE_BLINK
;
4256 deltatime
= TIMEDIFF(now
, last
);
4257 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
4263 while (XPending(xw
.dpy
)) {
4264 XNextEvent(xw
.dpy
, &ev
);
4265 if (XFilterEvent(&ev
, None
))
4267 if (handler
[ev
.type
])
4268 (handler
[ev
.type
])(&ev
);
4274 if (xev
&& !FD_ISSET(xfd
, &rfd
))
4276 if (!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
4278 if (TIMEDIFF(now
, lastblink
) \
4280 drawtimeout
.tv_nsec
= 1000;
4282 drawtimeout
.tv_nsec
= (1E6
* \
4287 drawtimeout
.tv_sec
= \
4288 drawtimeout
.tv_nsec
/ 1E9
;
4289 drawtimeout
.tv_nsec
%= (long)1E9
;
4301 die("%s " VERSION
" (c) 2010-2015 st engineers\n"
4302 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]\n"
4303 " [-i] [-t title] [-T title] [-w windowid] [-e command ...]"
4305 " st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]\n"
4306 " [-i] [-t title] [-T title] [-w windowid] -l line"
4307 " [stty_args ...]\n",
4312 main(int argc
, char *argv
[])
4314 uint cols
= 80, rows
= 24;
4325 opt_class
= EARGF(usage());
4332 opt_font
= EARGF(usage());
4335 xw
.gm
= XParseGeometry(EARGF(usage()),
4336 &xw
.l
, &xw
.t
, &cols
, &rows
);
4342 opt_io
= EARGF(usage());
4345 opt_line
= EARGF(usage());
4349 opt_title
= EARGF(usage());
4352 opt_embed
= EARGF(usage());
4361 /* eat all remaining arguments */
4363 if (!opt_title
&& !opt_line
)
4364 opt_title
= basename(xstrdup(argv
[0]));
4366 setlocale(LC_CTYPE
, "");
4367 XSetLocaleModifiers("");
4368 tnew(MAX(cols
, 1), MAX(rows
, 1));