38
38
from sdl2 import joystick , events , version , \
39
39
SDL_WasInit , SDL_Init , SDL_QuitSubSystem , SDL_GetError , \
40
40
SDL_INIT_JOYSTICK , SDL_INIT_VIDEO , version_info , \
41
- SDL_Event , SDL_PollEvent , SDL_Delay , SDL_Quit , \
41
+ SDL_Event , SDL_PollEvent , SDL_FlushEvent , SDL_Delay , SDL_Quit , \
42
42
SDL_JOYDEVICEADDED , SDL_JOYDEVICEREMOVED , SDL_QUIT , \
43
43
SDL_JOYBUTTONDOWN , SDL_JOYBUTTONUP , SDL_JOYHATMOTION , SDL_JOYAXISMOTION , \
44
44
SDL_GetTicks
@@ -255,7 +255,7 @@ def filter_active_events(event_queue: dict) -> list:
255
255
for e in event_queue :
256
256
if event_queue [e ][0 ] is None :
257
257
continue
258
-
258
+
259
259
last_fire_time = event_queue [e ][2 ]
260
260
repeat_count = event_queue [e ][1 ]
261
261
@@ -268,7 +268,13 @@ def filter_active_events(event_queue: dict) -> list:
268
268
269
269
# remove any duplicate events from the list
270
270
return list (set (filtered_events ))
271
-
271
+
272
+ """
273
+ Remove all queued events for a device
274
+ """
275
+ def remove_events_for_device (event_queue : dict , dev_index : int ):
276
+ return { key :value for (key ,value ) in event_queue .items () if not key .startswith (f"{ dev_index } _" )}
277
+
272
278
def event_loop (configs , joy_map , tty_fd ):
273
279
event = SDL_Event ()
274
280
@@ -346,6 +352,8 @@ def handle_new_input(e: SDL_Event, axis_norm_value: int = 0) -> bool:
346
352
if joystick .SDL_JoystickNumAxes (stick ) > 0 :
347
353
axis_prev_values [joystick .SDL_JoystickInstanceID (stick )] = [0 for x in range (joystick .SDL_JoystickNumAxes (stick ))]
348
354
355
+ # Remove any spurious axis movements reported by SDL during initialization
356
+ SDL_FlushEvent (SDL_JOYAXISMOTION );
349
357
continue
350
358
351
359
if event .jdevice .which not in active_devices :
@@ -355,6 +363,8 @@ def handle_new_input(e: SDL_Event, axis_norm_value: int = 0) -> bool:
355
363
356
364
if event .type == SDL_JOYDEVICEREMOVED :
357
365
joystick .SDL_JoystickClose (active_devices [event .jdevice .which ][1 ])
366
+ if event .jdevice .which in active_devices :
367
+ event_queue = remove_events_for_device (event_queue , active_devices [event .jdevice .which ][0 ])
358
368
active_devices .pop (event .jdevice .which , None )
359
369
axis_prev_values .pop (event .jdevice .which , None )
360
370
LOG .debug (f'Removed joystick #{ event .jdevice .which } ' )
0 commit comments