new 818051de2c8769029049ce3d36c6b856f47496c9
[wine/hacks.git] / dlls / user32 / hook.c
blobb537b7b31d6d866cbbc2f11b2dd8afa66327314f
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 FARPROC 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 /* FIXME: what if the tid belongs to another process? */
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;
160 inst = 0;
162 else /* system-global hook */
164 if (id == WH_KEYBOARD_LL || id == WH_MOUSE_LL) inst = 0;
165 else if (!inst || !(len = GetModuleFileNameW( inst, module, MAX_PATH )) || len >= MAX_PATH)
167 SetLastError( ERROR_HOOK_NEEDS_HMOD );
168 return 0;
172 SERVER_START_REQ( set_hook )
174 req->id = id;
175 req->pid = 0;
176 req->tid = tid;
177 req->event_min = EVENT_MIN;
178 req->event_max = EVENT_MAX;
179 req->flags = WINEVENT_INCONTEXT;
180 req->unicode = unicode;
181 if (inst) /* make proc relative to the module base */
183 req->proc = (void *)((char *)proc - (char *)inst);
184 wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
186 else req->proc = proc;
188 if (!wine_server_call_err( req ))
190 handle = reply->handle;
191 get_user_thread_info()->active_hooks = reply->active_hooks;
194 SERVER_END_REQ;
196 TRACE( "%s %p %x -> %p\n", hook_names[id-WH_MINHOOK], proc, tid, handle );
197 return handle;
201 /***********************************************************************
202 * call_hook_AtoW
204 static LRESULT call_hook_AtoW( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
206 LRESULT ret;
207 UNICODE_STRING usBuffer;
208 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
209 else
211 CBT_CREATEWNDA *cbtcwA = (CBT_CREATEWNDA *)lparam;
212 CBT_CREATEWNDW cbtcwW;
213 CREATESTRUCTW csW;
214 LPWSTR nameW = NULL;
215 LPWSTR classW = NULL;
217 cbtcwW.lpcs = &csW;
218 cbtcwW.hwndInsertAfter = cbtcwA->hwndInsertAfter;
219 csW = *(CREATESTRUCTW *)cbtcwA->lpcs;
221 if (HIWORD(cbtcwA->lpcs->lpszName))
223 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszName);
224 csW.lpszName = nameW = usBuffer.Buffer;
226 if (HIWORD(cbtcwA->lpcs->lpszClass))
228 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszClass);
229 csW.lpszClass = classW = usBuffer.Buffer;
231 ret = proc( code, wparam, (LPARAM)&cbtcwW );
232 cbtcwA->hwndInsertAfter = cbtcwW.hwndInsertAfter;
233 HeapFree( GetProcessHeap(), 0, nameW );
234 HeapFree( GetProcessHeap(), 0, classW );
236 return ret;
240 /***********************************************************************
241 * call_hook_WtoA
243 static LRESULT call_hook_WtoA( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
245 LRESULT ret;
247 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
248 else
250 CBT_CREATEWNDW *cbtcwW = (CBT_CREATEWNDW *)lparam;
251 CBT_CREATEWNDA cbtcwA;
252 CREATESTRUCTA csA;
253 int len;
254 LPSTR nameA = NULL;
255 LPSTR classA = NULL;
257 cbtcwA.lpcs = &csA;
258 cbtcwA.hwndInsertAfter = cbtcwW->hwndInsertAfter;
259 csA = *(CREATESTRUCTA *)cbtcwW->lpcs;
261 if (HIWORD(cbtcwW->lpcs->lpszName)) {
262 len = WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszName, -1, NULL, 0, NULL, NULL );
263 nameA = HeapAlloc( GetProcessHeap(), 0, len*sizeof(CHAR) );
264 WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszName, -1, nameA, len, NULL, NULL );
265 csA.lpszName = nameA;
268 if (HIWORD(cbtcwW->lpcs->lpszClass)) {
269 len = WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszClass, -1, NULL, 0, NULL, NULL );
270 classA = HeapAlloc( GetProcessHeap(), 0, len*sizeof(CHAR) );
271 WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszClass, -1, classA, len, NULL, NULL );
272 csA.lpszClass = classA;
275 ret = proc( code, wparam, (LPARAM)&cbtcwA );
276 cbtcwW->hwndInsertAfter = cbtcwA.hwndInsertAfter;
277 HeapFree( GetProcessHeap(), 0, nameA );
278 HeapFree( GetProcessHeap(), 0, classA );
280 return ret;
284 /***********************************************************************
285 * call_hook_proc
287 static LRESULT call_hook_proc( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam,
288 BOOL prev_unicode, BOOL next_unicode )
290 LRESULT ret;
292 if (TRACE_ON(relay))
293 DPRINTF( "%04x:Call hook proc %p (id=%s,code=%x,wp=%08lx,lp=%08lx)\n",
294 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam );
296 if (!prev_unicode == !next_unicode) ret = proc( code, wparam, lparam );
297 else if (prev_unicode) ret = call_hook_WtoA( proc, id, code, wparam, lparam );
298 else ret = call_hook_AtoW( proc, id, code, wparam, lparam );
300 if (TRACE_ON(relay))
301 DPRINTF( "%04x:Ret hook proc %p (id=%s,code=%x,wp=%08lx,lp=%08lx) retval=%08lx\n",
302 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, ret );
304 return ret;
308 /***********************************************************************
309 * get_hook_proc
311 * Retrieve the hook procedure real value for a module-relative proc
313 static void *get_hook_proc( void *proc, const WCHAR *module )
315 HMODULE mod;
317 if (!(mod = GetModuleHandleW(module)))
319 TRACE( "loading %s\n", debugstr_w(module) );
320 /* FIXME: the library will never be freed */
321 if (!(mod = LoadLibraryW(module))) return NULL;
323 return (char *)mod + (ULONG_PTR)proc;
326 /***********************************************************************
327 * call_hook
329 * Call hook either in current thread or send message to the destination
330 * thread.
332 static LRESULT call_hook( struct hook_info *info, INT code, WPARAM wparam, LPARAM lparam )
334 DWORD_PTR ret = 0;
336 if (info->tid)
338 struct hook_extra_info h_extra;
339 h_extra.handle = info->handle;
340 h_extra.lparam = lparam;
342 TRACE( "calling hook in thread %04x %s code %x wp %lx lp %lx\n",
343 info->tid, hook_names[info->id-WH_MINHOOK], code, wparam, lparam );
345 switch(info->id)
347 case WH_KEYBOARD_LL:
348 MSG_SendInternalMessageTimeout( info->pid, info->tid, WM_WINE_KEYBOARD_LL_HOOK,
349 wparam, (LPARAM)&h_extra, SMTO_ABORTIFHUNG,
350 get_ll_hook_timeout(), &ret );
351 break;
352 case WH_MOUSE_LL:
353 MSG_SendInternalMessageTimeout( info->pid, info->tid, WM_WINE_MOUSE_LL_HOOK,
354 wparam, (LPARAM)&h_extra, SMTO_ABORTIFHUNG,
355 get_ll_hook_timeout(), &ret );
356 break;
357 default:
358 ERR("Unknown hook id %d\n", info->id);
359 assert(0);
360 break;
363 else if (info->proc)
365 TRACE( "calling hook %p %s code %x wp %lx lp %lx module %s\n",
366 info->proc, hook_names[info->id-WH_MINHOOK], code, wparam,
367 lparam, debugstr_w(info->module) );
369 if (!info->module[0] ||
370 (info->proc = get_hook_proc( info->proc, info->module )) != NULL)
372 struct user_thread_info *thread_info = get_user_thread_info();
373 HHOOK prev = thread_info->hook;
374 BOOL prev_unicode = thread_info->hook_unicode;
376 thread_info->hook = info->handle;
377 thread_info->hook_unicode = info->next_unicode;
378 ret = call_hook_proc( (HOOKPROC)info->proc, info->id, code, wparam, lparam,
379 info->prev_unicode, info->next_unicode );
380 thread_info->hook = prev;
381 thread_info->hook_unicode = prev_unicode;
384 return ret;
387 /***********************************************************************
388 * HOOK_CallHooks
390 LRESULT HOOK_CallHooks( INT id, INT code, WPARAM wparam, LPARAM lparam, BOOL unicode )
392 struct user_thread_info *thread_info = get_user_thread_info();
393 struct hook_info info;
394 DWORD_PTR ret = 0;
396 USER_CheckNotLock();
398 if (!HOOK_IsHooked( id ))
400 TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
401 return 0;
404 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
405 info.prev_unicode = unicode;
406 info.id = id;
408 SERVER_START_REQ( start_hook_chain )
410 req->id = info.id;
411 req->event = EVENT_MIN;
412 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
413 if (!wine_server_call( req ))
415 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
416 info.handle = reply->handle;
417 info.pid = reply->pid;
418 info.tid = reply->tid;
419 info.proc = reply->proc;
420 info.next_unicode = reply->unicode;
421 thread_info->active_hooks = reply->active_hooks;
424 SERVER_END_REQ;
426 if (!info.tid && !info.proc) return 0;
427 ret = call_hook( &info, code, wparam, lparam );
429 SERVER_START_REQ( finish_hook_chain )
431 req->id = id;
432 wine_server_call( req );
434 SERVER_END_REQ;
435 return ret;
439 /***********************************************************************
440 * HOOK_IsHooked
442 BOOL HOOK_IsHooked( INT id )
444 struct user_thread_info *thread_info = get_user_thread_info();
446 if (!thread_info->active_hooks) return TRUE;
447 return (thread_info->active_hooks & (1 << (id - WH_MINHOOK))) != 0;
451 /***********************************************************************
452 * SetWindowsHookA (USER32.@)
454 HHOOK WINAPI SetWindowsHookA( INT id, HOOKPROC proc )
456 return SetWindowsHookExA( id, proc, 0, GetCurrentThreadId() );
460 /***********************************************************************
461 * SetWindowsHookW (USER32.@)
463 HHOOK WINAPI SetWindowsHookW( INT id, HOOKPROC proc )
465 return SetWindowsHookExW( id, proc, 0, GetCurrentThreadId() );
469 /***********************************************************************
470 * SetWindowsHookExA (USER32.@)
472 HHOOK WINAPI SetWindowsHookExA( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
474 return set_windows_hook( id, proc, inst, tid, FALSE );
477 /***********************************************************************
478 * SetWindowsHookExW (USER32.@)
480 HHOOK WINAPI SetWindowsHookExW( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
482 return set_windows_hook( id, proc, inst, tid, TRUE );
486 /***********************************************************************
487 * UnhookWindowsHook (USER32.@)
489 BOOL WINAPI UnhookWindowsHook( INT id, HOOKPROC proc )
491 BOOL ret;
493 TRACE( "%s %p\n", hook_names[id-WH_MINHOOK], proc );
495 SERVER_START_REQ( remove_hook )
497 req->handle = 0;
498 req->id = id;
499 req->proc = proc;
500 ret = !wine_server_call_err( req );
501 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
503 SERVER_END_REQ;
504 if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
505 return ret;
510 /***********************************************************************
511 * UnhookWindowsHookEx (USER32.@)
513 BOOL WINAPI UnhookWindowsHookEx( HHOOK hhook )
515 BOOL ret;
517 TRACE( "%p\n", hhook );
519 SERVER_START_REQ( remove_hook )
521 req->handle = hhook;
522 req->id = 0;
523 ret = !wine_server_call_err( req );
524 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
526 SERVER_END_REQ;
527 if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
528 return ret;
532 /***********************************************************************
533 * CallNextHookEx (USER32.@)
535 LRESULT WINAPI CallNextHookEx( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
537 struct user_thread_info *thread_info = get_user_thread_info();
538 struct hook_info info;
540 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
542 SERVER_START_REQ( get_hook_info )
544 req->handle = thread_info->hook;
545 req->get_next = 1;
546 req->event = EVENT_MIN;
547 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
548 if (!wine_server_call_err( req ))
550 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
551 info.handle = reply->handle;
552 info.id = reply->id;
553 info.pid = reply->pid;
554 info.tid = reply->tid;
555 info.proc = reply->proc;
556 info.next_unicode = reply->unicode;
559 SERVER_END_REQ;
561 info.prev_unicode = thread_info->hook_unicode;
562 return call_hook( &info, code, wparam, lparam );
566 LRESULT call_current_hook( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
568 struct hook_info info;
570 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
572 SERVER_START_REQ( get_hook_info )
574 req->handle = hhook;
575 req->get_next = 0;
576 req->event = EVENT_MIN;
577 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
578 if (!wine_server_call_err( req ))
580 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
581 info.handle = reply->handle;
582 info.id = reply->id;
583 info.pid = reply->pid;
584 info.tid = reply->tid;
585 info.proc = reply->proc;
586 info.next_unicode = reply->unicode;
589 SERVER_END_REQ;
591 info.prev_unicode = TRUE; /* assume Unicode for this function */
592 return call_hook( &info, code, wparam, lparam );
595 /***********************************************************************
596 * CallMsgFilterA (USER32.@)
598 BOOL WINAPI CallMsgFilterA( LPMSG msg, INT code )
600 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, FALSE )) return TRUE;
601 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, FALSE );
605 /***********************************************************************
606 * CallMsgFilterW (USER32.@)
608 BOOL WINAPI CallMsgFilterW( LPMSG msg, INT code )
610 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, TRUE )) return TRUE;
611 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, TRUE );
615 /***********************************************************************
616 * SetWinEventHook [USER32.@]
618 * Set up an event hook for a set of events.
620 * PARAMS
621 * event_min [I] Lowest event handled by pfnProc
622 * event_max [I] Highest event handled by pfnProc
623 * inst [I] DLL containing pfnProc
624 * proc [I] Callback event hook function
625 * pid [I] Process to get events from, or 0 for all processes
626 * tid [I] Thread to get events from, or 0 for all threads
627 * flags [I] Flags indicating the status of pfnProc
629 * RETURNS
630 * Success: A handle representing the hook.
631 * Failure: A NULL handle.
633 HWINEVENTHOOK WINAPI SetWinEventHook(DWORD event_min, DWORD event_max,
634 HMODULE inst, WINEVENTPROC proc,
635 DWORD pid, DWORD tid, DWORD flags)
637 HWINEVENTHOOK handle = 0;
638 WCHAR module[MAX_PATH];
639 DWORD len;
641 TRACE("%d,%d,%p,%p,%08x,%04x,%08x\n", event_min, event_max, inst,
642 proc, pid, tid, flags);
644 if (inst)
646 if (!(len = GetModuleFileNameW(inst, module, MAX_PATH)) || len >= MAX_PATH)
647 inst = 0;
650 if ((flags & WINEVENT_INCONTEXT) && !inst)
652 SetLastError(ERROR_HOOK_NEEDS_HMOD);
653 return 0;
656 if (event_min > event_max)
658 SetLastError(ERROR_INVALID_HOOK_FILTER);
659 return 0;
662 /* FIXME: what if the tid or pid belongs to another process? */
663 if (tid) /* thread-local hook */
664 inst = 0;
666 SERVER_START_REQ( set_hook )
668 req->id = WH_WINEVENT;
669 req->pid = pid;
670 req->tid = tid;
671 req->event_min = event_min;
672 req->event_max = event_max;
673 req->flags = flags;
674 req->unicode = 1;
675 if (inst) /* make proc relative to the module base */
677 req->proc = (void *)((char *)proc - (char *)inst);
678 wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
680 else req->proc = proc;
682 if (!wine_server_call_err( req ))
684 handle = reply->handle;
685 get_user_thread_info()->active_hooks = reply->active_hooks;
688 SERVER_END_REQ;
690 TRACE("-> %p\n", handle);
691 return handle;
695 /***********************************************************************
696 * UnhookWinEvent [USER32.@]
698 * Remove an event hook for a set of events.
700 * PARAMS
701 * hEventHook [I] Event hook to remove
703 * RETURNS
704 * Success: TRUE. The event hook has been removed.
705 * Failure: FALSE, if hEventHook is invalid.
707 BOOL WINAPI UnhookWinEvent(HWINEVENTHOOK hEventHook)
709 BOOL ret;
711 TRACE( "%p\n", hEventHook );
713 SERVER_START_REQ( remove_hook )
715 req->handle = hEventHook;
716 req->id = WH_WINEVENT;
717 ret = !wine_server_call_err( req );
718 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
720 SERVER_END_REQ;
721 return ret;
724 static inline BOOL find_first_hook(DWORD id, DWORD event, HWND hwnd, LONG object_id,
725 LONG child_id, struct hook_info *info)
727 struct user_thread_info *thread_info = get_user_thread_info();
728 BOOL ret;
730 if (!HOOK_IsHooked( id ))
732 TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
733 return FALSE;
736 SERVER_START_REQ( start_hook_chain )
738 req->id = id;
739 req->event = event;
740 req->window = hwnd;
741 req->object_id = object_id;
742 req->child_id = child_id;
743 wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
744 ret = !wine_server_call( req );
745 if (ret)
747 info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
748 info->handle = reply->handle;
749 info->proc = reply->proc;
750 info->tid = reply->tid;
751 thread_info->active_hooks = reply->active_hooks;
754 SERVER_END_REQ;
755 return ret && (info->tid || info->proc);
758 static inline BOOL find_next_hook(DWORD event, HWND hwnd, LONG object_id,
759 LONG child_id, struct hook_info *info)
761 BOOL ret;
763 SERVER_START_REQ( get_hook_info )
765 req->handle = info->handle;
766 req->get_next = 1;
767 req->event = event;
768 req->window = hwnd;
769 req->object_id = object_id;
770 req->child_id = child_id;
771 wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
772 ret = !wine_server_call( req );
773 if (ret)
775 info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
776 info->handle = reply->handle;
777 info->proc = reply->proc;
778 info->tid = reply->tid;
781 SERVER_END_REQ;
782 return ret;
785 static inline void find_hook_close(DWORD id)
787 SERVER_START_REQ( finish_hook_chain )
789 req->id = id;
790 wine_server_call( req );
792 SERVER_END_REQ;
795 /***********************************************************************
796 * NotifyWinEvent [USER32.@]
798 * Inform the OS that an event has occurred.
800 * PARAMS
801 * event [I] Id of the event
802 * hwnd [I] Window holding the object that created the event
803 * object_id [I] Type of object that created the event
804 * child_id [I] Child object of nId, or CHILDID_SELF.
806 * RETURNS
807 * Nothing.
809 void WINAPI NotifyWinEvent(DWORD event, HWND hwnd, LONG object_id, LONG child_id)
811 struct hook_info info;
813 TRACE("%04x,%p,%d,%d\n", event, hwnd, object_id, child_id);
815 if (!hwnd)
817 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
818 return;
821 USER_CheckNotLock();
823 #if 0
824 if (event & 0x80000000)
826 /* FIXME: on 64-bit platforms we need to invent some other way for
827 * passing parameters, nId and nChildId can't hold full [W|L]PARAM.
828 * struct call_hook *hook = (LRESULT *)hWnd;
829 * wparam = hook->wparam;
830 * lparam = hook->lparam;
832 LRESULT *ret = (LRESULT *)hwnd;
833 INT id, code, unicode;
835 id = (dwEvent & 0x7fff0000) >> 16;
836 code = event & 0x7fff;
837 unicode = event & 0x8000;
838 *ret = HOOK_CallHooks(id, code, object_id, child_id, unicode);
839 return;
841 #endif
843 if (!find_first_hook(WH_WINEVENT, event, hwnd, object_id, child_id, &info)) return;
847 if (info.proc)
849 TRACE( "calling WH_WINEVENT hook %p event %x hwnd %p %x %x module %s\n",
850 info.proc, event, hwnd, object_id, child_id, debugstr_w(info.module) );
852 if (!info.module[0] || (info.proc = get_hook_proc( info.proc, info.module )) != NULL)
854 if (TRACE_ON(relay))
855 DPRINTF( "%04x:Call winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
856 GetCurrentThreadId(), info.proc, info.handle, event, hwnd, object_id,
857 child_id, GetCurrentThreadId(), GetCurrentTime());
859 info.proc(info.handle, event, hwnd, object_id, child_id,
860 GetCurrentThreadId(), GetCurrentTime());
862 if (TRACE_ON(relay))
863 DPRINTF( "%04x:Ret winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
864 GetCurrentThreadId(), info.proc, info.handle, event, hwnd, object_id,
865 child_id, GetCurrentThreadId(), GetCurrentTime());
868 else
869 break;
871 while (find_next_hook(event, hwnd, object_id, child_id, &info));
873 find_hook_close(WH_WINEVENT);
877 /***********************************************************************
878 * IsWinEventHookInstalled [USER32.@]
880 * Determine if an event hook is installed for an event.
882 * PARAMS
883 * dwEvent [I] Id of the event
885 * RETURNS
886 * TRUE, If there are any hooks installed for the event.
887 * FALSE, Otherwise.
889 * BUGS
890 * Not implemented.
892 BOOL WINAPI IsWinEventHookInstalled(DWORD dwEvent)
894 FIXME("(%d)-stub!\n", dwEvent);
895 return TRUE;