pmaports/main/ttyescape/togglevt.sh
Caleb Connolly 20f93d942d
main/ttyescape: new aports (MR 2309)
Add ttyescape, a script and triggerhappy configuration to allow mobile device
users to access and use a shell without having to plug in to a computer.

One of the largest limitations with a mobile device is the lack of keyboard, for
mainstream OSs like Android and iOS, this is a non-issue as the whole OS stack
is built to automatically recover in case of a crash / hang, hiding the internal
state of affairs from users and making use of careful design to minimise the
impact.  When bringing Linux to mobile, we carry not only the benefits of the
Linux desktop but also it's limitations. In the event that your desktop manager
goes haywire or hangs completely, or your graphics drivers get unhappy, the
ability to quickly jump to a tty and start killing bad behaving programs or
reset your display manager is one that most of us take for granted.  But when
hit by similar errors on a mobile device there is no such recourse available,
users either have to reboot and hope that the issue doesn't occur again, or pull
out a laptop and pull up a shell (assuming ssh is enabled and the rndis
interface comes up).

ttyescape proposes to solve this issues by pieceing together several already
available tools, notably:
 - triggerhappy, a tool used to perform actions when
certain buttons or key combinations are pressed with no dependencies on the
display manager in use.
 - fbkeyboard, a framebuffer keyboard for tty's, it
renders on top of the current tty and uses the device touchscreen as input.
2021-08-09 23:57:24 -07:00

77 lines
2 KiB
Bash

#!/bin/sh
# Toggle between tty1 and tty2, launching fbkeyboard when on tty2
# THIS SCRIPT MUST BE RUN AS ROOT
# usage:
# togglevt.sh <state>
# where <state> is an optional arg to require that a counter be incremented before the action
# is performed. The default configuration will perform the switch when the power button has
# been pressed 3 times whilst the volume down button is being held.
# if no arguments are specified the switch will occur immediately.
[ "$(whoami)" != root ] && echo "This must be run as root" && exit 1
# shellcheck disable=SC1091
test -f /etc/conf.d/ttyescape.conf && . /etc/conf.d/ttyescape.conf
# default font, override this by setting it in /etc/conf.d/ttyescape.conf
FONT="${FONT:-/usr/share/consolefonts/ter-128n.psf.gz}"
# amount of times power must be pressed to trigger
PRESSCOUNT="${PRESSCOUNT:-3}"
TMPFILE="${TMPFILE:-/tmp/ttyescape.tmp}"
if [ ! -e /dev/uinput ]; then
if ! modprobe -q uinput; then
echo "uinput module not available, please enable it in your kernel"
fi
fi
switchtty() {
currentvt=$(cat /sys/devices/virtual/tty/tty0/active)
if [ "$currentvt" = "tty2" ]; then # switch to tty1 with normal UI
chvt 1
killall fbkeyboard
else # Switch to tty2 with fbkeyboard
setfont "$FONT" -C /dev/tty2
chvt 2
# sometimes fbkeyboard can be running already, we shouldn't start it in that case
[ "$(pgrep fbkeyboard)" ] || nohup fbkeyboard -r "$(cat /sys/class/graphics/fbcon/rotate)" &
fi
}
# If we receive a command that isn't start
# and we don't have the file used to count
# then we should do nothing
if [ -n "$1" ] && [ "$1" != "start" ] && [ ! -f "$TMPFILE" ]; then
exit 0
fi
case "$1" in
# No args means just DO IT
"")
switchtty
;;
# Start counting, this should
# run when voldown is pressed
"start")
echo "0" > "$TMPFILE"
;;
# Run when voldown releases
"reset")
rm "$TMPFILE"
;;
# Run when power pressed while
# voldown is pressed
"inc")
val="$(cat "$TMPFILE")"
val=$((val+1))
if [ $val -eq "$PRESSCOUNT" ]; then
rm "$TMPFILE"
switchtty
else
echo "$val" > "$TMPFILE"
fi
;;
*)
esac