3
3
import time
4
4
from threading import Event as TEvent
5
5
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
9
7
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
11
9
from hhd .controller .physical .evdev import B as EC
12
10
from hhd .controller .physical .evdev import GenericGamepadEvdev , enumerate_evs
13
11
from hhd .controller .virtual .uinput import UInputDevice
@@ -245,6 +243,22 @@ def plugin_run(
245
243
unhide_all ()
246
244
247
245
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
+
248
262
def controller_loop (
249
263
conf : Config ,
250
264
should_exit : TEvent ,
@@ -284,14 +298,14 @@ def controller_loop(
284
298
)
285
299
286
300
# Mute these so after suspend we do not get stray keypresses
287
- d_kbd_2 = GenericGamepadEvdev (
301
+ d_kbd_2 = DesktopDetectorEvdev (
288
302
vid = [MSI_CLAW_VID ],
289
303
pid = [MSI_CLAW_DINPUT_PID ],
290
304
required = False ,
291
305
grab = True ,
292
306
capabilities = {EC ("EV_KEY" ): [EC ("KEY_ESC" )]},
293
307
)
294
- d_mouse = GenericGamepadEvdev (
308
+ d_mouse = DesktopDetectorEvdev (
295
309
vid = [MSI_CLAW_VID ],
296
310
pid = [MSI_CLAW_DINPUT_PID ],
297
311
required = False ,
@@ -364,8 +378,6 @@ def prepare(m):
364
378
prepare (d_vend )
365
379
366
380
logger .info ("Emulated controller launched, have fun!" )
367
- prev = 0
368
- woke_up = 0
369
381
while not should_exit .is_set () and not updated .is_set ():
370
382
start = time .perf_counter ()
371
383
# Add timeout to call consumers a minimum amount of times per second
@@ -379,19 +391,14 @@ def prepare(m):
379
391
if id (d ) in to_run :
380
392
evs .extend (d .produce (r ))
381
393
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
386
398
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 :
392
400
logger .info ("Setting controller to dinput mode." )
393
401
d_vend .set_dinput_mode ()
394
- woke_up = None
395
402
396
403
evs = multiplexer .process (evs )
397
404
if evs :
0 commit comments