-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint_keys.rs
71 lines (61 loc) · 2.12 KB
/
print_keys.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use colpetto::{
event::{AsRawEvent, DeviceEvent, KeyboardEvent},
Event, Libinput, Result,
};
use rustix::{
fd::{FromRawFd, IntoRawFd, OwnedFd},
fs::{open, Mode, OFlags},
};
use std::{
ffi::{c_int, CStr},
os::fd::RawFd,
path::Path,
};
use tokio_stream::StreamExt;
fn open_restricted(path: &CStr, flags: c_int) -> Result<RawFd, c_int> {
open(path, OFlags::from_bits_retain(flags as u32), Mode::empty())
.map(IntoRawFd::into_raw_fd)
.map_err(|err| err.raw_os_error().wrapping_neg())
}
fn close_restricted(fd: RawFd) {
drop(unsafe { OwnedFd::from_raw_fd(fd) });
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let mut libinput = Libinput::with_logger(
open_restricted,
close_restricted,
Some(colpetto::tracing_logger),
)?;
libinput.udev_assign_seat(c"seat0")?;
let mut stream = libinput.event_stream()?;
while let Some(event) = stream.try_next().await? {
match event {
Event::Keyboard(KeyboardEvent::Key(event_key)) => {
let device = event_key.device();
let device_name = device.name().to_string_lossy();
let udev_device = device.udev_device().unwrap();
let device_path = udev_device.devnode().unwrap_or(Path::new("Unknown"));
let state = event_key.key_state();
let key = event_key.key();
println!(
"Key \"{key}\" {state} on \"{device_name}\" at node \"{device_path}\"",
device_path = device_path.display()
);
}
Event::Device(DeviceEvent::Added(event)) => {
let device = event.device();
let name = device.name().to_string_lossy();
println!("Found new device: \"{name}\"");
}
Event::Device(DeviceEvent::Removed(event)) => {
let device = event.device();
let name = device.name().to_string_lossy();
println!("Lost device: \"{name}\"");
}
_ => (),
}
}
Ok(())
}