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
use std::ops::Deref;
use core::{From, Surface};
use core::ids::SurfaceId;
use core::compositor::WSurface;
use core::subcompositor::SubCompositor;
use ffi::interfaces::subcompositor::wl_subcompositor_get_subsurface;
use ffi::interfaces::subsurface::{wl_subsurface, wl_subsurface_destroy,
wl_subsurface_set_position,
wl_subsurface_set_sync,
wl_subsurface_set_desync,
wl_subsurface_place_above,
wl_subsurface_place_below};
use ffi::FFI;
pub struct SubSurface<S: Surface> {
_subcompositor: SubCompositor,
ptr: *mut wl_subsurface,
surface: S,
parent: SurfaceId,
}
unsafe impl<S: Surface + Send> Send for SubSurface<S> {}
unsafe impl<S: Surface + Sync> Sync for SubSurface<S> {}
pub enum Stacking {
Above,
Below
}
impl<S: Surface> SubSurface<S> {
pub fn destroy(mut self) -> S {
use std::mem::{forget, replace, uninitialized};
unsafe {
let surface = replace(&mut self.surface, uninitialized());
wl_subsurface_destroy(self.ptr);
forget(self);
surface
}
}
pub fn set_position(&self, x: i32, y: i32) {
unsafe { wl_subsurface_set_position(self.ptr, x, y) }
}
pub fn set_sync(&self, b: bool) {
if b {
unsafe { wl_subsurface_set_sync(self.ptr) }
} else {
unsafe { wl_subsurface_set_desync(self.ptr) }
}
}
pub fn restack_sibling<R: Surface>(&self, other: &SubSurface<R>, stack: Stacking) {
assert!(self.parent == other.parent,
"Cannot restack a subsurface against one that isn't a sibling.");
assert!(self.get_wsurface().get_id() != other.get_wsurface().get_id(),
"Cannot restack a subsurface against itself.");
match stack {
Stacking::Above => unsafe {
wl_subsurface_place_above(self.ptr, other.get_wsurface().ptr_mut())
},
Stacking::Below => unsafe {
wl_subsurface_place_below(self.ptr, other.get_wsurface().ptr_mut())
}
}
}
pub fn restack_parent<R: Surface>(&self, other: &R, stack: Stacking) {
assert!(self.parent == other.get_wsurface().get_id(),
"Expected the parent of this subsurface.");
match stack {
Stacking::Above => unsafe {
wl_subsurface_place_above(self.ptr, other.get_wsurface().ptr_mut())
},
Stacking::Below => unsafe {
wl_subsurface_place_below(self.ptr, other.get_wsurface().ptr_mut())
}
}
}
}
impl<S: Surface> Deref for SubSurface<S> {
type Target = S;
fn deref<'d>(&'d self) -> &'d S {
&self.surface
}
}
impl<'p, S> From<(SubCompositor, S, &'p WSurface)> for SubSurface<S>
where S: Surface
{
fn from((subcompositor, surface, parent): (SubCompositor, S, &'p WSurface))
-> SubSurface<S>
{
let ptr = unsafe {
wl_subcompositor_get_subsurface(
subcompositor.ptr_mut(),
surface.get_wsurface().ptr_mut(),
parent.ptr_mut()
)
};
SubSurface {
_subcompositor: subcompositor,
ptr: ptr,
surface: surface,
parent: parent.get_id()
}
}
}
impl<S: Surface> Drop for SubSurface<S> {
fn drop(&mut self) {
unsafe { wl_subsurface_destroy(self.ptr) };
}
}
impl<S: Surface> FFI for SubSurface<S> {
type Ptr = wl_subsurface;
fn ptr(&self) -> *const wl_subsurface {
self.ptr as *const wl_subsurface
}
unsafe fn ptr_mut(&self) -> *mut wl_subsurface {
self.ptr
}
}