Skip to content

Instantly share code, notes, and snippets.

@jigpu
Created March 5, 2020 23:26
Show Gist options
  • Save jigpu/2cc07e3086de368222a6b9e62c4b4930 to your computer and use it in GitHub Desktop.
Save jigpu/2cc07e3086de368222a6b9e62c4b4930 to your computer and use it in GitHub Desktop.
Print out changes to evdev multitouch state
#!/usr/bin/env python3
import sys
import libevdev
AXES = {
'ABS_MT_TRACKING_ID': libevdev.EV_ABS.ABS_MT_TRACKING_ID,
'ABS_MT_POSITION_X': libevdev.EV_ABS.ABS_MT_POSITION_X,
'ABS_MT_POSITION_Y': libevdev.EV_ABS.ABS_MT_POSITION_Y
}
def slot_state(slot):
return { k:slot[v] for (k,v) in AXES.items() }
def slot_states(device):
return [ slot_state(s) for s in device.slots ]
def print_states(states):
for state in states:
trk_id = state['ABS_MT_TRACKING_ID']
x = state['ABS_MT_POSITION_X']
y = state['ABS_MT_POSITION_Y']
print(f"{trk_id}: {x}, {y}")
def active_contacts(device):
return [ x for x in slot_states(device) if x['ABS_MT_TRACKING_ID'] != -1 ]
def slot_status(device):
return [ 'X' if x['ABS_MT_TRACKING_ID'] != -1 else '.' for x in slot_states(device) ]
def print_update(device):
active = [ x['ABS_MT_TRACKING_ID'] for x in active_contacts(device) ]
count = len(active)
status = slot_status(device)
print(f" >> {''.join(status)} ({count} active contacts: {active}) {' '*30}")
#print(active)
def monitor_contact_count(device):
while True:
print_update(device)
for e in device.events():
if not e.matches(libevdev.EV_SYN.SYN_REPORT):
continue
print_update(device)
def main(args):
if len(args) != 2:
print(f"Usage: {args[0]} <file>")
return
fd = open(args[1], 'rb')
device = libevdev.Device(fd)
#print_slot_states(active_contacts(device))
monitor_contact_count(device)
if __name__ == "__main__":
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment