2019-08-30 17:45:27 +00:00
|
|
|
use std::sync::mpsc;
|
2019-09-07 20:23:04 +00:00
|
|
|
use std::os::raw::{c_void};
|
2019-08-31 14:07:45 +00:00
|
|
|
use widestring::{U16CString};
|
2019-09-05 17:36:53 +00:00
|
|
|
use crate::keyboard::{KeyEvent, KeyModifier};
|
|
|
|
use crate::keyboard::KeyModifier::*;
|
2019-09-07 20:23:04 +00:00
|
|
|
use crate::bridge::windows::*;
|
2019-08-30 17:45:27 +00:00
|
|
|
|
|
|
|
#[repr(C)]
|
2019-08-30 19:24:03 +00:00
|
|
|
pub struct WindowsKeyboardInterceptor {
|
2019-09-05 17:18:55 +00:00
|
|
|
pub sender: mpsc::Sender<KeyEvent>
|
2019-08-30 17:45:27 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 19:24:03 +00:00
|
|
|
impl super::KeyboardInterceptor for WindowsKeyboardInterceptor {
|
2019-08-30 17:45:27 +00:00
|
|
|
fn initialize(&self) {
|
|
|
|
unsafe {
|
2019-09-07 20:23:04 +00:00
|
|
|
let self_ptr = self as *const WindowsKeyboardInterceptor as *const c_void;
|
|
|
|
register_keypress_callback(self_ptr,keypress_callback);
|
2019-09-05 17:36:53 +00:00
|
|
|
initialize_window();
|
2019-08-30 17:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start(&self) {
|
2019-09-05 17:36:53 +00:00
|
|
|
unsafe {
|
|
|
|
eventloop();
|
|
|
|
}
|
2019-08-30 17:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 19:24:03 +00:00
|
|
|
pub struct WindowsKeyboardSender {
|
|
|
|
}
|
|
|
|
|
|
|
|
impl super::KeyboardSender for WindowsKeyboardSender {
|
|
|
|
fn send_string(&self, s: &str) {
|
2019-08-31 14:07:45 +00:00
|
|
|
let res = U16CString::from_str(s);
|
|
|
|
match res {
|
|
|
|
Ok(s) => {
|
|
|
|
unsafe {
|
|
|
|
send_string(s.as_ptr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => println!("Error while sending string: {}", e.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-06 08:21:33 +00:00
|
|
|
fn send_enter(&self) {
|
|
|
|
unsafe {
|
|
|
|
// Send the VK_RETURN key press
|
|
|
|
send_vkey(0x0D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-07 20:23:04 +00:00
|
|
|
fn trigger_paste(&self) {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2019-08-31 14:07:45 +00:00
|
|
|
fn delete_string(&self, count: i32) {
|
2019-08-30 19:24:03 +00:00
|
|
|
unsafe {
|
2019-08-31 14:07:45 +00:00
|
|
|
delete_string(count)
|
2019-08-30 19:24:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 17:45:27 +00:00
|
|
|
// Native bridge code
|
|
|
|
|
2019-09-07 20:23:04 +00:00
|
|
|
extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const i32, len: i32,
|
2019-09-05 17:36:53 +00:00
|
|
|
is_modifier: i32, key_code: i32) {
|
2019-08-30 17:45:27 +00:00
|
|
|
unsafe {
|
2019-09-07 20:23:04 +00:00
|
|
|
let _self = _self as *mut WindowsKeyboardInterceptor;
|
|
|
|
|
2019-09-05 17:36:53 +00:00
|
|
|
if is_modifier == 0 { // Char event
|
|
|
|
// Convert the received buffer to a character
|
|
|
|
let buffer = std::slice::from_raw_parts(raw_buffer, len as usize);
|
|
|
|
let r = std::char::from_u32(buffer[0] as u32);
|
2019-08-30 17:45:27 +00:00
|
|
|
|
2019-09-05 17:36:53 +00:00
|
|
|
// Send the char through the channel
|
|
|
|
if let Some(c) = r {
|
|
|
|
(*_self).sender.send(KeyEvent::Char(c)).unwrap();
|
|
|
|
}
|
|
|
|
}else{ // Modifier event
|
|
|
|
let modifier: Option<KeyModifier> = match key_code {
|
|
|
|
0x5B | 0x5C => Some(META),
|
|
|
|
0x10 => Some(SHIFT),
|
|
|
|
0x12 => Some(ALT),
|
|
|
|
0x11 => Some(CTRL),
|
|
|
|
0x08 => Some(BACKSPACE),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(modifier) = modifier {
|
|
|
|
(*_self).sender.send(KeyEvent::Modifier(modifier)).unwrap();
|
|
|
|
}
|
|
|
|
}
|
2019-08-30 17:45:27 +00:00
|
|
|
}
|
|
|
|
}
|