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
use std::env;
use std::ffi::OsString;
use std::io;
use std::ops::Deref;
use std::os::unix::io::{IntoRawFd, RawFd};
use std::os::unix::net::UnixStream;
use std::path::PathBuf;
use std::sync::Arc;
use nix::fcntl;
use {EventQueue, Proxy};
use imp::DisplayInner;
#[cfg(feature = "native_lib")]
use wayland_sys::client::wl_display;
#[derive(Debug)]
pub enum ConnectError {
NoWaylandLib,
XdgRuntimeDirNotSet,
NoCompositorListening,
InvalidName,
InvalidFd,
}
impl ::std::error::Error for ConnectError {
fn description(&self) -> &str {
match *self {
ConnectError::NoWaylandLib => "Could not find libwayland-client.so.",
ConnectError::XdgRuntimeDirNotSet => "XDG_RUNTIME_DIR is not set.",
ConnectError::NoCompositorListening => "Could not find a listening wayland compositor.",
ConnectError::InvalidName => "The wayland socket name is invalid.",
ConnectError::InvalidFd => "The FD provided in WAYLAND_SOCKET is invalid.",
}
}
}
impl ::std::fmt::Display for ConnectError {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
f.write_str(::std::error::Error::description(self))
}
}
pub struct Display {
pub(crate) inner: Arc<DisplayInner>,
}
impl Display {
pub fn connect_to_env() -> Result<(Display, EventQueue), ConnectError> {
if let Ok(txt) = env::var("WAYLAND_SOCKET") {
let fd = txt.parse::<i32>().map_err(|_| ConnectError::InvalidFd)?;
let flags = fcntl::fcntl(fd, fcntl::FcntlArg::F_GETFD);
let result = flags
.map(|f| fcntl::FdFlag::from_bits(f).unwrap() | fcntl::FdFlag::FD_CLOEXEC)
.and_then(|f| fcntl::fcntl(fd, fcntl::FcntlArg::F_SETFD(f)));
match result {
Ok(_) => {
unsafe { Display::from_fd(fd) }
}
Err(_) => {
let _ = ::nix::unistd::close(fd);
return Err(ConnectError::InvalidFd);
}
}
} else {
let mut socket_path = env::var_os("XDG_RUNTIME_DIR")
.map(Into::<PathBuf>::into)
.ok_or(ConnectError::XdgRuntimeDirNotSet)?;
socket_path.push(env::var_os("WAYLAND_DISPLAY").unwrap_or_else(|| "wayland-0".into()));
let socket = UnixStream::connect(socket_path).map_err(|_| ConnectError::NoCompositorListening)?;
unsafe { Display::from_fd(socket.into_raw_fd()) }
}
}
pub fn connect_to_name<S: Into<OsString>>(name: S) -> Result<(Display, EventQueue), ConnectError> {
let mut socket_path = env::var_os("XDG_RUNTIME_DIR")
.map(Into::<PathBuf>::into)
.ok_or(ConnectError::XdgRuntimeDirNotSet)?;
socket_path.push(name.into());
let socket = UnixStream::connect(socket_path).map_err(|_| ConnectError::NoCompositorListening)?;
unsafe { Display::from_fd(socket.into_raw_fd()) }
}
pub unsafe fn from_fd(fd: RawFd) -> Result<(Display, EventQueue), ConnectError> {
let (d_inner, evq_inner) = DisplayInner::from_fd(fd)?;
Ok((Display { inner: d_inner }, EventQueue::new(evq_inner)))
}
pub fn flush(&self) -> io::Result<()> {
self.inner.flush()
}
pub fn create_event_queue(&self) -> EventQueue {
let evq_inner = DisplayInner::create_event_queue(&self.inner);
EventQueue::new(evq_inner)
}
#[cfg(feature = "native_lib")]
pub unsafe fn from_external_display(display_ptr: *mut wl_display) -> (Display, EventQueue) {
let (d_inner, evq_inner) = DisplayInner::from_external(display_ptr);
(Display { inner: d_inner }, EventQueue::new(evq_inner))
}
#[cfg(feature = "native_lib")]
pub fn get_display_ptr(&self) -> *mut wl_display {
self.inner.ptr()
}
}
impl Deref for Display {
type Target = Proxy<::protocol::wl_display::WlDisplay>;
fn deref(&self) -> &Proxy<::protocol::wl_display::WlDisplay> {
self.inner.get_proxy()
}
}