I try to control the mouse in Linux. Xlib seems to works, but when I try to use it with OpenCV, it keeps returning:
Resource temporarily unavailable
So I decide to write "/dev/psaux". The code is as following:
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
unsigned char a[5]={0, 0xff, 0, 0x28, 0xff};
int fp = open ("/dev/psaux", O_WRONLY);
if(!fp)printf("open error:%s\n", strerror(errno));
for(int i = 0; i < 10; i++)
printf("write:%d\t\t%s\n", write(fp, a, 5), strerror(errno));
close(fp);
return 0;
}
Compile it with:
gcc my_psaux.c -o my_psaux -std=gnu99 -g
Run and get
$sudo ./my_psaux
write:5 Success
write:5 Success
write:5 Success
write:5 Success
write:5 Success
write:5 Success
write:5 Success
write:5 Success
write:5 Success
write:5 Success
However the mouse doesn't move. Then I open a new terminal, type in "sudo cat /dev/psaux" and run "my_psaux". But I just cat nothing. Nothing is written into "/dev/psaux" ?
Could anyone help me?
If this is not a good method to control the mouse, could anyone tell me another one?
The mouse is not a loopback/echo device. It's more like a terminal. Would you expect writing data to a terminal (which would appear on the screen) to make the same characters come back to you as input? The same applies to the mouse; the only point in writing to it is to send escape sequences that change its mode (e.g. the protocol used or the resolution).
If you want to "control" the mouse, you have to inject events some other way, or provide a fifo (named pipe) or pseudo-tty in place of
/dev/psaux
for the input system to read from. However this is probably a rather misguided way to do things...If you explain why you need to control the mouse, perhaps we could offer you better alternative approaches to what you're trying to do.
Great thanks to @R.. for reminding me of some other ways instead of
/dev/psaux
So I tried
/dev/input/mouse*
and/dev/input/event*
By using
I get this:
After testing, only
/dev/input/event10
works. The code is as following: