[input] Give input drivers a private data pointer

The pointer can be updated at any time using the driver handle (returned
by IN_RegisterData).
This commit is contained in:
Bill Currie 2021-08-30 09:54:36 +09:00
parent 05215e0295
commit b39b6031e8
4 changed files with 53 additions and 29 deletions

View file

@ -36,11 +36,11 @@ typedef struct {
} viewdelta_t;
typedef struct {
void (*init) (void);
void (*shutdown) (void);
void (*process_events) (void);
void (*clear_states) (void);
void (*grab_input) (int grab);
void (*init) (void *data);
void (*shutdown) (void *data);
void (*process_events) (void *data);
void (*clear_states) (void *data);
void (*grab_input) (void *data, int grab);
} in_driver_t;
extern viewdelta_t viewdelta;
@ -49,7 +49,8 @@ extern viewdelta_t viewdelta;
struct cvar_s;
void IN_RegisterDriver (in_driver_t *driver);
int IN_RegisterDriver (in_driver_t *driver, void *data);
void IN_DriverData (int handlle, void *data);
void IN_Init (struct cbuf_s *cbuf);
void IN_Init_Cvars (void);

View file

@ -58,7 +58,12 @@
#include "QF/sys.h"
#include "QF/vid.h"
static struct DARRAY_TYPE (in_driver_t) in_drivers = { .grow = 8, };
typedef struct {
in_driver_t driver;
void *data;
} in_regdriver_t;
static struct DARRAY_TYPE (in_regdriver_t) in_drivers = { .grow = 8, };
VISIBLE viewdelta_t viewdelta;
@ -79,10 +84,18 @@ qboolean in_mouse_avail;
float in_mouse_x, in_mouse_y;
static float in_old_mouse_x, in_old_mouse_y;
void
IN_RegisterDriver (in_driver_t *driver)
int
IN_RegisterDriver (in_driver_t *driver, void *data)
{
DARRAY_APPEND (&in_drivers, *driver);
in_regdriver_t rdriver = { *driver, data };
DARRAY_APPEND (&in_drivers, rdriver);
return in_drivers.size - 1;
}
void
IN_DriverData (int handle, void *data)
{
in_drivers.a[handle].data = data;
}
void
@ -90,8 +103,9 @@ IN_UpdateGrab (cvar_t *var) // called from context_*.c
{
if (var) {
for (size_t i = 0; i < in_drivers.size; i++) {
if (in_drivers.a[i].grab_input) {
in_drivers.a[i].grab_input (var->int_val);
in_regdriver_t *rd = &in_drivers.a[i];
if (rd->driver.grab_input) {
rd->driver.grab_input (rd->data, var->int_val);
}
}
}
@ -101,7 +115,8 @@ void
IN_ProcessEvents (void)
{
for (size_t i = 0; i < in_drivers.size; i++) {
in_drivers.a[i].process_events ();
in_regdriver_t *rd = &in_drivers.a[i];
rd->driver.process_events (rd->data);
}
}
@ -148,8 +163,9 @@ IN_shutdown (void *data)
Sys_MaskPrintf (SYS_vid, "IN_Shutdown\n");
for (size_t i = in_drivers.size; i-- > 0; ) {
if (in_drivers.a[i].shutdown) {
in_drivers.a[i].shutdown ();
in_regdriver_t *rd = &in_drivers.a[i];
if (rd->driver.shutdown) {
rd->driver.shutdown (rd->data);
}
}
@ -163,7 +179,8 @@ IN_Init (cbuf_t *cbuf)
IE_Init ();
for (size_t i = 0; i < in_drivers.size; i++) {
in_drivers.a[i].init ();
in_regdriver_t *rd = &in_drivers.a[i];
rd->driver.init (rd->data);
}
Key_Init (cbuf);
//JOY_Init ();
@ -200,8 +217,9 @@ void
IN_ClearStates (void)
{
for (size_t i = 0; i < in_drivers.size; i++) {
if (in_drivers.a[i].clear_states) {
in_drivers.a[i].clear_states ();
in_regdriver_t *rd = &in_drivers.a[i];
if (rd->driver.clear_states) {
rd->driver.clear_states (rd->data);
}
}
Key_ClearStates ();

View file

@ -49,14 +49,14 @@ in_evdev_keydest_callback (keydest_t key_dest, void *data)
}
static void
in_evdev_process_events (void)
in_evdev_process_events (void *data)
{
if (inputlib_check_input ()) {
}
}
static void
in_evdev_shutdown (void)
in_evdev_shutdown (void *data)
{
inputlib_close ();
}
@ -64,15 +64,20 @@ in_evdev_shutdown (void)
static void
device_add (device_t *dev)
{
Sys_Printf ("in_evdev: add %s\n", dev->path);
Sys_Printf (" %s\n", dev->name);
Sys_Printf (" %s\n", dev->phys);
Sys_Printf (" %s\n", dev->uniq);
}
static void
device_remove (device_t *dev)
{
Sys_Printf ("in_evdev: remove %s\n", dev->path);
}
static void
in_evdev_init (void)
in_evdev_init (void *data)
{
Key_KeydestCallback (in_evdev_keydest_callback, 0);
@ -80,7 +85,7 @@ in_evdev_init (void)
}
static void
in_evdev_clear_states (void)
in_evdev_clear_states (void *data)
{
}
@ -94,7 +99,7 @@ static in_driver_t in_evdev_driver = {
static void __attribute__((constructor))
in_evdev_register_driver (void)
{
IN_RegisterDriver (&in_evdev_driver);
IN_RegisterDriver (&in_evdev_driver, 0);
}
int in_evdev_force_link;

View file

@ -743,7 +743,7 @@ grab_error (int code, const char *device)
}
static void
in_x11_grab_input (int grab)
in_x11_grab_input (void *data, int grab)
{
if (!x_disp || !x_win)
return;
@ -781,13 +781,13 @@ in_x11_grab_input (int grab)
}
static void
in_x11_process_events (void)
in_x11_process_events (void *data)
{
X11_ProcessEvents (); // Get events from X server.
}
static void
in_x11_shutdown (void)
in_x11_shutdown (void *data)
{
Sys_MaskPrintf (SYS_vid, "in_x11_shutdown\n");
in_mouse_avail = 0;
@ -813,7 +813,7 @@ in_x11_init_cvars (void)
}
static void
in_x11_init (void)
in_x11_init (void *data)
{
// open the display
if (!x_disp)
@ -863,7 +863,7 @@ in_x11_init (void)
}
static void
in_x11_clear_states (void)
in_x11_clear_states (void *data)
{
}
@ -878,7 +878,7 @@ static in_driver_t in_x11_driver = {
static void __attribute__((constructor))
in_x11_register_driver (void)
{
IN_RegisterDriver (&in_x11_driver);
IN_RegisterDriver (&in_x11_driver, 0);
}
int x11_force_link;