From de01a59eeb8a1ee90733cd2e3019e0985df87d4d Mon Sep 17 00:00:00 2001 From: rxi Date: Sun, 16 Oct 2016 11:41:56 +0100 Subject: [PATCH] Changed mouse and keyboard to push directly to event.c's queue --- src/embed/boot.lua | 1 + src/event.c | 65 ++++++++++++++----------------------------- src/event.h | 2 ++ src/keyboard.c | 38 +++++++++++++------------ src/keyboard.h | 16 +---------- src/modules/l_event.c | 7 +++++ src/mouse.c | 53 ++++++++++------------------------- src/mouse.h | 15 +--------- 8 files changed, 67 insertions(+), 130 deletions(-) diff --git a/src/embed/boot.lua b/src/embed/boot.lua index 969a5d8..611590b 100644 --- a/src/embed/boot.lua +++ b/src/embed/boot.lua @@ -66,6 +66,7 @@ function love.run() while true do -- Handle events + love.event.pump() while 1 do local name, a,b,c,d = love.event.poll() if not name then diff --git a/src/event.c b/src/event.c index 0d2b2bb..e42f7f7 100644 --- a/src/event.c +++ b/src/event.c @@ -11,6 +11,14 @@ #include "mouse.h" #include "event.h" +#define BUFFER_SIZE 256 +#define BUFFER_MASK (BUFFER_SIZE - 1) + +static struct { + event_t buffer[BUFFER_SIZE]; + int writei, readi; +} events; + const char* event_typestr(int type) { switch (type) { @@ -25,52 +33,21 @@ const char* event_typestr(int type) { } +void event_push(event_t *e) { + events.buffer[events.writei++ & BUFFER_MASK] = *e; +} + + +void event_pump(void) { + keyboard_update(); + mouse_update(); +} + + int event_poll(event_t *e) { - - /* Poll keyboard */ - keyboard_Event ke; - if (keyboard_poll(&ke)) { - if (ke.type == KEYBOARD_PRESSED || ke.type == KEYBOARD_RELEASED) { - if (ke.type == KEYBOARD_PRESSED) { - e->type = EVENT_KEYBOARD_PRESSED; - } else { - e->type = EVENT_KEYBOARD_RELEASED; - } - e->keyboard.key = ke.key; - e->keyboard.isrepeat = ke.isrepeat; - - } else if (ke.type == KEYBOARD_TEXTINPUT) { - e->type = EVENT_KEYBOARD_TEXTINPUT; - strcpy(e->keyboard.text, ke.text); - } - + if (events.readi != events.writei) { + *e = events.buffer[events.readi++ & BUFFER_MASK]; return 1; } - - /* Poll mouse */ - mouse_Event me; - if (mouse_poll(&me)) { - if (me.type == MOUSE_PRESSED || me.type == MOUSE_RELEASED) { - if (me.type == MOUSE_PRESSED) { - e->type = EVENT_MOUSE_PRESSED; - } else { - e->type = EVENT_MOUSE_RELEASED; - } - e->mouse.button = me.button; - e->mouse.x = me.x; - e->mouse.y = me.y; - e->mouse.dx = me.dx; - e->mouse.dy = me.dy; - - } else if (me.type == MOUSE_MOVED) { - e->type = EVENT_MOUSE_MOVED; - e->mouse.x = me.x; - e->mouse.y = me.y; - } - - return 1; - } - - /* No events */ return 0; } diff --git a/src/event.h b/src/event.h index a50ba6f..234f836 100644 --- a/src/event.h +++ b/src/event.h @@ -39,6 +39,8 @@ typedef union { const char* event_typestr(int type); +void event_push(event_t *e); +void event_pump(void); int event_poll(event_t *e); #endif diff --git a/src/keyboard.c b/src/keyboard.c index f159f37..bf8523e 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -12,6 +12,7 @@ #include #include "luaobj.h" #include "keyboard.h" +#include "event.h" #define KEY_MAX 128 #define BUFFER_SIZE 32 @@ -153,6 +154,7 @@ static const char *scancodeMap[] = { volatile int keyboard_allowKeyRepeat = 0; volatile char keyboard_keyStates[KEY_MAX]; +enum { KEYPRESSED, KEYRELEASED }; typedef struct { unsigned char type, code, isrepeat; } KeyEvent; volatile struct { @@ -175,7 +177,7 @@ void keyboard_handler() { code &= ~(1 << 7); keyboard_keyStates[code] = 0; e = &keyboard_events.data[keyboard_events.writei & BUFFER_MASK]; - e->type = KEYBOARD_RELEASED; + e->type = KEYRELEASED; e->code = code; e->isrepeat = 0; keyboard_events.writei++; @@ -186,7 +188,7 @@ void keyboard_handler() { if (!isrepeat || keyboard_allowKeyRepeat) { keyboard_keyStates[code] = 1; e = &keyboard_events.data[keyboard_events.writei & BUFFER_MASK]; - e->type = KEYBOARD_PRESSED; + e->type = KEYPRESSED; e->code = code; e->isrepeat = isrepeat; keyboard_events.writei++; @@ -234,17 +236,19 @@ int keyboard_isDown(const char *key) { } -int keyboard_poll(keyboard_Event *e) { - +void keyboard_update(void) { /* Handle key press / release */ - if (keyboard_events.readi != keyboard_events.writei) { - KeyEvent ke = keyboard_events.data[keyboard_events.readi & BUFFER_MASK]; - e->type = ke.type; - e->code = ke.code; - e->key = scancodeMap[ke.code]; - e->isrepeat = ke.isrepeat; - keyboard_events.readi++; - return 1; + while (keyboard_events.readi != keyboard_events.writei) { + KeyEvent ke = keyboard_events.data[keyboard_events.readi++ & BUFFER_MASK]; + event_t e; + if (ke.type == KEYPRESSED) { + e.type = EVENT_KEYBOARD_PRESSED; + } else { + e.type = EVENT_KEYBOARD_RELEASED; + } + e.keyboard.key = scancodeMap[ke.code]; + e.keyboard.isrepeat = ke.isrepeat; + event_push(&e); } /* Handle text input */ @@ -259,11 +263,9 @@ int keyboard_poll(keyboard_Event *e) { } } if (i > 0) { - e->type = KEYBOARD_TEXTINPUT; - memcpy(e->text, buf, i); - e->text[i] = '\0'; - return 1; + event_t e; + e.type = EVENT_KEYBOARD_TEXTINPUT; + memcpy(e.keyboard.text, buf, i); + e.keyboard.text[i] = '\0'; } - - return 0; } diff --git a/src/keyboard.h b/src/keyboard.h index 364fa79..45552ad 100644 --- a/src/keyboard.h +++ b/src/keyboard.h @@ -8,24 +8,10 @@ #ifndef KEYBOARD_H #define KEYBOARD_H -typedef struct { - int type; - unsigned code; - const char *key; - char text[64]; - int isrepeat; -} keyboard_Event; - -enum { - KEYBOARD_PRESSED, - KEYBOARD_RELEASED, - KEYBOARD_TEXTINPUT -}; - int keyboard_init(void); void keyboard_deinit(void); void keyboard_setKeyRepeat(int allow); int keyboard_isDown(const char *key); -int keyboard_poll(keyboard_Event *e); +void keyboard_update(void); #endif diff --git a/src/modules/l_event.c b/src/modules/l_event.c index 7f4c11a..da66551 100644 --- a/src/modules/l_event.c +++ b/src/modules/l_event.c @@ -9,6 +9,12 @@ #include "event.h" +int l_event_pump(lua_State *L) { + event_pump(); + return 0; +} + + int l_event_poll(lua_State *L) { event_t e; if (event_poll(&e)) { @@ -48,6 +54,7 @@ int l_event_poll(lua_State *L) { int luaopen_event(lua_State *L) { luaL_Reg reg[] = { + { "pump", l_event_pump }, { "poll", l_event_poll }, { 0, 0 }, }; diff --git a/src/mouse.c b/src/mouse.c index c317e62..631dbb7 100644 --- a/src/mouse.c +++ b/src/mouse.c @@ -10,20 +10,13 @@ #include #include "luaobj.h" #include "mouse.h" - -#define BUFFER_SIZE 32 -#define BUFFER_MASK (BUFFER_SIZE - 1) +#include "event.h" int mouse_inited; int mouse_x, mouse_y; int mouse_lastX, mouse_lastY; int mouse_buttonStates[MOUSE_BUTTON_MAX]; -struct { - mouse_Event data[BUFFER_SIZE]; - int writei, readi; -} mouse_events; - void mouse_init(void) { union REGS regs = {}; @@ -46,13 +39,13 @@ void mouse_update(void) { /* Do moved event if mouse moved */ if (mouse_x != mouse_lastX || mouse_y != mouse_lastY) { - mouse_Event e; - e.type = MOUSE_MOVED; - e.x = mouse_x; - e.y = mouse_y; - e.dx = mouse_x - mouse_lastX; - e.dy = mouse_y - mouse_lastY; - mouse_events.data[mouse_events.writei++ & BUFFER_MASK] = e; + event_t e; + e.type = EVENT_MOUSE_MOVED; + e.mouse.x = mouse_x; + e.mouse.y = mouse_y; + e.mouse.dx = mouse_x - mouse_lastX; + e.mouse.dy = mouse_y - mouse_lastY; + event_push(&e); } /* Update last position */ @@ -69,12 +62,12 @@ void mouse_update(void) { int86(0x33, ®s, ®s); if (regs.x.bx) { /* Push event */ - mouse_Event e; - e.type = t == 0 ? MOUSE_PRESSED : MOUSE_RELEASED; - e.button = i; - e.x = mouse_x; - e.y = mouse_y; - mouse_events.data[mouse_events.writei++ & BUFFER_MASK] = e; + event_t e; + e.type = t == 0 ? EVENT_MOUSE_PRESSED : EVENT_MOUSE_RELEASED; + e.mouse.button = i; + e.mouse.x = mouse_x; + e.mouse.y = mouse_y; + event_push(&e); /* Set state */ mouse_buttonStates[i] = t == 0 ? 1 : 0; } @@ -83,24 +76,6 @@ void mouse_update(void) { } -int mouse_poll(mouse_Event *e) { -retry: - /* Event in event buffer? Store and return */ - if (mouse_events.readi != mouse_events.writei) { - *e = mouse_events.data[mouse_events.readi++ & BUFFER_MASK]; - return 1; - } - - /* No events in buffer -- update; return `0` if no new events were pushed by - * the update */ - mouse_update(); - if (mouse_events.readi != mouse_events.writei) { - goto retry; - } - - return 0; -} - int mouse_isDown(int button) { return mouse_buttonStates[button]; diff --git a/src/mouse.h b/src/mouse.h index 6d8e9f9..c78a382 100644 --- a/src/mouse.h +++ b/src/mouse.h @@ -8,22 +8,9 @@ enum { MOUSE_BUTTON_MAX }; -enum { - MOUSE_PRESSED, - MOUSE_RELEASED, - MOUSE_MOVED -}; - -typedef struct { - int type; - int x, y; - int dx, dy; - int button; -} mouse_Event; - void mouse_init(void); -int mouse_poll(mouse_Event *e); +void mouse_update(void); int mouse_isDown(int button); int mouse_getX(void); int mouse_getY(void);