forked from Exarkuniv/RetroPie-Extra
/
bgm123.sh
258 lines (222 loc) · 7.84 KB
/
bgm123.sh
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
#!/usr/bin/env bash
# This file is part of The RetroPie Project
#
# The RetroPie Project is the legal property of its developers, whose names are
# too numerous to list here. Please refer to the COPYRIGHT.md file distributed with this source.
#
# See the LICENSE.md file at the top-level directory of this distribution and
# at https://raw.githubusercontent.com/RetroPie/RetroPie-Setup/master/LICENSE.md
#
rp_module_id="bgm123"
rp_module_desc="Straightforward background music player using mpg123"
rp_module_help="Place your MP3 files in $datadir/bgm"
rp_module_section="exp"
rp_module_flags="!all rpi"
function _autostart_bgm123() {
echo "$configdir/all/autostart.sh"
}
function _bashrc_bgm123() {
echo "$home/.bashrc"
}
function _onstart_bgm123() {
echo "$configdir/all/runcommand-onstart.sh"
}
function _onend_bgm123() {
echo "$configdir/all/runcommand-onend.sh"
}
function _gamelist_bgm123() {
local xmlfile="$datadir/retropiemenu/gamelist.xml"
[[ -f "$xmlfile" ]] || xmlfile="$configdir/all/emulationstation/gamelists/retropie/gamelist.xml"
echo "$xmlfile"
}
function depends_bgm123() {
getDepends mpg123
}
function install_bin_bgm123() {
local fadescript="bgm_vol_fade.sh"
local menuconf="$md_inst/$md_id.menuconf"
local autoconf="$configdir/all/$md_id.cfg"
cp "$md_data/$fadescript" "$md_inst"
cp "$md_data/menu.sh" "$md_inst/$md_id.sh"
cp -f "$md_data/icon.png" "$datadir/retropiemenu/icons/$md_id.png"
sed -i 's|.*#menuconf.*|source "'"$menuconf"'" #menuconf|' "$md_inst/$md_id.sh"
sed -i 's|.*#autoconf.*|source "'"$autoconf"'" #autoconf|' "$md_inst/$md_id.sh"
sed -i 's|.*#autoconf.*|source "'"$autoconf"'" #autoconf|' "$md_inst/$fadescript"
ln -sf "$md_inst/$md_id.sh" "$datadir/retropiemenu/$md_id.sh"
chown -R $user:$user "$datadir/retropiemenu"
}
function configure_bgm123() {
local autostart
autostart="$(_autostart_bgm123)"
local bashrc
bashrc="$(_bashrc_bgm123)"
local onstart
onstart="$(_onstart_bgm123)"
local onend
onend="$(_onend_bgm123)"
local gamelist
gamelist="$(_gamelist_bgm123)"
local menuconf="$md_inst/$md_id.menuconf"
local autoconf="$configdir/all/$md_id.cfg"
local share="$datadir/bgm"
local file
[[ "$md_mode" == "remove" ]] && return
for file in "$autostart" "$bashrc" "$onstart" "$onend"; do
# preserve original file versions
if [[ -f "$file" && ! -f "$file.old.$md_id" ]]; then
cp -v "$file" "$file.old.$md_id"
chown $user:$user "$file.old.$md_id"
fi
done
cat > "$menuconf" << _EOF_
# Menu variables for bgm123
__backtitle="$__backtitle"
autostart="$autostart"
bashrc="$bashrc"
onstart="$onstart"
onend="$onend"
rootdir="$rootdir"
configdir="$configdir"
scriptdir="$scriptdir"
datadir="$datadir"
md_inst="$md_inst"
user="$user"
_EOF_
chown $user:$user "$menuconf"
local tmp
tmp="$(mktemp)"
iniConfig "=" '"' "$tmp"
echo -e '# Configuration file for bgm123\n' >> "$tmp"
iniSet "installed" "0"
iniSet "music_player" "mpg123"
iniSet "mapped_volume" "1"
copyDefaultConfig "$tmp" "$autoconf"
rm -f "$tmp"
if ! grep "<path>./$md_id.sh</path>" "$gamelist" >/dev/null; then
xmlstarlet ed -L -s "/gameList" -t elem -n "gameTMP" \
-s "//gameTMP" -t elem -n path -v "./$md_id.sh" \
-s "//gameTMP" -t elem -n name -v "Background Music" \
-s "//gameTMP" -t elem -n desc -v "Configure and control background music player. Enable or disable menu music while browsing and pause, resume, or skip current track." \
-s "//gameTMP" -t elem -n image -v "./icons/$md_id.png" \
-r "//gameTMP" -v "game" \
"$gamelist"
fi
mkUserDir "$share"
add_share_samba "bgm" "$share"
restart_samba
iniConfig "=" '"' "$autoconf"
iniGet "installed"
[[ "$ini_value" -eq 0 ]] && enable_bgm123
iniSet "installed" "1"
}
function enable_bgm123() {
local autostart
autostart="$(_autostart_bgm123)"
local bashrc
bashrc="$(_bashrc_bgm123)"
local onstart
onstart="$(_onstart_bgm123)"
local onend
onend="$(_onend_bgm123)"
local fadescript="$md_inst/bgm_vol_fade.sh"
local file
disable_bgm123
for file in "$autostart" "$bashrc" "$onstart" "$onend"; do
touch "$file"
chown $user:$user "$file"
done
echo -e "$(echo -e 'while pgrep omxplayer >/dev/null; do sleep 1; done #bgm123\n((vcgencmd force_audio hdmi 1) >/dev/null; sleep 6; (pgrep emulationstatio >/dev/null && mpg123 -Z "'$datadir'/bgm/"*.[mM][pP]3 >/dev/null 2>&1)) & #bgm123'; cat $autostart)" > "$autostart"
echo '[[ "$(tty)" == "/dev/tty1" ]] && ((vcgencmd force_audio hdmi 0) >/dev/null; sleep 0.5; pkill mpg123) #bgm123' >> "$bashrc"
echo 'bash "'"$fadescript"'" -STOP & #bgm123' >> "$onstart"
echo '(sleep 1; "bash '"$fadescript"'" -CONT) & #bgm123' >> "$onend"
}
function disable_bgm123() {
local autostart
autostart="$(_autostart_bgm123)"
local bashrc
bashrc="$(_bashrc_bgm123)"
local onstart
onstart="$(_onstart_bgm123)"
local onend
onend="$(_onend_bgm123)"
local file
# kill player now since .bashrc won't do it later
(vcgencmd force_audio hdmi 0) >/dev/null
sleep 0.5
pkill mpg123
for file in "$autostart" "$bashrc" "$onstart" "$onend"; do
if [[ -f "$file" ]]; then
# backup file and attempt to remove any existing bgm config
cp -f "$file" "$file.bak"
chown $user:$user "$file.bak"
sed -i '/#bgm/d' "$file"
# if file is now empty, remove it
[[ ! -s "$file" ]] && rm -f "$file"
fi
done
}
function remove_bgm123() {
local gamelist
gamelist="$(_gamelist_bgm123)"
rm -f "$datadir/retropiemenu/$md_id.sh" "$datadir/retropiemenu/icons/$md_id.png"
xmlstarlet ed -L -d "/gameList/game[contains(path,'$md_id.sh')]" "$gamelist"
disable_bgm123
remove_share_samba "bgm"
restart_samba
}
function play_pause_bgm123() {
if pgrep mpg123 >/dev/null; then
su "$user" -c "bash $md_inst/bgm_vol_fade.sh &"
else
su "$user" -c "((vcgencmd force_audio hdmi 1) >/dev/null; sleep 0.5; mpg123 -Z $datadir/bgm/*.[mM][pP]3 >/dev/null 2>&1) &"
fi
}
function next_track_bgm123() {
pkill mpg123
su "$user" -c "((vcgencmd force_audio hdmi 1) >/dev/null; sleep 0.5; mpg123 -Z $datadir/bgm/*.[mM][pP]3 >/dev/null 2>&1) &"
}
function gui_bgm123() {
local cmd=(dialog --backtitle "$__backtitle" --cancel-label "Back" --menu "Configuration for $md_id. Please choose an option." 22 86 16)
while true; do
local enabled=0
grep '#bgm123' "$configdir/all/autostart.sh" >/dev/null && enabled=1
local options=()
if [[ "$enabled" -eq 1 ]]; then
options+=(
E "Enable or disable background music (currently: Enabled)"
)
if pgrep emulationstatio >/dev/null; then
options+=(
P "Play / pause"
N "Next track"
)
fi
else
options+=(
E "Enable or disable background music (currently: Disabled)"
)
fi
local choice=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
if [[ -n "$choice" ]]; then
case "$choice" in
E)
if [[ "$enabled" -eq 1 ]]; then
disable_bgm123
printMsgs "dialog" "Background music disabled."
else
enable_bgm123
printMsgs "dialog" "Background music enabled."
fi
;;
P)
play_pause_bgm123
;;
N)
next_track_bgm123
;;
esac
else
break
fi
done
}