#!/bin/sh

# hdmi.sh
# 2014-01-09
# by Gernot Walzl

# HDMI-out script for NVIDIA cards
# Starts KDE on a separate X screen

# ftp://download.nvidia.com/XFree86/Linux-x86/290.10/README/xconfigoptions.html
# ftp://download.nvidia.com/XFree86/gpu-hdmi-audio-document/gpu-hdmi-audio.html

# /etc/X11/xorg.conf
#
#Section "Monitor"
#    Identifier "MONITOR_HDMI"
#    VertRefresh 60
#EndSection
#
#Section "Device"
#    Identifier "NVIDIA_HDMI"
#    Driver "nvidia"
#    Option "ConnectedMonitor" "DFP-1"
#    Option "UseDisplayDevice" "DFP-1"
#    Option "UseEdidDpi" "false"
#    Option "DPI" "125 x 125"
#EndSection
#
#Section "Screen"
#    Identifier "SCREEN_HDMI"
#    Device "NVIDIA_HDMI"
#    Monitor "MONITOR_HDMI"
#    DefaultDepth 24
#EndSection
#
#Section "ServerLayout"
#    Identifier "HDMI"
#    Screen "SCREEN_HDMI"
#    InputDevice "Mouse1" "CorePointer"
#    InputDevice "Keyboard1" "CoreKeyboard"
#EndSection

CONNECTED=$(xrandr -q | grep " connected " | grep HDMI | wc -l)
if [ $CONNECTED -eq 0 ]; then
  echo "Error: HDMI not connected."
  sleep 3
  exit 1
fi

init () {
  if [ -f ~/.asoundrc ]; then
    mv ~/.asoundrc ~/.asoundrc.orig
  fi
  cat > ~/.asoundrc <<EOF
pcm.!default {
  type hw
  card 1
  device 7
}
EOF
}

clean () {
  if [ -f ~/.asoundrc.orig ]; then
    mv ~/.asoundrc.orig ~/.asoundrc
  else
    rm -f ~/.asoundrc
  fi
}

init
trap clean EXIT INT TERM
startx -- :1 -layout HDMI