Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat: add subject container id to IPC response for commands

Lars Berger4aa38d3f328b
diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rs
index 3170bd8a..10b62872 100644
--- a/packages/wm/src/app_command.rs
+++ b/packages/wm/src/app_command.rs
@@ -56,13 +56,14 @@ pub enum AppCommand {
/// Requires an already running instance of the window manager.
Cmd {
#[clap(short = 'c')]
- context_container_id: Option<Uuid>,
+ subject_container_id: Option<Uuid>,
#[clap(subcommand)]
command: InvokeCommand,
},
- /// Subscribe to a WM event (e.g. `window_close`) and print
+ /// Subscribe to a WM event (e.g. `window_close`) and continuously print
+ /// the incoming events.
///
/// Requires an already running instance of the window manager.
Subscribe {
@@ -131,6 +132,7 @@ pub enum QueryCommand {
#[derive(Clone, Debug, ValueEnum)]
#[clap(rename_all = "snake_case")]
pub enum SubscribableEvent {
+ All,
BindingModesChanged,
FocusChanged,
FocusedContainerMoved,
diff --git a/packages/wm/src/ipc_server.rs b/packages/wm/src/ipc_server.rs
index 55d339f6..d29981f2 100644
--- a/packages/wm/src/ipc_server.rs
+++ b/packages/wm/src/ipc_server.rs
@@ -9,18 +9,17 @@ use tokio::{
sync::{mpsc, Mutex},
task,
};
-use tokio_tungstenite::{
- accept_async, tungstenite::Message, WebSocketStream,
-};
+use tokio_tungstenite::{accept_async, tungstenite::Message};
use tracing::{error, info};
use uuid::Uuid;
use crate::{
- app_command::{AppCommand, InvokeCommand, QueryCommand},
+ app_command::{AppCommand, QueryCommand},
containers::{Container, WindowContainer},
monitors::Monitor,
+ user_config::UserConfig,
+ wm::WindowManager,
wm_event::WmEvent,
- wm_state::WmState,
workspaces::Workspace,
};
@@ -50,7 +49,13 @@ pub enum ClientResponseData {
Workspaces(Vec<Workspace>),
BindingModes(Vec<String>),
Focused(Option<Container>),
- Command,
+ Command(CommandResponseData),
+}
+
+#[derive(Debug, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct CommandResponseData {
+ subject_container_id: Uuid,
}
#[derive(Debug, Serialize)]
@@ -65,15 +70,12 @@ pub struct EventSubscriptionMessage {
#[derive(Debug)]
struct EventSubscription {
subscription_id: String,
- stream: WebSocketStream<TcpStream>,
+ // stream: mpsc::UnboundedSender<EventSubscriptionMessage>,
}
pub struct IpcServer {
pub message_rx:
mpsc::UnboundedReceiver<(String, mpsc::UnboundedSender<Message>)>,
- pub wm_command_rx:
- mpsc::UnboundedReceiver<(InvokeCommand, Option<Uuid>)>,
- pub wm_command_tx: mpsc::UnboundedSender<(InvokeCommand, Option<Uuid>)>,
abort_handle: task::AbortHandle,
// /// Hashmap of event names and connections subscribed to that event.
// event_subs: Arc<Mutex<HashMap<Uuid, EventSubscription>>>,
@@ -82,7 +84,6 @@ pub struct IpcServer {
impl IpcServer {
pub async fn start() -> anyhow::Result<Self> {
let (message_tx, message_rx) = mpsc::unbounded_channel();
- let (wm_command_tx, wm_command_rx) = mpsc::unbounded_channel();
let server_addr = format!("127.0.0.1:{}", DEFAULT_IPC_PORT);
let server = TcpListener::bind(server_addr.clone()).await?;
@@ -112,8 +113,6 @@ impl IpcServer {
Ok(Self {
message_rx,
- wm_command_rx,
- wm_command_tx,
abort_handle: task.abort_handle(),
// event_subs: event_subs.clone(),
})
@@ -135,6 +134,7 @@ impl IpcServer {
.context("Error during websocket handshake.")?;
let (response_tx, mut response_rx) = mpsc::unbounded_channel();
+ // let (disconnection_tx, mut disconnection_rx) = mpsc::unbounded_channel();
let (mut outgoing, mut incoming) = ws_stream.split();
loop {
@@ -171,43 +171,50 @@ impl IpcServer {
Ok(())
}
- pub async fn process_message(
+ pub fn process_message(
&self,
message: String,
response_tx: mpsc::UnboundedSender<Message>,
- state: &mut WmState,
+ wm: &mut WindowManager,
+ config: &mut UserConfig,
) -> anyhow::Result<()> {
let app_command = AppCommand::try_parse_from(
iter::once("").chain(message.split_whitespace()),
- )?;
+ );
// TODO: Handle subscribe messages.
let response = match app_command {
- AppCommand::Query { command } => match command {
+ Ok(AppCommand::Query { command }) => match command {
QueryCommand::Windows => {
- Ok(ClientResponseData::Windows(state.windows()))
+ Ok(ClientResponseData::Windows(wm.state.windows()))
}
QueryCommand::Workspaces => {
- Ok(ClientResponseData::Workspaces(state.workspaces()))
+ Ok(ClientResponseData::Workspaces(wm.state.workspaces()))
}
QueryCommand::Monitors => {
- Ok(ClientResponseData::Monitors(state.monitors()))
+ Ok(ClientResponseData::Monitors(wm.state.monitors()))
}
QueryCommand::BindingModes => Ok(
- ClientResponseData::BindingModes(state.binding_modes.clone()),
+ ClientResponseData::BindingModes(wm.state.binding_modes.clone()),
),
QueryCommand::Focused => {
- Ok(ClientResponseData::Focused(state.focused_container()))
+ Ok(ClientResponseData::Focused(wm.state.focused_container()))
}
},
- AppCommand::Cmd {
- context_container_id,
+ Ok(AppCommand::Cmd {
+ subject_container_id,
command,
- } => self
- .wm_command_tx
- .send((command, context_container_id))
- .map_err(|_| anyhow::anyhow!("Failed to send WM command."))
- .and_then(|_| Ok(ClientResponseData::Command)),
+ }) => wm
+ .process_commands(vec![command], subject_container_id, config)
+ .map(|subject_container_id| {
+ ClientResponseData::Command(CommandResponseData {
+ subject_container_id,
+ })
+ }),
+ Ok(AppCommand::Subscribe { events }) => {
+ todo!()
+ }
+ Err(err) => Err(anyhow::anyhow!(err)),
_ => Err(anyhow::anyhow!("Unsupported IPC command.")),
};
@@ -228,10 +235,10 @@ impl IpcServer {
Ok(())
}
- pub async fn process_event(
+ pub fn process_event(
&mut self,
event: WmEvent,
- state: &mut WmState,
+ wm: &mut WindowManager,
) -> anyhow::Result<()> {
// // TODO: Spawn a task so that it doesn't block main thread execution.
// let subscriptions = self.event_subscriptions.lock().await;
diff --git a/packages/wm/src/main.rs b/packages/wm/src/main.rs
index fdc64696..d1c91aeb 100644
--- a/packages/wm/src/main.rs
+++ b/packages/wm/src/main.rs
@@ -93,27 +93,23 @@ async fn start_wm(
tokio::select! {
Some(event) = event_listener.event_rx.recv() => {
debug!("Received platform event: {:?}", event);
- let res = wm.process_event(event, &mut config);
- if let Err(err) = res {
+ if let Err(err) = wm.process_event(event, &mut config) {
error!("Failed to process event: {:?}", err);
}
},
Some((message, response_tx)) = ipc_server.message_rx.recv() => {
info!("Received IPC message: {:?}", message);
- ipc_server.process_message(message, response_tx, &mut wm.state).await;
- },
- Some((command, subject_container_id)) = ipc_server.wm_command_rx.recv() => {
- info!("Received WM command via IPC: {:?}", command);
- let res = wm
- .process_commands(vec![command], subject_container_id, &mut config);
- if let Err(err) = res {
- error!("Failed to process command: {:?}", err);
+ if let Err(err) = ipc_server
+ .process_message(message, response_tx, &mut wm, &mut config)
+ {
+ error!("Failed to process IPC message: {:?}", err);
}
},
Some(_) = config.changes_rx.recv() => {
info!("Received user config update: {:?}", config);
+
event_listener.update(&config, &Vec::new());
},
Some(wm_event) = wm.event_rx.recv() => {
@@ -123,7 +119,9 @@ async fn start_wm(
event_listener.update(&config, active_binding_modes);
}
- ipc_server.process_event(wm_event, &mut wm.state).await;
+ if let Err(err) = ipc_server.process_event(wm_event, &mut wm) {
+ error!("Failed to emit event over IPC: {:?}", err);
+ }
},
}
}
diff --git a/packages/wm/src/wm.rs b/packages/wm/src/wm.rs
index b7546df8..4632b568 100644
--- a/packages/wm/src/wm.rs
+++ b/packages/wm/src/wm.rs
@@ -51,7 +51,8 @@ impl WindowManager {
}
PlatformEvent::KeybindingTriggered(kb_config) => {
// Return early since we don't want to redraw twice.
- return self.process_commands(kb_config.commands, None, config);
+ self.process_commands(kb_config.commands, None, config)?;
+ return Ok(());
}
PlatformEvent::MouseMove(event) => {
handle_mouse_move(event, state, config)
@@ -94,7 +95,7 @@ impl WindowManager {
commands: Vec<InvokeCommand>,
subject_container_id: Option<Uuid>,
config: &mut UserConfig,
- ) -> anyhow::Result<()> {
+ ) -> anyhow::Result<Uuid> {
let state = &mut self.state;
// Get the container to run WM commands with.
@@ -124,6 +125,6 @@ impl WindowManager {
redraw(state)?;
sync_native_focus(state)?;
- Ok(())
+ Ok(subject_container.id())
}
}