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

View File

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

View File

@@ -24,6 +24,16 @@
#define MAX_PLAT_JOYSTICKS 8
#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_Y 0x40000000
#define SLIDER 0x20000000
@@ -84,11 +94,11 @@ typedef struct joystick_if_t {
const char *internal_name;
void *(*init)(void);
void (*close)(void *p);
uint8_t (*read)(void *p);
void (*write)(void *p);
int (*read_axis)(void *p, int axis);
void (*a0_over)(void *p);
void (*close)(void *priv);
uint8_t (*read)(void *priv);
void (*write)(void *priv);
int (*read_axis)(void *priv, int axis);
void (*a0_over)(void *priv);
int axis_count;
int button_count;
@@ -133,16 +143,16 @@ extern void joystick_init(void);
extern void joystick_close(void);
extern void joystick_process(void);
extern char *joystick_get_name(int js);
extern char *joystick_get_internal_name(int js);
extern int joystick_get_from_internal_name(char *s);
extern int joystick_get_max_joysticks(int js);
extern int joystick_get_axis_count(int js);
extern int joystick_get_button_count(int js);
extern int joystick_get_pov_count(int js);
extern char *joystick_get_axis_name(int js, int id);
extern char *joystick_get_button_name(int js, int id);
extern char *joystick_get_pov_name(int js, int id);
extern const char *joystick_get_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_max_joysticks(int js);
extern int joystick_get_axis_count(int js);
extern int joystick_get_button_count(int js);
extern int joystick_get_pov_count(int js);
extern const char *joystick_get_axis_name(int js, int id);
extern const char *joystick_get_button_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_remap(void *priv, uint16_t address);

View File

@@ -87,9 +87,9 @@ SettingsInput::onCurrentMachineChanged(int machineId)
mouseModel->removeRows(0, removeRows);
ui->comboBoxMouse->setCurrentIndex(selectedRow);
int i = 0;
char *joyName = joystick_get_name(i);
auto *joystickModel = ui->comboBoxJoystick->model();
int i = 0;
const char *joyName = joystick_get_name(i);
auto *joystickModel = ui->comboBoxJoystick->model();
removeRows = joystickModel->rowCount();
selectedRow = 0;
while (joyName) {
@@ -116,7 +116,7 @@ void
SettingsInput::on_comboBoxJoystick_currentIndexChanged(int index)
{
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));
if (btn == nullptr) {
continue;

View File

@@ -12,9 +12,11 @@
*
* Authors: Miran Grca, <mgrca8@gmail.com>
* GH Cao, <driver1998.ms@outlook.com>
* Jasmine Iwanek,
*
* Copyright 2016-2018 Miran Grca.
* Copyright 2020 GH Cao.
* Copyright 2021-2023 Jasmine Iwanek.
*/
#include <windows.h>
#include <windowsx.h>
@@ -443,7 +445,7 @@ joystick_process(void)
{
int d;
if (joystick_type == 0)
if (joystick_type == JS_TYPE_NONE)
return;
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
joystick_add_axis(raw_joystick_t *rawjoy, plat_joystick_t *joy, PHIDP_VALUE_CAPS prop)
{
LONG center;
if (joy->nr_axes >= 8)
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
* 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;
center = (rawjoy->axis[joy->nr_axes].max - rawjoy->axis[joy->nr_axes].min + 1) / 2;
if (center != 0x00)
joy->nr_axes++;
joy->nr_axes++;
}
void
@@ -450,7 +445,7 @@ joystick_process(void)
{
int d;
if (joystick_type == 7)
if (joystick_type == JS_TYPE_NONE)
return;
for (int c = 0; c < joystick_get_max_joysticks(joystick_type); c++) {

View File

@@ -1349,10 +1349,10 @@ static BOOL CALLBACK
#endif
win_settings_input_proc(HWND hdlg, UINT message, WPARAM wParam, LPARAM lParam)
{
wchar_t str[128];
char *joy_name;
int c;
int d;
wchar_t str[128];
const char *joy_name;
int c;
int d;
switch (message) {
case WM_INITDIALOG:
@@ -1408,7 +1408,7 @@ win_settings_input_proc(HWND hdlg, UINT message, WPARAM wParam, LPARAM lParam)
case 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);
break;