2019-08-30 17:45:27 +00:00
|
|
|
use std::thread;
|
|
|
|
use std::sync::mpsc;
|
2019-08-31 14:07:45 +00:00
|
|
|
use widestring::{U16CString};
|
2019-08-30 17:45:27 +00:00
|
|
|
|
|
|
|
#[repr(C)]
|
2019-08-30 19:24:03 +00:00
|
|
|
pub struct WindowsKeyboardInterceptor {
|
2019-08-30 17:45:27 +00:00
|
|
|
pub sender: mpsc::Sender<char>
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
register_keypress_callback(self,keypress_callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start(&self) {
|
|
|
|
thread::spawn(|| {
|
|
|
|
unsafe {
|
|
|
|
initialize_window();
|
|
|
|
eventloop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-30 19:24:03 +00:00
|
|
|
extern fn keypress_callback(_self: *mut WindowsKeyboardInterceptor, raw_buffer: *const i32, len: i32) {
|
2019-08-30 17:45:27 +00:00
|
|
|
unsafe {
|
|
|
|
// 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).unwrap();
|
|
|
|
|
|
|
|
// Send the char through the channel
|
|
|
|
(*_self).sender.send(r).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-31 14:07:45 +00:00
|
|
|
#[allow(improper_ctypes)]
|
2019-08-30 17:45:27 +00:00
|
|
|
#[link(name="winbridge", kind="static")]
|
|
|
|
extern {
|
2019-08-30 19:24:03 +00:00
|
|
|
fn register_keypress_callback(s: *const WindowsKeyboardInterceptor, cb: extern fn(_self: *mut WindowsKeyboardInterceptor, *const i32, i32));
|
2019-08-30 17:45:27 +00:00
|
|
|
fn initialize_window();
|
|
|
|
fn eventloop();
|
2019-08-30 19:24:03 +00:00
|
|
|
fn send_string(string: *const u16);
|
2019-08-31 14:07:45 +00:00
|
|
|
fn delete_string(count: i32);
|
2019-08-30 17:45:27 +00:00
|
|
|
}
|