mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / user32 / hook.c
blob4c6dbcf7202c293fef60816b6e2678b615ac91e5
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 struct hook_info
86 INT id;
87 void *proc;
88 void *handle;
89 DWORD pid, tid;
90 BOOL prev_unicode, next_unicode;
91 WCHAR module[MAX_PATH];
94 #define WH_WINEVENT (WH_MAXHOOK+1)
96 static const char * const hook_names[WH_WINEVENT - WH_MINHOOK + 1] =
98 "WH_MSGFILTER",
99 "WH_JOURNALRECORD",
100 "WH_JOURNALPLAYBACK",
101 "WH_KEYBOARD",
102 "WH_GETMESSAGE",
103 "WH_CALLWNDPROC",
104 "WH_CBT",
105 "WH_SYSMSGFILTER",
106 "WH_MOUSE",
107 "WH_HARDWARE",
108 "WH_DEBUG",
109 "WH_SHELL",
110 "WH_FOREGROUNDIDLE",
111 "WH_CALLWNDPROCRET",
112 "WH_KEYBOARD_LL",
113 "WH_MOUSE_LL",
114 "WH_WINEVENT"
118 /***********************************************************************
119 * get_ll_hook_timeout
122 static UINT get_ll_hook_timeout(void)
124 /* FIXME: should retrieve LowLevelHooksTimeout in HKEY_CURRENT_USER\Control Panel\Desktop */
125 return 2000;
129 /***********************************************************************
130 * set_windows_hook
132 * Implementation of SetWindowsHookExA and SetWindowsHookExW.
134 static HHOOK set_windows_hook( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid, BOOL unicode )
136 HHOOK handle = 0;
137 WCHAR module[MAX_PATH];
138 DWORD len;
140 if (!proc)
142 SetLastError( ERROR_INVALID_FILTER_PROC );
143 return 0;
146 if (tid) /* thread-local hook */
148 if (id == WH_JOURNALRECORD ||
149 id == WH_JOURNALPLAYBACK ||
150 id == WH_KEYBOARD_LL ||
151 id == WH_MOUSE_LL ||
152 id == WH_SYSMSGFILTER)
154 /* these can only be global */
155 SetLastError( ERROR_INVALID_PARAMETER );
156 return 0;
159 else /* system-global hook */
161 if (id == WH_KEYBOARD_LL || id == WH_MOUSE_LL) inst = 0;
162 else if (!inst)
164 SetLastError( ERROR_HOOK_NEEDS_HMOD );
165 return 0;
169 if (inst && (!(len = GetModuleFileNameW( inst, module, MAX_PATH )) || len >= MAX_PATH))
171 SetLastError( ERROR_INVALID_PARAMETER );
172 return 0;
175 SERVER_START_REQ( set_hook )
177 req->id = id;
178 req->pid = 0;
179 req->tid = tid;
180 req->event_min = EVENT_MIN;
181 req->event_max = EVENT_MAX;
182 req->flags = WINEVENT_INCONTEXT;
183 req->unicode = unicode;
184 if (inst) /* make proc relative to the module base */
186 req->proc = wine_server_client_ptr( (void *)((char *)proc - (char *)inst) );
187 wine_server_add_data( req, module, lstrlenW(module) * sizeof(WCHAR) );
189 else req->proc = wine_server_client_ptr( proc );
191 if (!wine_server_call_err( req ))
193 handle = wine_server_ptr_handle( reply->handle );
194 get_user_thread_info()->active_hooks = reply->active_hooks;
197 SERVER_END_REQ;
199 TRACE( "%s %p %x -> %p\n", hook_names[id-WH_MINHOOK], proc, tid, handle );
200 return handle;
203 #ifdef __i386__
204 /* Some apps pass a non-stdcall proc to SetWindowsHookExA,
205 * so we need a small assembly wrapper to call the proc.
207 extern LRESULT HOOKPROC_wrapper( HOOKPROC proc,
208 INT code, WPARAM wParam, LPARAM lParam );
209 __ASM_GLOBAL_FUNC( HOOKPROC_wrapper,
210 "pushl %ebp\n\t"
211 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
212 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
213 "movl %esp,%ebp\n\t"
214 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
215 "pushl %edi\n\t"
216 __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
217 "pushl %esi\n\t"
218 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
219 "pushl %ebx\n\t"
220 __ASM_CFI(".cfi_rel_offset %ebx,-12\n\t")
221 "pushl 20(%ebp)\n\t"
222 "pushl 16(%ebp)\n\t"
223 "pushl 12(%ebp)\n\t"
224 "movl 8(%ebp),%eax\n\t"
225 "call *%eax\n\t"
226 "leal -12(%ebp),%esp\n\t"
227 "popl %ebx\n\t"
228 __ASM_CFI(".cfi_same_value %ebx\n\t")
229 "popl %esi\n\t"
230 __ASM_CFI(".cfi_same_value %esi\n\t")
231 "popl %edi\n\t"
232 __ASM_CFI(".cfi_same_value %edi\n\t")
233 "leave\n\t"
234 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
235 __ASM_CFI(".cfi_same_value %ebp\n\t")
236 "ret" )
237 #else
238 static inline LRESULT HOOKPROC_wrapper( HOOKPROC proc,
239 INT code, WPARAM wParam, LPARAM lParam )
241 return proc( code, wParam, lParam );
243 #endif /* __i386__ */
246 /***********************************************************************
247 * call_hook_AtoW
249 static LRESULT call_hook_AtoW( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
251 LRESULT ret;
252 UNICODE_STRING usBuffer;
253 if (id != WH_CBT || code != HCBT_CREATEWND)
254 ret = HOOKPROC_wrapper( proc, code, wparam, lparam );
255 else
257 CBT_CREATEWNDA *cbtcwA = (CBT_CREATEWNDA *)lparam;
258 CBT_CREATEWNDW cbtcwW;
259 CREATESTRUCTW csW;
260 LPWSTR nameW = NULL;
261 LPWSTR classW = NULL;
263 cbtcwW.lpcs = &csW;
264 cbtcwW.hwndInsertAfter = cbtcwA->hwndInsertAfter;
265 csW = *(CREATESTRUCTW *)cbtcwA->lpcs;
267 if (!IS_INTRESOURCE(cbtcwA->lpcs->lpszName))
269 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszName);
270 csW.lpszName = nameW = usBuffer.Buffer;
272 if (!IS_INTRESOURCE(cbtcwA->lpcs->lpszClass))
274 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszClass);
275 csW.lpszClass = classW = usBuffer.Buffer;
277 ret = HOOKPROC_wrapper( proc, code, wparam, (LPARAM)&cbtcwW );
278 cbtcwA->hwndInsertAfter = cbtcwW.hwndInsertAfter;
279 HeapFree( GetProcessHeap(), 0, nameW );
280 HeapFree( GetProcessHeap(), 0, classW );
282 return ret;
286 /***********************************************************************
287 * call_hook_WtoA
289 static LRESULT call_hook_WtoA( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
291 LRESULT ret;
293 if (id != WH_CBT || code != HCBT_CREATEWND)
294 ret = HOOKPROC_wrapper( proc, code, wparam, lparam );
295 else
297 CBT_CREATEWNDW *cbtcwW = (CBT_CREATEWNDW *)lparam;
298 CBT_CREATEWNDA cbtcwA;
299 CREATESTRUCTA csA;
300 int len;
301 LPSTR nameA = NULL;
302 LPSTR classA = NULL;
304 cbtcwA.lpcs = &csA;
305 cbtcwA.hwndInsertAfter = cbtcwW->hwndInsertAfter;
306 csA = *(CREATESTRUCTA *)cbtcwW->lpcs;
308 if (!IS_INTRESOURCE(cbtcwW->lpcs->lpszName)) {
309 len = WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszName, -1, NULL, 0, NULL, NULL );
310 nameA = HeapAlloc( GetProcessHeap(), 0, len*sizeof(CHAR) );
311 WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszName, -1, nameA, len, NULL, NULL );
312 csA.lpszName = nameA;
315 if (!IS_INTRESOURCE(cbtcwW->lpcs->lpszClass)) {
316 len = WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszClass, -1, NULL, 0, NULL, NULL );
317 classA = HeapAlloc( GetProcessHeap(), 0, len*sizeof(CHAR) );
318 WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszClass, -1, classA, len, NULL, NULL );
319 csA.lpszClass = classA;
322 ret = HOOKPROC_wrapper( proc, code, wparam, (LPARAM)&cbtcwA );
323 cbtcwW->hwndInsertAfter = cbtcwA.hwndInsertAfter;
324 HeapFree( GetProcessHeap(), 0, nameA );
325 HeapFree( GetProcessHeap(), 0, classA );
327 return ret;
331 /***********************************************************************
332 * call_hook_proc
334 static LRESULT call_hook_proc( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam,
335 BOOL prev_unicode, BOOL next_unicode )
337 LRESULT ret;
339 TRACE_(relay)( "\1Call hook proc %p (id=%s,code=%x,wp=%08lx,lp=%08lx)\n",
340 proc, hook_names[id-WH_MINHOOK], code, wparam, lparam );
342 if (!prev_unicode == !next_unicode) ret = proc( code, wparam, lparam );
343 else if (prev_unicode) ret = call_hook_WtoA( proc, id, code, wparam, lparam );
344 else ret = call_hook_AtoW( proc, id, code, wparam, lparam );
346 TRACE_(relay)( "\1Ret hook proc %p (id=%s,code=%x,wp=%08lx,lp=%08lx) retval=%08lx\n",
347 proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, ret );
349 return ret;
353 /***********************************************************************
354 * get_hook_proc
356 * Retrieve the hook procedure real value for a module-relative proc
358 void *get_hook_proc( void *proc, const WCHAR *module, HMODULE *free_module )
360 HMODULE mod;
362 GetModuleHandleExW( 0, module, &mod );
363 *free_module = mod;
364 if (!mod)
366 TRACE( "loading %s\n", debugstr_w(module) );
367 /* FIXME: the library will never be freed */
368 if (!(mod = LoadLibraryExW(module, NULL, LOAD_WITH_ALTERED_SEARCH_PATH))) return NULL;
370 return (char *)mod + (ULONG_PTR)proc;
373 /***********************************************************************
374 * call_hook
376 * Call hook either in current thread or send message to the destination
377 * thread.
379 static LRESULT call_hook( struct hook_info *info, INT code, WPARAM wparam, LPARAM lparam )
381 DWORD_PTR ret = 0;
383 if (info->tid)
385 struct hook_extra_info h_extra;
386 h_extra.handle = info->handle;
387 h_extra.lparam = lparam;
389 TRACE( "calling hook in thread %04x %s code %x wp %lx lp %lx\n",
390 info->tid, hook_names[info->id-WH_MINHOOK], code, wparam, lparam );
392 switch(info->id)
394 case WH_KEYBOARD_LL:
395 MSG_SendInternalMessageTimeout( info->pid, info->tid, WM_WINE_KEYBOARD_LL_HOOK,
396 wparam, (LPARAM)&h_extra, SMTO_ABORTIFHUNG,
397 get_ll_hook_timeout(), &ret );
398 break;
399 case WH_MOUSE_LL:
400 MSG_SendInternalMessageTimeout( info->pid, info->tid, WM_WINE_MOUSE_LL_HOOK,
401 wparam, (LPARAM)&h_extra, SMTO_ABORTIFHUNG,
402 get_ll_hook_timeout(), &ret );
403 break;
404 default:
405 ERR("Unknown hook id %d\n", info->id);
406 assert(0);
407 break;
410 else if (info->proc)
412 struct user_thread_info *thread_info = get_user_thread_info();
413 HMODULE free_module = 0;
416 * Windows protects from stack overflow in recursive hook calls. Different Windows
417 * allow different depths.
419 if (thread_info->hook_call_depth >= 25)
421 WARN("Too many hooks called recursively, skipping call.\n");
422 return 0;
425 TRACE( "calling hook %p %s code %x wp %lx lp %lx module %s\n",
426 info->proc, hook_names[info->id-WH_MINHOOK], code, wparam,
427 lparam, debugstr_w(info->module) );
429 if (!info->module[0] ||
430 (info->proc = get_hook_proc( info->proc, info->module, &free_module )) != NULL)
432 HHOOK prev = thread_info->hook;
433 BOOL prev_unicode = thread_info->hook_unicode;
435 thread_info->hook = info->handle;
436 thread_info->hook_unicode = info->next_unicode;
437 thread_info->hook_call_depth++;
438 ret = call_hook_proc( info->proc, info->id, code, wparam, lparam,
439 info->prev_unicode, info->next_unicode );
440 thread_info->hook = prev;
441 thread_info->hook_unicode = prev_unicode;
442 thread_info->hook_call_depth--;
444 if (free_module) FreeLibrary(free_module);
448 if (info->id == WH_KEYBOARD_LL || info->id == WH_MOUSE_LL)
449 InterlockedIncrement( &global_key_state_counter ); /* force refreshing the key state cache */
451 return ret;
455 /***********************************************************************
456 * HOOK_IsHooked
458 static BOOL HOOK_IsHooked( INT id )
460 struct user_thread_info *thread_info = get_user_thread_info();
462 if (!thread_info->active_hooks) return TRUE;
463 return (thread_info->active_hooks & (1 << (id - WH_MINHOOK))) != 0;
467 /***********************************************************************
468 * HOOK_CallHooks
470 LRESULT HOOK_CallHooks( INT id, INT code, WPARAM wparam, LPARAM lparam, BOOL unicode )
472 struct user_thread_info *thread_info = get_user_thread_info();
473 struct hook_info info;
474 DWORD_PTR ret;
476 USER_CheckNotLock();
478 if (!HOOK_IsHooked( id ))
480 TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
481 return 0;
484 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
485 info.prev_unicode = unicode;
486 info.id = id;
488 SERVER_START_REQ( start_hook_chain )
490 req->id = info.id;
491 req->event = EVENT_MIN;
492 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
493 if (!wine_server_call( req ))
495 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
496 info.handle = wine_server_ptr_handle( reply->handle );
497 info.pid = reply->pid;
498 info.tid = reply->tid;
499 info.proc = wine_server_get_ptr( reply->proc );
500 info.next_unicode = reply->unicode;
501 thread_info->active_hooks = reply->active_hooks;
504 SERVER_END_REQ;
506 if (!info.tid && !info.proc) return 0;
507 ret = call_hook( &info, code, wparam, lparam );
509 SERVER_START_REQ( finish_hook_chain )
511 req->id = id;
512 wine_server_call( req );
514 SERVER_END_REQ;
515 return ret;
519 /***********************************************************************
520 * SetWindowsHookA (USER32.@)
522 HHOOK WINAPI SetWindowsHookA( INT id, HOOKPROC proc )
524 return SetWindowsHookExA( id, proc, 0, GetCurrentThreadId() );
528 /***********************************************************************
529 * SetWindowsHookW (USER32.@)
531 HHOOK WINAPI SetWindowsHookW( INT id, HOOKPROC proc )
533 return SetWindowsHookExW( id, proc, 0, GetCurrentThreadId() );
537 /***********************************************************************
538 * SetWindowsHookExA (USER32.@)
540 HHOOK WINAPI SetWindowsHookExA( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
542 return set_windows_hook( id, proc, inst, tid, FALSE );
545 /***********************************************************************
546 * SetWindowsHookExW (USER32.@)
548 HHOOK WINAPI SetWindowsHookExW( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
550 return set_windows_hook( id, proc, inst, tid, TRUE );
554 /***********************************************************************
555 * UnhookWindowsHook (USER32.@)
557 BOOL WINAPI UnhookWindowsHook( INT id, HOOKPROC proc )
559 BOOL ret;
561 TRACE( "%s %p\n", hook_names[id-WH_MINHOOK], proc );
563 SERVER_START_REQ( remove_hook )
565 req->handle = 0;
566 req->id = id;
567 req->proc = wine_server_client_ptr( proc );
568 ret = !wine_server_call_err( req );
569 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
571 SERVER_END_REQ;
572 if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
573 return ret;
578 /***********************************************************************
579 * UnhookWindowsHookEx (USER32.@)
581 BOOL WINAPI UnhookWindowsHookEx( HHOOK hhook )
583 BOOL ret;
585 SERVER_START_REQ( remove_hook )
587 req->handle = wine_server_user_handle( hhook );
588 req->id = 0;
589 ret = !wine_server_call_err( req );
590 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
592 SERVER_END_REQ;
593 if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
594 return ret;
598 /***********************************************************************
599 * CallNextHookEx (USER32.@)
601 LRESULT WINAPI CallNextHookEx( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
603 struct user_thread_info *thread_info = get_user_thread_info();
604 struct hook_info info;
606 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
608 SERVER_START_REQ( get_hook_info )
610 req->handle = wine_server_user_handle( thread_info->hook );
611 req->get_next = 1;
612 req->event = EVENT_MIN;
613 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
614 if (!wine_server_call_err( req ))
616 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
617 info.handle = wine_server_ptr_handle( reply->handle );
618 info.id = reply->id;
619 info.pid = reply->pid;
620 info.tid = reply->tid;
621 info.proc = wine_server_get_ptr( reply->proc );
622 info.next_unicode = reply->unicode;
625 SERVER_END_REQ;
627 info.prev_unicode = thread_info->hook_unicode;
628 return call_hook( &info, code, wparam, lparam );
632 LRESULT call_current_hook( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
634 struct hook_info info;
636 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
638 SERVER_START_REQ( get_hook_info )
640 req->handle = wine_server_user_handle( hhook );
641 req->get_next = 0;
642 req->event = EVENT_MIN;
643 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
644 if (!wine_server_call_err( req ))
646 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
647 info.handle = wine_server_ptr_handle( reply->handle );
648 info.id = reply->id;
649 info.pid = reply->pid;
650 info.tid = reply->tid;
651 info.proc = wine_server_get_ptr( reply->proc );
652 info.next_unicode = reply->unicode;
655 SERVER_END_REQ;
657 info.prev_unicode = TRUE; /* assume Unicode for this function */
658 return call_hook( &info, code, wparam, lparam );
661 /***********************************************************************
662 * CallMsgFilterA (USER32.@)
664 BOOL WINAPI CallMsgFilterA( LPMSG msg, INT code )
666 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, FALSE )) return TRUE;
667 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, FALSE );
671 /***********************************************************************
672 * CallMsgFilterW (USER32.@)
674 BOOL WINAPI CallMsgFilterW( LPMSG msg, INT code )
676 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, TRUE )) return TRUE;
677 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, TRUE );
681 /***********************************************************************
682 * SetWinEventHook [USER32.@]
684 * Set up an event hook for a set of events.
686 * PARAMS
687 * event_min [I] Lowest event handled by pfnProc
688 * event_max [I] Highest event handled by pfnProc
689 * inst [I] DLL containing pfnProc
690 * proc [I] Callback event hook function
691 * pid [I] Process to get events from, or 0 for all processes
692 * tid [I] Thread to get events from, or 0 for all threads
693 * flags [I] Flags indicating the status of pfnProc
695 * RETURNS
696 * Success: A handle representing the hook.
697 * Failure: A NULL handle.
699 HWINEVENTHOOK WINAPI SetWinEventHook(DWORD event_min, DWORD event_max,
700 HMODULE inst, WINEVENTPROC proc,
701 DWORD pid, DWORD tid, DWORD flags)
703 HWINEVENTHOOK handle = 0;
704 WCHAR module[MAX_PATH];
705 DWORD len;
707 TRACE("%d,%d,%p,%p,%08x,%04x,%08x\n", event_min, event_max, inst,
708 proc, pid, tid, flags);
710 if (inst)
712 if (!(len = GetModuleFileNameW(inst, module, MAX_PATH)) || len >= MAX_PATH)
713 inst = 0;
716 if ((flags & WINEVENT_INCONTEXT) && !inst)
718 SetLastError(ERROR_HOOK_NEEDS_HMOD);
719 return 0;
722 if (event_min > event_max)
724 SetLastError(ERROR_INVALID_HOOK_FILTER);
725 return 0;
728 /* FIXME: what if the tid or pid belongs to another process? */
729 if (tid) /* thread-local hook */
730 inst = 0;
732 SERVER_START_REQ( set_hook )
734 req->id = WH_WINEVENT;
735 req->pid = pid;
736 req->tid = tid;
737 req->event_min = event_min;
738 req->event_max = event_max;
739 req->flags = flags;
740 req->unicode = 1;
741 if (inst) /* make proc relative to the module base */
743 req->proc = wine_server_client_ptr( (void *)((char *)proc - (char *)inst) );
744 wine_server_add_data( req, module, lstrlenW(module) * sizeof(WCHAR) );
746 else req->proc = wine_server_client_ptr( proc );
748 if (!wine_server_call_err( req ))
750 handle = wine_server_ptr_handle( reply->handle );
751 get_user_thread_info()->active_hooks = reply->active_hooks;
754 SERVER_END_REQ;
756 TRACE("-> %p\n", handle);
757 return handle;
761 /***********************************************************************
762 * UnhookWinEvent [USER32.@]
764 * Remove an event hook for a set of events.
766 * PARAMS
767 * hEventHook [I] Event hook to remove
769 * RETURNS
770 * Success: TRUE. The event hook has been removed.
771 * Failure: FALSE, if hEventHook is invalid.
773 BOOL WINAPI UnhookWinEvent(HWINEVENTHOOK hEventHook)
775 BOOL ret;
777 SERVER_START_REQ( remove_hook )
779 req->handle = wine_server_user_handle( hEventHook );
780 req->id = WH_WINEVENT;
781 ret = !wine_server_call_err( req );
782 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
784 SERVER_END_REQ;
785 return ret;
788 static inline BOOL find_first_hook(DWORD id, DWORD event, HWND hwnd, LONG object_id,
789 LONG child_id, struct hook_info *info)
791 struct user_thread_info *thread_info = get_user_thread_info();
792 BOOL ret;
794 if (!HOOK_IsHooked( id ))
796 TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
797 return FALSE;
800 SERVER_START_REQ( start_hook_chain )
802 req->id = id;
803 req->event = event;
804 req->window = wine_server_user_handle( hwnd );
805 req->object_id = object_id;
806 req->child_id = child_id;
807 wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
808 ret = !wine_server_call( req );
809 if (ret)
811 info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
812 info->handle = wine_server_ptr_handle( reply->handle );
813 info->proc = wine_server_get_ptr( reply->proc );
814 info->tid = reply->tid;
815 thread_info->active_hooks = reply->active_hooks;
818 SERVER_END_REQ;
819 return ret && (info->tid || info->proc);
822 static inline BOOL find_next_hook(DWORD event, HWND hwnd, LONG object_id,
823 LONG child_id, struct hook_info *info)
825 BOOL ret;
827 SERVER_START_REQ( get_hook_info )
829 req->handle = wine_server_user_handle( info->handle );
830 req->get_next = 1;
831 req->event = event;
832 req->window = wine_server_user_handle( hwnd );
833 req->object_id = object_id;
834 req->child_id = child_id;
835 wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
836 ret = !wine_server_call( req );
837 if (ret)
839 info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
840 info->handle = wine_server_ptr_handle( reply->handle );
841 info->proc = wine_server_get_ptr( reply->proc );
842 info->tid = reply->tid;
845 SERVER_END_REQ;
846 return ret;
849 static inline void find_hook_close(DWORD id)
851 SERVER_START_REQ( finish_hook_chain )
853 req->id = id;
854 wine_server_call( req );
856 SERVER_END_REQ;
859 /***********************************************************************
860 * NotifyWinEvent [USER32.@]
862 * Inform the OS that an event has occurred.
864 * PARAMS
865 * event [I] Id of the event
866 * hwnd [I] Window holding the object that created the event
867 * object_id [I] Type of object that created the event
868 * child_id [I] Child object of nId, or CHILDID_SELF.
870 * RETURNS
871 * Nothing.
873 void WINAPI NotifyWinEvent(DWORD event, HWND hwnd, LONG object_id, LONG child_id)
875 struct hook_info info;
877 TRACE("%04x,%p,%d,%d\n", event, hwnd, object_id, child_id);
879 if (!hwnd)
881 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
882 return;
885 USER_CheckNotLock();
887 #if 0
888 if (event & 0x80000000)
890 /* FIXME: on 64-bit platforms we need to invent some other way for
891 * passing parameters, nId and nChildId can't hold full [W|L]PARAM.
892 * struct call_hook *hook = (LRESULT *)hWnd;
893 * wparam = hook->wparam;
894 * lparam = hook->lparam;
896 LRESULT *ret = (LRESULT *)hwnd;
897 INT id, code, unicode;
899 id = (dwEvent & 0x7fff0000) >> 16;
900 code = event & 0x7fff;
901 unicode = event & 0x8000;
902 *ret = HOOK_CallHooks(id, code, object_id, child_id, unicode);
903 return;
905 #endif
907 if (!find_first_hook(WH_WINEVENT, event, hwnd, object_id, child_id, &info)) return;
911 WINEVENTPROC proc = info.proc;
912 if (proc)
914 HMODULE free_module = 0;
915 TRACE( "calling WH_WINEVENT hook %p event %x hwnd %p %x %x module %s\n",
916 proc, event, hwnd, object_id, child_id, debugstr_w(info.module) );
918 if (!info.module[0] || (proc = get_hook_proc( proc, info.module, &free_module )) != NULL)
920 TRACE_(relay)( "\1Call winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
921 proc, info.handle, event, hwnd, object_id,
922 child_id, GetCurrentThreadId(), GetCurrentTime());
924 proc( info.handle, event, hwnd, object_id, child_id,
925 GetCurrentThreadId(), GetCurrentTime());
927 TRACE_(relay)( "\1Ret winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
928 proc, info.handle, event, hwnd, object_id,
929 child_id, GetCurrentThreadId(), GetCurrentTime());
931 if (free_module) FreeLibrary(free_module);
934 else
935 break;
937 while (find_next_hook(event, hwnd, object_id, child_id, &info));
939 find_hook_close(WH_WINEVENT);
943 /***********************************************************************
944 * IsWinEventHookInstalled [USER32.@]
946 * Determine if an event hook is installed for an event.
948 * PARAMS
949 * dwEvent [I] Id of the event
951 * RETURNS
952 * TRUE, If there are any hooks installed for the event.
953 * FALSE, Otherwise.
955 * BUGS
956 * Not implemented.
958 BOOL WINAPI IsWinEventHookInstalled(DWORD dwEvent)
960 /* FIXME: Needed by Office 2007 installer */
961 WARN("(%d)-stub!\n", dwEvent);
962 return TRUE;