|
| 1 | +extends OSPlatform |
| 2 | +class_name PlatformBazzite |
| 3 | + |
| 4 | +const SESSION_SELECT_PATH := "/usr/bin/steamos-session-select" |
| 5 | + |
| 6 | + |
| 7 | +func _init() -> void: |
| 8 | + logger.set_name("PlatformBazzite") |
| 9 | + logger.set_level(Log.LEVEL.INFO) |
| 10 | + |
| 11 | + |
| 12 | +func ready(root: Window) -> void: |
| 13 | + if not _has_session_switcher(): |
| 14 | + logger.info("No session switcher script detected") |
| 15 | + return |
| 16 | + _add_session_switcher(root) |
| 17 | + |
| 18 | + |
| 19 | +## Add a button to the power menu to allow session switching |
| 20 | +func _add_session_switcher(root: Window) -> void: |
| 21 | + # Get the power menu |
| 22 | + var power_menu := root.get_tree().get_first_node_in_group("power_menu") |
| 23 | + if not power_menu: |
| 24 | + logger.warn("No power menu was found. Unable to add session switcher.") |
| 25 | + return |
| 26 | + |
| 27 | + # Create a button that will perform the session switching |
| 28 | + var button_scene := load("res://core/ui/components/card_button.tscn") as PackedScene |
| 29 | + var switch_to_desktop := button_scene.instantiate() as CardButton |
| 30 | + switch_to_desktop.click_focuses = false |
| 31 | + switch_to_desktop.text = "Switch to Desktop" |
| 32 | + switch_to_desktop.pressed.connect(_switch_session.bind("desktop")) |
| 33 | + |
| 34 | + # Add the buttons just above the exit button |
| 35 | + var exit_button := power_menu.exit_button as Control |
| 36 | + var container := exit_button.get_parent() |
| 37 | + container.add_child(switch_to_desktop) |
| 38 | + container.move_child(switch_to_desktop, exit_button.get_index()) |
| 39 | + |
| 40 | + # Coerce the focus group to recalculate the focus neighbors |
| 41 | + var focus_group := power_menu.focus_group as FocusGroup |
| 42 | + focus_group.recalculate_focus() |
| 43 | + |
| 44 | + |
| 45 | +## Returns true if we detect the session switching script |
| 46 | +func _has_session_switcher() -> bool: |
| 47 | + return FileAccess.file_exists(SESSION_SELECT_PATH) |
| 48 | + |
| 49 | + |
| 50 | +## Switch to the given session |
| 51 | +func _switch_session(name: String) -> void: |
| 52 | + var out: Array = [] |
| 53 | + var code := OS.execute(SESSION_SELECT_PATH, [name], out) |
| 54 | + if code != OK: |
| 55 | + logger.error("Unable to switch sessions: " + out[0]) |
0 commit comments