Skip to content

Commit 510d2f2

Browse files
committed
set dinput mode on demand
1 parent 9366aac commit 510d2f2

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

src/hhd/device/claw/base.py

+25-18
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
import time
44
from threading import Event as TEvent
55

6-
from hhd.controller import DEBUG_MODE, Multiplexer
7-
from hhd.controller.lib.common import AM, BM, CM
8-
from hhd.controller.lib.hid import MAX_REPORT_SIZE
6+
from hhd.controller import DEBUG_MODE, Multiplexer, can_read
97
from hhd.controller.lib.hide import unhide_all
10-
from hhd.controller.physical.hidraw import EventCallback, GenericGamepadHidraw
8+
from hhd.controller.physical.hidraw import GenericGamepadHidraw
119
from hhd.controller.physical.evdev import B as EC
1210
from hhd.controller.physical.evdev import GenericGamepadEvdev, enumerate_evs
1311
from hhd.controller.virtual.uinput import UInputDevice
@@ -245,6 +243,22 @@ def plugin_run(
245243
unhide_all()
246244

247245

246+
class DesktopDetectorEvdev(GenericGamepadEvdev):
247+
def __init__(self, *args, **kwargs):
248+
super().__init__(*args, **kwargs)
249+
self.desktop = False
250+
251+
def produce(self, fds: Sequence[int]):
252+
if not self.dev or self.fd not in fds:
253+
return []
254+
255+
while can_read(self.fd):
256+
for e in self.dev.read():
257+
self.desktop = True
258+
259+
return []
260+
261+
248262
def controller_loop(
249263
conf: Config,
250264
should_exit: TEvent,
@@ -284,14 +298,14 @@ def controller_loop(
284298
)
285299

286300
# Mute these so after suspend we do not get stray keypresses
287-
d_kbd_2 = GenericGamepadEvdev(
301+
d_kbd_2 = DesktopDetectorEvdev(
288302
vid=[MSI_CLAW_VID],
289303
pid=[MSI_CLAW_DINPUT_PID],
290304
required=False,
291305
grab=True,
292306
capabilities={EC("EV_KEY"): [EC("KEY_ESC")]},
293307
)
294-
d_mouse = GenericGamepadEvdev(
308+
d_mouse = DesktopDetectorEvdev(
295309
vid=[MSI_CLAW_VID],
296310
pid=[MSI_CLAW_DINPUT_PID],
297311
required=False,
@@ -364,8 +378,6 @@ def prepare(m):
364378
prepare(d_vend)
365379

366380
logger.info("Emulated controller launched, have fun!")
367-
prev = 0
368-
woke_up = 0
369381
while not should_exit.is_set() and not updated.is_set():
370382
start = time.perf_counter()
371383
# Add timeout to call consumers a minimum amount of times per second
@@ -379,19 +391,14 @@ def prepare(m):
379391
if id(d) in to_run:
380392
evs.extend(d.produce(r))
381393

382-
# Detect wakeup through pause
383-
if start - prev > 1:
384-
woke_up = start
385-
prev = start
394+
# Detect if we are in desktop mode through events
395+
desktop_mode = d_mouse.desktop or d_kbd_2.desktop
396+
d_mouse.desktop = False
397+
d_kbd_2.desktop = False
386398

387-
# After wakeup, the controller waits a bit until it
388-
# realizes it woke up and switches to desktop mode.
389-
# Therefore we need to wait otherwise we race it and
390-
# end up stuck in desktop mode.
391-
if woke_up is not None and start - woke_up > 3:
399+
if desktop_mode:
392400
logger.info("Setting controller to dinput mode.")
393401
d_vend.set_dinput_mode()
394-
woke_up = None
395402

396403
evs = multiplexer.process(evs)
397404
if evs:

0 commit comments

Comments
 (0)