From 90044a0452c64d599f97a4881678a421f3dd357d Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Mon, 11 Jan 2016 20:54:46 +0100 Subject: rework media_control to work with pulseaudio 1) outsource check if spotify is playing in media_is_spotify_playing.sh 2) use media_is_spotify_playing in media_status.sh 3) rework media_control using media_is_spotify_playing.sh --- scripts/media_control.sh | 74 +++++++++++++++++++++++++++++++++---- scripts/media_is_spotify_playing.sh | 45 ++++++++++++++++++++++ scripts/media_status.sh | 34 +---------------- 3 files changed, 113 insertions(+), 40 deletions(-) create mode 100755 scripts/media_is_spotify_playing.sh 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 diff --git a/scripts/media_is_spotify_playing.sh b/scripts/media_is_spotify_playing.sh new file mode 100755 index 0000000..8c10aad --- /dev/null +++ b/scripts/media_is_spotify_playing.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +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 diff --git a/scripts/media_status.sh b/scripts/media_status.sh index 850a0c5..22116e0 100755 --- a/scripts/media_status.sh +++ b/scripts/media_status.sh @@ -1,39 +1,9 @@ #!/bin/bash -mpd_running=$(mpc | grep "\[playing\]") +mpd_running=$(mpc 2>/dev/null | grep "\[playing\]") +spotify_running=$(./media_is_spotify_playing.sh) 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 - if $spotify_running && [[ $mpd_running != "" ]] then echo spotify and mpd running -- cgit v1.2.3