include: New ddk/hidtypes.h include file.
[wine.git] / dlls / user32 / hook.c
blob090ec17267c133a0f922d195ee4c90c152daa0c1
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 "config.h"
66 #include "wine/port.h"
68 #include <stdarg.h>
69 #include <assert.h>
71 #include "windef.h"
72 #include "winbase.h"
73 #include "wingdi.h"
74 #include "winuser.h"
75 #include "winerror.h"
76 #include "win.h"
77 #include "user_private.h"
78 #include "wine/server.h"
79 #include "wine/unicode.h"
80 #include "wine/debug.h"
81 #include "winternl.h"
83 WINE_DEFAULT_DEBUG_CHANNEL(hook);
84 WINE_DECLARE_DEBUG_CHANNEL(relay);
86 struct hook_info
88 INT id;
89 void *proc;
90 void *handle;
91 DWORD pid, tid;
92 BOOL prev_unicode, next_unicode;
93 WCHAR module[MAX_PATH];
96 #define WH_WINEVENT (WH_MAXHOOK+1)
98 static const char * const hook_names[WH_WINEVENT - WH_MINHOOK + 1] =
100 "WH_MSGFILTER",
101 "WH_JOURNALRECORD",
102 "WH_JOURNALPLAYBACK",
103 "WH_KEYBOARD",
104 "WH_GETMESSAGE",
105 "WH_CALLWNDPROC",
106 "WH_CBT",
107 "WH_SYSMSGFILTER",
108 "WH_MOUSE",
109 "WH_HARDWARE",
110 "WH_DEBUG",
111 "WH_SHELL",
112 "WH_FOREGROUNDIDLE",
113 "WH_CALLWNDPROCRET",
114 "WH_KEYBOARD_LL",
115 "WH_MOUSE_LL",
116 "WH_WINEVENT"
120 /***********************************************************************
121 * get_ll_hook_timeout
124 static UINT get_ll_hook_timeout(void)
126 /* FIXME: should retrieve LowLevelHooksTimeout in HKEY_CURRENT_USER\Control Panel\Desktop */
127 return 2000;
131 /***********************************************************************
132 * set_windows_hook
134 * Implementation of SetWindowsHookExA and SetWindowsHookExW.
136 static HHOOK set_windows_hook( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid, BOOL unicode )
138 HHOOK handle = 0;
139 WCHAR module[MAX_PATH];
140 DWORD len;
142 if (!proc)
144 SetLastError( ERROR_INVALID_FILTER_PROC );
145 return 0;
148 if (tid) /* thread-local hook */
150 if (id == WH_JOURNALRECORD ||
151 id == WH_JOURNALPLAYBACK ||
152 id == WH_KEYBOARD_LL ||
153 id == WH_MOUSE_LL ||
154 id == WH_SYSMSGFILTER)
156 /* these can only be global */
157 SetLastError( ERROR_INVALID_PARAMETER );
158 return 0;
161 else /* system-global hook */
163 if (id == WH_KEYBOARD_LL || id == WH_MOUSE_LL) inst = 0;
164 else if (!inst)
166 SetLastError( ERROR_HOOK_NEEDS_HMOD );
167 return 0;
171 if (inst && (!(len = GetModuleFileNameW( inst, module, MAX_PATH )) || len >= MAX_PATH))
173 SetLastError( ERROR_INVALID_PARAMETER );
174 return 0;
177 SERVER_START_REQ( set_hook )
179 req->id = id;
180 req->pid = 0;
181 req->tid = tid;
182 req->event_min = EVENT_MIN;
183 req->event_max = EVENT_MAX;
184 req->flags = WINEVENT_INCONTEXT;
185 req->unicode = unicode;
186 if (inst) /* make proc relative to the module base */
188 req->proc = wine_server_client_ptr( (void *)((char *)proc - (char *)inst) );
189 wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
191 else req->proc = wine_server_client_ptr( proc );
193 if (!wine_server_call_err( req ))
195 handle = wine_server_ptr_handle( reply->handle );
196 get_user_thread_info()->active_hooks = reply->active_hooks;
199 SERVER_END_REQ;
201 TRACE( "%s %p %x -> %p\n", hook_names[id-WH_MINHOOK], proc, tid, handle );
202 return handle;
205 #ifdef __i386__
206 /* Some apps pass a non-stdcall proc to SetWindowsHookExA,
207 * so we need a small assembly wrapper to call the proc.
209 extern LRESULT HOOKPROC_wrapper( HOOKPROC proc,
210 INT code, WPARAM wParam, LPARAM lParam );
211 __ASM_GLOBAL_FUNC( HOOKPROC_wrapper,
212 "pushl %ebp\n\t"
213 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
214 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
215 "movl %esp,%ebp\n\t"
216 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
217 "pushl %edi\n\t"
218 __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
219 "pushl %esi\n\t"
220 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
221 "pushl %ebx\n\t"
222 __ASM_CFI(".cfi_rel_offset %ebx,-12\n\t")
223 "pushl 20(%ebp)\n\t"
224 "pushl 16(%ebp)\n\t"
225 "pushl 12(%ebp)\n\t"
226 "movl 8(%ebp),%eax\n\t"
227 "call *%eax\n\t"
228 "leal -12(%ebp),%esp\n\t"
229 "popl %ebx\n\t"
230 __ASM_CFI(".cfi_same_value %ebx\n\t")
231 "popl %esi\n\t"
232 __ASM_CFI(".cfi_same_value %esi\n\t")
233 "popl %edi\n\t"
234 __ASM_CFI(".cfi_same_value %edi\n\t")
235 "leave\n\t"
236 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
237 __ASM_CFI(".cfi_same_value %ebp\n\t")
238 "ret" )
239 #else
240 static inline LRESULT HOOKPROC_wrapper( HOOKPROC proc,
241 INT code, WPARAM wParam, LPARAM lParam )
243 return proc( code, wParam, lParam );
245 #endif /* __i386__ */
248 /***********************************************************************
249 * call_hook_AtoW
251 static LRESULT call_hook_AtoW( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
253 LRESULT ret;
254 UNICODE_STRING usBuffer;
255 if (id != WH_CBT || code != HCBT_CREATEWND)
256 ret = HOOKPROC_wrapper( proc, code, wparam, lparam );
257 else
259 CBT_CREATEWNDA *cbtcwA = (CBT_CREATEWNDA *)lparam;
260 CBT_CREATEWNDW cbtcwW;
261 CREATESTRUCTW csW;
262 LPWSTR nameW = NULL;
263 LPWSTR classW = NULL;
265 cbtcwW.lpcs = &csW;
266 cbtcwW.hwndInsertAfter = cbtcwA->hwndInsertAfter;
267 csW = *(CREATESTRUCTW *)cbtcwA->lpcs;
269 if (!IS_INTRESOURCE(cbtcwA->lpcs->lpszName))
271 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszName);
272 csW.lpszName = nameW = usBuffer.Buffer;
274 if (!IS_INTRESOURCE(cbtcwA->lpcs->lpszClass))
276 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszClass);
277 csW.lpszClass = classW = usBuffer.Buffer;
279 ret = HOOKPROC_wrapper( proc, code, wparam, (LPARAM)&cbtcwW );
280 cbtcwA->hwndInsertAfter = cbtcwW.hwndInsertAfter;
281 HeapFree( GetProcessHeap(), 0, nameW );
282 HeapFree( GetProcessHeap(), 0, classW );
284 return ret;
288 /***********************************************************************
289 * call_hook_WtoA
291 static LRESULT call_hook_WtoA( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
293 LRESULT ret;
295 if (id != WH_CBT || code != HCBT_CREATEWND)
296 ret = HOOKPROC_wrapper( proc, code, wparam, lparam );
297 else
299 CBT_CREATEWNDW *cbtcwW = (CBT_CREATEWNDW *)lparam;
300 CBT_CREATEWNDA cbtcwA;
301 CREATESTRUCTA csA;
302 int len;
303 LPSTR nameA = NULL;
304 LPSTR classA = NULL;
306 cbtcwA.lpcs = &csA;
307 cbtcwA.hwndInsertAfter = cbtcwW->hwndInsertAfter;
308 csA = *(CREATESTRUCTA *)cbtcwW->lpcs;
310 if (!IS_INTRESOURCE(cbtcwW->lpcs->lpszName)) {
311 len = WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszName, -1, NULL, 0, NULL, NULL );
312 nameA = HeapAlloc( GetProcessHeap(), 0, len*sizeof(CHAR) );
313 WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszName, -1, nameA, len, NULL, NULL );
314 csA.lpszName = nameA;
317 if (!IS_INTRESOURCE(cbtcwW->lpcs->lpszClass)) {
318 len = WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszClass, -1, NULL, 0, NULL, NULL );
319 classA = HeapAlloc( GetProcessHeap(), 0, len*sizeof(CHAR) );
320 WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszClass, -1, classA, len, NULL, NULL );
321 csA.lpszClass = classA;
324 ret = HOOKPROC_wrapper( proc, code, wparam, (LPARAM)&cbtcwA );
325 cbtcwW->hwndInsertAfter = cbtcwA.hwndInsertAfter;
326 HeapFree( GetProcessHeap(), 0, nameA );
327 HeapFree( GetProcessHeap(), 0, classA );
329 return ret;
333 /***********************************************************************
334 * call_hook_proc
336 static LRESULT call_hook_proc( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam,
337 BOOL prev_unicode, BOOL next_unicode )
339 LRESULT ret;
341 if (TRACE_ON(relay))
342 DPRINTF( "%04x:Call hook proc %p (id=%s,code=%x,wp=%08lx,lp=%08lx)\n",
343 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam );
345 if (!prev_unicode == !next_unicode) ret = proc( code, wparam, lparam );
346 else if (prev_unicode) ret = call_hook_WtoA( proc, id, code, wparam, lparam );
347 else ret = call_hook_AtoW( proc, id, code, wparam, lparam );
349 if (TRACE_ON(relay))
350 DPRINTF( "%04x:Ret hook proc %p (id=%s,code=%x,wp=%08lx,lp=%08lx) retval=%08lx\n",
351 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, ret );
353 return ret;
357 /***********************************************************************
358 * get_hook_proc
360 * Retrieve the hook procedure real value for a module-relative proc
362 void *get_hook_proc( void *proc, const WCHAR *module, HMODULE *free_module )
364 HMODULE mod;
366 GetModuleHandleExW( 0, module, &mod );
367 *free_module = mod;
368 if (!mod)
370 TRACE( "loading %s\n", debugstr_w(module) );
371 /* FIXME: the library will never be freed */
372 if (!(mod = LoadLibraryExW(module, NULL, LOAD_WITH_ALTERED_SEARCH_PATH))) return NULL;
374 return (char *)mod + (ULONG_PTR)proc;
377 /***********************************************************************
378 * call_hook
380 * Call hook either in current thread or send message to the destination
381 * thread.
383 static LRESULT call_hook( struct hook_info *info, INT code, WPARAM wparam, LPARAM lparam )
385 DWORD_PTR ret = 0;
387 if (info->tid)
389 struct hook_extra_info h_extra;
390 h_extra.handle = info->handle;
391 h_extra.lparam = lparam;
393 TRACE( "calling hook in thread %04x %s code %x wp %lx lp %lx\n",
394 info->tid, hook_names[info->id-WH_MINHOOK], code, wparam, lparam );
396 switch(info->id)
398 case WH_KEYBOARD_LL:
399 MSG_SendInternalMessageTimeout( info->pid, info->tid, WM_WINE_KEYBOARD_LL_HOOK,
400 wparam, (LPARAM)&h_extra, SMTO_ABORTIFHUNG,
401 get_ll_hook_timeout(), &ret );
402 break;
403 case WH_MOUSE_LL:
404 MSG_SendInternalMessageTimeout( info->pid, info->tid, WM_WINE_MOUSE_LL_HOOK,
405 wparam, (LPARAM)&h_extra, SMTO_ABORTIFHUNG,
406 get_ll_hook_timeout(), &ret );
407 break;
408 default:
409 ERR("Unknown hook id %d\n", info->id);
410 assert(0);
411 break;
414 else if (info->proc)
416 struct user_thread_info *thread_info = get_user_thread_info();
417 HMODULE free_module = 0;
420 * Windows protects from stack overflow in recursive hook calls. Different Windows
421 * allow different depths.
423 if (thread_info->hook_call_depth >= 25)
425 WARN("Too many hooks called recursively, skipping call.\n");
426 return 0;
429 TRACE( "calling hook %p %s code %x wp %lx lp %lx module %s\n",
430 info->proc, hook_names[info->id-WH_MINHOOK], code, wparam,
431 lparam, debugstr_w(info->module) );
433 if (!info->module[0] ||
434 (info->proc = get_hook_proc( info->proc, info->module, &free_module )) != NULL)
436 HHOOK prev = thread_info->hook;
437 BOOL prev_unicode = thread_info->hook_unicode;
439 thread_info->hook = info->handle;
440 thread_info->hook_unicode = info->next_unicode;
441 thread_info->hook_call_depth++;
442 ret = call_hook_proc( info->proc, info->id, code, wparam, lparam,
443 info->prev_unicode, info->next_unicode );
444 thread_info->hook = prev;
445 thread_info->hook_unicode = prev_unicode;
446 thread_info->hook_call_depth--;
448 if (free_module) FreeLibrary(free_module);
452 if (info->id == WH_KEYBOARD_LL || info->id == WH_MOUSE_LL)
453 interlocked_xchg_add( &global_key_state_counter, 1 ); /* force refreshing the key state cache */
455 return ret;
459 /***********************************************************************
460 * HOOK_IsHooked
462 static BOOL HOOK_IsHooked( INT id )
464 struct user_thread_info *thread_info = get_user_thread_info();
466 if (!thread_info->active_hooks) return TRUE;
467 return (thread_info->active_hooks & (1 << (id - WH_MINHOOK))) != 0;
471 /***********************************************************************
472 * HOOK_CallHooks
474 LRESULT HOOK_CallHooks( INT id, INT code, WPARAM wparam, LPARAM lparam, BOOL unicode )
476 struct user_thread_info *thread_info = get_user_thread_info();
477 struct hook_info info;
478 DWORD_PTR ret = 0;
480 USER_CheckNotLock();
482 if (!HOOK_IsHooked( id ))
484 TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
485 return 0;
488 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
489 info.prev_unicode = unicode;
490 info.id = id;
492 SERVER_START_REQ( start_hook_chain )
494 req->id = info.id;
495 req->event = EVENT_MIN;
496 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
497 if (!wine_server_call( req ))
499 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
500 info.handle = wine_server_ptr_handle( reply->handle );
501 info.pid = reply->pid;
502 info.tid = reply->tid;
503 info.proc = wine_server_get_ptr( reply->proc );
504 info.next_unicode = reply->unicode;
505 thread_info->active_hooks = reply->active_hooks;
508 SERVER_END_REQ;
510 if (!info.tid && !info.proc) return 0;
511 ret = call_hook( &info, code, wparam, lparam );
513 SERVER_START_REQ( finish_hook_chain )
515 req->id = id;
516 wine_server_call( req );
518 SERVER_END_REQ;
519 return ret;
523 /***********************************************************************
524 * SetWindowsHookA (USER32.@)
526 HHOOK WINAPI SetWindowsHookA( INT id, HOOKPROC proc )
528 return SetWindowsHookExA( id, proc, 0, GetCurrentThreadId() );
532 /***********************************************************************
533 * SetWindowsHookW (USER32.@)
535 HHOOK WINAPI SetWindowsHookW( INT id, HOOKPROC proc )
537 return SetWindowsHookExW( id, proc, 0, GetCurrentThreadId() );
541 /***********************************************************************
542 * SetWindowsHookExA (USER32.@)
544 HHOOK WINAPI SetWindowsHookExA( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
546 return set_windows_hook( id, proc, inst, tid, FALSE );
549 /***********************************************************************
550 * SetWindowsHookExW (USER32.@)
552 HHOOK WINAPI SetWindowsHookExW( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
554 return set_windows_hook( id, proc, inst, tid, TRUE );
558 /***********************************************************************
559 * UnhookWindowsHook (USER32.@)
561 BOOL WINAPI UnhookWindowsHook( INT id, HOOKPROC proc )
563 BOOL ret;
565 TRACE( "%s %p\n", hook_names[id-WH_MINHOOK], proc );
567 SERVER_START_REQ( remove_hook )
569 req->handle = 0;
570 req->id = id;
571 req->proc = wine_server_client_ptr( proc );
572 ret = !wine_server_call_err( req );
573 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
575 SERVER_END_REQ;
576 if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
577 return ret;
582 /***********************************************************************
583 * UnhookWindowsHookEx (USER32.@)
585 BOOL WINAPI UnhookWindowsHookEx( HHOOK hhook )
587 BOOL ret;
589 SERVER_START_REQ( remove_hook )
591 req->handle = wine_server_user_handle( hhook );
592 req->id = 0;
593 ret = !wine_server_call_err( req );
594 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
596 SERVER_END_REQ;
597 if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
598 return ret;
602 /***********************************************************************
603 * CallNextHookEx (USER32.@)
605 LRESULT WINAPI CallNextHookEx( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
607 struct user_thread_info *thread_info = get_user_thread_info();
608 struct hook_info info;
610 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
612 SERVER_START_REQ( get_hook_info )
614 req->handle = wine_server_user_handle( thread_info->hook );
615 req->get_next = 1;
616 req->event = EVENT_MIN;
617 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
618 if (!wine_server_call_err( req ))
620 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
621 info.handle = wine_server_ptr_handle( reply->handle );
622 info.id = reply->id;
623 info.pid = reply->pid;
624 info.tid = reply->tid;
625 info.proc = wine_server_get_ptr( reply->proc );
626 info.next_unicode = reply->unicode;
629 SERVER_END_REQ;
631 info.prev_unicode = thread_info->hook_unicode;
632 return call_hook( &info, code, wparam, lparam );
636 LRESULT call_current_hook( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
638 struct hook_info info;
640 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
642 SERVER_START_REQ( get_hook_info )
644 req->handle = wine_server_user_handle( hhook );
645 req->get_next = 0;
646 req->event = EVENT_MIN;
647 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
648 if (!wine_server_call_err( req ))
650 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
651 info.handle = wine_server_ptr_handle( reply->handle );
652 info.id = reply->id;
653 info.pid = reply->pid;
654 info.tid = reply->tid;
655 info.proc = wine_server_get_ptr( reply->proc );
656 info.next_unicode = reply->unicode;
659 SERVER_END_REQ;
661 info.prev_unicode = TRUE; /* assume Unicode for this function */
662 return call_hook( &info, code, wparam, lparam );
665 /***********************************************************************
666 * CallMsgFilterA (USER32.@)
668 BOOL WINAPI CallMsgFilterA( LPMSG msg, INT code )
670 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, FALSE )) return TRUE;
671 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, FALSE );
675 /***********************************************************************
676 * CallMsgFilterW (USER32.@)
678 BOOL WINAPI CallMsgFilterW( LPMSG msg, INT code )
680 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, TRUE )) return TRUE;
681 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, TRUE );
685 /***********************************************************************
686 * SetWinEventHook [USER32.@]
688 * Set up an event hook for a set of events.
690 * PARAMS
691 * event_min [I] Lowest event handled by pfnProc
692 * event_max [I] Highest event handled by pfnProc
693 * inst [I] DLL containing pfnProc
694 * proc [I] Callback event hook function
695 * pid [I] Process to get events from, or 0 for all processes
696 * tid [I] Thread to get events from, or 0 for all threads
697 * flags [I] Flags indicating the status of pfnProc
699 * RETURNS
700 * Success: A handle representing the hook.
701 * Failure: A NULL handle.
703 HWINEVENTHOOK WINAPI SetWinEventHook(DWORD event_min, DWORD event_max,
704 HMODULE inst, WINEVENTPROC proc,
705 DWORD pid, DWORD tid, DWORD flags)
707 HWINEVENTHOOK handle = 0;
708 WCHAR module[MAX_PATH];
709 DWORD len;
711 TRACE("%d,%d,%p,%p,%08x,%04x,%08x\n", event_min, event_max, inst,
712 proc, pid, tid, flags);
714 if (inst)
716 if (!(len = GetModuleFileNameW(inst, module, MAX_PATH)) || len >= MAX_PATH)
717 inst = 0;
720 if ((flags & WINEVENT_INCONTEXT) && !inst)
722 SetLastError(ERROR_HOOK_NEEDS_HMOD);
723 return 0;
726 if (event_min > event_max)
728 SetLastError(ERROR_INVALID_HOOK_FILTER);
729 return 0;
732 /* FIXME: what if the tid or pid belongs to another process? */
733 if (tid) /* thread-local hook */
734 inst = 0;
736 SERVER_START_REQ( set_hook )
738 req->id = WH_WINEVENT;
739 req->pid = pid;
740 req->tid = tid;
741 req->event_min = event_min;
742 req->event_max = event_max;
743 req->flags = flags;
744 req->unicode = 1;
745 if (inst) /* make proc relative to the module base */
747 req->proc = wine_server_client_ptr( (void *)((char *)proc - (char *)inst) );
748 wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
750 else req->proc = wine_server_client_ptr( proc );
752 if (!wine_server_call_err( req ))
754 handle = wine_server_ptr_handle( reply->handle );
755 get_user_thread_info()->active_hooks = reply->active_hooks;
758 SERVER_END_REQ;
760 TRACE("-> %p\n", handle);
761 return handle;
765 /***********************************************************************
766 * UnhookWinEvent [USER32.@]
768 * Remove an event hook for a set of events.
770 * PARAMS
771 * hEventHook [I] Event hook to remove
773 * RETURNS
774 * Success: TRUE. The event hook has been removed.
775 * Failure: FALSE, if hEventHook is invalid.
777 BOOL WINAPI UnhookWinEvent(HWINEVENTHOOK hEventHook)
779 BOOL ret;
781 SERVER_START_REQ( remove_hook )
783 req->handle = wine_server_user_handle( hEventHook );
784 req->id = WH_WINEVENT;
785 ret = !wine_server_call_err( req );
786 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
788 SERVER_END_REQ;
789 return ret;
792 static inline BOOL find_first_hook(DWORD id, DWORD event, HWND hwnd, LONG object_id,
793 LONG child_id, struct hook_info *info)
795 struct user_thread_info *thread_info = get_user_thread_info();
796 BOOL ret;
798 if (!HOOK_IsHooked( id ))
800 TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
801 return FALSE;
804 SERVER_START_REQ( start_hook_chain )
806 req->id = id;
807 req->event = event;
808 req->window = wine_server_user_handle( hwnd );
809 req->object_id = object_id;
810 req->child_id = child_id;
811 wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
812 ret = !wine_server_call( req );
813 if (ret)
815 info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
816 info->handle = wine_server_ptr_handle( reply->handle );
817 info->proc = wine_server_get_ptr( reply->proc );
818 info->tid = reply->tid;
819 thread_info->active_hooks = reply->active_hooks;
822 SERVER_END_REQ;
823 return ret && (info->tid || info->proc);
826 static inline BOOL find_next_hook(DWORD event, HWND hwnd, LONG object_id,
827 LONG child_id, struct hook_info *info)
829 BOOL ret;
831 SERVER_START_REQ( get_hook_info )
833 req->handle = wine_server_user_handle( info->handle );
834 req->get_next = 1;
835 req->event = event;
836 req->window = wine_server_user_handle( hwnd );
837 req->object_id = object_id;
838 req->child_id = child_id;
839 wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
840 ret = !wine_server_call( req );
841 if (ret)
843 info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
844 info->handle = wine_server_ptr_handle( reply->handle );
845 info->proc = wine_server_get_ptr( reply->proc );
846 info->tid = reply->tid;
849 SERVER_END_REQ;
850 return ret;
853 static inline void find_hook_close(DWORD id)
855 SERVER_START_REQ( finish_hook_chain )
857 req->id = id;
858 wine_server_call( req );
860 SERVER_END_REQ;
863 /***********************************************************************
864 * NotifyWinEvent [USER32.@]
866 * Inform the OS that an event has occurred.
868 * PARAMS
869 * event [I] Id of the event
870 * hwnd [I] Window holding the object that created the event
871 * object_id [I] Type of object that created the event
872 * child_id [I] Child object of nId, or CHILDID_SELF.
874 * RETURNS
875 * Nothing.
877 void WINAPI NotifyWinEvent(DWORD event, HWND hwnd, LONG object_id, LONG child_id)
879 struct hook_info info;
881 TRACE("%04x,%p,%d,%d\n", event, hwnd, object_id, child_id);
883 if (!hwnd)
885 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
886 return;
889 USER_CheckNotLock();
891 #if 0
892 if (event & 0x80000000)
894 /* FIXME: on 64-bit platforms we need to invent some other way for
895 * passing parameters, nId and nChildId can't hold full [W|L]PARAM.
896 * struct call_hook *hook = (LRESULT *)hWnd;
897 * wparam = hook->wparam;
898 * lparam = hook->lparam;
900 LRESULT *ret = (LRESULT *)hwnd;
901 INT id, code, unicode;
903 id = (dwEvent & 0x7fff0000) >> 16;
904 code = event & 0x7fff;
905 unicode = event & 0x8000;
906 *ret = HOOK_CallHooks(id, code, object_id, child_id, unicode);
907 return;
909 #endif
911 if (!find_first_hook(WH_WINEVENT, event, hwnd, object_id, child_id, &info)) return;
915 WINEVENTPROC proc = info.proc;
916 if (proc)
918 HMODULE free_module = 0;
919 TRACE( "calling WH_WINEVENT hook %p event %x hwnd %p %x %x module %s\n",
920 proc, event, hwnd, object_id, child_id, debugstr_w(info.module) );
922 if (!info.module[0] || (proc = get_hook_proc( proc, info.module, &free_module )) != NULL)
924 if (TRACE_ON(relay))
925 DPRINTF( "%04x:Call winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
926 GetCurrentThreadId(), proc, info.handle, event, hwnd, object_id,
927 child_id, GetCurrentThreadId(), GetCurrentTime());
929 proc( info.handle, event, hwnd, object_id, child_id,
930 GetCurrentThreadId(), GetCurrentTime());
932 if (TRACE_ON(relay))
933 DPRINTF( "%04x:Ret winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
934 GetCurrentThreadId(), proc, info.handle, event, hwnd, object_id,
935 child_id, GetCurrentThreadId(), GetCurrentTime());
937 if (free_module) FreeLibrary(free_module);
940 else
941 break;
943 while (find_next_hook(event, hwnd, object_id, child_id, &info));
945 find_hook_close(WH_WINEVENT);
949 /***********************************************************************
950 * IsWinEventHookInstalled [USER32.@]
952 * Determine if an event hook is installed for an event.
954 * PARAMS
955 * dwEvent [I] Id of the event
957 * RETURNS
958 * TRUE, If there are any hooks installed for the event.
959 * FALSE, Otherwise.
961 * BUGS
962 * Not implemented.
964 BOOL WINAPI IsWinEventHookInstalled(DWORD dwEvent)
966 /* FIXME: Needed by Office 2007 installer */
967 WARN("(%d)-stub!\n", dwEvent);
968 return TRUE;