Support multiple joystick + joystick selection in each port

This commit is contained in:
Chips 2016-07-26 16:12:13 +02:00
parent 50cdbeb68b
commit 20e90db51d
3 changed files with 95 additions and 40 deletions

View file

@ -215,6 +215,9 @@ extern void inputdevice_tablet_strobe (void);
extern int input_getqualifiers (void);
extern int inputdevice_get_device_total (int type);
extern TCHAR *inputdevice_get_device_name (int type, int devnum);
#define JSEM_MODE_DEFAULT 0
#define JSEM_MODE_MOUSE 1
#define JSEM_MODE_JOYSTICK 2

View file

@ -82,6 +82,12 @@ class StringListModel : public gcn::ListModel
return values.size();
}
int AddElement(const char * Elem)
{
values.push_back(Elem);
return 0;
}
std::string getElementAt(int i)
{
if(i < 0 || i >= values.size())
@ -167,6 +173,9 @@ class InputActionListener : public gcn::ActionListener
case 2: changed_prefs.jports[0].id = JSEM_JOYS; changed_prefs.jports[0].mode = JSEM_MODE_JOYSTICK; break;
case 3: changed_prefs.jports[0].id = JSEM_JOYS; changed_prefs.jports[0].mode = JSEM_MODE_JOYSTICK_CD32; break;
case 4: changed_prefs.jports[0].id = -1; changed_prefs.jports[0].mode = JSEM_MODE_DEFAULT; break;
default:changed_prefs.jports[0].id = JSEM_JOYS + cboPort0->getSelected() - 4;
changed_prefs.jports[0].mode = JSEM_MODE_JOYSTICK;
break;
}
inputdevice_updateconfig(&changed_prefs);
}
@ -179,6 +188,9 @@ class InputActionListener : public gcn::ActionListener
case 2: changed_prefs.jports[1].id = JSEM_JOYS; changed_prefs.jports[1].mode = JSEM_MODE_JOYSTICK; break;
case 3: changed_prefs.jports[1].id = JSEM_JOYS; changed_prefs.jports[1].mode = JSEM_MODE_JOYSTICK_CD32; break;
case 4: changed_prefs.jports[1].id = -1; changed_prefs.jports[1].mode = JSEM_MODE_DEFAULT; break;
default:changed_prefs.jports[1].id = JSEM_JOYS + cboPort1->getSelected() - 4;
changed_prefs.jports[1].mode = JSEM_MODE_JOYSTICK;
break;
}
inputdevice_updateconfig(&changed_prefs);
}
@ -261,6 +273,16 @@ void InitPanelInput(const struct _ConfigCategory& category)
{
inputActionListener = new InputActionListener();
if (ctrlPortList.getNumberOfElements() < (4 + inputdevice_get_device_total (IDTYPE_JOYSTICK)))
{
int i;
for(i=0; i<(inputdevice_get_device_total (IDTYPE_JOYSTICK) - 1); i++)
{
ctrlPortList.AddElement(inputdevice_get_device_name(IDTYPE_JOYSTICK,i + 1));
}
}
lblPort0 = new gcn::Label("Port0:");
lblPort0->setSize(100, LABEL_HEIGHT);
lblPort0->setAlignment(gcn::Graphics::RIGHT);
@ -536,9 +558,12 @@ void RefreshPanelInput(void)
else
cboPort0->setSelected(3);
break;
default:
case -1:
cboPort0->setSelected(4);
break;
default:
cboPort0->setSelected(changed_prefs.jports[0].id-JSEM_JOYS + 4);
break;
}
// Set current device in port 1
@ -555,9 +580,12 @@ void RefreshPanelInput(void)
else
cboPort1->setSelected(3);
break;
default:
case -1:
cboPort1->setSelected(4);
break;
default:
cboPort1->setSelected(changed_prefs.jports[1].id-JSEM_JOYS + 4);
break;
}
if (changed_prefs.input_autofire_linecnt == 0)

View file

@ -255,12 +255,11 @@ int input_get_default_keyboard (int num)
#define FIRST_JOY_AXIS 0
#define FIRST_JOY_BUTTON MAX_JOY_AXES
#define MAX_JOYSTICK 4
static int nr_joysticks = 0;
static char JoystickName[MAX_JOYSTICK][40];
static SDL_Joystick* Joysticktable[MAX_JOYSTICK];
static char JoystickName[MAX_INPUT_DEVICES][40];
static SDL_Joystick* Joysticktable[MAX_INPUT_DEVICES];
static int get_joystick_num (void)
@ -271,13 +270,13 @@ static int get_joystick_num (void)
static int init_joystick (void)
{
//This function is called many times... let's only do treatment one time...
if (nr_joysticks != 0)
//This function is called too many times... we can filter if number of joy is good...
if (nr_joysticks == SDL_NumJoysticks ())
return 1;
nr_joysticks = SDL_NumJoysticks ();
if (nr_joysticks > 4)
nr_joysticks = 4;
if (nr_joysticks > MAX_INPUT_DEVICES)
nr_joysticks = MAX_INPUT_DEVICES;
for (int cpt; cpt < nr_joysticks; cpt++)
{
Joysticktable[cpt] = SDL_JoystickOpen (cpt);
@ -317,9 +316,25 @@ static TCHAR *get_joystick_friendlyname (int joy)
static TCHAR *get_joystick_uniquename (int joy)
{
if (joy == 0)
return "JOY0";
if (joy == 1)
return "JOY1";
if (joy == 2)
return "JOY2";
if (joy == 3)
return "JOY3";
if (joy == 4)
return "JOY4";
if (joy == 5)
return "JOY5";
if (joy == 6)
return "JOY6";
return "JOY7";
}
static int get_joystick_widget_num (int joy)
{
return MAX_JOY_AXES + MAX_JOY_BUTTONS;
@ -390,10 +405,15 @@ static int get_joystick_flags (int num)
static void read_joystick (void)
{
if(currprefs.jports[0].id == JSEM_JOYS || currprefs.jports[1].id == JSEM_JOYS) {
for (int joyid = 0; joyid < MAX_JPORTS ; joyid ++)
// First handle fake joystick from pandora...
if(currprefs.jports[joyid].id == JSEM_JOYS)
{
Uint8 *keystate = SDL_GetKeyState(NULL);
if(!keystate[SDLK_RCTRL]) { // Right shoulder + dPad -> cursor keys
if(!keystate[SDLK_RCTRL])
{ // Right shoulder + dPad -> cursor keys
int axis = (keystate[SDLK_LEFT] ? -32767 : (keystate[SDLK_RIGHT] ? 32767 : 0));
if(!joyXviaCustom)
setjoystickstate (0, 0, axis, 32767);
@ -426,35 +446,39 @@ static void read_joystick (void)
if(!joyButXviaCustom[4])
setjoybuttonstate (0, 4, cd32_rwd);
}
// Raspberry: Temporary read SDL Joystick here...
for (int cpt = 0; cpt < nr_joysticks; cpt++)
else if (jsem_isjoy(joyid,&currprefs) != -1)
{
int hat = SDL_JoystickGetHat(Joysticktable[cpt],0);
int val = SDL_JoystickGetAxis(Joysticktable[cpt], 0);
// Now we handle real SDL joystick...
int hostjoyid = currprefs.jports[joyid].id - JSEM_JOYS -1;
int hat = SDL_JoystickGetHat(Joysticktable[hostjoyid],0);
int val = SDL_JoystickGetAxis(Joysticktable[hostjoyid], 0);
if (hat & SDL_HAT_RIGHT)
setjoystickstate (cpt, 0, 32767, 32767);
setjoystickstate (hostjoyid + 1, 0, 32767, 32767);
else
if (hat & SDL_HAT_LEFT)
setjoystickstate (cpt, 0, -32767, 32767);
setjoystickstate (hostjoyid + 1, 0, -32767, 32767);
else
setjoystickstate (cpt, 0, val, 32767);
val = SDL_JoystickGetAxis(Joysticktable[cpt], 1);
setjoystickstate (hostjoyid + 1, 0, val, 32767);
val = SDL_JoystickGetAxis(Joysticktable[hostjoyid], 1);
if (hat & SDL_HAT_UP)
setjoystickstate (cpt, 1, -32767, 32767);
setjoystickstate (hostjoyid + 1, 1, -32767, 32767);
else
if (hat & SDL_HAT_DOWN)
setjoystickstate (cpt, 1, 32767, 32767);
setjoystickstate (hostjoyid + 1, 1, 32767, 32767);
else
setjoystickstate (cpt, 1, val, 32767);
setjoystickstate (hostjoyid + 1, 1, val, 32767);
if (SDL_JoystickGetButton(Joysticktable[cpt], 0) & 1)
setjoybuttonstate (cpt, 0, 1 );
if (SDL_JoystickGetButton(Joysticktable[cpt], 1) & 1)
setjoybuttonstate (cpt, 1, 1 );
setjoybuttonstate (hostjoyid + 1, 0, (SDL_JoystickGetButton(Joysticktable[hostjoyid], 0) & 1) );
setjoybuttonstate (hostjoyid + 1, 1, (SDL_JoystickGetButton(Joysticktable[hostjoyid], 1) & 1) );
setjoybuttonstate (hostjoyid + 1, 2, (SDL_JoystickGetButton(Joysticktable[hostjoyid], 2) & 1) );
setjoybuttonstate (hostjoyid + 1, 3, (SDL_JoystickGetButton(Joysticktable[hostjoyid], 3) & 1) );
// cd32 start, ffw, rwd
setjoybuttonstate (hostjoyid + 1, 4, (SDL_JoystickGetButton(Joysticktable[hostjoyid], 4) & 1) );
setjoybuttonstate (hostjoyid + 1, 5, (SDL_JoystickGetButton(Joysticktable[hostjoyid], 5) & 1) );
setjoybuttonstate (hostjoyid + 1, 6, (SDL_JoystickGetButton(Joysticktable[hostjoyid], 6) & 1) );
}
}
struct inputdevice_functions inputdevicefunc_joystick = {