CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
host_api_ui.cpp
Go to the documentation of this file.
1
10
13#include "cdc_hal/IDisplay.h"
14#include "cdc_views/ToastView.h"
15#include "cdc_views/InfoView.h"
16#include "cdc_ui/ViewStack.h"
17#include "host_str_conv.h"
18
19#include <string>
20
21namespace {
22
23cdc::ui::ToastView::Icon toIcon(uint8_t v)
24{
25 switch (v) {
31 default: return cdc::ui::ToastView::Icon::NONE;
32 }
33}
34
35} // namespace
36
37extern "C" {
38
39int host_ui_push_toast(const char* text, uint8_t icon, uint16_t duration_ms)
40{
41 if (!text) return HOST_ERR_INVALID_ARG;
42 std::string cp = cdc::plugin_manager::toDisplay(text);
43 static cdc::ui::ToastView s_pluginToast;
44 s_pluginToast.init(cp.c_str(), toIcon(icon), duration_ms, true);
47 return HOST_OK;
48}
49
50int host_ui_push_message(const char* text, uint8_t icon, uint32_t duration_ms)
51{
52 return host_ui_push_toast(text, icon, static_cast<uint16_t>(duration_ms));
53}
54
55int host_ui_push_info(const char* title, const char* body)
56{
57 if (!title || !body) return HOST_ERR_INVALID_ARG;
58 std::string cpTitle = cdc::plugin_manager::toDisplay(title);
59 std::string cpBody = cdc::plugin_manager::toDisplay(body);
60 auto* info = new cdc::ui::InfoView();
61 info->init(cpTitle.c_str(), cpBody.c_str());
63 return HOST_OK;
64}
65
66int host_ui_pop(void)
67{
68 // A shown modal (toast/confirm/context menu) owns input and sits above the
69 // view stack, so "go back" must dismiss it first; pop() applies to the view
70 // stack only when no modal is up. Otherwise a plugin's ui::pop() would leave
71 // its modal stranded and pop an unrelated view underneath.
73 if (vs.hasModal()) {
74 vs.hideModal();
75 } else {
76 vs.pop();
77 }
78 return HOST_OK;
79}
80
82{
84 // The plugin's first view sits at base + 1; collapse every overlay above it
85 // in one step. Views below the plugin (launcher, home) stay untouched.
87 return HOST_OK;
88}
89
91{
92 return HOST_OK;
93}
94
95int host_ui_wink(uint8_t count, uint16_t period_ms)
96{
97 cdc::hal::winkBacklight(count ? count : 2, period_ms ? period_ms : 150);
98 return HOST_OK;
99}
100
101} // extern "C"
Discovers, loads, runs and unloads WASM plugins on the badge.
static PluginManager & instance() noexcept
uint8_t pluginBaseDepth() const noexcept
void init(const char *message, Icon icon=Icon::NONE, uint16_t durationMs=1500, bool dismissible=true)
Initializes toast message content and timing behavior.
Definition ToastView.cpp:26
void popToDepth(uint8_t targetDepth)
Pops views until the stack depth is at most targetDepth.
void render(bool synchronous=false)
Render current view (and modal if present) and flush to display.
static ViewStack & instance()
Returns singleton view-stack instance.
Definition ViewStack.cpp:34
void showModal(IView *modal)
void push(IView *view, void *context=nullptr)
int host_ui_pop(void)
Pop the topmost view.
int host_ui_repaint(void)
Force a repaint of the current view.
int host_ui_push_message(const char *text, uint8_t icon, uint32_t duration_ms)
Show a blocking message view that auto-dismisses after duration_ms.
#define UI_ICON_ERROR
Definition host_api.h:737
int host_ui_push_info(const char *title, const char *body)
Show a scrollable info screen with title and body.
#define UI_ICON_TASK
Definition host_api.h:771
#define UI_ICON_SUCCESS
Definition host_api.h:736
#define UI_ICON_ALERT
Definition host_api.h:754
int host_ui_push_toast(const char *text, uint8_t icon, uint16_t duration_ms)
Show a transient toast overlay.
int host_ui_pop_to_plugin(void)
Pop back to the plugin's first view.
int host_ui_wink(uint8_t count, uint16_t period_ms)
Blink the backlight as a visual identification signal.
#define UI_ICON_INFO
Definition host_api.h:770
CDC Badge OS plugin host API - canonical C ABI contract.
#define HOST_OK
Definition host_api.h:37
#define HOST_ERR_INVALID_ARG
Definition host_api.h:39
Internal UTF-8 <-> CP437 helpers for the plugin host API boundary.
void winkBacklight(uint8_t count=2, uint16_t period_ms=150)
Blink the backlight as a visual "look at me" signal.
std::string toDisplay(const char *utf8)
Decode a UTF-8 (with optional HTML entities) string into CP437 bytes.