Phosh: Automatically disable the soft keyboard when the Pine64 keyboard shell is attached

If you’re using a Phosh-based distro that runs SystemD such as Mobian follow the guide here.

If you’re using a distro running OpenRC such as PostmarketOS Phosh follow the guide below:

As OpenRC can only run commands as root it was quite a challenge getting the environment variables right so gsettings could be run as the user but I got it figured out. :slight_smile:

Guide: Pinephone PostmarketOS w/ Phosh: Automatically disable the soft keyboard when the pine64 keyboard shell is attached and visa versa

Create the script:

touch /usr/local/bin/soft-kbd-override
chmod +x /usr/local/bin/soft-kbd-override
vi /usr/local/bin/soft-kbd-override

# Paste the following and save:
#!/bin/sh

# Check interval is either the first parameter or every 10 seconds
CheckInterval=${1:-10}

# Attempt to get gnome-session pid till successful (phone startup)
while ! PID=$( pgrep gnome-sesson ); do
	sleep 3
done
export DBUS_SESSION_BUS_ADDRESS=$( xargs -0 -n 1 -a /proc/$PID/environ | grep '^DBUS_SESSION_BUS_ADDRESS=' | cut -d = -f2- )
sleep 5

PrevState=
while true; do
	cat /sys/class/power_supply/ip5xxx-battery/voltage_now 2>&1 >/dev/null && State='false' || State='true'

	if [ "$State" != "$PrevState" ]; then
		sudo \
				--user user \
				--set-home \
				--preserve-env \
				gsettings set org.gnome.desktop.a11y.applications screen-keyboard-enabled "$State" \
		&& PrevState=$State
	fi

	sleep "$CheckInterval"
done
# Test the script: (Ctrl+C to quit)
soft-kbd-override

Create the service to run the script:

vi /etc/init.d/soft-kbd-override

# Paste the following and save:
#!/sbin/openrc-run
name='Soft Keyboard Override'
description='Turns off the soft keyboard if a pine64 keyboard shell is detected'

command='/usr/local/bin/soft-kbd-override'
command_background='yes'
pidfile='/run/'$RC_SVCNAME'.pid
# Run soft-kbd-override, there should be no errors or hanging
rc-service soft-kbd-override start

# Enable soft-kbd-override at startup at the default runlevel
rc-update add soft-kbd-override
1 Like