read position mouse cursor

Hi to all!
I'm a teacher of maths and physics in an italian high school in Milan, Italy.
I need a simple program that read the position of mouse cursor in function of time and write the coordinates in a text file. The time resolution have to be something like 1/10 sec or better (I have to know this parameter in advance). I can do that with a macro in labview but I can't buy that program. This work will be a part of physics lab in my classroom: we want to plot oscillation (in function of time) of a pendulum followed by an optical mouse.
thank you in advance for help!

Christian Bonfanti

It's probably better to use a modern framework such as Qt or GTK+ but Googling brought up a few half-promising links about raw X11 mouse coordinates.

http://www.soe.ucsc.edu/classes/cmps160/Spring99/prog1.c
X11 Programming - Ubuntu Forums
How to get mouse's position? - Qt Centre Forum

I don't know anything about X11 really but hopefully at least this is a start.

This is the complete code for a Qt application I threw together in about ten minutes (called crosshair) which displays the current mouse coordinates in a window. You might be able to pull enough out of it to be useful. This is Qt 3.1, but Qt 4 is not a great deal different. You will need the Qt development libraries, not just the runtimes. The code comprises two files, crosshair.h and crosshair.cpp.

crosshair.h:

#ifndef CROSSHAIR_H
#define CROSSHAIR_H
#include <qwidget.h>
#include <qstring.h>
#include <qlabel.h>
#include <qevent.h>

class Crosshair : public QLabel
{
Q_OBJECT

public:
    Crosshair(QWidget *parent=0);

protected:
    void mousePressEvent(QMouseEvent *);

private:
    QTimer         *timer;

private slots:
    void timerfire();
};

#endif

crosshair.cpp:

#include <qapplication.h>
#include <qpushbutton.h>
#include <qtimer.h>
#include <qcursor.h>
#include <iostream>
#include "crosshair.h"

using namespace std;

int main(int argc,char **argv)
{
    QApplication a(argc,argv);

    Crosshair mousepos;

    a.setMainWidget(&mousepos);
    mousepos.show();
    return a.exec();
}

Crosshair::Crosshair(QWidget *parent) : QLabel(parent)
{
    setIndent(20);
    resize(100,30);
    move(1200,200);
    setText("0,0");
    timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(timerfire()));
    timer->start(50,false);
}

void Crosshair::mousePressEvent(QMouseEvent *)
{
    qApp->quit();
}

void Crosshair::timerfire()
{
    QPoint p=QCursor::pos();
    this->setText(QString().sprintf("%d,%d",p.x(),p.y()));
}

To build this, put both files in a directory called crosshair. cd to that directory and type

qmake -project
qmake
make

This does nothing more complex than inherit from a QLabel, set a timer to run 20x a second, grab the current cursor coordinates and write them into the label's text. Clicking in the window closes it. I use it for fixing up alignment bugs in JavaScript when I'm laying out objects.

You could open a file in the Crosshair class's constructor to store your data, and use gettimeofday(2) to get a timestamp. Nothing says Qt has to run in GUI mode (you can tell it explicitly not to in the QApplication constructor).

Qt from Trolltech: http://doc.trolltech.com