Joystick: Replace magic numbers for maximum axes/buttons/POVs with macros

This commit is contained in:
Alexander Babikov
2024-02-18 17:08:26 +05:00
parent 394e078991
commit e51f99c800
4 changed files with 34 additions and 30 deletions

View File

@@ -24,6 +24,10 @@
#define MAX_PLAT_JOYSTICKS 8 #define MAX_PLAT_JOYSTICKS 8
#define MAX_JOYSTICKS 4 #define MAX_JOYSTICKS 4
#define MAX_JOY_AXES 8
#define MAX_JOY_BUTTONS 32
#define MAX_JOY_POVS 4
#define JS_TYPE_NONE 0 #define JS_TYPE_NONE 0
#define JS_TYPE_2AXIS_4BUTTON 1 #define JS_TYPE_2AXIS_4BUTTON 1
#define JS_TYPE_2AXIS_6BUTTON 2 #define JS_TYPE_2AXIS_6BUTTON 2
@@ -46,24 +50,24 @@
typedef struct plat_joystick_t { typedef struct plat_joystick_t {
char name[260]; char name[260];
int a[8]; int a[MAX_JOY_AXES];
int b[32]; int b[MAX_JOY_BUTTONS];
int p[4]; int p[MAX_JOY_POVS];
struct { struct {
char name[260]; char name[260];
int id; int id;
} axis[8]; } axis[MAX_JOY_AXES];
struct { struct {
char name[260]; char name[260];
int id; int id;
} button[32]; } button[MAX_JOY_BUTTONS];
struct { struct {
char name[260]; char name[260];
int id; int id;
} pov[4]; } pov[MAX_JOY_POVS];
int nr_axes; int nr_axes;
int nr_buttons; int nr_buttons;
@@ -71,14 +75,14 @@ typedef struct plat_joystick_t {
} plat_joystick_t; } plat_joystick_t;
typedef struct joystick_t { typedef struct joystick_t {
int axis[8]; int axis[MAX_JOY_AXES];
int button[32]; int button[MAX_JOY_BUTTONS];
int pov[4]; int pov[MAX_JOY_POVS];
int plat_joystick_nr; int plat_joystick_nr;
int axis_mapping[8]; int axis_mapping[MAX_JOY_AXES];
int button_mapping[32]; int button_mapping[MAX_JOY_BUTTONS];
int pov_mapping[4][2]; int pov_mapping[MAX_JOY_POVS][2];
} joystick_t; } joystick_t;
typedef struct joystick_if_t { typedef struct joystick_if_t {
@@ -96,9 +100,9 @@ typedef struct joystick_if_t {
int button_count; int button_count;
int pov_count; int pov_count;
int max_joysticks; int max_joysticks;
const char *axis_names[8]; const char *axis_names[MAX_JOY_AXES];
const char *button_names[32]; const char *button_names[MAX_JOY_BUTTONS];
const char *pov_names[4]; const char *pov_names[MAX_JOY_POVS];
} joystick_if_t; } joystick_if_t;
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -88,14 +88,14 @@ typedef struct {
USHORT bitsize; USHORT bitsize;
LONG max; LONG max;
LONG min; LONG min;
} axis[8]; } axis[MAX_JOY_AXES];
struct raw_pov_t { struct raw_pov_t {
USAGE usage; USAGE usage;
USHORT link; USHORT link;
LONG max; LONG max;
LONG min; LONG min;
} pov[4]; } pov[MAX_JOY_POVS];
} raw_joystick_t; } raw_joystick_t;
plat_joystick_t plat_joystick_state[MAX_PLAT_JOYSTICKS]; plat_joystick_t plat_joystick_state[MAX_PLAT_JOYSTICKS];
@@ -108,7 +108,7 @@ raw_joystick_t raw_joystick_state[MAX_PLAT_JOYSTICKS];
void void
joystick_add_button(raw_joystick_t *rawjoy, plat_joystick_t *joy, USAGE usage) joystick_add_button(raw_joystick_t *rawjoy, plat_joystick_t *joy, USAGE usage)
{ {
if (joy->nr_buttons >= 32) if (joy->nr_buttons >= MAX_JOY_BUTTONS)
return; return;
if (usage < 1 || usage > 128) if (usage < 1 || usage > 128)
return; return;
@@ -121,7 +121,7 @@ joystick_add_button(raw_joystick_t *rawjoy, plat_joystick_t *joy, USAGE usage)
void void
joystick_add_axis(raw_joystick_t *rawjoy, plat_joystick_t *joy, PHIDP_VALUE_CAPS prop) joystick_add_axis(raw_joystick_t *rawjoy, plat_joystick_t *joy, PHIDP_VALUE_CAPS prop)
{ {
if (joy->nr_axes >= 8) if (joy->nr_axes >= MAX_JOY_AXES)
return; return;
switch (prop->Range.UsageMin) { switch (prop->Range.UsageMin) {
@@ -206,7 +206,7 @@ joystick_add_axis(raw_joystick_t *rawjoy, plat_joystick_t *joy, PHIDP_VALUE_CAPS
void void
joystick_add_pov(raw_joystick_t *rawjoy, plat_joystick_t *joy, PHIDP_VALUE_CAPS prop) joystick_add_pov(raw_joystick_t *rawjoy, plat_joystick_t *joy, PHIDP_VALUE_CAPS prop)
{ {
if (joy->nr_povs >= 4) if (joy->nr_povs >= MAX_JOY_POVS)
return; return;
sprintf(joy->pov[joy->nr_povs].name, "POV %d", joy->nr_povs + 1); sprintf(joy->pov[joy->nr_povs].name, "POV %d", joy->nr_povs + 1);

View File

@@ -84,7 +84,7 @@ DIEnumDeviceObjectsCallback(
plat_joystick_t *state = (plat_joystick_t *) pvRef; plat_joystick_t *state = (plat_joystick_t *) pvRef;
if (lpddoi->guidType == GUID_XAxis || lpddoi->guidType == GUID_YAxis || lpddoi->guidType == GUID_ZAxis || lpddoi->guidType == GUID_RxAxis || lpddoi->guidType == GUID_RyAxis || lpddoi->guidType == GUID_RzAxis) { if (lpddoi->guidType == GUID_XAxis || lpddoi->guidType == GUID_YAxis || lpddoi->guidType == GUID_ZAxis || lpddoi->guidType == GUID_RxAxis || lpddoi->guidType == GUID_RyAxis || lpddoi->guidType == GUID_RzAxis) {
if (state->nr_axes < 8) { if (state->nr_axes < MAX_JOY_AXES) {
memcpy(state->axis[state->nr_axes].name, lpddoi->tszName, strlen(lpddoi->tszName) + 1); memcpy(state->axis[state->nr_axes].name, lpddoi->tszName, strlen(lpddoi->tszName) + 1);
joystick_log("Axis %i : %s %x %x\n", state->nr_axes, state->axis[state->nr_axes].name, lpddoi->dwOfs, lpddoi->dwType); joystick_log("Axis %i : %s %x %x\n", state->nr_axes, state->axis[state->nr_axes].name, lpddoi->dwOfs, lpddoi->dwType);
if (lpddoi->guidType == GUID_XAxis) if (lpddoi->guidType == GUID_XAxis)
@@ -106,13 +106,13 @@ DIEnumDeviceObjectsCallback(
state->nr_axes++; state->nr_axes++;
} }
} else if (lpddoi->guidType == GUID_Button) { } else if (lpddoi->guidType == GUID_Button) {
if (state->nr_buttons < 32) { if (state->nr_buttons < MAX_JOY_BUTTONS) {
memcpy(state->button[state->nr_buttons].name, lpddoi->tszName, strlen(lpddoi->tszName) + 1); memcpy(state->button[state->nr_buttons].name, lpddoi->tszName, strlen(lpddoi->tszName) + 1);
joystick_log("Button %i : %s %x %x\n", state->nr_buttons, state->button[state->nr_buttons].name, lpddoi->dwOfs, lpddoi->dwType); joystick_log("Button %i : %s %x %x\n", state->nr_buttons, state->button[state->nr_buttons].name, lpddoi->dwOfs, lpddoi->dwType);
state->nr_buttons++; state->nr_buttons++;
} }
} else if (lpddoi->guidType == GUID_POV) { } else if (lpddoi->guidType == GUID_POV) {
if (state->nr_povs < 4) { if (state->nr_povs < MAX_JOY_POVS) {
memcpy(state->pov[state->nr_povs].name, lpddoi->tszName, strlen(lpddoi->tszName) + 1); memcpy(state->pov[state->nr_povs].name, lpddoi->tszName, strlen(lpddoi->tszName) + 1);
joystick_log("POV %i : %s %x %x\n", state->nr_povs, state->pov[state->nr_povs].name, lpddoi->dwOfs, lpddoi->dwType); joystick_log("POV %i : %s %x %x\n", state->nr_povs, state->pov[state->nr_povs].name, lpddoi->dwOfs, lpddoi->dwType);
state->nr_povs++; state->nr_povs++;
@@ -269,10 +269,10 @@ joystick_process(void)
plat_joystick_state[c].a[6] = joystate.rglSlider[0]; plat_joystick_state[c].a[6] = joystate.rglSlider[0];
plat_joystick_state[c].a[7] = joystate.rglSlider[1]; plat_joystick_state[c].a[7] = joystate.rglSlider[1];
for (b = 0; b < 16; b++) for (b = 0; b < MAX_JOY_BUTTONS; b++)
plat_joystick_state[c].b[b] = joystate.rgbButtons[b] & 0x80; plat_joystick_state[c].b[b] = joystate.rgbButtons[b] & 0x80;
for (b = 0; b < 4; b++) for (b = 0; b < MAX_JOY_POVS; b++)
plat_joystick_state[c].p[b] = joystate.rgdwPOV[b]; plat_joystick_state[c].p[b] = joystate.rgdwPOV[b];
// joystick_log("joystick %i - x=%i y=%i b[0]=%i b[1]=%i %i\n", c, joystick_state[c].x, joystick_state[c].y, joystick_state[c].b[0], joystick_state[c].b[1], joysticks_present); // joystick_log("joystick %i - x=%i y=%i b[0]=%i b[1]=%i %i\n", c, joystick_state[c].x, joystick_state[c].y, joystick_state[c].b[0], joystick_state[c].b[1], joysticks_present);
} }

View File

@@ -88,14 +88,14 @@ typedef struct {
USHORT bitsize; USHORT bitsize;
LONG max; LONG max;
LONG min; LONG min;
} axis[8]; } axis[MAX_JOY_AXES];
struct raw_pov_t { struct raw_pov_t {
USAGE usage; USAGE usage;
USHORT link; USHORT link;
LONG max; LONG max;
LONG min; LONG min;
} pov[4]; } pov[MAX_JOY_POVS];
} raw_joystick_t; } raw_joystick_t;
plat_joystick_t plat_joystick_state[MAX_PLAT_JOYSTICKS]; plat_joystick_t plat_joystick_state[MAX_PLAT_JOYSTICKS];
@@ -108,7 +108,7 @@ raw_joystick_t raw_joystick_state[MAX_PLAT_JOYSTICKS];
void void
joystick_add_button(raw_joystick_t *rawjoy, plat_joystick_t *joy, USAGE usage) joystick_add_button(raw_joystick_t *rawjoy, plat_joystick_t *joy, USAGE usage)
{ {
if (joy->nr_buttons >= 32) if (joy->nr_buttons >= MAX_JOY_BUTTONS)
return; return;
if (usage < 1 || usage > 128) if (usage < 1 || usage > 128)
return; return;
@@ -121,7 +121,7 @@ joystick_add_button(raw_joystick_t *rawjoy, plat_joystick_t *joy, USAGE usage)
void void
joystick_add_axis(raw_joystick_t *rawjoy, plat_joystick_t *joy, PHIDP_VALUE_CAPS prop) joystick_add_axis(raw_joystick_t *rawjoy, plat_joystick_t *joy, PHIDP_VALUE_CAPS prop)
{ {
if (joy->nr_axes >= 8) if (joy->nr_axes >= MAX_JOY_AXES)
return; return;
switch (prop->Range.UsageMin) { switch (prop->Range.UsageMin) {
@@ -206,7 +206,7 @@ joystick_add_axis(raw_joystick_t *rawjoy, plat_joystick_t *joy, PHIDP_VALUE_CAPS
void void
joystick_add_pov(raw_joystick_t *rawjoy, plat_joystick_t *joy, PHIDP_VALUE_CAPS prop) joystick_add_pov(raw_joystick_t *rawjoy, plat_joystick_t *joy, PHIDP_VALUE_CAPS prop)
{ {
if (joy->nr_povs >= 4) if (joy->nr_povs >= MAX_JOY_POVS)
return; return;
sprintf(joy->pov[joy->nr_povs].name, "POV %d", joy->nr_povs + 1); sprintf(joy->pov[joy->nr_povs].name, "POV %d", joy->nr_povs + 1);