stdout now shows device names

This commit is contained in:
cyber-sushi 2024-01-06 01:18:31 +01:00
parent 965d9acd25
commit 9965e71c2f
2 changed files with 21 additions and 34 deletions

View file

@ -22,7 +22,6 @@ impl EventReader {
pub fn new( pub fn new(
config: HashMap<String, Config>, config: HashMap<String, Config>,
stream: Arc<Mutex<EventStream>>, stream: Arc<Mutex<EventStream>>,
virt_dev: Arc<Mutex<VirtualDevices>>,
modifiers: Arc<Mutex<BTreeMap<Key, i32>>>, modifiers: Arc<Mutex<BTreeMap<Key, i32>>>,
current_desktop: Option<String>, current_desktop: Option<String>,
) -> Self { ) -> Self {
@ -30,6 +29,7 @@ impl EventReader {
for i in [0, 0] {position_vector.push(i)}; for i in [0, 0] {position_vector.push(i)};
let position_vector_mutex = Arc::new(Mutex::new(position_vector)); let position_vector_mutex = Arc::new(Mutex::new(position_vector));
let device_is_connected: Arc<Mutex<bool>> = Arc::new(Mutex::new(true)); let device_is_connected: Arc<Mutex<bool>> = Arc::new(Mutex::new(true));
let virt_dev = Arc::new(Mutex::new(VirtualDevices::new()));
Self { Self {
config: config, config: config,
stream: stream, stream: stream,
@ -42,6 +42,14 @@ impl EventReader {
} }
pub async fn start(&self) { 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 stream = self.stream.lock().await;
let mut analog_mode: &str = "left"; let mut analog_mode: &str = "left";
if let Some(stick) = self.config.get(&self.get_active_window().await).unwrap().settings.get("POINTER_STICK") { 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; let mut device_is_connected = self.device_is_connected.lock().await;
*device_is_connected = false; *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) { async fn convert_key_events(&self, event: InputEvent) {

View file

@ -5,7 +5,6 @@ use tokio_stream::StreamExt;
use evdev::{Device, EventStream, Key}; use evdev::{Device, EventStream, Key};
use crate::Config; use crate::Config;
use crate::event_reader::EventReader; use crate::event_reader::EventReader;
use crate::virtual_devices::VirtualDevices;
pub async fn start_monitoring_udev(config_files: Vec<Config>, mut tasks: Vec<JoinHandle<()>>) { pub async fn start_monitoring_udev(config_files: Vec<Config>, mut tasks: Vec<JoinHandle<()>>) {
@ -43,17 +42,17 @@ pub fn launch_tasks(config_files: &Vec<Config>, tasks: &mut Vec<JoinHandle<()>>)
); );
let current_desktop: Option<String> = match env::var("XDG_CURRENT_DESKTOP") { let current_desktop: Option<String> = match env::var("XDG_CURRENT_DESKTOP") {
Ok(desktop) if vec!["Hyprland".to_string(), "sway".to_string()].contains(&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) Option::Some(desktop)
}, },
Ok(desktop) => { Ok(desktop) => {
println!("Unsupported desktop: {}, won't be able to change bindings according to active window.\n println!(">> Unsupported desktop: {}, won't be able to change bindings according to active window.\n
Currently supported desktops: Hyprland, Sway.", desktop); Currently supported desktops: Hyprland, Sway.\n", desktop);
Option::None Option::None
}, },
Err(_) => { Err(_) => {
println!("Unable to retrieve the current desktop based on XDG_CURRENT_DESKTOP env var.\n 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."); Won't be able to change bindings according to the active window.\n");
Option::None Option::None
}, },
}; };
@ -72,38 +71,17 @@ pub fn launch_tasks(config_files: &Vec<Config>, tasks: &mut Vec<JoinHandle<()>>)
config_map.insert(window_class, config.clone()); config_map.insert(window_class, config.clone());
}; };
} }
let event_device = device.0.as_path().to_str().unwrap().to_string();
if !config_map.is_empty() { if !config_map.is_empty() {
tasks.push( let stream = Arc::new(Mutex::new(get_event_stream(Path::new(&event_device), config_map.clone())));
tokio::spawn( let reader = EventReader::new(config_map.clone(), stream, modifiers.clone(), current_desktop.clone());
create_new_reader( tasks.push(tokio::spawn(start_reader(reader)));
device.0.as_path().to_str().unwrap().to_string(),
config_map.clone(),
modifiers.clone(),
current_desktop.clone(),
)
)
)
} }
} }
} }
pub async fn create_new_reader(device: String, config: HashMap<String, Config>, modifiers: Arc<Mutex<BTreeMap<Key, i32>>>, current_desktop: Option<String>) { pub async fn start_reader(reader: EventReader) {
let stream: Arc<Mutex<EventStream>> = Arc::new ( reader.start().await;
Mutex::new (
get_event_stream (
Path::new(&device),
config.clone()
)
)
);
let virt_dev: Arc<Mutex<VirtualDevices>> = 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 fn get_event_stream(path: &Path, config: HashMap<String, Config>) -> EventStream { pub fn get_event_stream(path: &Path, config: HashMap<String, Config>) -> EventStream {