Fix clipboard bug on Windows. Fix #418

This commit is contained in:
Federico Terzi 2020-08-26 21:12:59 +02:00
parent 270b603e8f
commit 82caf06783
3 changed files with 23 additions and 22 deletions

View File

@ -791,6 +791,7 @@ int32_t start_process(wchar_t * _cmd) {
// CLIPBOARD
int32_t set_clipboard(wchar_t *text) {
int32_t result = 0;
const size_t len = wcslen(text) + 1;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len * sizeof(wchar_t));
memcpy(GlobalLock(hMem), text, len * sizeof(wchar_t));
@ -800,12 +801,14 @@ int32_t set_clipboard(wchar_t *text) {
}
EmptyClipboard();
if (!SetClipboardData(CF_UNICODETEXT, hMem)) {
return -2;
result = -2;
}
CloseClipboard();
return result;
}
int32_t get_clipboard(wchar_t *buffer, int32_t size) {
int32_t result = 0;
if (!OpenClipboard(NULL)) {
return -1;
}
@ -813,19 +816,19 @@ int32_t get_clipboard(wchar_t *buffer, int32_t size) {
// Get handle of clipboard object for ANSI text
HANDLE hData = GetClipboardData(CF_UNICODETEXT);
if (!hData) {
return -2;
result = -2;
}else{
HGLOBAL hMem = GlobalLock(hData);
if (!hMem) {
result = -3;
}else{
GlobalUnlock(hMem);
swprintf(buffer, size, L"%s", hMem);
}
}
HGLOBAL hMem = GlobalLock(hData);
if (!hMem) {
return -3;
}
GlobalUnlock(hMem);
swprintf(buffer, size, L"%s", hMem);
CloseClipboard();
return result;
}
int32_t set_clipboard_image(wchar_t *path) {

View File

@ -155,7 +155,7 @@ fn default_global_vars() -> Vec<MatchVariable> {
fn default_modulo_path() -> Option<String> {
None
}
fn default_mac_post_inject_delay() -> u64 {
fn default_post_inject_delay() -> u64 {
100
}
@ -245,8 +245,8 @@ pub struct Configs {
#[serde(default = "default_secure_input_watcher_interval")]
pub secure_input_watcher_interval: i32,
#[serde(default = "default_mac_post_inject_delay")]
pub mac_post_inject_delay: u64,
#[serde(default = "default_post_inject_delay")]
pub post_inject_delay: u64,
#[serde(default = "default_secure_input_notification")]
pub secure_input_notification: bool,

View File

@ -5,7 +5,7 @@ use std::sync::{atomic::AtomicBool, Arc};
pub struct InjectGuard {
is_injecting: Arc<AtomicBool>,
mac_post_inject_delay: u64,
post_inject_delay: u64,
}
impl InjectGuard {
@ -17,21 +17,19 @@ impl InjectGuard {
Self {
is_injecting,
mac_post_inject_delay: config.mac_post_inject_delay,
post_inject_delay: config.post_inject_delay,
}
}
}
impl Drop for InjectGuard {
fn drop(&mut self) {
debug!("releasing inject guard");
// On macOS, because the keyinjection is async, we need to wait a bit before
// Because the keyinjection is async, we need to wait a bit before
// giving back the control. Otherwise, the injected actions will be handled back
// by espanso itself.
if cfg!(target_os = "macos") {
std::thread::sleep(std::time::Duration::from_millis(self.mac_post_inject_delay));
}
std::thread::sleep(std::time::Duration::from_millis(self.post_inject_delay));
debug!("releasing inject guard");
// Re-allow espanso to interpret actions
self.is_injecting.store(false, Release);