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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
use std::cmp::max;
use std::io::{Seek, SeekFrom, Write};
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, Mutex, MutexGuard};
use byteorder::{WriteBytesExt, NativeEndian};
use tempfile::TempFile;
use wayland::core::{Surface, Registry};
use wayland::core::compositor::{WSurface, SurfaceId};
use wayland::core::seat::{Seat, Pointer, ButtonState};
use wayland::core::shell::{ShellSurface, ShellSurfaceResize};
use wayland::core::shm::{Buffer, ShmPool, ShmFormat};
use wayland::core::subcompositor::SubSurface;
pub const BORDER_TOP : usize = 0;
pub const BORDER_RIGHT : usize = 1;
pub const BORDER_BOTTOM : usize = 2;
pub const BORDER_LEFT : usize = 3;
const DECORATION_SIZE : i32 = 8;
const DECORATION_TOP_SIZE : i32 = 24;
#[derive(Debug)]
enum PtrLocation {
None,
Top,
Right,
Bottom,
Left
}
struct PointerState {
surfaces: Vec<SurfaceId>,
location: PtrLocation,
coordinates: (f64, f64),
surface_width: i32,
}
impl PointerState {
fn pointer_entered(&mut self, sid: SurfaceId) {
if self.surfaces[BORDER_TOP] == sid {
self.location = PtrLocation::Top;
} else if self.surfaces[BORDER_RIGHT] == sid {
self.location = PtrLocation::Right
} else if self.surfaces[BORDER_BOTTOM] == sid {
self.location = PtrLocation::Bottom;
} else if self.surfaces[BORDER_LEFT] == sid {
self.location = PtrLocation::Left
} else {
self.location = PtrLocation::None;
}
}
fn pointer_left(&mut self) {
self.location = PtrLocation::None;
}
}
pub struct DecoratedSurface<S: Surface> {
shell_surface: Arc<Mutex<Option<ShellSurface<S>>>>,
border_surfaces: Vec<SubSurface<WSurface>>,
buffers: Vec<Buffer>,
tempfile: TempFile,
pool: ShmPool,
height: i32,
width: i32,
buffer_capacity: usize,
_pointer: Option<Pointer<WSurface>>,
pointer_state: Arc<Mutex<PointerState>>
}
pub struct SurfaceGuard<'a, S: Surface + 'a> {
guard: MutexGuard<'a, Option<ShellSurface<S>>>
}
impl<'a, S: Surface + 'a> Deref for SurfaceGuard<'a, S> {
type Target = ShellSurface<S>;
fn deref(&self) -> &ShellSurface<S> {
self.guard.as_ref().unwrap()
}
}
impl<'a, S: Surface + 'a> DerefMut for SurfaceGuard<'a, S> {
fn deref_mut(&mut self) -> &mut ShellSurface<S> {
self.guard.as_mut().unwrap()
}
}
impl<S: Surface + Send + 'static> DecoratedSurface<S> {
pub fn resize(&mut self, width: i32, height: i32) {
let new_pxcount = max(DECORATION_TOP_SIZE * (DECORATION_SIZE * 2 + width),
max(DECORATION_TOP_SIZE * width, DECORATION_SIZE * height)
) as usize;
if new_pxcount * 4 > self.buffer_capacity {
self.tempfile.set_len((new_pxcount * 4) as u64).unwrap();
self.pool.resize((new_pxcount * 4) as i32);
self.buffer_capacity = new_pxcount * 4;
}
self.width = width;
self.height = height;
self.pointer_state.lock().unwrap().surface_width = width;
self.tempfile.seek(SeekFrom::Start(0)).unwrap();
for _ in 0..(new_pxcount*4) {
let _ = self.tempfile.write_u32::<NativeEndian>(0xFF444444);
}
self.tempfile.flush().unwrap();
self.buffers.clear();
{
let buffer = self.pool.create_buffer(
0,
self.width as i32 + (DECORATION_SIZE as i32) * 2,
DECORATION_TOP_SIZE as i32,
(self.width as i32 + (DECORATION_SIZE as i32) * 2) * 4,
ShmFormat::ARGB8888
).unwrap();
self.border_surfaces[BORDER_TOP].attach(&buffer, 0, 0);
self.border_surfaces[BORDER_TOP].set_position(
-(DECORATION_SIZE as i32),
-(DECORATION_TOP_SIZE as i32)
);
self.buffers.push(buffer);
}
{
let buffer = self.pool.create_buffer(
0, DECORATION_SIZE as i32,
self.height as i32, (DECORATION_SIZE*4) as i32,
ShmFormat::ARGB8888
).unwrap();
self.border_surfaces[BORDER_RIGHT].attach(&buffer, 0, 0);
self.border_surfaces[BORDER_RIGHT].set_position(self.width as i32, 0);
self.buffers.push(buffer);
}
{
let buffer = self.pool.create_buffer(
0,
self.width as i32 + (DECORATION_SIZE as i32) * 2,
DECORATION_SIZE as i32,
(self.width as i32 + (DECORATION_SIZE as i32) * 2) * 4,
ShmFormat::ARGB8888
).unwrap();
self.border_surfaces[BORDER_BOTTOM].attach(&buffer, 0, 0);
self.border_surfaces[BORDER_BOTTOM].set_position(-(DECORATION_SIZE as i32), self.height as i32);
self.buffers.push(buffer);
}
{
let buffer = self.pool.create_buffer(
0, DECORATION_SIZE as i32,
self.height as i32, (DECORATION_SIZE*4) as i32,
ShmFormat::ARGB8888
).unwrap();
self.border_surfaces[BORDER_LEFT].attach(&buffer, 0, 0);
self.border_surfaces[BORDER_LEFT].set_position(-(DECORATION_SIZE as i32), 0);
self.buffers.push(buffer);
}
for s in &self.border_surfaces { s.commit(); }
}
pub fn new(user_surface: S, width: i32, height: i32, registry: &Registry, seat: Option<&Seat>)
-> Result<DecoratedSurface<S>,S>
{
let comp = match registry.get_compositor() {
Some(c) => c,
None => return Err(user_surface)
};
let subcomp = match registry.get_subcompositor() {
Some(c) => c,
None => return Err(user_surface)
};
let shm = match registry.get_shm() {
Some(s) => s,
None => return Err(user_surface)
};
let shell = match registry.get_shell() {
Some(s) => s,
None => return Err(user_surface)
};
let pxcount = max(DECORATION_TOP_SIZE * DECORATION_SIZE,
max(DECORATION_TOP_SIZE * width, DECORATION_SIZE * height)
) as usize;
let tempfile = match TempFile::new() {
Ok(t) => t,
Err(_) => return Err(user_surface)
};
match tempfile.set_len((pxcount *4) as u64) {
Ok(()) => {},
Err(_) => return Err(user_surface)
};
let pool = shm.pool_from_fd(&tempfile, (pxcount * 4) as i32);
let border_surfaces: Vec<_> = (0..4).map(|_|
subcomp.get_subsurface(comp.create_surface(), user_surface.get_wsurface())
).collect();
for s in &border_surfaces { s.set_sync(false) }
let shell_surface = shell.get_shell_surface(user_surface);
shell_surface.set_toplevel();
let mut pointer_state = PointerState {
surfaces: Vec::with_capacity(4),
location: PtrLocation::None,
coordinates: (0., 0.),
surface_width: width
};
let mut pointer = seat.and_then(|seat| seat.get_pointer())
.map(|mut pointer| {
for s in &border_surfaces {
pointer.add_handled_surface(s.get_id());
pointer_state.surfaces.push(s.get_id());
}
pointer
});
let pointer_state = Arc::new(Mutex::new(pointer_state));
let shell_surface = Arc::new(Mutex::new(Some(shell_surface)));
if let Some(ref mut pointer) = pointer {
let my_pointer = pointer_state.clone();
pointer.set_enter_action(move |_pid, _serial, sid, x, y| {
let mut guard = my_pointer.lock().unwrap();
guard.pointer_entered(sid);
guard.coordinates = (x, y);
});
let my_pointer = pointer_state.clone();
pointer.set_leave_action(move |_pid, _serial, _sid| {
let mut guard = my_pointer.lock().unwrap();
guard.pointer_left();
guard.coordinates = (0., 0.);
});
let my_pointer = pointer_state.clone();
pointer.set_motion_action(move |_pid, _t, x, y| {
let mut guard = my_pointer.lock().unwrap();
guard.coordinates = (x, y);
});
let my_pointer = pointer_state.clone();
let my_seat = pointer.get_seat().clone();
let my_shell = shell_surface.clone();
pointer.set_button_action(move |_pid, serial, _t, button, state| {
if button != 0x110 { return; }
if state != ButtonState::Pressed { return; }
let pguard = my_pointer.lock().unwrap();
let sguard = my_shell.lock().unwrap();
let shell = match sguard.as_ref() {
Some(s) => s,
None => return
};
let (x, y) = pguard.coordinates;
let w = pguard.surface_width;
let (direction, resize) = match pguard.location {
PtrLocation::Top => {
if y < DECORATION_SIZE as f64 {
if x < DECORATION_SIZE as f64 {
(ShellSurfaceResize::TopLeft, true)
} else if x > w as f64 + DECORATION_SIZE as f64 {
(ShellSurfaceResize::TopRight, true)
} else {
(ShellSurfaceResize::Top, true)
}
} else {
if x < DECORATION_SIZE as f64 {
(ShellSurfaceResize::Left, true)
} else if x > w as f64 + DECORATION_SIZE as f64 {
(ShellSurfaceResize::Right, true)
} else {
(ShellSurfaceResize::None, false)
}
}
},
PtrLocation::Bottom => {
if x < DECORATION_SIZE as f64 {
(ShellSurfaceResize::BottomLeft, true)
} else if x > w as f64 + DECORATION_SIZE as f64 {
(ShellSurfaceResize::BottomRight, true)
} else {
(ShellSurfaceResize::Bottom, true)
}
},
PtrLocation::Left => (ShellSurfaceResize::Left, true),
PtrLocation::Right => (ShellSurfaceResize::Right, true),
PtrLocation::None => (ShellSurfaceResize::None, true)
};
if resize {
shell.start_resize(&my_seat, serial, direction);
} else {
shell.start_move(&my_seat, serial);
}
});
}
let mut me = DecoratedSurface {
shell_surface: shell_surface,
border_surfaces: border_surfaces,
buffers: Vec::new(),
tempfile: tempfile,
pool: pool,
height: height,
width: width,
buffer_capacity: pxcount * 4,
_pointer: pointer,
pointer_state: pointer_state
};
me.resize(width, height);
Ok(me)
}
pub fn get_shell(&self) -> SurfaceGuard<S> {
SurfaceGuard {
guard: self.shell_surface.lock().unwrap()
}
}
pub fn destroy(self) -> S {
self.shell_surface.lock().unwrap().take().unwrap().destroy()
}
}
pub fn substract_borders(width: i32, height: i32) -> (i32, i32) {
(
width - 2*(DECORATION_SIZE as i32),
height - DECORATION_SIZE as i32 - DECORATION_TOP_SIZE as i32
)
}