~q3cpma/mus

ref: e99afe099fd0b6cf6a4cce58902c9612ecd9fca9 mus/mus_client -rwxr-xr-x 3.6 KiB
e99afe09q3cpma Finish build.sh and README, move TODO to root 1 year, 1 month ago
                                                                                
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/sh
# Dependencies: flock(1)
# Portability:	GNU, *BSD (minus Open)
set -eu
rpath_cbin=$(dirname -- "$0")/mus_player
rpath_share=$(dirname -- "$0")
. "$rpath_share"/mus_util.sh


usage()
{
	cat <<EOF | text_format
**NAME**
    $(basename -- "$0") - Album playlist based music player (client)

**SYNOPSIS**
    $(basename -- "$0") [**-h**] [**-l** __FILE__] [**-u** __SOCKET__] COMMAND

**DESCRIPTION**
    The following commands can be sent to the player and apply only within an
    album (i.e. **TRACK_PREV** can't go back to the previous album):
        **TRACK_NEXT**        Skip the current track
        **TRACK_PREV**        Jump to the previous track
        **TRACK_FIRST**       Jump to the first track of the current album
        **TRACK_REWIND**      Restart the current track from the beginning
        **ALBUM_NEXT**        Skip the current album
        **PAUSE**             Pause playback
        **PLAY**              Resume playback
        **TOGGLE_PLAY_PAUSE** Toggle pause/play
        **STATUS**            Get the player and current track status
        **QUIT**              Cleanly shutdown

    And these commands can be used to manipulate the playlist:
        **PL_READ**          Print the contents of the playlist
        **PL_APPEND**        Append stdin to the playlist
        **PL_PREPEND**       Prepend stdin to the playlist
        **PL_WRITE**         Overwrite the playlist with stdin
        **PL_EDIT**          Edit the playlist with \$__VISUAL__, or
                         "\$__TERMINAL__ -e \$__EDITOR__" if \$__VISUAL__ isn't set

    All these commands are case insensitive (translated before transmission).


**OPTIONS**
    **-h**
        Print this notice and exit.

    **-l** __FILE__
        Set the playlist path. Override the \$__MUS_PLAYLIST__ environment
        variable.

    **-u** __SOCKET__
        Set the IPC UNIX domain socket path. Override the \$__MUS_SOCKET__
        environnement variable.

**ENVIRONMENT**
    __MUS_PLAYLIST__
        Album playlist path. Defaults to
        \${XDG_CONFIG_HOME:-\$HOME/.config}/mus/playlist.

    __MUS_SOCKET__
        IPC UNIX domain socket path. Defaults to /tmp/mus.sock.

EOF
	exit $1
}

playlist=${MUS_PLAYLIST:-${XDG_CONFIG_HOME:-$HOME/.config}/mus/playlist}
udsock=/tmp/mus.sock
pidfile=/tmp/mus_daemon.pid

while getopts "l:u:h" OPT
do
	case "$OPT" in
		l)
			playlist=$OPTARG
			;;
		u)
			udsock=$OPTARG
			;;
		h)
			usage 0
			;;
		\?)
			usage 1
			;;
	esac
done
shift $((OPTIND - 1))
[ $# -ne 1 ] && usage 1

arg=$(toupper "$1")
case "$arg" in
	TRACK_NEXT|TRACK_PREV|TRACK_FIRST|TRACK_REWIND|PAUSE|PLAY|TOGGLE_PLAY_PAUSE|STATUS|QUIT)
		cmd=$arg
		;;
	ALBUM_NEXT)
		cmd=QUIT
		;;
	*)
		[ ! -f "$playlist" ] &&
			{ mkdir -p -- "$(dirname -- "$playlist")"; touch -- "$playlist";}
		case "$arg" in
			PL_READ)
				flock_try_cmd 'cat -- "$1"' "$playlist"
				;;
			PL_APPEND)
				flock_try_cmd 'cat >>"$1"' "$playlist"
				;;
			PL_PREPEND)
				flock_try_cmd 'printf "%s\n" "$(cat - "$1")" >"$1"' "$playlist"
				;;
			PL_WRITE)
				flock_try_cmd 'cat >"$1"' "$playlist"
				;;
			PL_EDIT)
				flock_try_cmd '$1 "$2"' "${VISUAL:-$TERMINAL -e $EDITOR} --" \
					"$playlist"
				;;
			*)
				die "$1: unknown command"
				;;
		esac
		exit
		;;
esac


requirefile -f "$pidfile"
[ "$arg" = QUIT ] && kill -- "$(cat -- "$pidfile")"

if [ -S "$udsock" ]
then
	ans=$("$rpath_cbin"/mus_udsend "$udsock" "$cmd")
	if [ "$cmd" = STATUS ]
	then
		echop "$ans"
	else
		if [ "$ans" != "OK" ]
		then
			die "$ans: unexpected server answer to command $cmd"
		fi
	fi
fi
elif [ "$arg" != QUIT ]
then
	requirefile -S "$udsock" # To get the problem error message
fi