From b8d222384b7639bd65408f40900a028cdf27d87d Mon Sep 17 00:00:00 2001 From: elkowar <5300871+elkowar@users.noreply.github.com> Date: Sat, 14 Aug 2021 14:44:22 +0200 Subject: [PATCH] Make only some commands able to start the daemon --- crates/eww/src/app.rs | 4 ++-- crates/eww/src/main.rs | 19 +++++++++++++------ crates/eww/src/opts.rs | 7 +++++++ crates/eww/src/server.rs | 20 +++++++++++++++----- crates/yuck/examples/errors.rs | 31 ------------------------------- 5 files changed, 37 insertions(+), 44 deletions(-) delete mode 100644 crates/yuck/examples/errors.rs diff --git a/crates/eww/src/app.rs b/crates/eww/src/app.rs index 1518416..99e7692 100644 --- a/crates/eww/src/app.rs +++ b/crates/eww/src/app.rs @@ -268,7 +268,7 @@ impl App { self.eww_config = config; self.eww_state.clear_all_window_states(); - let window_names: Vec = self.open_windows.keys().chain(self.failed_windows.iter()).cloned().collect(); + let window_names: Vec = self.open_windows.keys().cloned().chain(self.failed_windows.iter().cloned()).dedup().collect(); for window_name in &window_names { self.open_window(&window_name, None, None, None, None)?; } @@ -386,7 +386,7 @@ fn respond_with_result(sender: DaemonResponseSender, result: Result) -> Re Ok(_) => sender.send_success(String::new()), Err(e) => { let formatted = error_handling_ctx::format_error(&e); - println!("{}", formatted); + println!("Action failed with error: {}", formatted); sender.send_failure(formatted) }, } diff --git a/crates/eww/src/main.rs b/crates/eww/src/main.rs index 21d8eaf..f1b7466 100644 --- a/crates/eww/src/main.rs +++ b/crates/eww/src/main.rs @@ -27,6 +27,7 @@ pub mod app; pub mod application_lifecycle; pub mod client; pub mod config; +mod daemon_response; pub mod display_backend; pub mod error; mod error_handling_ctx; @@ -38,7 +39,6 @@ pub mod script_var_handler; pub mod server; pub mod util; pub mod widgets; -mod daemon_response; fn main() { let eww_binary_name = std::env::args().next().unwrap(); @@ -63,12 +63,9 @@ fn main() { client::handle_client_only_action(&paths, action)?; false } - opts::Action::WithServer(ActionWithServer::KillServer) => { - handle_server_command(&paths, &ActionWithServer::KillServer, 1)?; - false - } + // a running daemon is necessary for this command - opts::Action::WithServer(action) => { + opts::Action::WithServer(action) if action.can_start_daemon() => { // attempt to just send the command to a running daemon if let Err(err) = handle_server_command(&paths, &action, 5) { // connecting to the daemon failed. Thus, start the daemon here! @@ -81,6 +78,7 @@ fn main() { let (command, response_recv) = action.into_daemon_command(); // start the daemon and give it the command + let fork_result = server::initialize_server(paths.clone(), Some(command))?; let is_parent = fork_result == ForkResult::Parent; if let (Some(recv), true) = (response_recv, is_parent) { @@ -91,6 +89,15 @@ fn main() { true } } + opts::Action::WithServer(ActionWithServer::KillServer) => { + handle_server_command(&paths, &ActionWithServer::KillServer, 1)?; + false + } + + opts::Action::WithServer(action) => { + handle_server_command(&paths, &action, 5)?; + true + } // make sure that there isn't already a Eww daemon running. opts::Action::Daemon if check_server_running(paths.get_ipc_socket_file()) => { diff --git a/crates/eww/src/opts.rs b/crates/eww/src/opts.rs index 636d660..e93fb7b 100644 --- a/crates/eww/src/opts.rs +++ b/crates/eww/src/opts.rs @@ -161,6 +161,13 @@ fn parse_var_update_arg(s: &str) -> Result<(VarName, DynVal)> { } impl ActionWithServer { + pub fn can_start_daemon(&self) -> bool { + match self { + ActionWithServer::OpenWindow { .. } | ActionWithServer::OpenMany { .. } => true, + _ => false, + } + } + pub fn into_daemon_command(self) -> (app::DaemonCommand, Option) { let command = match self { ActionWithServer::Update { mappings } => app::DaemonCommand::UpdateVars(mappings), diff --git a/crates/eww/src/server.rs b/crates/eww/src/server.rs index 9a6abd1..9be673b 100644 --- a/crates/eww/src/server.rs +++ b/crates/eww/src/server.rs @@ -1,7 +1,17 @@ -use crate::{EwwPaths, app::{self, DaemonCommand}, config, daemon_response, error_handling_ctx, eww_state::*, ipc_server, script_var_handler, util}; +use crate::{ + app::{self, DaemonCommand}, + config, daemon_response, error_handling_ctx, + eww_state::*, + ipc_server, script_var_handler, util, EwwPaths, +}; use anyhow::*; -use std::{collections::{HashMap, HashSet}, os::unix::io::AsRawFd, path::Path, sync::{atomic::Ordering, Arc}}; +use std::{ + collections::{HashMap, HashSet}, + os::unix::io::AsRawFd, + path::Path, + sync::{atomic::Ordering, Arc}, +}; use tokio::sync::mpsc::*; pub fn initialize_server(paths: EwwPaths, action: Option) -> Result { @@ -183,11 +193,11 @@ fn do_detach(log_file_path: impl AsRef) -> Result { match unsafe { nix::unistd::fork()? } { nix::unistd::ForkResult::Child => { nix::unistd::setsid()?; - match unsafe {nix::unistd::fork()? - }{ + match unsafe { nix::unistd::fork()? } { nix::unistd::ForkResult::Parent { .. } => std::process::exit(0), nix::unistd::ForkResult::Child => {} - }} + } + } nix::unistd::ForkResult::Parent { .. } => { return Ok(ForkResult::Parent); } diff --git a/crates/yuck/examples/errors.rs b/crates/yuck/examples/errors.rs deleted file mode 100644 index cd14f8a..0000000 --- a/crates/yuck/examples/errors.rs +++ /dev/null @@ -1,31 +0,0 @@ -// use eww_config::{ -// format_diagnostic::ToDiagnostic, -// parser::{ast::*, from_ast::FromAst}, -//}; - -fn main() { - println!("hi"); - // let mut files = codespan_reporting::files::SimpleFiles::new(); - - // let input = r#" - //(heyho ; :foo { "foo \" } bar " } - //; :baz {(foo == bar ? 12.2 : 12)} - //(foo) - //(defwidget foo [something bla] "foo") - //(baz))"#; - - // let file_id = files.add("foo.eww", input); - // let ast = eww_config::parser::parse_string(file_id, input); - // match ast.and_then(eww_config::parser::from_ast::Element::::from_ast) { - // Ok(ast) => { - // println!("{:?}", ast); - //} - // Err(err) => { - // dbg!(&err); - // let diag = err.to_diagnostic(); - // use codespan_reporting::term; - // let config = term::Config::default(); - // let mut writer = term::termcolor::StandardStream::stderr(term::termcolor::ColorChoice::Always); - // term::emit(&mut writer, &config, &files, &diag).unwrap(); - //} -}