Skip to content

Commit 992baaa

Browse files
committed
fix(GamepadProfile): Fix setting gamepad profile in certain situations.
- Gamepad profile and target devices will now properly load at startup. - Gamepad profile and target devices will now properly be set when changing the default profile. - Gamepad profile and target devices will now properly be set when InputPlumber is restarted.
1 parent c1ad682 commit 992baaa

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

core/systems/input/input_plumber.gd

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ var intercept_mode_current: INTERCEPT_MODE = INTERCEPT_MODE.NONE
4343
var intercept_triggers_current: PackedStringArray = ["Gamepad:Button:Guide"]
4444
var intercept_target_current: String = "Gamepad:Button:Guide"
4545

46+
## Emitted when a CompositeDevice is dicovered and identified as a new device
4647
signal composite_device_added(device: CompositeDevice)
48+
## Emitted when a CompositeDevice is dicovered over dbus but already exists in
49+
## the local map
50+
signal composite_device_changed(device: CompositeDevice)
51+
## Emitted when a CompositeDevice is removed
4752
signal composite_device_removed(dbus_path: String)
4853

4954

@@ -71,7 +76,7 @@ func _on_interfaces_added(dbus_path: String) -> void:
7176
logger.debug("Interfaces Added: " + str(dbus_path))
7277
if not "CompositeDevice" in dbus_path:
7378
return
74-
composite_devices = get_devices()
79+
composite_devices = get_devices(dbus_path)
7580
composite_devices_map.clear()
7681
for device in composite_devices:
7782
composite_devices_map[device.dbus_path] = device
@@ -149,8 +154,10 @@ func get_objects_of(pattern: String) -> Array:
149154
return devices
150155

151156

152-
## Retrieves all CompositeDevices currently on the InputPlumber DBus interface
153-
func get_devices() -> Array[CompositeDevice]:
157+
## Retrieves all CompositeDevices currently on the InputPlumber DBus interface. Will
158+
## emit composite_device_added if the given dbus path is a new device, or
159+
## composite_device_changed if it already existed
160+
func get_devices(dbus_path: String = "") -> Array[CompositeDevice]:
154161
logger.debug("Getting all composite devices.")
155162
var new_device_list: Array[CompositeDevice]
156163
new_device_list.assign(get_objects_of("CompositeDevice"))
@@ -163,7 +170,10 @@ func get_devices() -> Array[CompositeDevice]:
163170
if old_dev.dbus_path == device.dbus_path:
164171
existing_devices.append(old_dev)
165172
found = true
173+
if dbus_path == device.dbus_path:
174+
composite_device_changed.emit(device)
166175
break
176+
167177
# New device found
168178
if not found:
169179
existing_devices.append(device)

core/ui/card_ui/gamepad/gamepad_settings.gd

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func _ready() -> void:
5454
profile = _load_profile()
5555
_update_mapping_elements()
5656
delete_button.button_up.connect(on_delete)
57-
57+
5858
# Setup the gamepad type dropdown
5959
gamepad_type_dropdown.clear()
6060
for gamepad_type: String in self.gamepad_types:
@@ -64,16 +64,29 @@ func _ready() -> void:
6464
if self.profile:
6565
var gamepad_type := self.get_selected_target_gamepad()
6666
profile_gamepad = InputPlumberProfile.get_target_device_string(gamepad_type)
67+
logger.debug("Setting gamepad to " + profile_gamepad)
68+
else:
69+
logger.debug("No profile, unable to set gamepad type.")
6770
self._update_mapping_elements()
6871
gamepad_type_dropdown.item_selected.connect(on_gamepad_selected)
69-
72+
73+
# Load the default profile
74+
var profile_path = settings_manager.get_value("input", "gamepad_profile")
75+
profile_gamepad = settings_manager.get_value("input", "gamepad_profile_target")
76+
for gamepad in input_plumber.composite_devices:
77+
_set_gamepad_profile(gamepad, profile_path)
78+
7079
# Grab focus when the mapper exits
7180
var on_state_changed := func(_from: State, to: State):
7281
if to:
7382
return
7483
mapping_focus_group.grab_focus()
7584
state_machine.state_changed.connect(on_state_changed)
7685

86+
# Ensure new devices are set to the correct profile when added
87+
input_plumber.composite_device_added.connect(_set_gamepad_profile)
88+
input_plumber.composite_device_changed.connect(_set_gamepad_profile)
89+
7790

7891
## Called when the gamepad settings state is entered
7992
func _on_state_entered(_from: State) -> void:
@@ -145,6 +158,7 @@ func _on_state_entered(_from: State) -> void:
145158
# Set the profile text to the game name
146159
profile_label.text = library_item.name
147160

161+
148162
# Check to see if the given game has a gamepad profile
149163
var profile_path := settings_manager.get_library_value(library_item, "gamepad_profile", "") as String
150164
var profile_target_gamepad := settings_manager.get_library_value(library_item, "gamepad_profile_target", "") as String
@@ -558,13 +572,37 @@ func get_target_gamepad_text(gamepad_type: InputPlumberProfile.TargetDevice) ->
558572
return "Generic Gamepad"
559573

560574

575+
# Set the given profile for the given composte device.
576+
func _set_gamepad_profile(gamepad: InputPlumber.CompositeDevice, profile_path: String = "") -> void:
577+
if profile_path == "":
578+
if gamepad_state.has_meta("item"):
579+
library_item = gamepad_state.get_meta("item") as LibraryItem
580+
581+
# If no library item was set, but there's a running app, try to see if
582+
# there is a library item for it instead.
583+
if not library_item:
584+
library_item = launch_manager.get_current_app_library_item()
585+
586+
# If no library item was set with the state, then use the default
587+
if not library_item:
588+
profile_path = settings_manager.get_value("input", "gamepad_profile")
589+
else:
590+
profile_path = settings_manager.get_library_value(library_item, "gamepad_profile", "")
591+
592+
logger.debug("Setting " + gamepad.name + " to profile: " + profile_path)
593+
gamepad.load_profile_path(profile_path)
594+
if not profile_gamepad.is_empty():
595+
logger.debug("Setting target gamepad to: " + profile_gamepad)
596+
gamepad.set_target_devices([profile_gamepad, "keyboard", "mouse"])
597+
598+
561599
# Save the current profile to a file
562600
func _save_profile() -> void:
563601
var notify := Notification.new("")
564602
if not profile:
565603
logger.debug("No profile loaded to save")
566604
return
567-
605+
568606
# Handle global gamepad profiles
569607
if not library_item:
570608
logger.debug("No library item loaded to associate profile with")
@@ -576,10 +614,14 @@ func _save_profile() -> void:
576614
notify.text = "Failed to save global gamepad profile"
577615
notification_manager.show(notify)
578616
return
579-
617+
580618
# Update the game settings to use this global profile
581619
settings_manager.set_value("input", "gamepad_profile", path)
582620
settings_manager.set_value("input", "gamepad_profile_target", profile_gamepad)
621+
622+
for gamepad in input_plumber.composite_devices:
623+
_set_gamepad_profile(gamepad, path)
624+
583625
logger.debug("Saved global gamepad profile to: " + path)
584626
notify.text = "Global gamepad profile saved"
585627
notification_manager.show(notify)

0 commit comments

Comments
 (0)