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,52 +167,75 @@ static void SetControllerInputDevice(Controller* controller)
int HID_OnSDLEvent(void*, SDL_Event* event) int HID_OnSDLEvent(void*, SDL_Event* event)
{ {
if (event->type >= SDL_CONTROLLERAXISMOTION && event->type < SDL_FINGERDOWN) switch (event->type)
{ {
if (event->type == SDL_CONTROLLERDEVICEADDED) case SDL_KEYDOWN:
{ case SDL_KEYUP:
const auto freeIndex = FindFreeController(); hid::detail::g_inputDevice = hid::detail::EInputDevice::Keyboard;
break;
if (freeIndex != -1) case SDL_MOUSEMOTION:
g_controllers[freeIndex] = Controller(event->cdevice.which); case SDL_MOUSEBUTTONDOWN:
} case SDL_MOUSEBUTTONUP:
if (event->type == SDL_CONTROLLERDEVICEREMOVED) hid::detail::g_inputDevice = hid::detail::EInputDevice::Mouse;
{ break;
auto* controller = FindController(event->cdevice.which);
if (controller) case SDL_WINDOWEVENT:
controller->Close();
}
else if (event->type == SDL_CONTROLLERBUTTONDOWN || event->type == SDL_CONTROLLERBUTTONUP || event->type == SDL_CONTROLLERAXISMOTION)
{ {
auto* controller = FindController(event->cdevice.which); if (event->window.event == SDL_WINDOWEVENT_FOCUS_LOST)
if (controller)
{ {
if (event->type == SDL_CONTROLLERAXISMOTION) // Stop vibrating controllers on focus lost.
{ for (auto& controller : g_controllers)
if (abs(event->caxis.value) > 8000) controller.SetVibration({ 0, 0 });
SetControllerInputDevice(controller); }
controller->PollAxis(); break;
}
default:
{
if (event->type >= SDL_CONTROLLERAXISMOTION && event->type < SDL_FINGERDOWN)
{
if (event->type == SDL_CONTROLLERDEVICEADDED)
{
const auto freeIndex = FindFreeController();
if (freeIndex != -1)
g_controllers[freeIndex] = Controller(event->cdevice.which);
} }
else if (event->type == SDL_CONTROLLERDEVICEREMOVED)
{ {
SetControllerInputDevice(controller); auto* controller = FindController(event->cdevice.which);
controller->Poll(); if (controller)
controller->Close();
}
else if (event->type == SDL_CONTROLLERBUTTONDOWN || event->type == SDL_CONTROLLERBUTTONUP || event->type == SDL_CONTROLLERAXISMOTION)
{
auto* controller = FindController(event->cdevice.which);
if (controller)
{
if (event->type == SDL_CONTROLLERAXISMOTION)
{
if (abs(event->caxis.value) > 8000)
SetControllerInputDevice(controller);
controller->PollAxis();
}
else
{
SetControllerInputDevice(controller);
controller->Poll();
}
}
} }
} }
break;
} }
} }
else if (event->type == SDL_KEYDOWN || event->type == SDL_KEYUP)
{
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);