[input] Split binding event handler

There's now an internal event handler for taking care of device addition
and removal, and a public event handler for dealing with device input
events in various contexts In particular, so the clients can check for
the escape key.
This commit is contained in:
Bill Currie 2021-11-16 09:37:39 +09:00
parent 1868fe7404
commit b8baa04b2e
4 changed files with 36 additions and 2 deletions

View file

@ -234,6 +234,8 @@ int IN_RegisterAxis (in_axis_t *axis);
in_button_t *IN_FindButton (const char *name); in_button_t *IN_FindButton (const char *name);
in_axis_t *IN_FindAxis (const char *name); in_axis_t *IN_FindAxis (const char *name);
struct IE_event_s;
int IN_Binding_HandleEvent (const struct IE_event_s *ie_event);
void IN_Binding_Activate (void); void IN_Binding_Activate (void);
void IN_Binding_Init (void); void IN_Binding_Init (void);
struct plitem_s; struct plitem_s;

View file

@ -99,7 +99,7 @@ typedef enum {
| (1 << ie_remove_device) \ | (1 << ie_remove_device) \
) )
typedef struct { typedef struct IE_event_s {
IE_event_type type; IE_event_type type;
uint64_t when; uint64_t when;
union { union {

View file

@ -202,6 +202,19 @@ in_binding_event_handler (const IE_event_t *ie_event, void *unused)
static void (*handlers[ie_event_count]) (const IE_event_t *ie_event) = { static void (*handlers[ie_event_count]) (const IE_event_t *ie_event) = {
[ie_add_device] = in_binding_add_device, [ie_add_device] = in_binding_add_device,
[ie_remove_device] = in_binding_remove_device, [ie_remove_device] = in_binding_remove_device,
};
if (ie_event->type < 0 || ie_event->type >= ie_event_count
|| !handlers[ie_event->type]) {
return 0;
}
handlers[ie_event->type] (ie_event);
return 1;
}
int
IN_Binding_HandleEvent (const IE_event_t *ie_event)
{
static void (*handlers[ie_event_count]) (const IE_event_t *ie_event) = {
[ie_axis] = in_binding_axis, [ie_axis] = in_binding_axis,
[ie_button] = in_binding_button, [ie_button] = in_binding_button,
}; };

View file

@ -37,6 +37,7 @@
#include "QF/checksum.h" #include "QF/checksum.h"
#include "QF/cmd.h" #include "QF/cmd.h"
#include "QF/console.h"
#include "QF/cvar.h" #include "QF/cvar.h"
#include "QF/input.h" #include "QF/input.h"
#include "QF/keys.h" #include "QF/keys.h"
@ -45,6 +46,8 @@
#include "QF/teamplay.h" #include "QF/teamplay.h"
#include "QF/va.h" #include "QF/va.h"
#include "QF/input/event.h"
#include "compat.h" #include "compat.h"
#include "client/view.h" #include "client/view.h"
@ -61,6 +64,7 @@
int cl_game_context; int cl_game_context;
int cl_demo_context; int cl_demo_context;
static int cl_event_id;
cvar_t *cl_nodelta; cvar_t *cl_nodelta;
cvar_t *cl_maxnetfps; cvar_t *cl_maxnetfps;
@ -563,9 +567,24 @@ CL_SendCmd (void)
Netchan_Transmit (&cls.netchan, buf.cursize, buf.data); Netchan_Transmit (&cls.netchan, buf.cursize, buf.data);
} }
static int
cl_event_handler (const IE_event_t *ie_event, void *unused)
{
if (ie_event->type == ie_key) {
if (ie_event->key.code == QFK_ESCAPE) {
// FIXME this should bring up the menu
Con_SetState (con_active);
return 1;
}
}
return IN_Binding_HandleEvent (ie_event);
}
void void
CL_Input_Init (void) CL_Input_Init (void)
{ {
cl_event_id = IE_Add_Handler (cl_event_handler, 0);
for (int i = 0; cl_in_axes[i]; i++) { for (int i = 0; cl_in_axes[i]; i++) {
IN_RegisterAxis (cl_in_axes[i]); IN_RegisterAxis (cl_in_axes[i]);
} }
@ -584,7 +603,7 @@ void
CL_Input_Activate (void) CL_Input_Activate (void)
{ {
IMT_SetContext (cl_game_context); IMT_SetContext (cl_game_context);
IN_Binding_Activate (); IE_Set_Focus (cl_event_id);
} }
void void