clairlune 0.8.0

Procedural, animated ASCII-art desktop wallpapers. Local, seeded and deterministic; no AI, no network.
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
//! Applying wallpapers, on a worker thread: compose one scene per monitor (or
//! re-render saved recipes), render at each monitor's physical size, archive
//! the set, then set each still through the platform host. The static image is
//! always set first and kept current, so a crash never leaves a blank desktop.

use crate::archive::{Archive, Entry, EntryRecipes, MonitorScene, NewEntry};
use crate::settings::{Settings, ThemeMode};
use clairlune_platform::{Monitor, StillStore, WallpaperHost};
use clairlune_render::{IndexedImage, IndexedPalette, ScenePackage, encode_png};
use clairlune_scene::hash::hash_label;
use clairlune_scene::{Aspect, SceneRecipe, Theme, compose};
use std::path::{Path, PathBuf};
use std::sync::mpsc;
use std::time::Instant;

/// Work for the worker thread.
pub enum Job {
    /// A new scene per monitor from a base seed.
    New { seed: u64, settings: Box<Settings> },
    /// Saved recipes again (previous wallpaper, theme change, resolution
    /// change): same scenes, rendered for the current monitors and theme.
    Recipes {
        recipes: Box<EntryRecipes>,
        /// The folder they came from (archive entry or library item): its
        /// frozen scenes are played as saved when the monitors still match.
        source: Option<PathBuf>,
        /// A library item's own theme and Clawd choices, over the settings.
        theme: Option<Theme>,
        buddy: Option<bool>,
        settings: Box<Settings>,
        /// Archive it as a new entry (previous wallpaper) or only re-render
        /// the current one (theme change).
        archive: bool,
    },
    /// Copy the current wallpaper's primary image to the export folder.
    /// From `source` (an archive entry or library item folder), or the newest
    /// archive entry.
    Export {
        dest: PathBuf,
        source: Option<PathBuf>,
    },
    /// Export a saved scene as a looping GIF.
    ExportGif {
        dest: PathBuf,
        source: Option<PathBuf>,
        theme: Theme,
        buddy: bool,
    },
    /// Put back the wallpaper the user had before Clairlune.
    Restore,
}

/// What happened, for the tray tooltip and logs.
#[derive(Clone, Debug)]
pub enum Done {
    Applied {
        /// The folder of what's on screen (archive entry or library item).
        source: Option<PathBuf>,
        seed: u64,
        theme: Theme,
        archetype: String,
        activity: Option<String>,
        monitors: usize,
        ms: u64,
    },
    Exported(PathBuf),
    /// This system has no wallpaper host yet: scenes are made and archived,
    /// but not set. The reason is for the user.
    NoHost(String),
    /// The user's own wallpaper is back.
    Restored,
    Failed(String),
}

/// The theme a setting resolves to right now.
pub fn resolve_theme(mode: ThemeMode) -> Theme {
    match mode {
        ThemeMode::Light => Theme::Light,
        ThemeMode::Dark => Theme::Dark,
        ThemeMode::FollowSystem => match clairlune_platform::system_prefers_dark() {
            Some(false) => Theme::Light,
            _ => Theme::Dark,
        },
    }
}

/// A monitor's seed: the base seed on the primary, derived from the base seed
/// and the monitor id elsewhere (PLAN.md section 2.1).
pub fn monitor_seed(base: u64, m: &Monitor) -> u64 {
    if m.primary {
        base
    } else {
        hash_label(base, &m.id)
    }
}

pub(crate) fn png_of(img: &IndexedImage, palette: &IndexedPalette) -> Result<Vec<u8>, String> {
    let mut png = Vec::new();
    encode_png(img, palette, &mut png).map_err(|e| e.to_string())?;
    Ok(png)
}

/// Nearest-neighbour shrink to `width` (thumbnails keep the palette).
pub(crate) fn shrink(img: &IndexedImage, width: u32) -> IndexedImage {
    let width = width.min(img.width).max(1);
    let height = ((u64::from(img.height) * u64::from(width)) / u64::from(img.width)).max(1) as u32;
    let mut pixels = Vec::with_capacity((width * height) as usize);
    for y in 0..height {
        let sy = (u64::from(y) * u64::from(img.height) / u64::from(height)) as usize;
        for x in 0..width {
            let sx = (u64::from(x) * u64::from(img.width) / u64::from(width)) as usize;
            pixels.push(img.pixels[sy * img.width as usize + sx]);
        }
    }
    IndexedImage {
        width,
        height,
        pixels,
    }
}

/// One frozen scene per monitor. A saved entry's scenes are replayed as they
/// were saved when the monitor id and size still match (no generator
/// involved); otherwise the recipe is rendered again, and new wallpapers are
/// composed from the seed. Returns the base seed, the scenes, and how many
/// were replayed.
fn packages(
    saved: Option<Source>,
    seed: u64,
    monitors: &[Monitor],
    settings: &Settings,
) -> Result<(u64, Vec<ScenePackage>, usize), String> {
    let (base_seed, recipes) = scenes(saved.map(|s| s.recipes), seed, monitors, settings);
    let mut out = Vec::new();
    let mut replayed = 0;
    for (m, recipe) in monitors.iter().zip(&recipes) {
        let frozen = saved.and_then(|src| {
            let dir = src.dir?;
            let j = src
                .recipes
                .monitors
                .iter()
                .position(|s| s.monitor == m.id && (s.width, s.height) == (m.width, m.height))?;
            match ScenePackage::load(&dir.join("scene").join(j.to_string())) {
                Ok(p) => Some(p),
                Err(e) => {
                    tracing::warn!(target: "clairlune", dir = %dir.display(), error = %e, "saved scene unreadable; rendering the recipe");
                    None
                }
            }
        });
        let pkg = match frozen {
            Some(p) => {
                replayed += 1;
                p
            }
            None => ScenePackage::capture(recipe, m.width, m.height).map_err(|e| e.to_string())?,
        };
        out.push(pkg);
    }
    Ok((base_seed, out, replayed))
}

fn scenes(
    job_recipes: Option<&EntryRecipes>,
    seed: u64,
    monitors: &[Monitor],
    settings: &Settings,
) -> (u64, Vec<SceneRecipe>) {
    let ctx = settings.content_context();
    match job_recipes {
        None => (
            seed,
            monitors
                .iter()
                .map(|m| {
                    compose(
                        monitor_seed(seed, m),
                        Aspect::from_size(m.width, m.height),
                        &ctx,
                    )
                })
                .collect(),
        ),
        Some(r) => (
            r.base_seed,
            monitors
                .iter()
                .map(|m| match r.monitors.iter().find(|s| s.monitor == m.id) {
                    Some(s) => s.recipe.clone(),
                    None => compose(
                        monitor_seed(r.base_seed, m),
                        Aspect::from_size(m.width, m.height),
                        &ctx,
                    ),
                })
                .collect(),
        ),
    }
}

/// Runs jobs until the sender goes away, reporting each result through `done`.
pub fn worker(
    jobs: mpsc::Receiver<Job>,
    done: impl Fn(Done) + Send + 'static,
    archive_dir: PathBuf,
) {
    // COM and the wallpaper host belong to this thread.
    let host = match clairlune_platform::detect() {
        Ok(h) => Some(h),
        Err(e) => {
            tracing::warn!(target: "clairlune", error = %e, "no wallpaper host; scenes are archived but not set");
            done(Done::NoHost(format!(
                "Clairlune can't set the wallpaper on this system yet; scenes are saved in the archive. ({e})"
            )));
            None
        }
    };
    let store = StillStore::default_location();
    let archive = Archive::open(&archive_dir);
    // Before the first change, remember the user's own wallpaper so it can be
    // put back (`clairlune-cli restore`, later a button in Settings).
    if let (Some(h), Ok(s)) = (&host, &store) {
        match h.snapshot().map(|snap| s.remember_if_users(&snap)) {
            Ok(Ok(true)) => {
                tracing::info!(target: "clairlune", "remembered the current wallpaper for restore")
            }
            Ok(Ok(false)) => {}
            Ok(Err(e)) => {
                tracing::warn!(target: "clairlune", error = %e, "could not save the current wallpaper")
            }
            Err(e) => {
                tracing::warn!(target: "clairlune", error = %e, "could not read the current wallpaper")
            }
        }
    }
    for job in jobs {
        let result = match (&archive, &store) {
            (Ok(archive), Ok(store)) => run(job, host.as_deref(), store, archive),
            (Err(e), _) | (_, Err(e)) => Done::Failed(format!("data folder: {e}")),
        };
        if let Done::Failed(e) = &result {
            tracing::error!(target: "clairlune", error = %e, "job failed");
        }
        done(result);
    }
}

fn run(job: Job, host: Option<&dyn WallpaperHost>, store: &StillStore, archive: &Archive) -> Done {
    match job {
        Job::Export { dest, source } => export(archive, &dest, source.as_deref()),
        Job::ExportGif {
            dest,
            source,
            theme,
            buddy,
        } => export_gif(archive, &dest, source.as_deref(), theme, buddy),
        Job::Restore => restore(host, store),
        Job::New { seed, settings } => apply(None, seed, &settings, true, host, store, archive),
        Job::Recipes {
            recipes,
            source,
            theme,
            buddy,
            settings,
            archive: add,
        } => apply(
            Some(Source {
                recipes: &recipes,
                dir: source.as_deref(),
                theme,
                buddy,
            }),
            0,
            &settings,
            add,
            host,
            store,
            archive,
        ),
    }
    .unwrap_or_else(Done::Failed)
}

/// Saved scenes to show again, and how.
#[derive(Clone, Copy)]
struct Source<'a> {
    recipes: &'a EntryRecipes,
    /// Folder holding `scene/<i>/` packages.
    dir: Option<&'a Path>,
    theme: Option<Theme>,
    buddy: Option<bool>,
}

fn apply(
    saved: Option<Source>,
    seed: u64,
    settings: &Settings,
    add_to_archive: bool,
    host: Option<&dyn WallpaperHost>,
    store: &StillStore,
    archive: &Archive,
) -> Result<Done, String> {
    let started = Instant::now();
    let monitors = match host {
        Some(h) => h.monitors().map_err(|e| e.to_string())?,
        None => vec![Monitor {
            id: "default".into(),
            x: 0,
            y: 0,
            width: 1920,
            height: 1080,
            primary: true,
        }],
    };
    if monitors.is_empty() {
        return Err("no monitors found".into());
    }
    let wanted = saved
        .and_then(|s| s.theme)
        .unwrap_or_else(|| resolve_theme(settings.theme.mode));
    let buddy = saved.and_then(|s| s.buddy).unwrap_or(settings.buddy.show);
    let (base_seed, packages, replayed) = packages(saved, seed, &monitors, settings)?;
    let recipes: Vec<SceneRecipe> = packages.iter().map(|p| p.recipe.clone()).collect();
    let mut pngs = Vec::new();
    let mut used_theme = wanted;
    let mut primary_still = None;
    for (i, pkg) in packages.iter().enumerate() {
        // Night-only scenes keep their theme.
        let theme = if pkg.themes().contains(&wanted) {
            wanted
        } else {
            pkg.themes()[0]
        };
        let (img, palette) = pkg.still(theme, buddy).ok_or("scene has no frames")?;
        pngs.push(png_of(&img, palette)?);
        if i == 0 {
            used_theme = theme;
            primary_still = Some((img, palette.clone()));
        }
    }
    // The thumbnail: the primary scene shrunk to 480 px wide.
    let (img, palette) = primary_still.ok_or("no monitors")?;
    let thumb = shrink(&img, 480);
    let entry_recipes = EntryRecipes {
        base_seed,
        theme: used_theme.as_str().to_string(),
        monitors: monitors
            .iter()
            .zip(&recipes)
            .map(|(m, r)| MonitorScene {
                monitor: m.id.clone(),
                width: m.width,
                height: m.height,
                recipe: r.clone(),
            })
            .collect(),
    };
    let source = if add_to_archive {
        let entry = archive
            .add(&NewEntry {
                created: crate::timefmt::now_unix(),
                recipes: entry_recipes.clone(),
                pngs: pngs.clone(),
                thumb_png: png_of(&thumb, &palette)?,
                packages,
            })
            .map_err(|e| format!("archive: {e}"))?;
        let swept = archive.sweep(
            crate::timefmt::now_unix(),
            settings.archive.keep_days,
            settings.archive.max_mb.saturating_mul(1024 * 1024),
        );
        if swept.removed > 0 {
            tracing::info!(target: "clairlune", removed = swept.removed, freed = swept.freed, "archive retention");
        }
        Some(archive.dir().join(&entry.name))
    } else {
        saved.and_then(|s| s.dir.map(Path::to_path_buf))
    };
    if let Some(host) = host {
        for (m, png) in monitors.iter().zip(&pngs) {
            let path = store.write(&m.id, png).map_err(|e| format!("still: {e}"))?;
            host.set_static(m, &path).map_err(|e| e.to_string())?;
            store.prune(&m.id, &path);
        }
    }
    let primary = &recipes[0];
    let ms = started.elapsed().as_millis() as u64;
    tracing::info!(
        target: "clairlune",
        seed = base_seed,
        theme = used_theme.as_str(),
        monitors = monitors.len(),
        archetype = %primary.archetype,
        activity = primary.activity.as_deref().unwrap_or("-"),
        recipe_hash = %primary.hash_hex(),
        replayed,
        ms,
        "wallpaper applied"
    );
    Ok(Done::Applied {
        source,
        seed: base_seed,
        theme: used_theme,
        archetype: primary.archetype.clone(),
        activity: primary.activity.clone(),
        monitors: monitors.len(),
        ms,
    })
}

/// Puts back the remembered wallpaper and removes Clairlune's stills.
fn restore(host: Option<&dyn WallpaperHost>, store: &StillStore) -> Result<Done, String> {
    let host = host.ok_or("no wallpaper host on this system")?;
    let snapshot = store
        .original()
        .map_err(|e| e.to_string())?
        .ok_or("there is no earlier wallpaper to put back")?;
    host.restore(&snapshot).map_err(|e| e.to_string())?;
    store.forget_original().map_err(|e| e.to_string())?;
    let removed = store.clear_stills();
    tracing::info!(target: "clairlune", removed, "restored the user's wallpaper");
    Ok(Done::Restored)
}

/// The folder to export from: `source`, or the newest archive entry.
fn export_source(archive: &Archive, source: Option<&Path>) -> Result<PathBuf, String> {
    match source {
        Some(dir) => Ok(dir.to_path_buf()),
        None => {
            let entry: Entry = archive.latest().ok_or("nothing to export yet")?;
            Ok(archive.dir().join(entry.name))
        }
    }
}

fn file_stem(dir: &Path) -> String {
    dir.file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_else(|| "clairlune".into())
}

/// Exports a saved scene (primary monitor) as a 6 s looping GIF, played from
/// its frozen package, so it matches what was on screen.
fn export_gif(
    archive: &Archive,
    dest: &Path,
    source: Option<&Path>,
    theme: Theme,
    buddy: bool,
) -> Result<Done, String> {
    let dir = export_source(archive, source)?;
    let pkg = ScenePackage::load(&dir.join("scene").join("0"))
        .map_err(|e| format!("saved scene: {e}"))?;
    let theme = if pkg.themes().contains(&theme) {
        theme
    } else {
        pkg.themes()[0]
    };
    std::fs::create_dir_all(dest).map_err(|e| e.to_string())?;
    let out = dest.join(format!("{}.gif", file_stem(&dir)));
    let file = std::io::BufWriter::new(std::fs::File::create(&out).map_err(|e| e.to_string())?);
    let stats = clairlune_render::encode_package_gif(
        &pkg,
        theme,
        buddy,
        6000,
        clairlune_render::Smoothness::Balanced,
        file,
    )
    .map_err(|e| e.to_string())?;
    tracing::info!(target: "clairlune", path = %out.display(), frames = stats.frames_written, ms = stats.elapsed_ms, "exported gif");
    Ok(Done::Exported(out))
}

/// Copies a saved scene's primary image to `dest`.
fn export(archive: &Archive, dest: &Path, source: Option<&Path>) -> Result<Done, String> {
    let dir = export_source(archive, source)?;
    std::fs::create_dir_all(dest).map_err(|e| e.to_string())?;
    let src = dir.join("wallpaper-0.png");
    let out = dest.join(format!("{}.png", file_stem(&dir)));
    std::fs::copy(&src, &out).map_err(|e| format!("{}: {e}", src.display()))?;
    tracing::info!(target: "clairlune", path = %out.display(), "exported");
    Ok(Done::Exported(out))
}

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

    fn monitor(w: u32, h: u32) -> Monitor {
        Monitor {
            id: r"\?\DISPLAY#TEST#1".into(),
            x: 0,
            y: 0,
            width: w,
            height: h,
            primary: true,
        }
    }

    #[test]
    fn saved_scenes_replay_when_the_monitor_matches() {
        let dir = std::env::temp_dir().join(format!("clairlune-apply-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        let archive = Archive::open(&dir).unwrap();
        let settings = Settings::default();
        let m = monitor(640, 360);
        // A new wallpaper: composed and captured.
        let (seed, pkgs, replayed) =
            packages(None, 77, std::slice::from_ref(&m), &settings).unwrap();
        assert_eq!((seed, replayed), (77, 0));
        let recipes = EntryRecipes {
            base_seed: seed,
            theme: "dark".into(),
            monitors: vec![MonitorScene {
                monitor: m.id.clone(),
                width: 640,
                height: 360,
                recipe: pkgs[0].recipe.clone(),
            }],
        };
        let entry = archive
            .add(&NewEntry {
                created: 1,
                recipes: recipes.clone(),
                pngs: vec![vec![0; 4]],
                thumb_png: vec![0; 4],
                packages: pkgs,
            })
            .unwrap();
        let entry_dir = archive.dir().join(&entry.name);
        let src = Source {
            recipes: &recipes,
            dir: Some(&entry_dir),
            theme: None,
            buddy: None,
        };
        // Same monitor: replayed from the package.
        let (_, again, replayed) =
            packages(Some(src), 0, std::slice::from_ref(&m), &settings).unwrap();
        assert_eq!(replayed, 1);
        assert_eq!(again[0].recipe, recipes.monitors[0].recipe);
        // A different size: rendered again from the recipe.
        let (_, resized, replayed) =
            packages(Some(src), 0, &[monitor(800, 450)], &settings).unwrap();
        assert_eq!(replayed, 0);
        assert_eq!((resized[0].width, resized[0].height), (800, 450));
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn thumbnails_shrink_to_width() {
        let img = IndexedImage {
            width: 1920,
            height: 1080,
            pixels: vec![3; 1920 * 1080],
        };
        let t = shrink(&img, 480);
        assert_eq!((t.width, t.height), (480, 270));
        assert!(t.pixels.iter().all(|&p| p == 3));
    }
}