diff --git a/src/event_reader.rs b/src/event_reader.rs index a6c8288..c2c7778 100644 --- a/src/event_reader.rs +++ b/src/event_reader.rs @@ -22,7 +22,6 @@ impl EventReader { pub fn new( config: HashMap, stream: Arc>, - virt_dev: Arc>, modifiers: Arc>>, current_desktop: Option, ) -> Self { @@ -30,6 +29,7 @@ impl EventReader { for i in [0, 0] {position_vector.push(i)}; let position_vector_mutex = Arc::new(Mutex::new(position_vector)); let device_is_connected: Arc> = Arc::new(Mutex::new(true)); + let virt_dev = Arc::new(Mutex::new(VirtualDevices::new())); Self { config: config, stream: stream, @@ -42,6 +42,14 @@ impl EventReader { } pub async fn start(&self) { + println!("{:?} detected, reading events.", self.config.get(&self.get_active_window().await).unwrap().name); + tokio::join!( + self.event_loop(), + self.cursor_loop(), + ); + } + + pub async fn event_loop(&self) { let mut stream = self.stream.lock().await; let mut analog_mode: &str = "left"; if let Some(stick) = self.config.get(&self.get_active_window().await).unwrap().settings.get("POINTER_STICK") { @@ -107,6 +115,7 @@ impl EventReader { } let mut device_is_connected = self.device_is_connected.lock().await; *device_is_connected = false; + println!("Disconnected device {}.", self.config.get(&self.get_active_window().await).unwrap().name); } async fn convert_key_events(&self, event: InputEvent) { diff --git a/src/udev_monitor.rs b/src/udev_monitor.rs index 41beafe..e2b1a0d 100644 --- a/src/udev_monitor.rs +++ b/src/udev_monitor.rs @@ -5,7 +5,6 @@ use tokio_stream::StreamExt; use evdev::{Device, EventStream, Key}; use crate::Config; use crate::event_reader::EventReader; -use crate::virtual_devices::VirtualDevices; pub async fn start_monitoring_udev(config_files: Vec, mut tasks: Vec>) { @@ -43,17 +42,17 @@ pub fn launch_tasks(config_files: &Vec, tasks: &mut Vec>) ); let current_desktop: Option = match env::var("XDG_CURRENT_DESKTOP") { Ok(desktop) if vec!["Hyprland".to_string(), "sway".to_string()].contains(&desktop) => { - println!("Running on {}, active window detection enabled.", desktop); + println!(">> Running on {}, active window detection enabled.\n", desktop); Option::Some(desktop) }, Ok(desktop) => { - println!("Unsupported desktop: {}, won't be able to change bindings according to active window.\n - Currently supported desktops: Hyprland, Sway.", desktop); + println!(">> Unsupported desktop: {}, won't be able to change bindings according to active window.\n + Currently supported desktops: Hyprland, Sway.\n", desktop); Option::None }, Err(_) => { - println!("Unable to retrieve the current desktop based on XDG_CURRENT_DESKTOP env var.\n - Won't be able to change bindings according to the active window."); + println!(">> Unable to retrieve the current desktop based on XDG_CURRENT_DESKTOP env var.\n + Won't be able to change bindings according to the active window.\n"); Option::None }, }; @@ -72,38 +71,17 @@ pub fn launch_tasks(config_files: &Vec, tasks: &mut Vec>) config_map.insert(window_class, config.clone()); }; } + let event_device = device.0.as_path().to_str().unwrap().to_string(); if !config_map.is_empty() { - tasks.push( - tokio::spawn( - create_new_reader( - device.0.as_path().to_str().unwrap().to_string(), - config_map.clone(), - modifiers.clone(), - current_desktop.clone(), - ) - ) - ) + 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))); } } } -pub async fn create_new_reader(device: String, config: HashMap, modifiers: Arc>>, current_desktop: Option) { - let stream: Arc> = Arc::new ( - Mutex::new ( - get_event_stream ( - Path::new(&device), - config.clone() - ) - ) - ); - let virt_dev: Arc> = Arc::new(Mutex::new(VirtualDevices::new())); - let reader = EventReader::new(config.clone(), stream, virt_dev, modifiers, current_desktop); - println!("Mapped device detected at {:?}, reading events.", device); - tokio::join!( - reader.start(), - reader.cursor_loop(), - ); - println!("Disconnected device at {}.", device); +pub async fn start_reader(reader: EventReader) { + reader.start().await; } pub fn get_event_stream(path: &Path, config: HashMap) -> EventStream {