9 #include <sys/select.h>
10 #include <sys/ioctl.h>
19 static int inputmode
= TB_INPUT_ESC
;
21 /* bytebuffer.inl --------------------------------------------------------- */
29 static void bytebuffer_reserve(struct bytebuffer
*b
, int cap
) {
34 // prefer doubling capacity
35 if (b
->cap
* 2 >= cap
) {
39 char *newbuf
= realloc(b
->buf
, cap
);
44 static void bytebuffer_init(struct bytebuffer
*b
, int cap
) {
51 b
->buf
= malloc(cap
); // just assume malloc works always
55 static void bytebuffer_free(struct bytebuffer
*b
) {
60 static void bytebuffer_clear(struct bytebuffer
*b
) {
64 static void bytebuffer_append(struct bytebuffer
*b
, const char *data
, int len
) {
65 bytebuffer_reserve(b
, b
->len
+ len
);
66 memcpy(b
->buf
+ b
->len
, data
, len
);
70 static void bytebuffer_puts(struct bytebuffer
*b
, const char *str
) {
71 bytebuffer_append(b
, str
, strlen(str
));
74 static void bytebuffer_resize(struct bytebuffer
*b
, int len
) {
75 bytebuffer_reserve(b
, len
);
79 static void bytebuffer_flush(struct bytebuffer
*b
, int fd
) {
80 (void)write(fd
, b
->buf
, b
->len
);
84 static void bytebuffer_truncate(struct bytebuffer
*b
, int n
) {
89 const int nmove
= b
->len
- n
;
90 memmove(b
->buf
, b
->buf
+n
, nmove
);
94 /* term.inl --------------------------------------------------------------- */
113 #define ENTER_MOUSE_SEQ "\x1b[?1000h\x1b[?1002h\x1b[?1015h\x1b[?1006h"
114 #define EXIT_MOUSE_SEQ "\x1b[?1006l\x1b[?1015l\x1b[?1002l\x1b[?1000l"
116 #define EUNSUPPORTED_TERM -1
119 static const char *rxvt_256color_keys
[] = {
120 "\033[11~","\033[12~","\033[13~","\033[14~","\033[15~","\033[17~","\033[18~","\033[19~","\033[20~","\033[21~","\033[23~","\033[24~","\033[2~","\033[3~","\033[7~","\033[8~","\033[5~","\033[6~","\033[A","\033[B","\033[D","\033[C", 0
122 static const char *rxvt_256color_funcs
[] = {
123 "\0337\033[?47h", "\033[2J\033[?47l\0338", "\033[?25h", "\033[?25l", "\033[H\033[2J", "\033[m", "\033[4m", "\033[1m", "\033[5m", "\033[7m", "\033=", "\033>", ENTER_MOUSE_SEQ
, EXIT_MOUSE_SEQ
,
127 static const char *eterm_keys
[] = {
128 "\033[11~","\033[12~","\033[13~","\033[14~","\033[15~","\033[17~","\033[18~","\033[19~","\033[20~","\033[21~","\033[23~","\033[24~","\033[2~","\033[3~","\033[7~","\033[8~","\033[5~","\033[6~","\033[A","\033[B","\033[D","\033[C", 0
130 static const char *eterm_funcs
[] = {
131 "\0337\033[?47h", "\033[2J\033[?47l\0338", "\033[?25h", "\033[?25l", "\033[H\033[2J", "\033[m", "\033[4m", "\033[1m", "\033[5m", "\033[7m", "", "", "", "",
135 static const char *screen_keys
[] = {
136 "\033OP","\033OQ","\033OR","\033OS","\033[15~","\033[17~","\033[18~","\033[19~","\033[20~","\033[21~","\033[23~","\033[24~","\033[2~","\033[3~","\033[1~","\033[4~","\033[5~","\033[6~","\033OA","\033OB","\033OD","\033OC", 0
138 static const char *screen_funcs
[] = {
139 "\033[?1049h", "\033[?1049l", "\033[34h\033[?25h", "\033[?25l", "\033[H\033[J", "\033[m", "\033[4m", "\033[1m", "\033[5m", "\033[7m", "\033[?1h\033=", "\033[?1l\033>", ENTER_MOUSE_SEQ
, EXIT_MOUSE_SEQ
,
143 static const char *rxvt_unicode_keys
[] = {
144 "\033[11~","\033[12~","\033[13~","\033[14~","\033[15~","\033[17~","\033[18~","\033[19~","\033[20~","\033[21~","\033[23~","\033[24~","\033[2~","\033[3~","\033[7~","\033[8~","\033[5~","\033[6~","\033[A","\033[B","\033[D","\033[C", 0
146 static const char *rxvt_unicode_funcs
[] = {
147 "\033[?1049h", "\033[r\033[?1049l", "\033[?25h", "\033[?25l", "\033[H\033[2J", "\033[m\033(B", "\033[4m", "\033[1m", "\033[5m", "\033[7m", "\033=", "\033>", ENTER_MOUSE_SEQ
, EXIT_MOUSE_SEQ
,
151 static const char *linux_keys
[] = {
152 "\033[[A","\033[[B","\033[[C","\033[[D","\033[[E","\033[17~","\033[18~","\033[19~","\033[20~","\033[21~","\033[23~","\033[24~","\033[2~","\033[3~","\033[1~","\033[4~","\033[5~","\033[6~","\033[A","\033[B","\033[D","\033[C", 0
154 static const char *linux_funcs
[] = {
155 "", "", "\033[?25h\033[?0c", "\033[?25l\033[?1c", "\033[H\033[J", "\033[0;10m", "\033[4m", "\033[1m", "\033[5m", "\033[7m", "", "", "", "",
159 static const char *xterm_keys
[] = {
160 "\033OP","\033OQ","\033OR","\033OS","\033[15~","\033[17~","\033[18~","\033[19~","\033[20~","\033[21~","\033[23~","\033[24~","\033[2~","\033[3~","\033OH","\033OF","\033[5~","\033[6~","\033OA","\033OB","\033OD","\033OC", 0
162 static const char *xterm_funcs
[] = {
163 "\033[?1049h", "\033[?1049l", "\033[?12l\033[?25h", "\033[?25l", "\033[H\033[2J", "\033(B\033[m", "\033[4m", "\033[1m", "\033[5m", "\033[7m", "\033[?1h\033=", "\033[?1l\033>", ENTER_MOUSE_SEQ
, EXIT_MOUSE_SEQ
,
171 {"rxvt-256color", rxvt_256color_keys
, rxvt_256color_funcs
},
172 {"Eterm", eterm_keys
, eterm_funcs
},
173 {"screen", screen_keys
, screen_funcs
},
174 {"rxvt-unicode", rxvt_unicode_keys
, rxvt_unicode_funcs
},
175 {"linux", linux_keys
, linux_funcs
},
176 {"xterm", xterm_keys
, xterm_funcs
},
180 static bool init_from_terminfo
= false;
181 static const char **keys
;
182 static const char **funcs
;
184 static int try_compatible(const char *term
, const char *name
,
185 const char **tkeys
, const char **tfuncs
)
187 if (strstr(term
, name
)) {
193 return EUNSUPPORTED_TERM
;
196 static int init_term_builtin(void)
199 const char *term
= getenv("TERM");
202 for (i
= 0; terms
[i
].name
; i
++) {
203 if (!strcmp(terms
[i
].name
, term
)) {
204 keys
= terms
[i
].keys
;
205 funcs
= terms
[i
].funcs
;
210 /* let's do some heuristic, maybe it's a compatible terminal */
211 if (try_compatible(term
, "xterm", xterm_keys
, xterm_funcs
) == 0)
213 if (try_compatible(term
, "rxvt", rxvt_unicode_keys
, rxvt_unicode_funcs
) == 0)
215 if (try_compatible(term
, "linux", linux_keys
, linux_funcs
) == 0)
217 if (try_compatible(term
, "Eterm", eterm_keys
, eterm_funcs
) == 0)
219 if (try_compatible(term
, "screen", screen_keys
, screen_funcs
) == 0)
221 if (try_compatible(term
, "tmux", screen_keys
, screen_funcs
) == 0)
223 /* let's assume that 'cygwin' is xterm compatible */
224 if (try_compatible(term
, "cygwin", xterm_keys
, xterm_funcs
) == 0)
228 return EUNSUPPORTED_TERM
;
231 //----------------------------------------------------------------------
233 //----------------------------------------------------------------------
235 static char *read_file(const char *file
) {
236 FILE *f
= fopen(file
, "rb");
241 if (fstat(fileno(f
), &st
) != 0) {
246 char *data
= malloc(st
.st_size
);
252 if (fread(data
, 1, st
.st_size
, f
) != (size_t)st
.st_size
) {
262 static char *terminfo_try_path(const char *path
, const char *term
) {
264 snprintf(tmp
, sizeof(tmp
), "%s/%c/%s", path
, term
[0], term
);
265 tmp
[sizeof(tmp
)-1] = '\0';
266 char *data
= read_file(tmp
);
271 // fallback to darwin specific dirs structure
272 snprintf(tmp
, sizeof(tmp
), "%s/%x/%s", path
, term
[0], term
);
273 tmp
[sizeof(tmp
)-1] = '\0';
274 return read_file(tmp
);
277 static char *load_terminfo(void) {
279 const char *term
= getenv("TERM");
284 // if TERMINFO is set, no other directory should be searched
285 const char *terminfo
= getenv("TERMINFO");
287 return terminfo_try_path(terminfo
, term
);
290 // next, consider ~/.terminfo
291 const char *home
= getenv("HOME");
293 snprintf(tmp
, sizeof(tmp
), "%s/.terminfo", home
);
294 tmp
[sizeof(tmp
)-1] = '\0';
295 char *data
= terminfo_try_path(tmp
, term
);
300 // next, TERMINFO_DIRS
301 const char *dirs
= getenv("TERMINFO_DIRS");
303 snprintf(tmp
, sizeof(tmp
), "%s", dirs
);
304 tmp
[sizeof(tmp
)-1] = '\0';
305 char *dir
= strtok(tmp
, ":");
307 const char *cdir
= dir
;
308 if (strcmp(cdir
, "") == 0) {
309 cdir
= "/usr/share/terminfo";
311 char *data
= terminfo_try_path(cdir
, term
);
314 dir
= strtok(0, ":");
318 // fallback to /usr/share/terminfo
319 return terminfo_try_path("/usr/share/terminfo", term
);
322 #define TI_MAGIC 0432
323 #define TI_ALT_MAGIC 542
324 #define TI_HEADER_LENGTH 12
325 #define TB_KEYS_NUM 22
327 static const char *terminfo_copy_string(char *data
, int str
, int table
) {
328 const int16_t off
= *(int16_t*)(data
+ str
);
329 const char *src
= data
+ table
+ off
;
330 int len
= strlen(src
);
331 char *dst
= malloc(len
+1);
336 static const int16_t ti_funcs
[] = {
337 28, 40, 16, 13, 5, 39, 36, 27, 26, 34, 89, 88,
340 static const int16_t ti_keys
[] = {
341 66, 68 /* apparently not a typo; 67 is F10 for whatever reason */, 69,
342 70, 71, 72, 73, 74, 75, 67, 216, 217, 77, 59, 76, 164, 82, 81, 87, 61,
346 static int init_term(void) {
348 char *data
= load_terminfo();
350 init_from_terminfo
= false;
351 return init_term_builtin();
354 int16_t *header
= (int16_t*)data
;
356 const int number_sec_len
= header
[0] == TI_ALT_MAGIC
? 4 : 2;
358 if ((header
[1] + header
[2]) % 2) {
359 // old quirk to align everything on word boundaries
363 const int str_offset
= TI_HEADER_LENGTH
+
364 header
[1] + header
[2] + number_sec_len
* header
[3];
365 const int table_offset
= str_offset
+ 2 * header
[4];
367 keys
= malloc(sizeof(const char*) * (TB_KEYS_NUM
+1));
368 for (i
= 0; i
< TB_KEYS_NUM
; i
++) {
369 keys
[i
] = terminfo_copy_string(data
,
370 str_offset
+ 2 * ti_keys
[i
], table_offset
);
372 keys
[TB_KEYS_NUM
] = 0;
374 funcs
= malloc(sizeof(const char*) * T_FUNCS_NUM
);
375 // the last two entries are reserved for mouse. because the table offset is
376 // not there, the two entries have to fill in manually
377 for (i
= 0; i
< T_FUNCS_NUM
-2; i
++) {
378 funcs
[i
] = terminfo_copy_string(data
,
379 str_offset
+ 2 * ti_funcs
[i
], table_offset
);
382 funcs
[T_FUNCS_NUM
-2] = ENTER_MOUSE_SEQ
;
383 funcs
[T_FUNCS_NUM
-1] = EXIT_MOUSE_SEQ
;
385 init_from_terminfo
= true;
390 static void shutdown_term(void) {
391 if (init_from_terminfo
) {
393 for (i
= 0; i
< TB_KEYS_NUM
; i
++) {
394 free((void*)keys
[i
]);
396 // the last two entries are reserved for mouse. because the table offset
397 // is not there, the two entries have to fill in manually and do not
399 for (i
= 0; i
< T_FUNCS_NUM
-2; i
++) {
400 free((void*)funcs
[i
]);
407 /* input.inl -------------------------------------------------------------- */
409 // if s1 starts with s2 returns true, else false
410 // len is the length of s1
411 // s2 should be null-terminated
412 static bool starts_with(const char *s1
, int len
, const char *s2
)
415 while (*s2
&& n
< len
) {
423 static int parse_mouse_event(struct tb_event
*event
, const char *buf
, int len
) {
424 if (len
>= 6 && starts_with(buf
, len
, "\033[M")) {
425 // X10 mouse encoding, the simplest one
431 event
->key
= TB_KEY_MOUSE_WHEEL_UP
;
433 event
->key
= TB_KEY_MOUSE_LEFT
;
437 event
->key
= TB_KEY_MOUSE_WHEEL_DOWN
;
439 event
->key
= TB_KEY_MOUSE_MIDDLE
;
442 event
->key
= TB_KEY_MOUSE_RIGHT
;
445 event
->key
= TB_KEY_MOUSE_RELEASE
;
450 event
->type
= TB_EVENT_MOUSE
; // TB_EVENT_KEY by default
452 event
->mod
|= TB_MOD_MOTION
;
454 // the coord is 1,1 for upper left
455 event
->x
= (uint8_t)buf
[4] - 1 - 32;
456 event
->y
= (uint8_t)buf
[5] - 1 - 32;
459 } else if (starts_with(buf
, len
, "\033[<") || starts_with(buf
, len
, "\033[")) {
460 // xterm 1006 extended mode or urxvt 1015 extended mode
461 // xterm: \033 [ < Cb ; Cx ; Cy (M or m)
462 // urxvt: \033 [ Cb ; Cx ; Cy M
463 int i
, mi
= -1, starti
= -1;
464 int isM
, isU
, s1
= -1, s2
= -1;
465 int n1
= 0, n2
= 0, n3
= 0;
467 for (i
= 0; i
< len
; i
++) {
468 // We search the first (s1) and the last (s2) ';'
475 // We search for the first 'm' or 'M'
476 if ((buf
[i
] == 'm' || buf
[i
] == 'M') && mi
== -1) {
484 // whether it's a capital M or not
485 isM
= (buf
[mi
] == 'M');
495 if (s1
== -1 || s2
== -1 || s1
== s2
)
498 n1
= strtoul(&buf
[starti
], NULL
, 10);
499 n2
= strtoul(&buf
[s1
+ 1], NULL
, 10);
500 n3
= strtoul(&buf
[s2
+ 1], NULL
, 10);
508 event
->key
= TB_KEY_MOUSE_WHEEL_UP
;
510 event
->key
= TB_KEY_MOUSE_LEFT
;
515 event
->key
= TB_KEY_MOUSE_WHEEL_DOWN
;
517 event
->key
= TB_KEY_MOUSE_MIDDLE
;
521 event
->key
= TB_KEY_MOUSE_RIGHT
;
524 event
->key
= TB_KEY_MOUSE_RELEASE
;
531 // on xterm mouse release is signaled by lowercase m
532 event
->key
= TB_KEY_MOUSE_RELEASE
;
535 event
->type
= TB_EVENT_MOUSE
; // TB_EVENT_KEY by default
537 event
->mod
|= TB_MOD_MOTION
;
539 event
->x
= (uint8_t)n2
- 1;
540 event
->y
= (uint8_t)n3
- 1;
548 // convert escape sequence to event, and return consumed bytes on success (failure == 0)
549 static int parse_escape_seq(struct tb_event
*event
, const char *buf
, int len
)
551 int mouse_parsed
= parse_mouse_event(event
, buf
, len
);
553 if (mouse_parsed
!= 0)
556 // it's pretty simple here, find 'starts_with' match and return
557 // success, else return failure
559 for (i
= 0; keys
[i
]; i
++) {
560 if (starts_with(buf
, len
, keys
[i
])) {
562 event
->key
= 0xFFFF-i
;
563 return strlen(keys
[i
]);
569 static bool extract_event(struct tb_event
*event
, struct bytebuffer
*inbuf
)
571 const char *buf
= inbuf
->buf
;
572 const int len
= inbuf
->len
;
576 if (buf
[0] == '\033') {
577 int n
= parse_escape_seq(event
, buf
, len
);
584 bytebuffer_truncate(inbuf
, n
);
587 // it's not escape sequence, then it's ALT or ESC,
589 if (inputmode
&TB_INPUT_ESC
) {
590 // if we're in escape mode, fill ESC event, pop
591 // buffer, return success
593 event
->key
= TB_KEY_ESC
;
595 bytebuffer_truncate(inbuf
, 1);
597 } else if (inputmode
&TB_INPUT_ALT
) {
598 // if we're in alt mode, set ALT modifier to
599 // event and redo parsing
600 event
->mod
= TB_MOD_ALT
;
601 bytebuffer_truncate(inbuf
, 1);
602 return extract_event(event
, inbuf
);
604 assert(!"never got here");
608 // if we're here, this is not an escape sequence and not an alt sequence
609 // so, it's a FUNCTIONAL KEY or a UNICODE character
611 // first of all check if it's a functional key
612 if ((unsigned char)buf
[0] <= TB_KEY_SPACE
||
613 (unsigned char)buf
[0] == TB_KEY_BACKSPACE2
)
615 // fill event, pop buffer, return success */
617 event
->key
= (uint16_t)buf
[0];
618 bytebuffer_truncate(inbuf
, 1);
622 // feh... we got utf8 here
624 // check if there is all bytes
625 if (len
>= tb_utf8_char_length(buf
[0])) {
626 /* everything ok, fill event, pop buffer, return success */
627 tb_utf8_char_to_unicode(&event
->ch
, buf
);
629 bytebuffer_truncate(inbuf
, tb_utf8_char_length(buf
[0]));
633 // event isn't recognized, perhaps there is not enough bytes in utf8
638 /* -------------------------------------------------------- */
643 struct tb_cell
*cells
;
646 #define CELL(buf, x, y) (buf)->cells[(y) * (buf)->width + (x)]
647 #define IS_CURSOR_HIDDEN(cx, cy) (cx == -1 || cy == -1)
648 #define LAST_COORD_INIT -1
650 static struct termios orig_tios
;
652 static struct cellbuf back_buffer
;
653 static struct cellbuf front_buffer
;
654 static struct bytebuffer output_buffer
;
655 static struct bytebuffer input_buffer
;
657 static int termw
= -1;
658 static int termh
= -1;
660 static int outputmode
= TB_OUTPUT_NORMAL
;
663 static int winch_fds
[2];
665 static int lastx
= LAST_COORD_INIT
;
666 static int lasty
= LAST_COORD_INIT
;
667 static int cursor_x
= -1;
668 static int cursor_y
= -1;
670 static uint16_t background
= TB_DEFAULT
;
671 static uint16_t foreground
= TB_DEFAULT
;
673 static void write_cursor(int x
, int y
);
674 static void write_sgr(uint16_t fg
, uint16_t bg
);
676 static void cellbuf_init(struct cellbuf
*buf
, int width
, int height
);
677 static void cellbuf_resize(struct cellbuf
*buf
, int width
, int height
);
678 static void cellbuf_clear(struct cellbuf
*buf
);
679 static void cellbuf_free(struct cellbuf
*buf
);
681 static void update_size(void);
682 static void update_term_size(void);
683 static void send_attr(uint16_t fg
, uint16_t bg
);
684 static void send_char(int x
, int y
, uint32_t c
);
685 static void send_clear(void);
686 static void sigwinch_handler(int xxx
);
687 static int wait_fill_event(struct tb_event
*event
, struct timeval
*timeout
);
689 /* may happen in a different thread */
690 static volatile int buffer_size_change_request
;
692 /* -------------------------------------------------------- */
694 int tb_init_fd(int inout_
)
698 return TB_EFAILED_TO_OPEN_TTY
;
701 if (init_term() < 0) {
703 return TB_EUNSUPPORTED_TERMINAL
;
706 if (pipe(winch_fds
) < 0) {
708 return TB_EPIPE_TRAP_ERROR
;
712 memset(&sa
, 0, sizeof(sa
));
713 sa
.sa_handler
= sigwinch_handler
;
715 sigaction(SIGWINCH
, &sa
, 0);
717 tcgetattr(inout
, &orig_tios
);
720 memcpy(&tios
, &orig_tios
, sizeof(tios
));
722 tios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
723 | INLCR
| IGNCR
| ICRNL
| IXON
);
724 tios
.c_oflag
&= ~OPOST
;
725 tios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| ISIG
| IEXTEN
);
726 tios
.c_cflag
&= ~(CSIZE
| PARENB
);
729 tios
.c_cc
[VTIME
] = 0;
730 tcsetattr(inout
, TCSAFLUSH
, &tios
);
732 bytebuffer_init(&input_buffer
, 128);
733 bytebuffer_init(&output_buffer
, 32 * 1024);
735 bytebuffer_puts(&output_buffer
, funcs
[T_ENTER_CA
]);
736 bytebuffer_puts(&output_buffer
, funcs
[T_ENTER_KEYPAD
]);
737 bytebuffer_puts(&output_buffer
, funcs
[T_HIDE_CURSOR
]);
741 cellbuf_init(&back_buffer
, termw
, termh
);
742 cellbuf_init(&front_buffer
, termw
, termh
);
743 cellbuf_clear(&back_buffer
);
744 cellbuf_clear(&front_buffer
);
749 int tb_init_file(const char* name
){
750 return tb_init_fd(open(name
, O_RDWR
));
755 return tb_init_file("/dev/tty");
758 void tb_shutdown(void)
761 fputs("tb_shutdown() should not be called twice.", stderr
);
765 bytebuffer_puts(&output_buffer
, funcs
[T_SHOW_CURSOR
]);
766 bytebuffer_puts(&output_buffer
, funcs
[T_SGR0
]);
767 bytebuffer_puts(&output_buffer
, funcs
[T_CLEAR_SCREEN
]);
768 bytebuffer_puts(&output_buffer
, funcs
[T_EXIT_CA
]);
769 bytebuffer_puts(&output_buffer
, funcs
[T_EXIT_KEYPAD
]);
770 bytebuffer_puts(&output_buffer
, funcs
[T_EXIT_MOUSE
]);
771 bytebuffer_flush(&output_buffer
, inout
);
772 tcsetattr(inout
, TCSAFLUSH
, &orig_tios
);
779 cellbuf_free(&back_buffer
);
780 cellbuf_free(&front_buffer
);
781 bytebuffer_free(&output_buffer
);
782 bytebuffer_free(&input_buffer
);
786 void tb_present(void)
789 struct tb_cell
*back
, *front
;
791 /* invalidate cursor position */
792 lastx
= LAST_COORD_INIT
;
793 lasty
= LAST_COORD_INIT
;
795 if (buffer_size_change_request
) {
797 buffer_size_change_request
= 0;
800 for (y
= 0; y
< front_buffer
.height
; ++y
) {
801 for (x
= 0; x
< front_buffer
.width
; ) {
802 back
= &CELL(&back_buffer
, x
, y
);
803 front
= &CELL(&front_buffer
, x
, y
);
804 w
= wcwidth(back
->ch
);
806 if (memcmp(back
, front
, sizeof(struct tb_cell
)) == 0) {
810 memcpy(front
, back
, sizeof(struct tb_cell
));
811 send_attr(back
->fg
, back
->bg
);
812 if (w
> 1 && x
>= front_buffer
.width
- (w
- 1)) {
813 // Not enough room for wide ch, so send spaces
814 for (i
= x
; i
< front_buffer
.width
; ++i
) {
815 send_char(i
, y
, ' ');
818 send_char(x
, y
, back
->ch
);
819 for (i
= 1; i
< w
; ++i
) {
820 front
= &CELL(&front_buffer
, x
+ i
, y
);
822 front
->fg
= back
->fg
;
823 front
->bg
= back
->bg
;
829 if (!IS_CURSOR_HIDDEN(cursor_x
, cursor_y
))
830 write_cursor(cursor_x
, cursor_y
);
831 bytebuffer_flush(&output_buffer
, inout
);
834 void tb_set_cursor(int cx
, int cy
)
836 if (IS_CURSOR_HIDDEN(cursor_x
, cursor_y
) && !IS_CURSOR_HIDDEN(cx
, cy
))
837 bytebuffer_puts(&output_buffer
, funcs
[T_SHOW_CURSOR
]);
839 if (!IS_CURSOR_HIDDEN(cursor_x
, cursor_y
) && IS_CURSOR_HIDDEN(cx
, cy
))
840 bytebuffer_puts(&output_buffer
, funcs
[T_HIDE_CURSOR
]);
844 if (!IS_CURSOR_HIDDEN(cursor_x
, cursor_y
))
845 write_cursor(cursor_x
, cursor_y
);
848 void tb_put_cell(int x
, int y
, const struct tb_cell
*cell
)
850 if ((unsigned)x
>= (unsigned)back_buffer
.width
)
852 if ((unsigned)y
>= (unsigned)back_buffer
.height
)
854 CELL(&back_buffer
, x
, y
) = *cell
;
857 void tb_change_cell(int x
, int y
, uint32_t ch
, uint16_t fg
, uint16_t bg
)
859 struct tb_cell c
= {ch
, fg
, bg
};
860 tb_put_cell(x
, y
, &c
);
863 void tb_blit(int x
, int y
, int w
, int h
, const struct tb_cell
*cells
)
865 if (x
+ w
< 0 || x
>= back_buffer
.width
)
867 if (y
+ h
< 0 || y
>= back_buffer
.height
)
869 int xo
= 0, yo
= 0, ww
= w
, hh
= h
;
880 if (ww
> back_buffer
.width
- x
)
881 ww
= back_buffer
.width
- x
;
882 if (hh
> back_buffer
.height
- y
)
883 hh
= back_buffer
.height
- y
;
886 struct tb_cell
*dst
= &CELL(&back_buffer
, x
, y
);
887 const struct tb_cell
*src
= cells
+ yo
* w
+ xo
;
888 size_t size
= sizeof(struct tb_cell
) * ww
;
890 for (sy
= 0; sy
< hh
; ++sy
) {
891 memcpy(dst
, src
, size
);
892 dst
+= back_buffer
.width
;
897 struct tb_cell
*tb_cell_buffer(void)
899 return back_buffer
.cells
;
902 int tb_poll_event(struct tb_event
*event
)
904 return wait_fill_event(event
, 0);
907 int tb_peek_event(struct tb_event
*event
, int timeout
)
910 tv
.tv_sec
= timeout
/ 1000;
911 tv
.tv_usec
= (timeout
- (tv
.tv_sec
* 1000)) * 1000;
912 return wait_fill_event(event
, &tv
);
927 if (buffer_size_change_request
) {
929 buffer_size_change_request
= 0;
931 cellbuf_clear(&back_buffer
);
934 int tb_select_input_mode(int mode
)
937 if ((mode
& (TB_INPUT_ESC
| TB_INPUT_ALT
)) == 0)
938 mode
|= TB_INPUT_ESC
;
940 /* technically termbox can handle that, but let's be nice and show here
941 what mode is actually used */
942 if ((mode
& (TB_INPUT_ESC
| TB_INPUT_ALT
)) == (TB_INPUT_ESC
| TB_INPUT_ALT
))
943 mode
&= ~TB_INPUT_ALT
;
946 if (mode
&TB_INPUT_MOUSE
) {
947 bytebuffer_puts(&output_buffer
, funcs
[T_ENTER_MOUSE
]);
948 bytebuffer_flush(&output_buffer
, inout
);
950 bytebuffer_puts(&output_buffer
, funcs
[T_EXIT_MOUSE
]);
951 bytebuffer_flush(&output_buffer
, inout
);
957 int tb_select_output_mode(int mode
)
964 void tb_set_clear_attributes(uint16_t fg
, uint16_t bg
)
970 /* -------------------------------------------------------- */
972 static int convertnum(uint32_t num
, char* buf
) {
976 buf
[l
++] = '0' + (num
% 10);
979 for(i
= 0; i
< l
/ 2; i
++) {
981 buf
[i
] = buf
[l
- 1 - i
];
987 #define WRITE_LITERAL(X) bytebuffer_append(&output_buffer, (X), sizeof(X)-1)
988 #define WRITE_INT(X) bytebuffer_append(&output_buffer, buf, convertnum((X), buf))
990 static void write_cursor(int x
, int y
) {
992 WRITE_LITERAL("\033[");
999 static void write_sgr(uint16_t fg
, uint16_t bg
) {
1002 if (fg
== TB_DEFAULT
&& bg
== TB_DEFAULT
)
1005 switch (outputmode
) {
1008 case TB_OUTPUT_GRAYSCALE
:
1009 WRITE_LITERAL("\033[");
1010 if (fg
!= TB_DEFAULT
) {
1011 WRITE_LITERAL("38;5;");
1013 if (bg
!= TB_DEFAULT
) {
1017 if (bg
!= TB_DEFAULT
) {
1018 WRITE_LITERAL("48;5;");
1023 case TB_OUTPUT_NORMAL
:
1025 WRITE_LITERAL("\033[");
1026 if (fg
!= TB_DEFAULT
) {
1029 if (bg
!= TB_DEFAULT
) {
1033 if (bg
!= TB_DEFAULT
) {
1042 static void cellbuf_init(struct cellbuf
*buf
, int width
, int height
)
1044 buf
->cells
= (struct tb_cell
*)malloc(sizeof(struct tb_cell
) * width
* height
);
1047 buf
->height
= height
;
1050 static void cellbuf_resize(struct cellbuf
*buf
, int width
, int height
)
1052 if (buf
->width
== width
&& buf
->height
== height
)
1055 int oldw
= buf
->width
;
1056 int oldh
= buf
->height
;
1057 struct tb_cell
*oldcells
= buf
->cells
;
1059 cellbuf_init(buf
, width
, height
);
1062 int minw
= (width
< oldw
) ? width
: oldw
;
1063 int minh
= (height
< oldh
) ? height
: oldh
;
1066 for (i
= 0; i
< minh
; ++i
) {
1067 struct tb_cell
*csrc
= oldcells
+ (i
* oldw
);
1068 struct tb_cell
*cdst
= buf
->cells
+ (i
* width
);
1069 memcpy(cdst
, csrc
, sizeof(struct tb_cell
) * minw
);
1075 static void cellbuf_clear(struct cellbuf
*buf
)
1078 int ncells
= buf
->width
* buf
->height
;
1080 for (i
= 0; i
< ncells
; ++i
) {
1081 buf
->cells
[i
].ch
= ' ';
1082 buf
->cells
[i
].fg
= foreground
;
1083 buf
->cells
[i
].bg
= background
;
1087 static void cellbuf_free(struct cellbuf
*buf
)
1092 static void get_term_size(int *w
, int *h
)
1095 memset(&sz
, 0, sizeof(sz
));
1097 ioctl(inout
, TIOCGWINSZ
, &sz
);
1099 if (w
) *w
= sz
.ws_col
;
1100 if (h
) *h
= sz
.ws_row
;
1103 static void update_term_size(void)
1106 memset(&sz
, 0, sizeof(sz
));
1108 ioctl(inout
, TIOCGWINSZ
, &sz
);
1114 static void send_attr(uint16_t fg
, uint16_t bg
)
1116 #define LAST_ATTR_INIT 0xFFFF
1117 static uint16_t lastfg
= LAST_ATTR_INIT
, lastbg
= LAST_ATTR_INIT
;
1118 if (fg
!= lastfg
|| bg
!= lastbg
) {
1119 bytebuffer_puts(&output_buffer
, funcs
[T_SGR0
]);
1124 switch (outputmode
) {
1131 fgcol
= fg
& 0xFF; if (fgcol
> 215) fgcol
= 7;
1132 bgcol
= bg
& 0xFF; if (bgcol
> 215) bgcol
= 0;
1137 case TB_OUTPUT_GRAYSCALE
:
1138 fgcol
= fg
& 0xFF; if (fgcol
> 23) fgcol
= 23;
1139 bgcol
= bg
& 0xFF; if (bgcol
> 23) bgcol
= 0;
1144 case TB_OUTPUT_NORMAL
:
1151 bytebuffer_puts(&output_buffer
, funcs
[T_BOLD
]);
1153 bytebuffer_puts(&output_buffer
, funcs
[T_BLINK
]);
1154 if (fg
& TB_UNDERLINE
)
1155 bytebuffer_puts(&output_buffer
, funcs
[T_UNDERLINE
]);
1156 if ((fg
& TB_REVERSE
) || (bg
& TB_REVERSE
))
1157 bytebuffer_puts(&output_buffer
, funcs
[T_REVERSE
]);
1159 write_sgr(fgcol
, bgcol
);
1166 static void send_char(int x
, int y
, uint32_t c
)
1169 int bw
= tb_utf8_unicode_to_char(buf
, c
);
1170 if (x
-1 != lastx
|| y
!= lasty
)
1172 lastx
= x
; lasty
= y
;
1173 if(!c
) buf
[0] = ' '; // replace 0 with whitespace
1174 bytebuffer_append(&output_buffer
, buf
, bw
);
1177 static void send_clear(void)
1179 send_attr(foreground
, background
);
1180 bytebuffer_puts(&output_buffer
, funcs
[T_CLEAR_SCREEN
]);
1181 if (!IS_CURSOR_HIDDEN(cursor_x
, cursor_y
))
1182 write_cursor(cursor_x
, cursor_y
);
1183 bytebuffer_flush(&output_buffer
, inout
);
1185 /* we need to invalidate cursor position too and these two vars are
1186 * used only for simple cursor positioning optimization, cursor
1187 * actually may be in the correct place, but we simply discard
1188 * optimization once and it gives us simple solution for the case when
1190 lastx
= LAST_COORD_INIT
;
1191 lasty
= LAST_COORD_INIT
;
1194 static void sigwinch_handler(int xxx
)
1198 (void)write(winch_fds
[1], &zzz
, sizeof(int));
1201 static void update_size(void)
1204 cellbuf_resize(&back_buffer
, termw
, termh
);
1205 cellbuf_resize(&front_buffer
, termw
, termh
);
1206 cellbuf_clear(&front_buffer
);
1210 static int read_up_to(int n
) {
1212 const int prevlen
= input_buffer
.len
;
1213 bytebuffer_resize(&input_buffer
, prevlen
+ n
);
1216 while (read_n
<= n
) {
1219 r
= read(inout
, input_buffer
.buf
+ prevlen
+ read_n
, n
- read_n
);
1222 // While linux man for tty says when VMIN == 0 && VTIME == 0, read
1223 // should return 0 when there is nothing to read, cygwin's read returns
1224 // -1. Not sure why and if it's correct to ignore it, but let's pretend
1229 // EAGAIN / EWOULDBLOCK shouldn't occur here
1230 assert(errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
);
1235 bytebuffer_resize(&input_buffer
, prevlen
+ read_n
);
1239 assert(!"unreachable");
1243 static int wait_fill_event(struct tb_event
*event
, struct timeval
*timeout
)
1246 #define ENOUGH_DATA_FOR_PARSING 64
1248 memset(event
, 0, sizeof(struct tb_event
));
1250 // try to extract event from input buffer, return on success
1251 event
->type
= TB_EVENT_KEY
;
1252 if (extract_event(event
, &input_buffer
))
1255 // it looks like input buffer is incomplete, let's try the short path,
1256 // but first make sure there is enough space
1257 int n
= read_up_to(ENOUGH_DATA_FOR_PARSING
);
1260 if (n
> 0 && extract_event(event
, &input_buffer
))
1263 // n == 0, or not enough data, let's go to select
1266 FD_SET(inout
, &events
);
1267 FD_SET(winch_fds
[0], &events
);
1268 int maxfd
= (winch_fds
[0] > inout
) ? winch_fds
[0] : inout
;
1269 int result
= select(maxfd
+1, &events
, 0, 0, timeout
);
1273 if (FD_ISSET(inout
, &events
)) {
1274 event
->type
= TB_EVENT_KEY
;
1275 n
= read_up_to(ENOUGH_DATA_FOR_PARSING
);
1282 if (extract_event(event
, &input_buffer
))
1285 if (FD_ISSET(winch_fds
[0], &events
)) {
1286 event
->type
= TB_EVENT_RESIZE
;
1288 (void)read(winch_fds
[0], &zzz
, sizeof(int));
1289 buffer_size_change_request
= 1;
1290 get_term_size(&event
->w
, &event
->h
);
1291 return TB_EVENT_RESIZE
;