use crate::domain::Domain;
use crate::ecodes;
use std::fmt;
pub type EventValue = i32;
pub type Channel = (EventCode, Domain);
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct EventType(u16);
impl EventType {
pub const KEY: EventType = EventType(ecodes::EV_KEY);
pub const ABS: EventType = EventType(ecodes::EV_ABS);
pub const REP: EventType = EventType(ecodes::EV_REP);
pub const SYN: EventType = EventType(ecodes::EV_SYN);
pub const MSC: EventType = EventType(ecodes::EV_MSC);
pub fn is_key(self) -> bool {
self == EventType::KEY
}
pub fn is_abs(self) -> bool {
self == EventType::ABS
}
pub fn is_rep(self) -> bool {
self == EventType::REP
}
pub fn is_syn(self) -> bool {
self == EventType::SYN
}
}
impl EventType {
pub const unsafe fn new(value: u16) -> EventType {
EventType(value)
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct EventCode {
ev_type: EventType,
code: u16,
}
impl EventCode {
pub const MSC_SCAN: EventCode = EventCode {
ev_type: EventType::MSC,
code: ecodes::MSC_SCAN,
};
pub const unsafe fn new(ev_type: EventType, code: u16) -> EventCode {
EventCode { ev_type, code }
}
pub const fn ev_type(self) -> EventType {
self.ev_type
}
pub const fn code(self) -> u16 {
self.code
}
pub fn virtual_ev_type(self) -> VirtualEventType {
if self.ev_type().is_key() {
if ecodes::is_button_code(self) {
VirtualEventType::Button
} else {
VirtualEventType::Key
}
} else {
VirtualEventType::Other(self.ev_type())
}
}
}
impl From<EventType> for u16 {
fn from(ev_type: EventType) -> u16 {
ev_type.0
}
}
impl From<EventType> for u32 {
fn from(ev_type: EventType) -> u32 {
u16::from(ev_type) as u32
}
}
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum VirtualEventType {
Key,
Button,
Other(EventType),
}
impl VirtualEventType {
pub const KEY: &'static str = "key";
pub const BUTTON: &'static str = "btn";
pub fn ev_type(self) -> EventType {
match self {
VirtualEventType::Key | VirtualEventType::Button => EventType::KEY,
VirtualEventType::Other(ev_type) => ev_type,
}
}
}
#[repr(u8)]
pub enum EventFlag {
Withholdable = 0b0001,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct EventFlags(u8);
impl EventFlags {
pub fn empty() -> EventFlags {
EventFlags(0)
}
pub fn set(&mut self, flag: EventFlag) {
self.0 |= flag as u8;
}
pub fn unset(&mut self, flag: EventFlag) {
self.0 &= !(flag as u8);
}
pub fn get(&mut self, flag: EventFlag) -> bool {
(self.0 & flag as u8) != 0
}
}
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct Event {
pub code: EventCode,
pub value: EventValue,
pub previous_value: EventValue,
pub domain: Domain,
pub namespace: Namespace,
pub flags: EventFlags,
}
impl Event {
pub fn new(
code: EventCode,
value: EventValue,
previous_value: EventValue,
domain: Domain,
namespace: Namespace,
) -> Event {
let flags = EventFlags::empty();
Event {
code,
value,
previous_value,
domain,
namespace,
flags,
}
}
pub fn with_domain(mut self, new_domain: Domain) -> Event {
self.domain = new_domain;
self
}
pub fn ev_type(self) -> EventType {
self.code.ev_type()
}
pub fn channel(self) -> Channel {
(self.code, self.domain)
}
}
impl fmt::Display for Event {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let name = ecodes::event_name(self.code);
write!(f, "{}:{}", name, self.value)
}
}
impl fmt::Debug for Event {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let name = ecodes::event_name(self.code);
write!(
f,
"{}:{}..{}@{:?}",
name, self.previous_value, self.value, self.domain
)
}
}
#[derive(PartialEq, Eq, Clone, Copy, Hash, Debug)]
pub enum Namespace {
Input,
User,
Yielded,
Output,
}