Skip to content

Commit

Permalink
fix(GamepadManager): Do not hide devices in sysfs.
Browse files Browse the repository at this point in the history
  • Loading branch information
pastaq authored and Derek J. Clark committed Oct 19, 2023
1 parent 88afec6 commit 4f6a656
Showing 1 changed file with 9 additions and 36 deletions.
45 changes: 9 additions & 36 deletions core/systems/input/gamepad_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ signal gamepad_added(gamepad: ManagedGamepad)
signal gamepad_removed

var platform := load("res://core/global/platform.tres") as Platform
var device_hider := load("res://core/systems/input/device_hider.tres") as DeviceHider
var device_unhider := load("res://core/systems/input/device_hider.tres") as DeviceHider
var input_thread := load("res://core/systems/threading/input_thread.tres") as SharedThread

var gamepads := GamepadArray.new()
Expand All @@ -38,8 +38,8 @@ func _init() -> void:
logger.info("Not initializing. Ran from editor.")
return

# If we crashed, unhide any device events that were orphaned
await device_hider.restore_all_hidden()
# Unhide any device events that were orphaned by bad programs like HandyGCCS
await device_unhider.restore_all_hidden()

# Discover any gamepads and grab exclusive access to them. Create a
# duplicate virtual gamepad for each physical one.
Expand Down Expand Up @@ -143,17 +143,14 @@ func exit() -> void:
gamepad.phys_device.grab(false)
gamepad.virt_device.close()
gamepad.phys_device.close()
device_hider.restore_event_device(gamepad.phys_path)
if gamepad is HandheldGamepad:
logger.debug("Cleaning up handheld gamepad: " + gamepad.gamepad.phys_path)
gamepad.gamepad.phys_device.grab(false)
gamepad.gamepad.virt_device.close()
gamepad.gamepad.phys_device.close()
device_hider.restore_event_device(gamepad.gamepad.phys_path)

gamepad.kb_device.grab(false)
gamepad.kb_device.close()
device_hider.restore_event_device(gamepad.kb_event_path)


## Sets the gamepad intercept mode
Expand All @@ -167,8 +164,6 @@ func set_intercept(mode: ManagedGamepad.INTERCEPT_MODE) -> void:
## access variables from the main thread
func _process_input(_delta: float) -> void:
# Process the input for all currently managed gamepads
if not is_instance_valid(gamepads):
return
for gamepad in gamepads.items():
gamepad.process_input()

Expand Down Expand Up @@ -236,32 +231,20 @@ func _on_gamepad_change(device: int, connected: bool) -> void:

# Hide the device from other processes
var path := discovered_handheld.get_path()
logger.debug("Trying to re-hide handheld gamepad")
var hidden_path := await device_hider.hide_event_device(path)
if hidden_path == "":
logger.warn("Unable to re-hide handheld gamepad: " + path)

# Setup any handheld gamepads if they are discovered and not yet configured
if not gamepads.has_handheld() and discovered_handheld:
var path := discovered_handheld.get_path()
logger.info("A handheld gamepad was discovered at: " + path)
# Hide the device from other processes
logger.debug("Trying to hide handheld gamepad")
var hidden_path := await device_hider.hide_event_device(path)
if hidden_path == "":
logger.warn("Unable to hide handheld gamepad: " + path)
logger.warn("Opening the raw handheld gamepad instead")
# Try to open the non-hidden device instead
hidden_path = path

# Create a new managed gamepad with physical/virtual gamepad pair
logger.debug("Opening handheld gamepad at: " + hidden_path)
logger.debug("Opening handheld gamepad at: " + path)
var gamepad := HandheldGamepad.new()
if gamepad.open(hidden_path) != OK:
logger.error("Unable to create handheld gamepad for: " + hidden_path)
if hidden_path != path:
logger.debug("Restoring device back to its regular path")
device_hider.restore_event_device(hidden_path)
if gamepad.open(path) != OK:
logger.error("Unable to create handheld gamepad for: " + path)

else:
gamepad.setup(discovered_keyboards)
gamepads.add(gamepad)
Expand All @@ -281,20 +264,10 @@ func _on_gamepad_change(device: int, connected: bool) -> void:
logger.debug("Device appears to be virtual , skipping " + path)
continue

# Hide the device from other processes
var hidden_path := await device_hider.hide_event_device(path)
if hidden_path == "":
logger.warn("Unable to hide gamepad: " + path)
logger.warn("Opening the raw gamepad instead")
# Try to open the non-hidden device instead
hidden_path = path

# Create a new managed gamepad with physical/virtual gamepad pair
var gamepad := ManagedGamepad.new()
if gamepad.open(hidden_path) != OK:
logger.error("Unable to create managed gamepad for: " + hidden_path)
if hidden_path != path:
device_hider.restore_event_device(hidden_path)
if gamepad.open(path) != OK:
logger.error("Unable to create managed gamepad for: " + path)
continue

gamepads.add(gamepad)
Expand Down

0 comments on commit 4f6a656

Please sign in to comment.