Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions PKGBUILDS/pine64/acpi-kbd-autolock-pinetab2/PKGBUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Maintainer: ScottFreeCode <scottfreecode@gmail.com>
pkgname=acpi-kbd-autolock-pinetab2
pkgver=0.1.0
pkgrel=1
pkgdesc="Automatically turn off the hardware/case keyboard when it is flipped shut (or behind the tablet), for the PineTab 2"
arch=(any)
url="https://github.com/dreemurrs-embedded/Pine64-Arch"
license=('BSD')
depends=('acpid')
source=(lid toggle-keyboard.sh)

package() {
install -D -m644 "$srcdir"/lid \
"$pkgdir"/etc/acpi/events/lid
install -D -m755 "$srcdir"/toggle-keyboard.sh \
"$pkgdir"/etc/acpi/toggle-keyboard.sh
}

install=acpi-kbd-autolock-pinetab2.install

md5sums=('fdac6076a6c9cf9e3fad89302fc06cf9'
'be9a6fb2532a09e22226b2c148841909')
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
post_install() {
systemctl enable --now acpid
}
2 changes: 2 additions & 0 deletions PKGBUILDS/pine64/acpi-kbd-autolock-pinetab2/lid
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
event=button/lid
action=/etc/acpi/toggle-keyboard.sh %e
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to use logind for this? iirc it has an option to handle lid switch events, and that way you don't need to add an extra dependency.

Copy link
Copy Markdown
Author

@ScottFreeCode ScottFreeCode Jul 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have to dig up my research, but from what I read the systemd login interface only lets you choose from a predefined list of behaviors – screen off, suspend, shutdown, etc. – and there's theoretically a way to redefine those behaviors (suspend.target, etc.) but I didn't really think that was appropriate (anything more than screen off, e.g. suspend, should prevent keyboard usage anyway; and more importantly people's screens might be configured to turn off via other conditions as well), plus I didn't see an event that can turn the keyboard back on. As far as I was able to find out, ACPI is intended to be the lower level interface for more control of customization like this.

58 changes: 58 additions & 0 deletions PKGBUILDS/pine64/acpi-kbd-autolock-pinetab2/toggle-keyboard.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/sh

# NOTE: if the "on"/reactivate version of this command for some reason cannot
# find the saved keyboard USB ID or if the saved ID somehow fails to turn the
# keyboard back on (as indicated by a search for it again in the devices), this
# script will fall back to turning on ALL usb ports.
#
# If for some reason you NEED a usb port to stay off even in the event of an
# error from this script, you will want to uninstall this package!
#
# Most users it won't hurt to turn on all USB ports though; if you're not sure
# whether it's safe to use this, it should be safe.

ACTION=bind
MESSAGE=on
if [[ "$3" = "close" ]]
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we had a command (let's just call it lid-closed for example) that detected whether the keyboard's current state is flipped shut, then we could call this script on boot with the following modification:

Suggested change
if [[ "$3" = "close" ]]
if [[ "$3" = "close" ]] || lid-closed

then
ACTION=unbind
MESSAGE=off
fi

KEYBOARD=`grep -r --include=uevent Touchpad /sys/devices/ | cut -d/ -f7 | grep -v : | sort | uniq`

if [ -z "$KEYBOARD" -a -e /var/keyboard-usb-port ]
then
KEYBOARD=`cat /var/keyboard-usb-port`
fi

if [ -z "$KEYBOARD" ] && [[ "$ACTION" = "unbind" ]]
then
for user in `who | cut -f1 -d\ | sort | uniq`
do
sudo -u "$user" DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/"`id -u $user`"/bus \
notify-send -a 'PineTab2 case' "UNABLE TO FIND KEYBOARD USB PORT."
done
set -e
false
fi

if ! [ -z "$KEYBOARD" ]
then
echo -n "$KEYBOARD" > /var/keyboard-usb-port
echo "$KEYBOARD" > /sys/bus/usb/drivers/usb/"$ACTION"
fi

if [[ "$ACTION" = "bind" ]] && ! grep -r --include=uevent Touchpad /sys/devices/ 2>&1 >/dev/null
then
for device in `ls /sys/bus/usb/devices`
do
echo "$device" > /sys/bus/usb/drivers/usb/bind 2>/dev/null
done
fi

for user in `who | cut -f1 -d\ | sort | uniq`
do
sudo -u "$user" DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/"`id -u $user`"/bus \
notify-send -a 'PineTab2 case' "Keyboard turned $MESSAGE."
done