Skip to content

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

fix(domain): create a message loop within WindowEventService

Lars68ea3a4a4e7a
diff --git a/LarsWM.Infrastructure/WindowsApi/WindowEventService.cs b/LarsWM.Infrastructure/WindowsApi/WindowEventService.cs
index 92cc9197..bb267254 100644
--- a/LarsWM.Infrastructure/WindowsApi/WindowEventService.cs
+++ b/LarsWM.Infrastructure/WindowsApi/WindowEventService.cs
@@ -1,14 +1,20 @@
using LarsWM.Infrastructure.WindowsApi.Enums;
using System;
using System.Reactive.Subjects;
+using System.Threading;
+using System.Windows.Forms;
using static LarsWM.Infrastructure.WindowsApi.WindowsApiService;
namespace LarsWM.Infrastructure.WindowsApi
{
- class WindowEventService
+ public class WindowEventService
{
+ // TODO: Change from Subject to Observable.
+ // TODO: Create the Observable in constructor instead of `Init` method.
public Subject<WindowHookEvent> WindowHookSubject = new Subject<WindowHookEvent>();
+ public static int CHILDID_SELF = 0;
+
public struct WindowHookEvent
{
public EventConstant EventType { get; }
@@ -21,24 +27,31 @@ namespace LarsWM.Infrastructure.WindowsApi
}
}
- public void Initialise()
+ public void Init()
{
var callback = new WindowEventProc((IntPtr hWinEventHook, EventConstant eventType, IntPtr hwnd, ObjectIdentifier idObject, int idChild, uint dwEventThread, uint dwmsEventTime) =>
{
- var CHILDID_SELF = 0;
- var validEvent = idChild == CHILDID_SELF && idObject == ObjectIdentifier.OBJID_WINDOW && hwnd != IntPtr.Zero;
+ var isWindowEvent = idChild == CHILDID_SELF && idObject == ObjectIdentifier.OBJID_WINDOW && hwnd != IntPtr.Zero;
- if (!validEvent)
+ if (!isWindowEvent)
return;
var windowHookEvent = new WindowHookEvent(eventType, hwnd);
WindowHookSubject.OnNext(windowHookEvent);
});
- SetWinEventHook(EventConstant.EVENT_OBJECT_DESTROY, EventConstant.EVENT_OBJECT_SHOW, IntPtr.Zero, callback, 0, 0, 0);
- SetWinEventHook(EventConstant.EVENT_OBJECT_CLOAKED, EventConstant.EVENT_OBJECT_UNCLOAKED, IntPtr.Zero, callback, 0, 0, 0);
- SetWinEventHook(EventConstant.EVENT_SYSTEM_MINIMIZESTART, EventConstant.EVENT_SYSTEM_MINIMIZEEND, IntPtr.Zero, callback, 0, 0, 0);
- SetWinEventHook(EventConstant.EVENT_SYSTEM_FOREGROUND, EventConstant.EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, callback, 0, 0, 0);
+ // SetWinEventHook requires a message loop within the thread that is executing the code.
+ var thread = new Thread(() =>
+ {
+ SetWinEventHook(EventConstant.EVENT_OBJECT_DESTROY, EventConstant.EVENT_OBJECT_SHOW, IntPtr.Zero, callback, 0, 0, 0);
+ SetWinEventHook(EventConstant.EVENT_OBJECT_CLOAKED, EventConstant.EVENT_OBJECT_UNCLOAKED, IntPtr.Zero, callback, 0, 0, 0);
+ SetWinEventHook(EventConstant.EVENT_SYSTEM_MINIMIZESTART, EventConstant.EVENT_SYSTEM_MINIMIZEEND, IntPtr.Zero, callback, 0, 0, 0);
+ SetWinEventHook(EventConstant.EVENT_SYSTEM_FOREGROUND, EventConstant.EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, callback, 0, 0, 0);
+ Application.Run();
+ });
+ thread.Name = "LarsWMWindowHooks";
+ thread.Start();
+
}
}
}