diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 6ad8d0e..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,45 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "type": "lldb", - "request": "launch", - "name": "Debug executable 'eww'", - "cargo": { - "args": [ - "build", - "--bin=eww", - "--package=eww" - ], - "filter": { - "name": "eww", - "kind": "bin" - } - }, - "args": [], - "cwd": "${workspaceFolder}" - }, - { - "type": "lldb", - "request": "launch", - "name": "Debug unit tests in executable 'eww'", - "cargo": { - "args": [ - "test", - "--no-run", - "--bin=eww", - "--package=eww" - ], - "filter": { - "name": "eww", - "kind": "bin" - } - }, - "args": [], - "cwd": "${workspaceFolder}" - } - ] -} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index fdb2182..be09764 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -303,6 +303,7 @@ dependencies = [ "derive_more", "extend", "gdk", + "gdk-pixbuf", "gio", "glib", "grass", @@ -311,6 +312,7 @@ dependencies = [ "ipc-channel", "itertools", "lazy_static", + "libc", "log 0.4.11", "maplit", "notify", diff --git a/Cargo.toml b/Cargo.toml index b248670..afbe2bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ gtk = { version = "0.9", features = [ "v3_16" ] } gdk = { version = "", features = ["v3_16"] } gio = { version = "", features = ["v2_44"] } glib = { version = "", features = ["v2_44"] } +gdk-pixbuf = "0.9" regex = "1" try_match = "0.2.2" @@ -34,6 +35,7 @@ debug_stub_derive = "0.3" log = "0.4" pretty_env_logger = "0.4" lazy_static = "1.4.0" +libc = "0.2" #thiserror = "1.0" diff --git a/src/app.rs b/src/app.rs index 384d999..924ff24 100644 --- a/src/app.rs +++ b/src/app.rs @@ -23,11 +23,15 @@ pub struct App { } impl App { - pub fn handle_user_command(&mut self, opts: Opt) -> Result<()> { - match opts.action { - OptAction::Update { fieldname, value } => self.update_state(fieldname, value), + pub fn handle_user_command(&mut self, opts: &Opt) -> Result<()> { + match &opts.action { + OptAction::Update { fieldname, value } => self.update_state(fieldname.clone(), value.clone()), OptAction::OpenWindow { window_name } => self.open_window(&window_name)?, OptAction::CloseWindow { window_name } => self.close_window(&window_name)?, + OptAction::KillServer => { + log::info!("Received kill command, stopping server!"); + std::process::exit(0) + } } Ok(()) } @@ -36,7 +40,7 @@ impl App { log::debug!("Handling event: {:?}", &event); let result: Result<_> = try { match event { - EwwEvent::UserCommand(command) => self.handle_user_command(command)?, + EwwEvent::UserCommand(command) => self.handle_user_command(&command)?, EwwEvent::UpdateVar(key, value) => self.update_state(key, value), EwwEvent::ReloadConfig(config) => self.reload_all_windows(config)?, EwwEvent::ReloadCss(css) => self.load_css(&css)?, diff --git a/src/main.rs b/src/main.rs index 9bb2c1c..62905d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,6 +58,9 @@ pub struct Opt { #[structopt(subcommand)] action: OptAction, + + #[structopt(short = "-d", long = "--detach")] + should_detach: bool, } #[derive(StructOpt, Debug, Serialize, Deserialize)] pub enum OptAction { @@ -69,6 +72,9 @@ pub enum OptAction { #[structopt(name = "close")] CloseWindow { window_name: String }, + + #[structopt(name = "kill")] + KillServer, } fn try_main() -> Result<()> { @@ -79,6 +85,11 @@ fn try_main() -> Result<()> { sender.send(opts)?; } else { log::info!("No instance found... Initializing server."); + + if opts.should_detach { + do_detach(); + } + initialize_server(opts)?; } Ok(()) @@ -134,7 +145,7 @@ fn initialize_server(opts: Opt) -> Result<()> { } // run the command that eww was started with - app.handle_user_command(opts)?; + app.handle_user_command(&opts)?; run_server_thread(evt_send.clone()); let _hotwatch = run_filewatch_thread(&config_file_path, &scss_file_path, evt_send.clone())?; @@ -198,6 +209,41 @@ fn run_filewatch_thread>( Ok(hotwatch) } +fn do_detach() { + // detach from terminal + let pid = unsafe { libc::fork() }; + if dbg!(pid) < 0 { + panic!("Phailed to Phork: {}", std::io::Error::last_os_error()); + } + if pid != 0 { + std::process::exit(0); + } + + // close stdout to not spam output + if unsafe { libc::isatty(1) } != 0 { + unsafe { + libc::close(1); + } + } + //close stderr to not spam output + if unsafe { libc::isatty(2) } != 0 { + unsafe { + let fd = libc::open(std::ffi::CString::new("/dev/null").unwrap().as_ptr(), libc::O_RDWR); + if fd < 0 { + panic!("Phailed to open /dev/null?!: {}", std::io::Error::last_os_error()); + } else { + if libc::dup2(fd, libc::STDERR_FILENO) < 0 { + panic!( + "Phailed to dup stderr phd to /dev/null: {:?}", + std::io::Error::last_os_error() + ); + } + libc::close(fd); + } + } + } +} + #[extend::ext(pub)] impl hotwatch::Hotwatch { fn watch_file_changes(&mut self, file_path: P, callback: F) -> Result<()> diff --git a/src/widgets/widget_definitions.rs b/src/widgets/widget_definitions.rs index 17b7d90..40add27 100644 --- a/src/widgets/widget_definitions.rs +++ b/src/widgets/widget_definitions.rs @@ -96,7 +96,9 @@ fn build_gtk_button(bargs: &mut BuilderArgs) -> Result { fn build_gtk_image(bargs: &mut BuilderArgs) -> Result { let gtk_widget = gtk::Image::new(); resolve!(bargs, gtk_widget, { - resolve_str => "path" = req => |v| gtk_widget.set_from_file(Path::new(&v)) + resolve_str => "path" = req => |v| { + gtk_widget.set_from_file(Path::new(&v)); + } }); Ok(gtk_widget) }