Add macos clipboard bridge implementation

This commit is contained in:
Federico Terzi 2019-09-09 16:43:32 +02:00
parent 9216e2c4de
commit 1c0266c793
5 changed files with 68 additions and 5 deletions

View File

@ -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

View File

@ -122,3 +122,29 @@ int32_t get_active_app_identifier(char * buffer, int32_t size) {
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];
}

View File

@ -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,

View File

@ -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<String> {
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());
}
}
}
}

View File

@ -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};