symkey/capture.c

76 lines
1.9 KiB
C

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <linux/input.h>
void dump_event(struct timespec* start, struct input_event* event);
int stream_events(char* device_path);
static inline void timespec_diff(struct timespec* a, struct timespec* b, struct timespec* result) {
result->tv_sec = a->tv_sec - b->tv_sec;
result->tv_nsec = a->tv_nsec - b->tv_nsec;
if (result->tv_nsec < 0) {
--result->tv_sec;
result->tv_nsec += 1000000000L;
}
}
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;
struct timespec now;
struct timespec diff;
clock_gettime(CLOCK_MONOTONIC, &now);
timespec_diff(&now, start, &diff);
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,
bLeft, bMiddle, bRight, x, y);
//
// comment to disable the display of raw event structure datas
//
// for(i=0; i<sizeof(event); i++)
// {
// printf("%02X ", *ptr++);
// }
}
int main(int argc, char* argv[]) {
if(argc < 2) {
fprintf(stderr, "You must specify a mouse input to track like /dev/input/mouse1.");
exit(EXIT_FAILURE);
}
int result = stream_events(argv[1]);
exit(result);
}
int stream_events(char* device_path) {
int device_fd;
struct timespec start;
struct input_event event;
if((device_fd = open(device_path, O_RDONLY)) == -1) {
fprintf(stderr, "Failed to open %s. Are you running as root?\n", device_path);
return 1;
}
else {
fprintf(stderr, "Device open OK\n");
}
clock_gettime(CLOCK_MONOTONIC, &start);
while(read(device_fd, &event, sizeof(struct input_event)))
{
dump_event(&start, &event);
}
}