Joystick code improvements

This commit is contained in:
Jasmine Iwanek
2023-08-14 17:20:48 -04:00
parent 1811115b65
commit 7c0e1f7f83
7 changed files with 63 additions and 56 deletions

View File

@@ -619,35 +619,35 @@ load_input_devices(void)
else { else {
c = ini_section_get_int(cat, "joystick_type", 8); c = ini_section_get_int(cat, "joystick_type", 8);
switch (c) { switch (c) {
case 1: case JS_TYPE_2AXIS_4BUTTON:
joystick_type = joystick_get_from_internal_name("2axis_4button"); joystick_type = joystick_get_from_internal_name("2axis_4button");
break; break;
case 2: case JS_TYPE_2AXIS_6BUTTON:
joystick_type = joystick_get_from_internal_name("2axis_6button"); joystick_type = joystick_get_from_internal_name("2axis_6button");
break; break;
case 3: case JS_TYPE_2AXIS_8BUTTON:
joystick_type = joystick_get_from_internal_name("2axis_8button"); joystick_type = joystick_get_from_internal_name("2axis_8button");
break; break;
case 4: case JS_TYPE_4AXIS_4BUTTON:
joystick_type = joystick_get_from_internal_name("4axis_4button"); joystick_type = joystick_get_from_internal_name("4axis_4button");
break; break;
case 5: case JS_TYPE_CH_FLIGHTSTICK_PRO:
joystick_type = joystick_get_from_internal_name("ch_flightstick_pro"); joystick_type = joystick_get_from_internal_name("ch_flightstick_pro");
break; break;
case 6: case JS_TYPE_SIDEWINDER_PAD:
joystick_type = joystick_get_from_internal_name("sidewinder_pad"); joystick_type = joystick_get_from_internal_name("sidewinder_pad");
break; break;
case 7: case JS_TYPE_THRUSTMASTER_FCS:
joystick_type = joystick_get_from_internal_name("thrustmaster_fcs"); joystick_type = joystick_get_from_internal_name("thrustmaster_fcs");
break; break;
default: default:
joystick_type = 0; joystick_type = JS_TYPE_NONE;
break; break;
} }
} }
} }
} else } else
joystick_type = 0; joystick_type = JS_TYPE_NONE;
for (c = 0; c < joystick_get_max_joysticks(joystick_type); c++) { for (c = 0; c < joystick_get_max_joysticks(joystick_type); c++) {
sprintf(temp, "joystick_%i_nr", c); sprintf(temp, "joystick_%i_nr", c);

View File

@@ -58,7 +58,7 @@ typedef struct _joystick_instance_ {
void *dat; void *dat;
} joystick_instance_t; } joystick_instance_t;
int joystick_type = 0; int joystick_type = JS_TYPE_NONE;
static const joystick_if_t joystick_none = { static const joystick_if_t joystick_none = {
.name = "None", .name = "None",
@@ -128,21 +128,21 @@ int gameport_instance_id = 0;
or writes, and ports at the standard 200h location are prioritized. */ or writes, and ports at the standard 200h location are prioritized. */
static gameport_t *active_gameports = NULL; static gameport_t *active_gameports = NULL;
char * const char *
joystick_get_name(int js) joystick_get_name(int js)
{ {
if (!joysticks[js].joystick) if (!joysticks[js].joystick)
return NULL; return NULL;
return (char *) joysticks[js].joystick->name; return joysticks[js].joystick->name;
} }
char * const char *
joystick_get_internal_name(int js) joystick_get_internal_name(int js)
{ {
if (joysticks[js].joystick == NULL) if (joysticks[js].joystick == NULL)
return ""; return "";
return (char *) joysticks[js].joystick->internal_name; return joysticks[js].joystick->internal_name;
} }
int int
@@ -151,7 +151,7 @@ joystick_get_from_internal_name(char *s)
int c = 0; int c = 0;
while (joysticks[c].joystick != NULL) { while (joysticks[c].joystick != NULL) {
if (!strcmp((char *) joysticks[c].joystick->internal_name, s)) if (!strcmp(joysticks[c].joystick->internal_name, s))
return c; return c;
c++; c++;
} }
@@ -183,22 +183,22 @@ joystick_get_pov_count(int js)
return joysticks[js].joystick->pov_count; return joysticks[js].joystick->pov_count;
} }
char * const char *
joystick_get_axis_name(int js, int id) joystick_get_axis_name(int js, int id)
{ {
return (char *) joysticks[js].joystick->axis_names[id]; return joysticks[js].joystick->axis_names[id];
} }
char * const char *
joystick_get_button_name(int js, int id) joystick_get_button_name(int js, int id)
{ {
return (char *) joysticks[js].joystick->button_names[id]; return joysticks[js].joystick->button_names[id];
} }
char * const char *
joystick_get_pov_name(int js, int id) joystick_get_pov_name(int js, int id)
{ {
return (char *) joysticks[js].joystick->pov_names[id]; return joysticks[js].joystick->pov_names[id];
} }
static void static void
@@ -410,7 +410,7 @@ tmacm_init(UNUSED(const device_t *info))
dev = malloc(sizeof(gameport_t)); dev = malloc(sizeof(gameport_t));
memset(dev, 0x00, sizeof(gameport_t)); memset(dev, 0x00, sizeof(gameport_t));
port = device_get_config_hex16("port1_addr"); port = (uint16_t) device_get_config_hex16("port1_addr");
switch (port) { switch (port) {
case 0x201: case 0x201:
dev = gameport_add(&gameport_201_device); dev = gameport_add(&gameport_201_device);
@@ -428,7 +428,7 @@ tmacm_init(UNUSED(const device_t *info))
break; break;
} }
port = device_get_config_hex16("port2_addr"); port = (uint16_t) device_get_config_hex16("port2_addr");
switch (port) { switch (port) {
case 0x209: case 0x209:
dev = gameport_add(&gameport_209_device); dev = gameport_add(&gameport_209_device);

View File

@@ -24,6 +24,16 @@
#define MAX_PLAT_JOYSTICKS 8 #define MAX_PLAT_JOYSTICKS 8
#define MAX_JOYSTICKS 4 #define MAX_JOYSTICKS 4
#define JS_TYPE_NONE 0
#define JS_TYPE_2AXIS_4BUTTON 1
#define JS_TYPE_2AXIS_6BUTTON 2
#define JS_TYPE_2AXIS_8BUTTON 3
#define JS_TYPE_4AXIS_4BUTTON 4
#define JS_TYPE_CH_FLIGHTSTICK_PRO 5
#define JS_TYPE_SIDEWINDER_PAD 6
#define JS_TYPE_THRUSTMASTER_FCS 7
#define POV_X 0x80000000 #define POV_X 0x80000000
#define POV_Y 0x40000000 #define POV_Y 0x40000000
#define SLIDER 0x20000000 #define SLIDER 0x20000000
@@ -84,11 +94,11 @@ typedef struct joystick_if_t {
const char *internal_name; const char *internal_name;
void *(*init)(void); void *(*init)(void);
void (*close)(void *p); void (*close)(void *priv);
uint8_t (*read)(void *p); uint8_t (*read)(void *priv);
void (*write)(void *p); void (*write)(void *priv);
int (*read_axis)(void *p, int axis); int (*read_axis)(void *priv, int axis);
void (*a0_over)(void *p); void (*a0_over)(void *priv);
int axis_count; int axis_count;
int button_count; int button_count;
@@ -133,16 +143,16 @@ extern void joystick_init(void);
extern void joystick_close(void); extern void joystick_close(void);
extern void joystick_process(void); extern void joystick_process(void);
extern char *joystick_get_name(int js); extern const char *joystick_get_name(int js);
extern char *joystick_get_internal_name(int js); extern const char *joystick_get_internal_name(int js);
extern int joystick_get_from_internal_name(char *s); extern int joystick_get_from_internal_name(char *s);
extern int joystick_get_max_joysticks(int js); extern int joystick_get_max_joysticks(int js);
extern int joystick_get_axis_count(int js); extern int joystick_get_axis_count(int js);
extern int joystick_get_button_count(int js); extern int joystick_get_button_count(int js);
extern int joystick_get_pov_count(int js); extern int joystick_get_pov_count(int js);
extern char *joystick_get_axis_name(int js, int id); extern const char *joystick_get_axis_name(int js, int id);
extern char *joystick_get_button_name(int js, int id); extern const char *joystick_get_button_name(int js, int id);
extern char *joystick_get_pov_name(int js, int id); extern const char *joystick_get_pov_name(int js, int id);
extern void gameport_update_joystick_type(void); extern void gameport_update_joystick_type(void);
extern void gameport_remap(void *priv, uint16_t address); extern void gameport_remap(void *priv, uint16_t address);

View File

@@ -87,9 +87,9 @@ SettingsInput::onCurrentMachineChanged(int machineId)
mouseModel->removeRows(0, removeRows); mouseModel->removeRows(0, removeRows);
ui->comboBoxMouse->setCurrentIndex(selectedRow); ui->comboBoxMouse->setCurrentIndex(selectedRow);
int i = 0; int i = 0;
char *joyName = joystick_get_name(i); const char *joyName = joystick_get_name(i);
auto *joystickModel = ui->comboBoxJoystick->model(); auto *joystickModel = ui->comboBoxJoystick->model();
removeRows = joystickModel->rowCount(); removeRows = joystickModel->rowCount();
selectedRow = 0; selectedRow = 0;
while (joyName) { while (joyName) {
@@ -116,7 +116,7 @@ void
SettingsInput::on_comboBoxJoystick_currentIndexChanged(int index) SettingsInput::on_comboBoxJoystick_currentIndexChanged(int index)
{ {
int joystickId = ui->comboBoxJoystick->currentData().toInt(); int joystickId = ui->comboBoxJoystick->currentData().toInt();
for (int i = 0; i < 4; ++i) { for (int i = 0; i < MAX_JOYSTICKS; ++i) {
auto *btn = findChild<QPushButton *>(QString("pushButtonJoystick%1").arg(i + 1)); auto *btn = findChild<QPushButton *>(QString("pushButtonJoystick%1").arg(i + 1));
if (btn == nullptr) { if (btn == nullptr) {
continue; continue;

View File

@@ -12,9 +12,11 @@
* *
* Authors: Miran Grca, <mgrca8@gmail.com> * Authors: Miran Grca, <mgrca8@gmail.com>
* GH Cao, <driver1998.ms@outlook.com> * GH Cao, <driver1998.ms@outlook.com>
* Jasmine Iwanek,
* *
* Copyright 2016-2018 Miran Grca. * Copyright 2016-2018 Miran Grca.
* Copyright 2020 GH Cao. * Copyright 2020 GH Cao.
* Copyright 2021-2023 Jasmine Iwanek.
*/ */
#include <windows.h> #include <windows.h>
#include <windowsx.h> #include <windowsx.h>
@@ -443,7 +445,7 @@ joystick_process(void)
{ {
int d; int d;
if (joystick_type == 0) if (joystick_type == JS_TYPE_NONE)
return; return;
for (int c = 0; c < joystick_get_max_joysticks(joystick_type); c++) { for (int c = 0; c < joystick_get_max_joysticks(joystick_type); c++) {

View File

@@ -98,8 +98,6 @@ 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)
{ {
LONG center;
if (joy->nr_axes >= 8) if (joy->nr_axes >= 8)
return; return;
@@ -139,14 +137,11 @@ joystick_add_axis(raw_joystick_t *rawjoy, plat_joystick_t *joy, PHIDP_VALUE_CAPS
* Some joysticks will send -1 in LogicalMax, like Xbox Controllers * Some joysticks will send -1 in LogicalMax, like Xbox Controllers
* so we need to mask that to appropriate value (instead of 0xFFFFFFFF) * so we need to mask that to appropriate value (instead of 0xFFFFFFFF)
*/ */
rawjoy->axis[joy->nr_axes].max = prop->LogicalMax & ((1 << prop->BitSize) - 1); rawjoy->axis[joy->nr_axes].max = prop->LogicalMax & ((1ULL << prop->BitSize) - 1);
} }
rawjoy->axis[joy->nr_axes].min = prop->LogicalMin; rawjoy->axis[joy->nr_axes].min = prop->LogicalMin;
center = (rawjoy->axis[joy->nr_axes].max - rawjoy->axis[joy->nr_axes].min + 1) / 2; joy->nr_axes++;
if (center != 0x00)
joy->nr_axes++;
} }
void void
@@ -450,7 +445,7 @@ joystick_process(void)
{ {
int d; int d;
if (joystick_type == 7) if (joystick_type == JS_TYPE_NONE)
return; return;
for (int c = 0; c < joystick_get_max_joysticks(joystick_type); c++) { for (int c = 0; c < joystick_get_max_joysticks(joystick_type); c++) {

View File

@@ -1349,10 +1349,10 @@ static BOOL CALLBACK
#endif #endif
win_settings_input_proc(HWND hdlg, UINT message, WPARAM wParam, LPARAM lParam) win_settings_input_proc(HWND hdlg, UINT message, WPARAM wParam, LPARAM lParam)
{ {
wchar_t str[128]; wchar_t str[128];
char *joy_name; const char *joy_name;
int c; int c;
int d; int d;
switch (message) { switch (message) {
case WM_INITDIALOG: case WM_INITDIALOG:
@@ -1408,7 +1408,7 @@ win_settings_input_proc(HWND hdlg, UINT message, WPARAM wParam, LPARAM lParam)
case IDC_COMBO_JOYSTICK: case IDC_COMBO_JOYSTICK:
temp_joystick = settings_get_cur_sel(hdlg, IDC_COMBO_JOYSTICK); temp_joystick = settings_get_cur_sel(hdlg, IDC_COMBO_JOYSTICK);
for (c = 0; c < 4; c++) for (c = 0; c < MAX_JOYSTICKS; c++)
settings_enable_window(hdlg, IDC_JOY1 + c, joystick_get_max_joysticks(temp_joystick) > c); settings_enable_window(hdlg, IDC_JOY1 + c, joystick_get_max_joysticks(temp_joystick) > c);
break; break;