int main(int argc, char * argv[]) { int ret = tb_init(); if(ret) { fprintf(stderr, "tb_init() failed with error code %d\n", ret); return 1; } draw(); // event loop struct tb_event event; while(tb_poll_event(&event)) { switch(event.type) { case TB_EVENT_KEY: if(event.key == TB_KEY_ESC){ goto done; } break; case TB_EVENT_RESIZE: break; } } done: tb_shutdown(); return 0; }
char Window::wait_for_input() { // Blocks until keypress tb_poll_event(&input); switch (input.key) { case TB_KEY_BACKSPACE2: return '\b'; case TB_KEY_ENTER: return '\n'; case TB_KEY_SPACE: return ' '; case TB_KEY_TAB: return '\t'; } char c; tb_utf8_unicode_to_char(&c, input.ch); return c; }
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_peek_event( lua_State *L ) { int event_type = 0; struct luabox_State *lbstate; struct tb_event *event_struct; lua_pushlightuserdata( L, L ); lua_gettable( L, LUA_REGISTRYINDEX ); lbstate = lua_touserdata( L, -1 ); event_struct = &lbstate->event; if ( lua_isnumber( L, 1 ) ) { event_type = tb_peek_event( event_struct, (int) lua_tonumber( L, 1 )); } else { event_type = tb_poll_event( event_struct ); } switch ( event_type ) { LUABOX_CALL( TB_EVENT_KEY ) if ( event_struct->ch ) { char buffer[8] = {0}; tb_utf8_unicode_to_char( buffer, event_struct->ch ); lua_pushstring( L, buffer ); lua_pushnumber( L, event_struct->ch ); } else { lua_pushstring( L, "" ); lua_pushnumber( L, event_struct->key ); } lua_pushnumber( L, event_struct->mod ); LUABOX_RETURN( TB_EVENT_KEY, 3, 0 ) LUABOX_CALL( TB_EVENT_RESIZE ) lua_pushnumber( L, event_struct->w ); lua_pushnumber( L, event_struct->h ); LUABOX_RETURN( TB_EVENT_RESIZE, 2, 0 ) LUABOX_CALL( TB_EVENT_MOUSE ) lua_pushnumber( L, event_struct->x ); lua_pushnumber( L, event_struct->y ); lua_pushnumber( L, event_struct->key ); LUABOX_RETURN( TB_EVENT_MOUSE, 3, 0 ) } return 0; }
CAMLprim value tbstub_poll_event() { CAMLparam0(); CAMLlocal3(caml_e, caml_ch, caml_size); struct tb_event e; tb_poll_event(&e); // type event = // | Key of key -> block with tag 0 // | Ascii of char -> block with tag 1 // | Utf8 of int32 -> block with tag 2 // | Resize of int * int -> block with tag 3 if( e.type == TB_EVENT_KEY ) { // Key // // We deviate from tb_event definition of key here. // Some keys are really low enough to be considered ascii values. // // tb_poll_event reports ch as 0 whenever a 'key' is present. if( e.ch == 0 && e.key > 0xFF ) { caml_e = caml_alloc(1, 0); // We use a bit of a trick here to convert e.key to // type key = // | F1 -> Val_int(0) // ... // | Arrow_right -> Val_int(21) // // All (non-ascii) TB_KEY_* values are defined as (0xFFFF-0)(F1)...(0xFFFF-21)(ARROW_RIGHT). // Notice the pattern -> ^ ^ // // By "restoring" that offset number we get the int value that we need to represent the variant. Store_field(caml_e, 0, Val_int(0xFFFF - e.key)); } // Ascii // // tb_poll_event reports key as 0 whenever a ch is present. else if( e.ch <= 0xFF ) { caml_e = caml_alloc(1, 1); // Another bit of tricky code. // At this point, we know that either e.key < 255 && e.ch = 0 // or e.key = 0 && e.ch < 255 // So we just bitwise or the two values to get our ascii value. Store_field(caml_e, 0, Val_int(e.ch | e.key)); } // Utf8 else { // All else failed, so we need to represent the ch value as an int32 block, // since OCaml has no unicode support. caml_e = caml_alloc(1, 2); caml_ch = caml_copy_int32(e.ch); Store_field(caml_e, 0, caml_ch); } } // Resize else { caml_size = caml_alloc_tuple(2); Store_field(caml_size, 0, Val_int(e.w)); Store_field(caml_size, 1, Val_int(e.h)); caml_e = caml_alloc(1, 3); Store_field(caml_e, 0, caml_size); } CAMLreturn(caml_e); }