add -d flag to detach from terminal

This commit is contained in:
elkowar 2020-10-05 21:31:54 +02:00
parent 6f8a97c1bd
commit 9f69bbb440
6 changed files with 62 additions and 51 deletions

45
.vscode/launch.json vendored
View file

@ -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}"
}
]
}

2
Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

@ -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)?,

View file

@ -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<P: AsRef<Path>>(
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<P, F>(&mut self, file_path: P, callback: F) -> Result<()>

View file

@ -96,7 +96,9 @@ fn build_gtk_button(bargs: &mut BuilderArgs) -> Result<gtk::Button> {
fn build_gtk_image(bargs: &mut BuilderArgs) -> Result<gtk::Image> {
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)
}