Fix controllers

This commit is contained in:
Joey Yakimowich-Payne 2026-01-10 23:19:13 -07:00
commit ae65251cb4

View file

@ -214,6 +214,7 @@ class ControllerContext:
last_rumble_energy: float = 0.0 last_rumble_energy: float = 0.0
rumble_active: bool = False rumble_active: bool = False
axis_offsets: Dict[int, int] = field(default_factory=dict) axis_offsets: Dict[int, int] = field(default_factory=dict)
swap_abxy: bool = False
def capture_stick_offsets(controller: sdl2.SDL_GameController) -> Dict[int, int]: def capture_stick_offsets(controller: sdl2.SDL_GameController) -> Dict[int, int]:
@ -395,12 +396,9 @@ def toggle_abxy_for_context(ctx: ControllerContext, config: BridgeConfig, consol
if config.swap_abxy_global: if config.swap_abxy_global:
console.print("[yellow]Global --swap-abxy is enabled; disable it to use per-controller toggles.[/yellow]") console.print("[yellow]Global --swap-abxy is enabled; disable it to use per-controller toggles.[/yellow]")
return return
swapped = ctx.stable_id in config.swap_abxy_ids swapped = ctx.swap_abxy
action = "standard" if swapped else "swapped" action = "standard" if swapped else "swapped"
if swapped: ctx.swap_abxy = not swapped
config.swap_abxy_ids.discard(ctx.stable_id)
else:
config.swap_abxy_ids.add(ctx.stable_id)
console.print( console.print(
f"[cyan]Controller {ctx.controller_index} ({controller_display_name(ctx)}, inst {ctx.instance_id}) now using {action} ABXY layout.[/cyan]" f"[cyan]Controller {ctx.controller_index} ({controller_display_name(ctx)}, inst {ctx.instance_id}) now using {action} ABXY layout.[/cyan]"
) )
@ -425,7 +423,7 @@ def prompt_swap_abxy_controller(
table.add_column("GUID") table.add_column("GUID")
table.add_column("Layout", justify="center") table.add_column("Layout", justify="center")
for idx, ctx in enumerate(controllers): for idx, ctx in enumerate(controllers):
swapped = config.swap_abxy_global or (ctx.stable_id in config.swap_abxy_ids) swapped = config.swap_abxy_global or ctx.swap_abxy
state = "Swapped" if swapped else "Standard" state = "Swapped" if swapped else "Standard"
if config.swap_abxy_global: if config.swap_abxy_global:
state += " (global)" state += " (global)"
@ -988,8 +986,7 @@ def open_initial_contexts(
console.print(f"[red]Failed to open controller {index}: {exc}[/red]") console.print(f"[red]Failed to open controller {index}: {exc}[/red]")
continue continue
stable_id = guid stable_id = guid
if index in config.swap_abxy_indices: should_swap = index in config.swap_abxy_indices or stable_id in config.swap_abxy_ids
config.swap_abxy_ids.add(stable_id)
uart = open_uart_or_warn(port, args.baud, console) if port else None uart = open_uart_or_warn(port, args.baud, console) if port else None
if uart: if uart:
uarts.append(uart) uarts.append(uart)
@ -1007,6 +1004,7 @@ def open_initial_contexts(
stable_id=stable_id, stable_id=stable_id,
port=port, port=port,
uart=uart, uart=uart,
swap_abxy=should_swap,
) )
if config.zero_sticks: if config.zero_sticks:
zero_context_sticks(ctx, console) zero_context_sticks(ctx, console)
@ -1058,7 +1056,7 @@ def handle_button_event(
return return
current_button_map = ( current_button_map = (
config.button_map_swapped config.button_map_swapped
if (config.swap_abxy_global or ctx.stable_id in config.swap_abxy_ids) if (config.swap_abxy_global or ctx.swap_abxy)
else config.button_map_default else config.button_map_default
) )
button = event.cbutton.button button = event.cbutton.button
@ -1103,9 +1101,8 @@ def handle_device_added(
console.print(f"[red]Hotplug open failed for controller {idx}: {exc}[/red]") console.print(f"[red]Hotplug open failed for controller {idx}: {exc}[/red]")
return return
stable_id = guid stable_id = guid
# Promote any index-based swap flags to stable IDs on first sight. # Determine if this controller should have swapped ABXY based on index or GUID
if idx in config.swap_abxy_indices: should_swap = idx in config.swap_abxy_indices or stable_id in config.swap_abxy_ids
config.swap_abxy_ids.add(stable_id)
uart = open_uart_or_warn(port, args.baud, console) if port else None uart = open_uart_or_warn(port, args.baud, console) if port else None
if uart: if uart:
uarts.append(uart) uarts.append(uart)
@ -1123,6 +1120,7 @@ def handle_device_added(
stable_id=stable_id, stable_id=stable_id,
port=port, port=port,
uart=uart, uart=uart,
swap_abxy=should_swap,
) )
if config.zero_sticks: if config.zero_sticks:
zero_context_sticks(ctx, console) zero_context_sticks(ctx, console)
@ -1163,7 +1161,7 @@ def service_contexts(
for ctx in list(contexts.values()): for ctx in list(contexts.values()):
current_button_map = ( current_button_map = (
config.button_map_swapped config.button_map_swapped
if (config.swap_abxy_global or ctx.stable_id in config.swap_abxy_ids) if (config.swap_abxy_global or ctx.swap_abxy)
else config.button_map_default else config.button_map_default
) )
poll_controller_buttons(ctx, current_button_map) poll_controller_buttons(ctx, current_button_map)