tui.c (952B)
1 #include "tui.h" 2 3 #define TB_IMPL 4 #include "termbox2.h" 5 6 int 7 present(app_t* app) 8 { 9 struct tb_event ev = { 0 }; 10 int can_poll = -1; 11 int cannot_init = tb_init(); 12 13 tb_hide_cursor(); 14 15 if (cannot_init) { 16 fprintf(stderr, "could not TUI\n"); 17 return 1; 18 } 19 20 render(app); 21 22 while (1) { 23 can_poll = tb_poll_event(&ev); 24 25 if (can_poll == TB_OK) { 26 switch (ev.type) { 27 case (TB_EVENT_KEY): 28 29 if (ev.key == TB_KEY_CTRL_Q // 30 || ev.key == TB_KEY_ESC // 31 || ev.key == TB_KEY_CTRL_C // 32 || ev.ch == 'q') 33 goto RIP; 34 35 handle_key(app, ev); 36 37 break; 38 case (TB_EVENT_MOUSE): 39 break; 40 case (TB_EVENT_RESIZE): 41 break; 42 } 43 44 render(app); 45 46 } else if (can_poll == TB_ERR_POLL && tb_last_errno() == EINTR) { 47 continue; 48 49 } else if (can_poll != TB_ERR_NO_EVENT) { 50 fprintf(stderr, "(aborting) renderer error: %s\n", tb_strerror(can_poll)); 51 goto RIP; 52 } 53 } 54 55 RIP: 56 tb_shutdown(); 57 printf("\n"); 58 return 0; 59 }