Skip to content

Instantly share code, notes, and snippets.

@jigpu
Created April 8, 2022 23:36
Show Gist options
  • Save jigpu/6522366d477ea53cb390feab08eac494 to your computer and use it in GitHub Desktop.
Save jigpu/6522366d477ea53cb390feab08eac494 to your computer and use it in GitHub Desktop.
Simultaneously capture HID and libinput events from multiple devices
#!/bin/bash
# Usage: capture-multi.sh
# Simultaneously capture HID and libinput events from multiple devices
#
# This script must be run as root in order to read all events.
set -e
trap cleanup INT QUIT TERM
function cleanup {
echo
echo "Archiving capture data..."
sleep 0.1
jobs -p | xargs kill 2>/dev/null || true
wait
tar cvzf record_${NOW}.tar.gz record_${NOW}.*.log*
rm record_${NOW}.*.log*
echo
echo "Files have been archived to $(pwd)/record_$NOW.tar.gz"
echo "Please upload this file for analysis."
}
function hidraw_events {
ID=$(basename ${1})
for P in /sys/class/hidraw/${ID}/device/input/input*; do
for Q in $(ls -d ${P}/event*); do
echo "/dev/input/$(basename ${Q})"
done
done
}
function event_name {
ID=$(basename ${1})
cat /sys/class/input/${ID}/device/name
}
## Sanity Checks
if [[ "$EUID" -ne 0 ]]; then
echo "This command must be run as root to capture hardware and kernel logs."
exit 1
fi
REC_HID=$(command -v hid-recorder 2>&1 || true)
REC_LIBINPUT=$(libinput record --help >/dev/null 2>&1 && echo "libinput record" || true)
if [[ -z "${REC_LIBINPUT}" ]]; then REC_LIBINPUT=$(libinput-record --help >/dev/null 2>&1 && echo "libinput-record" || true); fi
ERRMSG=""
if [[ -z "${REC_HID}" ]]; then
ERRMSG="${ERRMSG}\n * hid-recorder [Try: \`yum install hid-replay\` (may require EPEL) or https://bentiss.github.io/hid-replay-docs/ ]"
fi
if [[ -z "${REC_LIBINPUT}" ]]; then
ERRMSG="${ERRMSG}\n * libinput record [Try: \`apt-get install libinput-tools\` or \`yum install libinput-utils\` or \`yum install libinput\`]"
fi
if [[ -n "${ERRMSG}" ]]; then
echo "The following utilities are missing and must be installed to capture logs:"
echo "${ERRMSG}"
exit 1
else
echo "Recording with \`${REC_HID}\` and \`${REC_LIBINPUT}\`"
echo
fi
## Device Selection
echo "Available devices:"
for F in /dev/hidraw*; do
echo "${F}"
for E in $(hidraw_events ${F}); do
echo " - $(event_name ${E})"
done
done
echo -n "Choose one or more devices (e.g. \"0\" or \"0 3\")): "
read DEVNUMS
HIDRAW_DEVS=""
LIBINPUT_DEVS=""
for D in ${DEVNUMS}; do
HIDRAW_DEVS="${HIDRAW_DEVS} /dev/hidraw${D}"
LIBINPUT_DEVS="${LIBINPUT_DEVS} $(hidraw_events /dev/hidraw${D})"
done
HIDRAW_DEVS=$(echo ${HIDRAW_DEVS})
LIBINPUT_DEVS=$(echo ${LIBINPUT_DEVS})
echo
## Device Record
NOW=$(date +%s)
echo "Recording the following into temporary files:"
echo " - hid-recorder: ${HIDRAW_DEVS}"
echo " - libinput-record: ${LIBINPUT_DEVS}"
(script -q -c "${REC_HID} ${HIDRAW_DEVS}" -f record_${NOW}.hid.log >/dev/null) &
${REC_LIBINPUT} -o record_${NOW}.libinput.log ${LIBINPUT_DEVS} &
echo
echo "Press CTRL+C to stop recording."
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment