0

Is it possible to detect mouse events from entire screen in linux? My application is programmed in C and uses gtk2. I tried to look the source of several programs but all I found was grabbing the entire screen and other programs no longer recieve those events and this is not what I'm looking for.

At the moment I'm just trying to read the device directly but this doesn't seem to be very good way.. I would like to get any mouse click events made by keyboard, scripts etc.

3
  • Maybe you want to code something which behaves like a window manager? tronche.com/gui/x/icccm and freedesktop.org/wiki/Specifications/wm-spec ??? Why do you want tp catch all mouse events??? Mar 6, 2012 at 20:28
  • I need to do something based on user activity (I only need the click event, not coordinates).
    – drwn
    Mar 7, 2012 at 14:36
  • The program is pretty specially tailored for a limited userbase (a small online gaming community) and its hard to describe for an outsider what its doing. A window manager is kinda overkill..
    – drwn
    Mar 7, 2012 at 14:38

2 Answers 2

2

A "solution" I've seen in some places is to select ButtonPressEvent on every window in the entire tree of X windows, using XSelectInput, and also selecting SubstructureNotifyMask to find out about new windows. This method is known to break some programs (by preventing mouse events from propagating from a window whose client has not selected ButtonPressEvent), so use it at your own risk. Also, it will not work if another client has a pointer grab.

You can maintain a pointer grab with GrabModeSync forever and allow mouse events to go to the window that would normally get them by calling XAllowEvents(event_mode=ReplayPointer). If you keep a full pointer grab all the time, then it will horribly break all your programs because they will expect to be able to grab the pointer. You can grab a single button + modifier combination with XGrabButton, but I suspect your window manager might interfere with this in practice (which may be a good argument for modifying your window manager to notify you of events or directly do whatever it is you need).

If all of the programs you use support AT-SPI (they probably don't) and you have Assistive Technologies enabled, you can get notification of mouse events from AT-SPI. This won't break anything, but it won't work for some programs.

So there really isn't a good solution, but maybe one of these broken solutions will work in the circumstances you need, applied in a limited way or with a huge disclaimer.

2
  • The first method actually could work, because there really isn't need to use many other programs in the same time, and those that need I can test if they will break.
    – drwn
    Mar 12, 2012 at 23:17
  • However, we already made the decision to switch to java. This is a small project and we are now platform-independent - and because there is a mousehook library for java which seems to work pretty well on many platforms: code.google.com/p/jnativehook
    – drwn
    Mar 12, 2012 at 23:27
1

If you need to know about mouse events specifically, there really isn't a way to do that unless you grab the pointer.

If you just want to detect whether the user is idle, there's a way to do that using the X screen saver extension.

XScreenSaverInfo screen_saver_info = { 0, };

XScreenSaverQueryInfo(xdisplay,
                      xroot,
                      &screen_saver_info);

if (screen_saver_info.idle > your_idle_threshold)
      // screen is idle.    

Note that you have to init the extension when you open the display,

int event_base, error_base;
XScreenSaverQueryExtension(xdisplay, &event_base, &error_base)

"man XScreenSaverQueryInfo" has a little more detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.