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 #define BATCH_TIME 10
13
14 static const NET::Properties s_cachableProperties = NET::WMName | NET::WMVisibleName;
15 static const NET::Properties2 s_cachableProperties2 = NET::WM2UserTime;
16
XWindowSystemEventBatcher(QObject * parent)17 XWindowSystemEventBatcher::XWindowSystemEventBatcher(QObject *parent)
18 : QObject(parent)
19 {
20 connect(KWindowSystem::self(), &KWindowSystem::windowAdded, this, &XWindowSystemEventBatcher::windowAdded);
21
22 // 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
23 connect(KWindowSystem::self(), &KWindowSystem::windowRemoved, this, [this](WId wid) {
24 m_cache.remove(wid);
25 emit windowRemoved(wid);
26 });
27
28 void (KWindowSystem::*myWindowChangeSignal)(WId window, NET::Properties properties, NET::Properties2 properties2) = &KWindowSystem::windowChanged;
29 QObject::connect(KWindowSystem::self(), myWindowChangeSignal, this, [this](WId window, NET::Properties properties, NET::Properties2 properties2) {
30 // if properties contained only cachable flags
31 if ((properties | s_cachableProperties) == s_cachableProperties && (properties2 | s_cachableProperties2) == s_cachableProperties2) {
32 m_cache[window].properties |= properties;
33 m_cache[window].properties2 |= properties2;
34 if (!m_timerId) {
35 m_timerId = startTimer(BATCH_TIME);
36 }
37 } else {
38 // submit all caches along with any real updates
39 auto it = m_cache.constFind(window);
40 if (it != m_cache.constEnd()) {
41 properties |= it->properties;
42 properties2 |= it->properties2;
43 m_cache.erase(it);
44 }
45 emit windowChanged(window, properties, properties2);
46 }
47 });
48 }
49
timerEvent(QTimerEvent * event)50 void XWindowSystemEventBatcher::timerEvent(QTimerEvent *event)
51 {
52 if (event->timerId() != m_timerId) {
53 return;
54 }
55 for (auto it = m_cache.constBegin(); it != m_cache.constEnd(); it++) {
56 emit windowChanged(it.key(), it.value().properties, it.value().properties2);
57 };
58 m_cache.clear();
59 killTimer(m_timerId);
60 m_timerId = 0;
61 }
62