[ref] clear_pane()
[sfm.git] / termbox.c
blobdf722673b6551208cc6f287e991ef568d4c035bf
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <signal.h>
7 #include <stdio.h>
8 #include <stdbool.h>
9 #include <sys/select.h>
10 #include <sys/ioctl.h>
11 #include <sys/time.h>
12 #include <sys/stat.h>
13 #include <termios.h>
14 #include <unistd.h>
15 #include <wchar.h>
17 #include "termbox.h"
19 static int inputmode = TB_INPUT_ESC;
21 /* bytebuffer.inl --------------------------------------------------------- */
23 struct bytebuffer {
24 char *buf;
25 int len;
26 int cap;
29 static void bytebuffer_reserve(struct bytebuffer *b, int cap) {
30 if (b->cap >= cap) {
31 return;
34 // prefer doubling capacity
35 if (b->cap * 2 >= cap) {
36 cap = b->cap * 2;
39 char *newbuf = realloc(b->buf, cap);
40 b->buf = newbuf;
41 b->cap = cap;
44 static void bytebuffer_init(struct bytebuffer *b, int cap) {
45 b->cap = 0;
46 b->len = 0;
47 b->buf = 0;
49 if (cap > 0) {
50 b->cap = cap;
51 b->buf = malloc(cap); // just assume malloc works always
55 static void bytebuffer_free(struct bytebuffer *b) {
56 if (b->buf)
57 free(b->buf);
60 static void bytebuffer_clear(struct bytebuffer *b) {
61 b->len = 0;
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);
67 b->len += 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);
76 b->len = len;
79 static void bytebuffer_flush(struct bytebuffer *b, int fd) {
80 (void)write(fd, b->buf, b->len);
81 bytebuffer_clear(b);
84 static void bytebuffer_truncate(struct bytebuffer *b, int n) {
85 if (n <= 0)
86 return;
87 if (n > b->len)
88 n = b->len;
89 const int nmove = b->len - n;
90 memmove(b->buf, b->buf+n, nmove);
91 b->len -= n;
94 /* term.inl --------------------------------------------------------------- */
95 enum {
96 T_ENTER_CA,
97 T_EXIT_CA,
98 T_SHOW_CURSOR,
99 T_HIDE_CURSOR,
100 T_CLEAR_SCREEN,
101 T_SGR0,
102 T_UNDERLINE,
103 T_BOLD,
104 T_BLINK,
105 T_REVERSE,
106 T_ENTER_KEYPAD,
107 T_EXIT_KEYPAD,
108 T_ENTER_MOUSE,
109 T_EXIT_MOUSE,
110 T_FUNCS_NUM,
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
118 // rxvt-256color
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,
126 // Eterm
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", "", "", "", "",
134 // screen
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,
142 // rxvt-unicode
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,
150 // linux
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", "", "", "", "",
158 // xterm
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,
166 static struct term {
167 const char *name;
168 const char **keys;
169 const char **funcs;
170 } terms[] = {
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},
177 {0, 0, 0},
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)) {
188 keys = tkeys;
189 funcs = tfuncs;
190 return 0;
193 return EUNSUPPORTED_TERM;
196 static int init_term_builtin(void)
198 int i;
199 const char *term = getenv("TERM");
201 if (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;
206 return 0;
210 /* let's do some heuristic, maybe it's a compatible terminal */
211 if (try_compatible(term, "xterm", xterm_keys, xterm_funcs) == 0)
212 return 0;
213 if (try_compatible(term, "rxvt", rxvt_unicode_keys, rxvt_unicode_funcs) == 0)
214 return 0;
215 if (try_compatible(term, "linux", linux_keys, linux_funcs) == 0)
216 return 0;
217 if (try_compatible(term, "Eterm", eterm_keys, eterm_funcs) == 0)
218 return 0;
219 if (try_compatible(term, "screen", screen_keys, screen_funcs) == 0)
220 return 0;
221 if (try_compatible(term, "tmux", screen_keys, screen_funcs) == 0)
222 return 0;
223 /* let's assume that 'cygwin' is xterm compatible */
224 if (try_compatible(term, "cygwin", xterm_keys, xterm_funcs) == 0)
225 return 0;
228 return EUNSUPPORTED_TERM;
231 //----------------------------------------------------------------------
232 // terminfo
233 //----------------------------------------------------------------------
235 static char *read_file(const char *file) {
236 FILE *f = fopen(file, "rb");
237 if (!f)
238 return 0;
240 struct stat st;
241 if (fstat(fileno(f), &st) != 0) {
242 fclose(f);
243 return 0;
246 char *data = malloc(st.st_size);
247 if (!data) {
248 fclose(f);
249 return 0;
252 if (fread(data, 1, st.st_size, f) != (size_t)st.st_size) {
253 fclose(f);
254 free(data);
255 return 0;
258 fclose(f);
259 return data;
262 static char *terminfo_try_path(const char *path, const char *term) {
263 char tmp[4096];
264 snprintf(tmp, sizeof(tmp), "%s/%c/%s", path, term[0], term);
265 tmp[sizeof(tmp)-1] = '\0';
266 char *data = read_file(tmp);
267 if (data) {
268 return data;
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) {
278 char tmp[4096];
279 const char *term = getenv("TERM");
280 if (!term) {
281 return 0;
284 // if TERMINFO is set, no other directory should be searched
285 const char *terminfo = getenv("TERMINFO");
286 if (terminfo) {
287 return terminfo_try_path(terminfo, term);
290 // next, consider ~/.terminfo
291 const char *home = getenv("HOME");
292 if (home) {
293 snprintf(tmp, sizeof(tmp), "%s/.terminfo", home);
294 tmp[sizeof(tmp)-1] = '\0';
295 char *data = terminfo_try_path(tmp, term);
296 if (data)
297 return data;
300 // next, TERMINFO_DIRS
301 const char *dirs = getenv("TERMINFO_DIRS");
302 if (dirs) {
303 snprintf(tmp, sizeof(tmp), "%s", dirs);
304 tmp[sizeof(tmp)-1] = '\0';
305 char *dir = strtok(tmp, ":");
306 while (dir) {
307 const char *cdir = dir;
308 if (strcmp(cdir, "") == 0) {
309 cdir = "/usr/share/terminfo";
311 char *data = terminfo_try_path(cdir, term);
312 if (data)
313 return data;
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);
332 strcpy(dst, src);
333 return dst;
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,
343 79, 83,
346 static int init_term(void) {
347 int i;
348 char *data = load_terminfo();
349 if (!data) {
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
360 header[2] += 1;
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;
386 free(data);
387 return 0;
390 static void shutdown_term(void) {
391 if (init_from_terminfo) {
392 int i;
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
398 // need to be freed.
399 for (i = 0; i < T_FUNCS_NUM-2; i++) {
400 free((void*)funcs[i]);
402 free(keys);
403 free(funcs);
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)
414 int n = 0;
415 while (*s2 && n < len) {
416 if (*s1++ != *s2++)
417 return false;
418 n++;
420 return *s2 == 0;
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
426 // \033 [ M Cb Cx Cy
427 int b = buf[3] - 32;
428 switch (b & 3) {
429 case 0:
430 if ((b & 64) != 0)
431 event->key = TB_KEY_MOUSE_WHEEL_UP;
432 else
433 event->key = TB_KEY_MOUSE_LEFT;
434 break;
435 case 1:
436 if ((b & 64) != 0)
437 event->key = TB_KEY_MOUSE_WHEEL_DOWN;
438 else
439 event->key = TB_KEY_MOUSE_MIDDLE;
440 break;
441 case 2:
442 event->key = TB_KEY_MOUSE_RIGHT;
443 break;
444 case 3:
445 event->key = TB_KEY_MOUSE_RELEASE;
446 break;
447 default:
448 return -6;
450 event->type = TB_EVENT_MOUSE; // TB_EVENT_KEY by default
451 if ((b & 32) != 0)
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;
458 return 6;
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) ';'
469 if (buf[i] == ';') {
470 if (s1 == -1)
471 s1 = i;
472 s2 = i;
475 // We search for the first 'm' or 'M'
476 if ((buf[i] == 'm' || buf[i] == 'M') && mi == -1) {
477 mi = i;
478 break;
481 if (mi == -1)
482 return 0;
484 // whether it's a capital M or not
485 isM = (buf[mi] == 'M');
487 if (buf[2] == '<') {
488 isU = 0;
489 starti = 3;
490 } else {
491 isU = 1;
492 starti = 2;
495 if (s1 == -1 || s2 == -1 || s1 == s2)
496 return 0;
498 n1 = strtoul(&buf[starti], NULL, 10);
499 n2 = strtoul(&buf[s1 + 1], NULL, 10);
500 n3 = strtoul(&buf[s2 + 1], NULL, 10);
502 if (isU)
503 n1 -= 32;
505 switch (n1 & 3) {
506 case 0:
507 if ((n1&64) != 0) {
508 event->key = TB_KEY_MOUSE_WHEEL_UP;
509 } else {
510 event->key = TB_KEY_MOUSE_LEFT;
512 break;
513 case 1:
514 if ((n1&64) != 0) {
515 event->key = TB_KEY_MOUSE_WHEEL_DOWN;
516 } else {
517 event->key = TB_KEY_MOUSE_MIDDLE;
519 break;
520 case 2:
521 event->key = TB_KEY_MOUSE_RIGHT;
522 break;
523 case 3:
524 event->key = TB_KEY_MOUSE_RELEASE;
525 break;
526 default:
527 return mi + 1;
530 if (!isM) {
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
536 if ((n1&32) != 0)
537 event->mod |= TB_MOD_MOTION;
539 event->x = (uint8_t)n2 - 1;
540 event->y = (uint8_t)n3 - 1;
542 return mi + 1;
545 return 0;
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)
554 return mouse_parsed;
556 // it's pretty simple here, find 'starts_with' match and return
557 // success, else return failure
558 int i;
559 for (i = 0; keys[i]; i++) {
560 if (starts_with(buf, len, keys[i])) {
561 event->ch = 0;
562 event->key = 0xFFFF-i;
563 return strlen(keys[i]);
566 return 0;
569 static bool extract_event(struct tb_event *event, struct bytebuffer *inbuf)
571 const char *buf = inbuf->buf;
572 const int len = inbuf->len;
573 if (len == 0)
574 return false;
576 if (buf[0] == '\033') {
577 int n = parse_escape_seq(event, buf, len);
578 if (n != 0) {
579 bool success = true;
580 if (n < 0) {
581 success = false;
582 n = -n;
584 bytebuffer_truncate(inbuf, n);
585 return success;
586 } else {
587 // it's not escape sequence, then it's ALT or ESC,
588 // check inputmode
589 if (inputmode&TB_INPUT_ESC) {
590 // if we're in escape mode, fill ESC event, pop
591 // buffer, return success
592 event->ch = 0;
593 event->key = TB_KEY_ESC;
594 event->mod = 0;
595 bytebuffer_truncate(inbuf, 1);
596 return true;
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 */
616 event->ch = 0;
617 event->key = (uint16_t)buf[0];
618 bytebuffer_truncate(inbuf, 1);
619 return true;
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);
628 event->key = 0;
629 bytebuffer_truncate(inbuf, tb_utf8_char_length(buf[0]));
630 return true;
633 // event isn't recognized, perhaps there is not enough bytes in utf8
634 // sequence
635 return false;
638 /* -------------------------------------------------------- */
640 struct cellbuf {
641 int width;
642 int height;
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;
662 static int inout;
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_)
696 inout = inout_;
697 if (inout == -1) {
698 return TB_EFAILED_TO_OPEN_TTY;
701 if (init_term() < 0) {
702 close(inout);
703 return TB_EUNSUPPORTED_TERMINAL;
706 if (pipe(winch_fds) < 0) {
707 close(inout);
708 return TB_EPIPE_TRAP_ERROR;
711 struct sigaction sa;
712 memset(&sa, 0, sizeof(sa));
713 sa.sa_handler = sigwinch_handler;
714 sa.sa_flags = 0;
715 sigaction(SIGWINCH, &sa, 0);
717 tcgetattr(inout, &orig_tios);
719 struct termios 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);
727 tios.c_cflag |= CS8;
728 tios.c_cc[VMIN] = 0;
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]);
738 send_clear();
740 update_term_size();
741 cellbuf_init(&back_buffer, termw, termh);
742 cellbuf_init(&front_buffer, termw, termh);
743 cellbuf_clear(&back_buffer);
744 cellbuf_clear(&front_buffer);
746 return 0;
749 int tb_init_file(const char* name){
750 return tb_init_fd(open(name, O_RDWR));
753 int tb_init(void)
755 return tb_init_file("/dev/tty");
758 void tb_shutdown(void)
760 if (termw == -1) {
761 fputs("tb_shutdown() should not be called twice.", stderr);
762 abort();
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);
774 shutdown_term();
775 close(inout);
776 close(winch_fds[0]);
777 close(winch_fds[1]);
779 cellbuf_free(&back_buffer);
780 cellbuf_free(&front_buffer);
781 bytebuffer_free(&output_buffer);
782 bytebuffer_free(&input_buffer);
783 termw = termh = -1;
786 void tb_present(void)
788 int x,y,w,i;
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) {
796 update_size();
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);
805 if (w < 1) w = 1;
806 if (memcmp(back, front, sizeof(struct tb_cell)) == 0) {
807 x += w;
808 continue;
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, ' ');
817 } else {
818 send_char(x, y, back->ch);
819 for (i = 1; i < w; ++i) {
820 front = &CELL(&front_buffer, x + i, y);
821 front->ch = 0;
822 front->fg = back->fg;
823 front->bg = back->bg;
826 x += w;
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]);
842 cursor_x = cx;
843 cursor_y = cy;
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)
851 return;
852 if ((unsigned)y >= (unsigned)back_buffer.height)
853 return;
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)
866 return;
867 if (y + h < 0 || y >= back_buffer.height)
868 return;
869 int xo = 0, yo = 0, ww = w, hh = h;
870 if (x < 0) {
871 xo = -x;
872 ww -= xo;
873 x = 0;
875 if (y < 0) {
876 yo = -y;
877 hh -= yo;
878 y = 0;
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;
885 int sy;
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;
893 src += w;
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)
909 struct timeval tv;
910 tv.tv_sec = timeout / 1000;
911 tv.tv_usec = (timeout - (tv.tv_sec * 1000)) * 1000;
912 return wait_fill_event(event, &tv);
915 int tb_width(void)
917 return termw;
920 int tb_height(void)
922 return termh;
925 void tb_clear(void)
927 if (buffer_size_change_request) {
928 update_size();
929 buffer_size_change_request = 0;
931 cellbuf_clear(&back_buffer);
934 int tb_select_input_mode(int mode)
936 if (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;
945 inputmode = mode;
946 if (mode&TB_INPUT_MOUSE) {
947 bytebuffer_puts(&output_buffer, funcs[T_ENTER_MOUSE]);
948 bytebuffer_flush(&output_buffer, inout);
949 } else {
950 bytebuffer_puts(&output_buffer, funcs[T_EXIT_MOUSE]);
951 bytebuffer_flush(&output_buffer, inout);
954 return inputmode;
957 int tb_select_output_mode(int mode)
959 if (mode)
960 outputmode = mode;
961 return outputmode;
964 void tb_set_clear_attributes(uint16_t fg, uint16_t bg)
966 foreground = fg;
967 background = bg;
970 /* -------------------------------------------------------- */
972 static int convertnum(uint32_t num, char* buf) {
973 int i, l = 0;
974 int ch;
975 do {
976 buf[l++] = '0' + (num % 10);
977 num /= 10;
978 } while (num);
979 for(i = 0; i < l / 2; i++) {
980 ch = buf[i];
981 buf[i] = buf[l - 1 - i];
982 buf[l - 1 - i] = ch;
984 return l;
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) {
991 char buf[32];
992 WRITE_LITERAL("\033[");
993 WRITE_INT(y+1);
994 WRITE_LITERAL(";");
995 WRITE_INT(x+1);
996 WRITE_LITERAL("H");
999 static void write_sgr(uint16_t fg, uint16_t bg) {
1000 char buf[32];
1002 if (fg == TB_DEFAULT && bg == TB_DEFAULT)
1003 return;
1005 switch (outputmode) {
1006 case TB_OUTPUT_256:
1007 case TB_OUTPUT_216:
1008 case TB_OUTPUT_GRAYSCALE:
1009 WRITE_LITERAL("\033[");
1010 if (fg != TB_DEFAULT) {
1011 WRITE_LITERAL("38;5;");
1012 WRITE_INT(fg);
1013 if (bg != TB_DEFAULT) {
1014 WRITE_LITERAL(";");
1017 if (bg != TB_DEFAULT) {
1018 WRITE_LITERAL("48;5;");
1019 WRITE_INT(bg);
1021 WRITE_LITERAL("m");
1022 break;
1023 case TB_OUTPUT_NORMAL:
1024 default:
1025 WRITE_LITERAL("\033[");
1026 if (fg != TB_DEFAULT) {
1027 WRITE_LITERAL("3");
1028 WRITE_INT(fg - 1);
1029 if (bg != TB_DEFAULT) {
1030 WRITE_LITERAL(";");
1033 if (bg != TB_DEFAULT) {
1034 WRITE_LITERAL("4");
1035 WRITE_INT(bg - 1);
1037 WRITE_LITERAL("m");
1038 break;
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);
1045 assert(buf->cells);
1046 buf->width = width;
1047 buf->height = height;
1050 static void cellbuf_resize(struct cellbuf *buf, int width, int height)
1052 if (buf->width == width && buf->height == height)
1053 return;
1055 int oldw = buf->width;
1056 int oldh = buf->height;
1057 struct tb_cell *oldcells = buf->cells;
1059 cellbuf_init(buf, width, height);
1060 cellbuf_clear(buf);
1062 int minw = (width < oldw) ? width : oldw;
1063 int minh = (height < oldh) ? height : oldh;
1064 int i;
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);
1072 free(oldcells);
1075 static void cellbuf_clear(struct cellbuf *buf)
1077 int i;
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)
1089 free(buf->cells);
1092 static void get_term_size(int *w, int *h)
1094 struct winsize sz;
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)
1105 struct winsize sz;
1106 memset(&sz, 0, sizeof(sz));
1108 ioctl(inout, TIOCGWINSZ, &sz);
1110 termw = sz.ws_col;
1111 termh = sz.ws_row;
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]);
1121 uint16_t fgcol;
1122 uint16_t bgcol;
1124 switch (outputmode) {
1125 case TB_OUTPUT_256:
1126 fgcol = fg & 0xFF;
1127 bgcol = bg & 0xFF;
1128 break;
1130 case TB_OUTPUT_216:
1131 fgcol = fg & 0xFF; if (fgcol > 215) fgcol = 7;
1132 bgcol = bg & 0xFF; if (bgcol > 215) bgcol = 0;
1133 fgcol += 0x10;
1134 bgcol += 0x10;
1135 break;
1137 case TB_OUTPUT_GRAYSCALE:
1138 fgcol = fg & 0xFF; if (fgcol > 23) fgcol = 23;
1139 bgcol = bg & 0xFF; if (bgcol > 23) bgcol = 0;
1140 fgcol += 0xe8;
1141 bgcol += 0xe8;
1142 break;
1144 case TB_OUTPUT_NORMAL:
1145 default:
1146 fgcol = fg & 0x0F;
1147 bgcol = bg & 0x0F;
1150 if (fg & TB_BOLD)
1151 bytebuffer_puts(&output_buffer, funcs[T_BOLD]);
1152 if (bg & TB_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);
1161 lastfg = fg;
1162 lastbg = bg;
1166 static void send_char(int x, int y, uint32_t c)
1168 char buf[7];
1169 int bw = tb_utf8_unicode_to_char(buf, c);
1170 if (x-1 != lastx || y != lasty)
1171 write_cursor(x, y);
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
1189 * cursor moved */
1190 lastx = LAST_COORD_INIT;
1191 lasty = LAST_COORD_INIT;
1194 static void sigwinch_handler(int xxx)
1196 (void) xxx;
1197 const int zzz = 1;
1198 (void)write(winch_fds[1], &zzz, sizeof(int));
1201 static void update_size(void)
1203 update_term_size();
1204 cellbuf_resize(&back_buffer, termw, termh);
1205 cellbuf_resize(&front_buffer, termw, termh);
1206 cellbuf_clear(&front_buffer);
1207 send_clear();
1210 static int read_up_to(int n) {
1211 assert(n > 0);
1212 const int prevlen = input_buffer.len;
1213 bytebuffer_resize(&input_buffer, prevlen + n);
1215 int read_n = 0;
1216 while (read_n <= n) {
1217 ssize_t r = 0;
1218 if (read_n < n) {
1219 r = read(inout, input_buffer.buf + prevlen + read_n, n - read_n);
1221 #ifdef __CYGWIN__
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
1225 // it's zero.
1226 if (r < 0) r = 0;
1227 #endif
1228 if (r < 0) {
1229 // EAGAIN / EWOULDBLOCK shouldn't occur here
1230 assert(errno != EAGAIN && errno != EWOULDBLOCK);
1231 return -1;
1232 } else if (r > 0) {
1233 read_n += r;
1234 } else {
1235 bytebuffer_resize(&input_buffer, prevlen + read_n);
1236 return read_n;
1239 assert(!"unreachable");
1240 return 0;
1243 static int wait_fill_event(struct tb_event *event, struct timeval *timeout)
1245 // ;-)
1246 #define ENOUGH_DATA_FOR_PARSING 64
1247 fd_set events;
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))
1253 return event->type;
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);
1258 if (n < 0)
1259 return -1;
1260 if (n > 0 && extract_event(event, &input_buffer))
1261 return event->type;
1263 // n == 0, or not enough data, let's go to select
1264 while (1) {
1265 FD_ZERO(&events);
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);
1270 if (!result)
1271 return 0;
1273 if (FD_ISSET(inout, &events)) {
1274 event->type = TB_EVENT_KEY;
1275 n = read_up_to(ENOUGH_DATA_FOR_PARSING);
1276 if (n < 0)
1277 return -1;
1279 if (n == 0)
1280 continue;
1282 if (extract_event(event, &input_buffer))
1283 return event->type;
1285 if (FD_ISSET(winch_fds[0], &events)) {
1286 event->type = TB_EVENT_RESIZE;
1287 int zzz = 0;
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;