Finish some unused SDL hat input support to use events rather than polling all of the time

This commit is contained in:
Sally Coolatta 2021-12-28 08:02:32 -05:00 committed by GenericHeroGuy
parent e669ca6edb
commit e1ffc206e7
8 changed files with 44 additions and 167 deletions

View file

@ -953,7 +953,7 @@ boolean CON_Responder(event_t *ev)
if (modeattacking || metalrecording || marathonmode)
return false;
if (ev->data1 >= KEY_JOY1) // See also: HUD_Responder
if (ev->data1 >= NUMKEYS) // See also: HUD_Responder
{
INT32 i;
for (i = 0; i < num_gamecontrols; i++)

View file

@ -2824,7 +2824,7 @@ static void Command_Map_f(void)
mustmodifygame = !(netgame || multiplayer) && !majormods;
if (mustmodifygame && !option_force)
if (mustmodifygame)
{
/* May want to be more descriptive? */
CONS_Printf(M_GetText("Sorry, level change disabled in single player.\n"));

View file

@ -4140,7 +4140,7 @@ boolean G_DemoTitleResponder(event_t *ev)
return true;
}
if (ch == KEY_ENTER || ch >= KEY_JOY1)
if (ch == KEY_ENTER || ch >= NUMKEYS)
{
demo.savemode = DSM_WILLSAVE;
return true;

View file

@ -1233,7 +1233,7 @@ boolean HU_Responder(event_t *ev)
// (Unless if you're sharing a keyboard, since you probably establish when you start chatting that you have dibs on it...)
// (Ahhh, the good ol days when I was a kid who couldn't afford an extra USB controller...)
if (ev->data1 >= KEY_JOY1)
if (ev->data1 >= NUMKEYS)
{
INT32 i;
for (i = 0; i < num_gamecontrols; i++)
@ -1283,7 +1283,7 @@ boolean HU_Responder(event_t *ev)
return true;
// Ignore non-keyboard keys, except when the talk key is bound
if (ev->data1 >= KEY_JOY1
if (ev->data1 >= NUMKEYS
&& !G_ControlBoundToKey(0, gc_talkkey, ev->data1, false))
return false;
@ -1326,7 +1326,7 @@ boolean HU_Responder(event_t *ev)
else if (c == KEY_ESCAPE
|| ((G_ControlBoundToKey(0, gc_talkkey, c, false)
|| G_ControlBoundToKey(0, gc_teamkey, c, false))
&& c >= KEY_JOY1)) // If it's not a keyboard key, then the chat button is used as a toggle.
&& c >= NUMKEYS)) // If it's not a keyboard key, then the chat button is used as a toggle.
{
chat_on = false;
c_input = 0; // reset input cursor

View file

@ -2671,7 +2671,7 @@ boolean M_Responder(event_t *ev)
}
// remap to keyboard keys if needed
if (deviceplayer == 0 && ch >= KEY_JOY1)
if (deviceplayer == 0 && ch >= NUMKEYS)
{
static INT32 joyremap[][2] = {
{ gc_accelerate, KEY_ENTER }, // these two first

View file

@ -1614,7 +1614,7 @@ boolean M_ScreenshotResponder(event_t *ev)
ch = ev->data1;
if (ch >= KEY_JOY1 && menuactive) // If it's not a keyboard key, then don't allow it in the menus!
if (ch >= NUMKEYS && menuactive) // If it's not a keyboard key, then don't allow it in the menus!
return false;
if (ch == KEY_F8 || G_ControlBoundToKey(0, gc_screenshot, ch, false)) // remappable F8

View file

@ -1164,14 +1164,6 @@ void I_UpdateJoystickDeviceIndices(UINT8 excludePlayer)
}
}
/** \brief Joystick buttons states
*/
static UINT64 lastjoybuttons[MAXSPLITSCREENPLAYERS] = {0,0,0,0};
/** \brief Joystick hats state
*/
static UINT64 lastjoyhats[MAXSPLITSCREENPLAYERS] = {0,0,0,0};
/** \brief Shuts down joystick
\return void
*/
@ -1179,12 +1171,12 @@ void I_ShutdownJoystick(UINT8 index)
{
INT32 i, j;
event_t event;
event.type=ev_keyup;
event.device = I_GetJoystickDeviceIndex(JoyInfo[index].dev);
event.type = ev_keyup;
event.data2 = 0;
event.data3 = 0;
lastjoybuttons[index] = lastjoyhats[index] = 0;
// emulate the up of all joystick buttons
for (i = 0; i < JOYBUTTONS; i++)
{
@ -1216,136 +1208,6 @@ void I_ShutdownJoystick(UINT8 index)
// don't shut down the subsystem here, because hotplugging
}
void I_GetJoystickEvents(UINT8 index)
{
static event_t event = {};
INT32 i = 0;
UINT64 joyhats = 0;
#if 0
UINT64 joybuttons = 0;
UINT32 axisx, axisy;
#endif
if (!joystick_started[index])
return;
if (!JoyInfo[index].dev) //I_ShutdownJoystick(index);
return;
#if 0
//faB: look for as much buttons as g_input code supports,
// we don't use the others
for (i = JoyInfo[index].buttons - 1; i >= 0; i--)
{
joybuttons <<= 1;
if (SDL_JoystickGetButton(JoyInfo[index].dev,i))
joybuttons |= 1;
}
if (joybuttons != lastjoybuttons[index])
{
INT64 j = 1; // keep only bits that changed since last time
INT64 newbuttons = joybuttons ^ lastjoybuttons[index];
lastjoybuttons[index] = joybuttons;
for (i = 0; i < JOYBUTTONS; i++, j <<= 1)
{
if (newbuttons & j) // button changed state?
{
if (joybuttons & j)
event.type = ev_keydown;
else
event.type = ev_keyup;
event.data1 = KEY_JOY1 + i;
D_PostEvent(&event);
}
}
}
#endif
joyhats |= SDL_GameControllerGetButton(JoyInfo[index].dev, SDL_CONTROLLER_BUTTON_DPAD_UP);
joyhats |= SDL_GameControllerGetButton(JoyInfo[index].dev, SDL_CONTROLLER_BUTTON_DPAD_DOWN) << 1;
joyhats |= SDL_GameControllerGetButton(JoyInfo[index].dev, SDL_CONTROLLER_BUTTON_DPAD_LEFT) << 2;
joyhats |= SDL_GameControllerGetButton(JoyInfo[index].dev, SDL_CONTROLLER_BUTTON_DPAD_RIGHT) << 3;
if (joyhats != lastjoyhats[index])
{
INT64 j = 1; // keep only bits that changed since last time
INT64 newhats = joyhats ^ lastjoyhats[index];
lastjoyhats[index] = joyhats;
for (i = 0; i < JOYHATS*4; i++, j <<= 1)
{
if (newhats & j) // hat changed state?
{
if (joyhats & j)
event.type = ev_keydown;
else
event.type = ev_keyup;
event.data1 = KEY_HAT1 + i;
D_PostEvent(&event);
}
}
}
#if 0
// send joystick axis positions
event.type = ev_joystick;
for (i = JOYAXISSET - 1; i >= 0; i--)
{
event.data1 = i;
if (i*2 + 1 <= JoyInfo[index].axises)
axisx = SDL_JoystickGetAxis(JoyInfo[index].dev, i*2 + 0);
else axisx = 0;
if (i*2 + 2 <= JoyInfo[index].axises)
axisy = SDL_JoystickGetAxis(JoyInfo[index].dev, i*2 + 1);
else axisy = 0;
// -32768 to 32767
axisx = axisx/32;
axisy = axisy/32;
if (Joystick[index].bGamepadStyle)
{
// gamepad control type, on or off, live or die
if (axisx < -(JOYAXISRANGE/2))
event.data2 = -1;
else if (axisx > (JOYAXISRANGE/2))
event.data2 = 1;
else
event.data2 = 0;
if (axisy < -(JOYAXISRANGE/2))
event.data3 = -1;
else if (axisy > (JOYAXISRANGE/2))
event.data3 = 1;
else
event.data3 = 0;
}
else
{
axisx = JoyInfo[index].scale?((axisx/JoyInfo[index].scale)*JoyInfo[index].scale):axisx;
axisy = JoyInfo[index].scale?((axisy/JoyInfo[index].scale)*JoyInfo[index].scale):axisy;
#ifdef SDL_JDEADZONE
if (-SDL_JDEADZONE <= axisx && axisx <= SDL_JDEADZONE) axisx = 0;
if (-SDL_JDEADZONE <= axisy && axisy <= SDL_JDEADZONE) axisy = 0;
#endif
// analog control style , just send the raw data
event.data2 = axisx; // x axis
event.data3 = axisy; // y axis
}
D_PostEvent(&event);
}
#endif
}
/** \brief Open joystick handle
\param fname name of joystick

View file

@ -838,29 +838,51 @@ static void Impl_HandleControllerAxisEvent(SDL_ControllerAxisEvent evt)
D_PostEvent(&event);
}
#if 0
static void Impl_HandleJoystickHatEvent(SDL_JoyHatEvent evt)
static void Impl_SendHatEvent(SDL_JoyHatEvent evt, UINT64 hatFlag, UINT8 keyOffset)
{
event_t event;
SDL_JoystickID joyid[MAXSPLITSCREENPLAYERS];
event.device = 1 + evt.which;
if (event.device == INT32_MAX)
{
return;
}
if (evt.hat >= JOYHATS)
event.data1 = KEY_HAT1 + keyOffset;
if (evt.hat < JOYHATS)
{
return; // ignore hats with too high an index
event.data1 += (evt.hat * 4);
}
else
{
return;
}
event.data1 = KEY_HAT1 + (evt.hat*4);
if (evt.value & hatFlag)
{
event.type = ev_keydown;
}
else
{
event.type = ev_keyup;
}
// NOTE: UNFINISHED
SDLJoyRemap(&event);
if (event.type != ev_console)
{
D_PostEvent(&event);
}
}
static void Impl_HandleJoystickHatEvent(SDL_JoyHatEvent evt)
{
Impl_SendHatEvent(evt, SDL_HAT_UP, 0);
Impl_SendHatEvent(evt, SDL_HAT_DOWN, 1);
Impl_SendHatEvent(evt, SDL_HAT_LEFT, 2);
Impl_SendHatEvent(evt, SDL_HAT_RIGHT, 3);
}
#endif
static void Impl_HandleControllerButtonEvent(SDL_ControllerButtonEvent evt, Uint32 type)
{
@ -958,11 +980,9 @@ void I_GetEvent(void)
case SDL_CONTROLLERAXISMOTION:
Impl_HandleControllerAxisEvent(evt.caxis);
break;
#if 0
case SDL_JOYHATMOTION:
Impl_HandleJoystickHatEvent(evt.jhat)
Impl_HandleJoystickHatEvent(evt.jhat);
break;
#endif
case SDL_CONTROLLERBUTTONUP:
case SDL_CONTROLLERBUTTONDOWN:
Impl_HandleControllerButtonEvent(evt.cbutton, evt.type);
@ -1166,17 +1186,12 @@ void I_StartupMouse(void)
void I_OsPolling(void)
{
SDL_Keymod mod;
UINT8 i;
if (consolevent)
I_GetConsoleEvents();
if (SDL_WasInit(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) == (SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER))
{
SDL_GameControllerUpdate();
for (i = 0; i < MAXSPLITSCREENPLAYERS; i++)
I_GetJoystickEvents(i);
}
I_GetEvent();