GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: add fn for getting all available monitors
Lars Berger39ec14aa715a
diff --git a/packages/wm/src/common/platform/native_monitor.rs b/packages/wm/src/common/platform/native_monitor.rsindex 4cc915d7..73100239 100644--- a/packages/wm/src/common/platform/native_monitor.rs+++ b/packages/wm/src/common/platform/native_monitor.rs@@ -1,20 +1,33 @@+use windows::Win32::{+ Foundation::{BOOL, LPARAM, RECT},+ Graphics::Gdi::{+ EnumDisplayMonitors, GetMonitorInfoW, HDC, HMONITOR, MONITORINFOEXW,+ },+};++pub type MonitorHandle = HMONITOR;++#[derive(Debug)]pub struct NativeMonitor {+ pub handle: MonitorHandle,pub device_name: String,- pub width: u32,- pub height: u32,- pub x: u32,- pub y: u32,+ pub width: i32,+ pub height: i32,+ pub x: i32,+ pub y: i32,}impl NativeMonitor {pub fn new(+ handle: MonitorHandle,device_name: String,- width: u32,- height: u32,- x: u32,- y: u32,+ width: i32,+ height: i32,+ x: i32,+ y: i32,) -> Self {Self {+ handle,device_name,width,height,@@ -23,3 +36,66 @@ impl NativeMonitor {}}}++pub fn available_monitors() -> Vec<NativeMonitor> {+ available_monitor_handles()+ .into_iter()+ .filter_map(|handle| handle_to_monitor(handle).ok())+ .collect()+}++/// Gets all available monitor handles.+fn available_monitor_handles() -> Vec<MonitorHandle> {+ let mut monitors: Vec<MonitorHandle> = Vec::new();++ unsafe {+ EnumDisplayMonitors(+ HDC::default(),+ None,+ Some(available_monitor_handles_proc),+ LPARAM(&mut monitors as *mut _ as _),+ );+ }++ monitors+}++/// Callback passed to `EnumDisplayMonitors` to get all available monitor+/// handles.+unsafe extern "system" fn available_monitor_handles_proc(+ handle: MonitorHandle,+ _hdc: HDC,+ _clip: *mut RECT,+ data: LPARAM,+) -> BOOL {+ let monitors = data.0 as *mut Vec<MonitorHandle>;+ unsafe { (*monitors).push(handle) };+ true.into()+}++/// Converts a monitor handle to an instance of `NativeMonitor`.+fn handle_to_monitor(+ handle: MonitorHandle,+) -> anyhow::Result<NativeMonitor> {+ let mut monitor_info = MONITORINFOEXW::default();+ monitor_info.monitorInfo.cbSize =+ std::mem::size_of::<MONITORINFOEXW>() as u32;++ unsafe {+ GetMonitorInfoW(handle, &mut monitor_info as *mut _ as *mut _)+ }+ .ok()?;++ let device_name = String::from_utf16_lossy(&monitor_info.szDevice);++ Ok(NativeMonitor::new(+ handle,+ device_name,+ monitor_info.monitorInfo.rcMonitor.right+ - monitor_info.monitorInfo.rcMonitor.left,+ monitor_info.monitorInfo.rcMonitor.bottom+ - monitor_info.monitorInfo.rcMonitor.top,+ monitor_info.monitorInfo.rcMonitor.left,+ monitor_info.monitorInfo.rcMonitor.top,+ ))+}diff --git a/packages/wm/src/common/platform/native_window.rs b/packages/wm/src/common/platform/native_window.rsindex 868de830..6fac2ea2 100644--- a/packages/wm/src/common/platform/native_window.rs+++ b/packages/wm/src/common/platform/native_window.rs@@ -1,10 +1,10 @@-use std::cell::RefCell;+use std::sync::{Arc, RwLock};use anyhow::Result;use windows::{core::PWSTR,Win32::{- Foundation::CloseHandle,+ Foundation::{CloseHandle, HWND},System::Threading::{OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32,PROCESS_QUERY_INFORMATION,@@ -15,46 +15,49 @@ use windows::{},};-use super::WindowHandle;+pub type WindowHandle = HWND;#[derive(Debug)]pub struct NativeWindow {pub handle: WindowHandle,- title: Option<RefCell<String>>,- process_name: Option<RefCell<String>>,- class_name: Option<RefCell<String>>,+ title: Arc<RwLock<Option<String>>>,+ process_name: Arc<RwLock<Option<String>>>,+ class_name: Arc<RwLock<Option<String>>>,}impl NativeWindow {pub fn new(handle: WindowHandle) -> Self {Self {handle,- title: None,- process_name: None,- class_name: None,+ title: Arc::new(RwLock::new(None)),+ process_name: Arc::new(RwLock::new(None)),+ class_name: Arc::new(RwLock::new(None)),}}- /// Gets the window title.+ /// Gets the window's title.////// If the window is invalid, returns an empty string.- fn title(&self) -> String {- match self.title {- Some(title) => title.borrow().clone(),+ pub fn title(&self) -> String {+ let title_guard = self.title.read().unwrap();+ match *title_guard {+ Some(ref title) => title.clone(),None => {let mut text: [u16; 512] = [0; 512];let length = unsafe { GetWindowTextW(self.handle, &mut text) };let title = String::from_utf16_lossy(&text[..length as usize]);- self.title = Some(RefCell::new(title));+ *self.title.write().unwrap() = Some(title.clone());title}}}- fn process_name(&self) -> Result<String> {- match self.process_name {- Some(process_name) => Ok(process_name.borrow().clone()),+ /// Gets the process name associated with the window.+ pub fn process_name(&self) -> Result<String> {+ let process_name_guard = self.process_name.read().unwrap();+ match *process_name_guard {+ Some(ref process_name) => Ok(process_name.clone()),None => {let mut process_id = 0u32;unsafe {@@ -79,15 +82,17 @@ impl NativeWindow {};let process_name = String::from_utf16(&buffer[..length as usize])?;- self.process_name = Some(RefCell::new(process_name));+ *self.process_name.write().unwrap() = Some(process_name.clone());Ok(process_name)}}}- fn class_name(&self) -> Result<String> {- match self.class_name {- Some(class_name) => Ok(class_name.borrow().clone()),+ /// Gets the class name of the window.+ pub fn class_name(&self) -> Result<String> {+ let class_name_guard = self.class_name.read().unwrap();+ match *class_name_guard {+ Some(ref class_name) => Ok(class_name.clone()),None => {let mut buffer = [0u16; 256];let result = unsafe { GetClassNameW(self.handle, &mut buffer) };@@ -98,8 +103,7 @@ impl NativeWindow {let class_name =String::from_utf16_lossy(&buffer[..result as usize]);- println!("Class name: {}", class_name);- self.class_name = Some(RefCell::new(class_name));+ *self.class_name.write().unwrap() = Some(class_name.clone());Ok(class_name)}}diff --git a/packages/wm/src/common/platform/platform.rs b/packages/wm/src/common/platform/platform.rsindex 7b067e34..0d04fed5 100644--- a/packages/wm/src/common/platform/platform.rs+++ b/packages/wm/src/common/platform/platform.rs@@ -2,19 +2,16 @@ use std::sync::Arc;use anyhow::Result;use tokio::sync::{mpsc::UnboundedReceiver, Mutex};-use windows::Win32::Foundation::HWND;use crate::user_config::UserConfig;-use super::{EventListener, NativeMonitor, NativeWindow};--pub type WindowHandle = HWND;+use super::{native_monitor, EventListener, NativeMonitor, NativeWindow};pub struct Platform;impl Platform {- pub fn monitors() -> Result<Vec<NativeMonitor>> {- todo!()+ pub fn monitors() -> Vec<NativeMonitor> {+ native_monitor::available_monitors()}pub fn manageable_windows() -> Result<Vec<NativeWindow>> {diff --git a/packages/wm/src/main.rs b/packages/wm/src/main.rsindex e70cc47f..e9cb544f 100644--- a/packages/wm/src/main.rs+++ b/packages/wm/src/main.rs@@ -68,6 +68,9 @@ async fn start_wm(config_path: Option<String>) -> Result<()> {let mut event_listener =Platform::new_event_listener(config, config_changes_rx).await?;+ let monitors = Platform::monitors();+ info!("Available monitors: {:?}", monitors);+loop {let wm_state = wm.state.clone();diff --git a/packages/wm/src/monitors/monitor.rs b/packages/wm/src/monitors/monitor.rsindex b86700a0..ef287ee6 100644--- a/packages/wm/src/monitors/monitor.rs+++ b/packages/wm/src/monitors/monitor.rs@@ -16,10 +16,10 @@ pub struct Monitor {pub parent: Option<ContainerRef>,pub children: Vec<ContainerRef>,pub device_name: String,- pub width: u32,- pub height: u32,- pub x: u32,- pub y: u32,+ pub width: i32,+ pub height: i32,+ pub x: i32,+ pub y: i32,}impl MonitorRef {diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rsindex 371a7876..81fc4056 100644--- a/packages/wm/src/wm_state.rs+++ b/packages/wm/src/wm_state.rs@@ -62,8 +62,14 @@ impl WmState {}pub fn add_monitor(&mut self) {- let monitor =- MonitorRef::new(NativeMonitor::new(String::from("aaa"), 0, 0, 0, 0));+ let monitor = MonitorRef::new(NativeMonitor::new(+ windows::Win32::Graphics::Gdi::HMONITOR(1),+ String::from("aaa"),+ 0,+ 0,+ 0,+ 0,+ ));self.root_container