From 7976a6d40a794105ada3297b333dfde986445c2f Mon Sep 17 00:00:00 2001 From: Tamas Csikel Date: Sat, 24 Oct 2026 09:06:06 +0200 Subject: [PATCH] SmartFox: launcher, Siri Remote driver, Debian image and installer Co-Authored-By: Claude Opus 5.5 --- sf-app/src/manifest.rs | 137 +++++++++++++++++++++++++++++++++++++++++ sf-app/src/settings.rs | 57 +++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 sf-app/src/manifest.rs create mode 100644 sf-app/src/settings.rs diff --git a/sf-app/src/manifest.rs b/sf-app/src/manifest.rs new file mode 100644 index 0000000..6733d10 --- /dev/null +++ b/sf-app/src/manifest.rs @@ -0,0 +1,137 @@ +//! App manifests: one `app.toml` per folder under the apps directory. + +use std::path::{Path, PathBuf}; + +use anyhow::{Context, Result}; +use serde::{Deserialize, Serialize}; + +use crate::settings::Settings; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum AppKind { + /// Runs in the shared Chromium web runtime. + Web, + /// Any executable: AppImage, Rust binary, script. + Exec, + /// Built into the launcher (e.g. settings screen). + Builtin, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct AppManifest { + pub id: String, + pub name: String, + pub kind: AppKind, + /// Short label above the title in the hero ("Saját app", "Castolás"...). + #[serde(default)] + pub category: String, + #[serde(default)] + pub description: String, + /// Web apps: start URL. May contain `${settings.}` placeholders. + #[serde(default)] + pub url: Option, + /// Exec apps: command line. + #[serde(default)] + pub exec: Option>, + /// Built-in icon name used by the launcher. + #[serde(default)] + pub icon: String, + /// Accent color (hex), used for focus glow and buttons. + #[serde(default = "default_accent")] + pub accent: String, + /// Three colors for the animated hero backdrop: highlight, mid, deep. + #[serde(default)] + pub palette: Vec, + /// Sort order on the home row. + #[serde(default)] + pub order: i32, + #[serde(default)] + pub web: WebOptions, + #[serde(default)] + pub dial: Option, + /// Folder the manifest was loaded from (not serialized). + #[serde(skip)] + pub dir: PathBuf, +} + +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +pub struct WebOptions { + /// Full user agent override. + #[serde(default)] + pub user_agent: Option, + /// Appended to the default user agent. + #[serde(default)] + pub user_agent_suffix: Option, + /// Unpacked extensions to load (paths relative to the app folder). + #[serde(default)] + pub extensions: Vec, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DialOptions { + /// DIAL application name, e.g. "YouTube". + pub name: String, +} + +fn default_accent() -> String { + "#ffffff".into() +} + +impl AppManifest { + /// Start URL with `${settings.key}` placeholders filled in. + pub fn resolved_url(&self, settings: &Settings) -> Option { + self.url.as_ref().map(|u| settings.expand(u)) + } +} + +/// Loads every `*/app.toml` under `dir`, sorted by `order` then name. +pub fn load_apps(dir: &Path) -> Result> { + let mut apps = Vec::new(); + let entries = std::fs::read_dir(dir).with_context(|| format!("reading {}", dir.display()))?; + for entry in entries.flatten() { + let path = entry.path().join("app.toml"); + if !path.is_file() { + continue; + } + match load_one(&path) { + Ok(app) => apps.push(app), + Err(e) => log::warn!("skipping {}: {e:#}", path.display()), + } + } + apps.sort_by(|a, b| a.order.cmp(&b.order).then_with(|| a.name.cmp(&b.name))); + Ok(apps) +} + +fn load_one(path: &Path) -> Result { + let text = std::fs::read_to_string(path)?; + let mut app: AppManifest = toml::from_str(&text)?; + app.dir = path.parent().unwrap_or(Path::new(".")).to_path_buf(); + Ok(app) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parses_web_manifest() { + let app: AppManifest = toml::from_str( + r##" + id = "netflex" + name = "Netflex" + kind = "web" + url = "${settings.netflex_server}/" + accent = "#E50914" + palette = ["#ff3b3b", "#7a0a10", "#0c0203"] + [web] + user_agent_suffix = "SMART-TV SmartFox/0.1" + "##, + ) + .unwrap(); + assert_eq!(app.kind, AppKind::Web); + let mut s = Settings::default(); + s.values.insert("netflex_server".into(), "http://10.0.0.5:7880".into()); + assert_eq!(app.resolved_url(&s).unwrap(), "http://10.0.0.5:7880/"); + } +} diff --git a/sf-app/src/settings.rs b/sf-app/src/settings.rs new file mode 100644 index 0000000..486cdf4 --- /dev/null +++ b/sf-app/src/settings.rs @@ -0,0 +1,57 @@ +//! Flat key/value settings stored in `settings.toml`. + +use std::collections::BTreeMap; +use std::path::Path; + +use anyhow::Result; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +pub struct Settings { + #[serde(flatten)] + pub values: BTreeMap, +} + +impl Settings { + /// Missing file means default settings. + pub fn load(path: &Path) -> Result { + if !path.exists() { + return Ok(Self::default()); + } + Ok(toml::from_str(&std::fs::read_to_string(path)?)?) + } + + pub fn save(&self, path: &Path) -> Result<()> { + if let Some(dir) = path.parent() { + std::fs::create_dir_all(dir)?; + } + std::fs::write(path, toml::to_string_pretty(self)?)?; + Ok(()) + } + + pub fn get(&self, key: &str) -> Option<&str> { + self.values.get(key).map(String::as_str) + } + + /// Replaces `${settings.key}` with the value (or an empty string). + pub fn expand(&self, input: &str) -> String { + let mut out = String::with_capacity(input.len()); + let mut rest = input; + while let Some(start) = rest.find("${settings.") { + out.push_str(&rest[..start]); + let after = &rest[start + "${settings.".len()..]; + match after.find('}') { + Some(end) => { + out.push_str(self.get(&after[..end]).unwrap_or("")); + rest = &after[end + 1..]; + } + None => { + out.push_str(&rest[start..]); + rest = ""; + } + } + } + out.push_str(rest); + out + } +}