grab all keyboard focuses on startup
authorRalf Jung <post@ralfj.de>
Sat, 13 Jul 2013 11:03:06 +0000 (13:03 +0200)
committerRalf Jung <post@ralfj.de>
Sat, 13 Jul 2013 11:03:06 +0000 (13:03 +0200)
qt/multypo.cpp
qt/multypo.h

index a5d81375048e331c225eeb45add07f89dfc4fab7..638114cde667c80b15703d107d4589e3e4ee3bd9 100644 (file)
@@ -26,40 +26,6 @@ MultypoWindow::~MultypoWindow()
 {
 }
 
 {
 }
 
-void MultypoWindow::showEvent ( QShowEvent * )
-{
-       if (xiInited) return;
-
-       Display *dpy = x11Info().display();
-
-       /* XInput Extension available? */
-       int eventID, errorID;
-       if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &eventID, &errorID)) {
-          qCritical() << "X Input extension not available";
-       }
-
-       /* Which version of XI2? We support 2.0 */
-       int major = 2, minor = 0;
-       if (XIQueryVersion(dpy, &major, &minor) == BadRequest) {
-               qCritical() << "XI2 not available. Server supports" << major << "." <<
-                                       minor;
-       }
-
-       qDebug() << "System is sane";
-
-
-       // Now let's listen to keypress events
-       XIEventMask eventmask;
-       unsigned char mask [1] = { 0 }; // will change
-       eventmask.deviceid = XIAllMasterDevices;
-       eventmask.mask_len = sizeof (mask); // in bytes, for whatever reason...
-       eventmask.mask = mask;
-       XISetMask(mask, XI_KeyPress);
-       XISelectEvents (dpy, winId(), &eventmask, 1);
-
-       xiInited = true;
-}
-
 void MultypoWindow::handleKeyPress(int device, QString string)
 {
        qDebug() << "Device" << device << "String" << string;
 void MultypoWindow::handleKeyPress(int device, QString string)
 {
        qDebug() << "Device" << device << "String" << string;
@@ -68,12 +34,51 @@ void MultypoWindow::handleKeyPress(int device, QString string)
 
 bool MultypoWindow::handleX11Event(XEvent *event)
 {
 
 bool MultypoWindow::handleX11Event(XEvent *event)
 {
-       if (!xiInited) return false;
        Display *dpy = x11Info().display();
        Display *dpy = x11Info().display();
+       /* Handle the first map event: We are finally visible! */
+       if (!xiInited && event->type == MapNotify) {
+               /* XInput Extension available? */
+               int eventID, errorID;
+               if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &eventID, &errorID)) {
+                  qCritical() << "X Input extension not available";
+               }
 
 
-       if (event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
-               && event->xcookie.evtype == XI_KeyPress && XGetEventData(dpy, &event->xcookie)) {
+               /* Which version of XI2? We support 2.0 */
+               int major = 2, minor = 0;
+               if (XIQueryVersion(dpy, &major, &minor) == BadRequest) {
+                       qCritical() << "XI2 not available. Server supports" << major << "." <<
+                                               minor;
+               }
+
+               qDebug() << "System supports XI2";
+
+
+               // Now let's listen to keypress events
+               XIEventMask eventmask;
+               unsigned char mask [1] = { 0 }; // will change
+               eventmask.deviceid = XIAllMasterDevices;
+               eventmask.mask_len = sizeof (mask); // in bytes, for whatever reason...
+               eventmask.mask = mask;
+               XISetMask(mask, XI_KeyPress);
+               XISelectEvents (dpy, winId(), &eventmask, 1);
+
+               // finally, grab all focuses
+               int ndevices;
+               XIDeviceInfo* devices = XIQueryDevice(dpy, XIAllMasterDevices, &ndevices);
+               for (int i = 0; i < ndevices; ++i) {
+                       if (devices[i].use != XIMasterKeyboard) continue;
+                       qDebug() << "Found master keyboard with ID" << devices[i].deviceid;
+                       XISetFocus(dpy, devices[i].deviceid, winId(), CurrentTime);
+               }
 
 
+               xiInited = true;
+
+               return false;
+       }
+       else if (xiInited && event->type == GenericEvent
+               && event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
+               && event->xcookie.evtype == XI_KeyPress && XGetEventData(dpy, &event->xcookie)) {
+               /* Handle XI event */
                XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
            KeyCode keycode = d_ev->detail;
            int kbdid = d_ev->deviceid;
                XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
            KeyCode keycode = d_ev->detail;
            int kbdid = d_ev->deviceid;
@@ -88,6 +93,7 @@ bool MultypoWindow::handleX11Event(XEvent *event)
 
                return true;
        }
 
                return true;
        }
-
-       return false;
+       else {
+               return false;
+       }
 }
 }
index f10a5d40e947d17b2a205a2340857529eef82815..b9dbf3faefc1d49c7a58b000473eb679a1b63f0c 100644 (file)
@@ -15,9 +15,6 @@ public:
 
        bool handleX11Event(XEvent *event);
 
 
        bool handleX11Event(XEvent *event);
 
-protected:
-       virtual void showEvent (QShowEvent *);
-
 private:
        void handleKeyPress(int device, QString string);
 
 private:
        void handleKeyPress(int device, QString string);