No more resetting bindings for joypad, create .ini at the first start, joystick hotplug for Linux

This commit is contained in:
erorcun 2021-01-22 23:40:28 +03:00
parent 45c2488e36
commit 1b32840424
8 changed files with 133 additions and 87 deletions

View file

@ -31,6 +31,9 @@ CControllerConfigManager::CControllerConfigManager()
void CControllerConfigManager::MakeControllerActionsBlank()
{
#ifdef LOAD_INI_SETTINGS
ms_padButtonsInited = 0;
#endif
for (int32 i = 0; i < MAX_CONTROLLERTYPES; i++)
{
for (int32 j = 0; j < MAX_CONTROLLERACTIONS; j++)
@ -317,6 +320,10 @@ void CControllerConfigManager::InitDefaultControlConfigMouse(CMouseControllerSta
}
}
#ifdef LOAD_INI_SETTINGS
uint32 CControllerConfigManager::ms_padButtonsInited = 0;
#endif
void CControllerConfigManager::InitDefaultControlConfigJoyPad(uint32 buttons)
{
m_bFirstCapture = true;
@ -325,6 +332,22 @@ void CControllerConfigManager::InitDefaultControlConfigJoyPad(uint32 buttons)
if (buttons > 16)
btn = 16;
#ifdef LOAD_INI_SETTINGS
uint32 buttonMin = ms_padButtonsInited;
if (buttonMin >= btn)
return;
ms_padButtonsInited = btn;
#define IF_BTN_IN_RANGE(n) \
case n: \
if (n <= buttonMin) \
return;
#else
#define IF_BTN_IN_RANGE(n) \
case n:
#endif
// Now we use SDL Game Controller DB
#if defined RW_D3D9 || defined RWLIBS
if ( AllValidWinJoys.m_aJoys[JOYSTICK1].m_nVendorID == 0x3427
@ -337,50 +360,50 @@ void CControllerConfigManager::InitDefaultControlConfigJoyPad(uint32 buttons)
switch (btn)
{
case 16:
IF_BTN_IN_RANGE(16)
SetControllerKeyAssociatedWithAction(GO_LEFT, 16, JOYSTICK);
case 15:
IF_BTN_IN_RANGE(15)
SetControllerKeyAssociatedWithAction(GO_BACK, 15, JOYSTICK);
case 14:
IF_BTN_IN_RANGE(14)
SetControllerKeyAssociatedWithAction(GO_RIGHT, 14, JOYSTICK);
case 13:
IF_BTN_IN_RANGE(13)
SetControllerKeyAssociatedWithAction(GO_FORWARD, 13, JOYSTICK);
case 12:
case 11:
IF_BTN_IN_RANGE(12)
IF_BTN_IN_RANGE(11)
SetControllerKeyAssociatedWithAction(PED_LOOKBEHIND, 11, JOYSTICK);
SetControllerKeyAssociatedWithAction(TOGGLE_SUBMISSIONS, 11, JOYSTICK);
case 10:
IF_BTN_IN_RANGE(10)
SetControllerKeyAssociatedWithAction(VEHICLE_HORN, 10, JOYSTICK);
SetControllerKeyAssociatedWithAction(PED_DUCK, 10, JOYSTICK);
case 9:
IF_BTN_IN_RANGE(9)
SetControllerKeyAssociatedWithAction(CAMERA_CHANGE_VIEW_ALL_SITUATIONS, 9, JOYSTICK);
case 8:
IF_BTN_IN_RANGE(8)
SetControllerKeyAssociatedWithAction(VEHICLE_HANDBRAKE, 8, JOYSTICK);
SetControllerKeyAssociatedWithAction(PED_LOCK_TARGET, 8, JOYSTICK);
case 7:
IF_BTN_IN_RANGE(7)
SetControllerKeyAssociatedWithAction(PED_ANSWER_PHONE, 7, JOYSTICK);
SetControllerKeyAssociatedWithAction(VEHICLE_CHANGE_RADIO_STATION, 7, JOYSTICK);
case 6:
IF_BTN_IN_RANGE(6)
SetControllerKeyAssociatedWithAction(PED_CYCLE_WEAPON_RIGHT, 6, JOYSTICK);
SetControllerKeyAssociatedWithAction(VEHICLE_LOOKRIGHT, 6, JOYSTICK);
case 5:
IF_BTN_IN_RANGE(5)
SetControllerKeyAssociatedWithAction(PED_CYCLE_WEAPON_LEFT, 5, JOYSTICK);
SetControllerKeyAssociatedWithAction(VEHICLE_LOOKLEFT, 5, JOYSTICK);
/*******************************************************************************************/
case 4:
IF_BTN_IN_RANGE(4)
SetControllerKeyAssociatedWithAction(VEHICLE_BRAKE, 4, JOYSTICK);
SetControllerKeyAssociatedWithAction(PED_JUMPING, 4, JOYSTICK);
SetControllerKeyAssociatedWithAction(PED_SNIPER_ZOOM_IN, 4, JOYSTICK);
case 3:
IF_BTN_IN_RANGE(3)
SetControllerKeyAssociatedWithAction(VEHICLE_ACCELERATE, 3, JOYSTICK);
SetControllerKeyAssociatedWithAction(PED_SPRINT, 3, JOYSTICK);
SetControllerKeyAssociatedWithAction(PED_SNIPER_ZOOM_OUT, 3, JOYSTICK);
case 2:
IF_BTN_IN_RANGE(2)
SetControllerKeyAssociatedWithAction(PED_FIREWEAPON, 2, JOYSTICK);
#ifdef BIND_VEHICLE_FIREWEAPON
SetControllerKeyAssociatedWithAction(VEHICLE_FIREWEAPON, 2, JOYSTICK);
#endif
case 1:
IF_BTN_IN_RANGE(1)
SetControllerKeyAssociatedWithAction(VEHICLE_ENTER_EXIT, 1, JOYSTICK);
/*******************************************************************************************/
}
@ -389,47 +412,47 @@ void CControllerConfigManager::InitDefaultControlConfigJoyPad(uint32 buttons)
{
switch (btn)
{
case 16:
IF_BTN_IN_RANGE(16)
SetControllerKeyAssociatedWithAction(GO_LEFT, 16, JOYSTICK);
case 15:
IF_BTN_IN_RANGE(15)
SetControllerKeyAssociatedWithAction(GO_BACK, 15, JOYSTICK);
case 14:
IF_BTN_IN_RANGE(14)
SetControllerKeyAssociatedWithAction(GO_RIGHT, 14, JOYSTICK);
case 13:
IF_BTN_IN_RANGE(13)
SetControllerKeyAssociatedWithAction(GO_FORWARD, 13, JOYSTICK);
case 12:
case 11:
IF_BTN_IN_RANGE(12)
IF_BTN_IN_RANGE(11)
SetControllerKeyAssociatedWithAction(PED_LOOKBEHIND, 11, JOYSTICK);
SetControllerKeyAssociatedWithAction(TOGGLE_SUBMISSIONS, 11, JOYSTICK);
case 10:
IF_BTN_IN_RANGE(10)
SetControllerKeyAssociatedWithAction(VEHICLE_HORN, 10, JOYSTICK);
SetControllerKeyAssociatedWithAction(PED_DUCK, 10, JOYSTICK);
case 9:
IF_BTN_IN_RANGE(9)
SetControllerKeyAssociatedWithAction(CAMERA_CHANGE_VIEW_ALL_SITUATIONS, 9, JOYSTICK);
case 8:
IF_BTN_IN_RANGE(8)
SetControllerKeyAssociatedWithAction(VEHICLE_HANDBRAKE, 8, JOYSTICK);
SetControllerKeyAssociatedWithAction(PED_LOCK_TARGET, 8, JOYSTICK);
case 7:
IF_BTN_IN_RANGE(7)
SetControllerKeyAssociatedWithAction(PED_ANSWER_PHONE, 7, JOYSTICK);
SetControllerKeyAssociatedWithAction(VEHICLE_CHANGE_RADIO_STATION, 7, JOYSTICK);
case 6:
IF_BTN_IN_RANGE(6)
SetControllerKeyAssociatedWithAction(PED_CYCLE_WEAPON_RIGHT, 6, JOYSTICK);
SetControllerKeyAssociatedWithAction(VEHICLE_LOOKRIGHT, 6, JOYSTICK);
case 5:
IF_BTN_IN_RANGE(5)
SetControllerKeyAssociatedWithAction(PED_CYCLE_WEAPON_LEFT, 5, JOYSTICK);
SetControllerKeyAssociatedWithAction(VEHICLE_LOOKLEFT, 5, JOYSTICK);
/*******************************************************************************************/
case 4:
IF_BTN_IN_RANGE(4)
SetControllerKeyAssociatedWithAction(VEHICLE_ENTER_EXIT, 4, JOYSTICK);
case 3:
IF_BTN_IN_RANGE(3)
SetControllerKeyAssociatedWithAction(VEHICLE_BRAKE, 3, JOYSTICK);
SetControllerKeyAssociatedWithAction(PED_JUMPING, 3, JOYSTICK);
SetControllerKeyAssociatedWithAction(PED_SNIPER_ZOOM_IN, 3, JOYSTICK);
case 2:
IF_BTN_IN_RANGE(2)
SetControllerKeyAssociatedWithAction(VEHICLE_ACCELERATE, 2, JOYSTICK);
SetControllerKeyAssociatedWithAction(PED_SPRINT, 2, JOYSTICK);
SetControllerKeyAssociatedWithAction(PED_SNIPER_ZOOM_OUT, 2, JOYSTICK);
case 1:
IF_BTN_IN_RANGE(1)
SetControllerKeyAssociatedWithAction(PED_FIREWEAPON, 1, JOYSTICK);
#ifdef BIND_VEHICLE_FIREWEAPON
SetControllerKeyAssociatedWithAction(VEHICLE_FIREWEAPON, 1, JOYSTICK);

View file

@ -145,6 +145,10 @@ public:
bool m_aSimCheckers[MAX_SIMS][MAX_CONTROLLERTYPES];
bool m_bMouseAssociated;
#ifdef LOAD_INI_SETTINGS
static uint32 ms_padButtonsInited;
#endif
CControllerConfigManager();
void MakeControllerActionsBlank();

View file

@ -3045,8 +3045,13 @@ CMenuManager::LoadSettings()
CFileMgr::SetDir("");
#ifdef LOAD_INI_SETTINGS
LoadINISettings();
LoadINIControllerSettings(); // Calling that after LoadINISettings is important because of gSelectedJoystickName loading
if (LoadINISettings()) {
LoadINIControllerSettings();
} else {
// no re3.ini, create it
SaveINISettings();
SaveINIControllerSettings();
}
#endif
#ifdef FIX_BUGS
@ -3160,12 +3165,6 @@ CMenuManager::SaveSettings()
#else
m_lastWorking3DAudioProvider = m_nPrefsAudio3DProviderIndex;
static bool firstTime = true;
// In other conditions we already call SaveINIControllerSettings explicitly.
if (firstTime) {
SaveINIControllerSettings();
firstTime = false;
}
SaveINISettings();
#endif
}
@ -5450,6 +5449,9 @@ CMenuManager::SwitchMenuOnAndOff()
ThingsToDoBeforeLeavingPage();
#endif
SaveSettings();
#ifdef LOAD_INI_SETTINGS
SaveINIControllerSettings();
#endif
pControlEdit = nil;
pEditString = nil;
DisplayComboButtonErrMsg = false;

View file

@ -16,6 +16,7 @@
#include "Collision.h"
#include "ModelInfo.h"
#include "Pad.h"
#include "ControllerConfig.h"
// Menu screens array is at the bottom of the file.
@ -278,6 +279,7 @@ void ScreenModeAfterChange(int8 before, int8 after)
#ifdef DONT_TRUST_RECOGNIZED_JOYSTICKS
wchar selectedJoystickUnicode[128];
int cachedButtonNum = -1;
wchar* DetectJoystickDraw(bool* disabled, bool userHovering) {
int numButtons;
@ -306,6 +308,7 @@ wchar* DetectJoystickDraw(bool* disabled, bool userHovering) {
strcpy(gSelectedJoystickName, joyname);
PSGLOBAL(joy1id) = found;
cachedButtonNum = numButtons;
}
}
if (PSGLOBAL(joy1id) == -1)
@ -315,6 +318,18 @@ wchar* DetectJoystickDraw(bool* disabled, bool userHovering) {
return selectedJoystickUnicode;
}
void DetectJoystickGoBack() {
if (cachedButtonNum != -1) {
#ifdef LOAD_INI_SETTINGS
ControlsManager.InitDefaultControlConfigJoyPad(cachedButtonNum);
SaveINIControllerSettings();
#else
// Otherwise no way to save gSelectedJoystickName or ms_padButtonsInited anyway :shrug: Why do you even use this config.??
#endif
cachedButtonNum = -1;
}
}
#endif
CMenuScreenCustom aScreens[] = {
@ -702,8 +717,7 @@ CMenuScreenCustom aScreens[] = {
#ifdef DONT_TRUST_RECOGNIZED_JOYSTICKS
// MENUPAGE_DETECT_JOYSTICK
{ "FEC_JOD", MENUPAGE_CONTROLLER_PC, new CCustomScreenLayout({0, 0, 0, false, false, 30}), nil,
{ "FEC_JOD", MENUPAGE_CONTROLLER_PC, new CCustomScreenLayout({0, 0, 0, false, false, 30}), DetectJoystickGoBack,
MENUACTION_LABEL, "FEC_JPR", { nil, SAVESLOT_NONE, MENUPAGE_NONE }, 0, 0, 0,
MENUACTION_CFO_DYNAMIC, "FEC_JDE", { new CCFODynamic(nil, nil, nil, DetectJoystickDraw, nil) }, 80, 200, MENUALIGN_LEFT,
MENUACTION_GOBACK, "FEDS_TB", {nil, SAVESLOT_NONE, MENUPAGE_NONE}, 320, 225, MENUALIGN_CENTER,

View file

@ -45,7 +45,7 @@ void TheModelViewer(void);
#endif
#ifdef LOAD_INI_SETTINGS
void LoadINISettings();
bool LoadINISettings();
void SaveINISettings();
void LoadINIControllerSettings();
void SaveINIControllerSettings();

View file

@ -249,6 +249,14 @@ const char *iniKeyboardButtons[] = {"ESC","F1","F2","F3","F4","F5","F6","F7","F8
void LoadINIControllerSettings()
{
#ifdef DONT_TRUST_RECOGNIZED_JOYSTICKS
ReadIniIfExists("Controller", "JoystickName", gSelectedJoystickName, 128);
#endif
// force to default GTA behaviour (never overwrite bindings on joy change/initialization) if user init'ed/set bindings before we introduced that
if (!ReadIniIfExists("Controller", "PadButtonsInited", &ControlsManager.ms_padButtonsInited)) {
ControlsManager.ms_padButtonsInited = cfg.category_size("Bindings") != 0 ? 16 : 0;
}
for (int32 i = 0; i < MAX_CONTROLLERACTIONS; i++) {
char value[128];
if (ReadIniIfExists("Bindings", iniControllerActions[i], value, 128)) {
@ -340,12 +348,17 @@ void SaveINIControllerSettings()
StoreIni("Bindings", iniControllerActions[i], value, 128);
}
cfg.write_file("re3.ini");
#ifdef DONT_TRUST_RECOGNIZED_JOYSTICKS
StoreIni("Controller", "JoystickName", gSelectedJoystickName, 128);
#endif
StoreIni("Controller", "PadButtonsInited", ControlsManager.ms_padButtonsInited);
cfg.write_file("reVC.ini");
}
void LoadINISettings()
bool LoadINISettings()
{
cfg.load_file("reVC.ini");
if (!cfg.load_file("reVC.ini"))
return false;
#ifdef IMPROVED_VIDEOMODE
ReadIniIfExists("VideoMode", "Width", &FrontEndMenuManager.m_nPrefsWidth);
@ -404,40 +417,6 @@ void LoadINISettings()
ReadIniIfExists("Draw", "FixSprites", &CDraw::ms_bFixSprites);
#endif
#ifdef DONT_TRUST_RECOGNIZED_JOYSTICKS
// Written by assuming the codes below will run after _InputInitialiseJoys().
std::string strval = cfg.get("Controller", "JoystickName", "");
const char *value = strval.c_str();
strcpy(gSelectedJoystickName, value);
if(gSelectedJoystickName[0] != '\0') {
for (int i = 0; i <= GLFW_JOYSTICK_LAST; i++) {
if (glfwJoystickPresent(i) && strncmp(gSelectedJoystickName, glfwGetJoystickName(i), strlen(gSelectedJoystickName)) == 0) {
if (PSGLOBAL(joy1id) != -1) {
PSGLOBAL(joy2id) = PSGLOBAL(joy1id);
}
PSGLOBAL(joy1id) = i;
int count;
glfwGetJoystickButtons(PSGLOBAL(joy1id), &count);
// We need to init and reload bindings, because;
// 1-joypad button number may differ with saved/prvly connected one
// 2-bindings are not init'ed if there is no joypad at the start
ControlsManager.InitDefaultControlConfigJoyPad(count);
CFileMgr::SetDirMyDocuments();
int32 gta3set = CFileMgr::OpenFile("gta3.set", "r");
if (gta3set) {
ControlsManager.LoadSettings(gta3set);
CFileMgr::CloseFile(gta3set);
}
CFileMgr::SetDir("");
// We call LoadINIControllerSettings after this func., so calling here isn't needed
break;
}
}
}
#endif
#ifdef CUSTOM_FRONTEND_OPTIONS
bool migrate = cfg.category_size("FrontendOptions") != 0;
for (int i = 0; i < MENUPAGES; i++) {
@ -463,6 +442,8 @@ void LoadINISettings()
}
}
#endif
return true;
}
void SaveINISettings()
@ -524,10 +505,6 @@ void SaveINISettings()
#ifdef FIX_SPRITES
StoreIni("Draw", "FixSprites", CDraw::ms_bFixSprites);
#endif
#ifdef DONT_TRUST_RECOGNIZED_JOYSTICKS
StoreIni("Controller", "JoystickName", gSelectedJoystickName, 128);
#endif
#ifdef CUSTOM_FRONTEND_OPTIONS
for (int i = 0; i < MENUPAGES; i++) {
for (int j = 0; j < NUM_MENUROWS; j++) {

View file

@ -1634,10 +1634,16 @@ main(int argc, char *argv[])
SystemParametersInfo(SPI_SETSTICKYKEYS, sizeof(STICKYKEYS), &NewStickyKeys, SPIF_SENDCHANGE);
#endif
// This part is needed because controller initialisation overwrites loaded settings.
{
CFileMgr::SetDirMyDocuments();
#ifdef LOAD_INI_SETTINGS
// At this point InitDefaultControlConfigJoyPad must have set all bindings to default and ms_padButtonsInited to number of detected buttons.
// We will load stored bindings below, but let's cache ms_padButtonsInited before LoadINIControllerSettings and LoadSettings clears it,
// so we can add new joy bindings **on top of** stored bindings.
int connectedPadButtons = ControlsManager.ms_padButtonsInited;
#endif
int32 gta3set = CFileMgr::OpenFile("gta_vc.set", "r");
if ( gta3set )
@ -1650,6 +1656,10 @@ main(int argc, char *argv[])
#ifdef LOAD_INI_SETTINGS
LoadINIControllerSettings();
if (connectedPadButtons != 0) {
ControlsManager.InitDefaultControlConfigJoyPad(connectedPadButtons);
SaveINIControllerSettings();
}
#endif
}
@ -2163,6 +2173,12 @@ void joysChangeCB(int jid, int event)
PSGLOBAL(joy1id) = jid;
#ifdef DONT_TRUST_RECOGNIZED_JOYSTICKS
strcpy(gSelectedJoystickName, glfwGetJoystickName(jid));
#endif
// This is behind LOAD_INI_SETTINGS, because otherwise the Init call below will destroy/overwrite your bindings.
#ifdef LOAD_INI_SETTINGS
int count;
glfwGetJoystickButtons(PSGLOBAL(joy1id), &count);
ControlsManager.InitDefaultControlConfigJoyPad(count);
#endif
} else if (PSGLOBAL(joy2id) == -1)
PSGLOBAL(joy2id) = jid;

View file

@ -2142,10 +2142,16 @@ WinMain(HINSTANCE instance,
ShowWindow(PSGLOBAL(window), cmdShow);
UpdateWindow(PSGLOBAL(window));
// This part is needed because controller initialisation overwrites loaded settings.
{
CFileMgr::SetDirMyDocuments();
#ifdef LOAD_INI_SETTINGS
// At this point InitDefaultControlConfigJoyPad must have set all bindings to default and ms_padButtonsInited to number of detected buttons.
// We will load stored bindings below, but let's cache ms_padButtonsInited before LoadINIControllerSettings and LoadSettings clears it,
// so we can add new joy bindings **on top of** stored bindings.
int connectedPadButtons = ControlsManager.ms_padButtonsInited;
#endif
int32 gta3set = CFileMgr::OpenFile("gta_vc.set", "r");
if ( gta3set )
@ -2158,6 +2164,10 @@ WinMain(HINSTANCE instance,
#ifdef LOAD_INI_SETTINGS
LoadINIControllerSettings();
if (connectedPadButtons != 0) {
ControlsManager.InitDefaultControlConfigJoyPad(connectedPadButtons);
SaveINIControllerSettings();
}
#endif
}