#!/usr/bin/env bash # CaptainTicket narrowcasting: turn a Raspberry Pi into a tckt.tv kiosk. # # curl -fsSL https://tckt.tv/pi.sh | sudo bash # curl -fsSL https://tckt.tv/pi.sh | sudo bash -s -- --rotate 90 # # Works on Raspberry Pi OS Bookworm and Trixie, with or without a desktop: # - with a desktop (labwc, Wayfire or X11) Chromium starts from the # desktop's autostart after an automatic login; # - on Lite (no desktop) it installs the `cage` kiosk compositor and starts # it from an automatic console login on tty1. # Chromium runs fullscreen in a restart loop, so a crash brings it back in a # few seconds; a crashed compositor restarts through the login. Screen # blanking is switched off. The pairing token lives in Chromium's own # profile, so a reboot or a re-run of this script never needs a new pairing. # # Options: # --url URL page to show (default https://tckt.tv) # --user NAME account that runs the kiosk (default: the sudo user, # else the first regular user) # --rotate DEG 0, 90, 180 or 270 for a portrait or upside-down screen # --reboot HH:MM reboot every night at this time (default: never) # --uninstall remove the kiosk again (automatic login stays on) # --no-reboot do not reboot at the end # # Settings end up in /etc/tckt-kiosk.conf; re-run the script to change them. set -euo pipefail # Everything runs from main() at the very end, so a download that breaks # off half-way executes nothing. KIOSK_URL="https://tckt.tv" KIOSK_USER="" KIOSK_ROTATE="0" KIOSK_REBOOT_AT="" UNINSTALL=0 REBOOT_AFTER=1 LAUNCHER=/usr/local/bin/tckt-kiosk CONFIG=/etc/tckt-kiosk.conf CRON_FILE=/etc/cron.d/tckt-kiosk MARK_BEGIN="# >>> tckt-kiosk >>>" MARK_END="# <<< tckt-kiosk <<<" log() { printf '\033[1;34m==>\033[0m %s\n' "$*"; } warn() { printf '\033[1;33m!!\033[0m %s\n' "$*" >&2; } die() { printf '\033[1;31mxx\033[0m %s\n' "$*" >&2 exit 1 } usage() { cat <<'EOF' Usage: curl -fsSL https://tckt.tv/pi.sh | sudo bash -s -- [options] --url URL page to show (default https://tckt.tv) --user NAME account that runs the kiosk (default: the sudo user) --rotate DEG 0, 90, 180 or 270 for a portrait or upside-down screen --reboot HH:MM reboot every night at this time (default: never) --uninstall remove the kiosk again --no-reboot do not reboot at the end EOF exit "${1:-0}" } parse_args() { while [ $# -gt 0 ]; do case "$1" in --url) KIOSK_URL="${2:?--url needs a value}"; shift 2 ;; --user) KIOSK_USER="${2:?--user needs a value}"; shift 2 ;; --rotate) KIOSK_ROTATE="${2:?--rotate needs a value}"; shift 2 ;; --reboot) KIOSK_REBOOT_AT="${2:?--reboot needs a value}"; shift 2 ;; --uninstall) UNINSTALL=1; shift ;; --no-reboot) REBOOT_AFTER=0; shift ;; -h | --help) usage 0 ;; *) warn "Unknown option: $1"; usage 1 ;; esac done case "$KIOSK_URL" in https://* | http://*) ;; *) die "--url must start with https:// or http://" ;; esac case "$KIOSK_URL" in *[[:space:]\"\'\`\$\\]*) die "--url contains characters that are not allowed" ;; esac case "$KIOSK_ROTATE" in 0 | 90 | 180 | 270) ;; *) die "--rotate must be 0, 90, 180 or 270" ;; esac if [ -n "$KIOSK_REBOOT_AT" ] && ! [[ "$KIOSK_REBOOT_AT" =~ ^([01][0-9]|2[0-3]):[0-5][0-9]$ ]]; then die "--reboot must be a time like 04:30" fi } resolve_user() { if [ -z "$KIOSK_USER" ]; then if [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != "root" ]; then KIOSK_USER="$SUDO_USER" else KIOSK_USER="$(getent passwd | awk -F: '$3 >= 1000 && $3 < 60000 { print $1; exit }')" fi fi [ -n "$KIOSK_USER" ] || die "No regular user found; pass one with --user NAME" [ "$KIOSK_USER" != "root" ] || die "Do not run the kiosk as root; pass --user NAME" getent passwd "$KIOSK_USER" >/dev/null || die "User '$KIOSK_USER' does not exist" KIOSK_HOME="$(getent passwd "$KIOSK_USER" | cut -d: -f6)" KIOSK_GROUP="$(id -gn "$KIOSK_USER")" } has_desktop() { command -v labwc >/dev/null 2>&1 || command -v wayfire >/dev/null 2>&1 || command -v startlxde-pi >/dev/null 2>&1 || command -v lxsession >/dev/null 2>&1 } # Write a file as the kiosk user (so they keep owning their own config). write_user_file() { local path="$1" runuser -u "$KIOSK_USER" -- mkdir -p "$(dirname "$path")" cat >"$path" chown "$KIOSK_USER:$KIOSK_GROUP" "$path" } # Remove a marker block (written by an earlier run) from a file. strip_block() { local path="$1" [ -f "$path" ] || return 0 sed -i "/^${MARK_BEGIN}\$/,/^${MARK_END}\$/d" "$path" } install_packages() { log "Installing Chromium$1" export DEBIAN_FRONTEND=noninteractive # "$CONFIG" <"$LAUNCHER" <<'EOF' #!/usr/bin/env bash # Started by the desktop autostart or by cage. Keeps Chromium running on the # kiosk URL; installed by https://tckt.tv/pi.sh. KIOSK_URL="https://tckt.tv" KIOSK_ROTATE="0" # shellcheck disable=SC1091 [ -r /etc/tckt-kiosk.conf ] && . /etc/tckt-kiosk.conf BROWSER="$(command -v chromium || command -v chromium-browser)" PROFILE="$HOME/.local/share/tckt-kiosk/chromium" mkdir -p "$PROFILE" # Screen blanking and the mouse pointer. if [ -n "${DISPLAY:-}" ] && [ -z "${WAYLAND_DISPLAY:-}" ]; then xset s off s noblank -dpms 2>/dev/null || true command -v unclutter >/dev/null && { unclutter -idle 1 -root & } fi # Rotation (Wayland compositors only; X11 uses the Screen Configuration app). if [ "$KIOSK_ROTATE" != "0" ] && [ -n "${WAYLAND_DISPLAY:-}" ] && command -v wlr-randr >/dev/null; then OUTPUT="" for _ in 1 2 3 4 5; do OUTPUT="$(wlr-randr 2>/dev/null | awk '/^[^ ]/ { print $1; exit }')" [ -n "$OUTPUT" ] && break sleep 1 done [ -n "$OUTPUT" ] && wlr-randr --output "$OUTPUT" --transform "$KIOSK_ROTATE" || true fi # Give the network up to 30 seconds. The player plays its cached playlist # offline, so after that Chromium starts anyway. for _ in $(seq 1 30); do curl -fsS -o /dev/null --max-time 3 "$KIOSK_URL" 2>/dev/null && break sleep 1 done OZONE=() [ -n "${WAYLAND_DISPLAY:-}" ] && OZONE=(--ozone-platform=wayland) while true; do # A killed Chromium marks its profile as crashed and would show a # "restore pages?" bubble over the screen on the next start. PREFS="$PROFILE/Default/Preferences" if [ -f "$PREFS" ]; then sed -i 's/"exited_cleanly":false/"exited_cleanly":true/; s/"exit_type":"[^"]*"/"exit_type":"Normal"/' "$PREFS" fi "$BROWSER" "${OZONE[@]}" \ --user-data-dir="$PROFILE" \ --kiosk \ --start-fullscreen \ --noerrdialogs \ --disable-infobars \ --disable-session-crashed-bubble \ --disable-features=Translate,TranslateUI \ --no-first-run \ --no-default-browser-check \ --password-store=basic \ --autoplay-policy=no-user-gesture-required \ --check-for-update-interval=31536000 \ --overscroll-history-navigation=0 \ --disable-pinch \ "$KIOSK_URL" sleep 3 done EOF chmod 755 "$LAUNCHER" } setup_desktop() { log "Desktop found: starting the kiosk from the desktop autostart" # labwc (the Raspberry Pi OS desktop since late 2024). Chromium's kiosk # window covers the panel; raspi-config below switches blanking off. if command -v labwc >/dev/null 2>&1; then write_user_file "$KIOSK_HOME/.config/labwc/autostart" </dev/null 2>&1; then local ini="$KIOSK_HOME/.config/wayfire.ini" [ -f "$ini" ] || write_user_file "$ini" >"$ini" fi grep -q '^\[idle\]' "$ini" || printf '\n[idle]\ndpms_timeout = -1\nscreensaver_timeout = -1\n' >>"$ini" chown "$KIOSK_USER:$KIOSK_GROUP" "$ini" fi # X11 (LXDE-pi). if command -v lxsession >/dev/null 2>&1 || command -v startlxde-pi >/dev/null 2>&1; then write_user_file "$KIOSK_HOME/.config/lxsession/LXDE-pi/autostart" </dev/null 2>&1; then raspi-config nonint do_boot_behaviour B4 # desktop, logged in automatically raspi-config nonint do_blanking 1 || true # 1 = blanking off else warn "raspi-config not found: switch on automatic desktop login yourself" fi } setup_lite() { log "No desktop found: starting the kiosk in cage on tty1" local profile="$KIOSK_HOME/.bash_profile" [ -f "$profile" ] || write_user_file "$profile" <<'EOF' # Load .profile as a login shell would without this file. [ -f "$HOME/.profile" ] && . "$HOME/.profile" EOF strip_block "$profile" cat >>"$profile" </dev/null 2>&1; then raspi-config nonint do_boot_behaviour B2 # console, logged in automatically fi # raspi-config only knows the first user; write the override for ours. install -d /etc/systemd/system/getty@tty1.service.d cat >/etc/systemd/system/getty@tty1.service.d/autologin.conf <"$CRON_FILE" chmod 644 "$CRON_FILE" else rm -f "$CRON_FILE" fi } uninstall() { log "Removing the CaptainTicket kiosk for $KIOSK_USER" rm -f "$LAUNCHER" "$CONFIG" "$CRON_FILE" rm -f /etc/systemd/system/getty@tty1.service.d/autologin.conf strip_block "$KIOSK_HOME/.bash_profile" strip_block "$KIOSK_HOME/.config/lxsession/LXDE-pi/autostart" local labwc="$KIOSK_HOME/.config/labwc/autostart" strip_block "$labwc" [ -f "$labwc" ] && ! grep -q '[^[:space:]]' "$labwc" && rm -f "$labwc" [ -f "$KIOSK_HOME/.config/wayfire.ini" ] && sed -i '/^tckt_kiosk = /d' "$KIOSK_HOME/.config/wayfire.ini" log "Done. The Chromium profile (with the pairing) is kept in $KIOSK_HOME/.local/share/tckt-kiosk." log "Reboot to return to the normal desktop or console." } main() { parse_args "$@" [ "$(id -u)" -eq 0 ] || die "Run as root: curl -fsSL https://tckt.tv/pi.sh | sudo bash" command -v apt-get >/dev/null 2>&1 || die "This script needs Raspberry Pi OS (or another Debian)" resolve_user if [ "$UNINSTALL" -eq 1 ]; then uninstall exit 0 fi local extra="" [ "$KIOSK_ROTATE" = "0" ] || extra="wlr-randr" if has_desktop; then command -v lxsession >/dev/null 2>&1 && extra="$extra unclutter" install_packages "" "${extra# }" else install_packages " and cage" "cage ${extra}" fi write_config write_launcher if has_desktop; then setup_desktop else setup_lite fi setup_reboot_cron log "Kiosk ready for user $KIOSK_USER, showing $KIOSK_URL" log "After the reboot the screen shows a six-character code: enter it under" log "Dashboard -> Narrowcasting -> Scherm toevoegen." if [ "$REBOOT_AFTER" -eq 1 ]; then log "Rebooting in 10 seconds (Ctrl+C to stay)" sleep 10 reboot fi } main "$@"