cosmic-applets/applets/cosmic-applet-workspaces/src/wayland.rs

406 lines
14 KiB
Rust
Raw Normal View History

2022-06-20 15:48:27 -04:00
use crate::{
2022-07-27 00:37:00 -04:00
utils::WorkspaceEvent,
2022-06-20 15:48:27 -04:00
wayland_source::WaylandSource,
};
2022-07-19 23:39:19 -04:00
use calloop::channel::*;
2022-07-27 00:37:00 -04:00
use cosmic_panel_config::CosmicPanelOuput;
2022-07-19 23:39:19 -04:00
use cosmic_protocols::workspace::v1::client::{
zcosmic_workspace_group_handle_v1::{self, ZcosmicWorkspaceGroupHandleV1},
zcosmic_workspace_handle_v1::{self, ZcosmicWorkspaceHandleV1},
zcosmic_workspace_manager_v1::{self, ZcosmicWorkspaceManagerV1},
};
2022-06-16 13:00:27 -04:00
use gtk4::glib;
use std::{
2022-07-27 00:37:00 -04:00
env, os::unix::net::UnixStream, path::PathBuf, time::Duration,
};
2022-06-16 10:48:15 -04:00
use wayland_client::{
2022-06-20 15:48:27 -04:00
event_created_child,
protocol::{
wl_output::{self, WlOutput},
wl_registry,
},
2022-07-27 00:37:00 -04:00
ConnectError,
2022-06-16 10:48:15 -04:00
};
2022-07-08 23:22:08 +02:00
use wayland_client::{Connection, Dispatch, QueueHandle};
2022-06-16 11:55:31 -04:00
2022-06-23 12:15:08 -04:00
pub fn spawn_workspaces(tx: glib::Sender<State>) -> SyncSender<WorkspaceEvent> {
2022-07-19 11:33:19 -04:00
let (workspaces_tx, workspaces_rx) = calloop::channel::sync_channel(100);
2022-06-20 16:15:19 -04:00
2022-07-19 11:33:19 -04:00
if let Ok(Ok(conn)) = std::env::var("WAYLAND_DISPLAY")
.map_err(anyhow::Error::msg)
2022-06-16 12:23:25 -04:00
.map(|display_str| {
2022-06-16 10:48:15 -04:00
let mut socket_path = env::var_os("XDG_RUNTIME_DIR")
.map(Into::<PathBuf>::into)
.ok_or(ConnectError::NoCompositor)?;
2022-06-16 12:23:25 -04:00
socket_path.push(display_str);
2022-06-16 10:48:15 -04:00
Ok(UnixStream::connect(socket_path).map_err(|_| ConnectError::NoCompositor)?)
})
.and_then(|s| s.map(|s| Connection::from_socket(s).map_err(anyhow::Error::msg)))
{
std::thread::spawn(move || {
2022-07-27 00:37:00 -04:00
let output = std::env::var("COSMIC_PANEL_OUTPUT").ok().and_then(|size| match size.parse::<CosmicPanelOuput>() {
Ok(CosmicPanelOuput::Name(n)) => Some(n),
// TODO handle Active & panic if the space is still configured for All instead of being assigned a named output
_ => Some("".to_string()),
}).unwrap_or_default();
2022-06-20 13:15:12 -04:00
let mut event_loop = calloop::EventLoop::<State>::try_new().unwrap();
let loop_handle = event_loop.handle();
let event_queue = conn.new_event_queue::<State>();
2022-06-16 10:48:15 -04:00
let qhandle = event_queue.handle();
2022-06-20 15:48:27 -04:00
WaylandSource::new(event_queue)
.expect("Failed to create wayland source")
.insert(loop_handle)
.unwrap();
2022-06-20 13:15:12 -04:00
2022-06-16 10:48:15 -04:00
let display = conn.display();
display.get_registry(&qhandle, ()).unwrap();
2022-06-16 11:55:31 -04:00
2022-06-16 10:48:15 -04:00
let mut state = State {
workspace_manager: None,
2022-06-16 11:55:31 -04:00
workspace_groups: Vec::new(),
configured_output: output,
expected_output: None,
2022-06-16 10:48:15 -04:00
tx,
running: true,
};
2022-06-23 12:15:08 -04:00
let loop_handle = event_loop.handle();
loop_handle
.insert_source(workspaces_rx, |e, _, state| match e {
2022-06-23 12:15:08 -04:00
Event::Msg(WorkspaceEvent::Activate(id)) => {
if let Some(w) = state
.workspace_groups
.iter()
.find_map(|g| g.workspaces.iter().find(|w| w.name == id))
{
w.workspace_handle.activate();
state.workspace_manager.as_ref().unwrap().commit();
2022-06-20 14:42:12 -04:00
}
2022-06-23 12:15:08 -04:00
}
Event::Msg(WorkspaceEvent::Scroll(v)) => {
2022-07-19 23:39:19 -04:00
if let Some((w_g, w_i)) = state.workspace_groups.iter().find_map(|g| {
if g.output != state.expected_output {
return None;
}
g.workspaces
.iter()
.position(|w| {
w.states
.contains(&zcosmic_workspace_handle_v1::State::Active)
})
.map(|w_i| (g, w_i))
}) {
2022-06-23 12:15:08 -04:00
let max_w = w_g.workspaces.len().wrapping_sub(1);
let d_i = if v > 0.0 {
if w_i == max_w {
0
2022-06-20 15:48:27 -04:00
} else {
2022-06-23 12:15:08 -04:00
w_i.wrapping_add(1)
2022-06-20 14:42:12 -04:00
}
2022-06-23 12:15:08 -04:00
} else {
if w_i == 0 {
max_w
} else {
w_i.wrapping_sub(1)
}
};
if let Some(w) = w_g.workspaces.get(d_i) {
w.workspace_handle.activate();
state.workspace_manager.as_ref().unwrap().commit();
2022-06-20 14:42:12 -04:00
}
}
2022-06-16 14:33:26 -04:00
}
Event::Closed => {
if let Some(workspace_manager) = &mut state.workspace_manager {
for g in &mut state.workspace_groups {
g.workspace_group_handle.destroy();
}
workspace_manager.stop();
2022-06-23 12:15:08 -04:00
}
}
})
.unwrap();
2022-06-23 12:15:08 -04:00
while state.running {
2022-06-20 15:48:27 -04:00
event_loop
.dispatch(Duration::from_millis(16), &mut state)
.unwrap();
}
});
} else {
2022-07-19 11:33:19 -04:00
eprintln!("ENV variable WAYLAND_DISPLAY is missing. Exiting...");
std::process::exit(1);
}
workspaces_tx
}
2022-06-15 16:51:08 -04:00
2022-06-16 12:08:31 -04:00
#[derive(Debug, Clone)]
pub struct State {
2022-06-16 10:48:15 -04:00
running: bool,
2022-06-16 13:00:27 -04:00
tx: glib::Sender<State>,
configured_output: String,
expected_output: Option<WlOutput>,
2022-07-08 23:22:08 +02:00
workspace_manager: Option<ZcosmicWorkspaceManagerV1>,
2022-06-16 11:55:31 -04:00
workspace_groups: Vec<WorkspaceGroup>,
}
impl State {
2022-06-16 14:33:26 -04:00
// XXX
2022-06-20 15:48:27 -04:00
pub fn workspace_list(&self) -> impl Iterator<Item = (String, u32)> + '_ {
self.workspace_groups
.iter()
.filter_map(|g| {
if g.output == self.expected_output {
2022-07-19 23:39:19 -04:00
Some(g.workspaces.iter().map(|w| {
(
w.name.clone(),
match &w.states {
x if x.contains(&zcosmic_workspace_handle_v1::State::Active) => 0,
x if x.contains(&zcosmic_workspace_handle_v1::State::Urgent) => 1,
x if x.contains(&zcosmic_workspace_handle_v1::State::Hidden) => 2,
_ => 3,
},
)
}))
} else {
None
}
})
2022-06-20 15:48:27 -04:00
.flatten()
}
}
2022-06-16 12:08:31 -04:00
#[derive(Debug, Clone)]
2022-06-16 11:55:31 -04:00
struct WorkspaceGroup {
2022-07-08 23:22:08 +02:00
workspace_group_handle: ZcosmicWorkspaceGroupHandleV1,
2022-06-16 11:55:31 -04:00
output: Option<WlOutput>,
workspaces: Vec<Workspace>,
}
2022-06-16 12:08:31 -04:00
#[derive(Debug, Clone)]
2022-06-16 11:55:31 -04:00
struct Workspace {
2022-07-08 23:22:08 +02:00
workspace_handle: ZcosmicWorkspaceHandleV1,
2022-06-16 11:55:31 -04:00
name: String,
coordinates: Vec<u32>,
states: Vec<zcosmic_workspace_handle_v1::State>,
2022-06-16 10:48:15 -04:00
}
2022-06-15 16:51:08 -04:00
2022-06-16 10:48:15 -04:00
impl Dispatch<wl_registry::WlRegistry, ()> for State {
fn event(
2022-07-19 11:33:19 -04:00
state: &mut Self,
2022-06-16 10:48:15 -04:00
registry: &wl_registry::WlRegistry,
event: wl_registry::Event,
_: &(),
_: &Connection,
qh: &QueueHandle<Self>,
) {
2022-06-16 11:55:31 -04:00
if let wl_registry::Event::Global {
name,
interface,
version,
} = event
{
2022-06-16 10:48:15 -04:00
match &interface[..] {
2022-07-08 23:22:08 +02:00
"zcosmic_workspace_manager_v1" => {
2022-06-16 11:55:31 -04:00
let workspace_manager = registry
2022-07-19 23:39:19 -04:00
.bind::<ZcosmicWorkspaceManagerV1, _, _>(name, 1, qh, ())
2022-06-16 11:55:31 -04:00
.unwrap();
2022-07-19 11:33:19 -04:00
state.workspace_manager = Some(workspace_manager);
2022-06-16 11:55:31 -04:00
}
2022-06-16 12:23:25 -04:00
"wl_output" => {
registry.bind::<WlOutput, _, _>(name, 1, qh, ()).unwrap();
}
2022-06-16 10:48:15 -04:00
_ => {}
}
}
}
}
2022-06-15 16:51:08 -04:00
2022-07-08 23:22:08 +02:00
impl Dispatch<ZcosmicWorkspaceManagerV1, ()> for State {
2022-06-16 10:48:15 -04:00
fn event(
2022-07-19 11:33:19 -04:00
state: &mut Self,
2022-07-08 23:22:08 +02:00
_: &ZcosmicWorkspaceManagerV1,
event: zcosmic_workspace_manager_v1::Event,
2022-06-16 10:48:15 -04:00
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
2022-06-16 11:55:31 -04:00
match event {
2022-07-08 23:22:08 +02:00
zcosmic_workspace_manager_v1::Event::WorkspaceGroup { workspace_group } => {
2022-07-19 11:33:19 -04:00
state.workspace_groups.push(WorkspaceGroup {
2022-06-16 11:55:31 -04:00
workspace_group_handle: workspace_group,
output: None,
workspaces: Vec::new(),
});
}
2022-07-08 23:22:08 +02:00
zcosmic_workspace_manager_v1::Event::Done => {
2022-07-19 11:33:19 -04:00
for group in &mut state.workspace_groups {
group.workspaces.sort_by(|w1, w2| {
2022-07-19 23:39:19 -04:00
w1.coordinates
.iter()
.zip(w2.coordinates.iter())
.skip_while(|(coord1, coord2)| coord1 == coord2)
.next()
.map(|(coord1, coord2)| coord1.cmp(coord2))
.unwrap_or(std::cmp::Ordering::Equal)
});
}
2022-07-19 11:33:19 -04:00
let _ = state.tx.send(state.clone());
2022-06-16 11:55:31 -04:00
}
2022-07-08 23:22:08 +02:00
zcosmic_workspace_manager_v1::Event::Finished => {
2022-07-19 11:33:19 -04:00
state.workspace_manager.take();
2022-06-16 11:55:31 -04:00
}
2022-07-08 23:22:08 +02:00
_ => {}
2022-06-16 11:55:31 -04:00
}
2022-06-16 10:48:15 -04:00
}
2022-07-08 23:22:08 +02:00
event_created_child!(State, ZcosmicWorkspaceManagerV1, [
0 => (ZcosmicWorkspaceGroupHandleV1, ())
]);
2022-06-16 10:48:15 -04:00
}
2022-06-16 11:55:31 -04:00
2022-07-08 23:22:08 +02:00
impl Dispatch<ZcosmicWorkspaceGroupHandleV1, ()> for State {
2022-06-16 11:55:31 -04:00
fn event(
2022-07-19 11:33:19 -04:00
state: &mut Self,
2022-07-08 23:22:08 +02:00
group: &ZcosmicWorkspaceGroupHandleV1,
event: zcosmic_workspace_group_handle_v1::Event,
2022-06-16 11:55:31 -04:00
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
match event {
2022-07-08 23:22:08 +02:00
zcosmic_workspace_group_handle_v1::Event::OutputEnter { output } => {
2022-07-19 11:33:19 -04:00
if let Some(group) = state
2022-06-16 11:55:31 -04:00
.workspace_groups
.iter_mut()
.find(|g| &g.workspace_group_handle == group)
{
group.output = Some(output);
}
}
2022-07-08 23:22:08 +02:00
zcosmic_workspace_group_handle_v1::Event::OutputLeave { output } => {
2022-07-19 11:33:19 -04:00
if let Some(group) = state.workspace_groups.iter_mut().find(|g| {
2022-06-16 11:55:31 -04:00
&g.workspace_group_handle == group && g.output.as_ref() == Some(&output)
}) {
group.output = None;
}
}
2022-07-08 23:22:08 +02:00
zcosmic_workspace_group_handle_v1::Event::Workspace { workspace } => {
2022-07-19 11:33:19 -04:00
if let Some(group) = state
2022-06-16 11:55:31 -04:00
.workspace_groups
.iter_mut()
.find(|g| &g.workspace_group_handle == group)
{
group.workspaces.push(Workspace {
workspace_handle: workspace,
name: String::new(),
coordinates: Vec::new(),
states: Vec::new(),
2022-06-16 11:55:31 -04:00
})
}
}
2022-07-08 23:22:08 +02:00
zcosmic_workspace_group_handle_v1::Event::Remove => {
2022-07-19 11:33:19 -04:00
if let Some(group) = state
2022-06-16 11:55:31 -04:00
.workspace_groups
.iter()
.position(|g| &g.workspace_group_handle == group)
{
2022-07-19 11:33:19 -04:00
state.workspace_groups.remove(group);
2022-06-16 11:55:31 -04:00
}
}
2022-07-08 23:22:08 +02:00
_ => {}
2022-06-16 11:55:31 -04:00
}
}
2022-07-08 23:22:08 +02:00
event_created_child!(State, ZcosmicWorkspaceGroupHandleV1, [
3 => (ZcosmicWorkspaceHandleV1, ())
]);
2022-06-16 11:55:31 -04:00
}
2022-07-08 23:22:08 +02:00
impl Dispatch<ZcosmicWorkspaceHandleV1, ()> for State {
2022-06-16 11:55:31 -04:00
fn event(
2022-07-19 11:33:19 -04:00
state: &mut Self,
2022-07-08 23:22:08 +02:00
workspace: &ZcosmicWorkspaceHandleV1,
event: zcosmic_workspace_handle_v1::Event,
2022-06-16 11:55:31 -04:00
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
match event {
2022-07-08 23:22:08 +02:00
zcosmic_workspace_handle_v1::Event::Name { name } => {
2022-07-19 11:33:19 -04:00
if let Some(w) = state.workspace_groups.iter_mut().find_map(|g| {
2022-06-16 11:55:31 -04:00
g.workspaces
.iter_mut()
.find(|w| &w.workspace_handle == workspace)
}) {
w.name = name;
}
}
2022-07-08 23:22:08 +02:00
zcosmic_workspace_handle_v1::Event::Coordinates { coordinates } => {
2022-07-19 11:33:19 -04:00
if let Some(w) = state.workspace_groups.iter_mut().find_map(|g| {
2022-06-16 11:55:31 -04:00
g.workspaces
.iter_mut()
.find(|w| &w.workspace_handle == workspace)
}) {
// wayland is host byte order
2022-07-19 23:39:19 -04:00
w.coordinates = coordinates
.chunks(4)
.map(|chunk| u32::from_ne_bytes(chunk.try_into().unwrap()))
.collect();
2022-06-16 11:55:31 -04:00
}
}
2022-07-19 23:39:19 -04:00
zcosmic_workspace_handle_v1::Event::State {
state: workspace_state,
} => {
2022-07-19 11:33:19 -04:00
if let Some(w) = state.workspace_groups.iter_mut().find_map(|g| {
2022-06-16 11:55:31 -04:00
g.workspaces
.iter_mut()
.find(|w| &w.workspace_handle == workspace)
}) {
// wayland is host byte order
2022-07-19 23:39:19 -04:00
w.states = workspace_state
.chunks(4)
.map(|chunk| {
zcosmic_workspace_handle_v1::State::try_from(u32::from_ne_bytes(
chunk.try_into().unwrap(),
))
.unwrap()
})
.collect();
2022-06-16 11:55:31 -04:00
}
}
2022-07-08 23:22:08 +02:00
zcosmic_workspace_handle_v1::Event::Remove => {
2022-07-19 11:33:19 -04:00
if let Some((g, w_i)) = state.workspace_groups.iter_mut().find_map(|g| {
2022-06-16 11:55:31 -04:00
g.workspaces
.iter_mut()
.position(|w| &w.workspace_handle == workspace)
.map(|p| (g, p))
}) {
g.workspaces.remove(w_i);
}
}
2022-07-08 23:22:08 +02:00
_ => {}
2022-06-16 11:55:31 -04:00
}
}
}
2022-06-16 12:23:25 -04:00
impl Dispatch<WlOutput, ()> for State {
fn event(
2022-07-19 11:33:19 -04:00
state: &mut Self,
o: &WlOutput,
e: wl_output::Event,
2022-06-16 12:23:25 -04:00
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
2022-06-20 15:48:27 -04:00
) {
match e {
2022-07-19 11:33:19 -04:00
wl_output::Event::Name { name } if name == state.configured_output => {
state.expected_output.replace(o.clone());
}
_ => {} // ignored
}
2022-06-20 15:48:27 -04:00
}
2022-06-16 12:23:25 -04:00
}