Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the window that currently has the cursor on top of it with X11?

Tags:

c++

c

x11

xserver

xorg

How can I retrieve the top window of which the cursor is on top of in the X11 server?

The window doesn't have to be ”active” (selected, open, whatever), it just has to have the cursor floating on top of it.

Thanks in advance.

like image 613
mkroman Avatar asked Dec 23 '10 02:12

mkroman


People also ask

How do I find my hidden cursor?

In Mouse Properties, on the Pointer Options tab, at the bottom, select Show location of pointer when I press the CTRL key, and then select OK. To see it in action, press CTRL.

How do I get current mouse position?

To get the current mouse position we are going to trigger a mouse event. In this case we will use 'mousemove' to log the current X and Y coordinates of the mouse to the console. For a more detailed list of mouse events you could have a read of this.

How do I get my cursor back on my laptop?

On the Devices screen, select Mouse in the left-hand column. Under Related settings in the right-hand panel, click Additional mouse options. In the Mouse Properties window, click the Pointer Options tab, and look under Visibility. Check the box next to Show location of pointer when I press the CTRL key.

Can you move cursor with JS?

Before we start, you need to know that it's not actually possible to move the mouse pointer to a position using Javascript, such functionality can we easily misused but we can simulate something similar.


2 Answers

You can use XQueryPointer() to get the mouse position. Then get a window list using XQueryTree(). XQueryTree() returns the window list in proper z-order so you can just loop through all the windows until you find one whose bounding box is under the pointer, XGetWindowAttributes() will give you everything you need to figure out the bounding box. I'm not sure what you would do with shaped windows though.

I haven't work with X11 for a few years so this might be a rather clunky approach but it should work. I also don't have my O'Reilly X11 books anymore, you'll want to get your hands on book one of that series if you're going to work with low level X11 stuff; I think the whole series is available for free online these days.

like image 65
mu is too short Avatar answered Oct 14 '22 05:10

mu is too short


I haven't programmed X11 for over a decade, so forgive me if I get this wrong.

I believe you can register for mouse movement events on your windows. If you handle such event by storing the window handle in some variable or other, and then handling the event so it doesn't percolate down the tree, then at the time you want to identify the window you can just query the variable.

However this will only work when the mouse is over a window you have registered a suitable event handler for, so you won't know about windows belonging to other applications - unless there is a way to register for events on other people's windows which may be possible.

The advantage over the other answer is that you don't have to traverse the whole tree. The disadvantage is that you need to handle a great many mouse movement events, and it may not work to find other people's windows.

I believe there may also be mouse enter and mouse leave events too which would reduce the amount of processing required.

like image 24
AlastairG Avatar answered Oct 14 '22 05:10

AlastairG