Added logic to init game pad mappings at startup

This commit is contained in:
Matthew Budd 2020-07-24 21:47:57 -04:00
parent 67e87787e3
commit da04bbdfc5
6 changed files with 160 additions and 110 deletions

View File

@ -26,7 +26,7 @@ GamePadConfDialog_t::GamePadConfDialog_t(QWidget *parent)
QPushButton *saveProfileButton; QPushButton *saveProfileButton;
QPushButton *applyProfileButton; QPushButton *applyProfileButton;
QPushButton *removeProfileButton; QPushButton *removeProfileButton;
QPushButton *loadDefaultButton; //QPushButton *loadDefaultButton;
QPushButton *clearAllButton; QPushButton *clearAllButton;
QPushButton *closebutton; QPushButton *closebutton;
QPushButton *clearButton[GAMEPAD_NUM_BUTTONS]; QPushButton *clearButton[GAMEPAD_NUM_BUTTONS];
@ -75,9 +75,17 @@ GamePadConfDialog_t::GamePadConfDialog_t(QWidget *parent)
char stmp[128]; char stmp[128];
sprintf( stmp, "%i: %s", i, js->getName() ); sprintf( stmp, "%i: %s", i, js->getName() );
devSel->addItem( tr(stmp), i ); devSel->addItem( tr(stmp), i );
} }
} }
} }
for (int i=0; i<devSel->count(); i++)
{
if ( devSel->itemData(i).toInt() == GamePad[portNum].getDeviceIndex() )
{
devSel->setCurrentIndex( i );
}
}
label = new QLabel(tr("GUID:")); label = new QLabel(tr("GUID:"));
guidLbl = new QLabel(); guidLbl = new QLabel();
@ -85,6 +93,8 @@ GamePadConfDialog_t::GamePadConfDialog_t(QWidget *parent)
hbox3->addWidget( label ); hbox3->addWidget( label );
hbox3->addWidget( guidLbl ); hbox3->addWidget( guidLbl );
guidLbl->setText( GamePad[portNum].getGUID() );
frame1 = new QGroupBox(tr("Mapping Profile:")); frame1 = new QGroupBox(tr("Mapping Profile:"));
//grid = new QGridLayout(); //grid = new QGridLayout();
vbox = new QVBoxLayout(); vbox = new QVBoxLayout();
@ -171,11 +181,11 @@ GamePadConfDialog_t::GamePadConfDialog_t(QWidget *parent)
} }
updateCntrlrDpy(); updateCntrlrDpy();
loadDefaultButton = new QPushButton(tr("Load Defaults")); //loadDefaultButton = new QPushButton(tr("Load Defaults"));
clearAllButton = new QPushButton(tr("Clear All")); clearAllButton = new QPushButton(tr("Clear All"));
closebutton = new QPushButton(tr("Close")); closebutton = new QPushButton(tr("Close"));
hbox4->addWidget( loadDefaultButton ); //hbox4->addWidget( loadDefaultButton );
hbox4->addWidget( clearAllButton ); hbox4->addWidget( clearAllButton );
hbox4->addWidget( closebutton ); hbox4->addWidget( closebutton );
@ -206,7 +216,7 @@ GamePadConfDialog_t::GamePadConfDialog_t(QWidget *parent)
connect(saveProfileButton , SIGNAL(clicked()), this, SLOT(saveProfileCallback(void)) ); connect(saveProfileButton , SIGNAL(clicked()), this, SLOT(saveProfileCallback(void)) );
connect(removeProfileButton, SIGNAL(clicked()), this, SLOT(deleteProfileCallback(void)) ); connect(removeProfileButton, SIGNAL(clicked()), this, SLOT(deleteProfileCallback(void)) );
connect(loadDefaultButton, SIGNAL(clicked()), this, SLOT(loadDefaults(void)) ); //connect(loadDefaultButton, SIGNAL(clicked()), this, SLOT(loadDefaults(void)) );
connect(clearAllButton , SIGNAL(clicked()), this, SLOT(clearAllCallback(void)) ); connect(clearAllButton , SIGNAL(clicked()), this, SLOT(clearAllCallback(void)) );
connect(closebutton , SIGNAL(clicked()), this, SLOT(closeWindow(void)) ); connect(closebutton , SIGNAL(clicked()), this, SLOT(closeWindow(void)) );
@ -334,6 +344,17 @@ void GamePadConfDialog_t::portSelect(int index)
//printf("Port Number:%i \n", index); //printf("Port Number:%i \n", index);
portNum = index; portNum = index;
updateCntrlrDpy(); updateCntrlrDpy();
for (int i=0; i<devSel->count(); i++)
{
if ( devSel->itemData(i).toInt() == GamePad[portNum].getDeviceIndex() )
{
devSel->setCurrentIndex( i );
}
}
guidLbl->setText( GamePad[portNum].getGUID() );
loadMapList();
} }
//---------------------------------------------------- //----------------------------------------------------
void GamePadConfDialog_t::deviceSelect(int index) void GamePadConfDialog_t::deviceSelect(int index)
@ -377,8 +398,8 @@ void GamePadConfDialog_t::oppDirEna(int state)
//---------------------------------------------------- //----------------------------------------------------
void GamePadConfDialog_t::changeButton(int padNo, int x) void GamePadConfDialog_t::changeButton(int padNo, int x)
{ {
char buf[256]; //char buf[256];
std::string prefix; //std::string prefix;
const char *keyNameStr; const char *keyNameStr;
if ( buttonConfigStatus == 2 ) if ( buttonConfigStatus == 2 )
@ -392,28 +413,8 @@ void GamePadConfDialog_t::changeButton(int padNo, int x)
button[x]->setText("Waiting" ); button[x]->setText("Waiting" );
snprintf (buf, sizeof(buf)-1, "SDL.Input.GamePad.%d.", padNo);
prefix = buf;
DWaitButton (NULL, &GamePad[padNo].bmap[x], &buttonConfigStatus ); DWaitButton (NULL, &GamePad[padNo].bmap[x], &buttonConfigStatus );
g_config->setOption (prefix + GamePadNames[x],
GamePad[padNo].bmap[x].ButtonNum);
if (GamePad[padNo].bmap[x].ButtType == BUTTC_KEYBOARD)
{
g_config->setOption (prefix + "DeviceType", "Keyboard");
}
else if (GamePad[padNo].bmap[x].ButtType == BUTTC_JOYSTICK)
{
g_config->setOption (prefix + "DeviceType", "Joystick");
}
else
{
g_config->setOption (prefix + "DeviceType", "Unknown");
}
g_config->setOption (prefix + "DeviceNum",
GamePad[padNo].bmap[x].DeviceNum);
keyNameStr = ButtonName( &GamePad[padNo].bmap[x] ); keyNameStr = ButtonName( &GamePad[padNo].bmap[x] );
keyName[x]->setText( keyNameStr ); keyName[x]->setText( keyNameStr );
@ -564,55 +565,6 @@ void GamePadConfDialog_t::clearAllCallback(void)
} }
} }
//---------------------------------------------------- //----------------------------------------------------
void GamePadConfDialog_t::loadDefaults(void)
{
int index, devIdx;
char buf[256];
std::string prefix;
index = devSel->currentIndex();
devIdx = devSel->itemData(index).toInt();
//printf("Selected Device:%i : %i \n", index, devIdx );
if ( devIdx == -1 )
{
snprintf (buf, sizeof(buf)-1, "SDL.Input.GamePad.%d.", portNum);
prefix = buf;
for (int x=0; x<GAMEPAD_NUM_BUTTONS; x++)
{
GamePad[portNum].bmap[x].ButtType = BUTTC_KEYBOARD;
GamePad[portNum].bmap[x].DeviceNum = 0;
GamePad[portNum].bmap[x].ButtonNum = DefaultGamePad[portNum][x];
g_config->setOption (prefix + GamePadNames[x],
GamePad[portNum].bmap[x].ButtonNum);
if (GamePad[portNum].bmap[x].ButtType == BUTTC_KEYBOARD)
{
g_config->setOption (prefix + "DeviceType", "Keyboard");
}
else if (GamePad[portNum].bmap[x].ButtType == BUTTC_JOYSTICK)
{
g_config->setOption (prefix + "DeviceType", "Joystick");
}
else
{
g_config->setOption (prefix + "DeviceType", "Unknown");
}
g_config->setOption (prefix + "DeviceNum",
GamePad[portNum].bmap[x].DeviceNum);
}
}
else
{
GamePad[portNum].setDeviceIndex( devIdx );
GamePad[portNum].loadDefaults();
}
updateCntrlrDpy();
}
//----------------------------------------------------
void GamePadConfDialog_t::createNewProfile( const char *name ) void GamePadConfDialog_t::createNewProfile( const char *name )
{ {
printf("Creating: %s \n", name ); printf("Creating: %s \n", name );
@ -663,6 +615,13 @@ void GamePadConfDialog_t::loadProfileCallback(void)
} }
if ( ret == 0 ) if ( ret == 0 )
{ {
std::string prefix;
sprintf( stmp, "SDL.Input.GamePad.%u.", portNum );
prefix = stmp;
g_config->setOption(prefix + "DeviceGUID", GamePad[portNum].getGUID() );
g_config->setOption(prefix + "Profile" , mapName.c_str() );
sprintf( stmp, "Mapping Loaded: %s/%s \n", GamePad[portNum].getGUID(), mapName.c_str() ); sprintf( stmp, "Mapping Loaded: %s/%s \n", GamePad[portNum].getGUID(), mapName.c_str() );
} }
else else

View File

@ -86,7 +86,7 @@ class GamePadConfDialog_t : public QDialog
void clearButton8(void); void clearButton8(void);
void clearButton9(void); void clearButton9(void);
void clearAllCallback(void); void clearAllCallback(void);
void loadDefaults(void); //void loadDefaults(void);
void ena4score(int state); void ena4score(int state);
void oppDirEna(int state); void oppDirEna(int state);
void portSelect(int index); void portSelect(int index);

View File

@ -317,10 +317,8 @@ InitConfig()
prefix = buf; prefix = buf;
config->addOption(prefix + "DeviceType", DefaultGamePadDevice[i]); config->addOption(prefix + "DeviceType", DefaultGamePadDevice[i]);
config->addOption(prefix + "DeviceNum", 0); config->addOption(prefix + "DeviceGUID", "");
for(unsigned int j = 0; j < GAMEPAD_NUM_BUTTONS; j++) { config->addOption(prefix + "Profile" , "");
config->addOption(prefix + GamePadNames[j], DefaultGamePad[i][j]);
}
} }
// PowerPad 0 - 1 // PowerPad 0 - 1

View File

@ -1960,7 +1960,7 @@ void InputCfg (const std::string & text)
UpdateInput (Config * config) UpdateInput (Config * config)
{ {
char buf[64]; char buf[64];
std::string device, prefix; std::string device, prefix, guid, mapping;
InitJoysticks(); InitJoysticks();
@ -2055,37 +2055,18 @@ UpdateInput (Config * config)
snprintf (buf, sizeof(buf)-1, "SDL.Input.GamePad.%u.", i); snprintf (buf, sizeof(buf)-1, "SDL.Input.GamePad.%u.", i);
prefix = buf; prefix = buf;
config->getOption (prefix + "DeviceType", &device); config->getOption (prefix + "DeviceType", &device );
if (device.find ("Keyboard") != std::string::npos) config->getOption (prefix + "DeviceGUID", &guid );
{ config->getOption (prefix + "Profile" , &mapping);
type = BUTTC_KEYBOARD;
}
else if (device.find ("Joystick") != std::string::npos)
{
type = BUTTC_JOYSTICK;
}
else
{
type = 0;
}
//FIXME GamePad[i].init( i, guid.c_str(), mapping.c_str() );
//config->getOption (prefix + "DeviceNum", &devnum);
//for (unsigned int j = 0; j < GAMEPAD_NUM_BUTTONS; j++)
//{
// config->getOption (prefix + GamePadNames[j], &button);
// GamePadConfig[i][j].ButtType = type;
// GamePadConfig[i][j].DeviceNum = devnum;
// GamePadConfig[i][j].ButtonNum = button;
//}
} }
// PowerPad 0 - 1 // PowerPad 0 - 1
for (unsigned int i = 0; i < POWERPAD_NUM_DEVICES; i++) for (unsigned int i = 0; i < POWERPAD_NUM_DEVICES; i++)
{ {
char buf[64]; char buf[64];
snprintf (buf, 32, "SDL.Input.PowerPad.%u.", i); snprintf (buf, sizeof(buf)-1, "SDL.Input.PowerPad.%u.", i);
prefix = buf; prefix = buf;
config->getOption (prefix + "DeviceType", &device); config->getOption (prefix + "DeviceType", &device);

View File

@ -55,6 +55,8 @@ jsDev_t::jsDev_t(void)
{ {
js = NULL; js = NULL;
gc = NULL; gc = NULL;
devIdx = 0;
portBindMask = 0;
}; };
//******************************************************************************** //********************************************************************************
@ -92,6 +94,25 @@ const char *jsDev_t::getGUID(void)
return ( guidStr.c_str() ); return ( guidStr.c_str() );
} }
//********************************************************************************
int jsDev_t::bindPort( int idx )
{
portBindMask |= (0x00000001 << idx);
return portBindMask;
}
//********************************************************************************
int jsDev_t::unbindPort( int idx )
{
portBindMask &= ~(0x00000001 << idx);
return portBindMask;
}
//********************************************************************************
int jsDev_t::getBindPorts(void)
{
return portBindMask;
}
//******************************************************************************** //********************************************************************************
bool jsDev_t::isGameController(void) bool jsDev_t::isGameController(void)
{ {
@ -243,7 +264,8 @@ int nesGamePadMap_t::parseMapping( const char *map )
//******************************************************************************** //********************************************************************************
GamePad_t::GamePad_t(void) GamePad_t::GamePad_t(void)
{ {
devIdx = -1; devIdx = -1;
portNum = 0;
for (int i=0; i<GAMEPAD_NUM_BUTTONS; i++) for (int i=0; i<GAMEPAD_NUM_BUTTONS; i++)
{ {
@ -257,11 +279,92 @@ GamePad_t::GamePad_t(void)
GamePad_t::~GamePad_t(void) GamePad_t::~GamePad_t(void)
{ {
}
//********************************************************************************
int GamePad_t::init( int port, const char *guid, const char *profile )
{
int i, mask;
portNum = port;
// First look for a controller that matches the specific GUID
// that is not already in use by another port.
if ( devIdx < 0 )
{
for (i=0; i<MAX_JOYSTICKS; i++)
{
mask = jsDev[i].getBindPorts();
if ( mask != 0 )
{
continue;
}
if ( !jsDev[i].isConnected() )
{
continue;
}
if ( strcmp( jsDev[i].getGUID(), guid ) == 0 )
{
setDeviceIndex( i );
if ( loadProfile( profile, guid ) )
{
loadDefaults();
}
break;
}
}
}
// If a specific controller was not matched,
// then look for any game controller that is plugged in
// and not already bound.
if ( devIdx < 0 )
{
for (i=0; i<MAX_JOYSTICKS; i++)
{
mask = jsDev[i].getBindPorts();
if ( mask != 0 )
{
continue;
}
if ( jsDev[i].isGameController() )
{
setDeviceIndex( i );
if ( loadProfile( profile ) )
{
loadDefaults();
}
break;
}
}
}
// If we get to this point and still have not found a
// game controller, then load default keyboard.
if ( (portNum == 0) && (devIdx < 0) )
{
loadDefaults();
}
return 0;
} }
//******************************************************************************** //********************************************************************************
int GamePad_t::setDeviceIndex( int in ) int GamePad_t::setDeviceIndex( int in )
{ {
if ( devIdx >= 0 )
{
jsDev[ devIdx ].unbindPort( portNum );
}
devIdx = in; devIdx = in;
if ( devIdx >= 0 )
{
jsDev[ devIdx ].bindPort( portNum );
}
return 0; return 0;
} }
//******************************************************************************** //********************************************************************************
@ -492,7 +595,7 @@ int GamePad_t::getDefaultMap( char *out, const char *guid )
{ {
bmap[x].ButtType = BUTTC_KEYBOARD; bmap[x].ButtType = BUTTC_KEYBOARD;
bmap[x].DeviceNum = 0; bmap[x].DeviceNum = 0;
bmap[x].ButtonNum = DefaultGamePad[0][x]; bmap[x].ButtonNum = DefaultGamePad[portNum][x];
} }
} }
} }
@ -505,6 +608,7 @@ int GamePad_t::loadDefaults(void)
if ( getDefaultMap( txtMap ) == 0 ) if ( getDefaultMap( txtMap ) == 0 )
{ {
//printf("Map:%s\n", txtMap );
setMapping( txtMap ); setMapping( txtMap );
} }

View File

@ -39,11 +39,15 @@ struct jsDev_t
bool isGameController(void); bool isGameController(void);
bool isConnected(void); bool isConnected(void);
void print(void); void print(void);
int bindPort( int idx );
int unbindPort( int idx );
int getBindPorts(void);
const char *getName(void); const char *getName(void);
const char *getGUID(void); const char *getGUID(void);
private: private:
int devIdx; int devIdx;
int portBindMask;
std::string guidStr; std::string guidStr;
std::string name; std::string name;
}; };
@ -52,19 +56,18 @@ class GamePad_t
{ {
public: public:
//int type;
int devIdx;
ButtConfig bmap[GAMEPAD_NUM_BUTTONS]; ButtConfig bmap[GAMEPAD_NUM_BUTTONS];
GamePad_t(void); GamePad_t(void);
~GamePad_t(void); ~GamePad_t(void);
int init( int port, const char *guid, const char *profile = NULL );
const char *getGUID(void); const char *getGUID(void);
int loadDefaults(void); int loadDefaults(void);
int loadProfile( const char *name, const char *guid = NULL ); int loadProfile( const char *name, const char *guid = NULL );
int getDeviceIndex(void){ return devIdx; }
int setDeviceIndex( int devIdx ); int setDeviceIndex( int devIdx );
int setMapping( const char *map ); int setMapping( const char *map );
int setMapping( nesGamePadMap_t *map ); int setMapping( nesGamePadMap_t *map );
@ -75,6 +78,11 @@ class GamePad_t
int saveMappingToFile( const char *filename, const char *txtMap ); int saveMappingToFile( const char *filename, const char *txtMap );
int saveCurrentMapToFile( const char *filename ); int saveCurrentMapToFile( const char *filename );
int deleteMapping( const char *name ); int deleteMapping( const char *name );
private:
int devIdx;
int portNum;
}; };
extern GamePad_t GamePad[4]; extern GamePad_t GamePad[4];