From f17b39868e7f0d1fbca7e60d21ea7b618b7e1f92 Mon Sep 17 00:00:00 2001 From: rxi Date: Sat, 24 Sep 2016 19:45:35 +0100 Subject: [PATCH] Implemented `love.textinput` callback --- src/keyboard.c | 38 ++++++++++++++++++++++++++------------ src/main.c | 4 +++- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/keyboard.c b/src/keyboard.c index 5ba8fc9..7a92306 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -5,8 +5,10 @@ * under the terms of the MIT license. See LICENSE for details. */ +#include #include #include +#include #include #include "luaobj.h" #include "keyboard.h" @@ -68,22 +70,13 @@ int keyboard_init(void) { (unsigned long)keyboard_handler); _go32_dpmi_get_protected_mode_interrupt_vector(9, &old_keyb_handler_seginfo); new_keyb_handler_seginfo.pm_offset = (int)keyboard_handler; - if (_go32_dpmi_allocate_iret_wrapper(&new_keyb_handler_seginfo) != 0) { - return 1; - } - if (_go32_dpmi_set_protected_mode_interrupt_vector( - 9, &new_keyb_handler_seginfo) != 0 - ) { - _go32_dpmi_free_iret_wrapper(&new_keyb_handler_seginfo); - return 1; - } + _go32_dpmi_chain_protected_mode_interrupt_vector(9, &new_keyb_handler_seginfo); return 0; } void keyboard_deinit(void) { _go32_dpmi_set_protected_mode_interrupt_vector(9, &old_keyb_handler_seginfo); - _go32_dpmi_free_iret_wrapper(&new_keyb_handler_seginfo); } @@ -235,6 +228,7 @@ int l_keyboard_poll(lua_State *L) { lua_newtable(L); int idx = 1; + /* Handle key presses / releases */ while (keyboard_events.readi != keyboard_events.writei) { lua_newtable(L); @@ -249,10 +243,30 @@ int l_keyboard_poll(lua_State *L) { lua_pushstring(L, scancodeMap[code]); lua_setfield(L, -2, "key"); - lua_rawseti(L, -2, idx); + lua_rawseti(L, -2, idx++); keyboard_events.readi++; - idx++; } + + /* Handle text input */ + char buf[64]; + int i = 0; + while ( kbhit() ) { + int chr = getch(); + if (chr == 0) { /* Discard "special" keys */ + getch(); + } else if (chr >= 32) { + buf[i++] = chr; + } + } + if (i > 0) { + lua_newtable(L); + lua_pushstring(L, "text"); + lua_setfield(L, -2, "type"); + lua_pushlstring(L, buf, i); + lua_setfield(L, -2, "text"); + lua_rawseti(L, -2, idx++); + } + return 1; } diff --git a/src/main.c b/src/main.c index 6b917ef..e45e21d 100644 --- a/src/main.c +++ b/src/main.c @@ -78,8 +78,10 @@ int main(void) { "for _, e in ipairs(love.keyboard.poll()) do\n" "if e.type == 'down' then\n" "if love.keypressed then love.keypressed(e.key, e.code) end\n" - "else\n" + "elseif e.type == 'up' then\n" "if love.keyreleased then love.keyreleased(e.key, e.code) end\n" + "elseif e.type == 'text' then\n" + "if love.textinput then love.textinput(e.text) end\n" "end\n" "end\n" /* Update */