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_views/ImageView.h"
19#include "cdc_ui/ViewStack.h"
20#include "host_str_conv.h"
21
22#include <string>
23
24namespace {
25
26cdc::ui::ToastView::Icon toIcon(uint8_t v)
27{
28 switch (v) {
34 default: return cdc::ui::ToastView::Icon::NONE;
35 }
36}
37
38} // namespace
39
40extern "C" {
41
42int host_ui_push_toast(const char* text, uint8_t icon, uint16_t duration_ms)
43{
44 if (!text) return HOST_ERR_INVALID_ARG;
45 std::string cp = cdc::plugin_manager::toDisplay(text);
46 static cdc::ui::ToastView s_pluginToast;
47 s_pluginToast.init(cp.c_str(), toIcon(icon), duration_ms, true);
50 return HOST_OK;
51}
52
53int host_ui_push_message(const char* text, uint8_t icon, uint32_t duration_ms)
54{
55 return host_ui_push_toast(text, icon, static_cast<uint16_t>(duration_ms));
56}
57
58int host_ui_push_info(const char* title, const char* body)
59{
60 if (!title || !body) return HOST_ERR_INVALID_ARG;
61 std::string cpTitle = cdc::plugin_manager::toDisplay(title);
62 std::string cpBody = cdc::plugin_manager::toDisplay(body);
63 auto* info = new cdc::ui::InfoView();
64 info->init(cpTitle.c_str(), cpBody.c_str());
66 return HOST_OK;
67}
68
69int host_ui_view_image(const uint8_t* data, uint32_t len)
70{
71 if (!data || len == 0) return HOST_ERR_INVALID_ARG;
72 cdc::ui::showImage(nullptr, data, len);
73 return HOST_OK;
74}
75
76int host_ui_view_markdown(const uint8_t* data, uint32_t len)
77{
78 if (!data || len == 0) return HOST_ERR_INVALID_ARG;
79 std::string src(reinterpret_cast<const char*>(data), len);
80 std::string body = cdc::plugin_manager::toDisplay(src.c_str());
81 cdc::ui::showMarkdown(nullptr, body.c_str(), body.size());
82 return HOST_OK;
83}
84
85int host_browser_open(const char* url)
86{
87 if (!url || !url[0]) return HOST_ERR_INVALID_ARG;
89 if (!opener) return HOST_ERR_NOT_SUPPORTED; // browser module not present
90 opener(url); // URL stays raw UTF-8 (not display-converted)
91 return HOST_OK;
92}
93
94int host_ui_pop(void)
95{
96 // A shown modal (toast/confirm/context menu) owns input and sits above the
97 // view stack, so "go back" must dismiss it first; pop() applies to the view
98 // stack only when no modal is up. Otherwise a plugin's ui::pop() would leave
99 // its modal stranded and pop an unrelated view underneath.
100 auto& vs = cdc::ui::ViewStack::instance();
101 if (vs.hasModal()) {
102 vs.hideModal();
103 } else {
104 vs.pop();
105 }
106 return HOST_OK;
107}
108
110{
112 // The plugin's first view sits at base + 1; collapse every overlay above it
113 // in one step. Views below the plugin (launcher, home) stay untouched.
115 return HOST_OK;
116}
117
119{
120 return HOST_OK;
121}
122
123int host_ui_wink(uint8_t count, uint16_t period_ms)
124{
125 cdc::hal::winkBacklight(count ? count : 2, period_ms ? period_ms : 150);
126 return HOST_OK;
127}
128
129} // extern "C"
Optional HTML-rendering hook, decoupling HTML producers from consumers.
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:53
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.
int host_ui_view_markdown(const uint8_t *data, uint32_t len)
Render and show Markdown from an in-memory (UTF-8) buffer. No capability required; the buffer is boun...
#define UI_ICON_ERROR
Definition host_api.h:834
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:868
#define UI_ICON_SUCCESS
Definition host_api.h:833
#define UI_ICON_ALERT
Definition host_api.h:851
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_view_image(const uint8_t *data, uint32_t len)
Decode and show an image (PNG/JPEG) from an in-memory buffer, dithered. No capability required; the b...
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:867
int host_browser_open(const char *url)
Open a URL in the badge browser (enters the browser and loads it). No capability required.
CDC Badge OS plugin host API - canonical C ABI contract.
#define HOST_ERR_NOT_SUPPORTED
Definition host_api.h:45
#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.
MarkdownView * showMarkdown(const char *title, const char *src, size_t len)
Parses and shows a Markdown source on the view stack.
void(*)(const char *url) UrlOpenerFn
Open a URL in the browser (enters the browser and loads it). url is UTF-8.
UrlOpenerFn urlOpener()
Current URL opener, or nullptr when none is registered.
ImageView * showImage(const char *title, const uint8_t *data, size_t len)
Decodes and shows an image on the view stack.