fluxframe-gui 0.11.0

GTK4/libadwaita GUI client for the FluxFrame control socket.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
//! Background relm4 worker driving the UNIX-socket IPC.
//!
//! The worker holds an open [`UnixStream`] to the daemon. Each
//! [`WorkerInput::Send`] writes the command as a JSON line and reads
//! the response back; the result returns via [`WorkerOutput::Reply`].
//! The handshake (`list_effects` + `list_presets` + `current_preset` +
//! `get_config` + `config_path` + `daemon_info`) runs once on the
//! worker thread right after init (via a self-sent
//! [`WorkerInput::Connect`] — relm4 runs `Worker::init` on the
//! caller's, i.e. the GTK main, thread, so `init` itself must not
//! block) and the result is delivered as a single
//! [`WorkerOutput::Connected`] message so the AppModel does not have to
//! weave one reply tag per step.
//!
//! Failures at any IPC step transition the worker into a "disconnected"
//! state and surface as [`WorkerOutput::Disconnected`]; the AppModel
//! recreates the worker to reconnect.

use std::io::{BufRead, BufReader, Write};
use std::os::unix::net::UnixStream;
use std::path::PathBuf;
use std::time::Duration;

use fluxframe_core::protocol::{Command, DaemonInfo, OutputInfo, Response};
use relm4::Worker;
use relm4::prelude::ComponentSender;

use crate::ipc::wire::{parse_inventory, parse_presets};
use crate::state::EffectInventory;

/// Commands sent from the AppModel into the worker thread.
#[derive(Debug)]
pub enum WorkerInput {
    /// Internal: open the socket and run the handshake. Self-sent by
    /// `init` so the blocking IO happens on the worker thread instead
    /// of the GTK main thread. A repeated `Connect` while already
    /// connected is a no-op.
    Connect,
    /// Send a typed [`Command`]; the worker tags the reply so the
    /// AppModel can match it against the originating request.
    Send {
        /// Caller-supplied correlation id.
        tag: u64,
        /// The command to write on the wire.
        command: Command,
    },
}

/// Messages the worker emits back to the AppModel.
#[derive(Debug)]
pub enum WorkerOutput {
    /// Connection established and the startup queries completed.
    Connected(Box<InitialState>),
    /// Reply to a `WorkerInput::Send`. `tag` echoes the request.
    Reply {
        /// Caller-supplied correlation id.
        tag: u64,
        /// Daemon response, parsed.
        response: Response,
    },
    /// Connection failed (initial connect, handshake, or steady-state
    /// IO). `reason` is suitable for an `adw::StatusPage` body.
    Disconnected {
        /// Human-readable reason for the disconnect.
        reason: String,
    },
}

/// Snapshot of the daemon-side state captured by the handshake.
///
/// Boxed because the [`Connected`](WorkerOutput::Connected) variant
/// would otherwise dominate the enum's size — the `EffectSchema`
/// vectors inside `EffectInventory` makes the struct itself ~200 bytes; the
/// rest of `WorkerOutput` is small.
#[derive(Debug)]
pub struct InitialState {
    /// Inventory + build features.
    pub inventory: EffectInventory,
    /// All preset names.
    pub presets: Vec<String>,
    /// Currently-active preset name.
    pub active_preset: String,
    /// Active preset's full configuration, as returned by
    /// `get_config { path: None }`. Consumed by the chain editor.
    pub active_config: serde_json::Value,
    /// Writable TOML path the daemon will Save into, or `None` when
    /// the daemon was started with no resolvable config. Surfaced by
    /// the GUI's Save button tooltip and used to grey the button out
    /// when `None`.
    pub config_path: Option<PathBuf>,
    /// Where the daemon publishes video, per `daemon_info`; `None` when
    /// the daemon is too old to answer it.
    pub output: Option<OutputInfo>,
}

/// Read timeout applied to the socket so a wedged daemon does not
/// hang the GUI indefinitely. 5 s is well above any realistic
/// `set_preset` cost (which includes ONNX session reload).
const READ_TIMEOUT: Duration = Duration::from_secs(5);

/// The relm4 worker handle.
pub struct IpcWorker {
    /// Daemon socket path, consumed by [`WorkerInput::Connect`].
    socket_path: PathBuf,
    /// Daemon socket; `None` before the connect and after a disconnect.
    stream: Option<BufferedStream>,
}

/// Bundled reader+writer halves of the connected socket.
struct BufferedStream {
    reader: BufReader<UnixStream>,
    writer: UnixStream,
    /// Read timeout in effect, echoed in the timeout error message.
    read_timeout: Duration,
}

/// Failure of a single IPC round trip.
#[derive(Debug)]
enum IpcError {
    /// Socket write or read failed.
    Io(std::io::Error),
    /// The daemon did not reply within the read timeout.
    Timeout(Duration),
    /// The daemon closed the connection (EOF on read).
    Closed,
    /// The command could not be serialised.
    Serialize(serde_json::Error),
    /// The response line is not a valid [`Response`].
    Parse(serde_json::Error),
}

impl std::fmt::Display for IpcError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Io(e) => write!(f, "socket IO failed: {e}"),
            Self::Timeout(d) if d.subsec_millis() == 0 => {
                write!(f, "daemon did not reply within {} s", d.as_secs())
            }
            Self::Timeout(d) => write!(f, "daemon did not reply within {} ms", d.as_millis()),
            Self::Closed => f.write_str("daemon closed the connection"),
            Self::Serialize(e) => write!(f, "serialise command failed: {e}"),
            Self::Parse(e) => write!(f, "parse response failed: {e}"),
        }
    }
}

impl BufferedStream {
    fn connect(path: &std::path::Path) -> std::io::Result<Self> {
        let stream = UnixStream::connect(path)?;
        tracing::info!(path = %path.display(), "socket connected, starting handshake");
        Self::from_stream(stream, READ_TIMEOUT)
    }

    /// Wrap an already-connected socket, applying `read_timeout`.
    fn from_stream(stream: UnixStream, read_timeout: Duration) -> std::io::Result<Self> {
        stream.set_read_timeout(Some(read_timeout))?;
        let writer = stream.try_clone()?;
        Ok(Self {
            reader: BufReader::new(stream),
            writer,
            read_timeout,
        })
    }

    /// Send one command line, read one response line, parse it.
    #[tracing::instrument(skip(self), level = "debug")]
    fn round_trip(&mut self, cmd: &Command) -> Result<Response, IpcError> {
        tracing::debug!(?cmd, "ipc round trip start");
        let payload = serde_json::to_string(cmd).map_err(IpcError::Serialize)?;
        self.writer
            .write_all(payload.as_bytes())
            .and_then(|()| self.writer.write_all(b"\n"))
            .and_then(|()| self.writer.flush())
            .map_err(IpcError::Io)?;
        let mut line = String::new();
        let n = self
            .reader
            .read_line(&mut line)
            .map_err(|e| match e.kind() {
                // `SO_RCVTIMEO` expiry surfaces as `WouldBlock` on Linux.
                std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut => {
                    IpcError::Timeout(self.read_timeout)
                }
                _ => IpcError::Io(e),
            })?;
        if n == 0 {
            return Err(IpcError::Closed);
        }
        serde_json::from_str::<Response>(line.trim_end()).map_err(IpcError::Parse)
    }
}

impl Worker for IpcWorker {
    /// `init` receives the socket path. It performs no IO: relm4 runs
    /// `init` on the caller's (GTK main) thread, so the connect and
    /// handshake are deferred to a self-sent [`WorkerInput::Connect`]
    /// handled on the worker thread. Failures still surface as
    /// [`WorkerOutput::Disconnected`] before any `Send` is served,
    /// because `Connect` is the first message in the queue.
    type Init = PathBuf;
    type Input = WorkerInput;
    type Output = WorkerOutput;

    fn init(socket_path: Self::Init, sender: ComponentSender<Self>) -> Self {
        sender.input(WorkerInput::Connect);
        Self {
            socket_path,
            stream: None,
        }
    }

    fn update(&mut self, msg: Self::Input, sender: ComponentSender<Self>) {
        let (tag, command) = match msg {
            WorkerInput::Connect => {
                self.connect(&sender);
                return;
            }
            WorkerInput::Send { tag, command } => (tag, command),
        };
        let Some(stream) = self.stream.as_mut() else {
            let _ = sender.output(WorkerOutput::Reply {
                tag,
                response: Response::err("not connected", None),
            });
            return;
        };
        match stream.round_trip(&command) {
            Ok(response) => {
                let _ = sender.output(WorkerOutput::Reply { tag, response });
            }
            Err(err) => {
                let reason = err.to_string();
                // Tear down the dead stream so subsequent Send calls
                // surface "not connected" rather than retrying on a
                // half-broken socket.
                self.stream = None;
                let _ = sender.output(WorkerOutput::Reply {
                    tag,
                    response: Response::err(reason.clone(), None),
                });
                let _ = sender.output(WorkerOutput::Disconnected { reason });
            }
        }
    }
}

impl IpcWorker {
    /// Open the socket and run the handshake, emitting
    /// [`WorkerOutput::Connected`] or [`WorkerOutput::Disconnected`].
    /// Runs on the worker thread; no-op when already connected.
    fn connect(&mut self, sender: &ComponentSender<Self>) {
        if self.stream.is_some() {
            tracing::debug!("ipc worker already connected; ignoring Connect");
            return;
        }
        match BufferedStream::connect(&self.socket_path) {
            Ok(mut stream) => match handshake(&mut stream) {
                Ok(initial) => {
                    let _ = sender.output(WorkerOutput::Connected(Box::new(initial)));
                    self.stream = Some(stream);
                }
                Err(reason) => {
                    let _ = sender.output(WorkerOutput::Disconnected { reason });
                }
            },
            Err(e) => {
                let hint = match e.kind() {
                    std::io::ErrorKind::ConnectionRefused => {
                        "the daemon is not running or not listening on this socket"
                    }
                    std::io::ErrorKind::NotFound => {
                        "the socket file does not exist — check the daemon's [control] config"
                    }
                    std::io::ErrorKind::PermissionDenied => {
                        "permission denied — check the socket file's owner/mode"
                    }
                    _ => "connection failed",
                };
                let _ = sender.output(WorkerOutput::Disconnected {
                    reason: format!("{hint} ({e})"),
                });
            }
        }
    }
}

/// Run the startup handshake: `list_effects` → `list_presets` →
/// `current_preset` → `get_config(None)` → `config_path` (optional) →
/// `daemon_info` (optional).
///
/// Pulled out as a free function so tests drive it over a
/// `UnixStream::pair` without spinning up the relm4 runtime.
#[tracing::instrument(skip(stream), level = "info")]
fn handshake(stream: &mut BufferedStream) -> Result<InitialState, String> {
    let inventory = one(stream, &Command::ListEffects, "list_effects", |d| {
        parse_inventory(&d)
    })?;
    let presets = one(stream, &Command::ListPresets, "list_presets", |d| {
        parse_presets(&d)
    })?;
    let active_preset = one(stream, &Command::CurrentPreset, "current_preset", |d| {
        d.as_str()
            .map(str::to_string)
            .ok_or_else(|| format!("current_preset payload is not a string: {d}"))
    })?;
    let active_config = one(stream, &Command::GetConfig { path: None }, "get_config", Ok)?;
    // Optional handshake step — older daemons that do not yet expose
    // `ConfigPath` reply with `Err(unknown command)`. Surface that as
    // "no writable path" instead of failing the whole handshake, so
    // the GUI gracefully degrades to read-only mode and the operator
    // still sees the editor.
    let config_path = match stream.round_trip(&Command::ConfigPath) {
        Ok(Response::Ok { data }) => parse_config_path(&data),
        Ok(Response::Err { error, hint }) => {
            tracing::warn!(
                error,
                ?hint,
                "config_path command refused; Save will be disabled"
            );
            None
        }
        Ok(_) => None,
        Err(reason) => return Err(format!("config_path failed: {reason}")),
    };
    // Optional as well: a daemon without `daemon_info` leaves the
    // preview on its historical default device.
    let output = match stream.round_trip(&Command::DaemonInfo) {
        Ok(Response::Ok { data }) => parse_output(data),
        Ok(Response::Err { error, .. }) => {
            tracing::debug!(error, "daemon_info refused; assuming an older daemon");
            None
        }
        Ok(_) => None,
        Err(reason) => return Err(format!("daemon_info failed: {reason}")),
    };
    Ok(InitialState {
        inventory,
        presets,
        active_preset,
        active_config,
        config_path,
        output,
    })
}

/// Decode the `DaemonInfo` payload into its output; an unexpected shape
/// is treated as "not reported" with a debug-level breadcrumb.
fn parse_output(data: serde_json::Value) -> Option<OutputInfo> {
    match serde_json::from_value::<DaemonInfo>(data) {
        Ok(info) => Some(info.output),
        Err(e) => {
            tracing::debug!(error = %e, "daemon_info: unexpected payload; treating as unreported");
            None
        }
    }
}

/// Decode the `ConfigPath` payload: `{"path":"…"}` → `Some(path)`,
/// JSON `null` → `None`. Anything else is treated as `None` with a
/// debug-level breadcrumb so a daemon shape change does not silently
/// hide a real path.
fn parse_config_path(data: &serde_json::Value) -> Option<PathBuf> {
    if data.is_null() {
        return None;
    }
    if let Some(p) = data.get("path").and_then(serde_json::Value::as_str) {
        Some(PathBuf::from(p))
    } else {
        tracing::debug!(
            ?data,
            "config_path: unexpected payload shape; treating as None"
        );
        None
    }
}

/// Execute one handshake step: round-trip the command, then either
/// run `parse` on the `Ok` payload or convert the daemon's `Err`
/// response into a labelled error string.
#[tracing::instrument(skip(stream, parse))]
fn one<T>(
    stream: &mut BufferedStream,
    cmd: &Command,
    label: &str,
    parse: impl FnOnce(serde_json::Value) -> Result<T, String>,
) -> Result<T, String> {
    match stream.round_trip(cmd).map_err(|e| e.to_string())? {
        Response::Ok { data } => parse(data),
        Response::Err { error, hint } => Err(format!("{label} failed: {error}{}", fmt_hint(hint))),
        // `Response` is `#[non_exhaustive]`; surface unknown variants
        // as a hard handshake failure rather than panicking.
        _ => Err(format!("{label} failed: unrecognised Response variant")),
    }
}

fn fmt_hint(hint: Option<String>) -> String {
    hint.map_or_else(String::new, |h| format!(" (hint: {h})"))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ipc_error_display_distinguishes_timeout_from_closed() {
        assert_eq!(
            IpcError::Timeout(READ_TIMEOUT).to_string(),
            "daemon did not reply within 5 s"
        );
        assert_eq!(IpcError::Closed.to_string(), "daemon closed the connection");
    }

    #[test]
    fn round_trip_reports_closed_peer() {
        let (ours, peer) = UnixStream::pair().expect("socket pair");
        let mut stream = BufferedStream::from_stream(ours, READ_TIMEOUT).expect("wrap");
        // Read the command line, then close without replying.
        let mut req = String::new();
        let reader = std::thread::spawn(move || {
            BufReader::new(peer)
                .read_line(&mut req)
                .expect("read request");
        });
        let err = stream
            .round_trip(&Command::ListPresets)
            .expect_err("closed peer must fail");
        reader.join().expect("peer thread");
        assert!(matches!(err, IpcError::Closed), "got {err:?}");
    }

    #[test]
    fn round_trip_reports_silent_peer_as_timeout() {
        let (ours, _peer) = UnixStream::pair().expect("socket pair");
        let mut stream =
            BufferedStream::from_stream(ours, Duration::from_millis(50)).expect("wrap");
        let err = stream
            .round_trip(&Command::ListPresets)
            .expect_err("silent peer must time out");
        assert!(matches!(err, IpcError::Timeout(_)), "got {err:?}");
        assert_eq!(err.to_string(), "daemon did not reply within 50 ms");
    }

    /// Spawn a mock daemon on `peer`: for each `(expected, reply)` pair,
    /// read one request line, assert it decodes to `expected`, and
    /// write `reply` back. The peer socket is dropped afterwards.
    fn spawn_mock_daemon(
        peer: UnixStream,
        script: Vec<(Command, Response)>,
    ) -> std::thread::JoinHandle<()> {
        std::thread::spawn(move || {
            let mut writer = peer.try_clone().expect("clone peer");
            let mut reader = BufReader::new(peer);
            for (expected, reply) in script {
                let mut line = String::new();
                reader.read_line(&mut line).expect("read request");
                let got: Command = serde_json::from_str(line.trim_end()).expect("decode request");
                assert_eq!(got, expected, "handshake request order");
                let payload = serde_json::to_string(&reply).expect("encode reply");
                writer
                    .write_all(format!("{payload}\n").as_bytes())
                    .expect("write reply");
            }
        })
    }

    fn inventory_payload() -> serde_json::Value {
        serde_json::json!({
            "mask": [],
            "background": [],
            "foreground": [],
            "post": [],
            "build_features": ["ml"]
        })
    }

    /// Full handshake script with the given answers to the two optional
    /// steps.
    fn handshake_script(
        config_path_reply: Response,
        daemon_info_reply: Response,
    ) -> Vec<(Command, Response)> {
        vec![
            (Command::ListEffects, Response::ok_with(inventory_payload())),
            (
                Command::ListPresets,
                Response::ok_with(serde_json::json!(["default", "office"])),
            ),
            (
                Command::CurrentPreset,
                Response::ok_with(serde_json::json!("office")),
            ),
            (
                Command::GetConfig { path: None },
                Response::ok_with(serde_json::json!({"marker": 42})),
            ),
            (Command::ConfigPath, config_path_reply),
            (Command::DaemonInfo, daemon_info_reply),
        ]
    }

    fn v4l2_info(device: &str) -> Response {
        Response::ok_with(serde_json::json!({"output": {"kind": "v4l2", "device": device}}))
    }

    fn run_handshake(script: Vec<(Command, Response)>) -> Result<InitialState, String> {
        let (ours, peer) = UnixStream::pair().expect("socket pair");
        let mut stream = BufferedStream::from_stream(ours, READ_TIMEOUT).expect("wrap");
        let daemon = spawn_mock_daemon(peer, script);
        let result = handshake(&mut stream);
        daemon.join().expect("mock daemon thread");
        result
    }

    #[test]
    fn handshake_collects_all_replies() {
        let initial = run_handshake(handshake_script(
            Response::ok_with(serde_json::json!({"path": "/etc/fluxframe.toml"})),
            v4l2_info("/dev/video11"),
        ))
        .expect("handshake succeeds");
        assert_eq!(
            initial.output,
            Some(OutputInfo::V4l2 {
                device: PathBuf::from("/dev/video11")
            })
        );
        assert_eq!(initial.presets, vec!["default", "office"]);
        assert_eq!(initial.active_preset, "office");
        assert_eq!(initial.active_config, serde_json::json!({"marker": 42}));
        assert_eq!(initial.inventory.build_features, vec!["ml".to_string()]);
        assert_eq!(
            initial.config_path,
            Some(PathBuf::from("/etc/fluxframe.toml"))
        );
    }

    /// An old daemon refusing `config_path` must degrade to read-only
    /// mode, not fail the handshake.
    #[test]
    fn handshake_tolerates_refused_config_path() {
        let initial = run_handshake(handshake_script(
            Response::err("unknown command", None),
            v4l2_info("/dev/video10"),
        ))
        .expect("refused config_path must not fail the handshake");
        assert_eq!(initial.config_path, None);
        assert_eq!(initial.active_preset, "office");
    }

    /// A daemon predating `daemon_info` must still connect; the output
    /// is simply unreported.
    #[test]
    fn handshake_tolerates_refused_daemon_info() {
        let initial = run_handshake(handshake_script(
            Response::ok_with(serde_json::Value::Null),
            Response::err("invalid command: unknown variant `daemon_info`", None),
        ))
        .expect("refused daemon_info must not fail the handshake");
        assert_eq!(initial.output, None);
        assert_eq!(initial.active_preset, "office");
    }

    /// A refused mandatory step aborts the handshake with a labelled
    /// error; no further requests are sent.
    #[test]
    fn handshake_fails_on_refused_list_effects() {
        let Err(err) = run_handshake(vec![(
            Command::ListEffects,
            Response::err("boom", Some("restart".to_string())),
        )]) else {
            panic!("refused list_effects must fail the handshake");
        };
        assert!(err.contains("list_effects"), "got: {err}");
        assert!(err.contains("boom"), "got: {err}");
    }

    #[test]
    fn parse_config_path_decodes_daemon_shapes() {
        assert_eq!(parse_config_path(&serde_json::Value::Null), None);
        assert_eq!(
            parse_config_path(&serde_json::json!({"path": "/x"})),
            Some(PathBuf::from("/x"))
        );
        assert_eq!(parse_config_path(&serde_json::json!({})), None);
    }
}