| 1 |
""" |
| 2 |
Usage: evtest [options] [<device>, ...] |
| 3 |
|
| 4 |
Input device enumerator and event monitor. |
| 5 |
|
| 6 |
Running evtest without any arguments will let you select |
| 7 |
from a list of all readable input devices. |
| 8 |
|
| 9 |
Options: |
| 10 |
-h, --help Show this help message and exit. |
| 11 |
-c, --capabilities List device capabilities and exit. |
| 12 |
-g, --grab Other applications will not receive events from |
| 13 |
the selected devices while evtest is running. |
| 14 |
|
| 15 |
Examples: |
| 16 |
evtest /dev/input/event0 /dev/input/event1 |
| 17 |
""" |
| 18 |
|
| 19 |
import atexit |
| 20 |
import optparse |
| 21 |
import re |
| 22 |
import select |
| 23 |
import sys |
| 24 |
import termios |
| 25 |
|
| 26 |
from . import AbsInfo, InputDevice, ecodes, list_devices |
| 27 |
|
| 28 |
|
| 29 |
def parseopt(): |
| 30 |
parser = optparse.OptionParser(add_help_option=False) |
| 31 |
parser.add_option("-h", "--help", action="store_true") |
| 32 |
parser.add_option("-g", "--grab", action="store_true") |
| 33 |
parser.add_option("-c", "--capabilities", action="store_true") |
| 34 |
return parser.parse_args() |
| 35 |
|
| 36 |
|
| 37 |
def main(): |
| 38 |
opts, devices = parseopt() |
| 39 |
if opts.help: |
| 40 |
print(__doc__.strip()) |
| 41 |
return 0 |
| 42 |
|
| 43 |
if not devices: |
| 44 |
devices = select_devices() |
| 45 |
else: |
| 46 |
devices = [InputDevice(path) for path in devices] |
| 47 |
|
| 48 |
if opts.capabilities: |
| 49 |
for device in devices: |
| 50 |
print_capabilities(device) |
| 51 |
return 0 |
| 52 |
|
| 53 |
if opts.grab: |
| 54 |
for device in devices: |
| 55 |
device.grab() |
| 56 |
|
| 57 |
# Disable tty echoing if stdin is a tty. |
| 58 |
if sys.stdin.isatty(): |
| 59 |
toggle_tty_echo(sys.stdin, enable=False) |
| 60 |
atexit.register(toggle_tty_echo, sys.stdin, enable=True) |
| 61 |
|
| 62 |
print("Listening for events (press ctrl-c to exit) ...") |
| 63 |
fd_to_device = {dev.fd: dev for dev in devices} |
| 64 |
while True: |
| 65 |
r, w, e = select.select(fd_to_device, [], []) |
| 66 |
|
| 67 |
for fd in r: |
| 68 |
for event in fd_to_device[fd].read(): |
| 69 |
print_event(event) |
| 70 |
|
| 71 |
|
| 72 |
def select_devices(device_dir="/dev/input"): |
| 73 |
""" |
| 74 |
Select one or more devices from a list of accessible input devices. |
| 75 |
""" |
| 76 |
|
| 77 |
def devicenum(device_path): |
| 78 |
digits = re.findall(r"\d+$", device_path) |
| 79 |
return [int(i) for i in digits] |
| 80 |
|
| 81 |
devices = sorted(list_devices(device_dir), key=devicenum) |
| 82 |
devices = [InputDevice(path) for path in devices] |
| 83 |
if not devices: |
| 84 |
msg = "error: no input devices found (do you have rw permission on %s/*?)" |
| 85 |
print(msg % device_dir, file=sys.stderr) |
| 86 |
sys.exit(1) |
| 87 |
|
| 88 |
dev_format = "{0:<3} {1.path:<20} {1.name:<35} {1.phys:<35} {1.uniq:<4}" |
| 89 |
dev_lines = [dev_format.format(num, dev) for num, dev in enumerate(devices)] |
| 90 |
|
| 91 |
print("ID {:<20} {:<35} {:<35} {}".format("Device", "Name", "Phys", "Uniq")) |
| 92 |
print("-" * len(max(dev_lines, key=len))) |
| 93 |
print("\n".join(dev_lines)) |
| 94 |
print() |
| 95 |
|
| 96 |
choices = input("Select devices [0-%s]: " % (len(dev_lines) - 1)) |
| 97 |
|
| 98 |
try: |
| 99 |
choices = choices.split() |
| 100 |
choices = [devices[int(num)] for num in choices] |
| 101 |
except ValueError: |
| 102 |
choices = None |
| 103 |
|
| 104 |
if not choices: |
| 105 |
msg = "error: invalid input - please enter one or more numbers separated by spaces" |
| 106 |
print(msg, file=sys.stderr) |
| 107 |
sys.exit(1) |
| 108 |
|
| 109 |
return choices |
| 110 |
|
| 111 |
|
| 112 |
def print_capabilities(device): |
| 113 |
capabilities = device.capabilities(verbose=True) |
| 114 |
input_props = device.input_props(verbose=True) |
| 115 |
|
| 116 |
print("Device name: {.name}".format(device)) |
| 117 |
print("Device info: {.info}".format(device)) |
| 118 |
print("Repeat settings: {}\n".format(device.repeat)) |
| 119 |
|
| 120 |
if ("EV_LED", ecodes.EV_LED) in capabilities: |
| 121 |
leds = ",".join(i[0] for i in device.leds(True)) |
| 122 |
print("Active LEDs: %s" % leds) |
| 123 |
|
| 124 |
active_keys = ",".join(k[0] for k in device.active_keys(True)) |
| 125 |
print("Active keys: %s\n" % active_keys) |
| 126 |
|
| 127 |
if input_props: |
| 128 |
print("Input properties:") |
| 129 |
for type, code in input_props: |
| 130 |
print(" %s %s" % (type, code)) |
| 131 |
print() |
| 132 |
|
| 133 |
print("Device capabilities:") |
| 134 |
for type, codes in capabilities.items(): |
| 135 |
print(" Type {} {}:".format(*type)) |
| 136 |
for code in codes: |
| 137 |
# code <- ('BTN_RIGHT', 273) or (['BTN_LEFT', 'BTN_MOUSE'], 272) |
| 138 |
if isinstance(code[1], AbsInfo): |
| 139 |
print(" Code {:<4} {}:".format(*code[0])) |
| 140 |
print(" {}".format(code[1])) |
| 141 |
else: |
| 142 |
# Multiple names may resolve to one value. |
| 143 |
s = ", ".join(code[0]) if isinstance(code[0], list) else code[0] |
| 144 |
print(" Code {:<4} {}".format(s, code[1])) |
| 145 |
print("") |
| 146 |
|
| 147 |
|
| 148 |
def print_event(e): |
| 149 |
if e.type == ecodes.EV_SYN: |
| 150 |
if e.code == ecodes.SYN_MT_REPORT: |
| 151 |
msg = "time {:<17} +++++++++++++ {} +++++++++++++" |
| 152 |
elif e.code == ecodes.SYN_DROPPED: |
| 153 |
msg = "time {:<17} !!!!!!!!!!!!! {} !!!!!!!!!!!!!" |
| 154 |
else: |
| 155 |
msg = "time {:<17} ------------- {} -------------" |
| 156 |
print(msg.format(e.timestamp(), ecodes.SYN[e.code])) |
| 157 |
else: |
| 158 |
if e.type in ecodes.bytype: |
| 159 |
codename = ecodes.bytype[e.type][e.code] |
| 160 |
else: |
| 161 |
codename = "?" |
| 162 |
|
| 163 |
evfmt = "time {:<17} type {} ({}), code {:<4} ({}), value {}" |
| 164 |
print(evfmt.format(e.timestamp(), e.type, ecodes.EV[e.type], e.code, codename, e.value)) |
| 165 |
|
| 166 |
|
| 167 |
def toggle_tty_echo(fh, enable=True): |
| 168 |
flags = termios.tcgetattr(fh.fileno()) |
| 169 |
if enable: |
| 170 |
flags[3] |= termios.ECHO |
| 171 |
else: |
| 172 |
flags[3] &= ~termios.ECHO |
| 173 |
termios.tcsetattr(fh.fileno(), termios.TCSANOW, flags) |
| 174 |
|
| 175 |
|
| 176 |
if __name__ == "__main__": |
| 177 |
try: |
| 178 |
ret = main() |
| 179 |
except (KeyboardInterrupt, EOFError): |
| 180 |
ret = 0 |
| 181 |
sys.exit(ret) |