Question

i'm trying to make an app for the remote control of mouse on linux server by android application. As a beginner, i've got lot of probs..before writing the linux Server with C, I tried to check if i can control and move the mouse on Linux according to the code :

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <linux/input.h>

int main(){
struct input_event event, event_end;
int i=0;
int fd = -1;

fd = open("/dev/input/event4", O_RDWR | O_NONBLOCK);
if(fd<0){
perror("error");
}

memset(&event, 0, sizeof(event));
memset(&event, 0, sizeof(event_end));
gettimeofday(&event.time, NULL);
event.type = EV_REL;
event.code = REL_X;
event.value = 100;
gettimeofday(&event_end.time, NULL);
event_end.type = EV_SYN;
event_end.code = SYN_REPORT;
event_end.value = 0;
for( i=0; i<5; i++){

write(fd, &event, sizeof(event));// Move the mouse
write(fd, &event_end, sizeof(event_end));// Show move

sleep(1);
}
close(fd);
return 0;
}

after build compile execute.. nothing happens cursor never moves..will it be somekind of VirtualBox setting issue?

sudo cat /dev/input/event4 shows weird string of symbols when i physically move the mouse. That means to be able to get contol of mouse cursor movement by event4 isn't it? i wonder why the cursor won't move... anyone can help?

And further, I would be glad if anyone suggests Library and Functions to control mouse

thx a lot

Was it helpful?

Solution

You can't just write to the device file and expect the drivers to behave as if actual hardware was sending these events. Things are way more complicated than that.

If you are only concerned with X Windows environment you are in luck. You can use this function to send events to the window:

http://tronche.com/gui/x/xlib/event-handling/XSendEvent.html

If you don't know which window should be receiving your events just send them to the root window, they will be routed appropriately.

Also there is a library for that.

http://www.x.org/releases/X11R7.6/doc/libXtst/recordlib.html

Just keep in mind that in X windows events have a flag that indicates whether the event came from the actual hardware or was synthesized by one of the above methods. In most cases programs just ignore this flag and behave the same regardless. But sometimes you can have weird surprises.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top