Merge branch 'feature-pointer' into dev

This commit is contained in:
Federico Terzi 2019-10-25 22:37:12 +02:00
commit 2c9a9c2287
15 changed files with 120 additions and 39 deletions

View File

@ -278,6 +278,12 @@ void delete_string(int32_t count) {
}
}
void left_arrow(int32_t count) {
for (int i = 0; i<count; i++) {
xdo_send_keysequence_window(xdo_context, CURRENTWINDOW, "Left", 8000);
}
}
void trigger_paste() {
xdo_send_keysequence_window(xdo_context, CURRENTWINDOW, "Control_L+v", 8000);
}
@ -452,4 +458,4 @@ int32_t is_current_window_terminal() {
}
return 0;
}
}

View File

@ -67,6 +67,11 @@ extern "C" void send_string(const char * string);
*/
extern "C" void delete_string(int32_t count);
/*
* Send the left arrow keypress, *count* times.
*/
extern "C" void left_arrow(int32_t count);
/*
* Trigger normal paste ( Pressing CTRL+V )
*/

View File

@ -65,6 +65,11 @@ void send_string(const char * string);
*/
void send_vkey(int32_t vk);
/*
* Send the Virtual Key press multiple times
*/
void send_multi_vkey(int32_t vk, int32_t count);
/*
* Send the backspace keypress, *count* times.
*/

View File

@ -107,23 +107,7 @@ void send_string(const char * string) {
}
void delete_string(int32_t count) {
dispatch_async(dispatch_get_main_queue(), ^(void) {
for (int i = 0; i < count; i++) {
CGEventRef keydown;
keydown = CGEventCreateKeyboardEvent(NULL, 0x33, true);
CGEventPost(kCGHIDEventTap, keydown);
CFRelease(keydown);
usleep(2000);
CGEventRef keyup;
keyup = CGEventCreateKeyboardEvent(NULL, 0x33, false);
CGEventPost(kCGHIDEventTap, keyup);
CFRelease(keyup);
usleep(2000);
}
});
send_multi_vkey(0x33, count);
}
void send_vkey(int32_t vk) {
@ -133,14 +117,34 @@ void send_vkey(int32_t vk) {
CGEventPost(kCGHIDEventTap, keydown);
CFRelease(keydown);
usleep(2000);
usleep(500);
CGEventRef keyup;
keyup = CGEventCreateKeyboardEvent(NULL, vk, false);
CGEventPost(kCGHIDEventTap, keyup);
CFRelease(keyup);
usleep(2000);
usleep(500);
});
}
void send_multi_vkey(int32_t vk, int32_t count) {
dispatch_async(dispatch_get_main_queue(), ^(void) {
for (int i = 0; i < count; i++) {
CGEventRef keydown;
keydown = CGEventCreateKeyboardEvent(NULL, vk, true);
CGEventPost(kCGHIDEventTap, keydown);
CFRelease(keydown);
usleep(500);
CGEventRef keyup;
keyup = CGEventCreateKeyboardEvent(NULL, vk, false);
CGEventPost(kCGHIDEventTap, keyup);
CFRelease(keyup);
usleep(500);
}
});
}

View File

@ -453,24 +453,7 @@ void send_string(const wchar_t * string) {
* Send the backspace keypress, *count* times.
*/
void delete_string(int32_t count) {
std::vector<INPUT> vec;
for (int i = 0; i < count; i++) {
INPUT input = { 0 };
input.type = INPUT_KEYBOARD;
input.ki.wScan = 0;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
input.ki.wVk = VK_BACK;
input.ki.dwFlags = 0; // 0 for key press
vec.push_back(input);
input.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
vec.push_back(input);
}
SendInput(vec.size(), vec.data(), sizeof(INPUT));
send_multi_vkey(VK_BACK, count);
}
void send_vkey(int32_t vk) {
@ -492,6 +475,27 @@ void send_vkey(int32_t vk) {
SendInput(vec.size(), vec.data(), sizeof(INPUT));
}
void send_multi_vkey(int32_t vk, int32_t count) {
std::vector<INPUT> vec;
for (int i = 0; i < count; i++) {
INPUT input = { 0 };
input.type = INPUT_KEYBOARD;
input.ki.wScan = 0;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
input.ki.wVk = vk;
input.ki.dwFlags = 0; // 0 for key press
vec.push_back(input);
input.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
vec.push_back(input);
}
SendInput(vec.size(), vec.data(), sizeof(INPUT));
}
void trigger_paste() {
std::vector<INPUT> vec;

View File

@ -64,6 +64,11 @@ extern "C" void send_string(const wchar_t * string);
*/
extern "C" void send_vkey(int32_t vk);
/*
* Send the given Virtual Key press multiple times
*/
extern "C" void send_multi_vkey(int32_t vk, int32_t count);
/*
* Send the backspace keypress, *count* times.
*/

View File

@ -39,6 +39,7 @@ extern {
pub fn send_string(string: *const c_char);
pub fn delete_string(count: i32);
pub fn left_arrow(count: i32);
pub fn trigger_paste();
pub fn trigger_terminal_paste();
}

View File

@ -55,6 +55,7 @@ extern {
pub fn send_string(string: *const c_char);
pub fn send_vkey(vk: i32);
pub fn send_multi_vkey(vk: i32, count: i32);
pub fn delete_string(count: i32);
pub fn trigger_paste();
}

View File

@ -55,6 +55,7 @@ extern {
pub fn eventloop();
pub fn send_string(string: *const u16);
pub fn send_vkey(vk: i32);
pub fn send_multi_vkey(vk: i32, count: i32);
pub fn delete_string(count: i32);
pub fn trigger_paste();
}

View File

@ -160,6 +160,25 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa
// Convert Windows style newlines into unix styles
target_string = target_string.replace("\r\n", "\n");
// Calculate cursor rewind moves if a Cursor Hint is present
let index = target_string.find("$|$");
let cursor_rewind = if let Some(index) = index {
// Convert the byte index to a char index
let char_str = &target_string[0..index];
let char_index = char_str.chars().count();
let total_size = target_string.chars().count();
// Remove the $|$ placeholder
target_string = target_string.replace("$|$", "");
// Calculate the amount of rewind moves needed (LEFT ARROW).
// Subtract also 3, equal to the number of chars of the placeholder "$|$"
let moves = (total_size - char_index - 3) as i32;
Some(moves)
}else{
None
};
match config.backend {
BackendType::Inject => {
// Send the expected string. On linux, newlines are managed automatically
@ -185,6 +204,11 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa
self.keyboard_manager.trigger_paste();
},
}
if let Some(moves) = cursor_rewind {
// Simulate left arrow key presses to bring the cursor into the desired position
self.keyboard_manager.move_cursor_left(moves);
}
}
fn on_enable_update(&self, status: bool) {

View File

@ -53,4 +53,10 @@ impl super::KeyboardManager for LinuxKeyboardManager {
fn delete_string(&self, count: i32) {
unsafe {delete_string(count)}
}
fn move_cursor_left(&self, count: i32) {
unsafe {
left_arrow(count);
}
}
}

View File

@ -48,4 +48,11 @@ impl super::KeyboardManager for MacKeyboardManager {
fn delete_string(&self, count: i32) {
unsafe {delete_string(count)}
}
fn move_cursor_left(&self, count: i32) {
unsafe {
// Simulate the Left arrow count times
send_multi_vkey(0x7B, count);
}
}
}

View File

@ -31,6 +31,7 @@ pub trait KeyboardManager {
fn send_enter(&self);
fn trigger_paste(&self);
fn delete_string(&self, count: i32);
fn move_cursor_left(&self, count: i32);
}
// WINDOWS IMPLEMENTATION

View File

@ -55,4 +55,11 @@ impl super::KeyboardManager for WindowsKeyboardManager {
delete_string(count)
}
}
fn move_cursor_left(&self, count: i32) {
unsafe {
// Send the left arrow key multiple times
send_multi_vkey(0x25, count)
}
}
}

View File

@ -55,6 +55,10 @@ impl<'a> From<&'a AutoMatch> for Match{
static ref VAR_REGEX: Regex = Regex::new("\\{\\{\\s*(\\w+)\\s*\\}\\}").unwrap();
}
// TODO: may need to replace windows newline (\r\n) with newline only (\n)
let new_replace = other.replace.clone();
// Check if the match contains variables
let has_vars = VAR_REGEX.is_match(&other.replace);
@ -70,7 +74,7 @@ impl<'a> From<&'a AutoMatch> for Match{
Self {
trigger: other.trigger.clone(),
replace: other.replace.clone(),
replace: new_replace,
vars: other.vars.clone(),
word: other.word.clone(),
_has_vars: has_vars,