statusfile="/tmp/mplayer.status"
volfile="${_DATA}/mplayer.volume"
-export DISPLAY=":0"
-export XDG_RUNTIME_DIR="/run/user/$(id |sed -rn '1s;.*uid=([0-9]+)[^0-9].*;\1;p')"
+[ -z "$DISPLAY" ] && export DISPLAY=":0"
+[ -z "$XDG_RUNTIME_DIR" ] && export XDG_RUNTIME_DIR="/run/user/$(id |sed -rn '1s;.*uid=([0-9]+)[^0-9].*;\1;p')"
printf '\rBREAK\r' >> "$statusfile"
+if [ "${_GET[api]}" = progress ]; then
+ cr="$(printf \\r)"
+ printf 'Content-Type: text/plain\r\n\r\n'
+
+ read length <${_DATA}/mplayer.meta
+ tail --pid=$$ -c100 -f "$statusfile" \
+ | while read -d "$cr" line; do
+ [ "$line" = "BREAK" ] && exit 0
+ printf '%s\n' "$line"
+ done \
+ | sed -urn '1~12s;^.* V: *+([0-9]+)(\.[0-9]+) A-V:.*$;\1;p'
+ exit 0
+elif [ "${_GET[api]}" = volume ]; then
+ printf 'Content-Type: text/plain\r\n\r\n'
+ validate "$(cat "$_DATA/mplayer.volume")" '[1-9]?[0-9]|100' 20
+ exit 0
+fi
+
if [ -n "${_POST[amp]+x}" ]; then
amp $(validate "${_POST[amp]}" '(on|off)' off)
elif [ -n "${_POST[vol]+x}" ]; then