Skip to content

Commit

Permalink
Fix engine won't restart after Display Changed
Browse files Browse the repository at this point in the history
  • Loading branch information
mgth committed May 19, 2024
1 parent 955e033 commit 57c0f52
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum LittleBigMouseEvent
Stopped,
Paused,
Dead,
SettingsChanged,
DisplayChanged,
DesktopChanged,
FocusChanged,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x64;x86</Platforms>
<Version>5.2.1.0</Version>
<Version>5.2.2.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
11 changes: 10 additions & 1 deletion LittleBigMouse.Hook/Daemon/LittleBigMouseDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void LittleBigMouseDaemon::Connect()
_hook->OnUnhooked.connect(this, &LittleBigMouseDaemon::Unhooked);

_hook->OnDisplayChanged.connect(this, &LittleBigMouseDaemon::DisplayChanged);
_hook->OnSettingChanged.connect(this, &LittleBigMouseDaemon::SettingChanged);
_hook->OnDesktopChanged.connect(this, &LittleBigMouseDaemon::DesktopChanged);
_hook->OnFocusChanged.connect(this, &LittleBigMouseDaemon::FocusChanged);
}
Expand Down Expand Up @@ -283,10 +284,18 @@ void LittleBigMouseDaemon::DisplayChanged() const
_remoteServer->Send("<DaemonMessage><Event>DisplayChanged</Event></DaemonMessage>\n",nullptr);
}

void LittleBigMouseDaemon::SettingChanged() const
{
//When display changed, we need to recompute zones, here we just stop the hook and inform ui to reload layout
if(_hook && _hook->Hooked())
_hook->Unhook();

_remoteServer->Send("<DaemonMessage><Event>SettingChanged</Event></DaemonMessage>\n",nullptr);
}

// Sytem switches to/from UAC desktop
void LittleBigMouseDaemon::DesktopChanged() const
{

_remoteServer->Send("<DaemonMessage><Event>DesktopChanged</Event></DaemonMessage>\n",nullptr);
}

Expand Down
1 change: 1 addition & 0 deletions LittleBigMouse.Hook/Daemon/LittleBigMouseDaemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class LittleBigMouseDaemon
void SendState(RemoteClient* client) const;

void DisplayChanged() const;
void SettingChanged() const;
void DesktopChanged() const;
[[nodiscard]] bool Excluded(const std::string& path) const;

Expand Down
30 changes: 0 additions & 30 deletions LittleBigMouse.Hook/Hook/Hooker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,33 +159,3 @@ void Hooker::Quit() const
LOG_TRACE("<Hook:BreakLoop>");
}
}


LRESULT __stdcall Hooker::DisplayChangedCallback(const int nCode, const WPARAM wParam, const LPARAM lParam)
{
LOG_TRACE("<hook:DisplayChanged2>");

const auto hook = Instance();
if (!hook) return CallNextHookEx(nullptr, nCode, wParam, lParam);

hook->OnDisplayChanged();

return CallNextHookEx(hook->_displayHookId, nCode, wParam, lParam);
}

LRESULT __stdcall Hooker::IniChangedCallback(const int nCode, const WPARAM wParam, const LPARAM lParam)
{
LOG_TRACE("<hook:IniChanged>");

const auto hook = Instance();
if (!hook) return CallNextHookEx(nullptr, nCode, wParam, lParam);

//if (nCode >= 0 && lParam != NULL && (wParam & WM_DISPLAYCHANGE) != 0)
//{
// hook && hook->OnIniChanged.fire();
//}

return CallNextHookEx(hook->_displayHookId, nCode, wParam, lParam);
}


4 changes: 1 addition & 3 deletions LittleBigMouse.Hook/Hook/Hooker.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class Hooker final
void HookMouse();
void UnhookMouse();


bool HookDisplayChange();
void UnhookDisplayChange();

Expand Down Expand Up @@ -62,6 +61,7 @@ class Hooker final
SIGNAL<void(MouseEventArg&)> OnMouseMove;
SIGNAL<void(const std::string&)> OnFocusChanged;
SIGNAL<void()> OnDisplayChanged;
SIGNAL<void()> OnSettingChanged;
SIGNAL<void()> OnDesktopChanged;

SIGNAL<void()> OnHooked;
Expand All @@ -85,8 +85,6 @@ class Hooker final

private:
static LRESULT __stdcall MouseCallback(const int nCode, const WPARAM wParam, const LPARAM lParam);
static LRESULT __stdcall DisplayChangedCallback(const int nCode, const WPARAM wParam, const LPARAM lParam);
static LRESULT __stdcall IniChangedCallback(const int nCode, const WPARAM wParam, const LPARAM lParam);
static LRESULT __stdcall WindowCallback(const int nCode, const WPARAM wParam, const LPARAM lParam);

static LRESULT CALLBACK DisplayChangeHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
Expand Down
2 changes: 1 addition & 1 deletion LittleBigMouse.Hook/Hook/HookerDisplayChanged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ LRESULT CALLBACK Hooker::DisplayChangeHandler(HWND hwnd, UINT msg, WPARAM wParam
if (wParam == SPI_SETWORKAREA)
{
LOG_TRACE("<HookerDispayChanged:SettingChange>");
hook->OnDisplayChanged();
hook->OnSettingChanged();
return 0;
}
default:
Expand Down
6 changes: 2 additions & 4 deletions LittleBigMouse.Hook/Hook/HookerWinEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void CALLBACK Hooker::WindowChangeHook(
{
static HWND lastHwnd = nullptr;

auto hook = Instance();
const auto hook = Instance();
if (!hook) return;

if (hWnd != lastHwnd) {
Expand Down Expand Up @@ -103,9 +103,7 @@ void CALLBACK Hooker::DesktopChangeHook(
DWORD event, HWND hWnd, LONG idObject,
LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
{
LOG_TRACE("desktop: ");

auto hook = Instance();
const auto hook = Instance();
if (!hook) return;

hook->OnDesktopChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ void StateChanged(object? sender, LittleBigMouseServiceEventArgs e)
Running = false;
});
break;
case LittleBigMouseEvent.SettingsChanged:
case LittleBigMouseEvent.DisplayChanged:
case LittleBigMouseEvent.DesktopChanged:
case LittleBigMouseEvent.FocusChanged:
Expand Down
19 changes: 16 additions & 3 deletions LittleBigMouse.Ui/LittleBigMouse.Ui.Avalonia/Main/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void UpdateLayout()

MonitorsLayout = _getNewMonitorLayout().UpdateFrom(monitors);

_mainWindow?.UpdateLayout();
//_mainWindow?.UpdateLayout();
}

Window _mainWindow = null!;
Expand Down Expand Up @@ -213,11 +213,12 @@ async Task DaemonEventReceived(object? sender, LittleBigMouseServiceEventArgs ar
await _notify.SetIconAsync("icon/lbm_paused",32);
break;

case LittleBigMouseEvent.SettingsChanged:
case LittleBigMouseEvent.DesktopChanged:
case LittleBigMouseEvent.DisplayChanged:
await DisplayChangedAsync();
break;

case LittleBigMouseEvent.DesktopChanged:
case LittleBigMouseEvent.FocusChanged:
AddSeenProcess(args.Payload);
break;
Expand All @@ -236,8 +237,20 @@ void AddSeenProcess(string process)
_processesCollector.SeenProcesses.Add(process);
}

private async Task DisplayChangedAsync()
bool _displayChangedTriggered = false;
readonly object _lockDisplayChanged = new();
async Task DisplayChangedAsync()
{
lock (_lockDisplayChanged)
{
if (_displayChangedTriggered) return;
_displayChangedTriggered = true;
}
await Task.Delay(5000);
lock (_lockDisplayChanged)
{
_displayChangedTriggered = false;
}
UpdateLayout();
if(MonitorsLayout.Options.Enabled)
{
Expand Down

0 comments on commit 57c0f52

Please sign in to comment.