-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream-helper
executable file
·271 lines (241 loc) · 4.82 KB
/
stream-helper
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/bin/bash
# Written by skiqqy
# Usage: stream-helper -h
# Purpose: Uses dmenu to ineract and control a stream instance.
socket=/tmp/mpvsoc
deps=( jq dmenu socat ss ) # List of dependencies needed by stream-helper
commands=( pp svol ) # Available commands in tui/gui mode. DO NOT CHANGE, names cannot have spaces.
version=beta-1.0v
usage ()
{
cat << EOF
Stream-Helper $version ~ The simple stream helper utility
Usage: stream-helper [options] [pid]
options
-------
-h Shows this message.
-s Specify the socket to use.
-d See the dependencies needed for full functionality (prints a message if one is missing).
-t Force tty mode.
-c List the commands and what they do.
Written by skiqqy
EOF
exit "$1"
}
# Prints information on commands.
# Usage help [command]
# If no command is given, print all commands.
help ()
{
if [ -n "$1" ]
then
print_command "$1"
return
fi
for c in ${commands[*]}
do
print_command "$c"
done
}
print_command ()
{
case "$1" in
pp)
echo -e "$1"': Pause/Play current stream instance.\n\t- Usage: pp [time]'
;;
svol)
echo -e "$1"': Adjust stream instance volume.\n\t- Usage: svol [(+|-) [amount]] [set to]'
;;
*)
echo -e "$1: Description $1 not yet implemented."
;;
esac
}
# Prints the volume of stream instance
getvol () {
echo '{ "command": ["get_property", "volume"] }' | socat - "$socket" | jq .data
}
# Control/Set the volume by an (optional) amount, or by 2 if none given.
# Usage: svol [(+|-) [amount]] [set to]
svol ()
{
if [ "$#" -eq 0 ]
then
vol=50 # No commands -> set volume to 50%
fi
case "$1" in
+)
opp=+
;;
-)
opp=-
;;
*)
if [ -z "$3" ] && [ ! "$#" -eq 0 ]
then
echo "Empty first argument requires non empty third arg."
return 1 # Check valid first argument
fi
;;
esac
[ -z "$2" ] && inc=2 || inc="$2"
ovol=$(getvol)
if [ -z "$3" ] && [ ! "$#" -eq 0 ]
then
vol=$(("$ovol" "$opp" "$inc")) # Calculate the volume change.
elif [ -n "$3" ]
then
vol="$3"
fi
if [[ "$vol" -gt 100 ]] || [[ $vol -lt 0 ]]
then
echo "$vol is an invalid volume value"
return 1
fi
echo '{ "command": ["set_property", "volume", '"$vol"'] }' | socat - "$socket" > /dev/null
echo "[VOLUME] $ovol changed to $vol"
}
# Pause/Play the current stream instance (for an optional time)
# Usage: pp [time]
pp ()
{
echo 'cycle pause' | socat - "$socket"
if [[ "$1" =~ ^[0-9]+$ ]]
then
sleep "$1"
echo 'cycle pause' | socat - "$socket"
fi
}
# This is just for testing
debug ()
{
svol + 10
svol - 10
svol - - 50
pp # Testing hard pause, no time given
sleep 1 # Stream should pause for 1 second
pp
sleep 3 # Play for 3 second
pp 2 # Sleep for 2 seconds
}
# Run a command, acts as a little safety net to prevent nasty stuff
# Usage: run "command <command flags>"
run ()
{
case "$1" in
pp*|svol*|help*) # User can type pp "flags" directly
$1 # I want splitting
;;
*)
echo "Invalid command"
return 1
;;
esac
}
# Gui eventloop that does gui things using dmenu
gui ()
{
coms="${commands[*]}"
input=$(echo -e "${coms// /\\n}" | dmenu -l ${#commands[*]} -p 'Enter Command:') # Get input
run "$input"
}
# Basically just a subshell of this shell lol
tty ()
{
if read -rp ">>> " input
then
run "$input"
else
return 2
fi
}
# Assert that dependecies are met. And print what they are needed for, shows a
# warning if a dependecy is missing.
check_deps ()
{
missing=0
for d in ${deps[*]}
do
if ! command -v "$d"
then
missing=1
echo "[WARNING] $d is missing."
fi
# Explain what it is needed for.
case "$d" in
dmenu)
msg='Used to draw the gui, this is optional, if you dont have it installed you can just use tty mode.'
;;
socat)
msg='Used to communicate with the socket, hard dependency.'
;;
ss)
msg='Used to check that the socket exists and is open, semi-optional.'
;;
jq)
msg='Used to parse the json returned by mpv. Mandatory.'
;;
*)
msg='Description for this dependency not given.'
;;
esac
echo -e "\t- $msg"
done
return "$missing"
}
main ()
{
# Vars
opts="hs:dtc"
tty=false
while getopts "$opts" opt
do
case "$opt" in
h)
usage 0
;;
s)
socket="$OPTARG"
;;
d)
check_deps
exit
;;
t)
tty=true
;;
c)
help # I dont pass commands on purpose
exit 0
;;
*)
usage 1
;;
esac
done
# Assert that dependencies are met
if ! check_deps > /dev/null
then
echo -e "Warning, you are missing some dependencies. This script may potentially break.\nPlease check $0 -d"
fi
if ! ss -l | grep "$socket" > /dev/null
then
echo "Socket '$socket' not found! Exiting."
exit 1
fi
if "$tty"
then
echo -e "stream-helper $version\nType \"help\" or \"help <command>\" for help"
for ((;;))
do
tty
if [ "$?" -eq 2 ] # 2 means exit
then
break
fi
done
else
gui
fi
}
main "$@"