static int lua_tb_init( lua_State *L ) { int err = tb_init(); struct luabox_State *lbstate; if ( err ) { lua_pushnil( L ); lua_pushinteger( L, err ); return 2; } if ( lua_gettop( L ) >= 1 ) { if ( lua_isnumber( L, 1 )) { uint16_t inputmode = (uint16_t) lua_tonumber( L, 1 ); tb_select_input_mode( inputmode ); } } if ( lua_gettop( L ) >= 2 ) { if ( lua_isnumber( L, 2 )) { uint16_t ouputmode = (uint16_t) lua_tonumber( L, 2 ); tb_select_output_mode( ouputmode ); } } lua_pushlightuserdata( L, L ); lbstate = lua_newuserdata( L, sizeof *lbstate ); lua_settable( L, LUA_REGISTRYINDEX ); return 0; }
inline Manager(std::unique_ptr<Widget>&& widget) { tb_init(); tb_select_input_mode(TB_INPUT_ESC | TB_INPUT_MOUSE); tb_select_output_mode(TB_OUTPUT_256); root = std::move(widget); resize(tb_width(), tb_height()); }
int main(int argv, char **argc) { int code = tb_init(); if (code < 0) { fprintf(stderr, "termbox init failed, code: %d\n", code); return -1; } tb_select_input_mode(TB_INPUT_ESC | TB_INPUT_MOUSE); int w = tb_width(); int h = tb_height(); reallocBackBuffer(w, h); updateAndRedrawAll(-1, -1); for (;;) { struct tb_event ev; int mx = -1; int my = -1; int t = tb_poll_event(&ev); if (t == -1) { tb_shutdown(); fprintf(stderr, "termbox poll event error\n"); return -1; } switch (t) { case TB_EVENT_KEY: if (ev.key == TB_KEY_ESC) { tb_shutdown(); return 0; } break; case TB_EVENT_MOUSE: if (ev.key == TB_KEY_MOUSE_LEFT) { mx = ev.x; my = ev.y; } break; case TB_EVENT_RESIZE: reallocBackBuffer(ev.w, ev.h); break; } updateAndRedrawAll(mx, my); } }
static int lua_tb_select_input_mode( lua_State *L ) { int mode = luaL_checkint( L, 1 ); lua_pushnumber( L, tb_select_input_mode( mode )); return 1; }