-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsync.rs
262 lines (239 loc) · 9.03 KB
/
sync.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
use chrono::Local;
use std::collections::HashSet;
use std::{thread::sleep, time::Duration};
use wayland_client::ConnectError;
use wl_clipboard_rs::paste::Error as PasteError;
use crate::clipboard::*;
use crate::error::{MyError, MyResult, StandardizedError};
use crate::log::{self, concise_numbers};
pub fn get_clipboards() -> MyResult<Vec<Box<dyn Clipboard>>> {
log::debug!("identifying unique clipboards...");
let mut clipboards = get_clipboards_spec(get_wayland);
// let x11_backend = X11Backend::new()?;
clipboards.extend(get_clipboards_spec(get_x11));
let start = clipboards
.iter()
.map(|c| c.get().unwrap_or_default())
.find(|s| !s.is_empty())
.unwrap_or_default();
log::sensitive!(log::info, "Clipboard contents at the start: '{start}'");
let mut remove_me = HashSet::new();
let len = clipboards.len();
for i in 0..clipboards.len() {
if !remove_me.contains(&i) {
let cb1 = &clipboards[i];
for (j, cb2) in clipboards.iter().enumerate().take(len).skip(i + 1) {
if are_same(&**cb1, &**cb2)? {
if cb1.rank() < cb2.rank() {
log::debug!("dupe detected: {cb1:?} == {cb2:?} -> removing {cb2:?}");
remove_me.insert(j);
} else {
log::debug!("dupe detected: {cb1:?} == {cb2:?} -> removing {cb1:?}");
remove_me.insert(i);
}
}
}
}
}
let clipboards = clipboards
.into_iter()
.enumerate()
.filter(|(i, _)| !remove_me.contains(i))
.map(|(_, c)| c)
.collect::<Vec<Box<dyn Clipboard>>>();
// let clipboards = dedupe(clipboards)?;
for c in clipboards.iter() {
c.set(&start)?;
}
log::info!("Using clipboards: {:?}", clipboards);
Ok(clipboards)
}
pub fn keep_synced(clipboards: &Vec<Box<dyn Clipboard>>) -> MyResult<()> {
if clipboards.is_empty() {
return Err(MyError::NoClipboards);
}
loop {
sleep(Duration::from_millis(100));
let new_value = await_change(clipboards)?;
for c in clipboards {
c.set(&new_value)?;
}
}
}
fn are_same(one: &dyn Clipboard, two: &dyn Clipboard) -> MyResult<bool> {
let d1 = &one.display();
let d2 = &two.display();
one.set(d1)?;
if d1 != &two.get()? {
return Ok(false);
}
two.set(d2)?;
if d2 != &one.get()? {
return Ok(false);
}
Ok(true)
}
// /// equality comparison is the bottleneck, and it's composed of two steps. so
// /// the purpose of this function is to minimize the number of executions of
// /// those steps. to do so, these steps are run at different times and combined
// /// at the end. this requires some extra computation in here but that's fine.
// fn dedupe(clipboards: Vec<Box<dyn Clipboard>>) -> MyResult<Vec<Box<dyn Clipboard>>> {
// let mut k_cant_read_v: HashMap<usize, HashSet<usize>> = HashMap::new();
// let mut k_can_read_v: HashMap<usize, HashSet<usize>> = HashMap::new();
// for (i, one) in clipboards.iter().enumerate() {
// // let i_can_read = k_can_read_v.entry(i).or_insert(HashSet::new()).clone();
// let i_cant_read = k_cant_read_v.entry(i).or_insert(HashSet::new()).clone();
// let d1 = &one.display();
// one.set(d1)?;
// for (j, two) in clipboards.iter().enumerate() {
// println!("{i} {j} {} {}", i != j, !i_cant_read.contains(&j));
// if i != j && !i_cant_read.contains(&j) {
// if d1 == &two.get()? {
// log::debug!("{two:?} can read {one:?}");
// k_can_read_v.entry(j).or_insert(HashSet::new()).insert(i);
// } else {
// log::debug!("{two:?} cannot read {one:?}");
// k_cant_read_v.entry(j).or_insert(HashSet::new()).insert(i);
// }
// }
// }
// }
// let mut remove_me: HashSet<usize> = HashSet::new();
// let mut dont_remove_me: HashSet<usize> = HashSet::new();
// println!("{k_cant_read_v:#?}");
// println!("{k_can_read_v:#?}");
// for (k, v) in k_can_read_v.iter() {
// for readable in v {
// if !dont_remove_me.contains(readable)
// && k_can_read_v
// .get(readable)
// .map(|what_readable_can_read| what_readable_can_read.contains(k))
// .unwrap_or(false)
// {
// remove_me.insert(*readable);
// dont_remove_me.insert(*k);
// }
// }
// }
// Ok(clipboards
// .into_iter()
// .enumerate()
// .filter(|(i, _)| !remove_me.contains(i))
// .map(|(_, c)| c)
// .collect::<Vec<Box<dyn Clipboard>>>())
// }
// /// equality comparison is the bottleneck, and it's composed of two steps. so
// /// the purpose of this function is to minimize the number of executions of
// /// those steps. to do so, these steps are run at different times and combined
// /// at the end. this requires some extra computation in here but that's fine.
// fn dedupe(clipboards: Vec<Box<dyn Clipboard>>) -> MyResult<Vec<Box<dyn Clipboard>>> {
// for (i, clipboard) in clipboards.iter().enumerate() {
// println!("write {i}");
// clipboard.set(&format!("{}{i}", clipboard.get()?))?;
// }
// let mut results = HashMap::new();
// for (i, clipboard) in clipboards.iter().enumerate() {
// println!("read {i}");
// results.insert(i, clipboard.get()?);
// }
// todo!()
// }
fn get_clipboards_spec<F: Fn(u8) -> MyResult<Option<Box<dyn Clipboard>>>>(
getter: F,
) -> Vec<Box<dyn Clipboard>> {
let mut clipboards: Vec<Box<dyn Clipboard>> = Vec::new();
let mut xcb_conn_err = None;
let mut xcb_conn_failed_clipboards = vec![];
for i in 0..u8::MAX {
let result = getter(i);
match result {
Ok(option) => {
if let Some(clipboard) = option {
log::debug!("Found clipboard: {:?}", clipboard);
clipboards.push(clipboard);
}
}
Err(MyError::TerminalClipboard(StandardizedError {
inner,
stdio: None,
})) if format!("{inner}") == "clipboard error: X11 clipboard error : XCB connection error: Connection" => {
xcb_conn_failed_clipboards.push(i);
xcb_conn_err = Some(inner);
},
Err(err) => log::error!(
"unexpected error while attempting to setup clipboard {}: {}",
i,
err
),
}
}
if let Some(err) = xcb_conn_err {
let displays = concise_numbers(&xcb_conn_failed_clipboards);
log::warning!(
"Issue connecting to some x11 clipboards. \
This is expected when hooking up to gnome wayland, and not a problem in that context. \
Details: '{err}' for x11 displays: {displays}",
);
}
clipboards
}
fn get_wayland(n: u8) -> MyResult<Option<Box<dyn Clipboard>>> {
let wl_display = format!("wayland-{}", n);
let clipboard = WlrClipboard {
display: wl_display.clone(),
};
let attempt = clipboard.get();
if let Err(MyError::WlcrsPaste(PasteError::WaylandConnection(
ConnectError::NoCompositorListening,
))) = attempt
{
return Ok(None);
}
if let Err(MyError::WlcrsPaste(PasteError::MissingProtocol {
name: "zwlr_data_control_manager_v1",
version: 1,
})) = attempt
{
log::warning!(
"{wl_display} does not support zwlr_data_control_manager_v1. If you are running \
gnome in wayland, that's OK because it provides an x11 clipboard, which will be used instead. \
Otherwise, `wl-copy` will be used to sync data *into* this clipboard, but it will not be possible \
to read data *from* this clipboard into other clipboards."
);
let command = WlCommandClipboard {
display: wl_display.clone(),
};
let Ok(gotten) = command.get() else {
return Ok(None);
};
let Ok(_) = command.set(&gotten) else {
return Ok(None);
};
return Ok(Some(Box::new(command)));
}
attempt?;
Ok(Some(Box::new(clipboard)))
}
fn get_x11(n: u8) -> MyResult<Option<Box<dyn Clipboard>>> {
let display = format!(":{}", n);
let clipboard = X11Clipboard::new(display)?;
clipboard.get()?;
Ok(Some(Box::new(clipboard)))
}
fn await_change(clipboards: &Vec<Box<dyn Clipboard>>) -> MyResult<String> {
let start = clipboards[0].get()?;
loop {
for c in clipboards {
if !c.should_poll() {
continue;
}
let new = c.get()?;
if new != start {
log::info!("clipboard updated from display {}", c.display());
log::sensitive!(log::info, "clipboard contents: '{}'", new);
return Ok(new);
}
}
sleep(Duration::from_millis(200));
}
}