espanso/espanso-detect/src/lib.rs

118 lines
3.2 KiB
Rust
Raw Normal View History

2019-09-15 16:29:11 +00:00
/*
* This file is part of espanso.
*
2021-01-29 20:55:47 +00:00
* Copyright (C) 2019-2021 Federico Terzi
2019-09-15 16:29:11 +00:00
*
* espanso is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* espanso is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
2021-02-14 21:01:42 +00:00
use anyhow::Result;
2021-03-14 14:50:54 +00:00
use hotkey::HotKey;
2021-02-14 21:01:42 +00:00
use log::info;
2021-01-29 20:55:47 +00:00
pub mod event;
2021-03-14 14:50:54 +00:00
pub mod hotkey;
pub mod layout;
2019-09-07 11:35:45 +00:00
2021-01-29 20:55:47 +00:00
#[cfg(target_os = "windows")]
2021-01-30 17:41:47 +00:00
pub mod win32;
2021-01-31 17:09:03 +00:00
#[cfg(target_os = "linux")]
2021-02-14 21:01:42 +00:00
#[cfg(not(feature = "wayland"))]
2021-02-04 21:12:30 +00:00
pub mod x11;
#[cfg(target_os = "linux")]
2021-02-08 20:23:28 +00:00
pub mod evdev;
2021-02-09 16:12:16 +00:00
#[cfg(target_os = "macos")]
pub mod mac;
#[macro_use]
2021-02-10 20:33:14 +00:00
extern crate lazy_static;
2021-02-14 21:01:42 +00:00
pub type SourceCallback = Box<dyn Fn(event::InputEvent)>;
pub trait Source {
fn initialize(&mut self) -> Result<()>;
fn eventloop(&self, event_callback: SourceCallback) -> Result<()>;
2021-02-14 21:01:42 +00:00
}
#[allow(dead_code)]
pub struct SourceCreationOptions {
// Only relevant in X11 Linux systems, use the EVDEV backend instead of X11.
2021-03-14 14:50:54 +00:00
pub use_evdev: bool,
2021-03-09 15:06:50 +00:00
2021-02-14 21:01:42 +00:00
// Can be used to overwrite the keymap configuration
// used by espanso to inject key presses.
2021-03-14 14:50:54 +00:00
pub evdev_keyboard_rmlvo: Option<KeyboardConfig>,
// List of global hotkeys the detection module has to register
2021-03-14 20:53:17 +00:00
// NOTE: Hotkeys don't work under the EVDEV backend yet (Wayland)
2021-03-14 14:50:54 +00:00
pub hotkeys: Vec<HotKey>,
2021-02-14 21:01:42 +00:00
}
// This struct identifies the keyboard layout that
// should be used by EVDEV when loading the keymap.
// For more information: https://xkbcommon.org/doc/current/structxkb__rule__names.html
#[derive(Debug, Clone)]
pub struct KeyboardConfig {
pub rules: Option<String>,
pub model: Option<String>,
pub layout: Option<String>,
pub variant: Option<String>,
pub options: Option<String>,
}
impl Default for SourceCreationOptions {
fn default() -> Self {
Self {
use_evdev: false,
evdev_keyboard_rmlvo: None,
2021-03-14 14:50:54 +00:00
hotkeys: Vec::new(),
2021-02-14 21:01:42 +00:00
}
}
}
#[cfg(target_os = "windows")]
pub fn get_source(options: SourceCreationOptions) -> Result<Box<dyn Source>> {
2021-02-14 21:01:42 +00:00
info!("using Win32Source");
Ok(Box::new(win32::Win32Source::new(&options.hotkeys)))
2021-02-14 21:01:42 +00:00
}
#[cfg(target_os = "macos")]
2021-03-14 14:50:54 +00:00
pub fn get_source(options: SourceCreationOptions) -> Result<Box<dyn Source>> {
2021-02-14 21:01:42 +00:00
info!("using CocoaSource");
2021-03-14 14:50:54 +00:00
Ok(Box::new(mac::CocoaSource::new(&options.hotkeys)))
2021-02-14 21:01:42 +00:00
}
#[cfg(target_os = "linux")]
#[cfg(not(feature = "wayland"))]
pub fn get_source(options: SourceCreationOptions) -> Result<Box<dyn Source>> {
if options.use_evdev {
info!("using EVDEVSource");
Ok(Box::new(evdev::EVDEVSource::new(options)))
} else {
info!("using X11Source");
2021-03-14 20:53:17 +00:00
Ok(Box::new(x11::X11Source::new(&options.hotkeys)))
2021-02-14 21:01:42 +00:00
}
}
#[cfg(target_os = "linux")]
#[cfg(feature = "wayland")]
pub fn get_source(options: SourceCreationOptions) -> Result<Box<dyn Source>> {
info!("using EVDEVSource");
Ok(Box::new(evdev::EVDEVSource::new(options)))
}
pub use layout::get_active_layout;