fookb-qt/xlib_wrapper.cpp
2020-03-11 22:55:48 +01:00

76 lines
1.9 KiB
C++

#include "xlib_wrapper.h"
#include <QApplication>
#include <QDebug>
/* X Window headers */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
/* XKB fun */
#include <X11/XKBlib.h>
XlibWrapper::XlibWrapper()
{
timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, SIGNAL(timeout()),
this, SLOT(XlibWrapperSlot()));
timer->start(1000);
}
/* Instead of this, should subscribe to the correct QEvent */
/*
* https://doc.qt.io/qt-5/qabstractnativeeventfilter.html
*
* case XCB_MAPPING_NOTIFY:
m_keyboard->handleMappingNotifyEvent((xcb_mapping_notify_event_t *)event);
break;
*/
void XlibWrapper::XlibWrapperSlot()
{
int event_rtrn;
int error_rtrn;
int reason_rtrn;
Display *dpy;
XkbEvent labuda;
int state = 0;
qDebug() << "Timer...";
dpy = XkbOpenDisplay(NULL,
&event_rtrn,
&error_rtrn, NULL, NULL, &reason_rtrn);
if (dpy == NULL) {
qDebug() << "Cannot open display";
qApp->quit();
}
/* We would like receive the only Xkb event: XkbStateNotify. And only
* when XkbLockGroup happens. */
if (False == XkbSelectEvents(dpy,
XkbUseCoreKbd,
XkbAllEventsMask,
0)) {
qDebug() << "Cannot XkbSelectEvents.";
qApp->quit();
}
if (False == XkbSelectEventDetails(dpy,
XkbUseCoreKbd,
XkbStateNotify,
XkbAllEventsMask,
XkbGroupLockMask)) {
qDebug() << "Cannot XkbSelectEventDetails.";
qApp->quit();
}
while (1) {
XNextEvent(dpy, &labuda.core);
state = labuda.state.group;
/* Should not be necessary, here works only with 2 keyboard dispositions */
if ((state < 0) || (state > 1))
state = 1;
qDebug() << "XNextEvent";
/* TODO: update flag depending on state */
}
}