#!/bin/sh
# ps3-session: the PS3 desktop's X session (ps3-desktop; started by /etc/profile.d/ps3-desktop.sh)
# The PS3 defaults (Openbox, tint2, GTK, PCManFM) come first among the system configuration
# directories; the user's own ~/.config files still win
export XDG_CONFIG_DIRS="/etc/xdg/ps3:${XDG_CONFIG_DIRS:-/etc/xdg}"
# The boot splash's last frame (ps3-splash) on the root window until the desktop window (PCManFM's,
# either version, or Xfce's) covers it; then the root goes back to plain blue, so its 8 MB
# picture doesn't stay in video memory (only if nothing else took the root picture over since:
# hsetroot keeps it in a retained client, freed with xkill; never longer than 2 minutes)
root_id() { xprop -root "$1" 2>/dev/null | sed -n 's/.*# //p'; }
splash=/usr/share/ps3-splash/desktop-splash.png
pm=
if [ -r $splash ] && command -v hsetroot >/dev/null; then
	hsetroot -solid '#14304d' -center $splash >/dev/null 2>&1 && pm=$(root_id ESETROOT_PMAP_ID)
else
	xsetroot -solid '#14304d'
fi
[ -n "$pm" ] && (t=0; until xwininfo -root -tree 2>/dev/null |
		grep -qE 'pcmanfm-desktop0|"pcmanfm" "Pcmanfm"|"xfdesktop" "Xfdesktop"' || [ $t -gt 120 ]; do
		sleep 1; t=$((t + 1)); done
	sleep 2
	if [ "$(root_id ESETROOT_PMAP_ID)" = "$pm" ]; then
		xsetroot -solid '#14304d'
		xprop -root -remove ESETROOT_PMAP_ID
		[ "$(root_id _XROOTPMAP_ID)" = "$pm" ] && xprop -root -remove _XROOTPMAP_ID
		xkill -id "$pm" >/dev/null 2>&1
	fi) &
# gvfs and the tray applets use the user bus (dbus-user-session). Openbox: tint2 panel (jgmenu, tray, clock),
# PCManFM desktop icons, network and volume applets, notifications, polkit agent. Bluetooth:
# blueman from the menu, no resident tray (~60 MB of Python). GTK apps don't load the GL
# driver just to probe it (GDK_GL=disable: RAM and GART for nothing; games use GL directly).
export GDK_GL=disable
# The helpers run in ps3-desktop.slice, which game mode freezes and pushes out of RAM while a
# game runs; openbox stays with X in ps3-display.slice.
# Every game in the menu starts in game mode: its entry through ps3-game, written at each login
# into the runtime directory, which comes first in XDG_DATA_DIRS (ps3-game-menu)
if [ -n "${XDG_RUNTIME_DIR:-}" ]; then
	export XDG_DATA_DIRS="$XDG_RUNTIME_DIR/ps3-game-menu:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
	/usr/lib/ps3/desktop/ps3-game-menu "$XDG_RUNTIME_DIR/ps3-game-menu" > /dev/null 2>&1 &
fi
# PS3_GAME_MODE (game mode: on, pads or off), which ps3-game and ps3-game-detect read from each
# game's own environment: /etc/environment's (pam_env at the login) or ~/.profile's reach this
# session by themselves; a value in ~/.config/environment.d (the user manager's) wins over
# /etc/environment's but not over ~/.profile's. A menu entry or a command line sets it per game.
m=$(systemctl --user show-environment 2>/dev/null | sed -n 's/^PS3_GAME_MODE=//p')
e=$(sed -n 's/^[[:space:]]*\(export[[:space:]]\{1,\}\)\{0,1\}PS3_GAME_MODE=["'"'"']\{0,1\}\([^"'"'"' ]*\).*/\2/p' /etc/environment 2>/dev/null | tail -1)
[ -n "$m" ] && [ "${PS3_GAME_MODE:-}" = "$e" ] && export PS3_GAME_MODE="$m"
unset m e
# The user bus and the services it starts need to know the display, and the login's PATH: the user
# manager's own has no /usr/games, where Debian puts most games (a game started from PCManFM, or
# anything the manager starts, was "not found")
case ":$PATH:" in *:/usr/games:*) ;; *) PATH="$PATH:/usr/local/games:/usr/games" ;; esac
export PATH
systemctl --user import-environment DISPLAY XAUTHORITY XDG_CONFIG_DIRS XDG_DATA_DIRS PATH
dbus-update-activation-environment --systemd DISPLAY XAUTHORITY XDG_CONFIG_DIRS XDG_DATA_DIRS PATH 2>/dev/null
d() { systemd-run --user --scope --slice=ps3-desktop.slice -q "$@" & }
# PCManFM 1.4 reads its desktop settings (the wallpaper) only from ~/.config, never from
# XDG_CONFIG_DIRS (an strace, 2026-10-23): a home without them gets ours, else PCManFM writes its
# black default there on the first start. The user's own settings win from then on.
c=${XDG_CONFIG_HOME:-$HOME/.config}/pcmanfm/default/desktop-items-0.conf
[ -e "$c" ] || install -D -m 644 /etc/xdg/ps3/pcmanfm/default/desktop-items-0.conf "$c"
# The startup chime, through the default device at the user's volume (PS3_STARTUP_CHIME=off: none).
# It also gives the volume applet something to set: snd_ps3 has no volume control, and alsa-lib's
# softvol makes "PCM" only when the device is first opened (ps3-system's ps3-sound-volume opens it
# silently at boot as well)
[ "${PS3_STARTUP_CHIME:-on}" = off ] || [ ! -r /usr/share/sounds/walnut/startup.wav ] ||
	d aplay -q /usr/share/sounds/walnut/startup.wav
d pcmanfm --desktop; d tint2; d nm-applet; d pnmixer; d lxpolkit; d dunst
# Game mode by itself for any fullscreen 3D program, however it was started (ps3-game-detect;
# it watches X and ends with it)
systemctl --user restart --no-block ps3-game-detect.service
exec openbox
