Only pass references around

This commit is contained in:
Travis Burtrum 2017-09-22 23:57:26 -04:00
parent 02fdb4cf49
commit 628b832af1
4 changed files with 29 additions and 18 deletions

View File

@ -44,6 +44,17 @@ with multiple keyboards, currently you must specify one:
find all eligible keyboard devices like: find all eligible keyboard devices like:
`grep -E 'Handlers|EV' /proc/bus/input/devices | grep -B1 120013 | grep -Eo event[0-9]+` `grep -E 'Handlers|EV' /proc/bus/input/devices | grep -B1 120013 | grep -Eo event[0-9]+`
For using the systemd unit with by-id or by-path:
```
$ systemd-escape --template=rusty-keys@.service by-id/usb-04c8_USB_Keyboard-event-kbd
rusty-keys@by\x2did-usb\x2d04c8_USB_Keyboard\x2devent\x2dkbd.service
```
How to install
--------------
* `cargo install rusty-keys`
* Arch Linux [AUR PKGBUILD](https://aur.archlinux.org/packages/rusty-keys/)
License License
------- -------
AGPLv3 for now, message me if you have a problem with this AGPLv3 for now, message me if you have a problem with this

View File

@ -20,23 +20,23 @@ impl Device {
#[doc(hidden)] #[doc(hidden)]
pub fn write(&mut self, kind: c_int, code: c_int, value: c_int) -> Res<()> { pub fn write(&mut self, kind: c_int, code: c_int, value: c_int) -> Res<()> {
let event = input_event { let mut event = input_event {
time: timeval { tv_sec: 0, tv_usec: 0 }, time: timeval { tv_sec: 0, tv_usec: 0 },
type_: kind as u16, type_: kind as u16,
code: code as u16, code: code as u16,
value: value as i32, value: value as i32,
}; };
self.write_event(event) self.write_event(&mut event)
} }
#[doc(hidden)] #[doc(hidden)]
pub fn write_event(&self, mut event: input_event) -> Res<()> { pub fn write_event(&self, event: &mut input_event) -> Res<()> {
unsafe { unsafe {
gettimeofday(&mut event.time, ptr::null_mut()); gettimeofday(&mut event.time, ptr::null_mut());
let ptr = &event as *const _ as *const u8; let ptr = event as *const _ as *const u8;
let size = mem::size_of_val(&event); let size = mem::size_of_val(event);
try!(unistd::write(self.fd, slice::from_raw_parts(ptr, size))); try!(unistd::write(self.fd, slice::from_raw_parts(ptr, size)));
} }

View File

@ -24,7 +24,7 @@ const KEY_RIGHTSHIFT_U16: u16 = KEY_RIGHTSHIFT as u16;
const KEY_CAPSLOCK_U16: u16 = KEY_CAPSLOCK as u16; const KEY_CAPSLOCK_U16: u16 = KEY_CAPSLOCK as u16;
trait KeyMapper { trait KeyMapper {
fn send_event(&mut self, key_state: &mut [bool], event: input_event, device: &Device); fn send_event(&self, key_state: &[bool], event: &mut input_event, device: &Device);
} }
pub struct KeyMaps { pub struct KeyMaps {
@ -155,7 +155,7 @@ impl KeyMaps {
//impl KeyMapper for KeyMaps { //impl KeyMapper for KeyMaps {
impl KeyMaps { impl KeyMaps {
pub fn send_event(&mut self, event: input_event, device: &Device) { pub fn send_event(&mut self, mut event: &mut input_event, device: &Device) {
//println!("type: {} code: {} value: {}", event.type_, event.code, event.value); //println!("type: {} code: {} value: {}", event.type_, event.code, event.value);
if event.value != 2 { if event.value != 2 {
// todo: index check here... // todo: index check here...
@ -191,7 +191,7 @@ impl KeyMaps {
} }
} }
} }
self.keymaps[self.current_keymap_index].send_event(&mut self.key_state, event, device); self.keymaps[self.current_keymap_index].send_event(&self.key_state, &mut event, device);
} }
} }
@ -238,7 +238,7 @@ impl KeyMap {
} }
impl KeyMapper for KeyMap { impl KeyMapper for KeyMap {
fn send_event(&mut self, key_state: &mut [bool], event: input_event, device: &Device) { fn send_event(&self, key_state: &[bool], event: &mut input_event, device: &Device) {
self.keymap[event.code as usize].send_event(key_state, event, device); self.keymap[event.code as usize].send_event(key_state, event, device);
//event.code = self.keymap[event.code as usize]; //event.code = self.keymap[event.code as usize];
//device.write_event(event).expect("could not write event?"); //device.write_event(event).expect("could not write event?");
@ -247,7 +247,7 @@ impl KeyMapper for KeyMap {
#[allow(unused_variables)] #[allow(unused_variables)]
impl KeyMapper for u16 { impl KeyMapper for u16 {
fn send_event(&mut self, key_state: &mut [bool], mut event: input_event, device: &Device) { fn send_event(&self, key_state: &[bool], mut event: &mut input_event, device: &Device) {
event.code = *self; event.code = *self;
device.write_event(event).expect("could not write event?"); device.write_event(event).expect("could not write event?");
} }
@ -259,7 +259,7 @@ struct Noop {}
#[allow(unused_variables)] #[allow(unused_variables)]
impl KeyMapper for Noop { impl KeyMapper for Noop {
fn send_event(&mut self, key_state: &mut [bool], event: input_event, device: &Device) { fn send_event(&self, key_state: &[bool], event: &mut input_event, device: &Device) {
device.write_event(event).expect("could not write event?"); device.write_event(event).expect("could not write event?");
} }
} }
@ -275,8 +275,8 @@ struct HalfInvertedKey {
} }
impl HalfInvertedKey { impl HalfInvertedKey {
fn send_key(&mut self, key_state: &mut [bool], mut event: input_event, device: &Device, left_shift: bool, right_shift: bool, caps_lock: bool) { fn send_key(&self, key_state: &[bool], event: &mut input_event, device: &Device, left_shift: bool, right_shift: bool, caps_lock: bool) {
let mut code = self.code; let code = self.code;
let value = event.value; let value = event.value;
let mut invert_shift = self.invert_shift; let mut invert_shift = self.invert_shift;
if value == DOWN { if value == DOWN {
@ -327,7 +327,7 @@ impl HalfInvertedKey {
} }
impl KeyMapper for HalfInvertedKey { impl KeyMapper for HalfInvertedKey {
fn send_event(&mut self, key_state: &mut [bool], event: input_event, device: &Device) { fn send_event(&self, key_state: &[bool], event: &mut input_event, device: &Device) {
let left_shift = key_state[LEFTSHIFT_INDEX]; let left_shift = key_state[LEFTSHIFT_INDEX];
let right_shift = key_state[RIGHTSHIFT_INDEX]; let right_shift = key_state[RIGHTSHIFT_INDEX];
let caps_lock = key_state[CAPSLOCK_INDEX]; let caps_lock = key_state[CAPSLOCK_INDEX];
@ -341,7 +341,7 @@ struct ShiftInvertedKey {
} }
impl KeyMapper for ShiftInvertedKey { impl KeyMapper for ShiftInvertedKey {
fn send_event(&mut self, key_state: &mut [bool], event: input_event, device: &Device) { fn send_event(&self, key_state: &[bool], event: &mut input_event, device: &Device) {
let left_shift = key_state[LEFTSHIFT_INDEX]; let left_shift = key_state[LEFTSHIFT_INDEX];
let right_shift = key_state[RIGHTSHIFT_INDEX]; let right_shift = key_state[RIGHTSHIFT_INDEX];
let caps_lock = key_state[CAPSLOCK_INDEX]; let caps_lock = key_state[CAPSLOCK_INDEX];

View File

@ -61,9 +61,9 @@ fn main() {
//println!("keymaps: {:?}", keymaps); //println!("keymaps: {:?}", keymaps);
loop { loop {
let event = input_device.read_event(); let mut event = input_device.read_event();
if event.type_ == EV_KEY_U16 { if event.type_ == EV_KEY_U16 {
key_map.send_event(event, &device); key_map.send_event(&mut event, &device);
/* /*
println!("type: {} code: {}", event.type_, event.code); println!("type: {} code: {}", event.type_, event.code);
if event.code == KEY_A as u16 { if event.code == KEY_A as u16 {
@ -71,7 +71,7 @@ fn main() {
} }
*/ */
} else { } else {
device.write_event(event).expect("could not write event?"); device.write_event(&mut event).expect("could not write event?");
} }
} }
} }