GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
fix: use subscription id from client response for cli event subscription
Lars Bergere44410ae81e8
diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rsindex ec3bba38..94a3a9db 100644--- a/packages/wm/src/app_command.rs+++ b/packages/wm/src/app_command.rs@@ -43,7 +43,7 @@ pub enum AppCommand {verbosity: Verbosity,},- /// Prints the window manager's state.+ /// Retrieves and outputs a specific part of the window manager's state.////// Requires an already running instance of the window manager.Query {@@ -62,8 +62,8 @@ pub enum AppCommand {command: InvokeCommand,},- /// Subscribe to one or more WM events (e.g. `window_close`), and- /// continuously print the incoming events.+ /// Subscribes to one or more WM events (e.g. `window_close`), and+ /// continuously outputs the incoming events.////// Requires an already running instance of the window manager.Subscribe {@@ -72,7 +72,7 @@ pub enum AppCommand {events: Vec<SubscribableEvent>,},- /// Unsubscribe from a WM event subscription.+ /// Unsubscribes from a prior event subscription.////// Requires an already running instance of the window manager.Unsubscribe {@@ -126,15 +126,15 @@ impl Verbosity {#[derive(Clone, Debug, Parser)]pub enum QueryCommand {- /// Prints all windows.+ /// Outputs all windows.Windows,- /// Prints all active workspaces.+ /// Outputs all active workspaces.Workspaces,- /// Prints all monitors.+ /// Outputs all monitors.Monitors,- /// Prints the active binding modes.+ /// Outputs the active binding modes.BindingModes,- /// Prints the focused container (either a window or an empty workspace).+ /// Outputs the focused container (either a window or an empty workspace).Focused,}diff --git a/packages/wm/src/ipc_client.rs b/packages/wm/src/ipc_client.rsindex 9dc0c06e..7c621df2 100644--- a/packages/wm/src/ipc_client.rs+++ b/packages/wm/src/ipc_client.rs@@ -1,5 +1,5 @@use anyhow::Context;-use futures_util::{stream, SinkExt, Stream, StreamExt};+use futures_util::{SinkExt, StreamExt};use serde::{Deserialize, Serialize};use serde_json::value::RawValue;use tokio::net::TcpStream;diff --git a/packages/wm/src/ipc_server.rs b/packages/wm/src/ipc_server.rsindex b5033578..58a8e14e 100644--- a/packages/wm/src/ipc_server.rs+++ b/packages/wm/src/ipc_server.rs@@ -3,7 +3,7 @@ use std::{iter, net::SocketAddr};use anyhow::Context;use clap::Parser;use futures_util::{SinkExt, StreamExt};-use serde::Serialize;+use serde::{Deserialize, Serialize};use tokio::{net::{TcpListener, TcpStream},sync::{broadcast, mpsc},@@ -35,10 +35,10 @@ pub enum ServerMessage {#[derive(Debug, Serialize)]#[serde(rename_all = "camelCase")]pub struct ClientResponseMessage {- client_message: String,- data: Option<ClientResponseData>,- error: Option<String>,- success: bool,+ pub client_message: String,+ pub data: Option<ClientResponseData>,+ pub error: Option<String>,+ pub success: bool,}#[derive(Debug, Serialize)]@@ -57,22 +57,22 @@ pub enum ClientResponseData {#[derive(Debug, Serialize)]#[serde(rename_all = "camelCase")]pub struct CommandData {- subject_container_id: Uuid,+ pub subject_container_id: Uuid,}-#[derive(Debug, Serialize)]+#[derive(Debug, Deserialize, Serialize)]#[serde(rename_all = "camelCase")]pub struct EventSubscribeData {- subscription_id: Uuid,+ pub subscription_id: Uuid,}#[derive(Debug, Serialize)]#[serde(rename_all = "camelCase")]pub struct EventSubscriptionMessage {- data: Option<serde_json::Value>,- error: Option<String>,- subscription_id: Uuid,- success: bool,+ pub data: Option<serde_json::Value>,+ pub error: Option<String>,+ pub subscription_id: Uuid,+ pub success: bool,}pub struct IpcServer {diff --git a/packages/wm/src/main.rs b/packages/wm/src/main.rsindex 7deaf007..8582eaea 100644--- a/packages/wm/src/main.rs+++ b/packages/wm/src/main.rs@@ -14,7 +14,7 @@ use tokio::process::Command;use tracing::{debug, error, info};use common::platform::Platform;-use ipc_server::IpcServer;+use ipc_server::{EventSubscribeData, IpcServer};use user_config::UserConfig;use wm::WindowManager;@@ -47,7 +47,7 @@ async fn main() {config_path,verbosity,} => start_wm(config_path, verbosity).await,- _ => start_cli(args).await,+ _ => start_cli(args, app_command).await,};if let Err(err) = res {@@ -135,7 +135,10 @@ async fn start_wm(}}-async fn start_cli(args: Vec<String>) -> Result<()> {+async fn start_cli(+ args: Vec<String>,+ app_command: AppCommand,+) -> Result<()> {tracing_subscriber::fmt().init();let mut client = IpcClient::connect().await?;@@ -151,21 +154,28 @@ async fn start_cli(args: Vec<String>) -> Result<()> {.await.context("Failed to receive response from IPC server.")?;- match client_response.subscription_id {- // Exit on first response received when not subscribing to an event.- None => {- println!("{}", serde_json::to_string(&client_response.data)?);- std::process::exit(0);- }- // Otherwise, continuously listen for server responses.- Some(subscription_id) => loop {- let response = client- .event_subscription(&subscription_id)- .await- .context("Failed to receive response from IPC server.")?;-- println!("{}", serde_json::to_string(&response.data)?);- },+ let is_subscribe_message =+ matches!(app_command, AppCommand::Subscribe { .. });++ // Exit on first response received when not subscribing to an event.+ if !is_subscribe_message {+ println!("{}", serde_json::to_string(&client_response)?);+ std::process::exit(0);+ }++ // Otherwise, expect a subscription ID in the client response.+ let subscribe_data = serde_json::from_str::<EventSubscribeData>(+ client_response.data.get(),+ )?;++ // Continuously listen for server responses.+ loop {+ let event_subscription = client+ .event_subscription(&subscribe_data.subscription_id)+ .await+ .context("Failed to receive response from IPC server.")?;++ println!("{}", serde_json::to_string(&event_subscription)?);}}