hid: stop vibrating controllers on focus lost

This commit is contained in:
Hyper 2024-12-11 22:32:39 +00:00
parent 38c275632b
commit 4f26d0b263

View file

@ -167,6 +167,33 @@ static void SetControllerInputDevice(Controller* controller)
int HID_OnSDLEvent(void*, SDL_Event* event) int HID_OnSDLEvent(void*, SDL_Event* event)
{ {
switch (event->type)
{
case SDL_KEYDOWN:
case SDL_KEYUP:
hid::detail::g_inputDevice = hid::detail::EInputDevice::Keyboard;
break;
case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
hid::detail::g_inputDevice = hid::detail::EInputDevice::Mouse;
break;
case SDL_WINDOWEVENT:
{
if (event->window.event == SDL_WINDOWEVENT_FOCUS_LOST)
{
// Stop vibrating controllers on focus lost.
for (auto& controller : g_controllers)
controller.SetVibration({ 0, 0 });
}
break;
}
default:
{
if (event->type >= SDL_CONTROLLERAXISMOTION && event->type < SDL_FINGERDOWN) if (event->type >= SDL_CONTROLLERAXISMOTION && event->type < SDL_FINGERDOWN)
{ {
if (event->type == SDL_CONTROLLERDEVICEADDED) if (event->type == SDL_CONTROLLERDEVICEADDED)
@ -205,13 +232,9 @@ int HID_OnSDLEvent(void*, SDL_Event* event)
} }
} }
} }
else if (event->type == SDL_KEYDOWN || event->type == SDL_KEYUP)
{ break;
hid::detail::g_inputDevice = hid::detail::EInputDevice::Keyboard;
} }
else if (event->type == SDL_MOUSEMOTION || event->type == SDL_MOUSEBUTTONDOWN || event->type == SDL_MOUSEBUTTONUP)
{
hid::detail::g_inputDevice = hid::detail::EInputDevice::Mouse;
} }
return 0; return 0;
@ -228,7 +251,6 @@ void hid::detail::Init()
SDL_SetHint(SDL_HINT_XINPUT_ENABLED, "1"); SDL_SetHint(SDL_HINT_XINPUT_ENABLED, "1");
SDL_InitSubSystem(SDL_INIT_EVENTS); SDL_InitSubSystem(SDL_INIT_EVENTS);
SDL_AddEventWatch(HID_OnSDLEvent, nullptr); SDL_AddEventWatch(HID_OnSDLEvent, nullptr);
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER); SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);