diff --git a/native/libmacbridge/bridge.h b/native/libmacbridge/bridge.h index 9a841da..6c9dcf9 100644 --- a/native/libmacbridge/bridge.h +++ b/native/libmacbridge/bridge.h @@ -56,8 +56,17 @@ int32_t get_active_app_bundle(char * buffer, int32_t size); */ int32_t get_active_app_identifier(char * buffer, int32_t size); +// CLIPBOARD + +/* + * Return the clipboard text + */ +int32_t get_clipboard(char * buffer, int32_t size); + +/* + * Set the clipboard text + */ +int32_t set_clipboard(char * text); + }; - - - #endif //ESPANSO_BRIDGE_H diff --git a/native/libmacbridge/bridge.mm b/native/libmacbridge/bridge.mm index 7948fe6..b3325f3 100644 --- a/native/libmacbridge/bridge.mm +++ b/native/libmacbridge/bridge.mm @@ -121,4 +121,30 @@ int32_t get_active_app_identifier(char * buffer, int32_t size) { [bundleId release]; return 1; +} + +int32_t get_clipboard(char * buffer, int32_t size) { + NSPasteboard *pasteboard = [NSPasteboard generalPasteboard]; + for (id element in pasteboard.pasteboardItems) { + NSString *string = [element stringForType: NSPasteboardTypeString]; + if (string != NULL) { + const char * text = [string UTF8String]; + snprintf(buffer, size, "%s", text); + + [string release]; + + return 1; + } + } + + return -1; +} + +int32_t set_clipboard(char * text) { + NSPasteboard *pasteboard = [NSPasteboard generalPasteboard]; + NSArray *array = @[NSPasteboardTypeString]; + [pasteboard declareTypes:array owner:nil]; + + NSString *nsText = [NSString stringWithUTF8String:text]; + [pasteboard setString:nsText forType:NSPasteboardTypeString]; } \ No newline at end of file diff --git a/src/bridge/macos.rs b/src/bridge/macos.rs index 5181608..40a0d03 100644 --- a/src/bridge/macos.rs +++ b/src/bridge/macos.rs @@ -7,6 +7,10 @@ extern { pub fn get_active_app_bundle(buffer: *mut c_char, size: i32) -> i32; pub fn get_active_app_identifier(buffer: *mut c_char, size: i32) -> i32; + // Clipboard + pub fn get_clipboard(buffer: *mut c_char, size: i32) -> i32; + pub fn set_clipboard(text: *const c_char) -> i32; + // Keyboard pub fn register_keypress_callback(s: *const c_void, cb: extern fn(_self: *mut c_void, *const u8, diff --git a/src/clipboard/macos.rs b/src/clipboard/macos.rs index c8ceb42..0d0d18c 100644 --- a/src/clipboard/macos.rs +++ b/src/clipboard/macos.rs @@ -1,14 +1,37 @@ +use std::os::raw::c_char; +use crate::bridge::macos::{get_clipboard, set_clipboard}; +use std::ffi::{CStr, CString}; + pub struct MacClipboardManager { } impl super::ClipboardManager for MacClipboardManager { fn get_clipboard(&self) -> Option { - unimplemented!(); + unsafe { + let mut buffer : [c_char; 2000] = [0; 2000]; + let res = get_clipboard(buffer.as_mut_ptr(), buffer.len() as i32); + + if res > 0 { + let c_string = CStr::from_ptr(buffer.as_ptr()); + + let string = c_string.to_str(); + if let Ok(string) = string { + return Some((*string).to_owned()); + } + } + } + + None } fn set_clipboard(&self, payload: &str) { - unimplemented!(); + let res = CString::new(payload); + if let Ok(cstr) = res { + unsafe { + set_clipboard(cstr.as_ptr()); + } + } } } diff --git a/src/main.rs b/src/main.rs index 200c594..d0acf5a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use crate::keyboard::{KeyboardInterceptor, KeyEvent}; use crate::matcher::Matcher; use crate::matcher::scrolling::ScrollingMatcher; use crate::engine::Engine; +use crate::clipboard::ClipboardManager; use crate::config::{ConfigSet, RuntimeConfigManager}; use crate::ui::UIManager; use std::{thread, time};