Skip to content

Commit

Permalink
Merge pull request #46 from jasonterando/master
Browse files Browse the repository at this point in the history
First pass at implementing configurable read timeout for X11
  • Loading branch information
ChurchTao authored Sep 20, 2024
2 parents 0e93637 + 1c05d70 commit 7c73b43
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,22 @@ fn main() {

```

## X11 - Clipboard Read Timeout

By default, in X11 clipboard-rs implements a read timeout of 500 ms. You can override or disable this timeout by creating **ClipboardContext** using `new_with_options`:

```rust
#[cfg(unix)]
fn setup_clipboard() -> ClipboardContext {
ClipboardContext::new_with_options(ClipboardContextX11Options { read_timeout: None }).unwrap()
}

#[cfg(not(unix))]
fn setup_clipboard(ctx: &mut ClipboardContext) -> ClipboardContext{
ClipboardContext::new().unwrap()
}
```

## Contributing

You are welcome to submit PRs and issues and contribute your code or ideas to the project. Due to my limited level, the library may also have bugs. You are welcome to point them out and I will modify them as soon as possible.
Expand Down
15 changes: 14 additions & 1 deletion examples/image.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(target_os = "linux")]
use clipboard_rs::ClipboardContextX11Options;
use clipboard_rs::{common::RustImage, Clipboard, ClipboardContext};

#[cfg(target_os = "macos")]
Expand All @@ -15,8 +17,19 @@ const TMP_PATH: &str = "C:\\Windows\\Temp\\";
))]
const TMP_PATH: &str = "/tmp/";

#[cfg(target_os = "linux")]
fn setup_clipboard() -> ClipboardContext {
ClipboardContext::new_with_options(ClipboardContextX11Options { read_timeout: None }).unwrap()
}

#[cfg(not(target_os = "linux"))]
fn setup_clipboard() -> ClipboardContext {
ClipboardContext::new().unwrap()
}

fn main() {
let ctx = ClipboardContext::new().unwrap();
let ctx = setup_clipboard();

let types = ctx.available_formats().unwrap();
println!("{:?}", types);

Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ pub mod common;
mod platform;
pub use common::{ClipboardContent, ClipboardHandler, ContentFormat, Result, RustImageData};
pub use image::imageops::FilterType;
#[cfg(target_os = "linux")]
pub use platform::ClipboardContextX11Options;
pub use platform::{ClipboardContext, ClipboardWatcherContext, WatcherShutdown};

pub trait Clipboard: Send {
/// zh: 获得剪切板当前内容的所有格式
/// en: Get all formats of the current content in the clipboard
Expand Down
4 changes: 3 additions & 1 deletion src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ mod x11;
target_os = "emscripten"
))
))]
pub use x11::{ClipboardContext, ClipboardWatcherContext, WatcherShutdown};
pub use x11::{
ClipboardContext, ClipboardContextX11Options, ClipboardWatcherContext, WatcherShutdown,
};
25 changes: 23 additions & 2 deletions src/platform/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,20 @@ x11rb::atom_manager! {
}
}

pub const DEFAULT_READ_TIMEOUT: u64 = 500;

// zh: 用于创建 X11 剪贴板上下文的选项
// en: Options for creating an X11 clipboard context
pub struct ClipboardContextX11Options {
// zh: 剪贴板读取操作超时
// en: Timeout for clipboard read operations
pub read_timeout: Option<Duration>,
}

const FILE_PATH_PREFIX: &str = "file://";
pub struct ClipboardContext {
inner: Arc<InnerContext>,
read_timeout: Option<Duration>,
}

struct ClipboardData {
Expand Down Expand Up @@ -293,6 +304,12 @@ impl InnerContext {

impl ClipboardContext {
pub fn new() -> Result<Self> {
Self::new_with_options(ClipboardContextX11Options {
read_timeout: Some(Duration::from_millis(DEFAULT_READ_TIMEOUT)),
})
}

pub fn new_with_options(options: ClipboardContextX11Options) -> Result<Self> {
// build connection to X server
let ctx = InnerContext::new()?;
let ctx_arc = Arc::new(ctx);
Expand All @@ -304,7 +321,11 @@ impl ClipboardContext {
println!("process_server_req error: {:?}", e);
}
});
Ok(Self { inner: ctx_arc })

Ok(Self {
inner: ctx_arc,
read_timeout: options.read_timeout,
})
}

fn read(&self, format: &Atom) -> Result<Vec<u8>> {
Expand All @@ -324,7 +345,7 @@ impl ClipboardContext {
clipboard,
*format,
atoms.PROPERTY,
Some(Duration::from_millis(500)),
self.read_timeout,
sequence_num,
)?;

Expand Down

0 comments on commit 7c73b43

Please sign in to comment.