Plasma-workspace

xwindowsystemeventbatcher.cpp
1/*
2 SPDX-FileCopyrightText: 2017 David Edmundson <davidedmundson@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "xwindowsystemeventbatcher.h"
8
9#include <QDebug>
10#include <QTimerEvent>
11
12#include <KX11Extras>
13
14#define BATCH_TIME 10
15
16static const NET::Properties s_cachableProperties = NET::WMName | NET::WMVisibleName | NET::WMIcon;
17static const NET::Properties2 s_cachableProperties2 = NET::WM2UserTime;
18
19XWindowSystemEventBatcher::XWindowSystemEventBatcher(QObject *parent)
20 : QObject(parent)
21{
22 connect(KX11Extras::self(), &KX11Extras::windowAdded, this, &XWindowSystemEventBatcher::windowAdded);
23
24 // remove our cache entries when we lose a window, otherwise we might fire change signals after a window is destroyed which wouldn't make sense
25 connect(KX11Extras::self(), &KX11Extras::windowRemoved, this, [this](WId wid) {
26 m_cache.remove(wid);
27 Q_EMIT windowRemoved(wid);
28 });
29
30 QObject::connect(KX11Extras::self(), &KX11Extras::windowChanged, this, [this](WId window, NET::Properties properties, NET::Properties2 properties2) {
31 // if properties contained only cachable flags
32 if ((properties | s_cachableProperties) == s_cachableProperties && (properties2 | s_cachableProperties2) == s_cachableProperties2) {
33 m_cache[window].properties |= properties;
34 m_cache[window].properties2 |= properties2;
35 if (!m_timerId) {
36 m_timerId = startTimer(BATCH_TIME);
37 }
38 } else {
39 // submit all caches along with any real updates
40 auto it = m_cache.constFind(window);
41 if (it != m_cache.constEnd()) {
42 properties |= it->properties;
43 properties2 |= it->properties2;
44 m_cache.erase(it);
45 }
46 Q_EMIT windowChanged(window, properties, properties2);
47 }
48 });
49}
50
51void XWindowSystemEventBatcher::timerEvent(QTimerEvent *event)
52{
53 if (event->timerId() != m_timerId) {
54 return;
55 }
56 for (auto it = m_cache.constBegin(); it != m_cache.constEnd(); it++) {
57 Q_EMIT windowChanged(it.key(), it.value().properties, it.value().properties2);
58 };
59 m_cache.clear();
60 killTimer(m_timerId);
61 m_timerId = 0;
62}
void windowChanged(WId id, NET::Properties properties, NET::Properties2 properties2)
void windowRemoved(WId id)
void windowAdded(WId id)
QWidget * window(QObject *job)
KGuiItem properties()
void clear()
const_iterator constBegin() const const
const_iterator constEnd() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool event(QEvent *e)
void killTimer(int id)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:06:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.