Run sway commands via IPC connection instead on shelling out and calling swaymsg

timeout_old
Tassilo Horn 4 years ago
parent dabd4e02a7
commit 44492a77e0
  1. 23
      src/cmds.rs
  2. 6
      src/demon.rs
  3. 10
      src/util.rs

@ -73,11 +73,11 @@ pub fn exec_swayr_cmd(
} }
fn focus_window_by_id(id: i64) { 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) { 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 { fn get_tree() -> r::Node {
@ -163,7 +163,7 @@ pub fn switch_workspace(extra_props: Option<&HashMap<i64, ipc::ExtraProps>>) {
if let Some(workspace) = if let Some(workspace) =
con::select_workspace("Switch to workspace", &workspaces) 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 { match ws_or_win {
con::WsOrWin::Ws { ws } => { 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()), 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 cmds = get_swaymsg_commands();
let cmd = util::wofi_select("Execute swaymsg command", &cmds); let cmd = util::wofi_select("Execute swaymsg command", &cmds);
if let Some(cmd) = cmd { 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),
} }
} }

@ -93,10 +93,12 @@ fn handle_window_event(
match change { match change {
r::WindowChange::New | r::WindowChange::Focus => { r::WindowChange::New | r::WindowChange::Focus => {
update_last_focus_time(container.id, extra_props); update_last_focus_time(container.id, extra_props);
println!("Handled window event type {:?}", change);
true true
} }
r::WindowChange::Close => { r::WindowChange::Close => {
remove_extra_props(container.id, extra_props); remove_extra_props(container.id, extra_props);
println!("Handled window event type {:?}", change);
true true
} }
_ => false, _ => false,
@ -120,6 +122,7 @@ fn handle_workspace_event(
.id, .id,
extra_props, extra_props,
); );
println!("Handled workspace event type {:?}", change);
true true
} }
r::WorkspaceChange::Empty => { r::WorkspaceChange::Empty => {
@ -127,7 +130,8 @@ fn handle_workspace_event(
current.expect("No current in Empty workspace event").id, current.expect("No current in Empty workspace event").id,
extra_props, extra_props,
); );
false println!("Handled workspace event type {:?}", change);
true
} }
_ => false, _ => false,
} }

@ -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>( pub fn wofi_select<'a, 'b, TS>(
prompt: &'a str, prompt: &'a str,
choices: &'b [TS], choices: &'b [TS],

Loading…
Cancel
Save