From 44492a77e0813474ac325e9fbc1816bf417796b3 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Thu, 18 Feb 2021 15:34:44 +0100 Subject: [PATCH] Run sway commands via IPC connection instead on shelling out and calling swaymsg --- src/cmds.rs | 23 ++++++++++++++++++----- src/demon.rs | 6 +++++- src/util.rs | 10 ---------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/cmds.rs b/src/cmds.rs index c1e46f5..5e42188 100644 --- a/src/cmds.rs +++ b/src/cmds.rs @@ -73,11 +73,11 @@ pub fn exec_swayr_cmd( } fn focus_window_by_id(id: i64) { - util::swaymsg(&[format!("[con_id={}]", id).as_str(), "focus"]); + run_sway_command(&[format!("[con_id={}]", id).as_str(), "focus"]); } fn quit_window_by_id(id: i64) { - util::swaymsg(&[format!("[con_id={}]", id).as_str(), "kill"]); + run_sway_command(&[format!("[con_id={}]", id).as_str(), "kill"]); } fn get_tree() -> r::Node { @@ -163,7 +163,7 @@ pub fn switch_workspace(extra_props: Option<&HashMap>) { if let Some(workspace) = con::select_workspace("Switch to workspace", &workspaces) { - util::swaymsg(&["workspace", "number", workspace.get_name()]); + run_sway_command(&["workspace", "number", workspace.get_name()]); } } @@ -179,7 +179,7 @@ pub fn switch_workspace_or_window( ) { match ws_or_win { con::WsOrWin::Ws { ws } => { - util::swaymsg(&["workspace", "number", ws.get_name()]); + run_sway_command(&["workspace", "number", ws.get_name()]); } con::WsOrWin::Win { win } => focus_window_by_id(win.get_id()), } @@ -325,6 +325,19 @@ pub fn exec_swaymsg_command() { let cmds = get_swaymsg_commands(); let cmd = util::wofi_select("Execute swaymsg command", &cmds); if let Some(cmd) = cmd { - util::swaymsg(&cmd.cmd); + run_sway_command(&cmd.cmd); + } +} + +pub fn run_sway_command(args: &[&str]) { + let cmd = args.join(" "); + println!("Running sway command: {}", cmd); + match s::Connection::new() { + Ok(mut con) => { + if let Err(err) = con.run_command(cmd) { + eprintln!("Could not run sway command: {}", err) + } + } + Err(err) => panic!(err), } } diff --git a/src/demon.rs b/src/demon.rs index 5fc45bd..5417a8e 100644 --- a/src/demon.rs +++ b/src/demon.rs @@ -93,10 +93,12 @@ fn handle_window_event( match change { r::WindowChange::New | r::WindowChange::Focus => { update_last_focus_time(container.id, extra_props); + println!("Handled window event type {:?}", change); true } r::WindowChange::Close => { remove_extra_props(container.id, extra_props); + println!("Handled window event type {:?}", change); true } _ => false, @@ -120,6 +122,7 @@ fn handle_workspace_event( .id, extra_props, ); + println!("Handled workspace event type {:?}", change); true } r::WorkspaceChange::Empty => { @@ -127,7 +130,8 @@ fn handle_workspace_event( current.expect("No current in Empty workspace event").id, extra_props, ); - false + println!("Handled workspace event type {:?}", change); + true } _ => false, } diff --git a/src/util.rs b/src/util.rs index ba90366..17df8c0 100644 --- a/src/util.rs +++ b/src/util.rs @@ -19,16 +19,6 @@ pub fn get_swayr_socket_path() -> String { ) } -pub fn swaymsg(args: &[&str]) -> String { - let mut cmd = proc::Command::new("swaymsg"); - for a in args { - cmd.arg(a); - } - - let output = cmd.output().expect("Error running swaymsg!"); - String::from_utf8(output.stdout).unwrap() -} - pub fn wofi_select<'a, 'b, TS>( prompt: &'a str, choices: &'b [TS],