Exemplo n.º 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
Exemplo n.º 2
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