2019-09-01 12:58:39 +00:00
|
|
|
use std::thread;
|
|
|
|
use std::sync::mpsc;
|
2019-09-01 14:50:20 +00:00
|
|
|
use std::os::raw::c_char;
|
|
|
|
use std::ffi::CString;
|
2019-09-01 12:58:39 +00:00
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct LinuxKeyboardInterceptor {
|
|
|
|
pub sender: mpsc::Sender<char>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl super::KeyboardInterceptor for LinuxKeyboardInterceptor {
|
|
|
|
fn initialize(&self) {
|
|
|
|
unsafe {
|
|
|
|
register_keypress_callback(self,keypress_callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start(&self) {
|
|
|
|
thread::spawn(|| {
|
|
|
|
unsafe {
|
|
|
|
initialize();
|
|
|
|
eventloop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LinuxKeyboardSender {
|
|
|
|
}
|
|
|
|
|
|
|
|
impl super::KeyboardSender for LinuxKeyboardSender {
|
|
|
|
fn send_string(&self, s: &str) {
|
2019-09-01 14:50:20 +00:00
|
|
|
let res = CString::new(s);
|
|
|
|
match res {
|
|
|
|
Ok(cstr) => unsafe { send_string(cstr.as_ptr()); }
|
|
|
|
Err(e) => panic!(e.to_string())
|
|
|
|
}
|
2019-09-01 12:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn delete_string(&self, count: i32) {
|
2019-09-01 14:50:20 +00:00
|
|
|
unsafe {delete_string(count)}
|
2019-09-01 12:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Native bridge code
|
|
|
|
|
|
|
|
extern fn keypress_callback(_self: *mut LinuxKeyboardInterceptor, raw_buffer: *const u8, len: i32) {
|
|
|
|
unsafe {
|
|
|
|
// Convert the received buffer to a character
|
|
|
|
let buffer = std::slice::from_raw_parts(raw_buffer, len as usize);
|
|
|
|
let r = String::from_utf8_lossy(buffer).chars().nth(0);
|
|
|
|
|
|
|
|
// Send the char through the channel
|
|
|
|
if let Some(c) = r {
|
|
|
|
//println!("'{}'",c);
|
|
|
|
(*_self).sender.send(c).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(improper_ctypes)]
|
|
|
|
#[link(name="linuxbridge", kind="static")]
|
|
|
|
extern {
|
|
|
|
fn register_keypress_callback(s: *const LinuxKeyboardInterceptor, cb: extern fn(_self: *mut LinuxKeyboardInterceptor, *const u8, i32));
|
|
|
|
fn initialize();
|
|
|
|
fn eventloop();
|
|
|
|
fn cleanup();
|
2019-09-01 14:50:20 +00:00
|
|
|
fn send_string(string: *const c_char);
|
|
|
|
fn delete_string(count: i32);
|
2019-09-01 12:58:39 +00:00
|
|
|
}
|