#include #include #include #include #define EVDEV "/dev/input/event0" // From linux/include/linux/input.h struct input_event { struct timeval time; unsigned short type; unsigned short code; unsigned int value; }; static inline double doubleTime(const timeval& tv) { return tv.tv_sec + tv.tv_usec / 1000000.0; } static const char* typeStr(int evType) { static char buf[16]; switch (evType) { case 0x00: return "RST"; case 0x01: return "KEY"; case 0x02: return "REL"; case 0x03: return "ABS"; case 0x04: return "MSC"; case 0x11: return "LED"; case 0x12: return "SND"; case 0x14: return "REP"; case 0x15: return "FF "; default: sprintf(buf, "%02x ", evType); return buf; } } static const char* keyStr(int key) { static char buf[16]; switch (key) { case 0x110: return "BTN_LEFT "; case 0x111: return "BTN_RIGHT "; case 0x112: return "BTN_MIDDLE "; case 0x113: return "BTN_SIDE "; case 0x114: return "BTN_EXTRA "; case 0x115: return "BTN_FORWARD"; case 0x116: return "BTN_BACK "; default: sprintf(buf, "%4x", key); return buf; } } static const char* relStr(int code) { static char buf[16]; switch (code) { case 0x00: return "X "; case 0x01: return "Y "; case 0x02: return "Z "; case 0x06: return "HWHEEL"; case 0x07: return "DIAL "; case 0x08: return "WHEEL "; case 0x09: return "MISC "; default: sprintf(buf, "%4x", code); return buf; } } static const char* absStr(int code) { static char buf[16]; switch (code) { case 0x00: return "ABS_X "; case 0x01: return "ABS_Y "; case 0x18: return "ABS_PRESSURE"; case 0x28: return "ABS_MISC "; default: sprintf(buf, "%4x", code); return buf; } } static const char* codeStr(int evType, int code) { static char buf[16]; switch (evType) { case 0x01: // KEY return keyStr(code); case 0x02: // REL return relStr(code); case 0x03: return absStr(code); // ABS default: sprintf(buf, "%4x", code); return buf; } } int main(int argc, char* argv[]) { const char* devName = EVDEV; if (argc > 1) devName = argv[1]; FILE* f = fopen(devName, "r"); if (!f) { fprintf(stderr, "Can't open file %s, errno:%d (%s)\n", devName, errno, strerror(errno)); exit(1); } input_event ev; double t0 = 0; while (fread(&ev, sizeof(input_event), 1, f) == 1) { if (t0 == 0) t0 = doubleTime(ev.time); double t = doubleTime(ev.time) - t0; printf("%10f %s %s %4d\n", t, typeStr(ev.type), codeStr(ev.type, ev.code), ev.value); } fclose(f); return 0; }