|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | + |
| 4 | +''' |
| 5 | +Usage: evtest [options] [<device>, ...] |
| 6 | +
|
| 7 | +Input device enumerator and event monitor. |
| 8 | +
|
| 9 | +Running evtest without any arguments will let you select |
| 10 | +from a list of all readable input devices. |
| 11 | +
|
| 12 | +Options: |
| 13 | + -h, --help Show this help message and exit. |
| 14 | + -c, --capabilities List device capabilities and exit. |
| 15 | + -g, --grab Other applications will not receive events from |
| 16 | + the selected devices while evtest is running. |
| 17 | +
|
| 18 | +Examples: |
| 19 | + evtest /dev/input/event0 /dev/input/event1 |
| 20 | +''' |
| 21 | + |
| 22 | + |
| 23 | +from __future__ import print_function |
| 24 | + |
| 25 | +import sys |
| 26 | +import select |
| 27 | +import optparse |
| 28 | + |
| 29 | +from evdev import ecodes, list_devices, AbsInfo, InputDevice |
| 30 | + |
| 31 | + |
| 32 | +def parseopt(): |
| 33 | + parser = optparse.OptionParser(add_help_option=False) |
| 34 | + parser.add_option('-h', '--help', action='store_true') |
| 35 | + parser.add_option('-g', '--grab', action='store_true') |
| 36 | + parser.add_option('-c', '--capabilities', action='store_true') |
| 37 | + return parser.parse_args() |
| 38 | + |
| 39 | + |
| 40 | +def main(): |
| 41 | + opts, devices = parseopt() |
| 42 | + if opts.help: |
| 43 | + print(__doc__.strip()) |
| 44 | + return 0 |
| 45 | + |
| 46 | + if not devices: |
| 47 | + devices = select_devices() |
| 48 | + else: |
| 49 | + devices = [InputDevice(path) for path in devices] |
| 50 | + |
| 51 | + if opts.capabilities: |
| 52 | + for device in devices: |
| 53 | + print_capabilities(device) |
| 54 | + return 0 |
| 55 | + |
| 56 | + if opts.grab: |
| 57 | + for device in devices: |
| 58 | + device.grab() |
| 59 | + |
| 60 | + print('Listening for events ...\n') |
| 61 | + fd_to_device = {dev.fd: dev for dev in devices} |
| 62 | + while True: |
| 63 | + r, w, e = select.select(fd_to_device, [], []) |
| 64 | + |
| 65 | + for fd in r: |
| 66 | + for event in fd_to_device[fd].read(): |
| 67 | + print_event(event) |
| 68 | + |
| 69 | + |
| 70 | +def select_devices(device_dir='/dev/input'): |
| 71 | + ''' |
| 72 | + Select one or more devices from a list of accessible input devices. |
| 73 | + ''' |
| 74 | + |
| 75 | + devices = reversed(list_devices(device_dir)) |
| 76 | + devices = [InputDevice(path) for path in devices] |
| 77 | + if not devices: |
| 78 | + msg = 'error: no input devices found (do you have rw permission on %s/*?)' |
| 79 | + print(msg % device_dir, file=sys.stderr) |
| 80 | + sys.exit(1) |
| 81 | + |
| 82 | + dev_format = '{0:<3} {1.fn:<20} {1.name:<35} {1.phys}' |
| 83 | + dev_lines = [dev_format.format(n, d) for n, d in enumerate(devices)] |
| 84 | + |
| 85 | + print('ID {:<20} {:<35} {}'.format('Device', 'Name', 'Phys')) |
| 86 | + print('-' * len(max(dev_lines, key=len))) |
| 87 | + print('\n'.join(dev_lines)) |
| 88 | + print() |
| 89 | + |
| 90 | + choice = input('Select devices [0-%s]: ' % (len(dev_lines)-1)) |
| 91 | + choice = choice.split() |
| 92 | + |
| 93 | + print() |
| 94 | + return [devices[int(num)] for num in choice] |
| 95 | + |
| 96 | + |
| 97 | +def print_capabilities(device): |
| 98 | + capabilities = device.capabilities(verbose=True) |
| 99 | + |
| 100 | + print('Device name: {.name}'.format(device)) |
| 101 | + print('Device info: {.info}'.format(device)) |
| 102 | + print('Repeat settings: {}\n'.format(device.repeat)) |
| 103 | + |
| 104 | + if ('EV_LED', ecodes.EV_LED) in capabilities: |
| 105 | + leds = ','.join(i[0] for i in device.leds(True)) |
| 106 | + print('Active LEDs: %s' % leds) |
| 107 | + |
| 108 | + active_keys = ','.join(k[0] for k in device.active_keys(True)) |
| 109 | + print('Active keys: %s\n' % active_keys) |
| 110 | + |
| 111 | + print('Device capabilities:') |
| 112 | + for type, codes in capabilities.items(): |
| 113 | + print(' Type {} {}:'.format(*type)) |
| 114 | + for code in codes: |
| 115 | + # code <- ('BTN_RIGHT', 273) or (['BTN_LEFT', 'BTN_MOUSE'], 272) |
| 116 | + if isinstance(code[1], AbsInfo): |
| 117 | + print(' Code {:<4} {}:'.format(*code[0])) |
| 118 | + print(' {}'.format(code[1])) |
| 119 | + else: |
| 120 | + # Multiple names may resolve to one value. |
| 121 | + s = ', '.join(code[0]) if isinstance(code[0], list) else code[0] |
| 122 | + print(' Code {:<4} {}'.format(s, code[1])) |
| 123 | + print('') |
| 124 | + |
| 125 | + |
| 126 | +def print_event(e): |
| 127 | + if e.type == ecodes.EV_SYN: |
| 128 | + if e.code == ecodes.SYN_MT_REPORT: |
| 129 | + msg = 'time {:<16} +++++++++ {} ++++++++' |
| 130 | + else: |
| 131 | + msg = 'time {:<16} --------- {} --------' |
| 132 | + print(msg.format(e.timestamp(), ecodes.SYN[e.code])) |
| 133 | + else: |
| 134 | + if e.type in ecodes.bytype: |
| 135 | + codename = ecodes.bytype[e.type][e.code] |
| 136 | + else: |
| 137 | + codename = '?' |
| 138 | + |
| 139 | + evfmt = 'time {:<16} type {} ({}), code {:<4} ({}), value {}' |
| 140 | + print(evfmt.format(e.timestamp(), e.type, ecodes.EV[e.type], e.code, codename, e.value)) |
| 141 | + |
| 142 | + |
| 143 | +if __name__ == '__main__': |
| 144 | + sys.exit(main()) |
0 commit comments