Skip to content

Commit

Permalink
MonitorManager locks
Browse files Browse the repository at this point in the history
  • Loading branch information
dalyIsaac committed Mar 21, 2024
1 parent 26e9990 commit 975f23e
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions src/Whim/Monitor/MonitorManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Whim;
/// </summary>
internal class MonitorManager : IInternalMonitorManager, IMonitorManager
{
private readonly object _lockObj = new();
private readonly IContext _context;
private readonly IInternalContext _internalContext;

Expand Down Expand Up @@ -74,6 +75,7 @@ public void Initialize()

public void OnWindowFocused(IWindow? window)
{
using Lock _ = new(_lockObj);
// If we know the window, use what the Butler knows instead of Windows.
if (window is not null)
{
Expand Down Expand Up @@ -119,6 +121,7 @@ public void OnWindowFocused(IWindow? window)

public void ActivateEmptyMonitor(IMonitor monitor)
{
using Lock _ = new(_lockObj);
Logger.Debug($"Activating empty monitor {monitor}");

if (!_monitors.Contains(monitor))
Expand All @@ -144,16 +147,46 @@ private void WindowMessageMonitor_SessionChanged(object? sender, WindowMessageMo

private void WindowMessageMonitor_MonitorsChanged(object? sender, WindowMessageMonitorEventArgs e)
{
OnMonitorsChanged(
e,
out List<IMonitor> unchangedMonitors,
out List<IMonitor> removedMonitors,
out List<IMonitor> addedMonitors
);

// Notify listeners of the unchanged, removed, and added monitors.
MonitorsChangedEventArgs args =
new()
{
UnchangedMonitors = unchangedMonitors,
RemovedMonitors = removedMonitors,
AddedMonitors = addedMonitors
};

if (addedMonitors.Count != 0 || removedMonitors.Count != 0)
{
_internalContext.ButlerEventHandlers.OnMonitorsChanged(args);
}
MonitorsChanged?.Invoke(this, args);
}

private void OnMonitorsChanged(
WindowMessageMonitorEventArgs e,
out List<IMonitor> unchangedMonitors,
out List<IMonitor> removedMonitors,
out List<IMonitor> addedMonitors
)
{
using Lock _ = new(_lockObj);
Logger.Debug($"Monitors changed: {e.MessagePayload}");

// Get the new monitors.
IMonitor[] previousMonitors = _monitors;
_monitors = GetCurrentMonitors();

List<IMonitor> unchangedMonitors = new();
List<IMonitor> removedMonitors = new();
List<IMonitor> addedMonitors = new();

unchangedMonitors = new();
removedMonitors = new();
addedMonitors = new();
HashSet<IMonitor> previousMonitorsSet = new(previousMonitors);
HashSet<IMonitor> currentMonitorsSet = new(_monitors);

Expand Down Expand Up @@ -183,21 +216,6 @@ private void WindowMessageMonitor_MonitorsChanged(object? sender, WindowMessageM
PrimaryMonitor = monitor;
}
}

// Notify listeners of the unchanged, removed, and added monitors.
MonitorsChangedEventArgs args =
new()
{
UnchangedMonitors = unchangedMonitors,
RemovedMonitors = removedMonitors,
AddedMonitors = addedMonitors
};

if (addedMonitors.Count != 0 || removedMonitors.Count != 0)
{
_internalContext.ButlerEventHandlers.OnMonitorsChanged(args);
}
MonitorsChanged?.Invoke(this, args);
}

private void MouseHook_MouseLeftButtonUp(object? sender, MouseEventArgs e)
Expand Down Expand Up @@ -267,6 +285,7 @@ private unsafe Monitor[] GetCurrentMonitors()

public IMonitor GetMonitorAtPoint(IPoint<int> point)
{
using Lock _ = new(_lockObj);
Logger.Debug($"Getting monitor at point {point}");
HMONITOR hmonitor = _internalContext.CoreNativeManager.MonitorFromPoint(
point.ToSystemPoint(),
Expand All @@ -285,6 +304,7 @@ public IMonitor GetMonitorAtPoint(IPoint<int> point)

public IMonitor GetPreviousMonitor(IMonitor monitor)
{
using Lock _ = new(_lockObj);
Logger.Debug($"Getting previous monitor for {monitor}");

int index = Array.IndexOf(_monitors, monitor);
Expand All @@ -299,6 +319,7 @@ public IMonitor GetPreviousMonitor(IMonitor monitor)

public IMonitor GetNextMonitor(IMonitor monitor)
{
using Lock _ = new(_lockObj);
Logger.Debug($"Getting next monitor for {monitor}");

int index = Array.IndexOf(_monitors, monitor);
Expand Down

0 comments on commit 975f23e

Please sign in to comment.