aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2016-06-02 18:14:02 +0200
committerFlorian Fischer <florian.fl.fischer@fau.de>2016-06-06 13:03:36 +0200
commit6b69e1bd4030a997e811e46ae791e028b964d1da (patch)
tree9eaa3e228999b606bf0b653e45302bad3701f387
parentc8adc26ff6476beec4e50698575a34bdeb0164e8 (diff)
downloaddotfiles-6b69e1bd4030a997e811e46ae791e028b964d1da.tar.gz
dotfiles-6b69e1bd4030a997e811e46ae791e028b964d1da.zip
rework media_control to remember the last player
README.md: mention that some scripts uses hardcoded locations scripts/media_is_spotify_playing.sh: use dbus to get spotify PlaybackStatus scripts/media_control: 1) introduce a new file: /tmp/media_control_last 2) use this file to store the last active player 3) send the command to the last active player if no player is running
-rw-r--r--README.md7
-rwxr-xr-xscripts/media_control.sh31
-rwxr-xr-xscripts/media_is_spotify_playing.sh96
3 files changed, 89 insertions, 45 deletions
diff --git a/README.md b/README.md
index 17d08a1..ac5a6dd 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,8 @@
# my dotfiles
+---
+
+## Installation
+Some scripts use a hardcoded location so please locate the dotfile repo
+at $HOME/.dotfiles.
+To do this run
+```git clone https://github.com/fischerling/dotfiles $HOME/.dotfiles```.
diff --git a/scripts/media_control.sh b/scripts/media_control.sh
index b158586..d8f7f79 100755
--- a/scripts/media_control.sh
+++ b/scripts/media_control.sh
@@ -1,9 +1,7 @@
#!/bin/bash
-#TODO
-# NOTE:
-# if spotify is running toggle is consumed by spotify.
-
+# To start the correct Player this script uses the file /tmp/media_control_last
+f="/tmp/media_control_last"
function spotify_send_command {
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.$1
@@ -55,8 +53,8 @@ then
exit
fi
- spotify_playing=$(./media_is_spotify_playing.sh)
- if $(mpc | grep "\[playing\]")
+ spotify_playing=$($HOME/.dotfiles/scripts/media_is_spotify_playing.sh)
+ if mpc | grep "\[playing\]" &> /dev/null
then
mpd_playing=true
else
@@ -69,25 +67,40 @@ then
elif $spotify_playing
then
spotify_send_command $spotify_cmd
+ echo spotify > $f
elif $mpd_playing
then
mpc $1
- # nothing is actually running
- # don't know who was playing -> send spotify all commands
+ echo mpd > $f
+ # nothing is actually running
+ # read last running file
else
- spotify_send_command $spotify_cmd
+ if [ -f $f ]
+ then
+ last=$(cat $f)
+
+ if [ $last = "spotify" ]
+ then
+ spotify_send_command $spotify_cmd
+ elif [ $last = "mpd" ]
+ then
+ mpc $1
+ fi
+ fi
fi
# only spotify is running
elif $spotify_running
then
spotify_send_command $spotify_cmd
+ echo spotify > $f
# only mpd is running
elif $mpd_running
then
if $mpd_controllable
then
mpc $1
+ echo mpd > $f
else
>&2 echo "mpd is running put mpc is not available to control it!"
fi
diff --git a/scripts/media_is_spotify_playing.sh b/scripts/media_is_spotify_playing.sh
index 8c10aad..f4f32a5 100755
--- a/scripts/media_is_spotify_playing.sh
+++ b/scripts/media_is_spotify_playing.sh
@@ -1,45 +1,69 @@
#!/bin/bash
-spotify_running=false
+# this script returns 0 if spotify is playing 1 otherwise
-if pgrep spotify &>/dev/null
+# spotify's dbus interface is able to tell us it PlaybackStatus :)
+
+status=`dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify \
+/org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get \
+string:'org.mpris.MediaPlayer2.Player' string:'PlaybackStatus' 2> /dev/null`
+
+if [ $? -ne 0 ]
then
- if type pacmd &>/dev/null
- then
- output=$(pacmd list-sink-inputs)
- state=""
- spotify_running=false
-
- while read -r line; do
- if [[ $line == *state* ]]
- then
- if [[ $line == *RUNNING* ]]
- then
- state="RUNNING"
- else
- state=""
- fi
- fi
-
- if [[ $line == *application.name* ]]
- then
- if [[ $state == "RUNNING" ]] && [[ $line == *Spotify* ]]
- then
- spotify_running=true
- break
- fi
- fi
- done <<< "$output"
- elif lsof | grep /dev/snd | grep spotify &>/dev/null
- then
- spotify_running=true
- fi
+ echo false
+ exit 1
fi
-echo $spotify_running
-if [[ $spotify_running == "true" ]]
+status=$(echo $status | grep -o -e \".*\" | cut -d \" -f 2)
+
+if [ $status != "Playing" ]
then
- exit 0
-else
+ echo false
exit 1
fi
+
+echo true
+
+#spotify_running=false
+
+#if pgrep spotify &>/dev/null
+#then
+ #if type pacmd &>/dev/null
+ #then
+ #output=$(pacmd list-sink-inputs)
+ #state=""
+ #spotify_running=false
+
+ #while read -r line; do
+ #if [[ $line == *state* ]]
+ #then
+ #if [[ $line == *RUNNING* ]]
+ #then
+ #state="RUNNING"
+ #else
+ #state=""
+ #fi
+ #fi
+
+ #if [[ $line == *application.name* ]]
+ #then
+ #if [[ $state == "RUNNING" ]] && [[ $line == *Spotify* ]]
+ #then
+ #spotify_running=true
+ #break
+ #fi
+ #fi
+ #done <<< "$output"
+ #elif lsof | grep /dev/snd | grep spotify &>/dev/null
+ #then
+ #spotify_running=true
+ #fi
+#fi
+
+#echo $spotify_running
+#if [[ $spotify_running == "true" ]]
+#then
+ #exit 0
+#else
+ #exit 1
+#fi