I want to record and play mouse events on my Linux machine.
Basically i store all the mouse events entered by the user into a text file. For this, i am simply reading the device file: /dev/input/event1 using read() and storing the events into a text file. While playback i read() the text file and write() the events to the device file /dev/input/event1.
Here's my code:
Recording
Code:
FILE *deviceConnection,*record_file;
struct input_event myEvent;
deviceConnection = fopen(argv[1], "rw");
record_file = fopen(argv[2], "rw+");
while (1) {
printf(".");
// read from the device file into the input_event structure */
fread(&myEvent, sizeof(struct input_event), 1, deviceConnection);
// Write the events to the text file */
fwrite(&myEvent, sizeof(struct input_event), 1, record_file);
fflush(record_file);
}
fclose(deviceConnection);
fclose(record_file);
Playback
Code:
FILE *record_file;
struct input_event myEvent;
char filename[128] ;
int devHandle = -1;
devHandle = open(argv[1], O_RDWR);
record_file = fopen(argv[2], "r+");
while (!feof(record_file))
{
fread(&myEvent, sizeof(struct input_event), 1, record_file);
if (myEvent.value != 0) {
printf ("We are in Key Press \n");
write(devHandle, &(myEvent), sizeof(struct input_event);
}
}
close (devHandle);
fclose(record_file);
Now, i am able to record and play the mouse events but while playback the mouse events are played relative to the current mouse position. This is because a standard mouse supports only relative events (A mouse supports key events: EV_KEY and Relative events: EV_REL).
Note: Absolute events (EV_ABS) are supported by pointing devices like joysticks etc.
Now, i want to get absolute mouse co-ordinates so that the mouse events can be played accurately irrespective of the current mouse position.
How can i achieve this? Can i convert the relative mouse position to absolute position?