Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
environment.hpp
Go to the documentation of this file.
1#pragma once
2
24
25#include <atomic>
26#include <cstdint>
27#include <functional>
28#include <memory>
29#include <mutex>
30#include <string>
31#include <vector>
32
33namespace pulp::platform {
34
36enum class ColorScheme : uint8_t {
41};
42
54
65
69enum class MemoryPressure : uint8_t {
75};
76
81 float top = 0.0f;
82 float bottom = 0.0f;
83 float left = 0.0f;
84 float right = 0.0f;
85
86 bool is_zero() const noexcept {
87 return top == 0.0f && bottom == 0.0f && left == 0.0f && right == 0.0f;
88 }
89};
90
95 float bottom = 0.0f;
98 float animation_duration = 0.0f;
99};
100
106 float width = 0.0f;
107 float height = 0.0f;
110 float scale = 1.0f;
112 float refresh_hz = 0.0f;
114 std::string name;
115};
116
120 bool display : 1;
121 bool safe_area : 1;
122 bool keyboard : 1;
123 bool orientation : 1;
124 bool color_scheme : 1;
125 bool lifecycle : 1;
127
129 : display(false), safe_area(false), keyboard(false),
130 orientation(false), color_scheme(false), lifecycle(false),
131 memory_pressure(false) {}
132
133 bool any() const noexcept {
134 return display || safe_area || keyboard || orientation
136 }
137};
138
151
158class Environment {
159public:
160 using Listener = std::function<void(const EnvironmentState&,
162
164 class Token {
165 public:
166 Token() = default;
167 ~Token() { reset(); }
168 Token(Token&& other) noexcept;
169 Token& operator=(Token&& other) noexcept;
170 Token(const Token&) = delete;
171 Token& operator=(const Token&) = delete;
172 void reset() noexcept;
173 bool valid() const noexcept { return id_ != 0; }
174 private:
175 friend class Environment;
176 Token(uint64_t id) : id_(id) {}
177 uint64_t id_ = 0;
178 };
179
181 static Environment& instance();
182
185
189
194 void publish(const EnvironmentState& next);
195
196 // ── Test hooks ──────────────────────────────────────────────────
200
204 static void reset_for_test();
205
206private:
207 Environment() = default;
208 Environment(const Environment&) = delete;
209 Environment& operator=(const Environment&) = delete;
210
211 void unsubscribe(uint64_t id);
212
213 mutable std::mutex mu_;
214 EnvironmentState state_;
215 // Entry carries an `active` flag so `publish` can take a snapshot
216 // of the subscription list under the lock, then drop the lock and
217 // still skip any listener that was unsubscribed in the meantime —
218 // either by another listener during the same dispatch or by a
219 // different thread calling `Token::reset()`. Using shared_ptr so
220 // the copy in `publish` and the live list share the same flag.
221 struct Entry {
222 uint64_t id;
223 Listener fn;
224 std::shared_ptr<std::atomic<bool>> active;
225 };
226 std::vector<Entry> listeners_;
227 uint64_t next_id_ = 1;
228};
229
230// ── Per-platform observer bootstraps ─────────────────────────────────
231// Hosts call start_environment_observer() once during startup. Each
232// platform adapter is idempotent — duplicate calls are a no-op so
233// plugin hosts loaded multiple times don't double-register.
234//
235// Adapters land per platform; this header forward-declares them
236// alphabetically. The convenience wrapper at the bottom dispatches to
237// the right one based on the build target.
238
239#if defined(__APPLE__)
240#include <TargetConditionals.h>
241#endif
242
243#if defined(__APPLE__) && TARGET_OS_IPHONE
244void start_environment_observer_ios();
245#elif defined(__APPLE__)
247#endif
248#if defined(__linux__) && !defined(__ANDROID__)
249void start_environment_observer_linux();
250#endif
251#if defined(_WIN32)
252void start_environment_observer_win();
253#endif
254
255// Use Apple's TargetConditionals.h macro rather than the CMake-defined
256// PULP_IOS variable — the CMake iOS branch adds environment_ios.mm to
257// pulp-platform but does not define PULP_IOS for the target, so before
258// this change an iOS build would fall through to the macOS branch and
259// try to link start_environment_observer_mac() (which is not in the
260// iOS source set). TARGET_OS_IPHONE is always defined on Apple via
261// TargetConditionals.h and is 1 only for iOS/tvOS/watchOS.
263#if defined(__APPLE__) && TARGET_OS_IPHONE
264 start_environment_observer_ios();
265#elif defined(__APPLE__)
267#elif defined(__linux__) && !defined(__ANDROID__)
268 start_environment_observer_linux();
269#elif defined(_WIN32)
270 start_environment_observer_win();
271#endif
272 // Other platforms can add adapters later; until then
273 // their hosts simply see the EnvironmentState defaults.
274}
275
276} // namespace pulp::platform
RAII subscription. Drop to unsubscribe.
Definition environment.hpp:164
~Token()
Definition environment.hpp:167
Token & operator=(Token &&other) noexcept
Token(Token &&other) noexcept
Token & operator=(const Token &)=delete
bool valid() const noexcept
Definition environment.hpp:173
friend class Environment
Definition environment.hpp:175
std::function< void(const EnvironmentState &, EnvironmentChange)> Listener
Definition environment.hpp:160
static Environment & instance()
Get the process singleton.
static void inject_for_test(const EnvironmentState &state)
Replace the singleton state and notify listeners.
Token subscribe(Listener listener)
Subscribe to changes.
void publish(const EnvironmentState &next)
Push a new state.
static void reset_for_test()
Reset the singleton to defaults and remove every listener.
EnvironmentState snapshot() const
Snapshot the current state. Safe from any thread.
Device capability vocabulary: one tier ladder, one thermal ladder, and the projection from observed i...
Definition child_process.hpp:15
LifecycleState
Coarse foreground/background lifecycle state.
Definition environment.hpp:47
@ foreground
Definition environment.hpp:48
@ inactive
App is visible but not the focused window (split-screen, banner).
Definition environment.hpp:50
@ unknown
Definition environment.hpp:52
@ background
Definition environment.hpp:51
ColorScheme
Light/dark color scheme reported by the OS.
Definition environment.hpp:36
@ light
Definition environment.hpp:37
@ dark
Definition environment.hpp:38
@ unknown
Platform did not report; treat as dark per Pulp's default theme.
Definition environment.hpp:40
MemoryPressure
Memory pressure level.
Definition environment.hpp:69
@ critical
Foreground app is the next likely target; release optional state.
Definition environment.hpp:74
@ moderate
Some background processes were killed; trim soft caches.
Definition environment.hpp:72
@ normal
Definition environment.hpp:70
Orientation
Device orientation.
Definition environment.hpp:57
@ flat
Definition environment.hpp:62
@ landscape_right
Definition environment.hpp:61
@ unknown
Definition environment.hpp:63
@ portrait
Definition environment.hpp:58
@ portrait_upside_down
Definition environment.hpp:59
@ landscape_left
Definition environment.hpp:60
void start_environment_observer_mac()
void start_environment_observer()
Definition environment.hpp:262
SequencerStateChannel is the audio-safe transport for a plugin's structured NON-parameter state: the ...
Definition audio_device_manager.hpp:48
Logical display geometry.
Definition environment.hpp:105
float scale
Definition environment.hpp:110
int physical_height
Definition environment.hpp:109
int physical_width
Definition environment.hpp:108
std::string name
Optional human-readable name (e.g. "Built-in Retina Display").
Definition environment.hpp:114
float height
Definition environment.hpp:107
float refresh_hz
Refresh rate in Hz. Zero when unknown.
Definition environment.hpp:112
float width
Definition environment.hpp:106
Bitmask of fields that changed in the snapshot delivered to a listener.
Definition environment.hpp:119
bool color_scheme
Definition environment.hpp:124
EnvironmentChange() noexcept
Definition environment.hpp:128
bool orientation
Definition environment.hpp:123
bool safe_area
Definition environment.hpp:121
bool memory_pressure
Definition environment.hpp:126
bool keyboard
Definition environment.hpp:122
bool display
Definition environment.hpp:120
bool lifecycle
Definition environment.hpp:125
bool any() const noexcept
Definition environment.hpp:133
Snapshot of the full environment state.
Definition environment.hpp:142
KeyboardInset keyboard
Definition environment.hpp:145
Orientation orientation
Definition environment.hpp:146
DisplayInfo display
Definition environment.hpp:143
SafeAreaInsets safe_area
Definition environment.hpp:144
ColorScheme color_scheme
Definition environment.hpp:147
LifecycleState lifecycle
Definition environment.hpp:148
MemoryPressure memory_pressure
Definition environment.hpp:149
Soft-keyboard inset (mobile only).
Definition environment.hpp:94
float animation_duration
Animation duration the OS reported for the show/hide transition, in seconds.
Definition environment.hpp:98
float bottom
Definition environment.hpp:95
Safe-area insets in CSS-pixel space (already divided by content_scale).
Definition environment.hpp:80
bool is_zero() const noexcept
Definition environment.hpp:86
float bottom
Definition environment.hpp:82
float left
Definition environment.hpp:83
float right
Definition environment.hpp:84
float top
Definition environment.hpp:81