Skip to content

Commit 1da986a

Browse files
kchibisovchrisduerr
authored andcommitted
Fix smithay-clipboard integration
Fixes: alacritty#2574
1 parent 5cf77bf commit 1da986a

File tree

6 files changed

+35
-51
lines changed

6 files changed

+35
-51
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3939
- Selecting trailing tab with semantic expansion
4040
- URL parser incorrectly handling Markdown URLs and angled brackets
4141
- Intermediate bytes of CSI sequences not checked
42+
- Wayland clipboard integration
4243

4344
## 0.3.3
4445

Diff for: Cargo.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: alacritty_terminal/src/clipboard.rs

+4-20
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ use std::ffi::c_void;
1717

1818
use copypasta::nop_clipboard::NopClipboardContext;
1919
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
20-
use copypasta::wayland_clipboard::{
21-
Clipboard as WaylandClipboardClipboard, Primary as WaylandPrimaryClipboard,
22-
WaylandClipboardContext,
23-
};
20+
use copypasta::wayland_clipboard;
2421
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
2522
use copypasta::x11_clipboard::{Primary as X11SelectionClipboard, X11ClipboardContext};
2623
use copypasta::{ClipboardContext, ClipboardProvider};
@@ -39,22 +36,9 @@ impl Clipboard {
3936
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
4037
pub fn new(display: Option<*mut c_void>) -> Self {
4138
if let Some(display) = display {
42-
return Self {
43-
clipboard: unsafe {
44-
Box::new(
45-
WaylandClipboardContext::<WaylandClipboardClipboard>::new_from_external(
46-
display,
47-
),
48-
)
49-
},
50-
selection: unsafe {
51-
Some(Box::new(
52-
WaylandClipboardContext::<WaylandPrimaryClipboard>::new_from_external(
53-
display,
54-
),
55-
))
56-
},
57-
};
39+
let (selection, clipboard) =
40+
unsafe { wayland_clipboard::create_clipboards_from_external(display) };
41+
return Self { clipboard: Box::new(clipboard), selection: Some(Box::new(selection)) };
5842
}
5943

6044
Self {

Diff for: copypasta/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ objc-foundation = "0.1"
1717

1818
[target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten"))))'.dependencies]
1919
x11-clipboard = "0.3"
20-
smithay-clipboard = "0.3.2"
20+
smithay-clipboard = "0.3.4"
2121
wayland-client = { version = "0.23.3", features = ["dlopen"] }
2222

2323
[target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten"))))'.dev-dependencies]

Diff for: copypasta/examples/wayland.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod wayland {
1414
extern crate copypasta;
1515
extern crate smithay_client_toolkit as sctk;
1616

17-
use wayland::copypasta::wayland_clipboard::{Clipboard, WaylandClipboardContext};
17+
use wayland::copypasta::wayland_clipboard::create_clipboards;
1818
use wayland::copypasta::ClipboardProvider;
1919

2020
use std::io::{Read, Seek, SeekFrom, Write};
@@ -37,7 +37,7 @@ mod wayland {
3737
Display::connect_to_env().expect("Failed to connect to the wayland server.");
3838
let env = Environment::from_display(&*display, &mut event_queue).unwrap();
3939

40-
let mut ctx = WaylandClipboardContext::<Clipboard>::new(&display);
40+
let (mut ctx, _) = create_clipboards(&display);
4141
let cb_contents = Arc::new(Mutex::new(String::new()));
4242

4343
let seat = env.manager.instantiate_range(2, 6, NewProxy::implement_dummy).unwrap();

Diff for: copypasta/src/wayland_clipboard.rs

+24-25
Original file line numberDiff line numberDiff line change
@@ -14,57 +14,56 @@
1414

1515
use std::error::Error;
1616
use std::ffi::c_void;
17-
use std::marker::PhantomData;
17+
use std::sync::{Arc, Mutex};
1818

1919
use smithay_clipboard::WaylandClipboard;
20+
2021
use wayland_client::sys::client::wl_display;
2122
use wayland_client::Display;
2223

2324
use common::ClipboardProvider;
2425

25-
pub trait ClipboardType: Send {}
26+
pub struct Clipboard {
27+
context: Arc<Mutex<WaylandClipboard>>,
28+
}
2629

27-
pub struct Clipboard;
28-
impl ClipboardType for Clipboard {}
30+
pub struct Primary {
31+
context: Arc<Mutex<WaylandClipboard>>,
32+
}
2933

30-
pub struct Primary;
31-
impl ClipboardType for Primary {}
34+
pub fn create_clipboards(display: &Display) -> (Primary, Clipboard) {
35+
let context = Arc::new(Mutex::new(WaylandClipboard::new(display)));
3236

33-
pub struct WaylandClipboardContext<T: ClipboardType>(WaylandClipboard, PhantomData<T>);
37+
(Primary { context: context.clone() }, Clipboard { context } )
38+
}
3439

35-
impl<T: ClipboardType> WaylandClipboardContext<T> {
36-
/// Create a new clipboard context.
37-
pub fn new(display: &Display) -> Self {
38-
WaylandClipboardContext(WaylandClipboard::new(display), PhantomData)
39-
}
40+
pub unsafe fn create_clipboards_from_external(display: *mut c_void) -> (Primary, Clipboard) {
41+
let context =
42+
Arc::new(Mutex::new(WaylandClipboard::new_from_external(display as *mut wl_display)));
4043

41-
/// Create a new clipboard context from an external pointer.
42-
pub unsafe fn new_from_external(display: *mut c_void) -> Self {
43-
WaylandClipboardContext(
44-
WaylandClipboard::new_from_external(display as *mut wl_display),
45-
PhantomData,
46-
)
47-
}
44+
(Primary { context: context.clone() }, Clipboard { context} )
4845
}
4946

50-
impl ClipboardProvider for WaylandClipboardContext<Clipboard> {
47+
impl ClipboardProvider for Clipboard {
5148
fn get_contents(&mut self) -> Result<String, Box<dyn Error>> {
52-
Ok(self.0.load(None))
49+
Ok(self.context.lock().unwrap().load(None))
5350
}
5451

5552
fn set_contents(&mut self, data: String) -> Result<(), Box<dyn Error>> {
56-
self.0.store(None, data);
53+
self.context.lock().unwrap().store(None, data);
54+
5755
Ok(())
5856
}
5957
}
6058

61-
impl ClipboardProvider for WaylandClipboardContext<Primary> {
59+
impl ClipboardProvider for Primary {
6260
fn get_contents(&mut self) -> Result<String, Box<dyn Error>> {
63-
Ok(self.0.load_primary(None))
61+
Ok(self.context.lock().unwrap().load_primary(None))
6462
}
6563

6664
fn set_contents(&mut self, data: String) -> Result<(), Box<dyn Error>> {
67-
self.0.store_primary(None, data);
65+
self.context.lock().unwrap().store_primary(None, data);
66+
6867
Ok(())
6968
}
7069
}

0 commit comments

Comments
 (0)