|
| 1 | +@icon("res://assets/editor-icons/fluent--pipeline-20-filled.svg") |
| 2 | +extends Node |
| 3 | +class_name PipeManager |
| 4 | + |
| 5 | +## Class for managing control messages sent through a named pipe |
| 6 | +## |
| 7 | +## The [PipeManager] creates a named pipe in `/run/user/<uid>/opengamepadui` |
| 8 | +## that can be used as a communication mechanism to send OpenGamepadUI commands |
| 9 | +## from another process. This is mostly done to handle custom `ogui://` URIs |
| 10 | +## which can be used to react in different ways. |
| 11 | + |
| 12 | +const RUN_FOLDER: String = "/run/user/{}/opengamepadui" |
| 13 | +const URI_PREFIX: String = "ogui://" |
| 14 | + |
| 15 | +signal line_written(line: String) |
| 16 | + |
| 17 | +var launch_manager := load("res://core/global/launch_manager.tres") as LaunchManager |
| 18 | +var library_manager := load("res://core/global/library_manager.tres") as LibraryManager |
| 19 | +var settings_manager := load("res://core/global/settings_manager.tres") as SettingsManager |
| 20 | + |
| 21 | +var pipe: FifoReader |
| 22 | +var pipe_path: String |
| 23 | +var logger := Log.get_logger("PipeManager", Log.LEVEL.DEBUG) |
| 24 | + |
| 25 | + |
| 26 | +func _ready() -> void: |
| 27 | + # Ensure the run directory exists |
| 28 | + var run_path := get_run_path() |
| 29 | + DirAccess.make_dir_recursive_absolute(run_path) |
| 30 | + |
| 31 | + # Create a unix named pipe |
| 32 | + pipe_path = get_pipe_path() |
| 33 | + if pipe_path.is_empty(): |
| 34 | + logger.error("Failed to get pipe path!") |
| 35 | + return |
| 36 | + logger.info("Opening pipe:", pipe_path) |
| 37 | + pipe = FifoReader.new() |
| 38 | + if pipe.open(pipe_path) != OK: |
| 39 | + return |
| 40 | + pipe.line_written.connect(_on_line_written) |
| 41 | + add_child(pipe) |
| 42 | + |
| 43 | + |
| 44 | +## Returns the path to the named pipe (e.g. /run/user/1000/opengamepadui/opengamepadui-0) |
| 45 | +func get_pipe_path() -> String: |
| 46 | + var run_path := get_run_path() |
| 47 | + if run_path.is_empty(): |
| 48 | + return "" |
| 49 | + var path := "/".join([run_path, "opengamepadui-0"]) |
| 50 | + return path |
| 51 | + |
| 52 | + |
| 53 | +## Returns the run path for the current user (e.g. /run/user/1000/opengamepadui) |
| 54 | +func get_run_path() -> String: |
| 55 | + var uid := get_uid() |
| 56 | + if uid < 0: |
| 57 | + return "" |
| 58 | + var run_folder := RUN_FOLDER.format([uid], "{}") |
| 59 | + |
| 60 | + return run_folder |
| 61 | + |
| 62 | + |
| 63 | +## Returns the current user id (e.g. 1000) |
| 64 | +func get_uid() -> int: |
| 65 | + var output: Array = [] |
| 66 | + if OS.execute("id", ["-u"], output) != OK: |
| 67 | + return -1 |
| 68 | + if output.is_empty(): |
| 69 | + return -1 |
| 70 | + var data := output[0] as String |
| 71 | + var uid := data.to_int() |
| 72 | + |
| 73 | + return uid |
| 74 | + |
| 75 | + |
| 76 | +func _on_line_written(line: String) -> void: |
| 77 | + if line.is_empty(): |
| 78 | + return |
| 79 | + logger.debug("Received piped message:", line) |
| 80 | + line_written.emit(line) |
| 81 | + |
| 82 | + # Check to see if the message is an ogui URI |
| 83 | + if line.begins_with(URI_PREFIX): |
| 84 | + _handle_uri_request(line) |
| 85 | + |
| 86 | + |
| 87 | +func _handle_uri_request(uri: String) -> void: |
| 88 | + var path := uri.replace(URI_PREFIX, "") |
| 89 | + var parts: Array[String] |
| 90 | + parts.assign(path.split("/")) |
| 91 | + if parts.is_empty(): |
| 92 | + return |
| 93 | + var cmd := parts.pop_front() as String |
| 94 | + logger.info("Received URI command:", cmd) |
| 95 | + match cmd: |
| 96 | + "run": |
| 97 | + _handle_run_cmd(parts) |
| 98 | + |
| 99 | + |
| 100 | +func _handle_run_cmd(args: Array[String]) -> void: |
| 101 | + if args.is_empty(): |
| 102 | + logger.debug("No game name was found in URI") |
| 103 | + return |
| 104 | + var game_name := args[0] |
| 105 | + var library_item := library_manager.get_app_by_name(game_name) |
| 106 | + if not library_item: |
| 107 | + logger.warn("Unable to find game with name:", game_name) |
| 108 | + return |
| 109 | + |
| 110 | + # Select the provider |
| 111 | + var launch_item := library_item.launch_items[0] as LibraryLaunchItem |
| 112 | + var section := "game.{0}".format([library_item.name.to_lower()]) |
| 113 | + var provider_id = settings_manager.get_value(section, "provider", "") |
| 114 | + if provider_id != "": |
| 115 | + var p := library_item.get_launch_item(provider_id) |
| 116 | + if p != null: |
| 117 | + launch_item = p |
| 118 | + |
| 119 | + # Start the game |
| 120 | + logger.info("Starting game:", game_name) |
| 121 | + launch_manager.launch(launch_item) |
| 122 | + |
| 123 | + |
| 124 | +func _exit_tree() -> void: |
| 125 | + if not pipe: |
| 126 | + return |
| 127 | + pipe.close() |
0 commit comments