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
|
#[cfg(any(not(unix), target_os = "macos", target_os = "android", target_os = "emscripten"))]
fn main() {
unimplemented!()
}
#[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))))]
fn main() {
wayland::main();
}
#[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))))]
mod wayland {
extern crate andrew;
extern crate copypasta;
extern crate smithay_client_toolkit as sctk;
use wayland::copypasta::wayland_clipboard::{Clipboard, WaylandClipboardContext};
use wayland::copypasta::ClipboardProvider;
use std::io::{Read, Seek, SeekFrom, Write};
use std::sync::{atomic, Arc, Mutex};
use wayland::sctk::keyboard::{map_keyboard_auto, Event as KbEvent, KeyState};
use wayland::sctk::utils::{DoubleMemPool, MemPool};
use wayland::sctk::window::{ConceptFrame, Event as WEvent, Window};
use wayland::sctk::Environment;
use wayland::sctk::reexports::client::protocol::{wl_shm, wl_surface};
use wayland::sctk::reexports::client::{Display, NewProxy};
use wayland::andrew::shapes::rectangle;
use wayland::andrew::text;
use wayland::andrew::text::fontconfig;
pub fn main() {
let (display, mut event_queue) =
Display::connect_to_env().expect("Failed to connect to the wayland server.");
let env = Environment::from_display(&*display, &mut event_queue).unwrap();
let mut ctx = WaylandClipboardContext::<Clipboard>::new(&display);
let cb_contents = Arc::new(Mutex::new(String::new()));
let seat = env.manager.instantiate_range(2, 6, NewProxy::implement_dummy).unwrap();
let need_redraw = Arc::new(atomic::AtomicBool::new(false));
let need_redraw_clone = need_redraw.clone();
let cb_contents_clone = cb_contents.clone();
map_keyboard_auto(&seat, move |event: KbEvent, _| {
if let KbEvent::Key { state: KeyState::Pressed, utf8: Some(text), .. } = event {
if text == " " {
*cb_contents_clone.lock().unwrap() = ctx.get_contents().unwrap();
need_redraw_clone.store(true, atomic::Ordering::Relaxed)
} else if text == "s" {
ctx.set_contents(
"This is an example text thats been copied to the wayland clipboard :)"
.to_string(),
)
.unwrap();
} else if text == "t" {
ctx.set_contents("Alternative text :)".to_string()).unwrap();
}
}
})
.unwrap();
let mut dimensions = (320u32, 240u32);
let surface = env.compositor.create_surface(NewProxy::implement_dummy).unwrap();
let next_action = Arc::new(Mutex::new(None::<WEvent>));
let waction = next_action.clone();
let mut window =
Window::<ConceptFrame>::init_from_env(&env, surface, dimensions, move |evt| {
let mut next_action = waction.lock().unwrap();
// Keep last event in priority order : Close > Configure > Refresh
let replace = match (&evt, &*next_action) {
(_, &None)
| (_, &Some(WEvent::Refresh))
| (&WEvent::Configure { .. }, &Some(WEvent::Configure { .. }))
| (&WEvent::Close, _) => true,
_ => false,
};
if replace {
*next_action = Some(evt);
}
})
.expect("Failed to create a window !");
window.new_seat(&seat);
window.set_title("Clipboard".to_string());
let mut pools =
DoubleMemPool::new(&env.shm, || {}).expect("Failed to create a memory pool !");
let mut font_data = Vec::new();
std::fs::File::open(
&fontconfig::FontConfig::new().unwrap().get_regular_family_fonts("sans").unwrap()[0],
)
.unwrap()
.read_to_end(&mut font_data)
.unwrap();
if !env.shell.needs_configure() {
// initial draw to bootstrap on wl_shell
if let Some(pool) = pools.pool() {
redraw(pool, window.surface(), dimensions, &font_data, "".to_string());
}
window.refresh();
}
loop {
match next_action.lock().unwrap().take() {
Some(WEvent::Close) => break,
Some(WEvent::Refresh) => {
window.refresh();
window.surface().commit();
},
Some(WEvent::Configure { new_size, .. }) => {
if let Some((w, h)) = new_size {
window.resize(w, h);
dimensions = (w, h)
}
window.refresh();
if let Some(pool) = pools.pool() {
redraw(
pool,
window.surface(),
dimensions,
&font_data,
cb_contents.lock().unwrap().clone(),
);
}
},
None => {},
}
if need_redraw.swap(false, atomic::Ordering::Relaxed) {
if let Some(pool) = pools.pool() {
redraw(
pool,
window.surface(),
dimensions,
&font_data,
cb_contents.lock().unwrap().clone(),
);
}
window.surface().damage_buffer(0, 0, dimensions.0 as i32, dimensions.1 as i32);
window.surface().commit();
}
event_queue.dispatch().unwrap();
}
}
fn redraw(
pool: &mut MemPool,
surface: &wl_surface::WlSurface,
dimensions: (u32, u32),
font_data: &[u8],
cb_contents: String,
) {
let (buf_x, buf_y) = (dimensions.0 as usize, dimensions.1 as usize);
pool.resize(4 * buf_x * buf_y).expect("Failed to resize the memory pool.");
let mut buf = vec![0; 4 * buf_x * buf_y];
let mut canvas =
andrew::Canvas::new(&mut buf, buf_x, buf_y, 4 * buf_x, andrew::Endian::native());
let bg = rectangle::Rectangle::new((0, 0), (buf_x, buf_y), None, Some([255, 170, 20, 45]));
canvas.draw(&bg);
let text_box = rectangle::Rectangle::new(
(buf_x / 30, buf_y / 35),
(buf_x - 2 * (buf_x / 30), (buf_x as f32 / 14.) as usize),
Some((3, [255, 255, 255, 255], rectangle::Sides::ALL, Some(4))),
None,
);
canvas.draw(&text_box);
let helper_text = text::Text::new(
(buf_x / 25, buf_y / 30),
[255, 255, 255, 255],
font_data,
buf_x as f32 / 40.,
2.0,
"Press space to draw clipboard contents",
);
canvas.draw(&helper_text);
let helper_text = text::Text::new(
(buf_x / 25, buf_y / 15),
[255, 255, 255, 255],
font_data,
buf_x as f32 / 40.,
2.0,
"Press 's' to store example text to clipboard",
);
canvas.draw(&helper_text);
for i in (0..cb_contents.len()).step_by(36) {
let content = if cb_contents.len() < i + 36 {
cb_contents[i..].to_string()
} else {
cb_contents[i..i + 36].to_string()
};
let text = text::Text::new(
(buf_x / 10, buf_y / 8 + (i as f32 * buf_y as f32 / 1000.) as usize),
[255, 255, 255, 255],
font_data,
buf_x as f32 / 40.,
2.0,
content,
);
canvas.draw(&text);
}
pool.seek(SeekFrom::Start(0)).unwrap();
pool.write_all(canvas.buffer).unwrap();
pool.flush().unwrap();
let new_buffer =
pool.buffer(0, buf_x as i32, buf_y as i32, 4 * buf_x as i32, wl_shm::Format::Argb8888);
surface.attach(Some(&new_buffer), 0, 0);
surface.commit();
}
}
|