diff --git a/espanso-detect/src/evdev/README.md b/espanso-detect/src/evdev/README.md index 1dc0077..3037eda 100644 --- a/espanso-detect/src/evdev/README.md +++ b/espanso-detect/src/evdev/README.md @@ -1,2 +1,3 @@ -This is a great starting point for wayland support: +This module is used to detect keyboard and mouse input using EVDEV layer, which is necessary on Wayland. +The module started as a port of this xkbcommon example https://github.com/xkbcommon/libxkbcommon/blob/master/tools/interactive-evdev.c \ No newline at end of file diff --git a/espanso-detect/src/evdev/device.rs b/espanso-detect/src/evdev/device.rs index 51da12e..4f3ba6b 100644 --- a/espanso-detect/src/evdev/device.rs +++ b/espanso-detect/src/evdev/device.rs @@ -9,7 +9,6 @@ use std::os::unix::io::AsRawFd; use std::{ ffi::{c_void, CStr}, fs::OpenOptions, - mem::zeroed, }; use std::{fs::File, os::unix::fs::OpenOptionsExt}; use thiserror::Error; @@ -31,19 +30,19 @@ const KEY_STATE_REPEAT: i32 = 2; #[derive(Debug)] pub enum RawInputEvent { - Keyboard(KeyboardEvent), - Mouse(MouseEvent), + Keyboard(RawKeyboardEvent), + Mouse(RawMouseEvent), } #[derive(Debug)] -pub struct KeyboardEvent { +pub struct RawKeyboardEvent { pub sym: u32, pub value: String, pub is_down: bool, } #[derive(Debug)] -pub struct MouseEvent { +pub struct RawMouseEvent { pub code: u16, pub is_down: bool, } @@ -143,7 +142,7 @@ impl Device { // Check if the current event originated from a mouse if code >= 0x110 && code <= 0x117 { // Mouse event - return Some(RawInputEvent::Mouse(MouseEvent { code, is_down })); + return Some(RawInputEvent::Mouse(RawMouseEvent { code, is_down })); } // Keyboard event @@ -173,7 +172,7 @@ impl Device { let content_raw = unsafe { CStr::from_ptr(buffer.as_ptr() as *mut i8) }; let content = content_raw.to_string_lossy().to_string(); - let event = KeyboardEvent { + let event = RawKeyboardEvent { is_down, sym, value: content, diff --git a/espanso-detect/src/evdev/mod.rs b/espanso-detect/src/evdev/mod.rs index 1be8517..7ab06d7 100644 --- a/espanso-detect/src/evdev/mod.rs +++ b/espanso-detect/src/evdev/mod.rs @@ -250,35 +250,21 @@ fn raw_to_mouse_button(raw: u16) -> Option { } } -/* TODO convert tests #[cfg(test)] mod tests { - use std::ffi::CString; + use device::RawMouseEvent; - use super::*; + use crate::event::{InputEvent, KeyboardEvent, Key::Other}; - fn default_raw_input_event() -> RawInputEvent { - RawInputEvent { - event_type: INPUT_EVENT_TYPE_KEYBOARD, - buffer: [0; 24], - buffer_len: 0, - key_code: 0, - key_sym: 0, - status: INPUT_STATUS_PRESSED, - } - } + use super::{*, device::{RawInputEvent, RawKeyboardEvent}}; #[test] fn raw_to_input_event_keyboard_works_correctly() { - let c_string = CString::new("k".to_string()).unwrap(); - let mut buffer: [u8; 24] = [0; 24]; - buffer[..1].copy_from_slice(c_string.as_bytes()); - - let mut raw = default_raw_input_event(); - raw.buffer = buffer; - raw.buffer_len = 1; - raw.status = INPUT_STATUS_RELEASED; - raw.key_sym = 0x4B; + let raw = RawInputEvent::Keyboard(RawKeyboardEvent { + sym: 0x4B, + value: "k".to_owned(), + is_down: false, + }); let result: Option = raw.into(); assert_eq!( @@ -294,10 +280,10 @@ mod tests { #[test] fn raw_to_input_event_mouse_works_correctly() { - let mut raw = default_raw_input_event(); - raw.event_type = INPUT_EVENT_TYPE_MOUSE; - raw.status = INPUT_STATUS_RELEASED; - raw.key_code = INPUT_MOUSE_RIGHT_BUTTON; + let raw = RawInputEvent::Mouse(RawMouseEvent { + code: BTN_RIGHT, + is_down: false, + }); let result: Option = raw.into(); assert_eq!( @@ -308,26 +294,4 @@ mod tests { }) ); } - - #[test] - fn raw_to_input_invalid_buffer() { - let buffer: [u8; 24] = [123; 24]; - - let mut raw = default_raw_input_event(); - raw.buffer = buffer; - raw.buffer_len = 5; - - let result: Option = raw.into(); - assert!(result.unwrap().into_keyboard().unwrap().value.is_none()); - } - - #[test] - fn raw_to_input_event_returns_none_when_missing_type() { - let mut raw = default_raw_input_event(); - raw.event_type = 0; - let result: Option = raw.into(); - assert!(result.is_none()); - } -} - -*/ +} \ No newline at end of file