README: mention SDL2 backend
[rofl0r-concol.git] / tbconsole.c
blobc786c9963c37745da76fc649ef1dd075d8112aa8
1 #include "console.h"
2 #include "console_keys.h"
3 #include "colors.h"
4 #include "../termbox/src/termbox.h"
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <stddef.h>
8 #include <unistd.h>
9 #include <string.h>
11 #pragma RcB2 DEP "tbconsole_chartab.c"
13 #define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
15 /* initialize a Console struct */
16 void console_init(struct Console* self) {
17 memset(self, 0, sizeof(struct Console));
18 self->backendtype = cb_termbox;
21 /* cleanup restores the original term behaviour and releases acquired resources. */
22 void console_cleanup(struct Console* self) {
23 (void) self;
24 tb_shutdown();
27 #include "nearestcolor.c"
29 int console_setcolor(struct Console* self, int is_fg, rgb_t mycolor) {
30 struct TbConsole *c = &self->backend.tb;
31 int *dest = is_fg ? &c->fgcolor : &c->bgcolor;
32 *dest = getNearestColor(mycolor);
33 return 1;
36 int console_getcolorcount(Console *self) { (void) self; return 8; }
38 void console_initoutput(struct Console* self) { (void) self; }
40 /* get width and height of the console display (in characters) */
41 void console_getbounds(struct Console* self, int* width, int* height) {
42 self->dim.x = *width = tb_width();
43 self->dim.y = *height = tb_height();
46 void console_goto(struct Console* self, int x, int y) {
47 self->cursor.x = x;
48 self->cursor.y = y;
51 /* prints a char and NOT advances cursor */
52 void console_addchar(struct Console* self, int c, unsigned int attributes) {
53 (void) attributes;
54 struct TbConsole *con = &self->backend.tb;
55 tb_change_cell(self->cursor.x, self->cursor.y, c, con->fgcolor, con->bgcolor);
57 /* prints a char and advances cursor */
58 void console_printchar(struct Console* self, int c, unsigned int attributes) {
59 int newx = self->cursor.x == (int) tb_width() ? 1 : self->cursor.x + 1;
60 int newy = self->cursor.x == (int) tb_width() ? self->cursor.y + 1 : self->cursor.y;
61 console_addchar(self, c, attributes);
62 console_goto(self, newx, newy);
65 void console_putchar(Console* self, int ch, int doupdate) {
66 console_addchar(self, ch, 0);
67 if(self->automove) console_advance_cursor(self, 1);
68 if(doupdate) console_refresh(self);
73 static void print_tb(const char *str, unsigned int x, unsigned int y, uint16_t fg, uint16_t bg) {
74 while (*str) {
75 uint32_t uni;
76 str += utf8_char_to_unicode(&uni, str);
77 tb_change_cell(x, y, uni, fg, bg);
78 x++;
82 void console_printf (struct Console* con, const char* fmt, ...) {
83 struct TbConsole *self = (struct TbConsole*) con;
84 console_initoutput(con);
85 char buf[256];
86 va_list ap;
87 va_start(ap, fmt);
88 ssize_t result = vsnprintf(buf, sizeof(buf), fmt, ap);
89 (void) result;
90 va_end(ap);
91 print_tb (buf, con->cursor.x, con->cursor.y, self->fgcolor, self->bgcolor);
94 void console_printfxy (struct Console* con, int x, int y, const char* fmt, ...) {
95 struct TbConsole *self = (struct TbConsole*) con;
96 console_initoutput(con);
97 char buf[256];
98 va_list ap;
99 va_start(ap, fmt);
100 ssize_t result = vsnprintf(buf, sizeof(buf), fmt, ap);
101 (void) result;
102 va_end(ap);
103 print_tb (buf, x, y, self->fgcolor, self->bgcolor);
105 #define TB_KEY_MIN TB_KEY_ARROW_RIGHT
106 #define TB_KEY_MAX TB_KEY_F1
107 static const unsigned char key_table[] = {
108 [TB_KEY_F1 - TB_KEY_MIN] = CK_F1 - 0x100,
109 [TB_KEY_F2 - TB_KEY_MIN] = CK_F2 - 0x100,
110 [TB_KEY_F3 - TB_KEY_MIN] = CK_F3 - 0x100,
111 [TB_KEY_F4 - TB_KEY_MIN] = CK_F4 - 0x100,
112 [TB_KEY_F5 - TB_KEY_MIN] = CK_F5 - 0x100,
113 [TB_KEY_F6 - TB_KEY_MIN] = CK_F6 - 0x100,
114 [TB_KEY_F7 - TB_KEY_MIN] = CK_F7 - 0x100,
115 [TB_KEY_F8 - TB_KEY_MIN] = CK_F8 - 0x100,
116 [TB_KEY_F9 - TB_KEY_MIN] = CK_F9 - 0x100,
117 [TB_KEY_F10 - TB_KEY_MIN] = CK_F10 - 0x100,
118 [TB_KEY_F11 - TB_KEY_MIN] = CK_F11 - 0x100,
119 [TB_KEY_F12 - TB_KEY_MIN] = CK_F12 - 0x100,
120 [TB_KEY_INSERT - TB_KEY_MIN] = CK_INS - 0x100,
121 [TB_KEY_DELETE - TB_KEY_MIN] = CK_DEL - 0x100,
122 [TB_KEY_HOME - TB_KEY_MIN] = CK_HOME - 0x100,
123 [TB_KEY_END - TB_KEY_MIN] = CK_END - 0x100,
124 [TB_KEY_PGUP - TB_KEY_MIN] = CK_PAGE_UP - 0x100,
125 [TB_KEY_PGDN - TB_KEY_MIN] = CK_PAGE_DOWN - 0x100,
126 [TB_KEY_ARROW_UP - TB_KEY_MIN] = CK_CURSOR_UP - 0x100,
127 [TB_KEY_ARROW_DOWN - TB_KEY_MIN] = CK_CURSOR_DOWN - 0x100,
128 [TB_KEY_ARROW_LEFT - TB_KEY_MIN] = CK_CURSOR_LEFT - 0x100,
129 [TB_KEY_ARROW_RIGHT - TB_KEY_MIN] = CK_CURSOR_RIGHT - 0x100,
131 #define key_table_size (sizeof(key_table) / sizeof(key_table[0]))
133 static int event_to_key(int retval, struct tb_event *e) {
134 if(retval == TB_EVENT_RESIZE) return CK_RESIZE_EVENT;
135 int ret = 0;
136 if (e->key >= TB_KEY_MIN) {
137 // special key
138 if(e->key - TB_KEY_MIN >= (ssize_t) key_table_size) {
139 //BUG
140 return CK_ERR;
142 ret = key_table[e->key - TB_KEY_MIN] + 0x100;
144 } else if (e->ch < 128) {
145 if (e->ch == 0 && e->key < 128)
146 ret = e->key;
147 else
148 ret = e->ch;
150 if (e->mod & TB_MOD_ALT) ret |= CK_MOD_ALT;
151 return ret;
154 /* blocking getkey */
155 int console_getkey(struct Console* self) {
156 (void) self;
157 struct tb_event e;
158 int ret = tb_poll_event(&e);
159 if(ret == 0) ret = -1;
160 return event_to_key(ret, &e);
163 /* non blocking getkey. returns -1 if no data is available */
164 int console_getkey_nb(struct Console* self) {
165 (void) self;
166 struct tb_event e;
167 int ret = tb_peek_event(&e, 0);
168 if(ret == 0) ret = -1;
169 return event_to_key(ret, &e);
171 #undef _POSIX_C_SOURCE
172 #define _POSIX_C_SOURCE 200809L
173 #include <time.h>
174 #include <errno.h>
176 int msleep(long millisecs) {
177 struct timespec req, rem;
178 req.tv_sec = millisecs / 1000;
179 req.tv_nsec = (millisecs % 1000) * 1000 * 1000;
180 int ret;
181 while((ret = nanosleep(&req, &rem)) == -1 && errno == EINTR) req = rem;
182 return ret;
185 void console_sleep(struct Console* self, int ms) {
186 (void) self;
187 msleep(ms);
190 void console_refresh(struct Console* self) {
191 (void) self;
192 tb_present();
195 void console_clear(struct Console* self) {
196 (void) self;
197 tb_clear();
200 void console_blink_cursor(struct Console* self) { (void) self; }
201 void console_lock(void) {}
202 void console_unlock(void) {}
203 void console_toggle_fullscreen(struct Console* self) { (void) self; }
205 void console_init_graphics(Console* self, point resolution, font* fnt) {
206 (void) resolution; (void) fnt;
207 tb_init();
208 tb_select_input_mode(TB_INPUT_ESC);
209 tb_clear();
210 self->dim.x = tb_width();
211 self->dim.y = tb_height();
214 void console_settitle(Console *self, const char *title) {
215 (void) self;
216 (void) title;