mirror of
https://github.com/moonlight-stream/moonlight-qt.git
synced 2026-06-17 22:23:31 +00:00
Batch controller axis events for reduced CPU usage on Steam Link
This commit is contained in:
+48
-29
@@ -652,41 +652,60 @@ Uint32 SdlInputHandler::mouseMoveTimerCallback(Uint32 interval, void *param)
|
|||||||
|
|
||||||
void SdlInputHandler::handleControllerAxisEvent(SDL_ControllerAxisEvent* event)
|
void SdlInputHandler::handleControllerAxisEvent(SDL_ControllerAxisEvent* event)
|
||||||
{
|
{
|
||||||
GamepadState* state = findStateForGamepad(event->which);
|
SDL_JoystickID gameControllerId = event->which;
|
||||||
|
GamepadState* state = findStateForGamepad(gameControllerId);
|
||||||
if (state == NULL) {
|
if (state == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (event->axis)
|
// Batch all pending axis motion events for this gamepad to save CPU time
|
||||||
{
|
SDL_Event nextEvent;
|
||||||
case SDL_CONTROLLER_AXIS_LEFTX:
|
for (;;) {
|
||||||
state->lsX = event->value;
|
switch (event->axis)
|
||||||
|
{
|
||||||
|
case SDL_CONTROLLER_AXIS_LEFTX:
|
||||||
|
state->lsX = event->value;
|
||||||
|
break;
|
||||||
|
case SDL_CONTROLLER_AXIS_LEFTY:
|
||||||
|
// Signed values have one more negative value than
|
||||||
|
// positive value, so inverting the sign on -32768
|
||||||
|
// could actually cause the value to overflow and
|
||||||
|
// wrap around to be negative again. Avoid that by
|
||||||
|
// capping the value at 32767.
|
||||||
|
state->lsY = -qMax(event->value, (short)-32767);
|
||||||
|
break;
|
||||||
|
case SDL_CONTROLLER_AXIS_RIGHTX:
|
||||||
|
state->rsX = event->value;
|
||||||
|
break;
|
||||||
|
case SDL_CONTROLLER_AXIS_RIGHTY:
|
||||||
|
state->rsY = -qMax(event->value, (short)-32767);
|
||||||
|
break;
|
||||||
|
case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
|
||||||
|
state->lt = (unsigned char)(event->value * 255UL / 32767);
|
||||||
|
break;
|
||||||
|
case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
|
||||||
|
state->rt = (unsigned char)(event->value * 255UL / 32767);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
||||||
|
"Unhandled controller axis: %d",
|
||||||
|
event->axis);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for another event to batch with
|
||||||
|
if (SDL_PeepEvents(&nextEvent, 1, SDL_PEEKEVENT, SDL_CONTROLLERAXISMOTION, SDL_CONTROLLERAXISMOTION) <= 0) {
|
||||||
break;
|
break;
|
||||||
case SDL_CONTROLLER_AXIS_LEFTY:
|
}
|
||||||
// Signed values have one more negative value than
|
|
||||||
// positive value, so inverting the sign on -32768
|
event = &nextEvent.caxis;
|
||||||
// could actually cause the value to overflow and
|
if (event->which != gameControllerId) {
|
||||||
// wrap around to be negative again. Avoid that by
|
// Stop batching if a different gamepad interrupts us
|
||||||
// capping the value at 32767.
|
|
||||||
state->lsY = -qMax(event->value, (short)-32767);
|
|
||||||
break;
|
break;
|
||||||
case SDL_CONTROLLER_AXIS_RIGHTX:
|
}
|
||||||
state->rsX = event->value;
|
|
||||||
break;
|
// Remove the next event to batch
|
||||||
case SDL_CONTROLLER_AXIS_RIGHTY:
|
SDL_PeepEvents(&nextEvent, 1, SDL_GETEVENT, SDL_CONTROLLERAXISMOTION, SDL_CONTROLLERAXISMOTION);
|
||||||
state->rsY = -qMax(event->value, (short)-32767);
|
|
||||||
break;
|
|
||||||
case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
|
|
||||||
state->lt = (unsigned char)(event->value * 255UL / 32767);
|
|
||||||
break;
|
|
||||||
case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
|
|
||||||
state->rt = (unsigned char)(event->value * 255UL / 32767);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
|
||||||
"Unhandled controller axis: %d",
|
|
||||||
event->axis);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sendGamepadState(state);
|
sendGamepadState(state);
|
||||||
|
|||||||
Reference in New Issue
Block a user