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