aboutsummaryrefslogtreecommitdiff
path: root/scripts/media_control.sh
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/media_control.sh')
-rwxr-xr-xscripts/media_control.sh74
1 files changed, 66 insertions, 8 deletions
diff --git a/scripts/media_control.sh b/scripts/media_control.sh
index f2e6612..b158586 100755
--- a/scripts/media_control.sh
+++ b/scripts/media_control.sh
@@ -4,6 +4,11 @@
# NOTE:
# if spotify is running toggle is consumed by spotify.
+
+function spotify_send_command {
+ dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.$1
+}
+
# arg parsing
if [ "$1" = "toggle" ]
then
@@ -18,20 +23,73 @@ fi
if [ "$spotify_cmd" != "" ]
then
- # can't be sure if spotify was running and should play again
- if [ "$spotify_cmd" = "PlayPause" ]
+ # check if spotify and mpd are running
+ if pgrep spotify &>/dev/null
then
- if [ "$(ps -A | grep spotify)" != "" ]
+ spotify_running=true
+ else
+ spotify_running=false
+ fi
+
+ if pgrep mpd &>/dev/null
+ then
+ mpd_running=true
+ if type mpc &>/dev/null
then
- dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.$spotify_cmd
+ mpd_controllable=true
else
+ mpd_controllable=false
+ fi
+ else
+ mpd_running=false
+ fi
+
+ # both are running -> decide who gets the command
+ # the playing program gets the commands
+ if $spotify_running && $mpd_running
+ then
+ if ! $mpd_controllable
+ then
+ spotify_send_command $spotify_cmd
+ >&2 echo "mpd is running put mpc is not available to control it!"
+ exit
+ fi
+
+ spotify_playing=$(./media_is_spotify_playing.sh)
+ if $(mpc | grep "\[playing\]")
+ then
+ mpd_playing=true
+ else
+ mpd_playing=false
+ fi
+
+ if $spotify_playing && $mpd_playing
+ then
+ >&2 echo "mpd and spotify are playing!"
+ elif $spotify_playing
+ then
+ spotify_send_command $spotify_cmd
+ elif $mpd_playing
+ then
mpc $1
+ # nothing is actually running
+ # don't know who was playing -> send spotify all commands
+ else
+ spotify_send_command $spotify_cmd
fi
- elif [ "$(lsof | grep /dev/snd | grep spotify)" != "" ]
+
+ # only spotify is running
+ elif $spotify_running
then
- dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.$spotify_cmd
- elif [ "$(mpc | grep '\[' | cut -f1 -d' ')" != "" ]
+ spotify_send_command $spotify_cmd
+ # only mpd is running
+ elif $mpd_running
then
- mpc $1 &>/dev/null
+ if $mpd_controllable
+ then
+ mpc $1
+ else
+ >&2 echo "mpd is running put mpc is not available to control it!"
+ fi
fi
fi