2026-05-10 02:18:40 +00:00
|
|
|
use std::cell::RefCell;
|
|
|
|
|
use std::rc::Rc;
|
2026-05-10 18:10:23 +00:00
|
|
|
use std::sync::mpsc;
|
2026-05-10 02:18:40 +00:00
|
|
|
|
2026-05-10 20:21:02 +00:00
|
|
|
thread_local! {
|
|
|
|
|
static ACTIVE_PIPELINES: RefCell<Vec<gstreamer::Pipeline>> = RefCell::new(Vec::new());
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 02:18:40 +00:00
|
|
|
use gtk4::prelude::*;
|
|
|
|
|
use gtk4::{self as gtk, Application, ApplicationWindow, Box as GtkBox, Grid, Label, Orientation, Picture};
|
|
|
|
|
use tracing::{info, warn};
|
|
|
|
|
|
2026-05-10 18:04:43 +00:00
|
|
|
use crate::bundle::KioskBundle;
|
2026-05-10 02:18:40 +00:00
|
|
|
use crate::pipeline;
|
|
|
|
|
use crate::server;
|
2026-05-10 20:15:58 +00:00
|
|
|
use crate::ws_client;
|
|
|
|
|
use crate::ServerMsg;
|
2026-05-10 02:18:40 +00:00
|
|
|
|
|
|
|
|
const APP_ID: &str = "dev.betterframe.kiosk";
|
|
|
|
|
|
|
|
|
|
pub fn build_app() -> Application {
|
|
|
|
|
let app = Application::builder().application_id(APP_ID).build();
|
|
|
|
|
app.connect_activate(activate);
|
|
|
|
|
app
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn activate(app: &Application) {
|
|
|
|
|
let window = ApplicationWindow::builder()
|
|
|
|
|
.application(app)
|
|
|
|
|
.title("BetterFrame")
|
|
|
|
|
.fullscreened(true)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
let provider = gtk::CssProvider::new();
|
|
|
|
|
provider.load_from_string("window { background-color: #1a1a2e; }");
|
|
|
|
|
gtk::style_context_add_provider_for_display(
|
2026-05-10 18:04:43 +00:00
|
|
|
&WidgetExt::display(&window),
|
2026-05-10 02:18:40 +00:00
|
|
|
&provider,
|
|
|
|
|
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
|
|
|
|
|
);
|
|
|
|
|
|
2026-05-10 18:09:06 +00:00
|
|
|
show_logo(&window);
|
|
|
|
|
window.present();
|
2026-05-10 02:18:40 +00:00
|
|
|
|
2026-05-10 18:10:23 +00:00
|
|
|
let (tx, rx) = mpsc::channel::<WorkerMsg>();
|
2026-05-10 02:18:40 +00:00
|
|
|
|
2026-05-10 18:11:31 +00:00
|
|
|
let server_url = std::env::var("BETTERFRAME_SERVER").ok()
|
|
|
|
|
.or_else(|| std::env::args().nth(1));
|
2026-05-10 02:18:40 +00:00
|
|
|
std::thread::spawn(move || {
|
|
|
|
|
let server = server::discover_server(server_url.as_deref());
|
|
|
|
|
info!("server: {server}");
|
|
|
|
|
|
|
|
|
|
let key = if server::is_paired() {
|
|
|
|
|
info!("already paired");
|
|
|
|
|
server::load_key()
|
|
|
|
|
} else {
|
|
|
|
|
let (code, expires) = server::initiate_pairing(&server);
|
|
|
|
|
info!("pairing code: {code} (expires {expires})");
|
2026-05-10 18:09:06 +00:00
|
|
|
let _ = tx.send(WorkerMsg::ShowPairingCode(code.clone()));
|
2026-05-10 02:18:40 +00:00
|
|
|
|
|
|
|
|
let (name, key) = server::poll_claim(&server, &code);
|
|
|
|
|
info!("paired as: {name}");
|
|
|
|
|
key
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let bundle = server::fetch_bundle(&server, &key);
|
|
|
|
|
info!("bundle: {} cameras, {} layouts", bundle.cameras.len(), bundle.layouts.len());
|
2026-05-10 18:09:06 +00:00
|
|
|
let _ = tx.send(WorkerMsg::RenderBundle(bundle));
|
2026-05-10 02:18:40 +00:00
|
|
|
|
2026-05-10 20:15:58 +00:00
|
|
|
// Spawn WS client in a separate thread for live updates
|
|
|
|
|
let server_ws = server.clone();
|
|
|
|
|
let key_ws = key.clone();
|
|
|
|
|
let (ws_tx, ws_rx) = mpsc::channel::<ServerMsg>();
|
|
|
|
|
let tx_for_reload = tx.clone();
|
|
|
|
|
let server_for_reload = server.clone();
|
|
|
|
|
let key_for_reload = key.clone();
|
|
|
|
|
|
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
|
ws_client::run(&server_ws, &key_ws, ws_tx);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Listen for WS messages and re-fetch bundle on reload
|
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
|
for msg in ws_rx {
|
|
|
|
|
match msg {
|
|
|
|
|
ServerMsg::ReloadBundle => {
|
|
|
|
|
info!("reloading bundle");
|
|
|
|
|
let bundle = server::fetch_bundle(&server_for_reload, &key_for_reload);
|
|
|
|
|
let _ = tx_for_reload.send(WorkerMsg::RenderBundle(bundle));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-10 20:39:53 +00:00
|
|
|
// Heartbeat loop — also reports display geometry
|
2026-05-10 02:18:40 +00:00
|
|
|
loop {
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(60));
|
2026-05-10 20:39:53 +00:00
|
|
|
let displays = query_displays();
|
|
|
|
|
server::heartbeat(&server, &key, &displays);
|
2026-05-10 02:18:40 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-10 18:10:23 +00:00
|
|
|
// Poll channel from UI thread via timeout
|
2026-05-10 18:09:06 +00:00
|
|
|
let window_clone = window.clone();
|
2026-05-10 18:10:23 +00:00
|
|
|
gtk::glib::timeout_add_local(std::time::Duration::from_millis(100), move || {
|
|
|
|
|
while let Ok(msg) = rx.try_recv() {
|
|
|
|
|
match msg {
|
|
|
|
|
WorkerMsg::ShowPairingCode(code) => show_pairing_code(&window_clone, &code),
|
|
|
|
|
WorkerMsg::RenderBundle(bundle) => render_bundle(&window_clone, bundle),
|
|
|
|
|
}
|
2026-05-10 18:09:06 +00:00
|
|
|
}
|
|
|
|
|
gtk::glib::ControlFlow::Continue
|
|
|
|
|
});
|
2026-05-10 02:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-10 18:09:06 +00:00
|
|
|
enum WorkerMsg {
|
|
|
|
|
ShowPairingCode(String),
|
|
|
|
|
RenderBundle(KioskBundle),
|
|
|
|
|
}
|
2026-05-10 02:18:40 +00:00
|
|
|
|
2026-05-10 20:39:53 +00:00
|
|
|
/// Query connected HDMI displays from sysfs. Returns (name, width, height).
|
|
|
|
|
/// Reads /sys/class/drm/*/status and /sys/class/drm/*/modes.
|
|
|
|
|
fn query_displays() -> Vec<(String, u32, u32)> {
|
|
|
|
|
let mut out = Vec::new();
|
|
|
|
|
let Ok(entries) = std::fs::read_dir("/sys/class/drm") else { return out };
|
|
|
|
|
for entry in entries.flatten() {
|
|
|
|
|
let name = entry.file_name().to_string_lossy().to_string();
|
|
|
|
|
// Skip non-HDMI connectors and the "card" parents
|
|
|
|
|
if !name.contains("-HDMI-") && !name.contains("-DP-") { continue; }
|
|
|
|
|
let path = entry.path();
|
|
|
|
|
let status = std::fs::read_to_string(path.join("status")).unwrap_or_default();
|
|
|
|
|
if status.trim() != "connected" { continue; }
|
|
|
|
|
let modes = std::fs::read_to_string(path.join("modes")).unwrap_or_default();
|
|
|
|
|
// First line = preferred mode
|
|
|
|
|
let mode = modes.lines().next().unwrap_or("");
|
|
|
|
|
let parts: Vec<&str> = mode.split('x').collect();
|
|
|
|
|
if parts.len() != 2 { continue; }
|
|
|
|
|
let w: u32 = parts[0].parse().unwrap_or(0);
|
|
|
|
|
let h: u32 = parts[1].trim().parse().unwrap_or(0);
|
|
|
|
|
if w == 0 || h == 0 { continue; }
|
|
|
|
|
// Strip "cardN-" prefix for cleaner name
|
|
|
|
|
let clean_name = name.split_once('-').map(|(_, rest)| rest.to_string()).unwrap_or(name);
|
|
|
|
|
out.push((clean_name, w, h));
|
|
|
|
|
}
|
|
|
|
|
out
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 18:09:06 +00:00
|
|
|
fn show_pairing_code(window: &ApplicationWindow, code: &str) {
|
2026-05-10 02:18:40 +00:00
|
|
|
let vbox = GtkBox::new(Orientation::Vertical, 20);
|
|
|
|
|
vbox.set_valign(gtk::Align::Center);
|
|
|
|
|
vbox.set_halign(gtk::Align::Center);
|
|
|
|
|
vbox.set_vexpand(true);
|
|
|
|
|
|
|
|
|
|
let title = Label::new(Some("BetterFrame"));
|
2026-05-10 18:09:06 +00:00
|
|
|
add_css(&title, ".title { font-size: 24px; color: #888; font-weight: 300; }");
|
2026-05-10 02:18:40 +00:00
|
|
|
title.add_css_class("title");
|
|
|
|
|
|
|
|
|
|
let code_label = Label::new(Some(code));
|
2026-05-10 18:09:06 +00:00
|
|
|
add_css(&code_label, ".code { font-size: 72px; color: #fff; font-weight: 700; letter-spacing: 12px; font-family: monospace; }");
|
|
|
|
|
code_label.add_css_class("code");
|
2026-05-10 02:18:40 +00:00
|
|
|
|
|
|
|
|
let hint = Label::new(Some("Enter this code in BetterFrame admin to pair"));
|
2026-05-10 18:09:06 +00:00
|
|
|
add_css(&hint, ".hint { font-size: 14px; color: #666; }");
|
2026-05-10 02:18:40 +00:00
|
|
|
hint.add_css_class("hint");
|
|
|
|
|
|
|
|
|
|
vbox.append(&title);
|
|
|
|
|
vbox.append(&code_label);
|
|
|
|
|
vbox.append(&hint);
|
2026-05-10 18:09:06 +00:00
|
|
|
window.set_child(Some(&vbox));
|
2026-05-10 02:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn render_bundle(window: &ApplicationWindow, bundle: KioskBundle) {
|
2026-05-10 20:21:02 +00:00
|
|
|
// Stop and clear any existing pipelines BEFORE building the new layout
|
|
|
|
|
ACTIVE_PIPELINES.with(|p| {
|
|
|
|
|
let mut pipes = p.borrow_mut();
|
|
|
|
|
info!("stopping {} active pipelines before re-render", pipes.len());
|
|
|
|
|
for pipe in pipes.iter() {
|
|
|
|
|
pipeline::stop(pipe);
|
|
|
|
|
}
|
|
|
|
|
pipes.clear();
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-10 02:18:40 +00:00
|
|
|
let layout = bundle.layouts.iter()
|
|
|
|
|
.find(|l| l.is_default)
|
|
|
|
|
.or_else(|| bundle.layouts.first());
|
|
|
|
|
|
|
|
|
|
let Some(layout) = layout else {
|
|
|
|
|
warn!("no layouts in bundle");
|
|
|
|
|
show_logo(window);
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-10 19:55:19 +00:00
|
|
|
if layout.cells.is_empty() {
|
|
|
|
|
warn!("layout has no cells");
|
2026-05-10 02:18:40 +00:00
|
|
|
show_logo(window);
|
|
|
|
|
return;
|
2026-05-10 19:39:09 +00:00
|
|
|
}
|
2026-05-10 02:18:40 +00:00
|
|
|
|
|
|
|
|
info!("rendering layout '{}' with {}x{} grid, {} cells",
|
2026-05-10 19:39:09 +00:00
|
|
|
layout.name, layout.grid_cols, layout.grid_rows, layout.cells.len());
|
2026-05-10 02:18:40 +00:00
|
|
|
|
|
|
|
|
let grid = Grid::new();
|
|
|
|
|
grid.set_row_homogeneous(true);
|
|
|
|
|
grid.set_column_homogeneous(true);
|
|
|
|
|
grid.set_vexpand(true);
|
|
|
|
|
grid.set_hexpand(true);
|
|
|
|
|
|
|
|
|
|
let cam_map: std::collections::HashMap<u32, &crate::bundle::BundleCamera> =
|
|
|
|
|
bundle.cameras.iter().map(|c| (c.id, c)).collect();
|
|
|
|
|
|
|
|
|
|
let pipelines: Rc<RefCell<Vec<gstreamer::Pipeline>>> = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
|
|
|
|
|
|
for cell in &layout.cells {
|
|
|
|
|
let widget: gtk::Widget = match cell.content_type.as_str() {
|
|
|
|
|
"camera" => {
|
|
|
|
|
if let Some(cam_id) = cell.camera_id {
|
|
|
|
|
if let Some(cam) = cam_map.get(&cam_id) {
|
|
|
|
|
if let Some(uri) = cam.stream_uri(cell.stream_selector.as_deref()) {
|
|
|
|
|
match pipeline::create_camera_pipeline(&cam.name, uri) {
|
|
|
|
|
Some((pipe, sink)) => {
|
|
|
|
|
let paintable = sink.property::<gtk::gdk::Paintable>("paintable");
|
|
|
|
|
let picture = Picture::for_paintable(&paintable);
|
|
|
|
|
picture.set_content_fit(gtk::ContentFit::Cover);
|
|
|
|
|
picture.set_vexpand(true);
|
|
|
|
|
picture.set_hexpand(true);
|
|
|
|
|
pipeline::play(&pipe);
|
|
|
|
|
pipelines.borrow_mut().push(pipe);
|
|
|
|
|
picture.upcast()
|
|
|
|
|
}
|
2026-05-10 18:09:06 +00:00
|
|
|
None => placeholder(&format!("{} (pipeline error)", cam.name)),
|
2026-05-10 02:18:40 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-05-10 18:09:06 +00:00
|
|
|
placeholder(&format!("{} (no stream)", cam.name))
|
2026-05-10 02:18:40 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-05-10 18:09:06 +00:00
|
|
|
placeholder("Unknown camera")
|
2026-05-10 02:18:40 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-05-10 18:09:06 +00:00
|
|
|
placeholder("No camera assigned")
|
2026-05-10 02:18:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
"html" => {
|
2026-05-10 20:39:53 +00:00
|
|
|
let html = cell.html_content.as_deref().unwrap_or("");
|
|
|
|
|
let webview = webkit6::WebView::new();
|
|
|
|
|
webkit6::prelude::WebViewExt::load_html(&webview, html, None);
|
|
|
|
|
webview.set_vexpand(true);
|
|
|
|
|
webview.set_hexpand(true);
|
|
|
|
|
webview.upcast()
|
2026-05-10 02:18:40 +00:00
|
|
|
}
|
|
|
|
|
"web" => {
|
|
|
|
|
let url = cell.web_url.as_deref().unwrap_or("about:blank");
|
2026-05-10 20:39:53 +00:00
|
|
|
let webview = webkit6::WebView::new();
|
|
|
|
|
webkit6::prelude::WebViewExt::load_uri(&webview, url);
|
|
|
|
|
webview.set_vexpand(true);
|
|
|
|
|
webview.set_hexpand(true);
|
|
|
|
|
webview.upcast()
|
2026-05-10 02:18:40 +00:00
|
|
|
}
|
2026-05-10 18:09:06 +00:00
|
|
|
_ => placeholder("Unknown content"),
|
2026-05-10 02:18:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
grid.attach(
|
|
|
|
|
&widget,
|
2026-05-10 19:55:19 +00:00
|
|
|
cell.col as i32,
|
|
|
|
|
cell.row as i32,
|
|
|
|
|
cell.col_span as i32,
|
|
|
|
|
cell.row_span as i32,
|
2026-05-10 02:18:40 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.set_child(Some(&grid));
|
|
|
|
|
|
2026-05-10 20:21:02 +00:00
|
|
|
// Move newly-created pipelines into the global registry so we can stop
|
|
|
|
|
// them on the next reload-bundle
|
|
|
|
|
ACTIVE_PIPELINES.with(|p| {
|
|
|
|
|
let mut global = p.borrow_mut();
|
|
|
|
|
for pipe in pipelines.borrow().iter() {
|
|
|
|
|
global.push(pipe.clone());
|
2026-05-10 02:18:40 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn show_logo(window: &ApplicationWindow) {
|
|
|
|
|
let label = Label::new(Some("BetterFrame"));
|
2026-05-10 18:09:06 +00:00
|
|
|
add_css(&label, "label { font-size: 48px; color: #fff; font-weight: 300; }");
|
2026-05-10 02:18:40 +00:00
|
|
|
label.set_valign(gtk::Align::Center);
|
|
|
|
|
label.set_halign(gtk::Align::Center);
|
|
|
|
|
label.set_vexpand(true);
|
|
|
|
|
window.set_child(Some(&label));
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 18:09:06 +00:00
|
|
|
fn placeholder(text: &str) -> gtk::Widget {
|
2026-05-10 02:18:40 +00:00
|
|
|
let label = Label::new(Some(text));
|
2026-05-10 18:09:06 +00:00
|
|
|
add_css(&label, "label { color: #666; font-size: 14px; background-color: #111; }");
|
2026-05-10 02:18:40 +00:00
|
|
|
label.set_vexpand(true);
|
|
|
|
|
label.set_hexpand(true);
|
2026-05-10 18:09:06 +00:00
|
|
|
label.upcast()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn add_css(widget: &impl IsA<gtk::Widget>, css: &str) {
|
2026-05-10 02:18:40 +00:00
|
|
|
let provider = gtk::CssProvider::new();
|
2026-05-10 18:09:06 +00:00
|
|
|
provider.load_from_string(css);
|
2026-05-10 02:18:40 +00:00
|
|
|
gtk::style_context_add_provider_for_display(
|
2026-05-10 18:09:06 +00:00
|
|
|
&widget.display(),
|
2026-05-10 02:18:40 +00:00
|
|
|
&provider,
|
|
|
|
|
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
|
|
|
|
|
);
|
|
|
|
|
}
|