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
//! EI client implementation.
//!
//! Create a connection over a Unix socket with [`Context`].
//!
//! Client-side protocol bindings are exported here, and they consist of interface proxies (like
//! [`device::Device`]) and event enums (like [`device::Event`]).
use std::{
env, io,
os::unix::{
io::{AsFd, AsRawFd, BorrowedFd, RawFd},
net::UnixStream,
},
path::PathBuf,
};
use crate::{wire::Backend, PendingRequestResult};
// Re-export generate bindings
pub use crate::eiproto_ei::*;
/// A connection, seen from the client side.
#[derive(Clone, Debug)]
pub struct Context(pub(crate) Backend);
impl AsFd for Context {
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}
impl AsRawFd for Context {
fn as_raw_fd(&self) -> RawFd {
self.0.as_fd().as_raw_fd()
}
}
impl Context {
/// Creates a `Context` from a `UnixStream`.
///
/// # Example
///
/// ```no_run
/// use std::os::unix::net::UnixStream;
/// use reis::ei::Context;
///
/// let socket = UnixStream::connect("/example/path").unwrap();
/// // Or receive from, for example, the RemoteDesktop XDG desktop protal.
///
/// let context = Context::new(socket).unwrap();
/// ```
///
/// # Errors
///
/// Will return `Err` if setting the socket to non-blocking mode fails.
pub fn new(socket: UnixStream) -> io::Result<Self> {
Ok(Self(Backend::new(socket, true)?))
}
/// Connects to a socket based on the `LIBEI_SOCKET` environment variable, and creates
/// a `Context` from it.
///
/// # Example
///
/// ```no_run
/// use reis::ei::Context;
///
/// let context = Context::connect_to_env().expect("Shouldn't error").unwrap();
/// ```
///
/// # Errors
///
/// Will return `Err` if the resolved socket path cannot be connected to or if
/// [`Context::new`] fails.
pub fn connect_to_env() -> io::Result<Option<Self>> {
let Some(path) = env::var_os("LIBEI_SOCKET") else {
// XXX return error type
return Ok(None);
};
let path = PathBuf::from(path);
let path = if path.is_relative() {
let Some(runtime_dir) = env::var_os("XDG_RUNTIME_DIR") else {
// XXX return not found
return Ok(None);
};
let mut new_path = PathBuf::from(runtime_dir);
new_path.push(path);
new_path
} else {
path
};
let socket = UnixStream::connect(path)?;
Self::new(socket).map(Some)
}
/// Reads any pending data on the socket into the internal buffer.
///
/// Returns `UnexpectedEof` if end-of-file is reached.
///
/// # Errors
///
/// Will return `Err` if there is an I/O error.
pub fn read(&self) -> io::Result<usize> {
self.0.read()
}
/// Returns an event that is readily available.
// XXX iterator
pub fn pending_event(&self) -> Option<PendingRequestResult<Event>> {
self.0.pending(Event::parse)
}
/// Returns the interface proxy for the `ei_handshake` object.
#[must_use]
#[allow(clippy::missing_panics_doc)] // infallible; Backend always creates ei_handshake object at 0
pub fn handshake(&self) -> handshake::Handshake {
self.0.object_for_id(0).unwrap().downcast_unchecked()
}
/// Sends buffered messages. Call after you're finished with sending requests.
///
/// # Errors
///
/// An error will be returned if sending the buffered messages fails.
pub fn flush(&self) -> rustix::io::Result<()> {
self.0.flush()
}
}
#[doc(hidden)]
pub trait Interface: crate::wire::Interface {}