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
#[cfg(feature = "native_lib")]
use std::ffi::{CString, OsString};
use std::io;
use std::ops::Deref;
#[cfg(feature = "native_lib")]
use std::os::unix::ffi::OsStringExt;
use std::sync::Arc;
use EventQueue;
use Proxy;
#[cfg(feature = "native_lib")]
use wayland_sys::client::*;
#[derive(Debug)]
pub enum ConnectError {
NoWaylandLib,
NoCompositorListening,
InvalidName,
}
pub(crate) struct DisplayInner {
proxy: Proxy<::protocol::wl_display::WlDisplay>,
display: *mut wl_display,
}
unsafe impl Send for DisplayInner {}
unsafe impl Sync for DisplayInner {}
impl DisplayInner {
#[cfg(feature = "native_lib")]
pub(crate) fn ptr(&self) -> *mut wl_display {
self.display
}
}
impl Drop for DisplayInner {
fn drop(&mut self) {
#[cfg(not(feature = "native_lib"))]
{
unimplemented!();
}
#[cfg(feature = "native_lib")]
{
unsafe {
if self.proxy.c_ptr() == (self.display as *mut _) {
ffi_dispatch!(
WAYLAND_CLIENT_HANDLE,
wl_display_disconnect,
self.proxy.c_ptr() as *mut wl_display
);
}
}
}
}
}
pub struct Display {
inner: Arc<DisplayInner>,
}
impl Display {
#[cfg(feature = "native_lib")]
unsafe fn make_display(ptr: *mut wl_display) -> Result<(Display, EventQueue), ConnectError> {
if ptr.is_null() {
return Err(ConnectError::NoCompositorListening);
}
let display = Display {
inner: Arc::new(DisplayInner {
proxy: Proxy::from_display(ptr),
display: ptr,
}),
};
let evq = EventQueue::new(display.inner.clone(), None);
Ok((display, evq))
}
#[cfg(feature = "native_lib")]
pub unsafe fn from_external_display(display_ptr: *mut wl_display) -> (Display, EventQueue) {
let evq_ptr = ffi_dispatch!(WAYLAND_CLIENT_HANDLE, wl_display_create_queue, display_ptr);
let wrapper_ptr = ffi_dispatch!(
WAYLAND_CLIENT_HANDLE,
wl_proxy_create_wrapper,
display_ptr as *mut _
);
ffi_dispatch!(WAYLAND_CLIENT_HANDLE, wl_proxy_set_queue, wrapper_ptr, evq_ptr);
let display = Display {
inner: Arc::new(DisplayInner {
proxy: Proxy::from_display_wrapper(wrapper_ptr),
display: display_ptr,
}),
};
let evq = EventQueue::new(display.inner.clone(), Some(evq_ptr));
(display, evq)
}
#[cfg(feature = "native_lib")]
pub fn get_display_ptr(&self) -> *mut wl_display {
self.inner.ptr()
}
pub fn connect_to_env() -> Result<(Display, EventQueue), ConnectError> {
#[cfg(not(feature = "native_lib"))]
{
unimplemented!()
}
#[cfg(feature = "native_lib")]
{
if !::wayland_sys::client::is_lib_available() {
return Err(ConnectError::NoWaylandLib);
}
unsafe {
let display_ptr =
ffi_dispatch!(WAYLAND_CLIENT_HANDLE, wl_display_connect, ::std::ptr::null());
Display::make_display(display_ptr)
}
}
}
pub fn connect_to_name<S: Into<OsString>>(name: S) -> Result<(Display, EventQueue), ConnectError> {
#[cfg(not(feature = "native_lib"))]
{
unimplemented!()
}
#[cfg(feature = "native_lib")]
{
if !::wayland_sys::client::is_lib_available() {
return Err(ConnectError::NoWaylandLib);
}
let name = CString::new(name.into().into_vec()).map_err(|_| ConnectError::InvalidName)?;
unsafe {
let display_ptr = ffi_dispatch!(WAYLAND_CLIENT_HANDLE, wl_display_connect, name.as_ptr());
Display::make_display(display_ptr)
}
}
}
pub fn flush(&self) -> io::Result<i32> {
let ret = unsafe { ffi_dispatch!(WAYLAND_CLIENT_HANDLE, wl_display_flush, self.inner.ptr()) };
if ret >= 0 {
Ok(ret)
} else {
Err(io::Error::last_os_error())
}
}
pub fn create_event_queue(&self) -> EventQueue {
#[cfg(not(feature = "native_lib"))]
{
unimplemented!()
}
#[cfg(feature = "native_lib")]
unsafe {
let ptr = ffi_dispatch!(WAYLAND_CLIENT_HANDLE, wl_display_create_queue, self.inner.ptr());
EventQueue::new(self.inner.clone(), Some(ptr))
}
}
}
impl Deref for Display {
type Target = Proxy<::protocol::wl_display::WlDisplay>;
fn deref(&self) -> &Proxy<::protocol::wl_display::WlDisplay> {
&self.inner.proxy
}
}