README: mention SDL2 backend
[rofl0r-concol.git] / libconcol.lua
blob14de87b63ee9a966fa461a0dcf92d399dfbfab83
1 -- concol LuaJIT FFI binding
2 -- Copyright (C) 2012 rofl0r
4 local ffi = require("ffi")
6 ffi.cdef[[
7 typedef struct {
8 int x;
9 int y;
10 } point;
12 typedef struct {
13 point topleft;
14 point bottomright;
15 } rect;
17 typedef enum {
18 EV_MOUSE = 0,
19 EV_KEY_DOWN = 1,
20 EV_TIMER = 2,
21 EV_RESIZE = 3,
22 EV_COUNT = 4, // the number of total event types
23 } event_type;
25 typedef enum {
26 MB_NONE = -1,
27 MB_LEFT = 0,
28 MB_RIGHT = 1,
29 MB_MIDDLE = 2,
30 MB_COUNT = 3
31 } mouse_button;
33 typedef enum {
34 ME_BUTTON_DOWN = 0,
35 ME_BUTTON_UP,
36 ME_WHEEL_DOWN,
37 ME_WHEEL_UP,
38 ME_MOVE,
39 } mouse_event_type;
41 typedef struct {
42 point coords;
43 mouse_event_type mouse_ev;
44 mouse_button button;
45 } mouse_event;
47 typedef struct {
48 unsigned long counter;
49 int active:1; // the timer event is sent to all "subscribed" windows.
50 //this is to make them aware if the window is active or not.
51 } timer_event;
53 typedef struct {
54 rect newcoords;
55 } resize_event;
57 typedef enum {
58 MOD_SHIFT = 1 << 0,
59 MOD_ALT = 1 << 1,
60 MOD_CTRL = 1 << 2,
61 MOD_ALTGR = 1 << 3,
62 MOD_FLAG = 1 << 4,
63 } key_modifier;
65 typedef struct {
66 int ch;
67 key_modifier modifiers;
68 } key_event;
70 // this struct requires std=gnu89 or gnu99 to work. i am to lazy to make the union non-anonymous.
71 typedef struct {
72 event_type etype;
73 union {
74 mouse_event mouse_ev;
75 timer_event timer_ev;
76 key_event key_ev;
77 resize_event resize_ev;
79 } event;
81 typedef union {
82 struct {
83 unsigned char a;
84 unsigned char b;
85 unsigned char g;
86 unsigned char r;
88 uint32_t asInt;
89 } rgb_t;
91 typedef struct {
92 rgb_t bgcolor;
93 rgb_t fgcolor;
94 } rgb_tuple;
96 typedef union {
97 struct colors {
98 unsigned char b;
99 unsigned char g;
100 unsigned char r;
101 unsigned char a;
102 } colors;
103 uint32_t val;
104 } sdl_rgb_t;
106 typedef struct {
107 sdl_rgb_t bgcolor;
108 sdl_rgb_t fgcolor;
109 } sdl_rgb_tuple;
111 typedef struct {
112 point dim;
113 unsigned long pointsperchar;
114 char* characters;
115 } font;
117 typedef struct SDLConsole {
118 void *surface;
119 sdl_rgb_tuple color;
120 point res;
121 font* fnt;
122 int paintmode:1;
123 int cursorblink:1;
124 int fullscreen:1;
125 } SDLConsole;
127 typedef struct TbConsole {
128 int fgcolor;
129 int bgcolor;
130 } TbConsole;
132 typedef struct {
133 int fgcol;
134 int bgcol;
135 } Colorpair;
137 typedef struct NcConsole {
138 char org_term[32];
139 int hasColors:1;
140 int canChangeColors:1;
141 int hasMouse:1;
142 int maxcolor;
144 //attr_t lastattr;
145 unsigned int lastattr;
146 Colorpair lastused;
147 Colorpair active;
148 Colorpair termPairs[256];
149 rgb_t colors[256];
151 rgb_t org_colors[256];
152 short int org_fgcolors[256];
153 short int org_bgcolors[256];
154 } NcConsole;
156 enum ConsoleBackend {
157 cb_sdl = 0,
158 cb_ncurses,
159 cb_termbox,
162 typedef struct Console {
163 enum ConsoleBackend backendtype;
164 point cursor;
165 point dim; //dimensions
166 mouse_event mouse;
167 int automove:1; // flag which affects putchar and printf (cursor will be advanced)
168 int isblinking:1;
169 union {
170 TbConsole tb;
171 SDLConsole sdl;
172 NcConsole nc;
173 } backend;
174 } Console;
176 void console_init(struct Console* self);
177 void console_cleanup(struct Console* self);
178 int console_setcolor(struct Console* self, int is_fg, rgb_t mycolor);
179 int console_setcolors(struct Console* self, rgb_t bgcolor, rgb_t fgcolor);
180 void console_initoutput(struct Console* self);
181 void console_getbounds(struct Console* self, int* width, int* height);
182 void console_getcursor(struct Console* self, int* x, int* y);
183 void console_goto(struct Console* self, int x, int y);
184 /* prints a char and NOT advances cursor */
185 void console_addchar(struct Console* self, int c, unsigned int attributes);
186 /* prints a char and advances cursor */
187 void console_printchar(struct Console* self, int c, unsigned int attributes);
188 /* prints a char and updates (redraws) the screen when doupdate is 1. advances cursor if automove is set. */
189 void console_putchar(Console* self, int ch, int doupdate);
190 void console_printf (struct Console* con, const char* fmt, ...);
191 /* blocking */
192 int console_getkey(struct Console* self);
193 /* non-blocking */
194 int console_getkey_nb(struct Console* self);
195 void console_sleep(struct Console* self, int ms);
196 void console_refresh(struct Console* self);
197 void console_clear(struct Console* self);
198 void console_lock(void);
199 void console_unlock(void);
200 void console_blink_cursor(struct Console* self);
201 void console_fill(Console *c, rect* area, int ch);
202 mouse_event console_getmouse(Console* c);
203 void console_advance_cursor(Console* c, int steps);
204 void console_setautomove(Console* c, int automove);
205 void console_linebreak(Console* c);
206 void console_cursor_up(Console* c);
207 void console_cursor_down(Console* c);
208 void console_cursor_left(Console* c);
209 void console_cursor_right(Console* c);
210 void console_unblink(Console* c);
211 enum ConsoleBackend console_getbackendtype(Console *c);
212 void console_toggle_fullscreen(Console *c);
213 void console_init_graphics(Console* self, point resolution, font* fnt);
215 extern font testfont;
219 local N = {} -- exported functions and types
221 local concol_n = ffi.load("./libconcol256.so") -- libnessdb.so namespace
222 N.buffer_t = ffi.typeof("char[?]") -- char buffer type
223 N.console_t = ffi.typeof("struct Console")
224 N.font_t = ffi.typeof("font")
225 N.console = ffi.new("struct Console")
226 N.rgb_t = ffi.typeof("rgb_t")
230 function N.openlib(lib_path)
231 concol_n = ffi.load(lib_path)
234 function N.init()
235 concol_n.console_init(N.console)
238 function N.init_graphics(xres, yres)
239 local pt = ffi.new("point");
240 local fnt = ffi.cast("font *", N.testfont)
241 pt.x = xres
242 pt.y = yres
243 concol_n.console_init_graphics(N.console, pt, fnt)
247 function N.cleanup()
248 concol_n.console_cleanup(N.console)
251 function N.getkey()
252 return concol_n.console_getkey(N.console)
255 function N.goto(x, y)
256 concol_n.console_goto(N.console, x, y)
259 function N.putchar(ch, doupdate)
260 concol_n.console_putchar(N.console, string.byte(ch), doupdate)
263 function N.rgb(r, g, b)
264 local col
265 col = N.rgb_t(0, b, g, r)
266 return col
269 function N.setcolor(isfg, col)
270 concol_n.console_setcolor(N.console, isfg, col)
274 return N