2016-08-16 13:31:14 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2016 Martin Gräßlin <mgraesslin@kde.org>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************/
|
|
|
|
#include "xinputintegration.h"
|
2016-08-19 14:56:21 +00:00
|
|
|
#include "main.h"
|
2016-08-16 13:31:14 +00:00
|
|
|
#include "logging.h"
|
2017-03-22 20:03:04 +00:00
|
|
|
#include "gestures.h"
|
2016-08-19 14:56:21 +00:00
|
|
|
#include "platform.h"
|
2017-03-22 20:03:04 +00:00
|
|
|
#include "screenedge.h"
|
2016-08-16 13:31:14 +00:00
|
|
|
#include "x11cursor.h"
|
|
|
|
|
2017-02-03 16:26:51 +00:00
|
|
|
#include "input.h"
|
2016-08-16 13:31:14 +00:00
|
|
|
#include "x11eventfilter.h"
|
2017-02-03 16:26:51 +00:00
|
|
|
#include "modifier_only_shortcuts.h"
|
2016-08-16 13:31:14 +00:00
|
|
|
#include <kwinglobals.h>
|
|
|
|
|
|
|
|
#include <X11/extensions/XInput2.h>
|
|
|
|
#include <X11/extensions/XI2proto.h>
|
|
|
|
|
2016-08-19 14:56:21 +00:00
|
|
|
#include <linux/input.h>
|
|
|
|
|
2016-08-16 13:31:14 +00:00
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2017-03-22 20:03:04 +00:00
|
|
|
static inline qreal fixed1616ToReal(FP1616 val)
|
|
|
|
{
|
|
|
|
return (val) * 1.0 / (1 << 16);
|
|
|
|
}
|
|
|
|
|
2016-08-16 13:31:14 +00:00
|
|
|
class XInputEventFilter : public X11EventFilter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
XInputEventFilter(int xi_opcode)
|
2017-03-22 20:03:04 +00:00
|
|
|
: X11EventFilter(XCB_GE_GENERIC, xi_opcode, QVector<int>{XI_RawMotion, XI_RawButtonPress, XI_RawButtonRelease, XI_RawKeyPress, XI_RawKeyRelease, XI_TouchBegin, XI_TouchUpdate, XI_TouchOwnership, XI_TouchEnd})
|
2016-08-16 13:31:14 +00:00
|
|
|
{}
|
|
|
|
virtual ~XInputEventFilter() = default;
|
|
|
|
|
|
|
|
bool event(xcb_generic_event_t *event) override {
|
|
|
|
xcb_ge_generic_event_t *ge = reinterpret_cast<xcb_ge_generic_event_t *>(event);
|
2017-03-22 20:03:04 +00:00
|
|
|
xi2PrepareXIGenericDeviceEvent(ge);
|
2016-08-16 13:31:14 +00:00
|
|
|
switch (ge->event_type) {
|
2017-02-03 16:26:51 +00:00
|
|
|
case XI_RawKeyPress: {
|
|
|
|
auto re = reinterpret_cast<xXIRawEvent*>(event);
|
|
|
|
kwinApp()->platform()->keyboardKeyPressed(re->detail - 8, re->time);
|
2016-08-16 13:31:14 +00:00
|
|
|
break;
|
2017-02-03 16:26:51 +00:00
|
|
|
}
|
|
|
|
case XI_RawKeyRelease: {
|
|
|
|
auto re = reinterpret_cast<xXIRawEvent*>(event);
|
|
|
|
kwinApp()->platform()->keyboardKeyReleased(re->detail - 8, re->time);
|
2016-08-16 13:31:14 +00:00
|
|
|
break;
|
2017-02-03 16:26:51 +00:00
|
|
|
}
|
|
|
|
case XI_RawButtonPress: {
|
2016-08-19 14:56:21 +00:00
|
|
|
auto e = reinterpret_cast<xXIRawEvent*>(event);
|
|
|
|
switch (e->detail) {
|
|
|
|
// TODO: this currently ignores left handed settings, for current usage not needed
|
|
|
|
// if we want to use also for global mouse shortcuts, this needs to reflect state correctly
|
|
|
|
case XCB_BUTTON_INDEX_1:
|
|
|
|
kwinApp()->platform()->pointerButtonPressed(BTN_LEFT, e->time);
|
|
|
|
break;
|
|
|
|
case XCB_BUTTON_INDEX_2:
|
|
|
|
kwinApp()->platform()->pointerButtonPressed(BTN_MIDDLE, e->time);
|
|
|
|
break;
|
|
|
|
case XCB_BUTTON_INDEX_3:
|
|
|
|
kwinApp()->platform()->pointerButtonPressed(BTN_RIGHT, e->time);
|
|
|
|
break;
|
|
|
|
case XCB_BUTTON_INDEX_4:
|
|
|
|
case XCB_BUTTON_INDEX_5:
|
|
|
|
// vertical axis, ignore on press
|
|
|
|
break;
|
|
|
|
// TODO: further buttons, horizontal scrolling?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m_x11Cursor) {
|
|
|
|
m_x11Cursor->schedulePoll();
|
|
|
|
}
|
|
|
|
break;
|
2017-02-03 16:26:51 +00:00
|
|
|
case XI_RawButtonRelease: {
|
2016-08-19 14:56:21 +00:00
|
|
|
auto e = reinterpret_cast<xXIRawEvent*>(event);
|
|
|
|
switch (e->detail) {
|
|
|
|
// TODO: this currently ignores left handed settings, for current usage not needed
|
|
|
|
// if we want to use also for global mouse shortcuts, this needs to reflect state correctly
|
|
|
|
case XCB_BUTTON_INDEX_1:
|
|
|
|
kwinApp()->platform()->pointerButtonReleased(BTN_LEFT, e->time);
|
|
|
|
break;
|
|
|
|
case XCB_BUTTON_INDEX_2:
|
|
|
|
kwinApp()->platform()->pointerButtonReleased(BTN_MIDDLE, e->time);
|
|
|
|
break;
|
|
|
|
case XCB_BUTTON_INDEX_3:
|
|
|
|
kwinApp()->platform()->pointerButtonReleased(BTN_RIGHT, e->time);
|
|
|
|
break;
|
|
|
|
case XCB_BUTTON_INDEX_4:
|
|
|
|
kwinApp()->platform()->pointerAxisVertical(120, e->time);
|
|
|
|
break;
|
|
|
|
case XCB_BUTTON_INDEX_5:
|
|
|
|
kwinApp()->platform()->pointerAxisVertical(-120, e->time);
|
|
|
|
break;
|
|
|
|
// TODO: further buttons, horizontal scrolling?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m_x11Cursor) {
|
|
|
|
m_x11Cursor->schedulePoll();
|
|
|
|
}
|
|
|
|
break;
|
2017-03-22 20:03:04 +00:00
|
|
|
case XI_TouchBegin: {
|
|
|
|
auto e = reinterpret_cast<xXIDeviceEvent*>(ge);
|
|
|
|
m_lastTouchPositions.insert(e->detail, QPointF(fixed1616ToReal(e->event_x), fixed1616ToReal(e->event_y)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case XI_TouchUpdate: {
|
|
|
|
auto e = reinterpret_cast<xXIDeviceEvent*>(event);
|
|
|
|
const QPointF touchPosition = QPointF(fixed1616ToReal(e->event_x), fixed1616ToReal(e->event_y));
|
|
|
|
if (e->detail == m_trackingTouchId) {
|
|
|
|
const auto last = m_lastTouchPositions.value(e->detail);
|
|
|
|
ScreenEdges::self()->gestureRecognizer()->updateSwipeGesture(QSizeF(touchPosition.x() - last.x(), touchPosition.y() - last.y()));
|
|
|
|
}
|
|
|
|
m_lastTouchPositions.insert(e->detail, touchPosition);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case XI_TouchEnd: {
|
|
|
|
auto e = reinterpret_cast<xXIDeviceEvent*>(event);
|
|
|
|
if (e->detail == m_trackingTouchId) {
|
|
|
|
ScreenEdges::self()->gestureRecognizer()->endSwipeGesture();
|
|
|
|
}
|
|
|
|
m_lastTouchPositions.remove(e->detail);
|
|
|
|
m_trackingTouchId = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case XI_TouchOwnership: {
|
|
|
|
auto e = reinterpret_cast<xXITouchOwnershipEvent*>(event);
|
|
|
|
auto it = m_lastTouchPositions.constFind(e->touchid);
|
|
|
|
if (it == m_lastTouchPositions.constEnd()) {
|
|
|
|
XIAllowTouchEvents(display(), e->deviceid, e->sourceid, e->touchid, XIRejectTouch);
|
|
|
|
} else {
|
|
|
|
if (ScreenEdges::self()->gestureRecognizer()->startSwipeGesture(it.value()) > 0) {
|
|
|
|
m_trackingTouchId = e->touchid;
|
|
|
|
}
|
|
|
|
XIAllowTouchEvents(display(), e->deviceid, e->sourceid, e->touchid, m_trackingTouchId == e->touchid ? XIAcceptTouch : XIRejectTouch);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-08-16 13:31:14 +00:00
|
|
|
default:
|
|
|
|
if (m_x11Cursor) {
|
|
|
|
m_x11Cursor->schedulePoll();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setCursor(const QPointer<X11Cursor> &cursor) {
|
|
|
|
m_x11Cursor = cursor;
|
|
|
|
}
|
2017-03-22 20:03:04 +00:00
|
|
|
void setDisplay(Display *display) {
|
|
|
|
m_x11Display = display;
|
|
|
|
}
|
2016-08-16 13:31:14 +00:00
|
|
|
|
|
|
|
private:
|
2017-03-22 20:03:04 +00:00
|
|
|
Display *display() const {
|
|
|
|
return m_x11Display;
|
|
|
|
}
|
|
|
|
|
|
|
|
void xi2PrepareXIGenericDeviceEvent(xcb_ge_generic_event_t *event) {
|
|
|
|
// xcb event structs contain stuff that wasn't on the wire, the full_sequence field
|
|
|
|
// adds an extra 4 bytes and generic events cookie data is on the wire right after the standard 32 bytes.
|
|
|
|
// Move this data back to have the same layout in memory as it was on the wire
|
|
|
|
// and allow casting, overwriting the full_sequence field.
|
|
|
|
memmove((char*) event + 32, (char*) event + 36, event->length * 4);
|
|
|
|
}
|
|
|
|
|
2016-08-16 13:31:14 +00:00
|
|
|
QPointer<X11Cursor> m_x11Cursor;
|
2017-03-22 20:03:04 +00:00
|
|
|
Display *m_x11Display = nullptr;
|
|
|
|
uint32_t m_trackingTouchId = 0;
|
|
|
|
QHash<uint32_t, QPointF> m_lastTouchPositions;
|
2016-08-16 13:31:14 +00:00
|
|
|
};
|
|
|
|
|
2016-10-07 14:24:27 +00:00
|
|
|
class XKeyPressReleaseEventFilter : public X11EventFilter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
XKeyPressReleaseEventFilter(uint32_t type)
|
|
|
|
: X11EventFilter(type)
|
|
|
|
{}
|
|
|
|
~XKeyPressReleaseEventFilter() = default;
|
|
|
|
|
|
|
|
bool event(xcb_generic_event_t *event) override {
|
|
|
|
xcb_key_press_event_t *ke = reinterpret_cast<xcb_key_press_event_t *>(event);
|
2017-02-03 16:26:51 +00:00
|
|
|
if (ke->event == ke->root) {
|
2016-10-07 14:24:27 +00:00
|
|
|
const uint8_t eventType = event->response_type & ~0x80;
|
|
|
|
if (eventType == XCB_KEY_PRESS) {
|
2017-02-03 16:26:51 +00:00
|
|
|
kwinApp()->platform()->keyboardKeyPressed(ke->detail - 8, ke->time);
|
2016-10-07 14:24:27 +00:00
|
|
|
} else {
|
2017-02-03 16:26:51 +00:00
|
|
|
kwinApp()->platform()->keyboardKeyReleased(ke->detail - 8, ke->time);
|
2016-10-07 14:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2016-08-16 13:31:14 +00:00
|
|
|
|
2016-11-11 08:59:46 +00:00
|
|
|
XInputIntegration::XInputIntegration(Display *display, QObject *parent)
|
2016-08-16 13:31:14 +00:00
|
|
|
: QObject(parent)
|
2016-11-11 08:59:46 +00:00
|
|
|
, m_x11Display(display)
|
2016-08-16 13:31:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
XInputIntegration::~XInputIntegration() = default;
|
|
|
|
|
|
|
|
void XInputIntegration::init()
|
|
|
|
{
|
|
|
|
Display *dpy = display();
|
|
|
|
int xi_opcode, event, error;
|
|
|
|
// init XInput extension
|
|
|
|
if (!XQueryExtension(dpy, "XInputExtension", &xi_opcode, &event, &error)) {
|
|
|
|
qCDebug(KWIN_X11STANDALONE) << "XInputExtension not present";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify that the XInput extension is at at least version 2.0
|
2017-03-22 20:03:04 +00:00
|
|
|
int major = 2, minor = 2;
|
2016-08-16 13:31:14 +00:00
|
|
|
int result = XIQueryVersion(dpy, &major, &minor);
|
2017-03-22 20:03:04 +00:00
|
|
|
if (result != Success) {
|
|
|
|
qCDebug(KWIN_X11STANDALONE) << "Failed to init XInput 2.2, trying 2.0";
|
|
|
|
minor = 0;
|
2016-08-16 13:31:14 +00:00
|
|
|
if (XIQueryVersion(dpy, &major, &minor) != Success) {
|
|
|
|
qCDebug(KWIN_X11STANDALONE) << "Failed to init XInput";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_hasXInput = true;
|
|
|
|
m_xiOpcode = xi_opcode;
|
|
|
|
m_majorVersion = major;
|
|
|
|
m_minorVersion = minor;
|
|
|
|
qCDebug(KWIN_X11STANDALONE) << "Has XInput support" << m_majorVersion << "." << m_minorVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
void XInputIntegration::setCursor(X11Cursor *cursor)
|
|
|
|
{
|
|
|
|
m_x11Cursor = QPointer<X11Cursor>(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void XInputIntegration::startListening()
|
|
|
|
{
|
|
|
|
// this assumes KWin is the only one setting events on the root window
|
|
|
|
// given Qt's source code this seems to be true. If it breaks, we need to change
|
|
|
|
XIEventMask evmasks[1];
|
|
|
|
unsigned char mask1[XIMaskLen(XI_LASTEVENT)];
|
|
|
|
|
|
|
|
memset(mask1, 0, sizeof(mask1));
|
|
|
|
|
|
|
|
XISetMask(mask1, XI_RawMotion);
|
|
|
|
XISetMask(mask1, XI_RawButtonPress);
|
|
|
|
XISetMask(mask1, XI_RawButtonRelease);
|
|
|
|
if (m_majorVersion >= 2 && m_minorVersion >= 1) {
|
|
|
|
// we need to listen to all events, which is only available with XInput 2.1
|
|
|
|
XISetMask(mask1, XI_RawKeyPress);
|
|
|
|
XISetMask(mask1, XI_RawKeyRelease);
|
|
|
|
}
|
2017-03-22 20:03:04 +00:00
|
|
|
if (m_majorVersion >=2 && m_minorVersion >= 2) {
|
|
|
|
// touch events since 2.2
|
|
|
|
XISetMask(mask1, XI_TouchBegin);
|
|
|
|
XISetMask(mask1, XI_TouchUpdate);
|
|
|
|
XISetMask(mask1, XI_TouchOwnership);
|
|
|
|
XISetMask(mask1, XI_TouchEnd);
|
|
|
|
}
|
2016-08-16 13:31:14 +00:00
|
|
|
|
|
|
|
evmasks[0].deviceid = XIAllMasterDevices;
|
|
|
|
evmasks[0].mask_len = sizeof(mask1);
|
|
|
|
evmasks[0].mask = mask1;
|
|
|
|
XISelectEvents(display(), rootWindow(), evmasks, 1);
|
2017-03-22 20:03:04 +00:00
|
|
|
|
|
|
|
if (m_majorVersion >=2 && m_minorVersion >= 2) {
|
|
|
|
XIGrabModifiers mods = { int(XIAnyModifier), 0 };
|
|
|
|
XIEventMask touchEvmasks[1];
|
|
|
|
unsigned char touchMask[XIMaskLen(XI_LASTEVENT)];
|
|
|
|
|
|
|
|
memset(touchMask, 0, sizeof(touchMask));
|
|
|
|
|
|
|
|
XISetMask(touchMask, XI_TouchBegin);
|
|
|
|
XISetMask(touchMask, XI_TouchUpdate);
|
|
|
|
XISetMask(touchMask, XI_TouchOwnership);
|
|
|
|
XISetMask(touchMask, XI_TouchEnd);
|
|
|
|
|
|
|
|
touchEvmasks[0].deviceid = XIAllMasterDevices;
|
|
|
|
touchEvmasks[0].mask_len = sizeof(touchMask);
|
|
|
|
touchEvmasks[0].mask = touchMask;
|
|
|
|
|
|
|
|
XIGrabTouchBegin(display(), XIAllMasterDevices, rootWindow(), False, touchEvmasks, 1, &mods);
|
|
|
|
}
|
2016-08-16 13:31:14 +00:00
|
|
|
m_xiEventFilter.reset(new XInputEventFilter(m_xiOpcode));
|
|
|
|
m_xiEventFilter->setCursor(m_x11Cursor);
|
2017-03-22 20:03:04 +00:00
|
|
|
m_xiEventFilter->setDisplay(display());
|
2016-10-07 14:24:27 +00:00
|
|
|
m_keyPressFilter.reset(new XKeyPressReleaseEventFilter(XCB_KEY_PRESS));
|
|
|
|
m_keyReleaseFilter.reset(new XKeyPressReleaseEventFilter(XCB_KEY_RELEASE));
|
2017-02-03 16:26:51 +00:00
|
|
|
|
|
|
|
// install the input event spies also relevant for X11 platform
|
|
|
|
input()->installInputEventSpy(new ModifierOnlyShortcuts);
|
2016-08-16 13:31:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|