Phosphor
Qt6 / Wayland library suite for window-management tools
Loading...
Searching...
No Matches
VirtualDesktopManager.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
9#include "phosphorworkspaces_export.h"
10#include <QHash>
11#include <QList>
12#include <QMetaObject>
13#include <QObject>
14#include <QString>
15#include <QStringList>
16
17#include <memory>
18
19namespace PhosphorWorkspaces {
20
29class PHOSPHORWORKSPACES_EXPORT VirtualDesktopManager : public QObject, public PhosphorEngine::IVirtualDesktopManager
30{
31 Q_OBJECT
32
33public:
36 enum class CallAnswer {
41 Answered,
45 Unanswered,
46 };
47 Q_ENUM(CallAnswer)
48
49
50 explicit VirtualDesktopManager(QObject* parent = nullptr);
53 explicit VirtualDesktopManager(std::unique_ptr<IVirtualDesktopBackend> backend, QObject* parent = nullptr);
55
56 bool init();
57 void start();
58 void stop();
59
60 int currentDesktop() const override;
61 int currentDesktopForScreen(const QString& screenId) const override;
67 bool perScreenModeActive() const override;
68
74 void updateScreenDesktop(const QString& screenId, int desktop);
75
80 void removeScreenDesktop(const QString& screenId);
85 bool hasScreenDesktopReport(const QString& screenId) const;
116 void renameScreen(const QString& oldId, const QString& newId);
120 QString liveScreenIdFor(const QString& screenId) const;
121
126 void refresh();
127
131 void setCurrentDesktop(int desktop);
137 void setCurrentDesktopById(const QString& desktopId);
144 void createDesktop(uint position, const QString& name);
147 void removeDesktop(const QString& desktopId);
151 void setDesktopName(const QString& desktopId, const QString& name);
152 int desktopCount() const;
156 int desktopRows() const;
157 QStringList desktopNames() const;
161 QStringList desktopIds() const;
163 QString desktopIdAt(int desktop) const;
165 int desktopIndexOf(const QString& desktopId) const;
173 QStringList rawDesktopNames() const;
181 bool isAvailable() const;
182
187 void seedScreenDesktop(const QString& screenId, int desktop);
188
196 void setBaselineDesktopIds(const QStringList& desktopIds);
197
198Q_SIGNALS:
199 void currentDesktopChanged(int desktop);
207 void desktopCountChanged(int count);
234 void desktopRemovedAt(int removedPosition);
239 void screenDesktopChanged(const QString& screenId, int desktop);
242 void screenDesktopSeeded(const QString& screenId, int desktop);
254 void desktopsRenumbered(const PhosphorEngine::DesktopRemap& remap);
274 void desktopRenamed(const QString& desktopId, const QString& name);
278 void createDesktopAnswered(uint position, const QString& name,
287 void kwinDesktopCreated(const QString& desktopId);
289 void kwinDesktopRemoved(const QString& desktopId);
302 void desktopListChanged(const QStringList& desktopIds);
303
304protected:
311 bool commitDesktopList(QList<DesktopEntry> desktops, const QString& currentId, int rows);
314 void ingestDesktopCreated(const QString& desktopId, int position, const QString& name);
315 void ingestDesktopDataChanged(const QString& desktopId, int position, const QString& name);
316
317private Q_SLOTS:
318 // The backend's change signals (IVirtualDesktopBackend), connected while
319 // subscribed. Slots so the tests can invoke them by name.
320 void onSourceCountChanged(int count);
321 void onSourceCurrentChanged(const QString& desktopId);
322 void onSourceDesktopCreatedUnreadable(const QString& desktopId);
323 void onSourceDesktopRemoved(const QString& desktopId);
324 void onSourceRowsChanged(int rows);
325 void refreshFromSource();
326
327private:
331 void subscribeToSource();
332 void unsubscribeFromSource();
334 bool commitSnapshot(IVirtualDesktopBackend::Snapshot snapshot);
337 void clampScreenDesktopsToCount();
341 QString takeReportedScreenId(const QString& screenId);
343 void noteDesktopListChanging();
346 void retryOrAbandonRefresh();
349 bool liveListMatches(int count) const;
351 void commitLiveList();
352
353 std::unique_ptr<IVirtualDesktopBackend> m_backend;
355 QList<QMetaObject::Connection> m_sourceConnections;
356 bool m_running = false;
359 bool m_subscribed = false;
361 bool m_available = false;
362 int m_currentDesktop = 1;
366 QHash<QString, int> m_screenDesktops;
369 QHash<QString, QString> m_renamedScreenIds;
370 int m_desktopCount = 1;
371 int m_desktopRows = 1;
372 QStringList m_desktopNames;
373 QStringList m_desktopIds;
374 uint m_refreshGeneration = 0;
376 QStringList m_baselineDesktopIds;
379 bool m_desktopListChangePending = false;
382 QStringList m_rawDesktopNames;
386 QStringList m_liveIds;
388 QHash<QString, QString> m_liveRawNames;
391 QString m_pendingCurrentId;
395 bool m_refreshInFlight = false;
397 int m_refreshRetries = 0;
398};
399
400} // namespace PhosphorWorkspaces
Definition IVirtualDesktopManager.h:13
Where a VirtualDesktopManager's desktops come from: the transport to the compositor that owns them.
Definition IVirtualDesktopBackend.h:43
void removeScreenDesktop(const QString &screenId)
Drop a screen's recorded per-output desktop when the output is removed, so the map doesn't retain sta...
void screenDesktopSeeded(const QString &screenId, int desktop)
A screen's desktop was recorded as the output (re)connected (seedScreenDesktop).
void desktopsChanged()
The desktop LIST changed — a desktop was added, removed, reordered, or renamed.
void renameScreen(const QString &oldId, const QString &newId)
Carry a recorded per-output desktop across a screen-id rename.
void createDesktopAnswered(uint position, const QString &name, PhosphorWorkspaces::VirtualDesktopManager::CallAnswer answer)
KWin answered the createDesktop call made with these arguments, after any kwinDesktopCreated it cause...
void removeDesktopAnswered(const QString &desktopId, PhosphorWorkspaces::VirtualDesktopManager::CallAnswer answer)
KWin answered the removeDesktop call for desktopId, after any kwinDesktopRemoved it caused (CallAnswe...
void desktopRemovedAt(int removedPosition)
Exactly one desktop was removed, and this is the 1-based POSITION it held before it went.
bool isAvailable() const
True once init() reached the backend's desktops (for KWin's, once KWin's VirtualDesktopManager interf...
void screenDesktopChanged(const QString &screenId, int desktop)
A single screen's current virtual desktop changed (per-output virtual desktops).
QStringList desktopIds() const
KWin's desktop UUIDs, in position order and index-aligned with desktopNames().
bool commitDesktopList(QList< DesktopEntry > desktops, const QString &currentId, int rows)
Commit one whole desktop list: the core behind both of the backend's reads and the list built from it...
void createDesktop(uint position, const QString &name)
Ask KWin to create a desktop at the given 0-based global position (KWin's D-Bus signature; the caller...
void ingestDesktopCreated(const QString &desktopId, int position, const QString &name)
The backend's desktopCreated and desktopDataChanged.
VirtualDesktopManager(QObject *parent=nullptr)
No backend: unavailable, a single synthetic desktop.
void desktopListChangeSettled()
The change desktopListChanging announced has committed, or was given up on after the list could not b...
int currentDesktop() const override
The global (active-screen) current virtual desktop, 1-based.
QStringList rawDesktopNames() const
KWin's names EXACTLY as KWin reports them, aligned 1:1 with desktopIds().
CallAnswer
How the compositor answered one of the desktop methods (createDesktopAnswered, removeDesktopAnswered)...
Definition VirtualDesktopManager.h:36
void desktopCountChanged(int count)
The committed count changed.
void updateScreenDesktop(const QString &screenId, int desktop)
Record a screen's current virtual desktop (1-based).
void setCurrentDesktopById(const QString &desktopId)
Switch by KWin's desktop UUID rather than by position.
void setBaselineDesktopIds(const QStringList &desktopIds)
The desktop ids a previous session saw, in position order, so a change KWin made while nothing was li...
void desktopRenamed(const QString &desktopId, const QString &name)
A desktop present before and after a commit carries a different raw KWin name (rawDesktopNames).
bool perScreenModeActive() const override
Superseded, kept for the installed API (IVirtualDesktopManager): no production code calls it,...
int currentDesktopForScreen(const QString &screenId) const override
The current virtual desktop for a specific screen, 1-based.
QString desktopIdAt(int desktop) const
UUID at 1-based global index, or empty when out of range.
void setDesktopName(const QString &desktopId, const QString &name)
Ask KWin to rename the desktop with the given UUID string.
QString liveScreenIdFor(const QString &screenId) const
The id a per-output report naming screenId is recorded under: the new id while screenId is an aliased...
void setCurrentDesktop(int desktop)
Ask KWin to switch to the desktop at this 1-based position.
void kwinDesktopCreated(const QString &desktopId)
A KWin desktopCreated arrived carrying this UUID.
int desktopIndexOf(const QString &desktopId) const
1-based global index of the UUID, or 0 when unknown.
void kwinDesktopRemoved(const QString &desktopId)
A KWin desktopRemoved arrived carrying this UUID (same timing contract).
PhosphorWorkspaces::DesktopEntry DesktopEntry
Definition VirtualDesktopManager.h:305
void removeDesktop(const QString &desktopId)
Ask KWin to remove the desktop with the given UUID string.
void ingestDesktopDataChanged(const QString &desktopId, int position, const QString &name)
void desktopsRenumbered(const PhosphorEngine::DesktopRemap &remap)
The desktop positions moved: a removal, an insert, a reorder, or any mix of them, as one old-to-new m...
void seedScreenDesktop(const QString &screenId, int desktop)
Record the desktop an output shows as it is connected or comes back.
int desktopRows() const
Number of rows in KWin's virtual-desktop grid (>= 1).
void desktopListChanging()
KWin started a change that can move desktop positions (a create, a remove, a move),...
void desktopListChanged(const QStringList &desktopIds)
The ordered global id list settled (create, remove, reorder).
bool hasScreenDesktopReport(const QString &screenId) const
True when a per-output report exists for screenId (or, for a virtual screen id, for its parent output...
void refresh()
Re-read the whole desktop list from the backend, for a consumer that knows its picture went stale (th...
Definition DesktopRemap.h:12
Definition WindowTrackingService.h:46