1 #!/bin/bash
   2 
   3 play_icon='>'
   4 pause_icon='||'
   5 players=0
   6 
   7 print(){
   8     (( $players > 1 )) && printf '[%9s] ' $1
   9     echo $icon ${title/\.mp3/} "(-$tl)"
  10 }
  11 
  12 for i in mocp cmus mpd audacious rhythmbox spotify; do
  13     [[ $(pgrep -x $i) ]] && ((players++))
  14 done
  15 
  16 # Moc {{{
  17 
  18 if [[ $(pgrep -x mocp) ]]; then
  19     case $1 in
  20         play) mocp --play; sleep 0.2;;
  21         stop) mocp --stop;;
  22        pause) mocp --toggle-pause;;
  23         prev) mocp --previous; sleep 0.1;;
  24         next) mocp --next; sleep 0.1;;
  25         seek) mocp --seek $2; sleep 0.1;;
  26     esac
  27 
  28     IFS='|' read status tt ct tl file title <<< \
  29     $(mocp --format '%state|%tt|%ct|%tl|%file|%artist - %song')
  30 
  31     if [[ $status != STOP ]]; then
  32         [[ $status == PLAY ]] &&
  33         icon=$play_icon || icon=$pause_icon
  34 
  35         (( ${#title} < 4 )) &&
  36         title=$(basename "$file")
  37 
  38         print mocp
  39     fi
  40 fi
  41 
  42 # }}}
  43 # DBus {{{
  44 
  45 query(){ # {{{
  46     qdbus org.mpris.MediaPlayer2.$player \
  47     /org/mpris/MediaPlayer2 \
  48     org.mpris.MediaPlayer2.Player.$1 $2 2>&-
  49 } # }}}
  50 get(){ # {{{
  51     grep "$1: " <<< "$meta" | cut -d : -f 3
  52 } # }}}
  53 
  54 for player in audacious rhythmbox spotify; do
  55     [[ ! $(pgrep -x $player) ]] &&
  56     continue
  57 
  58     case $1 in
  59         play) query Play; sleep 0.2;;
  60         stop) query Stop;;
  61        pause) query PlayPause;;
  62         prev) query Previous;;
  63         next) query Next;;
  64         seek) query Seek $(($2 * 1000000));;
  65     esac >&-
  66 
  67     status=$(query PlaybackStatus)
  68     if [[ $status != Stopped ]]; then
  69         [[ $status == Playing ]] &&
  70         icon=$play_icon || icon=$pause_icon
  71 
  72         meta=$(query Metadata)
  73         artist=$(get artist)
  74         title=$(get title)
  75 
  76         len=$(($(get length) / 1000000))
  77         pos=$(($(query Position) / 1000000))
  78 
  79         tt=$(date -ud @$len +'%M:%S')
  80         ct=$(date -ud @$pos +'%M:%S')
  81         tl=$(date -ud @$(($len - $pos)) +'%M:%S')
  82 
  83         [[ $artist ]] &&
  84         title="$artist - $title"
  85 
  86         print $player
  87     fi
  88 done
  89 
  90 # }}}
  91 # Cmus {{{
  92 
  93 if [[ $(pgrep -x cmus) ]]; then
  94     case $1 in
  95         play) cmus-remote --play; sleep 0.2;;
  96         stop) cmus-remote --stop;;
  97        pause) cmus-remote --pause;;
  98         prev) cmus-remote --prev;;
  99         next) cmus-remote --next;;
 100         seek) cmus-remote --seek $2;;
 101     esac
 102 
 103     declare -A array
 104     while read k1 k2 v; do
 105         [[ $k1 == set || $k1 == tag ]] &&
 106         array[$k2]="$v" || array[$k1]="$k2 $v"
 107     done <<< "$(cmus-remote --query)"
 108 
 109     status=${array[status]}
 110     if [[ $status != stopped* ]]; then
 111         [[ $status == playing* ]] &&
 112         icon=$play_icon || icon=$pause_icon
 113 
 114         file=${array[file]}
 115         artist=${array[artist]}
 116         title=${array[title]}
 117 
 118         len=${array[duration]}
 119         pos=${array[position]}
 120 
 121         tt=$(date -ud @$len +'%M:%S')
 122         ct=$(date -ud @$pos +'%M:%S')
 123         tl=$(date -ud @$(($len - $pos)) +'%M:%S')
 124 
 125         [[ $title ]] &&
 126         title="$artist - ${title}" ||
 127         title=$(basename "$file")
 128 
 129         print cmus
 130     fi
 131 fi
 132 
 133 # }}}
 134 # Mpd {{{
 135 
 136 if [[ $(pgrep -x mpd) ]]; then
 137     case $1 in
 138         play) mpc play;;
 139         stop) mpc stop;;
 140        pause) mpc toggle;;
 141         prev) mpc prev;;
 142         next) mpc next;;
 143         seek) mpc seek $2;;
 144     esac >&-
 145 
 146     array=()
 147     while read line; do
 148         array+=("$line")
 149     done <<< "$(mpc --format '[%artist% - %title%]|[%file%]')"
 150 
 151     if [[ ${array[1]} ]]; then
 152         title=${array[0]##*/}
 153         status=$(cut -d ' ' -f 1 <<< ${array[1]})
 154 
 155         tm=$(cut -d ' ' -f 3 <<< ${array[1]})
 156         tt=$(cut -d / -f 2 <<< $tm)
 157         ct=$(cut -d / -f 1 <<< $tm)
 158 
 159         [[ $status == \[playing\] ]] &&
 160         icon=$play_icon || icon=$pause_icon
 161 
 162         print mpd
 163     fi
 164 fi
 165 
 166 # }}}