Skip to content

Commit

Permalink
Merge pull request PCSX2#168 from SeventySixx/main
Browse files Browse the repository at this point in the history
Added dead zone options for L and R gamepad thumbsticks
  • Loading branch information
inactive123 committed Sep 25, 2021
2 parents 75f2e86 + eff02a9 commit 2bca850
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 5 deletions.
119 changes: 119 additions & 0 deletions libretro/libretro_core_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,125 @@ struct retro_core_option_definition option_defs[] = {
},
"100"},

{ INT_PCSX2_OPT_GAMEPAD_L_DEADZONE,
"Gamepad: Left Stick Dead Zone",
"Set the dead zone of left thumbstick",
{
{"0", "No Dead Zone (default)"},
{"1", "1%"},
{"2", "2%"},
{"3", "3%"},
{"4", "4%"},
{"5", "5%"},
{"6", "6%"},
{"7", "7%"},
{"8", "8%"},
{"9", "9%"},
{"10", "10%"},
{"11", "11%"},
{"12", "12%"},
{"13", "13%"},
{"14", "14%"},
{"15", "15%"},
{"16", "16%"},
{"17", "17%"},
{"18", "18%"},
{"19", "19%"},
{"20", "20%"},
{"21", "21%"},
{"22", "22%"},
{"23", "23%"},
{"24", "24%"},
{"25", "25%"},
{"26", "26%"},
{"27", "27%"},
{"28", "28%"},
{"29", "29%"},
{"30", "30%"},
{"31", "31%"},
{"32", "32%"},
{"33", "33%"},
{"34", "34%"},
{"35", "35%"},
{"36", "36%"},
{"37", "37%"},
{"38", "38%"},
{"39", "39%"},
{"40", "40%"},
{"41", "41%"},
{"42", "42%"},
{"43", "43%"},
{"44", "44%"},
{"45", "45%"},
{"46", "46%"},
{"47", "47%"},
{"48", "48%"},
{"49", "49%"},
{"50", "50%"},
{NULL, NULL},
},
"enabled" },


{ INT_PCSX2_OPT_GAMEPAD_R_DEADZONE,
"Gamepad: Right Stick Dead Zone",
"Set the dead zone of right thumbstick",
{
{"0", "No Dead Zone (default)"},
{"1", "1%"},
{"2", "2%"},
{"3", "3%"},
{"4", "4%"},
{"5", "5%"},
{"6", "6%"},
{"7", "7%"},
{"8", "8%"},
{"9", "9%"},
{"10", "10%"},
{"11", "11%"},
{"12", "12%"},
{"13", "13%"},
{"14", "14%"},
{"15", "15%"},
{"16", "16%"},
{"17", "17%"},
{"18", "18%"},
{"19", "19%"},
{"20", "20%"},
{"21", "21%"},
{"22", "22%"},
{"23", "23%"},
{"24", "24%"},
{"25", "25%"},
{"26", "26%"},
{"27", "27%"},
{"28", "28%"},
{"29", "29%"},
{"30", "30%"},
{"31", "31%"},
{"32", "32%"},
{"33", "33%"},
{"34", "34%"},
{"35", "35%"},
{"36", "36%"},
{"37", "37%"},
{"38", "38%"},
{"39", "39%"},
{"40", "40%"},
{"41", "41%"},
{"42", "42%"},
{"43", "43%"},
{"44", "44%"},
{"45", "45%"},
{"46", "46%"},
{"47", "47%"},
{"48", "48%"},
{"49", "49%"},
{"50", "50%"},
{NULL, NULL},
},
"enabled" },

{BOOL_PCSX2_OPT_ENABLE_CHEATS,
"Patches: Enable Cheats",
"Enabled: Checks the 'system/pcsx2/cheats' directory for a PNACH file for the running content and, if found, \
Expand Down
9 changes: 9 additions & 0 deletions libretro/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,17 @@ static struct retro_perf_callback perf_cb;

static bool init_failed = false;
int option_upscale_mult = 1;
int option_pad_left_deadzone = 0;
int option_pad_right_deadzone = 0;

std::string sel_bios_path = "";
retro_environment_t environ_cb;
retro_video_refresh_t video_cb;
struct retro_hw_render_callback hw_render;
unsigned libretro_msg_interface_version = 0;
retro_log_printf_t log_cb;


std::string retroarch_system_path;

// renderswitch - tells GSdx to go into dx9 sw if "renderswitch" is set.
Expand Down Expand Up @@ -390,6 +394,9 @@ void retro_init(void)
SSE_RoundMode VUs_roundMode = (SSE_RoundMode)option_value(INT_PCSX2_OPT_VU_ROUND_MODE, KeyOptionInt::return_type);
g_Conf->EmuOptions.Cpu.sseVUMXCSR.SetRoundMode(VUs_roundMode);

option_pad_left_deadzone = option_value(INT_PCSX2_OPT_GAMEPAD_L_DEADZONE, KeyOptionInt::return_type);
option_pad_right_deadzone = option_value(INT_PCSX2_OPT_GAMEPAD_R_DEADZONE, KeyOptionInt::return_type);

static retro_disk_control_ext_callback disk_control = {
DiskControl::set_eject_state,
DiskControl::get_eject_state,
Expand Down Expand Up @@ -858,6 +865,8 @@ void retro_run(void)
option_value(BOOL_PCSX2_OPT_GAMEPAD_RUMBLE_ENABLE, KeyOptionBool::return_type),
option_value(INT_PCSX2_OPT_GAMEPAD_RUMBLE_FORCE, KeyOptionInt::return_type)
);
option_pad_left_deadzone = option_value(INT_PCSX2_OPT_GAMEPAD_L_DEADZONE, KeyOptionInt::return_type);
option_pad_right_deadzone = option_value(INT_PCSX2_OPT_GAMEPAD_R_DEADZONE, KeyOptionInt::return_type);

}

Expand Down
2 changes: 2 additions & 0 deletions libretro/options_enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#define INT_PCSX2_OPT_VU_CLAMPING_MODE "pcsx2_vu_clamping_mode"
#define INT_PCSX2_OPT_VU_ROUND_MODE "pcsx2_vu_round_mode"
#define INT_PCSX2_OPT_DITHERING "pcsx2_dithering"
#define INT_PCSX2_OPT_GAMEPAD_L_DEADZONE "pcsx2_gamepad_l_deadzone"
#define INT_PCSX2_OPT_GAMEPAD_R_DEADZONE "pcsx2_gamepad_r_deadzone"

#define INT_PCSX2_OPT_USERHACK_TEXTURE_OFFSET_X_HUNDREDS "pcsx2_userhack_texture_offset_x_hundreds"
#define INT_PCSX2_OPT_USERHACK_TEXTURE_OFFSET_X_TENS "pcsx2_userhack_texture_offset_x_tens"
Expand Down
3 changes: 2 additions & 1 deletion libretro/options_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ extern retro_log_printf_t log_cb;
extern void GSUpdateOptions();
extern void ResetContentStuffs();
extern int option_upscale_mult;

extern int option_pad_left_deadzone;
extern int option_pad_right_deadzone;
/*
* These are quick fixes to provide system paths at pcsx2 app startup.
* Because of the huge refactoring, paths are not saved/loaded from inis files anymore,
Expand Down
69 changes: 65 additions & 4 deletions pcsx2/PAD/PAD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include <unistd.h>
#endif

#include "options_tools.h"

const u32 revision = 2;
const u32 build = 0; // increase that with each version
#define PAD_SAVE_STATE_VERSION ((revision << 8) | (build << 0))
Expand Down Expand Up @@ -228,29 +230,41 @@ u16 KeyStatus::get(u32 pad)
return new_mask;
}


// +- 32766
u8 KeyStatus::get(u32 pad, u32 index)
{
int x = 0;
int y = 0;
int val = 0;
switch (index)
{
case PAD_R_LEFT:
case PAD_R_RIGHT:
val = input_cb(pad, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X);
x = input_cb(pad, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X);
y = input_cb(pad, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y);
val = ApplyDeadZoneX(x, y, option_pad_right_deadzone);
break;

case PAD_R_DOWN:
case PAD_R_UP:
val = input_cb(pad, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y);
x = input_cb(pad, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X);
y = input_cb(pad, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y);
val = ApplyDeadZoneY(x, y, option_pad_right_deadzone);
break;

case PAD_L_LEFT:
case PAD_L_RIGHT:
val = input_cb(pad, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
x = input_cb(pad, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
y = input_cb(pad, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y);
val = ApplyDeadZoneX(x, y, option_pad_left_deadzone);
break;

case PAD_L_DOWN:
case PAD_L_UP:
val = input_cb(pad, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y);
x = input_cb(pad, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
y = input_cb(pad, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y);
val = ApplyDeadZoneY(x, y, option_pad_left_deadzone);
break;

default:
Expand All @@ -265,6 +279,53 @@ u8 KeyStatus::get(u32 pad, u32 index)
return 0x80 + (val >> 8);
}


int ApplyDeadZoneX(int val_x, int val_y, float deadzone_percent) {

float deadzone_axis = 32767.0f * deadzone_percent / 100.0f;

float magnitude = (float)sqrt((val_x * val_x) + (val_y * val_y));

if ((magnitude < deadzone_axis) || magnitude == 0.0f)
return 0;
else
{
float normalized_x = val_x / magnitude;
int ret_val = (int)((normalized_x * ((magnitude - deadzone_axis) / (32767.0f - deadzone_axis))) * 32767.0f);
if (abs(ret_val) > 32767) {
ret_val = 32767 * abs(ret_val) / ret_val;
}
return ret_val;

}

}

int ApplyDeadZoneY(int val_x, int val_y, float deadzone_percent) {

float deadzone_val_percent = 50.0f;
float deadzone_axis = 32767.0f * deadzone_percent / 100.0f;

float magnitude = (float)sqrt((val_x * val_x) + (val_y * val_y));

if ((magnitude < deadzone_axis) || magnitude == 0.0f)
return 0;
else
{
// we keep a smooth ""kick" into motion
float normalized_y = val_y / magnitude;
int ret_val = (int)((normalized_y * ((magnitude - deadzone_axis) / (32767.0f - deadzone_axis))) * 32767.0f);
if (abs(ret_val) > 32767) {
ret_val = 32767 * abs(ret_val) / ret_val;
}
return ret_val;

}


}


s32 PADinit(u32 flags)
{
Pad::reset_all();
Expand Down
2 changes: 2 additions & 0 deletions pcsx2/PAD/PAD.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,7 @@ s32 PADsetSlot(u8 port, u8 slot);
void PADshutdown();

void GamePad_DoRumble(unsigned type, unsigned pad);
int ApplyDeadZoneX(int val_x, int val_y, float deadzone_percent);
int ApplyDeadZoneY(int val_x, int val_y, float deadzone_percent);

#endif

0 comments on commit 2bca850

Please sign in to comment.