#!/bin/sh
# Start the lock screen: its own X server on the compositor's privileged
# socket, and the Windows-style greeter on it, in lock mode for user $1.
#
# Started by sg-lockd, whose pipes are this script's stdin and stdout; the
# greeter inherits them and speaks the lock protocol on them. Nothing else may
# hold them open, so Xwayland gets /dev/null.
#
# The X server is separate from the user session's on purpose: while locked the
# compositor routes input only to privileged clients, so the user session's X
# server receives no key events at all -- there is nothing for a keylogger in
# it to see, frozen or not (ADR 0009).
set -u
SG_LIB="${SG_LIB:-/usr/lib/stained-glass}"
# shellcheck source=lib/sg-common.sh
. "$SG_LIB/sg-common.sh"
sg_wine_env
unset WAYLAND_DISPLAY SG_WINSTATION

user="${1:?user}"
N="${SG_LOCK_DISPLAY_NUM:-99}"
while [ -e "/tmp/.X${N}-lock" ] || [ -e "/tmp/.X11-unix/X${N}" ]; do N=$((N + 1)); done

# Only this account may connect to it: an Xwayland started without -auth
# admits every local account, and any program in any session could then type
# into this lock screen (XTEST) or read it (XGetImage).
XDIR=$(mktemp -d "${TMPDIR:-/tmp}/sg-lock-XXXXXX") || { exit 1; }
sg_x_cookie "$XDIR/Xauthority" || { rm -rf "$XDIR"; exit 1; }
WAYLAND_DISPLAY="${SG_LOCK_PRIV:?SG_LOCK_PRIV}" Xwayland ":$N" -noreset -auth "$XDIR/Xauthority" -nolisten tcp \
    </dev/null >/dev/null 2>&1 &
_w=0
while [ ! -e "/tmp/.X11-unix/X$N" ] && [ "$_w" -lt 100 ]; do sleep 0.1; _w=$((_w + 1)); done

DISPLAY=":$N"
XAUTHORITY="$XDIR/Xauthority"
export DISPLAY XAUTHORITY
# Not exec: the cookie goes when the lock screen does.
# SG_LOCK_MODE=sas: the security screen (Ctrl+Alt+Del) instead of the lock.
mode=/lock
[ "${SG_LOCK_MODE:-}" = sas ] && mode=/sas
# at the user's display scale (sg-lockd hands it over: SG_LOCK_SCALE), else
# the one the screen recommends: on a high-resolution screen it was a small
# box; at the screen's 175% when the user had picked 150% (sg-common.sh)
sg_lock_scale x
wine "${SG_LIBEXEC:-/usr/libexec/stained-glass}/sg-greeter64.exe" "$mode" "$user" 2>>"${SG_LOG_DIR:-/tmp}/lock-ui.log"
rc=$?
rm -rf "$XDIR"
exit $rc
