Repository: espanso/espanso (language: rust)

Issue:
Copy Paste loosing after uses 
Hi,

I use espanso with Windows 10. I loose paste function after using espanso.

To retrieve it, i need to reload config.

Thx


Discussion / hints:
Hey @jeff38100,

Thank you for reporting! Indeed I noticed this problem myself a couple of days ago. It doesn't always happen, but sometimes it does. Do you have any idea of what match could be triggering it? I wasn't able to reproduce it now

Let me know if you figure out the steps to reproduce it.

Cheers :)

Maybe related to this I got another bug.

I use "dt-" to get the actual date "2020-08-26" with espanso. 
This replacement works only the first time. The second try gives me "2dt-" instead of "2020-08-26". 
After "Reload config" it works as it should, however only at the first time. Next time the replacement shows "2dt-".
Same with ":espanso" - it gives at the second time "H:espanso" back. 

Windows 10 Pro Version 2004, 64bit.
@stdt thank you for the report. I'm not able to reproduce the issue reliably, but it does happen sometimes and it's pretty annoying. I'll make sure to investigate on this one asap. Could you please post the output of `espanso log` here?

Cheers :)
Guys I've tracked that down, I'll ship a fix in the next release (hopefully coming in a couple of days)
Thanks for the issue. I had exactly the same problem and I was actually trying tons of methods to fix it (I didn't think it was caused by espanso). But I personnaly need to completely stop espanso so as to make paste function work again.
Guys I released the new version, please update espanso and let me know if the problem is still present
My bad, it seems the problem is still there after this release. I'll make sure to investigate further
Actually, this is pretty strange. I can't reproduce it anymore, so this release might have fixed the problem. When you have the chance, install the new version and try it out
Still the same problem for me... Can't use copy/paste (and it makes impossible the understanding of my other problems) with 0.7.1

Fix patch:
diff --git a/native/libwinbridge/bridge.cpp b/native/libwinbridge/bridge.cpp
index 55d9567d..6db15c9d 100644
--- a/native/libwinbridge/bridge.cpp
+++ b/native/libwinbridge/bridge.cpp
@@ -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;
-    }
-
-    HGLOBAL hMem = GlobalLock(hData);
-    if (!hMem) {
-        return -3;
+        result = -2;
+    }else{
+        HGLOBAL hMem = GlobalLock(hData);
+        if (!hMem) {
+            result = -3;
+        }else{
+            GlobalUnlock(hMem);
+            swprintf(buffer, size, L"%s", hMem);
+        }
     }
 
-    GlobalUnlock(hMem);
-
-    swprintf(buffer, size, L"%s", hMem);
-
     CloseClipboard();
+    return result;
 }
 
 int32_t set_clipboard_image(wchar_t *path) {
diff --git a/src/config/mod.rs b/src/config/mod.rs
index 13dea998..58ab6265 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -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,
diff --git a/src/guard.rs b/src/guard.rs
index 2f5b1f6e..5ada9ffb 100644
--- a/src/guard.rs
+++ b/src/guard.rs
@@ -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);


Test patch: