From 0ba18eba51e94bae4b909064e613fac64630643b Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 1 Sep 2019 16:50:20 +0200 Subject: [PATCH] Add basic typing functionality on linux --- build.rs | 2 ++ native/liblinuxbridge/bridge.cpp | 21 +++++++++++++++++++++ native/liblinuxbridge/bridge.h | 10 ++++++++++ src/keyboard/linux.rs | 13 ++++++++++--- src/main.rs | 5 ++++- 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/build.rs b/build.rs index edf162b..c5f79f0 100644 --- a/build.rs +++ b/build.rs @@ -27,9 +27,11 @@ fn print_config() { #[cfg(target_os = "linux")] fn print_config() { + println!("cargo:rustc-link-search=native=/usr/lib/x86_64-linux-gnu/"); println!("cargo:rustc-link-lib=static=linuxbridge"); println!("cargo:rustc-link-lib=dylib=X11"); println!("cargo:rustc-link-lib=dylib=Xtst"); + println!("cargo:rustc-link-lib=dylib=xdo"); } fn main() diff --git a/native/liblinuxbridge/bridge.cpp b/native/liblinuxbridge/bridge.cpp index 57706fa..f8d7c37 100644 --- a/native/liblinuxbridge/bridge.cpp +++ b/native/liblinuxbridge/bridge.cpp @@ -1,5 +1,6 @@ #include "bridge.h" +#include #include #include #include @@ -12,6 +13,9 @@ #include #include #include +extern "C" { // Needed to avoid C++ compiler name mangling + #include +} /* This code uses the X11 Record Extension to receive keyboard @@ -46,6 +50,8 @@ Display *ctrl_disp = NULL; XRecordRange *record_range; XRecordContext context; +xdo_t * xdo_context; + // Callback invoked when a new key event occur. void event_callback (XPointer, XRecordInterceptData*); @@ -59,6 +65,8 @@ void register_keypress_callback(void * self, KeypressCallback callback) { int32_t initialize() { + setlocale(LC_ALL, ""); + /* Open the connections to the X server. RE recommends to open 2 connections to the X server: @@ -107,6 +115,8 @@ int32_t initialize() { if (!context) { return -5; } + + xdo_context = xdo_new(NULL); } int32_t eventloop() { @@ -123,6 +133,7 @@ void cleanup() { XFree (record_range); XCloseDisplay(data_disp); XCloseDisplay(ctrl_disp); + xdo_free(xdo_context); } void event_callback(XPointer p, XRecordInterceptData *hook) @@ -182,4 +193,14 @@ void event_callback(XPointer p, XRecordInterceptData *hook) } XRecordFreeData(hook); +} + +void send_string(const char * string) { + xdo_enter_text_window(xdo_context, CURRENTWINDOW, string, 0); +} + +void delete_string(int32_t count) { + for (int i = 0; i unsafe { send_string(cstr.as_ptr()); } + Err(e) => panic!(e.to_string()) + } } fn delete_string(&self, count: i32) { - + unsafe {delete_string(count)} } } @@ -60,4 +65,6 @@ extern { fn initialize(); fn eventloop(); fn cleanup(); + fn send_string(string: *const c_char); + fn delete_string(count: i32); } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 232bf5b..477dbd1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,10 @@ fn main() { let engine = Engine::new(&sender); - let matches = vec![Match{trigger:"e'".to_owned(), result: "รจ".to_owned()}, Match{trigger:":lol".to_owned(), result: "๐Ÿ˜‚".to_owned()}]; + let matches = vec![Match{trigger:"e'".to_owned(), result: "รจ".to_owned()}, + Match{trigger:":lol".to_owned(), result: "๐Ÿ˜‚".to_owned()}, + Match{trigger:":lll".to_owned(), result: "yo".to_owned()}, + ]; let mut matcher = ScrollingMatcher::new(&matches, &engine); matcher.watch(&rxc);