diff --git a/capture.c b/capture.c index f41f5d7..3cf6c95 100644 --- a/capture.c +++ b/capture.c @@ -1,12 +1,25 @@ +#include #include #include #include #include #include -#include -void dump_event(struct timespec* start, struct input_event* event); -int stream_events(char* device_path); +#include +#include + +#define MAX_EVENTS 16 +#define CONTENT_BUFFER_SIZE 32 + +static char* KEYBOARD = "k"; +static char* MOUSE = "m"; + +int dump_event(struct timespec* start, int fd, char* type); + +int event_content_keyboard(char* buffer, int buffer_size, struct input_event* event); +int event_content_mouse(char* buffer, int buffer_size, struct input_event* event); + +int stream_events(char* mouse_path, char* keyboard_path); static inline void timespec_diff(struct timespec* a, struct timespec* b, struct timespec* result) { result->tv_sec = a->tv_sec - b->tv_sec; @@ -17,59 +30,138 @@ static inline void timespec_diff(struct timespec* a, struct timespec* b, struct } } -void dump_event(struct timespec* start, struct input_event* event) { - unsigned char button, bLeft, bMiddle, bRight; - unsigned char *ptr = (unsigned char*)event; - int i; - char x, y; +int dump_event(struct timespec* start, int fd, char* type) { + struct input_event event; struct timespec now; struct timespec diff; + char content_buffer[CONTENT_BUFFER_SIZE]; + + int result = read(fd, &event, sizeof(struct input_event)); + if(result < 0) { + fprintf(stderr, "Failed to read an event: %d", errno); + return 1; + } clock_gettime(CLOCK_MONOTONIC, &now); timespec_diff(&now, start, &diff); + if(type == MOUSE) { + if(event_content_mouse(content_buffer, CONTENT_BUFFER_SIZE, &event)) { + return 1; + } + } else if(type == KEYBOARD) { + if(event.type == EV_SYN) { + return 0; + } + if(event_content_keyboard(content_buffer, CONTENT_BUFFER_SIZE, &event)) { + return 1; + } + } else { + fprintf(stderr, "Unknown event type.\n"); + return 1; + } + + printf("%ld.%ld,%s,%s\n", + diff.tv_sec, + diff.tv_nsec, + type, + content_buffer); + + return 0; +} + +int event_content_keyboard(char* buffer, int buffer_size, struct input_event* event) { + sprintf(buffer, "?"); + return 0; +} + +int event_content_mouse(char* buffer, int buffer_size, struct input_event* event) { + unsigned char button, bLeft, bMiddle, bRight; + unsigned char *ptr = (unsigned char*)event; + int i; + char x, y; button = ptr[0]; bLeft = button & 0x1; bMiddle = ( button & 0x4 ) > 0; bRight = ( button & 0x2 ) > 0; - x=(char) ptr[1];y=(char) ptr[2]; - printf("%ld.%ld,m,l%d,m%d,r%d,x%d,y%d\n", - diff.tv_sec, - diff.tv_nsec, + x=(char) ptr[1]; + y=(char) ptr[2]; + + int chars = sprintf(buffer, "l%d,m%d,r%d,x%d,y%d", bLeft, bMiddle, bRight, x, y); - - // - // comment to disable the display of raw event structure datas - // - // for(i=0; i