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
parent f0a3dc74bb
commit 92aff0b57c
9 changed files with 48 additions and 166 deletions

View file

@ -915,7 +915,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

@ -2559,7 +2559,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

@ -3839,7 +3839,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

@ -718,6 +718,9 @@ INT32 G_PlayerInputAnalog(UINT8 p, INT32 gc, boolean menu)
}
}
#if 1
(void)menu;
#else
if (p == 0 && menu == true)
{
// We don't want menus to become unnavigable if people unbind
@ -742,6 +745,7 @@ INT32 G_PlayerInputAnalog(UINT8 p, INT32 gc, boolean menu)
}
}
}
#endif
return 0;
}

View file

@ -1097,7 +1097,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, j;
for (i = 0; i < num_gamecontrols; i++)
@ -1158,7 +1158,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
/*&& (ev->data1 != gamecontrol[0][gc_talkkey][0]
&& ev->data1 != gamecontrol[0][gc_talkkey][1])*/)
return false;
@ -1224,7 +1224,7 @@ boolean HU_Responder(event_t *ev)
else if (c == KEY_ESCAPE
/*|| ((c == gamecontrol[0][gc_talkkey][0] || c == gamecontrol[0][gc_talkkey][1]
|| c == gamecontrol[0][gc_teamkey][0] || c == gamecontrol[0][gc_teamkey][1])
&& 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

@ -790,7 +790,7 @@ boolean M_Responder(event_t *ev)
return false;
}
if (ev->type == ev_keydown && ev->data1 < KEY_JOY1)
if (ev->type == ev_keydown && ev->data1 < NUMKEYS)
{
// Record keyboard presses
menuKey = ev->data1;

View file

@ -1760,7 +1760,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 /*|| ch == gamecontrol[0][gc_screenshot][0] || ch == gamecontrol[0][gc_screenshot][1]*/) // remappable F8

View file

@ -1073,14 +1073,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
*/
@ -1088,12 +1080,12 @@ void I_ShutdownJoystick(UINT8 index)
{
INT32 i;
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++)
{
@ -1122,136 +1114,6 @@ void I_ShutdownJoystick(UINT8 index)
// don't shut down the subsystem here, because hotplugging
}
void I_GetJoystickEvents(UINT8 index)
{
static event_t event = {0,0,0,0,0};
INT32 i = 0;
UINT64 joyhats = 0;
#if 0
UINT64 joybuttons = 0;
Sint16 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
for (i = JoyInfo[index].hats - 1; i >= 0; i--)
{
Uint8 hat = SDL_JoystickGetHat(JoyInfo[index].dev, i);
if (hat & SDL_HAT_UP ) joyhats|=(UINT64)0x1<<(0 + 4*i);
if (hat & SDL_HAT_DOWN ) joyhats|=(UINT64)0x1<<(1 + 4*i);
if (hat & SDL_HAT_LEFT ) joyhats|=(UINT64)0x1<<(2 + 4*i);
if (hat & SDL_HAT_RIGHT) joyhats|=(UINT64)0x1<<(3 + 4*i);
}
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

@ -798,29 +798,51 @@ static void Impl_HandleJoystickAxisEvent(SDL_JoyAxisEvent 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_HandleJoystickButtonEvent(SDL_JoyButtonEvent evt, Uint32 type)
{
@ -908,11 +930,9 @@ void I_GetEvent(void)
case SDL_JOYAXISMOTION:
Impl_HandleJoystickAxisEvent(evt.jaxis);
break;
#if 0
case SDL_JOYHATMOTION:
Impl_HandleJoystickHatEvent(evt.jhat)
Impl_HandleJoystickHatEvent(evt.jhat);
break;
#endif
case SDL_JOYBUTTONUP:
case SDL_JOYBUTTONDOWN:
Impl_HandleJoystickButtonEvent(evt.jbutton, evt.type);
@ -1113,16 +1133,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_JOYSTICK)
{
SDL_JoystickUpdate();
for (i = 0; i < MAXSPLITSCREENPLAYERS; i++)
I_GetJoystickEvents(i);
}
I_GetEvent();