CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
ViewStack.h
Go to the documentation of this file.
1#pragma once
2
3#include "IView.h"
4#include <cstdint>
5#include <freertos/FreeRTOS.h>
6#include <freertos/semphr.h>
7
8namespace cdc::ui {
9
18class ViewStack {
19public:
20 static constexpr uint8_t MAX_DEPTH = 20;
21
25 static ViewStack& instance();
26
32 void push(IView* view, void* context = nullptr);
33
38 void pop();
39
45 void replace(IView* view, void* context = nullptr);
46
50 void popToRoot();
51
59 void popToAnchor(IView* anchor);
60
68 void popToDepth(uint8_t targetDepth);
69
73 IView* current() const;
74
78 IView* at(uint8_t depth) const;
79
83 uint8_t depth() const { return depth_; }
84
88 bool isEmpty() const { return depth_ == 0; }
89
90 // === Input dispatch ===
91
96 void dispatchKey(char key);
97
102 void dispatchLongPress(char key);
103
108 void dispatchTick(uint32_t nowMs);
109
110 // === Rendering ===
111
118 void render(bool synchronous = false);
119
123 bool needsRender() const;
124
125 // === Modal support ===
126
133 void showModal(IView* modal);
134
138 void hideModal();
139
149 void removeModal(IView* modal);
150
154 bool hasModal() const { return modalDepth_ > 0; }
155
159 IView* getModal() const { return modalDepth_ > 0 ? modals_[modalDepth_ - 1] : nullptr; }
160
165 void forceFullRefresh() { needsFullRefresh_ = true; }
166
167 // === Exclusive lock (e.g. for FIDO2 prompts) ===
168
177 bool acquireExclusive(const void* owner);
178
184 bool releaseExclusive(const void* owner);
185
189 const void* exclusiveOwner() const { return exclusiveOwner_; }
190
191 // === Inactivity timeout ===
192
196 using InactivityCallback = void(*)();
197
203 void setInactivityTimeout(InactivityCallback callback, uint32_t timeoutMs);
204
209
214 void checkInactivity(uint32_t nowMs);
215
216private:
217 ViewStack() = default;
218
219 static constexpr uint8_t MAX_MODAL_DEPTH = 4;
220
221 IView* stack_[MAX_DEPTH] = {};
222 uint8_t depth_ = 0;
223 IView* modals_[MAX_MODAL_DEPTH] = {};
224 uint8_t modalDepth_ = 0;
225 IView* pendingPush_ = nullptr;
226 void* pendingContext_ = nullptr;
227 bool needsFullRefresh_ = true; // True after view changes
228 const void* exclusiveOwner_ = nullptr;
229 SemaphoreHandle_t mutex_ = nullptr;
230
231 void ensureMutex();
232
233 // Unlocked helpers used when the caller already holds mutex_.
234 void push_unlocked(IView* view, void* context);
235 void pop_unlocked();
236 void hideModal_unlocked();
237 void removeModal_unlocked(IView* modal);
238
239 // Inactivity timeout
240 InactivityCallback inactivityCallback_ = nullptr;
241 uint32_t inactivityTimeoutMs_ = 0;
242 uint32_t lastActivityMs_ = 0;
243};
244
245} // namespace cdc::ui
void popToDepth(uint8_t targetDepth)
Pops views until the stack depth is at most targetDepth.
void setInactivityTimeout(InactivityCallback callback, uint32_t timeoutMs)
IView * getModal() const
Definition ViewStack.h:159
bool isEmpty() const
Definition ViewStack.h:88
IView * current() const
void dispatchLongPress(char key)
void replace(IView *view, void *context=nullptr)
void dispatchKey(char key)
void dispatchTick(uint32_t nowMs)
bool needsRender() const
static ViewStack & instance()
Returns singleton view-stack instance.
Definition ViewStack.cpp:34
void removeModal(IView *modal)
Remove a specific modal from any position in the modal stack.
bool hasModal() const
Definition ViewStack.h:154
const void * exclusiveOwner() const
Returns current exclusive owner, or nullptr if none.
Definition ViewStack.h:189
void showModal(IView *modal)
bool releaseExclusive(const void *owner)
Releases exclusive ownership.
static constexpr uint8_t MAX_DEPTH
Definition ViewStack.h:20
void(*)() InactivityCallback
Definition ViewStack.h:196
bool acquireExclusive(const void *owner)
Acquires exclusive ownership of the view stack.
void checkInactivity(uint32_t nowMs)
void popToAnchor(IView *anchor)
Pops views until the specified anchor view is the current view.
void push(IView *view, void *context=nullptr)
uint8_t depth() const
Definition ViewStack.h:83
void forceFullRefresh()
Definition ViewStack.h:165
IView * at(uint8_t depth) const
void resetInactivityTimer()
Centralized key-code constants for cdc_views.
Definition IModule.h:8