From 9f976a4b8dd78b7b4312806b2f78c609879c3ea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Tue, 15 Nov 2016 08:22:41 +0100 Subject: [PATCH] Support multiple event types in X11EventFilter Summary: So far the X11EventFilter only supported one event type. But most usecases for an X11EventFilter require to support multiple event types. E.g. a filter listens for both key press and key release. This change extends the internal X11EventFilter to support multiple types and Workspace::workspaceEvent makes use of that. Reviewers: #kwin, #plasma Subscribers: plasma-devel, kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D3361 --- autotests/test_xrandr_screens.cpp | 4 ++-- events.cpp | 6 +++--- x11eventfilter.cpp | 18 +++++++++++++++++- x11eventfilter.h | 10 ++++++++-- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/autotests/test_xrandr_screens.cpp b/autotests/test_xrandr_screens.cpp index 9ee401e0ed..674198b39f 100644 --- a/autotests/test_xrandr_screens.cpp +++ b/autotests/test_xrandr_screens.cpp @@ -135,8 +135,8 @@ void TestXRandRScreens::testStartup() { KWin::MockWorkspace ws; QScopedPointer screens(new XRandRScreens(this)); - QVERIFY(screens->eventType() != 0); - QCOMPARE(screens->eventType(), Xcb::Extensions::self()->randrNotifyEvent()); + QVERIFY(!screens->eventTypes().isEmpty()); + QCOMPARE(screens->eventTypes().first(), Xcb::Extensions::self()->randrNotifyEvent()); QCOMPARE(screens->extension(), 0); QCOMPARE(screens->genericEventTypes(), QVector{0}); screens->init(); diff --git a/events.cpp b/events.cpp index d8da5a7c0a..d48afa2ba2 100644 --- a/events.cpp +++ b/events.cpp @@ -182,7 +182,7 @@ QVector s_xcbEerrors({ void Workspace::registerEventFilter(X11EventFilter *filter) { - if (filter->eventType() == XCB_GE_GENERIC) + if (filter->isGenericEvent()) m_genericEventFilters.append(filter); else m_eventFilters.append(filter); @@ -190,7 +190,7 @@ void Workspace::registerEventFilter(X11EventFilter *filter) void Workspace::unregisterEventFilter(X11EventFilter *filter) { - if (filter->eventType() == XCB_GE_GENERIC) + if (filter->isGenericEvent()) m_genericEventFilters.removeOne(filter); else m_eventFilters.removeOne(filter); @@ -243,7 +243,7 @@ bool Workspace::workspaceEvent(xcb_generic_event_t *e) } } else { foreach (X11EventFilter *filter, m_eventFilters) { - if (filter->eventType() == eventType && filter->event(e)) { + if (filter->eventTypes().contains(eventType) && filter->event(e)) { return true; } } diff --git a/x11eventfilter.cpp b/x11eventfilter.cpp index 643ca0ee10..ea2daf91be 100644 --- a/x11eventfilter.cpp +++ b/x11eventfilter.cpp @@ -24,13 +24,21 @@ along with this program. If not, see . namespace KWin { +X11EventFilter::X11EventFilter(const QVector &eventTypes) + : m_eventTypes(eventTypes) + , m_extension(0) +{ + Workspace::self()->registerEventFilter(this); +} + + X11EventFilter::X11EventFilter(int eventType, int opcode, int genericEventType) : X11EventFilter(eventType, opcode, QVector{genericEventType}) { } X11EventFilter::X11EventFilter(int eventType, int opcode, const QVector< int > &genericEventTypes) - : m_eventType(eventType), m_extension(opcode), m_genericEventTypes(genericEventTypes) + : m_eventTypes(QVector{eventType}), m_extension(opcode), m_genericEventTypes(genericEventTypes) { Workspace::self()->registerEventFilter(this); } @@ -42,4 +50,12 @@ X11EventFilter::~X11EventFilter() } } +bool X11EventFilter::isGenericEvent() const +{ + if (m_eventTypes.count() != 1) { + return false; + } + return m_eventTypes.first() == XCB_GE_GENERIC; +} + } diff --git a/x11eventfilter.h b/x11eventfilter.h index f352fdc307..ea25ef02a8 100644 --- a/x11eventfilter.h +++ b/x11eventfilter.h @@ -38,6 +38,7 @@ public: */ X11EventFilter(int eventType, int opcode = 0, int genericEventType = 0); X11EventFilter(int eventType, int opcode, const QVector &genericEventTypes); + X11EventFilter(const QVector &eventTypes); /** * Destroys the event filter. @@ -47,7 +48,7 @@ public: /** * Returns the type of events to filter. */ - int eventType() const { return m_eventType; } + QVector eventTypes() const { return m_eventTypes; } /** * Returns the major opcode of the extension. @@ -70,8 +71,13 @@ public: */ virtual bool event(xcb_generic_event_t *event) = 0; + /** + * Whether the event filter is for XCB_GE_GENERIC events. + **/ + bool isGenericEvent() const; + private: - int m_eventType; + QVector m_eventTypes; int m_extension; QVector m_genericEventTypes; };