termbox.h (5049B)
1 #ifndef TERMBOX_H 2 #define TERMBOX_H 3 4 #include <stdbool.h> 5 #include <stdint.h> 6 7 /* for shared objects */ 8 #if __GNUC__ >= 4 9 #define SO_IMPORT __attribute__((visibility("default"))) 10 #else 11 #define SO_IMPORT 12 #endif 13 14 #ifdef __cplusplus 15 extern "C" { 16 #endif 17 18 /* --------------- keys ---------------- */ 19 20 /* These are safe subset of terminfo keys, which exists on all popular terminals. 21 I think it's important to use only these and not others. 22 */ 23 #define TB_KEY_F1 (0xFFFF-0) 24 #define TB_KEY_F2 (0xFFFF-1) 25 #define TB_KEY_F3 (0xFFFF-2) 26 #define TB_KEY_F4 (0xFFFF-3) 27 #define TB_KEY_F5 (0xFFFF-4) 28 #define TB_KEY_F6 (0xFFFF-5) 29 #define TB_KEY_F7 (0xFFFF-6) 30 #define TB_KEY_F8 (0xFFFF-7) 31 #define TB_KEY_F9 (0xFFFF-8) 32 #define TB_KEY_F10 (0xFFFF-9) 33 #define TB_KEY_F11 (0xFFFF-10) 34 #define TB_KEY_F12 (0xFFFF-11) 35 #define TB_KEY_INSERT (0xFFFF-12) 36 #define TB_KEY_DELETE (0xFFFF-13) 37 #define TB_KEY_HOME (0xFFFF-14) 38 #define TB_KEY_END (0xFFFF-15) 39 #define TB_KEY_PGUP (0xFFFF-16) 40 #define TB_KEY_PGDN (0xFFFF-17) 41 #define TB_KEY_ARROW_UP (0xFFFF-18) 42 #define TB_KEY_ARROW_DOWN (0xFFFF-19) 43 #define TB_KEY_ARROW_LEFT (0xFFFF-20) 44 #define TB_KEY_ARROW_RIGHT (0xFFFF-21) 45 46 /* These are all keys below SPACE character and BACKSPACE key */ 47 #define TB_KEY_CTRL_TILDE 0x00 48 #define TB_KEY_CTRL_2 0x00 /* clash with 'CTRL_TILDE' */ 49 #define TB_KEY_CTRL_A 0x01 50 #define TB_KEY_CTRL_B 0x02 51 #define TB_KEY_CTRL_C 0x03 52 #define TB_KEY_CTRL_D 0x04 53 #define TB_KEY_CTRL_E 0x05 54 #define TB_KEY_CTRL_F 0x06 55 #define TB_KEY_CTRL_G 0x07 56 #define TB_KEY_BACKSPACE 0x08 57 #define TB_KEY_CTRL_H 0x08 /* clash with 'CTRL_BACKSPACE' */ 58 #define TB_KEY_TAB 0x09 59 #define TB_KEY_CTRL_I 0x09 /* clash with 'TAB' */ 60 #define TB_KEY_CTRL_J 0x0A 61 #define TB_KEY_CTRL_K 0x0B 62 #define TB_KEY_CTRL_L 0x0C 63 #define TB_KEY_ENTER 0x0D 64 #define TB_KEY_CTRL_M 0x0D /* clash with 'ENTER' */ 65 #define TB_KEY_CTRL_N 0x0E 66 #define TB_KEY_CTRL_O 0x0F 67 #define TB_KEY_CTRL_P 0x10 68 #define TB_KEY_CTRL_Q 0x11 69 #define TB_KEY_CTRL_R 0x12 70 #define TB_KEY_CTRL_S 0x13 71 #define TB_KEY_CTRL_T 0x14 72 #define TB_KEY_CTRL_U 0x15 73 #define TB_KEY_CTRL_V 0x16 74 #define TB_KEY_CTRL_W 0x17 75 #define TB_KEY_CTRL_X 0x18 76 #define TB_KEY_CTRL_Y 0x19 77 #define TB_KEY_CTRL_Z 0x1A 78 #define TB_KEY_ESC 0x1B 79 #define TB_KEY_CTRL_LSQ_BRACKET 0x1B /* clash with 'ESC' */ 80 #define TB_KEY_CTRL_3 0x1B /* clash with 'ESC' */ 81 #define TB_KEY_CTRL_4 0x1C 82 #define TB_KEY_CTRL_BACKSLASH 0x1C /* clash with 'CTRL_4' */ 83 #define TB_KEY_CTRL_5 0x1D 84 #define TB_KEY_CTRL_RSQ_BRACKET 0x1D /* clash with 'CTRL_5' */ 85 #define TB_KEY_CTRL_6 0x1E 86 #define TB_KEY_CTRL_7 0x1F 87 #define TB_KEY_CTRL_SLASH 0x1F /* clash with 'CTRL_7' */ 88 #define TB_KEY_CTRL_UNDERSCORE 0x1F /* clash with 'CTRL_7' */ 89 #define TB_KEY_SPACE 0x20 90 #define TB_KEY_BACKSPACE2 0x7F 91 #define TB_KEY_CTRL_8 0x7F /* clash with 'DELETE' */ 92 93 /* These are fail ones (do not exist) */ 94 /* #define TB_KEY_CTRL_1 clash with '1' */ 95 /* #define TB_KEY_CTRL_9 clash with '9' */ 96 /* #define TB_KEY_CTRL_0 clash with '0' */ 97 98 /* Currently there is only one modificator */ 99 #define TB_MOD_ALT 0x01 100 101 /* colors */ 102 #define TB_BLACK 0x00 103 #define TB_RED 0x01 104 #define TB_GREEN 0x02 105 #define TB_YELLOW 0x03 106 #define TB_BLUE 0x04 107 #define TB_MAGENTA 0x05 108 #define TB_CYAN 0x06 109 #define TB_WHITE 0x07 110 #define TB_DEFAULT 0x0F 111 112 /* attributes */ 113 #define TB_BOLD 0x10 114 #define TB_UNDERLINE 0x20 115 116 struct tb_cell { 117 uint32_t ch; 118 uint16_t fg; 119 uint16_t bg; 120 }; 121 122 struct tb_event { 123 uint8_t type; 124 uint8_t mod; 125 uint16_t key; 126 uint32_t ch; 127 int32_t w; 128 int32_t h; 129 }; 130 131 #define TB_EUNSUPPORTED_TERMINAL -1 132 #define TB_EFAILED_TO_OPEN_TTY -2 133 #define TB_EPIPE_TRAP_ERROR -3 134 135 SO_IMPORT int tb_init(void); 136 SO_IMPORT void tb_shutdown(void); 137 138 SO_IMPORT unsigned int tb_width(void); 139 SO_IMPORT unsigned int tb_height(void); 140 141 SO_IMPORT void tb_clear(void); 142 SO_IMPORT void tb_present(void); 143 144 #define TB_HIDE_CURSOR -1 145 SO_IMPORT void tb_set_cursor(int cx, int cy); 146 147 SO_IMPORT void tb_put_cell(unsigned int x, unsigned int y, const struct tb_cell *cell); 148 SO_IMPORT void tb_change_cell(unsigned int x, unsigned int y, uint32_t ch, uint16_t fg, uint16_t bg); 149 SO_IMPORT void tb_blit(unsigned int x, unsigned int y, unsigned int w, unsigned int h, const struct tb_cell *cells); 150 151 #define TB_INPUT_ESC 1 152 #define TB_INPUT_ALT 2 153 /* with 0 returns current input mode */ 154 SO_IMPORT int tb_select_input_mode(int mode); 155 SO_IMPORT void tb_set_clear_attributes(uint16_t fg, uint16_t bg); 156 157 #define TB_EVENT_KEY 1 158 #define TB_EVENT_RESIZE 2 159 160 SO_IMPORT int tb_peek_event(struct tb_event *event, unsigned int timeout); 161 SO_IMPORT int tb_poll_event(struct tb_event *event); 162 /* these return: 163 0 - no events, no errors, 164 1 - key event 165 2 - resize event 166 -1 - error (input buffer overflow, discarded input) 167 168 timeout in milliseconds 169 */ 170 171 172 /* glib based interface */ 173 /* TODO */ 174 175 /* utility utf8 functions */ 176 #define TB_EOF -1 177 SO_IMPORT int utf8_char_length(char c); 178 SO_IMPORT int utf8_char_to_unicode(uint32_t *out, const char *c); 179 SO_IMPORT int utf8_unicode_to_char(char *out, uint32_t c); 180 181 #ifdef __cplusplus 182 } 183 #endif 184 185 #endif /* ifndef TERMBOX_H */