Implement SwayClient

This commit is contained in:
Takashi Kokubun 2021-12-20 21:11:22 -08:00
parent d0bffa8c2f
commit 60e6ada024
No known key found for this signature in database
GPG Key ID: 6FFC433B12EE23DD
6 changed files with 138 additions and 0 deletions

58
Cargo.lock generated
View File

@ -80,6 +80,12 @@ dependencies = [
"hashbrown", "hashbrown",
] ]
[[package]]
name = "itoa"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@ -176,6 +182,17 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "serde_json"
version = "1.0.73"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcbd0344bc6533bc7ec56df11d42fb70f1b912351c0825ccb7211b59d8af7cf5"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]] [[package]]
name = "serde_yaml" name = "serde_yaml"
version = "0.8.23" version = "0.8.23"
@ -188,6 +205,26 @@ dependencies = [
"yaml-rust", "yaml-rust",
] ]
[[package]]
name = "swayipc"
version = "3.0.0-alpha.3"
source = "git+https://github.com/k0kubun/swayipc-rs#580b5b6e02b911547c4741bfe240ea8d81d1147c"
dependencies = [
"serde",
"serde_json",
"swayipc-types",
]
[[package]]
name = "swayipc-types"
version = "1.0.0-alpha.3"
source = "git+https://github.com/k0kubun/swayipc-rs#580b5b6e02b911547c4741bfe240ea8d81d1147c"
dependencies = [
"serde",
"serde_json",
"thiserror",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.82" version = "1.0.82"
@ -205,6 +242,26 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "thiserror"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "unicode-width" name = "unicode-width"
version = "0.1.9" version = "0.1.9"
@ -246,6 +303,7 @@ dependencies = [
"nix", "nix",
"serde", "serde",
"serde_yaml", "serde_yaml",
"swayipc",
"x11", "x11",
] ]

View File

@ -8,6 +8,7 @@ edition = "2021"
[dependencies] [dependencies]
evdev = "0.11.3" evdev = "0.11.3"
getopts = "0.2" getopts = "0.2"
swayipc = { git = "https://github.com/k0kubun/swayipc-rs" }
lazy_static = "1.4.0" lazy_static = "1.4.0"
nix = "0.23.1" nix = "0.23.1"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }

View File

@ -109,6 +109,9 @@ keymap:
'3': C-M-3 '3': C-M-3
'0': C-M-0 '0': C-M-0
# workaround
C-c: Esc
- name: Chrome, Slack (modified from Default) - name: Chrome, Slack (modified from Default)
wm_class: wm_class:
only: [Google-chrome, Slack] only: [Google-chrome, Slack]

View File

@ -1 +1,2 @@
pub mod x11_client; pub mod x11_client;
pub mod sway_client;

70
src/client/sway_client.rs Normal file
View File

@ -0,0 +1,70 @@
use std::fs::read_dir;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::net::UnixStream;
use swayipc::Connection;
pub struct SwayClient {
connection: Option<Connection>,
supported: Option<bool>,
}
impl SwayClient {
pub fn new() -> SwayClient {
SwayClient {
connection: None,
supported: None,
}
}
pub fn supported(&mut self) -> bool {
match self.supported {
Some(supported) => supported,
None => {
self.supported = Some(false);
if let Some(socket) = find_socket() {
if let Ok(unix_stream) = UnixStream::connect(socket) {
self.connection = Some(Connection(unix_stream));
self.supported = Some(true);
}
}
self.supported.unwrap()
}
}
}
pub fn current_wm_class(&mut self) -> Option<String> {
if !self.supported() {
return None;
}
let connection = match &mut self.connection {
Some(connection) => connection,
None => return None,
};
if let Ok(node) = connection.get_tree() {
if let Some(node) = node.find_focused(|n| n.focused) {
return node.app_id
}
}
None
}
}
// e.g. "/run/user/1000/sway-ipc.1000.2575.sock"
fn find_socket() -> Option<String> {
let uid = 1000; // Assume a first nornal Linux user
if let Some(run_user) = read_dir(format!("/run/user/{}", uid)).as_mut().ok() {
while let Some(entry) = run_user.next() {
let path = entry.ok()?.path();
if let Some(fname) = path.file_name() {
if fname.as_bytes().starts_with(b"sway-ipc.") {
if let Ok(path) = path.into_os_string().into_string() {
return Some(path)
}
}
}
}
}
None
}

View File

@ -3,6 +3,7 @@ use crate::input::{event_loop, select_device};
use getopts::Options; use getopts::Options;
use std::env; use std::env;
use std::process::exit; use std::process::exit;
use crate::client::sway_client::SwayClient;
extern crate getopts; extern crate getopts;
@ -23,6 +24,10 @@ fn abort(message: &str) -> ! {
} }
fn main() { fn main() {
let mut client = SwayClient::new();
println!("supported: {}", client.supported());
println!("active: {:?}", client.current_wm_class());
let argv: Vec<String> = env::args().collect(); let argv: Vec<String> = env::args().collect();
let program = argv[0].clone(); let program = argv[0].clone();