]> git.plutz.net Git - serve0/blob - actions/playctl.sh
2934b90fd2b8be4d868451e8df27bcf49691768c
[serve0] / actions / playctl.sh
1 #!/bin/zsh
2 # Copyright 2016 Paul Hänsch
3 #
4 # This file is part of Serve0
5 #
6 # Serve0 is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Serve0 is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU Affero General Public License for more details.
15 #
16 # You should have received a copy of the GNU Affero General Public License
17 # along with Serve0  If not, see <http://www.gnu.org/licenses/>.
18
19 fifofile="${_DATA}/mplayer.fifo"
20 statusfile="${_DATA}/mplayer.status"
21 statusfile="/tmp/mplayer.status"
22 volfile="${_DATA}/mplayer.volume"
23
24 export DISPLAY=":0"
25 export XDG_RUNTIME_DIR="/run/user/$(id |sed -rn '1s;.*uid=([0-9]+)[^0-9].*;\1;p')"
26
27 printf '\rBREAK\r' >> "$statusfile"
28
29 if [ "${_GET[api]}" = progress ]; then
30   cr="$(printf \\r)"
31   printf 'Content-Type: text/plain\r\n\r\n'
32
33   tail --pid=$$ -c100 -f "$statusfile" \
34   | while read -d "$cr" line; do
35     [ "$line" = "BREAK" ] && exit 0
36     printf '%s\n' "$line"
37   done \
38   | sed -urn '1~12s;^.* V: *+([0-9]+)(\.[0-9]+) A-V:.*$;\1;p' \
39   | while read m; do
40     [ "$m" != "$previous" ] && printf "%d %d\n" "$(($m * 100 / $length))" "$m"
41     previous="$m"
42   done
43   exit 0
44 elif [ "${_GET[api]}" = volume ]; then
45   printf 'Content-Type: text/plain\r\n\r\n'
46   validate "$(cat "$_DATA/mplayer.volume")" '[1-9]?[0-9]|100' 20
47   exit 0
48 fi
49
50 if   [ -n "${_POST[amp]+x}" ]; then
51   amp $(validate "${_POST[amp]}" '(on|off)' off)
52 elif [ -n "${_POST[vol]+x}" ]; then
53   vol=$(validate "${_POST[vol]}" '[1-9]?[0-9]|100' 10)
54   printf %s\\n $vol >"$volfile"
55   printf 'volume %s 1\n' $vol >"$fifofile"
56 elif [ -n "${_POST[ctl]+x}" ]; then
57   printf '%s\n' $(validate "${_POST[ctl]}" "pause|stop" pause) >"$fifofile"
58 elif [ -n "${_POST[seek]+x}" ]; then
59   printf 'seek %s\n' $(validate "${_POST[seek]}" "[+-]?[0-9]+" 0) >"$fifofile"
60 elif [ -n "${_POST[pasink]+x}" ]; then
61   stream="$(\
62     pactl list sink-inputs \
63     | sed -rn '/Sink Input #/{:X;N;/application.name =/!bX;s;Sink Input #([0-9]+).+application.name = "(MPlayer|mplayer2)";\1;p}' \
64   )"
65   pactl move-sink-input "$stream" "${_POST[pasink]}"
66 elif [ -n "${_POST[osd]+x}" ]; then
67   case ${_POST[osd]} in
68     progression) printf "osd_show_progression\\n";;
69   esac >"$fifofile"
70 fi &
71
72 redirect "${_POST[referrer]}"