Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
VirtualScreenId.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
6#include <QLatin1String>
7#include <QString>
8
9namespace PhosphorIdentity {
10
25namespace VirtualScreenId {
26
28inline constexpr QLatin1String Separator{"/vs:"};
29
31inline bool isVirtual(const QString& screenId)
32{
33 int pos = screenId.indexOf(Separator);
34 return pos > 0; // Must have non-empty physical ID before separator
35}
36
39inline QString extractPhysicalId(const QString& screenId)
40{
41 int sep = screenId.indexOf(Separator);
42 return (sep > 0) ? screenId.left(sep) : screenId;
43}
44
47inline int extractIndex(const QString& screenId)
48{
49 int sep = screenId.indexOf(Separator);
50 if (sep <= 0) {
51 return -1;
52 }
53 bool ok = false;
54 int index = screenId.mid(sep + Separator.size()).toInt(&ok);
55 return (ok && index >= 0) ? index : -1;
56}
57
60inline QString make(const QString& physicalScreenId, int index)
61{
62 if (physicalScreenId.isEmpty() || index < 0) {
63 return {};
64 }
65 return physicalScreenId + Separator + QString::number(index);
66}
67
70inline bool samePhysical(const QString& idA, const QString& idB)
71{
72 return extractPhysicalId(idA) == extractPhysicalId(idB);
73}
74
79inline bool isVirtualScreenCrossing(const QString& oldScreenId, const QString& newScreenId)
80{
81 if (oldScreenId.isEmpty() || newScreenId.isEmpty() || oldScreenId == newScreenId) {
82 return false;
83 }
84 // At least one side must be a virtual ID — two plain physical IDs that
85 // differ are a physical monitor change, not a VS crossing.
86 if (!isVirtual(oldScreenId) && !isVirtual(newScreenId)) {
87 return false;
88 }
89 return samePhysical(oldScreenId, newScreenId);
90}
91
92} // namespace VirtualScreenId
93
94} // namespace PhosphorIdentity
QString extractPhysicalId(const QString &screenId)
Extract the physical screen ID from a virtual screen ID.
Definition VirtualScreenId.h:39
bool isVirtual(const QString &screenId)
Check if a screen ID is a virtual screen ID (contains "/vs:").
Definition VirtualScreenId.h:31
int extractIndex(const QString &screenId)
Extract the virtual screen index from a virtual screen ID.
Definition VirtualScreenId.h:47
QString make(const QString &physicalScreenId, int index)
Construct a virtual screen ID from physical ID and index.
Definition VirtualScreenId.h:60
constexpr QLatin1String Separator
Separator between physical screen ID and virtual index.
Definition VirtualScreenId.h:28
bool samePhysical(const QString &idA, const QString &idB)
Check if two screen IDs share the same physical screen.
Definition VirtualScreenId.h:70
bool isVirtualScreenCrossing(const QString &oldScreenId, const QString &newScreenId)
Detect a virtual-screen crossing: the screen IDs differ, but both belong to the same physical monitor...
Definition VirtualScreenId.h:79
Definition ScreenId.h:26