Lecko
Lecko

Reputation: 1395

Handle X11 events with Qt5

I am porting my application from Qt4 to Qt5.

As far as I know, Qt5 does not support handling Xlib events anymore and uses XCB events (event handler gets xcb_generic_event_t object). However spnav library I am using supports only X11 events (it parses XEvent object to retrieve the necessary information). Is it possible to handle X11 events in Qt5 or convert xcb_generic_event_t to XEvent?

In Qt4 code looked like:

bool MyApplication::x11EventFilter(XEvent *event) 
{
    spnav_event sev;
    spnav_x11_event(event,&sev);

    if(sev.type == SPNAV_EVENT_MOTION) 
    {
           emit joystickPositionEvent(sev.motion.x,sev.motion.y,sev.motion.z,sev.motion.rx,sev.motion.ry,sev.motion.rz);
    } 
    else if(sev.type == SPNAV_EVENT_BUTTON)
    {   
        emit joystickButtonEvent(sev.button.bnum,sev.button.press!=0);
    }

    return false;   
}

Upvotes: 1

Views: 2984

Answers (1)

Ian Rees
Ian Rees

Reputation: 11

In case others encounter the same issue: spnav_x11_event() only requires a couple fields to be copied - here's the code I've ended up with for FreeCAD:

spnav_event navEvent;

// Qt4 used XEvents in native event filters, but Qt5 changed to XCB.  The
// SpaceNavigator API only works with XEvent, so we need to construct a
// temporary XEvent with just enough information for spnav_x11_event()
auto xcb_ev(static_cast<const xcb_client_message_event_t *>(message));
if ((xcb_ev->response_type & 0x7F) == XCB_CLIENT_MESSAGE) {
    XClientMessageEvent xev;

    xev.type = ClientMessage;
    xev.message_type = xcb_ev->type;
    memcpy(xev.data.b, xcb_ev->data.data8, sizeof(xev.data.b));
    xev.serial = 0; // These are just to squash warnings...
    xev.send_event = 0;
    xev.display = 0;
    xev.window = 0;
    xev.format = 0;

    if (!spnav_x11_event(reinterpret_cast<XEvent *>(&xev), &navEvent)) {
        return false;
    }
} else {
    return false;
}
// navEvent is now initialised

Upvotes: 1

Related Questions