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
#[cfg(feature = "native_lib")]
use std::ffi::{CString, OsString};
use std::io;
#[cfg(feature = "native_lib")]
use std::os::unix::ffi::OsStringExt;
use std::sync::Arc;
use std::ops::Deref;
use Proxy;
use EventQueue;
#[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>,
}
impl DisplayInner {
#[cfg(feature = "native_lib")]
pub(crate) fn ptr(&self) -> *mut wl_display {
self.proxy.c_ptr() as *mut _
}
}
impl Drop for DisplayInner {
fn drop(&mut self) {
#[cfg(not(feature = "native_lib"))]
{
unimplemented!();
}
#[cfg(feature = "native_lib")]
{
unsafe {
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),
}),
};
let evq = EventQueue::new(display.inner.clone(), None);
Ok((display, evq))
}
pub fn connect_to_env() -> Result<(Display, EventQueue), ConnectError> {
#[cfg(not(feature = "native_lib"))]
{
unimplemented!()
}
#[cfg(feature = "native_lib")]
{
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
}
}