Skip to content

Commit 461af66

Browse files
authored
Merge pull request #3423 from cmitu/joy2key-fix3
joy2key: fix a couple of bugs in the SDL version
2 parents 7a8f528 + 99fb97f commit 461af66

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

scriptmodules/admin/joy2key/joy2key_sdl.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from sdl2 import joystick, events, version, \
3939
SDL_WasInit, SDL_Init, SDL_QuitSubSystem, SDL_GetError, \
4040
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, \
4242
SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED, SDL_QUIT, \
4343
SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_JOYHATMOTION, SDL_JOYAXISMOTION, \
4444
SDL_GetTicks
@@ -255,7 +255,7 @@ def filter_active_events(event_queue: dict) -> list:
255255
for e in event_queue:
256256
if event_queue[e][0] is None:
257257
continue
258-
258+
259259
last_fire_time = event_queue[e][2]
260260
repeat_count = event_queue[e][1]
261261

@@ -268,7 +268,13 @@ def filter_active_events(event_queue: dict) -> list:
268268

269269
# remove any duplicate events from the list
270270
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+
272278
def event_loop(configs, joy_map, tty_fd):
273279
event = SDL_Event()
274280

@@ -346,6 +352,8 @@ def handle_new_input(e: SDL_Event, axis_norm_value: int = 0) -> bool:
346352
if joystick.SDL_JoystickNumAxes(stick) > 0:
347353
axis_prev_values[joystick.SDL_JoystickInstanceID(stick)] = [0 for x in range(joystick.SDL_JoystickNumAxes(stick))]
348354

355+
# Remove any spurious axis movements reported by SDL during initialization
356+
SDL_FlushEvent(SDL_JOYAXISMOTION);
349357
continue
350358

351359
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:
355363

356364
if event.type == SDL_JOYDEVICEREMOVED:
357365
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])
358368
active_devices.pop(event.jdevice.which, None)
359369
axis_prev_values.pop(event.jdevice.which, None)
360370
LOG.debug(f'Removed joystick #{event.jdevice.which}')

0 commit comments

Comments
 (0)