2022-09-25 22:49:00 +01:00
|
|
|
use async_once::AsyncOnce;
|
|
|
|
use color_eyre::Report;
|
|
|
|
use futures_util::StreamExt;
|
2022-08-25 21:53:42 +01:00
|
|
|
use lazy_static::lazy_static;
|
2022-09-25 22:49:00 +01:00
|
|
|
use std::sync::Arc;
|
2022-10-10 21:59:44 +01:00
|
|
|
use swayipc_async::{Connection, Event, EventType, WorkspaceEvent};
|
2022-08-25 21:53:42 +01:00
|
|
|
use tokio::spawn;
|
2022-09-25 22:49:00 +01:00
|
|
|
use tokio::sync::broadcast::{channel, Receiver, Sender};
|
|
|
|
use tokio::sync::Mutex;
|
|
|
|
use tracing::{info, trace};
|
2022-08-14 20:40:11 +01:00
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
pub struct SwayEventClient {
|
|
|
|
workspace_tx: Sender<Box<WorkspaceEvent>>,
|
|
|
|
_workspace_rx: Receiver<Box<WorkspaceEvent>>,
|
2022-08-14 20:40:11 +01:00
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
impl SwayEventClient {
|
|
|
|
fn new() -> Self {
|
|
|
|
let (workspace_tx, workspace_rx) = channel(16);
|
2022-08-28 16:57:41 +01:00
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
let workspace_tx2 = workspace_tx.clone();
|
2022-08-21 23:36:07 +01:00
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
spawn(async move {
|
|
|
|
let workspace_tx = workspace_tx2;
|
2022-08-21 23:36:07 +01:00
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
let client = Connection::new().await?;
|
|
|
|
info!("Sway IPC subscription client connected");
|
2022-08-25 21:53:42 +01:00
|
|
|
|
2022-10-10 21:59:44 +01:00
|
|
|
let event_types = [EventType::Workspace];
|
2022-08-21 23:36:07 +01:00
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
let mut events = client.subscribe(event_types).await?;
|
2022-08-25 21:53:42 +01:00
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
while let Some(event) = events.next().await {
|
|
|
|
trace!("event: {:?}", event);
|
2022-10-10 21:59:44 +01:00
|
|
|
if let Event::Workspace(ev) = event? {
|
|
|
|
workspace_tx.send(ev)?;
|
2022-09-25 22:49:00 +01:00
|
|
|
};
|
2022-08-25 21:53:42 +01:00
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
Ok::<(), Report>(())
|
|
|
|
});
|
2022-08-21 23:36:07 +01:00
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
Self {
|
|
|
|
workspace_tx,
|
|
|
|
_workspace_rx: workspace_rx,
|
2022-08-21 23:36:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
/// Gets an event receiver for workspace events
|
|
|
|
pub fn subscribe_workspace(&self) -> Receiver<Box<WorkspaceEvent>> {
|
|
|
|
self.workspace_tx.subscribe()
|
2022-08-21 23:36:07 +01:00
|
|
|
}
|
|
|
|
}
|
2022-08-25 21:53:42 +01:00
|
|
|
|
|
|
|
lazy_static! {
|
2022-09-25 22:49:00 +01:00
|
|
|
static ref CLIENT: AsyncOnce<Arc<Mutex<Connection>>> = AsyncOnce::new(async {
|
|
|
|
let client = Connection::new()
|
|
|
|
.await
|
|
|
|
.expect("Failed to connect to Sway socket");
|
|
|
|
Arc::new(Mutex::new(client))
|
|
|
|
});
|
|
|
|
static ref SUB_CLIENT: SwayEventClient = SwayEventClient::new();
|
2022-08-25 21:53:42 +01:00
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
/// Gets the sway IPC client
|
|
|
|
pub async fn get_client() -> Arc<Mutex<Connection>> {
|
|
|
|
let client = CLIENT.get().await;
|
|
|
|
Arc::clone(client)
|
2022-08-25 21:53:42 +01:00
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
/// Gets the sway IPC event subscription client
|
|
|
|
pub fn get_sub_client() -> &'static SwayEventClient {
|
|
|
|
&SUB_CLIENT
|
2022-08-25 21:53:42 +01:00
|
|
|
}
|