Hello.
I am new to the Linux OS, and I am having some difficulties in creating an application (in c++) for reading the data transmitted by the computer mouse to the OS. ( I'm using linux because i think it's easier to access this type of information than in windows...)
The objective of the application is to use an optical mouse as a motion capture sensor (replacing the need to buy industrial encoders that are too expensive...)
Using the 'xxd' in the terminal, i can now understand the imps/2 protocol, and how to interpret each bit transmitted.
The command I use is: xxd -b -c 3 /dev/input/mice
Now, my problem
I want to create a C++ application for interpreting in real-time the data transmitted by the mouse. Until now I have thought about 3 processes, and none of them is working...
process #1: opening a pipe with the terminal, using the xxd command i previously explained:
void readSensor2()
{
FILE *fp;
char line[BUFSIZ];
if ((fp = popen("sudo xxd -ps -c 3 /dev/input/mice", "r")) != NULL)
{
while (fgets( line, BUFSIZ, fp) != NULL)
{
/*
if (strcmp(line, "080001")==0 )
printf("forward \n");
else if (strcmp(line, "2800ff")==0)
printf("backward \n");
else if (strcmp(line, "080100")==0)
printf("right \n");
else if (strcmp(line, "18ff00")==0)
printf("left \n");
*/
printf("%s", line);
}
pclose(fp);
}
}
really simple example, just to test the idea... the problem here, is that (i don't know why...) i can't have a real-time analysis... the data only appears each 2-3 seconds...
and if i do this:
void readSensor()
{
FILE *fp;
char line[BUFSIZ];
fp = popen("sudo xxd -l 3 -ps /dev/input/mice", "r");
fgets( line, BUFSIZ, fp);
if (strcmp(line, "080001")==0)
printf("forward \n");
else if (strcmp(line, "2800ff")==0)
printf("backward \n");
else if (strcmp(line, "080100")==0)
printf("right \n");
else if (strcmp(line, "18ff00")==0)
printf("left \n");
//printf("%s", line);
pclose(fp);
}
be constantly calling the readSensor() function, too much data is lost.
process #2: using 'ncurses'
The problem is that ncurses as far as I know, only captures the mouse movement in the screen. I mean, if the pointer is in the right upper corner, and you are still moving it further, no movement will be detected.
process #3: using GPM
here, i think relies my solution
but i can't find any good documentation for programming using gpm..
...and i can't compile the code... it says that
undefined reference to `Gpm_Open'
(...)
code:
#include <stdio.h>
#include <gpm.h>
#include <ncurses.h>
int my_handler(Gpm_Event *event, void *data)
{ printf("Event Type : %d at x=%d y=%d\n", event->type, event->x, event->y);
return 0;
}
int main()
{ Gpm_Connect conn;
int c;
conn.eventMask = ~0; /* Want to know about all the events */
conn.defaultMask = 0; /* don't handle anything by default */
conn.minMod = 0; /* want everything */
conn.maxMod = ~0; /* all modifiers included */
if(Gpm_Open(&conn, 0) == -1)
printf("Cannot connect to mouse server\n");
gpm_handler = my_handler;
while((c = Gpm_Getc(stdin)) != EOF)
printf("%c", c);
Gpm_Close();
return 0;
}
the best solution would definitly be without using gpm too...
any ideas??
thanks for reading this!!