1 /* See LICENSE for licence details. */
2 #define VERSION "0.3.0"
7 #define _XOPEN_SOURCE 600
23 #include <sys/ioctl.h>
24 #include <sys/select.h>
27 #include <sys/types.h>
31 #include <X11/Xatom.h>
33 #include <X11/Xutil.h>
34 #include <X11/cursorfont.h>
35 #include <X11/keysym.h>
40 //#define DUMP_KEYSYMS
44 //#define DUMP_PROG_OUTPUT
45 //#define DUMP_PROG_INPUT
47 //#define DUMP_IO_READ
48 //#define DUMP_IO_WRITE
50 #if defined(DUMP_IO_READ) || defined(DUMP_IO_WRITE)
55 # define DUMP_KEYPAD_SWITCH(strflag,strstate) do { fprintf(stderr, "KEYPAD %s (%s)\n", (strstate), (strflag)); } while (0)
57 # define DUMP_KEYPAD_SWITCH(strflag,strstate) ((void)(sizeof(strflag)+sizeof(strstate)))
61 ////////////////////////////////////////////////////////////////////////////////
63 "sterm " VERSION " (c) 2010-2012 st engineers and Ketmar // Vampire Avalon\n" \
64 "usage: sterm [-t title] [-c class] [-w windowid] [-T termname] [-C config_file] [-v] [-e command...]\n"
67 ////////////////////////////////////////////////////////////////////////////////
68 #define FONT "-*-liberation mono-medium-*-*-*-17-*-*-*-*-*-koi8-u"
69 #define BOLDFONT "-*-liberation mono-medium-*-*-*-17-*-*-*-*-*-koi8-u"
70 #define FONTTAB "-*-helvetica-*-*-*-*-12-*-*-*-*-*-*-*"
72 #define FONT "-*-terminus-bold-*-*-*-20-*-*-*-*-*-koi8-u"
73 #define BOLDFONT "-*-terminus-bold-*-*-*-20-*-*-*-*-*-koi8-u"
77 /* Space in pixels around the terminal buffer */
81 /* Default shell to use if SHELL is not set in the env */
82 #define SHELL "/bin/sh"
85 /* Terminal colors (16 first used in escape sequence) */
86 static const char *defcolornames
[] = {
107 /* 8 normal colors */
116 /* 8 bright colors */
129 /* more colors can be added after 255 to use with DefaultXX */
130 static const char *defextcolornames
[] = {
133 /* root terminal fg and bg */
136 /* bold and underline */
142 /* Default colors (colorname index) foreground, background, cursor, unfocused cursor */
143 #define DEFAULT_FG (7)
144 #define DEFAULT_BG (0)
145 #define DEFAULT_CS (256)
146 #define DEFAULT_UCS (257)
148 #define TNAME "xterm"
150 /* double-click timeout (in milliseconds) between clicks for selection */
151 #define DOUBLECLICK_TIMEOUT (300)
152 #define TRIPLECLICK_TIMEOUT (2*DOUBLECLICK_TIMEOUT)
153 #define SELECT_TIMEOUT 20 /* 20 ms */
154 #define DRAW_TIMEOUT 18 /* 18 ms */
159 ////////////////////////////////////////////////////////////////////////////////
160 #define MAX_COLOR (511)
162 /* XEMBED messages */
163 #define XEMBED_FOCUS_IN (4)
164 #define XEMBED_FOCUS_OUT (5)
167 /* Arbitrary sizes */
168 #define ESC_TITLE_SIZ (256)
169 #define ESC_BUF_SIZ (256)
170 #define ESC_ARG_SIZ (16)
171 #define DRAW_BUF_SIZ (2048)
173 #define OBUFSIZ (256)
174 #define WBUFSIZ (256)
177 /* masks for key translation */
178 #define XK_NO_MOD (UINT_MAX)
179 #define XK_ANY_MOD (0)
182 /* misc utility macros */
183 #define SERRNO strerror(errno)
184 #define MIN(a, b) ((a) < (b) ? (a) : (b))
185 #define MAX(a, b) ((a) < (b) ? (b) : (a))
186 #define LEN(a) (sizeof(a)/sizeof(a[0]))
187 #define DEFAULT(a, b) (a) = ((a) ? (a) : (b))
188 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
189 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
190 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
191 #define IS_SET(flag) (term->mode&(flag))
192 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000+(t1.tv_usec-t2.tv_usec)/1000)
193 #define X2COL(x) (((x)-opt_border)/xw.cw)
194 #define Y2ROW(y) (((y)-opt_border)/xw.ch-(term!=NULL?term->topline:0))
195 #define IS_GFX(mode) (((mode)&(ATTR_GFX|ATTR_G1)) == ATTR_GFX || ((mode)&(ATTR_GFX1|ATTR_G1)) == (ATTR_GFX1|ATTR_G1))
198 ////////////////////////////////////////////////////////////////////////////////
199 enum glyph_attribute
{
202 ATTR_UNDERLINE
= 0x02,
211 enum cursor_movement
{
227 GLYPH_SET
= 0x01, /* for selection only */
235 MODE_APPKEYPAD
= 0x04,
236 MODE_ALTSCREEN
= 0x08,
238 MODE_MOUSEBTN
= 0x20,
239 MODE_MOUSEMOTION
= 0x40,
240 MODE_MOUSE
= 0x20|0x40,
249 ESC_ALTCHARSET
= 0x10,
263 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
266 ////////////////////////////////////////////////////////////////////////////////
267 typedef unsigned char uchar
;
268 typedef unsigned int uint
;
269 typedef unsigned long ulong
;
270 typedef unsigned short ushort
;
273 typedef struct __attribute__((packed
)) {
274 char c
[UTF_SIZ
]; /* character code */
275 uchar mode
; /* attribute flags */
276 ushort fg
; /* foreground */
277 ushort bg
; /* background */
278 uchar state
; /* state flags */
285 Glyph attr
; /* current char attributes */
292 /* CSI Escape sequence structs */
293 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
295 char buf
[ESC_BUF_SIZ
]; /* raw string */
296 int len
; /* raw string length */
298 int arg
[ESC_ARG_SIZ
];
299 int narg
; /* nb of args */
304 /* Purely graphic info */
315 int w
; /* window width */
316 int h
; /* window height */
317 int bufw
; /* pixmap width */
318 int bufh
; /* pixmap height */
319 int ch
; /* char height */
320 int cw
; /* char width */
321 char state
; /* focus, redraw, visible */
323 int tch
; /* tab text char height */
326 //struct timeval lastdraw;
330 /* TODO: use better name for vars... */
335 struct { int x
, y
; } b
, e
;
338 struct timeval tclick1
;
339 struct timeval tclick2
;
343 /* Drawing Context */
345 ulong
*col
; //[LEN(colorname) < 256 ? 256 : LEN(colorname)];
358 /* Internal representation of the screen */
363 int row
; /* nb row */
364 int col
; /* nb col */
365 int topline
; /* top line for drawing (0: no history; 1: show one history line; etc) */
366 int linecount
; /* full, with history */
367 int maxhistory
;/* max history lines; 0: none; <0: infinite */
368 Line
*line
; /* screen */
369 Line
*alt
; /* alternate screen */
370 char *dirty
; /* dirtyness of lines */
371 TCursor c
; /* cursor */
372 int top
; /* top scroll limit */
373 int bot
; /* bottom scroll limit */
374 int mode
; /* terminal mode flags */
375 int esc
; /* escape state flags */
377 TCursor csaved
; /* saved cursor info */
378 // old cursor position
382 char title
[ESC_TITLE_SIZ
+1];
390 #ifdef DUMP_PROG_OUTPUT
398 char drawbuf
[DRAW_BUF_SIZ
];
423 ////////////////////////////////////////////////////////////////////////////////
425 static uchar
*unimap
= NULL
; // 0 or 65535 items; 127: special; high bit set: use alt(gfx) mode; 0: empty
427 static char **opt_cmd
= NULL
;
428 static char *opt_title
= NULL
;
429 static char *opt_embed
= NULL
;
430 static char *opt_class
= NULL
;
431 static char *opt_term
= NULL
;
432 static char *opt_fontnorm
= NULL
;
433 static char *opt_fontbold
= NULL
;
434 static char *opt_fonttab
= NULL
;
435 static char *opt_shell
= NULL
;
436 static char *opt_colornames
[512];
437 static int opt_term_locked
= 0;
438 static int opt_doubleclick_timeout
= DOUBLECLICK_TIMEOUT
;
439 static int opt_tripleclick_timeout
= TRIPLECLICK_TIMEOUT
;
440 static int opt_tabsize
= TAB
;
441 static int opt_border
= BORDER
;
442 static int defaultFG
= DEFAULT_FG
;
443 static int defaultBG
= DEFAULT_BG
;
444 static int defaultCursorFG
= 0;
445 static int defaultCursorBG
= DEFAULT_CS
;
446 static int defaultCursorInactiveFG
= 0;
447 static int defaultCursorInactiveBG
= DEFAULT_UCS
;
448 static int defaultBoldFG
= -1;
449 static int defaultUnderlineFG
= -1;
450 static int normalTabFG
= 258;
451 static int normalTabBG
= 257;
452 static int activeTabFG
= 258;
453 static int activeTabBG
= 0;
454 static int opt_maxhistory
= 512;
455 static int opt_ptrblank
= 2000; // delay; 0: never
456 static int ptrBlanked
= 0;
457 static uint ptrLastMove
= 0;
459 static Term
**term_array
= NULL
;
460 static int term_count
= 0;
461 static int term_array_size
= 0;
462 static Term
*term
; // current terminal
463 static int termidx
; // current terminal index; DON'T RELAY ON IT!
464 static int updateTabBar
;
469 static Atom XA_VT_SELECTION
;
470 static Atom XA_CLIPBOARD
;
472 static Atom XA_TARGETS
;
473 static Atom XA_NETWM_NAME
;
476 ////////////////////////////////////////////////////////////////////////////////
483 static KeyTransDef
*keytrans
= NULL
;
484 static int keytrans_size
= 0;
485 static int keytrans_used
= 0;
496 static KeyInfoDef
*keybinds
= NULL
;
497 static int keybinds_size
= 0;
498 static int keybinds_used
= 0;
500 static KeyInfoDef
*keymap
= NULL
;
501 static int keymap_size
= 0;
502 static int keymap_used
= 0;
505 ////////////////////////////////////////////////////////////////////////////////
506 static void executeCommands (const char *str
);
508 static void ttyresize (void);
509 static void tputc (const char *c
); // `c` is utf-8
510 static void ttywrite (const char *s
, size_t n
);
511 static void tsetdirt (int top
, int bot
);
512 static void tfulldirt (void);
514 static void xseturgency (int add
);
515 static void xfixsel (void);
516 static void xblankPointer (void);
517 static void xunblankPointer (void);
519 static void draw (int forced
);
522 static inline void ttywritestr (const char *s
) { if (s
!= NULL
&& s
[0]) ttywrite(s
, strlen(s
)); }
525 ////////////////////////////////////////////////////////////////////////////////
527 static void trimstr (char *s
) {
530 while (*s
&& isspace(*s
)) ++s
;
531 for (e
= s
+strlen(s
); e
> s
; --e
) if (!isspace(e
[-1])) break;
532 if (e
<= s
) *s
= 0; else *e
= 0;
536 // parse the argument list
537 // return error message or NULL
540 // 's': string (char *)
541 // 'i': integer (int *)
542 // 'b': boolean (int *)
543 // '|': optional arguments follows
544 // '.': stop parsing, ignore rest
545 // 'R': stop parsing, set rest ptr (char *)
546 // string modifiers (also for 'R'):
547 // '!' -- don't allow empty strings
548 // '-' -- trim spaces
553 // WARNING! `line` will be modified!
554 // WARNING! string pointers will point INSIDE `line`, so don't discard it!
556 const char *iniParseArguments (char *line
, const char *fmt
, ...) {
560 if (line
== NULL
) return "alas";
564 char spec
= *fmt
++, *args
;
566 if (spec
== '|') { inOptional
= 1; continue; }
567 if (spec
== '.') { va_end(ap
); return NULL
; }
569 while (*line
&& isspace(*line
)) ++line
;
570 if (*line
== '#') *line
= 0;
573 char **p
= va_arg(ap
, char **);
577 if (*fmt
== '!') { ++fmt
; noempty
= 1; }
578 else if (*fmt
== '-') { ++fmt
; trimstr(line
); }
581 if (noempty
&& !line
[0]) return "invalid empty arg";
582 if (p
!= NULL
) *p
= line
;
588 // end of line, stop right here
590 if (!inOptional
) return "out of args";
596 char *dest
= args
, qch
= '#';
599 if (line
[0] == '"' || line
[0] == '\'') qch
= *line
++;
601 while (*line
&& *line
!= qch
) {
602 if (qch
== '#' && isspace(*line
)) break;
606 case 'n': *dest
++ = '\n'; ++line
; break;
607 case 'r': *dest
++ = '\r'; ++line
; break;
608 case 't': *dest
++ = '\t'; ++line
; break;
609 case 'a': *dest
++ = '\a'; ++line
; break;
610 case 'e': *dest
++ = '\x1b'; ++line
; break; // esc
611 case 's': *dest
++ = ' '; ++line
; break;
614 if (!isxdigit(*line
)) { va_end(ap
); return "invalid hex escape"; }
615 n
= toupper(*line
)-'0'; if (n
> 9) n
-= 7;
617 if (isxdigit(*line
)) {
618 int b
= toupper(*line
)-'0'; if (b
> 9) b
-= 7;
627 for (int f
= 0; f
< 4; ++f
) {
628 if (*line
< '0' || *line
> '7') break;
629 n
= (n
*8)+(line
[0]-'0');
630 if (n
> 255) { va_end(ap
); return "invalid oct escape"; }
633 if (n
== 0) { va_end(ap
); return "invalid oct escape"; }
636 case '1'...'9': // decimal
638 for (int f
= 0; f
< 3; ++f
) {
639 if (*line
< '0' || *line
> '9') break;
640 n
= (n
*8)+(line
[0]-'0');
641 if (n
> 255) { va_end(ap
); return "invalid dec escape"; }
644 if (n
== 0) { va_end(ap
); return "invalid oct escape"; }
656 if (*line
!= qch
) return "unfinished string";
658 } else if (*line
!= '#') ++line
;
661 // now process and convert argument
665 case 's': { /* string */
666 int noempty
= 0, trim
= 0;
670 if (*fmt
== '!') { noempty
= 1; ++fmt
; }
671 else if (*fmt
== '-') { trim
= 1; ++fmt
; }
675 if (trim
) trimstr(args
);
677 if (noempty
&& !args
[0]) { va_end(ap
); return "invalid empty string"; }
678 p
= va_arg(ap
, char **);
679 if (p
!= NULL
) *p
= args
;
684 return "invalid integer";
686 int *p
= va_arg(ap
, int *);
691 n
= strtol(args
, &eptr
, 0);
692 if (*eptr
) { va_end(ap
); return "invalid integer"; }
696 int minmax
[2], haveminmax
[2];
698 haveminmax
[0] = haveminmax
[1] = 0;
699 minmax
[0] = minmax
[1] = 0;
701 for (int f
= 0; f
< 2; ++f
) {
702 if (isdigit(*fmt
) || *fmt
== '-' || *fmt
== '+') {
705 if (*fmt
== '-') neg
= 1;
706 if (!isdigit(*fmt
)) {
708 if (!isdigit(*fmt
)) { va_end(ap
); return "invalid integer bounds"; }
710 while (isdigit(*fmt
)) {
711 minmax
[f
] = minmax
[f
]*10+(fmt
[0]-'0');
714 if (neg
) minmax
[f
] = -minmax
[f
];
715 //fprintf(stderr, "got: %d\n", minmax[f]);
718 if (f
== 1) { va_end(ap
); return "invalid integer bounds: extra comma"; }
719 // do nothing, we are happy
721 } else if (*fmt
== '}') {
724 } else { va_end(ap
); return "invalid integer bounds"; }
726 if (*fmt
!= '}') { va_end(ap
); return "invalid integer bounds"; }
729 //fprintf(stderr, "b: (%d,%d) (%d,%d)\n", haveminmax[0], minmax[0], haveminmax[1], minmax[1]);
730 if ((haveminmax
[0] && n
< minmax
[0]) || (haveminmax
[1] && n
> minmax
[1])) { va_end(ap
); return "integer out of bounds"; }
736 case 'b': { /* bool */
737 int *p
= va_arg(ap
, int *);
740 if (!args
[0]) { va_end(ap
); return "invalid boolean"; }
741 if (strcasecmp(args
, "true") == 0 || strcasecmp(args
, "on") == 0 ||
742 strcasecmp(args
, "tan") == 0 || strcasecmp(args
, "1") == 0) {
744 } else if (strcasecmp(args
, "false") == 0 || strcasecmp(args
, "off") == 0 ||
745 strcasecmp(args
, "ona") == 0 || strcasecmp(args
, "0") == 0) {
750 return "invalid boolean";
755 return "invalid format specifier";
759 while (*line
&& isspace(*line
)) ++line
;
760 if (!line
[0] || line
[0] == '#') return NULL
;
765 ////////////////////////////////////////////////////////////////////////////////
767 static int utf8decode (const char *s
, long *u
) {
773 if (~c
& B7
) { /* 0xxxxxxx */
776 } else if ((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
777 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
779 } else if ((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
780 *u
= c
&(B3
|B2
|B1
|B0
);
782 } else if ((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
789 for (int f
= n
; f
> 0; --f
, ++rtn
, ++s
) {
791 if ((c
& (B7
|B6
)) != B7
) goto invalid
; /* 10xxxxxx */
793 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
795 if ((n
== 1 && *u
< 0x80) ||
796 (n
== 2 && *u
< 0x800) ||
797 (n
== 3 && *u
< 0x10000) ||
798 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
808 static int utf8encode (const long *u
, char *s
) {
816 *sp
= uc
; /* 0xxxxxxx */
818 } else if (*u
< 0x800) {
819 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
821 } else if (uc
< 0x10000) {
822 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
824 } else if (uc
<= 0x10FFFF) {
825 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
831 for (int f
= n
; f
> 0; --f
, ++sp
) *sp
= ((uc
>> 6*(f
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
842 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
843 UTF-8 otherwise return 0 */
844 static int isfullutf8 (const char *s
, int b
) {
851 if ((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1) return 0;
852 if ((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) && (b
== 1 || (b
== 2 && (*c2
&(B7
|B6
)) == B7
))) return 0;
853 if ((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) && (b
== 1 || (b
== 2 && (*c2
&(B7
|B6
)) == B7
) || (b
== 3 && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
))) return 0;
858 static int utf8size (const char *s
) {
862 if ((c
&(B7
|B6
|B5
)) == (B7
|B6
)) return 2;
863 if ((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) return 3;
868 ////////////////////////////////////////////////////////////////////////////////
870 static char *SPrintfVA (const char *fmt
, va_list vaorig
) {
875 if (buf
== NULL
) { fprintf(stderr
, "\nFATAL: out of memory!\n"); abort(); }
881 olen
= vsnprintf(buf
, len
, fmt
, va
);
883 if (olen
>= 0 && olen
< len
) return buf
;
884 if (olen
< 0) olen
= len
*2-1;
885 nb
= realloc(buf
, olen
+1);
886 if (nb
== NULL
) { fprintf(stderr
, "\nFATAL: out of memory!\n"); abort(); }
893 static __attribute__((format(printf
,1,2))) char *SPrintf (const char *fmt
, ...) {
898 buf
= SPrintfVA(fmt
, va
);
904 static __attribute__((noreturn
)) __attribute__((format(printf
,1,2))) void die (const char *errstr
, ...) {
907 fprintf(stderr
, "FATAL: ");
908 va_start(ap
, errstr
);
909 vfprintf(stderr
, errstr
, ap
);
911 fprintf(stderr
, "\n");
916 ////////////////////////////////////////////////////////////////////////////////
917 static void fixWindowTitle (const Term
*t
) {
918 const char *title
= (t
!= NULL
) ? t
->title
: NULL
;
920 if (title
== NULL
|| !title
[0]) {
922 if (title
== NULL
) title
= "";
924 XStoreName(xw
.dpy
, xw
.win
, title
);
925 XChangeProperty(xw
.dpy
, xw
.win
, XA_NETWM_NAME
, XA_UTF8
, 8, PropModeReplace
, (const unsigned char *)title
, strlen(title
));
929 static void switchToTerm (int idx
, int redraw
) {
930 if (idx
>= 0 && idx
< term_count
&& term_array
[idx
] != NULL
&& term_array
[idx
] != term
) {
932 term
= term_array
[termidx
];
935 fixWindowTitle(term
);
939 //fprintf(stderr, "term #%d\n", termidx);
944 static Term
*termalloc (void) {
947 if (term_count
>= term_array_size
) {
948 int newsz
= (term_count
==0) ? 1 : term_array_size
+64;
949 Term
**n
= realloc(term_array
, sizeof(Term
*)*newsz
);
951 if (n
== NULL
&& term_count
== 0) die("out of memory!");
953 term_array_size
= newsz
;
955 if ((t
= calloc(1, sizeof(Term
))) == NULL
) return NULL
;
956 t
->wrbufsize
= WBUFSIZ
;
957 t
->deffg
= defaultFG
;
958 t
->defbg
= defaultBG
;
960 term_array
[term_count
++] = t
;
965 // newer delete last terminal!
966 static void termfree (int idx
) {
967 if (idx
>= 0 && idx
< term_count
&& term_array
[idx
] != NULL
) {
968 Term
*t
= term_array
[idx
];
971 kill(t
->pid
, SIGKILL
);
978 if (idx
== termidx
) {
979 if (term_count
> 1) {
980 switchToTerm((idx
> 0) ? idx
-1 : 1, 0);
986 for (int y
= 0; y
< t
->row
; ++y
) free(t
->alt
[y
]);
987 for (int y
= 0; y
< t
->linecount
; ++y
) {
988 //fprintf(stderr, "y=%d\n", y);
994 if (t
->execcmd
!= NULL
) free(t
->execcmd
);
997 // not current, and current at the right
1000 for (int f
= idx
+1; f
< term_count
; ++f
) term_array
[f
-1] = term_array
[f
];
1002 XFreePixmap(xw
.dpy
, t
->picbuf
);
1010 static void termcleanup (void) {
1013 while (f
< term_count
) {
1014 if (term_array
[f
]->dead
) {
1023 //FIXME: is it safe to assume that signal interrupted main program?
1024 static void sigchld (int a
) {
1025 //if (waitpid(term->pid, &stat, 0) < 0) die("waiting for pid %hd failed: %s", term->pid, SERRNO);
1028 pid_t res
= waitpid(-1, &stat
, WNOHANG
);
1030 if (res
== (pid_t
)-1 || res
== 0) break;
1031 //if (WIFEXITED(stat)) exit(WEXITSTATUS(stat));
1032 //exit(EXIT_FAILURE);
1033 for (int f
= 0; f
< term_count
; ++f
) {
1034 if (term_array
[f
]->pid
== res
) {
1035 // this terminal should die
1036 //if (term_count == 1) exit(0);
1037 //close(term_array[f]->cmdfd);
1038 //term_array[f]->cmdfd = -1;
1039 term_array
[f
]->dead
= 1;
1040 term_array
[f
]->pid
= 0;
1046 signal(SIGCHLD
, sigchld
);
1050 ////////////////////////////////////////////////////////////////////////////////
1051 static void keytrans_reset (void) {
1052 if (keytrans
) free(keytrans
);
1059 static void keytrans_add (const char *src
, const char *dst
) {
1060 KeySym kssrc
= XStringToKeysym(src
);
1061 KeySym ksdst
= XStringToKeysym(dst
);
1063 if (kssrc
== NoSymbol
) die("invalid keysym: '%s'", src
);
1064 if (ksdst
== NoSymbol
) die("invalid keysym: '%s'", dst
);
1065 if (kssrc
== ksdst
) return; // idiot
1067 for (int f
= 0; f
< keytrans_used
; ++f
) {
1068 if (keytrans
[f
].src
== kssrc
) {
1070 keytrans
[f
].dst
= ksdst
;
1075 if (keytrans_used
>= keytrans_size
) {
1076 int newsize
= keytrans_size
+64;
1077 KeyTransDef
*n
= realloc(keytrans
, sizeof(KeyTransDef
)*newsize
);
1079 if (n
== NULL
) die("out of memory");
1080 keytrans_size
= newsize
;
1083 keytrans
[keytrans_used
].src
= kssrc
;
1084 keytrans
[keytrans_used
].dst
= ksdst
;
1089 ////////////////////////////////////////////////////////////////////////////////
1090 static void parsekeyname (const char *str
, KeySym
*ks
, uint
*mask
, int *kp
) {
1091 char *s
= alloca(strlen(str
)+1);
1093 if (s
== NULL
) die("out of memory");
1104 while (*s
&& isspace(*s
)) ++s
;
1105 for (e
= s
; *e
&& !isspace(*e
) && *e
!= '+'; ++e
) ;
1108 if (strcasecmp(s
, "alt") == 0) mm
= Mod1Mask
;
1109 else if (strcasecmp(s
, "win") == 0) mm
= Mod4Mask
;
1110 else if (strcasecmp(s
, "ctrl") == 0) mm
= ControlMask
;
1111 else if (strcasecmp(s
, "shift") == 0) mm
= ShiftMask
;
1112 else if (strcasecmp(s
, "any") == 0) mm
= XK_NO_MOD
; //!
1113 else if (strcasecmp(s
, "kpad") == 0) *kp
= 1;
1116 if ((*ks
= XStringToKeysym(s
)) == NoSymbol
) break;
1117 //fprintf(stderr, "[%s]\n", s);
1122 while (*s
&& isspace(*s
)) ++s
;
1124 if (*s
!= '+') { *ks
= NoSymbol
; break; }
1127 if (mm
== XK_NO_MOD
) *mask
= XK_ANY_MOD
;
1128 else if (*mask
== XK_NO_MOD
) *mask
= mm
;
1129 else if (*mask
!= XK_ANY_MOD
) *mask
|= mm
;
1132 if (*s
) { *ks
= NoSymbol
; break; }
1135 if (*ks
== NoSymbol
) die("invalid key name: '%s'", str
);
1136 //fprintf(stderr, "mask=0x%08x, kp=%d\n", *mask, *kp);
1140 ////////////////////////////////////////////////////////////////////////////////
1141 static void keybinds_reset (void) {
1142 if (keybinds
) free(keybinds
);
1149 static void keybind_add (const char *key
, const char *act
) {
1154 parsekeyname(key
, &ks
, &mask
, &kp
);
1156 for (int f
= 0; f
< keybinds_used
; ++f
) {
1157 if (keybinds
[f
].key
== ks
&& keybinds
[f
].mask
== mask
) {
1158 // replace or remove
1159 free(keybinds
[f
].str
);
1160 if (act
== NULL
|| !act
[0]) {
1162 for (int c
= f
+1; c
< keybinds_used
; ++c
) keybinds
[c
-1] = keybinds
[c
];
1165 if ((keybinds
[f
].str
= strdup(act
)) == NULL
) die("out of memory");
1171 if (keybinds_used
>= keybinds_size
) {
1172 int newsize
= keybinds_size
+64;
1173 KeyInfoDef
*n
= realloc(keybinds
, sizeof(KeyInfoDef
)*newsize
);
1175 if (n
== NULL
) die("out of memory");
1176 keybinds_size
= newsize
;
1179 keybinds
[keybinds_used
].key
= ks
;
1180 keybinds
[keybinds_used
].mask
= mask
;
1181 keybinds
[keybinds_used
].kp
= 0;
1182 if ((keybinds
[keybinds_used
].str
= strdup(act
)) == NULL
) die("out of memory");
1187 ////////////////////////////////////////////////////////////////////////////////
1188 static void keymap_reset (void) {
1189 if (keymap
) free(keymap
);
1196 static void keymap_add (const char *key
, const char *act
) {
1201 parsekeyname(key
, &ks
, &mask
, &kp
);
1203 for (int f
= 0; f
< keymap_used
; ++f
) {
1204 if (keymap
[f
].key
== ks
&& keymap
[f
].mask
== mask
&& keymap
[f
].kp
== kp
) {
1205 // replace or remove
1206 free(keymap
[f
].str
);
1209 for (int c
= f
+1; c
< keymap_used
; ++c
) keymap
[c
-1] = keymap
[c
];
1212 if ((keymap
[f
].str
= strdup(act
)) == NULL
) die("out of memory");
1218 if (keymap_used
>= keymap_size
) {
1219 int newsize
= keymap_size
+128;
1220 KeyInfoDef
*n
= realloc(keymap
, sizeof(KeyInfoDef
)*newsize
);
1222 if (n
== NULL
) die("out of memory");
1223 keymap_size
= newsize
;
1226 keymap
[keymap_used
].key
= ks
;
1227 keymap
[keymap_used
].mask
= mask
;
1228 keymap
[keymap_used
].kp
= kp
;
1229 if ((keymap
[keymap_used
].str
= strdup(act
)) == NULL
) die("out of memory");
1234 ////////////////////////////////////////////////////////////////////////////////
1235 // locale conversions
1236 static iconv_t icFromLoc
;
1237 static iconv_t icToLoc
;
1238 static int needConversion
= 0;
1241 static void initLCConversion (void) {
1242 const char *lct
= setlocale(LC_CTYPE
, NULL
);
1246 if (strrchr(lct
, '.')) lct
= strrchr(lct
, '.')+1;
1247 if (strcasecmp(lct
, "utf8") == 0 || strcasecmp(lct
, "utf-8") == 0) return;
1248 //fprintf(stderr, "locale: [%s]\n", lct);
1249 icFromLoc
= iconv_open("UTF-8", lct
);
1250 if (icFromLoc
== (iconv_t
)-1) die("can't initialize locale conversion");
1251 cstr
= SPrintf("%s//TRANSLIT", lct
);
1252 icToLoc
= iconv_open(cstr
, "UTF-8");
1254 if (icToLoc
== (iconv_t
)-1) die("can't initialize locale conversion");
1259 static int loc2utf (char *dest
, const char *src
, int len
) {
1260 if (needConversion
) {
1268 il
= iconv(icFromLoc
, &ibuf
, &il
, &obuf
, &ol
);
1269 if (il
== (size_t)-1) return 0;
1272 if (len
> 0) memmove(dest
, src
, len
);
1278 static int utf2loc (char *dest
, const char *src
, int len
) {
1279 if (needConversion
) {
1287 il
= iconv(icToLoc
, &ibuf
, &il
, &obuf
, &ol
);
1288 if (il
== (size_t)-1) return 0;
1291 if (len
> 0) memmove(dest
, src
, len
);
1297 ////////////////////////////////////////////////////////////////////////////////
1299 static struct timespec mclk_sttime
; // starting time of monotonic clock
1302 static void mclock_init (void) {
1303 clock_gettime(CLOCK_MONOTONIC
/*CLOCK_MONOTONIC_RAW*/, &mclk_sttime
);
1307 static uint
mclock_ticks (void) {
1310 clock_gettime(CLOCK_MONOTONIC
/*CLOCK_MONOTONIC_RAW*/, &tp
);
1311 tp
.tv_sec
-= mclk_sttime
.tv_sec
;
1312 return tp
.tv_sec
*1000+(tp
.tv_nsec
/1000000);
1316 ////////////////////////////////////////////////////////////////////////////////
1318 static void inline markDirty (int lineno
, int flag
) {
1319 if (term
!= NULL
&& lineno
>= 0 && lineno
< term
->row
) {
1320 term
->dirty
[lineno
] |= flag
;
1321 term
->wantRedraw
= 1;
1322 term
->lastDrawTime
= mclock_ticks(); //FIXME: avoid excess redraw?
1327 static void selinit (void) {
1328 memset(&term
->sel
.tclick1
, 0, sizeof(term
->sel
.tclick1
));
1329 memset(&term
->sel
.tclick2
, 0, sizeof(term
->sel
.tclick2
));
1332 term
->sel
.clip
= NULL
;
1333 term
->sel
.xtarget
= XA_UTF8
;
1334 if (term
->sel
.xtarget
== None
) term
->sel
.xtarget
= XA_STRING
;
1338 static inline int selected (int x
, int y
) {
1339 if (term
->sel
.ey
== y
&& term
->sel
.by
== y
) {
1340 int bx
= MIN(term
->sel
.bx
, term
->sel
.ex
);
1341 int ex
= MAX(term
->sel
.bx
, term
->sel
.ex
);
1343 return BETWEEN(x
, bx
, ex
);
1346 ((term
->sel
.b
.y
< y
&& y
< term
->sel
.e
.y
) || (y
== term
->sel
.e
.y
&& x
<= term
->sel
.e
.x
)) ||
1347 (y
== term
->sel
.b
.y
&& x
>= term
->sel
.b
.x
&& (x
<= term
->sel
.e
.x
|| term
->sel
.b
.y
!= term
->sel
.e
.y
));
1351 static void getbuttoninfo (XEvent
*e
, int *b
, int *x
, int *y
) {
1352 if (b
) *b
= e
->xbutton
.button
;
1353 *x
= X2COL(e
->xbutton
.x
);
1354 *y
= Y2ROW(e
->xbutton
.y
);
1355 term
->sel
.b
.x
= term
->sel
.by
< term
->sel
.ey
? term
->sel
.bx
: term
->sel
.ex
;
1356 term
->sel
.b
.y
= MIN(term
->sel
.by
, term
->sel
.ey
);
1357 term
->sel
.e
.x
= term
->sel
.by
< term
->sel
.ey
? term
->sel
.ex
: term
->sel
.bx
;
1358 term
->sel
.e
.y
= MAX(term
->sel
.by
, term
->sel
.ey
);
1362 static void mousereport (XEvent
*e
) {
1363 int x
= X2COL(e
->xbutton
.x
);
1364 int y
= Y2ROW(e
->xbutton
.y
);
1365 int button
= e
->xbutton
.button
;
1366 int state
= e
->xbutton
.state
;
1367 //char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 };
1370 if (term
== NULL
) return;
1371 if (x
< 0 || x
>= term
->col
|| y
< 0 || y
>= term
->row
) return;
1372 sprintf(buf
, "\x1b[M%c%c%c", 0, 32+x
+1, 32+y
+1);
1374 if (e
->xbutton
.type
== MotionNotify
) {
1375 if (!IS_SET(MODE_MOUSEMOTION
) || (x
== term
->mouseox
&& y
== term
->mouseoy
)) return;
1376 button
= term
->mouseob
+32;
1379 } else if (e
->xbutton
.type
== ButtonRelease
|| button
== AnyButton
) {
1383 if (button
>= 3) button
+= 64-3;
1384 if (e
->xbutton
.type
== ButtonPress
) {
1385 term
->mouseob
= button
;
1390 buf
[3] = 32+button
+(state
& ShiftMask
? 4 : 0)+(state
& Mod4Mask
? 8 : 0)+(state
& ControlMask
? 16 : 0);
1395 static void xfixsel (void) {
1396 if (term
== NULL
) return;
1397 if (term
->sel
.clip
!= NULL
) {
1398 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
1399 XSetSelectionOwner(xw
.dpy
, XA_CLIPBOARD
, xw
.win
, CurrentTime
);
1401 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, None
, CurrentTime
);
1402 XSetSelectionOwner(xw
.dpy
, XA_CLIPBOARD
, None
, CurrentTime
);
1408 static void xsetsel (char *str
) {
1409 /* register the selection for both the clipboard and the primary */
1410 if (term
== NULL
) return;
1411 free(term
->sel
.clip
);
1412 term
->sel
.clip
= str
;
1414 //fprintf(stderr, "[%s]\n", str);
1418 static void selcopy (void) {
1420 int x
, y
, bufsize
, is_selected
= 0;
1422 if (term
== NULL
|| term
->sel
.bx
== -1) {
1425 bufsize
= (term
->col
+1)*(term
->sel
.e
.y
-term
->sel
.b
.y
+1)*UTF_SIZ
;
1426 ptr
= str
= malloc(bufsize
);
1427 /* append every set & selected glyph to the selection */
1428 for (y
= 0; y
< term
->row
; ++y
) {
1430 for (x
= 0; x
< term
->col
; ++x
) {
1431 is_selected
= selected(x
, y
);
1432 if (is_selected
) term
->line
[y
][x
].state
|= GLYPH_DIRTY
;
1433 if ((term
->line
[y
][x
].state
& GLYPH_SET
) && is_selected
) {
1434 int size
= utf8size(term
->line
[y
][x
].c
);
1435 memcpy(ptr
, term
->line
[y
][x
].c
, size
);
1439 /* \n at the end of every selected line except for the last one */
1440 if (is_selected
&& y
< term
->sel
.e
.y
) {
1451 static void selnotify (XEvent
*e
) {
1452 ulong nitems
, ofs
, rem
;
1456 XSelectionEvent
*se
= (XSelectionEvent
*)e
;
1459 if (term
== NULL
) return;
1464 fprintf(stderr, "selnotify!\n");
1466 name = se->selection != None ? XGetAtomName(se->display, se->selection) : NULL;
1467 fprintf(stderr, " selection: [%s]\n", name);
1468 if (name != NULL) XFree(name);
1470 name = se->target != None ? XGetAtomName(se->display, se->target) : NULL;
1471 fprintf(stderr, " target: [%s]\n", name);
1472 if (name != NULL) XFree(name);
1474 name = se->property != None ? XGetAtomName(se->display, se->property) : NULL;
1475 fprintf(stderr, " property: [%s]\n", name);
1476 if (name != NULL) XFree(name);
1480 if (se
->property
!= XA_VT_SELECTION
) return;
1482 fprintf(stderr, "selection:\n");
1483 fprintf(stderr, " primary: %d\n", se->selection == XA_PRIMARY);
1484 fprintf(stderr, " secondary: %d\n", se->selection == XA_SECONDARY);
1485 fprintf(stderr, " clipboard: %d\n", se->selection == XA_CLIPBOARD);
1486 fprintf(stderr, " vtsel: %d\n", se->selection == XA_VT_SELECTION);
1487 fprintf(stderr, "target:\n");
1488 fprintf(stderr, " primary: %d\n", se->target == XA_PRIMARY);
1489 fprintf(stderr, " secondary: %d\n", se->target == XA_SECONDARY);
1490 fprintf(stderr, " clipboard: %d\n", se->target == XA_CLIPBOARD);
1491 fprintf(stderr, " vtsel: %d\n", se->target == XA_VT_SELECTION);
1493 if (se
->target
== XA_UTF8
) {
1495 } else if (se
->target
== XA_STRING
) {
1500 if (!isutf8
) return;
1503 if (XGetWindowProperty(xw
.dpy
, xw
.win
, se
->property
, ofs
, BUFSIZ
/4, False
, AnyPropertyType
, &type
, &format
, &nitems
, &rem
, &data
)) {
1504 fprintf(stderr
, "Clipboard allocation failed\n");
1507 //fprintf(stderr, "nitems=%d; format=%d; rem=%d\n", (int)nitems, format, (int)rem);
1508 ttywrite((const char *)data
, nitems
*format
/8);
1510 /* number of 32-bit chunks returned */
1511 ofs
+= nitems
*format
/32;
1516 static void selpaste (Atom which
) {
1517 //fprintf(stderr, "selpaste %d!\n", which == XA_PRIMARY);
1518 if (term
== NULL
) return;
1519 //XConvertSelection(xw.dpy, XA_PRIMARY, term->sel.xtarget, XA_PRIMARY, xw.win, CurrentTime);
1520 if (XGetSelectionOwner(xw
.dpy
, which
) != None
) {
1521 XConvertSelection(xw
.dpy
, which
, term
->sel
.xtarget
, XA_VT_SELECTION
, xw
.win
, CurrentTime
);
1526 char *name = XGetAtomName(xw.dpy, which);
1528 fprintf(stderr, "no selection owner for [%s]!\n", name);
1532 if (which
== XA_PRIMARY
) selpaste(XA_SECONDARY
);
1533 else if (which
== XA_SECONDARY
) selpaste(XA_CLIPBOARD
);
1537 static void selrequest (XEvent
*e
) {
1538 XSelectionRequestEvent
*xsre
;
1539 XSelectionEvent xev
;
1541 if (term
== NULL
) return;
1542 xsre
= (XSelectionRequestEvent
*)e
;
1543 xev
.type
= SelectionNotify
;
1544 xev
.requestor
= xsre
->requestor
;
1545 xev
.selection
= xsre
->selection
;
1546 xev
.target
= xsre
->target
;
1547 xev
.time
= xsre
->time
;
1549 xev
.property
= None
;
1554 fprintf(stderr, "selrequest!\n");
1556 name = xsre->selection != None ? XGetAtomName(xsre->display, xsre->selection) : NULL;
1557 fprintf(stderr, " selection: [%s]\n", name);
1558 if (name != NULL) XFree(name);
1560 name = xsre->target != None ? XGetAtomName(xsre->display, xsre->target) : NULL;
1561 fprintf(stderr, " target: [%s]\n", name);
1562 if (name != NULL) XFree(name);
1564 name = xsre->property != None ? XGetAtomName(xsre->display, xsre->property) : NULL;
1565 fprintf(stderr, " property: [%s]\n", name);
1566 if (name != NULL) XFree(name);
1569 if (xsre
->target
== XA_TARGETS
) {
1570 /* respond with the supported type */
1571 Atom string
= XA_UTF8
;
1573 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
, XA_ATOM
, 32, PropModeReplace
, (uchar
*)&string
, 1);
1574 xev
.property
= xsre
->property
;
1575 } else if (xsre
->target
== XA_UTF8
&& term
->sel
.clip
!= NULL
) {
1576 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
, XA_UTF8
, 8, PropModeReplace
, (uchar
*)term
->sel
.clip
, strlen(term
->sel
.clip
));
1577 xev
.property
= xsre
->property
;
1578 } else if (xsre
->target
== XA_STRING
&& term
->sel
.clip
!= NULL
) {
1579 char *s
= malloc(strlen(term
->sel
.clip
)*4+8);
1582 int len
= utf2loc(s
, term
->sel
.clip
, strlen(term
->sel
.clip
));
1584 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
, xsre
->target
, 8, PropModeReplace
, (uchar
*)s
, len
);
1585 xev
.property
= xsre
->property
;
1589 /* all done, send a notification to the listener */
1590 if (!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*)&xev
)) fprintf(stderr
, "Error sending SelectionNotify event\n");
1594 static void selclear (XEvent
*e
) {
1595 if (term
== NULL
) return;
1598 term
->sel
.ex
= term
->sel
.bx
= -1;
1599 term
->sel
.ey
= term
->sel
.by
= -1;
1605 static void bpress (XEvent
*e
) {
1607 ptrLastMove
= mclock_ticks();
1609 if (term
== NULL
) return;
1611 if (e
->xbutton
.button
== Button1
&& (e
->xbutton
.state
&ShiftMask
) != 0) {
1612 if (term
->sel
.bx
!= -1) tsetdirt(term
->sel
.b
.y
, term
->sel
.e
.y
);
1614 term
->sel
.ex
= term
->sel
.bx
= X2COL(e
->xbutton
.x
);
1615 term
->sel
.ey
= term
->sel
.by
= Y2ROW(e
->xbutton
.y
);
1619 if (e
->xbutton
.button
== Button3
&& (e
->xbutton
.state
&ShiftMask
) != 0) {
1624 if (IS_SET(MODE_MOUSE
)) mousereport(e
);
1628 static void brelease (XEvent
*e
) {
1632 ptrLastMove
= mclock_ticks();
1634 if (term
== NULL
) return;
1636 if ((e
->xbutton
.state
&ShiftMask
) != 0) reportit
= 0;
1637 if (reportit
&& IS_SET(MODE_MOUSE
)) {
1641 if (e
->xbutton
.button
== Button2
) {
1642 selpaste(XA_PRIMARY
);
1643 } else if (e
->xbutton
.button
== Button1
) {
1645 getbuttoninfo(e
, NULL
, &term
->sel
.ex
, &term
->sel
.ey
);
1646 if (term
->sel
.bx
== term
->sel
.ex
&& term
->sel
.by
== term
->sel
.ey
) {
1649 markDirty(term
->sel
.ey
, 2);
1651 gettimeofday(&now
, NULL
);
1652 if (TIMEDIFF(now
, term
->sel
.tclick2
) <= opt_tripleclick_timeout
) {
1653 /* triple click on the line */
1654 term
->sel
.b
.x
= term
->sel
.bx
= 0;
1655 term
->sel
.e
.x
= term
->sel
.ex
= term
->col
;
1656 term
->sel
.b
.y
= term
->sel
.e
.y
= term
->sel
.ey
;
1658 } else if (TIMEDIFF(now
, term
->sel
.tclick1
) <= opt_doubleclick_timeout
) {
1659 /* double click to select word */
1660 term
->sel
.bx
= term
->sel
.ex
;
1661 while (term
->sel
.bx
> 0 && term
->line
[term
->sel
.ey
][term
->sel
.bx
-1].state
& GLYPH_SET
&& term
->line
[term
->sel
.ey
][term
->sel
.bx
-1].c
[0] != ' ') --term
->sel
.bx
;
1662 term
->sel
.b
.x
= term
->sel
.bx
;
1663 while (term
->sel
.ex
< term
->col
-1 && term
->line
[term
->sel
.ey
][term
->sel
.ex
+1].state
& GLYPH_SET
&& term
->line
[term
->sel
.ey
][term
->sel
.ex
+1].c
[0] != ' ') ++term
->sel
.ex
;
1664 term
->sel
.e
.x
= term
->sel
.ex
;
1665 term
->sel
.b
.y
= term
->sel
.e
.y
= term
->sel
.ey
;
1672 memcpy(&term
->sel
.tclick2
, &term
->sel
.tclick1
, sizeof(struct timeval
));
1673 gettimeofday(&term
->sel
.tclick1
, NULL
);
1678 static void bmotion (XEvent
*e
) {
1680 ptrLastMove
= mclock_ticks();
1682 if (term
== NULL
) return;
1684 if (term
->sel
.mode
) {
1685 int oldey
= term
->sel
.ey
, oldex
= term
->sel
.ex
;
1687 getbuttoninfo(e
, NULL
, &term
->sel
.ex
, &term
->sel
.ey
);
1688 if (oldey
!= term
->sel
.ey
|| oldex
!= term
->sel
.ex
) {
1689 int starty
= MIN(oldey
, term
->sel
.ey
);
1690 int endy
= MAX(oldey
, term
->sel
.ey
);
1692 tsetdirt(starty
, endy
);
1697 if (IS_SET(MODE_MOUSE
)) {
1704 ////////////////////////////////////////////////////////////////////////////////
1706 static inline void setWantRedraw (void) { if (term
!= NULL
) term
->wantRedraw
= 1; }
1710 static void dump (char c) {
1713 fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
1714 if (++col % 10 == 0) fprintf(stderr, "\n");
1719 static __attribute__((noreturn
)) void execsh (const char *str
) {
1723 char *envshell
= getenv("SHELL");
1725 DEFAULT(envshell
, opt_shell
);
1726 setenv("TERM", opt_term
, 1);
1727 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1731 args
= calloc(32768, sizeof(char *));
1732 if (args
== NULL
) exit(EXIT_FAILURE
);
1736 while (*str
&& isspace(*str
)) ++str
;
1740 while (*str
&& !isspace(*str
)) {
1741 if (*str
++ == '\\') {
1746 args
[argc
] = calloc(str
-b
+1, 1);
1747 memcpy(args
[argc
], b
, str
-b
);
1750 FILE *fo = fopen("z.log", "a");
1751 fprintf(fo, "%d: [%s]\n", argc, args[argc]);
1757 if (argc
< 1) exit(EXIT_FAILURE
);
1759 execvp(args
[0], args
);
1764 static int ttynew (Term
*term
) {
1766 struct winsize w
= {term
->row
, term
->col
, 0, 0};
1767 static int signalset
= 0;
1769 if (openpty(&m
, &s
, NULL
, NULL
, &w
) < 0) die("openpty failed: %s", SERRNO
);
1773 switch (term
->pid
= fork()) {
1774 case -1: /* error */
1775 fprintf(stderr
, "fork failed");
1778 setsid(); /* create a new process group */
1779 dup2(s
, STDIN_FILENO
);
1780 dup2(s
, STDOUT_FILENO
);
1781 dup2(s
, STDERR_FILENO
);
1782 if (ioctl(s
, TIOCSCTTY
, NULL
) < 0) die("ioctl TIOCSCTTY failed: %s", SERRNO
);
1785 execsh(term
->execcmd
);
1787 default: /* master */
1792 if (!signalset
) { signalset
= 1; signal(SIGCHLD
, sigchld
); }
1799 ////////////////////////////////////////////////////////////////////////////////
1801 static int ttycanread (void) {
1804 struct timeval timeout
= {0};
1806 if (term
->dead
|| term
->cmdfd
< 0) return 0;
1808 FD_SET(term
->cmdfd
, &rfd
);
1809 if (select(term
->cmdfd
+1, &rfd
, NULL
, NULL
, &timeout
) < 0) {
1810 if (errno
== EINTR
) continue;
1811 die("select failed: %s", SERRNO
);
1813 if (FD_ISSET(term
->cmdfd
, &rfd
)) return 1;
1820 static int ttycanwrite (void) {
1823 struct timeval timeout
= {0};
1825 if (term
->dead
|| term
->cmdfd
< 0) return 0;
1827 FD_SET(term
->cmdfd
, &wfd
);
1828 if (select(term
->cmdfd
+1, NULL
, &wfd
, NULL
, &timeout
) < 0) {
1829 if (errno
== EINTR
) continue;
1830 die("select failed: %s", SERRNO
);
1832 if (FD_ISSET(term
->cmdfd
, &wfd
)) return 1;
1840 static void wrstr (const char *s
, int len
) {
1841 if (s
== NULL
) return;
1843 unsigned char c
= (unsigned char)(*s
++);
1845 if (c
< 32) fprintf(stderr
, "{%u}", c
); else fwrite(&c
, 1, 1, stderr
);
1851 static void ttyread (void) {
1855 /* append read bytes to unprocessed bytes */
1856 if (term
== NULL
|| term
->dead
|| term
->cmdfd
< 0) return;
1857 #ifdef DUMP_PROG_OUTPUT
1858 term
->xobuflen
= term
->obuflen
;
1860 left
= OBUFSIZ
-term
->obuflen
;
1861 //dlogf("0: ttyread before: free=%d, used=%d", left, term->obuflen);
1862 while (left
> 0 && ttycanread()) {
1865 //if ((ret = read(term->cmdfd, term->obuf+term->obuflen, 1)) < 0) die("Couldn't read from shell: %s", SERRNO);
1866 if ((ret
= read(term
->cmdfd
, term
->obuf
+term
->obuflen
, left
)) < 0) {
1867 //fprintf(stderr, "Warning: couldn't read from tty #%d: %s\n", termidx, SERRNO);
1870 term
->obuflen
+= ret
;
1873 //dlogf("1: ttyread after: free=%d, used=%d", left, term->obuflen);
1874 /* process every complete utf8 char */
1875 #ifdef DUMP_PROG_OUTPUT
1877 FILE *fo
= fopen("zlogo.log", "ab");
1879 fwrite(term
->obuf
+term
->xobuflen
, term
->obuflen
-term
->xobuflen
, 1, fo
);
1885 if (needConversion
) {
1886 // need conversion from locale to utf-8
1887 //fprintf(stderr, "buf: %d bytes\n", term->obuflen);
1888 while (term
->obuflen
> 0) {
1889 char obuf
[UTF_SIZ
+1];
1892 len
= loc2utf(obuf
, ptr
, 1);
1895 fprintf(stderr
, "rdc: [");
1897 fprintf(stderr
, "] --> [");
1899 fprintf(stderr
, "]\n");
1912 // don't do any conversion
1913 while (term
->obuflen
>= UTF_SIZ
|| isfullutf8(ptr
, term
->obuflen
)) {
1916 int charsize
= utf8decode(ptr
, &utf8c
); /* returns size of utf8 char in bytes */
1919 len
= utf8encode(&utf8c
, s
);
1922 fprintf(stderr
, "rdx: [");
1924 fprintf(stderr
, "]\n");
1933 term
->obuflen
-= charsize
;
1935 //dlogf("2: ttyread afterproc: used=%d", term->obuflen);
1937 /* keep any uncomplete utf8 char for the next call */
1938 if (term
->obuflen
> 0) memmove(term
->obuf
, ptr
, term
->obuflen
);
1942 static void ttyflushwrbuf (void) {
1943 if (term
== NULL
|| term
->dead
|| term
->cmdfd
< 0) return;
1944 if (term
->wrbufpos
>= term
->wrbufused
) {
1945 term
->wrbufpos
= term
->wrbufused
= 0;
1948 //dlogf("0: ttyflushwrbuf before: towrite=%d", term->wrbufused-term->wrbufpos);
1949 while (term
->wrbufpos
< term
->wrbufused
&& ttycanwrite()) {
1952 if ((ret
= write(term
->cmdfd
, term
->wrbuf
+term
->wrbufpos
, term
->wrbufused
-term
->wrbufpos
)) == -1) {
1953 //fprintf(stderr, "Warning: write error on tty #%d: %s\n", termidx, SERRNO);
1955 term
->wrbufpos
+= ret
;
1957 if (term
->wrbufpos
> 0) {
1958 int left
= term
->wrbufused
-term
->wrbufpos
;
1961 // write buffer is empty
1962 term
->wrbufpos
= term
->wrbufused
= 0;
1964 memmove(term
->wrbuf
, term
->wrbuf
+term
->wrbufpos
, left
);
1966 term
->wrbufused
= left
;
1969 //dlogf("1: ttyflushwrbuf after: towrite=%d", term->wrbufused-term->wrbufpos);
1973 // convert char to locale and write it
1974 static void ttywriterawchar (const char *s
, int len
) {
1978 if (s
== NULL
|| len
< 1) return;
1979 clen
= utf2loc(loc
, s
, len
);
1980 if (clen
< 1) return;
1981 #ifdef DUMP_IO_WRITE
1983 fprintf(stderr
, "wrc: [");
1985 fprintf(stderr
, "] --> [");
1987 fprintf(stderr
, "]\n");
1992 while (term
->wrbufused
+clen
>= term
->wrbufsize
) {
1993 //FIXME: make write buffer dynamic?
1994 // force write at least one char
1995 //dlogf("ttywrite: forced write");
1996 if (write(term
->cmdfd
, term
->wrbuf
+term
->wrbufpos
, 1) == -1) {
1997 //fprintf(stderr, "Warning: write error on tty #%d: %s\n", termidx, SERRNO);
2001 ttyflushwrbuf(); // make room for char
2003 memcpy(term
->wrbuf
+term
->wrbufused
, loc
, clen
);
2004 term
->wrbufused
+= clen
;
2008 static void ttywrite (const char *s
, size_t n
) {
2009 if (term
== NULL
|| term
->dead
|| term
->cmdfd
< 0) return;
2010 #ifdef DUMP_PROG_INPUT
2011 if (s
!= NULL
&& n
> 0) {
2012 FILE *fo
= fopen("zlogw.log", "ab");
2014 fwrite(s
, n
, 1, fo
);
2020 if (s
!= NULL
&& n
> 0) {
2022 unsigned char c
= (unsigned char)(s
[0]);
2024 if (term
->ubufpos
> 0 && isfullutf8(term
->ubuf
, term
->ubufpos
)) {
2025 // have complete char
2026 ttywriterawchar(term
->ubuf
, term
->ubufpos
);
2031 if (term
->ubufpos
== 0) {
2034 ttywriterawchar(s
, 1);
2035 } else if ((c
&0xc0) == 0xc0) {
2037 term
->ubuf
[term
->ubufpos
++] = *s
;
2039 // ignore unsynced utf-8
2046 if (c
< 128 || term
->ubufpos
>= UTF_SIZ
|| (c
&0xc0) == 0xc0) {
2047 // discard previous utf-8, it's bad
2052 term
->ubuf
[term
->ubufpos
++] = *s
;
2055 if (isfullutf8(term
->ubuf
, term
->ubufpos
)) {
2056 // have complete char
2057 ttywriterawchar(term
->ubuf
, term
->ubufpos
);
2066 ////////////////////////////////////////////////////////////////////////////////
2068 static void ttyresize (void) {
2071 if (term
!= NULL
&& term
->cmdfd
>= 0) {
2072 w
.ws_row
= term
->row
;
2073 w
.ws_col
= term
->col
;
2074 w
.ws_xpixel
= w
.ws_ypixel
= 0;
2075 if (ioctl(term
->cmdfd
, TIOCSWINSZ
, &w
) < 0) fprintf(stderr
, "Warning: couldn't set window size: %s\n", SERRNO
);
2081 ////////////////////////////////////////////////////////////////////////////////
2083 static void csidump (void) {
2085 for (int f
= 1; f
< term
->escseq
.len
; ++f
) {
2086 uint c
= (term
->escseq
.buf
[f
]&0xff);
2088 if (isprint(c
)) putchar(c
);
2089 else if (c
== '\n') printf("(\\n)");
2090 else if (c
== '\r') printf("(\\r)");
2091 else if (c
== 0x1b) printf("(\\e)");
2092 else printf("(%02x)", c
);
2098 static void tsetdirt (int top
, int bot
) {
2099 LIMIT(top
, 0, term
->row
-1);
2100 LIMIT(bot
, 0, term
->row
-1);
2101 for (int y
= top
; y
<= bot
; ++y
) markDirty(y
, 2);
2105 static void tfulldirt (void) {
2106 tsetdirt(0, term
->row
-1);
2110 static void tmoveto (int x
, int y
) {
2111 LIMIT(x
, 0, term
->col
-1);
2112 LIMIT(y
, 0, term
->row
-1);
2113 term
->c
.state
&= ~CURSOR_WRAPNEXT
;
2114 if (term
->c
.x
!= x
|| term
->c
.y
!= y
) {
2122 static void tclearregion (int x1
, int y1
, int x2
, int y2
) {
2125 if (x1
> x2
) { temp
= x1
; x1
= x2
; x2
= temp
; }
2126 if (y1
> y2
) { temp
= y1
; y1
= y2
; y2
= temp
; }
2127 LIMIT(x1
, 0, term
->col
-1);
2128 LIMIT(x2
, 0, term
->col
-1);
2129 LIMIT(y1
, 0, term
->row
-1);
2130 LIMIT(y2
, 0, term
->row
-1);
2131 for (int y
= y1
; y
<= y2
; ++y
) {
2132 markDirty(y
, (x1
<= 0 && x2
>= term
->col
-1) ? 2 : 1);
2133 for (int x
= x1
; x
<= x2
; ++x
) {
2134 term
->line
[y
][x
].fg
= term
->c
.attr
.fg
;
2135 term
->line
[y
][x
].bg
= term
->c
.attr
.bg
;
2136 term
->line
[y
][x
].state
= GLYPH_DIRTY
;
2137 term
->line
[y
][x
].mode
= ATTR_NULL
|(term
->c
.attr
.mode
&(ATTR_DEFFG
|ATTR_DEFBG
));
2138 term
->line
[y
][x
].c
[0] = ' ';
2144 static void tcursor (int mode
) {
2145 if (mode
== CURSOR_SAVE
) {
2146 term
->csaved
= term
->c
;
2147 } else if (mode
== CURSOR_LOAD
) {
2148 term
->c
= term
->csaved
;
2149 tmoveto(term
->c
.x
, term
->c
.y
);
2155 static void treset (void) {
2158 term
->c
= (TCursor
){{
2159 .mode
= ATTR_NULL
|ATTR_DEFFG
|ATTR_DEFBG
,
2162 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
2163 term
->c
.attr
.fg
= term
->deffg
;
2164 term
->c
.attr
.bg
= term
->defbg
;
2166 g
.state
= GLYPH_DIRTY
;
2167 g
.mode
= term
->c
.attr
.mode
;
2168 g
.fg
= term
->c
.attr
.fg
;
2169 g
.bg
= term
->c
.attr
.bg
;
2174 term
->bot
= term
->row
-1;
2175 term
->mode
= MODE_WRAP
/* | MODE_MOUSEBTN*/;
2176 //tclearregion(0, 0, term->col-1, term->row-1);
2177 for (int y
= 0; y
< term
->row
; ++y
) {
2179 for (int x
= 0; x
< term
->col
; ++x
) term
->alt
[y
][x
] = term
->line
[y
][x
] = g
;
2181 for (int y
= term
->row
; y
< term
->linecount
; ++y
) {
2182 for (int x
= 0; x
< term
->col
; ++x
) term
->line
[y
][x
] = g
;
2184 tcursor(CURSOR_SAVE
);
2190 static int tinitialize (int col
, int row
) {
2191 //memset(term, 0, sizeof(Term));
2192 term
->wrbufsize
= WBUFSIZ
;
2193 term
->deffg
= term
->deffg
;
2194 term
->defbg
= term
->defbg
;
2197 term
->dirty
= calloc(term
->row
, sizeof(*term
->dirty
));
2198 term
->maxhistory
= opt_maxhistory
;
2199 term
->linecount
= term
->maxhistory
+term
->row
;
2200 term
->line
= calloc(term
->linecount
, sizeof(Line
));
2201 term
->alt
= calloc(term
->row
, sizeof(Line
));
2202 for (int y
= 0; y
< term
->linecount
; ++y
) term
->line
[y
] = calloc(term
->col
, sizeof(Glyph
));
2203 for (int y
= 0; y
< term
->row
; ++y
) term
->alt
[y
] = calloc(term
->col
, sizeof(Glyph
));
2210 static void tswapscreen (void) {
2211 for (int f
= 0; f
< term
->row
; ++f
) {
2212 Line t
= term
->line
[f
];
2214 term
->line
[f
] = term
->alt
[f
];
2217 term
->mode
^= MODE_ALTSCREEN
;
2222 //FIXME: will not work for history
2223 static void selscroll (int orig
, int n
) {
2224 if (term
->sel
.bx
== -1) return;
2226 if (BETWEEN(term
->sel
.by
, orig
, term
->bot
) || BETWEEN(term
->sel
.ey
, orig
, term
->bot
)) {
2227 if ((term
->sel
.by
+= n
) > term
->bot
|| (term
->sel
.ey
+= n
) < term
->top
) {
2231 if (term
->sel
.by
< term
->top
) {
2232 term
->sel
.by
= term
->top
;
2235 if (term
->sel
.ey
> term
->bot
) {
2236 term
->sel
.ey
= term
->bot
;
2237 term
->sel
.ex
= term
->col
;
2239 term
->sel
.b
.y
= term
->sel
.by
, term
->sel
.b
.x
= term
->sel
.bx
;
2240 term
->sel
.e
.y
= term
->sel
.ey
, term
->sel
.e
.x
= term
->sel
.ex
;
2245 static void tscrolldown (int orig
, int n
) {
2248 LIMIT(n
, 0, term
->bot
-orig
+1);
2249 //fprintf(stderr, "tscrolldown(%d, %d)\n", orig, n);
2250 tclearregion(0, term
->bot
-n
+1, term
->col
-1, term
->bot
);
2251 for (int f
= term
->bot
; f
>= orig
+n
; --f
) {
2252 temp
= term
->line
[f
];
2253 term
->line
[f
] = term
->line
[f
-n
];
2254 term
->line
[f
-n
] = temp
;
2262 static void tscrollup (int orig
, int n
) {
2265 if (term
== NULL
) return;
2266 LIMIT(n
, 0, term
->bot
-orig
+1);
2268 //fprintf(stderr, "tscrollup(%d, %d)\n", orig, n);
2269 if (n
== 1 && orig
== 0 && term
->bot
>= term
->row
-1 && term
->maxhistory
> 0) {
2270 Line l
= term
->line
[term
->linecount
-1];
2272 for (int f
= term
->linecount
-1; f
> term
->row
; --f
) term
->line
[f
] = term
->line
[f
-1];
2273 term
->line
[term
->row
] = l
;
2274 for (int x
= 0; x
< term
->col
; ++x
) l
[x
] = term
->line
[0][x
];
2277 tclearregion(0, orig
, term
->col
-1, orig
+n
-1);
2279 for (int f
= orig
; f
<= term
->bot
-n
; ++f
) {
2280 temp
= term
->line
[f
];
2281 term
->line
[f
] = term
->line
[f
+n
];
2282 term
->line
[f
+n
] = temp
;
2286 selscroll(orig
, -n
);
2290 static void tnewline (int first_col
) {
2293 if (y
== term
->bot
) tscrollup(term
->top
, 1); else ++y
;
2294 tmoveto(first_col
? 0 : term
->c
.x
, y
);
2298 static void csiparse (void) {
2299 const char *p
= term
->escseq
.buf
;
2301 term
->escseq
.narg
= 0;
2302 if (*p
== '?') { term
->escseq
.priv
= 1; ++p
; }
2303 while (p
< term
->escseq
.buf
+term
->escseq
.len
) {
2304 int n
= term
->escseq
.arg
[term
->escseq
.narg
];
2306 for (; *p
&& isdigit(*p
); ++p
) n
= n
*10+(p
[0]-'0');
2307 term
->escseq
.arg
[term
->escseq
.narg
] = n
;
2309 if (*p
== ';' && term
->escseq
.narg
+1 < ESC_ARG_SIZ
) {
2310 ++term
->escseq
.narg
;
2313 term
->escseq
.mode
= *p
;
2314 ++term
->escseq
.narg
;
2321 static void tsetchar (const char *c
) {
2323 int rev
= 0, gfx
= 0;
2324 int x
= term
->c
.x
, y
= term
->c
.y
;
2326 if (!needConversion
&& unimap
!= NULL
) {
2330 if (cc
>= 0 && cc
<= 65535) {
2331 uchar uc
= unimap
[cc
];
2352 if (rev || gfx || (term->line[y][x].state & GLYPH_SET) == 0 || ATTRCMP(term->line[y][x], term->c.attr)) {
2353 term->line[y][x] = term->c.attr;
2354 if (rev) term->line[y][x].mode ^= ATTR_REVERSE;
2355 if (gfx) term->line[y][x].mode |= ATTR_GFX;
2357 int clen = utf8size(c), olen = utf8size(term->line[y][x].c);
2359 if (clen < 1 || (clen == olen && memcmp(c, term->line[y][x].c, clen) == 0)) return;
2364 term
->line
[y
][x
] = term
->c
.attr
;
2365 if (rev
) term
->line
[y
][x
].mode
^= ATTR_REVERSE
;
2367 term
->line
[y
][x
].mode
&= ~(ATTR_GFX
|ATTR_GFX1
);
2368 term
->line
[y
][x
].mode
|= (term
->line
[y
][x
].mode
&ATTR_G1
) ? ATTR_GFX1
: ATTR_GFX
;
2371 term
->line
[y
][x
].state
|= (GLYPH_SET
| GLYPH_DIRTY
);
2372 memmove(term
->line
[y
][x
].c
, c
, UTF_SIZ
);
2374 if (IS_GFX(term
->line
[y
][x
].mode
)) {
2375 unsigned char c
= (unsigned char)(term
->line
[y
][x
].c
[0]);
2377 if (c
> 95 && c
< 128) term
->line
[y
][x
].c
[0] -= 95;
2378 else if (c
> 127) term
->line
[y
][x
].c
[0] = ' ';
2380 //dlogf("tsetchar(%d,%d): [%c] (%d); dirty=%d\n", x, y, c[0], term->wantRedraw, term->dirty[y]);
2384 static void tdeletechar (int n
) {
2385 int src
= term
->c
.x
+n
;
2386 int dst
= term
->c
.x
;
2387 int size
= term
->col
-src
;
2389 markDirty(term
->c
.y
, 2);
2390 if (src
>= term
->col
) {
2391 tclearregion(term
->c
.x
, term
->c
.y
, term
->col
-1, term
->c
.y
);
2393 memmove(&term
->line
[term
->c
.y
][dst
], &term
->line
[term
->c
.y
][src
], size
*sizeof(Glyph
));
2394 tclearregion(term
->col
-n
, term
->c
.y
, term
->col
-1, term
->c
.y
);
2399 static void tinsertblank (int n
) {
2400 int src
= term
->c
.x
;
2402 int size
= term
->col
-dst
;
2404 markDirty(term
->c
.y
, 2);
2405 if (dst
>= term
->col
) {
2406 tclearregion(term
->c
.x
, term
->c
.y
, term
->col
-1, term
->c
.y
);
2408 memmove(&term
->line
[term
->c
.y
][dst
], &term
->line
[term
->c
.y
][src
], size
*sizeof(Glyph
));
2409 tclearregion(src
, term
->c
.y
, dst
-1, term
->c
.y
);
2414 static void tinsertblankline (int n
) {
2415 if (term
->c
.y
< term
->top
|| term
->c
.y
> term
->bot
) return;
2416 tscrolldown(term
->c
.y
, n
);
2420 static void tdeleteline (int n
) {
2421 if (term
->c
.y
< term
->top
|| term
->c
.y
> term
->bot
) return;
2422 tscrollup(term
->c
.y
, n
);
2426 static void tsetattr (int *attr
, int l
) {
2427 for (int f
= 0; f
< l
; ++f
) {
2430 term
->c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD
);
2431 term
->c
.attr
.mode
|= ATTR_DEFFG
| ATTR_DEFBG
;
2432 term
->c
.attr
.fg
= term
->deffg
;
2433 term
->c
.attr
.bg
= term
->defbg
;
2436 term
->c
.attr
.mode
|= ATTR_BOLD
;
2439 term
->c
.attr
.mode
|= ATTR_UNDERLINE
;
2442 term
->c
.attr
.mode
|= ATTR_REVERSE
;
2445 term
->c
.attr
.mode
&= ~ATTR_BOLD
;
2448 term
->c
.attr
.mode
&= ~ATTR_UNDERLINE
;
2451 term
->c
.attr
.mode
&= ~ATTR_REVERSE
;
2454 if (f
+2 < l
&& attr
[f
+1] == 5) {
2456 if (BETWEEN(attr
[f
], 0, 255)) {
2457 term
->c
.attr
.fg
= attr
[f
];
2458 term
->c
.attr
.mode
&= ~ATTR_DEFFG
;
2460 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[f
]);
2463 //fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[f]);
2464 term
->c
.attr
.fg
= term
->deffg
;
2465 term
->c
.attr
.mode
|= ATTR_DEFFG
;
2469 term
->c
.attr
.fg
= term
->deffg
;
2470 term
->c
.attr
.mode
|= ATTR_DEFFG
;
2473 if (f
+2 < l
&& attr
[f
+1] == 5) {
2475 if (BETWEEN(attr
[f
], 0, 255)) {
2476 term
->c
.attr
.bg
= attr
[f
];
2477 term
->c
.attr
.mode
&= ~ATTR_DEFBG
;
2479 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[f
]);
2482 fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[f
]);
2486 term
->c
.attr
.bg
= term
->defbg
;
2487 term
->c
.attr
.mode
|= ATTR_DEFBG
;
2490 if (BETWEEN(attr
[f
], 30, 37)) { term
->c
.attr
.fg
= attr
[f
]-30; term
->c
.attr
.mode
&= ~ATTR_DEFFG
; }
2491 else if (BETWEEN(attr
[f
], 40, 47)) { term
->c
.attr
.bg
= attr
[f
]-40; term
->c
.attr
.mode
&= ~ATTR_DEFBG
; }
2492 else if (BETWEEN(attr
[f
], 90, 97)) { term
->c
.attr
.fg
= attr
[f
]-90+8; term
->c
.attr
.mode
&= ~ATTR_DEFFG
; }
2493 else if (BETWEEN(attr
[f
], 100, 107)) { term
->c
.attr
.fg
= attr
[f
]-100+8; term
->c
.attr
.mode
&= ~ATTR_DEFFG
; }
2494 else { fprintf(stderr
, "erresc: gfx attr %d unknown\n", attr
[f
]); csidump(); }
2501 static void tsetscroll (int t
, int b
) {
2504 LIMIT(t
, 0, term
->row
-1);
2505 LIMIT(b
, 0, term
->row
-1);
2516 ////////////////////////////////////////////////////////////////////////////////
2518 static void csihandle (void) {
2519 switch (term
->escseq
.mode
) {
2520 case '@': /* ICH -- Insert <n> blank char */
2521 DEFAULT(term
->escseq
.arg
[0], 1);
2522 tinsertblank(term
->escseq
.arg
[0]);
2524 case 'A': /* CUU -- Cursor <n> Up */
2526 DEFAULT(term
->escseq
.arg
[0], 1);
2527 tmoveto(term
->c
.x
, term
->c
.y
-term
->escseq
.arg
[0]);
2529 case 'B': /* CUD -- Cursor <n> Down */
2530 DEFAULT(term
->escseq
.arg
[0], 1);
2531 tmoveto(term
->c
.x
, term
->c
.y
+term
->escseq
.arg
[0]);
2533 case 'C': /* CUF -- Cursor <n> Forward */
2535 DEFAULT(term
->escseq
.arg
[0], 1);
2536 tmoveto(term
->c
.x
+term
->escseq
.arg
[0], term
->c
.y
);
2538 case 'D': /* CUB -- Cursor <n> Backward */
2539 DEFAULT(term
->escseq
.arg
[0], 1);
2540 tmoveto(term
->c
.x
-term
->escseq
.arg
[0], term
->c
.y
);
2542 case 'E': /* CNL -- Cursor <n> Down and first col */
2543 DEFAULT(term
->escseq
.arg
[0], 1);
2544 tmoveto(0, term
->c
.y
+term
->escseq
.arg
[0]);
2546 case 'F': /* CPL -- Cursor <n> Up and first col */
2547 DEFAULT(term
->escseq
.arg
[0], 1);
2548 tmoveto(0, term
->c
.y
-term
->escseq
.arg
[0]);
2550 case 'G': /* CHA -- Move to <col> */
2551 case '`': /* XXX: HPA -- same? */
2552 DEFAULT(term
->escseq
.arg
[0], 1);
2553 tmoveto(term
->escseq
.arg
[0]-1, term
->c
.y
);
2555 case 'H': /* CUP -- Move to <row> <col> */
2556 case 'f': /* XXX: HVP -- same? */
2557 DEFAULT(term
->escseq
.arg
[0], 1);
2558 DEFAULT(term
->escseq
.arg
[1], 1);
2559 tmoveto(term
->escseq
.arg
[1]-1, term
->escseq
.arg
[0]-1);
2561 /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
2562 case 'J': /* ED -- Clear screen */
2564 switch (term
->escseq
.arg
[0]) {
2566 tclearregion(term
->c
.x
, term
->c
.y
, term
->col
-1, term
->c
.y
);
2567 if (term
->c
.y
< term
->row
-1) tclearregion(0, term
->c
.y
+1, term
->col
-1, term
->row
-1);
2570 if (term
->c
.y
> 1) tclearregion(0, 0, term
->col
-1, term
->c
.y
-1);
2571 tclearregion(0, term
->c
.y
, term
->c
.x
, term
->c
.y
);
2574 tclearregion(0, 0, term
->col
-1, term
->row
-1);
2580 case 'K': /* EL -- Clear line */
2581 switch (term
->escseq
.arg
[0]) {
2583 tclearregion(term
->c
.x
, term
->c
.y
, term
->col
-1, term
->c
.y
);
2586 tclearregion(0, term
->c
.y
, term
->c
.x
, term
->c
.y
);
2589 tclearregion(0, term
->c
.y
, term
->col
-1, term
->c
.y
);
2593 case 'S': /* SU -- Scroll <n> line up */
2594 DEFAULT(term
->escseq
.arg
[0], 1);
2595 tscrollup(term
->top
, term
->escseq
.arg
[0]);
2597 case 'T': /* SD -- Scroll <n> line down */
2598 DEFAULT(term
->escseq
.arg
[0], 1);
2599 tscrolldown(term
->top
, term
->escseq
.arg
[0]);
2601 case 'L': /* IL -- Insert <n> blank lines */
2602 DEFAULT(term
->escseq
.arg
[0], 1);
2603 tinsertblankline(term
->escseq
.arg
[0]);
2605 case 'l': /* RM -- Reset Mode */
2606 if (term
->escseq
.priv
) {
2607 switch (term
->escseq
.arg
[0]) {
2608 case 1: // 1001 for xterm compatibility
2609 DUMP_KEYPAD_SWITCH("1", "OFF");
2610 term
->mode
&= ~MODE_APPKEYPAD
;
2612 case 5: /* DECSCNM -- Remove reverse video */
2613 if (IS_SET(MODE_REVERSE
)) {
2614 term
->mode
&= ~MODE_REVERSE
;
2618 case 7: /* autowrap off */
2619 term
->mode
&= ~MODE_WRAP
;
2621 case 12: /* att610 -- Stop blinking cursor (IGNORED) */
2623 case 20: /* non-standard code? */
2624 term
->mode
&= ~MODE_CRLF
;
2626 case 25: /* hide cursor */
2627 term
->c
.state
|= CURSOR_HIDE
;
2629 case 1000: /* disable X11 xterm mouse reporting */
2630 term
->mode
&= ~MODE_MOUSEBTN
;
2633 term
->mode
&= ~MODE_MOUSEMOTION
;
2635 case 1049: /* = 1047 and 1048 */
2638 if (IS_SET(MODE_ALTSCREEN
)) {
2639 tclearregion(0, 0, term
->col
-1, term
->row
-1);
2642 if (term
->escseq
.arg
[0] != 1049) break;
2644 tcursor(CURSOR_LOAD
);
2650 switch (term
->escseq
.arg
[0]) {
2652 term
->mode
&= ~MODE_INSERT
;
2659 case 'M': /* DL -- Delete <n> lines */
2660 DEFAULT(term
->escseq
.arg
[0], 1);
2661 tdeleteline(term
->escseq
.arg
[0]);
2663 case 'X': /* ECH -- Erase <n> char */
2664 DEFAULT(term
->escseq
.arg
[0], 1);
2665 tclearregion(term
->c
.x
, term
->c
.y
, term
->c
.x
+ term
->escseq
.arg
[0], term
->c
.y
);
2667 case 'P': /* DCH -- Delete <n> char */
2668 DEFAULT(term
->escseq
.arg
[0], 1);
2669 tdeletechar(term
->escseq
.arg
[0]);
2671 /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
2672 case 'd': /* VPA -- Move to <row> */
2673 DEFAULT(term
->escseq
.arg
[0], 1);
2674 tmoveto(term
->c
.x
, term
->escseq
.arg
[0]-1);
2676 case 'h': /* SM -- Set terminal mode */
2677 if (term
->escseq
.priv
) {
2678 switch (term
->escseq
.arg
[0]) {
2680 DUMP_KEYPAD_SWITCH("1", "ON");
2681 term
->mode
|= MODE_APPKEYPAD
;
2683 case 5: /* DECSCNM -- Reverve video */
2684 if (!IS_SET(MODE_REVERSE
)) {
2685 term
->mode
|= MODE_REVERSE
;
2690 term
->mode
|= MODE_WRAP
;
2693 term
->mode
|= MODE_CRLF
;
2695 case 12: /* att610 -- Start blinking cursor (IGNORED) */
2696 /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
2697 if (term
->escseq
.narg
> 1 && term
->escseq
.arg
[1] != 25) break;
2699 term
->c
.state
&= ~CURSOR_HIDE
;
2701 case 1000: /* 1000,1002: enable xterm mouse report */
2702 term
->mode
|= MODE_MOUSEBTN
;
2705 term
->mode
|= MODE_MOUSEMOTION
;
2707 case 1049: /* = 1047 and 1048 */
2710 if (IS_SET(MODE_ALTSCREEN
)) tclearregion(0, 0, term
->col
-1, term
->row
-1); else tswapscreen();
2711 if (term
->escseq
.arg
[0] != 1049) break;
2713 tcursor(CURSOR_SAVE
);
2715 default: goto unknown
;
2718 switch (term
->escseq
.arg
[0]) {
2720 term
->mode
|= MODE_INSERT
;
2727 case 'm': /* SGR -- Terminal attribute (color) */
2728 tsetattr(term
->escseq
.arg
, term
->escseq
.narg
);
2730 case 'r': /* DECSTBM -- Set Scrolling Region */
2731 if (term
->escseq
.priv
&& term
->escseq
.arg
[0] == 1001) {
2732 // xterm compatibility
2733 DUMP_KEYPAD_SWITCH("1001", "OFF");
2734 term
->mode
&= ~MODE_APPKEYPAD
;
2735 } else if (term
->escseq
.priv
) {
2738 DEFAULT(term
->escseq
.arg
[0], 1);
2739 DEFAULT(term
->escseq
.arg
[1], term
->row
);
2740 tsetscroll(term
->escseq
.arg
[0]-1, term
->escseq
.arg
[1]-1);
2744 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2745 if (term
->escseq
.priv
&& term
->escseq
.arg
[0] == 1001) {
2746 // xterm compatibility
2747 DUMP_KEYPAD_SWITCH("1001", "ON");
2748 term
->mode
|= MODE_APPKEYPAD
;
2750 tcursor(CURSOR_SAVE
);
2753 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2754 tcursor(CURSOR_LOAD
);
2758 fprintf(stderr
, "erresc: unknown csi ");
2765 static void csireset (void) {
2766 memset(&term
->escseq
, 0, sizeof(term
->escseq
));
2770 static void tputtab (void) {
2771 int space
= opt_tabsize
-term
->c
.x
%opt_tabsize
;
2773 if (space
> 0) tmoveto(term
->c
.x
+space
, term
->c
.y
);
2777 ////////////////////////////////////////////////////////////////////////////////
2778 // put char to output buffer or process command
2779 static void tputc (const char *c
) {
2782 //dlogf("tputc: [%c]\n", c[0]);
2783 if (term
->esc
& ESC_START
) {
2784 if (term
->esc
& ESC_CSI
) {
2785 term
->escseq
.buf
[term
->escseq
.len
++] = ascii
;
2786 if (BETWEEN(ascii
, 0x40, 0x7E) || term
->escseq
.len
>= ESC_BUF_SIZ
) {
2791 } else if (term
->esc
& ESC_OSC
) {
2792 /* TODO: handle other OSC */
2796 term
->esc
= ESC_START
| ESC_TITLE
;
2799 } else if (term
->esc
& ESC_TITLE
) {
2800 int len
= utf8size(c
);
2802 if (ascii
== '\a' || term
->titlelen
+len
>= ESC_TITLE_SIZ
) {
2804 term
->title
[term
->titlelen
] = '\0';
2805 fixWindowTitle(term
);
2807 } else if (len
> 0) {
2808 memcpy(term
->title
+term
->titlelen
, c
, len
);
2809 term
->titlelen
+= len
;
2810 term
->title
[term
->titlelen
] = '\0';
2812 } else if (term
->esc
& ESC_ALTCHARSET
) {
2815 case '0': /* Line drawing crap */
2816 term
->c
.attr
.mode
|= ATTR_GFX
;
2818 case 'B': /* Back to regular text */
2819 term
->c
.attr
.mode
&= ~ATTR_GFX
;
2822 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2823 term
->c
.attr
.mode
&= ~ATTR_GFX
;
2826 } else if (term
->esc
& ESC_ALTG1
) {
2829 case '0': /* Line drawing crap */
2830 term
->c
.attr
.mode
|= ATTR_GFX1
;
2832 case 'B': /* Back to regular text */
2833 term
->c
.attr
.mode
&= ~ATTR_GFX1
;
2836 fprintf(stderr
, "esc unhandled charset: ESC ) %c\n", ascii
);
2837 term
->c
.attr
.mode
&= ~ATTR_GFX1
;
2840 } else if (term
->esc
& ESC_HASH
) {
2843 case '8': /* DECALN -- DEC screen alignment test -- fill screen with E's */
2844 //tfillscreenwithE();
2847 } else if (term
->esc
& ESC_PERCENT
) {
2851 case '[': term
->esc
|= ESC_CSI
; break;
2852 case ']': term
->esc
|= ESC_OSC
; break;
2853 case '(': term
->esc
|= ESC_ALTCHARSET
; break;
2854 case ')': term
->esc
|= ESC_ALTG1
; break;
2855 case '#': term
->esc
|= ESC_HASH
; break;
2856 case '%': term
->esc
|= ESC_PERCENT
; break;
2857 case 'D': /* IND -- Linefeed */
2859 if (term
->c
.y
== term
->bot
) tscrollup(term
->top
, 1); else tmoveto(term
->c
.x
, term
->c
.y
+1);
2861 case 'E': /* NEL -- Next line */
2863 tnewline(1); /* always go to first col */
2865 case 'M': /* RI -- Reverse linefeed */
2867 if (term
->c
.y
== term
->top
) tscrolldown(term
->top
, 1); else tmoveto(term
->c
.x
, term
->c
.y
-1);
2869 case 'c': /* RIS -- Reset to inital state */
2873 case '=': /* DECPAM -- Application keypad */
2874 DUMP_KEYPAD_SWITCH("=", "ON");
2876 term
->mode
|= MODE_APPKEYPAD
;
2878 case '>': /* DECPNM -- Normal keypad */
2879 DUMP_KEYPAD_SWITCH(">", "OFF");
2881 term
->mode
&= ~MODE_APPKEYPAD
;
2883 case '7': /* DECSC -- Save Cursor */
2884 /* Save current state (cursor coordinates, attributes, character sets pointed at by G0, G1) */
2887 tcursor(CURSOR_SAVE
);
2889 case '8': /* DECRC -- Restore Cursor */
2892 tcursor(CURSOR_LOAD
);
2894 case 'Z': /* DEC private identification */
2896 ttywritestr("\x1b[?1;2c");
2900 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X ('%c')\n", (uchar
)ascii
, isprint(ascii
)?ascii
:'.');
2905 if (term
->sel
.bx
!= -1 && BETWEEN(term
->c
.y
, term
->sel
.by
, term
->sel
.ey
)) term
->sel
.bx
= -1;
2907 case '\t': tputtab(); break;
2908 case '\b': tmoveto(term
->c
.x
-1, term
->c
.y
); break;
2909 case '\r': tmoveto(0, term
->c
.y
); break;
2910 case '\v': tnewline(0); break;
2911 case '\f': case '\n': tnewline(IS_SET(MODE_CRLF
)); break; /* go to first col if the mode is set */
2912 case '\a': if (!(xw
.state
& WIN_FOCUSED
)) xseturgency(1); XBell(xw
.dpy
, 100); break;
2913 case 14: term
->c
.attr
.mode
|= ATTR_G1
; break;
2914 case 15: term
->c
.attr
.mode
&= ~ATTR_G1
; break;
2915 case '\033': csireset(); term
->esc
= ESC_START
; break;
2917 if (needConversion
&& IS_GFX(term
->c
.attr
.mode
)) {
2921 if (cc
< 32 || cc
>= 127) break; //FIXME: nothing at all
2923 if ((unsigned char)ascii
< 32 || ascii
== 127) break; // seems that this chars are empty too
2925 if (IS_SET(MODE_WRAP
) && (term
->c
.state
&CURSOR_WRAPNEXT
)) tnewline(1); /* always go to first col */
2927 if (term
->c
.x
+1 < term
->col
) tmoveto(term
->c
.x
+1, term
->c
.y
); else term
->c
.state
|= CURSOR_WRAPNEXT
;
2934 ////////////////////////////////////////////////////////////////////////////////
2936 static int tresize (int col
, int row
) {
2937 int mincol
= MIN(col
, term
->col
);
2938 int slide
= term
->c
.y
-row
+1;
2941 if (col
< 1 || row
< 1) return 0;
2943 g
.state
= GLYPH_DIRTY
;
2944 g
.mode
= ATTR_NULL
|ATTR_DEFFG
|ATTR_DEFBG
;
2951 tsetscroll(0, term
->row
-1);
2952 for (; slide
> 0; --slide
) tscrollup(0, 1); // to fill history
2955 if (row
< term
->row
) {
2956 /* free unneeded rows */
2957 for (int f
= row
; f
< term
->row
; ++f
) free(term
->alt
[f
]);
2958 for (int f
= term
->linecount
-(term
->row
-row
); f
< term
->linecount
; ++f
) free(term
->line
[f
]);
2959 term
->linecount
-= (term
->row
-row
);
2960 /* resize to new height */
2961 term
->alt
= realloc(term
->alt
, row
*sizeof(Line
));
2962 term
->line
= realloc(term
->line
, term
->linecount
*sizeof(Line
));
2963 } else if (row
> term
->row
) {
2964 /* resize to new height */
2965 term
->alt
= realloc(term
->alt
, row
*sizeof(Line
));
2966 term
->line
= realloc(term
->line
, (row
+term
->maxhistory
)*sizeof(Line
));
2967 /* add more lines */
2968 for (int f
= term
->row
; f
< row
; ++f
) {
2969 term
->alt
[f
] = calloc(col
, sizeof(Glyph
));
2970 for (int x
= 0; x
< col
; ++x
) term
->alt
[f
][x
] = g
;
2972 for (int f
= 0; f
< row
-term
->row
; ++f
) {
2973 int y
= term
->linecount
++;
2975 term
->line
[y
] = calloc(col
, sizeof(Glyph
));
2976 for (int x
= 0; x
< col
; ++x
) term
->line
[y
][x
] = g
;
2980 if (row
!= term
->row
) {
2981 term
->dirty
= realloc(term
->dirty
, row
*sizeof(*term
->dirty
));
2984 /* resize each row to new width, zero-pad if needed */
2985 for (int f
= 0; f
< term
->linecount
; ++f
) {
2986 term
->line
[f
] = realloc(term
->line
[f
], col
*sizeof(Glyph
));
2987 for (int x
= mincol
; x
< col
; ++x
) term
->line
[f
][x
] = g
;
2990 term
->alt
[f
] = realloc(term
->alt
[f
], col
*sizeof(Glyph
));
2991 for (int x
= mincol
; x
< col
; ++x
) term
->alt
[f
][x
] = g
;
2994 /* update terminal size */
2998 /* make use of the LIMIT in tmoveto */
2999 tmoveto(term
->c
.x
, term
->c
.y
);
3000 /* reset scrolling region */
3001 tsetscroll(0, row
-1);
3007 static void xresize (int col
, int row
) {
3011 if (term
== NULL
) return;
3012 oldw
= term
->picbufw
;
3013 oldh
= term
->picbufh
;
3014 term
->picbufw
= MAX(1, col
*xw
.cw
);
3015 term
->picbufh
= MAX(1, row
*xw
.ch
);
3016 newbuf
= XCreatePixmap(xw
.dpy
, xw
.win
, term
->picbufw
, term
->picbufh
, XDefaultDepth(xw
.dpy
, xw
.scr
));
3017 XCopyArea(xw
.dpy
, term
->picbuf
, newbuf
, dc
.gc
, 0, 0, term
->picbufw
, term
->picbufh
, 0, 0);
3018 XFreePixmap(xw
.dpy
, term
->picbuf
);
3019 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[term
->defbg
]);
3020 if (term
->picbufw
> oldw
) {
3021 XFillRectangle(xw
.dpy
, newbuf
, dc
.gc
, oldw
, 0, term
->picbufw
-oldw
, MIN(term
->picbufh
, oldh
));
3022 } else if (term
->picbufw
< oldw
&& (opt_border
> 0 || xw
.w
> term
->picbufw
)) {
3023 XClearArea(xw
.dpy
, xw
.win
, opt_border
+term
->picbufw
, opt_border
, xw
.w
-term
->picbufh
-opt_border
, opt_border
+MIN(term
->picbufh
, oldh
), False
);
3025 if (term
->picbufh
> oldh
) {
3026 XFillRectangle(xw
.dpy
, newbuf
, dc
.gc
, 0, oldh
, term
->picbufw
, term
->picbufh
-oldh
);
3027 } else if (term
->picbufh
< oldh
&& (opt_border
> 0 || xw
.tabheight
> 0 || xw
.h
> term
->picbufh
)) {
3028 XClearArea(xw
.dpy
, xw
.win
, opt_border
, opt_border
+term
->picbufh
, xw
.w
-2*opt_border
, xw
.h
-term
->picbufh
-opt_border
-xw
.tabheight
, False
);
3030 term
->picbuf
= newbuf
;
3036 ////////////////////////////////////////////////////////////////////////////////
3037 // x11 drawing and utils
3038 static void xloadcols (void) {
3041 ulong white
= WhitePixel(xw
.dpy
, xw
.scr
);
3043 dc
.col
= calloc(MAX_COLOR
+1, sizeof(dc
.col
[0]));
3044 for (f
= 0; f
<= MAX_COLOR
; ++f
) dc
.col
[f
] = white
;
3045 /* load colors [0-15] */
3046 for (f
= 0; f
<= 15; ++f
) {
3047 const char *cname
= opt_colornames
[f
]!=NULL
?opt_colornames
[f
]:defcolornames
[f
];
3049 if (!XAllocNamedColor(xw
.dpy
, xw
.cmap
, cname
, &color
, &color
)) {
3050 fprintf(stderr
, "WARNING: could not allocate color #%d: '%s'\n", f
, cname
);
3052 dc
.col
[f
] = color
.pixel
;
3055 /* load colors [256-...] */
3056 for (f
= 256; f
<= MAX_COLOR
; ++f
) {
3057 const char *cname
= opt_colornames
[f
];
3059 if (cname
== NULL
) {
3060 if (LEN(defextcolornames
) <= f
-256) continue;
3061 cname
= defextcolornames
[f
-256];
3063 if (cname
== NULL
) continue;
3064 if (!XAllocNamedColor(xw
.dpy
, xw
.cmap
, cname
, &color
, &color
)) {
3065 fprintf(stderr
, "WARNING: could not allocate color #%d: '%s'\n", f
, cname
);
3067 dc
.col
[f
] = color
.pixel
;
3070 /* load colors [16-255] ; same colors as xterm */
3071 for (f
= 16, r
= 0; r
< 6; ++r
) {
3072 for (g
= 0; g
< 6; ++g
) {
3073 for (b
= 0; b
< 6; ++b
) {
3074 if (opt_colornames
[f
] != NULL
) {
3075 if (!XAllocNamedColor(xw
.dpy
, xw
.cmap
, opt_colornames
[f
], &color
, &color
)) {
3076 fprintf(stderr
, "WARNING: could not allocate color #%d: '%s'\n", f
, opt_colornames
[f
]);
3078 dc
.col
[f
] = color
.pixel
;
3081 color
.red
= r
== 0 ? 0 : 0x3737+0x2828*r
;
3082 color
.green
= g
== 0 ? 0 : 0x3737+0x2828*g
;
3083 color
.blue
= b
== 0 ? 0 : 0x3737+0x2828*b
;
3084 if (!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
3085 fprintf(stderr
, "WARNING: could not allocate color #%d\n", f
);
3087 dc
.col
[f
] = color
.pixel
;
3094 for (r
= 0; r
< 24; ++r
, ++f
) {
3095 if (opt_colornames
[f
] != NULL
) {
3096 if (!XAllocNamedColor(xw
.dpy
, xw
.cmap
, opt_colornames
[f
], &color
, &color
)) {
3097 fprintf(stderr
, "WARNING: could not allocate color #%d: '%s'\n", f
, opt_colornames
[f
]);
3099 dc
.col
[f
] = color
.pixel
;
3102 color
.red
= color
.green
= color
.blue
= 0x0808+0x0a0a*r
;
3103 if (!XAllocColor(xw
.dpy
, xw
.cmap
, &color
)) {
3104 fprintf(stderr
, "WARNING: could not allocate color #%d\n", f
);
3106 dc
.col
[f
] = color
.pixel
;
3111 for (int f
= 0; f
< LEN(opt_colornames
); ++f
) if (opt_colornames
[f
]) free(opt_colornames
[f
]);
3115 static void xclear (int x1
, int y1
, int x2
, int y2
) {
3116 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[IS_SET(MODE_REVERSE
) ? term
->deffg
: term
->defbg
]);
3117 XFillRectangle(xw
.dpy
, term
->picbuf
, dc
.gc
, x1
*xw
.cw
, y1
*xw
.ch
, (x2
-x1
+1)*xw
.cw
, (y2
-y1
+1)*xw
.ch
);
3121 static void xhints (void) {
3122 XClassHint
class = {opt_class
, opt_title
};
3123 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
3125 .flags
= PSize
| PResizeInc
| PBaseSize
,
3128 .height_inc
= xw
.ch
,
3130 .base_height
= 2*opt_border
+xw
.tabheight
,
3131 .base_width
= 2*opt_border
,
3133 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, &size
, &wm
, &class);
3137 static XFontSet
xinitfont (const char *fontstr
) {
3139 char *def
, **missing
;
3143 set
= XCreateFontSet(xw
.dpy
, fontstr
, &missing
, &n
, &def
);
3145 while (n
--) fprintf(stderr
, "sterm: missing fontset: %s\n", missing
[n
]);
3146 XFreeStringList(missing
);
3152 static void xgetfontinfo (XFontSet set
, int *ascent
, int *descent
, short *lbearing
, short *rbearing
, Font
*fid
) {
3153 XFontStruct
**xfonts
;
3157 *ascent
= *descent
= *lbearing
= *rbearing
= 0;
3158 n
= XFontsOfFontSet(set
, &xfonts
, &font_names
);
3159 for (int f
= 0; f
< n
; ++f
) {
3160 if (f
== 0) *fid
= (*xfonts
)->fid
;
3161 *ascent
= MAX(*ascent
, (*xfonts
)->ascent
);
3162 *descent
= MAX(*descent
, (*xfonts
)->descent
);
3163 *lbearing
= MAX(*lbearing
, (*xfonts
)->min_bounds
.lbearing
);
3164 *rbearing
= MAX(*rbearing
, (*xfonts
)->max_bounds
.rbearing
);
3170 static void initfonts (const char *fontstr
, const char *bfontstr
, const char *tabfont
) {
3171 if ((dc
.font
[0].set
= xinitfont(fontstr
)) == NULL
) die("can't load font %s", fontstr
);
3172 xgetfontinfo(dc
.font
[0].set
, &dc
.font
[0].ascent
, &dc
.font
[0].descent
, &dc
.font
[0].lbearing
, &dc
.font
[0].rbearing
, &dc
.font
[0].fid
);
3174 if ((dc
.font
[1].set
= xinitfont(bfontstr
)) == NULL
) die("can't load font %s", bfontstr
);
3175 xgetfontinfo(dc
.font
[1].set
, &dc
.font
[1].ascent
, &dc
.font
[1].descent
, &dc
.font
[1].lbearing
, &dc
.font
[1].rbearing
, &dc
.font
[1].fid
);
3177 if ((dc
.font
[2].set
= xinitfont(tabfont
)) == NULL
) die("can't load font %s", tabfont
);
3178 xgetfontinfo(dc
.font
[2].set
, &dc
.font
[2].ascent
, &dc
.font
[2].descent
, &dc
.font
[2].lbearing
, &dc
.font
[2].rbearing
, &dc
.font
[2].fid
);
3182 static void xinit (void) {
3183 XSetWindowAttributes attrs
;
3185 XColor blackcolor
= { 0, 0, 0, 0, 0, 0 };
3187 if (!(xw
.dpy
= XOpenDisplay(NULL
))) die("can't open display");
3189 XA_VT_SELECTION
= XInternAtom(xw
.dpy
, "_STERM_SELECTION_", 0);
3190 XA_CLIPBOARD
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
3191 XA_UTF8
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
3192 XA_NETWM_NAME
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", 0);
3193 XA_TARGETS
= XInternAtom(xw
.dpy
, "TARGETS", 0);
3194 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
3196 xw
.scr
= XDefaultScreen(xw
.dpy
);
3198 initfonts(opt_fontnorm
, opt_fontbold
, opt_fonttab
);
3199 /* XXX: Assuming same size for bold font */
3200 xw
.cw
= dc
.font
[0].rbearing
-dc
.font
[0].lbearing
;
3201 xw
.ch
= dc
.font
[0].ascent
+dc
.font
[0].descent
;
3202 xw
.tch
= dc
.font
[2].ascent
+dc
.font
[2].descent
;
3203 xw
.tabheight
= xw
.tch
+2;
3204 //fprintf(stderr, "tabh: %d\n", xw.tabheight);
3207 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
3209 /* window - default size */
3210 term
->picbufh
= term
->row
*xw
.ch
;
3211 term
->picbufw
= term
->col
*xw
.cw
;
3213 xw
.h
= term
->picbufh
+2*opt_border
+xw
.tabheight
;
3214 xw
.w
= term
->picbufw
+2*opt_border
;
3216 attrs
.background_pixel
= dc
.col
[defaultBG
];
3217 attrs
.border_pixel
= dc
.col
[defaultBG
];
3218 attrs
.bit_gravity
= NorthWestGravity
;
3219 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
3220 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
3221 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
3222 | EnterWindowMask
| LeaveWindowMask
;
3223 attrs
.colormap
= xw
.cmap
;
3224 //fprintf(stderr, "oe: [%s]\n", opt_embed);
3225 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
3226 xw
.win
= XCreateWindow(xw
.dpy
, parent
, 0, 0,
3227 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
3228 XDefaultVisual(xw
.dpy
, xw
.scr
),
3229 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
3232 term
->picbuf
= XCreatePixmap(xw
.dpy
, xw
.win
, term
->picbufw
, term
->picbufh
, XDefaultDepth(xw
.dpy
, xw
.scr
));
3233 xw
.pictab
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.tabheight
>0?xw
.tabheight
:1, XDefaultDepth(xw
.dpy
, xw
.scr
));
3235 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
3236 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
| XIMStatusNothing
, XNClientWindow
, xw
.win
, XNFocusWindow
, xw
.win
, NULL
);
3238 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
3239 /* white cursor, black outline */
3240 xw
.cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
3241 XDefineCursor(xw
.dpy
, xw
.win
, xw
.cursor
);
3242 XRecolorCursor(xw
.dpy
, xw
.cursor
,
3243 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
3244 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
3245 fixWindowTitle(term
);
3246 //XStoreName(xw.dpy, xw.win, opt_title);
3248 XSetForeground(xw
.dpy
, dc
.gc
, 0);
3249 XFillRectangle(xw
.dpy
, term
->picbuf
, dc
.gc
, 0, 0, term
->picbufw
, term
->picbufh
);
3250 if (xw
.tabheight
> 0) XFillRectangle(xw
.dpy
, xw
.pictab
, dc
.gc
, 0, 0, xw
.w
, xw
.tabheight
);
3252 XMapWindow(xw
.dpy
, xw
.win
);
3255 xw
.blankPtr
= XCreateGlyphCursor(xw
.dpy
, dc
.font
[0].fid
, dc
.font
[0].fid
, ' ', ' ', &blackcolor
, &blackcolor
);
3261 static void xblankPointer (void) {
3262 if (!ptrBlanked
&& xw
.blankPtr
!= None
) {
3264 XDefineCursor(xw
.dpy
, xw
.win
, xw
.blankPtr
);
3270 static void xunblankPointer (void) {
3271 if (ptrBlanked
&& xw
.cursor
!= None
) {
3273 XDefineCursor(xw
.dpy
, xw
.win
, xw
.cursor
);
3275 ptrLastMove
= mclock_ticks();
3280 static void xdraws (const char *s
, const Glyph
*base
, int x
, int y
, int charlen
, int bytelen
) {
3281 int fg
= base
->fg
, bg
= base
->bg
, temp
;
3282 int winx
= x
*xw
.cw
, winy
= y
*xw
.ch
+dc
.font
[0].ascent
, width
= charlen
*xw
.cw
;
3283 XFontSet fontset
= dc
.font
[0].set
;
3284 int defF
= base
->mode
&ATTR_DEFFG
, defB
= base
->mode
&ATTR_DEFBG
;
3286 /* only switch default fg/bg if term is in RV mode */
3287 if (IS_SET(MODE_REVERSE
)) {
3288 if (defF
) fg
= term
->defbg
;
3289 if (defB
) bg
= term
->deffg
;
3291 if (base
->mode
&ATTR_REVERSE
) defF
= defB
= 0;
3292 if (base
->mode
& ATTR_BOLD
) {
3293 if (defF
&& defB
&& defaultBoldFG
>= 0) fg
= defaultBoldFG
;
3294 else if (fg
< 8) fg
+= 8;
3295 fontset
= dc
.font
[1].set
;
3297 if (base
->mode
& ATTR_UNDERLINE
&& defaultUnderlineFG
>= 0) {
3298 if (defF
&& defB
) fg
= defaultUnderlineFG
;
3301 if (base
->mode
&ATTR_REVERSE
) { temp
= fg
; fg
= bg
; bg
= temp
; }
3303 XSetBackground(xw
.dpy
, dc
.gc
, dc
.col
[bg
]);
3304 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[fg
]);
3308 FILE *fo = fopen("zlog.log", "ab");
3309 fprintf(fo, "xdraws: x=%d; y=%d; charlen=%d; bytelen=%d\n", x, y, charlen, bytelen);
3314 if (IS_GFX(base
->mode
)) {
3315 XmbDrawImageString(xw
.dpy
, term
->picbuf
, fontset
, dc
.gc
, winx
, winy
, s
, bytelen
);
3316 } else if (!needConversion
) {
3317 XmbDrawImageString(xw
.dpy
, term
->picbuf
, fontset
, dc
.gc
, winx
, winy
, s
, bytelen
);
3320 //k8: dunno why, but Xutf8DrawImageString() ignores ascii (at least for terminus)
3321 const char *pos
= s
;
3324 while (pos
< s
+bytelen
) {
3328 if ((unsigned char)(pos
[0]) < 128) {
3329 for (e
= pos
+1; e
< s
+bytelen
&& (unsigned char)(*e
) < 128; ++e
) ;
3331 XmbDrawImageString(xw
.dpy
, term
->picbuf
, fontset
, dc
.gc
, xpos
, winy
, pos
, e
-pos
);
3334 FILE *fo = fopen("zlog.log", "ab");
3335 fprintf(fo, " norm: clen=%d (%d); [", clen, (int)(e-pos));
3336 fwrite(pos, 1, e-pos, fo);
3342 for (clen
= 0, e
= pos
; e
< s
+bytelen
&& (unsigned char)(*e
) >= 128; ++e
) {
3343 if (((unsigned char)(e
[0])&0xc0) == 0xc0) ++clen
;
3345 Xutf8DrawImageString(xw
.dpy
, term
->picbuf
, fontset
, dc
.gc
, xpos
, winy
, pos
, e
-pos
);
3348 FILE *fo = fopen("zlog.log", "ab");
3349 fprintf(fo, " utf8: clen=%d (%d); [", clen, (int)(e-pos));
3350 fwrite(pos, 1, e-pos, fo);
3362 if (base
->mode
& ATTR_UNDERLINE
) {
3363 XDrawLine(xw
.dpy
, term
->picbuf
, dc
.gc
, winx
, winy
+1, winx
+width
-1, winy
+1);
3368 /* copy buffer pixmap to screen pixmap */
3369 static void xcopy (int x
, int y
, int cols
, int rows
) {
3370 int src_x
= x
*xw
.cw
, src_y
= y
*xw
.ch
, src_w
= cols
*xw
.cw
, src_h
= rows
*xw
.ch
;
3371 int dst_x
= opt_border
+src_x
, dst_y
= opt_border
+src_y
;
3373 XCopyArea(xw
.dpy
, term
->picbuf
, xw
.win
, dc
.gc
, src_x
, src_y
, src_w
, src_h
, dst_x
, dst_y
);
3377 static void xdrawcursor (void) {
3381 if (term
== NULL
) return;
3383 LIMIT(term
->oldcx
, 0, term
->col
-1);
3384 LIMIT(term
->oldcy
, 0, term
->row
-1);
3387 scry
= term
->oldcy
+term
->topline
;
3388 if (scry
< term
->row
&&
3389 (term
->oldcy
!= term
->c
.y
|| term
->oldcx
!= term
->c
.x
|| (term
->c
.state
&CURSOR_HIDE
) || !(xw
.state
& WIN_FOCUSED
))) {
3390 /* remove the old cursor */
3391 sl
= utf8size(term
->line
[term
->oldcy
][scrx
].c
);
3392 xdraws(term
->line
[term
->oldcy
][scrx
].c
, &term
->line
[term
->oldcy
][scrx
], scrx
, scry
, 1, sl
);
3393 //xclear(scrx, term->oldcy, scrx, term->oldcy);
3394 xcopy(scrx
, scry
, 1, 1);
3396 /* draw the new one */
3397 if (!(term
->c
.state
&CURSOR_HIDE
)) {
3401 scry
= term
->c
.y
+term
->topline
;
3402 if (scry
< term
->row
) {
3403 if (!(xw
.state
& WIN_FOCUSED
)) {
3404 if (defaultCursorInactiveBG
< 0) {
3405 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultCursorBG
]);
3406 XDrawRectangle(xw
.dpy
, term
->picbuf
, dc
.gc
, scrx
*xw
.cw
, scry
*xw
.ch
, xw
.cw
-1, xw
.ch
-1);
3409 g
.bg
= defaultCursorInactiveBG
;
3410 g
.fg
= defaultCursorInactiveFG
;
3412 g
.fg
= defaultCursorFG
;
3413 g
.bg
= defaultCursorBG
;
3415 memcpy(g
.c
, term
->line
[term
->c
.y
][scrx
].c
, UTF_SIZ
);
3418 if (IS_SET(MODE_REVERSE
)) g
.mode
|= ATTR_REVERSE
;
3420 xdraws(g
.c
, &g
, scrx
, scry
, 1, sl
);
3422 term
->oldcy
= term
->c
.y
;
3425 xcopy(scrx
, scry
, 1, 1);
3430 #define TAB_VISIBLE (6)
3432 static void xdrawTabBar (void) {
3433 if (xw
.tabheight
> 0 && updateTabBar
) {
3434 static int tableft
= 0;
3436 int tabw
= xw
.w
/TAB_VISIBLE
;
3437 XFontSet fontset
= dc
.font
[2].set
;
3439 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[normalTabBG
]);
3440 XFillRectangle(xw
.dpy
, xw
.pictab
, dc
.gc
, 0, 0, xw
.w
, xw
.tabheight
);
3442 if (tableft
+TAB_VISIBLE
-1 > termidx
) tableft
= termidx
-TAB_VISIBLE
;
3443 else if (termidx
< tableft
) tableft
= termidx
;
3444 if (tableft
< 0) tableft
= 0;
3446 for (int f
= tabstart
; f
< TAB_VISIBLE
; ++f
) {
3447 int x
= (f
-tabstart
)*tabw
;;
3451 if (f
>= term_count
) {
3452 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[normalTabBG
]);
3453 XFillRectangle(xw
.dpy
, xw
.pictab
, dc
.gc
, x
, 0, xw
.w
, xw
.tabheight
);
3456 title
= term_array
[f
]->title
;
3457 if (!title
[0]) title
= opt_title
;
3458 tit
= SPrintf("[%d]%s", f
, title
);
3461 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[f
== termidx
? activeTabBG
: normalTabBG
]);
3462 XFillRectangle(xw
.dpy
, xw
.pictab
, dc
.gc
, x
, 0, tabw
, xw
.tabheight
);
3464 XSetBackground(xw
.dpy
, dc
.gc
, dc
.col
[f
== termidx
? activeTabBG
: normalTabBG
]);
3465 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[f
== termidx
? activeTabFG
: normalTabFG
]);
3467 if (needConversion
) {
3470 while (*title
&& xx
< x
+tabw
) {
3471 const char *e
= title
;
3474 memset(&r
, 0, sizeof(r
));
3476 if ((unsigned char)(*e
) > 127) {
3477 while (*e
&& (unsigned char)(*e
) > 127) ++e
;
3478 Xutf8DrawImageString(xw
.dpy
, xw
.pictab
, fontset
, dc
.gc
, xx
, dc
.font
[2].ascent
+2, title
, e
-title
);
3479 Xutf8TextExtents(fontset
, title
, e
-title
, &r
, NULL
);
3481 while (*e
&& (unsigned char)(*e
) <= 127) ++e
;
3482 XmbDrawImageString(xw
.dpy
, xw
.pictab
, fontset
, dc
.gc
, xx
, dc
.font
[2].ascent
+2, title
, e
-title
);
3483 XmbTextExtents(fontset
, title
, e
-title
, &r
, NULL
);
3489 XmbDrawImageString(xw
.dpy
, xw
.pictab
, fontset
, dc
.gc
, x
+2, dc
.font
[2].ascent
+2, title
, strlen(title
));
3491 Xutf8DrawImageString(xw
.dpy
, xw
.pictab
, fontset
, dc
.gc
, x
+2, dc
.font
[2].ascent
+2, title
, strlen(title
));
3495 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[f
== termidx
? activeTabBG
: normalTabBG
]);
3496 XFillRectangle(xw
.dpy
, xw
.pictab
, dc
.gc
, x
+tabw
-2, 0, 2, xw
.tabheight
);
3498 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[normalTabFG
]);
3499 XDrawLine(xw
.dpy
, xw
.pictab
, dc
.gc
, x
+tabw
-1, 0, x
+tabw
-1, xw
.tabheight
);
3502 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[normalTabFG
]);
3503 //XDrawRectangle(xw.dpy, xw.pictab, dc.gc, -1, 0, xw.w+1, xw.tabheight+1);
3504 XDrawLine(xw
.dpy
, xw
.pictab
, dc
.gc
, 0, 0, xw
.w
, 0);
3506 XCopyArea(xw
.dpy
, xw
.pictab
, xw
.win
, dc
.gc
, 0, 0, xw
.w
, xw
.tabheight
, 0, xw
.h
-xw
.tabheight
);
3511 static void drawline (int x1
, int x2
, int scry
, int lineno
) {
3516 //dlogf("drawline: x1=%d; x2=%d; scry=%d; row:%d; lineno=%d\n", x1, x2, scry, term->row, lineno);
3517 if (scry
< 0 || scry
>= term
->row
) {
3520 if (lineno
< 0 || lineno
>= term
->linecount
) {
3521 xclear(0, scry
, term
->col
-1, scry
);
3522 xcopy(0, scry
, term
->col
, 1);
3524 if (lineno
< term
->row
&& term
->topline
== 0) {
3525 //if (term->topline != 0) term->dirty[lineno] = 2;
3526 if (!term
->dirty
[lineno
]) return;
3527 // fix 'dirty' flag for line
3528 if (term
->dirty
[lineno
]&0x02) {
3529 // mark full line as dirty
3530 for (int x
= 0; x
< term
->col
; ++x
) term
->line
[lineno
][x
].state
|= GLYPH_DIRTY
;
3532 // correct 'dirty' flag
3533 term
->dirty
[lineno
] = 0;
3534 if (x1
> 0) for (int x
= 0; x
< x1
; ++x
) if (term
->line
[lineno
][x
].state
&GLYPH_DIRTY
) { term
->dirty
[lineno
] = 1; break; }
3535 if (!term
->dirty
[lineno
] && x2
< term
->col
) for (int x
= x2
; x
< term
->col
; ++x
) if (term
->line
[lineno
][x
].state
&GLYPH_DIRTY
) { term
->dirty
[lineno
] = 1; break; }
3537 // find dirty region
3538 for (stx
= x1
; stx
< x2
; ++stx
) if (term
->line
[lineno
][stx
].state
&GLYPH_DIRTY
) break;
3539 for (ex
= x2
; ex
> stx
; --ex
) if (term
->line
[lineno
][ex
-1].state
&GLYPH_DIRTY
) break;
3540 if (stx
>= x2
|| ex
<= stx
) return; // nothing to do
3541 //dlogf(" region: (%d,%d)\n", stx, ex);
3543 term
->dirty
[lineno
] = 0;
3548 base
= term
->line
[lineno
][stx
];
3551 //xclear(stx, scry, ex-1, scry); //k8: actually, we don't need this for good monospace fonts, so...
3552 for (int x
= stx
; x
< ex
; ++x
) {
3553 new = term
->line
[lineno
][x
];
3554 term
->line
[lineno
][x
].state
&= ~GLYPH_DIRTY
; //!
3555 if (term
->sel
.bx
!= -1 && new.c
[0] && selected(x
, lineno
)) new.mode
^= ATTR_REVERSE
;
3556 if (ib
> 0 && (ATTRCMP(base
, new) || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3557 // flush draw buffer
3558 xdraws(term
->drawbuf
, &base
, ox
, scry
, ic
, ib
);
3561 if (ib
== 0) { ox
= x
; base
= new; }
3562 sl
= utf8size(new.c
);
3563 memcpy(term
->drawbuf
+ib
, new.c
, sl
);
3567 if (ib
> 0) xdraws(term
->drawbuf
, &base
, ox
, scry
, ic
, ib
);
3568 //xcopy(0, scry, term->col, 1);
3569 if (term
->c
.y
== lineno
&& term
->c
.x
>= stx
&& term
->c
.x
< ex
) xdrawcursor();
3570 xcopy(stx
, scry
, ex
-stx
, 1);
3575 static void drawregion (int x1
, int y1
, int x2
, int y2
, int forced
) {
3576 if (!forced
&& (xw
.state
&WIN_VISIBLE
) == 0) {
3577 //dlogf("invisible");
3578 term
->lastDrawTime
= 1;
3579 term
->wantRedraw
= 1;
3583 if (term
->topline
< term
->row
) {
3584 for (int y
= y1
; y
< y2
; ++y
) drawline(x1
, x2
, y
+term
->topline
, y
);
3586 if (term
->topline
> 0) {
3587 int scry
= MIN(term
->topline
, term
->row
), y
= term
->row
;
3589 if (term
->topline
>= term
->row
) y
+= term
->topline
-term
->row
;
3590 while (--scry
>= 0) {
3591 drawline(0, term
->col
, scry
, y
);
3598 term
->lastDrawTime
= mclock_ticks();
3599 term
->wantRedraw
= 0;
3603 static void draw (int forced
) {
3605 //dlogf("draw(%d)\n", forced);
3606 drawregion(0, 0, term
->col
, term
->row
, forced
);
3611 static void expose (XEvent
*ev
) {
3612 XExposeEvent
*e
= &ev
->xexpose
;
3614 if (xw
.state
&WIN_REDRAW
) {
3615 if (!e
->count
&& term
!= NULL
) {
3616 xw
.state
&= ~WIN_REDRAW
;
3617 xcopy(0, 0, term
->col
, term
->row
);
3619 } else if (term
!= NULL
) {
3620 XCopyArea(xw
.dpy
, term
->picbuf
, xw
.win
, dc
.gc
, e
->x
-opt_border
, e
->y
-opt_border
, e
->width
, e
->height
, e
->x
, e
->y
);
3627 static void visibility (XEvent
*ev
) {
3628 XVisibilityEvent
*e
= &ev
->xvisibility
;
3630 if (e
->state
== VisibilityFullyObscured
) xw
.state
&= ~WIN_VISIBLE
;
3631 else if ((xw
.state
&WIN_VISIBLE
) == 0) xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
; /* need a full redraw for next Expose, not just a buf copy */
3635 static void unmap (XEvent
*ev
) {
3636 xw
.state
&= ~WIN_VISIBLE
;
3640 static void xseturgency (int add
) {
3641 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3643 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3644 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3649 static void focus (XEvent
*ev
) {
3650 if (ev
->type
== FocusIn
) {
3651 xw
.state
|= WIN_FOCUSED
;
3654 xw
.state
&= ~WIN_FOCUSED
;
3659 xcopy(0, 0, term
->col
, term
->row
);
3663 ////////////////////////////////////////////////////////////////////////////////
3665 static const char *kmap (KeySym k
, uint state
) {
3666 const char *res
= NULL
;
3667 state
&= ~Mod2Mask
; // numlock
3668 for (int f
= 0; f
< keymap_used
; ++f
) {
3669 uint mask
= keymap
[f
].mask
;
3671 if (keymap
[f
].key
== k
&& ((state
&mask
) == mask
|| (mask
== XK_NO_MOD
&& !state
))) {
3672 //fprintf(stderr, "kp: %d\n", IS_SET(MODE_APPKEYPAD));
3673 if (!IS_SET(MODE_APPKEYPAD
)) {
3674 if (!keymap
[f
].kp
) {
3675 //fprintf(stderr, "nokp! %d %s\n", keymap[f].kp, keymap[f].str+1);
3676 return keymap
[f
].str
; // non-keypad hit
3680 //fprintf(stderr, "kp! %d %s\n", keymap[f].kp, keymap[f].str+1);
3681 if (keymap
[f
].kp
) return keymap
[f
].str
; // keypad hit
3682 res
= keymap
[f
].str
; // kp mode, but non-kp mapping found
3689 static const char *kbind (KeySym k
, uint state
) {
3690 state
&= ~Mod2Mask
; // numlock
3691 for (int f
= 0; f
< keybinds_used
; ++f
) {
3692 uint mask
= keybinds
[f
].mask
;
3694 if (keybinds
[f
].key
== k
&& ((state
&mask
) == mask
|| (mask
== XK_NO_MOD
&& !state
))) return keybinds
[f
].str
;
3700 static KeySym
do_keytrans (KeySym ks
) {
3701 for (int f
= 0; f
< keytrans_used
; ++f
) if (keytrans
[f
].src
== ks
) return keytrans
[f
].dst
;
3706 static void kpress (XEvent
*ev
) {
3707 XKeyEvent
*e
= &ev
->xkey
;
3708 KeySym ksym
= NoSymbol
;
3714 if (term
== NULL
) return;
3715 //if (len < 1) len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
3716 if ((len
= Xutf8LookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
)) > 0) buf
[len
] = 0;
3717 // leave only known mods
3718 e
->state
&= (Mod1Mask
| Mod4Mask
| ControlMask
| ShiftMask
);
3721 const char *ksname
= XKeysymToString(ksym
);
3723 fprintf(stderr
, "utf(%d):[%s] (%s) 0x%08x\n", len
, len
>=0?buf
:"<shit>", ksname
, (unsigned int)e
->state
);
3726 if ((kstr
= kbind(ksym
, e
->state
)) != NULL
) {
3728 if (term
->topline
!= 0) {
3730 term
->wantRedraw
= 0;
3731 term
->lastDrawTime
= 0;
3733 executeCommands(kstr
);
3737 if ((kstr
= kmap(do_keytrans(ksym
), e
->state
)) != NULL
) {
3738 if (kstr
[0]) ttywritestr(kstr
);
3740 int meta
= (e
->state
&Mod1Mask
);
3742 int shift = (e->state&ShiftMask);
3743 int ctrl = (e->state&ControlMask);
3748 ttywritestr("\x1b\x0a");
3750 if (IS_SET(MODE_CRLF
)) ttywritestr("\r\n"); else ttywritestr("\r");
3755 if (meta
&& len
== 1) ttywritestr("\x1b");
3764 ////////////////////////////////////////////////////////////////////////////////
3766 static void cmessage (XEvent
*e
) {
3767 /* See xembed specs http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
3768 if (e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3769 if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3770 xw
.state
|= WIN_FOCUSED
;
3772 } else if (e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3773 xw
.state
&= ~WIN_FOCUSED
;
3780 ////////////////////////////////////////////////////////////////////////////////
3781 static void resize (XEvent
*e
) {
3785 if (e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
) return;
3786 xw
.w
= e
->xconfigure
.width
;
3787 xw
.h
= e
->xconfigure
.height
;
3788 col
= (xw
.w
-2*opt_border
)/xw
.cw
;
3789 row
= (xw
.h
-2*opt_border
-xw
.tabheight
)/xw
.ch
;
3790 if (col
== term
->col
&& row
== term
->row
) return;
3791 for (int f
= 0; f
< term_count
; ++f
) {
3792 term
= term_array
[f
];
3793 if (tresize(col
, row
) && ot
== term
) draw(1);
3798 XFreePixmap(xw
.dpy
, xw
.pictab
);
3799 xw
.pictab
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.tabheight
>0?xw
.tabheight
:1, XDefaultDepth(xw
.dpy
, xw
.scr
));
3804 static inline int last_draw_too_old (void) {
3805 return (mclock_ticks()-term
->lastDrawTime
>= DRAW_TIMEOUT
);
3809 ////////////////////////////////////////////////////////////////////////////////
3811 static void (*handler
[LASTEvent
])(XEvent
*) = {
3812 [KeyPress
] = kpress
,
3813 [ClientMessage
] = cmessage
,
3814 [ConfigureNotify
] = resize
,
3815 [VisibilityNotify
] = visibility
,
3816 [UnmapNotify
] = unmap
,
3820 [MotionNotify
] = bmotion
,
3821 [ButtonPress
] = bpress
,
3822 [ButtonRelease
] = brelease
,
3823 [SelectionNotify
] = selnotify
,
3824 [SelectionRequest
] = selrequest
,
3825 [SelectionClear
] = selclear
,
3829 static void run (void) {
3830 //int stuff_to_print = 0;
3831 int xfd
= XConnectionNumber(xw
.dpy
);
3833 ptrLastMove
= mclock_ticks();
3834 while (term_count
> 0) {
3837 struct timeval timeout
;
3844 //FD_SET(term->cmdfd, &rfd);
3845 // have something to write?
3846 for (int f
= 0; f
< term_count
; ++f
) {
3847 Term
*t
= term_array
[f
];
3849 if (!t
->dead
&& term
->cmdfd
>= 0 && t
->pid
!= 0) {
3850 if (t
->cmdfd
> maxfd
) maxfd
= t
->cmdfd
;
3851 FD_SET(t
->cmdfd
, &rfd
);
3852 if (t
->wrbufpos
< t
->wrbufused
) FD_SET(t
->cmdfd
, &wfd
);
3857 timeout
.tv_usec
= SELECT_TIMEOUT
*1000;
3858 if (select(maxfd
+1, &rfd
, &wfd
, NULL
, &timeout
) < 0) {
3859 if (errno
== EINTR
) continue;
3860 die("select failed: %s", SERRNO
);
3864 for (int f
= 0; f
< term_count
; ++f
) {
3865 Term
*t
= term_array
[f
];
3867 if (!t
->dead
&& term
->cmdfd
>= 0 && term
->pid
!= 0) {
3869 if (FD_ISSET(t
->cmdfd
, &wfd
)) ttyflushwrbuf();
3870 if (FD_ISSET(t
->cmdfd
, &rfd
)) ttyread(); //t->wantRedraw = 1;
3876 if (term_count
== 0) break;
3878 if (updateTabBar
|| (term
!= NULL
&& !term
->dead
&& term
->wantRedraw
&& last_draw_too_old())) {
3882 if (XPending(xw
.dpy
)) {
3883 while (XPending(xw
.dpy
)) {
3884 XNextEvent(xw
.dpy
, &ev
);
3885 if (XFilterEvent(&ev
, xw
.win
)) continue;
3886 if (handler
[ev
.type
]) (handler
[ev
.type
])(&ev
);
3890 if (opt_ptrblank
> 0 && mclock_ticks()-ptrLastMove
>= opt_ptrblank
) {
3897 ////////////////////////////////////////////////////////////////////////////////
3898 typedef const char * (*IniHandlerFn
) (const char *optname
, const char *fmt
, char *argstr
, void *udata
);
3908 static const char *inifnGenericOneArg (const char *optname
, const char *fmt
, char *argstr
, void *udata
) {
3909 return iniParseArguments(argstr
, fmt
, udata
);
3913 static const char *inifnGenericOneStr (const char *optname
, const char *fmt
, char *argstr
, void *udata
) {
3915 const char *err
= iniParseArguments(argstr
, fmt
, &s
);
3917 if (err
!= NULL
) return err
;
3918 if ((s
= strdup(s
)) == NULL
) return "out of memory";
3920 char **ustr
= (char **)udata
;
3922 if (*ustr
) free(*ustr
);
3929 static const IniCommand iniCommands
[] = {
3930 {"term", "s!-", &opt_term
, inifnGenericOneStr
},
3931 {"class", "s!-", &opt_class
, inifnGenericOneStr
},
3932 {"title", "s!-", &opt_title
, inifnGenericOneStr
},
3933 {"fontnorm", "s!-", &opt_fontnorm
, inifnGenericOneStr
},
3934 {"fontbold", "s!-", &opt_fontbold
, inifnGenericOneStr
},
3935 {"fonttab", "s!-", &opt_fonttab
, inifnGenericOneStr
},
3936 {"shell", "s!-", &opt_shell
, inifnGenericOneStr
},
3937 {"doubleclick_timeout", "i{0,10000}", &opt_doubleclick_timeout
, inifnGenericOneArg
},
3938 {"tripleclick_timeout", "i{0,10000}", &opt_tripleclick_timeout
, inifnGenericOneArg
},
3939 {"tabsize", "i{1,256}", &opt_tabsize
, inifnGenericOneArg
},
3940 {"border", "i{0,256}", &opt_border
, inifnGenericOneArg
},
3941 {"defaultfg", "i{0,511}", &defaultFG
, inifnGenericOneArg
},
3942 {"defaultbg", "i{0,511}", &defaultBG
, inifnGenericOneArg
},
3943 {"defaultcursorfg", "i{0,511}", &defaultCursorFG
, inifnGenericOneArg
},
3944 {"defaultcursorbg", "i{0,511}", &defaultCursorBG
, inifnGenericOneArg
},
3945 {"defaultinactivecursorfg", "i{0,511}", &defaultCursorInactiveFG
, inifnGenericOneArg
},
3946 {"defaultinactivecursorbg", "i{-1,511}", &defaultCursorInactiveBG
, inifnGenericOneArg
},
3947 {"defaultboldfg", "i{-1,511}", &defaultBoldFG
, inifnGenericOneArg
},
3948 {"defaultunderlinefg", "i{-1,511}", &defaultUnderlineFG
, inifnGenericOneArg
},
3949 {"normaltabfg", "i{0,511}", &normalTabFG
, inifnGenericOneArg
},
3950 {"normaltabbg", "i{0,511}", &normalTabBG
, inifnGenericOneArg
},
3951 {"activetabfg", "i{0,511}", &activeTabFG
, inifnGenericOneArg
},
3952 {"activetabbg", "i{0,511}", &activeTabBG
, inifnGenericOneArg
},
3953 {"maxhistory", "i{0,65535}", &opt_maxhistory
, inifnGenericOneArg
},
3954 {"ptrblank", "i{0,65535}", &opt_ptrblank
, inifnGenericOneArg
},
3955 {NULL
, NULL
, NULL
, NULL
}
3959 #define INI_LINE_SIZE (32768)
3961 // <0: file not found
3962 // >0: file loading error
3964 static int loadConfig (const char *fname
) {
3965 int inifelse
= 0; // 0: not; 1: doing true; 2: doing false; -1: waiting else/endif; -2: waiting endif
3966 FILE *fi
= fopen(fname
, "r");
3967 const char *err
= NULL
;
3971 if (fi
== NULL
) return -1;
3972 if ((line
= malloc(INI_LINE_SIZE
)) == NULL
) { err
= "out of memory"; goto quit
; }
3974 while (fgets(line
, INI_LINE_SIZE
-1, fi
) != NULL
) {
3975 char *optname
, *argstr
;
3979 line
[INI_LINE_SIZE
-1] = 0;
3981 for (optname
= line
; *optname
&& isspace(*optname
); ++optname
) ;
3982 if (!optname
[0] || optname
[0] == '#') continue; // comment
3983 if (!isalnum(optname
[0])) { err
= "invalid option name"; goto quit
; }
3986 if (!argstr
[0] || isspace(argstr
[0])) break;
3987 if (!isalnum(argstr
[0]) && argstr
[0] != '_' && argstr
[0] != '.') { err
= "invalid option name"; goto quit
; }
3988 *argstr
= tolower(*argstr
);
3991 if (*argstr
) *argstr
++ = 0;
3993 if (strcasecmp(optname
, "ifterm") == 0) {
3996 if (inifelse
!= 0) { err
= "nested ifs are not allowed"; goto quit
; }
3998 if ((err
= iniParseArguments(argstr
, "s", &val
)) != NULL
) goto quit
;
3999 if (strcasecmp(opt_term
, val
) == 0) inifelse
= 1;
4002 if (strcasecmp(optname
, "else") == 0) {
4004 case -1: inifelse
= 2; break;
4005 case 2: case -2: case 0: err
= "else without if"; goto quit
;
4006 case 1: inifelse
= -2; break;
4010 if (strcasecmp(optname
, "endif") == 0) {
4012 case -1: case -2: case 1: case 2: inifelse
= 0; break;
4013 case 0: err
= "endif without if"; goto quit
;
4020 //fprintf(stderr, "skip: [%s]\n", argstr);
4023 if (opt_term_locked
&& strcasecmp(optname
, "term") == 0) continue; // termname given in command line
4024 // ok, we have option name in `optname` and arguments in `argstr`
4025 if (strncmp(optname
, "color.", 6) == 0) {
4030 if (!optname
[0]) { err
= "invalid color option"; goto quit
; }
4032 if (!isdigit(*optname
)) { err
= "invalid color option"; goto quit
; }
4033 n
= (n
*10)+(optname
[0]-'0');
4036 if (n
< 0 || n
> 511) { err
= "invalid color index"; goto quit
; }
4038 if ((err
= iniParseArguments(argstr
, "s!-", &s
)) != NULL
) goto quit
;
4039 if ((s
= strdup(s
)) == NULL
) { err
= "out of memory"; goto quit
; }
4040 if (opt_colornames
[n
] != NULL
) free(opt_colornames
[n
]);
4041 opt_colornames
[n
] = s
;
4045 if (strcmp(optname
, "unimap") == 0) {
4049 //unimap 0x2592 0x61 alt
4050 if ((err
= iniParseArguments(argstr
, "i{0,65535}i{0,126}|s!-", &uni
, &ch
, &alt
)) != NULL
) goto quit
;
4051 if (alt
!= NULL
&& strcasecmp(alt
, "alt") != 0) { err
= "invalid unimap"; goto quit
; }
4052 if (unimap
== NULL
) {
4053 if ((unimap
= calloc(65536, sizeof(unimap
[0]))) == NULL
) { err
= "out of memory"; goto quit
; }
4055 if (alt
!= NULL
&& ch
== 0) alt
= NULL
;
4056 if (alt
!= NULL
&& ch
< 96) { err
= "invalid unimap"; goto quit
; }
4058 if (alt
!= NULL
) unimap
[uni
] |= 0x80;
4062 if (strcmp(optname
, "keytrans_reset") == 0) {
4063 if ((err
= iniParseArguments(argstr
, "")) != NULL
) goto quit
;
4067 if (strcmp(optname
, "keytrans") == 0) {
4068 char *src
= NULL
, *dst
= NULL
;
4070 if ((err
= iniParseArguments(argstr
, "s!-s!-", &src
, &dst
)) != NULL
) goto quit
;
4071 keytrans_add(src
, dst
);
4075 if (strcmp(optname
, "keybind_reset") == 0) {
4076 if ((err
= iniParseArguments(argstr
, "")) != NULL
) goto quit
;
4080 if (strcmp(optname
, "keybind") == 0) {
4081 char *key
= NULL
, *act
= NULL
;
4083 if ((err
= iniParseArguments(argstr
, "s!-R!", &key
, &act
)) != NULL
) goto quit
;
4084 keybind_add(key
, act
);
4088 if (strcmp(optname
, "keymap_reset") == 0) {
4089 if ((err
= iniParseArguments(argstr
, "")) != NULL
) goto quit
;
4093 if (strcmp(optname
, "keymap") == 0) {
4094 char *key
= NULL
, *str
= NULL
;
4096 if ((err
= iniParseArguments(argstr
, "s!-s!-", &key
, &str
)) != NULL
) goto quit
;
4097 keymap_add(key
, str
);
4101 for (int f
= 0; iniCommands
[f
].name
!= NULL
; ++f
) {
4102 if (strcmp(iniCommands
[f
].name
, optname
) == 0) {
4103 if ((err
= iniCommands
[f
].fn(optname
, iniCommands
[f
].fmt
, argstr
, iniCommands
[f
].udata
)) != NULL
) goto quit
;
4108 if (!goodoption
) { err
= "unknown option"; goto quit
; }
4111 if (line
!= NULL
) free(line
);
4113 if (err
== NULL
&& inifelse
!= 0) err
= "if without endif";
4114 if (err
!= NULL
) die("ini error at line %d: %s", lineno
, err
);
4119 static void initDefaultOptions (void) {
4120 opt_title
= strdup("sterm");
4121 opt_class
= strdup("sterm");
4122 opt_term
= strdup(TNAME
);
4123 opt_fontnorm
= strdup(FONT
);
4124 opt_fontbold
= strdup(BOLDFONT
);
4125 opt_fonttab
= strdup(FONTTAB
);
4126 opt_shell
= strdup(SHELL
);
4128 memset(opt_colornames
, 0, sizeof(opt_colornames
));
4129 for (int f
= 0; f
< LEN(defcolornames
); ++f
) opt_colornames
[f
] = strdup(defcolornames
[f
]);
4130 for (int f
= 0; f
< LEN(defextcolornames
); ++f
) opt_colornames
[f
+256] = strdup(defextcolornames
[f
]);
4132 keytrans_add("KP_Home", "Home");
4133 keytrans_add("KP_Left", "Left");
4134 keytrans_add("KP_Up", "Up");
4135 keytrans_add("KP_Right", "Right");
4136 keytrans_add("KP_Down", "Down");
4137 keytrans_add("KP_Prior", "Prior");
4138 keytrans_add("KP_Next", "Next");
4139 keytrans_add("KP_End", "End");
4140 keytrans_add("KP_Begin", "Begin");
4141 keytrans_add("KP_Insert", "Insert");
4142 keytrans_add("KP_Delete", "Delete");
4144 keybind_add("shift+Insert", "PastePrimary");
4145 keybind_add("alt+Insert", "PasteSecondary");
4147 keymap_add("BackSpace", "\177");
4148 keymap_add("Insert", "\x1b[2~");
4149 keymap_add("Delete", "\x1b[3~");
4150 keymap_add("Home", "\x1b[1~");
4151 keymap_add("End", "\x1b[4~");
4152 keymap_add("Prior", "\x1b[5~");
4153 keymap_add("Next", "\x1b[6~");
4154 keymap_add("F1", "\x1bOP");
4155 keymap_add("F2", "\x1bOQ");
4156 keymap_add("F3", "\x1bOR");
4157 keymap_add("F4", "\x1bOS");
4158 keymap_add("F5", "\x1b[15~");
4159 keymap_add("F6", "\x1b[17~");
4160 keymap_add("F7", "\x1b[18~");
4161 keymap_add("F8", "\x1b[19~");
4162 keymap_add("F9", "\x1b[20~");
4163 keymap_add("F10", "\x1b[21~");
4164 keymap_add("Up", "\x1bOA");
4165 keymap_add("Down", "\x1bOB");
4166 keymap_add("Right", "\x1bOC");
4167 keymap_add("Left", "\x1bOD");
4168 keymap_add("kpad+Up", "\x1bOA");
4169 keymap_add("kpad+Down", "\x1bOB");
4170 keymap_add("kpad+Right", "\x1bOC");
4171 keymap_add("kpad+Left", "\x1bOD");
4175 ////////////////////////////////////////////////////////////////////////////////
4176 static Term
*oldTerm
;
4177 static int oldTermIdx
;
4178 static Term
*newTerm
;
4179 static int newTermIdx
;
4180 static int newTermSwitch
;
4183 typedef void (*CmdHandlerFn
) (const char *cmdname
, char *argstr
);
4191 static void cmdPastePrimary (const char *cmdname
, char *argstr
) {
4192 selpaste(XA_PRIMARY
);
4196 static void cmdPasteSecondary (const char *cmdname
, char *argstr
) {
4197 selpaste(XA_SECONDARY
);
4201 static void cmdPasteClipboard (const char *cmdname
, char *argstr
) {
4202 selpaste(XA_CLIPBOARD
);
4206 static void cmdExec (const char *cmdname
, char *argstr
) {
4207 if (term
->execcmd
!= NULL
) free(term
->execcmd
);
4209 term
->execcmd
= strdup(argstr
);
4211 term
->execcmd
= NULL
;
4216 static int parseTabArgs (char *argstr
, int *noswitch
, int nowrap
, int idx
) {
4220 while (*argstr
&& isspace(*argstr
)) ++argstr
;
4221 if (!argstr
[0]) break;
4222 if (iniParseArguments(argstr
, "s-R-", &arg
, &argstr
) != NULL
) break;
4224 if (strcasecmp(arg
, "noswitch") == 0) *noswitch
= 1;
4225 else if (strcasecmp(arg
, "switch") == 0) *noswitch
= 0;
4226 else if (strcasecmp(arg
, "nowrap") == 0) nowrap
= 1;
4227 else if (strcasecmp(arg
, "wrap") == 0) nowrap
= 0;
4228 else if (strcasecmp(arg
, "first") == 0) idx
= 0;
4229 else if (strcasecmp(arg
, "last") == 0) idx
= term_count
-1;
4230 else if (strcasecmp(arg
, "prev") == 0) idx
= -1;
4231 else if (strcasecmp(arg
, "next") == 0) idx
= -2;
4236 n
= strtol(arg
, &eptr
, 0);
4237 if (!eptr
[0] && n
>= 0 && n
< term_count
) idx
= n
;
4242 if ((idx
= termidx
-1) < 0) idx
= nowrap
? 0 : term_count
-1;
4245 if ((idx
= termidx
+1) >= term_count
) idx
= nowrap
? term_count
-1 : 0;
4252 static void flushNewTerm (void) {
4253 if (newTerm
!= NULL
) {
4255 termidx
= newTermIdx
;
4256 tinitialize(term_array
[0]->col
, term_array
[0]->row
);
4258 term
->picbufh
= term
->row
*xw
.ch
;
4259 term
->picbufw
= term
->col
*xw
.cw
;
4260 term
->picbuf
= XCreatePixmap(xw
.dpy
, xw
.win
, term
->picbufw
, term
->picbufh
, XDefaultDepth(xw
.dpy
, xw
.scr
));
4261 XFillRectangle(xw
.dpy
, term
->picbuf
, dc
.gc
, 0, 0, term
->picbufw
, term
->picbufh
);
4263 if (ttynew(term
) != 0) {
4265 termidx
= oldTermIdx
;
4266 termfree(newTermIdx
);
4270 if (newTermSwitch
) {
4273 switchToTerm(newTermIdx
, 1);
4275 oldTermIdx
= termidx
;
4278 termidx
= oldTermIdx
;
4286 static void cmdNewTab (const char *cmdname
, char *argstr
) {
4287 int noswitch
= 0, idx
;
4290 if ((newTerm
= termalloc()) == NULL
) return;
4291 /*idx =*/ parseTabArgs(argstr
, &noswitch
, 0, termidx
);
4293 if (!noswitch
) oldTermIdx
= idx
;
4294 newTermIdx
= termidx
= idx
;
4296 newTermSwitch
= !noswitch
;
4300 static void cmdCloseTab (const char *cmdname
, char *argstr
) {
4302 if (!term
->dead
) kill(term
->pid
, SIGTERM
);
4306 static void cmdKillTab (const char *cmdname
, char *argstr
) {
4308 if (!term
->dead
) kill(term
->pid
, SIGKILL
);
4312 static void cmdSwitchToTab (const char *cmdname
, char *argstr
) {
4316 switchToTerm(parseTabArgs(argstr
, &noswitch
, 0, -2), 1);
4318 oldTermIdx
= termidx
;
4322 static void cmdMoveTabTo (const char *cmdname
, char *argstr
) {
4323 int noswitch
= 0, idx
;
4326 idx
= parseTabArgs(argstr
, &noswitch
, 0, termidx
);
4327 if (idx
!= termidx
&& idx
>= 0 && idx
< term_count
) {
4328 Term
*t
= term_array
[termidx
];
4330 // remove current term
4331 for (int f
= termidx
+1; f
< term_count
; ++f
) term_array
[f
-1] = term_array
[f
];
4333 for (int f
= term_count
-2; f
>= idx
; --f
) term_array
[f
+1] = term_array
[f
];
4334 term_array
[idx
] = t
;
4343 static void cmdDefaultFG (const char *cmdname
, char *argstr
) {
4346 if (iniParseArguments(argstr
, "i{0,511}", &c
) == NULL
) term
->deffg
= c
;
4350 static void cmdDefaultBG (const char *cmdname
, char *argstr
) {
4353 if (iniParseArguments(argstr
, "i{0,511}", &c
) == NULL
) term
->defbg
= c
;
4357 static void scrollHistory (int delta
) {
4358 if (term
->maxhistory
< 1) return; // no history
4359 term
->topline
+= delta
;
4360 if (term
->topline
> term
->maxhistory
-term
->row
) term
->topline
= term
->maxhistory
-term
->row
;
4361 if (term
->topline
< 0) term
->topline
= 0;
4367 static void cmdScrollHistoryLineUp (const char *cmdname
, char *argstr
) {
4372 static void cmdScrollHistoryPageUp (const char *cmdname
, char *argstr
) {
4373 scrollHistory(term
->row
);
4377 static void cmdScrollHistoryLineDown (const char *cmdname
, char *argstr
) {
4382 static void cmdScrollHistoryPageDown (const char *cmdname
, char *argstr
) {
4383 scrollHistory(-term
->row
);
4387 static void cmdScrollHistoryTop (const char *cmdname
, char *argstr
) {
4388 scrollHistory(term
->linecount
);
4392 static void cmdScrollHistoryBottom (const char *cmdname
, char *argstr
) {
4393 scrollHistory(-term
->linecount
);
4397 static const Command commandList
[] = {
4398 {"PastePrimary", cmdPastePrimary
},
4399 {"PasteSecondary", cmdPasteSecondary
},
4400 {"PasteClipboard", cmdPasteClipboard
},
4402 {"NewTab", cmdNewTab
}, // 'noswitch' 'next' 'prev' 'first' 'last'
4403 {"CloseTab", cmdCloseTab
},
4404 {"KillTab", cmdKillTab
},
4405 {"SwitchToTab", cmdSwitchToTab
}, // 'next' 'prev' 'first' 'last' 'nowrap' 'wrap'
4406 {"MoveTabTo", cmdMoveTabTo
}, // 'next' 'prev' 'first' 'last' 'nowrap' 'wrap'
4407 {"defaultfg", cmdDefaultFG
},
4408 {"defaultbg", cmdDefaultBG
},
4409 {"ScrollHistoryLineUp", cmdScrollHistoryLineUp
},
4410 {"ScrollHistoryPageUp", cmdScrollHistoryPageUp
},
4411 {"ScrollHistoryLineDown", cmdScrollHistoryLineDown
},
4412 {"ScrollHistoryPageDown", cmdScrollHistoryPageDown
},
4413 {"ScrollHistoryTop", cmdScrollHistoryTop
},
4414 {"ScrollHistoryBottom", cmdScrollHistoryBottom
},
4416 {"term", cmdTermName},
4417 {"title", cmdWinTitle},
4418 {"tabsize", cmdTabSize},
4419 {"defaultcursorfg", cmdDefaultCursorFG},
4420 {"defaultcursorbg", cmdDefaultCursorBG},
4421 {"defaultinactivecursorfg", cmdDefaultInactiveCursorFG},
4422 {"defaultinactivecursorbg", cmdDefaultInactiveCursorBG},
4423 {"defaultboldfg", cmdDefaultBoldFG},
4424 {"defaultunderlinefg", cmdDefaultUnderlineFG},
4430 static void executeCommand (const char *str
, int slen
) {
4434 if (str
== NULL
) return;
4435 if (slen
< 0) slen
= strlen(str
);
4437 for (int f
= 0; f
< slen
; ++f
) if (!str
[f
]) { slen
= f
; break; }
4439 while (slen
> 0 && isspace(*str
)) { ++str
; --slen
; }
4440 if (slen
< 1 || !str
[0]) return;
4442 for (e
= str
; slen
> 0 && !isspace(*e
); ++e
, --slen
) ;
4444 if (e
-str
> 127) return;
4445 cmdname
= alloca(e
-str
+8);
4446 if (cmdname
== NULL
) return;
4447 memcpy(cmdname
, str
, e
-str
);
4449 while (slen
> 0 && isspace(*e
)) { ++e
; --slen
; }
4451 for (int f
= 0; commandList
[f
].name
!= NULL
; ++f
) {
4452 if (strcasecmp(commandList
[f
].name
, cmdname
) == 0) {
4453 char *left
= calloc(slen
+2, 1);
4456 if (slen
> 0) memcpy(left
, e
, slen
);
4457 //fprintf(stderr, "command: [%s]; args: [%s]\n", cmdname, left);
4458 commandList
[f
].fn(cmdname
, left
);
4467 static void executeCommands (const char *str
) {
4469 oldTermIdx
= termidx
;
4472 if (str
== NULL
) return;
4477 while (*str
&& isspace(*str
)) ++str
;
4479 if (*str
== ';') { ++str
; continue; }
4484 if (*ce
== ';' && qch
== ' ') break;
4485 if (qch
!= ' ' && *ce
== qch
) { qch
= ' '; ++ce
; continue; }
4486 if (*ce
== '"' || *ce
== '\'') {
4487 if (qch
== ' ') qch
= *ce
;
4491 if (*ce
++ == '\\' && *ce
) ++ce
;
4494 executeCommand(str
, ce
-str
);
4495 if (*ce
) str
= ce
+1; else break;
4498 switchToTerm(oldTermIdx
, 1);
4502 ////////////////////////////////////////////////////////////////////////////////
4503 int main (int argc
, char *argv
[]) {
4504 char *configfile
= NULL
;
4508 for (int f
= 1; f
< argc
; f
++) {
4509 if (strcmp(argv
[f
], "-name") == 0) { ++f
; continue; }
4510 if (strcmp(argv
[f
], "-into") == 0) { ++f
; continue; }
4511 if (strcmp(argv
[f
], "-embed") == 0) { ++f
; continue; }
4512 switch (argv
[f
][0] != '-' || argv
[f
][2] ? -1 : argv
[f
][1]) {
4513 case 'e': f
= argc
+1; break;
4523 opt_term
= strdup(argv
[f
]);
4524 opt_term_locked
= 1;
4529 if (configfile
) free(configfile
);
4530 configfile
= strdup(argv
[f
]);
4536 fprintf(stderr
, "%s", USAGE
);
4541 initDefaultOptions();
4542 if (configfile
== NULL
) {
4543 const char *home
= getenv("HOME");
4546 configfile
= SPrintf("%s/.sterm.rc", home
);
4547 if (loadConfig(configfile
) == 0) goto cfgdone
;
4548 free(configfile
); configfile
= NULL
;
4550 configfile
= SPrintf("%s/.config/sterm.rc", home
);
4551 if (loadConfig(configfile
) == 0) goto cfgdone
;
4552 free(configfile
); configfile
= NULL
;
4555 configfile
= SPrintf("/etc/sterm.rc");
4556 if (loadConfig(configfile
) == 0) goto cfgdone
;
4557 free(configfile
); configfile
= NULL
;
4559 configfile
= SPrintf("/etc/sterm/sterm.rc");
4560 if (loadConfig(configfile
) == 0) goto cfgdone
;
4561 free(configfile
); configfile
= NULL
;
4563 configfile
= SPrintf("./.sterm.rc");
4564 if (loadConfig(configfile
) == 0) goto cfgdone
;
4565 free(configfile
); configfile
= NULL
;
4568 if (loadConfig(configfile
) < 0) die("config file '%s' not found!", configfile
);
4571 if (configfile
!= NULL
) free(configfile
);
4573 for (int f
= 1; f
< argc
; f
++) {
4574 if (strcmp(argv
[f
], "-name") == 0) { ++f
; continue; }
4575 if (strcmp(argv
[f
], "-into") == 0 || strcmp(argv
[f
], "-embed") == 0) {
4576 if (opt_embed
) free(opt_embed
);
4577 opt_embed
= strdup(argv
[f
]);
4580 switch (argv
[f
][0] != '-' || argv
[f
][2] ? -1 : argv
[f
][1]) {
4584 opt_title
= strdup(argv
[f
]);
4590 opt_class
= strdup(argv
[f
]);
4595 if (opt_embed
) free(opt_embed
);
4596 opt_embed
= strdup(argv
[f
]);
4600 /* eat every remaining arguments */
4601 if (++f
< argc
) opt_cmd
= &argv
[f
];
4608 if (configfile
) free(configfile
);
4609 configfile
= strdup(argv
[f
]);
4615 fprintf(stderr
, "%s", USAGE
);
4620 setenv("TERM", opt_term
, 1);
4622 setlocale(LC_ALL
, "");
4627 if (term
->execcmd
!= NULL
) { free(term
->execcmd
); term
->execcmd
= NULL
; }
4628 tinitialize(80, 25);
4629 if (ttynew(term
) != 0) die("can't run process");