Add AppDefaults app selection to control panel
[wine/gsoc-2012-control.git] / dlls / user32 / hook.c
blob1ba5ddab08e3fee0f2de56ae291de12aaa7e352d
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 "winuser.h"
74 #include "winerror.h"
75 #include "win.h"
76 #include "user_private.h"
77 #include "wine/server.h"
78 #include "wine/unicode.h"
79 #include "wine/debug.h"
80 #include "winternl.h"
82 WINE_DEFAULT_DEBUG_CHANNEL(hook);
83 WINE_DECLARE_DEBUG_CHANNEL(relay);
85 struct hook_info
87 INT id;
88 void *proc;
89 void *handle;
90 DWORD pid, tid;
91 BOOL prev_unicode, next_unicode;
92 WCHAR module[MAX_PATH];
95 #define WH_WINEVENT (WH_MAXHOOK+1)
97 static const char * const hook_names[WH_WINEVENT - WH_MINHOOK + 1] =
99 "WH_MSGFILTER",
100 "WH_JOURNALRECORD",
101 "WH_JOURNALPLAYBACK",
102 "WH_KEYBOARD",
103 "WH_GETMESSAGE",
104 "WH_CALLWNDPROC",
105 "WH_CBT",
106 "WH_SYSMSGFILTER",
107 "WH_MOUSE",
108 "WH_HARDWARE",
109 "WH_DEBUG",
110 "WH_SHELL",
111 "WH_FOREGROUNDIDLE",
112 "WH_CALLWNDPROCRET",
113 "WH_KEYBOARD_LL",
114 "WH_MOUSE_LL",
115 "WH_WINEVENT"
119 /***********************************************************************
120 * get_ll_hook_timeout
123 static UINT get_ll_hook_timeout(void)
125 /* FIXME: should retrieve LowLevelHooksTimeout in HKEY_CURRENT_USER\Control Panel\Desktop */
126 return 2000;
130 /***********************************************************************
131 * set_windows_hook
133 * Implementation of SetWindowsHookExA and SetWindowsHookExW.
135 static HHOOK set_windows_hook( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid, BOOL unicode )
137 HHOOK handle = 0;
138 WCHAR module[MAX_PATH];
139 DWORD len;
141 if (!proc)
143 SetLastError( ERROR_INVALID_FILTER_PROC );
144 return 0;
147 if (tid) /* thread-local hook */
149 if (id == WH_JOURNALRECORD ||
150 id == WH_JOURNALPLAYBACK ||
151 id == WH_KEYBOARD_LL ||
152 id == WH_MOUSE_LL ||
153 id == WH_SYSMSGFILTER)
155 /* these can only be global */
156 SetLastError( ERROR_INVALID_PARAMETER );
157 return 0;
160 else /* system-global hook */
162 if (id == WH_KEYBOARD_LL || id == WH_MOUSE_LL) inst = 0;
163 else if (!inst)
165 SetLastError( ERROR_HOOK_NEEDS_HMOD );
166 return 0;
170 if (inst && (!(len = GetModuleFileNameW( inst, module, MAX_PATH )) || len >= MAX_PATH))
172 SetLastError( ERROR_INVALID_PARAMETER );
173 return 0;
176 SERVER_START_REQ( set_hook )
178 req->id = id;
179 req->pid = 0;
180 req->tid = tid;
181 req->event_min = EVENT_MIN;
182 req->event_max = EVENT_MAX;
183 req->flags = WINEVENT_INCONTEXT;
184 req->unicode = unicode;
185 if (inst) /* make proc relative to the module base */
187 req->proc = wine_server_client_ptr( (void *)((char *)proc - (char *)inst) );
188 wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
190 else req->proc = wine_server_client_ptr( proc );
192 if (!wine_server_call_err( req ))
194 handle = wine_server_ptr_handle( reply->handle );
195 get_user_thread_info()->active_hooks = reply->active_hooks;
198 SERVER_END_REQ;
200 TRACE( "%s %p %x -> %p\n", hook_names[id-WH_MINHOOK], proc, tid, handle );
201 return handle;
205 /***********************************************************************
206 * call_hook_AtoW
208 static LRESULT call_hook_AtoW( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
210 LRESULT ret;
211 UNICODE_STRING usBuffer;
212 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
213 else
215 CBT_CREATEWNDA *cbtcwA = (CBT_CREATEWNDA *)lparam;
216 CBT_CREATEWNDW cbtcwW;
217 CREATESTRUCTW csW;
218 LPWSTR nameW = NULL;
219 LPWSTR classW = NULL;
221 cbtcwW.lpcs = &csW;
222 cbtcwW.hwndInsertAfter = cbtcwA->hwndInsertAfter;
223 csW = *(CREATESTRUCTW *)cbtcwA->lpcs;
225 if (!IS_INTRESOURCE(cbtcwA->lpcs->lpszName))
227 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszName);
228 csW.lpszName = nameW = usBuffer.Buffer;
230 if (!IS_INTRESOURCE(cbtcwA->lpcs->lpszClass))
232 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszClass);
233 csW.lpszClass = classW = usBuffer.Buffer;
235 ret = proc( code, wparam, (LPARAM)&cbtcwW );
236 cbtcwA->hwndInsertAfter = cbtcwW.hwndInsertAfter;
237 HeapFree( GetProcessHeap(), 0, nameW );
238 HeapFree( GetProcessHeap(), 0, classW );
240 return ret;
244 /***********************************************************************
245 * call_hook_WtoA
247 static LRESULT call_hook_WtoA( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
249 LRESULT ret;
251 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
252 else
254 CBT_CREATEWNDW *cbtcwW = (CBT_CREATEWNDW *)lparam;
255 CBT_CREATEWNDA cbtcwA;
256 CREATESTRUCTA csA;
257 int len;
258 LPSTR nameA = NULL;
259 LPSTR classA = NULL;
261 cbtcwA.lpcs = &csA;
262 cbtcwA.hwndInsertAfter = cbtcwW->hwndInsertAfter;
263 csA = *(CREATESTRUCTA *)cbtcwW->lpcs;
265 if (!IS_INTRESOURCE(cbtcwW->lpcs->lpszName)) {
266 len = WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszName, -1, NULL, 0, NULL, NULL );
267 nameA = HeapAlloc( GetProcessHeap(), 0, len*sizeof(CHAR) );
268 WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszName, -1, nameA, len, NULL, NULL );
269 csA.lpszName = nameA;
272 if (!IS_INTRESOURCE(cbtcwW->lpcs->lpszClass)) {
273 len = WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszClass, -1, NULL, 0, NULL, NULL );
274 classA = HeapAlloc( GetProcessHeap(), 0, len*sizeof(CHAR) );
275 WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszClass, -1, classA, len, NULL, NULL );
276 csA.lpszClass = classA;
279 ret = proc( code, wparam, (LPARAM)&cbtcwA );
280 cbtcwW->hwndInsertAfter = cbtcwA.hwndInsertAfter;
281 HeapFree( GetProcessHeap(), 0, nameA );
282 HeapFree( GetProcessHeap(), 0, classA );
284 return ret;
288 /***********************************************************************
289 * call_hook_proc
291 static LRESULT call_hook_proc( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam,
292 BOOL prev_unicode, BOOL next_unicode )
294 LRESULT ret;
296 if (TRACE_ON(relay))
297 DPRINTF( "%04x:Call hook proc %p (id=%s,code=%x,wp=%08lx,lp=%08lx)\n",
298 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam );
300 if (!prev_unicode == !next_unicode) ret = proc( code, wparam, lparam );
301 else if (prev_unicode) ret = call_hook_WtoA( proc, id, code, wparam, lparam );
302 else ret = call_hook_AtoW( proc, id, code, wparam, lparam );
304 if (TRACE_ON(relay))
305 DPRINTF( "%04x:Ret hook proc %p (id=%s,code=%x,wp=%08lx,lp=%08lx) retval=%08lx\n",
306 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, ret );
308 return ret;
312 /***********************************************************************
313 * get_hook_proc
315 * Retrieve the hook procedure real value for a module-relative proc
317 void *get_hook_proc( void *proc, const WCHAR *module )
319 HMODULE mod;
321 if (!(mod = GetModuleHandleW(module)))
323 TRACE( "loading %s\n", debugstr_w(module) );
324 /* FIXME: the library will never be freed */
325 if (!(mod = LoadLibraryExW(module, NULL, LOAD_WITH_ALTERED_SEARCH_PATH))) return NULL;
327 return (char *)mod + (ULONG_PTR)proc;
330 /***********************************************************************
331 * call_hook
333 * Call hook either in current thread or send message to the destination
334 * thread.
336 static LRESULT call_hook( struct hook_info *info, INT code, WPARAM wparam, LPARAM lparam )
338 DWORD_PTR ret = 0;
340 if (info->tid)
342 struct hook_extra_info h_extra;
343 h_extra.handle = info->handle;
344 h_extra.lparam = lparam;
346 TRACE( "calling hook in thread %04x %s code %x wp %lx lp %lx\n",
347 info->tid, hook_names[info->id-WH_MINHOOK], code, wparam, lparam );
349 switch(info->id)
351 case WH_KEYBOARD_LL:
352 MSG_SendInternalMessageTimeout( info->pid, info->tid, WM_WINE_KEYBOARD_LL_HOOK,
353 wparam, (LPARAM)&h_extra, SMTO_ABORTIFHUNG,
354 get_ll_hook_timeout(), &ret );
355 break;
356 case WH_MOUSE_LL:
357 MSG_SendInternalMessageTimeout( info->pid, info->tid, WM_WINE_MOUSE_LL_HOOK,
358 wparam, (LPARAM)&h_extra, SMTO_ABORTIFHUNG,
359 get_ll_hook_timeout(), &ret );
360 break;
361 default:
362 ERR("Unknown hook id %d\n", info->id);
363 assert(0);
364 break;
367 else if (info->proc)
369 TRACE( "calling hook %p %s code %x wp %lx lp %lx module %s\n",
370 info->proc, hook_names[info->id-WH_MINHOOK], code, wparam,
371 lparam, debugstr_w(info->module) );
373 if (!info->module[0] ||
374 (info->proc = get_hook_proc( info->proc, info->module )) != NULL)
376 struct user_thread_info *thread_info = get_user_thread_info();
377 HHOOK prev = thread_info->hook;
378 BOOL prev_unicode = thread_info->hook_unicode;
380 thread_info->hook = info->handle;
381 thread_info->hook_unicode = info->next_unicode;
382 ret = call_hook_proc( info->proc, info->id, code, wparam, lparam,
383 info->prev_unicode, info->next_unicode );
384 thread_info->hook = prev;
385 thread_info->hook_unicode = prev_unicode;
389 if (info->id == WH_KEYBOARD_LL || info->id == WH_MOUSE_LL)
390 get_user_thread_info()->key_state_time = 0; /* force refreshing the key state cache */
392 return ret;
396 /***********************************************************************
397 * HOOK_IsHooked
399 static BOOL HOOK_IsHooked( INT id )
401 struct user_thread_info *thread_info = get_user_thread_info();
403 if (!thread_info->active_hooks) return TRUE;
404 return (thread_info->active_hooks & (1 << (id - WH_MINHOOK))) != 0;
408 /***********************************************************************
409 * HOOK_CallHooks
411 LRESULT HOOK_CallHooks( INT id, INT code, WPARAM wparam, LPARAM lparam, BOOL unicode )
413 struct user_thread_info *thread_info = get_user_thread_info();
414 struct hook_info info;
415 DWORD_PTR ret = 0;
417 USER_CheckNotLock();
419 if (!HOOK_IsHooked( id ))
421 TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
422 return 0;
425 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
426 info.prev_unicode = unicode;
427 info.id = id;
429 SERVER_START_REQ( start_hook_chain )
431 req->id = info.id;
432 req->event = EVENT_MIN;
433 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
434 if (!wine_server_call( req ))
436 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
437 info.handle = wine_server_ptr_handle( reply->handle );
438 info.pid = reply->pid;
439 info.tid = reply->tid;
440 info.proc = wine_server_get_ptr( reply->proc );
441 info.next_unicode = reply->unicode;
442 thread_info->active_hooks = reply->active_hooks;
445 SERVER_END_REQ;
447 if (!info.tid && !info.proc) return 0;
448 ret = call_hook( &info, code, wparam, lparam );
450 SERVER_START_REQ( finish_hook_chain )
452 req->id = id;
453 wine_server_call( req );
455 SERVER_END_REQ;
456 return ret;
460 /***********************************************************************
461 * SetWindowsHookA (USER32.@)
463 HHOOK WINAPI SetWindowsHookA( INT id, HOOKPROC proc )
465 return SetWindowsHookExA( id, proc, 0, GetCurrentThreadId() );
469 /***********************************************************************
470 * SetWindowsHookW (USER32.@)
472 HHOOK WINAPI SetWindowsHookW( INT id, HOOKPROC proc )
474 return SetWindowsHookExW( id, proc, 0, GetCurrentThreadId() );
478 /***********************************************************************
479 * SetWindowsHookExA (USER32.@)
481 HHOOK WINAPI SetWindowsHookExA( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
483 return set_windows_hook( id, proc, inst, tid, FALSE );
486 /***********************************************************************
487 * SetWindowsHookExW (USER32.@)
489 HHOOK WINAPI SetWindowsHookExW( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
491 return set_windows_hook( id, proc, inst, tid, TRUE );
495 /***********************************************************************
496 * UnhookWindowsHook (USER32.@)
498 BOOL WINAPI UnhookWindowsHook( INT id, HOOKPROC proc )
500 BOOL ret;
502 TRACE( "%s %p\n", hook_names[id-WH_MINHOOK], proc );
504 SERVER_START_REQ( remove_hook )
506 req->handle = 0;
507 req->id = id;
508 req->proc = wine_server_client_ptr( proc );
509 ret = !wine_server_call_err( req );
510 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
512 SERVER_END_REQ;
513 if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
514 return ret;
519 /***********************************************************************
520 * UnhookWindowsHookEx (USER32.@)
522 BOOL WINAPI UnhookWindowsHookEx( HHOOK hhook )
524 BOOL ret;
526 SERVER_START_REQ( remove_hook )
528 req->handle = wine_server_user_handle( hhook );
529 req->id = 0;
530 ret = !wine_server_call_err( req );
531 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
533 SERVER_END_REQ;
534 if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
535 return ret;
539 /***********************************************************************
540 * CallNextHookEx (USER32.@)
542 LRESULT WINAPI CallNextHookEx( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
544 struct user_thread_info *thread_info = get_user_thread_info();
545 struct hook_info info;
547 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
549 SERVER_START_REQ( get_hook_info )
551 req->handle = wine_server_user_handle( thread_info->hook );
552 req->get_next = 1;
553 req->event = EVENT_MIN;
554 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
555 if (!wine_server_call_err( req ))
557 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
558 info.handle = wine_server_ptr_handle( reply->handle );
559 info.id = reply->id;
560 info.pid = reply->pid;
561 info.tid = reply->tid;
562 info.proc = wine_server_get_ptr( reply->proc );
563 info.next_unicode = reply->unicode;
566 SERVER_END_REQ;
568 info.prev_unicode = thread_info->hook_unicode;
569 return call_hook( &info, code, wparam, lparam );
573 LRESULT call_current_hook( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
575 struct hook_info info;
577 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
579 SERVER_START_REQ( get_hook_info )
581 req->handle = wine_server_user_handle( hhook );
582 req->get_next = 0;
583 req->event = EVENT_MIN;
584 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
585 if (!wine_server_call_err( req ))
587 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
588 info.handle = wine_server_ptr_handle( reply->handle );
589 info.id = reply->id;
590 info.pid = reply->pid;
591 info.tid = reply->tid;
592 info.proc = wine_server_get_ptr( reply->proc );
593 info.next_unicode = reply->unicode;
596 SERVER_END_REQ;
598 info.prev_unicode = TRUE; /* assume Unicode for this function */
599 return call_hook( &info, code, wparam, lparam );
602 /***********************************************************************
603 * CallMsgFilterA (USER32.@)
605 BOOL WINAPI CallMsgFilterA( LPMSG msg, INT code )
607 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, FALSE )) return TRUE;
608 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, FALSE );
612 /***********************************************************************
613 * CallMsgFilterW (USER32.@)
615 BOOL WINAPI CallMsgFilterW( LPMSG msg, INT code )
617 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, TRUE )) return TRUE;
618 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, TRUE );
622 /***********************************************************************
623 * SetWinEventHook [USER32.@]
625 * Set up an event hook for a set of events.
627 * PARAMS
628 * event_min [I] Lowest event handled by pfnProc
629 * event_max [I] Highest event handled by pfnProc
630 * inst [I] DLL containing pfnProc
631 * proc [I] Callback event hook function
632 * pid [I] Process to get events from, or 0 for all processes
633 * tid [I] Thread to get events from, or 0 for all threads
634 * flags [I] Flags indicating the status of pfnProc
636 * RETURNS
637 * Success: A handle representing the hook.
638 * Failure: A NULL handle.
640 HWINEVENTHOOK WINAPI SetWinEventHook(DWORD event_min, DWORD event_max,
641 HMODULE inst, WINEVENTPROC proc,
642 DWORD pid, DWORD tid, DWORD flags)
644 HWINEVENTHOOK handle = 0;
645 WCHAR module[MAX_PATH];
646 DWORD len;
648 TRACE("%d,%d,%p,%p,%08x,%04x,%08x\n", event_min, event_max, inst,
649 proc, pid, tid, flags);
651 if (inst)
653 if (!(len = GetModuleFileNameW(inst, module, MAX_PATH)) || len >= MAX_PATH)
654 inst = 0;
657 if ((flags & WINEVENT_INCONTEXT) && !inst)
659 SetLastError(ERROR_HOOK_NEEDS_HMOD);
660 return 0;
663 if (event_min > event_max)
665 SetLastError(ERROR_INVALID_HOOK_FILTER);
666 return 0;
669 /* FIXME: what if the tid or pid belongs to another process? */
670 if (tid) /* thread-local hook */
671 inst = 0;
673 SERVER_START_REQ( set_hook )
675 req->id = WH_WINEVENT;
676 req->pid = pid;
677 req->tid = tid;
678 req->event_min = event_min;
679 req->event_max = event_max;
680 req->flags = flags;
681 req->unicode = 1;
682 if (inst) /* make proc relative to the module base */
684 req->proc = wine_server_client_ptr( (void *)((char *)proc - (char *)inst) );
685 wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
687 else req->proc = wine_server_client_ptr( proc );
689 if (!wine_server_call_err( req ))
691 handle = wine_server_ptr_handle( reply->handle );
692 get_user_thread_info()->active_hooks = reply->active_hooks;
695 SERVER_END_REQ;
697 TRACE("-> %p\n", handle);
698 return handle;
702 /***********************************************************************
703 * UnhookWinEvent [USER32.@]
705 * Remove an event hook for a set of events.
707 * PARAMS
708 * hEventHook [I] Event hook to remove
710 * RETURNS
711 * Success: TRUE. The event hook has been removed.
712 * Failure: FALSE, if hEventHook is invalid.
714 BOOL WINAPI UnhookWinEvent(HWINEVENTHOOK hEventHook)
716 BOOL ret;
718 SERVER_START_REQ( remove_hook )
720 req->handle = wine_server_user_handle( hEventHook );
721 req->id = WH_WINEVENT;
722 ret = !wine_server_call_err( req );
723 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
725 SERVER_END_REQ;
726 return ret;
729 static inline BOOL find_first_hook(DWORD id, DWORD event, HWND hwnd, LONG object_id,
730 LONG child_id, struct hook_info *info)
732 struct user_thread_info *thread_info = get_user_thread_info();
733 BOOL ret;
735 if (!HOOK_IsHooked( id ))
737 TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
738 return FALSE;
741 SERVER_START_REQ( start_hook_chain )
743 req->id = id;
744 req->event = event;
745 req->window = wine_server_user_handle( hwnd );
746 req->object_id = object_id;
747 req->child_id = child_id;
748 wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
749 ret = !wine_server_call( req );
750 if (ret)
752 info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
753 info->handle = wine_server_ptr_handle( reply->handle );
754 info->proc = wine_server_get_ptr( reply->proc );
755 info->tid = reply->tid;
756 thread_info->active_hooks = reply->active_hooks;
759 SERVER_END_REQ;
760 return ret && (info->tid || info->proc);
763 static inline BOOL find_next_hook(DWORD event, HWND hwnd, LONG object_id,
764 LONG child_id, struct hook_info *info)
766 BOOL ret;
768 SERVER_START_REQ( get_hook_info )
770 req->handle = wine_server_user_handle( info->handle );
771 req->get_next = 1;
772 req->event = event;
773 req->window = wine_server_user_handle( hwnd );
774 req->object_id = object_id;
775 req->child_id = child_id;
776 wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
777 ret = !wine_server_call( req );
778 if (ret)
780 info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
781 info->handle = wine_server_ptr_handle( reply->handle );
782 info->proc = wine_server_get_ptr( reply->proc );
783 info->tid = reply->tid;
786 SERVER_END_REQ;
787 return ret;
790 static inline void find_hook_close(DWORD id)
792 SERVER_START_REQ( finish_hook_chain )
794 req->id = id;
795 wine_server_call( req );
797 SERVER_END_REQ;
800 /***********************************************************************
801 * NotifyWinEvent [USER32.@]
803 * Inform the OS that an event has occurred.
805 * PARAMS
806 * event [I] Id of the event
807 * hwnd [I] Window holding the object that created the event
808 * object_id [I] Type of object that created the event
809 * child_id [I] Child object of nId, or CHILDID_SELF.
811 * RETURNS
812 * Nothing.
814 void WINAPI NotifyWinEvent(DWORD event, HWND hwnd, LONG object_id, LONG child_id)
816 struct hook_info info;
818 TRACE("%04x,%p,%d,%d\n", event, hwnd, object_id, child_id);
820 if (!hwnd)
822 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
823 return;
826 USER_CheckNotLock();
828 #if 0
829 if (event & 0x80000000)
831 /* FIXME: on 64-bit platforms we need to invent some other way for
832 * passing parameters, nId and nChildId can't hold full [W|L]PARAM.
833 * struct call_hook *hook = (LRESULT *)hWnd;
834 * wparam = hook->wparam;
835 * lparam = hook->lparam;
837 LRESULT *ret = (LRESULT *)hwnd;
838 INT id, code, unicode;
840 id = (dwEvent & 0x7fff0000) >> 16;
841 code = event & 0x7fff;
842 unicode = event & 0x8000;
843 *ret = HOOK_CallHooks(id, code, object_id, child_id, unicode);
844 return;
846 #endif
848 if (!find_first_hook(WH_WINEVENT, event, hwnd, object_id, child_id, &info)) return;
852 WINEVENTPROC proc = info.proc;
853 if (proc)
855 TRACE( "calling WH_WINEVENT hook %p event %x hwnd %p %x %x module %s\n",
856 proc, event, hwnd, object_id, child_id, debugstr_w(info.module) );
858 if (!info.module[0] || (proc = get_hook_proc( proc, info.module )) != NULL)
860 if (TRACE_ON(relay))
861 DPRINTF( "%04x:Call winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
862 GetCurrentThreadId(), proc, info.handle, event, hwnd, object_id,
863 child_id, GetCurrentThreadId(), GetCurrentTime());
865 proc( info.handle, event, hwnd, object_id, child_id,
866 GetCurrentThreadId(), GetCurrentTime());
868 if (TRACE_ON(relay))
869 DPRINTF( "%04x:Ret winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
870 GetCurrentThreadId(), proc, info.handle, event, hwnd, object_id,
871 child_id, GetCurrentThreadId(), GetCurrentTime());
874 else
875 break;
877 while (find_next_hook(event, hwnd, object_id, child_id, &info));
879 find_hook_close(WH_WINEVENT);
883 /***********************************************************************
884 * IsWinEventHookInstalled [USER32.@]
886 * Determine if an event hook is installed for an event.
888 * PARAMS
889 * dwEvent [I] Id of the event
891 * RETURNS
892 * TRUE, If there are any hooks installed for the event.
893 * FALSE, Otherwise.
895 * BUGS
896 * Not implemented.
898 BOOL WINAPI IsWinEventHookInstalled(DWORD dwEvent)
900 /* FIXME: Needed by Office 2007 installer */
901 WARN("(%d)-stub!\n", dwEvent);
902 return TRUE;