Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
InMemoryConfigStore.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 "IConfigStore.h"
7#include "phosphorscreenscore_export.h"
8
9namespace Phosphor::Screens {
10
21class PHOSPHORSCREENSCORE_EXPORT InMemoryConfigStore final : public IConfigStore
22{
23 Q_OBJECT
24public:
32 static constexpr int DefaultMaxScreensPerPhysical = 8;
33
34 explicit InMemoryConfigStore(QObject* parent = nullptr)
35 : IConfigStore(parent)
36 , m_maxScreensPerPhysical(DefaultMaxScreensPerPhysical)
37 {
38 }
39
43 explicit InMemoryConfigStore(int maxScreensPerPhysical, QObject* parent = nullptr)
44 : IConfigStore(parent)
45 , m_maxScreensPerPhysical(maxScreensPerPhysical)
46 {
47 }
48
49 QHash<QString, VirtualScreenConfig> loadAll() const override
50 {
51 return m_configs;
52 }
53
54 VirtualScreenConfig get(const QString& physicalScreenId) const override
55 {
56 return m_configs.value(physicalScreenId);
57 }
58
59 bool save(const QString& physicalScreenId, const VirtualScreenConfig& config) override
60 {
61 if (config.isEmpty()) {
62 return remove(physicalScreenId);
63 }
64 // Honour the IConfigStore contract: reject what
65 // VirtualScreenConfig::isValid rejects. Mirrors SettingsConfigStore's
66 // behaviour so tests exercise the same acceptance surface real
67 // consumers see. A 0 cap means "no cap" — the default — but callers
68 // can pass a production-matching cap via the constructor to catch
69 // over-limit regressions the lightweight default would otherwise hide.
70 if (!VirtualScreenConfig::isValid(config, physicalScreenId, m_maxScreensPerPhysical)) {
71 return false;
72 }
73 const auto it = m_configs.constFind(physicalScreenId);
74 // Exact operator== (not approxEqual). In-memory storage has no JSON
75 // round-trip to round off floats, so there's nothing for the
76 // tolerance window to absorb — but approxEqual is non-transitive,
77 // and using a non-transitive predicate as a dedup key can collapse
78 // a sequence of sub-tolerance drifts into a single equivalence class
79 // the caller didn't intend. Settings-backed stores keep approxEqual
80 // because they DO serialise through JSON; this store doesn't need it.
81 if (it != m_configs.constEnd() && it.value() == config) {
82 return true; // No-op write — don't fire changed().
83 }
84 m_configs.insert(physicalScreenId, config);
85 Q_EMIT changed();
86 return true;
87 }
88
89 bool remove(const QString& physicalScreenId) override
90 {
91 if (m_configs.remove(physicalScreenId) > 0) {
92 Q_EMIT changed();
93 }
94 return true;
95 }
96
97private:
98 QHash<QString, VirtualScreenConfig> m_configs;
99 int m_maxScreensPerPhysical;
100};
101
102} // namespace Phosphor::Screens
Pluggable persistence for virtual-screen configurations.
Definition IConfigStore.h:37
Trivial IConfigStore that holds its state in process memory.
Definition InMemoryConfigStore.h:22
bool remove(const QString &physicalScreenId) override
Drop the entry for physicalScreenId.
Definition InMemoryConfigStore.h:89
InMemoryConfigStore(QObject *parent=nullptr)
Definition InMemoryConfigStore.h:34
bool save(const QString &physicalScreenId, const VirtualScreenConfig &config) override
Persist config for physicalScreenId.
Definition InMemoryConfigStore.h:59
QHash< QString, VirtualScreenConfig > loadAll() const override
Snapshot every persisted virtual-screen config, keyed by physical screen ID.
Definition InMemoryConfigStore.h:49
InMemoryConfigStore(int maxScreensPerPhysical, QObject *parent=nullptr)
Construct with an explicit maximum-virtual-screens-per-physical cap.
Definition InMemoryConfigStore.h:43
VirtualScreenConfig get(const QString &physicalScreenId) const override
Single-key read for callers that only need one entry (e.g.
Definition InMemoryConfigStore.h:54
Definition IWindowTrackingService.h:26
Configuration for how a physical screen is subdivided into virtual screens.
Definition VirtualScreen.h:124
bool isEmpty() const
Definition VirtualScreen.h:132