begin work on multiple joystick support

This commit is contained in:
Arisotura
2019-06-11 18:55:04 +02:00
parent 543a40c7cb
commit b9183b8818
4 changed files with 115 additions and 52 deletions

View File

@ -29,8 +29,11 @@
#include "DlgInputConfig.h"
extern int JoystickID;
extern SDL_Joystick* Joystick;
extern void OpenJoystick();
namespace DlgInputConfig
{
@ -303,6 +306,13 @@ void OnJoyStartConfig(uiButton* btn, void* data)
}
void OnJoystickChanged(uiCombobox* cb, void* data)
{
JoystickID = uiComboboxSelected(cb);
OpenJoystick();
}
int OnCloseWindow(uiWindow* window, void* blarg)
{
InputDlgData* dlg = (InputDlgData*)(uiControl(window)->UserData);
@ -458,6 +468,36 @@ void Open(int type)
uiButtonOnClicked(btn, OnJoyStartConfig, (type==0) ? &dskeyorder[i] : &identity[i]);
uiControlSetMinSize(uiControl(btn), width, 1);
}
if (type == 0)
{
uiLabel* dummy = uiNewLabel(" ");
uiGridAppend(b_key, uiControl(dummy), 0, dlg->numkeys, 2, 1, 1, uiAlignFill, 1, uiAlignCenter);
uiCombobox* joycombo = uiNewCombobox();
uiGridAppend(b_joy, uiControl(joycombo), 0, dlg->numkeys, 2, 1, 1, uiAlignFill, 1, uiAlignCenter);
int numjoys = SDL_NumJoysticks();
if (numjoys < 1)
{
uiComboboxAppend(joycombo, "(no joysticks available)");
uiControlDisable(uiControl(joycombo));
}
else
{
for (int i = 0; i < numjoys; i++)
{
const char* joyname = SDL_JoystickNameForIndex(i);
char fullname[256];
snprintf(fullname, 256, "%d. %s", i, joyname);
uiComboboxAppend(joycombo, fullname);
}
uiComboboxSetSelected(joycombo, JoystickID);
uiComboboxOnSelected(joycombo, OnJoystickChanged, NULL);
}
}
}
uiLabel* filler = uiNewLabel("");

View File

@ -30,6 +30,8 @@ int JoyMapping[12];
int HKKeyMapping[HK_MAX];
int HKJoyMapping[HK_MAX];
int JoystickID;
int WindowWidth;
int WindowHeight;
int WindowMaximized;
@ -98,6 +100,8 @@ ConfigEntry PlatformConfigFile[] =
{"HKJoy_FastForward", 0, &HKJoyMapping[HK_FastForward], -1, NULL, 0},
{"HKJoy_FastForwardToggle", 0, &HKJoyMapping[HK_FastForwardToggle], -1, NULL, 0},
{"JoystickID", 0, &JoystickID, 0, NULL, 0},
{"WindowWidth", 0, &WindowWidth, 256, NULL, 0},
{"WindowHeight", 0, &WindowHeight, 384, NULL, 0},
{"WindowMax", 0, &WindowMaximized, 0, NULL, 0},

View File

@ -39,6 +39,8 @@ extern int JoyMapping[12];
extern int HKKeyMapping[HK_MAX];
extern int HKJoyMapping[HK_MAX];
extern int JoystickID;
extern int WindowWidth;
extern int WindowHeight;
extern int WindowMaximized;

View File

@ -146,6 +146,8 @@ bool Touching = false;
u32 KeyInputMask;
u32 HotkeyMask;
bool LidStatus;
int JoystickID;
SDL_Joystick* Joystick;
SDL_AudioDeviceID AudioDevice, MicDevice;
@ -547,12 +549,6 @@ void MicLoadWav(char* name)
SDL_FreeWAV(buf);
}
void UpdateWindowTitle(void* data)
{
uiWindowSetTitle(MainWindow, (const char*)data);
}
void AudioCallback(void* data, Uint8* stream, int len)
{
// resampling:
@ -619,46 +615,6 @@ void MicCallback(void* data, Uint8* stream, int len)
}
}
bool JoyButtonPressed(int btnid, int njoybuttons, Uint8* joybuttons, Uint32 hat)
{
if (btnid < 0) return false;
hat &= ~(hat >> 4);
bool pressed = false;
if (btnid == 0x101) // up
pressed = (hat & SDL_HAT_UP);
else if (btnid == 0x104) // down
pressed = (hat & SDL_HAT_DOWN);
else if (btnid == 0x102) // right
pressed = (hat & SDL_HAT_RIGHT);
else if (btnid == 0x108) // left
pressed = (hat & SDL_HAT_LEFT);
else if (btnid < njoybuttons)
pressed = (joybuttons[btnid] & ~(joybuttons[btnid] >> 1)) & 0x01;
return pressed;
}
bool JoyButtonHeld(int btnid, int njoybuttons, Uint8* joybuttons, Uint32 hat)
{
if (btnid < 0) return false;
bool pressed = false;
if (btnid == 0x101) // up
pressed = (hat & SDL_HAT_UP);
else if (btnid == 0x104) // down
pressed = (hat & SDL_HAT_DOWN);
else if (btnid == 0x102) // right
pressed = (hat & SDL_HAT_RIGHT);
else if (btnid == 0x108) // left
pressed = (hat & SDL_HAT_LEFT);
else if (btnid < njoybuttons)
pressed = joybuttons[btnid] & 0x01;
return pressed;
}
void FeedMicInput()
{
int type = Config::MicInputType;
@ -722,6 +678,68 @@ void FeedMicInput()
}
}
void OpenJoystick()
{
if (Joystick) SDL_JoystickClose(Joystick);
int num = SDL_NumJoysticks();
if (num < 1)
{
Joystick = NULL;
return;
}
if (JoystickID >= num)
JoystickID = 0;
Joystick = SDL_JoystickOpen(JoystickID);
}
bool JoyButtonPressed(int btnid, int njoybuttons, Uint8* joybuttons, Uint32 hat)
{
if (btnid < 0) return false;
hat &= ~(hat >> 4);
bool pressed = false;
if (btnid == 0x101) // up
pressed = (hat & SDL_HAT_UP);
else if (btnid == 0x104) // down
pressed = (hat & SDL_HAT_DOWN);
else if (btnid == 0x102) // right
pressed = (hat & SDL_HAT_RIGHT);
else if (btnid == 0x108) // left
pressed = (hat & SDL_HAT_LEFT);
else if (btnid < njoybuttons)
pressed = (joybuttons[btnid] & ~(joybuttons[btnid] >> 1)) & 0x01;
return pressed;
}
bool JoyButtonHeld(int btnid, int njoybuttons, Uint8* joybuttons, Uint32 hat)
{
if (btnid < 0) return false;
bool pressed = false;
if (btnid == 0x101) // up
pressed = (hat & SDL_HAT_UP);
else if (btnid == 0x104) // down
pressed = (hat & SDL_HAT_DOWN);
else if (btnid == 0x102) // right
pressed = (hat & SDL_HAT_RIGHT);
else if (btnid == 0x108) // left
pressed = (hat & SDL_HAT_LEFT);
else if (btnid < njoybuttons)
pressed = joybuttons[btnid] & 0x01;
return pressed;
}
void UpdateWindowTitle(void* data)
{
uiWindowSetTitle(MainWindow, (const char*)data);
}
int EmuThreadFunc(void* burp)
{
NDS::Init();
@ -786,7 +804,8 @@ int EmuThreadFunc(void* burp)
}
if (!Joystick && (SDL_NumJoysticks() > 0))
{
Joystick = SDL_JoystickOpen(0);
JoystickID = Config::JoystickID;
OpenJoystick();
if (Joystick)
{
njoybuttons = SDL_JoystickNumButtons(Joystick);
@ -2627,11 +2646,9 @@ int main(int argc, char** argv)
MicWavBuffer = NULL;
if (Config::MicInputType == 3) MicLoadWav(Config::MicWavPath);
// TODO: support more joysticks
if (SDL_NumJoysticks() > 0)
Joystick = SDL_JoystickOpen(0);
else
JoystickID = Config::JoystickID;
Joystick = NULL;
OpenJoystick();
EmuRunning = 2;
RunningSomething = false;