-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch.sh
215 lines (190 loc) · 6.4 KB
/
launch.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
#!/bin/sh
echo "$0" "$@"
progdir="$(dirname "$0")"
cd "$progdir" || exit 1
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$progdir/lib"
echo 1 >/tmp/stay_awake
trap "rm -f /tmp/stay_awake" EXIT INT TERM HUP QUIT
BUTTON_LOG="$progdir/log/buttons.log"
SERVICE_NAME="remote-term"
HUMAN_READABLE_NAME="Remote Terminal"
SUPPORTS_DAEMON_MODE=1
service_on() {
cd /mnt/SDCARD/ || exit
if [ -f "$progdir/log/service.log" ]; then
mv "$progdir/log/service.log" "$progdir/log/service.log.old"
fi
port=8080
if [ -f "$progdir/port" ]; then
port="$(cat "$progdir/port")"
fi
if [ -z "$port" ]; then
port=8080
fi
shell="$SHELL"
if [ -f "$progdir/shell" ]; then
shell="$(cat "$progdir/shell")"
fi
if [ -z "$shell" ]; then
shell="/bin/sh"
fi
PORT="$port" SHELL="$shell" "$progdir/bin/remote-term" >"$progdir/log/service.log" 2>&1 &
}
service_off() {
killall "$SERVICE_NAME"
}
show_message() {
message="$1"
seconds="$2"
if [ -z "$seconds" ]; then
seconds="forever"
fi
killall sdl2imgshow
echo "$message"
if [ "$seconds" = "forever" ]; then
"$progdir/bin/sdl2imgshow" \
-i "$progdir/res/background.png" \
-f "$progdir/res/fonts/BPreplayBold.otf" \
-s 27 \
-c "220,220,220" \
-q \
-t "$message" &
else
"$progdir/bin/sdl2imgshow" \
-i "$progdir/res/background.png" \
-f "$progdir/res/fonts/BPreplayBold.otf" \
-s 27 \
-c "220,220,220" \
-q \
-t "$message"
sleep "$seconds"
fi
}
monitor_buttons() {
if [ -f "$BUTTON_LOG" ]; then
mv "$BUTTON_LOG" "$BUTTON_LOG.old"
fi
touch "$BUTTON_LOG"
chmod +x "$progdir/bin/evtest"
for dev in /dev/input/event*; do
[ -e "$dev" ] || continue
"$progdir/bin/evtest" "$dev" 2>&1 | while read -r line; do
if echo "$line" | grep -q "code 17 (ABS_HAT0Y).*value -1"; then
echo "D_PAD_UP detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 17 (ABS_HAT0Y).*value 1"; then
echo "D_PAD_DOWN detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 16 (ABS_HAT0X).*value 1"; then
echo "D_PAD_RIGHT detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 16 (ABS_HAT0X).*value -1"; then
echo "D_PAD_LEFT detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 308 (BTN_WEST).*value 1"; then
echo "BUTTON_X detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 305 (BTN_EAST).*value 1"; then
echo "BUTTON_A detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 304 (BTN_SOUTH).*value 1"; then
echo "BUTTON_B detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 307 (BTN_NORTH).*value 1"; then
echo "BUTTON_Y detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 317 (BTN_THUMBL).*value 1"; then
echo "HOTKEY_1 detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 318 (BTN_THUMBR).*value 1"; then
echo "HOTKEY_2 detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 310 (BTN_TL).*value 1"; then
echo "L1 detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 311 (BTN_TR).*value 1"; then
echo "R1 detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 2 (ABS_Z).*value 255"; then
echo "L2 detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 5 (ABS_RZ).*value 255"; then
echo "R2 detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 316 (BTN_MODE).*value 1"; then
echo "MENU detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 314 (BTN_SELECT).*value 1"; then
echo "SELECT detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 315 (BTN_START).*value 1"; then
echo "START detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 115 (KEY_VOLUMEUP).*value 1"; then
echo "VOLUME_UP detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 114 (KEY_VOLUMEDOWN).*value 1"; then
echo "VOLUME_DOWN detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 116 (KEY_POWER).*value 1"; then
echo "POWER detected" >>"$BUTTON_LOG"
fi
done &
done
}
wait_for_button() {
button="$1"
while true; do
if grep -q "$button" "$BUTTON_LOG"; then
break
fi
sleep 0.1
done
}
wait_for_service() {
max_counter="$1"
counter=0
while ! pgrep "$SERVICE_NAME" >/dev/null 2>&1; do
counter=$((counter + 1))
if [ "$counter" -gt "$max_counter" ]; then
return 1
fi
sleep 1
done
}
main_daemonize() {
echo "Toggling $SERVICE_NAME..."
if pgrep "$SERVICE_NAME"; then
show_message "Disabling the $HUMAN_READABLE_NAME" 2
service_off
else
show_message "Enabling the $HUMAN_READABLE_NAME" 2
service_on
if ! wait_for_service 10; then
show_message "Failed to start $HUMAN_READABLE_NAME" 2
return 1
fi
fi
show_message "Done" 1
}
main_process() {
if pgrep "$SERVICE_NAME"; then
show_message "Disabling the $HUMAN_READABLE_NAME" 2
service_off
fi
show_message "Starting $HUMAN_READABLE_NAME" 2
service_on
sleep 1
echo "Waiting for $HUMAN_READABLE_NAME to be running"
if ! wait_for_service 10; then
show_message "Failed to start $HUMAN_READABLE_NAME" 2
return 1
fi
show_message "Press B to exit"
monitor_buttons
wait_for_button "BUTTON_B"
show_message "Stopping $HUMAN_READABLE_NAME"
service_off
killall evtest
sync
sleep 1
show_message "Done" 1
}
main() {
if [ "$SUPPORTS_DAEMON_MODE" -eq 0 ]; then
service_on
return $?
fi
if [ -f "$progdir/daemon-mode" ]; then
main_daemonize
else
main_process
fi
killall sdl2imgshow
}
mkdir -p "$progdir/log"
if [ -f "$progdir/log/launch.log" ]; then
mv "$progdir/log/launch.log" "$progdir/log/launch.log.old"
fi
main "$@" >"$progdir/log/launch.log" 2>&1