wallr 0.8.1

The wallpaper engine for Wayland - GPU transitions, video wallpapers, and theme pipeline integration.
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
#![recursion_limit = "256"]

use anyhow::Result;
use clap::Parser;
use colored::Colorize;
use tracing_subscriber::EnvFilter;
use wallr_common::cli::{Commands, ConfigCommands, EffectArgs, IpcCommands, WallrCli};
use wallr_common::config::{self, WallrConfig};
use wallr_common::effect::{Effect, FadeParams};
use wallr_core::ipc::{IpcCommand, IpcResponse, send_ipc_command};
use wallr_core::wallpaper::{DiagnosticStatus, WallpaperEngine};

/// Upper bound for `wallr set -` input. It is read before any format
/// detection or staging, so an endless pipe must not grow memory without
/// limit.
const MAX_STDIN_BYTES: u64 = 256 * 1024 * 1024;

fn config_value<'a>(value: &'a serde_yaml::Value, key: &str) -> Option<&'a serde_yaml::Value> {
    key.split('.')
        .try_fold(value, |current, part| current.get(part))
}

fn set_config_value(
    value: &mut serde_yaml::Value,
    key: &str,
    replacement: serde_yaml::Value,
) -> anyhow::Result<()> {
    let parts: Vec<&str> = key.split('.').collect();
    if parts.is_empty() {
        anyhow::bail!("config key cannot be empty");
    }
    let mut current = value;
    for part in &parts[..parts.len() - 1] {
        current = current
            .get_mut(*part)
            .ok_or_else(|| anyhow::anyhow!("unknown config key: {key}"))?;
    }
    let last = parts[parts.len() - 1];
    if last.is_empty() || current.get(last).is_none() {
        anyhow::bail!("unknown config key: {key}");
    }
    current[last] = replacement;
    Ok(())
}

fn ensure_response(response: IpcResponse) -> anyhow::Result<IpcResponse> {
    if response.success {
        Ok(response)
    } else {
        anyhow::bail!(
            "{}",
            response
                .message
                .unwrap_or_else(|| "daemon reported failure".to_string())
        )
    }
}

fn print_cli_message(message: &str) {
    for line in message.trim_end().lines() {
        let trimmed = line.trim_start();
        let indent = &line[..line.len() - trimmed.len()];
        let lower = trimmed.to_ascii_lowercase();

        if lower.starts_with("error")
            || lower.starts_with("failed")
            || lower.starts_with("no ")
            || lower.starts_with("unknown ")
        {
            println!("{}{}", indent, trimmed.red());
        } else if let Some((label, value)) = trimmed.split_once(':') {
            println!("{}{}: {}", indent, label.cyan().bold(), value.trim_start());
        } else {
            println!("{}{}", indent, trimmed.green());
        }
    }
}

/// Combine CLI `--effect`/override flags into the transition effect.
/// With no `--effect` flag the default is a plain linear crossfade,
/// matching awww's default `simple` feel; explicit `-e fade` keeps polish.
fn pick_effect(effect_args: &EffectArgs) -> anyhow::Result<(Effect, Option<u32>)> {
    let effect = effect_args.to_effect(Effect::Fade(FadeParams {
        easing: wallr_common::effect::Easing::Linear,
        ..FadeParams::default()
    }));
    let duration_ms = effect_args
        .duration
        .as_deref()
        .map(wallr_common::config::parse_duration)
        .transpose()?
        .map(|duration| {
            u32::try_from(duration.as_millis()).map_err(|_| {
                anyhow::anyhow!(
                    "transition duration {}ms exceeds the supported maximum",
                    duration.as_millis()
                )
            })
        })
        .transpose()?;
    Ok((effect, duration_ms))
}

fn detect_image_extension(bytes: &[u8]) -> &'static str {
    if bytes.starts_with(b"GIF8") {
        "gif"
    } else if bytes.starts_with(b"\x89PNG\r\n\x1a\n") {
        "png"
    } else if bytes.starts_with(b"\xff\xd8\xff") {
        "jpg"
    } else if bytes.len() >= 12 && &bytes[0..4] == b"RIFF" && &bytes[8..12] == b"WEBP" {
        "webp"
    } else {
        // Let the image decoder report the actual format. PNG is the common
        // screenshot format and is also what grim emits.
        "png"
    }
}

fn stage_stdin(bytes: &[u8], extension: &str) -> anyhow::Result<std::path::PathBuf> {
    use std::io::Write;
    use std::os::unix::fs::OpenOptionsExt;

    let directory = std::env::temp_dir();
    let mut attempt = 0u32;
    loop {
        let path = directory.join(format!(
            "wallr-stdin-{}-{}-{}.{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos(),
            attempt,
            extension
        ));
        match std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .mode(0o600)
            .open(&path)
        {
            Ok(mut file) => {
                file.write_all(bytes)?;
                file.flush()?;
                return Ok(path);
            }
            Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {
                attempt = attempt.wrapping_add(1);
            }
            Err(err) => return Err(err.into()),
        }
    }
}

// All decoding, rendering, and filesystem work is dispatched through
// `spawn_blocking`; a current-thread scheduler avoids keeping one idle async
// worker per CPU in the long-lived daemon and keeps the IPC-only CLI light.
fn main() {
    let cli = WallrCli::parse();
    let result = match tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .max_blocking_threads(4)
        .build()
    {
        Ok(runtime) => runtime.block_on(async_main(cli)),
        Err(error) => Err(error.into()),
    };
    if let Err(error) = result {
        eprintln!("{} {}", "error:".red().bold(), error);
        std::process::exit(1);
    }
}

async fn async_main(cli: WallrCli) -> Result<()> {
    let filter = if cli.verbose > 0 {
        "wallr=debug"
    } else if cli.quiet {
        "wallr=error"
    } else {
        "wallr=off"
    };

    let _ = tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::new(filter))
        .try_init();

    let config_path_owned = cli.config.clone();
    let config_path = config_path_owned.as_deref();
    let config = match config::load_config(config_path) {
        Ok(config) => config,
        // `wallr config set -c new.yaml` is also how users create a fresh
        // config file. Keep that one command lenient, but report a missing
        // explicit file to every other command instead of silently using
        // defaults with the wrong socket/paths.
        Err(err)
            if config_path.is_some()
                && matches!(
                    &cli.command,
                    Commands::Config {
                        subcommand: ConfigCommands::Set { .. }
                    }
                ) =>
        {
            let _ = err;
            WallrConfig::default()
        }
        Err(err) => return Err(err.into()),
    };

    match cli.command {
        Commands::Set {
            path,
            no_theme,
            theme,
            fps,
            monitor,
            mode,
            effect_args,
        } => {
            let socket_path = config::expand_path(&config.daemon.socket)?;
            let daemon_running = tokio::net::UnixStream::connect(&socket_path).await.is_ok();

            if !daemon_running {
                if !config.daemon.auto_start {
                    anyhow::bail!(
                        "daemon is not running and daemon.auto_start is false; \
                         start it with `wallr daemon`"
                    );
                }
                let exe = std::env::current_exe()?;
                let mut daemon_command = std::process::Command::new(&exe);
                if let Some(path) = &config_path_owned {
                    daemon_command.arg("--config").arg(path);
                }
                daemon_command.arg("daemon");
                let _ = daemon_command
                    .stdin(std::process::Stdio::null())
                    .stdout(std::process::Stdio::null())
                    .stderr(std::process::Stdio::null())
                    .spawn()?;

                let mut waited = 0u64;
                loop {
                    tokio::time::sleep(std::time::Duration::from_millis(100)).await;
                    waited += 100;
                    if tokio::net::UnixStream::connect(&socket_path).await.is_ok() {
                        break;
                    }
                    if waited >= 3000 {
                        anyhow::bail!(
                            "daemon did not start within 3 seconds. \
                             run `wallr daemon` manually and check for errors."
                        );
                    }
                }
            }

            let (effect, duration_ms) = pick_effect(&effect_args)?;

            // A `-` path reads image bytes from stdin (like `awww img -`),
            // staging them in a private temp file: downstream code paths key
            // GIF/static decoding off the file extension.
            let (path, staged) = if path.as_os_str() == "-" {
                use std::io::Read;
                let mut bytes = Vec::new();
                std::io::stdin()
                    .take(MAX_STDIN_BYTES + 1)
                    .read_to_end(&mut bytes)?;
                anyhow::ensure!(!bytes.is_empty(), "no image data on stdin");
                anyhow::ensure!(
                    bytes.len() as u64 <= MAX_STDIN_BYTES,
                    "stdin image exceeds the {} MiB limit",
                    MAX_STDIN_BYTES / (1024 * 1024)
                );
                let ext = detect_image_extension(&bytes);
                (stage_stdin(&bytes, ext)?, true)
            } else {
                (path, false)
            };
            let canonical_path = path.canonicalize().unwrap_or_else(|_| path.clone());
            let response = send_ipc_command(
                socket_path,
                IpcCommand::Preview {
                    path: canonical_path.to_string_lossy().to_string(),
                    effect: Some(effect),
                    duration_ms,
                    fps,
                    no_theme,
                    theme_override: theme,
                    monitor,
                    scaling_mode: mode.or(Some(config.wallpaper.mode.clone())),
                },
            )
            .await;
            if staged {
                let _ = std::fs::remove_file(&path);
            }
            ensure_response(response?)?;
            println!("{} {}", "✓".green().bold(), "wallpaper applied".green());
        }

        Commands::Doctor => {
            let engine = WallpaperEngine::new(config)?;
            let report = engine.doctor();
            println!("\n{}", "wallr doctor".bold());
            println!("{}", "─".repeat(32).dimmed());
            for check in &report.checks {
                let icon = match check.status {
                    DiagnosticStatus::Pass => "✓".green(),
                    DiagnosticStatus::Warn => "!".yellow(),
                    DiagnosticStatus::Fail => "✗".red(),
                };
                println!(
                    "  {} {:<22} {}",
                    icon,
                    check.name.to_ascii_lowercase().bold(),
                    check.message.dimmed()
                );
            }
            println!();
        }

        Commands::Config { subcommand } => match subcommand {
            ConfigCommands::Path => {
                println!("{}", config::config_path().display().to_string().cyan());
            }
            ConfigCommands::Get { key } => {
                let yaml = serde_yaml::to_string(&config)?;
                let value: serde_yaml::Value = serde_yaml::from_str(&yaml)?;
                match config_value(&value, &key) {
                    Some(found) => println!("{}", serde_yaml::to_string(found)?.trim_end()),
                    None => anyhow::bail!("unknown config key: {key}"),
                }
            }
            ConfigCommands::Set { key, value } => {
                let path = cli.config.clone().unwrap_or_else(config::config_path);
                let mut yaml: serde_yaml::Value = if path.exists() {
                    serde_yaml::from_str(&std::fs::read_to_string(&path)?)?
                } else {
                    serde_yaml::to_value(&config)?
                };
                let replacement: serde_yaml::Value =
                    serde_yaml::from_str(&value).unwrap_or(serde_yaml::Value::String(value));
                set_config_value(&mut yaml, &key, replacement)?;
                if let Some(parent) = path.parent() {
                    std::fs::create_dir_all(parent)?;
                }
                std::fs::write(&path, serde_yaml::to_string(&yaml)?)?;
                println!("{} {}", "updated".green(), path.display());
            }
        },

        Commands::Reload => {
            let socket_path = config::expand_path(&config.daemon.socket)?;
            if tokio::net::UnixStream::connect(&socket_path).await.is_ok() {
                let response =
                    ensure_response(send_ipc_command(socket_path, IpcCommand::Reload).await?)?;
                println!(
                    "{} {}",
                    "✓".green().bold(),
                    "configuration reloaded".green()
                );
                if let Some(msg) = response.message {
                    print_cli_message(&msg);
                }
            } else {
                let engine = WallpaperEngine::new(config)?;
                tokio::task::spawn_blocking(move || engine.reload_blocking()).await??;
                println!("{} configuration and hooks reloaded", "✓".green());
            }
        }

        Commands::Monitor { subcommand } => {
            use wallr_common::cli::MonitorCommands;
            let socket_path = config::expand_path(&config.daemon.socket)?;
            let response = match subcommand {
                MonitorCommands::List => {
                    send_ipc_command(socket_path, IpcCommand::MonitorList).await?
                }
                MonitorCommands::Current => {
                    send_ipc_command(socket_path, IpcCommand::MonitorCurrent).await?
                }
            };
            let response = ensure_response(response)?;
            if let Some(msg) = response.message {
                print_cli_message(&msg);
            }
        }

        Commands::Quit => {
            let socket_path = config::expand_path(&config.daemon.socket)?;
            if tokio::net::UnixStream::connect(&socket_path).await.is_ok() {
                let response =
                    ensure_response(send_ipc_command(socket_path, IpcCommand::Stop).await?)?;
                if let Some(msg) = response.message {
                    print_cli_message(&msg);
                }
            } else {
                anyhow::bail!("daemon is not running");
            }
        }

        Commands::Daemon { max_fps } => {
            let daemon = std::env::current_exe()?.with_file_name("wallr-daemon");
            let mut command = std::process::Command::new(daemon);
            if let Some(path) = config_path_owned {
                command.arg("--config").arg(path);
            }
            command.arg("daemon");
            if let Some(fps) = max_fps {
                command.arg("--max-fps").arg(fps.to_string());
            }
            let status = command.status()?;
            if !status.success() {
                anyhow::bail!("wallr-daemon exited with {status}");
            }
        }

        Commands::Ipc { subcommand } => {
            let socket_path = config::expand_path(&config.daemon.socket)?;
            let cmd = match subcommand {
                IpcCommands::Pause { monitor } => IpcCommand::Pause { monitor },
                IpcCommands::Resume { monitor } => IpcCommand::Resume { monitor },
                IpcCommands::Reload => IpcCommand::Reload,
                IpcCommands::Stop => IpcCommand::Stop,
                IpcCommands::Status => IpcCommand::Status,
                IpcCommands::Info { monitor } => IpcCommand::Info { monitor },
                IpcCommands::Seek { timestamp, monitor } => {
                    let timestamp_ms = wallr_common::cli::parse_timestamp_ms(&timestamp)
                        .map_err(|e| anyhow::anyhow!(e))?;
                    IpcCommand::Seek {
                        timestamp_ms,
                        monitor,
                    }
                }
                IpcCommands::Blank {
                    monitor,
                    effect_args,
                } => {
                    let (effect, duration_ms) = pick_effect(&effect_args)?;
                    let opt_effect = if effect_args.effect.is_some()
                        || effect_args.from.is_some()
                        || effect_args.to.is_some()
                    {
                        Some(effect)
                    } else {
                        None
                    };
                    IpcCommand::Blank {
                        monitor,
                        effect: opt_effect,
                        duration_ms,
                    }
                }
                IpcCommands::Restore {
                    monitor,
                    effect_args,
                } => {
                    let (effect, duration_ms) = pick_effect(&effect_args)?;
                    let opt_effect = if effect_args.effect.is_some()
                        || effect_args.from.is_some()
                        || effect_args.to.is_some()
                    {
                        Some(effect)
                    } else {
                        None
                    };
                    IpcCommand::Restore {
                        monitor,
                        effect: opt_effect,
                        duration_ms,
                    }
                }
            };
            let response = ensure_response(send_ipc_command(socket_path, cmd).await?)?;
            if let Some(msg) = response.message {
                print_cli_message(&msg);
            }
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{detect_image_extension, set_config_value};

    #[test]
    fn detects_stdin_formats_from_magic() {
        assert_eq!(detect_image_extension(b"GIF89a...."), "gif");
        assert_eq!(detect_image_extension(b"\x89PNG\r\n\x1a\n...."), "png");
        assert_eq!(detect_image_extension(b"\xff\xd8\xff...."), "jpg");
        assert_eq!(detect_image_extension(b"RIFF1234WEBP...."), "webp");
        assert_eq!(detect_image_extension(b"unknown"), "png");
    }

    #[test]
    fn config_set_rejects_unknown_final_keys() {
        let mut value = serde_yaml::from_str("wallpaper:\n  mode: fill\n").unwrap();
        assert!(set_config_value(&mut value, "wallpaper.nope", serde_yaml::Value::Null).is_err());
        assert!(set_config_value(&mut value, "wallpaper.mode", serde_yaml::Value::Null).is_ok());
    }
}