PDA

View Full Version : help needed in mouse displacement measurement program



montamer
May 9th, 2009, 03:14 PM
Hi
Im trying to write a program which outputs the distance the mouse travelled and in what direction.
I dont know whr to start, can someone give a hint or a program which does this?
-Thanx

montamer
May 9th, 2009, 03:21 PM
one thing im thinking is use
popen("cat /dev/input/mice","r")
and read the data coming from the device.

But when i type
"sudo cat /dev/input/mice" I get the output; but i cant tell wht the output is exactly to interprete in the program.

crazyfuturamanoob
May 9th, 2009, 07:55 PM
Maybe get the cursor position directly from X11? You didn't say which language you use so I assume it's C.



#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <assert.h>

int main (void)
{
Display *dpy;
Window root, child;
int rootX, rootY, winX, winY;
unsigned int mask;

dpy = XOpenDisplay(NULL);
assert(dpy);

while (1) {
XQueryPointer(dpy,DefaultRootWindow(dpy),&root,&child,&rootX,&rootY,&winX,&winY,&mask);
printf("x=%d - y=%d\n", rootX, rootY);
/*sleep(1);*/
}
}


(remember to add -lX11 when compiling)

AlecSchueler
May 9th, 2009, 08:09 PM
...You didn't say which language you use so I assume it's C...
The example ("popen('cat /dev/input/mice','r')") looks like Python.

Arndt
May 9th, 2009, 09:48 PM
one thing im thinking is use
popen("cat /dev/input/mice","r")
and read the data coming from the device.

But when i type
"sudo cat /dev/input/mice" I get the output; but i cant tell wht the output is exactly to interprete in the program.

I found these two sites which seem useful: http://kerneltrap.org/node/6786 and http://www.computer-engineering.org/ps2mouse/.

crazyfuturamanoob
May 10th, 2009, 06:56 AM
Make a C function which returns mouse coordinates and compile it as Python module?

crazyfuturamanoob
May 10th, 2009, 07:54 AM
I read the python docs and made you a module which does this. Simply run "python setup.py build" to compile it. Then put the output .so file in same directory with your python program.

Usage:


import xmouse

while (1):
coords = xmouse.get_pos()
print str(coords)



Edit: there is also a Python implementation of Xlib so it's possible to do this without a C module.