Skip to content

Commit

Permalink
logging added, permission checks added, basic error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinyzenith committed Jan 5, 2022
1 parent 328c310 commit c44b634
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 13 deletions.
144 changes: 137 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
[package]
name = "swhkd"
name = "Simple-Wayland-HotKey-Daemon"
version = "0.1.0"
edition = "2021"

[dependencies]
evdev = "0.11.3"
glob = "0.3.0"
log = "0.4.0"
env_logger = "0.8.4"
nix = "0.23.1"


clap = "3.0.4"
tokio = { version = "1.15.0", features = ["full"] }

[[bin]]
Expand Down
51 changes: 46 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,69 @@
use evdev::Device;
use evdev::Key;
use glob::glob;
use nix::unistd;
use std::process::exit;

pub fn main() {
std::env::set_var("RUST_LOG", "trace");
env_logger::init();
log::trace!("Logger initialized.");

if unistd::Uid::current().is_root() {
log::error!("Refusing to run swhkd as root.");
exit(1);
}

log::trace!("Checking if invoking user is in input group or not.");
let groups = unistd::getgroups();
let mut is_in_input_group: bool = false;
'init_loop: for (_, groups) in groups.iter().enumerate() {
for group in groups {
let group = unistd::Group::from_gid(*group);
if group.unwrap().unwrap().name == "input" {
log::debug!("Invoking user is in input group.");
is_in_input_group = true;
break 'init_loop;
}
}
}

if is_in_input_group == false {
log::error!("Invoking user is NOT in input group.");
exit(1);
}

log::trace!("Attempting to find all keyboard file descriptors.");
for entry in glob("/dev/input/event*").expect("Failed to read /dev/input/event*.") {
match entry {
Ok(path) => {
check_keyboard(path.into_os_string().into_string().unwrap());
}
Err(_) => continue,
Err(error) => log::error!("Unexpected error occured: {}", error),
}
}
}

fn check_keyboard(input_path: String) -> bool {
let device = Device::open(&input_path).expect("Failed to open device.");
pub fn check_keyboard(input_path: String) -> bool {
/* Try to open the device fd. */
let device = Device::open(&input_path);
match &device {
Ok(_) => (),
Err(error) => {
log::error!("Failed to open device {}: {}", input_path, error);
}
}

/* Check for the presence of enter key. */
if device
.unwrap()
.supported_keys()
.map_or(false, |keys| keys.contains(Key::KEY_ENTER))
{
println!("{} is a keyboard.", input_path);
log::debug!("{} is a keyboard.", input_path);
return true;
} else {
println!("{} is not a keyboard.", input_path);
log::debug!("{} is not a keyboard.", input_path);
return false;
}
}

0 comments on commit c44b634

Please sign in to comment.