Skip to content

Commit

Permalink
Some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
davystrong committed Sep 6, 2021
1 parent 24ace66 commit 264935a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/key_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod tests {
}
}

/// Create an input struct from the key code and event
fn create_input(key_code: u16, event: u32) -> winuser::INPUT {
let kb_input_u = unsafe {
let mut kb_input_u = winuser::INPUT_u::default();
Expand All @@ -38,6 +39,7 @@ fn create_input(key_code: u16, event: u32) -> winuser::INPUT {
}
}

/// Trigger thef list o key events through the Windows api
pub fn trigger_keys(
key_codes: &[u16],
events: &[u32],
Expand All @@ -56,6 +58,7 @@ pub fn trigger_keys(
)
}

/// Get the speed at which the keyboard repeats a keystroke
pub fn get_keyboard_speed() -> Result<u32, error_code::ErrorCode<error_code::SystemCategory>> {
let mut raw_speed = 0u32;
unsafe {
Expand All @@ -79,8 +82,10 @@ pub fn get_max_key_delay() -> Result<u16, error_code::ErrorCode<error_code::Syst
get_keyboard_speed().map(|raw_speed| raw_speed_to_millis(raw_speed as u8) * 8 / 10)
}

pub fn is_key_pressed(v_key: i32) -> Result<bool, error_code::ErrorCode<error_code::SystemCategory>> {
pub fn is_key_pressed(
v_key: i32,
) -> Result<bool, error_code::ErrorCode<error_code::SystemCategory>> {
// Mask as per https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getasynckeystate
let mask = 1i16 << 15;
get_async_key_state(v_key).map(|state| state & mask != 0)
}
}
22 changes: 14 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@ use std::mem;
use std::sync::{atomic, RwLock};
use winapi::um::winuser;

use crate::{
key_utils::trigger_keys,
winapi_functions::{
add_clipboard_format_listener, create_window_ex_a, register_class_ex_a, register_hotkey,
remove_clipboard_format_listener, sleep, unregister_hotkey,
},
};

const MAX_RETRIES: u8 = 10;

// Have to use global variables to allow access from C callback
static INTERNAL_UPDATE: atomic::AtomicBool = atomic::AtomicBool::new(false);
static MAX_HISTORY: atomic::AtomicUsize = atomic::AtomicUsize::new(10);

lazy_static! {
static ref CB_HISTORY: RwLock<VecDeque<String>> = RwLock::new(VecDeque::new());
}

use crate::{
key_utils::trigger_keys,
winapi_functions::{
add_clipboard_format_listener, create_window_ex_a, register_class_ex_a, register_hotkey,
remove_clipboard_format_listener, sleep, unregister_hotkey,
},
};

extern "system" fn message_watcher_proc(
h_wnd: *mut winapi::shared::windef::HWND__,
u_msg: u32,
Expand Down Expand Up @@ -135,8 +136,10 @@ extern "system" fn message_watcher_proc(
}

pub fn run(opts: Opts) {
// Move options to global variables
MAX_HISTORY.store(opts.max_history, atomic::Ordering::Relaxed);

// Create and register a class
let class_name = "filo-clipboard_class";
let window_name = "filo-clipboard";

Expand All @@ -157,6 +160,8 @@ pub fn run(opts: Opts) {
};

register_class_ex_a(&lp_wnd_class).unwrap();

// Create the invisible window
create_window_ex_a(
winuser::WS_EX_LEFT,
class_name,
Expand All @@ -173,6 +178,7 @@ pub fn run(opts: Opts) {
)
.unwrap();

// Event loop
let mut lp_msg = winuser::MSG::default();
// println!("Ready");
unsafe {
Expand Down

0 comments on commit 264935a

Please sign in to comment.