refactor: connect to privileged socket if possible

This commit is contained in:
Ashley Wulber 2023-11-07 19:50:16 -05:00 • committed by Ashley Wulber
parent 21fc43e578
commit 2141230229
2 changed files with 41 additions and 14 deletions

View file

@ -1,4 +1,9 @@
use crate::wayland_subscription::{ToplevelRequest, ToplevelUpdate, WaylandRequest, WaylandUpdate}; use crate::wayland_subscription::{ToplevelRequest, ToplevelUpdate, WaylandRequest, WaylandUpdate};
use std::os::{
fd::{FromRawFd, RawFd},
unix::net::UnixStream,
};
use cctk::{ use cctk::{
sctk::{ sctk::{
self, self,
@ -171,7 +176,19 @@ pub(crate) fn wayland_handler(
tx: UnboundedSender<WaylandUpdate>, tx: UnboundedSender<WaylandUpdate>,
rx: calloop::channel::Channel<WaylandRequest>, rx: calloop::channel::Channel<WaylandRequest>,
) { ) {
let conn = Connection::connect_to_env().unwrap(); let socket = std::env::var("X_PRIVILEGED_WAYLAND_SOCKET")
.ok()
.and_then(|fd| {
fd.parse::<RawFd>()
.ok()
.map(|fd| unsafe { UnixStream::from_raw_fd(fd) })
});
let conn = if let Some(socket) = socket {
Connection::from_socket(socket).unwrap()
} else {
Connection::connect_to_env().unwrap()
};
let (globals, event_queue) = registry_queue_init(&conn).unwrap(); let (globals, event_queue) = registry_queue_init(&conn).unwrap();
let mut event_loop = calloop::EventLoop::<AppData>::try_new().unwrap(); let mut event_loop = calloop::EventLoop::<AppData>::try_new().unwrap();

View file

@ -14,12 +14,18 @@ use cctk::{
}; };
use cosmic_protocols::workspace::v1::client::zcosmic_workspace_handle_v1; use cosmic_protocols::workspace::v1::client::zcosmic_workspace_handle_v1;
use futures::{channel::mpsc, executor::block_on, SinkExt}; use futures::{channel::mpsc, executor::block_on, SinkExt};
use std::{env, os::unix::net::UnixStream, path::PathBuf, time::Duration}; use std::{
os::{
fd::{FromRawFd, RawFd},
unix::net::UnixStream,
},
time::Duration,
};
use wayland_client::backend::ObjectId; use wayland_client::backend::ObjectId;
use wayland_client::{ use wayland_client::{
globals::registry_queue_init, globals::registry_queue_init,
protocol::wl_output::{self, WlOutput}, protocol::wl_output::{self, WlOutput},
ConnectError, Proxy, Proxy,
}; };
use wayland_client::{Connection, QueueHandle, WEnum}; use wayland_client::{Connection, QueueHandle, WEnum};
@ -33,18 +39,22 @@ pub type WorkspaceList = Vec<(String, Option<zcosmic_workspace_handle_v1::State>
pub fn spawn_workspaces(tx: mpsc::Sender<WorkspaceList>) -> SyncSender<WorkspaceEvent> { pub fn spawn_workspaces(tx: mpsc::Sender<WorkspaceList>) -> SyncSender<WorkspaceEvent> {
let (workspaces_tx, workspaces_rx) = calloop::channel::sync_channel(100); let (workspaces_tx, workspaces_rx) = calloop::channel::sync_channel(100);
if let Ok(Ok(conn)) = std::env::var("WAYLAND_DISPLAY") let socket = std::env::var("X_PRIVILEGED_WAYLAND_SOCKET")
.map_err(anyhow::Error::msg) .ok()
.map(|display_str| { .and_then(|fd| {
let mut socket_path = env::var_os("XDG_RUNTIME_DIR") fd.parse::<RawFd>()
.map(Into::<PathBuf>::into) .ok()
.ok_or(ConnectError::NoCompositor)?; .map(|fd| unsafe { UnixStream::from_raw_fd(fd) })
socket_path.push(display_str); });
Ok(UnixStream::connect(socket_path).map_err(|_| ConnectError::NoCompositor)?) let conn = if let Some(socket) = socket {
}) Connection::from_socket(socket)
.and_then(|s| s.map(|s| Connection::from_socket(s).map_err(anyhow::Error::msg))) } else {
{ Connection::connect_to_env()
}
.map_err(anyhow::Error::msg);
if let Ok(conn) = conn {
std::thread::spawn(move || { std::thread::spawn(move || {
let configured_output = std::env::var("COSMIC_PANEL_OUTPUT") let configured_output = std::env::var("COSMIC_PANEL_OUTPUT")
.ok() .ok()