2019-04-10 11:07:56 +00:00
|
|
|
/*
|
|
|
|
Looking Glass - KVM FrameRelay (KVMFR) Client
|
|
|
|
Copyright (C) 2017-2019 Geoffrey McRae <geoff@hostfission.com>
|
|
|
|
https://looking-glass.hostfission.com
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 2 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
|
|
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
|
|
|
Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "windows/mousehook.h"
|
2020-01-02 11:21:42 +00:00
|
|
|
#include "common/windebug.h"
|
2019-04-10 11:07:56 +00:00
|
|
|
#include "platform.h"
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
struct mouseHook
|
|
|
|
{
|
|
|
|
bool installed;
|
|
|
|
HHOOK hook;
|
|
|
|
MouseHookFn callback;
|
2020-01-26 05:23:35 +00:00
|
|
|
int x, y;
|
2021-01-17 01:38:05 +00:00
|
|
|
HANDLE event;
|
|
|
|
HANDLE thread;
|
2019-04-10 11:07:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct mouseHook mouseHook = { 0 };
|
|
|
|
|
|
|
|
// forwards
|
|
|
|
static LRESULT WINAPI mouseHook_hook(int nCode, WPARAM wParam, LPARAM lParam);
|
|
|
|
|
2021-01-17 01:38:05 +00:00
|
|
|
static bool switchDesktopAndHook(void)
|
2019-04-10 11:07:56 +00:00
|
|
|
{
|
2021-01-17 01:38:05 +00:00
|
|
|
HDESK desk = OpenInputDesktop(0, FALSE, GENERIC_ALL);
|
|
|
|
if (!desk)
|
|
|
|
{
|
|
|
|
DEBUG_WINERROR("Failed to OpenInputDesktop", GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetThreadDesktop(desk))
|
|
|
|
{
|
|
|
|
DEBUG_WINERROR("Failed to SetThreadDesktop", GetLastError());
|
|
|
|
CloseDesktop(desk);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
CloseDesktop(desk);
|
|
|
|
|
2021-01-27 21:55:14 +00:00
|
|
|
POINT position;
|
|
|
|
GetCursorPos(&position);
|
|
|
|
|
|
|
|
mouseHook.x = position.x;
|
|
|
|
mouseHook.y = position.y;
|
|
|
|
mouseHook.callback(position.x, position.y);
|
|
|
|
|
2021-01-17 01:38:05 +00:00
|
|
|
mouseHook.hook = SetWindowsHookEx(WH_MOUSE_LL, mouseHook_hook, NULL, 0);
|
|
|
|
if (!mouseHook.hook)
|
|
|
|
{
|
|
|
|
DEBUG_WINERROR("Failed to install the mouse hook", GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2019-04-10 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 01:38:05 +00:00
|
|
|
static VOID WINAPI winEventProc(HWINEVENTHOOK hWinEventHook, DWORD event,
|
|
|
|
HWND hwnd, LONG idObject, LONG idChild, DWORD idEventThread, DWORD dwmsEventTime)
|
2019-04-10 11:07:56 +00:00
|
|
|
{
|
2021-01-17 01:38:05 +00:00
|
|
|
switch (event)
|
|
|
|
{
|
|
|
|
case EVENT_SYSTEM_DESKTOPSWITCH:
|
|
|
|
UnhookWindowsHookEx(mouseHook.hook);
|
|
|
|
switchDesktopAndHook();
|
|
|
|
break;
|
|
|
|
}
|
2019-04-10 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 01:38:05 +00:00
|
|
|
static DWORD WINAPI threadProc(LPVOID lParam) {
|
|
|
|
if (mouseHook.installed)
|
2019-04-10 11:07:56 +00:00
|
|
|
{
|
2021-01-17 01:38:05 +00:00
|
|
|
DEBUG_WARN("Mouse hook already installed");
|
|
|
|
return 0;
|
|
|
|
}
|
2019-04-10 11:07:56 +00:00
|
|
|
|
2021-01-27 21:55:14 +00:00
|
|
|
mouseHook.callback = (MouseHookFn)lParam;
|
2021-01-17 01:38:05 +00:00
|
|
|
if (!switchDesktopAndHook())
|
|
|
|
return 0;
|
2019-04-10 11:07:56 +00:00
|
|
|
|
2021-01-17 01:38:05 +00:00
|
|
|
mouseHook.installed = true;
|
|
|
|
|
|
|
|
HWINEVENTHOOK eventHook = SetWinEventHook(
|
|
|
|
EVENT_SYSTEM_DESKTOPSWITCH, EVENT_SYSTEM_DESKTOPSWITCH, NULL,
|
|
|
|
winEventProc, 0, 0, WINEVENT_OUTOFCONTEXT
|
|
|
|
);
|
|
|
|
if (!eventHook)
|
2019-04-10 11:07:56 +00:00
|
|
|
{
|
2021-01-17 01:38:05 +00:00
|
|
|
DEBUG_WINERROR("Failed to SetWinEventHook", GetLastError());
|
|
|
|
goto exit;
|
|
|
|
}
|
2019-04-10 11:07:56 +00:00
|
|
|
|
2021-01-17 01:38:05 +00:00
|
|
|
MSG msg;
|
|
|
|
while (true) {
|
|
|
|
switch (MsgWaitForMultipleObjects(1, &mouseHook.event, FALSE, INFINITE, QS_ALLINPUT)) {
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
DEBUG_INFO("Mouse hook thread received quit request");
|
|
|
|
PostQuitMessage(0);
|
|
|
|
break;
|
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
|
|
|
|
{
|
|
|
|
if (msg.message == WM_QUIT)
|
|
|
|
goto exit;
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto exit;
|
|
|
|
}
|
2019-04-10 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 01:38:05 +00:00
|
|
|
exit:
|
|
|
|
if (eventHook) UnhookWinEvent(eventHook);
|
|
|
|
UnhookWindowsHookEx(mouseHook.hook);
|
|
|
|
mouseHook.installed = false;
|
2019-04-10 11:07:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-17 01:38:05 +00:00
|
|
|
void mouseHook_install(MouseHookFn callback)
|
|
|
|
{
|
|
|
|
if (!mouseHook.event)
|
|
|
|
{
|
|
|
|
mouseHook.event = CreateEventA(NULL, FALSE, FALSE, NULL);
|
|
|
|
if (!mouseHook.event)
|
|
|
|
{
|
|
|
|
DEBUG_WINERROR("Failed to create mouse hook uninstall event", GetLastError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mouseHook.thread = CreateThread(NULL, 0, threadProc, callback, 0, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mouseHook_remove(void)
|
|
|
|
{
|
|
|
|
if (!mouseHook.event)
|
|
|
|
return;
|
2021-01-21 06:05:30 +00:00
|
|
|
|
2021-01-17 01:38:05 +00:00
|
|
|
SetEvent(mouseHook.event);
|
|
|
|
WaitForSingleObject(mouseHook.thread, INFINITE);
|
|
|
|
CloseHandle(mouseHook.thread);
|
|
|
|
}
|
|
|
|
|
2019-04-10 11:07:56 +00:00
|
|
|
static LRESULT WINAPI mouseHook_hook(int nCode, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
if (nCode == HC_ACTION && wParam == WM_MOUSEMOVE)
|
|
|
|
{
|
|
|
|
MSLLHOOKSTRUCT *msg = (MSLLHOOKSTRUCT *)lParam;
|
2020-01-26 05:23:35 +00:00
|
|
|
if (mouseHook.x != msg->pt.x || mouseHook.y != msg->pt.y)
|
|
|
|
{
|
|
|
|
mouseHook.x = msg->pt.x;
|
|
|
|
mouseHook.y = msg->pt.y;
|
|
|
|
mouseHook.callback(msg->pt.x, msg->pt.y);
|
|
|
|
}
|
2019-04-10 11:07:56 +00:00
|
|
|
}
|
|
|
|
return CallNextHookEx(mouseHook.hook, nCode, wParam, lParam);
|
2020-01-26 05:23:35 +00:00
|
|
|
}
|