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
//! X11 <-> Wayland Clipboard Bridge
//!
//! This program synchronizes clipboard content between X11 and Wayland compositors.
use clip_bridge::{
ClipboardContent, ClipboardType, SyncEvent,
wayland::{GlobalData, WaylandState},
x11::X11State,
};
// ============================================================================
// Main Application
// ============================================================================
//
use tracing::{debug, error, info};
use wayland_client::{Connection, DispatchError};
use tokio::{sync::mpsc, task::JoinHandle};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize logging
tracing_subscriber::fmt().init();
info!("Starting X11 <-> Wayland Clipboard Bridge");
// Create channels for sync events
let (x11_to_wayland_tx, mut x11_to_wayland_rx) = mpsc::unbounded_channel::<SyncEvent>();
let (wayland_to_x11_tx, mut wayland_to_x11_rx) = mpsc::unbounded_channel::<SyncEvent>();
// Create channels for setting clipboard
let (set_x11_clipboard_tx, set_x11_clipboard_rx) =
mpsc::unbounded_channel::<(String, ClipboardType)>();
let (set_wayland_clipboard_tx, set_wayland_clipboard_rx) =
mpsc::unbounded_channel::<(String, ClipboardType)>();
// Clone for X11 thread
let x11_sync_tx = x11_to_wayland_tx.clone();
let wayland_sync_tx = wayland_to_x11_tx.clone();
// Spawn X11 thread
let x11_handle = tokio::task::spawn_blocking(move || {
info!("[X11] Initializing X11 connection");
let (conn, screen_num) =
x11rb::connect(None).map_err(|e| format!("Failed to connect to X11: {}", e))?;
let mut x11_state = X11State::new(conn, screen_num, x11_sync_tx, set_x11_clipboard_rx)
.map_err(|e| format!("Failed to create X11 state: {}", e))?;
info!("[X11] Connection established, window: {}", x11_state.window);
// Run X11 event loop
// Note: We don't request clipboard content here on startup.
// Instead, we wait for XFixes selection events which indicate
// when another application owns the selection. This avoids the
// race condition where we request content before any app has set it.
if let Err(e) = x11_state.run_event_loop() {
error!("[X11] Event loop error: {}", e);
}
Ok::<(), String>(())
});
// Initialize Wayland
info!("[Wayland] Initializing Wayland connection");
let wayland_conn = Connection::connect_to_env()?;
let display = wayland_conn.display();
let mut event_queue = wayland_conn.new_event_queue();
let qh = event_queue.handle();
let mut wayland_state = WaylandState::new(
qh.clone(),
wayland_sync_tx,
set_wayland_clipboard_tx.clone(),
);
// Get registry
display.get_registry(&qh, GlobalData);
// Roundtrip to initialize globals
event_queue.roundtrip(&mut wayland_state)?;
info!("[Wayland] Connection established");
// Main sync loop
let wayland_handle: JoinHandle<Result<(), DispatchError>> =
tokio::task::spawn_blocking(move || {
let mut set_wayland_clipboard_rx = set_wayland_clipboard_rx;
loop {
if let Ok((content, clipboard_type)) = set_wayland_clipboard_rx.try_recv() {
wayland_state.set_clipboard_content(content, clipboard_type);
}
event_queue.roundtrip(&mut wayland_state)?;
if let Err(e) = event_queue.dispatch_pending(&mut wayland_state) {
error!("[Wayland] Dispatch error: {}", e);
}
}
});
// Handle sync events in main task
tokio::spawn(async move {
let mut x11_content: Option<String> = None;
let mut primary_content: Option<String> = None;
info!("[Sync] Starting sync loop");
loop {
tokio::select! {
Some(event) = x11_to_wayland_rx.recv() => {
debug!("[Sync] Received event from X11: {:?}", event);
match event {
SyncEvent::X11ToWayland { content, clipboard_type } => {
debug!("[Sync] Matching content: {:?}", content);
match content {
ClipboardContent::Text(text) => {
debug!("[Sync] X11 text content: {:?}", text);
debug!("[Sync] Current x11_content: {:?}", x11_content);
match clipboard_type {
ClipboardType::Clipboard => {
if x11_content.as_ref() != Some(&text) {
info!("[Sync] X11 -> Wayland clipboard: {} chars", text.len());
x11_content = Some(text.clone());
debug!("[Sync] Sending to Wayland clipboard channel");
match set_wayland_clipboard_tx.send((text, ClipboardType::Clipboard)) {
Ok(_) => debug!("[Sync] Sent to Wayland clipboard channel successfully"),
Err(e) => error!("[Sync] Failed to send to Wayland clipboard channel: {}", e),
}
} else {
debug!("[Sync] X11 clipboard content unchanged, skipping");
}
}
ClipboardType::Primary => {
if primary_content.as_ref() != Some(&text) {
info!("[Sync] X11 -> Wayland primary: {} chars", text.len());
primary_content = Some(text.clone());
debug!("[Sync] Sending to Wayland primary channel");
match set_wayland_clipboard_tx.send((text, ClipboardType::Primary)) {
Ok(_) => debug!("[Sync] Sent to Wayland primary channel successfully"),
Err(e) => error!("[Sync] Failed to send to Wayland primary channel: {}", e),
}
} else {
debug!("[Sync] X11 primary content unchanged, skipping");
}
}
}
}
ClipboardContent::Empty => {
debug!("[Sync] X11 empty content");
match clipboard_type {
ClipboardType::Clipboard => {
x11_content = None;
}
ClipboardType::Primary => {
primary_content = None;
}
}
}
}
}
_ => {
debug!("[Sync] Unhandled event from X11: {:?}", event);
}
}
}
Some(event) = wayland_to_x11_rx.recv() => {
debug!("[Sync] Received event from Wayland: {:?}", event);
match event {
SyncEvent::WaylandToX11 { content, clipboard_type } => {
debug!("[Sync] Matching Wayland content: {:?}", content);
match content {
ClipboardContent::Text(text) => {
debug!("[Sync] Wayland text content: {:?}", text);
match clipboard_type {
ClipboardType::Clipboard => {
if x11_content.as_ref() != Some(&text) {
info!("[Sync] Wayland -> X11 clipboard: {} chars", text.len());
x11_content = Some(text.clone());
debug!("[Sync] Sending to X11 clipboard channel");
match set_x11_clipboard_tx.send((text, ClipboardType::Clipboard)) {
Ok(_) => debug!("[Sync] Sent to X11 clipboard channel successfully"),
Err(e) => error!("[Sync] Failed to send to X11 clipboard channel: {}", e),
}
} else {
debug!("[Sync] Wayland clipboard content unchanged, skipping");
}
}
ClipboardType::Primary => {
if primary_content.as_ref() != Some(&text) {
info!("[Sync] Wayland -> X11 primary: {} chars", text.len());
primary_content = Some(text.clone());
debug!("[Sync] Sending to X11 primary channel");
match set_x11_clipboard_tx.send((text, ClipboardType::Primary)) {
Ok(_) => debug!("[Sync] Sent to X11 primary channel successfully"),
Err(e) => error!("[Sync] Failed to send to X11 primary channel: {}", e),
}
} else {
debug!("[Sync] Wayland primary content unchanged, skipping");
}
}
}
}
ClipboardContent::Empty => {
debug!("[Sync] Wayland empty content");
match clipboard_type {
ClipboardType::Clipboard => {
x11_content = None;
}
ClipboardType::Primary => {
primary_content = None;
}
}
}
}
}
_ => {
debug!("[Sync] Unhandled event from Wayland: {:?}", event);
}
}
}
}
}
});
// Wait for tasks
let (x11_result, wayland_result) = tokio::join!(x11_handle, wayland_handle);
if let Err(e) = x11_result {
error!("X11 task error: {:?}", e);
}
if let Err(e) = wayland_result {
error!("Wayland task error: {:?}", e);
}
info!("Clipboard bridge shutting down");
Ok(())
}