Skip to content

Instantly share code, notes, and snippets.

@mattie47
Last active October 22, 2019 02:06
Show Gist options
  • Save mattie47/6518f2b55056efec3de3a93ab2b33fb2 to your computer and use it in GitHub Desktop.
Save mattie47/6518f2b55056efec3de3a93ab2b33fb2 to your computer and use it in GitHub Desktop.
#!/bin/bash
#Simple script to control your Raspberry Pi with your TV remote using libcec
#
# Copyleft 2017 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com>
# GPL licensed (see end of file) * Use at your own risk!
# Some minor modifications by Matt Read.
#
# You can set this to be run in your desktop "startup programs", or
# at the last line of /etc/rc.local, just to say a couple options
#
# keys:
# OK - launch kodi
# up - page up
# down - page down
# left - control + shift + tab
# right - Control + tab
#
# This is just an example to provide ideas, so you should change what each button does.
#
export DISPLAY=:0 # Ensure we're using the main HDMI GUI X Session.
trap "exit" INT # Allows killing the script with 'Ctrl + C'.
USER=pi # user that will be logged in
OSDCOLOR=red # color for xosd messages
MOUSESPEED=50 # how many pixels a mouse will move on each press
dbg=echo # uncomment to just print the command without executing it
VERBOSE=1 # print echov lines
type cec-client &>/dev/null || { echo "cec-client is required"; echo "sudo apt install cec-utils"; exit; }
type xdotool &>/dev/null || NOXDOTOOL=#
USR_CMD="su - $USER -c"
_XAUTH="/home/$USER/.Xauthority"
function d(){ eval "$NOXDOTOOL $@" ; }
function echov(){ [[ "$VERBOSE" == "1" ]] && echo $@ ; }
function filter_key(){ grep -q "key pressed: $1 .* duration" <( echo "$2" ) ; }
function mouse_move(){ XAUTHORITY="$_XAUTH" DISPLAY=:0 xdotool mousemove_relative -- $@; }
function mouseclick(){ XAUTHORITY="$_XAUTH" DISPLAY=:0 xdotool click --repeat 2 1 ; }
function osdecho(){ type osd_cat &>/dev/null && echo "$@" | \
XAUTHORITY="$_XAUTH" DISPLAY=:0 osd_cat -ptop -Acenter -c$OSDCOLOR; }
while :; do
cec-client | while read l; do
echov $l
if filter_key "up" "$l"; then
xdotool key "Page_Up"
fi
if filter_key "left" "$l"; then
xdotool key "Control_L+Shift_L+Tab"
fi
if filter_key "down" "$l"; then
xdotool key "Page_Down"
fi
if filter_key "right" "$l"; then
xdotool key "Control_L+Tab"
fi
if filter_key "exit" "$l"; then
$dbg $USR_CMD "pkill -SIGTERM -f lxsession"
fi
if filter_key "play" "$l"; then
echov "mouse mode off"
osdecho "mouse mode off"
MOUSEMODE=0
fi
done
done
# License
#
# This script 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 script 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 script; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment