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
This commit is contained in:
Martin Gräßlin 2016-11-15 08:22:41 +01:00
parent 730fd05f58
commit 9f976a4b8d
4 changed files with 30 additions and 8 deletions

View file

@ -135,8 +135,8 @@ void TestXRandRScreens::testStartup()
{
KWin::MockWorkspace ws;
QScopedPointer<XRandRScreens> 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<int>{0});
screens->init();

View file

@ -182,7 +182,7 @@ QVector<QByteArray> 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;
}
}

View file

@ -24,13 +24,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
namespace KWin
{
X11EventFilter::X11EventFilter(const QVector<int> &eventTypes)
: m_eventTypes(eventTypes)
, m_extension(0)
{
Workspace::self()->registerEventFilter(this);
}
X11EventFilter::X11EventFilter(int eventType, int opcode, int genericEventType)
: X11EventFilter(eventType, opcode, QVector<int>{genericEventType})
{
}
X11EventFilter::X11EventFilter(int eventType, int opcode, const QVector< int > &genericEventTypes)
: m_eventType(eventType), m_extension(opcode), m_genericEventTypes(genericEventTypes)
: m_eventTypes(QVector<int>{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;
}
}

View file

@ -38,6 +38,7 @@ public:
*/
X11EventFilter(int eventType, int opcode = 0, int genericEventType = 0);
X11EventFilter(int eventType, int opcode, const QVector<int> &genericEventTypes);
X11EventFilter(const QVector<int> &eventTypes);
/**
* Destroys the event filter.
@ -47,7 +48,7 @@ public:
/**
* Returns the type of events to filter.
*/
int eventType() const { return m_eventType; }
QVector<int> 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<int> m_eventTypes;
int m_extension;
QVector<int> m_genericEventTypes;
};