#!/bin/zsh
setopt no_unset
xpra_display=${1#:}
shift
args=(
	--splash=no
	--daemon=no
	--opengl=no
	--dpi=96
	--pixel-depth=24

	# try to prevent invisible windows
	--border=$(hsl-to-rgb $(( (xpra_display*0.27460512339682597)%1 )) 1 $((2./3)) ),2
	--min-size=32x32
	# --max-size=
	--modal-windows=no

	--minimal=yes
        --notifications=no
        --cursors=yes
        --xsettings=yes
        --system-tray=no
        --bell=yes

        --opengl=no
        --mmap=yes
        --clipboard=no
        --pulseaudio=no
	--webcam=no
	--speaker=off
	--microphone=off

	--tray=no

	--keyboard-raw=yes
	
	##--keyboard-model thinkpad
	--keyboard-options=terminate:ctrl_alt_bksp,caps:ctrl_modifier,compose:ralt
	--keyboard-layouts=us
	--keyboard-variants=
)

# Hacky hack. Wait until xpra client resets the layout and then set the actual one.
(
	export DISPLAY=:$xpra_display XAUTHORITY=/run/Xauthority.$xpra_display
	until xmodmap -pke | grep -q 'keycode \+37 = Control'; do
		sleep 0.125 || break
		kill -0 $$ || exit 1
	done
	until xkb; do
		sleep 0.125 || break
		kill -0 $$ || exit 1
	done
) &!
exec xpra attach "$args[@]" "$@" :$xpra_display