Skip to content

Commit

Permalink
hook in main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
mgth committed May 6, 2024
1 parent b7e2cc4 commit 16e8a15
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 27 deletions.
4 changes: 2 additions & 2 deletions LittleBigMouse.Hook/Config.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#define LOGTOFILE 1
#define DEBUG_LOG 0
#define DEBUG_LEVEL 1
#define DEBUG_LOG 1
#define DEBUG_LEVEL 0

#define USE_NANOSIGNALSLOT 0
14 changes: 9 additions & 5 deletions LittleBigMouse.Hook/Daemon/LittleBigMouseDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ void LittleBigMouseDaemon::Connect()

void LittleBigMouseDaemon::Run(const std::string& path)
{
LOG_TRACE("Daemon started");
// connect to events
Connect();
LOG_TRACE("Connected");

//start remote server
if(_remoteServer)
Expand All @@ -96,14 +98,15 @@ void LittleBigMouseDaemon::Run(const std::string& path)
return;
}
}

// pump messages
if(_hook)
_hook->Start();
LOG_TRACE("RemoteServer");

// load layout from file
if(!path.empty())
LoadFromFile(path);

// pump messages
if(_hook)
_hook->RunThread();

// wait remote server to stop
if(_remoteServer)
Expand Down Expand Up @@ -318,7 +321,8 @@ void LittleBigMouseDaemon::FocusChanged(const std::string& path)
}
}

_remoteServer->Send("<DaemonMessage><Event>FocusChanged</Event><Payload>"+path+"</Payload></DaemonMessage>\n",nullptr);
if(_remoteServer)
_remoteServer->Send("<DaemonMessage><Event>FocusChanged</Event><Payload>"+path+"</Payload></DaemonMessage>\n",nullptr);
}

void LittleBigMouseDaemon::ReceiveClientMessage(const std::string& message, RemoteClient* client)
Expand Down
31 changes: 24 additions & 7 deletions LittleBigMouse.Hook/Hook/Hooker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ void Hooker::DoHook()

if(_hookMouse)
{
//HookMouse();
HookMouse();
}

// HookFocusEvent();
// HookEventSystemDesktopSwitch();
HookFocusEvent();
HookEventSystemDesktopSwitch();
HookDisplayChange();

}
Expand All @@ -70,14 +70,25 @@ int Hooker::Loop()
{
MSG msg;

while (GetMessage(&msg, nullptr, 0, 0))
LOG_TRACE("<Hook:Start>");


auto ret = GetMessage(&msg, nullptr, 0, 0);
while (ret)
{
// if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
TranslateMessage(&msg);
DispatchMessage(&msg);

if(ret == -1)
{
LOG_DEBUG("<Hook:Error>");
(int)msg.wParam;
}
ret = GetMessage(&msg, nullptr, 0, 0);
}

return (int) msg.wParam;
return (int)msg.wParam;
}


Expand Down Expand Up @@ -115,15 +126,21 @@ void Hooker::RunThread()

while(!Stopping())
{
//DoSetPriority(_priority); // TODO : Activate this line
DoSetPriority(_priority);
LOG_TRACE("SetPriority");


DoHook();
LOG_TRACE("Hook");

Loop();
LOG_TRACE("Loop");

DoUnhook();
LOG_TRACE("Unhook");

//DoSetPriority(Below); // TODO : Activate this line
DoSetPriority(Below);
LOG_TRACE("SetPriority");
}

LOG_TRACE("<Hook:Stopped>");
Expand Down
10 changes: 4 additions & 6 deletions LittleBigMouse.Hook/Hook/HookerDisplayChanged.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Hooker.h"
#include "Strings/str.h"

#define MAX_LOADSTRING 100

Expand All @@ -19,7 +20,7 @@ ATOM Hooker::RegisterClassLbm(HINSTANCE hInstance)
wcex.hCursor = nullptr; //LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = nullptr; //(HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = nullptr; //MAKEINTRESOURCEW(IDC_WINDOWSPROJECT1);
wcex.lpszClassName = szWindowClass;
wcex.lpszClassName = L"HookerDisplayChange";
wcex.hIconSm = nullptr; //LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassExW(&wcex);
Expand All @@ -28,9 +29,8 @@ ATOM Hooker::RegisterClassLbm(HINSTANCE hInstance)
BOOL Hooker::InitInstance(HINSTANCE hInstance)
{
_hInst = hInstance;
auto szWindowClass = L"HookerDisplayChange";

HWND hWnd = CreateWindowW(szWindowClass, szWindowClass, WS_OVERLAPPEDWINDOW,
HWND hWnd = CreateWindowW(L"HookerDisplayChange", szWindowClass, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

if (!hWnd)
Expand All @@ -52,8 +52,6 @@ bool Hooker::HookDisplayChange()
{
LOG_TRACE("<Hook:HookDisplayChange> : CreateHookWindow");

auto test = this;

const auto hInstance = GetModuleHandle(nullptr);

auto c = RegisterClassLbm(hInstance);
Expand All @@ -66,7 +64,7 @@ bool Hooker::HookDisplayChange()
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, (sizeof(buf) / sizeof(wchar_t)), NULL);
LOG_TRACE("<Hook:HookDisplayChange> : " << buf);
LOG_TRACE("<Hook:HookDisplayChange> : " << ToString(buf));
#endif

return false;
Expand Down
1 change: 1 addition & 0 deletions LittleBigMouse.Hook/Logger/Logger.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "Config.h"

#if _DEBUG && DEBUG_LOG

Expand Down
8 changes: 4 additions & 4 deletions LittleBigMouse.Hook/Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,

SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 );

//RemoteServerSocket server;
//MouseEngine engine;
RemoteServerSocket server;
MouseEngine engine;
auto hook = Hooker();

auto p = GetParentProcess();
Expand All @@ -115,8 +115,8 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
// }
//}

///auto daemon = LittleBigMouseDaemon( &server , &engine, &hook );
auto daemon = LittleBigMouseDaemon( nullptr , nullptr, &hook );
auto daemon = LittleBigMouseDaemon( &server , &engine, &hook );
///auto daemon = LittleBigMouseDaemon( nullptr , nullptr, &hook );

if(uiMode)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<StackPanel VerticalAlignment="Center">
<TextBlock HorizontalAlignment="Center" Margin="0,0,0,5" Text="Little Big Mouse" FontSize="20" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Center" Text="{Binding Version}" TextWrapping="Wrap" FontSize="15"/>
<TextBlock HorizontalAlignment="Center" Text="Copyright (C) 2015 - 2023" FontSize="15"/>
<TextBlock HorizontalAlignment="Center" Text="Copyright (C) 2015 - 2024" FontSize="15"/>
<TextBlock PointerPressed="InputElement_OnPointerPressed" HorizontalAlignment="Center" Margin="0,0,0,20" Text="Mathieu GRENET" FontSize="15"/>

<icons:IconView HorizontalAlignment="Center" Margin="0,0,0,20" Path="Icon/lbm_logo" Height="50"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public void LaunchDaemon()
var processes = Process.GetProcessesByName("LittleBigMouse.Hook");
foreach (var process in processes)
{
if(process.HasExited) continue;
Debug.WriteLine($"Already running : {process.ProcessName} {process.Id}");
return;
}
Expand Down

0 comments on commit 16e8a15

Please sign in to comment.