Skip to content

Commit

Permalink
4.0.0-beta.3: Fix linux impl
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh committed Jan 13, 2024
1 parent a0cef27 commit ca78c3b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
20 changes: 18 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
name: Rust

on: [push, pull_request]
on:
push:
branches:
- master
paths:
- '.github/workflows/rust.yml'
- 'src/**.rs'
- 'tests/**.rs'
- 'Cargo.toml'
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
branches:
- '**'
paths:
- '.github/workflows/rust.yml'
- 'src/**.rs'
- 'tests/**.rs'
- 'Cargo.toml'

jobs:
build:

runs-on: ${{ matrix.os }}

strategy:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "clipboard-master"
version = "4.0.0-beta.2"
version = "4.0.0-beta.3"
authors = ["Douman <[email protected]>"]
keywords = ["Windows", "winapi", "clipboard"]
description = "Simple utility crate to monitor clipboard changes"
Expand Down
56 changes: 39 additions & 17 deletions src/master/x11.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use crate::{CallbackResult, ClipboardHandler};

use std::io;
use std::sync::mpsc::{self, SyncSender, Receiver, sync_channel};

///Shutdown channel
///
///On drop requests shutdown to gracefully close clipboard listener as soon as possible.
pub struct Shutdown {
sender: SyncSender<()>,
}

impl Drop for Shutdown {
#[inline(always)]
fn drop(&mut self) {
let _ = self.sender.send(());
}
}

Expand All @@ -22,21 +26,28 @@ impl Drop for Shutdown {
///- On `windows` it creates dummy window that monitors each clipboard change message.
pub struct Master<H> {
handler: H,
sender: SyncSender<()>,
recv: Receiver<()>
}

impl<H: ClipboardHandler> Master<H> {
#[inline(always)]
///Creates new instance.
pub fn new(handler: H) -> io::Result<Self> {
let (sender, recv) = sync_channel(0);

Ok(Self {
handler,
sender,
recv,
})
}

#[inline(always)]
///Creates shutdown channel.
pub fn shutdown_channel(&self) -> Shutdown {
Shutdown {
sender: self.sender.clone()
}
}

Expand All @@ -55,32 +66,43 @@ impl<H: ClipboardHandler> Master<H> {
};

loop {
let res = clipboard.load_wait(
let res = clipboard.load(
clipboard.getter.atoms.clipboard,
clipboard.getter.atoms.incr,
clipboard.getter.atoms.property,
self.handler.sleep_interval(),
);
if let Err(error) = res {
let error = io::Error::new(
io::ErrorKind::Other,
format!("Failed to load clipboard: {:?}", error),
);
match res {
Ok(_) => {
match self.handler.on_clipboard_change() {
CallbackResult::Next => (),
CallbackResult::Stop => break,
CallbackResult::StopWithError(error) => {
return Err(error);
}
}
},
Err(x11_clipboard::error::Error::Timeout) => (),
Err(error) => {
let error = io::Error::new(
io::ErrorKind::Other,
format!("Failed to load clipboard: {:?}", error),
);

match self.handler.on_clipboard_error(error) {
CallbackResult::Next => continue,
CallbackResult::Stop => break,
CallbackResult::StopWithError(error) => {
return Err(error);
match self.handler.on_clipboard_error(error) {
CallbackResult::Next => (),
CallbackResult::Stop => break,
CallbackResult::StopWithError(error) => {
return Err(error);
}
}
}
}

match self.handler.on_clipboard_change() {
CallbackResult::Next => (),
CallbackResult::Stop => break,
CallbackResult::StopWithError(error) => {
return Err(error);
}
match self.recv.try_recv() {
Ok(()) => break,
Err(mpsc::TryRecvError::Empty) => continue,
Err(mpsc::TryRecvError::Disconnected) => break,
}
}

Expand Down

0 comments on commit ca78c3b

Please sign in to comment.