Skip to content

Instantly share code, notes, and snippets.

@Karry
Last active August 22, 2019 22:33
Show Gist options
  • Save Karry/7345106 to your computer and use it in GitHub Desktop.
Save Karry/7345106 to your computer and use it in GitHub Desktop.
Purpose of this script is to monitor HDMI state on my Raspberry PI and start XBMC / Kodi when output is plugged in and terminate it when it is unplugged. It is tested with Raspbian distribution and XBMC / Kodi compiled from git.
#!/bin/bash
## DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
## Version 2, December 2004
##
## Copyright (C) 2013 Lukáš Karas <[email protected]>
##
## Everyone is permitted to copy and distribute verbatim or modified
## copies of this license document, and changing it is allowed as long
## as the name is changed.
##
## DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
## TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
##
## 0. You just DO WHAT THE FUCK YOU WANT TO.
## Purpose of this script is to monitor HDMI state on my Raspberry PI
## and start XBMC when output is plugged in and terminate it when
## it is unplugged.
## It is tested with Raspbian distribution and XBMC compiled from git.
HOME=/root/
cd $HOME
## for debug, print variables
env
echo
## https://github.com/raspberrypi/userland/blob/4062ce5b2492b4eae4bb9816a7a43c00f7a216de/interface/vmcs_host/vc_hdmi.h#L468
# typedef enum {
# VC_HDMI_UNPLUGGED = (1 << 0), /**<HDMI cable is detached */
# VC_HDMI_ATTACHED = (1 << 1), /**<HDMI cable is attached but not powered on */
# VC_HDMI_DVI = (1 << 2), /**<HDMI is on but in DVI mode (no audio) */
# VC_HDMI_HDMI = (1 << 3), /**<HDMI is on and HDMI mode is active */
# VC_HDMI_HDCP_UNAUTH = (1 << 4), /**<HDCP authentication is broken (e.g. Ri mismatched) or not active */
# VC_HDMI_HDCP_AUTH = (1 << 5), /**<HDCP is active */
# VC_HDMI_HDCP_KEY_DOWNLOAD = (1 << 6), /**<HDCP key download successful/fail */
# VC_HDMI_HDCP_SRM_DOWNLOAD = (1 << 7), /**<HDCP revocation list download successful/fail */
# VC_HDMI_CHANGING_MODE = (1 << 8), /**<HDMI is starting to change mode, clock has not yet been set */
# } VC_HDMI_NOTIFY_T;
function try_start_xbmc {
echo "try start up xbmc"
if [ `ps aux | grep -Pc "[x]bmc|[k]odi"` -gt 0 ] ; then
echo "Xbmc is running already"
else
echo "switching to vt7, and clear it"
chvt 7
# hide cursor
echo -e '\033[?17;0;0c' > /dev/tty7 2>/dev/null
# clear it
echo -e '\x1b\x5b\x48\x1b\x5b\x32\x4a' > /dev/tty7 2>/dev/null
#reset
#clear
#clear_console
if [ -f /usr/lib/kodi/kodi.bin ] ; then
LD_PRELOAD=/usr/lib/libtag.so /usr/lib/kodi/kodi.bin --debug 2>&1 &
else
LD_PRELOAD=/usr/lib/libtag.so /usr/lib/xbmc/xbmc.bin --debug 2>&1 &
fi
PID=`ps | grep -P "[x]bmc|[k]odi" | head -1 | awk '{print $1}'`
echo "started xbmc ($PID)"
cpufreq-set -g performance
fi
}
function try_shutdown_xbmc {
echo "try shutdown xbmc"
echo "sending Application.Quit command to localhost 9090"
echo '{"jsonrpc": "2.0", "method": "Application.Quit", "id": 1}' | netcat localhost 9090
echo
chvt 1
sleep 10
cpufreq-set -g ondemand
PID=`ps | grep -P "[x]bmc|[k]odi" | head -1 | awk '{print $1}'`
if [ `ps $PID | grep -Pc "[x]bmc|[k]odi"` -eq 1 ] ; then
echo "xbmc is still live, killing pid $PID"
kill $PID
fi
}
function hdmi_event {
event=$1
date +"%Y-%m-%d %H:%M:%S $event"
state=`tvservice -s | awk '{print $2}'`
echo "tvservice state: $state ($(( $state & 0x03 )))"
if [ $(( $state & 0x03 )) -eq $(( 0x02 )) ] ; then # attached
try_start_xbmc
fi
if [ $(( $state & 0x03 )) -eq $(( 0x01 )) ] ; then # unplugged
try_shutdown_xbmc
fi
echo
}
hdmi_event "startup"
tvservice -M 2>&1 | while read event ; do
hdmi_event "$event"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment