rusty-keys/src/linux/device/device.rs

89 lines
1.9 KiB
Rust
Raw Normal View History

2016-04-24 22:32:24 -04:00
use std::{mem, ptr, slice};
2017-11-16 23:21:21 -05:00
use libc::{timeval, gettimeofday, input_event, c_int};
use nix::{unistd, ioctl_none};
2021-09-24 00:27:10 -04:00
use crate::Result;
2016-04-24 22:32:24 -04:00
use crate::linux::device::codes::*;
ioctl_none!(ui_dev_destroy, b'U', 2);
2016-04-25 11:57:45 -04:00
/// The virtual device.
2016-04-24 22:32:24 -04:00
pub struct Device {
fd: c_int,
}
impl Device {
2016-04-25 11:57:45 -04:00
/// Wrap a file descriptor in a `Device`.
2016-04-24 22:32:24 -04:00
pub fn new(fd: c_int) -> Self {
Device {
fd: fd
}
}
2016-04-30 16:54:02 -04:00
#[doc(hidden)]
2021-09-24 00:27:10 -04:00
pub fn write(&self, kind: c_int, code: c_int, value: c_int) -> Result<()> {
2017-09-22 23:57:26 -04:00
let mut event = input_event {
time: timeval { tv_sec: 0, tv_usec: 0 },
type_: kind as u16,
code: code as u16,
value: value as i32,
};
2016-04-24 22:32:24 -04:00
2017-09-22 23:57:26 -04:00
self.write_event(&mut event)
2017-09-08 01:16:34 -04:00
}
#[doc(hidden)]
2021-09-24 00:27:10 -04:00
pub fn write_event(&self, event: &mut input_event) -> Result<()> {
2017-09-08 01:16:34 -04:00
unsafe {
2016-04-24 22:32:24 -04:00
gettimeofday(&mut event.time, ptr::null_mut());
2017-09-22 23:57:26 -04:00
let ptr = event as *const _ as *const u8;
let size = mem::size_of_val(event);
2016-04-24 22:32:24 -04:00
2019-09-29 23:49:23 -04:00
unistd::write(self.fd, slice::from_raw_parts(ptr, size))?;
2016-04-24 22:32:24 -04:00
}
Ok(())
}
2016-04-25 11:57:45 -04:00
/// Synchronize the device.
2021-09-24 00:27:10 -04:00
pub fn synchronize(&self) -> Result<()> {
2016-04-24 22:32:24 -04:00
self.write(EV_SYN, SYN_REPORT, 0)
}
2016-05-07 10:38:59 -04:00
/// Send an event.
2021-09-24 00:27:10 -04:00
pub fn send(&self, kind: c_int, code: c_int, value: i32) -> Result<()> {
2017-09-01 00:24:35 -04:00
self.write(kind, code, value)
2016-05-07 10:38:59 -04:00
}
2016-04-25 11:57:45 -04:00
/// Send a press event.
2021-09-24 00:27:10 -04:00
pub fn press(&self, kind: c_int, code: c_int) -> Result<()> {
2017-09-01 00:24:35 -04:00
self.write(kind, code, 1)
2016-04-24 22:32:24 -04:00
}
2016-04-25 11:57:45 -04:00
/// Send a release event.
2021-09-24 00:27:10 -04:00
pub fn release(&self, kind: c_int, code: c_int) -> Result<()> {
2017-09-01 00:24:35 -04:00
self.write(kind, code, 0)
2016-04-24 22:32:24 -04:00
}
2016-04-25 11:57:45 -04:00
/// Send a press and release event.
2021-09-24 00:27:10 -04:00
pub fn click(&self, kind: c_int, code: c_int) -> Result<()> {
2019-09-29 23:49:23 -04:00
self.press(kind, code)?;
2021-09-24 00:27:10 -04:00
self.release(kind, code)
2016-04-25 11:12:11 -04:00
}
2016-04-25 11:57:45 -04:00
/// Send a relative or absolute positioning event.
2021-09-24 00:27:10 -04:00
pub fn position(&self, kind: c_int, code: c_int, value: i32) -> Result<()> {
2017-09-01 00:24:35 -04:00
self.write(kind, code, value)
2016-04-24 22:32:24 -04:00
}
}
impl Drop for Device {
fn drop(&mut self) {
unsafe {
2021-09-24 00:27:10 -04:00
// ignore error here so as to not panic in a drop
ui_dev_destroy(self.fd).ok();
2016-04-24 22:32:24 -04:00
}
}
}