1#[macro_use]
47mod macros;
48mod device;
49pub mod enums;
50pub mod logging;
51mod uinput;
52pub mod util;
53
54use bitflags::bitflags;
55use libc::{c_uint, suseconds_t, time_t};
56use std::convert::{TryFrom, TryInto};
57use std::time::{Duration, SystemTime, SystemTimeError, UNIX_EPOCH};
58
59use enums::*;
60use util::*;
61
62pub use util::EventCodeIterator;
63pub use util::EventTypeIterator;
64pub use util::InputPropIterator;
65
66use evdev_sys as raw;
67
68#[doc(inline)]
69pub use device::Device;
70#[doc(inline)]
71pub use device::DeviceWrapper;
72#[doc(inline)]
73pub use device::Enable;
74#[doc(inline)]
75pub use device::EnableCodeData;
76#[doc(inline)]
77pub use device::UninitDevice;
78#[doc(inline)]
79pub use uinput::UInputDevice;
80
81#[cfg(feature = "serde")]
82use serde::{Deserialize, Serialize};
83
84pub enum GrabMode {
85 Grab = raw::LIBEVDEV_GRAB as isize,
87 Ungrab = raw::LIBEVDEV_UNGRAB as isize,
89}
90
91bitflags! {
92 pub struct ReadFlag: u32 {
93 const SYNC = 1;
95 const NORMAL = 2;
97 const FORCE_SYNC = 4;
100 const BLOCKING = 8;
102 }
103}
104
105#[derive(PartialEq)]
106pub enum ReadStatus {
107 Success = raw::LIBEVDEV_READ_STATUS_SUCCESS as isize,
110 Sync = raw::LIBEVDEV_READ_STATUS_SYNC as isize,
114}
115
116pub enum LedState {
117 On = raw::LIBEVDEV_LED_ON as isize,
119 Off = raw::LIBEVDEV_LED_OFF as isize,
121}
122
123#[derive(Debug)]
124pub struct DeviceId {
125 pub bustype: BusType,
126 pub vendor: u16,
127 pub product: u16,
128 pub version: u16,
129}
130
131#[derive(Clone, Copy, Debug)]
132pub struct AbsInfo {
134 pub value: i32,
136 pub minimum: i32,
138 pub maximum: i32,
140 pub fuzz: i32,
143 pub flat: i32,
146 pub resolution: i32,
149}
150
151impl AbsInfo {
152 pub const fn from_raw(absinfo: libc::input_absinfo) -> AbsInfo {
153 AbsInfo {
154 value: absinfo.value,
155 minimum: absinfo.minimum,
156 maximum: absinfo.maximum,
157 fuzz: absinfo.fuzz,
158 flat: absinfo.flat,
159 resolution: absinfo.resolution,
160 }
161 }
162
163 pub const fn as_raw(&self) -> libc::input_absinfo {
164 libc::input_absinfo {
165 value: self.value,
166 minimum: self.minimum,
167 maximum: self.maximum,
168 fuzz: self.fuzz,
169 flat: self.flat,
170 resolution: self.resolution,
171 }
172 }
173}
174
175#[cfg_attr(feature = "serde", derive(Serialize), derive(Deserialize))]
176#[derive(Copy, Clone, Eq, Hash, PartialOrd, Ord, Debug, PartialEq)]
177pub struct TimeVal {
178 pub tv_sec: time_t,
179 pub tv_usec: suseconds_t,
180}
181
182impl TryFrom<SystemTime> for TimeVal {
183 type Error = SystemTimeError;
184 fn try_from(system_time: SystemTime) -> Result<Self, Self::Error> {
185 let d = system_time.duration_since(UNIX_EPOCH)?;
186 Ok(TimeVal {
187 tv_sec: d.as_secs() as time_t,
188 tv_usec: d.subsec_micros() as suseconds_t,
189 })
190 }
191}
192
193impl TryInto<SystemTime> for TimeVal {
194 type Error = ();
195 fn try_into(self) -> Result<SystemTime, Self::Error> {
198 let secs = self.tv_sec.try_into().map_err(|_| ())?;
199 let nanos = (self.tv_usec * 1000).try_into().map_err(|_| ())?;
200 let duration = Duration::new(secs, nanos);
201 UNIX_EPOCH.checked_add(duration).ok_or(())
202 }
203}
204
205impl TimeVal {
206 pub const fn new(tv_sec: time_t, tv_usec: suseconds_t) -> TimeVal {
207 const MICROS_PER_SEC: suseconds_t = 1_000_000;
208 TimeVal {
209 tv_sec: tv_sec + tv_usec / MICROS_PER_SEC,
210 tv_usec: tv_usec % MICROS_PER_SEC,
211 }
212 }
213
214 pub const fn from_raw(timeval: &libc::timeval) -> TimeVal {
215 TimeVal {
216 tv_sec: timeval.tv_sec,
217 tv_usec: timeval.tv_usec,
218 }
219 }
220
221 pub const fn as_raw(&self) -> libc::timeval {
222 libc::timeval {
223 tv_sec: self.tv_sec,
224 tv_usec: self.tv_usec,
225 }
226 }
227}
228
229#[cfg_attr(feature = "serde", derive(Serialize), derive(Deserialize))]
231#[derive(Clone, Debug, PartialEq, Eq, Hash)]
232pub struct InputEvent {
233 pub time: TimeVal,
235 pub event_code: EventCode,
236 pub value: i32,
237}
238
239impl InputEvent {
240 pub const fn new(timeval: &TimeVal, code: &EventCode, value: i32) -> InputEvent {
241 InputEvent {
242 time: *timeval,
243 event_code: *code,
244 value,
245 }
246 }
247
248 pub fn event_type(&self) -> Option<EventType> {
249 int_to_event_type(event_code_to_int(&self.event_code).0)
250 }
251
252 pub fn from_raw(event: &libc::input_event) -> InputEvent {
253 let ev_type = event.type_ as u32;
254 let event_code = int_to_event_code(ev_type, event.code as u32);
255 InputEvent {
256 time: TimeVal::from_raw(&event.time),
257 event_code,
258 value: event.value,
259 }
260 }
261
262 pub fn as_raw(&self) -> libc::input_event {
263 let (ev_type, ev_code) = event_code_to_int(&self.event_code);
264 libc::input_event {
265 time: self.time.as_raw(),
266 type_: ev_type as u16,
267 code: ev_code as u16,
268 value: self.value,
269 }
270 }
271
272 pub fn is_type(&self, ev_type: &EventType) -> bool {
273 unsafe { raw::libevdev_event_is_type(&self.as_raw(), *ev_type as c_uint) == 1 }
274 }
275
276 pub fn is_code(&self, code: &EventCode) -> bool {
277 let (ev_type, ev_code) = event_code_to_int(code);
278
279 unsafe { raw::libevdev_event_is_code(&self.as_raw(), ev_type, ev_code) == 1 }
280 }
281}