From 7c16782f538fd04e4f5092244eec67253603bf4a Mon Sep 17 00:00:00 2001 From: Xyct <87l46110@gmail.com> Date: Mon, 3 Jun 2024 17:33:56 +0800 Subject: [PATCH] Limit max polling rate (#142) * limit max swipe polling rate * move polling rate limit to ControlMode --- .../Controls/Backend/Action/PlayAction.swift | 1 + PlayTools/Controls/Frontend/ControlMode.swift | 30 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/PlayTools/Controls/Backend/Action/PlayAction.swift b/PlayTools/Controls/Backend/Action/PlayAction.swift index 4f024362..86df3399 100644 --- a/PlayTools/Controls/Backend/Action/PlayAction.swift +++ b/PlayTools/Controls/Backend/Action/PlayAction.swift @@ -360,6 +360,7 @@ class SwipeAction: Action { counter += 1 self.location.x += deltaX self.location.y -= deltaY + Toucher.touchcam(point: self.location, phase: UITouch.Phase.moved, tid: &id) } diff --git a/PlayTools/Controls/Frontend/ControlMode.swift b/PlayTools/Controls/Frontend/ControlMode.swift index cf6eb9a2..d0d08914 100644 --- a/PlayTools/Controls/Frontend/ControlMode.swift +++ b/PlayTools/Controls/Frontend/ControlMode.swift @@ -71,9 +71,8 @@ public class ControlMode: Equatable { }) } - AKInterface.shared!.setupMouseMoved({deltaX, deltaY in - self.mouseAdapter.handleMove(deltaX: deltaX, deltaY: deltaY) - }) + // Mouse polling rate as high as 1000 causes issue to some games + setupMouseMoved(maxPollingRate: 125) AKInterface.shared!.setupMouseButton(left: true, right: false, {_, pressed in self.mouseAdapter.handleLeftButton(pressed: pressed) @@ -90,6 +89,31 @@ public class ControlMode: Equatable { ActionDispatcher.build() } + private func setupMouseMoved(maxPollingRate: Int) { + let minMoveInterval = + DispatchTimeInterval.milliseconds(1000/maxPollingRate) + var lastMoveWhen = DispatchTime.now() + // Repeat the return value of last processed event + var consumed = true + var movement: CGVector = CGVector() + + AKInterface.shared!.setupMouseMoved({deltaX, deltaY in + // limit move frequency + let now = DispatchTime.now() + movement.dy += deltaY + movement.dx += deltaX + if now < lastMoveWhen.advanced(by: minMoveInterval) { + return consumed + } + + lastMoveWhen = now + consumed = self.mouseAdapter.handleMove(deltaX: movement.dx, deltaY: movement.dy) + movement.dy = 0 + movement.dx = 0 + return consumed + }) + } + public func set(_ mode: ControlModeLiteral) { let wasHidden = mouseAdapter?.cursorHidden() ?? false let first = mouseAdapter == nil