rename multixkbd -> multypo; get rid of UI magic
[multypo.git] / qt / multypo.cpp
1 #include "multypo.h"
2
3 #include <QX11Info>
4 #include <QDebug>
5
6 #include <X11/Xlib.h>
7 #include <X11/extensions/XInput2.h>
8 #include <X11/Xutil.h>
9
10 MultypoWindow::MultypoWindow(QWidget *parent) :
11         QWidget(parent),
12         xiInited(false)
13 {
14         
15 }
16
17 MultypoWindow::~MultypoWindow()
18 {
19 }
20
21 void MultypoWindow::showEvent ( QShowEvent * )
22 {
23         if (xiInited) return;
24
25         Display *dpy = x11Info().display();
26
27         /* XInput Extension available? */
28         int eventID, errorID;
29         if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &eventID, &errorID)) {
30            qCritical() << "X Input extension not available";
31         }
32
33         /* Which version of XI2? We support 2.0 */
34         int major = 2, minor = 0;
35         if (XIQueryVersion(dpy, &major, &minor) == BadRequest) {
36                 qCritical() << "XI2 not available. Server supports" << major << "." <<
37                                         minor;
38         }
39
40         qDebug() << "System is sane";
41
42
43         // Now let's listen to keypress events
44         XIEventMask eventmask;
45         unsigned char mask [1] = { 0 }; // will change
46         eventmask.deviceid = XIAllMasterDevices;
47         eventmask.mask_len = sizeof (mask); // in bytes, for whatever reason...
48         eventmask.mask = mask;
49         XISetMask(mask, XI_KeyPress);
50         XISelectEvents (dpy, winId(), &eventmask, 1);
51
52         xiInited = true;
53
54         /*activateWindow();
55         raise();
56         setFocus(Qt::OtherFocusReason);*/
57 }
58
59 void MultypoWindow::handleKeyPress(int device, QString string)
60 {
61         qDebug() << "Device" << device << "String" << string;
62 }
63
64 bool MultypoWindow::handleX11Event(XEvent *event)
65 {
66         if (!xiInited) return false;
67         Display *dpy = x11Info().display();
68
69         if (event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
70                 && event->xcookie.evtype == XI_KeyPress && XGetEventData(dpy, &event->xcookie)) {
71
72                 XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
73             KeyCode keycode = d_ev->detail;
74             int kbdid = d_ev->deviceid;
75
76                 if (!(d_ev->flags & XIKeyRepeat)) {
77                         int keysyms_per_keycode;
78                         KeySym keysym = XGetKeyboardMapping (dpy, keycode, 1, &keysyms_per_keycode)[0];
79                         handleKeyPress(kbdid, XKeysymToString(keysym));
80                 }
81
82                 XFreeEventData(dpy, &event->xcookie);
83
84                 return true;
85         }
86
87         return false;
88 }