2 #include "console_keys.h"
4 #include "../termbox/src/termbox.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
) {
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
);
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
) {
51 /* prints a char and NOT advances cursor */
52 void console_addchar(struct Console
* self
, int c
, unsigned int 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) {
76 str += utf8_char_to_unicode(&uni, str);
77 tb_change_cell(x, y, uni, fg, bg);
82 void console_printf (struct Console* con, const char* fmt, ...) {
83 struct TbConsole *self = (struct TbConsole*) con;
84 console_initoutput(con);
88 ssize_t result = vsnprintf(buf, sizeof(buf), fmt, 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);
100 ssize_t result = vsnprintf(buf, sizeof(buf), fmt, 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
;
136 if (e
->key
>= TB_KEY_MIN
) {
138 if(e
->key
- TB_KEY_MIN
>= (ssize_t
) key_table_size
) {
142 ret
= key_table
[e
->key
- TB_KEY_MIN
] + 0x100;
144 } else if (e
->ch
< 128) {
145 if (e
->ch
== 0 && e
->key
< 128)
150 if (e
->mod
& TB_MOD_ALT
) ret
|= CK_MOD_ALT
;
154 /* blocking getkey */
155 int console_getkey(struct Console
* self
) {
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
) {
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
176 int msleep(long millisecs
) {
177 struct timespec req
, rem
;
178 req
.tv_sec
= millisecs
/ 1000;
179 req
.tv_nsec
= (millisecs
% 1000) * 1000 * 1000;
181 while((ret
= nanosleep(&req
, &rem
)) == -1 && errno
== EINTR
) req
= rem
;
185 void console_sleep(struct Console
* self
, int ms
) {
190 void console_refresh(struct Console
* self
) {
195 void console_clear(struct Console
* self
) {
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
;
208 tb_select_input_mode(TB_INPUT_ESC
);
210 self
->dim
.x
= tb_width();
211 self
->dim
.y
= tb_height();
214 void console_settitle(Console
*self
, const char *title
) {