1use std::{env, os::unix::net::UnixStream, path::PathBuf};
2
3pub struct Display {
4 socket: UnixStream,
5}
6
7#[derive(Clone, Copy, PartialEq, Eq, Debug)]
8pub enum ConnectError {
9 XDG_RUNTIME_DIR_NOT_SET,
10 WAYLAND_DISPLAY_NOT_SET,
11 SOCKET_CONNECTION_FAILED,
12}
13
14impl Display {
15 pub fn connect() -> Result<Display, ConnectError> {
18 let socket_path = {
19 let mut path = PathBuf::from(env::var_os("XDG_RUNTIME_DIR").ok_or(ConnectError::XDG_RUNTIME_DIR_NOT_SET)?);
20 path.push(env::var_os("WAYLAND_DISPLAY").ok_or(ConnectError::WAYLAND_DISPLAY_NOT_SET)?);
21 path
22 };
23 println!("Connecting to socket at {:?}", socket_path);
24
25 let socket = UnixStream::connect(socket_path).map_err(|_| ConnectError::SOCKET_CONNECTION_FAILED)?;
26
27 Ok(Display { socket })
28 }
29}