Skip to content

Commit

Permalink
feat: add trace to rdev::grab (TO REVERT)
Browse files Browse the repository at this point in the history
  • Loading branch information
jersou committed Apr 9, 2023
1 parent 6dc4534 commit 9134766
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions rdev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,27 @@ categories = ["development-tools::testing", "api-bindings", "hardware-support"]
license = "MIT"

[dependencies]
serde = {version = "1.0", features = ["derive"], optional=true}
serde = { version = "1.0", features = ["derive"], optional = true }
lazy_static = "1.4"
log = "0.4.17"

[features]
serialize = ["serde"]
unstable_grab = ["evdev-rs", "epoll", "inotify"]

[target.'cfg(target_os = "macos")'.dependencies]
cocoa = "0.22"
core-graphics = {version = "0.19.0", features = ["highsierra"]}
core-foundation = {version = "0.7"}
core-foundation-sys = {version = "0.7"}
core-graphics = { version = "0.19.0", features = ["highsierra"] }
core-foundation = { version = "0.7" }
core-foundation-sys = { version = "0.7" }


[target.'cfg(target_os = "linux")'.dependencies]
libc = "0.2"
x11 = {version = "2.18", features = ["xlib", "xrecord", "xinput"]}
evdev-rs = {version = "0.4.0", optional=true}
epoll = {version = "4.1.0", optional=true}
inotify = {version = "0.8.2", default-features=false, optional=true}
x11 = { version = "2.18", features = ["xlib", "xrecord", "xinput"] }
evdev-rs = { version = "0.4.0", optional = true }
epoll = { version = "4.1.0", optional = true }
inotify = { version = "0.8.2", default-features = false, optional = true }

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3", features = ["winuser", "errhandlingapi", "processthreadsapi"] }
Expand All @@ -44,7 +45,7 @@ serde_json = "1.0"
# because that leads to unexpected behavior and flaky tests, so we need
# to run thoses tests in sequence instead.
serial_test = "0.4"
tokio = {version = "1.5", features=["sync", "macros", "rt-multi-thread"]}
tokio = { version = "1.5", features = ["sync", "macros", "rt-multi-thread"] }

[[example]]
name = "serialize"
Expand Down
46 changes: 41 additions & 5 deletions rdev/src/linux/grab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use evdev_rs::{
Device, InputEvent, UInputDevice,
};
use inotify::{Inotify, WatchMask};
use log::{error, trace};

use crate::linux::common::Display;
use crate::linux::keyboard::Keyboard;
Expand Down Expand Up @@ -355,18 +356,23 @@ where
// TODO detecter perm err, supp des device
'root_loop: loop {
eprintln!("root_loop");
trace!("setup_devices");
let (epoll_fd, mut devices, output_devices) = setup_devices()?;
trace!("setup_inotify");
let mut inotify = setup_inotify(epoll_fd, &devices)?;

//grab devices
let _grab = devices
.iter_mut()
.try_for_each(|device| device.grab(evdev_rs::GrabMode::Grab))?;
trace!("grab devices");
let _grab = devices.iter_mut().try_for_each(|device| {
trace!("grab devices: {:?}", device.fd());
device.grab(evdev_rs::GrabMode::Grab)
})?;

// create buffer for epoll to fill
let mut epoll_buffer = [epoll::Event::new(epoll::Events::empty(), 0); 4];
let mut inotify_buffer = vec![0_u8; 4096];
'event_loop: loop {
trace!("epoll::wait");
let num_events = epoll::wait(epoll_fd, -1, &mut epoll_buffer)?;

//map and simulate events, dealing with
Expand Down Expand Up @@ -398,6 +404,7 @@ where
Err(_) => {
let device_fd = device.fd().unwrap().into_raw_fd();
let empty_event = epoll::Event::new(epoll::Events::empty(), 0);
trace!("epoll::ctl");
epoll::ctl(epoll_fd, EPOLL_CTL_DEL, device_fd, empty_event)?;
continue 'events;
}
Expand All @@ -407,6 +414,7 @@ where
if let (Some(event), Some(out_device)) =
(event, output_devices.get(device_idx))
{
trace!("out_device.write_event");
out_device.write_event(&event)?;
}
if grab_status == GrabStatus::Stop {
Expand Down Expand Up @@ -446,9 +454,12 @@ where
T: AsRef<Path>,
{
let mut res = Vec::new();
trace!("get_device_files: read_dir");
for entry in read_dir(path)? {
trace!("get_device_files: entry");
let entry = entry?;
// /dev/input files are character devices
trace!("get_device_files: entry.file_type()");
if !entry.file_type()?.is_char_device() {
continue;
}
Expand All @@ -473,7 +484,10 @@ where
{
continue;
}
res.push(File::open(path)?);
trace!("get_device_files: File::open({})", path.display());
let file = File::open(&path);
trace!("file open of {} : {:?}", path.display(), file);
res.push(file?);
}
Ok(res)
}
Expand Down Expand Up @@ -520,15 +534,37 @@ fn add_device_to_epoll_from_inotify_event(
/// a libevdev copy of its corresponding device.The epoll_fd is level-triggered
/// on any available data in the original devices.
fn setup_devices() -> io::Result<(RawFd, Vec<Device>, Vec<UInputDevice>)> {
trace!("setup_devices: get_device_files");
let device_files = get_device_files(DEV_PATH)?;
trace!("setup_devices: epoll_watch_all");
let epoll_fd = epoll_watch_all(device_files.iter())?;
trace!("setup_devices: iter Device::new_from_fd");
let devices = device_files
.into_iter()
.map(Device::new_from_fd)
.collect::<io::Result<Vec<Device>>>()?;
trace!("setup_devices: create_from_device");
let output_devices = devices
.iter()
.map(|device| UInputDevice::create_from_device(device))
.map(|device| {
let res = UInputDevice::create_from_device(device);
if let Err(error) = &res {
error!(
"setup_devices: create_from_device: {} {:?} : {:?}",
device.name().unwrap_or_default(),
device.fd().unwrap(),
error
);
} else {
trace!(
"setup_devices: create_from_device: {} {:?} : OK",
device.name().unwrap_or_default(),
device.fd().unwrap(),
);
}
res
})
// .filter(|d| d.is_ok())
.collect::<io::Result<Vec<UInputDevice>>>()?;
Ok((epoll_fd, devices, output_devices))
}
Expand Down

0 comments on commit 9134766

Please sign in to comment.