From 23939a59b346b9d6b69978645ed76f09d01e5feb Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Tue, 5 May 2020 21:20:54 +0200 Subject: [PATCH] Add option to delay injected text on Linux. See #248 --- native/liblinuxbridge/bridge.cpp | 9 +++++++-- native/liblinuxbridge/bridge.h | 2 +- native/liblinuxbridge/fast_xdo.cpp | 3 +-- src/bridge/linux.rs | 2 +- src/config/mod.rs | 4 ++++ src/keyboard/linux.rs | 2 +- 6 files changed, 15 insertions(+), 7 deletions(-) diff --git a/native/liblinuxbridge/bridge.cpp b/native/liblinuxbridge/bridge.cpp index 5db95c7..e7cb2f4 100644 --- a/native/liblinuxbridge/bridge.cpp +++ b/native/liblinuxbridge/bridge.cpp @@ -325,7 +325,7 @@ void fast_release_all_keys() { XFlush(xdo_context->xdpy); } -void fast_send_string(const char * string) { +void fast_send_string(const char * string, int32_t delay) { // It may happen that when an expansion is triggered, some keys are still pressed. // This causes a problem if the expanded match contains that character, as the injection // will not be able to register that keypress (as it is already pressed). @@ -337,7 +337,12 @@ void fast_send_string(const char * string) { int revert_to; XGetInputFocus(xdo_context->xdpy, &focused, &revert_to); - fast_enter_text_window(xdo_context, focused, string, 1); + int actual_delay = 1; + if delay > 0 { + actual_delay = delay * 1000; + } + + fast_enter_text_window(xdo_context, focused, string, actual_delay); } void _fast_send_keycode_to_focused_window(int KeyCode, int32_t count, int32_t delay) { diff --git a/native/liblinuxbridge/bridge.h b/native/liblinuxbridge/bridge.h index 9402bf0..5e2c359 100644 --- a/native/liblinuxbridge/bridge.h +++ b/native/liblinuxbridge/bridge.h @@ -65,7 +65,7 @@ extern "C" void send_string(const char * string); /* * Type the given string by simulating Key Presses using a faster inject method */ -extern "C" void fast_send_string(const char * string); +extern "C" void fast_send_string(const char * string, int32_t delay); /* * Send the backspace keypress, *count* times. diff --git a/native/liblinuxbridge/fast_xdo.cpp b/native/liblinuxbridge/fast_xdo.cpp index 0d60fcb..4308941 100644 --- a/native/liblinuxbridge/fast_xdo.cpp +++ b/native/liblinuxbridge/fast_xdo.cpp @@ -215,8 +215,7 @@ int fast_enter_text_window(const xdo_t *xdo, Window window, const char *string, key.needs_binding = 0; fast_send_keysequence_window_list_do(xdo, window, &key, 1, False, NULL, delay / 2); - /* XXX: Flush here or at the end? or never? */ - //XFlush(xdo->xdpy); + XFlush(xdo->xdpy); } /* walk string generating a keysequence */ //free(keys); diff --git a/src/bridge/linux.rs b/src/bridge/linux.rs index a3f96f7..5c0ec4c 100644 --- a/src/bridge/linux.rs +++ b/src/bridge/linux.rs @@ -48,7 +48,7 @@ extern { pub fn trigger_ctrl_alt_paste(); pub fn trigger_copy(); - pub fn fast_send_string(string: *const c_char); + pub fn fast_send_string(string: *const c_char, delay: i32); pub fn fast_delete_string(count: i32, delay: i32); pub fn fast_left_arrow(count: i32); pub fn fast_send_enter(); diff --git a/src/config/mod.rs b/src/config/mod.rs index cb6b215..1c18e15 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -63,6 +63,7 @@ fn default_enable_passive() -> bool { false } fn default_enable_active() -> bool { true } fn default_backspace_limit() -> i32 { 3 } fn default_backspace_delay() -> i32 { 0 } +fn default_inject_delay() -> i32 { 0 } fn default_restore_clipboard_delay() -> i32 { 300 } fn default_exclude_default_entries() -> bool {false} fn default_secure_input_watcher_enabled() -> bool {true} @@ -172,6 +173,9 @@ pub struct Configs { #[serde(default = "default_backspace_delay")] pub backspace_delay: i32, + #[serde(default = "default_inject_delay")] + pub inject_delay: i32, + #[serde(default = "default_matches")] pub matches: Vec, diff --git a/src/keyboard/linux.rs b/src/keyboard/linux.rs index a33ceba..622c1e1 100644 --- a/src/keyboard/linux.rs +++ b/src/keyboard/linux.rs @@ -32,7 +32,7 @@ impl super::KeyboardManager for LinuxKeyboardManager { match res { Ok(cstr) => unsafe { if active_config.fast_inject { - fast_send_string(cstr.as_ptr()); + fast_send_string(cstr.as_ptr(), active_config.inject_delay); }else{ send_string(cstr.as_ptr()); }