Skip to content

Instantly share code, notes, and snippets.

@ACamposPT
Last active May 12, 2020 14:48
Show Gist options
  • Save ACamposPT/fddb8512d72782aec5073efb93d549ec to your computer and use it in GitHub Desktop.
Save ACamposPT/fddb8512d72782aec5073efb93d549ec to your computer and use it in GitHub Desktop.
#!/bin/sh
# Get input device info
# This script parse the output of command:
# /proc/bus/input/devices
#
# How to use:
# . /input-device-info.sh
# MY_DEVICE_PATH=$(inputDeviceEventHandlerPath "my-device-name")
# echo 'MY_DEVICE_PATH:' ${MY_DEVICE_PATH}
inputDevicesInfoFilePath ()
{
echo "/proc/bus/input/devices"
}
inputDevicesInfo ()
{
cat $(inputDevicesInfoFilePath)
}
# arguments: device name, file line prefix
inputDeviceValue ()
{
# constants
local INFO_FILE=$(inputDevicesInfoFilePath)
local NAME_PREFIX="N: Name="
# name the function arguments
local devName=$1
local linePrefix=$2
# find the line number in the info file containing
# both the name prefix and device name argument
local lnNo=$(grep -n "${NAME_PREFIX}" ${INFO_FILE} | grep ${devName} | head -n1 | cut -d: -f1)
# starting from the line number previously determined,
# find the first line which contains the prefix argument
# and extract the value token from that line
local value=$(tail +${lnNo} ${INFO_FILE} | grep "${linePrefix}" | head -n1 | cut -d= -f2)
# "return" the value via an echo
# if no value was found, don't echo anything
# but (literally) return an error code
if [ -z "${value}" ] ; then return 1; fi;
echo ${value}
}
# arguments: device name
inputDeviceHandlers ()
{
echo $(inputDeviceValue $1 "H: Handlers=")
}
# arguments: device name
inputDeviceEventHandlerPath ()
{
# constants
local INPUT_DEVICE_DIR_PATH="/dev/input/"
# get the handlers for the device (as a space delimited list)
# if nothing is found return error code 1 and don't echo anything
local handlers=$(inputDeviceHandlers $1)
if [ -z "${handlers}" ] ; then return 1; fi;
# interate through the list (splits on white space implictly)
for handler in ${handlers}
do
# if the handler starts with "event", then echo the path
# and return from the function successfully
case ${handler} in event*)
echo ${INPUT_DEVICE_DIR_PATH}${handler}
return
esac
done
# if no event handler was found, don't echo anything
# but (literally) return an error code
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment