1
0
Fork 0
mirror of https://github.com/reisub0/sway-alttab synced 2024-05-13 02:36:05 +02:00

Initial commit

Signed-off-by: reisub0 <reisub0@gmail.com>
This commit is contained in:
reisub0 2019-12-08 14:37:39 +05:30
commit 230039ca55
No known key found for this signature in database
GPG Key ID: 58333CC586D729B4
3 changed files with 83 additions and 0 deletions

15
.gitignore vendored Normal file
View File

@ -0,0 +1,15 @@
/target
**/*.rs.bk
# create by https://github.com/iamcco/coc-gitignore (Sat Dec 07 2019 04:18:05 GMT+0530 (India Standard Time))
# Rust.gitignore:
# Generated by Cargo
# will have compiled files and executables
/target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk

11
Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "sway-alttab"
version = "0.1.0"
authors = ["reisub0 <reisub0@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
swayipc = {"version" = "^2.2.3", "features" = ["async"]}
signal-hook = {"version" = "^0.1"}

57
src/main.rs Normal file
View File

@ -0,0 +1,57 @@
use std::sync::{Arc, Mutex};
use swayipc::reply::Event::Window;
use swayipc::reply::WindowChange;
use swayipc::{block_on, Connection, EventType};
type Res<T> = std::result::Result<T, Box<dyn std::error::Error>>;
async fn get_current_focused_id() -> i64 {
Connection::new()
.await
.unwrap()
.get_tree()
.await
.unwrap()
.find_focused_as_ref(|n| n.focused)
.unwrap()
.id
}
fn handle_signal(last_focused: &Arc<Mutex<i64>>) {
block_on(async {
Connection::new()
.await
.unwrap()
.run_command(format!(
"[con_id={}] focus",
(*last_focused).lock().unwrap()
))
.await
.unwrap();
})
}
fn main() -> Res<()> {
block_on(async {
let subs = [EventType::Window];
let sub = Connection::new().await?;
let mut events = sub.subscribe(&subs).await?;
let last_focus = Arc::new(Mutex::new(0));
let mut cur_focus = get_current_focused_id().await;
let clone = Arc::clone(&last_focus);
unsafe { signal_hook::register(signal_hook::SIGUSR1, move || handle_signal(&clone))? };
loop {
let event = events.next().await?;
if let Window(ev) = event {
if ev.change == WindowChange::Focus {
let mut last = last_focus.lock().unwrap();
*last = cur_focus;
cur_focus = ev.container.id;
}
}
}
})
}