You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
4.0 KiB
128 lines
4.0 KiB
3 years ago
|
use clap::{arg, App};
|
||
2 years ago
|
use evdev::{Device, Key};
|
||
3 years ago
|
use nix::unistd;
|
||
3 years ago
|
use std::env;
|
||
|
use std::path::Path;
|
||
3 years ago
|
use std::process::exit;
|
||
3 years ago
|
|
||
|
pub fn main() {
|
||
2 years ago
|
let args = set_flags().get_matches();
|
||
2 years ago
|
env::set_var("RUST_LOG", "swhkd=warn");
|
||
|
|
||
3 years ago
|
if args.is_present("debug") {
|
||
|
env::set_var("RUST_LOG", "swhkd=trace");
|
||
|
}
|
||
|
|
||
3 years ago
|
env_logger::init();
|
||
|
log::trace!("Logger initialized.");
|
||
|
|
||
2 years ago
|
if !permission_check() {
|
||
3 years ago
|
exit(1);
|
||
|
}
|
||
|
|
||
3 years ago
|
/* Get appropriate config file path */
|
||
3 years ago
|
let config_file_path: std::path::PathBuf;
|
||
3 years ago
|
if args.is_present("config") {
|
||
3 years ago
|
config_file_path = Path::new(args.value_of("config").unwrap()).to_path_buf();
|
||
3 years ago
|
} else {
|
||
2 years ago
|
config_file_path = check_config_xdg();
|
||
3 years ago
|
}
|
||
3 years ago
|
log::debug!("Using config file path: {:#?}", config_file_path);
|
||
3 years ago
|
|
||
2 years ago
|
if !config_file_path.exists() {
|
||
|
log::error!("{:#?} doesn't exist", config_file_path);
|
||
|
exit(1);
|
||
|
}
|
||
|
|
||
3 years ago
|
log::trace!("Attempting to find all keyboard file descriptors.");
|
||
2 years ago
|
let mut keyboard_devices: Vec<Device> = Vec::new();
|
||
2 years ago
|
for (_, device) in evdev::enumerate().enumerate() {
|
||
2 years ago
|
if check_keyboard(&device) == true {
|
||
|
keyboard_devices.push(device);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if keyboard_devices.len() == 0 {
|
||
|
log::error!("No valid keyboard device was detected!");
|
||
|
exit(1);
|
||
3 years ago
|
}
|
||
2 years ago
|
log::debug!("{} Keyboard device(s) detected.", keyboard_devices.len());
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
pub fn permission_check() -> bool {
|
||
3 years ago
|
let groups = unistd::getgroups();
|
||
|
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.");
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
|
if unistd::Uid::current().is_root() {
|
||
|
log::warn!("Running swhkd as root!!!");
|
||
|
return true;
|
||
|
} else {
|
||
|
log::error!("Invoking user is NOT in input group.");
|
||
|
return false;
|
||
|
}
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
pub fn check_keyboard(device: &Device) -> bool {
|
||
3 years ago
|
/* Check for the presence of enter key. */
|
||
2 years ago
|
if device.supported_keys().map_or(false, |keys| keys.contains(Key::KEY_ENTER)) {
|
||
|
log::debug!(
|
||
|
"{} ({}) is a keyboard.",
|
||
|
device.name().unwrap(),
|
||
|
device.physical_path().unwrap()
|
||
|
);
|
||
3 years ago
|
return true;
|
||
|
} else {
|
||
2 years ago
|
log::trace!(
|
||
|
"{} ({}) is not a keyboard.",
|
||
|
device.name().unwrap(),
|
||
|
device.physical_path().unwrap()
|
||
|
);
|
||
3 years ago
|
return false;
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
|
pub fn set_flags() -> App<'static> {
|
||
|
let app = App::new("swhkd")
|
||
|
.version(env!("CARGO_PKG_VERSION"))
|
||
|
.author(env!("CARGO_PKG_AUTHORS"))
|
||
|
.about("Simple Wayland HotKey Daemon")
|
||
|
.arg(
|
||
|
arg!(-c --config <CONFIG_FILE_PATH>)
|
||
|
.required(false)
|
||
|
.help("Set a custom config file path"),
|
||
|
)
|
||
|
.arg(arg!(-d - -debug).required(false).help("Enable debug mode"));
|
||
|
return app;
|
||
|
}
|
||
|
|
||
|
pub fn check_config_xdg() -> std::path::PathBuf {
|
||
|
let config_file_path: std::path::PathBuf;
|
||
|
match env::var("XDG_CONFIG_HOME") {
|
||
|
Ok(val) => {
|
||
|
config_file_path = Path::new(&val).join("swhkd/swhkdrc");
|
||
|
log::debug!("XDG_CONFIG_HOME exists: {:#?}", val);
|
||
|
return config_file_path;
|
||
|
}
|
||
|
Err(_) => {
|
||
|
log::error!("XDG_CONFIG_HOME has not been set.");
|
||
2 years ago
|
config_file_path = Path::new("/etc/swhkd/swhkdrc").to_path_buf();
|
||
|
log::warn!(
|
||
|
"Note: Due to the design of the application, the invoking user is always root."
|
||
|
);
|
||
2 years ago
|
log::warn!("You can set a custom config file with the -c option.");
|
||
|
log::warn!("Adding your user to the input group could solve this.");
|
||
|
log::warn!("However that's a massive security flaw and basically defeats the purpose of using wayland.");
|
||
|
log::warn!("The following issue may be addressed in the future, but it is certainly not a priority right now.");
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
return config_file_path;
|
||
2 years ago
|
}
|