Revert "winex11: Refuse to set the pixel format for HWND_MESSAGE windows."
[wine.git] / dlls / user32 / hook.c
blobc9e9e208a1f81e21d67359962209e3e0f6bdff9f
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;
206 /***********************************************************************
207 * call_hook_AtoW
209 static LRESULT call_hook_AtoW( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
211 LRESULT ret;
212 UNICODE_STRING usBuffer;
213 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
214 else
216 CBT_CREATEWNDA *cbtcwA = (CBT_CREATEWNDA *)lparam;
217 CBT_CREATEWNDW cbtcwW;
218 CREATESTRUCTW csW;
219 LPWSTR nameW = NULL;
220 LPWSTR classW = NULL;
222 cbtcwW.lpcs = &csW;
223 cbtcwW.hwndInsertAfter = cbtcwA->hwndInsertAfter;
224 csW = *(CREATESTRUCTW *)cbtcwA->lpcs;
226 if (!IS_INTRESOURCE(cbtcwA->lpcs->lpszName))
228 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszName);
229 csW.lpszName = nameW = usBuffer.Buffer;
231 if (!IS_INTRESOURCE(cbtcwA->lpcs->lpszClass))
233 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszClass);
234 csW.lpszClass = classW = usBuffer.Buffer;
236 ret = proc( code, wparam, (LPARAM)&cbtcwW );
237 cbtcwA->hwndInsertAfter = cbtcwW.hwndInsertAfter;
238 HeapFree( GetProcessHeap(), 0, nameW );
239 HeapFree( GetProcessHeap(), 0, classW );
241 return ret;
245 /***********************************************************************
246 * call_hook_WtoA
248 static LRESULT call_hook_WtoA( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
250 LRESULT ret;
252 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
253 else
255 CBT_CREATEWNDW *cbtcwW = (CBT_CREATEWNDW *)lparam;
256 CBT_CREATEWNDA cbtcwA;
257 CREATESTRUCTA csA;
258 int len;
259 LPSTR nameA = NULL;
260 LPSTR classA = NULL;
262 cbtcwA.lpcs = &csA;
263 cbtcwA.hwndInsertAfter = cbtcwW->hwndInsertAfter;
264 csA = *(CREATESTRUCTA *)cbtcwW->lpcs;
266 if (!IS_INTRESOURCE(cbtcwW->lpcs->lpszName)) {
267 len = WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszName, -1, NULL, 0, NULL, NULL );
268 nameA = HeapAlloc( GetProcessHeap(), 0, len*sizeof(CHAR) );
269 WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszName, -1, nameA, len, NULL, NULL );
270 csA.lpszName = nameA;
273 if (!IS_INTRESOURCE(cbtcwW->lpcs->lpszClass)) {
274 len = WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszClass, -1, NULL, 0, NULL, NULL );
275 classA = HeapAlloc( GetProcessHeap(), 0, len*sizeof(CHAR) );
276 WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszClass, -1, classA, len, NULL, NULL );
277 csA.lpszClass = classA;
280 ret = proc( code, wparam, (LPARAM)&cbtcwA );
281 cbtcwW->hwndInsertAfter = cbtcwA.hwndInsertAfter;
282 HeapFree( GetProcessHeap(), 0, nameA );
283 HeapFree( GetProcessHeap(), 0, classA );
285 return ret;
289 /***********************************************************************
290 * call_hook_proc
292 static LRESULT call_hook_proc( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam,
293 BOOL prev_unicode, BOOL next_unicode )
295 LRESULT ret;
297 if (TRACE_ON(relay))
298 DPRINTF( "%04x:Call hook proc %p (id=%s,code=%x,wp=%08lx,lp=%08lx)\n",
299 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam );
301 if (!prev_unicode == !next_unicode) ret = proc( code, wparam, lparam );
302 else if (prev_unicode) ret = call_hook_WtoA( proc, id, code, wparam, lparam );
303 else ret = call_hook_AtoW( proc, id, code, wparam, lparam );
305 if (TRACE_ON(relay))
306 DPRINTF( "%04x:Ret hook proc %p (id=%s,code=%x,wp=%08lx,lp=%08lx) retval=%08lx\n",
307 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, ret );
309 return ret;
313 /***********************************************************************
314 * get_hook_proc
316 * Retrieve the hook procedure real value for a module-relative proc
318 void *get_hook_proc( void *proc, const WCHAR *module )
320 HMODULE mod;
322 if (!(mod = GetModuleHandleW(module)))
324 TRACE( "loading %s\n", debugstr_w(module) );
325 /* FIXME: the library will never be freed */
326 if (!(mod = LoadLibraryExW(module, NULL, LOAD_WITH_ALTERED_SEARCH_PATH))) return NULL;
328 return (char *)mod + (ULONG_PTR)proc;
331 /***********************************************************************
332 * call_hook
334 * Call hook either in current thread or send message to the destination
335 * thread.
337 static LRESULT call_hook( struct hook_info *info, INT code, WPARAM wparam, LPARAM lparam )
339 DWORD_PTR ret = 0;
341 if (info->tid)
343 struct hook_extra_info h_extra;
344 h_extra.handle = info->handle;
345 h_extra.lparam = lparam;
347 TRACE( "calling hook in thread %04x %s code %x wp %lx lp %lx\n",
348 info->tid, hook_names[info->id-WH_MINHOOK], code, wparam, lparam );
350 switch(info->id)
352 case WH_KEYBOARD_LL:
353 MSG_SendInternalMessageTimeout( info->pid, info->tid, WM_WINE_KEYBOARD_LL_HOOK,
354 wparam, (LPARAM)&h_extra, SMTO_ABORTIFHUNG,
355 get_ll_hook_timeout(), &ret );
356 break;
357 case WH_MOUSE_LL:
358 MSG_SendInternalMessageTimeout( info->pid, info->tid, WM_WINE_MOUSE_LL_HOOK,
359 wparam, (LPARAM)&h_extra, SMTO_ABORTIFHUNG,
360 get_ll_hook_timeout(), &ret );
361 break;
362 default:
363 ERR("Unknown hook id %d\n", info->id);
364 assert(0);
365 break;
368 else if (info->proc)
370 TRACE( "calling hook %p %s code %x wp %lx lp %lx module %s\n",
371 info->proc, hook_names[info->id-WH_MINHOOK], code, wparam,
372 lparam, debugstr_w(info->module) );
374 if (!info->module[0] ||
375 (info->proc = get_hook_proc( info->proc, info->module )) != NULL)
377 struct user_thread_info *thread_info = get_user_thread_info();
378 HHOOK prev = thread_info->hook;
379 BOOL prev_unicode = thread_info->hook_unicode;
381 thread_info->hook = info->handle;
382 thread_info->hook_unicode = info->next_unicode;
383 ret = call_hook_proc( info->proc, info->id, code, wparam, lparam,
384 info->prev_unicode, info->next_unicode );
385 thread_info->hook = prev;
386 thread_info->hook_unicode = prev_unicode;
390 if (info->id == WH_KEYBOARD_LL || info->id == WH_MOUSE_LL)
391 get_user_thread_info()->key_state_time = 0; /* force refreshing the key state cache */
393 return ret;
397 /***********************************************************************
398 * HOOK_IsHooked
400 static BOOL HOOK_IsHooked( INT id )
402 struct user_thread_info *thread_info = get_user_thread_info();
404 if (!thread_info->active_hooks) return TRUE;
405 return (thread_info->active_hooks & (1 << (id - WH_MINHOOK))) != 0;
409 /***********************************************************************
410 * HOOK_CallHooks
412 LRESULT HOOK_CallHooks( INT id, INT code, WPARAM wparam, LPARAM lparam, BOOL unicode )
414 struct user_thread_info *thread_info = get_user_thread_info();
415 struct hook_info info;
416 DWORD_PTR ret = 0;
418 USER_CheckNotLock();
420 if (!HOOK_IsHooked( id ))
422 TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
423 return 0;
426 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
427 info.prev_unicode = unicode;
428 info.id = id;
430 SERVER_START_REQ( start_hook_chain )
432 req->id = info.id;
433 req->event = EVENT_MIN;
434 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
435 if (!wine_server_call( req ))
437 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
438 info.handle = wine_server_ptr_handle( reply->handle );
439 info.pid = reply->pid;
440 info.tid = reply->tid;
441 info.proc = wine_server_get_ptr( reply->proc );
442 info.next_unicode = reply->unicode;
443 thread_info->active_hooks = reply->active_hooks;
446 SERVER_END_REQ;
448 if (!info.tid && !info.proc) return 0;
449 ret = call_hook( &info, code, wparam, lparam );
451 SERVER_START_REQ( finish_hook_chain )
453 req->id = id;
454 wine_server_call( req );
456 SERVER_END_REQ;
457 return ret;
461 /***********************************************************************
462 * SetWindowsHookA (USER32.@)
464 HHOOK WINAPI SetWindowsHookA( INT id, HOOKPROC proc )
466 return SetWindowsHookExA( id, proc, 0, GetCurrentThreadId() );
470 /***********************************************************************
471 * SetWindowsHookW (USER32.@)
473 HHOOK WINAPI SetWindowsHookW( INT id, HOOKPROC proc )
475 return SetWindowsHookExW( id, proc, 0, GetCurrentThreadId() );
479 /***********************************************************************
480 * SetWindowsHookExA (USER32.@)
482 HHOOK WINAPI SetWindowsHookExA( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
484 return set_windows_hook( id, proc, inst, tid, FALSE );
487 /***********************************************************************
488 * SetWindowsHookExW (USER32.@)
490 HHOOK WINAPI SetWindowsHookExW( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
492 return set_windows_hook( id, proc, inst, tid, TRUE );
496 /***********************************************************************
497 * UnhookWindowsHook (USER32.@)
499 BOOL WINAPI UnhookWindowsHook( INT id, HOOKPROC proc )
501 BOOL ret;
503 TRACE( "%s %p\n", hook_names[id-WH_MINHOOK], proc );
505 SERVER_START_REQ( remove_hook )
507 req->handle = 0;
508 req->id = id;
509 req->proc = wine_server_client_ptr( proc );
510 ret = !wine_server_call_err( req );
511 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
513 SERVER_END_REQ;
514 if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
515 return ret;
520 /***********************************************************************
521 * UnhookWindowsHookEx (USER32.@)
523 BOOL WINAPI UnhookWindowsHookEx( HHOOK hhook )
525 BOOL ret;
527 SERVER_START_REQ( remove_hook )
529 req->handle = wine_server_user_handle( hhook );
530 req->id = 0;
531 ret = !wine_server_call_err( req );
532 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
534 SERVER_END_REQ;
535 if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
536 return ret;
540 /***********************************************************************
541 * CallNextHookEx (USER32.@)
543 LRESULT WINAPI CallNextHookEx( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
545 struct user_thread_info *thread_info = get_user_thread_info();
546 struct hook_info info;
548 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
550 SERVER_START_REQ( get_hook_info )
552 req->handle = wine_server_user_handle( thread_info->hook );
553 req->get_next = 1;
554 req->event = EVENT_MIN;
555 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
556 if (!wine_server_call_err( req ))
558 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
559 info.handle = wine_server_ptr_handle( reply->handle );
560 info.id = reply->id;
561 info.pid = reply->pid;
562 info.tid = reply->tid;
563 info.proc = wine_server_get_ptr( reply->proc );
564 info.next_unicode = reply->unicode;
567 SERVER_END_REQ;
569 info.prev_unicode = thread_info->hook_unicode;
570 return call_hook( &info, code, wparam, lparam );
574 LRESULT call_current_hook( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
576 struct hook_info info;
578 ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
580 SERVER_START_REQ( get_hook_info )
582 req->handle = wine_server_user_handle( hhook );
583 req->get_next = 0;
584 req->event = EVENT_MIN;
585 wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
586 if (!wine_server_call_err( req ))
588 info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
589 info.handle = wine_server_ptr_handle( reply->handle );
590 info.id = reply->id;
591 info.pid = reply->pid;
592 info.tid = reply->tid;
593 info.proc = wine_server_get_ptr( reply->proc );
594 info.next_unicode = reply->unicode;
597 SERVER_END_REQ;
599 info.prev_unicode = TRUE; /* assume Unicode for this function */
600 return call_hook( &info, code, wparam, lparam );
603 /***********************************************************************
604 * CallMsgFilterA (USER32.@)
606 BOOL WINAPI CallMsgFilterA( LPMSG msg, INT code )
608 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, FALSE )) return TRUE;
609 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, FALSE );
613 /***********************************************************************
614 * CallMsgFilterW (USER32.@)
616 BOOL WINAPI CallMsgFilterW( LPMSG msg, INT code )
618 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, TRUE )) return TRUE;
619 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, TRUE );
623 /***********************************************************************
624 * SetWinEventHook [USER32.@]
626 * Set up an event hook for a set of events.
628 * PARAMS
629 * event_min [I] Lowest event handled by pfnProc
630 * event_max [I] Highest event handled by pfnProc
631 * inst [I] DLL containing pfnProc
632 * proc [I] Callback event hook function
633 * pid [I] Process to get events from, or 0 for all processes
634 * tid [I] Thread to get events from, or 0 for all threads
635 * flags [I] Flags indicating the status of pfnProc
637 * RETURNS
638 * Success: A handle representing the hook.
639 * Failure: A NULL handle.
641 HWINEVENTHOOK WINAPI SetWinEventHook(DWORD event_min, DWORD event_max,
642 HMODULE inst, WINEVENTPROC proc,
643 DWORD pid, DWORD tid, DWORD flags)
645 HWINEVENTHOOK handle = 0;
646 WCHAR module[MAX_PATH];
647 DWORD len;
649 TRACE("%d,%d,%p,%p,%08x,%04x,%08x\n", event_min, event_max, inst,
650 proc, pid, tid, flags);
652 if (inst)
654 if (!(len = GetModuleFileNameW(inst, module, MAX_PATH)) || len >= MAX_PATH)
655 inst = 0;
658 if ((flags & WINEVENT_INCONTEXT) && !inst)
660 SetLastError(ERROR_HOOK_NEEDS_HMOD);
661 return 0;
664 if (event_min > event_max)
666 SetLastError(ERROR_INVALID_HOOK_FILTER);
667 return 0;
670 /* FIXME: what if the tid or pid belongs to another process? */
671 if (tid) /* thread-local hook */
672 inst = 0;
674 SERVER_START_REQ( set_hook )
676 req->id = WH_WINEVENT;
677 req->pid = pid;
678 req->tid = tid;
679 req->event_min = event_min;
680 req->event_max = event_max;
681 req->flags = flags;
682 req->unicode = 1;
683 if (inst) /* make proc relative to the module base */
685 req->proc = wine_server_client_ptr( (void *)((char *)proc - (char *)inst) );
686 wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
688 else req->proc = wine_server_client_ptr( proc );
690 if (!wine_server_call_err( req ))
692 handle = wine_server_ptr_handle( reply->handle );
693 get_user_thread_info()->active_hooks = reply->active_hooks;
696 SERVER_END_REQ;
698 TRACE("-> %p\n", handle);
699 return handle;
703 /***********************************************************************
704 * UnhookWinEvent [USER32.@]
706 * Remove an event hook for a set of events.
708 * PARAMS
709 * hEventHook [I] Event hook to remove
711 * RETURNS
712 * Success: TRUE. The event hook has been removed.
713 * Failure: FALSE, if hEventHook is invalid.
715 BOOL WINAPI UnhookWinEvent(HWINEVENTHOOK hEventHook)
717 BOOL ret;
719 SERVER_START_REQ( remove_hook )
721 req->handle = wine_server_user_handle( hEventHook );
722 req->id = WH_WINEVENT;
723 ret = !wine_server_call_err( req );
724 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
726 SERVER_END_REQ;
727 return ret;
730 static inline BOOL find_first_hook(DWORD id, DWORD event, HWND hwnd, LONG object_id,
731 LONG child_id, struct hook_info *info)
733 struct user_thread_info *thread_info = get_user_thread_info();
734 BOOL ret;
736 if (!HOOK_IsHooked( id ))
738 TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
739 return FALSE;
742 SERVER_START_REQ( start_hook_chain )
744 req->id = id;
745 req->event = event;
746 req->window = wine_server_user_handle( hwnd );
747 req->object_id = object_id;
748 req->child_id = child_id;
749 wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
750 ret = !wine_server_call( req );
751 if (ret)
753 info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
754 info->handle = wine_server_ptr_handle( reply->handle );
755 info->proc = wine_server_get_ptr( reply->proc );
756 info->tid = reply->tid;
757 thread_info->active_hooks = reply->active_hooks;
760 SERVER_END_REQ;
761 return ret && (info->tid || info->proc);
764 static inline BOOL find_next_hook(DWORD event, HWND hwnd, LONG object_id,
765 LONG child_id, struct hook_info *info)
767 BOOL ret;
769 SERVER_START_REQ( get_hook_info )
771 req->handle = wine_server_user_handle( info->handle );
772 req->get_next = 1;
773 req->event = event;
774 req->window = wine_server_user_handle( hwnd );
775 req->object_id = object_id;
776 req->child_id = child_id;
777 wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
778 ret = !wine_server_call( req );
779 if (ret)
781 info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
782 info->handle = wine_server_ptr_handle( reply->handle );
783 info->proc = wine_server_get_ptr( reply->proc );
784 info->tid = reply->tid;
787 SERVER_END_REQ;
788 return ret;
791 static inline void find_hook_close(DWORD id)
793 SERVER_START_REQ( finish_hook_chain )
795 req->id = id;
796 wine_server_call( req );
798 SERVER_END_REQ;
801 /***********************************************************************
802 * NotifyWinEvent [USER32.@]
804 * Inform the OS that an event has occurred.
806 * PARAMS
807 * event [I] Id of the event
808 * hwnd [I] Window holding the object that created the event
809 * object_id [I] Type of object that created the event
810 * child_id [I] Child object of nId, or CHILDID_SELF.
812 * RETURNS
813 * Nothing.
815 void WINAPI NotifyWinEvent(DWORD event, HWND hwnd, LONG object_id, LONG child_id)
817 struct hook_info info;
819 TRACE("%04x,%p,%d,%d\n", event, hwnd, object_id, child_id);
821 if (!hwnd)
823 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
824 return;
827 USER_CheckNotLock();
829 #if 0
830 if (event & 0x80000000)
832 /* FIXME: on 64-bit platforms we need to invent some other way for
833 * passing parameters, nId and nChildId can't hold full [W|L]PARAM.
834 * struct call_hook *hook = (LRESULT *)hWnd;
835 * wparam = hook->wparam;
836 * lparam = hook->lparam;
838 LRESULT *ret = (LRESULT *)hwnd;
839 INT id, code, unicode;
841 id = (dwEvent & 0x7fff0000) >> 16;
842 code = event & 0x7fff;
843 unicode = event & 0x8000;
844 *ret = HOOK_CallHooks(id, code, object_id, child_id, unicode);
845 return;
847 #endif
849 if (!find_first_hook(WH_WINEVENT, event, hwnd, object_id, child_id, &info)) return;
853 WINEVENTPROC proc = info.proc;
854 if (proc)
856 TRACE( "calling WH_WINEVENT hook %p event %x hwnd %p %x %x module %s\n",
857 proc, event, hwnd, object_id, child_id, debugstr_w(info.module) );
859 if (!info.module[0] || (proc = get_hook_proc( proc, info.module )) != NULL)
861 if (TRACE_ON(relay))
862 DPRINTF( "%04x:Call winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
863 GetCurrentThreadId(), proc, info.handle, event, hwnd, object_id,
864 child_id, GetCurrentThreadId(), GetCurrentTime());
866 proc( info.handle, event, hwnd, object_id, child_id,
867 GetCurrentThreadId(), GetCurrentTime());
869 if (TRACE_ON(relay))
870 DPRINTF( "%04x:Ret winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
871 GetCurrentThreadId(), proc, info.handle, event, hwnd, object_id,
872 child_id, GetCurrentThreadId(), GetCurrentTime());
875 else
876 break;
878 while (find_next_hook(event, hwnd, object_id, child_id, &info));
880 find_hook_close(WH_WINEVENT);
884 /***********************************************************************
885 * IsWinEventHookInstalled [USER32.@]
887 * Determine if an event hook is installed for an event.
889 * PARAMS
890 * dwEvent [I] Id of the event
892 * RETURNS
893 * TRUE, If there are any hooks installed for the event.
894 * FALSE, Otherwise.
896 * BUGS
897 * Not implemented.
899 BOOL WINAPI IsWinEventHookInstalled(DWORD dwEvent)
901 /* FIXME: Needed by Office 2007 installer */
902 WARN("(%d)-stub!\n", dwEvent);
903 return TRUE;