File: screenpool.cpp

package info (click to toggle)
latte-dock 0.10.9-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 11,480 kB
  • sloc: cpp: 43,957; xml: 833; javascript: 734; sh: 43; makefile: 3
file content (317 lines) | stat: -rw-r--r-- 8,695 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
    SPDX-FileCopyrightText: 2016 Marco Martin <mart@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "screenpool.h"

// local
#include <config-latte.h>

// Qt
#include <QDebug>
#include <QFile>
#include <QGuiApplication>
#include <QScreen>

// KDE
#include <KLocalizedString>

// X11
#if HAVE_X11
#include <QtX11Extras/QX11Info>
#include <xcb/xcb.h>
#include <xcb/randr.h>
#include <xcb/xcb_event.h>
#endif

namespace Latte {

const int ScreenPool::FIRSTSCREENID;

ScreenPool::ScreenPool(KSharedConfig::Ptr config, QObject *parent)
    : QObject(parent),
      m_configGroup(KConfigGroup(config, QStringLiteral("ScreenConnectors")))
{
    qApp->installNativeEventFilter(this);

    m_configSaveTimer.setSingleShot(true);
    connect(&m_configSaveTimer, &QTimer::timeout, this, [this]() {
        m_configGroup.sync();
    });

    connect(qGuiApp, &QGuiApplication::screenAdded, this, &ScreenPool::onScreenAdded, Qt::UniqueConnection);
    connect(qGuiApp, &QGuiApplication::screenRemoved, this, &ScreenPool::onScreenRemoved, Qt::UniqueConnection);
}

ScreenPool::~ScreenPool()
{
    m_configGroup.sync();
}

void ScreenPool::load()
{
    m_lastPrimaryConnector = QString();
    m_screensTable.clear();

    QScreen *primary = qGuiApp->primaryScreen();

    if (primary) {
        m_lastPrimaryConnector = primary->name();
    }

    //restore the known ids to connector mappings
    for (const QString &key : m_configGroup.keyList()) {
        if (key.toInt() <= 0) {
            continue;
        }

        QString serialized = m_configGroup.readEntry(key, QString());

        Data::Screen screenRecord(key, serialized);
        //qDebug() << "org.kde.latte ::: " << screenRecord.id << ":" << screenRecord.serialize();

        if (!key.isEmpty() && !serialized.isEmpty() && !m_screensTable.containsId(key)) {
            m_screensTable << screenRecord;
            qDebug() << "org.kde.latte :: Known Screen - " << screenRecord.id << " : " << screenRecord.name << " : " << screenRecord.geometry;
        }
    }

    // if there are already connected unknown screens, map those
    // all needs to be populated as soon as possible, otherwise
    // containment->screen() will return an incorrect -1
    // at startup, if it' asked before corona::addOutput()
    // is performed, driving to the creation of a new containment
    for (QScreen *screen : qGuiApp->screens()) {
        onScreenRemoved(screen);

        if (!m_screensTable.containsName(screen->name())) {
            insertScreenMapping(screen->name());
        } else {
            updateScreenGeometry(screen);
        }

        onScreenAdded(screen);
    }
}

void ScreenPool::onScreenAdded(const QScreen *screen)
{
    connect(screen, &QScreen::geometryChanged, this, [&, screen]() {
        updateScreenGeometry(screen);
    });
}

void ScreenPool::onScreenRemoved(const QScreen *screen)
{
    disconnect(screen, &QScreen::geometryChanged, this, nullptr);
}

void ScreenPool::updateScreenGeometry(const QScreen *screen)
{
    if (!screen) {
        return;
    }

    if (m_screensTable.containsName(screen->name())) {
        updateScreenGeometry(id(screen->name()), screen->geometry());
    }
}

void ScreenPool::updateScreenGeometry(const int &screenId, const QRect &screenGeometry)
{
    QString scrIdStr = QString::number(screenId);

    if (!m_screensTable.containsId(scrIdStr) || m_screensTable[scrIdStr].geometry == screenGeometry) {
        return;
    }

    m_screensTable[scrIdStr].geometry = screenGeometry;
    save();

    emit screenGeometryChanged();
}


Latte::Data::ScreensTable ScreenPool::screensTable()
{   
    return m_screensTable;
}

void ScreenPool::reload(QString path)
{
    QFile rcfile(QString(path + "/lattedockrc"));

    if (rcfile.exists()) {
        qDebug() << "load screen connectors from ::: " << rcfile.fileName();
        KSharedConfigPtr newFile = KSharedConfig::openConfig(rcfile.fileName());
        m_configGroup = KConfigGroup(newFile, QStringLiteral("ScreenConnectors"));
        load();
    }
}

void ScreenPool::removeScreens(const Latte::Data::ScreensTable &obsoleteScreens)
{
    for (int i=0; i<obsoleteScreens.rowCount(); ++i) {
        if (!m_screensTable.containsId(obsoleteScreens[i].id)) {
            return;
        }

        m_screensTable.remove(obsoleteScreens[i].id);
        m_configGroup.deleteEntry(obsoleteScreens[i].id);
    }
}

int ScreenPool::primaryScreenId() const
{
    return id(qGuiApp->primaryScreen()->name());
}

void ScreenPool::save()
{
    QMap<int, QString>::const_iterator i;

    for (int i=0; i<m_screensTable.rowCount(); ++i) {
        Data::Screen screenRecord = m_screensTable[i];
        if (screenRecord.id.toInt() >= FIRSTSCREENID) {
            m_configGroup.writeEntry(screenRecord.id, screenRecord.serialize());
        }
    }

    //write to disck every 10 seconds at most
    m_configSaveTimer.start(10000);
}

void ScreenPool::insertScreenMapping(const QString &connector)
{
    //the ":" check fixes the strange plasma/qt issues when changing layouts
    //there are case that the QScreen instead of the correct screen name
    //returns "0:0", this check prevents from breaking the screens database
    //from garbage ids
    if (m_screensTable.containsName(connector) || connector.startsWith(":")) {
        return;
    }

    qDebug() << "add connector..." << connector;

    Data::Screen screenRecord;
    screenRecord.id = QString::number(firstAvailableId());
    screenRecord.name = connector;

    //! update screen geometry
    for (const auto scr : qGuiApp->screens()) {
        if (scr->name() == connector) {
            screenRecord.geometry = scr->geometry();
            break;
        }
    }

    m_screensTable << screenRecord;
    save();
}

int ScreenPool::id(const QString &connector) const
{
    QString screenId = m_screensTable.idForName(connector);
    return screenId.isEmpty() ? -1 : screenId.toInt();
}

QString ScreenPool::connector(int id) const
{   
    QString idStr = QString::number(id);
    return (m_screensTable.containsId(idStr) ? m_screensTable[idStr].name : QString());
}

int ScreenPool::firstAvailableId() const
{
    //start counting from 10, first numbers will be used for special cases e.g. primaryScreen, id=0
    int availableId = FIRSTSCREENID;

    for (int row=0; row<m_screensTable.rowCount(); ++row) {
        if (!m_screensTable.containsId(QString::number(availableId))) {
            return availableId;
        }

        availableId++;
    }

    return availableId;
}

bool ScreenPool::hasScreenId(int screenId) const
{
    return ((screenId>=0) && m_screensTable.containsId(QString::number(screenId)));
}

bool ScreenPool::isScreenActive(int screenId) const
{
    if (hasScreenId(screenId)) {
        QString scrName = connector(screenId);

        for (const auto scr : qGuiApp->screens()) {
            if (scr->name() == scrName) {
                return true;
            }
        }
    }

    return false;
}

QScreen *ScreenPool::screenForId(int id)
{
    const auto screens = qGuiApp->screens();
    QScreen *screen{qGuiApp->primaryScreen()};

    if (hasScreenId(id)) {
        QString scrName = connector(id);

        for (const auto scr : screens) {
            if (scr->name() == scrName) {
                return scr;
            }
        }
    }

    return screen;
}

bool ScreenPool::nativeEventFilter(const QByteArray &eventType, void *message, long int *result)
{
    Q_UNUSED(result);
#if HAVE_X11

    // a particular edge case: when we switch the only enabled screen
    // we don't have any signal about it, the primary screen changes but we have the same old QScreen* getting recycled
    // see https://bugs.kde.org/show_bug.cgi?id=373880
    // if this slot will be invoked many times, their//second time on will do nothing as name and primaryconnector will be the same by then
    if (eventType != "xcb_generic_event_t") {
        return false;
    }

    xcb_generic_event_t *ev = static_cast<xcb_generic_event_t *>(message);

    const auto responseType = XCB_EVENT_RESPONSE_TYPE(ev);

    const xcb_query_extension_reply_t *reply = xcb_get_extension_data(QX11Info::connection(), &xcb_randr_id);

    if (responseType == reply->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY) {
        if (qGuiApp->primaryScreen()->name() != m_lastPrimaryConnector) {
            //new screen?
            if (id(qGuiApp->primaryScreen()->name()) < 0) {
                insertScreenMapping(qGuiApp->primaryScreen()->name());
            }

            m_lastPrimaryConnector = qGuiApp->primaryScreen()->name();
            emit primaryPoolChanged();
        }
    }

#endif
    return false;
}

}

#include "moc_screenpool.cpp"