d2d1: Create feature level 10.0 device context state objects.
[wine.git] / dlls / user32 / hook.c
blob159797020f2d754c740ea45f5d42ed242a8ea14a
1 /*
2 * Windows hook functions
4 * Copyright 2002 Alexandre Julliard
5 * Copyright 2005 Dmitry Timoshkov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * NOTES:
22 * Status of the various hooks:
23 * WH_MSGFILTER OK
24 * WH_JOURNALRECORD Partially implemented
25 * WH_JOURNALPLAYBACK Partially implemented
26 * WH_KEYBOARD OK
27 * WH_GETMESSAGE OK (FIXME: A/W mapping?)
28 * WH_CALLWNDPROC OK (FIXME: A/W mapping?)
29 * WH_CBT
30 * HCBT_MOVESIZE OK
31 * HCBT_MINMAX OK
32 * HCBT_QS OK
33 * HCBT_CREATEWND OK
34 * HCBT_DESTROYWND OK
35 * HCBT_ACTIVATE OK
36 * HCBT_CLICKSKIPPED OK
37 * HCBT_KEYSKIPPED OK
38 * HCBT_SYSCOMMAND OK
39 * HCBT_SETFOCUS OK
40 * WH_SYSMSGFILTER OK
41 * WH_MOUSE OK
42 * WH_HARDWARE Not supported in Win32
43 * WH_DEBUG Not implemented
44 * WH_SHELL
45 * HSHELL_WINDOWCREATED OK
46 * HSHELL_WINDOWDESTROYED OK
47 * HSHELL_ACTIVATESHELLWINDOW Not implemented
48 * HSHELL_WINDOWACTIVATED Not implemented
49 * HSHELL_GETMINRECT Not implemented
50 * HSHELL_REDRAW Not implemented
51 * HSHELL_TASKMAN Not implemented
52 * HSHELL_LANGUAGE Not implemented
53 * HSHELL_SYSMENU Not implemented
54 * HSHELL_ENDTASK Not implemented
55 * HSHELL_ACCESSIBILITYSTATE Not implemented
56 * HSHELL_APPCOMMAND Not implemented
57 * HSHELL_WINDOWREPLACED Not implemented
58 * HSHELL_WINDOWREPLACING Not implemented
59 * WH_FOREGROUNDIDLE Not implemented
60 * WH_CALLWNDPROCRET OK (FIXME: A/W mapping?)
61 * WH_KEYBOARD_LL Implemented but should use SendMessage instead
62 * WH_MOUSE_LL Implemented but should use SendMessage instead
65 #include <stdarg.h>
66 #include <assert.h>
68 #include "windef.h"
69 #include "winbase.h"
70 #include "winnls.h"
71 #include "wingdi.h"
72 #include "winuser.h"
73 #include "winerror.h"
74 #include "win.h"
75 #include "user_private.h"
76 #include "wine/server.h"
77 #include "wine/asm.h"
78 #include "wine/debug.h"
79 #include "winternl.h"
81 WINE_DEFAULT_DEBUG_CHANNEL(hook);
82 WINE_DECLARE_DEBUG_CHANNEL(relay);
84 static struct user_api_hook original_user_api =
86 USER_ScrollBarDraw,
87 USER_ScrollBarProc,
89 static struct user_api_hook hooked_user_api;
90 struct user_api_hook *user_api = &original_user_api;
92 struct hook_info
94 INT id;
95 void *proc;
96 void *handle;
97 DWORD pid, tid;
98 BOOL prev_unicode, next_unicode;
99 WCHAR module[MAX_PATH];
102 #define WH_WINEVENT (WH_MAXHOOK+1)
104 static const char * const hook_names[WH_WINEVENT - WH_MINHOOK + 1] =
106 "WH_MSGFILTER",
107 "WH_JOURNALRECORD",
108 "WH_JOURNALPLAYBACK",
109 "WH_KEYBOARD",
110 "WH_GETMESSAGE",
111 "WH_CALLWNDPROC",
112 "WH_CBT",
113 "WH_SYSMSGFILTER",
114 "WH_MOUSE",
115 "WH_HARDWARE",
116 "WH_DEBUG",
117 "WH_SHELL",
118 "WH_FOREGROUNDIDLE",
119 "WH_CALLWNDPROCRET",
120 "WH_KEYBOARD_LL",
121 "WH_MOUSE_LL",
122 "WH_WINEVENT"
126 /***********************************************************************
127 * get_ll_hook_timeout
130 static UINT get_ll_hook_timeout(void)
132 /* FIXME: should retrieve LowLevelHooksTimeout in HKEY_CURRENT_USER\Control Panel\Desktop */
133 return 2000;
137 /***********************************************************************
138 * set_windows_hook
140 * Implementation of SetWindowsHookExA and SetWindowsHookExW.
142 static HHOOK set_windows_hook( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid, BOOL unicode )
144 HHOOK handle = 0;
145 WCHAR module[MAX_PATH];
146 DWORD len;
148 if (!proc)
150 SetLastError( ERROR_INVALID_FILTER_PROC );
151 return 0;
154 if (tid) /* thread-local hook */
156 if (id == WH_JOURNALRECORD ||
157 id == WH_JOURNALPLAYBACK ||
158 id == WH_KEYBOARD_LL ||
159 id == WH_MOUSE_LL ||
160 id == WH_SYSMSGFILTER)
162 /* these can only be global */
163 SetLastError( ERROR_INVALID_PARAMETER );
164 return 0;
167 else /* system-global hook */
169 if (id == WH_KEYBOARD_LL || id == WH_MOUSE_LL) inst = 0;
170 else if (!inst)
172 SetLastError( ERROR_HOOK_NEEDS_HMOD );
173 return 0;
177 if (inst && (!(len = GetModuleFileNameW( inst, module, MAX_PATH )) || len >= MAX_PATH))
179 SetLastError( ERROR_INVALID_PARAMETER );
180 return 0;
183 SERVER_START_REQ( set_hook )
185 req->id = id;
186 req->pid = 0;
187 req->tid = tid;
188 req->event_min = EVENT_MIN;
189 req->event_max = EVENT_MAX;
190 req->flags = WINEVENT_INCONTEXT;
191 req->unicode = unicode;
192 if (inst) /* make proc relative to the module base */
194 req->proc = wine_server_client_ptr( (void *)((char *)proc - (char *)inst) );
195 wine_server_add_data( req, module, lstrlenW(module) * sizeof(WCHAR) );
197 else req->proc = wine_server_client_ptr( proc );
199 if (!wine_server_call_err( req ))
201 handle = wine_server_ptr_handle( reply->handle );
202 get_user_thread_info()->active_hooks = reply->active_hooks;
205 SERVER_END_REQ;
207 TRACE( "%s %p %x -> %p\n", hook_names[id-WH_MINHOOK], proc, tid, handle );
208 return handle;
211 #ifdef __i386__
212 /* Some apps pass a non-stdcall proc to SetWindowsHookExA,
213 * so we need a small assembly wrapper to call the proc.
215 extern LRESULT HOOKPROC_wrapper( HOOKPROC proc,
216 INT code, WPARAM wParam, LPARAM lParam );
217 __ASM_GLOBAL_FUNC( HOOKPROC_wrapper,
218 "pushl %ebp\n\t"
219 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
220 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
221 "movl %esp,%ebp\n\t"
222 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
223 "pushl %edi\n\t"
224 __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
225 "pushl %esi\n\t"
226 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
227 "pushl %ebx\n\t"
228 __ASM_CFI(".cfi_rel_offset %ebx,-12\n\t")
229 "pushl 20(%ebp)\n\t"
230 "pushl 16(%ebp)\n\t"
231 "pushl 12(%ebp)\n\t"
232 "movl 8(%ebp),%eax\n\t"
233 "call *%eax\n\t"
234 "leal -12(%ebp),%esp\n\t"
235 "popl %ebx\n\t"
236 __ASM_CFI(".cfi_same_value %ebx\n\t")
237 "popl %esi\n\t"
238 __ASM_CFI(".cfi_same_value %esi\n\t")
239 "popl %edi\n\t"
240 __ASM_CFI(".cfi_same_value %edi\n\t")
241 "leave\n\t"
242 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
243 __ASM_CFI(".cfi_same_value %ebp\n\t")
244 "ret" )
245 #else
246 static inline LRESULT HOOKPROC_wrapper( HOOKPROC proc,
247 INT code, WPARAM wParam, LPARAM lParam )
249 return proc( code, wParam, lParam );
251 #endif /* __i386__ */
254 /***********************************************************************
255 * call_hook_AtoW
257 static LRESULT call_hook_AtoW( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
259 LRESULT ret;
260 UNICODE_STRING usBuffer;
261 if (id != WH_CBT || code != HCBT_CREATEWND)
262 ret = HOOKPROC_wrapper( proc, code, wparam, lparam );
263 else
265 CBT_CREATEWNDA *cbtcwA = (CBT_CREATEWNDA *)lparam;
266 CBT_CREATEWNDW cbtcwW;
267 CREATESTRUCTW csW;
268 LPWSTR nameW = NULL;
269 LPWSTR classW = NULL;
271 cbtcwW.lpcs = &csW;
272 cbtcwW.hwndInsertAfter = cbtcwA->hwndInsertAfter;
273 csW = *(CREATESTRUCTW *)cbtcwA->lpcs;
275 if (!IS_INTRESOURCE(cbtcwA->lpcs->lpszName))
277 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszName);
278 csW.lpszName = nameW = usBuffer.Buffer;
280 if (!IS_INTRESOURCE(cbtcwA->lpcs->lpszClass))
282 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszClass);
283 csW.lpszClass = classW = usBuffer.Buffer;
285 ret = HOOKPROC_wrapper( proc, code, wparam, (LPARAM)&cbtcwW );
286 cbtcwA->hwndInsertAfter = cbtcwW.hwndInsertAfter;
287 HeapFree( GetProcessHeap(), 0, nameW );
288 HeapFree( GetProcessHeap(), 0, classW );
290 return ret;
294 /***********************************************************************
295 * call_hook_WtoA
297 static LRESULT call_hook_WtoA( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
299 LRESULT ret;
301 if (id != WH_CBT || code != HCBT_CREATEWND)
302 ret = HOOKPROC_wrapper( proc, code, wparam, lparam );
303 else
305 CBT_CREATEWNDW *cbtcwW = (CBT_CREATEWNDW *)lparam;
306 CBT_CREATEWNDA cbtcwA;
307 CREATESTRUCTA csA;
308 int len;
309 LPSTR nameA = NULL;
310 LPSTR classA = NULL;
312 cbtcwA.lpcs = &csA;
313 cbtcwA.hwndInsertAfter = cbtcwW->hwndInsertAfter;
314 csA = *(CREATESTRUCTA *)cbtcwW->lpcs;
316 if (!IS_INTRESOURCE(cbtcwW->lpcs->lpszName)) {
317 len = WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszName, -1, NULL, 0, NULL, NULL );
318 nameA = HeapAlloc( GetProcessHeap(), 0, len*sizeof(CHAR) );
319 WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszName, -1, nameA, len, NULL, NULL );
320 csA.lpszName = nameA;
323 if (!IS_INTRESOURCE(cbtcwW->lpcs->lpszClass)) {
324 len = WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszClass, -1, NULL, 0, NULL, NULL );
325 classA = HeapAlloc( GetProcessHeap(), 0, len*sizeof(CHAR) );
326 WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszClass, -1, classA, len, NULL, NULL );
327 csA.lpszClass = classA;
330 ret = HOOKPROC_wrapper( proc, code, wparam, (LPARAM)&cbtcwA );
331 cbtcwW->hwndInsertAfter = cbtcwA.hwndInsertAfter;
332 HeapFree( GetProcessHeap(), 0, nameA );
333 HeapFree( GetProcessHeap(), 0, classA );
335 return ret;
339 /***********************************************************************
340 * call_hook_proc
342 static LRESULT call_hook_proc( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam,
343 BOOL prev_unicode, BOOL next_unicode )
345 LRESULT ret;
347 TRACE_(relay)( "\1Call hook proc %p (id=%s,code=%x,wp=%08lx,lp=%08lx)\n",
348 proc, hook_names[id-WH_MINHOOK], code, wparam, lparam );
350 if (!prev_unicode == !next_unicode) ret = proc( code, wparam, lparam );
351 else if (prev_unicode) ret = call_hook_WtoA( proc, id, code, wparam, lparam );
352 else ret = call_hook_AtoW( proc, id, code, wparam, lparam );
354 TRACE_(relay)( "\1Ret hook proc %p (id=%s,code=%x,wp=%08lx,lp=%08lx) retval=%08lx\n",
355 proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, ret );
357 return ret;
361 /***********************************************************************
362 * get_hook_proc
364 * Retrieve the hook procedure real value for a module-relative proc
366 void *get_hook_proc( void *proc, const WCHAR *module, HMODULE *free_module )
368 HMODULE mod;
370 GetModuleHandleExW( 0, module, &mod );
371 *free_module = mod;
372 if (!mod)
374 TRACE( "loading %s\n", debugstr_w(module) );
375 /* FIXME: the library will never be freed */
376 if (!(mod = LoadLibraryExW(module, NULL, LOAD_WITH_ALTERED_SEARCH_PATH))) return NULL;
378 return (char *)mod + (ULONG_PTR)proc;
381 /***********************************************************************
382 * call_hook
384 * Call hook either in current thread or send message to the destination
385 * thread.
387 static LRESULT call_hook( struct hook_info *info, INT code, WPARAM wparam, LPARAM lparam )
389 DWORD_PTR ret = 0;
391 if (info->tid)
393 struct hook_extra_info h_extra;
394 h_extra.handle = info->handle;
395 h_extra.lparam = lparam;
397 TRACE( "calling hook in thread %04x %s code %x wp %lx lp %lx\n",
398 info->tid, hook_names[info->id-WH_MINHOOK], code, wparam, lparam );
400 switch(info->id)
402 case WH_KEYBOARD_LL:
403 MSG_SendInternalMessageTimeout( info->pid, info->tid, WM_WINE_KEYBOARD_LL_HOOK,
404 wparam, (LPARAM)&h_extra, SMTO_ABORTIFHUNG,
405 get_ll_hook_timeout(), &ret );
406 break;
407 case WH_MOUSE_LL:
408 MSG_SendInternalMessageTimeout( info->pid, info->tid, WM_WINE_MOUSE_LL_HOOK,
409 wparam, (LPARAM)&h_extra, SMTO_ABORTIFHUNG,
410 get_ll_hook_timeout(), &ret );
411 break;
412 default:
413 ERR("Unknown hook id %d\n", info->id);
414 assert(0);
415 break;
418 else if (info->proc)
420 struct user_thread_info *thread_info = get_user_thread_info();
421 HMODULE free_module = 0;
424 * Windows protects from stack overflow in recursive hook calls. Different Windows
425 * allow different depths.
427 if (thread_info->hook_call_depth >= 25)
429 WARN("Too many hooks called recursively, skipping call.\n");
430 return 0;
433 TRACE( "calling hook %p %s code %x wp %lx lp %lx module %s\n",
434 info->proc, hook_names[info->id-WH_MINHOOK], code, wparam,
435 lparam, debugstr_w(info->module) );
437 if (!info->module[0] ||
438 (info->proc = get_hook_proc( info->proc, info->module, &free_module )) != NULL)
440 HHOOK prev = thread_info->hook;
441 BOOL prev_unicode = thread_info->hook_unicode;
443 thread_info->hook = info->handle;
444 thread_info->hook_unicode = info->next_unicode;
445 thread_info->hook_call_depth++;
446 ret = call_hook_proc( info->proc, info->id, code, wparam, lparam,
447 info->prev_unicode, info->next_unicode );
448 thread_info->hook = prev;
449 thread_info->hook_unicode = prev_unicode;
450 thread_info->hook_call_depth--;
452 if (free_module) FreeLibrary(free_module);
456 if (info->id == WH_KEYBOARD_LL || info->id == WH_MOUSE_LL)
457 InterlockedIncrement( &global_key_state_counter ); /* force refreshing the key state cache */
459 return ret;
463 /***********************************************************************
464 * HOOK_IsHooked
466 static BOOL HOOK_IsHooked( INT id )
468 struct user_thread_info *thread_info = get_user_thread_info();
470 if (!thread_info->active_hooks) return TRUE;
471 return (thread_info->active_hooks & (1 << (id - WH_MINHOOK))) != 0;
475 /***********************************************************************
476 * HOOK_CallHooks
478 LRESULT HOOK_CallHooks( INT id, INT code, WPARAM wparam, LPARAM lparam, BOOL unicode )
480 struct user_thread_info *thread_info = get_user_thread_info();
481 struct hook_info info;
482 DWORD_PTR ret;
484 USER_CheckNotLock();
486 if (!HOOK_IsHooked( id ))
488 TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
489 return 0;
492 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
493 info.prev_unicode = unicode;
494 info.id = id;
496 SERVER_START_REQ( start_hook_chain )
498 req->id = info.id;
499 req->event = EVENT_MIN;
500 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
501 if (!wine_server_call( req ))
503 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
504 info.handle = wine_server_ptr_handle( reply->handle );
505 info.pid = reply->pid;
506 info.tid = reply->tid;
507 info.proc = wine_server_get_ptr( reply->proc );
508 info.next_unicode = reply->unicode;
509 thread_info->active_hooks = reply->active_hooks;
512 SERVER_END_REQ;
514 if (!info.tid && !info.proc) return 0;
515 ret = call_hook( &info, code, wparam, lparam );
517 SERVER_START_REQ( finish_hook_chain )
519 req->id = id;
520 wine_server_call( req );
522 SERVER_END_REQ;
523 return ret;
527 /***********************************************************************
528 * SetWindowsHookA (USER32.@)
530 HHOOK WINAPI SetWindowsHookA( INT id, HOOKPROC proc )
532 return SetWindowsHookExA( id, proc, 0, GetCurrentThreadId() );
536 /***********************************************************************
537 * SetWindowsHookW (USER32.@)
539 HHOOK WINAPI SetWindowsHookW( INT id, HOOKPROC proc )
541 return SetWindowsHookExW( id, proc, 0, GetCurrentThreadId() );
545 /***********************************************************************
546 * SetWindowsHookExA (USER32.@)
548 HHOOK WINAPI SetWindowsHookExA( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
550 return set_windows_hook( id, proc, inst, tid, FALSE );
553 /***********************************************************************
554 * SetWindowsHookExW (USER32.@)
556 HHOOK WINAPI SetWindowsHookExW( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
558 return set_windows_hook( id, proc, inst, tid, TRUE );
562 /***********************************************************************
563 * UnhookWindowsHook (USER32.@)
565 BOOL WINAPI UnhookWindowsHook( INT id, HOOKPROC proc )
567 BOOL ret;
569 TRACE( "%s %p\n", hook_names[id-WH_MINHOOK], proc );
571 SERVER_START_REQ( remove_hook )
573 req->handle = 0;
574 req->id = id;
575 req->proc = wine_server_client_ptr( proc );
576 ret = !wine_server_call_err( req );
577 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
579 SERVER_END_REQ;
580 if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
581 return ret;
586 /***********************************************************************
587 * UnhookWindowsHookEx (USER32.@)
589 BOOL WINAPI UnhookWindowsHookEx( HHOOK hhook )
591 BOOL ret;
593 SERVER_START_REQ( remove_hook )
595 req->handle = wine_server_user_handle( hhook );
596 req->id = 0;
597 ret = !wine_server_call_err( req );
598 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
600 SERVER_END_REQ;
601 if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
602 return ret;
606 /***********************************************************************
607 * CallNextHookEx (USER32.@)
609 LRESULT WINAPI CallNextHookEx( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
611 struct user_thread_info *thread_info = get_user_thread_info();
612 struct hook_info info;
614 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
616 SERVER_START_REQ( get_hook_info )
618 req->handle = wine_server_user_handle( thread_info->hook );
619 req->get_next = 1;
620 req->event = EVENT_MIN;
621 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
622 if (!wine_server_call_err( req ))
624 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
625 info.handle = wine_server_ptr_handle( reply->handle );
626 info.id = reply->id;
627 info.pid = reply->pid;
628 info.tid = reply->tid;
629 info.proc = wine_server_get_ptr( reply->proc );
630 info.next_unicode = reply->unicode;
633 SERVER_END_REQ;
635 info.prev_unicode = thread_info->hook_unicode;
636 return call_hook( &info, code, wparam, lparam );
640 LRESULT call_current_hook( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
642 struct hook_info info;
644 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
646 SERVER_START_REQ( get_hook_info )
648 req->handle = wine_server_user_handle( hhook );
649 req->get_next = 0;
650 req->event = EVENT_MIN;
651 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
652 if (!wine_server_call_err( req ))
654 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
655 info.handle = wine_server_ptr_handle( reply->handle );
656 info.id = reply->id;
657 info.pid = reply->pid;
658 info.tid = reply->tid;
659 info.proc = wine_server_get_ptr( reply->proc );
660 info.next_unicode = reply->unicode;
663 SERVER_END_REQ;
665 info.prev_unicode = TRUE; /* assume Unicode for this function */
666 return call_hook( &info, code, wparam, lparam );
669 /***********************************************************************
670 * CallMsgFilterA (USER32.@)
672 BOOL WINAPI CallMsgFilterA( LPMSG msg, INT code )
674 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, FALSE )) return TRUE;
675 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, FALSE );
679 /***********************************************************************
680 * CallMsgFilterW (USER32.@)
682 BOOL WINAPI CallMsgFilterW( LPMSG msg, INT code )
684 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, TRUE )) return TRUE;
685 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, TRUE );
689 /***********************************************************************
690 * SetWinEventHook [USER32.@]
692 * Set up an event hook for a set of events.
694 * PARAMS
695 * event_min [I] Lowest event handled by pfnProc
696 * event_max [I] Highest event handled by pfnProc
697 * inst [I] DLL containing pfnProc
698 * proc [I] Callback event hook function
699 * pid [I] Process to get events from, or 0 for all processes
700 * tid [I] Thread to get events from, or 0 for all threads
701 * flags [I] Flags indicating the status of pfnProc
703 * RETURNS
704 * Success: A handle representing the hook.
705 * Failure: A NULL handle.
707 HWINEVENTHOOK WINAPI SetWinEventHook(DWORD event_min, DWORD event_max,
708 HMODULE inst, WINEVENTPROC proc,
709 DWORD pid, DWORD tid, DWORD flags)
711 HWINEVENTHOOK handle = 0;
712 WCHAR module[MAX_PATH];
713 DWORD len;
715 TRACE("%d,%d,%p,%p,%08x,%04x,%08x\n", event_min, event_max, inst,
716 proc, pid, tid, flags);
718 if (inst)
720 if (!(len = GetModuleFileNameW(inst, module, MAX_PATH)) || len >= MAX_PATH)
721 inst = 0;
724 if ((flags & WINEVENT_INCONTEXT) && !inst)
726 SetLastError(ERROR_HOOK_NEEDS_HMOD);
727 return 0;
730 if (event_min > event_max)
732 SetLastError(ERROR_INVALID_HOOK_FILTER);
733 return 0;
736 /* FIXME: what if the tid or pid belongs to another process? */
737 if (tid) /* thread-local hook */
738 inst = 0;
740 SERVER_START_REQ( set_hook )
742 req->id = WH_WINEVENT;
743 req->pid = pid;
744 req->tid = tid;
745 req->event_min = event_min;
746 req->event_max = event_max;
747 req->flags = flags;
748 req->unicode = 1;
749 if (inst) /* make proc relative to the module base */
751 req->proc = wine_server_client_ptr( (void *)((char *)proc - (char *)inst) );
752 wine_server_add_data( req, module, lstrlenW(module) * sizeof(WCHAR) );
754 else req->proc = wine_server_client_ptr( proc );
756 if (!wine_server_call_err( req ))
758 handle = wine_server_ptr_handle( reply->handle );
759 get_user_thread_info()->active_hooks = reply->active_hooks;
762 SERVER_END_REQ;
764 TRACE("-> %p\n", handle);
765 return handle;
769 /***********************************************************************
770 * UnhookWinEvent [USER32.@]
772 * Remove an event hook for a set of events.
774 * PARAMS
775 * hEventHook [I] Event hook to remove
777 * RETURNS
778 * Success: TRUE. The event hook has been removed.
779 * Failure: FALSE, if hEventHook is invalid.
781 BOOL WINAPI UnhookWinEvent(HWINEVENTHOOK hEventHook)
783 BOOL ret;
785 SERVER_START_REQ( remove_hook )
787 req->handle = wine_server_user_handle( hEventHook );
788 req->id = WH_WINEVENT;
789 ret = !wine_server_call_err( req );
790 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
792 SERVER_END_REQ;
793 return ret;
796 static inline BOOL find_first_hook(DWORD id, DWORD event, HWND hwnd, LONG object_id,
797 LONG child_id, struct hook_info *info)
799 struct user_thread_info *thread_info = get_user_thread_info();
800 BOOL ret;
802 if (!HOOK_IsHooked( id ))
804 TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
805 return FALSE;
808 SERVER_START_REQ( start_hook_chain )
810 req->id = id;
811 req->event = event;
812 req->window = wine_server_user_handle( hwnd );
813 req->object_id = object_id;
814 req->child_id = child_id;
815 wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
816 ret = !wine_server_call( req );
817 if (ret)
819 info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
820 info->handle = wine_server_ptr_handle( reply->handle );
821 info->proc = wine_server_get_ptr( reply->proc );
822 info->tid = reply->tid;
823 thread_info->active_hooks = reply->active_hooks;
826 SERVER_END_REQ;
827 return ret && (info->tid || info->proc);
830 static inline BOOL find_next_hook(DWORD event, HWND hwnd, LONG object_id,
831 LONG child_id, struct hook_info *info)
833 BOOL ret;
835 SERVER_START_REQ( get_hook_info )
837 req->handle = wine_server_user_handle( info->handle );
838 req->get_next = 1;
839 req->event = event;
840 req->window = wine_server_user_handle( hwnd );
841 req->object_id = object_id;
842 req->child_id = child_id;
843 wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
844 ret = !wine_server_call( req );
845 if (ret)
847 info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
848 info->handle = wine_server_ptr_handle( reply->handle );
849 info->proc = wine_server_get_ptr( reply->proc );
850 info->tid = reply->tid;
853 SERVER_END_REQ;
854 return ret;
857 static inline void find_hook_close(DWORD id)
859 SERVER_START_REQ( finish_hook_chain )
861 req->id = id;
862 wine_server_call( req );
864 SERVER_END_REQ;
867 /***********************************************************************
868 * NotifyWinEvent [USER32.@]
870 * Inform the OS that an event has occurred.
872 * PARAMS
873 * event [I] Id of the event
874 * hwnd [I] Window holding the object that created the event
875 * object_id [I] Type of object that created the event
876 * child_id [I] Child object of nId, or CHILDID_SELF.
878 * RETURNS
879 * Nothing.
881 void WINAPI NotifyWinEvent(DWORD event, HWND hwnd, LONG object_id, LONG child_id)
883 struct hook_info info;
885 TRACE("%04x,%p,%d,%d\n", event, hwnd, object_id, child_id);
887 if (!hwnd)
889 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
890 return;
893 USER_CheckNotLock();
895 #if 0
896 if (event & 0x80000000)
898 /* FIXME: on 64-bit platforms we need to invent some other way for
899 * passing parameters, nId and nChildId can't hold full [W|L]PARAM.
900 * struct call_hook *hook = (LRESULT *)hWnd;
901 * wparam = hook->wparam;
902 * lparam = hook->lparam;
904 LRESULT *ret = (LRESULT *)hwnd;
905 INT id, code, unicode;
907 id = (dwEvent & 0x7fff0000) >> 16;
908 code = event & 0x7fff;
909 unicode = event & 0x8000;
910 *ret = HOOK_CallHooks(id, code, object_id, child_id, unicode);
911 return;
913 #endif
915 if (!find_first_hook(WH_WINEVENT, event, hwnd, object_id, child_id, &info)) return;
919 WINEVENTPROC proc = info.proc;
920 if (proc)
922 HMODULE free_module = 0;
923 TRACE( "calling WH_WINEVENT hook %p event %x hwnd %p %x %x module %s\n",
924 proc, event, hwnd, object_id, child_id, debugstr_w(info.module) );
926 if (!info.module[0] || (proc = get_hook_proc( proc, info.module, &free_module )) != NULL)
928 TRACE_(relay)( "\1Call winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
929 proc, info.handle, event, hwnd, object_id,
930 child_id, GetCurrentThreadId(), GetCurrentTime());
932 proc( info.handle, event, hwnd, object_id, child_id,
933 GetCurrentThreadId(), GetCurrentTime());
935 TRACE_(relay)( "\1Ret winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
936 proc, info.handle, event, hwnd, object_id,
937 child_id, GetCurrentThreadId(), GetCurrentTime());
939 if (free_module) FreeLibrary(free_module);
942 else
943 break;
945 while (find_next_hook(event, hwnd, object_id, child_id, &info));
947 find_hook_close(WH_WINEVENT);
951 /***********************************************************************
952 * IsWinEventHookInstalled [USER32.@]
954 * Determine if an event hook is installed for an event.
956 * PARAMS
957 * dwEvent [I] Id of the event
959 * RETURNS
960 * TRUE, If there are any hooks installed for the event.
961 * FALSE, Otherwise.
963 * BUGS
964 * Not implemented.
966 BOOL WINAPI IsWinEventHookInstalled(DWORD dwEvent)
968 /* FIXME: Needed by Office 2007 installer */
969 WARN("(%d)-stub!\n", dwEvent);
970 return TRUE;
973 /* Undocumented RegisterUserApiHook() */
974 BOOL WINAPI RegisterUserApiHook(const struct user_api_hook *new, struct user_api_hook *old)
976 if (!new)
977 return FALSE;
979 USER_Lock();
980 hooked_user_api = *new;
981 user_api = &hooked_user_api;
982 if (old)
983 *old = original_user_api;
984 USER_Unlock();
985 return TRUE;
988 /* Undocumented UnregisterUserApiHook() */
989 void WINAPI UnregisterUserApiHook(void)
991 InterlockedExchangePointer((void **)&user_api, &original_user_api);