79 lines
2.4 KiB
GDScript3
79 lines
2.4 KiB
GDScript3
|
|
extends Plugin
|
||
|
|
|
||
|
|
## Adds a "Switch to Steam" button to the power menu on ChimeraOS and SteamOS.
|
||
|
|
##
|
||
|
|
## OpenGamepadUI's built-in ChimeraOS platform support adds a "Switch to
|
||
|
|
## Desktop" button but has no way to return to Steam game mode. This plugin
|
||
|
|
## adds that missing button so users can round-trip between an exclusive
|
||
|
|
## OpenGamepadUI session and Steam.
|
||
|
|
|
||
|
|
const SESSION_SELECT_PATH := "/usr/lib/os-session-select"
|
||
|
|
const POWER_MENU_GROUP := "power_menu"
|
||
|
|
const BUTTON_NAME := "SwitchToSteamButton"
|
||
|
|
const MAX_ATTEMPTS := 600
|
||
|
|
|
||
|
|
|
||
|
|
func _ready() -> void:
|
||
|
|
logger = Log.get_logger("SessionSwitch", Log.LEVEL.INFO)
|
||
|
|
if not _has_session_switcher():
|
||
|
|
logger.info("No session switcher script detected: " + SESSION_SELECT_PATH)
|
||
|
|
return
|
||
|
|
_add_button_when_ready()
|
||
|
|
|
||
|
|
|
||
|
|
func _add_button_when_ready() -> void:
|
||
|
|
for i in MAX_ATTEMPTS:
|
||
|
|
if _try_add_button():
|
||
|
|
return
|
||
|
|
await get_tree().process_frame
|
||
|
|
logger.info("Power menu never became available. Skipping 'Switch to Steam' button.")
|
||
|
|
|
||
|
|
|
||
|
|
func _try_add_button() -> bool:
|
||
|
|
var power_menu := get_tree().get_first_node_in_group(POWER_MENU_GROUP)
|
||
|
|
if not power_menu:
|
||
|
|
return false
|
||
|
|
|
||
|
|
var exit_button = power_menu.get("exit_button")
|
||
|
|
if not exit_button:
|
||
|
|
return false
|
||
|
|
|
||
|
|
var container = exit_button.get_parent()
|
||
|
|
if not container:
|
||
|
|
return false
|
||
|
|
if container.has_node(BUTTON_NAME):
|
||
|
|
return true
|
||
|
|
|
||
|
|
var button_scene := load("res://core/ui/components/card_button.tscn") as PackedScene
|
||
|
|
if not button_scene:
|
||
|
|
logger.error("Unable to load card_button scene.")
|
||
|
|
return true
|
||
|
|
var button := button_scene.instantiate()
|
||
|
|
button.name = BUTTON_NAME
|
||
|
|
button.set("click_focuses", false)
|
||
|
|
button.set("text", "Switch to Steam")
|
||
|
|
button.pressed.connect(_switch_session.bind("steam"))
|
||
|
|
|
||
|
|
container.add_child(button)
|
||
|
|
container.move_child(button, exit_button.get_index())
|
||
|
|
|
||
|
|
var focus_group = power_menu.get("focus_group")
|
||
|
|
if focus_group and focus_group.has_method("recalculate_focus"):
|
||
|
|
focus_group.recalculate_focus()
|
||
|
|
|
||
|
|
logger.info("Added 'Switch to Steam' button to the power menu.")
|
||
|
|
return true
|
||
|
|
|
||
|
|
|
||
|
|
## Returns true if the session switcher script is present.
|
||
|
|
func _has_session_switcher() -> bool:
|
||
|
|
return FileAccess.file_exists(SESSION_SELECT_PATH)
|
||
|
|
|
||
|
|
|
||
|
|
## Switches to the given session using the OS session switcher.
|
||
|
|
func _switch_session(name: String) -> void:
|
||
|
|
var out: Array = []
|
||
|
|
var code := OS.execute(SESSION_SELECT_PATH, [name], out)
|
||
|
|
if code != OK:
|
||
|
|
logger.error("Unable to switch sessions: " + out[0])
|