1 -- concol LuaJIT FFI binding
2 -- Copyright (C) 2012 rofl0r
4 local ffi
= require("ffi")
22 EV_COUNT = 4, // the number of total event types
43 mouse_event_type mouse_ev;
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.
67 key_modifier modifiers;
70 // this struct requires std=gnu89 or gnu99 to work. i am to lazy to make the union non-anonymous.
77 resize_event resize_ev;
113 unsigned long pointsperchar;
117 typedef struct SDLConsole {
127 typedef struct TbConsole {
137 typedef struct NcConsole {
140 int canChangeColors:1;
145 unsigned int lastattr;
148 Colorpair termPairs[256];
151 rgb_t org_colors[256];
152 short int org_fgcolors[256];
153 short int org_bgcolors[256];
156 enum ConsoleBackend {
162 typedef struct Console {
163 enum ConsoleBackend backendtype;
165 point dim; //dimensions
167 int automove:1; // flag which affects putchar and printf (cursor will be advanced)
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, ...);
192 int console_getkey(struct Console* self);
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
)
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
)
243 concol_n
.console_init_graphics(N
.console
, pt
, fnt
)
248 concol_n
.console_cleanup(N
.console
)
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
)
265 col
= N
.rgb_t(0, b
, g
, r
)
269 function N
.setcolor(isfg
, col
)
270 concol_n
.console_setcolor(N
.console
, isfg
, col
)