9 #include <X11/extensions/XInput2.h>
10 #include <X11/Xutil.h>
12 MultypoWindow::MultypoWindow(QWidget *parent) :
17 setStyleSheet("background-color: black; color: green; font-size: 30pt");
20 setLayout(new QVBoxLayout(this));
22 mainLabel = new QLabel("Hit <Esc> to quit", this);
23 layout()->addWidget(mainLabel);
25 /* Fullscreen, no cursor */
26 setWindowState(Qt::WindowFullScreen);
27 setCursor(QCursor(Qt::BlankCursor));
30 MultypoWindow::~MultypoWindow()
34 void MultypoWindow::handleKeyPress(int device, QString string)
36 qDebug() << "Device" << device << "String" << string;
38 if (string == "Escape") {
43 if (!players.contains(device)) {
44 players[device] = new Player(this);
46 players[device]->handleKey(string);
49 bool MultypoWindow::handleX11Event(XEvent *event)
51 Display *dpy = x11Info().display();
52 /* Handle the first map event: We are finally visible! */
53 if (!xiInited && event->type == MapNotify) {
54 /* XInput Extension available? */
56 if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &eventID, &errorID)) {
57 qCritical() << "X Input extension not available";
60 /* Which version of XI2? We support 2.0 */
61 int major = 2, minor = 1;
62 if (XIQueryVersion(dpy, &major, &minor) == BadRequest) {
63 qCritical() << "XI2 not available. Server supports" << major << "." <<
67 qDebug() << "System supports XI2";
70 // Now let's listen to keypress events
71 XIEventMask eventmask;
72 unsigned char mask [1] = { 0 }; // will change
73 eventmask.deviceid = XIAllMasterDevices;
74 eventmask.mask_len = sizeof (mask); // in bytes, for whatever reason...
75 eventmask.mask = mask;
76 XISetMask(mask, XI_KeyPress);
77 XISelectEvents (dpy, winId(), &eventmask, 1);
79 // finally, grab all focuses
81 XIDeviceInfo* devices = XIQueryDevice(dpy, XIAllMasterDevices, &ndevices);
82 for (int i = 0; i < ndevices; ++i) {
83 if (devices[i].use != XIMasterKeyboard) continue;
84 qDebug() << "Found master keyboard with ID" << devices[i].deviceid;
85 //XISetFocus(dpy, devices[i].deviceid, winId(), CurrentTime);
87 XIFreeDeviceInfo(devices);
93 else if (xiInited && event->type == GenericEvent
94 && event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
95 && event->xcookie.evtype == XI_KeyPress) {
97 if (XGetEventData(dpy, &event->xcookie) != True) {
98 qCritical() << "Error getting event data";
102 /* Handle XI event */
103 XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
104 KeyCode keycode = d_ev->detail;
105 int kbdid = d_ev->deviceid;
107 if (!(d_ev->flags & XIKeyRepeat)) {
108 int keysyms_per_keycode;
109 KeySym keysym = XGetKeyboardMapping (dpy, keycode, 1, &keysyms_per_keycode)[0];
110 handleKeyPress(kbdid, XKeysymToString(keysym));
113 XFreeEventData(dpy, &event->xcookie);