2022-08-13 00:56:52 +02:00
|
|
|
|
#!/bin/bash
|
2019-06-13 20:12:29 +02:00
|
|
|
|
|
2022-08-10 18:37:33 +02:00
|
|
|
|
# Fonction de réinitialisation en cas de perte de la connexion
|
|
|
|
|
self-reset() {
|
|
|
|
|
echo "<span foreground=\"#515151\">[--:-- --:-- --:--]</span>"
|
|
|
|
|
sleep 5
|
|
|
|
|
exec "$0"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trap self-reset PIPE
|
|
|
|
|
|
|
|
|
|
# Initialisation de la connexion au serveur
|
|
|
|
|
coproc nc -U /home/breizh/.mpd/socket
|
|
|
|
|
IN=${COPROC[1]}
|
|
|
|
|
OUT=${COPROC[0]}
|
2022-08-13 01:38:52 +02:00
|
|
|
|
#echo "password xxx" >&"${IN}"
|
|
|
|
|
sed '/^OK/q' <&"$OUT" &>/dev/null
|
2022-08-10 18:37:33 +02:00
|
|
|
|
|
|
|
|
|
# Initialisation des valeurs par défaut et des fonctions
|
2022-08-13 01:38:52 +02:00
|
|
|
|
declare -i SCROLL=50 I=0
|
2022-08-10 18:37:33 +02:00
|
|
|
|
declare -- artist song oldfile
|
2019-09-22 03:31:29 +02:00
|
|
|
|
|
2019-06-14 09:42:47 +02:00
|
|
|
|
human_time() {
|
2022-08-13 01:12:58 +02:00
|
|
|
|
local seconds="${1:-0}"
|
|
|
|
|
if [[ "$seconds" -le 0 ]]
|
2019-06-14 09:42:47 +02:00
|
|
|
|
then
|
|
|
|
|
echo "--:--"
|
|
|
|
|
return
|
|
|
|
|
fi
|
2022-08-13 01:38:52 +02:00
|
|
|
|
local minutes=$(( seconds / 60 ))
|
|
|
|
|
seconds=$(( seconds % 60 ))
|
2022-03-15 00:18:30 +01:00
|
|
|
|
if [[ "$minutes" -ge 60 ]]
|
2019-06-14 09:42:47 +02:00
|
|
|
|
then
|
2022-08-13 01:38:52 +02:00
|
|
|
|
local hours=$(( minutes / 60 ))
|
|
|
|
|
minutes=$(( minutes % 60 ))
|
2022-03-15 00:18:30 +01:00
|
|
|
|
printf "%02dh%02d\n" "$hours" "$minutes"
|
2019-06-14 09:42:47 +02:00
|
|
|
|
else
|
|
|
|
|
printf "%02d:%02d\n" "$minutes" "$seconds"
|
|
|
|
|
fi
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-10 18:37:33 +02:00
|
|
|
|
# Initialisation de l’état
|
2022-08-13 01:38:52 +02:00
|
|
|
|
echo "status" >&"$IN"
|
|
|
|
|
while read -t 1 -u "$OUT" output
|
2022-08-10 18:37:33 +02:00
|
|
|
|
do
|
|
|
|
|
[[ "$output" == "OK" ]] && break
|
|
|
|
|
o[${output%%:*}]="${output##*: }"
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Boucle principale
|
|
|
|
|
while [[ -n "$COPROC_PID" ]]
|
|
|
|
|
do
|
|
|
|
|
# Gestion du clic, fait office de délai si aucune action (une seconde).
|
|
|
|
|
# En cas de clic, l’actualisation de l’affichage sera instantanné.
|
|
|
|
|
read -t 1 BLOCK_BUTTON
|
2019-06-13 20:12:29 +02:00
|
|
|
|
|
2022-08-10 18:37:33 +02:00
|
|
|
|
case $BLOCK_BUTTON in
|
2022-08-13 00:44:56 +02:00
|
|
|
|
1|3)
|
|
|
|
|
if [[ "${o["state"]}" == stop ]]
|
|
|
|
|
then
|
2022-08-13 01:38:52 +02:00
|
|
|
|
echo "play" >&"${IN}" || self-reset
|
|
|
|
|
sed '/^OK$/q' <&"$OUT" &>/dev/null
|
2022-08-13 00:44:56 +02:00
|
|
|
|
fi
|
2022-08-13 00:54:00 +02:00
|
|
|
|
;;&
|
2022-08-13 00:44:56 +02:00
|
|
|
|
2)
|
|
|
|
|
if [[ "${o["state"]}" == stop ]]
|
|
|
|
|
then
|
2022-08-13 01:38:52 +02:00
|
|
|
|
echo "play" >&"${IN}" || self-reset
|
2022-08-13 00:44:56 +02:00
|
|
|
|
else
|
2022-08-13 01:38:52 +02:00
|
|
|
|
echo "pause" >&"${IN}" || self-reset
|
2022-08-13 00:44:56 +02:00
|
|
|
|
fi
|
2022-08-13 00:54:00 +02:00
|
|
|
|
;;&
|
2022-08-13 01:38:52 +02:00
|
|
|
|
1) echo "previous" >&"${IN}" || self-reset ;;&
|
|
|
|
|
3) echo "next" >&"${IN}" || self-reset ;;&
|
|
|
|
|
1|2|3) sed '/^OK$/q' <&"$OUT" &>/dev/null ;;
|
|
|
|
|
4) I="I-2"; [[ "$I" -lt 0 ]] && I=0 ;;
|
2022-08-10 18:37:33 +02:00
|
|
|
|
esac
|
2019-06-13 20:12:29 +02:00
|
|
|
|
|
2022-08-10 18:37:33 +02:00
|
|
|
|
# Réinitialisation de l’état
|
2022-08-13 00:48:35 +02:00
|
|
|
|
unset o output
|
2022-08-10 18:37:33 +02:00
|
|
|
|
declare -A o
|
2019-06-14 09:42:47 +02:00
|
|
|
|
|
2022-08-10 18:37:33 +02:00
|
|
|
|
# Récupération de l’état
|
2022-08-13 01:38:52 +02:00
|
|
|
|
echo "status" >&"$IN" || self-reset
|
|
|
|
|
while read -t 1 -u "$OUT" output
|
2022-08-10 18:37:33 +02:00
|
|
|
|
do
|
|
|
|
|
[[ "$output" == "OK" ]] && break
|
|
|
|
|
o[${output%%:*}]="${output##*: }"
|
|
|
|
|
done
|
2022-08-13 00:48:35 +02:00
|
|
|
|
|
2022-08-10 18:37:33 +02:00
|
|
|
|
# Récupération des informations du morceau en cours
|
2022-08-13 01:38:52 +02:00
|
|
|
|
echo "currentsong" >&"$IN" || self-reset
|
|
|
|
|
while read -t 1 -u "$OUT" output
|
2022-08-10 18:37:33 +02:00
|
|
|
|
do
|
|
|
|
|
[[ "$output" == "OK" ]] && break
|
|
|
|
|
o[${output%%:*}]="${output##*: }"
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Choix des couleurs selon l’état
|
|
|
|
|
case "${o["state"]}" in
|
|
|
|
|
play ) status="<span foreground=\"#99CC99\">" ;;
|
|
|
|
|
pause ) status="<span foreground=\"#FFCC66\">" ;;
|
|
|
|
|
stop ) status="<span foreground=\"#F2777A\">" ;;
|
2019-06-14 09:42:47 +02:00
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
|
2022-08-10 18:37:33 +02:00
|
|
|
|
# En cas de changement de morceau,
|
|
|
|
|
# on réinitialise l’affichage défilant
|
|
|
|
|
if [[ "${o["file"]}" != "$oldfile" ]]
|
2019-06-13 20:12:29 +02:00
|
|
|
|
then
|
2022-08-10 18:37:33 +02:00
|
|
|
|
unset bloc1 bloc2 bloc3 bloc4
|
|
|
|
|
artist="${o["Artist"]:-${o["Name"]}}"
|
|
|
|
|
song="${o["Title"]}"
|
2022-08-13 00:34:09 +02:00
|
|
|
|
[[ -z "$artist$song" ]] && song="$(basename "${o["file"]}")"
|
2022-08-10 18:37:33 +02:00
|
|
|
|
|
|
|
|
|
if [[ "$(( ${#song} + ${#artist} + 1 ))" -gt "$SCROLL" ]]
|
|
|
|
|
then
|
|
|
|
|
scroll=true
|
|
|
|
|
song=" $song "
|
|
|
|
|
else
|
|
|
|
|
scroll=false
|
|
|
|
|
song=" $song"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
bloc1="$artist"
|
|
|
|
|
bloc2="$song"
|
|
|
|
|
|
|
|
|
|
oldfile="${o["file"]}"
|
|
|
|
|
I=0
|
2019-06-13 20:12:29 +02:00
|
|
|
|
fi
|
2022-08-10 18:37:33 +02:00
|
|
|
|
|
|
|
|
|
# Gestion du défilement
|
|
|
|
|
if "$scroll"
|
2019-06-13 20:12:29 +02:00
|
|
|
|
then
|
2022-08-10 18:37:33 +02:00
|
|
|
|
unset bloc1 bloc2 bloc3 bloc4
|
|
|
|
|
declare -- bloc1 bloc2 bloc3 bloc4
|
|
|
|
|
bloc1="${artist:$I:$SCROLL}"
|
|
|
|
|
if [[ "${#bloc1}" -eq 0 ]]
|
|
|
|
|
then
|
|
|
|
|
bloc2="${song:$(( I - ${#artist} )):$SCROLL}"
|
|
|
|
|
if [[ "$(( ${#bloc1} + ${#bloc2} ))" -lt "$SCROLL" ]]
|
|
|
|
|
then
|
|
|
|
|
bloc3="${artist:0:$(( SCROLL - ${#bloc2} ))}"
|
|
|
|
|
if [[ "$(( ${#bloc1} + ${#bloc2} + ${#bloc3} ))" -lt "$SCROLL" ]]
|
|
|
|
|
then
|
|
|
|
|
bloc4="${song:0:$(( SCROLL - ${#bloc1} - ${#bloc2} - ${#bloc3} ))}"
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
bloc2="${song:0:$(( SCROLL - ${#bloc1} ))}"
|
|
|
|
|
if [[ "$(( ${#bloc1} + ${#bloc2} ))" -lt "$SCROLL" ]]
|
|
|
|
|
then
|
|
|
|
|
bloc3="${artist:0:$(( SCROLL - ${#bloc2} - ${#bloc1} ))}"
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
I+=1
|
|
|
|
|
[[ "$I" -ge "$(( ${#artist} + ${#song} ))" ]] && I=0
|
2019-06-13 20:12:29 +02:00
|
|
|
|
fi
|
|
|
|
|
|
2022-08-10 18:37:33 +02:00
|
|
|
|
# Gestion du temps
|
|
|
|
|
|
2022-08-13 01:12:58 +02:00
|
|
|
|
declare -i time_elapsed time_total time_left
|
|
|
|
|
|
|
|
|
|
time_elapsed="${o["time"]%:*}"
|
|
|
|
|
time_total="${o["time"]#*:}"
|
|
|
|
|
time_left="${time_total:-${o["duration"]%.*}} - ${time_elapsed:-${o["Time"]}}"
|
|
|
|
|
|
2022-08-13 01:38:52 +02:00
|
|
|
|
time="[$(human_time "${time_elapsed}") $(human_time "${time_left}") $(human_time "${time_total}")]"
|
2019-06-13 20:12:29 +02:00
|
|
|
|
|
2022-08-13 01:17:28 +02:00
|
|
|
|
bloc1="${bloc1//&/&}"
|
|
|
|
|
bloc2="${bloc2//&/&}"
|
|
|
|
|
bloc3="${bloc3//&/&}"
|
|
|
|
|
bloc4="${bloc4//&/&}"
|
|
|
|
|
|
2022-08-10 18:37:33 +02:00
|
|
|
|
# DEBUG
|
|
|
|
|
printf '<b>%s</b>%s<b>%s</b>%s%s\n' "$bloc1" "$bloc2" "$bloc3" "$bloc4" " $status$time</span>"
|
|
|
|
|
done
|