From 20f93d942d703b33b5d6e1b249f839504b287dcd Mon Sep 17 00:00:00 2001 From: Caleb Connolly Date: Sun, 4 Jul 2021 20:14:58 +0100 Subject: [PATCH] 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. --- main/ttyescape/APKBUILD | 33 ++++++++++ main/ttyescape/etc-conf-d-ttyescape.conf | 8 +++ main/ttyescape/togglevt.sh | 77 ++++++++++++++++++++++ main/ttyescape/ttyescape-triggerhappy.conf | 5 ++ main/ttyescape/ttyescape.post-install | 4 ++ 5 files changed, 127 insertions(+) create mode 100644 main/ttyescape/APKBUILD create mode 100644 main/ttyescape/etc-conf-d-ttyescape.conf create mode 100644 main/ttyescape/togglevt.sh create mode 100644 main/ttyescape/ttyescape-triggerhappy.conf create mode 100644 main/ttyescape/ttyescape.post-install diff --git a/main/ttyescape/APKBUILD b/main/ttyescape/APKBUILD new file mode 100644 index 000000000..7d510ec72 --- /dev/null +++ b/main/ttyescape/APKBUILD @@ -0,0 +1,33 @@ +# Maintainer: Caleb Connolly +pkgname=ttyescape +pkgver=0.1 +pkgrel=0 +pkgdesc="Daemon to allow users to escape to a tty" +url="https://postmarketos.org" +arch="all" +license="GPL-3.0-or-later" +depends="triggerhappy fbkeyboard terminus-font kbd" +install="$pkgname.post-install" +source=" + togglevt.sh + ttyescape-triggerhappy.conf + etc-conf-d-ttyescape.conf +" +options="!check" + +package() { + install -Dm755 "$srcdir"/togglevt.sh \ + "$pkgdir"/usr/bin/togglevt.sh + + install -Dm755 "$srcdir"/ttyescape-triggerhappy.conf \ + "$pkgdir"/etc/triggerhappy/triggers.d/ttyescape.conf + + install -Dm755 "$srcdir"/etc-conf-d-ttyescape.conf \ + "$pkgdir"/etc/conf.d/ttyescape.conf +} + +sha512sums=" +5a56740678073c72c1952e36742fb0786f932e718835c39502ab51f9731426e2b8479de38d4c44c35bc84a895b74ea0dc7bd39fd9eb89b603973ed6e43abb377 togglevt.sh +f8bf3273cf87392ab2092a005417bc58cb3ae6ad25b9118b76c68481d9d8fc7d964a9d16fc7645f6f9ff0676dccd381be3b846464b2e60a6452b8c883bffb6f1 ttyescape-triggerhappy.conf +5429c7029e6db292cccbd4d5fdb4eb07cbbe050fe886784ebb1f88d790de35c06714c2ad38d366d88ed78a09d4d20e0826541f8f578885384ff6b033ac606af8 etc-conf-d-ttyescape.conf +" diff --git a/main/ttyescape/etc-conf-d-ttyescape.conf b/main/ttyescape/etc-conf-d-ttyescape.conf new file mode 100644 index 000000000..e60292b7e --- /dev/null +++ b/main/ttyescape/etc-conf-d-ttyescape.conf @@ -0,0 +1,8 @@ + +# tty font to use +FONT="/usr/share/consolefonts/ter-128n.psf.gz" +# Number of times the power button must be pressed whilst volume down +# is being held to trigger the tty switch +PRESSCOUNT=3 +# tmpfile to use for counting state +TMPFILE="/tmp/ttyescape.tmp" diff --git a/main/ttyescape/togglevt.sh b/main/ttyescape/togglevt.sh new file mode 100644 index 000000000..33229a7be --- /dev/null +++ b/main/ttyescape/togglevt.sh @@ -0,0 +1,77 @@ +#!/bin/sh + +# Toggle between tty1 and tty2, launching fbkeyboard when on tty2 +# THIS SCRIPT MUST BE RUN AS ROOT +# usage: +# togglevt.sh +# where 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 diff --git a/main/ttyescape/ttyescape-triggerhappy.conf b/main/ttyescape/ttyescape-triggerhappy.conf new file mode 100644 index 000000000..ae37533f7 --- /dev/null +++ b/main/ttyescape/ttyescape-triggerhappy.conf @@ -0,0 +1,5 @@ +KEY_POWER+KEY_VOLUMEDOWN 0 /usr/bin/togglevt.sh inc +KEY_VOLUMEDOWN 1 /usr/bin/togglevt.sh start +KEY_VOLUMEDOWN 0 /usr/bin/togglevt.sh reset +# noop to prevent garbage being typed in console +KEY_VOLUMEDOWN 2 /bin/true diff --git a/main/ttyescape/ttyescape.post-install b/main/ttyescape/ttyescape.post-install new file mode 100644 index 000000000..a304cd294 --- /dev/null +++ b/main/ttyescape/ttyescape.post-install @@ -0,0 +1,4 @@ +#!/bin/sh + +# Enable triggerhappy +rc-update -q add triggerhappy default