Skip to content

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

feat: get class name, process name, and title of `NativeWindow`

Lars Berger41bf87ae7097
diff --git a/packages/wm/src/cli.rs b/packages/wm/src/cli.rs
index df1de5ef..8f8a4397 100644
--- a/packages/wm/src/cli.rs
+++ b/packages/wm/src/cli.rs
@@ -9,14 +9,14 @@ pub struct Cli {
#[derive(Subcommand, Debug)]
pub enum CliCommand {
- /// Start the window manager.
+ /// Starts the window manager.
Start {
/// Custom path to user config file.
#[clap(short, long)]
config_path: Option<String>,
},
- /// Query the window manager's state.
+ /// Queries the window manager's state.
Query {
windows: bool,
monitors: bool,
@@ -24,6 +24,6 @@ pub enum CliCommand {
focused_container: bool,
},
- /// Run a window manager command.
+ /// Runs a window manager command.
Cmd {},
}
diff --git a/packages/wm/src/common/direction.rs b/packages/wm/src/common/direction.rs
index 968668f4..0c0d9453 100644
--- a/packages/wm/src/common/direction.rs
+++ b/packages/wm/src/common/direction.rs
@@ -9,7 +9,7 @@ pub enum Direction {
}
impl Direction {
- /// Get the inverse of a given direction.
+ /// Gets the inverse of a given direction.
///
/// Example:
/// ```
@@ -24,7 +24,7 @@ impl Direction {
}
}
- /// Parse a string into a direction.
+ /// Parses a string into a direction.
///
/// Example:
/// ```
diff --git a/packages/wm/src/common/platform/event_listener.rs b/packages/wm/src/common/platform/event_listener.rs
index aea3640c..f52c60ce 100644
--- a/packages/wm/src/common/platform/event_listener.rs
+++ b/packages/wm/src/common/platform/event_listener.rs
@@ -34,7 +34,7 @@ pub struct EventListener {
}
impl EventListener {
- /// Start listening for platform events.
+ /// Starts listening for platform events.
pub async fn start(
config: Arc<Mutex<UserConfig>>,
config_changes_rx: UnboundedReceiver<UserConfig>,
diff --git a/packages/wm/src/common/platform/event_window.rs b/packages/wm/src/common/platform/event_window.rs
index 19b61361..c887acdd 100644
--- a/packages/wm/src/common/platform/event_window.rs
+++ b/packages/wm/src/common/platform/event_window.rs
@@ -183,7 +183,7 @@ impl EventWindow {
}
}
- /// Create several window event hooks via `SetWinEventHook`.
+ /// Creates several window event hooks via `SetWinEventHook`.
fn hook_win_events() -> Result<Vec<HWINEVENTHOOK>> {
let event_ranges = [
(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE),
@@ -207,7 +207,7 @@ impl EventWindow {
})
}
- /// Create a window hook for the specified event range.
+ /// Creates a window hook for the specified event range.
unsafe fn hook_win_event(
event_min: u32,
event_max: u32,
@@ -229,7 +229,7 @@ impl EventWindow {
Ok(hook_handle)
}
- /// Create the event window and start a message loop.
+ /// Creates the event window and starts a message loop.
unsafe fn create_window(
mut abort_rx: oneshot::Receiver<()>,
) -> Result<HWND> {
@@ -283,7 +283,7 @@ impl EventWindow {
Ok(handle)
}
- /// Destroy the event window and stop the message loop.
+ /// Destroys the event window and stops the message loop.
pub fn destroy(&mut self) {
if let Some(abort_tx) = self.abort_tx.take() {
if abort_tx.send(()).is_err() {
diff --git a/packages/wm/src/common/platform/native_window.rs b/packages/wm/src/common/platform/native_window.rs
index 99aab9e1..868de830 100644
--- a/packages/wm/src/common/platform/native_window.rs
+++ b/packages/wm/src/common/platform/native_window.rs
@@ -1,6 +1,19 @@
use std::cell::RefCell;
use anyhow::Result;
+use windows::{
+ core::PWSTR,
+ Win32::{
+ Foundation::CloseHandle,
+ System::Threading::{
+ OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32,
+ PROCESS_QUERY_INFORMATION,
+ },
+ UI::WindowsAndMessaging::{
+ GetClassNameW, GetWindowTextW, GetWindowThreadProcessId,
+ },
+ },
+};
use super::WindowHandle;
@@ -22,38 +35,75 @@ impl NativeWindow {
}
}
- // fn title(&self) -> Result<String> {
- // match self.title {
- // Some(title) => Ok(title.borrow().clone()),
- // None => {
- // let title = windows_api::window_title_by_handle()?;
- // self.title = Some(RefCell::new(title));
- // Ok(title)
- // }
- // }
- // }
+ /// Gets the window title.
+ ///
+ /// If the window is invalid, returns an empty string.
+ fn title(&self) -> String {
+ match self.title {
+ Some(title) => title.borrow().clone(),
+ None => {
+ let mut text: [u16; 512] = [0; 512];
+ let length = unsafe { GetWindowTextW(self.handle, &mut text) };
- // fn process_name(&self) -> Result<String> {
- // match self.process_name {
- // Some(process_name) => Ok(process_name.borrow().clone()),
- // None => {
- // let process_name = windows_api::process_name_by_handle()?;
- // self.process_name = Some(RefCell::new(process_name));
- // Ok(process_name)
- // }
- // }
- // }
+ let title = String::from_utf16_lossy(&text[..length as usize]);
+ self.title = Some(RefCell::new(title));
+ title
+ }
+ }
+ }
- // fn class_name(&self) -> Result<String> {
- // match self.class_name {
- // Some(class_name) => Ok(class_name.borrow().clone()),
- // None => {
- // let class_name = windows_api::class_name_by_handle()?;
- // self.class_name = Some(RefCell::new(class_name));
- // Ok(class_name)
- // }
- // }
- // }
+ fn process_name(&self) -> Result<String> {
+ match self.process_name {
+ Some(process_name) => Ok(process_name.borrow().clone()),
+ None => {
+ let mut process_id = 0u32;
+ unsafe {
+ GetWindowThreadProcessId(self.handle, Some(&mut process_id));
+ }
+
+ let process_handle = unsafe {
+ OpenProcess(PROCESS_QUERY_INFORMATION, false, process_id)
+ }?;
+
+ let mut buffer = [0u16; 256];
+ let mut length = buffer.len() as u32;
+ unsafe {
+ QueryFullProcessImageNameW(
+ process_handle,
+ PROCESS_NAME_WIN32,
+ PWSTR(buffer.as_mut_ptr()),
+ &mut length,
+ )?;
+
+ CloseHandle(process_handle)?;
+ };
+
+ let process_name = String::from_utf16(&buffer[..length as usize])?;
+ self.process_name = Some(RefCell::new(process_name));
+ Ok(process_name)
+ }
+ }
+ }
+
+ fn class_name(&self) -> Result<String> {
+ match self.class_name {
+ Some(class_name) => Ok(class_name.borrow().clone()),
+ None => {
+ let mut buffer = [0u16; 256];
+ let result = unsafe { GetClassNameW(self.handle, &mut buffer) };
+
+ if result == 0 {
+ return Err(windows::core::Error::from_win32().into());
+ }
+
+ let class_name =
+ String::from_utf16_lossy(&buffer[..result as usize]);
+ println!("Class name: {}", class_name);
+ self.class_name = Some(RefCell::new(class_name));
+ Ok(class_name)
+ }
+ }
+ }
pub fn is_visible(&self) -> bool {
todo!()
diff --git a/packages/wm/src/common/tiling_direction.rs b/packages/wm/src/common/tiling_direction.rs
index dee10cdb..3cea6aea 100644
--- a/packages/wm/src/common/tiling_direction.rs
+++ b/packages/wm/src/common/tiling_direction.rs
@@ -9,7 +9,7 @@ pub enum TilingDirection {
}
impl TilingDirection {
- /// Get the inverse of a given tiling direction.
+ /// Gets the inverse of a given tiling direction.
///
/// Example:
/// ```
@@ -22,7 +22,7 @@ impl TilingDirection {
}
}
- /// Get the tiling direction that is needed when moving or shifting
+ /// Gets the tiling direction that is needed when moving or shifting
/// focus in a given direction.
///
/// Example:
diff --git a/packages/wm/src/containers/common_container.rs b/packages/wm/src/containers/common_container.rs
index 5379e4a8..4ac5b5df 100644
--- a/packages/wm/src/containers/common_container.rs
+++ b/packages/wm/src/containers/common_container.rs
@@ -9,7 +9,7 @@ pub trait CommonContainer {
fn borrow_children(&self) -> Ref<'_, Vec<ContainerRef>>;
fn borrow_children_mut(&self) -> RefMut<'_, Vec<ContainerRef>>;
- /// Return a reference to the parent node, unless this node is the root
+ /// Returns a reference to the parent node, unless this node is the root
/// of the tree.
///
/// # Panics
diff --git a/packages/wm/src/main.rs b/packages/wm/src/main.rs
index 9c5f3d49..e70cc47f 100644
--- a/packages/wm/src/main.rs
+++ b/packages/wm/src/main.rs
@@ -108,7 +108,7 @@ async fn start_wm(config_path: Option<String>) -> Result<()> {
}
}
-/// Launch watcher binary. This is a separate process that is responsible
+/// Launches watcher binary. This is a separate process that is responsible
/// for restoring hidden windows in case the main WM process crashes.
///
/// This assumes the watcher binary exists in the same directory as the WM