Exemple #1
0
def main():
    parser = argparse.ArgumentParser(description='Record a HID device')
    parser.add_argument('device',
                        metavar='/dev/hidrawX',
                        nargs="*",
                        type=argparse.FileType('r'),
                        help='Path to the hidraw device node')
    parser.add_argument('--output',
                        metavar='output-file',
                        nargs=1,
                        default=[sys.stdout],
                        type=argparse.FileType('w'),
                        help='The file to record to (default: stdout)')
    args = parser.parse_args()

    devices = {}
    last_index = -1
    poll = select.poll()
    is_first_event = True

    # argparse always gives us a list for nargs 1
    output = args.output[0]

    try:
        if not args.device:
            args.device = [open(list_devices())]

        for idx, fd in enumerate(args.device):
            device = HidrawDevice(fd)
            if len(args.device) > 1:
                print(f'D: {idx}', file=output)
            device.dump(output)
            poll.register(fd, select.POLLIN)
            devices[fd.fileno()] = (idx, device)

        if len(devices) == 1:
            last_index = 0

        while True:
            events = poll.poll()
            for fd, event in events:
                idx, device = devices[fd]
                device.read_events()
                if last_index != idx:
                    print(f'D: {idx}', file=output)
                    last_index = idx
                device.dump(output)

                if is_first_event:
                    is_first_event = False
                    for idx, d in devices.values():
                        d.time_offset = device.time_offset

    except PermissionError:
        print('Insufficient permissions, please run me as root.',
              file=sys.stderr)
    except KeyboardInterrupt:
        pass
Exemple #2
0
def list_devices():
    outfile = sys.stdout if os.isatty(sys.stdout.fileno()) else sys.stderr
    devices = {}
    for fname in os.listdir('/dev/'):
        if not fname.startswith('hidraw'):
            continue

        with open('/dev/{fname}'.format(**locals())) as f:
            d = HidrawDevice(f)
            devices[int(fname[6:])] = d.name

    if not devices:
        print('No devices found', file=sys.stderr)
        sys.exit(1)

    print('Available devices:', file=outfile)
    for num, name in sorted(devices.items()):
        print('/dev/hidraw{num}:	{name}'.format(**locals()), file=outfile)

    lo = min(devices.keys())
    hi = max(devices.keys())

    print('Select the device event number [{lo}-{hi}]: '.format(**locals()),
          end='', flush=True, file=outfile)
    try:
        num = int(sys.stdin.readline())
        if num < lo or num > hi:
            raise ValueError
        return '/dev/hidraw{num}'.format(**locals())
    except ValueError:
        print('Invalid device', file=sys.stderr)
        sys.exit(1)
Exemple #3
0
def main(device_list, output):
    '''Record a HID device'''

    devices = {}
    last_index = -1
    poll = select.poll()
    is_first_event = True

    try:
        if not device_list:
            device_list = [open(list_devices())]

        for idx, fd in enumerate(device_list):
            device = HidrawDevice(fd)
            if len(device_list) > 1:
                print(f'D: {idx}', file=output)
            device.dump(output)
            poll.register(fd, select.POLLIN)
            devices[fd.fileno()] = (idx, device)

        if len(devices) == 1:
            last_index = 0

        while True:
            events = poll.poll()
            for fd, event in events:
                idx, device = devices[fd]
                device.read_events()
                if last_index != idx:
                    print(f'D: {idx}', file=output)
                    last_index = idx
                device.dump(output)

                if is_first_event:
                    is_first_event = False
                    for idx, d in devices.values():
                        d.time_offset = device.time_offset

    except PermissionError:
        print('Insufficient permissions, please run me as root.',
              file=sys.stderr)
    except KeyboardInterrupt:
        pass