Skip to content

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

feat: remove `wm_command` mod; replace uses with `InvokeCommand`

Lars Berger4ea1d3791ac4
diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rs
index 14942233..b8025588 100644
--- a/packages/wm/src/app_command.rs
+++ b/packages/wm/src/app_command.rs
@@ -1,11 +1,12 @@
use std::path::PathBuf;
use clap::{Args, Parser, Subcommand};
+use serde::{Deserialize, Deserializer};
use tracing::Level;
use crate::common::{Direction, LengthValue, TilingDirection};
-#[derive(Parser, Debug)]
+#[derive(Parser, Clone, Debug)]
#[clap(author, version, about, long_about = None)]
pub enum AppCommand {
/// Starts the window manager.
@@ -58,7 +59,7 @@ impl AppCommand {
}
/// Verbosity flags to be used with `#[command(flatten)]`.
-#[derive(Args, Debug)]
+#[derive(Args, Clone, Debug)]
#[clap(about = None, long_about = None)]
pub struct Verbosity {
/// Enables verbose logging.
@@ -81,7 +82,7 @@ impl Verbosity {
}
}
-#[derive(Subcommand, Debug)]
+#[derive(Subcommand, Clone, Debug)]
#[clap(rename_all = "snake_case")]
pub enum QueryCommand {
/// Prints all windows.
@@ -94,7 +95,7 @@ pub enum QueryCommand {
Focused,
}
-#[derive(Subcommand, Debug)]
+#[derive(Parser, Clone, Debug)]
#[clap(rename_all = "snake_case")]
pub enum InvokeCommand {
Close,
@@ -155,7 +156,18 @@ pub enum InvokeCommand {
},
}
-#[derive(Args, Debug)]
+impl<'de> Deserialize<'de> for InvokeCommand {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ let str = String::deserialize(deserializer)?;
+ InvokeCommand::try_parse_from(str.split_whitespace())
+ .map_err(serde::de::Error::custom)
+ }
+}
+
+#[derive(Args, Clone, Debug)]
#[group(required = true, multiple = true)]
pub struct InvokeChangeBordersCommand {
#[clap(long)]
@@ -165,7 +177,7 @@ pub struct InvokeChangeBordersCommand {
height: Option<LengthValue>,
}
-#[derive(Args, Debug)]
+#[derive(Args, Clone, Debug)]
#[group(required = true, multiple = false)]
pub struct InvokeFocusCommand {
#[clap(long = "dir")]
@@ -184,7 +196,7 @@ pub struct InvokeFocusCommand {
recent_workspace: bool,
}
-#[derive(Args, Debug)]
+#[derive(Args, Clone, Debug)]
#[group(required = true, multiple = false)]
pub struct InvokeMoveCommand {
/// Direction to move the window.
@@ -196,7 +208,7 @@ pub struct InvokeMoveCommand {
workspace: Option<String>,
}
-#[derive(Args, Debug)]
+#[derive(Args, Clone, Debug)]
#[group(required = true, multiple = true)]
pub struct InvokeResizeCommand {
#[clap(long)]
diff --git a/packages/wm/src/ipc_server.rs b/packages/wm/src/ipc_server.rs
index 1229a35a..bfd9fa86 100644
--- a/packages/wm/src/ipc_server.rs
+++ b/packages/wm/src/ipc_server.rs
@@ -6,7 +6,7 @@ use ipc_client::DEFAULT_IPC_ADDR;
use tokio::{
net::TcpListener,
sync::{
- mpsc::{self, UnboundedReceiver, UnboundedSender},
+ mpsc::{self},
Mutex,
},
task,
@@ -14,7 +14,9 @@ use tokio::{
use tokio_tungstenite::accept_async;
use tracing::info;
-use crate::{wm_command::WmCommand, wm_event::WmEvent, wm_state::WmState};
+use crate::{
+ app_command::InvokeCommand, wm_event::WmEvent, wm_state::WmState,
+};
#[derive(Debug)]
pub enum IpcMessage {
@@ -25,8 +27,8 @@ pub enum IpcMessage {
}
pub struct IpcServer {
- pub message_rx: UnboundedReceiver<IpcMessage>,
- pub wm_command_rx: UnboundedReceiver<WmCommand>,
+ pub message_rx: mpsc::UnboundedReceiver<IpcMessage>,
+ pub wm_command_rx: mpsc::UnboundedReceiver<InvokeCommand>,
}
impl IpcServer {
@@ -34,7 +36,7 @@ impl IpcServer {
let (message_tx, message_rx) = mpsc::unbounded_channel::<IpcMessage>();
let (wm_command_tx, wm_command_rx) =
- mpsc::unbounded_channel::<WmCommand>();
+ mpsc::unbounded_channel::<InvokeCommand>();
let server = TcpListener::bind(DEFAULT_IPC_ADDR).await?;
diff --git a/packages/wm/src/main.rs b/packages/wm/src/main.rs
index be5150a2..6cecae2e 100644
--- a/packages/wm/src/main.rs
+++ b/packages/wm/src/main.rs
@@ -1,20 +1,27 @@
-use clap::Parser;
+use std::{env, path::PathBuf};
+
+use anyhow::{Context, Result};
+use tokio::process::Command;
use tracing::info;
+use common::platform::Platform;
+use ipc_server::IpcServer;
+use user_config::UserConfig;
+use wm::WindowManager;
+
use crate::app_command::AppCommand;
mod app_command;
mod common;
-// mod containers;
-// mod ipc_server;
-// mod monitors;
-// mod user_config;
+mod containers;
+mod ipc_server;
+mod monitors;
+mod user_config;
mod windows;
-// mod wm;
-// mod wm_command;
-// mod wm_event;
-// mod wm_state;
-// mod workspaces;
+mod wm;
+mod wm_event;
+mod wm_state;
+mod workspaces;
#[tokio::main]
async fn main() {
@@ -23,6 +30,7 @@ async fn main() {
config_path,
verbosity,
} => {
+ // Set log level based on verbosity flag.
tracing_subscriber::fmt()
.with_max_level(verbosity.level())
.init();
@@ -32,19 +40,15 @@ async fn main() {
verbosity.level().to_string()
);
- // start_wm(config_path).await?;
+ if let Err(err) = start_wm(config_path).await {
+ eprintln!("Failed to start GlazeWM: {}", err);
+ }
}
_ => todo!(),
}
-
- // let config_path = None;
-
- // if let Err(err) = start_wm(config_path).await {
- // eprintln!("Failed to start GlazeWM: {}", err);
- // }
}
-async fn start_wm(config_path: Option<String>) -> Result<()> {
+async fn start_wm(config_path: Option<PathBuf>) -> Result<()> {
// Ensure that only one instance of the WM is running.
let _ = Platform::new_single_instance()?;
diff --git a/packages/wm/src/user_config.rs b/packages/wm/src/user_config.rs
index 1604f382..29fa7013 100644
--- a/packages/wm/src/user_config.rs
+++ b/packages/wm/src/user_config.rs
@@ -8,9 +8,9 @@ use tokio::{
};
use crate::{
+ app_command::InvokeCommand,
common::{LengthValue, RectDelta},
monitors::Monitor,
- wm_command::WmCommand,
workspaces::Workspace,
};
@@ -20,7 +20,7 @@ pub struct KeybindingConfig {
pub bindings: Vec<String>,
/// WM commands to run when the keybinding is triggered.
- pub commands: Vec<WmCommand>,
+ pub commands: Vec<InvokeCommand>,
}
#[derive(Clone, Debug, Deserialize)]
@@ -100,7 +100,7 @@ pub struct WindowRuleConfig {
pub match_process_name: Option<String>,
pub match_class_name: Option<String>,
pub match_title: Option<String>,
- pub commands: Vec<WmCommand>,
+ pub commands: Vec<InvokeCommand>,
}
#[derive(Clone, Debug, Deserialize)]
@@ -135,16 +135,13 @@ const SAMPLE_CONFIG: &str =
impl UserConfig {
/// Read and validate the user config from the given path.
pub async fn read(
- config_path: Option<String>,
+ config_path: Option<PathBuf>,
) -> Result<Arc<Mutex<Self>>> {
let default_config_path = home::home_dir()
.context("Unable to get home directory.")?
.join(".glzr/glazewm/config.yaml");
- let config_path = match config_path {
- Some(val) => PathBuf::from(val),
- None => default_config_path,
- };
+ let config_path = config_path.unwrap_or(default_config_path);
// Create new config file from sample if it doesn't exist.
if !config_path.exists() {
diff --git a/packages/wm/src/wm.rs b/packages/wm/src/wm.rs
index 3b594954..08addedf 100644
--- a/packages/wm/src/wm.rs
+++ b/packages/wm/src/wm.rs
@@ -2,20 +2,17 @@ use std::sync::Arc;
use anyhow::Result;
use tokio::sync::{
- mpsc::{self, UnboundedReceiver, UnboundedSender},
+ mpsc::{self},
Mutex,
};
use crate::{
- common::platform::PlatformEvent,
- user_config::{ParsedConfig, UserConfig},
- wm_command::WmCommand,
- wm_event::WmEvent,
- wm_state::WmState,
+ app_command::InvokeCommand, common::platform::PlatformEvent,
+ user_config::UserConfig, wm_event::WmEvent, wm_state::WmState,
};
pub struct WindowManager {
- pub event_rx: UnboundedReceiver<WmEvent>,
+ pub event_rx: mpsc::UnboundedReceiver<WmEvent>,
pub state: Arc<Mutex<WmState>>,
}
@@ -37,74 +34,7 @@ impl WindowManager {
// TODO
}
- pub async fn process_command(&mut self, command: WmCommand) {
- match command {
- WmCommand::CloseWindow => {
- todo!()
- }
- WmCommand::DisableBindingMode(_) => {
- todo!()
- }
- WmCommand::ExitWm => {
- todo!()
- }
- WmCommand::EnableBindingMode(_) => {
- todo!()
- }
- WmCommand::FocusInDirection(_) => {
- todo!()
- }
- WmCommand::FocusRecentWorkspace => {
- todo!()
- }
- WmCommand::FocusWorkspaceInSequence => {
- todo!()
- }
- WmCommand::FocusWorkspace(_) => {
- todo!()
- }
- WmCommand::IgnoreWindow => {
- todo!()
- }
- WmCommand::MoveWindow(_) => {
- todo!()
- }
- WmCommand::MoveWindowToWorkspace(_) => {
- todo!()
- }
- WmCommand::MoveWorkspace(_) => {
- todo!()
- }
- WmCommand::Noop => {
- todo!()
- }
- WmCommand::Redraw => {
- todo!()
- }
- WmCommand::ReloadConfig => {
- todo!()
- }
- WmCommand::ResizeWindowWidth(_) => {
- todo!()
- }
- WmCommand::ResizeWindowHeight(_) => {
- todo!()
- }
- WmCommand::SetTilingDirection(_) => {
- todo!()
- }
- WmCommand::SetWindowBorders(_) => {
- todo!()
- }
- WmCommand::SetWindowFloating => {
- todo!()
- }
- WmCommand::ToggleTilingDirection => {
- todo!()
- }
- WmCommand::ToggleFocusMode => {
- todo!()
- }
- }
+ pub async fn process_command(&mut self, command: InvokeCommand) {
+ // TODO
}
}
diff --git a/packages/wm/src/wm_command.rs b/packages/wm/src/wm_command.rs
deleted file mode 100644
index f7e06045..00000000
--- a/packages/wm/src/wm_command.rs
+++ /dev/null
@@ -1,90 +0,0 @@
-use anyhow::Result;
-use serde::{Deserialize, Deserializer};
-
-use crate::common::{Direction, LengthUnit, TilingDirection};
-
-#[derive(Clone, Debug)]
-pub enum WmCommand {
- CloseWindow,
- DisableBindingMode(String),
- ExitWm,
- EnableBindingMode(String),
- FocusInDirection(Direction),
- FocusRecentWorkspace,
- FocusWorkspaceInSequence,
- FocusWorkspace(String),
- IgnoreWindow,
- MoveWindow(Direction),
- MoveWindowToWorkspace(String),
- MoveWorkspace(Direction),
- Noop,
- Redraw,
- ReloadConfig,
- ResizeWindowWidth(LengthUnit),
- ResizeWindowHeight(LengthUnit),
- SetTilingDirection(TilingDirection),
- SetWindowBorders(LengthUnit),
- SetWindowFloating,
- ToggleTilingDirection,
- ToggleFocusMode,
-}
-
-impl WmCommand {
- pub fn from_str(unparsed: &str) -> Result<Self> {
- let parts: Vec<&str> = unparsed.split_whitespace().collect();
-
- let command = match parts.as_slice() {
- ["close_window"] => WmCommand::CloseWindow,
- ["exit_wm"] => WmCommand::ExitWm,
- ["disable_binding_mode", name] => {
- WmCommand::DisableBindingMode(name.to_string())
- }
- ["focus", direction] => {
- WmCommand::FocusInDirection(Direction::from_str(direction)?)
- }
- ["focus_workspace", name] => {
- WmCommand::FocusWorkspace(name.to_string())
- }
- ["enable_binding_mode", name] => {
- WmCommand::EnableBindingMode(name.to_string())
- }
- ["ignore_window"] => WmCommand::IgnoreWindow,
- ["move_window", direction] => {
- WmCommand::MoveWindow(Direction::from_str(direction)?)
- }
- ["move_window_to_workspace", name] => {
- WmCommand::MoveWindowToWorkspace(name.to_string())
- }
- ["move_workspace", direction] => {
- WmCommand::MoveWorkspace(Direction::from_str(direction)?)
- }
- ["noop"] => WmCommand::Noop,
- ["redraw"] => WmCommand::Redraw,
- ["reload_config"] => WmCommand::ReloadConfig,
- ["set_tiling_direction", tiling_direction] => {
- WmCommand::SetTilingDirection(TilingDirection::from_str(
- tiling_direction,
- )?)
- }
- ["toggle_tiling_direction", "vertical"] => {
- WmCommand::ToggleTilingDirection
- }
- ["toggle_focus_mode"] => WmCommand::ToggleFocusMode,
- // _ => bail!("Not a known command."),
- // TODO
- _ => WmCommand::Noop,
- };
-
- Ok(command)
- }
-}
-
-impl<'de> Deserialize<'de> for WmCommand {
- fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
- where
- D: Deserializer<'de>,
- {
- let str = String::deserialize(deserializer)?;
- Self::from_str(&str).map_err(serde::de::Error::custom)
- }
-}
diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs
index 28c63ac2..d4f2eb8b 100644
--- a/packages/wm/src/wm_state.rs
+++ b/packages/wm/src/wm_state.rs
@@ -1,17 +1,12 @@
-use anyhow::anyhow;
use tokio::sync::mpsc::UnboundedSender;
use tracing::warn;
use uuid::Uuid;
use crate::{
- common::{
- platform::{NativeMonitor, NativeWindow, Platform},
- TilingDirection,
- },
+ common::platform::{NativeMonitor, NativeWindow, Platform},
containers::{
- commands::attach_container,
- traits::{CommonBehavior, TilingBehavior},
- Container, RootContainer, WindowContainer,
+ commands::attach_container, traits::CommonBehavior, Container,
+ RootContainer, WindowContainer,
},
monitors::{commands::add_monitor, Monitor},
user_config::{BindingModeConfig, UserConfig},