How can I get the absolute mouse position in Linux in C

As far as I know, two ways to get the mouse position use libgpm or reading the file /dev/input/mice . But the latter returns the relative position from the last position. So my question is how can I get the absolute position of the mouse , although read /dev/input/mice or another way.

And I want to implement this function using C or C++ . Any information would be appreciated.

0
source share
1 answer

Firstly, the mouse device probably sends only relative movements, so there is no way to get an absolute position (just try to raise the mouse with your hand and place it in another place), except for the integration of movement.

And almost all Linux GUI environments are above X11 , so this is the X11 server (usually the Xorg process) that deals with the mouse (this is the only process actually looking at /dev/input/mice )

Then you will need to create an X11 client application. See this and this question. But you will be much better off using some existing tool library such as Qt or GTK; see, for example, QMouseEvent and QWidget :: mouseMoveEvent in Qt and GtkWidget "signal-notification-event-signal" in Gtk (and many other functions).

See also this question.

+2
source

All Articles