Initial version built with xlib

This commit is contained in:
Jack Yu 2023-05-22 02:18:16 -07:00
commit e8966ab00b
4 changed files with 186 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

92
Cargo.lock generated Normal file
View File

@ -0,0 +1,92 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cc"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
[[package]]
name = "errno"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1"
dependencies = [
"errno-dragonfly",
"libc",
"winapi",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "exec"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "886b70328cba8871bfc025858e1de4be16b1d5088f2ba50b57816f4210672615"
dependencies = [
"errno",
"libc",
]
[[package]]
name = "libc"
version = "0.2.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1"
[[package]]
name = "pkg-config"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
[[package]]
name = "superl_launch"
version = "1.0.0"
dependencies = [
"exec",
"x11",
]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "x11"
version = "2.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "502da5464ccd04011667b11c435cb992822c2c0dbde1770c988480d312a0db2e"
dependencies = [
"libc",
"pkg-config",
]

10
Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "superl_launch"
version = "1.0.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
exec = "0.3"
x11 = { version = "2.21", features = ["xlib"]}

83
src/main.rs Normal file
View File

@ -0,0 +1,83 @@
extern crate exec;
extern crate x11;
use std::env;
use std::process;
use std::ptr;
const KEYID_LSUPER: u32 = 130;
fn usage(prog: &String) -> () {
println!("usage: {prog} [cmd...]");
println!("Launch command if only Left Super is pressed then released.");
}
fn x11_open_display() -> Option<*mut x11::xlib::Display> {
unsafe {
let display = x11::xlib::XOpenDisplay(ptr::null());
if display == ptr::null_mut() {
return None;
}
return Some(display);
}
}
fn x11_close_display(display: *mut x11::xlib::Display) {
unsafe {
x11::xlib::XCloseDisplay(display);
}
}
fn x11_check_lsuper() -> Option<bool> {
match x11_open_display() {
Some(display) => {
loop {
let mut keymap: [i8; 32] = [0; 32];
unsafe {
x11::xlib::XQueryKeymap(display, keymap.as_mut_ptr());
}
let (count, keyid, _) : (u32, u32, bool) = keymap.iter().fold(
(0, 0, false), |x, y| match x {
(c, k, s) => (c + y.count_ones(),
if s {k} else {k + y.leading_zeros()},
if s {s} else {y.leading_zeros() < 8}),
});
if count == 0 {
x11_close_display(display);
return Some(true);
}
if count > 1 || keyid != KEYID_LSUPER {
x11_close_display(display);
return Some(false);
}
}
},
None => {
println!("Error: can't open display");
return None;
}
}
}
fn single_release_launch(cmd: &String, argv: &[String]) -> () {
match x11_check_lsuper() {
Some(ret) => {
if ret == true {
println!("Error: {}", exec::Command::new(cmd).args(argv).exec());
}
},
None => (),
}
}
fn main() -> () {
let args: Vec<String> = env::args().collect();
match &args[..] {
[prog] => usage(prog),
[_, cmd] => single_release_launch(cmd, &[]),
[_, cmd, argv @ ..] => single_release_launch(cmd, argv),
_ => (),
}
process::exit(1);
}