Skip to content

Instantly share code, notes, and snippets.

@lf-araujo
Last active September 27, 2020 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lf-araujo/1879c0ffbf080ce18bc9465b0c7b957c to your computer and use it in GitHub Desktop.
Save lf-araujo/1879c0ffbf080ce18bc9465b0c7b957c to your computer and use it in GitHub Desktop.
hdmi.sh
#!/bin/bash
# STEAM DE - Script for starting Steam at login
# Copyright (C) 2012 Thomaz de Oliveira dos Reis <thor27@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
## Global vars
export XFWM4_PID
export STEAM_PID
export STEAM_DE_PID=$$
export WM_NAME
## Define logging function
INFO(){ echo "INFO: $*" | tee -a /tmp/steam-de.$UID.log; }
WARN(){ echo "WARN: $*" | tee -a /tmp/steam-de.$UID.log; }
ERRO(){ echo "ERRO: $*" | tee -a /tmp/steam-de.$UID.log; exit 1; }
function cmd_exist(){
command -v "$1" &> /dev/null;
}
function get_path(){
command -v "$1"
}
function steam_shutdown(){
steam -shutdown &
count=0
while [ -d /proc/$OLD_STEAM_PID ]; do
sleep 0.1
((count++==100)) && break
done
}
function steam_stopped(){
if [ -f $HOME/.steampid ]; then
read OLD_STEAM_PID < /home/luis/.var/app/com.valvesoftware.Steam/.steampid
if [ ! -d /proc/$OLD_STEAM_PID ]; then
unset OLD_STEAM_PID
rm -f /home/luis/.var/app/com.valvesoftware.Steam/.steampid
else
steam_shutdown
fi
fi
PIDS_TO_KILL=(
$OLD_STEAM_PID
$(pidof steam)
$(pidof steam.sh)
$(pidof MainThrd)
)
((${#PIDS_TO_KILL[@]} == 0)) && return
# Stop all running steam process
INFO "Pids to kill: ${PIDS_TO_KILL[*]}"
for pid in "${PIDS_TO_KILL[@]}"; do
while ((PID > 1)) && [ -d "/proc/$PID" ]; do
kill $pid &> /dev/null && sleep 1
kill -s 9 $pid &> /dev/null
done
done
}
function check_steam_window(){
wmctrl -l | tr -s " " | cut -f4- -d" " | grep '^Steam$'
}
function disable_joysticks() {
find /dev/input -name "js*" | \
while read -r dev; do
# Get the name of the joystick model
name="`udevadm info --query=property --name=$dev | grep ID_VENDOR_ENC | cut -c15- -` `udevadm info --query=property --name=$dev | grep ID_MODEL_ENC | cut -c14- -`"
# Escape \ sequences
name="`echo -e "$name"`"
# Disable the joystick
xinput --list | grep "$name" | grep -Pwo "id=[0-9]*" | grep -Pwo "[0-9]*" | xargs xinput --disable
done
}
function kill_OpenBox(){
openbox --exit
}
function kill_XFWM4(){
[ -z "$XFWM4_PID" ] && return
[ ! -d "/proc/$XFWM4_PID" ] && return
kill $XFWM4_PID &> /dev/null && sleep 1
kill -s 9 $XFWM4_PID &> /dev/null
}
function steam_de_exit(){
INFO "Steam-DE cooling"
steam_stopped
case $WM_NAME in
xfwm4) kill_XFWM4 ;;
openbox) kill_OpenBox ;;
esac
INFO "Steam-DE stopped"
exit 0
}
trap steam_de_exit SIGINT SIGTERM EXIT
## Main
INFO "Initialization"
# Vars to switch logic
# if supported things found
export STEAM STEAM_NATIVE
export OPTIRUN PRIMUSRUN
export NVIDIA_SETTINGS
export GNOME_SETTINGS_DAEMON
export PREFIX=""
export PROGRAM="flatpak run com.valvesoftware.Steam"
export STEAM_ARGS="-tenfoot -enableremotecontrol"
if cmd_exist steam; then
STEAM="$(get_path steam)"
INFO "Check: steam found"
else
ERRO "Steam not found in PATH"
fi
if cmd_exist xfwm4; then
WM_NAME=xfwm4
STATUS="found"
else
STATUS="missing"
fi
INFO "Check: xfwm4 $STATUS"
if cmd_exist openbox; then
WM_NAME=openbox
STATUS="found"
else
STATUS="missing"
fi
INFO "Check: openbox $STATUS"
if [ -z "$WM_NAME" ]; then
ERRO "Can't find supported WM in PATH"
fi
if cmd_exist steam-native; then
STEAM_NATIVE="$(get_path steam-native)"
STATUS="found"
else
STATUS="missing"
fi
INFO "Check: steam-native $STATUS"
if cmd_exist optirun; then
OPTIRUN="$(get_path optirun)"
STATUS="found"
else
STATUS="missing"
fi
INFO "Check: optirun $STATUS"
if cmd_exist primusrun; then
PRIMUSRUN="$(get_path primusrun)"
STATUS="found"
else
STATUS="missing"
fi
INFO "Check: primusrun $STATUS"
if cmd_exist nvidia-settings; then
NVIDIA_SETTINGS="$(get_path nvidia-settings)"
STATUS="found"
else
STATUS="missing"
fi
INFO "Check: nvidia-settings $STATUS"
if cmd_exist gnome-settings-daemon; then
GNOME_SETTINGS_DAEMON="$(get_path gnome-settings-daemon)"
STATUS="found"
else
STATUS="missing"
fi
INFO "Check: gnome-settings-daemon $STATUS"
# if [ ! -z "$STEAM_NATIVE" ]; then
# INFO "Use steam-native instead of steam"
# PROGRAM="$STEAM_NATIVE"
# else
# INFO "Use steam instead of steam-native"
# PROGRAM="$STEAM"
# fi
if [ ! -z "$OPTIRUN" ] || [ ! -z "$PRIMUSRUN" ]; then
if [ ! -z "$PRIMUSRUN" ]; then
INFO "Use primusrun instead of optirun"
export vblank_mode=0
PREFIX="$OPTIRUN -b primus"
else
INFO "Use optirun instead of primusrun"
PREFIX="$OPTIRUN"
fi
fi
# Disable xinput for joysticks
if cmd_exist xinput; then
INFO "Disable xinput for jousticks"
disable_joysticks
fi
case $WM_NAME in
xfwm4)
INFO "Start XFWM4 Window manager"
xfwm4 --replace --daemon --compositor=off
XFWM4_PID="$(pidof xfwm4)"
;;
openbox)
INFO "Start OpenBox Window manager"
openbox --replace &
;;
esac
if [ ! -z "$NVIDIA_SETTINGS" ] || [ ! -z "$GNOME_SETTINGS_DAEMON" ]; then
INFO "Start basic video settings process"
if [ ! -z "$NVIDIA_SETTINGS" ]; then
INFO "Run nvidia-settings"
nvidia-settings -l
fi
if [ ! -z "$GNOME_SETTINGS_DAEMON" ]; then
INFO "Run gnome-settings-daemon"
gnome-settings-daemon &
fi
fi
INFO "Ensure that steam stopped"
steam_stopped
export SDL_VIDEO_MINIMIZE_ON_FOCUS_LOSS=0
INFO "Disable: DPMS"
xset -dpms
INFO "Disable: screen blanking"
xset s off
# Trying to pass video to external monitor
IS_HDMI_CONNECTED=`cat /sys/class/drm/card0/*HDMI*/status | sed -n 2p`
HDMI=`xrandr --prop | grep "[^dis]connected" | cut --delimiter=" " -f1 | sed -n 2p`
PRIMARY_MONITOR=`xrandr --prop | grep "[^dis]connected" | cut --delimiter=" " -f1 | sed -n 1p`
USER_NAME=`who | grep "(:0)" | cut -f 1 -d ' '`
USER_ID=`id -u $USER_NAME`
if echo $IS_HDMI_CONNECTED | grep "connected"; then
export PULSE_SERVER="unix:/run/user/"$USER_ID"/pulse/native"
xrandr --output $PRIMARY_MONITOR --off
xrandr --output $HDMI --preferred
pulseaudio -D
sudo -u $USER_NAME pactl --server $PULSE_SERVER set-card-profile 0 output:hdmi-stereo-extra1
pavucontrol
INFO "Trying to pass to external HDMI video and sound"
fi
INFO "Execute Steam"
INFO "Exec line: $PREFIX $PROGRAM $STEAM_ARGS"
if [ ! -z "$PREFIX" ]; then
$PREFIX $PROGRAM $STEAM_ARGS &
else
$PROGRAM $STEAM_ARGS &
fi
INFO "Wait until steam window will be visible"
while ! check_steam_window; do
sleep 0.5
read STEAM_PID < /home/luis/.var/app/com.valvesoftware.Steam/.steampid
if [ ! -d "/proc/$STEAM_PID" ]; then
zenity --error --text "Ops! Steam had some trouble to run. That can be a problem with steam itself or with your configuration."
ERRO "Ops! Steam had some trouble to run. That can be a problem with steam itself or with your configuration."
fi
done
INFO "Steam working"
INFO "Wait while steam running"
# self kill on steam exit
{
while [ -d "/proc/$STEAM_PID" ]; do sleep 1; done
kill $STEAM_DE_PID &> /dev/null
pkexec prime-select intel
} &
while check_steam_window; do
sleep 10
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment