Merge pull request #2047 from tsoding/joystick-support

Add Joystick Support
This commit is contained in:
oy 2019-03-07 19:11:25 +01:00 committed by GitHub
commit b7823b2886
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 153 additions and 25 deletions

View file

@ -37,10 +37,14 @@ CInput::CInput()
mem_zero(m_aInputCount, sizeof(m_aInputCount));
mem_zero(m_aInputState, sizeof(m_aInputState));
m_pJoystick = 0;
m_InputCounter = 1;
m_InputGrabbed = 0;
m_pClipboardText = 0;
m_PreviousHat = 0;
m_MouseDoubleClick = false;
m_NumEvents = 0;
@ -61,6 +65,35 @@ void CInput::Init()
// FIXME: unicode handling: use SDL_StartTextInput/SDL_StopTextInput on inputs
MouseModeRelative();
if(!SDL_WasInit(SDL_INIT_JOYSTICK))
{
if(SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0)
{
dbg_msg("joystick", "unable to init SDL joystick: %s", SDL_GetError());
return;
}
}
if(SDL_NumJoysticks() > 0)
{
m_pJoystick = SDL_JoystickOpen(0);
if(!m_pJoystick) {
dbg_msg("joystick", "Could not open 0th joystick: %s", SDL_GetError());
return;
}
dbg_msg("joystick", "Opened Joystick 0");
dbg_msg("joystick", "Name: %s", SDL_JoystickNameForIndex(0));
dbg_msg("joystick", "Number of Axes: %d", SDL_JoystickNumAxes(m_pJoystick));
dbg_msg("joystick", "Number of Buttons: %d", SDL_JoystickNumButtons(m_pJoystick));
dbg_msg("joystick", "Number of Balls: %d", SDL_JoystickNumBalls(m_pJoystick));
}
else
{
dbg_msg("joystick", "No joysticks found");
}
}
void CInput::MouseRelative(float *x, float *y)
@ -70,11 +103,20 @@ void CInput::MouseRelative(float *x, float *y)
int nx = 0, ny = 0;
float Sens = g_Config.m_InpMousesens/100.0f;
SDL_GetRelativeMouseState(&nx,&ny);
*x = nx*Sens;
*y = ny*Sens;
float jx = 0.0f;
float jy = 0.0f;
if(m_pJoystick)
{
jx = static_cast<float>(SDL_JoystickGetAxis(m_pJoystick, g_Config.m_JoystickX)) / 32768.0f * 50.0f;
jy = static_cast<float>(SDL_JoystickGetAxis(m_pJoystick, g_Config.m_JoystickY)) / 32768.0f * 50.0f;
}
*x = (nx + jx)*Sens;
*y = (ny + jy)*Sens;
}
void CInput::MouseModeAbsolute()
@ -191,6 +233,67 @@ int CInput::Update()
Scancode = Event.key.keysym.scancode;
break;
// handle the joystick events
case SDL_JOYBUTTONUP:
Action = IInput::FLAG_RELEASE;
// fall through
case SDL_JOYBUTTONDOWN:
Key = Event.jbutton.button + KEY_JOYSTICK_BUTTON_0;
Scancode = Key;
break;
case SDL_JOYHATMOTION:
switch (Event.jhat.value) {
case SDL_HAT_LEFTUP:
Key = KEY_JOY_HAT_LEFTUP;
Scancode = Key;
m_PreviousHat = Key;
break;
case SDL_HAT_UP:
Key = KEY_JOY_HAT_UP;
Scancode = Key;
m_PreviousHat = Key;
break;
case SDL_HAT_RIGHTUP:
Key = KEY_JOY_HAT_RIGHTUP;
Scancode = Key;
m_PreviousHat = Key;
break;
case SDL_HAT_LEFT:
Key = KEY_JOY_HAT_LEFT;
Scancode = Key;
m_PreviousHat = Key;
break;
case SDL_HAT_CENTERED:
Action = IInput::FLAG_RELEASE;
Key = m_PreviousHat;
Scancode = m_PreviousHat;
m_PreviousHat = 0;
break;
case SDL_HAT_RIGHT:
Key = KEY_JOY_HAT_RIGHT;
Scancode = Key;
m_PreviousHat = Key;
break;
case SDL_HAT_LEFTDOWN:
Key = KEY_JOY_HAT_LEFTDOWN;
Scancode = Key;
m_PreviousHat = Key;
break;
case SDL_HAT_DOWN:
Key = KEY_JOY_HAT_DOWN;
Scancode = Key;
m_PreviousHat = Key;
break;
case SDL_HAT_RIGHTDOWN:
Key = KEY_JOY_HAT_RIGHTDOWN;
Scancode = Key;
m_PreviousHat = Key;
break;
}
break;
// handle mouse buttons
case SDL_MOUSEBUTTONUP:
Action = IInput::FLAG_RELEASE;
@ -231,7 +334,7 @@ int CInput::Update()
}
break;
#endif
// other messages
case SDL_QUIT:
return 1;

View file

@ -7,10 +7,13 @@ class CInput : public IEngineInput
{
IEngineGraphics *m_pGraphics;
IConsole *m_pConsole;
SDL_Joystick *m_pJoystick;
int m_InputGrabbed;
char *m_pClipboardText;
int m_PreviousHat;
bool m_MouseDoubleClick;
void AddEvent(char *pText, int Key, int Flags);

View file

@ -428,26 +428,26 @@ const char g_aaKeyStrings[512][20] =
"mouse9",
"mousewheelup",
"mousewheeldown",
"&422",
"&423",
"&424",
"&425",
"&426",
"&427",
"&428",
"&429",
"&430",
"&431",
"&432",
"&433",
"&434",
"&435",
"&436",
"&437",
"&438",
"&439",
"&440",
"&441",
"joystick0",
"joystick1",
"joystick2",
"joystick3",
"joystick4",
"joystick5",
"joystick6",
"joystick7",
"joystick8",
"joystick9",
"joystick10",
"joystick11",
"joy_hat_leftup",
"joy_hat_up",
"joy_hat_rightup",
"joy_hat_left",
"joy_hat_right",
"joy_hat_leftdown",
"joy_hat_down",
"joy_hat_rightdown",
"&442",
"&443",
"&444",
@ -519,4 +519,3 @@ const char g_aaKeyStrings[512][20] =
"&510",
"&511",
};

View file

@ -254,6 +254,26 @@ enum
KEY_MOUSE_9 = 419,
KEY_MOUSE_WHEEL_UP = 420,
KEY_MOUSE_WHEEL_DOWN = 421,
KEY_JOYSTICK_BUTTON_0 = 422,
KEY_JOYSTICK_BUTTON_1 = 423,
KEY_JOYSTICK_BUTTON_2 = 424,
KEY_JOYSTICK_BUTTON_3 = 425,
KEY_JOYSTICK_BUTTON_4 = 426,
KEY_JOYSTICK_BUTTON_5 = 427,
KEY_JOYSTICK_BUTTON_6 = 428,
KEY_JOYSTICK_BUTTON_7 = 429,
KEY_JOYSTICK_BUTTON_8 = 430,
KEY_JOYSTICK_BUTTON_9 = 431,
KEY_JOYSTICK_BUTTON_10 = 432,
KEY_JOYSTICK_BUTTON_11 = 433,
KEY_JOY_HAT_LEFTUP = 434,
KEY_JOY_HAT_UP = 435,
KEY_JOY_HAT_RIGHTUP = 436,
KEY_JOY_HAT_LEFT = 437,
KEY_JOY_HAT_RIGHT = 438,
KEY_JOY_HAT_LEFTDOWN = 439,
KEY_JOY_HAT_DOWN = 440,
KEY_JOY_HAT_RIGHTDOWN = 441,
KEY_LAST,
};

View file

@ -64,6 +64,9 @@ MACRO_CONFIG_INT(GfxUseX11XRandRWM, gfx_use_x11xrandr_wm, 1, 0, 1, CFGFLAG_SAVE|
MACRO_CONFIG_INT(InpGrab, inp_grab, 0, 0, 1, CFGFLAG_SAVE|CFGFLAG_CLIENT, "Disable OS mouse settings such as mouse acceleration, use raw mouse input mode")
MACRO_CONFIG_INT(InpMousesens, inp_mousesens, 100, 5, 100000, CFGFLAG_SAVE|CFGFLAG_CLIENT, "Ingame mouse sensitivity")
MACRO_CONFIG_INT(JoystickX, joystick_x, 0, 0, 6, CFGFLAG_SAVE|CFGFLAG_CLIENT, "Joystick Axis that controls X axis of mouse")
MACRO_CONFIG_INT(JoystickY, joystick_y, 1, 0, 6, CFGFLAG_SAVE|CFGFLAG_CLIENT, "Joystick Axis that controls Y axis of mouse")
MACRO_CONFIG_STR(SvName, sv_name, 128, "unnamed server", CFGFLAG_SAVE|CFGFLAG_SERVER, "Server name")
MACRO_CONFIG_STR(SvHostname, sv_hostname, 128, "", CFGFLAG_SAVE|CFGFLAG_SERVER, "Server hostname")
MACRO_CONFIG_STR(Bindaddr, bindaddr, 128, "", CFGFLAG_SAVE|CFGFLAG_CLIENT|CFGFLAG_SERVER|CFGFLAG_MASTER, "Address to bind the client/server to")