blob: bbc15088494a2d908152af7f41b8aebb4f700a24 [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- WMHooks.cxx
20
Pierre Ossman338e73a2016-07-07 15:35:13 +020021#include <os/Mutex.h>
22#include <os/Thread.h>
23
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000024#include <rfb_win32/WMHooks.h>
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000025#include <rfb_win32/Service.h>
26#include <rfb_win32/MsgWindow.h>
27#include <rfb_win32/IntervalTimer.h>
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000028#include <rfb/LogWriter.h>
29
30#include <list>
31
32using namespace rfb;
33using namespace rfb::win32;
34
35static LogWriter vlog("WMHooks");
36
37
Pierre Ossmanfc08bee2016-01-12 12:32:15 +010038static HMODULE hooksLibrary;
39
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000040typedef UINT (*WM_Hooks_WMVAL_proto)();
Pierre Ossmanfc08bee2016-01-12 12:32:15 +010041static WM_Hooks_WMVAL_proto WM_Hooks_WindowChanged;
42static WM_Hooks_WMVAL_proto WM_Hooks_WindowBorderChanged;
43static WM_Hooks_WMVAL_proto WM_Hooks_WindowClientAreaChanged;
44static WM_Hooks_WMVAL_proto WM_Hooks_RectangleChanged;
45#ifdef _DEBUG
46static WM_Hooks_WMVAL_proto WM_Hooks_Diagnostic;
47#endif
48
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000049typedef BOOL (*WM_Hooks_Install_proto)(DWORD owner, DWORD thread);
Pierre Ossmanfc08bee2016-01-12 12:32:15 +010050static WM_Hooks_Install_proto WM_Hooks_Install;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000051typedef BOOL (*WM_Hooks_Remove_proto)(DWORD owner);
Pierre Ossmanfc08bee2016-01-12 12:32:15 +010052static WM_Hooks_Remove_proto WM_Hooks_Remove;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000053#ifdef _DEBUG
54typedef void (*WM_Hooks_SetDiagnosticRange_proto)(UINT min, UINT max);
Pierre Ossmanfc08bee2016-01-12 12:32:15 +010055static WM_Hooks_SetDiagnosticRange_proto WM_Hooks_SetDiagnosticRange;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000056#endif
57
Pierre Ossmanfc08bee2016-01-12 12:32:15 +010058typedef BOOL (*WM_Hooks_EnableRealInputs_proto)(BOOL pointer, BOOL keyboard);
59static WM_Hooks_EnableRealInputs_proto WM_Hooks_EnableRealInputs;
60
61
62static void LoadHooks()
63{
64 if (hooksLibrary != NULL)
65 return;
66
67 hooksLibrary = LoadLibrary("wm_hooks.dll");
68 if (hooksLibrary == NULL)
69 return;
70
71 WM_Hooks_WindowChanged = (WM_Hooks_WMVAL_proto)GetProcAddress(hooksLibrary, "WM_Hooks_WindowChanged");
72 if (WM_Hooks_WindowChanged == NULL)
73 goto error;
74 WM_Hooks_WindowBorderChanged = (WM_Hooks_WMVAL_proto)GetProcAddress(hooksLibrary, "WM_Hooks_WindowBorderChanged");
75 if (WM_Hooks_WindowBorderChanged == NULL)
76 goto error;
77 WM_Hooks_WindowClientAreaChanged = (WM_Hooks_WMVAL_proto)GetProcAddress(hooksLibrary, "WM_Hooks_WindowClientAreaChanged");
78 if (WM_Hooks_WindowClientAreaChanged == NULL)
79 goto error;
80 WM_Hooks_RectangleChanged = (WM_Hooks_WMVAL_proto)GetProcAddress(hooksLibrary, "WM_Hooks_RectangleChanged");
81 if (WM_Hooks_RectangleChanged == NULL)
82 goto error;
83#ifdef _DEBUG
84 WM_Hooks_Diagnostic = (WM_Hooks_WMVAL_proto)GetProcAddress(hooksLibrary, "WM_Hooks_Diagnostic");
85 if (WM_Hooks_Diagnostic == NULL)
86 goto error;
87#endif
88
89 WM_Hooks_Install = (WM_Hooks_Install_proto)GetProcAddress(hooksLibrary, "WM_Hooks_Install");
90 if (WM_Hooks_Install == NULL)
91 goto error;
92 WM_Hooks_Remove = (WM_Hooks_Remove_proto)GetProcAddress(hooksLibrary, "WM_Hooks_Remove");
93 if (WM_Hooks_Remove == NULL)
94 goto error;
95#ifdef _DEBUG
96 WM_Hooks_SetDiagnosticRange = (WM_Hooks_SetDiagnosticRange_proto)GetProcAddress(hooksLibrary, "WM_Hooks_SetDiagnosticRange");
97 if (WM_Hooks_SetDiagnosticRange == NULL)
98 goto error;
99#endif
100
101 WM_Hooks_EnableRealInputs = (WM_Hooks_EnableRealInputs_proto)GetProcAddress(hooksLibrary, "WM_Hooks_EnableRealInputs");
102 if (WM_Hooks_EnableRealInputs == NULL)
103 goto error;
104
105 return;
106
107error:
108 FreeLibrary(hooksLibrary);
109 hooksLibrary = NULL;
110}
111
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000112
Pierre Ossman338e73a2016-07-07 15:35:13 +0200113class WMHooksThread : public os::Thread {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000114public:
Pierre Ossman338e73a2016-07-07 15:35:13 +0200115 WMHooksThread() : active(true), thread_id(-1) { }
116 void stop();
117 DWORD getThreadId() { return thread_id; }
118protected:
119 virtual void worker();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000120protected:
121 bool active;
Pierre Ossman338e73a2016-07-07 15:35:13 +0200122 DWORD thread_id;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000123};
124
Pierre Ossmanfc08bee2016-01-12 12:32:15 +0100125static WMHooksThread* hook_mgr = 0;
126static std::list<WMHooks*> hooks;
Pierre Ossman338e73a2016-07-07 15:35:13 +0200127static os::Mutex hook_mgr_lock;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000128
129
130static bool StartHookThread() {
131 if (hook_mgr)
132 return true;
Pierre Ossmanfc08bee2016-01-12 12:32:15 +0100133 if (hooksLibrary == NULL)
134 return false;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000135 vlog.debug("creating thread");
136 hook_mgr = new WMHooksThread();
Pierre Ossman338e73a2016-07-07 15:35:13 +0200137 hook_mgr->start();
138 while (hook_mgr->getThreadId() == (DWORD)-1)
139 Sleep(0);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000140 vlog.debug("installing hooks");
Pierre Ossmanfc08bee2016-01-12 12:32:15 +0100141 if (!WM_Hooks_Install(hook_mgr->getThreadId(), 0)) {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000142 vlog.error("failed to initialise hooks");
Pierre Ossman338e73a2016-07-07 15:35:13 +0200143 hook_mgr->stop();
144 delete hook_mgr;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000145 hook_mgr = 0;
146 return false;
147 }
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000148 return true;
149}
150
151static void StopHookThread() {
152 if (!hook_mgr)
153 return;
Pierre Ossmanfc08bee2016-01-12 12:32:15 +0100154 if (!hooks.empty())
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000155 return;
156 vlog.debug("closing thread");
Pierre Ossman338e73a2016-07-07 15:35:13 +0200157 hook_mgr->stop();
158 delete hook_mgr;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000159 hook_mgr = 0;
160}
161
162
163static bool AddHook(WMHooks* hook) {
164 vlog.debug("adding hook");
Pierre Ossman338e73a2016-07-07 15:35:13 +0200165 os::AutoMutex a(&hook_mgr_lock);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000166 if (!StartHookThread())
167 return false;
168 hooks.push_back(hook);
169 return true;
170}
171
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000172static bool RemHook(WMHooks* hook) {
173 {
174 vlog.debug("removing hook");
Pierre Ossman338e73a2016-07-07 15:35:13 +0200175 os::AutoMutex a(&hook_mgr_lock);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000176 hooks.remove(hook);
177 }
178 StopHookThread();
179 return true;
180}
181
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000182static void NotifyHooksRegion(const Region& r) {
Pierre Ossman338e73a2016-07-07 15:35:13 +0200183 os::AutoMutex a(&hook_mgr_lock);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000184 std::list<WMHooks*>::iterator i;
185 for (i=hooks.begin(); i!=hooks.end(); i++)
186 (*i)->NotifyHooksRegion(r);
187}
188
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000189
190void
Pierre Ossman338e73a2016-07-07 15:35:13 +0200191WMHooksThread::worker() {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000192 // Obtain message ids for all supported hook messages
Pierre Ossmanfc08bee2016-01-12 12:32:15 +0100193 UINT windowMsg = WM_Hooks_WindowChanged();
194 UINT clientAreaMsg = WM_Hooks_WindowClientAreaChanged();
195 UINT borderMsg = WM_Hooks_WindowBorderChanged();
196 UINT rectangleMsg = WM_Hooks_RectangleChanged();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000197#ifdef _DEBUG
Pierre Ossmanfc08bee2016-01-12 12:32:15 +0100198 UINT diagnosticMsg = WM_Hooks_Diagnostic();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000199#endif
200 MSG msg;
201 RECT wrect;
202 HWND hwnd;
203 int count = 0;
204
205 // Update delay handling
206 // We delay updates by 40-80ms, so that the triggering application has time to
207 // actually complete them before we notify the hook callbacks & they go off
208 // capturing screen state.
209 const int updateDelayMs = 40;
210 MsgWindow updateDelayWnd(_T("WMHooks::updateDelay"));
211 IntervalTimer updateDelayTimer(updateDelayWnd.getHandle(), 1);
212 Region updates[2];
213 int activeRgn = 0;
214
215 vlog.debug("starting hook thread");
216
Pierre Ossman338e73a2016-07-07 15:35:13 +0200217 thread_id = GetCurrentThreadId();
218
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000219 while (active && GetMessage(&msg, NULL, 0, 0)) {
220 count++;
221
222 if (msg.message == WM_TIMER) {
223 // Actually notify callbacks of graphical updates
224 NotifyHooksRegion(updates[1-activeRgn]);
225 if (updates[activeRgn].is_empty())
226 updateDelayTimer.stop();
227 activeRgn = 1-activeRgn;
228 updates[activeRgn].clear();
229
230 } else if (msg.message == windowMsg) {
231 // An entire window has (potentially) changed
232 hwnd = (HWND) msg.lParam;
233 if (IsWindow(hwnd) && IsWindowVisible(hwnd) && !IsIconic(hwnd) &&
234 GetWindowRect(hwnd, &wrect) && !IsRectEmpty(&wrect)) {
235 updates[activeRgn].assign_union(Rect(wrect.left, wrect.top,
236 wrect.right, wrect.bottom));
237 updateDelayTimer.start(updateDelayMs);
238 }
239
240 } else if (msg.message == clientAreaMsg) {
241 // The client area of a window has (potentially) changed
242 hwnd = (HWND) msg.lParam;
243 if (IsWindow(hwnd) && IsWindowVisible(hwnd) && !IsIconic(hwnd) &&
244 GetClientRect(hwnd, &wrect) && !IsRectEmpty(&wrect))
245 {
246 POINT pt = {0,0};
247 if (ClientToScreen(hwnd, &pt)) {
248 updates[activeRgn].assign_union(Rect(wrect.left+pt.x, wrect.top+pt.y,
249 wrect.right+pt.x, wrect.bottom+pt.y));
250 updateDelayTimer.start(updateDelayMs);
251 }
252 }
253
254 } else if (msg.message == borderMsg) {
255 hwnd = (HWND) msg.lParam;
256 if (IsWindow(hwnd) && IsWindowVisible(hwnd) && !IsIconic(hwnd) &&
257 GetWindowRect(hwnd, &wrect) && !IsRectEmpty(&wrect))
258 {
259 Region changed(Rect(wrect.left, wrect.top, wrect.right, wrect.bottom));
260 RECT crect;
261 POINT pt = {0,0};
262 if (GetClientRect(hwnd, &crect) && ClientToScreen(hwnd, &pt) &&
263 !IsRectEmpty(&crect))
264 {
265 changed.assign_subtract(Rect(crect.left+pt.x, crect.top+pt.y,
266 crect.right+pt.x, crect.bottom+pt.y));
267 }
268 if (!changed.is_empty()) {
269 updates[activeRgn].assign_union(changed);
270 updateDelayTimer.start(updateDelayMs);
271 }
272 }
273 } else if (msg.message == rectangleMsg) {
274 Rect r = Rect(LOWORD(msg.wParam), HIWORD(msg.wParam),
275 LOWORD(msg.lParam), HIWORD(msg.lParam));
276 if (!r.is_empty()) {
277 updates[activeRgn].assign_union(r);
278 updateDelayTimer.start(updateDelayMs);
279 }
280
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000281#ifdef _DEBUG
282 } else if (msg.message == diagnosticMsg) {
Pierre Ossman023df7e2015-09-29 15:42:58 +0200283 vlog.info("DIAG msg=%x(%d) wnd=%lx",
284 (unsigned)msg.wParam, (int)msg.wParam,
285 (unsigned long)msg.lParam);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000286#endif
287 }
288 }
289
290 vlog.debug("stopping hook thread - processed %d events", count);
Pierre Ossmanfc08bee2016-01-12 12:32:15 +0100291 WM_Hooks_Remove(getThreadId());
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000292}
293
Pierre Ossman338e73a2016-07-07 15:35:13 +0200294void
295WMHooksThread::stop() {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000296 vlog.debug("stopping WMHooks thread");
297 active = false;
298 PostThreadMessage(thread_id, WM_QUIT, 0, 0);
Pierre Ossman338e73a2016-07-07 15:35:13 +0200299 vlog.debug("waiting for WMHooks thread");
300 wait();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000301}
302
303// -=- WMHooks class
304
305rfb::win32::WMHooks::WMHooks() : updateEvent(0) {
Pierre Ossmanfc08bee2016-01-12 12:32:15 +0100306 LoadHooks();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000307}
308
309rfb::win32::WMHooks::~WMHooks() {
310 RemHook(this);
311}
312
313bool rfb::win32::WMHooks::setEvent(HANDLE ue) {
314 if (updateEvent)
315 RemHook(this);
316 updateEvent = ue;
317 return AddHook(this);
318}
319
320bool rfb::win32::WMHooks::getUpdates(UpdateTracker* ut) {
321 if (!updatesReady) return false;
Pierre Ossman338e73a2016-07-07 15:35:13 +0200322 os::AutoMutex a(&hook_mgr_lock);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000323 updates.copyTo(ut);
324 updates.clear();
325 updatesReady = false;
326 return true;
327}
328
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000329#ifdef _DEBUG
330void
331rfb::win32::WMHooks::setDiagnosticRange(UINT min, UINT max) {
Pierre Ossmanfc08bee2016-01-12 12:32:15 +0100332 WM_Hooks_SetDiagnosticRange(min, max);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000333}
334#endif
335
336void rfb::win32::WMHooks::NotifyHooksRegion(const Region& r) {
337 // hook_mgr_lock is already held at this point
338 updates.add_changed(r);
339 updatesReady = true;
340 SetEvent(updateEvent);
341}
342
343
344// -=- WMBlockInput class
345
346rfb::win32::WMBlockInput::WMBlockInput() : active(false) {
Pierre Ossmanfc08bee2016-01-12 12:32:15 +0100347 LoadHooks();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000348}
349
350rfb::win32::WMBlockInput::~WMBlockInput() {
351 blockInputs(false);
352}
353
Pierre Ossmanfc08bee2016-01-12 12:32:15 +0100354static bool blocking = false;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000355static bool blockRealInputs(bool block_) {
356 // NB: Requires blockMutex to be held!
Pierre Ossmanfc08bee2016-01-12 12:32:15 +0100357 if (hooksLibrary == NULL)
358 return false;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000359 if (block_) {
Pierre Ossmanfc08bee2016-01-12 12:32:15 +0100360 if (blocking)
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000361 return true;
362 // Enable blocking
Pierre Ossmanfc08bee2016-01-12 12:32:15 +0100363 if (!WM_Hooks_EnableRealInputs(false, false))
364 return false;
365 blocking = true;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000366 }
Pierre Ossmanfc08bee2016-01-12 12:32:15 +0100367 if (blocking) {
368 WM_Hooks_EnableRealInputs(true, true);
369 blocking = false;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000370 }
Pierre Ossmanfc08bee2016-01-12 12:32:15 +0100371 return block_ == blocking;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000372}
373
Pierre Ossman338e73a2016-07-07 15:35:13 +0200374static os::Mutex blockMutex;
375static int blockCount = 0;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000376
377bool rfb::win32::WMBlockInput::blockInputs(bool on) {
378 if (active == on) return true;
Pierre Ossman338e73a2016-07-07 15:35:13 +0200379 os::AutoMutex a(&blockMutex);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000380 int newCount = on ? blockCount+1 : blockCount-1;
381 if (!blockRealInputs(newCount > 0))
382 return false;
383 blockCount = newCount;
384 active = on;
385 return true;
386}