keymapper.rs is now fully platform agnostic, successfully passed through generic hell
This commit is contained in:
parent
50b84dea0a
commit
f47d9e4be7
307
src/keymapper.rs
307
src/keymapper.rs
@ -1,57 +1,87 @@
|
|||||||
|
|
||||||
use crate::Device;
|
|
||||||
use libc::{c_int, input_event};
|
|
||||||
use uinput_sys::{KEY_LEFTSHIFT, KEY_RIGHTSHIFT, KEY_CAPSLOCK};
|
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::hash::Hash;
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
use crate::{Error, Result};
|
use crate::{Error, Result};
|
||||||
|
|
||||||
// 1 is down, 0 is up
|
|
||||||
const DOWN: i32 = 1;
|
|
||||||
const UP: i32 = 0;
|
|
||||||
const INVERT_KEY_FLAG: char = '^';
|
const INVERT_KEY_FLAG: char = '^';
|
||||||
const CAPS_MODIFY_KEY_FLAG: char = '*';
|
const CAPS_MODIFY_KEY_FLAG: char = '*';
|
||||||
const HALF_KEY_SEPARATOR: char = ':';
|
const HALF_KEY_SEPARATOR: char = ':';
|
||||||
|
|
||||||
const LEFTSHIFT_INDEX: usize = KEY_LEFTSHIFT as usize;
|
// nightly only...
|
||||||
const RIGHTSHIFT_INDEX: usize = KEY_RIGHTSHIFT as usize;
|
//pub trait KeyCode = Into<usize> + TryFrom<usize> + Copy + Clone + Eq + Hash + Default + 'static;
|
||||||
const CAPSLOCK_INDEX: usize = KEY_CAPSLOCK as usize;
|
|
||||||
|
|
||||||
const KEY_LEFTSHIFT_U16: u16 = KEY_LEFTSHIFT as u16;
|
#[derive(PartialEq)]
|
||||||
const KEY_RIGHTSHIFT_U16: u16 = KEY_RIGHTSHIFT as u16;
|
pub enum KeyState {
|
||||||
const KEY_CAPSLOCK_U16: u16 = KEY_CAPSLOCK as u16;
|
DOWN,
|
||||||
|
UP,
|
||||||
trait KeyMapper {
|
OTHER,
|
||||||
fn send_event(&self, key_state: &[bool], event: &mut input_event, device: &Device) -> Result<()>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct KeyMaps {
|
pub trait KeyEvent<T>
|
||||||
keymaps: Vec<Box<KeyMapper>>,
|
where
|
||||||
keymap_index_keys: HashMap<u16, usize>,
|
T: Into<usize>,
|
||||||
|
{
|
||||||
|
fn code(&self) -> T;
|
||||||
|
fn value(&self) -> KeyState;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Keyboard<T, E, R = ()>
|
||||||
|
where
|
||||||
|
T: Into<usize>,
|
||||||
|
E: KeyEvent<T>,
|
||||||
|
{
|
||||||
|
fn send(&self, event: &mut E) -> Result<R>;
|
||||||
|
fn send_mod_code(&self, code: T, event: &mut E) -> Result<R>;
|
||||||
|
fn send_mod_code_value(&self, code: T, up_not_down: bool, event: &mut E) -> Result<R>;
|
||||||
|
fn synchronize(&self) -> Result<R>;
|
||||||
|
fn left_shift_code(&self) -> T;
|
||||||
|
fn right_shift_code(&self) -> T;
|
||||||
|
fn caps_lock_code(&self) -> T;
|
||||||
|
fn block_key(&self) -> Result<R>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait KeyMapper<K, T, E, R>
|
||||||
|
where
|
||||||
|
T: Into<usize>,
|
||||||
|
E: KeyEvent<T>,
|
||||||
|
K: Keyboard<T, E, R>,
|
||||||
|
{
|
||||||
|
fn send_event(&self, key_state: &[bool], event: &mut E, device: &K) -> Result<R>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct KeyMaps<K, T, E, R = ()>
|
||||||
|
where
|
||||||
|
T: Into<usize> + Copy + Clone + Eq + Hash,
|
||||||
|
E: KeyEvent<T>,
|
||||||
|
K: Keyboard<T, E, R>,
|
||||||
|
{
|
||||||
|
keymaps: Vec<Box<dyn KeyMapper<K, T, E, R>>>,
|
||||||
|
keymap_index_keys: HashMap<T, usize>,
|
||||||
switch_layout_keys: Vec<usize>,
|
switch_layout_keys: Vec<usize>,
|
||||||
key_state: [bool; KEY_MAX],
|
key_state: [bool; KEY_MAX],
|
||||||
revert_default_key: u16,
|
revert_default_key: T,
|
||||||
revert_keymap_index: usize,
|
revert_keymap_index: usize,
|
||||||
// above do not change, below does
|
// above do not change, below does
|
||||||
chosen_keymap_index: usize,
|
chosen_keymap_index: usize,
|
||||||
current_keymap_index: usize,
|
current_keymap_index: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_key(key_map: &HashMap<&'static str, c_int>, key: &str) -> u16 {
|
fn parse_key<T: Clone + Copy>(key_map: &HashMap<&'static str, T>, key: &str) -> T {
|
||||||
match key_map.get(key.trim_matches(|c: char| c.is_whitespace() || c == INVERT_KEY_FLAG || c == CAPS_MODIFY_KEY_FLAG)) {
|
match key_map.get(key.trim_matches(|c: char| c.is_whitespace() || c == INVERT_KEY_FLAG || c == CAPS_MODIFY_KEY_FLAG)) {
|
||||||
Some(key_code) => *key_code as u16,
|
Some(key_code) => *key_code,
|
||||||
None => panic!("unknown key: {}", key.trim())
|
None => panic!("unknown key: {}", key.trim())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_keymap_numeric(key_map: &HashMap<&'static str, c_int>, keymap: &str) -> Vec<u16> {
|
fn parse_keymap_numeric<T: Clone + Copy>(key_map: &HashMap<&'static str, T>, keymap: &str) -> Vec<T> {
|
||||||
keymap.split(",").map(|k| parse_key(key_map, k)).collect()
|
keymap.split(",").map(|k| parse_key(key_map, k)).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_key_half_inverted(key_map: &HashMap<&'static str, c_int>, key: &str) -> HalfInvertedKey {
|
fn parse_key_half_inverted<T: Clone + Copy>(key_map: &HashMap<&'static str, T>, key: &str) -> HalfInvertedKey<T> {
|
||||||
HalfInvertedKey {
|
HalfInvertedKey {
|
||||||
code: parse_key(key_map, key),
|
code: parse_key(key_map, key),
|
||||||
invert_shift: key.contains(INVERT_KEY_FLAG),
|
invert_shift: key.contains(INVERT_KEY_FLAG),
|
||||||
@ -60,14 +90,14 @@ fn parse_key_half_inverted(key_map: &HashMap<&'static str, c_int>, key: &str) ->
|
|||||||
}
|
}
|
||||||
|
|
||||||
// maybe shortcut to this if not contains * or :
|
// maybe shortcut to this if not contains * or :
|
||||||
fn parse_keymap_u16(key_map: &HashMap<&'static str, c_int>, keymap: &str) -> Vec<u16> {
|
fn parse_keymap_u16<T: Clone + Copy>(key_map: &HashMap<&'static str, T>, keymap: &str) -> Vec<T> {
|
||||||
keymap.split(",").map(|k| parse_key(key_map, k)).collect()
|
keymap.split(",").map(|k| parse_key(key_map, k)).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: how do I return an iterator here instead of .collect to Vec?
|
// todo: how do I return an iterator here instead of .collect to Vec?
|
||||||
fn parse_keymap(key_map: &HashMap<&'static str, c_int>, keymap: &str) -> Vec<Key> {
|
fn parse_keymap<T: Copy>(key_map: &HashMap<&'static str, T>, keymap: &str) -> Vec<Key<T>> {
|
||||||
keymap.split(",").map(|k| {
|
keymap.split(",").map(|k| {
|
||||||
let ret: Key = if k.contains(HALF_KEY_SEPARATOR) {
|
let ret: Key<T> = if k.contains(HALF_KEY_SEPARATOR) {
|
||||||
let keys: Vec<&str> = k.split(HALF_KEY_SEPARATOR).collect();
|
let keys: Vec<&str> = k.split(HALF_KEY_SEPARATOR).collect();
|
||||||
if keys.len() != 2 {
|
if keys.len() != 2 {
|
||||||
panic!("split key can only have 2 keys, 1 :, has {} keys", keys.len());
|
panic!("split key can only have 2 keys, 1 :, has {} keys", keys.len());
|
||||||
@ -84,13 +114,18 @@ fn parse_keymap(key_map: &HashMap<&'static str, c_int>, keymap: &str) -> Vec<Key
|
|||||||
}).collect()
|
}).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KeyMaps {
|
impl<K, T, E, R> KeyMaps<K, T, E, R>
|
||||||
pub fn from_cfg<P: AsRef<Path>>(key_map: &HashMap<&'static str, c_int>, path: P) -> KeyMaps {
|
where
|
||||||
|
T: Into<usize> + TryFrom<usize> + Copy + Clone + Eq + Hash + Default + 'static,
|
||||||
|
E: KeyEvent<T>,
|
||||||
|
K: Keyboard<T, E, R>,
|
||||||
|
{
|
||||||
|
pub fn from_cfg<P: AsRef<Path>>(key_map: &HashMap<&'static str, T>, path: P) -> KeyMaps<K, T, E, R> {
|
||||||
let key_map_config = parse_cfg(path).expect("provided config cannot be found/parsed");
|
let key_map_config = parse_cfg(path).expect("provided config cannot be found/parsed");
|
||||||
KeyMaps::new(key_map, key_map_config)
|
KeyMaps::new(key_map, key_map_config)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(key_map: &HashMap<&'static str, c_int>, config: KeymapConfig) -> KeyMaps {
|
pub fn new(key_map: &HashMap<&'static str, T>, config: KeymapConfig) -> KeyMaps<K, T, E, R> {
|
||||||
if config.keymaps.len() < 2 {
|
if config.keymaps.len() < 2 {
|
||||||
panic!("must have at least 2 keymaps (original and mapped) but only have {},", config.keymaps.len());
|
panic!("must have at least 2 keymaps (original and mapped) but only have {},", config.keymaps.len());
|
||||||
}
|
}
|
||||||
@ -99,10 +134,10 @@ impl KeyMaps {
|
|||||||
}
|
}
|
||||||
let base_keymap = parse_keymap_numeric(key_map, &config.keymaps[0]);
|
let base_keymap = parse_keymap_numeric(key_map, &config.keymaps[0]);
|
||||||
//println!("base_keymap : {:?}", base_keymap);
|
//println!("base_keymap : {:?}", base_keymap);
|
||||||
let mut keymaps: Vec<Box<KeyMapper>> = vec!(Box::new(Key::Noop)); // todo: can we share the box?
|
let mut keymaps: Vec<Box<dyn KeyMapper<K, T, E, R>>> = vec!(Box::new(Key::Noop)); // todo: can we share the box?
|
||||||
let mut keymap_index_keys: HashMap<u16, usize> = HashMap::new();
|
let mut keymap_index_keys: HashMap<T, usize> = HashMap::new();
|
||||||
for (x, v) in config.keymaps.iter().enumerate() {
|
for (x, v) in config.keymaps.iter().enumerate() {
|
||||||
keymap_index_keys.insert(*key_map.get(&*x.to_string()).unwrap() as u16, x);
|
keymap_index_keys.insert(*key_map.get(&*x.to_string()).unwrap(), x);
|
||||||
if x == 0 {
|
if x == 0 {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -141,7 +176,7 @@ impl KeyMaps {
|
|||||||
KeyMaps {
|
KeyMaps {
|
||||||
keymaps: keymaps,
|
keymaps: keymaps,
|
||||||
keymap_index_keys: keymap_index_keys,
|
keymap_index_keys: keymap_index_keys,
|
||||||
switch_layout_keys: config.switch_layout_keys.iter().map(|k| parse_key(key_map, k) as usize).collect(),
|
switch_layout_keys: config.switch_layout_keys.iter().map(|k| parse_key(key_map, k).into()).collect(),
|
||||||
key_state: [false; KEY_MAX],
|
key_state: [false; KEY_MAX],
|
||||||
// todo: detect key state? at least CAPSLOCK...
|
// todo: detect key state? at least CAPSLOCK...
|
||||||
revert_default_key: parse_key(key_map, &config.revert_default_key),
|
revert_default_key: parse_key(key_map, &config.revert_default_key),
|
||||||
@ -150,20 +185,21 @@ impl KeyMaps {
|
|||||||
current_keymap_index: config.default_keymap_index,
|
current_keymap_index: config.default_keymap_index,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
//}
|
||||||
|
|
||||||
//impl KeyMapper for KeyMaps {
|
//impl KeyMapper for KeyMaps {
|
||||||
impl KeyMaps {
|
//impl KeyMaps {
|
||||||
pub fn send_event(&mut self, mut event: &mut input_event, device: &Device) -> Result<()> {
|
pub fn send_event(&mut self, mut event: &mut E, device: &K) -> Result<R> {
|
||||||
//println!("type: {} code: {} value: {}", event.type_, event.code, event.value);
|
//println!("type: {} code: {} value: {}", event.type_, event.code, event.value);
|
||||||
if event.value != 2 {
|
let value = event.value();
|
||||||
|
if value != KeyState::OTHER {
|
||||||
// todo: index check here...
|
// todo: index check here...
|
||||||
if event.code == KEY_CAPSLOCK_U16 {
|
if event.code() == device.caps_lock_code() {
|
||||||
if event.value == DOWN {
|
if value == KeyState::DOWN {
|
||||||
self.key_state[CAPSLOCK_INDEX] = !self.key_state[CAPSLOCK_INDEX];
|
self.key_state[device.caps_lock_code().into()] = !self.key_state[device.caps_lock_code().into()];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.key_state[event.code as usize] = event.value == DOWN;
|
self.key_state[event.code().into()] = value == KeyState::DOWN;
|
||||||
}
|
}
|
||||||
let mut switch_layout_keys_pressed = true;
|
let mut switch_layout_keys_pressed = true;
|
||||||
for layout_switch_key in self.switch_layout_keys.iter_mut() {
|
for layout_switch_key in self.switch_layout_keys.iter_mut() {
|
||||||
@ -174,18 +210,18 @@ impl KeyMaps {
|
|||||||
}
|
}
|
||||||
//println!("switch_layout_keys_pressed: {}", self.switch_layout_keys_pressed);
|
//println!("switch_layout_keys_pressed: {}", self.switch_layout_keys_pressed);
|
||||||
if switch_layout_keys_pressed {
|
if switch_layout_keys_pressed {
|
||||||
let new_index = self.keymap_index_keys.get(&event.code);
|
let new_index = self.keymap_index_keys.get(&event.code());
|
||||||
if new_index.is_some() {
|
if new_index.is_some() {
|
||||||
self.chosen_keymap_index = *new_index.unwrap();
|
self.chosen_keymap_index = *new_index.unwrap();
|
||||||
self.current_keymap_index = self.chosen_keymap_index; // todo: what if revert_default_key is held? for now ignore
|
self.current_keymap_index = self.chosen_keymap_index; // todo: what if revert_default_key is held? for now ignore
|
||||||
return Ok(()); // we don't want to also send this keypress, so bail
|
return device.block_key(); // we don't want to also send this keypress, so bail
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if event.code == self.revert_default_key {
|
if event.code() == self.revert_default_key {
|
||||||
match event.value {
|
match value {
|
||||||
// todo: ctrl+c will get c stuck because code c value 1 will be sent, but then we'll let go of ctrl, and code j value 0 is sent, so c is never released... fix that...
|
// todo: ctrl+c will get c stuck because code c value 1 will be sent, but then we'll let go of ctrl, and code j value 0 is sent, so c is never released... fix that...
|
||||||
DOWN => self.current_keymap_index = self.revert_keymap_index,
|
KeyState::DOWN => self.current_keymap_index = self.revert_keymap_index,
|
||||||
UP => self.current_keymap_index = self.chosen_keymap_index,
|
KeyState::UP => self.current_keymap_index = self.chosen_keymap_index,
|
||||||
_ => () // do nothing for 2
|
_ => () // do nothing for 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -197,17 +233,17 @@ impl KeyMaps {
|
|||||||
// 249 is one more than KEY_MICMUTE which is max key in uinput-sys event.rs
|
// 249 is one more than KEY_MICMUTE which is max key in uinput-sys event.rs
|
||||||
const KEY_MAX: usize = 249;
|
const KEY_MAX: usize = 249;
|
||||||
|
|
||||||
struct KeyMap {
|
struct KeyMap<T: Into<usize> + Copy> {
|
||||||
//keymap: Vec<Key>,
|
//keymap: Vec<Key>,
|
||||||
keymap: [Key; KEY_MAX],
|
keymap: [Key<T>; KEY_MAX],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KeyMap {
|
impl<T: Into<usize> + Copy> KeyMap<T> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
//let mut keymap = [0u16; KEY_MAX];
|
//let mut keymap = [0u16; KEY_MAX];
|
||||||
//let mut keymap : [Box<KeyMapper>; KEY_MAX] = [Box::new(NOOP); KEY_MAX];
|
//let mut keymap : [Box<KeyMapper>; KEY_MAX] = [Box::new(NOOP); KEY_MAX];
|
||||||
//let mut keymap : [Box<KeyMapper>; KEY_MAX] = [Box::new(0u16); KEY_MAX];
|
//let mut keymap : [Box<KeyMapper>; KEY_MAX] = [Box::new(0u16); KEY_MAX];
|
||||||
let keymap : [Key; KEY_MAX] = [Key::Noop; KEY_MAX];
|
let keymap: [Key<T>; KEY_MAX] = [Key::Noop; KEY_MAX];
|
||||||
/*
|
/*
|
||||||
let mut keymap: Vec<Key> = Vec::with_capacity(KEY_MAX);
|
let mut keymap: Vec<Key> = Vec::with_capacity(KEY_MAX);
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
@ -234,25 +270,30 @@ impl KeyMap {
|
|||||||
self.keymap[from as usize] = to;
|
self.keymap[from as usize] = to;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
pub fn map(&mut self, from: u16, to: Key) {
|
pub fn map(&mut self, from: T, to: Key<T>) {
|
||||||
self.keymap[from as usize] = to;
|
self.keymap[from.into()] = to;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KeyMapper for KeyMap {
|
impl<K, T, E, R> KeyMapper<K, T, E, R> for KeyMap<T>
|
||||||
fn send_event(&self, key_state: &[bool], event: &mut input_event, device: &Device) -> Result<()> {
|
where
|
||||||
self.keymap[event.code as usize].send_event(key_state, event, device)
|
T: Into<usize> + Copy,
|
||||||
|
E: KeyEvent<T>,
|
||||||
|
K: Keyboard<T, E, R>,
|
||||||
|
{
|
||||||
|
fn send_event(&self, key_state: &[bool], event: &mut E, device: &K) -> Result<R> {
|
||||||
|
self.keymap[event.code().into()].send_event(key_state, event, device)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CodeKeyMap {
|
struct CodeKeyMap<T: Into<usize> + TryFrom<usize> + Copy + Default> {
|
||||||
//keymap: Vec<Key>,
|
//keymap: Vec<Key>,
|
||||||
keymap: [u16; KEY_MAX],
|
keymap: [T; KEY_MAX],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CodeKeyMap {
|
impl<T: Into<usize> + TryFrom<usize> + Copy + Default> CodeKeyMap<T> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let mut keymap = [0u16; KEY_MAX];
|
let mut keymap = [T::default(); KEY_MAX];
|
||||||
// which is rustier
|
// which is rustier
|
||||||
/*
|
/*
|
||||||
for x in 0..KEY_MAX {
|
for x in 0..KEY_MAX {
|
||||||
@ -260,7 +301,7 @@ impl CodeKeyMap {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
for (x, v) in keymap.iter_mut().enumerate() {
|
for (x, v) in keymap.iter_mut().enumerate() {
|
||||||
*v = x as u16;
|
*v = T::try_from(x).unwrap_or_else(|_| panic!("cannot convert from usize to T ????"));
|
||||||
}
|
}
|
||||||
//println!("keymap: {:?}", &keymap[..]);
|
//println!("keymap: {:?}", &keymap[..]);
|
||||||
CodeKeyMap {
|
CodeKeyMap {
|
||||||
@ -268,29 +309,27 @@ impl CodeKeyMap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn map(&mut self, from: u16, to: u16) {
|
pub fn map(&mut self, from: T, to: T) {
|
||||||
self.keymap[from as usize] = to;
|
self.keymap[from.into()] = to;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KeyMapper for CodeKeyMap {
|
impl<K, T, E, R> KeyMapper<K, T, E, R> for CodeKeyMap<T>
|
||||||
fn send_event(&self, key_state: &[bool], event: &mut input_event, device: &Device) -> Result<()> {
|
where
|
||||||
self.keymap[event.code as usize].send_event(key_state, event, device)
|
T: Into<usize> + TryFrom<usize> + Copy + Default,
|
||||||
}
|
E: KeyEvent<T>,
|
||||||
}
|
K: Keyboard<T, E, R>,
|
||||||
|
{
|
||||||
#[allow(unused_variables, unused_mut)]
|
fn send_event(&self, _key_state: &[bool], event: &mut E, device: &K) -> Result<R> {
|
||||||
impl KeyMapper for u16 {
|
device.send_mod_code(self.keymap[event.code().into()], event)
|
||||||
fn send_event(&self, key_state: &[bool], mut event: &mut input_event, device: &Device) -> Result<()> {
|
//self.keymap[event.code().into()].send_event(key_state, event, device)
|
||||||
event.code = *self;
|
|
||||||
device.write_event(event)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo:capslock_nomodify is like a whole-key thing, not a half-key thing, split code/invert_shift to own struct, send into send_key from *InvertedKey, maybe anyway, consider it, maybe 1 char for whole key and another for half?
|
// todo:capslock_nomodify is like a whole-key thing, not a half-key thing, split code/invert_shift to own struct, send into send_key from *InvertedKey, maybe anyway, consider it, maybe 1 char for whole key and another for half?
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
struct HalfInvertedKey {
|
struct HalfInvertedKey<T: Clone + Copy> {
|
||||||
code: u16,
|
code: T,
|
||||||
// code this is describing
|
// code this is describing
|
||||||
invert_shift: bool,
|
invert_shift: bool,
|
||||||
// true to invert shift for this code
|
// true to invert shift for this code
|
||||||
@ -298,100 +337,102 @@ struct HalfInvertedKey {
|
|||||||
// true means capslock does not normally modify this, but you would like it to
|
// true means capslock does not normally modify this, but you would like it to
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HalfInvertedKey {
|
fn send_half_inverted_key<K, T, E, R>(half_inverted_key: &HalfInvertedKey<T>, event: &mut E, device: &K, left_shift: bool, right_shift: bool, caps_lock: bool) -> Result<R>
|
||||||
fn send_key(&self, key_state: &[bool], event: &mut input_event, device: &Device, left_shift: bool, right_shift: bool, caps_lock: bool) -> Result<()> {
|
where
|
||||||
let code = self.code;
|
T: Into<usize> + Clone + Copy,
|
||||||
let value = event.value;
|
E: KeyEvent<T>,
|
||||||
let mut invert_shift = self.invert_shift;
|
K: Keyboard<T, E, R>,
|
||||||
if value == DOWN {
|
{
|
||||||
if caps_lock && self.capslock_nomodify {
|
let value = event.value();
|
||||||
|
let mut invert_shift = half_inverted_key.invert_shift;
|
||||||
|
if value == KeyState::DOWN {
|
||||||
|
if caps_lock && half_inverted_key.capslock_nomodify {
|
||||||
invert_shift = !invert_shift;
|
invert_shift = !invert_shift;
|
||||||
}
|
}
|
||||||
if invert_shift {
|
if invert_shift {
|
||||||
if left_shift {
|
let (shift_code, up_not_down) = if left_shift {
|
||||||
event.code = KEY_LEFTSHIFT_U16;
|
(device.left_shift_code(), true)
|
||||||
event.value = UP;
|
|
||||||
} else if right_shift {
|
} else if right_shift {
|
||||||
event.code = KEY_RIGHTSHIFT_U16;
|
(device.right_shift_code(), true)
|
||||||
event.value = UP;
|
|
||||||
} else {
|
} else {
|
||||||
event.code = KEY_LEFTSHIFT_U16;
|
(device.left_shift_code(), false)
|
||||||
event.value = DOWN;
|
};
|
||||||
}
|
device.send_mod_code_value(shift_code, up_not_down, event)?;
|
||||||
//event.code.send_event(key_state, event, device);
|
|
||||||
device.write_event(event)?;
|
|
||||||
// SYN_REPORT after, then key, then key's SYN_REPORT
|
// SYN_REPORT after, then key, then key's SYN_REPORT
|
||||||
device.synchronize()?;
|
device.synchronize()?;
|
||||||
event.code = code; // not needed since u16 does it
|
|
||||||
event.value = value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
code.send_event(key_state, event, device)?;
|
let ret = device.send_mod_code(half_inverted_key.code, event)?;
|
||||||
if value == UP {
|
if value == KeyState::UP {
|
||||||
if caps_lock && self.capslock_nomodify {
|
if caps_lock && half_inverted_key.capslock_nomodify {
|
||||||
invert_shift = !invert_shift;
|
invert_shift = !invert_shift;
|
||||||
}
|
}
|
||||||
if invert_shift {
|
if invert_shift {
|
||||||
if left_shift {
|
let (shift_code, up_not_down) = if left_shift {
|
||||||
event.code = KEY_LEFTSHIFT_U16;
|
(device.left_shift_code(), false)
|
||||||
event.value = DOWN;
|
|
||||||
} else if right_shift {
|
} else if right_shift {
|
||||||
event.code = KEY_RIGHTSHIFT_U16;
|
(device.right_shift_code(), false)
|
||||||
event.value = DOWN;
|
|
||||||
} else {
|
} else {
|
||||||
event.code = KEY_LEFTSHIFT_U16;
|
(device.left_shift_code(), true)
|
||||||
event.value = UP;
|
};
|
||||||
}
|
|
||||||
//event.code.send_event(key_state, event, device);
|
|
||||||
// SYN_REPORT first after key, then shift, then key's SYN_REPORT which will be used for shift's
|
// SYN_REPORT first after key, then shift, then key's SYN_REPORT which will be used for shift's
|
||||||
device.synchronize()?;
|
device.synchronize()?;
|
||||||
device.write_event(event)?;
|
device.send_mod_code_value(shift_code, up_not_down, event)?;
|
||||||
// neither of these are needed now...
|
|
||||||
event.code = code; // not needed since u16 does it
|
|
||||||
event.value = value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(ret)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KeyMapper for HalfInvertedKey {
|
impl<K, T, E, R> KeyMapper<K, T, E, R> for HalfInvertedKey<T>
|
||||||
fn send_event(&self, key_state: &[bool], event: &mut input_event, device: &Device) -> Result<()> {
|
where
|
||||||
let left_shift = key_state[LEFTSHIFT_INDEX];
|
T: Into<usize> + Clone + Copy,
|
||||||
let right_shift = key_state[RIGHTSHIFT_INDEX];
|
E: KeyEvent<T>,
|
||||||
let caps_lock = key_state[CAPSLOCK_INDEX];
|
K: Keyboard<T, E, R>,
|
||||||
self.send_key(key_state, event, device, left_shift, right_shift, caps_lock)
|
{
|
||||||
|
fn send_event(&self, key_state: &[bool], event: &mut E, device: &K) -> Result<R> {
|
||||||
|
let left_shift = key_state[device.left_shift_code().into()];
|
||||||
|
let right_shift = key_state[device.right_shift_code().into()];
|
||||||
|
let caps_lock = key_state[device.caps_lock_code().into()];
|
||||||
|
send_half_inverted_key(self, event, device, left_shift, right_shift, caps_lock)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
enum Key {
|
enum Key<T>
|
||||||
|
where
|
||||||
|
T: Copy + Clone
|
||||||
|
{
|
||||||
Noop,
|
Noop,
|
||||||
Direct(u16),
|
Direct(T),
|
||||||
HalfKey(HalfInvertedKey),
|
HalfKey(HalfInvertedKey<T>),
|
||||||
FullKey(HalfInvertedKey, HalfInvertedKey),
|
FullKey(HalfInvertedKey<T>, HalfInvertedKey<T>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KeyMapper for Key {
|
impl<K, T, E, R> KeyMapper<K, T, E, R> for Key<T>
|
||||||
fn send_event(&self, key_state: &[bool], event: &mut input_event, device: &Device) -> Result<()> {
|
where
|
||||||
|
T: Into<usize> + Copy,
|
||||||
|
E: KeyEvent<T>,
|
||||||
|
K: Keyboard<T, E, R>,
|
||||||
|
{
|
||||||
|
fn send_event(&self, key_state: &[bool], event: &mut E, device: &K) -> Result<R> {
|
||||||
match *self {
|
match *self {
|
||||||
Key::Noop => {
|
Key::Noop => {
|
||||||
device.write_event(event)
|
device.send(event)
|
||||||
},
|
},
|
||||||
Key::Direct(code) => {
|
Key::Direct(code) => {
|
||||||
code.send_event(key_state, event, device)
|
device.send_mod_code(code, event)
|
||||||
},
|
},
|
||||||
Key::HalfKey(ref key_half) => {
|
Key::HalfKey(ref key_half) => {
|
||||||
key_half.send_event(key_state, event, device)
|
key_half.send_event(key_state, event, device)
|
||||||
},
|
},
|
||||||
Key::FullKey(ref noshift_half, ref shift_half) => {
|
Key::FullKey(ref noshift_half, ref shift_half) => {
|
||||||
let left_shift = key_state[LEFTSHIFT_INDEX];
|
let left_shift = key_state[device.left_shift_code().into()];
|
||||||
let right_shift = key_state[RIGHTSHIFT_INDEX];
|
let right_shift = key_state[device.right_shift_code().into()];
|
||||||
let caps_lock = key_state[CAPSLOCK_INDEX];
|
let caps_lock = key_state[device.caps_lock_code().into()];
|
||||||
if caps_lock != (left_shift || right_shift) {
|
if caps_lock != (left_shift || right_shift) {
|
||||||
shift_half.send_key(key_state, event, device, left_shift, right_shift, caps_lock)
|
send_half_inverted_key(shift_half, event, device, left_shift, right_shift, caps_lock)
|
||||||
} else {
|
} else {
|
||||||
noshift_half.send_key(key_state, event, device, left_shift, right_shift, caps_lock)
|
send_half_inverted_key(noshift_half, event, device, left_shift, right_shift, caps_lock)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ pub use error::Error;
|
|||||||
pub type Result<T> = ::std::result::Result<T, Error>;
|
pub type Result<T> = ::std::result::Result<T, Error>;
|
||||||
|
|
||||||
pub mod keymapper;
|
pub mod keymapper;
|
||||||
pub use keymapper::KeyMaps;
|
pub use keymapper::*;
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
mod linux;
|
mod linux;
|
||||||
|
@ -69,7 +69,7 @@ impl Builder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn event(mut self, key_codes: Values<&str, c_int>) -> Res<Self> {
|
pub fn event(mut self, key_codes: Values<&str, u16>) -> Res<Self> {
|
||||||
self.abs = None;
|
self.abs = None;
|
||||||
//let test_ev_key : c_int = EV_KEY as c_int;
|
//let test_ev_key : c_int = EV_KEY as c_int;
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -22,6 +22,10 @@ use std::{env, thread};
|
|||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::sync::mpsc::Sender;
|
use std::sync::mpsc::Sender;
|
||||||
|
|
||||||
|
// 1 is down, 0 is up
|
||||||
|
const DOWN: i32 = 1;
|
||||||
|
const UP: i32 = 0;
|
||||||
|
|
||||||
use getopts::Options;
|
use getopts::Options;
|
||||||
|
|
||||||
use inotify::{
|
use inotify::{
|
||||||
@ -30,12 +34,74 @@ use inotify::{
|
|||||||
WatchMask,
|
WatchMask,
|
||||||
};
|
};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::os::raw::c_int;
|
|
||||||
|
|
||||||
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
const EV_KEY_U16: u16 = EV_KEY as u16;
|
const EV_KEY_U16: u16 = EV_KEY as u16;
|
||||||
|
|
||||||
|
type LinuxKeyMaps = KeyMaps<Device, u16, input_event>;
|
||||||
|
|
||||||
|
impl KeyEvent<u16> for input_event {
|
||||||
|
fn code(&self) -> u16 {
|
||||||
|
self.code
|
||||||
|
}
|
||||||
|
|
||||||
|
fn value(&self) -> KeyState {
|
||||||
|
match self.value {
|
||||||
|
UP => KeyState::UP,
|
||||||
|
DOWN => KeyState::DOWN,
|
||||||
|
_ => KeyState::OTHER,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Keyboard<u16, input_event> for Device {
|
||||||
|
fn send(&self, event: &mut input_event) -> Result<()> {
|
||||||
|
self.write_event(event)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn send_mod_code(&self, code: u16, event: &mut input_event) -> Result<()> {
|
||||||
|
event.code = code;
|
||||||
|
Keyboard::send(self, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn send_mod_code_value(&self, code: u16, up_not_down: bool, event: &mut input_event) -> Result<()> {
|
||||||
|
event.code = code;
|
||||||
|
let value = event.value;
|
||||||
|
if up_not_down {
|
||||||
|
event.value = UP;
|
||||||
|
} else {
|
||||||
|
event.value = DOWN;
|
||||||
|
}
|
||||||
|
Keyboard::send(self, event)?;
|
||||||
|
// set it back
|
||||||
|
event.value = value;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn synchronize(&self) -> Result<()> {
|
||||||
|
Device::synchronize(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn left_shift_code(&self) -> u16 {
|
||||||
|
KEY_LEFTSHIFT as u16
|
||||||
|
}
|
||||||
|
|
||||||
|
fn right_shift_code(&self) -> u16 {
|
||||||
|
KEY_RIGHTSHIFT as u16
|
||||||
|
}
|
||||||
|
|
||||||
|
fn caps_lock_code(&self) -> u16 {
|
||||||
|
KEY_CAPSLOCK as u16
|
||||||
|
}
|
||||||
|
|
||||||
|
fn block_key(&self) -> Result<()> {
|
||||||
|
Ok(()) // we don't actually use/need this here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Config {
|
struct Config {
|
||||||
device_files: Vec<String>,
|
device_files: Vec<String>,
|
||||||
@ -62,7 +128,7 @@ pub fn main_res() -> Result<()> {
|
|||||||
.event(key_map.values())?
|
.event(key_map.values())?
|
||||||
.create()?;
|
.create()?;
|
||||||
|
|
||||||
let mut key_map = KeyMaps::from_cfg(&key_map, &config.config_file);
|
let mut key_map = LinuxKeyMaps::from_cfg(&key_map, &config.config_file);
|
||||||
//println!("keymaps: {:?}", keymaps);
|
//println!("keymaps: {:?}", keymaps);
|
||||||
|
|
||||||
if config.device_files.len() == 1 {
|
if config.device_files.len() == 1 {
|
||||||
@ -135,7 +201,7 @@ pub fn main_res() -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_event(key_map: &mut KeyMaps, mut event: input_event, device: &Device) -> Result<()> {
|
fn send_event(key_map: &mut LinuxKeyMaps, mut event: input_event, device: &Device) -> Result<()> {
|
||||||
if event.type_ == EV_KEY_U16 {
|
if event.type_ == EV_KEY_U16 {
|
||||||
key_map.send_event(&mut event, &device)?
|
key_map.send_event(&mut event, &device)?
|
||||||
} else {
|
} else {
|
||||||
@ -233,7 +299,7 @@ fn get_keyboard_device_filenames() -> Vec<String> {
|
|||||||
filenames
|
filenames
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn key_map() -> HashMap<&'static str, c_int> {
|
pub fn key_map() -> HashMap<&'static str, u16> {
|
||||||
[
|
[
|
||||||
// generated like:
|
// generated like:
|
||||||
// grep -o 'KEY_[^ :;]*' ~/.cargo/registry/src/github.com-1ecc6299db9ec823/uinput-sys-0.1.3/src/events.rs | sed 's/^KEY_//' | awk '{print "(\""$1"\", KEY_"$1"),"}'
|
// grep -o 'KEY_[^ :;]*' ~/.cargo/registry/src/github.com-1ecc6299db9ec823/uinput-sys-0.1.3/src/events.rs | sed 's/^KEY_//' | awk '{print "(\""$1"\", KEY_"$1"),"}'
|
||||||
@ -541,5 +607,5 @@ pub fn key_map() -> HashMap<&'static str, c_int> {
|
|||||||
("P0", KEY_KP0),
|
("P0", KEY_KP0),
|
||||||
("PDOT", KEY_KPDOT),
|
("PDOT", KEY_KPDOT),
|
||||||
("PENT", KEY_KPENTER),
|
("PENT", KEY_KPENTER),
|
||||||
].iter().cloned().map(|(m, v)| (m, v)).collect()
|
].iter().cloned().map(|(m, v)| (m, v as u16)).collect()
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user