feat: add top panel widgets
This commit is contained in:
1 parent
352c1307fe
commit
b8085e0e2a
14 files changed
+1016
-47
No files matched your search
@@ -12,4 +12,4 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#define MONTAUK_BUILD_NUMBER 125
|
||||
#define MONTAUK_BUILD_NUMBER 126
|
||||
@@ -40,9 +40,9 @@ struct CatalogBuiltinPage {
|
||||
|
||||
inline constexpr CatalogBuiltinPage kCatalogDesktopSettings = {
|
||||
"desktop-settings", "Desktop Settings", "0:/os/icons/preferences-desktop-apps.svg",
|
||||
"Personalization", "Wallpaper, accent color, panel and clock",
|
||||
"Personalization", "Wallpaper, accent color, panel, widgets and clock",
|
||||
"background wallpaper image accent color colour theme appearance panel "
|
||||
"shadows clock 24-hour scale"
|
||||
"shadows clock 24-hour scale top bar widgets cpu memory temperature weather"
|
||||
};
|
||||
inline constexpr CatalogBuiltinPage kCatalogUserManagement = {
|
||||
"user-management", "User Management", "0:/os/icons/user-home.svg",
|
||||
|
||||
@@ -100,6 +100,14 @@ struct DesktopSettings {
|
||||
bool clock_24h; // 24-hour clock format
|
||||
int ui_scale; // 0=Small, 1=Default, 2=Large
|
||||
int tz_offset_minutes; // timezone offset from UTC in minutes
|
||||
|
||||
// Top bar widgets (desktop/widgets.cpp): the ids pinned to the panel, in
|
||||
// left-to-right order, plus the options individual widgets read.
|
||||
static constexpr int MAX_PANEL_WIDGETS = 8;
|
||||
static constexpr int PANEL_WIDGET_ID_CAP = 16;
|
||||
char panel_widgets[MAX_PANEL_WIDGETS][PANEL_WIDGET_ID_CAP];
|
||||
int panel_widget_count;
|
||||
bool weather_fahrenheit; // weather widget unit
|
||||
};
|
||||
|
||||
// The standard folders in a home directory, in display order, with their
|
||||
|
||||
@@ -54,7 +54,7 @@ LDFLAGS := \
|
||||
|
||||
# ---- C++ source files ----
|
||||
|
||||
CORE_SRCS := main.cpp window.cpp panel.cpp messaging.cpp desktop_icons.cpp wifi.cpp battery.cpp brightness.cpp compose.cpp input.cpp dialogs.cpp launcher.cpp desktop_builtin.cpp desktop_catalog.cpp consent.cpp font_data.cpp stb_truetype_impl.cpp
|
||||
CORE_SRCS := main.cpp window.cpp panel.cpp widgets.cpp messaging.cpp desktop_icons.cpp wifi.cpp battery.cpp brightness.cpp compose.cpp input.cpp dialogs.cpp launcher.cpp desktop_builtin.cpp desktop_catalog.cpp consent.cpp font_data.cpp stb_truetype_impl.cpp
|
||||
APP_SRCS := $(sort $(shell find apps -name '*.cpp' -print))
|
||||
SRCS := $(CORE_SRCS) $(APP_SRCS)
|
||||
OBJS := $(addprefix $(OBJDIR)/,$(SRCS:.cpp=.o))
|
||||
|
||||
@@ -25,7 +25,8 @@ static void settings_on_draw(Window* win, Framebuffer& fb) {
|
||||
switch (st->active_tab) {
|
||||
case 0: settings_draw_appearance(content, st); break;
|
||||
case 1: settings_draw_display(content, st); break;
|
||||
case 2: settings_draw_about(content, st); break;
|
||||
case 2: settings_draw_widgets(content, st); break;
|
||||
case 3: settings_draw_about(content, st); break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,6 +56,9 @@ static void settings_on_mouse(Window* win, MouseEvent& ev) {
|
||||
case 1:
|
||||
settings_handle_display_click(st, mx, cy);
|
||||
break;
|
||||
case 2:
|
||||
settings_handle_widgets_click(win, st, mx, cy);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
#include "settings_internal.hpp"
|
||||
#include "../../widgets.hpp"
|
||||
|
||||
namespace settings_app {
|
||||
|
||||
@@ -41,6 +42,13 @@ void settings_persist(SettingsState* st) {
|
||||
montauk::config::set_bool(&doc, "display.clock_24h", s.clock_24h);
|
||||
montauk::config::set_bool(&doc, "display.show_shadows", s.show_shadows);
|
||||
|
||||
// Top bar widgets
|
||||
char widget_list[DesktopSettings::MAX_PANEL_WIDGETS * DesktopSettings::PANEL_WIDGET_ID_CAP];
|
||||
desktop_widgets_format_list(st->desktop, widget_list, sizeof(widget_list));
|
||||
montauk::config::set_string(&doc, "panel.widgets", widget_list);
|
||||
montauk::config::set_string(&doc, "widgets.weather_unit",
|
||||
s.weather_fahrenheit ? "f" : "c");
|
||||
|
||||
montauk::config::save_user(user, "desktop", &doc);
|
||||
doc.destroy();
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace settings_app {
|
||||
|
||||
struct SettingsState {
|
||||
DesktopState* desktop;
|
||||
int active_tab; // 0=Appearance, 1=Display, 2=About
|
||||
int active_tab; // 0=Appearance, 1=Display, 2=Widgets, 3=About
|
||||
montauk::abi::SysInfo sys_info;
|
||||
uint64_t uptime_ms;
|
||||
WallpaperFileList wp_files;
|
||||
@@ -68,8 +68,8 @@ inline const Color accent_palette[SWATCH_COUNT] = {
|
||||
};
|
||||
|
||||
inline constexpr int TAB_BAR_H = 36;
|
||||
inline constexpr int TAB_COUNT = 3;
|
||||
inline constexpr const char* tab_labels[TAB_COUNT] = { "Appearance", "Display", "About" };
|
||||
inline constexpr int TAB_COUNT = 4;
|
||||
inline constexpr const char* tab_labels[TAB_COUNT] = { "Appearance", "Display", "Widgets", "About" };
|
||||
|
||||
inline constexpr int WP_ITEM_H = 24;
|
||||
|
||||
@@ -90,6 +90,9 @@ bool settings_handle_appearance_click(Window* win, SettingsState* st, int mx, in
|
||||
void settings_draw_display(Canvas& c, SettingsState* st);
|
||||
bool settings_handle_display_click(SettingsState* st, int mx, int cy);
|
||||
|
||||
void settings_draw_widgets(Canvas& c, SettingsState* st);
|
||||
bool settings_handle_widgets_click(Window* win, SettingsState* st, int mx, int cy);
|
||||
|
||||
void settings_draw_about(Canvas& c, SettingsState* st);
|
||||
|
||||
void settings_persist(SettingsState* st);
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* widgets.cpp
|
||||
* Widgets tab for the embedded desktop settings app
|
||||
* Copyright (c) 2026 Daniel Hammer
|
||||
*/
|
||||
|
||||
// Lists every top bar widget the desktop registers (desktop/widgets.cpp).
|
||||
// Pinned ones come first, in the order they appear on the bar; the tab only
|
||||
// edits DesktopSettings, and the panel picks the change up on its next frame.
|
||||
|
||||
#include "settings_internal.hpp"
|
||||
#include "../../widgets.hpp"
|
||||
|
||||
namespace settings_app {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int ROW_H = 46;
|
||||
constexpr int MOVE_BTN_W = 52;
|
||||
constexpr int MOVE_BTN_H = 24;
|
||||
constexpr int MOVE_BTN_GAP = 6;
|
||||
constexpr int UNIT_BTN_W = 96;
|
||||
constexpr int UNIT_BTN_H = 28;
|
||||
constexpr int MAX_ROWS = 16;
|
||||
|
||||
struct WidgetRow {
|
||||
const PanelWidgetInfo* info;
|
||||
int pinned_index; // position on the bar, -1 when not pinned
|
||||
};
|
||||
|
||||
// One geometry shared by drawing and hit testing, so they cannot drift.
|
||||
struct WidgetsLayout {
|
||||
WidgetRow rows[MAX_ROWS];
|
||||
int row_count;
|
||||
int pinned_count;
|
||||
int x;
|
||||
int width;
|
||||
int list_y;
|
||||
int unit_y;
|
||||
|
||||
Rect row_rect(int i) const { return {x, list_y + i * ROW_H, width, ROW_H}; }
|
||||
Rect check_rect(int i) const {
|
||||
return {x, list_y + i * ROW_H + 4, width - 2 * MOVE_BTN_W - MOVE_BTN_GAP - 12, 20};
|
||||
}
|
||||
Rect down_rect(int i) const {
|
||||
return {x + width - MOVE_BTN_W, list_y + i * ROW_H + (ROW_H - MOVE_BTN_H) / 2 - 2,
|
||||
MOVE_BTN_W, MOVE_BTN_H};
|
||||
}
|
||||
Rect up_rect(int i) const {
|
||||
Rect d = down_rect(i);
|
||||
return {d.x - MOVE_BTN_W - MOVE_BTN_GAP, d.y, MOVE_BTN_W, MOVE_BTN_H};
|
||||
}
|
||||
Rect celsius_rect() const { return {x + 180, unit_y, UNIT_BTN_W, UNIT_BTN_H}; }
|
||||
Rect fahrenheit_rect() const {
|
||||
return {x + 180 + UNIT_BTN_W + 8, unit_y, UNIT_BTN_W, UNIT_BTN_H};
|
||||
}
|
||||
};
|
||||
|
||||
WidgetsLayout widgets_layout(const SettingsState* st, int content_w) {
|
||||
const DesktopState* ds = st->desktop;
|
||||
WidgetsLayout l;
|
||||
l.row_count = 0;
|
||||
l.x = 16;
|
||||
l.width = content_w - 2 * l.x;
|
||||
|
||||
int total = desktop_widget_count();
|
||||
// Pinned widgets first, in bar order...
|
||||
for (int p = 0; p < ds->settings.panel_widget_count && l.row_count < MAX_ROWS; p++) {
|
||||
for (int i = 0; i < total; i++) {
|
||||
const PanelWidgetInfo* info = desktop_widget_info(i);
|
||||
if (montauk::streq(info->id, ds->settings.panel_widgets[p])) {
|
||||
l.rows[l.row_count++] = {info, p};
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
l.pinned_count = l.row_count;
|
||||
// ...then the rest, in registry order.
|
||||
for (int i = 0; i < total && l.row_count < MAX_ROWS; i++) {
|
||||
const PanelWidgetInfo* info = desktop_widget_info(i);
|
||||
if (desktop_widget_pinned_index(ds, info->id) < 0)
|
||||
l.rows[l.row_count++] = {info, -1};
|
||||
}
|
||||
|
||||
int sfh = system_font_height();
|
||||
l.list_y = 12 + (sfh + 6) + (sfh + 12);
|
||||
l.unit_y = l.list_y + l.row_count * ROW_H + 8 + 14;
|
||||
return l;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void settings_draw_widgets(Canvas& c, SettingsState* st) {
|
||||
DesktopState* ds = st->desktop;
|
||||
mtk::Theme theme = settings_theme(st);
|
||||
WidgetsLayout l = widgets_layout(st, c.w);
|
||||
int sfh = system_font_height();
|
||||
|
||||
c.text(l.x, 12, "Top Bar Widgets", theme.text);
|
||||
c.text(l.x, 12 + sfh + 6, "Pinned widgets show in the top bar in this order.",
|
||||
theme.text_muted);
|
||||
|
||||
for (int i = 0; i < l.row_count; i++) {
|
||||
const WidgetRow& row = l.rows[i];
|
||||
bool pinned = row.pinned_index >= 0;
|
||||
|
||||
Rect check = l.check_rect(i);
|
||||
mtk::draw_checkbox(c, check, row.info->name, mtk::check_state(pinned), theme);
|
||||
c.text(check.x + 24, check.y + check.h + 1, row.info->description, theme.text_muted);
|
||||
|
||||
if (pinned) {
|
||||
bool can_up = row.pinned_index > 0;
|
||||
bool can_down = row.pinned_index < l.pinned_count - 1;
|
||||
mtk::draw_button(c, l.up_rect(i), "Up", mtk::BUTTON_SECONDARY,
|
||||
mtk::widget_state(false, false, can_up), theme);
|
||||
mtk::draw_button(c, l.down_rect(i), "Down", mtk::BUTTON_SECONDARY,
|
||||
mtk::widget_state(false, false, can_down), theme);
|
||||
}
|
||||
}
|
||||
|
||||
mtk::draw_separator(c, l.x, l.unit_y - 14, l.width, theme);
|
||||
|
||||
bool fahrenheit = ds->settings.weather_fahrenheit;
|
||||
c.text(l.x, l.unit_y + 6, "Weather Unit", theme.text);
|
||||
mtk::draw_button(c, l.celsius_rect(), "Celsius", mtk::BUTTON_SECONDARY,
|
||||
mtk::widget_state(!fahrenheit), theme);
|
||||
mtk::draw_button(c, l.fahrenheit_rect(), "Fahrenheit", mtk::BUTTON_SECONDARY,
|
||||
mtk::widget_state(fahrenheit), theme);
|
||||
}
|
||||
|
||||
bool settings_handle_widgets_click(Window* win, SettingsState* st, int mx, int cy) {
|
||||
DesktopState* ds = st->desktop;
|
||||
WidgetsLayout l = widgets_layout(st, win->content_w);
|
||||
|
||||
for (int i = 0; i < l.row_count; i++) {
|
||||
const WidgetRow& row = l.rows[i];
|
||||
bool pinned = row.pinned_index >= 0;
|
||||
|
||||
if (pinned && l.up_rect(i).contains(mx, cy)) {
|
||||
if (desktop_widget_move(ds, row.info->id, -1)) settings_persist(st);
|
||||
return true;
|
||||
}
|
||||
if (pinned && l.down_rect(i).contains(mx, cy)) {
|
||||
if (desktop_widget_move(ds, row.info->id, +1)) settings_persist(st);
|
||||
return true;
|
||||
}
|
||||
if (l.check_rect(i).contains(mx, cy)) {
|
||||
desktop_widget_set_pinned(ds, row.info->id, !pinned);
|
||||
settings_persist(st);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool want_f = l.fahrenheit_rect().contains(mx, cy);
|
||||
if (want_f || l.celsius_rect().contains(mx, cy)) {
|
||||
ds->settings.weather_fahrenheit = want_f;
|
||||
settings_persist(st);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace settings_app
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
#include "desktop_internal.hpp"
|
||||
#include "widgets.hpp"
|
||||
#include <montauk/user.h>
|
||||
|
||||
bool gui::desktop_refresh_keyboard_layouts(DesktopState* ds, bool force) {
|
||||
@@ -655,6 +656,14 @@ void gui::desktop_handle_mouse(DesktopState* ds) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Pinned widgets. Tested before the window pills: the panel stops
|
||||
// drawing pills where the widgets begin.
|
||||
if (desktop_widgets_click(ds, mx, my)) {
|
||||
ds->app_menu_open = false;
|
||||
gui::mtk::context_menu_close(ds->ctx_menu);
|
||||
return;
|
||||
}
|
||||
|
||||
// Window indicator buttons
|
||||
int indicator_x = 40;
|
||||
for (int i = 0; i < ds->window_count; i++) {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
#include "desktop_internal.hpp"
|
||||
#include "widgets.hpp"
|
||||
#include <montauk/config.h>
|
||||
#include <montauk/user.h>
|
||||
#include <gui/clipboard.hpp>
|
||||
@@ -241,6 +242,9 @@ void gui::desktop_init(DesktopState* ds) {
|
||||
ds->settings.clock_24h = true;
|
||||
ds->settings.ui_scale = 1;
|
||||
ds->settings.tz_offset_minutes = 60; // default CET (UTC+1)
|
||||
ds->settings.panel_widget_count = 0; // nothing pinned until the user asks
|
||||
ds->settings.weather_fahrenheit = false;
|
||||
desktop_widgets_init(ds);
|
||||
|
||||
// Load per-user desktop settings
|
||||
{
|
||||
@@ -294,6 +298,11 @@ void gui::desktop_init(DesktopState* ds) {
|
||||
ds->settings.clock_24h = doc.get_bool("display.clock_24h", true);
|
||||
ds->settings.show_shadows = doc.get_bool("display.show_shadows", true);
|
||||
|
||||
// Top bar widgets
|
||||
desktop_widgets_load_list(ds, doc.get_string("panel.widgets", ""));
|
||||
ds->settings.weather_fahrenheit =
|
||||
montauk::streq(doc.get_string("widgets.weather_unit", "c"), "f");
|
||||
|
||||
doc.destroy();
|
||||
}
|
||||
|
||||
@@ -449,6 +458,7 @@ static bool desktop_refresh_panel_state(DesktopState* ds, uint64_t now) {
|
||||
if (desktop_wifi_poll(ds, now)) changed = true;
|
||||
if (desktop_battery_poll(ds, now)) changed = true;
|
||||
if (desktop_brightness_poll(ds, now)) changed = true;
|
||||
if (desktop_widgets_poll(ds, now)) changed = true;
|
||||
|
||||
// Event-driven sync: the kernel mixer bumps a serial on every state
|
||||
// change. Reading it is one cheap syscall; refresh only when the serial
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
#include "desktop_internal.hpp"
|
||||
#include "widgets.hpp"
|
||||
#include <gui/mtk/theme.hpp>
|
||||
|
||||
void gui::desktop_draw_panel(DesktopState* ds) {
|
||||
@@ -48,40 +49,6 @@ void gui::desktop_draw_panel(DesktopState* ds) {
|
||||
}
|
||||
}
|
||||
|
||||
// Window indicator pills (center area)
|
||||
int indicator_x = 40;
|
||||
for (int i = 0; i < ds->window_count; i++) {
|
||||
Window* win = &ds->windows[i];
|
||||
if (win->state == WIN_CLOSED) continue;
|
||||
|
||||
int tw = text_width(win->title);
|
||||
int pad = 12;
|
||||
int iw = tw + pad * 2;
|
||||
if (iw > 150) iw = 150;
|
||||
|
||||
// Pre-blended indicator pill colors (opaque, blended against PANEL_BG)
|
||||
Color pill_bg = (i == ds->focused_window)
|
||||
? colors::PANEL_INDICATOR_ACTIVE
|
||||
: colors::PANEL_INDICATOR_INACTIVE;
|
||||
|
||||
fill_rounded_rect(fb, indicator_x, 4, iw, 24, 6, pill_bg);
|
||||
|
||||
// Active window accent underline bar
|
||||
if (i == ds->focused_window) {
|
||||
fb.fill_rect(indicator_x + 4, 26, iw - 8, 2, ds->settings.accent_color);
|
||||
}
|
||||
|
||||
// Truncate title if too long
|
||||
char short_title[20];
|
||||
montauk::strncpy(short_title, win->title, 18);
|
||||
|
||||
int tx = indicator_x + pad;
|
||||
int ty = 4 + (24 - system_font_height()) / 2;
|
||||
draw_text(fb, tx, ty, short_title, colors::PANEL_TEXT);
|
||||
|
||||
indicator_x += iw + 4;
|
||||
}
|
||||
|
||||
// Date + Clock (right side)
|
||||
montauk::abi::DateTime dt;
|
||||
montauk::gettime(&dt);
|
||||
@@ -248,6 +215,46 @@ void gui::desktop_draw_panel(DesktopState* ds) {
|
||||
} else {
|
||||
ds->battery_icon_rect = {0, 0, 0, 0};
|
||||
}
|
||||
|
||||
// Pinned widgets sit left of the status icons (widgets.cpp).
|
||||
int widgets_left = desktop_widgets_draw(ds, next_icon_x + 16);
|
||||
int pill_limit_x = widgets_left - 8;
|
||||
|
||||
// Window indicator pills fill the space between the app menu and
|
||||
// whatever the right side ended up needing.
|
||||
int indicator_x = 40;
|
||||
for (int i = 0; i < ds->window_count; i++) {
|
||||
Window* win = &ds->windows[i];
|
||||
if (win->state == WIN_CLOSED) continue;
|
||||
|
||||
int tw = text_width(win->title);
|
||||
int pad = 12;
|
||||
int iw = tw + pad * 2;
|
||||
if (iw > 150) iw = 150;
|
||||
if (indicator_x + iw > pill_limit_x) break;
|
||||
|
||||
// Pre-blended indicator pill colors (opaque, blended against PANEL_BG)
|
||||
Color pill_bg = (i == ds->focused_window)
|
||||
? colors::PANEL_INDICATOR_ACTIVE
|
||||
: colors::PANEL_INDICATOR_INACTIVE;
|
||||
|
||||
fill_rounded_rect(fb, indicator_x, 4, iw, 24, 6, pill_bg);
|
||||
|
||||
// Active window accent underline bar
|
||||
if (i == ds->focused_window) {
|
||||
fb.fill_rect(indicator_x + 4, 26, iw - 8, 2, ds->settings.accent_color);
|
||||
}
|
||||
|
||||
// Truncate title if too long
|
||||
char short_title[20];
|
||||
montauk::strncpy(short_title, win->title, 18);
|
||||
|
||||
int tx = indicator_x + pad;
|
||||
int ty = 4 + (24 - system_font_height()) / 2;
|
||||
draw_text(fb, tx, ty, short_title, colors::PANEL_TEXT);
|
||||
|
||||
indicator_x += iw + 4;
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
|
||||
@@ -0,0 +1,668 @@
|
||||
/*
|
||||
* widgets.cpp
|
||||
* Top bar widgets: small live readouts pinned to the panel
|
||||
* Copyright (c) 2026 Daniel Hammer
|
||||
*/
|
||||
|
||||
// A widget is one row in g_widgets: an id, what Desktop Settings calls it, and
|
||||
// four callbacks. The panel only ever talks to the registry, so adding a
|
||||
// widget means writing those callbacks and adding the row -- nothing in
|
||||
// panel.cpp, input.cpp or the settings tab changes.
|
||||
//
|
||||
// poll sample the data source; return true when the readout changed
|
||||
// measure content width in pixels, 0 to stay off the panel for now
|
||||
// draw paint the content at (x, PANEL_HEIGHT) -- the cell is `w` wide
|
||||
// click optional; a widget with one gets a hover highlight
|
||||
//
|
||||
// Which widgets are pinned, and in what order, lives in DesktopSettings and
|
||||
// is edited from the Widgets tab of Desktop Settings.
|
||||
|
||||
#include "desktop_internal.hpp"
|
||||
#include "widgets.hpp"
|
||||
#include <montauk/capabilities.h>
|
||||
|
||||
namespace {
|
||||
|
||||
struct PanelWidget {
|
||||
PanelWidgetInfo info;
|
||||
uint32_t interval_ms; // time between polls
|
||||
bool (*poll)(DesktopState* ds, uint64_t now);
|
||||
int (*measure)(DesktopState* ds);
|
||||
void (*draw)(DesktopState* ds, int x, int w);
|
||||
void (*click)(DesktopState* ds); // may be null
|
||||
uint64_t last_poll; // 0 = poll on the next tick
|
||||
Rect rect; // on-panel cell, w == 0 when hidden
|
||||
};
|
||||
|
||||
constexpr int CELL_PAD = 6; // space either side of a widget's content
|
||||
constexpr int CELL_GAP = 2; // between neighbouring cells
|
||||
constexpr int LABEL_GAP = 5; // between the parts inside one widget
|
||||
|
||||
constexpr Color DIM_TEXT = Color::from_rgb(0xA8, 0xB0, 0xBA);
|
||||
constexpr Color WARN_TEXT = Color::from_rgb(0xFF, 0x8A, 0x6A);
|
||||
|
||||
int text_y() { return (PANEL_HEIGHT - system_font_height()) / 2; }
|
||||
|
||||
// The panel colour nudged towards white: a track or hover fill that stays
|
||||
// visible on every panel colour the settings offer.
|
||||
Color panel_tint(const DesktopState* ds, int amount) {
|
||||
Color pc = ds->settings.panel_color;
|
||||
auto lift = [amount](uint8_t v) {
|
||||
int r = (int)v + amount;
|
||||
return (uint8_t)(r > 255 ? 255 : r);
|
||||
};
|
||||
return Color::from_rgb(lift(pc.r), lift(pc.g), lift(pc.b));
|
||||
}
|
||||
|
||||
// Percent readouts reserve room for "100%" so the widgets beside them do not
|
||||
// shuffle sideways every time a digit appears or disappears.
|
||||
int percent_width() { return text_width("100%"); }
|
||||
|
||||
void draw_percent(DesktopState* ds, int right_x, int value, bool valid) {
|
||||
char buf[8];
|
||||
if (valid) snprintf(buf, sizeof(buf), "%d%%", value);
|
||||
else montauk::strcpy(buf, "--%");
|
||||
draw_text(ds->fb, right_x - text_width(buf), text_y(), buf, colors::PANEL_TEXT);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// CPU usage
|
||||
// ============================================================================
|
||||
|
||||
// The kernel reports accumulated scheduler runtime per process; usage is how
|
||||
// much of the wall-clock capacity (elapsed time x cores) all of them consumed
|
||||
// between two samples. Same method as the Process Manager.
|
||||
|
||||
constexpr int CPU_MAX_PROCS = 256;
|
||||
constexpr int CPU_HISTORY = 14;
|
||||
constexpr int CPU_BAR_W = 2;
|
||||
constexpr int CPU_BAR_GAP = 1;
|
||||
constexpr int CPU_GRAPH_W = CPU_HISTORY * (CPU_BAR_W + CPU_BAR_GAP) - CPU_BAR_GAP;
|
||||
constexpr int CPU_GRAPH_H = 14;
|
||||
|
||||
struct CpuSnapshot { int pid; uint64_t cpu_ms; };
|
||||
|
||||
montauk::abi::ProcInfo g_cpu_procs[CPU_MAX_PROCS];
|
||||
CpuSnapshot g_cpu_prev[CPU_MAX_PROCS];
|
||||
int g_cpu_prev_count = 0;
|
||||
uint64_t g_cpu_prev_wall = 0;
|
||||
bool g_cpu_have_prev = false;
|
||||
bool g_cpu_valid = false;
|
||||
int g_cpu_percent = 0;
|
||||
int g_cpu_cores = 0;
|
||||
uint8_t g_cpu_history[CPU_HISTORY]; // oldest first
|
||||
|
||||
// The CPU device entry's detail reads "<model>, <n> cores".
|
||||
int cpu_core_count() {
|
||||
montauk::abi::DevInfo devs[16];
|
||||
int count = montauk::devlist(devs, 16);
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (devs[i].category != 0) continue;
|
||||
const char* p = devs[i].detail;
|
||||
while (*p && *p != ',') p++;
|
||||
while (*p && (*p < '0' || *p > '9')) p++;
|
||||
int cores = 0;
|
||||
while (*p >= '0' && *p <= '9') cores = cores * 10 + (*p++ - '0');
|
||||
if (cores > 0) return cores;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool cpu_poll(DesktopState*, uint64_t now) {
|
||||
if (g_cpu_cores <= 0) g_cpu_cores = cpu_core_count();
|
||||
|
||||
int count = montauk::proclist(g_cpu_procs, CPU_MAX_PROCS);
|
||||
if (count < 0) count = 0;
|
||||
|
||||
// A process that exited since the last sample simply drops out; one that
|
||||
// appeared has no baseline yet and is counted from the next sample on.
|
||||
uint64_t busy = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
for (int j = 0; j < g_cpu_prev_count; j++) {
|
||||
if (g_cpu_prev[j].pid != g_cpu_procs[i].pid) continue;
|
||||
if (g_cpu_procs[i].cpuTimeMs > g_cpu_prev[j].cpu_ms)
|
||||
busy += g_cpu_procs[i].cpuTimeMs - g_cpu_prev[j].cpu_ms;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int old_percent = g_cpu_percent;
|
||||
bool old_valid = g_cpu_valid;
|
||||
uint64_t elapsed = g_cpu_have_prev ? now - g_cpu_prev_wall : 0;
|
||||
if (elapsed > 0) {
|
||||
uint64_t percent = busy * 100 / (elapsed * (uint64_t)g_cpu_cores);
|
||||
g_cpu_percent = percent > 100 ? 100 : (int)percent;
|
||||
g_cpu_valid = true;
|
||||
for (int i = 1; i < CPU_HISTORY; i++) g_cpu_history[i - 1] = g_cpu_history[i];
|
||||
g_cpu_history[CPU_HISTORY - 1] = (uint8_t)g_cpu_percent;
|
||||
}
|
||||
|
||||
g_cpu_prev_count = count;
|
||||
for (int i = 0; i < count; i++)
|
||||
g_cpu_prev[i] = {g_cpu_procs[i].pid, g_cpu_procs[i].cpuTimeMs};
|
||||
g_cpu_prev_wall = now;
|
||||
g_cpu_have_prev = true;
|
||||
|
||||
// The history scrolls on every sample, so any valid sample repaints.
|
||||
return g_cpu_valid || old_valid != g_cpu_valid || old_percent != g_cpu_percent;
|
||||
}
|
||||
|
||||
int cpu_measure(DesktopState*) {
|
||||
return text_width("CPU") + LABEL_GAP + CPU_GRAPH_W + LABEL_GAP + percent_width();
|
||||
}
|
||||
|
||||
void cpu_draw(DesktopState* ds, int x, int w) {
|
||||
Framebuffer& fb = ds->fb;
|
||||
draw_text(fb, x, text_y(), "CPU", DIM_TEXT);
|
||||
|
||||
int gx = x + text_width("CPU") + LABEL_GAP;
|
||||
int gy = (PANEL_HEIGHT - CPU_GRAPH_H) / 2;
|
||||
Color track = panel_tint(ds, 0x24);
|
||||
for (int i = 0; i < CPU_HISTORY; i++) {
|
||||
int bx = gx + i * (CPU_BAR_W + CPU_BAR_GAP);
|
||||
// Every column keeps a 1px stub so an idle machine still reads as a
|
||||
// graph rather than an empty gap.
|
||||
fb.fill_rect(bx, gy, CPU_BAR_W, CPU_GRAPH_H, track);
|
||||
int h = g_cpu_history[i] * CPU_GRAPH_H / 100;
|
||||
if (h < 1) h = 1;
|
||||
fb.fill_rect(bx, gy + CPU_GRAPH_H - h, CPU_BAR_W, h, ds->settings.accent_color);
|
||||
}
|
||||
|
||||
draw_percent(ds, x + w, g_cpu_percent, g_cpu_valid);
|
||||
}
|
||||
|
||||
void open_process_manager(DesktopState* ds) {
|
||||
desktop_spawn_app(ds, "0:/apps/procmgr/procmgr.elf");
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Memory usage
|
||||
// ============================================================================
|
||||
|
||||
constexpr int MEM_BAR_W = 36;
|
||||
constexpr int MEM_BAR_H = 6;
|
||||
|
||||
bool g_mem_valid = false;
|
||||
int g_mem_percent = 0;
|
||||
|
||||
bool mem_poll(DesktopState*, uint64_t) {
|
||||
montauk::abi::MemStats ms;
|
||||
montauk::memset(&ms, 0, sizeof(ms));
|
||||
montauk::memstats(&ms);
|
||||
|
||||
bool valid = ms.totalBytes > 0;
|
||||
int percent = valid ? (int)(ms.usedBytes * 100 / ms.totalBytes) : 0;
|
||||
if (percent > 100) percent = 100;
|
||||
|
||||
bool changed = valid != g_mem_valid || percent != g_mem_percent;
|
||||
g_mem_valid = valid;
|
||||
g_mem_percent = percent;
|
||||
return changed;
|
||||
}
|
||||
|
||||
int mem_measure(DesktopState*) {
|
||||
return text_width("MEM") + LABEL_GAP + MEM_BAR_W + LABEL_GAP + percent_width();
|
||||
}
|
||||
|
||||
void mem_draw(DesktopState* ds, int x, int w) {
|
||||
Framebuffer& fb = ds->fb;
|
||||
draw_text(fb, x, text_y(), "MEM", DIM_TEXT);
|
||||
|
||||
int bx = x + text_width("MEM") + LABEL_GAP;
|
||||
int by = (PANEL_HEIGHT - MEM_BAR_H) / 2;
|
||||
fill_rounded_rect(fb, bx, by, MEM_BAR_W, MEM_BAR_H, 3, panel_tint(ds, 0x30));
|
||||
int fill = g_mem_percent * MEM_BAR_W / 100;
|
||||
if (g_mem_valid && fill >= MEM_BAR_H) {
|
||||
// Running low is worth a colour change; anything else is the accent.
|
||||
Color c = g_mem_percent >= 90 ? WARN_TEXT : ds->settings.accent_color;
|
||||
fill_rounded_rect(fb, bx, by, fill, MEM_BAR_H, 3, c);
|
||||
}
|
||||
|
||||
draw_percent(ds, x + w, g_mem_percent, g_mem_valid);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// CPU temperature
|
||||
// ============================================================================
|
||||
|
||||
// Package temperature from the kernel's power snapshot. A machine that does
|
||||
// not report one (tempC == 0, or no SYS_POWERINFO at all) shows nothing even
|
||||
// when the widget is pinned.
|
||||
|
||||
int g_temp_c = 0;
|
||||
bool g_temp_throttling = false;
|
||||
|
||||
bool temp_poll(DesktopState*, uint64_t) {
|
||||
montauk::abi::PowerInfo info;
|
||||
montauk::memset(&info, 0, sizeof(info));
|
||||
bool ok = montauk::syscall1(montauk::abi::SYS_POWERINFO, (uint64_t)&info) == 0;
|
||||
|
||||
int temp = ok ? info.tempC : 0;
|
||||
bool throttling = ok && info.throttling;
|
||||
bool changed = temp != g_temp_c || throttling != g_temp_throttling;
|
||||
g_temp_c = temp;
|
||||
g_temp_throttling = throttling;
|
||||
return changed;
|
||||
}
|
||||
|
||||
void temp_text(char* buf, int size) {
|
||||
snprintf(buf, size, "%d°C", g_temp_c);
|
||||
}
|
||||
|
||||
int temp_measure(DesktopState*) {
|
||||
if (g_temp_c <= 0) return 0;
|
||||
char buf[16];
|
||||
temp_text(buf, sizeof(buf));
|
||||
return text_width("CPU") + LABEL_GAP + text_width(buf);
|
||||
}
|
||||
|
||||
void temp_draw(DesktopState* ds, int x, int w) {
|
||||
char buf[16];
|
||||
temp_text(buf, sizeof(buf));
|
||||
draw_text(ds->fb, x, text_y(), "CPU", DIM_TEXT);
|
||||
draw_text(ds->fb, x + w - text_width(buf), text_y(), buf,
|
||||
g_temp_throttling ? WARN_TEXT : colors::PANEL_TEXT);
|
||||
}
|
||||
|
||||
void open_power_manager(DesktopState* ds) {
|
||||
desktop_spawn_app(ds, "0:/apps/powermgr/powermgr.elf");
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Weather
|
||||
// ============================================================================
|
||||
|
||||
// The fetch is an HTTPS request, far too slow and too fallible to run inside
|
||||
// the compositor. The Weather app does it for us instead: started with
|
||||
// "--widget" it opens no window, prints one tab-separated line and waits to be
|
||||
// reaped. Its output arrives through the redirected-child stream, which reads
|
||||
// without blocking, so the panel never waits on the network.
|
||||
//
|
||||
// MTKWX <TAB> 1 <TAB> tempC <TAB> feelsC <TAB> icon.svg <TAB> description <TAB> location
|
||||
// MTKWX <TAB> 0 <TAB> error message
|
||||
|
||||
constexpr const char* WEATHER_APP = "0:/apps/weather/weather.elf";
|
||||
constexpr uint64_t WEATHER_REFRESH_MS = 30ull * 60 * 1000;
|
||||
constexpr uint64_t WEATHER_RETRY_MS = 2ull * 60 * 1000;
|
||||
// Generous: the first run may sit behind a network consent prompt.
|
||||
constexpr uint64_t WEATHER_FETCH_TIMEOUT_MS = 120ull * 1000;
|
||||
constexpr uint64_t WEATHER_NO_NETWORK_RECHECK_MS = 5000;
|
||||
|
||||
struct WeatherState {
|
||||
bool valid; // temp_c/icon hold a real observation
|
||||
int temp_c;
|
||||
SvgIcon icon;
|
||||
char icon_name[64];
|
||||
|
||||
int child_pid; // > 0 while a fetch is running
|
||||
uint64_t fetch_deadline;
|
||||
uint64_t next_fetch; // 0 = as soon as the network is up
|
||||
char line[512]; // partial output line from the helper
|
||||
int line_len;
|
||||
};
|
||||
|
||||
WeatherState g_wx = {};
|
||||
|
||||
void weather_stop_fetch() {
|
||||
if (g_wx.child_pid > 0) montauk::kill(g_wx.child_pid);
|
||||
g_wx.child_pid = 0;
|
||||
g_wx.line_len = 0;
|
||||
}
|
||||
|
||||
// The name comes from another process: accept only a plain file name.
|
||||
bool weather_icon_name_ok(const char* name) {
|
||||
if (!name[0]) return false;
|
||||
for (int i = 0; name[i]; i++) {
|
||||
char ch = name[i];
|
||||
bool ok = (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')
|
||||
|| ch == '-' || ch == '.';
|
||||
if (!ok || i >= 60) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void weather_set_icon(const char* name) {
|
||||
if (!weather_icon_name_ok(name)) name = "weather-none-available.svg";
|
||||
if (g_wx.icon.pixels && montauk::streq(g_wx.icon_name, name)) return;
|
||||
|
||||
char path[96];
|
||||
snprintf(path, sizeof(path), "0:/os/icons/%s", name);
|
||||
SvgIcon loaded = svg_load(path, 16, 16, colors::PANEL_TEXT);
|
||||
if (!loaded.pixels) return; // keep whatever was showing
|
||||
if (g_wx.icon.pixels) svg_free(g_wx.icon);
|
||||
g_wx.icon = loaded;
|
||||
montauk::strncpy(g_wx.icon_name, name, sizeof(g_wx.icon_name));
|
||||
}
|
||||
|
||||
bool parse_int_field(const char* s, int* out) {
|
||||
int sign = 1, i = 0, value = 0;
|
||||
if (s[0] == '-') { sign = -1; i = 1; }
|
||||
if (s[i] < '0' || s[i] > '9') return false;
|
||||
for (; s[i] >= '0' && s[i] <= '9' && i < 8; i++) value = value * 10 + (s[i] - '0');
|
||||
if (s[i] != '\0') return false;
|
||||
*out = sign * value;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Handles one complete output line. Returns true when it was the helper's
|
||||
// answer (success or failure), i.e. the fetch is over; *changed reports
|
||||
// whether the readout moved.
|
||||
bool weather_handle_line(char* line, uint64_t now, bool* changed) {
|
||||
char* fields[8];
|
||||
int count = 0;
|
||||
fields[count++] = line;
|
||||
for (char* p = line; *p && count < 8; p++) {
|
||||
if (*p == '\t') { *p = '\0'; fields[count++] = p + 1; }
|
||||
}
|
||||
if (count < 2 || !montauk::streq(fields[0], "MTKWX")) return false;
|
||||
|
||||
int temp = 0;
|
||||
if (montauk::streq(fields[1], "1") && count >= 5 && parse_int_field(fields[2], &temp)) {
|
||||
if (!g_wx.valid || g_wx.temp_c != temp
|
||||
|| !montauk::streq(g_wx.icon_name, fields[4])) *changed = true;
|
||||
g_wx.valid = true;
|
||||
g_wx.temp_c = temp;
|
||||
weather_set_icon(fields[4]);
|
||||
g_wx.next_fetch = now + WEATHER_REFRESH_MS;
|
||||
} else {
|
||||
// Keep showing the last observation; just try again sooner.
|
||||
g_wx.next_fetch = now + WEATHER_RETRY_MS;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool weather_poll(DesktopState* ds, uint64_t now) {
|
||||
bool changed = false;
|
||||
|
||||
if (g_wx.child_pid > 0) {
|
||||
char buf[256];
|
||||
bool done = false;
|
||||
int n;
|
||||
while (!done && (n = montauk::childio_read(g_wx.child_pid, buf, sizeof(buf))) > 0) {
|
||||
for (int i = 0; i < n && !done; i++) {
|
||||
if (buf[i] == '\n') {
|
||||
g_wx.line[g_wx.line_len] = '\0';
|
||||
done = weather_handle_line(g_wx.line, now, &changed);
|
||||
g_wx.line_len = 0;
|
||||
} else if (buf[i] != '\r' && g_wx.line_len < (int)sizeof(g_wx.line) - 1) {
|
||||
g_wx.line[g_wx.line_len++] = buf[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (done) {
|
||||
weather_stop_fetch();
|
||||
} else if (n < 0) {
|
||||
// Exited without an answer. The pid is no longer ours to kill.
|
||||
g_wx.child_pid = 0;
|
||||
g_wx.line_len = 0;
|
||||
g_wx.next_fetch = now + WEATHER_RETRY_MS;
|
||||
} else if (now >= g_wx.fetch_deadline) {
|
||||
weather_stop_fetch(); // hung
|
||||
g_wx.next_fetch = now + WEATHER_RETRY_MS;
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
if (now < g_wx.next_fetch) return false;
|
||||
|
||||
// No address yet (still associating, cable unplugged): look again soon
|
||||
// rather than burning a fetch that can only fail.
|
||||
if (ds->cached_net_cfg.ipAddress == 0) {
|
||||
g_wx.next_fetch = now + WEATHER_NO_NETWORK_RECHECK_MS;
|
||||
return false;
|
||||
}
|
||||
|
||||
int pid = montauk::caps::launch(WEATHER_APP, {.args = "--widget", .redirected = true});
|
||||
if (pid > 0) {
|
||||
g_wx.child_pid = pid;
|
||||
g_wx.line_len = 0;
|
||||
g_wx.fetch_deadline = now + WEATHER_FETCH_TIMEOUT_MS;
|
||||
} else {
|
||||
// Weather is an optional component; without it the widget stays "--".
|
||||
g_wx.next_fetch = now + WEATHER_REFRESH_MS;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void weather_text(const DesktopState* ds, char* buf, int size) {
|
||||
bool f = ds->settings.weather_fahrenheit;
|
||||
if (!g_wx.valid) {
|
||||
snprintf(buf, size, "--°%s", f ? "F" : "C");
|
||||
return;
|
||||
}
|
||||
int value = g_wx.temp_c;
|
||||
if (f) {
|
||||
// Round to nearest; the +/-2 keeps negative values rounding correctly
|
||||
// under C++ truncating division.
|
||||
int scaled = value * 9;
|
||||
value = (scaled + (scaled >= 0 ? 2 : -2)) / 5 + 32;
|
||||
}
|
||||
snprintf(buf, size, "%d°%s", value, f ? "F" : "C");
|
||||
}
|
||||
|
||||
int weather_measure(DesktopState* ds) {
|
||||
char buf[16];
|
||||
weather_text(ds, buf, sizeof(buf));
|
||||
int w = text_width(buf);
|
||||
if (g_wx.valid && g_wx.icon.pixels) w += 16 + LABEL_GAP;
|
||||
return w;
|
||||
}
|
||||
|
||||
void weather_draw(DesktopState* ds, int x, int w) {
|
||||
char buf[16];
|
||||
weather_text(ds, buf, sizeof(buf));
|
||||
if (g_wx.valid && g_wx.icon.pixels) {
|
||||
ds->fb.blit_alpha(x, (PANEL_HEIGHT - 16) / 2, g_wx.icon.width, g_wx.icon.height,
|
||||
g_wx.icon.pixels);
|
||||
}
|
||||
draw_text(ds->fb, x + w - text_width(buf), text_y(), buf, colors::PANEL_TEXT);
|
||||
}
|
||||
|
||||
void weather_click(DesktopState* ds) {
|
||||
desktop_spawn_app(ds, WEATHER_APP);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Registry
|
||||
// ============================================================================
|
||||
|
||||
PanelWidget g_widgets[] = {
|
||||
{{"cpu", "CPU Usage", "Processor load with a short history graph"},
|
||||
2000, cpu_poll, cpu_measure, cpu_draw, open_process_manager, 0, {0, 0, 0, 0}},
|
||||
{{"memory", "Memory Usage", "Share of physical memory in use"},
|
||||
2000, mem_poll, mem_measure, mem_draw, open_process_manager, 0, {0, 0, 0, 0}},
|
||||
{{"temperature", "CPU Temperature", "Processor package temperature"},
|
||||
3000, temp_poll, temp_measure, temp_draw, open_power_manager, 0, {0, 0, 0, 0}},
|
||||
// Polled often because the poll is what drains the fetch helper's output;
|
||||
// the fetches themselves are 30 minutes apart.
|
||||
{{"weather", "Weather", "Current conditions for your location"},
|
||||
500, weather_poll, weather_measure, weather_draw, weather_click, 0, {0, 0, 0, 0}},
|
||||
};
|
||||
constexpr int WIDGET_COUNT = sizeof(g_widgets) / sizeof(g_widgets[0]);
|
||||
|
||||
PanelWidget* find_widget(const char* id) {
|
||||
for (int i = 0; i < WIDGET_COUNT; i++)
|
||||
if (montauk::streq(g_widgets[i].info.id, id)) return &g_widgets[i];
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// A widget that left the panel stops polling; drop what it was doing so it
|
||||
// starts clean (and promptly) if it is pinned again.
|
||||
void widget_unpinned(PanelWidget* w) {
|
||||
w->last_poll = 0;
|
||||
w->rect = {0, 0, 0, 0};
|
||||
if (w == find_widget("weather")) {
|
||||
weather_stop_fetch();
|
||||
g_wx.next_fetch = 0;
|
||||
} else if (w == find_widget("cpu")) {
|
||||
g_cpu_have_prev = false;
|
||||
g_cpu_valid = false;
|
||||
montauk::memset(g_cpu_history, 0, sizeof(g_cpu_history));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int desktop_widget_count() { return WIDGET_COUNT; }
|
||||
|
||||
const PanelWidgetInfo* desktop_widget_info(int index) {
|
||||
return index >= 0 && index < WIDGET_COUNT ? &g_widgets[index].info : nullptr;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Pinned set
|
||||
// ============================================================================
|
||||
|
||||
int desktop_widget_pinned_index(const DesktopState* ds, const char* id) {
|
||||
const DesktopSettings& s = ds->settings;
|
||||
for (int i = 0; i < s.panel_widget_count; i++)
|
||||
if (montauk::streq(s.panel_widgets[i], id)) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
void desktop_widget_set_pinned(DesktopState* ds, const char* id, bool pinned) {
|
||||
DesktopSettings& s = ds->settings;
|
||||
PanelWidget* w = find_widget(id);
|
||||
if (!w) return;
|
||||
|
||||
int at = desktop_widget_pinned_index(ds, id);
|
||||
if (pinned) {
|
||||
if (at >= 0 || s.panel_widget_count >= DesktopSettings::MAX_PANEL_WIDGETS) return;
|
||||
montauk::strncpy(s.panel_widgets[s.panel_widget_count++], id,
|
||||
DesktopSettings::PANEL_WIDGET_ID_CAP);
|
||||
w->last_poll = 0;
|
||||
} else {
|
||||
if (at < 0) return;
|
||||
for (int i = at; i < s.panel_widget_count - 1; i++)
|
||||
montauk::strncpy(s.panel_widgets[i], s.panel_widgets[i + 1],
|
||||
DesktopSettings::PANEL_WIDGET_ID_CAP);
|
||||
s.panel_widget_count--;
|
||||
widget_unpinned(w);
|
||||
}
|
||||
}
|
||||
|
||||
bool desktop_widget_move(DesktopState* ds, const char* id, int dir) {
|
||||
DesktopSettings& s = ds->settings;
|
||||
int at = desktop_widget_pinned_index(ds, id);
|
||||
int to = at + (dir < 0 ? -1 : 1);
|
||||
if (at < 0 || to < 0 || to >= s.panel_widget_count) return false;
|
||||
|
||||
char tmp[DesktopSettings::PANEL_WIDGET_ID_CAP];
|
||||
montauk::strncpy(tmp, s.panel_widgets[at], sizeof(tmp));
|
||||
montauk::strncpy(s.panel_widgets[at], s.panel_widgets[to], sizeof(tmp));
|
||||
montauk::strncpy(s.panel_widgets[to], tmp, sizeof(tmp));
|
||||
return true;
|
||||
}
|
||||
|
||||
void desktop_widgets_load_list(DesktopState* ds, const char* list) {
|
||||
ds->settings.panel_widget_count = 0;
|
||||
if (!list) return;
|
||||
|
||||
const char* p = list;
|
||||
while (*p) {
|
||||
char id[DesktopSettings::PANEL_WIDGET_ID_CAP];
|
||||
int n = 0;
|
||||
while (*p && *p != ',') {
|
||||
if (*p != ' ' && n < (int)sizeof(id) - 1) id[n++] = *p;
|
||||
p++;
|
||||
}
|
||||
id[n] = '\0';
|
||||
if (*p == ',') p++;
|
||||
if (n > 0) desktop_widget_set_pinned(ds, id, true);
|
||||
}
|
||||
}
|
||||
|
||||
void desktop_widgets_format_list(const DesktopState* ds, char* out, int out_size) {
|
||||
if (out_size <= 0) return;
|
||||
out[0] = '\0';
|
||||
for (int i = 0; i < ds->settings.panel_widget_count; i++) {
|
||||
if (i > 0) str_append(out, ",", out_size);
|
||||
str_append(out, ds->settings.panel_widgets[i], out_size);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Panel integration
|
||||
// ============================================================================
|
||||
|
||||
void desktop_widgets_init(DesktopState* ds) {
|
||||
(void)ds;
|
||||
for (int i = 0; i < WIDGET_COUNT; i++) {
|
||||
g_widgets[i].last_poll = 0;
|
||||
g_widgets[i].rect = {0, 0, 0, 0};
|
||||
}
|
||||
}
|
||||
|
||||
bool desktop_widgets_poll(DesktopState* ds, uint64_t now) {
|
||||
bool changed = false;
|
||||
for (int i = 0; i < ds->settings.panel_widget_count; i++) {
|
||||
PanelWidget* w = find_widget(ds->settings.panel_widgets[i]);
|
||||
if (!w) continue;
|
||||
if (w->last_poll != 0 && now - w->last_poll < w->interval_ms) continue;
|
||||
// The first sample after pinning always repaints: the widget goes
|
||||
// from absent to present even if its value did not "change".
|
||||
bool first = w->last_poll == 0;
|
||||
w->last_poll = now ? now : 1;
|
||||
if (w->poll(ds, now) || first) changed = true;
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
int desktop_widgets_draw(DesktopState* ds, int right_x) {
|
||||
DesktopSettings& s = ds->settings;
|
||||
|
||||
for (int i = 0; i < WIDGET_COUNT; i++) g_widgets[i].rect = {0, 0, 0, 0};
|
||||
|
||||
// Measure first: the row is laid out left-to-right but anchored on the
|
||||
// right, so its start depends on every width.
|
||||
PanelWidget* shown[DesktopSettings::MAX_PANEL_WIDGETS];
|
||||
int widths[DesktopSettings::MAX_PANEL_WIDGETS];
|
||||
int shown_count = 0;
|
||||
int total = 0;
|
||||
for (int i = 0; i < s.panel_widget_count; i++) {
|
||||
PanelWidget* w = find_widget(s.panel_widgets[i]);
|
||||
if (!w || w->last_poll == 0) continue; // no sample yet
|
||||
int content = w->measure(ds);
|
||||
if (content <= 0) continue;
|
||||
if (shown_count > 0) total += CELL_GAP;
|
||||
total += content + 2 * CELL_PAD;
|
||||
shown[shown_count] = w;
|
||||
widths[shown_count] = content;
|
||||
shown_count++;
|
||||
}
|
||||
if (shown_count == 0) return right_x;
|
||||
|
||||
// Never run under the app menu button on a very narrow screen: drop
|
||||
// widgets from the left until the rest fit.
|
||||
int first = 0;
|
||||
while (first < shown_count && right_x - total < 44) {
|
||||
total -= widths[first] + 2 * CELL_PAD + CELL_GAP;
|
||||
first++;
|
||||
}
|
||||
if (first >= shown_count) return right_x;
|
||||
|
||||
int x = right_x - total;
|
||||
int left = x;
|
||||
for (int i = first; i < shown_count; i++) {
|
||||
PanelWidget* w = shown[i];
|
||||
int cell_w = widths[i] + 2 * CELL_PAD;
|
||||
w->rect = {x, 4, cell_w, PANEL_HEIGHT - 8};
|
||||
if (w->click && w->rect.contains(ds->mouse.x, ds->mouse.y))
|
||||
fill_rounded_rect(ds->fb, w->rect.x, w->rect.y, w->rect.w, w->rect.h, 6,
|
||||
panel_tint(ds, 0x2C));
|
||||
w->draw(ds, x + CELL_PAD, widths[i]);
|
||||
x += cell_w + CELL_GAP;
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
bool desktop_widgets_click(DesktopState* ds, int mx, int my) {
|
||||
for (int i = 0; i < WIDGET_COUNT; i++) {
|
||||
PanelWidget* w = &g_widgets[i];
|
||||
if (w->rect.w <= 0 || !w->rect.contains(mx, my)) continue;
|
||||
if (w->click) w->click(ds);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* widgets.hpp
|
||||
* Top bar widgets: small live readouts pinned to the panel
|
||||
* Copyright (c) 2026 Daniel Hammer
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <gui/desktop.hpp>
|
||||
|
||||
// What Desktop Settings shows for one widget. `id` is the name stored in
|
||||
// desktop.toml (panel.widgets), so it must stay stable.
|
||||
struct PanelWidgetInfo {
|
||||
const char* id;
|
||||
const char* name;
|
||||
const char* description;
|
||||
};
|
||||
|
||||
// ---- Registry (every widget the desktop knows, pinned or not) ----
|
||||
int desktop_widget_count();
|
||||
const PanelWidgetInfo* desktop_widget_info(int index);
|
||||
|
||||
// ---- Pinned set (DesktopSettings::panel_widgets, left-to-right) ----
|
||||
int desktop_widget_pinned_index(const gui::DesktopState* ds, const char* id); // -1 = not pinned
|
||||
void desktop_widget_set_pinned(gui::DesktopState* ds, const char* id, bool pinned);
|
||||
bool desktop_widget_move(gui::DesktopState* ds, const char* id, int dir); // -1 left, +1 right
|
||||
// Parse / format the comma-separated list kept in desktop.toml. Unknown ids
|
||||
// are dropped on load, so a config written by a newer desktop still loads.
|
||||
void desktop_widgets_load_list(gui::DesktopState* ds, const char* list);
|
||||
void desktop_widgets_format_list(const gui::DesktopState* ds, char* out, int out_size);
|
||||
|
||||
// ---- Panel integration ----
|
||||
void desktop_widgets_init(gui::DesktopState* ds);
|
||||
// Samples the pinned widgets that are due. True when the panel must repaint.
|
||||
bool desktop_widgets_poll(gui::DesktopState* ds, uint64_t now);
|
||||
// Draws the pinned widgets so they end at `right_x`; returns the x of their
|
||||
// left edge (== right_x when nothing is shown).
|
||||
int desktop_widgets_draw(gui::DesktopState* ds, int right_x);
|
||||
// True when the press landed on a widget (and was handled).
|
||||
bool desktop_widgets_click(gui::DesktopState* ds, int mx, int my);
|
||||
@@ -3,6 +3,8 @@
|
||||
* MontaukOS Weather app - standalone Window Server process
|
||||
* Fetches current weather from wttr.in via HTTPS (BearSSL)
|
||||
* Displays temperature, description, feels like, and location
|
||||
* Started with "--widget" it opens no window: it fetches once and prints the
|
||||
* result for the desktop's top bar weather widget (desktop/widgets.cpp)
|
||||
* Copyright (c) 2026 Daniel Hammer
|
||||
*/
|
||||
|
||||
@@ -64,6 +66,13 @@ static int g_win_h = INIT_H;
|
||||
|
||||
static char* g_resp_buf = nullptr;
|
||||
|
||||
// Raw values of the last successful fetch, for --widget mode.
|
||||
static bool g_widget_mode = false;
|
||||
static bool g_have_temp = false;
|
||||
static int g_temp_c = 0;
|
||||
static int g_feels_c = 0;
|
||||
static const char* g_icon_file = "weather-none-available.svg";
|
||||
|
||||
// Fonts
|
||||
static TrueTypeFont* g_font = nullptr; // Roboto Medium
|
||||
static TrueTypeFont* g_font_bold = nullptr; // Roboto Bold
|
||||
@@ -262,9 +271,13 @@ static void do_fetch() {
|
||||
else
|
||||
snprintf(g_location, sizeof(g_location), "Unknown location");
|
||||
|
||||
// Load matching weather icon
|
||||
// Load matching weather icon (the top bar widget renders its own)
|
||||
int code = parse_int(code_raw);
|
||||
load_weather_icon(weather_code_to_icon(code));
|
||||
g_icon_file = weather_code_to_icon(code);
|
||||
g_have_temp = temp_raw[0] != '\0';
|
||||
g_temp_c = parse_int(temp_raw);
|
||||
g_feels_c = parse_int(feels_raw);
|
||||
if (!g_widget_mode) load_weather_icon(g_icon_file);
|
||||
|
||||
g_phase = AppPhase::DONE;
|
||||
}
|
||||
@@ -359,18 +372,54 @@ static void render(Canvas& canvas) {
|
||||
mtk::BUTTON_PRIMARY, state, theme);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Widget mode
|
||||
// ============================================================================
|
||||
|
||||
// The desktop runs "weather.elf --widget" as a redirected child so the HTTPS
|
||||
// fetch happens outside the compositor. The answer is one tab-separated line:
|
||||
//
|
||||
// MTKWX <TAB> 1 <TAB> tempC <TAB> feelsC <TAB> icon.svg <TAB> description <TAB> location
|
||||
// MTKWX <TAB> 0 <TAB> error message
|
||||
//
|
||||
// Keep it in step with weather_handle_line() in desktop/widgets.cpp.
|
||||
[[noreturn]] static void run_widget_mode() {
|
||||
g_widget_mode = true;
|
||||
do_fetch();
|
||||
|
||||
static char line[512];
|
||||
if (g_phase == AppPhase::DONE && g_have_temp) {
|
||||
snprintf(line, sizeof(line), "MTKWX\t1\t%d\t%d\t%s\t%s\t%s\n",
|
||||
g_temp_c, g_feels_c, g_icon_file, g_desc, g_location);
|
||||
} else {
|
||||
snprintf(line, sizeof(line), "MTKWX\t0\t%s\n",
|
||||
g_status[0] ? g_status : "Error: no weather data");
|
||||
}
|
||||
montauk::print(line);
|
||||
|
||||
// Output still in the stream is lost once this process is gone, so stay
|
||||
// until the desktop has read the line (it kills us when it has). The
|
||||
// timeout only matters if the desktop itself went away.
|
||||
for (int i = 0; i < 100; i++) montauk::sleep_ms(100);
|
||||
montauk::exit(0);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Entry point
|
||||
// ============================================================================
|
||||
|
||||
extern "C" void _start() {
|
||||
if (!fonts::init())
|
||||
montauk::exit(1);
|
||||
|
||||
// Allocate response buffer from heap
|
||||
g_resp_buf = (char*)malloc(RESP_MAX + 1);
|
||||
if (!g_resp_buf) montauk::exit(1);
|
||||
|
||||
char args[32] = {};
|
||||
montauk::getargs(args, sizeof(args));
|
||||
if (strcmp(args, "--widget") == 0) run_widget_mode();
|
||||
|
||||
if (!fonts::init())
|
||||
montauk::exit(1);
|
||||
|
||||
// Load fonts
|
||||
auto load_font = [](const char* path) -> TrueTypeFont* {
|
||||
TrueTypeFont* f = (TrueTypeFont*)montauk::malloc(sizeof(TrueTypeFont));
|
||||
|
||||
Reference in new issue
Block a user