Skip to content

Commit

Permalink
Drop daemonizing logic
Browse files Browse the repository at this point in the history
Run in foreground, which is what service managers expect. Usages like
running `exec swww` in sway's config continue to work fine. User session
start-up scripts can simply run `swww &`.

Fixes: #182
  • Loading branch information
WhyNotHugo committed Jun 7, 2024
1 parent 319b7fb commit 0389f02
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 39 deletions.
7 changes: 0 additions & 7 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,6 @@ pub enum Swww {
/// Exits if there is already a daemon running. We check that by seeing if
/// $XDG_RUNTIME_DIR/swww.socket exists.
Init {
///Don't fork the daemon. This will keep it running in the current terminal.
///
///The only advantage of this would be seeing the logging real time. Note that for release
///builds we only log info, warnings and errors, so you won't be seeing much (ideally).
#[clap(long)]
no_daemon: bool,

///Don't load the cache *during initialization* (it still loads on monitor (re)connection)
///
///If want to always pass an image for `swww` to load, this option can help make the
Expand Down
43 changes: 11 additions & 32 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use clap::Parser;
use std::{path::PathBuf, process::Stdio, time::Duration};
use std::{
os::unix::{net::UnixStream, process::CommandExt},
path::PathBuf,
time::Duration,
};

use utils::{
cache,
Expand All @@ -14,10 +18,7 @@ use cli::{ResizeStrategy, Swww};

fn main() -> Result<(), String> {
let swww = Swww::parse();
if let Swww::Init {
no_daemon, format, ..
} = &swww
{
if let Swww::Init { .. } = &swww {
eprintln!(
"DEPRECATION WARNING: `swww init` IS DEPRECATED. Call `swww-daemon` directly instead"
);
Expand Down Expand Up @@ -49,10 +50,7 @@ fn main() -> Result<(), String> {
}
}
}
spawn_daemon(*no_daemon, format)?;
if *no_daemon {
return Ok(());
}
return Err(spawn_daemon());
}

if let Swww::ClearCache = &swww {
Expand Down Expand Up @@ -282,30 +280,11 @@ fn split_cmdline_outputs(outputs: &str) -> Box<[String]> {
.collect()
}

fn spawn_daemon(no_daemon: bool, format: &Option<cli::PixelFormat>) -> Result<(), String> {
/// Only returns if `exec` fails.
fn spawn_daemon() -> String {
let mut cmd = std::process::Command::new("swww-daemon");

if let Some(format) = format {
cmd.arg("--format");
cmd.arg(match format {
cli::PixelFormat::Xrgb => "xrgb",
cli::PixelFormat::Xbgr => "xbgr",
cli::PixelFormat::Rgb => "rgb",
cli::PixelFormat::Bgr => "bgr",
});
}

if no_daemon {
match cmd.status() {
Ok(_) => Ok(()),
Err(e) => Err(format!("error spawning swww-daemon: {e}")),
}
} else {
match cmd.stdout(Stdio::null()).stderr(Stdio::null()).spawn() {
Ok(_) => Ok(()),
Err(e) => Err(format!("error spawning swww-daemon: {e}")),
}
}
let err = cmd.exec();
format!("failed to exec into swww-daemon: {err}")
}

fn is_daemon_running() -> Result<bool, String> {
Expand Down

0 comments on commit 0389f02

Please sign in to comment.