blob: 6923db7e2914f076026975e91e05520aaf1790e5 [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2003 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// -=- wm_hooks.cxx
20//
21// Window Message Hooks Dynamic Link library
22
23#define _WIN32_WINNT 0x0400
24#include <tchar.h>
25
26#include <wm_hooks/wm_hooks.h>
27
28UINT WM_HK_PingThread = RegisterWindowMessage(_T("RFB.WM_Hooks.PingThread"));
29
30UINT WM_HK_WindowChanged = RegisterWindowMessage(_T("RFB.WM_Hooks.WindowChanged"));
31UINT WM_Hooks_WindowChanged() {
32 return WM_HK_WindowChanged;
33}
34
35UINT WM_HK_WindowClientAreaChanged = RegisterWindowMessage(_T("RFB.WM_Hooks.WindowClientAreaChanged"));
36UINT WM_Hooks_WindowClientAreaChanged() {
37 return WM_HK_WindowClientAreaChanged;
38}
39
40UINT WM_HK_WindowBorderChanged = RegisterWindowMessage(_T("RFB.WM_Hooks.WindowBorderChanged"));
41UINT WM_Hooks_WindowBorderChanged() {
42 return WM_HK_WindowBorderChanged;
43}
44
45UINT WM_HK_RectangleChanged = RegisterWindowMessage(_T("RFB.WM_Hooks.RectangleChanged"));
46UINT WM_Hooks_RectangleChanged() {
47 return WM_HK_RectangleChanged;
48}
49
50UINT WM_HK_CursorChanged = RegisterWindowMessage(_T("RFB.WM_Hooks.CursorChanged"));
51UINT WM_Hooks_CursorChanged() {
52 return WM_HK_CursorChanged;
53}
54
55#ifdef _DEBUG
56UINT WM_HK_Diagnostic = RegisterWindowMessage(_T("RFB.WM_Hooks.Diagnostic"));
57UINT WM_Hooks_Diagnostic() {
58 return WM_HK_Diagnostic;
59}
60#endif
61
62ATOM ATOM_Popup_Selection = GlobalAddAtom(_T("RFB.WM_Hooks.PopupSelectionAtom"));
63
64//
65// -=- DLL entry point
66//
67
68HINSTANCE dll_instance = 0;
69
70BOOL WINAPI DllMain(HANDLE instance, ULONG reason, LPVOID reserved) {
71 switch (reason) {
72 case DLL_PROCESS_ATTACH:
73 dll_instance = (HINSTANCE)instance;
74 return TRUE;
75 case DLL_PROCESS_DETACH:
76 return TRUE;
77 case DLL_THREAD_DETACH:
78 WM_Hooks_Remove(GetCurrentThreadId());
79 return TRUE;
80 default:
81 return TRUE;
82 };
83}
84
85//
86// -=- Display update hooks
87//
88
89#pragma data_seg(".WM_Hooks_Shared")
90DWORD hook_owner = 0;
91DWORD hook_target = 0;
92HHOOK hook_CallWndProc = 0;
93HHOOK hook_CallWndProcRet = 0;
94HHOOK hook_GetMessage = 0;
95HHOOK hook_DialogMessage = 0;
96BOOL enable_cursor_shape = FALSE;
97HCURSOR cursor = 0;
98#ifdef _DEBUG
99UINT diagnostic_min=1;
100UINT diagnostic_max=0;
101#endif
102#pragma data_seg()
103
104#ifdef _DEBUG
105DLLEXPORT void WM_Hooks_SetDiagnosticRange(UINT min, UINT max) {
106 diagnostic_min = min; diagnostic_max=max;
107}
108#endif
109
110bool NotifyHookOwner(UINT event, WPARAM wParam, LPARAM lParam) {
111 if (hook_owner) {
112 return PostThreadMessage(hook_owner, event, wParam, lParam)!=0;
113 /*
114 if (last_event)
115 return PostThreadMessage(hook_owner, last_event, last_wParam, last_lParam);
116 last_event = event;
117 last_wParam = wParam;
118 last_lParam = lParam;
119 return true;
120 */
121 }
122 return false;
123}
124
125bool NotifyWindow(HWND hwnd, UINT msg) {
126 return NotifyHookOwner(WM_HK_WindowChanged, msg, (LPARAM)hwnd);
127}
128bool NotifyWindowBorder(HWND hwnd, UINT msg) {
129 return NotifyHookOwner(WM_HK_WindowBorderChanged, msg, (LPARAM)hwnd);
130}
131bool NotifyWindowClientArea(HWND hwnd, UINT msg) {
132 return NotifyHookOwner(WM_HK_WindowClientAreaChanged, msg, (LPARAM)hwnd);
133}
134bool NotifyRectangle(RECT* rect) {
135 WPARAM w = MAKELONG((SHORT)rect->left, (SHORT)rect->top);
136 LPARAM l = MAKELONG((SHORT)rect->right, (SHORT)rect->bottom);
137 return NotifyHookOwner(WM_HK_RectangleChanged, w, l);
138}
139bool NotifyCursor(HCURSOR cursor) {
140 return NotifyHookOwner(WM_HK_CursorChanged, 0, (LPARAM)cursor);
141}
142
143void ProcessWindowMessage(UINT msg, HWND wnd, WPARAM wParam, LPARAM lParam) {
144#ifdef _DEBUG
145 if ((msg >= diagnostic_min) && (msg <= diagnostic_max))
146 PostThreadMessage(hook_owner, WM_HK_Diagnostic, msg, (LPARAM)wnd);
147#endif
148 if (!IsWindowVisible(wnd)) return;
149 switch (msg) {
150
151 // -=- Border update events
152 case WM_NCPAINT:
153 case WM_NCACTIVATE:
154 NotifyWindowBorder(wnd, msg);
155 break;
156
157 // -=- Client area update events
158 case BM_SETCHECK:
159 case BM_SETSTATE:
160 case EM_SETSEL:
161 case WM_CHAR:
162 case WM_ENABLE:
163 case WM_KEYUP:
164 case WM_LBUTTONUP:
165 case WM_MBUTTONUP:
166 case WM_PALETTECHANGED:
167 case WM_RBUTTONUP:
168 case WM_SYSCOLORCHANGE:
169 case WM_SETTEXT:
170 case WM_SETFOCUS:
171 //case WM_TIMER:
172 NotifyWindowClientArea(wnd, msg);
173 break;
174 case WM_HSCROLL:
175 case WM_VSCROLL:
176 if (((int) LOWORD(wParam) == SB_THUMBTRACK) || ((int) LOWORD(wParam) == SB_ENDSCROLL))
177 NotifyWindow(wnd, msg);
178 break;
179
180 case WM_WINDOWPOSCHANGING:
181 case WM_DESTROY:
182 {
183 RECT wrect;
184 if (GetWindowRect(wnd, &wrect)) {
185 NotifyRectangle(&wrect);
186 }
187 }
188 break;
189
190 case WM_PAINT:
191 // *** could improve this
192 NotifyWindowClientArea(wnd, msg);
193 break;
194
195 // Handle pop-up menus appearing
196 case 482:
197 NotifyWindow(wnd, 482);
198 break;
199
200 // Handle pop-up menus having items selected
201 case 485:
202 {
203 HANDLE prop = GetProp(wnd, (LPCTSTR) MAKELONG(ATOM_Popup_Selection, 0));
204 if (prop != (HANDLE) wParam) {
205 NotifyWindow(wnd, 485);
206 SetProp(wnd,
207 (LPCTSTR) MAKELONG(ATOM_Popup_Selection, 0),
208 (HANDLE) wParam);
209 }
210 }
211 break;
212
213 case WM_NCMOUSEMOVE:
214 case WM_MOUSEMOVE:
215 if (enable_cursor_shape) {
216 HCURSOR new_cursor = GetCursor();
217 if (new_cursor != cursor) {
218 cursor = new_cursor;
219 NotifyCursor(cursor);
220 }
221 }
222 break;
223
224 /* ***
225 if (prf_use_GetUpdateRect)
226 {
227 HRGN region;
228 region = CreateRectRgn(0, 0, 0, 0);
229
230 // Get the affected region
231 if (GetUpdateRgn(hWnd, region, FALSE) != ERROR)
232 {
233 int buffsize;
234 UINT x;
235 RGNDATA *buff;
236 POINT TopLeft;
237
238 // Get the top-left point of the client area
239 TopLeft.x = 0;
240 TopLeft.y = 0;
241 if (!ClientToScreen(hWnd, &TopLeft))
242 break;
243
244 // Get the size of buffer required
245 buffsize = GetRegionData(region, 0, 0);
246 if (buffsize != 0)
247 {
248 buff = (RGNDATA *) new BYTE [buffsize];
249 if (buff == NULL)
250 break;
251
252 // Now get the region data
253 if(GetRegionData(region, buffsize, buff))
254 {
255 for (x=0; x<(buff->rdh.nCount); x++)
256 {
257 // Obtain the rectangles from the list
258 RECT *urect = (RECT *) (((BYTE *) buff) + sizeof(RGNDATAHEADER) + (x * sizeof(RECT)));
259 SendDeferredUpdateRect(
260 hWnd,
261 (SHORT) (TopLeft.x + urect->left),
262 (SHORT) (TopLeft.y + urect->top),
263 (SHORT) (TopLeft.x + urect->right),
264 (SHORT) (TopLeft.y + urect->bottom)
265 );
266 }
267 }
268
269 delete [] buff;
270 }
271 }
272
273 // Now free the region
274 if (region != NULL)
275 DeleteObject(region);
276 }
277 */
278 };
279}
280
281LRESULT CALLBACK HookCallWndProc(int nCode, WPARAM wParam, LPARAM lParam) {
282 if (nCode == HC_ACTION) {
283 CWPSTRUCT* info = (CWPSTRUCT*) lParam;
284 ProcessWindowMessage(info->message, info->hwnd, info->wParam, info->lParam);
285 }
286 return CallNextHookEx(hook_CallWndProc, nCode, wParam, lParam);
287}
288
289LRESULT CALLBACK HookCallWndProcRet(int nCode, WPARAM wParam, LPARAM lParam) {
290 if (nCode == HC_ACTION) {
291 CWPRETSTRUCT* info = (CWPRETSTRUCT*) lParam;
292 ProcessWindowMessage(info->message, info->hwnd, info->wParam, info->lParam);
293 }
294 return CallNextHookEx(hook_CallWndProcRet, nCode, wParam, lParam);
295}
296
297LRESULT CALLBACK HookGetMessage(int nCode, WPARAM wParam, LPARAM lParam) {
298 if (nCode == HC_ACTION) {
299 if (wParam & PM_REMOVE) {
300 MSG* msg = (MSG*) lParam;
301 ProcessWindowMessage(msg->message, msg->hwnd, msg->wParam, msg->lParam);
302 }
303 }
304 return CallNextHookEx(hook_GetMessage, nCode, wParam, lParam);
305}
306
307LRESULT CALLBACK HookDialogMessage(int nCode, WPARAM wParam, LPARAM lParam) {
308 if (nCode == HC_ACTION) {
309 MSG* msg = (MSG*) lParam;
310 ProcessWindowMessage(msg->message, msg->hwnd, msg->wParam, msg->lParam);
311 }
312 return CallNextHookEx(hook_DialogMessage, nCode, wParam, lParam);
313}
314
315// - WM_Hooks_Install
316
317BOOL WM_Hooks_Install(DWORD owner, DWORD thread) {
318 // - Are there already hooks set?
319 if (hook_owner) {
320 if (!PostThreadMessage(hook_owner, WM_HK_PingThread, 0, 0)) {
321 WM_Hooks_Remove(hook_owner);
322 } else {
323 return FALSE;
324 }
325 }
326
327 // - Initialise the hooks
328 hook_owner = owner;
329 hook_target = thread;
330
331 hook_CallWndProc = SetWindowsHookEx(WH_CALLWNDPROC, HookCallWndProc, dll_instance, thread);
332 //hook_CallWndProcRet = SetWindowsHookEx(WH_CALLWNDPROCRET, HookCallWndProcRet, dll_instance, thread);
333 hook_GetMessage = SetWindowsHookEx(WH_GETMESSAGE, HookGetMessage, dll_instance, thread);
334 hook_DialogMessage = SetWindowsHookEx(WH_SYSMSGFILTER, HookDialogMessage, dll_instance, thread);
335
336 if (!hook_CallWndProc /*|| !hook_CallWndProcRet*/ || !hook_GetMessage || !hook_DialogMessage) {
337 WM_Hooks_Remove(owner);
338 return FALSE;
339 }
340
341 return TRUE;
342}
343
344// - WM_Hooks_Remove
345
346BOOL WM_Hooks_Remove(DWORD owner) {
347 if (owner != hook_owner) return FALSE;
348 if (hook_CallWndProc) {
349 UnhookWindowsHookEx(hook_CallWndProc);
350 hook_CallWndProc = 0;
351 }
352 if (hook_CallWndProcRet) {
353 UnhookWindowsHookEx(hook_CallWndProcRet);
354 hook_CallWndProcRet = 0;
355 }
356 if (hook_GetMessage) {
357 UnhookWindowsHookEx(hook_GetMessage);
358 hook_GetMessage = 0;
359 }
360 if (hook_DialogMessage) {
361 UnhookWindowsHookEx(hook_DialogMessage);
362 hook_DialogMessage = 0;
363 }
364 hook_owner = 0;
365 hook_target = 0;
366 return TRUE;
367}
368
369//
370// -=- User input hooks
371//
372
373#pragma data_seg(".WM_Hooks_Shared")
374HHOOK hook_keyboard = 0;
375HHOOK hook_pointer = 0;
376bool enable_real_ptr = true;
377bool enable_synth_ptr = true;
378bool enable_real_kbd = true;
379bool enable_synth_kbd = true;
380#pragma data_seg()
381
382#ifdef WH_KEYBOARD_LL
383LRESULT CALLBACK HookKeyboardHook(int nCode, WPARAM wParam, LPARAM lParam) {
384 if (nCode >= 0) {
385 KBDLLHOOKSTRUCT* info = (KBDLLHOOKSTRUCT*) lParam;
386 bool real_event = (info->flags & LLKHF_INJECTED) == 0;
387 if ((real_event && !enable_real_kbd) ||
388 (!real_event && !enable_synth_kbd)) {
389 return 1;
390 }
391 }
392 return CallNextHookEx(hook_keyboard, nCode, wParam, lParam);
393}
394
395LRESULT CALLBACK HookPointerHook(int nCode, WPARAM wParam, LPARAM lParam) {
396 if (nCode >= 0) {
397 MSLLHOOKSTRUCT* info = (MSLLHOOKSTRUCT*) lParam;
398 bool real_event = (info->flags & LLMHF_INJECTED) == 0;
399 if ((real_event && !enable_real_ptr) ||
400 (!real_event && !enable_synth_ptr)) {
401 return 1;
402 }
403 }
404 return CallNextHookEx(hook_keyboard, nCode, wParam, lParam);
405}
406
407bool RefreshInputHooks() {
408 bool success = true;
409 bool set_ptr_hook = !enable_real_ptr || !enable_synth_ptr;
410 bool set_kbd_hook = !enable_real_kbd || !enable_synth_kbd;
411 if (hook_keyboard && !set_kbd_hook) {
412 UnhookWindowsHookEx(hook_keyboard);
413 hook_keyboard = 0;
414 }
415 if (hook_pointer && !set_ptr_hook) {
416 UnhookWindowsHookEx(hook_pointer);
417 hook_pointer = 0;
418 }
419 if (!hook_keyboard && set_kbd_hook) {
420 hook_keyboard = SetWindowsHookEx(WH_KEYBOARD_LL, HookKeyboardHook, dll_instance, 0);
421 if (!hook_keyboard) success = false;
422 }
423 if (!hook_pointer && set_ptr_hook) {
424 hook_pointer = SetWindowsHookEx(WH_MOUSE_LL, HookPointerHook, dll_instance, 0);
425 if (!hook_pointer) success = false;
426 }
427 return success;
428}
429#else
430#pragma message("WARNING: low-level mouse and keyboard hooks not supported")
431#endif
432
433// - WM_Hooks_EnableRealInputs
434
435BOOL WM_Hooks_EnableRealInputs(BOOL pointer, BOOL keyboard) {
436#ifdef WH_KEYBOARD_LL
437 enable_real_ptr = pointer!=0;
438 enable_real_kbd = keyboard!=0;
439 return RefreshInputHooks();
440#else
441 return FALSE;
442#endif
443}
444
445// - WM_Hooks_EnableSynthInputs
446
447BOOL WM_Hooks_EnableSynthInputs(BOOL pointer, BOOL keyboard) {
448#ifdef WH_KEYBOARD_LL
449 enable_synth_ptr = pointer!=0;
450 enable_synth_kbd = keyboard!=0;
451 return RefreshInputHooks();
452#else
453 return FALSE;
454#endif
455}
456
457// - WM_Hooks_EnableCursorShape
458
459BOOL WM_Hooks_EnableCursorShape(BOOL enable) {
460 enable_cursor_shape = enable;
461 return FALSE;
462}