Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions core/systems/ipc/pipe_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ class_name PipeManager

## Class for managing control messages sent through a named pipe
##
## The [PipeManager] creates a named pipe in `/run/user/<uid>/opengamepadui`
## The [PipeManager] creates a named pipe in `$XDG_RUNTIME_DIR/opengamepadui`
## that can be used as a communication mechanism to send OpenGamepadUI commands
## from another process. This is mostly done to handle custom `ogui://` URIs
## which can be used to react in different ways.

const RUN_FOLDER: String = "/run/user/{}/opengamepadui"
const URI_PREFIX: String = "ogui://"

signal line_written(line: String)
Expand All @@ -26,6 +25,9 @@ var logger := Log.get_logger("PipeManager", Log.LEVEL.DEBUG)
func _ready() -> void:
# Ensure the run directory exists
var run_path := get_run_path()
if run_path.is_empty():
logger.error("$XDG_RUNTIME_DIR is not set; unable to create named pipe")
return
DirAccess.make_dir_recursive_absolute(run_path)

# Create a unix named pipe
Expand All @@ -52,25 +54,16 @@ func get_pipe_path() -> String:

## Returns the run path for the current user (e.g. /run/user/1000/opengamepadui)
func get_run_path() -> String:
var uid := get_uid()
if uid < 0:
var runtime_dir := get_runtime_dir()
if runtime_dir.is_empty():
return ""
var run_folder := RUN_FOLDER.format([uid], "{}")

return run_folder

return "/".join([runtime_dir, "opengamepadui"])

## Returns the current user id (e.g. 1000)
func get_uid() -> int:
var output: Array = []
if OS.execute("id", ["-u"], output) != OK:
return -1
if output.is_empty():
return -1
var data := output[0] as String
var uid := data.to_int()

return uid
## Returns the XDG runtime directory (e.g. /run/user/1000)
func get_runtime_dir() -> String:
return OS.get_environment("XDG_RUNTIME_DIR")


func _on_line_written(line: String) -> void:
Expand Down
2 changes: 1 addition & 1 deletion docs/class-reference/PipeManager.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Class for managing control messages sent through a named pipe
## Description

The [PipeManager](../PipeManager) creates a named pipe in `/run/user/<uid>/opengamepadui` that can be used as a communication mechanism to send OpenGamepadUI commands from another process. This is mostly done to handle custom `ogui://` URIs which can be used to react in different ways.
The [PipeManager](../PipeManager) creates a named pipe in `$XDG_RUNTIME_DIR/opengamepadui` that can be used as a communication mechanism to send OpenGamepadUI commands from another process. This is mostly done to handle custom `ogui://` URIs which can be used to react in different ways.
## Properties

| Type | Name | Default |
Expand Down
7 changes: 4 additions & 3 deletions rootfs/usr/bin/opengamepadui
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ ARCH=$(uname -m)

OGUI_BIN=${OGUI_BIN:-"${PREFIX}/share/opengamepadui/opengamepad-ui.${ARCH}"}
GAMESCOPE_CMD=${GAMESCOPE_CMD:-gamescope -e -w 1920 -h 1080 -f --xwayland-count 2}
RUNTIME_DIR=${XDG_RUNTIME_DIR}

# Check to see if a URI is being sent
if [[ "$1" == ogui://* ]]; then
if ls /run/user/${UID}/opengamepadui/opengamepadui-0 >/dev/null 2>&1; then
echo "$1" >/run/user/${UID}/opengamepadui/opengamepadui-0
if ls "${RUNTIME_DIR}/opengamepadui/opengamepadui-0" >/dev/null 2>&1; then
echo "$1" >"${RUNTIME_DIR}/opengamepadui/opengamepadui-0"
exit 0
fi
fi

# Launch normally if gamescope is already running
if ls /run/user/${UID}/gamescope* >/dev/null 2>&1; then
if ls "${RUNTIME_DIR}"/gamescope* >/dev/null 2>&1; then
echo "Executing: ${OGUI_BIN}" "$@"
exec ${OGUI_BIN} "$@"
fi
Expand Down
Loading