From 8c55c00a5f2cafd84e46aaf2e0309339394b9d00 Mon Sep 17 00:00:00 2001 From: htrefil <8711792+htrefil@users.noreply.github.com> Date: Thu, 17 Sep 2020 17:49:23 +0200 Subject: [PATCH] Move enum Event to crate input, begin work on EventWriter --- {proto => input}/src/event.rs | 0 input/src/event_writer.rs | 33 ++++++++++++++++++++++++++++++++- input/src/lib.rs | 5 +++++ proto/src/lib.rs | 2 +- 4 files changed, 38 insertions(+), 2 deletions(-) rename {proto => input}/src/event.rs (100%) diff --git a/proto/src/event.rs b/input/src/event.rs similarity index 100% rename from proto/src/event.rs rename to input/src/event.rs diff --git a/input/src/event_writer.rs b/input/src/event_writer.rs index 1565955..9c46978 100644 --- a/input/src/event_writer.rs +++ b/input/src/event_writer.rs @@ -1 +1,32 @@ -pub struct EventWriter {} +use crate::async_file::{AsyncFile, OpenMode}; +use crate::bindings; +use libc::c_int; +use std::io::Error; +use std::os::unix::io::AsRawFd; + +pub struct EventWriter { + file: AsyncFile, +} + +impl EventWriter { + pub async fn new() -> Result { + let file = AsyncFile::open("/dev/uinput", OpenMode::Write).await?; + let fd = file.as_raw_fd(); + + for evbit in &[bindings::EV_KEY, bindings::EV_REL] { + // Doesn't work, UI_SET_KEYBIT not found. + // Probably too complicated for bindgen to be able to do something with it. + check_ioctl(unsafe { libc::ioctl(fd, bindings::UI_SET_KEYBIT, evbit) })?; + } + + Ok(EventWriter { file }) + } +} + +fn check_ioctl(ret: c_int) -> Result<(), Error> { + if ret == -1 { + return Err(Error::last_os_error()); + } + + Ok(()) +} diff --git a/input/src/lib.rs b/input/src/lib.rs index 8bf63c4..81d3232 100644 --- a/input/src/lib.rs +++ b/input/src/lib.rs @@ -1,4 +1,9 @@ mod async_file; mod bindings; +mod event; mod event_reader; mod event_writer; + +pub use event::Event; +pub use event_reader::EventReader; +pub use event_writer::EventWriter; diff --git a/proto/src/lib.rs b/proto/src/lib.rs index 428d00a..8b13789 100644 --- a/proto/src/lib.rs +++ b/proto/src/lib.rs @@ -1 +1 @@ -mod event; +