use std::collections::HashMap;
use std::ffi::OsString;
use std::os::unix::net::UnixStream;
use std::os::fd::{AsRawFd, RawFd};
use std::process::Command;
use std::sync::{Arc, RwLock};
use smithay::{
desktop::{PopupManager, Space, Window, WindowSurfaceType},
input::{Seat, SeatState},
reexports::{
wayland_server::{
backend::{ClientData, ClientId, DisconnectReason},
},
calloop::{generic::Generic, EventLoop, Interest, LoopSignal, Mode, PostAction, RegistrationToken}
},
wayland::{
socket::ListeningSocketSource,
},
};
pub struct TabbDE {
pub start_time: std::time::Instant,
pub client_contexts: HashMap<OsString, Arc<ClientContext>>
}
impl TabbDE {
pub fn new(event_loop: &mut EventLoop<TabbDE>) -> Self {
let start_time = std::time::Instant::now();
Self {
start_time,
client_contexts: Default::default(),
}
}
pub fn init_subprocess(&mut self, event_loop: &mut EventLoop<TabbDE>, command: &mut Command) {
let created_time = std::time::Instant::now();
let listening_socket = ListeningSocketSource::new_auto().unwrap();
let socket_name = listening_socket.socket_name().to_os_string();
command.env("WAYLAND_DISPLAY", &socket_name);
let loop_handle = event_loop.handle();
let listener_token = loop_handle
.insert_source(listening_socket, move |client_stream, _, state| {
let addr = client_stream
.local_addr()
.unwrap();
let path = addr
.as_pathname()
.unwrap()
.as_os_str();
let fd = client_stream.as_raw_fd();
let context = ClientConnection {
created_time: std::time::Instant::now(),
client_stream
};
state
.client_contexts[path]
.as_ref()
.connections
.write()
.expect("Couldn't lock state.client_contexts[].connection")
.insert(fd, context);
})
.expect("Failed to init the wayland event source.");
let context = ClientContext {
socket_name: socket_name.clone(),
created_time,
listener_token,
connections: Default::default()
};
self.client_contexts.insert(socket_name, Arc::new(context));
command
.spawn()
.expect("Couldn't open subprocess");
command.env_remove("WAYLAND_DISPLAY");
}
}
pub struct ClientContext {
pub socket_name: OsString,
pub created_time: std::time::Instant,
pub listener_token: RegistrationToken,
pub connections: RwLock<HashMap<RawFd, ClientConnection>>
}
pub struct ClientConnection {
pub created_time: std::time::Instant,
pub client_stream: UnixStream
}