You are not logged in.

#1 2023-09-08 05:49:42

SlothSpunky77
Member
Registered: 2023-03-03
Posts: 47

[SOLVED] tablet mode detection script to enable onboard (or any osk)

I use a Lenovo Ideapad with arch+awesomewm and no desktop environment.
I'm trying to write a bash script to listen to

libinput debug-events --device /dev/input/event4 | grep state

so that I could echo "tablet" and "laptop" as and when I flip my laptop back and forth.
My bash script (I'm new to this):

#!/bin/bash
#
#Check if it's in tablet mode
while true
do
	if [ "$(libinput debug-events --device /dev/input/event4 | grep state)" = "event4   SWITCH_TOGGLE           +0.000s       switch tablet-mode state 1" ]; then
		echo "tablet"
	elif [ "$(libinput debug-events --device /dev/input/event4 | grep state)" = "event4   SWITCH_TOGGLE           +0.000s       switch tablet-mode state 0" ]; then
		echo "laptop"
	else
		echo "unknown"
	fi
	sleep 1
done

I could use

if ! pgrep -x "onboard" > /dev/null; then
		        onboard &
	        fi

maybe in another script to work with this script.
I've been trying to find a way to work with libinput inside scripts but I haven't found any methods. Is there another way to get the same results by checking for a '0' or '1', perhaps inside '/sys/devices/platform/'? I use a Lenovo Ideapad, so I don't have thinkpad-acpi-events as a subdirectory inside platform. Instead, I have one called 'yoga', which I'm guessing is from the yoga-usage-mode package.
I tried to look into iio-sensor-proxy which was of no avail.

Any help is appreciated, thank you very much.

Last edited by SlothSpunky77 (2023-09-11 06:28:04)

Offline

#2 2023-09-08 12:28:42

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,462
Website

Re: [SOLVED] tablet mode detection script to enable onboard (or any osk)

I don't have any tablets to check, but I'd bet that this state is reflected in one of the pseudo-files under /sys/class/input/event4/device/*

But you'd probably be better off with a udev rule to trigger on the state change rather than trying to poll continuously.

EDIT: even if you really do need to listen to `libinput debug-events` there's no reason to poll every second, just wait for the relevant input, e.g.,

libinput debug-events --device /dev/input/event4 | while read line; do
   # handle $line here
done

Last edited by Trilby (2023-09-08 13:18:28)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2023-09-11 06:27:35

SlothSpunky77
Member
Registered: 2023-03-03
Posts: 47

Re: [SOLVED] tablet mode detection script to enable onboard (or any osk)

Awesome, I wrote something like this:

#!/bin/bash
#
#Check if it's in tablet mode
libinput debug-events --device /dev/input/event4 | while read line; do
mode=$(echo "$line" | grep -o 'state [0-9]' | awk '{print $2}')
if [ "$mode" = "1" ]; then
	echo "tablet"
	exit 0
else 
 	echo "laptop"
	exit 0
fi
done

Thanks for the help, I'll add it to udev if needed.

Offline

Board footer

Powered by FluxBB