From 8f867520520ecb80d846e60329d664ed89107204 Mon Sep 17 00:00:00 2001 From: DoumanAsh Date: Mon, 5 Feb 2024 18:42:58 +0900 Subject: [PATCH] Use the same x11 Clipboard instance to work around buggy dtor --- src/master/x11.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/master/x11.rs b/src/master/x11.rs index 54b12ac..1aad757 100644 --- a/src/master/x11.rs +++ b/src/master/x11.rs @@ -1,6 +1,7 @@ use crate::{CallbackResult, ClipboardHandler}; use std::io; +use std::sync::OnceLock; use std::sync::mpsc::{self, SyncSender, Receiver, sync_channel}; ///Shutdown channel @@ -54,8 +55,7 @@ impl Master { ///Starts Master by waiting for any change pub fn run(&mut self) -> io::Result<()> { - let clipboard = x11_clipboard::Clipboard::new(); - let clipboard = match clipboard { + let clipboard = match Self::x11_clipboard() { Ok(clipboard) => clipboard, Err(error) => { return Err(io::Error::new( @@ -108,4 +108,15 @@ impl Master { Ok(()) } + + ///Gets one time initialized x11 clipboard. + /// + ///This is only available on linux + /// + ///Prefer to use it on Linux as underlying `x11-clipboard` crate has buggy dtor + ///and doesn't clean up all resources associated with `Clipboard` + pub fn x11_clipboard() -> &'static Result { + static CLIP: OnceLock> = OnceLock::new(); + CLIP.get_or_init(x11_clipboard::Clipboard::new) + } }