Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinAskestad committed Jul 8, 2023
1 parent db689ec commit ec4e386
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 105 deletions.
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::env;
use copy_to_output::copy_to_output;
use std::env;

fn main() {
copy_to_output("default.yaml", &env::var("PROFILE").unwrap()).expect("Could not copy");
Expand Down
32 changes: 18 additions & 14 deletions src/appwindow.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{ffi::c_void, sync::OnceLock};

use log::{error, info};
use log::{debug, error, info};
use windows::{
w,
Win32::{
Expand All @@ -12,7 +12,8 @@ use windows::{
CreateWindowExW, DeregisterShellHookWindow, CHILDID_SELF, CREATESTRUCTA,
CW_USEDEFAULT, EVENT_OBJECT_CLOAKED, EVENT_OBJECT_UNCLOAKED,
EVENT_SYSTEM_MINIMIZEEND, EVENT_SYSTEM_MINIMIZESTART, GWLP_USERDATA, OBJID_WINDOW,
WINDOW_EX_STYLE, WM_CREATE, WM_DESTROY, WNDCLASSW, WS_OVERLAPPEDWINDOW,
WINDOW_EX_STYLE, WM_APP, WM_CREATE, WM_DESTROY, WM_USER, WNDCLASSW,
WS_OVERLAPPEDWINDOW,
},
},
},
Expand All @@ -22,7 +23,9 @@ use grout_wm::Result;

use crate::{
win32,
windowmanager::{WindowManager, WM_CLOAKED, WM_MINIMIZEEND, WM_MINIMIZESTART, WM_UNCLOAKED},
windowmanager::{
WindowManager, MSG_CLOAKED, MSG_MINIMIZEEND, MSG_MINIMIZESTART, MSG_UNCLOAKED,
},
};

static MY_HWND: OnceLock<HWND> = OnceLock::new();
Expand Down Expand Up @@ -139,16 +142,17 @@ impl AppWindow {
return;
}
if let Some(&my_hwnd) = MY_HWND.get() {
if event == EVENT_SYSTEM_MINIMIZEEND {
win32::post_message(my_hwnd, WM_MINIMIZEEND, WPARAM(0), LPARAM(hwnd.0));
}
if event == EVENT_SYSTEM_MINIMIZESTART {
win32::post_message(my_hwnd, WM_MINIMIZESTART, WPARAM(0), LPARAM(hwnd.0));
}
if event == EVENT_OBJECT_UNCLOAKED {
win32::post_message(my_hwnd, WM_UNCLOAKED, WPARAM(0), LPARAM(hwnd.0));
} else if event == EVENT_OBJECT_CLOAKED {
win32::post_message(my_hwnd, WM_CLOAKED, WPARAM(0), LPARAM(hwnd.0));
debug!("event: {event}");
let msg = match event {
EVENT_OBJECT_CLOAKED => MSG_CLOAKED,
EVENT_OBJECT_UNCLOAKED => MSG_UNCLOAKED,
EVENT_SYSTEM_MINIMIZEEND => MSG_MINIMIZEEND,
EVENT_SYSTEM_MINIMIZESTART => MSG_MINIMIZESTART,
_ => event,
};
debug!("msg: {msg}");
if msg >= WM_USER || msg < WM_APP {
win32::post_message(my_hwnd, msg, WPARAM(0), LPARAM(hwnd.0));
}
}
}
Expand All @@ -160,7 +164,7 @@ impl AppWindow {
win32::post_quit_message(0);
LRESULT(0)
}
WM_CREATE=> {
WM_CREATE => {
info!("Creating application window");
let create_struct = lparam.0 as *const CREATESTRUCTA;
let wm = unsafe { (*create_struct).lpCreateParams as *mut WindowManager };
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::env;
use serde::{Deserialize, Serialize};
use log::info;
use serde::{Deserialize, Serialize};
use std::env;

#[derive(Deserialize, Serialize)]
pub struct Config {
Expand Down
22 changes: 20 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
#[macro_export]
macro_rules! any {
($xs:expr, $x:expr) => {
$xs.iter().any(|&x| x.0 == $x)
};
}

#[macro_export]
macro_rules! has_flag {
($value:expr, $flag:expr) => {
($value & $flag) != 0
};
}

pub struct Error {
pub(crate) message: String,
}
Expand All @@ -19,13 +33,17 @@ impl std::fmt::Display for Error {

impl std::convert::From<&str> for Error {
fn from(err: &str) -> Self {
Error { message: String::from(err) }
Error {
message: String::from(err),
}
}
}

impl std::convert::From<windows::core::Error> for Error {
fn from(err: windows::core::Error) -> Self {
Error { message: err.to_string() }
Error {
message: err.to_string(),
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ mod window;
mod windowmanager;

use crate::appwindow::AppWindow;
use crate::windowmanager::WindowManager;
use crate::config::Config;
use crate::windowmanager::WindowManager;

fn main() -> Result<()> {
let mutex_handle = win32::get_mutex().unwrap_or_else(|_e|{
let mutex_handle = win32::get_mutex().unwrap_or_else(|_e| {
error!("Can't run multiple instances");
std::process::exit(1);
});
Expand Down
40 changes: 22 additions & 18 deletions src/win32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,29 @@ use windows::{
DWM_CLOAKED_SHELL,
},
System::{
Com::{CoCreateInstance, CoInitialize, CoUninitialize, CLSCTX_ALL},
LibraryLoader::GetModuleHandleA,
ProcessStatus::{
EnumProcessModules, GetModuleBaseNameW, GetModuleInformation, MODULEINFO,
},
Threading::{
CreateMutexW, OpenProcess, ReleaseMutex, PROCESS_QUERY_INFORMATION, PROCESS_VM_READ,
}, Com::{CoUninitialize, CoInitialize, CLSCTX_ALL, CoCreateInstance},
},
},
UI::{
Accessibility::{SetWinEventHook, HWINEVENTHOOK, WINEVENTPROC},
Shell::{IVirtualDesktopManager, VirtualDesktopManager as VirtualDesktopManager_ID},
WindowsAndMessaging::{
DefWindowProcW, EnumWindows, FindWindowW, GetClassNameW, GetParent,
GetSystemMetrics, GetWindow, GetWindowLongPtrW, GetWindowTextLengthW,
GetWindowTextW, GetWindowThreadProcessId, IsIconic, IsWindowVisible, PostMessageW,
PostQuitMessage, RegisterClassW, RegisterShellHookWindow, RegisterWindowMessageW,
SetWindowLongPtrW, SetWindowPos, ShowWindow, SystemParametersInfoW, GET_WINDOW_CMD,
GWL_EXSTYLE, GWL_STYLE, HWND_TOP, SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN,
SM_XVIRTUALSCREEN, SM_YVIRTUALSCREEN, SPI_GETWORKAREA, SWP_NOACTIVATE,
SW_SHOWMINNOACTIVE, SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, WINDOW_LONG_PTR_INDEX,
WINEVENT_OUTOFCONTEXT, WNDCLASSW, WNDENUMPROC,
}, Shell::{IVirtualDesktopManager, VirtualDesktopManager as VirtualDesktopManager_ID},
DefWindowProcW, EnumWindows, FindWindowW, GetClassNameW, GetSystemMetrics,
GetWindow, GetWindowLongPtrW, GetWindowTextLengthW, GetWindowTextW,
GetWindowThreadProcessId, IsIconic, IsWindowVisible, PostMessageW, PostQuitMessage,
RegisterClassW, RegisterShellHookWindow, RegisterWindowMessageW, SetWindowLongPtrW,
SetWindowPos, ShowWindow, SystemParametersInfoW, GET_WINDOW_CMD, GWL_EXSTYLE,
GWL_STYLE, HWND_TOP, SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN, SM_XVIRTUALSCREEN,
SM_YVIRTUALSCREEN, SPI_GETWORKAREA, SWP_NOACTIVATE, SW_SHOWMINNOACTIVE,
SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, WINDOW_LONG_PTR_INDEX, WINEVENT_OUTOFCONTEXT,
WNDCLASSW, WNDENUMPROC,
},
},
},
};
Expand All @@ -49,15 +51,19 @@ pub struct Win32Com;
impl Win32Com {
pub fn new() -> Result<Self> {
info!("Initialize COM");
unsafe { CoInitialize(None)?; }
unsafe {
CoInitialize(None)?;
}
Ok(Win32Com)
}
}

impl Drop for Win32Com {
fn drop(&mut self) {
info!("Uninitializing COM");
unsafe { CoUninitialize(); }
unsafe {
CoUninitialize();
}
}
}
pub fn is_cloaked(hwnd: HWND) -> bool {
Expand All @@ -83,10 +89,6 @@ pub fn is_iconic(hwnd: HWND) -> bool {
unsafe { IsIconic(hwnd).into() }
}

pub fn get_parent(hwnd: HWND) -> HWND {
unsafe { GetParent(hwnd) }
}

pub fn get_window_long_ptr(hwnd: HWND, nindex: WINDOW_LONG_PTR_INDEX) -> isize {
unsafe { GetWindowLongPtrW(hwnd, nindex) }
}
Expand Down Expand Up @@ -284,7 +286,9 @@ pub struct VirtualDesktopManager(IVirtualDesktopManager);
impl VirtualDesktopManager {
pub fn new() -> Result<Self> {
info!("Instanciate VirtualDesktopManager");
unsafe { CoInitialize(None)?; }
unsafe {
CoInitialize(None)?;
}
let virtual_desktop_managr =
unsafe { CoCreateInstance(&VirtualDesktopManager_ID, None, CLSCTX_ALL)? };
Ok(Self(virtual_desktop_managr))
Expand Down
51 changes: 33 additions & 18 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,48 @@ use windows::Win32::Foundation::HWND;
use crate::win32;

#[derive(Clone, Copy)]
pub struct Window {
pub hwnd: HWND,
pub minimized: bool,
pub selected: bool,
}
pub struct Window(pub HWND);

impl Window {
pub fn new(hwnd: HWND) -> Self {
Window {
hwnd,
minimized: Default::default(),
selected: Default::default(),
}
Window(hwnd)
}

pub fn is_iconic(&self) -> bool {
win32::is_iconic(self.0)
}

pub fn title(&self) -> String {
win32::get_window_text(self.0)
}

pub fn class_name(&self) -> String {
win32::get_window_classname(self.0)
}

pub fn exstyle(&self) -> u32 {
win32::get_window_exstyle(self.0)
}

pub fn style(&self) -> u32 {
win32::get_window_style(self.0)
}

pub fn process_name(&self) -> String {
win32::get_exe_filename(self.0).unwrap_or("".to_owned())
}
}

impl fmt::Debug for Window {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Window")
.field("hwnd", &self.hwnd)
.field("title", &win32::get_window_text(self.hwnd))
.field("class", &win32::get_window_classname(self.hwnd))
.field("minimized", &self.minimized)
.field("parent", &win32::get_parent(self.hwnd))
.field("ex_style", &win32::get_window_exstyle(self.hwnd))
.field("style", &win32::get_window_style(self.hwnd))
.field("process", &win32::get_exe_filename(self.hwnd))
.field("hwnd", &self.0)
.field("title", &self.title())
.field("class", &self.class_name())
.field("minimized", &self.is_iconic())
.field("ex_style", &self.exstyle())
.field("style", &self.style())
.field("process", &self.process_name())
.finish()
}
}
Loading

0 comments on commit ec4e386

Please sign in to comment.