PDA

View Full Version : Getting mouse release event system-wide



rittchat
7th April 2011, 10:01
Hi

I want to get system wide mouse release event in Linux. I created a ruApplication class from QApplication.

The header file is



#include <QApplication>
#include <QAbstractEventDispatcher>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>

class ruApplication : public QApplication
{
Q_OBJECT
public:
explicit ruApplication(int c, char *v[]);

protected:
bool x11EventFilter(XEvent *);
static bool ruEventFilter(void *message);

public slots:

private:
QAbstractEventDispatcher *evInstance;

};


The cpp file is



ruApplication::ruApplication(int c, char *v[]) :
QApplication(c,v)
{
evInstance = QAbstractEventDispatcher::instance();
evInstance->setEventFilter((QAbstractEventDispatcher::EventFil ter)ruEventFilter);
}

bool ruApplication::x11EventFilter(XEvent *evt)
{
if(evt->type == ButtonRelease)
qDebug("x11Event Filter");
return false;
}

bool ruApplication::ruEventFilter(void *message)
{
XEvent *evt;
evt = (XEvent *) message;
if(evt->type == ButtonRelease)
qDebug("ruEvent Filter");
return false;
}


The main.cpp is



int main(int argc, char *argv[])
{
ruApplication a(argc, argv);
Dialog w();
w.show();

return a.exec();
}


But when I am clicking out side the client area of the dialog, I am not getting any mouse event. What am I doing wrong? If this is not the correct way of doing this, can you suggest me, how to get system wide mouse release event in Linux?

thanks

stampede
7th April 2011, 15:07
Does this event filter works ? I mean, are you getting other events ?


bool ruApplication::x11EventFilter(XEvent *evt)
{
qDebug() << "x11EventFilter" << evt->type;
if(evt->type == ButtonRelease)
qDebug("x11Event Filter");
return false;
}

bool ruApplication::ruEventFilter(void *message)
{
XEvent *evt;
evt = (XEvent *) message;
qDebug() << "ruEventFilter" << evt->type;
if(evt->type == ButtonRelease)
qDebug("ruEvent Filter");
return false;
}

Sorry I can't help you more, can't test on linux right now.

high_flyer
7th April 2011, 16:36
The suggested solution will work only for the event sent to this application, but the OP wants a system wide filter.
For a system wide event filter, if at all possible, you will have to tell X to deliver all events first to your app... try Linux forums for that.

rittchat
8th April 2011, 04:56
Hi,

Thanks for reply. I will try other linux forums.

cspiel
9th February 2012, 08:08
Hi,
a little bit late. But maybe it helps somebody else:

Have a look at project dwm at suckless dot org. See method void setup(int argc, char **argv) in dwm.c how to initialize a XLib Display and set the XEvent mask. Be sure to use the same Display for your QApplication. Use the constructor with the Display parameter, if you want to attach to a running X client:


class MyApplication: public QApplication {
public:
MyApplication(Display* dpy, int& argc, char** argv): QApplication(dpy, argc, argv) {}
bool x11EventFilter(XEvent *e);
};

What I tried is to attach a QApplication to dwm and replaced the main event loop of dwm by the qt event loop. My implementation of x11EventFilter delivers XEvents back to dwms event handlers.

Or you try this which I also tested successfully:
Use method
Display * QX11Info::display () [static]
after intantiation of your ruApplication and then set/modify the event mask.

Example:
main.cpp:



#include <QApplication>
#include <QX11Info>
#include <X11/Xlib.h>
#include <X11/cursorfont.h>

class MyApplication: public QApplication {
public:
MyApplication(int& argc, char** argv): QApplication(argc, argv) {}
bool x11EventFilter(XEvent *e);
};

bool MyApplication::x11EventFilter(XEvent *e) {
qDebug("x11Event Filter called");

return false;
}

int main(int argc, char *argv[])
{
MyApplication a(argc, argv);
Display* dpy = QX11Info::display();

XSetWindowAttributes wa;
wa.cursor = XCreateFontCursor(dpy, XC_left_ptr);
wa.event_mask = ButtonPressMask;

int screen = DefaultScreen(dpy);
Window root = RootWindow(dpy, screen);
XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
XSelectInput(dpy, root, wa.event_mask);

return a.exec();
}


qttest.pro:


QT += core gui

TEMPLATE = app

SOURCES += main.cpp

LIBS += -L/usr/X11R6/lib -lX11


Start in a separate X Server:
xinit ./qttest -- :1