From 4bb2525cf62348125305599e2aa50fac72b22302 Mon Sep 17 00:00:00 2001 From: cyber-sushi Date: Mon, 15 Apr 2024 06:30:48 +0200 Subject: [PATCH] Added more diagnostics to stdout during initialization --- src/event_reader.rs | 4 ++-- src/udev_monitor.rs | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/event_reader.rs b/src/event_reader.rs index 38afebd..624d913 100644 --- a/src/event_reader.rs +++ b/src/event_reader.rs @@ -90,7 +90,7 @@ impl EventReader { } pub async fn start(&self) { - println!("{:?} detected, reading events.", self.config.get(&get_active_window(&self.current_desktop, &self.config).await).unwrap().name); + println!("{:?} detected, reading events.\n", self.config.get(&get_active_window(&self.current_desktop, &self.config).await).unwrap().name); tokio::join!( self.event_loop(), self.cursor_loop(), @@ -246,7 +246,7 @@ impl EventReader { } let mut device_is_connected = self.device_is_connected.lock().await; *device_is_connected = false; - println!("Disconnected device {}.", self.config.get(&get_active_window(&self.current_desktop, &self.config).await).unwrap().name); + println!("Disconnected device \"{}\".\n", self.config.get(&get_active_window(&self.current_desktop, &self.config).await).unwrap().name); } async fn convert_key_events(&self, event: InputEvent) { diff --git a/src/udev_monitor.rs b/src/udev_monitor.rs index 7e16321..c266242 100644 --- a/src/udev_monitor.rs +++ b/src/udev_monitor.rs @@ -1,4 +1,4 @@ -use std::{collections::{HashMap, BTreeMap}, sync::Arc, path::Path, env}; +use std::{collections::{HashMap, BTreeMap}, sync::Arc, path::Path, process::Command, env}; use tokio::sync::Mutex; use tokio::task::JoinHandle; use tokio_stream::StreamExt; @@ -9,14 +9,14 @@ use crate::event_reader::EventReader; pub async fn start_monitoring_udev(config_files: Vec, mut tasks: Vec>) { launch_tasks(&config_files, &mut tasks); - let mut monitor = tokio_udev::AsyncMonitorSocket::new( + let mut monitor = tokio_udev::AsyncMonitorSocket::new ( tokio_udev::MonitorBuilder::new().unwrap() .match_subsystem(std::ffi::OsStr::new("input")).unwrap() .listen().unwrap() ).unwrap(); while let Some(Ok(event)) = monitor.next().await { if is_mapped(&event.device(), &config_files) { - println!("Reinitializing..."); + println!("---------------------\nReinitializing...\n"); for task in &tasks { task.abort(); } @@ -66,7 +66,28 @@ pub fn launch_tasks(config_files: &Vec, tasks: &mut Vec>) }, _ => Option::None }; + let user_has_access = match Command::new("groups").output() { + Ok(groups) if std::str::from_utf8(&groups.stdout.as_slice()).unwrap().contains("input") => { + println!("Running with evdev permissions.\nScanning for event devices with a matching config file...\n"); //todo: make the config dir customizable through env variable + true + }, + Ok(groups) if std::str::from_utf8(&groups.stdout.as_slice()).unwrap().contains("root") => { + println!("Running as root.\nScanning for event devices with a matching config file...\n"); + true + } + Ok(groups) => { + println!("Warning: user has no access to event devices, Makima might not be able to detect all connected devices.\n\ + Note: use `sudo usermod -aG input ` and reboot. Alternatively, run Makima as root. Continuing...\n"); + println!("{:?}", &std::str::from_utf8(&groups.stdout.as_slice()).unwrap()); + false + }, + Err(_) => { + println!("Unable to determine if user has access to event devices. Continuing...\n"); + false + }, + }; let devices: evdev::EnumerateDevices = evdev::enumerate(); + let mut devices_found = 0; for device in devices { let mut config_map: HashMap = HashMap::new(); for config in config_files { @@ -86,8 +107,14 @@ pub fn launch_tasks(config_files: &Vec, tasks: &mut Vec>) let stream = Arc::new(Mutex::new(get_event_stream(Path::new(&event_device), config_map.clone()))); let reader = EventReader::new(config_map.clone(), stream, modifiers.clone(), current_desktop.clone()); tasks.push(tokio::spawn(start_reader(reader))); + devices_found += 1 } } + if devices_found == 0 && !user_has_access { + println!("No matching devices found.\nNote: make sure that your user has access to event devices.\n"); + } else if devices_found == 0 && user_has_access { + println!("No matching devices found.\nNote: double-check that your device and its respective config file have the same name, as reported by `evtest`.\n"); + } } pub async fn start_reader(reader: EventReader) {