Skip to main content

ubuntu kiosk with phidgethub using touch screen

#!/bin/bash
set -eu
# Enable pipefail if supported
if set -o | grep -q pipefail; then
    set -o pipefail
fi

read -p "Press [Enter] key to continue... stopping screen blanking (best effort for GNOME; harmless on Plasma)"
##############################################
# SECTION 1: POWER MANAGEMENT AND SCREEN BLANKING
##############################################
echo "=== SECTION 1: POWER MANAGEMENT AND SCREEN BLANKING ==="
# Stop screen blanking/suspend for kiosk session (no-op if schema absent)
sudo -u kiosk dbus-run-session gsettings set org.gnome.desktop.session idle-delay 0 || true
sudo -u kiosk dbus-run-session gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'nothing' || true
sudo -u kiosk dbus-run-session gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type 'nothing' || true

##############################################
# SECTION 2: INITIAL SYSTEM SETUP
##############################################
echo "=== SECTION 2: INITIAL SYSTEM SETUP ==="
apt update -yq

#wait for user input 
read -p "Press [Enter] key to continue... try gdm3, ssdm has issues with my app"

#######################################################
# SECTION 3: DESKTOP ENVIRONMENT INSTALLATION
#######################################################
echo "=== SECTION 3: DESKTOP ENVIRONMENT INSTALLATION ==="
apt install -yq kde-plasma-desktop chromium-browser xdotool unclutter sed curl libinput-tools net-tools ssh
systemctl enable ssh

#wait for user input 
read -p "Press [Enter] key to continue... plasma installed. installing other stuff for phidget and enabling ssh  "

##############################################
# SECTION 4: PHIDGET SETUP AND DEPENDENCIES
##############################################
echo "=== SECTION 4: PHIDGET SETUP AND DEPENDENCIES ==="
apt install -y chromium-browser sed xdotool unclutter
curl -fsSL https://www.phidgets.com/downloads/setup_linux | bash -
apt install -y libphidget22 phidget22networkserver
apt install -y libphidget22* phidget22*
apt install -y python3 npm nodejs 
ifconfig

##############################################
# SECTION 5: TOUCHSCREEN CONFIGURATION
##############################################
echo "=== SECTION 5: TOUCHSCREEN CONFIGURATION ==="
#setup touchscreen
echo -e "usbtouchscreen\nusbhid" |  tee /etc/modules-load.d/touchscreen.conf
modprobe usbtouchscreen
modprobe usbhid
lsmod | grep usb
lsmod | grep hid
lsmod | grep usb
lsmod | grep hid
dmesg | grep -i touch

##############################################
# SECTION 6: PHIDGET NETWORK SERVER SETUP
##############################################
echo "=== SECTION 6: PHIDGET NETWORK SERVER SETUP ==="
#setup phidget22networkserver 
DEFAULT_PHIDGET_PORT_VAR=""
DEFAULT_WEB_PORT_VAR=""
SERVICE_NAME="phidget22networkserver"
INIT_PATH="/etc/init.d/$SERVICE_NAME"
#v1 (works???)
if [[ "$(id -u)" -ne 0 ]]; then
  echo "Run this as root."
  exit 1
fi

cat > "$INIT_PATH" <<'EOF'
#!/bin/sh
### BEGIN INIT INFO
# Provides:          phidget22networkserver
# Required-Start:    $network $remote_fs
# Required-Stop:     $network $remote_fs
# Should-Start:      avahi avahi-daemon
# Should-Stop:       avahi avahi-daemon
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Phidget Network Server
# Description:       Phidget network server for accessing Phidgets over the network.
### END INIT INFO

sudo phidget22networkserver -c /etc/phidgets/phidget22networkserver.pc -D

EOF

#add ipv6 to phidget config??? { port: 8080 }   

chmod +x "$INIT_PATH"
update-rc.d "$SERVICE_NAME" defaults
service "$SERVICE_NAME" start
systemctl daemon-reload
systemctl enable "$SERVICE_NAME"
systemctl restart "$SERVICE_NAME"
systemctl status "$SERVICE_NAME"

#wait for user input 
read -p "Press [Enter] key to continue... this is usuially where shit breaks"

##############################################
# SECTION 7: KIOSK USER SETUP
##############################################
echo "=== SECTION 7: KIOSK USER SETUP ==="
# 1) Ensure kiosk exists
if ! id kiosk &>/dev/null; then adduser --disabled-password --gecos "" kiosk; fi
passwd -d kiosk || true
usermod -aG sudo kiosk

install -d -m 0755 /home/kiosk/.config/autostart
chown -R kiosk:kiosk /home/kiosk

read -p "Press [Enter] key to continue... touch works now, configuring GDM for Plasma Wayland"

# Force Wayland in GDM
sed -i 's/^#\?WaylandEnable=.*/WaylandEnable=true/' /etc/gdm3/custom.conf
grep WaylandEnable /etc/gdm3/custom.conf || true

read -p "Press [Enter] key to continue... setting autologin for GDM3 (Plasma Wayland)"

##############################################
# SECTION 8: GDM3 AUTOLOGIN CONFIGURATION
##############################################
echo "=== SECTION 8: GDM3 AUTOLOGIN CONFIGURATION ==="
# GDM3 Autologin to Plasma Wayland
if [ -f /etc/gdm3/custom.conf ]; then
  awk '
    BEGIN{inDaemon=0; hasALE=0; hasAL=0; hasDS=0}
    /^\[daemon\]/{inDaemon=1; print; next}
    /^\[/{ if(inDaemon && !(hasALE&&hasAL&&hasDS)){print "AutomaticLoginEnable=true\nAutomaticLogin=kiosk\nDefaultSession=plasmawayland.desktop"}; inDaemon=0; print; next}
    {
      if(inDaemon){
        if($0 ~ /^AutomaticLoginEnable=/){$0="AutomaticLoginEnable=true"; hasALE=1}
        if($0 ~ /^AutomaticLogin=/){$0="AutomaticLogin=kiosk"; hasAL=1}
        if($0 ~ /^DefaultSession=/){$0="DefaultSession=plasmawayland.desktop"; hasDS=1}
      }
      print
    }
    END{
      if(inDaemon && !(hasALE&&hasAL&&hasDS)){
        print "AutomaticLoginEnable=true"
        print "AutomaticLogin=kiosk"
        print "DefaultSession=plasmawayland.desktop"
      }
    }
  ' /etc/gdm3/custom.conf > /tmp/custom.conf && mv /tmp/custom.conf /etc/gdm3/custom.conf

  # Make sure AccountsService maps kiosk -> plasmawayland
  install -d -m 0755 /var/lib/AccountsService/users
  cat >/var/lib/AccountsService/users/kiosk <<'EOF'
[User]
XSession=plasmawayland
SystemAccount=false
EOF
  chmod 0644 /var/lib/AccountsService/users/kiosk

  echo "gdm3 shared/default-x-display-manager select gdm3" | debconf-set-selections
  systemctl restart gdm3
fi

##############################################
# SECTION 9: SDDM CONFIGURATION (ALTERNATIVE)
##############################################
echo "=== SECTION 9: SDDM CONFIGURATION (ALTERNATIVE) ==="
# SDDM Variant (Plasma-native)
if [ -f /etc/sddm.conf ]; then
  sed -i '/^\[Autologin\]/,/^\[/{s/^User.*/User=kiosk/;s/^Session.*/Session=plasmawayland/}' /etc/sddm.conf || true
  if ! grep -q '^\[Autologin\]' /etc/sddm.conf; then
    tee -a /etc/sddm.conf >/dev/null <<'EOF'

[Autologin]
User=kiosk
Session=plasmawayland
EOF
  fi
  systemctl restart sddm
fi

# Show resulting configs (best-effort)
[ -f /etc/gdm3/custom.conf ] && cat /etc/gdm3/custom.conf || true
[ -f /etc/sddm.conf ] && cat /etc/sddm.conf || true

read -p "Press [Enter] key to continue... creating Chromium autostart"

##############################################
# SECTION 10: CHROMIUM KIOSK AUTOSTART SETUP
##############################################
echo "=== SECTION 10: CHROMIUM KIOSK AUTOSTART SETUP ==="
# Create Chromium kiosk autostart for Plasma session
APP_URL="http://localhost:8080"
cat >/home/kiosk/.config/autostart/chromium-kiosk.desktop <<EOF
[Desktop Entry]
Type=Application
Name=Chromium Kiosk
Exec=/usr/bin/chromium-browser --ozone-platform=wayland --enable-features=UseOzonePlatform,WaylandWindowDecorations --no-first-run --no-default-browser-check --disable-session-crashed-bubble --disable-infobars --start-maximized --kiosk "$APP_URL"
X-GNOME-Autostart-enabled=true
X-KDE-AutostartScript=true
EOF
chown kiosk:kiosk /home/kiosk/.config/autostart/chromium-kiosk.desktop
chmod 0644 /home/kiosk/.config/autostart/chromium-kiosk.desktop

read -p "Press [Enter] key to continue... verifying Wayland toggles and kiosk autostart path"
##############################################
# SECTION 11: WAYLAND AND KIOSK VERIFICATION
##############################################
echo "=== SECTION 11: WAYLAND AND KIOSK VERIFICATION ==="
# Ensure WaylandEnable=false is commented if present
if grep -q '^WaylandEnable=false' /etc/gdm3/custom.conf; then
  sed -i 's/^WaylandEnable=false/#WaylandEnable=false/' /etc/gdm3/custom.conf
fi

# Make sure APP_URL uses http (Phidget web piece typically)
sed -i 's#https://localhost:8080#http://localhost:8080#g' /home/kiosk/.config/autostart/chromium-kiosk.desktop
chown kiosk:kiosk /home/kiosk/.config/autostart/chromium-kiosk.desktop

read -p "Press [Enter] key to continue... disabling touch gestures (GNOME keys; ignored on Plasma)"
##############################################
# SECTION 12: TOUCH GESTURE CONFIGURATION
##############################################
echo "=== SECTION 12: TOUCH GESTURE CONFIGURATION ==="
# Remove Wayland touch shortcuts (GNOME dconf; harmless if schema missing)
gsettings set org.gnome.desktop.peripherals.touchpad three-finger-swipe-enabled false || true
gsettings set org.gnome.desktop.peripherals.touchpad pinch-to-zoom false || true

read -p "Press [Enter] key to continue... applying KWin tweaks"
##############################################
# SECTION 13: KWIN TWEAKS AND FINAL SETUP
##############################################
echo "=== SECTION 13: KWIN TWEAKS AND FINAL SETUP ==="
kwriteconfig5 --file kwinrc --group "Global Shortcuts" --key _k_friendly_name "" || true
qdbus6 org.kde.KWin /KWin reconfigure || true

read -p "Press [Enter] key to continue... enabling kiosk service (if defined elsewhere)"
systemctl enable kiosk || true

read -p "Press [Enter] key to continue... restarting gdm3 to apply autologin"

##############################################
# SECTION 14: FINAL SYSTEM RESTART
##############################################
echo "=== SECTION 14: FINAL SYSTEM RESTART ==="
systemctl restart gdm3