blob: 2d69053819da5485370bfa41e7b7ed8d65fc4812 [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
21#include <rfb_win32/WMHooks.h>
22#include <rfb_win32/DynamicFn.h>
23#include <rfb_win32/Service.h>
24#include <rfb_win32/MsgWindow.h>
25#include <rfb_win32/IntervalTimer.h>
26#include <rfb/Threading.h>
27#include <rfb/LogWriter.h>
28
29#include <list>
30
31using namespace rfb;
32using namespace rfb::win32;
33
34static LogWriter vlog("WMHooks");
35
36
37typedef UINT (*WM_Hooks_WMVAL_proto)();
38typedef BOOL (*WM_Hooks_Install_proto)(DWORD owner, DWORD thread);
39typedef BOOL (*WM_Hooks_Remove_proto)(DWORD owner);
40typedef BOOL (*WM_Hooks_EnableCursorShape_proto)(BOOL enable);
41#ifdef _DEBUG
42typedef void (*WM_Hooks_SetDiagnosticRange_proto)(UINT min, UINT max);
43DynamicFn<WM_Hooks_SetDiagnosticRange_proto> WM_Hooks_SetDiagnosticRange(_T("wm_hooks.dll"), "WM_Hooks_SetDiagnosticRange");
44#endif
45
46
47class WMHooksThread : public Thread {
48public:
49 WMHooksThread() : Thread("WMHookThread"), active(true),
50 WM_Hooks_Install(_T("wm_hooks.dll"), "WM_Hooks_Install"),
51 WM_Hooks_Remove(_T("wm_hooks.dll"), "WM_Hooks_Remove"),
52 WM_Hooks_EnableCursorShape(_T("wm_hooks.dll"), "WM_Hooks_EnableCursorShape") {
53 }
54 virtual void run();
55 virtual Thread* join();
56 DynamicFn<WM_Hooks_Install_proto> WM_Hooks_Install;;
57 DynamicFn<WM_Hooks_Remove_proto> WM_Hooks_Remove;
58 DynamicFn<WM_Hooks_EnableCursorShape_proto> WM_Hooks_EnableCursorShape;
59protected:
60 bool active;
61};
62
63WMHooksThread* hook_mgr = 0;
64std::list<WMHooks*> hooks;
65std::list<WMCursorHooks*> cursor_hooks;
66Mutex hook_mgr_lock;
67HCURSOR hook_cursor = (HCURSOR)LoadImage(0, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
68
69
70static bool StartHookThread() {
71 if (hook_mgr)
72 return true;
73 vlog.debug("creating thread");
74 hook_mgr = new WMHooksThread();
75 if (!hook_mgr->WM_Hooks_Install.isValid() ||
76 !hook_mgr->WM_Hooks_Remove.isValid()) {
77 vlog.debug("hooks not available");
78 return false;
79 }
80 vlog.debug("installing hooks");
81 if (!(*hook_mgr->WM_Hooks_Install)(hook_mgr->getThreadId(), 0)) {
82 vlog.error("failed to initialise hooks");
83 delete hook_mgr->join();
84 hook_mgr = 0;
85 return false;
86 }
87 vlog.debug("starting thread");
88 hook_mgr->start();
89 return true;
90}
91
92static void StopHookThread() {
93 if (!hook_mgr)
94 return;
95 if (!hooks.empty() || !cursor_hooks.empty())
96 return;
97 vlog.debug("closing thread");
98 delete hook_mgr->join();
99 hook_mgr = 0;
100}
101
102
103static bool AddHook(WMHooks* hook) {
104 vlog.debug("adding hook");
105 Lock l(hook_mgr_lock);
106 if (!StartHookThread())
107 return false;
108 hooks.push_back(hook);
109 return true;
110}
111
112static bool AddCursorHook(WMCursorHooks* hook) {
113 vlog.debug("adding cursor hook");
114 Lock l(hook_mgr_lock);
115 if (!StartHookThread())
116 return false;
117 if (!hook_mgr->WM_Hooks_EnableCursorShape.isValid())
118 return false;
119 if (cursor_hooks.empty() && !(*hook_mgr->WM_Hooks_EnableCursorShape)(TRUE))
120 return false;
121 cursor_hooks.push_back(hook);
122 return true;
123}
124
125static bool RemHook(WMHooks* hook) {
126 {
127 vlog.debug("removing hook");
128 Lock l(hook_mgr_lock);
129 hooks.remove(hook);
130 }
131 StopHookThread();
132 return true;
133}
134
135static bool RemCursorHook(WMCursorHooks* hook) {
136 {
137 vlog.debug("removing cursor hook");
138 Lock l(hook_mgr_lock);
139 cursor_hooks.remove(hook);
140 if (hook_mgr->WM_Hooks_EnableCursorShape.isValid() &&
141 cursor_hooks.empty())
142 (*hook_mgr->WM_Hooks_EnableCursorShape)(FALSE);
143 }
144 StopHookThread();
145 return true;
146}
147
148static void NotifyHooksRegion(const Region& r) {
149 Lock l(hook_mgr_lock);
150 std::list<WMHooks*>::iterator i;
151 for (i=hooks.begin(); i!=hooks.end(); i++)
152 (*i)->NotifyHooksRegion(r);
153}
154
155static void NotifyHooksCursor(HCURSOR c) {
156 Lock l(hook_mgr_lock);
157 hook_cursor = c;
158}
159
160
161static UINT GetMsgVal(DynamicFn<WM_Hooks_WMVAL_proto>& fn) {
162 if (fn.isValid())
163 return (*fn)();
164 return WM_NULL;
165}
166
167void
168WMHooksThread::run() {
169 // Obtain message ids for all supported hook messages
170 DynamicFn<WM_Hooks_WMVAL_proto> WM_Hooks_WindowChanged(_T("wm_hooks.dll"), "WM_Hooks_WindowChanged");
171 DynamicFn<WM_Hooks_WMVAL_proto> WM_Hooks_WindowBorderChanged(_T("wm_hooks.dll"), "WM_Hooks_WindowBorderChanged");
172 DynamicFn<WM_Hooks_WMVAL_proto> WM_Hooks_WindowClientAreaChanged(_T("wm_hooks.dll"), "WM_Hooks_WindowClientAreaChanged");
173 DynamicFn<WM_Hooks_WMVAL_proto> WM_Hooks_RectangleChanged(_T("wm_hooks.dll"), "WM_Hooks_RectangleChanged");
174 DynamicFn<WM_Hooks_WMVAL_proto> WM_Hooks_CursorChanged(_T("wm_hooks.dll"), "WM_Hooks_CursorChanged");
175 UINT windowMsg = GetMsgVal(WM_Hooks_WindowChanged);
176 UINT clientAreaMsg = GetMsgVal(WM_Hooks_WindowClientAreaChanged);
177 UINT borderMsg = GetMsgVal(WM_Hooks_WindowBorderChanged);
178 UINT rectangleMsg = GetMsgVal(WM_Hooks_RectangleChanged);
179 UINT cursorMsg = GetMsgVal(WM_Hooks_CursorChanged);
180#ifdef _DEBUG
181 DynamicFn<WM_Hooks_WMVAL_proto> WM_Hooks_Diagnostic(_T("wm_hooks.dll"), "WM_Hooks_Diagnostic");
182 UINT diagnosticMsg = GetMsgVal(WM_Hooks_Diagnostic);
183#endif
184 MSG msg;
185 RECT wrect;
186 HWND hwnd;
187 int count = 0;
188
189 // Update delay handling
190 // We delay updates by 40-80ms, so that the triggering application has time to
191 // actually complete them before we notify the hook callbacks & they go off
192 // capturing screen state.
193 const int updateDelayMs = 40;
194 MsgWindow updateDelayWnd(_T("WMHooks::updateDelay"));
195 IntervalTimer updateDelayTimer(updateDelayWnd.getHandle(), 1);
196 Region updates[2];
197 int activeRgn = 0;
198
199 vlog.debug("starting hook thread");
200
201 while (active && GetMessage(&msg, NULL, 0, 0)) {
202 count++;
203
204 if (msg.message == WM_TIMER) {
205 // Actually notify callbacks of graphical updates
206 NotifyHooksRegion(updates[1-activeRgn]);
207 if (updates[activeRgn].is_empty())
208 updateDelayTimer.stop();
209 activeRgn = 1-activeRgn;
210 updates[activeRgn].clear();
211
212 } else if (msg.message == windowMsg) {
213 // An entire window has (potentially) changed
214 hwnd = (HWND) msg.lParam;
215 if (IsWindow(hwnd) && IsWindowVisible(hwnd) && !IsIconic(hwnd) &&
216 GetWindowRect(hwnd, &wrect) && !IsRectEmpty(&wrect)) {
217 updates[activeRgn].assign_union(Rect(wrect.left, wrect.top,
218 wrect.right, wrect.bottom));
219 updateDelayTimer.start(updateDelayMs);
220 }
221
222 } else if (msg.message == clientAreaMsg) {
223 // The client area of a window has (potentially) changed
224 hwnd = (HWND) msg.lParam;
225 if (IsWindow(hwnd) && IsWindowVisible(hwnd) && !IsIconic(hwnd) &&
226 GetClientRect(hwnd, &wrect) && !IsRectEmpty(&wrect))
227 {
228 POINT pt = {0,0};
229 if (ClientToScreen(hwnd, &pt)) {
230 updates[activeRgn].assign_union(Rect(wrect.left+pt.x, wrect.top+pt.y,
231 wrect.right+pt.x, wrect.bottom+pt.y));
232 updateDelayTimer.start(updateDelayMs);
233 }
234 }
235
236 } else if (msg.message == borderMsg) {
237 hwnd = (HWND) msg.lParam;
238 if (IsWindow(hwnd) && IsWindowVisible(hwnd) && !IsIconic(hwnd) &&
239 GetWindowRect(hwnd, &wrect) && !IsRectEmpty(&wrect))
240 {
241 Region changed(Rect(wrect.left, wrect.top, wrect.right, wrect.bottom));
242 RECT crect;
243 POINT pt = {0,0};
244 if (GetClientRect(hwnd, &crect) && ClientToScreen(hwnd, &pt) &&
245 !IsRectEmpty(&crect))
246 {
247 changed.assign_subtract(Rect(crect.left+pt.x, crect.top+pt.y,
248 crect.right+pt.x, crect.bottom+pt.y));
249 }
250 if (!changed.is_empty()) {
251 updates[activeRgn].assign_union(changed);
252 updateDelayTimer.start(updateDelayMs);
253 }
254 }
255 } else if (msg.message == rectangleMsg) {
256 Rect r = Rect(LOWORD(msg.wParam), HIWORD(msg.wParam),
257 LOWORD(msg.lParam), HIWORD(msg.lParam));
258 if (!r.is_empty()) {
259 updates[activeRgn].assign_union(r);
260 updateDelayTimer.start(updateDelayMs);
261 }
262
263 } else if (msg.message == cursorMsg) {
264 NotifyHooksCursor((HCURSOR)msg.lParam);
265#ifdef _DEBUG
266 } else if (msg.message == diagnosticMsg) {
267 vlog.info("DIAG msg=%x(%d) wnd=%lx", msg.wParam, msg.wParam, msg.lParam);
268#endif
269 }
270 }
271
272 vlog.debug("stopping hook thread - processed %d events", count);
273 (*WM_Hooks_Remove)(getThreadId());
274}
275
276Thread*
277WMHooksThread::join() {
278 vlog.debug("stopping WMHooks thread");
279 active = false;
280 PostThreadMessage(thread_id, WM_QUIT, 0, 0);
281 vlog.debug("joining WMHooks thread");
282 return Thread::join();
283}
284
285// -=- WMHooks class
286
287rfb::win32::WMHooks::WMHooks() : updateEvent(0) {
288}
289
290rfb::win32::WMHooks::~WMHooks() {
291 RemHook(this);
292}
293
294bool rfb::win32::WMHooks::setEvent(HANDLE ue) {
295 if (updateEvent)
296 RemHook(this);
297 updateEvent = ue;
298 return AddHook(this);
299}
300
301bool rfb::win32::WMHooks::getUpdates(UpdateTracker* ut) {
302 if (!updatesReady) return false;
303 Lock l(hook_mgr_lock);
304 updates.copyTo(ut);
305 updates.clear();
306 updatesReady = false;
307 return true;
308}
309
310bool rfb::win32::WMHooks::areAvailable() {
311 WMHooksThread wmht;
312 return wmht.WM_Hooks_Install.isValid();
313}
314
315#ifdef _DEBUG
316void
317rfb::win32::WMHooks::setDiagnosticRange(UINT min, UINT max) {
318 if (WM_Hooks_SetDiagnosticRange.isValid())
319 (*WM_Hooks_SetDiagnosticRange)(min, max);
320}
321#endif
322
323void rfb::win32::WMHooks::NotifyHooksRegion(const Region& r) {
324 // hook_mgr_lock is already held at this point
325 updates.add_changed(r);
326 updatesReady = true;
327 SetEvent(updateEvent);
328}
329
330
331// -=- WMBlockInput class
332
333rfb::win32::WMBlockInput::WMBlockInput() : active(false) {
334}
335
336rfb::win32::WMBlockInput::~WMBlockInput() {
337 blockInputs(false);
338}
339
340typedef BOOL (*WM_Hooks_EnableRealInputs_proto)(BOOL pointer, BOOL keyboard);
341DynamicFn<WM_Hooks_EnableRealInputs_proto>* WM_Hooks_EnableRealInputs = 0;
342static bool blockRealInputs(bool block_) {
343 // NB: Requires blockMutex to be held!
344 if (block_) {
345 if (WM_Hooks_EnableRealInputs)
346 return true;
347 // Enable blocking
348 WM_Hooks_EnableRealInputs = new DynamicFn<WM_Hooks_EnableRealInputs_proto>(_T("wm_hooks.dll"), "WM_Hooks_EnableRealInputs");
349 if (WM_Hooks_EnableRealInputs->isValid() && (**WM_Hooks_EnableRealInputs)(false, false))
350 return true;
351 }
352 if (WM_Hooks_EnableRealInputs) {
353 // Clean up the DynamicFn, either if init failed, or block_ is false
354 if (WM_Hooks_EnableRealInputs->isValid())
355 (**WM_Hooks_EnableRealInputs)(true, true);
356 delete WM_Hooks_EnableRealInputs;
357 WM_Hooks_EnableRealInputs = 0;
358 }
359 return block_ == (WM_Hooks_EnableRealInputs != 0);
360}
361
362Mutex blockMutex;
363int blockCount = 0;
364
365bool rfb::win32::WMBlockInput::blockInputs(bool on) {
366 if (active == on) return true;
367 Lock l(blockMutex);
368 int newCount = on ? blockCount+1 : blockCount-1;
369 if (!blockRealInputs(newCount > 0))
370 return false;
371 blockCount = newCount;
372 active = on;
373 return true;
374}
375
376
377// -=- WMCursorHooks class
378
379rfb::win32::WMCursorHooks::WMCursorHooks() {
380}
381
382rfb::win32::WMCursorHooks::~WMCursorHooks() {
383 RemCursorHook(this);
384}
385
386bool
387rfb::win32::WMCursorHooks::start() {
388 return AddCursorHook(this);
389}
390
391HCURSOR
392rfb::win32::WMCursorHooks::getCursor() const {
393 return hook_cursor;
394}