Skip to content

Commit

Permalink
small clean up, for some reason virtual device doesn't work on laptop…
Browse files Browse the repository at this point in the history
…/fedora
  • Loading branch information
AethanFoot committed Mar 10, 2024
1 parent 30352bb commit 318f0e6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 46 deletions.
15 changes: 3 additions & 12 deletions lefthk-core/src/evdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::errors::{self, LeftError};

#[derive(Debug)]
pub enum Task {
KeyboardEvent((PathBuf, InputEvent)),
KeyboardEvent(InputEvent),
KeyboardAdded(PathBuf),
KeyboardRemoved(PathBuf),
}
Expand Down Expand Up @@ -43,7 +43,7 @@ impl Default for EvDev {

let builder = errors::exit!(VirtualDeviceBuilder::new());

let mut device = builder
let device = builder
.name("LeftHK Virtual Keyboard")
.input_id(InputId::new(BusType::BUS_I8042, 1, 1, 1))
.with_keys(&keys)
Expand All @@ -52,10 +52,6 @@ impl Default for EvDev {
.unwrap()
.build()
.unwrap();
println!("Device: {:?}", device.get_syspath());

let devnode = device.enumerate_dev_nodes_blocking().unwrap().next();
println!("Devnode: {:?}", devnode);

let (task_transmitter, task_receiver) = mpsc::channel(128);

Expand Down Expand Up @@ -97,19 +93,14 @@ impl EvDev {
let transmitter = self.task_transmitter.clone();

let mut stream = errors::r#return!(device.into_event_stream());
let p = path.clone();
tokio::task::spawn(async move {
while !guard.is_closed() {
match stream.next_event().await {
Ok(event) => {
transmitter
.send(Task::KeyboardEvent((p.clone(), event)))
.await
.unwrap();
transmitter.send(Task::KeyboardEvent(event)).await.unwrap();
}
Err(err) => {
tracing::warn!("Evdev device stream failed with {:?}", err);
// poll_fn(|cx| guard.poll_closed(cx)).await;
break;
}
}
Expand Down
39 changes: 5 additions & 34 deletions lefthk-core/src/worker/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
pub mod context;

use std::path::PathBuf;

use crate::child::Children;
use crate::config::{command, Keybind};
use crate::errors::{self, LeftError};
use crate::evdev::{EvDev, Task};
use crate::ipc::Pipe;
use crate::keysym_lookup::{self, is_modifier, MOD_MASK};
use evdev::{EventType, InputEvent, InputEventKind, Key};
use evdev::{InputEvent, InputEventKind, Key};
use xdg::BaseDirectories;

#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -83,8 +81,8 @@ impl Worker {
}
Some(task) = self.evdev.task_receiver.recv() => {
match task {
Task::KeyboardEvent((path, event)) => {
self.handle_event(path, event);
Task::KeyboardEvent(event) => {
self.handle_event(event);
}
Task::KeyboardAdded(path) => {
self.evdev.add_device(path);
Expand All @@ -109,7 +107,7 @@ impl Worker {
errors::exit!(Pipe::new(pipe_file).await)
}

fn handle_event(&mut self, path: PathBuf, event: InputEvent) {
fn handle_event(&mut self, event: InputEvent) {
let r#type = KeyEventType::from(event.value());
let mut eaten = false;
match r#type {
Expand Down Expand Up @@ -168,35 +166,8 @@ impl Worker {
KeyEventType::Repeat | KeyEventType::Unknown => {}
}
if !eaten {
self.pass_event(path, event);
}
}

fn pass_event(&mut self, _path: PathBuf, event: InputEvent) {
match self.evdev.device.emit(&[event]) {
Ok(_) => {
tracing::debug!("Successfully sent event: {:?}", event);
}
Err(err) => {
tracing::warn!("Failed to pass event: {:?}", err);
}
let _ = self.evdev.device.emit(&[event]);
}
let code = Key::KEY_K.code();
let down_event = InputEvent::new(EventType::KEY, code, 1);
self.evdev.device.emit(&[down_event]).unwrap();
println!("Pressed.");
// sleep(Duration::from_secs(2));

// alternativeley we can create a InputEvent, which will be any variant of InputEvent
// depending on the type_ value
let up_event = InputEvent::new(EventType::KEY, code, 0);
self.evdev.device.emit(&[up_event]).unwrap();
println!("Released.");
// sleep(Duration::from_secs(2));
// match self.evdev.devices.get(&path) {
// Some(device) => errors::log!(device.write_event(event)),
// None => errors::log!(Err(LeftError::UInputNotFound)),
// }
}

fn check_for_keybind(&self) -> Option<Keybind> {
Expand Down

0 comments on commit 318f0e6

Please # to comment.