Skip to content

Commit

Permalink
feat(deck): Implement GNOME/Steam session switching
Browse files Browse the repository at this point in the history
Adapted from ChimeraOS
  • Loading branch information
EyeCantCU committed Aug 2, 2023
1 parent 64f4a17 commit af950ff
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
50 changes: 50 additions & 0 deletions system_files/deck/gnome/usr/bin/gnome-session-oneshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash

set -e

die() { echo >&2 "!! $*"; exit 1; }

SENTINEL_FILE="steamos-session-select"

# If we proceed, execute this
CHAINED_SESSION="/usr/bin/gnome-session"
# If we decide the sentinel is consumed, execute this command instead and fail
RESTORE_SESSION=(/usr/bin/steamos-session-select) # No arguments restores the session

# Find or check config sentinel
function check_sentinel()
{
if [[ -z ${HOME+x} ]]; then
echo >&2 "$0: No \$HOME variable!"
# Rather than break we'll just launch plasma and hope for the best?
return 0
fi

local config_dir="${XDG_CONF_DIR:-"$HOME/.config"}"
(
cd "$HOME"
cd "$config_dir"
sentinel_value="$(cat "$SENTINEL_FILE")"
if [[ "$sentinel_value" == "wayland" ]]; then
echo "/usr/bin/gnome-session"
else
return 1
fi
rm "$SENTINEL_FILE"

) || return 1 # If we couldn't read the value or it wasn't what we wanted

# Found value and removed it, we're good to continue
return 0
}

if CONFIGURED_CHAINED_SESSION=$(check_sentinel); then
# We found and consumed the oneshot sentinel, proceed to launch plasma
echo >&2 "$0: Found and removed sentinel file for one-shot desktop, proceeding to launch"
exec "$CONFIGURED_CHAINED_SESSION"
else
echo >&2 "$0: Sentinel value not found, executing session-select to restore session"
"${RESTORE_SESSION[@]}" || echo >&2 "$0: !! Failed to restore previous session, executing chained session"
# Session restore should've stopped us, if it is broken at least let plasma continue to open
exec "$CHAINED_SESSION"
fi
90 changes: 90 additions & 0 deletions system_files/deck/gnome/usr/bin/steamos-session-select
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#! /usr/bin/bash

set -e

die() { echo >&2 "!! $*"; exit 1; }

# File this script will modify, in addition to (potentially) the per-user sentinel file
CONF_FILE="/etc/sddm.conf.d/zz-steamos-autologin.conf"

SENTINEL_FILE="steamos-session-select"

# For sanity this shipped file must be present, to ensure we're still on a normal-looking steamos setup.
CHECK_FILE="/etc/sddm.conf.d/steamos.conf"

session="${1:-gamescope}"
session_type="wayland"

session_launcher="gamescope-session"
create_sentinel=""

if [[ "$2" == "--sentinel-created" ]]; then
SENTINEL_CREATED=1
session_type="wayland"
fi

# Update config sentinel
if [[ -z $SENTINEL_CREATED ]]; then
[[ $EUID == 0 ]] && die "Running $0 as root is not allowed"

[[ -n ${HOME+x} ]] || die "No \$HOME variable"
config_dir="${XDG_CONF_DIR:-"$HOME/.config"}"
session_type=$(
cd "$HOME"
mkdir -p "$config_dir"
cd "$config_dir"
if [[ -f "steamos-session-type" ]]; then
cp steamos-session-type "$SENTINEL_FILE"
else
echo "wayland" > "$SENTINEL_FILE"
fi
cat "$SENTINEL_FILE"
)

# clear steam game desktop shortcut clutter
DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}
grep --files-with-matches "Exec=steam steam://rungameid/" ${DATA_HOME}/applications/* | tr '\n' '\0' | xargs -0 -I {} rm {} || true

# If we were executed as a session user and then re-execute as root below, we don't want to set root's sentinel too
export SENTINEL_CREATED=1
fi

# We use "plasma" as "desktop" to hook up to SteamOS's scripts
case "$session" in
plasma-wayland-persistent)
session_launcher="gnome-session"
;;
plasma-x11-persistent)
session_launcher="gnome-session"
;;
desktop|plasma)
session_launcher="gnome-session-oneshot"
create_sentinel=1
;;
gamescope)
session_launcher="gamescope-session"
create_sentinel=1
;;
*)
echo >&2 "!! Unrecognized session '$session'"
exit 1
;;
esac

echo "Updated user selected session to $session_launcher"

# Become root
if [[ $EUID != 0 ]]; then
exec pkexec "$(realpath $0)" "$session" --sentinel-created
exit 1
fi

{
echo "[Autologin]"
echo "Session=$session_launcher"
} > "$CONF_FILE"

echo "Updated system autologin session to $session_launcher"
systemctl reset-failed sddm
systemctl restart sddm
echo "Restarted SDDM"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Desktop Entry]
Name=GNOME on Wayland (single time session)
Comment=This session logs you into GNOME one time
Exec=/usr/bin/gnome-session-oneshot
TryExec=/usr/bin/gnome-session-oneshot
Type=Application
DesktopNames=GNOME
X-GDM-SessionRegisters=true

0 comments on commit af950ff

Please sign in to comment.