cosmic-applets/cosmic-app-list/src/wayland_subscription.rs

157 lines
4.2 KiB
Rust
Raw Normal View History

//! # DBus interface proxy for: `org.freedesktop.UPower.KbdBacklight`
//!
//! This code was generated by `zbus-xmlgen` `2.0.1` from DBus introspection data.
//! Source: `Interface '/org/freedesktop/UPower/KbdBacklight' from service 'org.freedesktop.UPower' on system bus`.
2024-07-01 00:35:23 +02:00
use cctk::{
sctk::{output::OutputInfo, reexports::calloop},
toplevel_info::ToplevelInfo,
wayland_client::protocol::wl_output::WlOutput,
};
2024-10-30 22:51:08 -04:00
use cosmic::{
iced::{self, stream, Subscription},
iced_core::image::Bytes,
};
2024-07-01 00:35:23 +02:00
use cosmic_protocols::{
toplevel_info::v1::client::zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
workspace::v1::client::zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1,
};
use image::EncodableLayout;
use futures::{
channel::mpsc::{unbounded, UnboundedReceiver},
SinkExt, StreamExt,
};
use once_cell::sync::Lazy;
2024-10-30 22:51:08 -04:00
use std::fmt::Debug;
use tokio::sync::Mutex;
2023-11-06 16:46:44 +01:00
use crate::wayland_handler::wayland_handler;
pub static WAYLAND_RX: Lazy<Mutex<Option<UnboundedReceiver<WaylandUpdate>>>> =
Lazy::new(|| Mutex::new(None));
pub fn wayland_subscription() -> iced::Subscription<WaylandUpdate> {
2024-10-30 22:51:08 -04:00
Subscription::run_with_id(
std::any::TypeId::of::<WaylandUpdate>(),
2024-10-30 22:51:08 -04:00
stream::channel(50, move |mut output| async move {
let mut state = State::Waiting;
loop {
state = start_listening(state, &mut output).await;
}
2024-10-30 22:51:08 -04:00
}),
)
}
pub enum State {
Waiting,
Finished,
}
#[derive(Debug, Clone)]
pub struct WaylandImage {
2024-10-30 22:51:08 -04:00
pub img: Bytes,
pub width: u32,
pub height: u32,
}
impl WaylandImage {
pub fn new(img: image::RgbaImage) -> Self {
2024-10-30 22:51:08 -04:00
Self {
// TODO avoid copy?
img: Bytes::copy_from_slice(img.as_bytes()),
width: img.width(),
height: img.height(),
}
}
}
impl AsRef<[u8]> for WaylandImage {
fn as_ref(&self) -> &[u8] {
self.img.as_bytes()
}
}
async fn start_listening(
state: State,
2023-11-06 16:46:44 +01:00
output: &mut futures::channel::mpsc::Sender<WaylandUpdate>,
) -> State {
match state {
State::Waiting => {
let mut guard = WAYLAND_RX.lock().await;
let rx = {
if guard.is_none() {
let (calloop_tx, calloop_rx) = calloop::channel::channel();
let (toplevel_tx, toplevel_rx) = unbounded();
let _ = std::thread::spawn(move || {
wayland_handler(toplevel_tx, calloop_rx);
});
*guard = Some(toplevel_rx);
_ = output.send(WaylandUpdate::Init(calloop_tx)).await;
}
guard.as_mut().unwrap()
};
match rx.next().await {
Some(u) => {
_ = output.send(u).await;
State::Waiting
}
None => {
2023-11-06 16:46:44 +01:00
_ = output.send(WaylandUpdate::Finished).await;
tracing::error!("Wayland handler thread died");
2023-11-16 17:26:18 +00:00
State::Finished
}
}
}
State::Finished => iced::futures::future::pending().await,
}
}
#[derive(Clone, Debug)]
2023-11-06 16:46:44 +01:00
pub enum WaylandUpdate {
Init(calloop::channel::Sender<WaylandRequest>),
Finished,
2023-11-06 16:46:44 +01:00
Toplevel(ToplevelUpdate),
Workspace(Vec<ZcosmicWorkspaceHandleV1>),
Output(OutputUpdate),
2024-01-31 12:44:56 +00:00
ActivationToken {
token: Option<String>,
app_id: Option<String>,
2024-01-31 12:44:56 +00:00
exec: String,
gpu_idx: Option<usize>,
},
Image(ZcosmicToplevelHandleV1, WaylandImage),
2023-11-06 16:46:44 +01:00
}
#[derive(Clone, Debug)]
pub enum ToplevelUpdate {
2023-11-16 18:20:57 +00:00
Add(ZcosmicToplevelHandleV1, ToplevelInfo),
Update(ZcosmicToplevelHandleV1, ToplevelInfo),
Remove(ZcosmicToplevelHandleV1),
2023-11-06 16:46:44 +01:00
}
2024-04-09 14:45:04 -05:00
#[derive(Clone, Debug)]
pub enum OutputUpdate {
Add(WlOutput, OutputInfo),
Update(WlOutput, OutputInfo),
Remove(WlOutput),
}
2023-11-06 16:46:44 +01:00
#[derive(Clone, Debug)]
pub enum WaylandRequest {
Toplevel(ToplevelRequest),
2024-01-31 12:44:56 +00:00
TokenRequest {
app_id: String,
exec: String,
gpu_idx: Option<usize>,
},
Screencopy(ZcosmicToplevelHandleV1),
}
#[derive(Debug, Clone)]
pub enum ToplevelRequest {
Activate(ZcosmicToplevelHandleV1),
Minimize(ZcosmicToplevelHandleV1),
Quit(ZcosmicToplevelHandleV1),
}