Skip to content

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

feat(domain,infra): move `KeybindingService` to infrastructure project; avoid calling commands directly

Lars Berger28e21af0c21f
diff --git a/LarsWM.Bootstrapper/Startup.cs b/LarsWM.Bootstrapper/Startup.cs
index 82b8a1d1..0039e98d 100644
--- a/LarsWM.Bootstrapper/Startup.cs
+++ b/LarsWM.Bootstrapper/Startup.cs
@@ -1,5 +1,4 @@
using LarsWM.Bar;
-using LarsWM.Domain.Common.Services;
using LarsWM.Domain.Monitors;
using LarsWM.Domain.Monitors.Commands;
using LarsWM.Domain.UserConfigs.Commands;
@@ -46,14 +45,16 @@ namespace LarsWM.Bootstrapper
{
// Launch bar WPF application. Spawns bar window when monitors are added, so the service needs
// to be initialized before populating initial state.
+ // TODO: Rename `Init` method to `Start`.
_barManagerService.Init();
// Populate initial monitors, windows, workspaces and user config.
PopulateInitialState();
- _keybindingService.Init();
+ _keybindingService.Start();
+ // TODO: Rename `Init` method to `Start`.
_windowEventService.Init();
- // TODO: Rename `Configure` method to `Init`.
+ // TODO: Rename `Configure` method to `Start`.
_windowHooksHandler.Configure();
}
diff --git a/LarsWM.Domain/Common/Services/KeybindingService.cs b/LarsWM.Domain/Common/Services/KeybindingService.cs
deleted file mode 100644
index 85926898..00000000
--- a/LarsWM.Domain/Common/Services/KeybindingService.cs
+++ /dev/null
@@ -1,135 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Linq;
-using System.Reactive.Subjects;
-using System.Runtime.InteropServices;
-using System.Threading;
-using System.Windows.Forms;
-using LarsWM.Domain.Common.Enums;
-using LarsWM.Domain.Containers;
-using LarsWM.Domain.Containers.Commands;
-using LarsWM.Domain.Windows;
-using LarsWM.Domain.Windows.Commands;
-using LarsWM.Domain.Workspaces.Commands;
-using LarsWM.Infrastructure.Bussing;
-using static LarsWM.Infrastructure.WindowsApi.WindowsApiService;
-
-namespace LarsWM.Domain.Common.Services
-{
- public class KeybindingService
- {
- private Subject<Keys> _modKeypresses = new Subject<Keys>();
- public static readonly uint WM_KEYDOWN = 0x100;
- public static readonly uint WM_SYSKEYDOWN = 0x104;
-
- private Bus _bus;
- private ContainerService _containerService;
-
- public KeybindingService(Bus bus, ContainerService containerService)
- {
- _bus = bus;
- _containerService = containerService;
- }
-
- public void Init()
- {
- // SetWindowsHookEx requires a message loop within the thread that is executing the code.
- var thread = new Thread(() =>
- {
- SetWindowsHookEx(HookType.WH_KEYBOARD_LL, KbHookProc, Process.GetCurrentProcess().MainModule.BaseAddress, 0);
- Application.Run();
- });
- thread.Name = "LarsWMKeybindingService";
- thread.Start();
-
- HandleKeybindings();
- }
-
-
- private IntPtr KbHookProc(int nCode, IntPtr wParam, IntPtr lParam)
- {
- // If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.
- // CallNextHookEx passes hook notification to other applications.
- // TODO: Flatten this if-statement.
- if (nCode == 0 && ((uint)wParam == WM_KEYDOWN || (uint)wParam == WM_SYSKEYDOWN))
- {
- var modifiersPressed = new List<Keys>();
-
- if ((GetKeyState(Keys.LMenu) & 0x8000) == 0x8000)
- modifiersPressed.Add(Keys.LMenu);
-
- if (modifiersPressed.Count() == 0)
- return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
-
- // Get struct with details about keyboard input event.
- var rawEventStruct = Marshal.PtrToStructure(lParam, typeof(LowLevelKeyboardInputEvent));
- var inputEvent = (LowLevelKeyboardInputEvent)rawEventStruct;
-
- _modKeypresses.OnNext(inputEvent.Key);
-
- // Avoid forwarding the key input to other applications.
- return new IntPtr(1);
- }
-
- return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
- }
-
- private void HandleKeybindings()
- {
- _modKeypresses.Subscribe(pressedKey =>
- {
- if (pressedKey == Keys.J)
- Debug.WriteLine("Alt+J keybinding successfully triggered.");
- if (pressedKey == Keys.D1)
- _bus.Invoke(new FocusWorkspaceCommand("1"));
- if (pressedKey == Keys.D2)
- _bus.Invoke(new FocusWorkspaceCommand("2"));
- if (pressedKey == Keys.D3)
- _bus.Invoke(new FocusWorkspaceCommand("3"));
- if (pressedKey == Keys.D4)
- _bus.Invoke(new FocusWorkspaceCommand("4"));
- if (pressedKey == Keys.D5)
- _bus.Invoke(new FocusWorkspaceCommand("5"));
- if (pressedKey == Keys.D6)
- _bus.Invoke(new FocusWorkspaceCommand("6"));
- if (pressedKey == Keys.D7)
- _bus.Invoke(new FocusWorkspaceCommand("7"));
- if (pressedKey == Keys.D8)
- _bus.Invoke(new FocusWorkspaceCommand("8"));
- if (pressedKey == Keys.D9)
- _bus.Invoke(new FocusWorkspaceCommand("9"));
- if (pressedKey == Keys.Q)
- {
- var focusedWindow = _containerService.FocusedContainer as Window;
-
- if (focusedWindow == null)
- return;
-
- SendMessage(focusedWindow.Hwnd, SendMessageType.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
- }
- if (pressedKey == Keys.A)
- _bus.Invoke(new MoveFocusedWindowCommand(Direction.LEFT));
- if (pressedKey == Keys.S)
- _bus.Invoke(new MoveFocusedWindowCommand(Direction.DOWN));
- if (pressedKey == Keys.W)
- _bus.Invoke(new MoveFocusedWindowCommand(Direction.UP));
- if (pressedKey == Keys.D)
- _bus.Invoke(new MoveFocusedWindowCommand(Direction.RIGHT));
- if (pressedKey == Keys.U)
- _bus.Invoke(new ResizeFocusedWindowCommand(ResizeDirection.SHRINK_WIDTH));
- if (pressedKey == Keys.I)
- _bus.Invoke(new ResizeFocusedWindowCommand(ResizeDirection.SHRINK_HEIGHT));
- if (pressedKey == Keys.O)
- _bus.Invoke(new ResizeFocusedWindowCommand(ResizeDirection.GROW_HEIGHT));
- if (pressedKey == Keys.P)
- _bus.Invoke(new ResizeFocusedWindowCommand(ResizeDirection.GROW_WIDTH));
- if (pressedKey == Keys.V)
- _bus.Invoke(new ChangeContainerLayoutCommand(Layout.VERTICAL));
- if (pressedKey == Keys.B)
- _bus.Invoke(new ChangeContainerLayoutCommand(Layout.HORIZONTAL));
- }
- );
- }
- }
-}
diff --git a/LarsWM.Domain/DependencyInjection.cs b/LarsWM.Domain/DependencyInjection.cs
index b4a1e16c..f697a8b2 100644
--- a/LarsWM.Domain/DependencyInjection.cs
+++ b/LarsWM.Domain/DependencyInjection.cs
@@ -1,4 +1,3 @@
-using LarsWM.Domain.Common.Services;
using LarsWM.Domain.Containers;
using LarsWM.Domain.Containers.CommandHandlers;
using LarsWM.Domain.Monitors;
@@ -20,7 +19,6 @@ namespace LarsWM.Domain
{
public static IServiceCollection AddDomainServices(this IServiceCollection services)
{
- services.AddSingleton<KeybindingService>();
services.AddSingleton<ContainerService>();
services.AddSingleton<MonitorService>();
services.AddSingleton<UserConfigService>();
diff --git a/LarsWM.Infrastructure/DependencyInjection.cs b/LarsWM.Infrastructure/DependencyInjection.cs
index 32818c2a..286d9b16 100644
--- a/LarsWM.Infrastructure/DependencyInjection.cs
+++ b/LarsWM.Infrastructure/DependencyInjection.cs
@@ -10,6 +10,7 @@ namespace LarsWM.Infrastructure
{
services.AddSingleton<Bus, Bus>();
services.AddSingleton<WindowEventService>();
+ services.AddSingleton<KeybindingService>();
// TODO: Change WindowsApiFacade & WindowsApiService to be compatible with DI.
diff --git a/LarsWM.Infrastructure/WindowsApi/KeybindingService.cs b/LarsWM.Infrastructure/WindowsApi/KeybindingService.cs
new file mode 100644
index 00000000..278bdc3e
--- /dev/null
+++ b/LarsWM.Infrastructure/WindowsApi/KeybindingService.cs
@@ -0,0 +1,80 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Reactive.Subjects;
+using System.Runtime.InteropServices;
+using System.Threading;
+using System.Windows.Forms;
+using LarsWM.Infrastructure.Bussing;
+using static LarsWM.Infrastructure.WindowsApi.WindowsApiService;
+
+namespace LarsWM.Infrastructure.WindowsApi
+{
+ public class KeybindingService
+ {
+ private Subject<Keys> _modKeypresses = new Subject<Keys>();
+ public static readonly uint WM_KEYDOWN = 0x100;
+ public static readonly uint WM_SYSKEYDOWN = 0x104;
+
+ private Bus _bus;
+
+ public KeybindingService(Bus bus)
+ {
+ _bus = bus;
+ }
+
+ public void Start()
+ {
+ var thread = new Thread(() => CreateKeybindingHook());
+ thread.Name = "LarsWMKeybindingService";
+ thread.Start();
+ }
+
+ public void SetModKey(string modKey)
+ {
+ throw new NotImplementedException();
+ }
+
+ public void AddGlobalKeybinding(string keyCombination, Action callback)
+ {
+ throw new NotImplementedException();
+ }
+
+ private void CreateKeybindingHook()
+ {
+ SetWindowsHookEx(HookType.WH_KEYBOARD_LL, KeybindingHookProc, Process.GetCurrentProcess().MainModule.BaseAddress, 0);
+
+ // `SetWindowsHookEx` requires a message loop within the thread that is executing the code.
+ Application.Run();
+ }
+
+ private IntPtr KeybindingHookProc(int nCode, IntPtr wParam, IntPtr lParam)
+ {
+ // If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.
+ // CallNextHookEx passes hook notification to other applications.
+ // TODO: Flatten this if-statement.
+ if (nCode == 0 && ((uint)wParam == WM_KEYDOWN || (uint)wParam == WM_SYSKEYDOWN))
+ {
+ var modifiersPressed = new List<Keys>();
+
+ if ((GetKeyState(Keys.LMenu) & 0x8000) == 0x8000)
+ modifiersPressed.Add(Keys.LMenu);
+
+ if (modifiersPressed.Count() == 0)
+ return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
+
+ // Get struct with details about keyboard input event.
+ var rawEventStruct = Marshal.PtrToStructure(lParam, typeof(LowLevelKeyboardInputEvent));
+ var inputEvent = (LowLevelKeyboardInputEvent)rawEventStruct;
+
+ _modKeypresses.OnNext(inputEvent.Key);
+
+ // Avoid forwarding the key input to other applications.
+ return new IntPtr(1);
+ }
+
+ return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
+ }
+ }
+}