From 796b08b4308c2c5408885b8a0816f22bf3d61d9d Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 1 Sep 2019 20:46:46 +0200 Subject: [PATCH] Improve code quality --- native/liblinuxbridge/bridge.cpp | 2 +- src/engine.rs | 4 ++-- src/keyboard/linux.rs | 6 ++++++ src/main.rs | 3 ++- src/matcher/scrolling.rs | 10 +++++----- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/native/liblinuxbridge/bridge.cpp b/native/liblinuxbridge/bridge.cpp index c76801e..e84a7a1 100644 --- a/native/liblinuxbridge/bridge.cpp +++ b/native/liblinuxbridge/bridge.cpp @@ -180,7 +180,7 @@ void event_callback(XPointer p, XRecordInterceptData *hook) switch (event_type) { case KeyRelease: - printf ("%d %d KeyPress: \t%s\t%s\n", key_code, res, XKeysymToString(XkbKeycodeToKeysym(ctrl_disp, key_code, 0, 0)), buffer.data()); + //printf ("%d %d KeyPress: \t%s\t%s\n", key_code, res, XKeysymToString(XkbKeycodeToKeysym(ctrl_disp, key_code, 0, 0)), buffer.data()); if (res > 0) { // Send only printable chars, todo: change keypress_callback(interceptor_instance, buffer.data(), buffer.size()); } diff --git a/src/engine.rs b/src/engine.rs index 41dc997..8410cc5 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -2,11 +2,11 @@ use crate::matcher::{Match, MatchReceiver}; use crate::keyboard::KeyboardSender; pub struct Engine<'a>{ - sender: &'a KeyboardSender + sender: &'a dyn KeyboardSender } impl <'a> Engine<'a> { - pub fn new(sender: &'a KeyboardSender) -> Engine<'a> { + pub fn new(sender: &'a dyn KeyboardSender) -> Engine<'a> { Engine{sender} } } diff --git a/src/keyboard/linux.rs b/src/keyboard/linux.rs index 674da1c..db14d3c 100644 --- a/src/keyboard/linux.rs +++ b/src/keyboard/linux.rs @@ -25,6 +25,12 @@ impl super::KeyboardInterceptor for LinuxKeyboardInterceptor { } } +impl Drop for LinuxKeyboardInterceptor { + fn drop(&mut self) { + unsafe { cleanup(); } + } +} + pub struct LinuxKeyboardSender { } diff --git a/src/main.rs b/src/main.rs index 280fcd4..de4f6ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,8 +23,9 @@ fn main() { let engine = Engine::new(&sender); let matches = vec![Match{trigger:"e'".to_owned(), result: "รจ".to_owned()}, + Match{trigger:"e/".to_owned(), result: "รฉ".to_owned()}, Match{trigger:":lol".to_owned(), result: "๐Ÿ˜‚".to_owned()}, - Match{trigger:":lll".to_owned(), result: "hello".to_owned()}, + Match{trigger:":llol".to_owned(), result: "๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚".to_owned()}, ]; let mut matcher = ScrollingMatcher::new(&matches, &engine); diff --git a/src/matcher/scrolling.rs b/src/matcher/scrolling.rs index 4644cf9..3c5166a 100644 --- a/src/matcher/scrolling.rs +++ b/src/matcher/scrolling.rs @@ -2,7 +2,7 @@ use crate::matcher::{Match, MatchReceiver}; pub struct ScrollingMatcher<'a>{ matches: &'a Vec, - receiver: &'a MatchReceiver, + receiver: &'a dyn MatchReceiver, current_set: Vec> } @@ -27,16 +27,16 @@ impl <'a> super::Matcher for ScrollingMatcher<'a> { self.current_set = old_matches; self.current_set.append(&mut new_matches); - let mut foundMatch = None; + let mut found_match = None; for entry in self.current_set.iter_mut() { if entry.remaining.len() == 0 { - foundMatch = Some(entry._match); + found_match = Some(entry._match); break; } } - if let Some(_match) = foundMatch { + if let Some(_match) = found_match { self.current_set.clear(); self.receiver.on_match(_match); } @@ -44,7 +44,7 @@ impl <'a> super::Matcher for ScrollingMatcher<'a> { } impl <'a> ScrollingMatcher<'a> { - pub fn new(matches:&'a Vec, receiver: &'a MatchReceiver) -> ScrollingMatcher<'a> { + pub fn new(matches:&'a Vec, receiver: &'a dyn MatchReceiver) -> ScrollingMatcher<'a> { let current_set = Vec::new(); ScrollingMatcher{ matches, receiver, current_set } }