aboutsummaryrefslogtreecommitdiff
path: root/bin/dmenu_playerctl
blob: 64cbe41442e647efe357051544553773b2ce5ce4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/mksh

select_player() {
  echo $1 | tr ' ' '\n' | ${DMENU}
}

prompt() {
  a=$(playerctl -p $1 metadata xesam:artist)
  t=$(playerctl -p $1 metadata xesam:title)
  if [ -n "$a" -a -n "$t" ]; then
    echo "$1: $a - $t"
  else
    echo "$1:"
  fi
}

play_pause() {
  [ $(playerctl -p $1 status) == "Playing" ] && echo "Pause" || echo "Play"
}

player_menu() {
  CMDS="$(play_pause $1)\nStop\nNext\nPrevious\nVolume"
  ret=$(echo $CMDS | ${DMENU} -p "$(prompt $1)" -i | tr '[A-Z]' '[a-z]')
  case "$ret" in
    play|pause|next|previous) playerctl -p $1 $ret ;;
    volume) volume_menu $1 ;;
  esac
}

volume_menu() {
  cmds="Up\nDown"
  while :
  do
    vol=$(echo "$(playerctl -p $1 volume) * 100" | bc)
    vol=${vol%.*}
    ret=$(echo "$cmds" | ${DMENU} -p "$1: $vol" -i | tr '[A-Z]' '[a-z]')
    case "$ret" in
      up)
        playerctl -p $1 volume $(echo "($vol + 5) / 100" | bc -l)
        cmds="Up\nDown"
      ;;
      down)
        playerctl -p $1 volume $(echo "($vol - 5) / 100" | bc -l)
        cmds="Down\nUp"
      ;;
      *)
        [ -z "$ret" ] && break
        [ -n "$(echo $ret | tr -d '[[:digit:]]')" ] && break
        playerctl -p $1 volume $(echo "$ret / 100" | bc -l)
        if [ "$ret" -gt "$vol" ]; then
          cmds="Up\nDown"
        else
          cmds="Down\nUp"
        fi
      ;;
    esac
  done
}

: ${DMENU:=dmenu}
: ${PLAYER:=$(playerctl -l)}
case "$(echo ${PLAYER} | wc -w)" in
  0) echo "No player running" && exit ;;
  1) player_menu "$PLAYER" ;;
  *) player_menu $(select_player "$PLAYER") ;;
esac