Changed mouse and keyboard to push directly to event.c's queue
This commit is contained in:
@@ -66,6 +66,7 @@ function love.run()
|
|||||||
|
|
||||||
while true do
|
while true do
|
||||||
-- Handle events
|
-- Handle events
|
||||||
|
love.event.pump()
|
||||||
while 1 do
|
while 1 do
|
||||||
local name, a,b,c,d = love.event.poll()
|
local name, a,b,c,d = love.event.poll()
|
||||||
if not name then
|
if not name then
|
||||||
|
|||||||
65
src/event.c
65
src/event.c
@@ -11,6 +11,14 @@
|
|||||||
#include "mouse.h"
|
#include "mouse.h"
|
||||||
#include "event.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) {
|
const char* event_typestr(int type) {
|
||||||
switch (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) {
|
int event_poll(event_t *e) {
|
||||||
|
if (events.readi != events.writei) {
|
||||||
/* Poll keyboard */
|
*e = events.buffer[events.readi++ & BUFFER_MASK];
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,8 @@ typedef union {
|
|||||||
|
|
||||||
|
|
||||||
const char* event_typestr(int type);
|
const char* event_typestr(int type);
|
||||||
|
void event_push(event_t *e);
|
||||||
|
void event_pump(void);
|
||||||
int event_poll(event_t *e);
|
int event_poll(event_t *e);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
#include <dpmi.h>
|
#include <dpmi.h>
|
||||||
#include "luaobj.h"
|
#include "luaobj.h"
|
||||||
#include "keyboard.h"
|
#include "keyboard.h"
|
||||||
|
#include "event.h"
|
||||||
|
|
||||||
#define KEY_MAX 128
|
#define KEY_MAX 128
|
||||||
#define BUFFER_SIZE 32
|
#define BUFFER_SIZE 32
|
||||||
@@ -153,6 +154,7 @@ static const char *scancodeMap[] = {
|
|||||||
volatile int keyboard_allowKeyRepeat = 0;
|
volatile int keyboard_allowKeyRepeat = 0;
|
||||||
volatile char keyboard_keyStates[KEY_MAX];
|
volatile char keyboard_keyStates[KEY_MAX];
|
||||||
|
|
||||||
|
enum { KEYPRESSED, KEYRELEASED };
|
||||||
typedef struct { unsigned char type, code, isrepeat; } KeyEvent;
|
typedef struct { unsigned char type, code, isrepeat; } KeyEvent;
|
||||||
|
|
||||||
volatile struct {
|
volatile struct {
|
||||||
@@ -175,7 +177,7 @@ void keyboard_handler() {
|
|||||||
code &= ~(1 << 7);
|
code &= ~(1 << 7);
|
||||||
keyboard_keyStates[code] = 0;
|
keyboard_keyStates[code] = 0;
|
||||||
e = &keyboard_events.data[keyboard_events.writei & BUFFER_MASK];
|
e = &keyboard_events.data[keyboard_events.writei & BUFFER_MASK];
|
||||||
e->type = KEYBOARD_RELEASED;
|
e->type = KEYRELEASED;
|
||||||
e->code = code;
|
e->code = code;
|
||||||
e->isrepeat = 0;
|
e->isrepeat = 0;
|
||||||
keyboard_events.writei++;
|
keyboard_events.writei++;
|
||||||
@@ -186,7 +188,7 @@ void keyboard_handler() {
|
|||||||
if (!isrepeat || keyboard_allowKeyRepeat) {
|
if (!isrepeat || keyboard_allowKeyRepeat) {
|
||||||
keyboard_keyStates[code] = 1;
|
keyboard_keyStates[code] = 1;
|
||||||
e = &keyboard_events.data[keyboard_events.writei & BUFFER_MASK];
|
e = &keyboard_events.data[keyboard_events.writei & BUFFER_MASK];
|
||||||
e->type = KEYBOARD_PRESSED;
|
e->type = KEYPRESSED;
|
||||||
e->code = code;
|
e->code = code;
|
||||||
e->isrepeat = isrepeat;
|
e->isrepeat = isrepeat;
|
||||||
keyboard_events.writei++;
|
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 */
|
/* Handle key press / release */
|
||||||
if (keyboard_events.readi != keyboard_events.writei) {
|
while (keyboard_events.readi != keyboard_events.writei) {
|
||||||
KeyEvent ke = keyboard_events.data[keyboard_events.readi & BUFFER_MASK];
|
KeyEvent ke = keyboard_events.data[keyboard_events.readi++ & BUFFER_MASK];
|
||||||
e->type = ke.type;
|
event_t e;
|
||||||
e->code = ke.code;
|
if (ke.type == KEYPRESSED) {
|
||||||
e->key = scancodeMap[ke.code];
|
e.type = EVENT_KEYBOARD_PRESSED;
|
||||||
e->isrepeat = ke.isrepeat;
|
} else {
|
||||||
keyboard_events.readi++;
|
e.type = EVENT_KEYBOARD_RELEASED;
|
||||||
return 1;
|
}
|
||||||
|
e.keyboard.key = scancodeMap[ke.code];
|
||||||
|
e.keyboard.isrepeat = ke.isrepeat;
|
||||||
|
event_push(&e);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Handle text input */
|
/* Handle text input */
|
||||||
@@ -259,11 +263,9 @@ int keyboard_poll(keyboard_Event *e) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
e->type = KEYBOARD_TEXTINPUT;
|
event_t e;
|
||||||
memcpy(e->text, buf, i);
|
e.type = EVENT_KEYBOARD_TEXTINPUT;
|
||||||
e->text[i] = '\0';
|
memcpy(e.keyboard.text, buf, i);
|
||||||
return 1;
|
e.keyboard.text[i] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,24 +8,10 @@
|
|||||||
#ifndef KEYBOARD_H
|
#ifndef KEYBOARD_H
|
||||||
#define 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);
|
int keyboard_init(void);
|
||||||
void keyboard_deinit(void);
|
void keyboard_deinit(void);
|
||||||
void keyboard_setKeyRepeat(int allow);
|
void keyboard_setKeyRepeat(int allow);
|
||||||
int keyboard_isDown(const char *key);
|
int keyboard_isDown(const char *key);
|
||||||
int keyboard_poll(keyboard_Event *e);
|
void keyboard_update(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -9,6 +9,12 @@
|
|||||||
#include "event.h"
|
#include "event.h"
|
||||||
|
|
||||||
|
|
||||||
|
int l_event_pump(lua_State *L) {
|
||||||
|
event_pump();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int l_event_poll(lua_State *L) {
|
int l_event_poll(lua_State *L) {
|
||||||
event_t e;
|
event_t e;
|
||||||
if (event_poll(&e)) {
|
if (event_poll(&e)) {
|
||||||
@@ -48,6 +54,7 @@ int l_event_poll(lua_State *L) {
|
|||||||
|
|
||||||
int luaopen_event(lua_State *L) {
|
int luaopen_event(lua_State *L) {
|
||||||
luaL_Reg reg[] = {
|
luaL_Reg reg[] = {
|
||||||
|
{ "pump", l_event_pump },
|
||||||
{ "poll", l_event_poll },
|
{ "poll", l_event_poll },
|
||||||
{ 0, 0 },
|
{ 0, 0 },
|
||||||
};
|
};
|
||||||
|
|||||||
53
src/mouse.c
53
src/mouse.c
@@ -10,20 +10,13 @@
|
|||||||
#include <dos.h>
|
#include <dos.h>
|
||||||
#include "luaobj.h"
|
#include "luaobj.h"
|
||||||
#include "mouse.h"
|
#include "mouse.h"
|
||||||
|
#include "event.h"
|
||||||
#define BUFFER_SIZE 32
|
|
||||||
#define BUFFER_MASK (BUFFER_SIZE - 1)
|
|
||||||
|
|
||||||
int mouse_inited;
|
int mouse_inited;
|
||||||
int mouse_x, mouse_y;
|
int mouse_x, mouse_y;
|
||||||
int mouse_lastX, mouse_lastY;
|
int mouse_lastX, mouse_lastY;
|
||||||
int mouse_buttonStates[MOUSE_BUTTON_MAX];
|
int mouse_buttonStates[MOUSE_BUTTON_MAX];
|
||||||
|
|
||||||
struct {
|
|
||||||
mouse_Event data[BUFFER_SIZE];
|
|
||||||
int writei, readi;
|
|
||||||
} mouse_events;
|
|
||||||
|
|
||||||
|
|
||||||
void mouse_init(void) {
|
void mouse_init(void) {
|
||||||
union REGS regs = {};
|
union REGS regs = {};
|
||||||
@@ -46,13 +39,13 @@ void mouse_update(void) {
|
|||||||
|
|
||||||
/* Do moved event if mouse moved */
|
/* Do moved event if mouse moved */
|
||||||
if (mouse_x != mouse_lastX || mouse_y != mouse_lastY) {
|
if (mouse_x != mouse_lastX || mouse_y != mouse_lastY) {
|
||||||
mouse_Event e;
|
event_t e;
|
||||||
e.type = MOUSE_MOVED;
|
e.type = EVENT_MOUSE_MOVED;
|
||||||
e.x = mouse_x;
|
e.mouse.x = mouse_x;
|
||||||
e.y = mouse_y;
|
e.mouse.y = mouse_y;
|
||||||
e.dx = mouse_x - mouse_lastX;
|
e.mouse.dx = mouse_x - mouse_lastX;
|
||||||
e.dy = mouse_y - mouse_lastY;
|
e.mouse.dy = mouse_y - mouse_lastY;
|
||||||
mouse_events.data[mouse_events.writei++ & BUFFER_MASK] = e;
|
event_push(&e);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Update last position */
|
/* Update last position */
|
||||||
@@ -69,12 +62,12 @@ void mouse_update(void) {
|
|||||||
int86(0x33, ®s, ®s);
|
int86(0x33, ®s, ®s);
|
||||||
if (regs.x.bx) {
|
if (regs.x.bx) {
|
||||||
/* Push event */
|
/* Push event */
|
||||||
mouse_Event e;
|
event_t e;
|
||||||
e.type = t == 0 ? MOUSE_PRESSED : MOUSE_RELEASED;
|
e.type = t == 0 ? EVENT_MOUSE_PRESSED : EVENT_MOUSE_RELEASED;
|
||||||
e.button = i;
|
e.mouse.button = i;
|
||||||
e.x = mouse_x;
|
e.mouse.x = mouse_x;
|
||||||
e.y = mouse_y;
|
e.mouse.y = mouse_y;
|
||||||
mouse_events.data[mouse_events.writei++ & BUFFER_MASK] = e;
|
event_push(&e);
|
||||||
/* Set state */
|
/* Set state */
|
||||||
mouse_buttonStates[i] = t == 0 ? 1 : 0;
|
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) {
|
int mouse_isDown(int button) {
|
||||||
return mouse_buttonStates[button];
|
return mouse_buttonStates[button];
|
||||||
|
|||||||
15
src/mouse.h
15
src/mouse.h
@@ -8,22 +8,9 @@ enum {
|
|||||||
MOUSE_BUTTON_MAX
|
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);
|
void mouse_init(void);
|
||||||
int mouse_poll(mouse_Event *e);
|
void mouse_update(void);
|
||||||
int mouse_isDown(int button);
|
int mouse_isDown(int button);
|
||||||
int mouse_getX(void);
|
int mouse_getX(void);
|
||||||
int mouse_getY(void);
|
int mouse_getY(void);
|
||||||
|
|||||||
Reference in New Issue
Block a user