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
use std::io::Error as IoError;
use std::ptr;
use std::sync::{Arc, Mutex};
use libc::{c_void, uint32_t};
use ffi::interfaces::callback::{wl_callback, wl_callback_listener, wl_callback_add_listener};
use ffi::interfaces::display::{wl_display, wl_display_sync};
use ffi::abi;
#[cfg(feature = "dlopen")]
use ffi::abi::WAYLAND_CLIENT_HANDLE as WCH;
#[cfg(not(feature = "dlopen"))]
use ffi::abi::{wl_display_connect, wl_display_disconnect, wl_display_flush,
wl_display_dispatch, wl_display_dispatch_pending,
wl_display_roundtrip};
use ffi::FFI;
use super::{From, Registry};
struct InternalDisplay {
ptr: *mut wl_display,
listener: Box<DisplayListener>
}
unsafe impl Send for InternalDisplay {}
#[derive(Clone)]
pub struct Display {
internal: Arc<Mutex<InternalDisplay>>
}
impl Display {
pub fn get_registry(&self) -> Registry {
From::from(self.clone())
}
pub fn sync_roundtrip(&self) {
let internal = self.internal.lock().unwrap();
unsafe {
ffi_dispatch!(WCH, wl_display_roundtrip, internal.ptr);
}
}
pub fn dispatch_pending(&self) {
let internal = self.internal.lock().unwrap();
unsafe {
ffi_dispatch!(WCH, wl_display_dispatch_pending, internal.ptr);
}
}
pub fn dispatch(&self) {
let internal = self.internal.lock().unwrap();
unsafe {
ffi_dispatch!(WCH, wl_display_dispatch, internal.ptr);
}
}
pub fn flush(&self) -> Result<(), IoError> {
let internal = self.internal.lock().unwrap();
if unsafe { ffi_dispatch!(WCH, wl_display_flush, internal.ptr) } < 0 {
Err(IoError::last_os_error())
} else {
Ok(())
}
}
pub fn sync(&self) {
let internal = self.internal.lock().unwrap();
unsafe {
let callback = wl_display_sync(internal.ptr);
wl_callback_add_listener(
callback,
&DONE_LISTENER as *const _,
&*internal.listener as *const _ as *mut _
);
}
}
pub fn set_sync_callback<F: Fn() + 'static>(&self, f: F) {
let mut internal = self.internal.lock().unwrap();
*internal.listener.done.lock().unwrap() = Box::new(f);
}
}
impl Drop for InternalDisplay {
fn drop(&mut self) {
unsafe {
ffi_dispatch!(WCH, wl_display_disconnect, self.ptr);
}
}
}
impl FFI for Display {
type Ptr = wl_display;
fn ptr(&self) -> *const wl_display {
self.internal.lock().unwrap().ptr as *const wl_display
}
unsafe fn ptr_mut(&self) -> *mut wl_display {
self.internal.lock().unwrap().ptr
}
}
#[cfg(feature = "dlopen")]
pub fn default_display() -> Option<Display> {
unsafe {
let handle = match abi::WAYLAND_CLIENT_OPTION.as_ref() {
Some(h) => h,
None => return None
};
let ptr = (handle.wl_display_connect)(ptr::null_mut());
if ptr.is_null() {
None
} else {
Some(Display {
internal: Arc::new(Mutex::new(InternalDisplay{
ptr: ptr,
listener: Box::new(DisplayListener::default_handlers())
}))
})
}
}
}
#[cfg(not(feature = "dlopen"))]
pub fn default_display() -> Option<Display> {
unsafe {
let ptr = abi::wl_display_connect(ptr::null_mut());
if ptr.is_null() {
None
} else {
Some(Display {
internal: Arc::new(Mutex::new(InternalDisplay{
ptr: ptr,
listener: Box::new(DisplayListener::default_handlers())
}))
})
}
}
}
struct DisplayListener {
done: Mutex<Box<Fn()>>,
}
impl DisplayListener {
fn default_handlers() -> DisplayListener {
DisplayListener {
done: Mutex::new(Box::new(move || {}))
}
}
}
extern "C" fn display_done(data: *mut c_void, _callback: *mut wl_callback, _data: uint32_t) {
let listener = unsafe { &*(data as *const DisplayListener) };
(listener.done.lock().unwrap())();
}
static DONE_LISTENER: wl_callback_listener = wl_callback_listener {
done: display_done,
};