Skip to content

Commit

Permalink
Merge pull request #230 from WhyNotHugo/main
Browse files Browse the repository at this point in the history
Drop daemonizing logic
  • Loading branch information
LGFae authored Jun 11, 2024
2 parents ceb6306 + c8a1981 commit 316fa39
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 167 deletions.
2 changes: 1 addition & 1 deletion doc/swww-daemon.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ daemon will take care of both creating and deleting that file when it is
initialized or killed.

# SEE ALSO
*swww-init*(1)
*swww*(1)
3 changes: 0 additions & 3 deletions doc/swww-img.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,6 @@ to read from stdin instead.
The images sent will be cached at _$XDG_CACHE_HOME/swww_ or _$HOME/.cache/swww_
if $XDG_CACHE_HOME does not exist. For each monitor, there will be a file in
those locations corresponding to the current image/animation being displayed.
Importantly, **cache will only be loaded during initialization if you use swww
init**. That is, calling `swww-daemon` directly will **NOT** load the cache, but
calling `swww-init` will.

The `swww-daemon` will actually wait until the first image has been set before
trying to load the cache.
Expand Down
42 changes: 0 additions & 42 deletions doc/swww-init.1.scd

This file was deleted.

5 changes: 1 addition & 4 deletions doc/swww.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ swww - A Solution to your Wayland Wallpaper Woes
*img*
Sends an image (or animated gif) for the daemon to display

*init*
Initializes the daemon

*kill*
Kills the daemon

Expand Down Expand Up @@ -63,5 +60,5 @@ protocol*. Typically, _wlr-roots_ based compositors.
previous image when a monitor is (re)connected or turned on.

# SEE ALSO
*swww-daemon*(1) *swww-clear*(1) *swww-img*(1) *swww-init*(1) *swww-kill*(1)
*swww-daemon*(1) *swww-clear*(1) *swww-img*(1) *swww-kill*(1)
*swww-query*(1)
28 changes: 0 additions & 28 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,34 +174,6 @@ pub enum Swww {
/// Use `-` to read from stdin
Img(Img),

/// [DEPRECATED] Initializes the daemon.
///
/// 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
///results some reliable: `swww-daemon --no-cache && swww img <some img>`
#[clap(long)]
no_cache: bool,

/// Force the daemon to use a specific wl_shm format
///
/// IMPORTANT: make sure this is a value your compositor actually supports! `swww` will
/// automatically select the best format for itself during initialization; this is only
/// here for fallback, debug, and workaround purposes
#[clap(long)]
format: Option<PixelFormat>,
},

///Kills the daemon
Kill,

Expand Down
90 changes: 1 addition & 89 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use std::{process::Stdio, time::Duration};
use std::time::Duration;

use utils::{
cache,
Expand All @@ -14,46 +14,6 @@ use cli::{CliImage, ResizeStrategy, Swww};

fn main() -> Result<(), String> {
let swww = Swww::parse();
if let Swww::Init {
no_daemon, format, ..
} = &swww
{
eprintln!(
"DEPRECATION WARNING: `swww init` IS DEPRECATED. Call `swww-daemon` directly instead"
);
match is_daemon_running() {
Ok(false) => {
let socket_path = get_socket_path();
if socket_path.exists() {
eprintln!(
"WARNING: socket file {} was not deleted when the previous daemon exited",
socket_path.to_string_lossy()
);
if let Err(e) = std::fs::remove_file(socket_path) {
return Err(format!("failed to delete previous socket: {e}"));
}
}
}
Ok(true) => {
return Err("There seems to already be another instance running...".to_string())
}
Err(e) => {
eprintln!("WARNING: failed to read '/proc' directory to determine whether the daemon is running: {e}
Falling back to trying to checking if the socket file exists...");
let socket_path = get_socket_path();
if socket_path.exists() {
return Err(format!(
"Found socket at {}. There seems to be an instance already running...",
socket_path.to_string_lossy()
));
}
}
}
spawn_daemon(*no_daemon, format)?;
if *no_daemon {
return Ok(());
}
}

if let Swww::ClearCache = &swww {
return cache::clean().map_err(|e| format!("failed to clean the cache: {e}"));
Expand Down Expand Up @@ -143,12 +103,6 @@ fn make_request(args: &Swww) -> Result<Option<RequestSend>, String> {

Ok(Some(RequestSend::Img(img_request)))
}
Swww::Init { no_cache, .. } => {
if !*no_cache {
restore_from_cache(&[])?;
}
Ok(None)
}
Swww::Kill => Ok(Some(RequestSend::Kill)),
Swww::Query => Ok(Some(RequestSend::Query)),
}
Expand Down Expand Up @@ -308,48 +262,6 @@ fn split_cmdline_outputs(outputs: &str) -> Box<[String]> {
.collect()
}

fn spawn_daemon(no_daemon: bool, format: &Option<cli::PixelFormat>) -> Result<(), 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}")),
}
}
}

fn is_daemon_running() -> Result<bool, String> {
let socket = match connect_to_socket(&get_socket_path(), 5, 100) {
Ok(s) => s,
// likely a connection refused; either way, this is a reliable signal there's no surviving
// daemon.
Err(_) => return Ok(false),
};

RequestSend::Ping.send(&socket)?;
let answer = Answer::receive(read_socket(&socket)?);
match answer {
Answer::Ping(_) => Ok(true),
_ => Err("Daemon did not return Answer::Ping, as expected".to_string()),
}
}

fn restore_from_cache(requested_outputs: &[String]) -> Result<(), String> {
let (_, _, outputs) = get_format_dims_and_outputs(requested_outputs)?;

Expand Down

0 comments on commit 316fa39

Please sign in to comment.