Files
swkb/src/main.rs

64 lines
2.1 KiB
Rust
Raw Normal View History

2020-07-28 15:54:05 +03:00
use std::collections::HashMap;
use swayipc::reply::Event;
use swayipc::{Connection, EventType, Fallible};
2020-11-14 10:29:15 -08:00
fn get_input_id(c: &mut Connection) -> Vec<String> {
2020-07-28 15:54:05 +03:00
let mut ids: Vec<String> = Vec::new();
2020-11-14 10:29:15 -08:00
match c.get_inputs() {
Ok(inputs) => {
for i in inputs {
ids.push(i.identifier);
2020-07-28 15:54:05 +03:00
}
}
2020-11-14 10:29:15 -08:00
_ => {}
2020-07-28 15:54:05 +03:00
}
2020-11-14 10:29:15 -08:00
ids
2020-07-28 15:54:05 +03:00
}
2020-11-14 10:29:15 -08:00
fn main() -> Fallible<()> {
let mut connection = Connection::new()?;
let inputs = get_input_id(&mut connection);
let mut layouts: HashMap<i64, i64> = HashMap::new();
2020-07-28 15:54:05 +03:00
let subs = [EventType::Input, EventType::Window];
2020-11-14 10:29:15 -08:00
let mut events = connection.subscribe(&subs)?;
let mut current_window: i64 = 0;
while let Some(event) = events.next() {
2020-07-29 17:21:22 +03:00
match event {
Ok(Event::Input(event)) => {
2020-07-28 15:54:05 +03:00
let layouts_list = event.input.xkb_layout_names;
2020-11-14 10:29:15 -08:00
let layout_name = event
.input
.xkb_active_layout_name
.unwrap_or("none".to_string());
2020-08-03 23:29:45 +03:00
if layout_name == "none" {
2020-11-14 10:29:15 -08:00
continue;
2020-08-03 23:29:45 +03:00
}
2020-11-14 10:29:15 -08:00
let index = layouts_list
.iter()
.position(|r| *r == layout_name)
.unwrap_or(0) as i64;
2020-07-28 15:54:05 +03:00
layouts.insert(current_window, index);
}
2020-11-14 10:29:15 -08:00
Ok(Event::Window(event)) => match event.change {
2020-07-28 15:54:05 +03:00
swayipc::reply::WindowChange::Focus => {
2020-11-14 10:29:15 -08:00
let mut connection = Connection::new()?;
2020-07-28 15:54:05 +03:00
for input in &inputs {
2020-11-14 10:29:15 -08:00
connection.run_command(format!(
2020-07-28 15:54:05 +03:00
"input {} xkb_switch_layout {}",
input,
2020-11-14 10:34:01 -08:00
layouts
.get_key_value(&event.container.id)
.unwrap_or((&0, &0))
.1
2020-11-14 10:29:15 -08:00
))?;
2020-07-28 15:54:05 +03:00
}
2020-11-14 10:29:15 -08:00
current_window = event.container.id;
2020-07-28 15:54:05 +03:00
}
_ => (),
},
_ => (),
}
}
unreachable!();
}