10 #include <X11/extensions/XInput2.h>
11 #include <X11/Xutil.h>
13 MultypoWindow::MultypoWindow(QWidget *parent) :
18 setStyleSheet("background-color: black; color: green; font-size: 30pt");
21 setLayout(new QVBoxLayout(this));
23 mainLabel = new QLabel("Hit <Esc> to quit", this);
24 layout()->addWidget(mainLabel);
26 /* Fullscreen, no cursor */
27 setWindowState(Qt::WindowFullScreen);
28 setCursor(QCursor(Qt::BlankCursor));
30 words.open(stdin,QIODevice::ReadOnly);
33 MultypoWindow::~MultypoWindow()
37 void MultypoWindow::resetPlayerText() {
38 for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
39 it.value()->resetText();
43 void MultypoWindow::nextWord() {
44 QByteArray tmp = words.readLine();
45 QString word = QString::fromUtf8(tmp);
46 mainLabel->setText(word);
49 void MultypoWindow::handleKeyPress(int device, QString string)
51 qDebug() << "Device" << device << "String" << string;
53 if (string == "Escape") {
58 if (!players.contains(device)) {
59 players[device] = new Player(this);
61 players[device]->handleKey(string);
63 if (gameStarted) { // ingame
65 } else { // name entering phase
66 bool allHaveNames = true;
67 for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
68 if (! it.value()->hasName()) {
73 if (!players.empty() && allHaveNames) {
80 bool MultypoWindow::handleX11Event(XEvent *event)
82 Display *dpy = x11Info().display();
83 /* Handle the first map event: We are finally visible! */
84 if (!xiInited && event->type == MapNotify) {
85 /* XInput Extension available? */
87 if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &eventID, &errorID)) {
88 qCritical() << "X Input extension not available";
91 /* Which version of XI2? We support 2.0 */
92 int major = 2, minor = 1;
93 if (XIQueryVersion(dpy, &major, &minor) == BadRequest) {
94 qCritical() << "XI2 not available. Server supports" << major << "." <<
98 qDebug() << "System supports XI2";
101 // Now let's listen to keypress events
102 XIEventMask eventmask;
103 unsigned char mask [1] = { 0 }; // will change
104 eventmask.deviceid = XIAllMasterDevices;
105 eventmask.mask_len = sizeof (mask); // in bytes, for whatever reason...
106 eventmask.mask = mask;
107 XISetMask(mask, XI_KeyPress);
108 XISelectEvents (dpy, winId(), &eventmask, 1);
110 // finally, grab all focuses
112 XIDeviceInfo* devices = XIQueryDevice(dpy, XIAllMasterDevices, &ndevices);
113 for (int i = 0; i < ndevices; ++i) {
114 if (devices[i].use != XIMasterKeyboard) continue;
115 qDebug() << "Found master keyboard with ID" << devices[i].deviceid;
116 //XISetFocus(dpy, devices[i].deviceid, winId(), CurrentTime);
118 XIFreeDeviceInfo(devices);
124 else if (xiInited && event->type == GenericEvent
125 && event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
126 && event->xcookie.evtype == XI_KeyPress) {
128 if (XGetEventData(dpy, &event->xcookie) != True) {
129 qCritical() << "Error getting event data";
130 // FIXME return true;
133 /* Handle XI event */
134 XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
135 KeyCode keycode = d_ev->detail;
136 int kbdid = d_ev->deviceid;
138 if (!(d_ev->flags & XIKeyRepeat)) {
139 int keysyms_per_keycode;
140 KeySym keysym = XGetKeyboardMapping (dpy, keycode, 1, &keysyms_per_keycode)[0];
141 handleKeyPress(kbdid, XKeysymToString(keysym));
144 XFreeEventData(dpy, &event->xcookie);