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 NUMMAXLEN(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)
70 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
71 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
72 #define DIVCEIL(n, d) (((n) + ((d) - 1)) / (d))
73 #define ISCONTROLC0(c) (BETWEEN(c, 0, 0x1f) || (c) == '\177')
74 #define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f))
75 #define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c))
76 #define ISDELIM(u) (utf8strchr(worddelimiters, u) != NULL)
77 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
78 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || \
80 #define IS_SET(flag) ((term.mode & (flag)) != 0)
81 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + \
82 (t1.tv_nsec-t2.tv_nsec)/1E6)
83 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
85 #define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
86 #define IS_TRUECOL(x) (1 << 24 & (x))
87 #define TRUERED(x) (((x) & 0xff0000) >> 8)
88 #define TRUEGREEN(x) (((x) & 0xff00))
89 #define TRUEBLUE(x) (((x) & 0xff) << 8)
92 #define ISO14755CMD "dmenu -w %lu -p codepoint: </dev/null"
94 enum glyph_attribute
{
99 ATTR_UNDERLINE
= 1 << 3,
101 ATTR_REVERSE
= 1 << 5,
102 ATTR_INVISIBLE
= 1 << 6,
103 ATTR_STRUCK
= 1 << 7,
106 ATTR_WDUMMY
= 1 << 10,
107 ATTR_BOLD_FAINT
= ATTR_BOLD
| ATTR_FAINT
,
110 enum cursor_movement
{
123 MODE_INSERT
= 1 << 1,
124 MODE_APPKEYPAD
= 1 << 2,
125 MODE_ALTSCREEN
= 1 << 3,
127 MODE_MOUSEBTN
= 1 << 5,
128 MODE_MOUSEMOTION
= 1 << 6,
129 MODE_REVERSE
= 1 << 7,
130 MODE_KBDLOCK
= 1 << 8,
133 MODE_APPCURSOR
= 1 << 11,
134 MODE_MOUSESGR
= 1 << 12,
136 MODE_BLINK
= 1 << 14,
137 MODE_FBLINK
= 1 << 15,
138 MODE_FOCUS
= 1 << 16,
139 MODE_MOUSEX10
= 1 << 17,
140 MODE_MOUSEMANY
= 1 << 18,
141 MODE_BRCKTPASTE
= 1 << 19,
142 MODE_PRINT
= 1 << 20,
144 MODE_SIXEL
= 1 << 22,
145 MODE_MOUSE
= MODE_MOUSEBTN
|MODE_MOUSEMOTION
|MODE_MOUSEX10\
162 ESC_STR
= 4, /* OSC, PM, APC */
164 ESC_STR_END
= 16, /* a final string was encountered */
165 ESC_TEST
= 32, /* Enter in test mode */
175 enum selection_mode
{
181 enum selection_type
{
186 enum selection_snap
{
191 typedef unsigned char uchar
;
192 typedef unsigned int uint
;
193 typedef unsigned long ulong
;
194 typedef unsigned short ushort
;
196 typedef uint_least32_t Rune
;
198 typedef XftDraw
*Draw
;
199 typedef XftColor Color
;
202 Rune u
; /* character code */
203 ushort mode
; /* attribute flags */
204 uint32_t fg
; /* foreground */
205 uint32_t bg
; /* background */
211 Glyph attr
; /* current char attributes */
217 /* CSI Escape sequence structs */
218 /* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
220 char buf
[ESC_BUF_SIZ
]; /* raw string */
221 int len
; /* raw string length */
223 int arg
[ESC_ARG_SIZ
];
224 int narg
; /* nb of args */
228 /* STR Escape sequence structs */
229 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
231 char type
; /* ESC type ... */
232 char buf
[STR_BUF_SIZ
]; /* raw string */
233 int len
; /* raw string length */
234 char *args
[STR_ARG_SIZ
];
235 int narg
; /* nb of args */
238 /* Internal representation of the screen */
240 int row
; /* nb row */
241 int col
; /* nb col */
242 Line
*line
; /* screen */
243 Line
*alt
; /* alternate screen */
244 int *dirty
; /* dirtyness of lines */
245 XftGlyphFontSpec
*specbuf
; /* font spec buffer used for rendering */
246 TCursor c
; /* cursor */
247 int top
; /* top scroll limit */
248 int bot
; /* bottom scroll limit */
249 int mode
; /* terminal mode flags */
250 int esc
; /* escape state flags */
251 char trantbl
[4]; /* charset table translation */
252 int charset
; /* current charset */
253 int icharset
; /* selected charset for sequence */
254 int numlock
; /* lock numbers in keyboard */
258 /* Purely graphic info */
264 Atom xembed
, wmdeletewin
, netwmname
, netwmpid
;
269 XSetWindowAttributes attrs
;
271 int isfixed
; /* is fixed geometry? */
272 int l
, t
; /* left and top offset */
273 int gm
; /* geometry mask */
274 int tw
, th
; /* tty width and height */
275 int w
, h
; /* window width and height */
276 int ch
; /* char height */
277 int cw
; /* char width */
278 char state
; /* focus, redraw, visible */
279 int cursor
; /* cursor style */
292 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
293 signed char appkey
; /* application keypad */
294 signed char appcursor
; /* application cursor */
295 signed char crlf
; /* crlf mode */
303 * Selection variables:
304 * nb – normalized coordinates of the beginning of the selection
305 * ne – normalized coordinates of the end of the selection
306 * ob – original coordinates of the beginning of the selection
307 * oe – original coordinates of the end of the selection
313 char *primary
, *clipboard
;
316 struct timespec tclick1
;
317 struct timespec tclick2
;
330 void (*func
)(const Arg
*);
334 /* function definitions used in config.h */
335 static void clipcopy(const Arg
*);
336 static void clippaste(const Arg
*);
337 static void numlock(const Arg
*);
338 static void selpaste(const Arg
*);
339 static void xzoom(const Arg
*);
340 static void xzoomabs(const Arg
*);
341 static void xzoomreset(const Arg
*);
342 static void printsel(const Arg
*);
343 static void printscreen(const Arg
*) ;
344 static void iso14755(const Arg
*);
345 static void toggleprinter(const Arg
*);
346 static void sendbreak(const Arg
*);
348 /* Config.h for applying patches and the configuration. */
364 /* Drawing Context */
366 Color col
[MAX(LEN(colorname
), 256)];
367 Font font
, bfont
, ifont
, ibfont
;
371 static void die(const char *, ...);
372 static void draw(void);
373 static void redraw(void);
374 static void drawregion(int, int, int, int);
375 static void execsh(void);
376 static void stty(void);
377 static void sigchld(int);
378 static void run(void);
380 static void csidump(void);
381 static void csihandle(void);
382 static void csiparse(void);
383 static void csireset(void);
384 static int eschandle(uchar
);
385 static void strdump(void);
386 static void strhandle(void);
387 static void strparse(void);
388 static void strreset(void);
390 static int tattrset(int);
391 static void tprinter(char *, size_t);
392 static void tdumpsel(void);
393 static void tdumpline(int);
394 static void tdump(void);
395 static void tclearregion(int, int, int, int);
396 static void tcursor(int);
397 static void tdeletechar(int);
398 static void tdeleteline(int);
399 static void tinsertblank(int);
400 static void tinsertblankline(int);
401 static int tlinelen(int);
402 static void tmoveto(int, int);
403 static void tmoveato(int, int);
404 static void tnew(int, int);
405 static void tnewline(int);
406 static void tputtab(int);
407 static void tputc(Rune
);
408 static void treset(void);
409 static void tresize(int, int);
410 static void tscrollup(int, int);
411 static void tscrolldown(int, int);
412 static void tsetattr(int *, int);
413 static void tsetchar(Rune
, Glyph
*, int, int);
414 static void tsetscroll(int, int);
415 static void tswapscreen(void);
416 static void tsetdirt(int, int);
417 static void tsetdirtattr(int);
418 static void tsetmode(int, int, int *, int);
419 static void tfulldirt(void);
420 static void techo(Rune
);
421 static void tcontrolcode(uchar
);
422 static void tdectest(char );
423 static void tdefutf8(char);
424 static int32_t tdefcolor(int *, int *, int);
425 static void tdeftran(char);
426 static inline int match(uint
, uint
);
427 static void ttynew(void);
428 static size_t ttyread(void);
429 static void ttyresize(void);
430 static void ttysend(char *, size_t);
431 static void ttywrite(const char *, size_t);
432 static void tstrsequence(uchar
);
434 static inline ushort
sixd_to_16bit(int);
435 static int xmakeglyphfontspecs(XftGlyphFontSpec
*, const Glyph
*, int, int, int);
436 static void xdrawglyphfontspecs(const XftGlyphFontSpec
*, Glyph
, int, int, int);
437 static void xdrawglyph(Glyph
, int, int);
438 static void xhints(void);
439 static void xclear(int, int, int, int);
440 static void xdrawcursor(void);
441 static void xinit(void);
442 static void xloadcols(void);
443 static int xsetcolorname(int, const char *);
444 static int xgeommasktogravity(int);
445 static int xloadfont(Font
*, FcPattern
*);
446 static void xloadfonts(char *, double);
447 static void xsettitle(char *);
448 static void xresettitle(void);
449 static void xsetpointermotion(int);
450 static void xseturgency(int);
451 static void xsetsel(char *, Time
);
452 static void xunloadfont(Font
*);
453 static void xunloadfonts(void);
454 static void xresize(int, int);
456 static void expose(XEvent
*);
457 static void visibility(XEvent
*);
458 static void unmap(XEvent
*);
459 static char *kmap(KeySym
, uint
);
460 static void kpress(XEvent
*);
461 static void cmessage(XEvent
*);
462 static void cresize(int, int);
463 static void resize(XEvent
*);
464 static void focus(XEvent
*);
465 static void brelease(XEvent
*);
466 static void bpress(XEvent
*);
467 static void bmotion(XEvent
*);
468 static void propnotify(XEvent
*);
469 static void selnotify(XEvent
*);
470 static void selclear(XEvent
*);
471 static void selrequest(XEvent
*);
473 static void selinit(void);
474 static void selnormalize(void);
475 static inline int selected(int, int);
476 static char *getsel(void);
477 static void selcopy(Time
);
478 static void selscroll(int, int);
479 static void selsnap(int *, int *, int);
480 static int x2col(int);
481 static int y2row(int);
482 static void getbuttoninfo(XEvent
*);
483 static void mousereport(XEvent
*);
485 static size_t utf8decode(char *, Rune
*, size_t);
486 static Rune
utf8decodebyte(char, size_t *);
487 static size_t utf8encode(Rune
, char *);
488 static char utf8encodebyte(Rune
, size_t);
489 static char *utf8strchr(char *s
, Rune u
);
490 static size_t utf8validate(Rune
*, size_t);
492 static ssize_t
xwrite(int, const char *, size_t);
493 static void *xmalloc(size_t);
494 static void *xrealloc(void *, size_t);
495 static char *xstrdup(char *);
497 static void usage(void);
499 static void (*handler
[LASTEvent
])(XEvent
*) = {
501 [ClientMessage
] = cmessage
,
502 [ConfigureNotify
] = resize
,
503 [VisibilityNotify
] = visibility
,
504 [UnmapNotify
] = unmap
,
508 [MotionNotify
] = bmotion
,
509 [ButtonPress
] = bpress
,
510 [ButtonRelease
] = brelease
,
512 * Uncomment if you want the selection to disappear when you select something
513 * different in another window.
515 /* [SelectionClear] = selclear, */
516 [SelectionNotify
] = selnotify
,
518 * PropertyNotify is only turned on when there is some INCR transfer happening
519 * for the selection retrieval.
521 [PropertyNotify
] = propnotify
,
522 [SelectionRequest
] = selrequest
,
529 static CSIEscape csiescseq
;
530 static STREscape strescseq
;
533 static Selection sel
;
535 static char **opt_cmd
= NULL
;
536 static char *opt_class
= NULL
;
537 static char *opt_embed
= NULL
;
538 static char *opt_font
= NULL
;
539 static char *opt_io
= NULL
;
540 static char *opt_line
= NULL
;
541 static char *opt_name
= NULL
;
542 static char *opt_title
= NULL
;
543 static int oldbutton
= 3; /* button event on startup: 3 = release */
545 static char *usedfont
= NULL
;
546 static double usedfontsize
= 0;
547 static double defaultfontsize
= 0;
549 static uchar utfbyte
[UTF_SIZ
+ 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
550 static uchar utfmask
[UTF_SIZ
+ 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
551 static Rune utfmin
[UTF_SIZ
+ 1] = { 0, 0, 0x80, 0x800, 0x10000};
552 static Rune utfmax
[UTF_SIZ
+ 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
554 /* Font Ring Cache */
568 /* Fontcache is an array now. A new font will be appended to the array. */
569 static Fontcache frc
[16];
570 static int frclen
= 0;
573 xwrite(int fd
, const char *s
, size_t len
)
579 r
= write(fd
, s
, len
);
592 void *p
= malloc(len
);
595 die("Out of memory\n");
601 xrealloc(void *p
, size_t len
)
603 if ((p
= realloc(p
, len
)) == NULL
)
604 die("Out of memory\n");
612 if ((s
= strdup(s
)) == NULL
)
613 die("Out of memory\n");
619 utf8decode(char *c
, Rune
*u
, size_t clen
)
621 size_t i
, j
, len
, type
;
627 udecoded
= utf8decodebyte(c
[0], &len
);
628 if (!BETWEEN(len
, 1, UTF_SIZ
))
630 for (i
= 1, j
= 1; i
< clen
&& j
< len
; ++i
, ++j
) {
631 udecoded
= (udecoded
<< 6) | utf8decodebyte(c
[i
], &type
);
638 utf8validate(u
, len
);
644 utf8decodebyte(char c
, size_t *i
)
646 for (*i
= 0; *i
< LEN(utfmask
); ++(*i
))
647 if (((uchar
)c
& utfmask
[*i
]) == utfbyte
[*i
])
648 return (uchar
)c
& ~utfmask
[*i
];
654 utf8encode(Rune u
, char *c
)
658 len
= utf8validate(&u
, 0);
662 for (i
= len
- 1; i
!= 0; --i
) {
663 c
[i
] = utf8encodebyte(u
, 0);
666 c
[0] = utf8encodebyte(u
, len
);
672 utf8encodebyte(Rune u
, size_t i
)
674 return utfbyte
[i
] | (u
& ~utfmask
[i
]);
678 utf8strchr(char *s
, Rune u
)
684 for (i
= 0, j
= 0; i
< len
; i
+= j
) {
685 if (!(j
= utf8decode(&s
[i
], &r
, len
- i
)))
695 utf8validate(Rune
*u
, size_t i
)
697 if (!BETWEEN(*u
, utfmin
[i
], utfmax
[i
]) || BETWEEN(*u
, 0xD800, 0xDFFF))
699 for (i
= 1; *u
> utfmax
[i
]; ++i
)
708 clock_gettime(CLOCK_MONOTONIC
, &sel
.tclick1
);
709 clock_gettime(CLOCK_MONOTONIC
, &sel
.tclick2
);
714 sel
.clipboard
= NULL
;
715 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
716 if (sel
.xtarget
== None
)
717 sel
.xtarget
= XA_STRING
;
726 return LIMIT(x
, 0, term
.col
-1);
735 return LIMIT(y
, 0, term
.row
-1);
743 if (term
.line
[y
][i
- 1].mode
& ATTR_WRAP
)
746 while (i
> 0 && term
.line
[y
][i
- 1].u
== ' ')
757 if (sel
.type
== SEL_REGULAR
&& sel
.ob
.y
!= sel
.oe
.y
) {
758 sel
.nb
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.ob
.x
: sel
.oe
.x
;
759 sel
.ne
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.oe
.x
: sel
.ob
.x
;
761 sel
.nb
.x
= MIN(sel
.ob
.x
, sel
.oe
.x
);
762 sel
.ne
.x
= MAX(sel
.ob
.x
, sel
.oe
.x
);
764 sel
.nb
.y
= MIN(sel
.ob
.y
, sel
.oe
.y
);
765 sel
.ne
.y
= MAX(sel
.ob
.y
, sel
.oe
.y
);
767 selsnap(&sel
.nb
.x
, &sel
.nb
.y
, -1);
768 selsnap(&sel
.ne
.x
, &sel
.ne
.y
, +1);
770 /* expand selection over line breaks */
771 if (sel
.type
== SEL_RECTANGULAR
)
773 i
= tlinelen(sel
.nb
.y
);
776 if (tlinelen(sel
.ne
.y
) <= sel
.ne
.x
)
777 sel
.ne
.x
= term
.col
- 1;
781 selected(int x
, int y
)
783 if (sel
.mode
== SEL_EMPTY
)
786 if (sel
.type
== SEL_RECTANGULAR
)
787 return BETWEEN(y
, sel
.nb
.y
, sel
.ne
.y
)
788 && BETWEEN(x
, sel
.nb
.x
, sel
.ne
.x
);
790 return BETWEEN(y
, sel
.nb
.y
, sel
.ne
.y
)
791 && (y
!= sel
.nb
.y
|| x
>= sel
.nb
.x
)
792 && (y
!= sel
.ne
.y
|| x
<= sel
.ne
.x
);
796 selsnap(int *x
, int *y
, int direction
)
798 int newx
, newy
, xt
, yt
;
799 int delim
, prevdelim
;
805 * Snap around if the word wraps around at the end or
806 * beginning of a line.
808 prevgp
= &term
.line
[*y
][*x
];
809 prevdelim
= ISDELIM(prevgp
->u
);
811 newx
= *x
+ direction
;
813 if (!BETWEEN(newx
, 0, term
.col
- 1)) {
815 newx
= (newx
+ term
.col
) % term
.col
;
816 if (!BETWEEN(newy
, 0, term
.row
- 1))
822 yt
= newy
, xt
= newx
;
823 if (!(term
.line
[yt
][xt
].mode
& ATTR_WRAP
))
827 if (newx
>= tlinelen(newy
))
830 gp
= &term
.line
[newy
][newx
];
831 delim
= ISDELIM(gp
->u
);
832 if (!(gp
->mode
& ATTR_WDUMMY
) && (delim
!= prevdelim
833 || (delim
&& gp
->u
!= prevgp
->u
)))
844 * Snap around if the the previous line or the current one
845 * has set ATTR_WRAP at its end. Then the whole next or
846 * previous line will be selected.
848 *x
= (direction
< 0) ? 0 : term
.col
- 1;
850 for (; *y
> 0; *y
+= direction
) {
851 if (!(term
.line
[*y
-1][term
.col
-1].mode
856 } else if (direction
> 0) {
857 for (; *y
< term
.row
-1; *y
+= direction
) {
858 if (!(term
.line
[*y
][term
.col
-1].mode
869 getbuttoninfo(XEvent
*e
)
872 uint state
= e
->xbutton
.state
& ~(Button1Mask
| forceselmod
);
874 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
876 sel
.oe
.x
= x2col(e
->xbutton
.x
);
877 sel
.oe
.y
= y2row(e
->xbutton
.y
);
880 sel
.type
= SEL_REGULAR
;
881 for (type
= 1; type
< LEN(selmasks
); ++type
) {
882 if (match(selmasks
[type
], state
)) {
890 mousereport(XEvent
*e
)
892 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
893 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
899 if (e
->xbutton
.type
== MotionNotify
) {
900 if (x
== ox
&& y
== oy
)
902 if (!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
904 /* MOUSE_MOTION: no reporting if no button is pressed */
905 if (IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
908 button
= oldbutton
+ 32;
912 if (!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
919 if (e
->xbutton
.type
== ButtonPress
) {
923 } else if (e
->xbutton
.type
== ButtonRelease
) {
925 /* MODE_MOUSEX10: no button release reporting */
926 if (IS_SET(MODE_MOUSEX10
))
928 if (button
== 64 || button
== 65)
933 if (!IS_SET(MODE_MOUSEX10
)) {
934 button
+= ((state
& ShiftMask
) ? 4 : 0)
935 + ((state
& Mod4Mask
) ? 8 : 0)
936 + ((state
& ControlMask
) ? 16 : 0);
939 if (IS_SET(MODE_MOUSESGR
)) {
940 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
942 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
943 } else if (x
< 223 && y
< 223) {
944 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
945 32+button
, 32+x
+1, 32+y
+1);
959 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
964 for (ms
= mshortcuts
; ms
< mshortcuts
+ LEN(mshortcuts
); ms
++) {
965 if (e
->xbutton
.button
== ms
->b
966 && match(ms
->mask
, e
->xbutton
.state
)) {
967 ttysend(ms
->s
, strlen(ms
->s
));
972 if (e
->xbutton
.button
== Button1
) {
973 clock_gettime(CLOCK_MONOTONIC
, &now
);
975 /* Clear previous selection, logically and visually. */
977 sel
.mode
= SEL_EMPTY
;
978 sel
.type
= SEL_REGULAR
;
979 sel
.oe
.x
= sel
.ob
.x
= x2col(e
->xbutton
.x
);
980 sel
.oe
.y
= sel
.ob
.y
= y2row(e
->xbutton
.y
);
983 * If the user clicks below predefined timeouts specific
984 * snapping behaviour is exposed.
986 if (TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
987 sel
.snap
= SNAP_LINE
;
988 } else if (TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
989 sel
.snap
= SNAP_WORD
;
996 sel
.mode
= SEL_READY
;
997 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
998 sel
.tclick2
= sel
.tclick1
;
1007 int y
, bufsize
, lastx
, linelen
;
1013 bufsize
= (term
.col
+1) * (sel
.ne
.y
-sel
.nb
.y
+1) * UTF_SIZ
;
1014 ptr
= str
= xmalloc(bufsize
);
1016 /* append every set & selected glyph to the selection */
1017 for (y
= sel
.nb
.y
; y
<= sel
.ne
.y
; y
++) {
1018 if ((linelen
= tlinelen(y
)) == 0) {
1023 if (sel
.type
== SEL_RECTANGULAR
) {
1024 gp
= &term
.line
[y
][sel
.nb
.x
];
1027 gp
= &term
.line
[y
][sel
.nb
.y
== y
? sel
.nb
.x
: 0];
1028 lastx
= (sel
.ne
.y
== y
) ? sel
.ne
.x
: term
.col
-1;
1030 last
= &term
.line
[y
][MIN(lastx
, linelen
-1)];
1031 while (last
>= gp
&& last
->u
== ' ')
1034 for ( ; gp
<= last
; ++gp
) {
1035 if (gp
->mode
& ATTR_WDUMMY
)
1038 ptr
+= utf8encode(gp
->u
, ptr
);
1042 * Copy and pasting of line endings is inconsistent
1043 * in the inconsistent terminal and GUI world.
1044 * The best solution seems like to produce '\n' when
1045 * something is copied from st and convert '\n' to
1046 * '\r', when something to be pasted is received by
1048 * FIXME: Fix the computer world.
1050 if ((y
< sel
.ne
.y
|| lastx
>= linelen
) && !(last
->mode
& ATTR_WRAP
))
1060 xsetsel(getsel(), t
);
1064 propnotify(XEvent
*e
)
1066 XPropertyEvent
*xpev
;
1067 Atom clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1069 xpev
= &e
->xproperty
;
1070 if (xpev
->state
== PropertyNewValue
&&
1071 (xpev
->atom
== XA_PRIMARY
||
1072 xpev
->atom
== clipboard
)) {
1078 selnotify(XEvent
*e
)
1080 ulong nitems
, ofs
, rem
;
1082 uchar
*data
, *last
, *repl
;
1083 Atom type
, incratom
, property
;
1085 incratom
= XInternAtom(xw
.dpy
, "INCR", 0);
1088 if (e
->type
== SelectionNotify
) {
1089 property
= e
->xselection
.property
;
1090 } else if(e
->type
== PropertyNotify
) {
1091 property
= e
->xproperty
.atom
;
1095 if (property
== None
)
1099 if (XGetWindowProperty(xw
.dpy
, xw
.win
, property
, ofs
,
1100 BUFSIZ
/4, False
, AnyPropertyType
,
1101 &type
, &format
, &nitems
, &rem
,
1103 fprintf(stderr
, "Clipboard allocation failed\n");
1107 if (e
->type
== PropertyNotify
&& nitems
== 0 && rem
== 0) {
1109 * If there is some PropertyNotify with no data, then
1110 * this is the signal of the selection owner that all
1111 * data has been transferred. We won't need to receive
1112 * PropertyNotify events anymore.
1114 MODBIT(xw
.attrs
.event_mask
, 0, PropertyChangeMask
);
1115 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
1119 if (type
== incratom
) {
1121 * Activate the PropertyNotify events so we receive
1122 * when the selection owner does send us the next
1125 MODBIT(xw
.attrs
.event_mask
, 1, PropertyChangeMask
);
1126 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
,
1130 * Deleting the property is the transfer start signal.
1132 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
1137 * As seen in getsel:
1138 * Line endings are inconsistent in the terminal and GUI world
1139 * copy and pasting. When receiving some selection data,
1140 * replace all '\n' with '\r'.
1141 * FIXME: Fix the computer world.
1144 last
= data
+ nitems
* format
/ 8;
1145 while ((repl
= memchr(repl
, '\n', last
- repl
))) {
1149 if (IS_SET(MODE_BRCKTPASTE
) && ofs
== 0)
1150 ttywrite("\033[200~", 6);
1151 ttysend((char *)data
, nitems
* format
/ 8);
1152 if (IS_SET(MODE_BRCKTPASTE
) && rem
== 0)
1153 ttywrite("\033[201~", 6);
1155 /* number of 32-bit chunks returned */
1156 ofs
+= nitems
* format
/ 32;
1160 * Deleting the property again tells the selection owner to send the
1161 * next data chunk in the property.
1163 XDeleteProperty(xw
.dpy
, xw
.win
, (int)property
);
1167 selpaste(const Arg
*dummy
)
1169 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1170 xw
.win
, CurrentTime
);
1174 clipcopy(const Arg
*dummy
)
1178 if (sel
.clipboard
!= NULL
)
1179 free(sel
.clipboard
);
1181 if (sel
.primary
!= NULL
) {
1182 sel
.clipboard
= xstrdup(sel
.primary
);
1183 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1184 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1189 clippaste(const Arg
*dummy
)
1193 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1194 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, clipboard
,
1195 xw
.win
, CurrentTime
);
1203 sel
.mode
= SEL_IDLE
;
1205 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1209 selrequest(XEvent
*e
)
1211 XSelectionRequestEvent
*xsre
;
1212 XSelectionEvent xev
;
1213 Atom xa_targets
, string
, clipboard
;
1216 xsre
= (XSelectionRequestEvent
*) e
;
1217 xev
.type
= SelectionNotify
;
1218 xev
.requestor
= xsre
->requestor
;
1219 xev
.selection
= xsre
->selection
;
1220 xev
.target
= xsre
->target
;
1221 xev
.time
= xsre
->time
;
1222 if (xsre
->property
== None
)
1223 xsre
->property
= xsre
->target
;
1226 xev
.property
= None
;
1228 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1229 if (xsre
->target
== xa_targets
) {
1230 /* respond with the supported type */
1231 string
= sel
.xtarget
;
1232 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1233 XA_ATOM
, 32, PropModeReplace
,
1234 (uchar
*) &string
, 1);
1235 xev
.property
= xsre
->property
;
1236 } else if (xsre
->target
== sel
.xtarget
|| xsre
->target
== XA_STRING
) {
1238 * xith XA_STRING non ascii characters may be incorrect in the
1239 * requestor. It is not our problem, use utf8.
1241 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1242 if (xsre
->selection
== XA_PRIMARY
) {
1243 seltext
= sel
.primary
;
1244 } else if (xsre
->selection
== clipboard
) {
1245 seltext
= sel
.clipboard
;
1248 "Unhandled clipboard selection 0x%lx\n",
1252 if (seltext
!= NULL
) {
1253 XChangeProperty(xsre
->display
, xsre
->requestor
,
1254 xsre
->property
, xsre
->target
,
1256 (uchar
*)seltext
, strlen(seltext
));
1257 xev
.property
= xsre
->property
;
1261 /* all done, send a notification to the listener */
1262 if (!XSendEvent(xsre
->display
, xsre
->requestor
, 1, 0, (XEvent
*) &xev
))
1263 fprintf(stderr
, "Error sending SelectionNotify event\n");
1267 xsetsel(char *str
, Time t
)
1272 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, t
);
1273 if (XGetSelectionOwner(xw
.dpy
, XA_PRIMARY
) != xw
.win
)
1280 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
1285 if (e
->xbutton
.button
== Button2
) {
1287 } else if (e
->xbutton
.button
== Button1
) {
1288 if (sel
.mode
== SEL_READY
) {
1290 selcopy(e
->xbutton
.time
);
1293 sel
.mode
= SEL_IDLE
;
1294 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1301 int oldey
, oldex
, oldsby
, oldsey
;
1303 if (IS_SET(MODE_MOUSE
) && !(e
->xbutton
.state
& forceselmod
)) {
1311 sel
.mode
= SEL_READY
;
1318 if (oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
1319 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
1323 die(const char *errstr
, ...)
1327 va_start(ap
, errstr
);
1328 vfprintf(stderr
, errstr
, ap
);
1336 char **args
, *sh
, *prog
;
1337 const struct passwd
*pw
;
1338 char buf
[sizeof(long) * 8 + 1];
1341 if ((pw
= getpwuid(getuid())) == NULL
) {
1343 die("getpwuid:%s\n", strerror(errno
));
1345 die("who are you?\n");
1348 if ((sh
= getenv("SHELL")) == NULL
)
1349 sh
= (pw
->pw_shell
[0]) ? pw
->pw_shell
: shell
;
1357 args
= (opt_cmd
) ? opt_cmd
: (char *[]) {prog
, NULL
};
1359 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1361 unsetenv("COLUMNS");
1363 unsetenv("TERMCAP");
1364 setenv("LOGNAME", pw
->pw_name
, 1);
1365 setenv("USER", pw
->pw_name
, 1);
1366 setenv("SHELL", sh
, 1);
1367 setenv("HOME", pw
->pw_dir
, 1);
1368 setenv("TERM", termname
, 1);
1369 setenv("WINDOWID", buf
, 1);
1371 signal(SIGCHLD
, SIG_DFL
);
1372 signal(SIGHUP
, SIG_DFL
);
1373 signal(SIGINT
, SIG_DFL
);
1374 signal(SIGQUIT
, SIG_DFL
);
1375 signal(SIGTERM
, SIG_DFL
);
1376 signal(SIGALRM
, SIG_DFL
);
1388 if ((p
= waitpid(pid
, &stat
, WNOHANG
)) < 0)
1389 die("Waiting for pid %hd failed: %s\n", pid
, strerror(errno
));
1394 if (!WIFEXITED(stat
) || WEXITSTATUS(stat
))
1395 die("child finished with error '%d'\n", stat
);
1403 char cmd
[_POSIX_ARG_MAX
], **p
, *q
, *s
;
1406 if ((n
= strlen(stty_args
)) > sizeof(cmd
)-1)
1407 die("incorrect stty parameters\n");
1408 memcpy(cmd
, stty_args
, n
);
1410 siz
= sizeof(cmd
) - n
;
1411 for (p
= opt_cmd
; p
&& (s
= *p
); ++p
) {
1412 if ((n
= strlen(s
)) > siz
-1)
1413 die("stty parameter length too long\n");
1420 if (system(cmd
) != 0)
1421 perror("Couldn't call stty");
1428 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1431 term
.mode
|= MODE_PRINT
;
1432 iofd
= (!strcmp(opt_io
, "-")) ?
1433 1 : open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1435 fprintf(stderr
, "Error opening %s:%s\n",
1436 opt_io
, strerror(errno
));
1441 if ((cmdfd
= open(opt_line
, O_RDWR
)) < 0)
1442 die("open line failed: %s\n", strerror(errno
));
1448 /* seems to work fine on linux, openbsd and freebsd */
1449 if (openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1450 die("openpty failed: %s\n", strerror(errno
));
1452 switch (pid
= fork()) {
1454 die("fork failed\n");
1458 setsid(); /* create a new process group */
1462 if (ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1463 die("ioctl TIOCSCTTY failed: %s\n", strerror(errno
));
1471 signal(SIGCHLD
, sigchld
);
1479 static char buf
[BUFSIZ
];
1480 static int buflen
= 0;
1482 int charsize
; /* size of utf8 char in bytes */
1486 /* append read bytes to unprocessed bytes */
1487 if ((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1488 die("Couldn't read from shell: %s\n", strerror(errno
));
1494 if (IS_SET(MODE_UTF8
) && !IS_SET(MODE_SIXEL
)) {
1495 /* process a complete utf8 char */
1496 charsize
= utf8decode(ptr
, &unicodep
, buflen
);
1506 tputc(*ptr
++ & 0xFF);
1510 /* keep any uncomplete utf8 char for the next call */
1512 memmove(buf
, ptr
, buflen
);
1518 ttywrite(const char *s
, size_t n
)
1525 * Remember that we are using a pty, which might be a modem line.
1526 * Writing too much will clog the line. That's why we are doing this
1528 * FIXME: Migrate the world to Plan 9.
1533 FD_SET(cmdfd
, &wfd
);
1534 FD_SET(cmdfd
, &rfd
);
1536 /* Check if we can write. */
1537 if (pselect(cmdfd
+1, &rfd
, &wfd
, NULL
, NULL
, NULL
) < 0) {
1540 die("select failed: %s\n", strerror(errno
));
1542 if (FD_ISSET(cmdfd
, &wfd
)) {
1544 * Only write the bytes written by ttywrite() or the
1545 * default of 256. This seems to be a reasonable value
1546 * for a serial line. Bigger values might clog the I/O.
1548 if ((r
= write(cmdfd
, s
, (n
< lim
)? n
: lim
)) < 0)
1552 * We weren't able to write out everything.
1553 * This means the buffer is getting full
1561 /* All bytes have been written. */
1565 if (FD_ISSET(cmdfd
, &rfd
))
1571 die("write error on tty: %s\n", strerror(errno
));
1575 ttysend(char *s
, size_t n
)
1582 if (!IS_SET(MODE_ECHO
))
1586 for (t
= s
; t
< lim
; t
+= len
) {
1587 if (IS_SET(MODE_UTF8
) && !IS_SET(MODE_SIXEL
)) {
1588 len
= utf8decode(t
, &u
, n
);
1605 w
.ws_row
= term
.row
;
1606 w
.ws_col
= term
.col
;
1607 w
.ws_xpixel
= xw
.tw
;
1608 w
.ws_ypixel
= xw
.th
;
1609 if (ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1610 fprintf(stderr
, "Couldn't set window size: %s\n", strerror(errno
));
1618 for (i
= 0; i
< term
.row
-1; i
++) {
1619 for (j
= 0; j
< term
.col
-1; j
++) {
1620 if (term
.line
[i
][j
].mode
& attr
)
1629 tsetdirt(int top
, int bot
)
1633 LIMIT(top
, 0, term
.row
-1);
1634 LIMIT(bot
, 0, term
.row
-1);
1636 for (i
= top
; i
<= bot
; i
++)
1641 tsetdirtattr(int attr
)
1645 for (i
= 0; i
< term
.row
-1; i
++) {
1646 for (j
= 0; j
< term
.col
-1; j
++) {
1647 if (term
.line
[i
][j
].mode
& attr
) {
1658 tsetdirt(0, term
.row
-1);
1664 static TCursor c
[2];
1665 int alt
= IS_SET(MODE_ALTSCREEN
);
1667 if (mode
== CURSOR_SAVE
) {
1669 } else if (mode
== CURSOR_LOAD
) {
1671 tmoveto(c
[alt
].x
, c
[alt
].y
);
1680 term
.c
= (TCursor
){{
1684 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1686 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1687 for (i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1690 term
.bot
= term
.row
- 1;
1691 term
.mode
= MODE_WRAP
|MODE_UTF8
;
1692 memset(term
.trantbl
, CS_USA
, sizeof(term
.trantbl
));
1695 for (i
= 0; i
< 2; i
++) {
1697 tcursor(CURSOR_SAVE
);
1698 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1704 tnew(int col
, int row
)
1706 term
= (Term
){ .c
= { .attr
= { .fg
= defaultfg
, .bg
= defaultbg
} } };
1716 Line
*tmp
= term
.line
;
1718 term
.line
= term
.alt
;
1720 term
.mode
^= MODE_ALTSCREEN
;
1725 tscrolldown(int orig
, int n
)
1730 LIMIT(n
, 0, term
.bot
-orig
+1);
1732 tsetdirt(orig
, term
.bot
-n
);
1733 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1735 for (i
= term
.bot
; i
>= orig
+n
; i
--) {
1736 temp
= term
.line
[i
];
1737 term
.line
[i
] = term
.line
[i
-n
];
1738 term
.line
[i
-n
] = temp
;
1745 tscrollup(int orig
, int n
)
1750 LIMIT(n
, 0, term
.bot
-orig
+1);
1752 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1753 tsetdirt(orig
+n
, term
.bot
);
1755 for (i
= orig
; i
<= term
.bot
-n
; i
++) {
1756 temp
= term
.line
[i
];
1757 term
.line
[i
] = term
.line
[i
+n
];
1758 term
.line
[i
+n
] = temp
;
1761 selscroll(orig
, -n
);
1765 selscroll(int orig
, int n
)
1770 if (BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1771 if ((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1775 if (sel
.type
== SEL_RECTANGULAR
) {
1776 if (sel
.ob
.y
< term
.top
)
1777 sel
.ob
.y
= term
.top
;
1778 if (sel
.oe
.y
> term
.bot
)
1779 sel
.oe
.y
= term
.bot
;
1781 if (sel
.ob
.y
< term
.top
) {
1782 sel
.ob
.y
= term
.top
;
1785 if (sel
.oe
.y
> term
.bot
) {
1786 sel
.oe
.y
= term
.bot
;
1787 sel
.oe
.x
= term
.col
;
1795 tnewline(int first_col
)
1799 if (y
== term
.bot
) {
1800 tscrollup(term
.top
, 1);
1804 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1810 char *p
= csiescseq
.buf
, *np
;
1819 csiescseq
.buf
[csiescseq
.len
] = '\0';
1820 while (p
< csiescseq
.buf
+csiescseq
.len
) {
1822 v
= strtol(p
, &np
, 10);
1825 if (v
== LONG_MAX
|| v
== LONG_MIN
)
1827 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1829 if (*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1833 csiescseq
.mode
[0] = *p
++;
1834 csiescseq
.mode
[1] = (p
< csiescseq
.buf
+csiescseq
.len
) ? *p
: '\0';
1837 /* for absolute user moves, when decom is set */
1839 tmoveato(int x
, int y
)
1841 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1845 tmoveto(int x
, int y
)
1849 if (term
.c
.state
& CURSOR_ORIGIN
) {
1854 maxy
= term
.row
- 1;
1856 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1857 term
.c
.x
= LIMIT(x
, 0, term
.col
-1);
1858 term
.c
.y
= LIMIT(y
, miny
, maxy
);
1862 tsetchar(Rune u
, Glyph
*attr
, int x
, int y
)
1864 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1865 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1866 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1867 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1868 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1869 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1870 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1871 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1872 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1876 * The table is proudly stolen from rxvt.
1878 if (term
.trantbl
[term
.charset
] == CS_GRAPHIC0
&&
1879 BETWEEN(u
, 0x41, 0x7e) && vt100_0
[u
- 0x41])
1880 utf8decode(vt100_0
[u
- 0x41], &u
, UTF_SIZ
);
1882 if (term
.line
[y
][x
].mode
& ATTR_WIDE
) {
1883 if (x
+1 < term
.col
) {
1884 term
.line
[y
][x
+1].u
= ' ';
1885 term
.line
[y
][x
+1].mode
&= ~ATTR_WDUMMY
;
1887 } else if (term
.line
[y
][x
].mode
& ATTR_WDUMMY
) {
1888 term
.line
[y
][x
-1].u
= ' ';
1889 term
.line
[y
][x
-1].mode
&= ~ATTR_WIDE
;
1893 term
.line
[y
][x
] = *attr
;
1894 term
.line
[y
][x
].u
= u
;
1898 tclearregion(int x1
, int y1
, int x2
, int y2
)
1904 temp
= x1
, x1
= x2
, x2
= temp
;
1906 temp
= y1
, y1
= y2
, y2
= temp
;
1908 LIMIT(x1
, 0, term
.col
-1);
1909 LIMIT(x2
, 0, term
.col
-1);
1910 LIMIT(y1
, 0, term
.row
-1);
1911 LIMIT(y2
, 0, term
.row
-1);
1913 for (y
= y1
; y
<= y2
; y
++) {
1915 for (x
= x1
; x
<= x2
; x
++) {
1916 gp
= &term
.line
[y
][x
];
1919 gp
->fg
= term
.c
.attr
.fg
;
1920 gp
->bg
= term
.c
.attr
.bg
;
1933 LIMIT(n
, 0, term
.col
- term
.c
.x
);
1937 size
= term
.col
- src
;
1938 line
= term
.line
[term
.c
.y
];
1940 memmove(&line
[dst
], &line
[src
], size
* sizeof(Glyph
));
1941 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1950 LIMIT(n
, 0, term
.col
- term
.c
.x
);
1954 size
= term
.col
- dst
;
1955 line
= term
.line
[term
.c
.y
];
1957 memmove(&line
[dst
], &line
[src
], size
* sizeof(Glyph
));
1958 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1962 tinsertblankline(int n
)
1964 if (BETWEEN(term
.c
.y
, term
.top
, term
.bot
))
1965 tscrolldown(term
.c
.y
, n
);
1971 if (BETWEEN(term
.c
.y
, term
.top
, term
.bot
))
1972 tscrollup(term
.c
.y
, n
);
1976 tdefcolor(int *attr
, int *npar
, int l
)
1981 switch (attr
[*npar
+ 1]) {
1982 case 2: /* direct color in RGB space */
1983 if (*npar
+ 4 >= l
) {
1985 "erresc(38): Incorrect number of parameters (%d)\n",
1989 r
= attr
[*npar
+ 2];
1990 g
= attr
[*npar
+ 3];
1991 b
= attr
[*npar
+ 4];
1993 if (!BETWEEN(r
, 0, 255) || !BETWEEN(g
, 0, 255) || !BETWEEN(b
, 0, 255))
1994 fprintf(stderr
, "erresc: bad rgb color (%u,%u,%u)\n",
1997 idx
= TRUECOLOR(r
, g
, b
);
1999 case 5: /* indexed color */
2000 if (*npar
+ 2 >= l
) {
2002 "erresc(38): Incorrect number of parameters (%d)\n",
2007 if (!BETWEEN(attr
[*npar
], 0, 255))
2008 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[*npar
]);
2012 case 0: /* implemented defined (only foreground) */
2013 case 1: /* transparent */
2014 case 3: /* direct color in CMY space */
2015 case 4: /* direct color in CMYK space */
2018 "erresc(38): gfx attr %d unknown\n", attr
[*npar
]);
2026 tsetattr(int *attr
, int l
)
2031 for (i
= 0; i
< l
; i
++) {
2034 term
.c
.attr
.mode
&= ~(
2043 term
.c
.attr
.fg
= defaultfg
;
2044 term
.c
.attr
.bg
= defaultbg
;
2047 term
.c
.attr
.mode
|= ATTR_BOLD
;
2050 term
.c
.attr
.mode
|= ATTR_FAINT
;
2053 term
.c
.attr
.mode
|= ATTR_ITALIC
;
2056 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
2058 case 5: /* slow blink */
2060 case 6: /* rapid blink */
2061 term
.c
.attr
.mode
|= ATTR_BLINK
;
2064 term
.c
.attr
.mode
|= ATTR_REVERSE
;
2067 term
.c
.attr
.mode
|= ATTR_INVISIBLE
;
2070 term
.c
.attr
.mode
|= ATTR_STRUCK
;
2073 term
.c
.attr
.mode
&= ~(ATTR_BOLD
| ATTR_FAINT
);
2076 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
2079 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
2082 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
2085 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
2088 term
.c
.attr
.mode
&= ~ATTR_INVISIBLE
;
2091 term
.c
.attr
.mode
&= ~ATTR_STRUCK
;
2094 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
2095 term
.c
.attr
.fg
= idx
;
2098 term
.c
.attr
.fg
= defaultfg
;
2101 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
2102 term
.c
.attr
.bg
= idx
;
2105 term
.c
.attr
.bg
= defaultbg
;
2108 if (BETWEEN(attr
[i
], 30, 37)) {
2109 term
.c
.attr
.fg
= attr
[i
] - 30;
2110 } else if (BETWEEN(attr
[i
], 40, 47)) {
2111 term
.c
.attr
.bg
= attr
[i
] - 40;
2112 } else if (BETWEEN(attr
[i
], 90, 97)) {
2113 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
2114 } else if (BETWEEN(attr
[i
], 100, 107)) {
2115 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
2118 "erresc(default): gfx attr %d unknown\n",
2119 attr
[i
]), csidump();
2127 tsetscroll(int t
, int b
)
2131 LIMIT(t
, 0, term
.row
-1);
2132 LIMIT(b
, 0, term
.row
-1);
2143 tsetmode(int priv
, int set
, int *args
, int narg
)
2148 for (lim
= args
+ narg
; args
< lim
; ++args
) {
2151 case 1: /* DECCKM -- Cursor key */
2152 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
2154 case 5: /* DECSCNM -- Reverse video */
2156 MODBIT(term
.mode
, set
, MODE_REVERSE
);
2157 if (mode
!= term
.mode
)
2160 case 6: /* DECOM -- Origin */
2161 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
2164 case 7: /* DECAWM -- Auto wrap */
2165 MODBIT(term
.mode
, set
, MODE_WRAP
);
2167 case 0: /* Error (IGNORED) */
2168 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
2169 case 3: /* DECCOLM -- Column (IGNORED) */
2170 case 4: /* DECSCLM -- Scroll (IGNORED) */
2171 case 8: /* DECARM -- Auto repeat (IGNORED) */
2172 case 18: /* DECPFF -- Printer feed (IGNORED) */
2173 case 19: /* DECPEX -- Printer extent (IGNORED) */
2174 case 42: /* DECNRCM -- National characters (IGNORED) */
2175 case 12: /* att610 -- Start blinking cursor (IGNORED) */
2177 case 25: /* DECTCEM -- Text Cursor Enable Mode */
2178 MODBIT(term
.mode
, !set
, MODE_HIDE
);
2180 case 9: /* X10 mouse compatibility mode */
2181 xsetpointermotion(0);
2182 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2183 MODBIT(term
.mode
, set
, MODE_MOUSEX10
);
2185 case 1000: /* 1000: report button press */
2186 xsetpointermotion(0);
2187 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2188 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
2190 case 1002: /* 1002: report motion on button press */
2191 xsetpointermotion(0);
2192 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2193 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
2195 case 1003: /* 1003: enable all mouse motions */
2196 xsetpointermotion(set
);
2197 MODBIT(term
.mode
, 0, MODE_MOUSE
);
2198 MODBIT(term
.mode
, set
, MODE_MOUSEMANY
);
2200 case 1004: /* 1004: send focus events to tty */
2201 MODBIT(term
.mode
, set
, MODE_FOCUS
);
2203 case 1006: /* 1006: extended reporting mode */
2204 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
2207 MODBIT(term
.mode
, set
, MODE_8BIT
);
2209 case 1049: /* swap screen & set/restore cursor as xterm */
2210 if (!allowaltscreen
)
2212 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
2214 case 47: /* swap screen */
2216 if (!allowaltscreen
)
2218 alt
= IS_SET(MODE_ALTSCREEN
);
2220 tclearregion(0, 0, term
.col
-1,
2223 if (set
^ alt
) /* set is always 1 or 0 */
2229 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
2231 case 2004: /* 2004: bracketed paste mode */
2232 MODBIT(term
.mode
, set
, MODE_BRCKTPASTE
);
2234 /* Not implemented mouse modes. See comments there. */
2235 case 1001: /* mouse highlight mode; can hang the
2236 terminal by design when implemented. */
2237 case 1005: /* UTF-8 mouse mode; will confuse
2238 applications not supporting UTF-8
2240 case 1015: /* urxvt mangled mouse mode; incompatible
2241 and can be mistaken for other control
2245 "erresc: unknown private set/reset mode %d\n",
2251 case 0: /* Error (IGNORED) */
2253 case 2: /* KAM -- keyboard action */
2254 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
2256 case 4: /* IRM -- Insertion-replacement */
2257 MODBIT(term
.mode
, set
, MODE_INSERT
);
2259 case 12: /* SRM -- Send/Receive */
2260 MODBIT(term
.mode
, !set
, MODE_ECHO
);
2262 case 20: /* LNM -- Linefeed/new line */
2263 MODBIT(term
.mode
, set
, MODE_CRLF
);
2267 "erresc: unknown set/reset mode %d\n",
2281 switch (csiescseq
.mode
[0]) {
2284 fprintf(stderr
, "erresc: unknown csi ");
2288 case '@': /* ICH -- Insert <n> blank char */
2289 DEFAULT(csiescseq
.arg
[0], 1);
2290 tinsertblank(csiescseq
.arg
[0]);
2292 case 'A': /* CUU -- Cursor <n> Up */
2293 DEFAULT(csiescseq
.arg
[0], 1);
2294 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
2296 case 'B': /* CUD -- Cursor <n> Down */
2297 case 'e': /* VPR --Cursor <n> Down */
2298 DEFAULT(csiescseq
.arg
[0], 1);
2299 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
2301 case 'i': /* MC -- Media Copy */
2302 switch (csiescseq
.arg
[0]) {
2307 tdumpline(term
.c
.y
);
2313 term
.mode
&= ~MODE_PRINT
;
2316 term
.mode
|= MODE_PRINT
;
2320 case 'c': /* DA -- Device Attributes */
2321 if (csiescseq
.arg
[0] == 0)
2322 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2324 case 'C': /* CUF -- Cursor <n> Forward */
2325 case 'a': /* HPR -- Cursor <n> Forward */
2326 DEFAULT(csiescseq
.arg
[0], 1);
2327 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
2329 case 'D': /* CUB -- Cursor <n> Backward */
2330 DEFAULT(csiescseq
.arg
[0], 1);
2331 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
2333 case 'E': /* CNL -- Cursor <n> Down and first col */
2334 DEFAULT(csiescseq
.arg
[0], 1);
2335 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
2337 case 'F': /* CPL -- Cursor <n> Up and first col */
2338 DEFAULT(csiescseq
.arg
[0], 1);
2339 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
2341 case 'g': /* TBC -- Tabulation clear */
2342 switch (csiescseq
.arg
[0]) {
2343 case 0: /* clear current tab stop */
2344 term
.tabs
[term
.c
.x
] = 0;
2346 case 3: /* clear all the tabs */
2347 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
2353 case 'G': /* CHA -- Move to <col> */
2355 DEFAULT(csiescseq
.arg
[0], 1);
2356 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
2358 case 'H': /* CUP -- Move to <row> <col> */
2360 DEFAULT(csiescseq
.arg
[0], 1);
2361 DEFAULT(csiescseq
.arg
[1], 1);
2362 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
2364 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
2365 DEFAULT(csiescseq
.arg
[0], 1);
2366 tputtab(csiescseq
.arg
[0]);
2368 case 'J': /* ED -- Clear screen */
2370 switch (csiescseq
.arg
[0]) {
2372 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
2373 if (term
.c
.y
< term
.row
-1) {
2374 tclearregion(0, term
.c
.y
+1, term
.col
-1,
2380 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
2381 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2384 tclearregion(0, 0, term
.col
-1, term
.row
-1);
2390 case 'K': /* EL -- Clear line */
2391 switch (csiescseq
.arg
[0]) {
2393 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
2397 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2400 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
2404 case 'S': /* SU -- Scroll <n> line up */
2405 DEFAULT(csiescseq
.arg
[0], 1);
2406 tscrollup(term
.top
, csiescseq
.arg
[0]);
2408 case 'T': /* SD -- Scroll <n> line down */
2409 DEFAULT(csiescseq
.arg
[0], 1);
2410 tscrolldown(term
.top
, csiescseq
.arg
[0]);
2412 case 'L': /* IL -- Insert <n> blank lines */
2413 DEFAULT(csiescseq
.arg
[0], 1);
2414 tinsertblankline(csiescseq
.arg
[0]);
2416 case 'l': /* RM -- Reset Mode */
2417 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
2419 case 'M': /* DL -- Delete <n> lines */
2420 DEFAULT(csiescseq
.arg
[0], 1);
2421 tdeleteline(csiescseq
.arg
[0]);
2423 case 'X': /* ECH -- Erase <n> char */
2424 DEFAULT(csiescseq
.arg
[0], 1);
2425 tclearregion(term
.c
.x
, term
.c
.y
,
2426 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
2428 case 'P': /* DCH -- Delete <n> char */
2429 DEFAULT(csiescseq
.arg
[0], 1);
2430 tdeletechar(csiescseq
.arg
[0]);
2432 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
2433 DEFAULT(csiescseq
.arg
[0], 1);
2434 tputtab(-csiescseq
.arg
[0]);
2436 case 'd': /* VPA -- Move to <row> */
2437 DEFAULT(csiescseq
.arg
[0], 1);
2438 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
2440 case 'h': /* SM -- Set terminal mode */
2441 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
2443 case 'm': /* SGR -- Terminal attribute (color) */
2444 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
2446 case 'n': /* DSR – Device Status Report (cursor position) */
2447 if (csiescseq
.arg
[0] == 6) {
2448 len
= snprintf(buf
, sizeof(buf
),"\033[%i;%iR",
2449 term
.c
.y
+1, term
.c
.x
+1);
2453 case 'r': /* DECSTBM -- Set Scrolling Region */
2454 if (csiescseq
.priv
) {
2457 DEFAULT(csiescseq
.arg
[0], 1);
2458 DEFAULT(csiescseq
.arg
[1], term
.row
);
2459 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
2463 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2464 tcursor(CURSOR_SAVE
);
2466 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2467 tcursor(CURSOR_LOAD
);
2470 switch (csiescseq
.mode
[1]) {
2471 case 'q': /* DECSCUSR -- Set Cursor Style */
2472 DEFAULT(csiescseq
.arg
[0], 1);
2473 if (!BETWEEN(csiescseq
.arg
[0], 0, 6)) {
2476 xw
.cursor
= csiescseq
.arg
[0];
2492 for (i
= 0; i
< csiescseq
.len
; i
++) {
2493 c
= csiescseq
.buf
[i
] & 0xff;
2496 } else if (c
== '\n') {
2498 } else if (c
== '\r') {
2500 } else if (c
== 0x1b) {
2503 printf("(%02x)", c
);
2512 memset(&csiescseq
, 0, sizeof(csiescseq
));
2521 term
.esc
&= ~(ESC_STR_END
|ESC_STR
);
2523 par
= (narg
= strescseq
.narg
) ? atoi(strescseq
.args
[0]) : 0;
2525 switch (strescseq
.type
) {
2526 case ']': /* OSC -- Operating System Command */
2532 xsettitle(strescseq
.args
[1]);
2534 case 4: /* color set */
2537 p
= strescseq
.args
[2];
2539 case 104: /* color reset, here p = NULL */
2540 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2541 if (xsetcolorname(j
, p
)) {
2542 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2545 * TODO if defaultbg color is changed, borders
2553 case 'k': /* old title set compatibility */
2554 xsettitle(strescseq
.args
[0]);
2556 case 'P': /* DCS -- Device Control String */
2557 term
.mode
|= ESC_DCS
;
2558 case '_': /* APC -- Application Program Command */
2559 case '^': /* PM -- Privacy Message */
2563 fprintf(stderr
, "erresc: unknown str ");
2571 char *p
= strescseq
.buf
;
2574 strescseq
.buf
[strescseq
.len
] = '\0';
2579 while (strescseq
.narg
< STR_ARG_SIZ
) {
2580 strescseq
.args
[strescseq
.narg
++] = p
;
2581 while ((c
= *p
) != ';' && c
!= '\0')
2595 printf("ESC%c", strescseq
.type
);
2596 for (i
= 0; i
< strescseq
.len
; i
++) {
2597 c
= strescseq
.buf
[i
] & 0xff;
2600 } else if (isprint(c
)) {
2602 } else if (c
== '\n') {
2604 } else if (c
== '\r') {
2606 } else if (c
== 0x1b) {
2609 printf("(%02x)", c
);
2618 memset(&strescseq
, 0, sizeof(strescseq
));
2622 sendbreak(const Arg
*arg
)
2624 if (tcsendbreak(cmdfd
, 0))
2625 perror("Error sending break");
2629 tprinter(char *s
, size_t len
)
2631 if (iofd
!= -1 && xwrite(iofd
, s
, len
) < 0) {
2632 fprintf(stderr
, "Error writing in %s:%s\n",
2633 opt_io
, strerror(errno
));
2640 iso14755(const Arg
*arg
)
2642 char cmd
[sizeof(ISO14755CMD
) + NUMMAXLEN(xw
.win
)];
2644 char *us
, *e
, codepoint
[9], uc
[UTF_SIZ
];
2645 unsigned long utf32
;
2647 snprintf(cmd
, sizeof(cmd
), ISO14755CMD
, xw
.win
);
2648 if (!(p
= popen(cmd
, "r")))
2651 us
= fgets(codepoint
, sizeof(codepoint
), p
);
2654 if (!us
|| *us
== '\0' || *us
== '-' || strlen(us
) > 7)
2656 if ((utf32
= strtoul(us
, &e
, 16)) == ULONG_MAX
||
2657 (*e
!= '\n' && *e
!= '\0'))
2660 ttysend(uc
, utf8encode(utf32
, uc
));
2664 toggleprinter(const Arg
*arg
)
2666 term
.mode
^= MODE_PRINT
;
2670 printscreen(const Arg
*arg
)
2676 printsel(const Arg
*arg
)
2686 if ((ptr
= getsel())) {
2687 tprinter(ptr
, strlen(ptr
));
2698 bp
= &term
.line
[n
][0];
2699 end
= &bp
[MIN(tlinelen(n
), term
.col
) - 1];
2700 if (bp
!= end
|| bp
->u
!= ' ') {
2701 for ( ;bp
<= end
; ++bp
)
2702 tprinter(buf
, utf8encode(bp
->u
, buf
));
2712 for (i
= 0; i
< term
.row
; ++i
)
2722 while (x
< term
.col
&& n
--)
2723 for (++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2726 while (x
> 0 && n
++)
2727 for (--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2730 term
.c
.x
= LIMIT(x
, 0, term
.col
-1);
2736 if (ISCONTROL(u
)) { /* control code */
2741 } else if (u
!= '\n' && u
!= '\r' && u
!= '\t') {
2750 tdefutf8(char ascii
)
2753 term
.mode
|= MODE_UTF8
;
2754 else if (ascii
== '@')
2755 term
.mode
&= ~MODE_UTF8
;
2759 tdeftran(char ascii
)
2761 static char cs
[] = "0B";
2762 static int vcs
[] = {CS_GRAPHIC0
, CS_USA
};
2765 if ((p
= strchr(cs
, ascii
)) == NULL
) {
2766 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2768 term
.trantbl
[term
.icharset
] = vcs
[p
- cs
];
2777 if (c
== '8') { /* DEC screen alignment test. */
2778 for (x
= 0; x
< term
.col
; ++x
) {
2779 for (y
= 0; y
< term
.row
; ++y
)
2780 tsetchar('E', &term
.c
.attr
, x
, y
);
2786 tstrsequence(uchar c
)
2791 case 0x90: /* DCS -- Device Control String */
2793 term
.esc
|= ESC_DCS
;
2795 case 0x9f: /* APC -- Application Program Command */
2798 case 0x9e: /* PM -- Privacy Message */
2801 case 0x9d: /* OSC -- Operating System Command */
2806 term
.esc
|= ESC_STR
;
2810 tcontrolcode(uchar ascii
)
2817 tmoveto(term
.c
.x
-1, term
.c
.y
);
2820 tmoveto(0, term
.c
.y
);
2825 /* go to first col if the mode is set */
2826 tnewline(IS_SET(MODE_CRLF
));
2828 case '\a': /* BEL */
2829 if (term
.esc
& ESC_STR_END
) {
2830 /* backwards compatibility to xterm */
2833 if (!(xw
.state
& WIN_FOCUSED
))
2836 XkbBell(xw
.dpy
, xw
.win
, bellvolume
, (Atom
)NULL
);
2839 case '\033': /* ESC */
2841 term
.esc
&= ~(ESC_CSI
|ESC_ALTCHARSET
|ESC_TEST
);
2842 term
.esc
|= ESC_START
;
2844 case '\016': /* SO (LS1 -- Locking shift 1) */
2845 case '\017': /* SI (LS0 -- Locking shift 0) */
2846 term
.charset
= 1 - (ascii
- '\016');
2848 case '\032': /* SUB */
2849 tsetchar('?', &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2850 case '\030': /* CAN */
2853 case '\005': /* ENQ (IGNORED) */
2854 case '\000': /* NUL (IGNORED) */
2855 case '\021': /* XON (IGNORED) */
2856 case '\023': /* XOFF (IGNORED) */
2857 case 0177: /* DEL (IGNORED) */
2859 case 0x80: /* TODO: PAD */
2860 case 0x81: /* TODO: HOP */
2861 case 0x82: /* TODO: BPH */
2862 case 0x83: /* TODO: NBH */
2863 case 0x84: /* TODO: IND */
2865 case 0x85: /* NEL -- Next line */
2866 tnewline(1); /* always go to first col */
2868 case 0x86: /* TODO: SSA */
2869 case 0x87: /* TODO: ESA */
2871 case 0x88: /* HTS -- Horizontal tab stop */
2872 term
.tabs
[term
.c
.x
] = 1;
2874 case 0x89: /* TODO: HTJ */
2875 case 0x8a: /* TODO: VTS */
2876 case 0x8b: /* TODO: PLD */
2877 case 0x8c: /* TODO: PLU */
2878 case 0x8d: /* TODO: RI */
2879 case 0x8e: /* TODO: SS2 */
2880 case 0x8f: /* TODO: SS3 */
2881 case 0x91: /* TODO: PU1 */
2882 case 0x92: /* TODO: PU2 */
2883 case 0x93: /* TODO: STS */
2884 case 0x94: /* TODO: CCH */
2885 case 0x95: /* TODO: MW */
2886 case 0x96: /* TODO: SPA */
2887 case 0x97: /* TODO: EPA */
2888 case 0x98: /* TODO: SOS */
2889 case 0x99: /* TODO: SGCI */
2891 case 0x9a: /* DECID -- Identify Terminal */
2892 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2894 case 0x9b: /* TODO: CSI */
2895 case 0x9c: /* TODO: ST */
2897 case 0x90: /* DCS -- Device Control String */
2898 case 0x9d: /* OSC -- Operating System Command */
2899 case 0x9e: /* PM -- Privacy Message */
2900 case 0x9f: /* APC -- Application Program Command */
2901 tstrsequence(ascii
);
2904 /* only CAN, SUB, \a and C1 chars interrupt a sequence */
2905 term
.esc
&= ~(ESC_STR_END
|ESC_STR
);
2909 * returns 1 when the sequence is finished and it hasn't to read
2910 * more characters for this sequence, otherwise 0
2913 eschandle(uchar ascii
)
2917 term
.esc
|= ESC_CSI
;
2920 term
.esc
|= ESC_TEST
;
2923 term
.esc
|= ESC_UTF8
;
2925 case 'P': /* DCS -- Device Control String */
2926 case '_': /* APC -- Application Program Command */
2927 case '^': /* PM -- Privacy Message */
2928 case ']': /* OSC -- Operating System Command */
2929 case 'k': /* old title set compatibility */
2930 tstrsequence(ascii
);
2932 case 'n': /* LS2 -- Locking shift 2 */
2933 case 'o': /* LS3 -- Locking shift 3 */
2934 term
.charset
= 2 + (ascii
- 'n');
2936 case '(': /* GZD4 -- set primary charset G0 */
2937 case ')': /* G1D4 -- set secondary charset G1 */
2938 case '*': /* G2D4 -- set tertiary charset G2 */
2939 case '+': /* G3D4 -- set quaternary charset G3 */
2940 term
.icharset
= ascii
- '(';
2941 term
.esc
|= ESC_ALTCHARSET
;
2943 case 'D': /* IND -- Linefeed */
2944 if (term
.c
.y
== term
.bot
) {
2945 tscrollup(term
.top
, 1);
2947 tmoveto(term
.c
.x
, term
.c
.y
+1);
2950 case 'E': /* NEL -- Next line */
2951 tnewline(1); /* always go to first col */
2953 case 'H': /* HTS -- Horizontal tab stop */
2954 term
.tabs
[term
.c
.x
] = 1;
2956 case 'M': /* RI -- Reverse index */
2957 if (term
.c
.y
== term
.top
) {
2958 tscrolldown(term
.top
, 1);
2960 tmoveto(term
.c
.x
, term
.c
.y
-1);
2963 case 'Z': /* DECID -- Identify Terminal */
2964 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2966 case 'c': /* RIS -- Reset to inital state */
2971 case '=': /* DECPAM -- Application keypad */
2972 term
.mode
|= MODE_APPKEYPAD
;
2974 case '>': /* DECPNM -- Normal keypad */
2975 term
.mode
&= ~MODE_APPKEYPAD
;
2977 case '7': /* DECSC -- Save Cursor */
2978 tcursor(CURSOR_SAVE
);
2980 case '8': /* DECRC -- Restore Cursor */
2981 tcursor(CURSOR_LOAD
);
2983 case '\\': /* ST -- String Terminator */
2984 if (term
.esc
& ESC_STR_END
)
2988 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2989 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
3003 control
= ISCONTROL(u
);
3004 if (!IS_SET(MODE_UTF8
) && !IS_SET(MODE_SIXEL
)) {
3008 len
= utf8encode(u
, c
);
3009 if (!control
&& (width
= wcwidth(u
)) == -1) {
3010 memcpy(c
, "\357\277\275", 4); /* UTF_INVALID */
3015 if (IS_SET(MODE_PRINT
))
3019 * STR sequence must be checked before anything else
3020 * because it uses all following characters until it
3021 * receives a ESC, a SUB, a ST or any other C1 control
3024 if (term
.esc
& ESC_STR
) {
3025 if (u
== '\a' || u
== 030 || u
== 032 || u
== 033 ||
3027 term
.esc
&= ~(ESC_START
|ESC_STR
|ESC_DCS
);
3028 if (IS_SET(MODE_SIXEL
)) {
3029 /* TODO: render sixel */;
3030 term
.mode
&= ~MODE_SIXEL
;
3033 term
.esc
|= ESC_STR_END
;
3034 goto check_control_code
;
3038 if (IS_SET(MODE_SIXEL
)) {
3039 /* TODO: implement sixel mode */
3042 if (term
.esc
&ESC_DCS
&& strescseq
.len
== 0 && u
== 'q')
3043 term
.mode
|= MODE_SIXEL
;
3045 if (strescseq
.len
+len
>= sizeof(strescseq
.buf
)-1) {
3047 * Here is a bug in terminals. If the user never sends
3048 * some code to stop the str or esc command, then st
3049 * will stop responding. But this is better than
3050 * silently failing with unknown characters. At least
3051 * then users will report back.
3053 * In the case users ever get fixed, here is the code:
3062 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
3063 strescseq
.len
+= len
;
3069 * Actions of control codes must be performed as soon they arrive
3070 * because they can be embedded inside a control sequence, and
3071 * they must not cause conflicts with sequences.
3076 * control codes are not shown ever
3079 } else if (term
.esc
& ESC_START
) {
3080 if (term
.esc
& ESC_CSI
) {
3081 csiescseq
.buf
[csiescseq
.len
++] = u
;
3082 if (BETWEEN(u
, 0x40, 0x7E)
3083 || csiescseq
.len
>= \
3084 sizeof(csiescseq
.buf
)-1) {
3090 } else if (term
.esc
& ESC_UTF8
) {
3092 } else if (term
.esc
& ESC_ALTCHARSET
) {
3094 } else if (term
.esc
& ESC_TEST
) {
3099 /* sequence already finished */
3103 * All characters which form part of a sequence are not
3108 if (sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
3111 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
3112 if (IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
3113 gp
->mode
|= ATTR_WRAP
;
3115 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
3118 if (IS_SET(MODE_INSERT
) && term
.c
.x
+width
< term
.col
)
3119 memmove(gp
+width
, gp
, (term
.col
- term
.c
.x
- width
) * sizeof(Glyph
));
3121 if (term
.c
.x
+width
> term
.col
) {
3123 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
3126 tsetchar(u
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
3129 gp
->mode
|= ATTR_WIDE
;
3130 if (term
.c
.x
+1 < term
.col
) {
3132 gp
[1].mode
= ATTR_WDUMMY
;
3135 if (term
.c
.x
+width
< term
.col
) {
3136 tmoveto(term
.c
.x
+width
, term
.c
.y
);
3138 term
.c
.state
|= CURSOR_WRAPNEXT
;
3143 tresize(int col
, int row
)
3146 int minrow
= MIN(row
, term
.row
);
3147 int mincol
= MIN(col
, term
.col
);
3151 if (col
< 1 || row
< 1) {
3153 "tresize: error resizing to %dx%d\n", col
, row
);
3158 * slide screen to keep cursor where we expect it -
3159 * tscrollup would work here, but we can optimize to
3160 * memmove because we're freeing the earlier lines
3162 for (i
= 0; i
<= term
.c
.y
- row
; i
++) {
3166 /* ensure that both src and dst are not NULL */
3168 memmove(term
.line
, term
.line
+ i
, row
* sizeof(Line
));
3169 memmove(term
.alt
, term
.alt
+ i
, row
* sizeof(Line
));
3171 for (i
+= row
; i
< term
.row
; i
++) {
3176 /* resize to new width */
3177 term
.specbuf
= xrealloc(term
.specbuf
, col
* sizeof(XftGlyphFontSpec
));
3179 /* resize to new height */
3180 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
3181 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
3182 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
3183 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
3185 /* resize each row to new width, zero-pad if needed */
3186 for (i
= 0; i
< minrow
; i
++) {
3187 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
3188 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
3191 /* allocate any new rows */
3192 for (/* i == minrow */; i
< row
; i
++) {
3193 term
.line
[i
] = xmalloc(col
* sizeof(Glyph
));
3194 term
.alt
[i
] = xmalloc(col
* sizeof(Glyph
));
3196 if (col
> term
.col
) {
3197 bp
= term
.tabs
+ term
.col
;
3199 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
3200 while (--bp
> term
.tabs
&& !*bp
)
3202 for (bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
3205 /* update terminal size */
3208 /* reset scrolling region */
3209 tsetscroll(0, row
-1);
3210 /* make use of the LIMIT in tmoveto */
3211 tmoveto(term
.c
.x
, term
.c
.y
);
3212 /* Clearing both screens (it makes dirty all lines) */
3214 for (i
= 0; i
< 2; i
++) {
3215 if (mincol
< col
&& 0 < minrow
) {
3216 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
3218 if (0 < col
&& minrow
< row
) {
3219 tclearregion(0, minrow
, col
- 1, row
- 1);
3222 tcursor(CURSOR_LOAD
);
3228 xresize(int col
, int row
)
3230 xw
.tw
= MAX(1, col
* xw
.cw
);
3231 xw
.th
= MAX(1, row
* xw
.ch
);
3233 XFreePixmap(xw
.dpy
, xw
.buf
);
3234 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
3235 DefaultDepth(xw
.dpy
, xw
.scr
));
3236 XftDrawChange(xw
.draw
, xw
.buf
);
3237 xclear(0, 0, xw
.w
, xw
.h
);
3241 sixd_to_16bit(int x
)
3243 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
3247 xloadcolor(int i
, const char *name
, Color
*ncolor
)
3249 XRenderColor color
= { .alpha
= 0xffff };
3252 if (BETWEEN(i
, 16, 255)) { /* 256 color */
3253 if (i
< 6*6*6+16) { /* same colors as xterm */
3254 color
.red
= sixd_to_16bit( ((i
-16)/36)%6 );
3255 color
.green
= sixd_to_16bit( ((i
-16)/6) %6 );
3256 color
.blue
= sixd_to_16bit( ((i
-16)/1) %6 );
3257 } else { /* greyscale */
3258 color
.red
= 0x0808 + 0x0a0a * (i
- (6*6*6+16));
3259 color
.green
= color
.blue
= color
.red
;
3261 return XftColorAllocValue(xw
.dpy
, xw
.vis
,
3262 xw
.cmap
, &color
, ncolor
);
3264 name
= colorname
[i
];
3267 return XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, ncolor
);
3278 for (cp
= dc
.col
; cp
< &dc
.col
[LEN(dc
.col
)]; ++cp
)
3279 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
3282 for (i
= 0; i
< LEN(dc
.col
); i
++)
3283 if (!xloadcolor(i
, NULL
, &dc
.col
[i
])) {
3285 die("Could not allocate color '%s'\n", colorname
[i
]);
3287 die("Could not allocate color %d\n", i
);
3293 xsetcolorname(int x
, const char *name
)
3297 if (!BETWEEN(x
, 0, LEN(dc
.col
)))
3301 if (!xloadcolor(x
, name
, &ncolor
))
3304 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, &dc
.col
[x
]);
3311 * Absolute coordinates.
3314 xclear(int x1
, int y1
, int x2
, int y2
)
3316 XftDrawRect(xw
.draw
,
3317 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
3318 x1
, y1
, x2
-x1
, y2
-y1
);
3324 XClassHint
class = {opt_name
? opt_name
: termname
,
3325 opt_class
? opt_class
: termname
};
3326 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
3327 XSizeHints
*sizeh
= NULL
;
3329 sizeh
= XAllocSizeHints();
3331 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
3332 sizeh
->height
= xw
.h
;
3333 sizeh
->width
= xw
.w
;
3334 sizeh
->height_inc
= xw
.ch
;
3335 sizeh
->width_inc
= xw
.cw
;
3336 sizeh
->base_height
= 2 * borderpx
;
3337 sizeh
->base_width
= 2 * borderpx
;
3339 sizeh
->flags
|= PMaxSize
| PMinSize
;
3340 sizeh
->min_width
= sizeh
->max_width
= xw
.w
;
3341 sizeh
->min_height
= sizeh
->max_height
= xw
.h
;
3343 if (xw
.gm
& (XValue
|YValue
)) {
3344 sizeh
->flags
|= USPosition
| PWinGravity
;
3347 sizeh
->win_gravity
= xgeommasktogravity(xw
.gm
);
3350 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
3356 xgeommasktogravity(int mask
)
3358 switch (mask
& (XNegative
|YNegative
)) {
3360 return NorthWestGravity
;
3362 return NorthEastGravity
;
3364 return SouthWestGravity
;
3367 return SouthEastGravity
;
3371 xloadfont(Font
*f
, FcPattern
*pattern
)
3377 match
= XftFontMatch(xw
.dpy
, xw
.scr
, pattern
, &result
);
3381 if (!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
3382 FcPatternDestroy(match
);
3386 XftTextExtentsUtf8(xw
.dpy
, f
->match
,
3387 (const FcChar8
*) ascii_printable
,
3388 strlen(ascii_printable
), &extents
);
3391 f
->pattern
= FcPatternDuplicate(pattern
);
3393 f
->ascent
= f
->match
->ascent
;
3394 f
->descent
= f
->match
->descent
;
3396 f
->rbearing
= f
->match
->max_advance_width
;
3398 f
->height
= f
->ascent
+ f
->descent
;
3399 f
->width
= DIVCEIL(extents
.xOff
, strlen(ascii_printable
));
3405 xloadfonts(char *fontstr
, double fontsize
)
3411 if (fontstr
[0] == '-') {
3412 pattern
= XftXlfdParse(fontstr
, False
, False
);
3414 pattern
= FcNameParse((FcChar8
*)fontstr
);
3418 die("st: can't open font %s\n", fontstr
);
3421 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
3422 FcPatternDel(pattern
, FC_SIZE
);
3423 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
3424 usedfontsize
= fontsize
;
3426 if (FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
) ==
3428 usedfontsize
= fontval
;
3429 } else if (FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
) ==
3434 * Default font size is 12, if none given. This is to
3435 * have a known usedfontsize value.
3437 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
3440 defaultfontsize
= usedfontsize
;
3443 if (xloadfont(&dc
.font
, pattern
))
3444 die("st: can't open font %s\n", fontstr
);
3446 if (usedfontsize
< 0) {
3447 FcPatternGetDouble(dc
.font
.match
->pattern
,
3448 FC_PIXEL_SIZE
, 0, &fontval
);
3449 usedfontsize
= fontval
;
3451 defaultfontsize
= fontval
;
3454 /* Setting character width and height. */
3455 xw
.cw
= ceilf(dc
.font
.width
* cwscale
);
3456 xw
.ch
= ceilf(dc
.font
.height
* chscale
);
3458 FcPatternDel(pattern
, FC_SLANT
);
3459 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
3460 if (xloadfont(&dc
.ifont
, pattern
))
3461 die("st: can't open font %s\n", fontstr
);
3463 FcPatternDel(pattern
, FC_WEIGHT
);
3464 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
3465 if (xloadfont(&dc
.ibfont
, pattern
))
3466 die("st: can't open font %s\n", fontstr
);
3468 FcPatternDel(pattern
, FC_SLANT
);
3469 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
3470 if (xloadfont(&dc
.bfont
, pattern
))
3471 die("st: can't open font %s\n", fontstr
);
3473 FcPatternDestroy(pattern
);
3477 xunloadfont(Font
*f
)
3479 XftFontClose(xw
.dpy
, f
->match
);
3480 FcPatternDestroy(f
->pattern
);
3482 FcFontSetDestroy(f
->set
);
3488 /* Free the loaded fonts in the font cache. */
3490 XftFontClose(xw
.dpy
, frc
[--frclen
].font
);
3492 xunloadfont(&dc
.font
);
3493 xunloadfont(&dc
.bfont
);
3494 xunloadfont(&dc
.ifont
);
3495 xunloadfont(&dc
.ibfont
);
3499 xzoom(const Arg
*arg
)
3503 larg
.f
= usedfontsize
+ arg
->f
;
3508 xzoomabs(const Arg
*arg
)
3511 xloadfonts(usedfont
, arg
->f
);
3519 xzoomreset(const Arg
*arg
)
3523 if (defaultfontsize
> 0) {
3524 larg
.f
= defaultfontsize
;
3535 pid_t thispid
= getpid();
3536 XColor xmousefg
, xmousebg
;
3538 if (!(xw
.dpy
= XOpenDisplay(NULL
)))
3539 die("Can't open display\n");
3540 xw
.scr
= XDefaultScreen(xw
.dpy
);
3541 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
3545 die("Could not init fontconfig.\n");
3547 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
3548 xloadfonts(usedfont
, 0);
3551 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
3554 /* adjust fixed window geometry */
3555 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
3556 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
3557 if (xw
.gm
& XNegative
)
3558 xw
.l
+= DisplayWidth(xw
.dpy
, xw
.scr
) - xw
.w
- 2;
3559 if (xw
.gm
& YNegative
)
3560 xw
.t
+= DisplayHeight(xw
.dpy
, xw
.scr
) - xw
.h
- 2;
3563 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
3564 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
3565 xw
.attrs
.bit_gravity
= NorthWestGravity
;
3566 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
3567 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
3568 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
3569 xw
.attrs
.colormap
= xw
.cmap
;
3571 if (!(opt_embed
&& (parent
= strtol(opt_embed
, NULL
, 0))))
3572 parent
= XRootWindow(xw
.dpy
, xw
.scr
);
3573 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.l
, xw
.t
,
3574 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
3575 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
3576 | CWEventMask
| CWColormap
, &xw
.attrs
);
3578 memset(&gcvalues
, 0, sizeof(gcvalues
));
3579 gcvalues
.graphics_exposures
= False
;
3580 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
3582 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
3583 DefaultDepth(xw
.dpy
, xw
.scr
));
3584 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
3585 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
3587 /* Xft rendering context */
3588 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
3591 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3592 XSetLocaleModifiers("@im=local");
3593 if ((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3594 XSetLocaleModifiers("@im=");
3595 if ((xw
.xim
= XOpenIM(xw
.dpy
,
3596 NULL
, NULL
, NULL
)) == NULL
) {
3597 die("XOpenIM failed. Could not open input"
3602 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
3603 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
3604 XNFocusWindow
, xw
.win
, NULL
);
3606 die("XCreateIC failed. Could not obtain input method.\n");
3608 /* white cursor, black outline */
3609 cursor
= XCreateFontCursor(xw
.dpy
, mouseshape
);
3610 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
3612 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousefg
], &xmousefg
) == 0) {
3613 xmousefg
.red
= 0xffff;
3614 xmousefg
.green
= 0xffff;
3615 xmousefg
.blue
= 0xffff;
3618 if (XParseColor(xw
.dpy
, xw
.cmap
, colorname
[mousebg
], &xmousebg
) == 0) {
3619 xmousebg
.red
= 0x0000;
3620 xmousebg
.green
= 0x0000;
3621 xmousebg
.blue
= 0x0000;
3624 XRecolorCursor(xw
.dpy
, cursor
, &xmousefg
, &xmousebg
);
3626 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
3627 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
3628 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
3629 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
3631 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
3632 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
3633 PropModeReplace
, (uchar
*)&thispid
, 1);
3636 XMapWindow(xw
.dpy
, xw
.win
);
3638 XSync(xw
.dpy
, False
);
3642 xmakeglyphfontspecs(XftGlyphFontSpec
*specs
, const Glyph
*glyphs
, int len
, int x
, int y
)
3644 float winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
, xp
, yp
;
3645 ushort mode
, prevmode
= USHRT_MAX
;
3646 Font
*font
= &dc
.font
;
3647 int frcflags
= FRC_NORMAL
;
3648 float runewidth
= xw
.cw
;
3652 FcPattern
*fcpattern
, *fontpattern
;
3653 FcFontSet
*fcsets
[] = { NULL
};
3654 FcCharSet
*fccharset
;
3655 int i
, f
, numspecs
= 0;
3657 for (i
= 0, xp
= winx
, yp
= winy
+ font
->ascent
; i
< len
; ++i
) {
3658 /* Fetch rune and mode for current glyph. */
3660 mode
= glyphs
[i
].mode
;
3662 /* Skip dummy wide-character spacing. */
3663 if (mode
== ATTR_WDUMMY
)
3666 /* Determine font for glyph if different from previous glyph. */
3667 if (prevmode
!= mode
) {
3670 frcflags
= FRC_NORMAL
;
3671 runewidth
= xw
.cw
* ((mode
& ATTR_WIDE
) ? 2.0f
: 1.0f
);
3672 if ((mode
& ATTR_ITALIC
) && (mode
& ATTR_BOLD
)) {
3674 frcflags
= FRC_ITALICBOLD
;
3675 } else if (mode
& ATTR_ITALIC
) {
3677 frcflags
= FRC_ITALIC
;
3678 } else if (mode
& ATTR_BOLD
) {
3680 frcflags
= FRC_BOLD
;
3682 yp
= winy
+ font
->ascent
;
3685 /* Lookup character index with default font. */
3686 glyphidx
= XftCharIndex(xw
.dpy
, font
->match
, rune
);
3688 specs
[numspecs
].font
= font
->match
;
3689 specs
[numspecs
].glyph
= glyphidx
;
3690 specs
[numspecs
].x
= (short)xp
;
3691 specs
[numspecs
].y
= (short)yp
;
3697 /* Fallback on font cache, search the font cache for match. */
3698 for (f
= 0; f
< frclen
; f
++) {
3699 glyphidx
= XftCharIndex(xw
.dpy
, frc
[f
].font
, rune
);
3700 /* Everything correct. */
3701 if (glyphidx
&& frc
[f
].flags
== frcflags
)
3703 /* We got a default font for a not found glyph. */
3704 if (!glyphidx
&& frc
[f
].flags
== frcflags
3705 && frc
[f
].unicodep
== rune
) {
3710 /* Nothing was found. Use fontconfig to find matching font. */
3713 font
->set
= FcFontSort(0, font
->pattern
,
3715 fcsets
[0] = font
->set
;
3718 * Nothing was found in the cache. Now use
3719 * some dozen of Fontconfig calls to get the
3720 * font for one single character.
3722 * Xft and fontconfig are design failures.
3724 fcpattern
= FcPatternDuplicate(font
->pattern
);
3725 fccharset
= FcCharSetCreate();
3727 FcCharSetAddChar(fccharset
, rune
);
3728 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3730 FcPatternAddBool(fcpattern
, FC_SCALABLE
, 1);
3732 FcConfigSubstitute(0, fcpattern
,
3734 FcDefaultSubstitute(fcpattern
);
3736 fontpattern
= FcFontSetMatch(0, fcsets
, 1,
3740 * Overwrite or create the new cache entry.
3742 if (frclen
>= LEN(frc
)) {
3743 frclen
= LEN(frc
) - 1;
3744 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
3745 frc
[frclen
].unicodep
= 0;
3748 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
3750 frc
[frclen
].flags
= frcflags
;
3751 frc
[frclen
].unicodep
= rune
;
3753 glyphidx
= XftCharIndex(xw
.dpy
, frc
[frclen
].font
, rune
);
3758 FcPatternDestroy(fcpattern
);
3759 FcCharSetDestroy(fccharset
);
3762 specs
[numspecs
].font
= frc
[f
].font
;
3763 specs
[numspecs
].glyph
= glyphidx
;
3764 specs
[numspecs
].x
= (short)xp
;
3765 specs
[numspecs
].y
= (short)yp
;
3774 xdrawglyphfontspecs(const XftGlyphFontSpec
*specs
, Glyph base
, int len
, int x
, int y
)
3776 int charlen
= len
* ((base
.mode
& ATTR_WIDE
) ? 2 : 1);
3777 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
3778 width
= charlen
* xw
.cw
;
3779 Color
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
3780 XRenderColor colfg
, colbg
;
3783 /* Determine foreground and background colors based on mode. */
3784 if (base
.fg
== defaultfg
) {
3785 if (base
.mode
& ATTR_ITALIC
)
3786 base
.fg
= defaultitalic
;
3787 else if ((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
))
3788 base
.fg
= defaultitalic
;
3789 else if (base
.mode
& ATTR_UNDERLINE
)
3790 base
.fg
= defaultunderline
;
3793 if (IS_TRUECOL(base
.fg
)) {
3794 colfg
.alpha
= 0xffff;
3795 colfg
.red
= TRUERED(base
.fg
);
3796 colfg
.green
= TRUEGREEN(base
.fg
);
3797 colfg
.blue
= TRUEBLUE(base
.fg
);
3798 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
3801 fg
= &dc
.col
[base
.fg
];
3804 if (IS_TRUECOL(base
.bg
)) {
3805 colbg
.alpha
= 0xffff;
3806 colbg
.green
= TRUEGREEN(base
.bg
);
3807 colbg
.red
= TRUERED(base
.bg
);
3808 colbg
.blue
= TRUEBLUE(base
.bg
);
3809 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
3812 bg
= &dc
.col
[base
.bg
];
3815 /* Change basic system colors [0-7] to bright system colors [8-15] */
3816 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_BOLD
&& BETWEEN(base
.fg
, 0, 7))
3817 fg
= &dc
.col
[base
.fg
+ 8];
3819 if (IS_SET(MODE_REVERSE
)) {
3820 if (fg
== &dc
.col
[defaultfg
]) {
3821 fg
= &dc
.col
[defaultbg
];
3823 colfg
.red
= ~fg
->color
.red
;
3824 colfg
.green
= ~fg
->color
.green
;
3825 colfg
.blue
= ~fg
->color
.blue
;
3826 colfg
.alpha
= fg
->color
.alpha
;
3827 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
3832 if (bg
== &dc
.col
[defaultbg
]) {
3833 bg
= &dc
.col
[defaultfg
];
3835 colbg
.red
= ~bg
->color
.red
;
3836 colbg
.green
= ~bg
->color
.green
;
3837 colbg
.blue
= ~bg
->color
.blue
;
3838 colbg
.alpha
= bg
->color
.alpha
;
3839 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
3845 if (base
.mode
& ATTR_REVERSE
) {
3851 if ((base
.mode
& ATTR_BOLD_FAINT
) == ATTR_FAINT
) {
3852 colfg
.red
= fg
->color
.red
/ 2;
3853 colfg
.green
= fg
->color
.green
/ 2;
3854 colfg
.blue
= fg
->color
.blue
/ 2;
3855 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
3859 if (base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
3862 if (base
.mode
& ATTR_INVISIBLE
)
3865 /* Intelligent cleaning up of the borders. */
3867 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
3868 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
3870 if (x
+ charlen
>= term
.col
) {
3871 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
3872 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
3875 xclear(winx
, 0, winx
+ width
, borderpx
);
3876 if (y
== term
.row
-1)
3877 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
3879 /* Clean up the region we want to draw to. */
3880 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
3882 /* Set the clip region because Xft is sometimes dirty. */
3887 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3889 /* Render the glyphs. */
3890 XftDrawGlyphFontSpec(xw
.draw
, fg
, specs
, len
);
3892 /* Render underline and strikethrough. */
3893 if (base
.mode
& ATTR_UNDERLINE
) {
3894 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ dc
.font
.ascent
+ 1,
3898 if (base
.mode
& ATTR_STRUCK
) {
3899 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ 2 * dc
.font
.ascent
/ 3,
3903 /* Reset clip to none. */
3904 XftDrawSetClip(xw
.draw
, 0);
3908 xdrawglyph(Glyph g
, int x
, int y
)
3911 XftGlyphFontSpec spec
;
3913 numspecs
= xmakeglyphfontspecs(&spec
, &g
, 1, x
, y
);
3914 xdrawglyphfontspecs(&spec
, g
, numspecs
, x
, y
);
3920 static int oldx
= 0, oldy
= 0;
3922 Glyph g
= {' ', ATTR_NULL
, defaultbg
, defaultcs
}, og
;
3923 int ena_sel
= sel
.ob
.x
!= -1 && sel
.alt
== IS_SET(MODE_ALTSCREEN
);
3926 LIMIT(oldx
, 0, term
.col
-1);
3927 LIMIT(oldy
, 0, term
.row
-1);
3931 /* adjust position if in dummy */
3932 if (term
.line
[oldy
][oldx
].mode
& ATTR_WDUMMY
)
3934 if (term
.line
[term
.c
.y
][curx
].mode
& ATTR_WDUMMY
)
3937 /* remove the old cursor */
3938 og
= term
.line
[oldy
][oldx
];
3939 if (ena_sel
&& selected(oldx
, oldy
))
3940 og
.mode
^= ATTR_REVERSE
;
3941 xdrawglyph(og
, oldx
, oldy
);
3943 g
.u
= term
.line
[term
.c
.y
][term
.c
.x
].u
;
3946 * Select the right color for the right mode.
3948 if (IS_SET(MODE_REVERSE
)) {
3949 g
.mode
|= ATTR_REVERSE
;
3951 if (ena_sel
&& selected(term
.c
.x
, term
.c
.y
)) {
3952 drawcol
= dc
.col
[defaultcs
];
3955 drawcol
= dc
.col
[defaultrcs
];
3959 if (ena_sel
&& selected(term
.c
.x
, term
.c
.y
)) {
3960 drawcol
= dc
.col
[defaultrcs
];
3964 drawcol
= dc
.col
[defaultcs
];
3968 if (IS_SET(MODE_HIDE
))
3971 /* draw the new one */
3972 if (xw
.state
& WIN_FOCUSED
) {
3973 switch (xw
.cursor
) {
3974 case 7: /* st extension: snowman */
3975 utf8decode("☃", &g
.u
, UTF_SIZ
);
3976 case 0: /* Blinking Block */
3977 case 1: /* Blinking Block (Default) */
3978 case 2: /* Steady Block */
3979 g
.mode
|= term
.line
[term
.c
.y
][curx
].mode
& ATTR_WIDE
;
3980 xdrawglyph(g
, term
.c
.x
, term
.c
.y
);
3982 case 3: /* Blinking Underline */
3983 case 4: /* Steady Underline */
3984 XftDrawRect(xw
.draw
, &drawcol
,
3985 borderpx
+ curx
* xw
.cw
,
3986 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- \
3988 xw
.cw
, cursorthickness
);
3990 case 5: /* Blinking bar */
3991 case 6: /* Steady bar */
3992 XftDrawRect(xw
.draw
, &drawcol
,
3993 borderpx
+ curx
* xw
.cw
,
3994 borderpx
+ term
.c
.y
* xw
.ch
,
3995 cursorthickness
, xw
.ch
);
3999 XftDrawRect(xw
.draw
, &drawcol
,
4000 borderpx
+ curx
* xw
.cw
,
4001 borderpx
+ term
.c
.y
* xw
.ch
,
4003 XftDrawRect(xw
.draw
, &drawcol
,
4004 borderpx
+ curx
* xw
.cw
,
4005 borderpx
+ term
.c
.y
* xw
.ch
,
4007 XftDrawRect(xw
.draw
, &drawcol
,
4008 borderpx
+ (curx
+ 1) * xw
.cw
- 1,
4009 borderpx
+ term
.c
.y
* xw
.ch
,
4011 XftDrawRect(xw
.draw
, &drawcol
,
4012 borderpx
+ curx
* xw
.cw
,
4013 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
4016 oldx
= curx
, oldy
= term
.c
.y
;
4025 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
4027 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
4028 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
4035 xsettitle(opt_title
? opt_title
: "st");
4048 drawregion(0, 0, term
.col
, term
.row
);
4049 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
4051 XSetForeground(xw
.dpy
, dc
.gc
,
4052 dc
.col
[IS_SET(MODE_REVERSE
)?
4053 defaultfg
: defaultbg
].pixel
);
4057 drawregion(int x1
, int y1
, int x2
, int y2
)
4059 int i
, x
, y
, ox
, numspecs
;
4061 XftGlyphFontSpec
*specs
;
4062 int ena_sel
= sel
.ob
.x
!= -1 && sel
.alt
== IS_SET(MODE_ALTSCREEN
);
4064 if (!(xw
.state
& WIN_VISIBLE
))
4067 for (y
= y1
; y
< y2
; y
++) {
4073 specs
= term
.specbuf
;
4074 numspecs
= xmakeglyphfontspecs(specs
, &term
.line
[y
][x1
], x2
- x1
, x1
, y
);
4077 for (x
= x1
; x
< x2
&& i
< numspecs
; x
++) {
4078 new = term
.line
[y
][x
];
4079 if (new.mode
== ATTR_WDUMMY
)
4081 if (ena_sel
&& selected(x
, y
))
4082 new.mode
^= ATTR_REVERSE
;
4083 if (i
> 0 && ATTRCMP(base
, new)) {
4084 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
4096 xdrawglyphfontspecs(specs
, base
, i
, ox
, y
);
4108 visibility(XEvent
*ev
)
4110 XVisibilityEvent
*e
= &ev
->xvisibility
;
4112 MODBIT(xw
.state
, e
->state
!= VisibilityFullyObscured
, WIN_VISIBLE
);
4118 xw
.state
&= ~WIN_VISIBLE
;
4122 xsetpointermotion(int set
)
4124 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
4125 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
4129 xseturgency(int add
)
4131 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
4133 MODBIT(h
->flags
, add
, XUrgencyHint
);
4134 XSetWMHints(xw
.dpy
, xw
.win
, h
);
4141 XFocusChangeEvent
*e
= &ev
->xfocus
;
4143 if (e
->mode
== NotifyGrab
)
4146 if (ev
->type
== FocusIn
) {
4147 XSetICFocus(xw
.xic
);
4148 xw
.state
|= WIN_FOCUSED
;
4150 if (IS_SET(MODE_FOCUS
))
4151 ttywrite("\033[I", 3);
4153 XUnsetICFocus(xw
.xic
);
4154 xw
.state
&= ~WIN_FOCUSED
;
4155 if (IS_SET(MODE_FOCUS
))
4156 ttywrite("\033[O", 3);
4161 match(uint mask
, uint state
)
4163 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
4167 numlock(const Arg
*dummy
)
4173 kmap(KeySym k
, uint state
)
4178 /* Check for mapped keys out of X11 function keys. */
4179 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
4180 if (mappedkeys
[i
] == k
)
4183 if (i
== LEN(mappedkeys
)) {
4184 if ((k
& 0xFFFF) < 0xFD00)
4188 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
4192 if (!match(kp
->mask
, state
))
4195 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
4197 if (term
.numlock
&& kp
->appkey
== 2)
4200 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
4203 if (IS_SET(MODE_CRLF
) ? kp
->crlf
< 0 : kp
->crlf
> 0)
4215 XKeyEvent
*e
= &ev
->xkey
;
4217 char buf
[32], *customkey
;
4223 if (IS_SET(MODE_KBDLOCK
))
4226 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
4228 for (bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
4229 if (ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
4230 bp
->func(&(bp
->arg
));
4235 /* 2. custom keys from config.h */
4236 if ((customkey
= kmap(ksym
, e
->state
))) {
4237 ttysend(customkey
, strlen(customkey
));
4241 /* 3. composed string from input method */
4244 if (len
== 1 && e
->state
& Mod1Mask
) {
4245 if (IS_SET(MODE_8BIT
)) {
4248 len
= utf8encode(c
, buf
);
4265 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
4267 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
4268 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
4269 xw
.state
|= WIN_FOCUSED
;
4271 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
4272 xw
.state
&= ~WIN_FOCUSED
;
4274 } else if (e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
4275 /* Send SIGHUP to shell */
4282 cresize(int width
, int height
)
4291 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
4292 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
4301 if (e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
4304 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
4312 int w
= xw
.w
, h
= xw
.h
;
4314 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
4315 struct timespec drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
4318 /* Waiting for window mapping */
4320 XNextEvent(xw
.dpy
, &ev
);
4322 * This XFilterEvent call is required because of XOpenIM. It
4323 * does filter out the key event and some client message for
4324 * the input method too.
4326 if (XFilterEvent(&ev
, None
))
4328 if (ev
.type
== ConfigureNotify
) {
4329 w
= ev
.xconfigure
.width
;
4330 h
= ev
.xconfigure
.height
;
4332 } while (ev
.type
!= MapNotify
);
4338 clock_gettime(CLOCK_MONOTONIC
, &last
);
4341 for (xev
= actionfps
;;) {
4343 FD_SET(cmdfd
, &rfd
);
4346 if (pselect(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
, NULL
) < 0) {
4349 die("select failed: %s\n", strerror(errno
));
4351 if (FD_ISSET(cmdfd
, &rfd
)) {
4354 blinkset
= tattrset(ATTR_BLINK
);
4356 MODBIT(term
.mode
, 0, MODE_BLINK
);
4360 if (FD_ISSET(xfd
, &rfd
))
4363 clock_gettime(CLOCK_MONOTONIC
, &now
);
4364 drawtimeout
.tv_sec
= 0;
4365 drawtimeout
.tv_nsec
= (1000 * 1E6
)/ xfps
;
4369 if (blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
4370 tsetdirtattr(ATTR_BLINK
);
4371 term
.mode
^= MODE_BLINK
;
4375 deltatime
= TIMEDIFF(now
, last
);
4376 if (deltatime
> 1000 / (xev
? xfps
: actionfps
)) {
4382 while (XPending(xw
.dpy
)) {
4383 XNextEvent(xw
.dpy
, &ev
);
4384 if (XFilterEvent(&ev
, None
))
4386 if (handler
[ev
.type
])
4387 (handler
[ev
.type
])(&ev
);
4393 if (xev
&& !FD_ISSET(xfd
, &rfd
))
4395 if (!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
4397 if (TIMEDIFF(now
, lastblink
) \
4399 drawtimeout
.tv_nsec
= 1000;
4401 drawtimeout
.tv_nsec
= (1E6
* \
4406 drawtimeout
.tv_sec
= \
4407 drawtimeout
.tv_nsec
/ 1E9
;
4408 drawtimeout
.tv_nsec
%= (long)1E9
;
4420 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
4421 " [-n name] [-o file]\n"
4422 " [-T title] [-t title] [-w windowid]"
4423 " [[-e] command [args ...]]\n"
4424 " %s [-aiv] [-c class] [-f font] [-g geometry]"
4425 " [-n name] [-o file]\n"
4426 " [-T title] [-t title] [-w windowid] -l line"
4427 " [stty_args ...]\n", argv0
, argv0
);
4431 main(int argc
, char *argv
[])
4433 uint cols
= 80, rows
= 24;
4437 xw
.cursor
= cursorshape
;
4444 opt_class
= EARGF(usage());
4451 opt_font
= EARGF(usage());
4454 xw
.gm
= XParseGeometry(EARGF(usage()),
4455 &xw
.l
, &xw
.t
, &cols
, &rows
);
4461 opt_io
= EARGF(usage());
4464 opt_line
= EARGF(usage());
4467 opt_name
= EARGF(usage());
4471 opt_title
= EARGF(usage());
4474 opt_embed
= EARGF(usage());
4477 die("%s " VERSION
" (c) 2010-2016 st engineers\n", argv0
);
4485 /* eat all remaining arguments */
4487 if (!opt_title
&& !opt_line
)
4488 opt_title
= basename(xstrdup(argv
[0]));
4490 setlocale(LC_CTYPE
, "");
4491 XSetLocaleModifiers("");
4492 tnew(MAX(cols
, 1), MAX(rows
, 1));