wined3d: Implement a detection for the MacOS OpenGL implementation.
[wine] / dlls / user32 / hook.c
1 /*
2  * Windows hook functions
3  *
4  * Copyright 2002 Alexandre Julliard
5  * Copyright 2005 Dmitry Timoshkov
6  *
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.
11  *
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.
16  *
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
20  *
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
63  */
64
65 #include "config.h"
66 #include "wine/port.h"
67
68 #include <stdarg.h>
69 #include <assert.h>
70
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"
81
82 WINE_DEFAULT_DEBUG_CHANNEL(hook);
83 WINE_DECLARE_DEBUG_CHANNEL(relay);
84
85 struct hook_info
86 {
87     INT id;
88     FARPROC proc;
89     void *handle;
90     DWORD pid, tid;
91     BOOL prev_unicode, next_unicode;
92     WCHAR module[MAX_PATH];
93 };
94
95 #define WH_WINEVENT (WH_MAXHOOK+1)
96
97 static const char * const hook_names[WH_WINEVENT - WH_MINHOOK + 1] =
98 {
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"
116 };
117
118
119 /***********************************************************************
120  *              get_ll_hook_timeout
121  *
122  */
123 static UINT get_ll_hook_timeout(void)
124 {
125     /* FIXME: should retrieve LowLevelHooksTimeout in HKEY_CURRENT_USER\Control Panel\Desktop */
126     return 2000;
127 }
128
129
130 /***********************************************************************
131  *              set_windows_hook
132  *
133  * Implementation of SetWindowsHookExA and SetWindowsHookExW.
134  */
135 static HHOOK set_windows_hook( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid, BOOL unicode )
136 {
137     HHOOK handle = 0;
138     WCHAR module[MAX_PATH];
139     DWORD len;
140
141     if (!proc)
142     {
143         SetLastError( ERROR_INVALID_FILTER_PROC );
144         return 0;
145     }
146
147     if (tid)  /* thread-local hook */
148     {
149         if (id == WH_JOURNALRECORD ||
150             id == WH_JOURNALPLAYBACK ||
151             id == WH_KEYBOARD_LL ||
152             id == WH_MOUSE_LL ||
153             id == WH_SYSMSGFILTER)
154         {
155             /* these can only be global */
156             SetLastError( ERROR_INVALID_PARAMETER );
157             return 0;
158         }
159     }
160     else  /* system-global hook */
161     {
162         if (id == WH_KEYBOARD_LL || id == WH_MOUSE_LL) inst = 0;
163         else if (!inst)
164         {
165             SetLastError( ERROR_HOOK_NEEDS_HMOD );
166             return 0;
167         }
168     }
169
170     if (inst && (!(len = GetModuleFileNameW( inst, module, MAX_PATH )) || len >= MAX_PATH))
171     {
172         SetLastError( ERROR_INVALID_PARAMETER );
173         return 0;
174     }
175
176     SERVER_START_REQ( set_hook )
177     {
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 */
186         {
187             req->proc = (void *)((char *)proc - (char *)inst);
188             wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
189         }
190         else req->proc = proc;
191
192         if (!wine_server_call_err( req ))
193         {
194             handle = reply->handle;
195             get_user_thread_info()->active_hooks = reply->active_hooks;
196         }
197     }
198     SERVER_END_REQ;
199
200     TRACE( "%s %p %x -> %p\n", hook_names[id-WH_MINHOOK], proc, tid, handle );
201     return handle;
202 }
203
204
205 /***********************************************************************
206  *              call_hook_AtoW
207  */
208 static LRESULT call_hook_AtoW( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
209 {
210     LRESULT ret;
211     UNICODE_STRING usBuffer;
212     if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
213     else
214     {
215         CBT_CREATEWNDA *cbtcwA = (CBT_CREATEWNDA *)lparam;
216         CBT_CREATEWNDW cbtcwW;
217         CREATESTRUCTW csW;
218         LPWSTR nameW = NULL;
219         LPWSTR classW = NULL;
220
221         cbtcwW.lpcs = &csW;
222         cbtcwW.hwndInsertAfter = cbtcwA->hwndInsertAfter;
223         csW = *(CREATESTRUCTW *)cbtcwA->lpcs;
224
225         if (HIWORD(cbtcwA->lpcs->lpszName))
226         {
227             RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszName);
228             csW.lpszName = nameW = usBuffer.Buffer;
229         }
230         if (HIWORD(cbtcwA->lpcs->lpszClass))
231         {
232             RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszClass);
233             csW.lpszClass = classW = usBuffer.Buffer;
234         }
235         ret = proc( code, wparam, (LPARAM)&cbtcwW );
236         cbtcwA->hwndInsertAfter = cbtcwW.hwndInsertAfter;
237         HeapFree( GetProcessHeap(), 0, nameW );
238         HeapFree( GetProcessHeap(), 0, classW );
239     }
240     return ret;
241 }
242
243
244 /***********************************************************************
245  *              call_hook_WtoA
246  */
247 static LRESULT call_hook_WtoA( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
248 {
249     LRESULT ret;
250
251     if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
252     else
253     {
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;
260
261         cbtcwA.lpcs = &csA;
262         cbtcwA.hwndInsertAfter = cbtcwW->hwndInsertAfter;
263         csA = *(CREATESTRUCTA *)cbtcwW->lpcs;
264
265         if (HIWORD(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;
270         }
271
272         if (HIWORD(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;
277         }
278
279         ret = proc( code, wparam, (LPARAM)&cbtcwA );
280         cbtcwW->hwndInsertAfter = cbtcwA.hwndInsertAfter;
281         HeapFree( GetProcessHeap(), 0, nameA );
282         HeapFree( GetProcessHeap(), 0, classA );
283     }
284     return ret;
285 }
286
287
288 /***********************************************************************
289  *              call_hook_proc
290  */
291 static LRESULT call_hook_proc( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam,
292                                BOOL prev_unicode, BOOL next_unicode )
293 {
294     LRESULT ret;
295
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 );
299
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 );
303
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 );
307
308     return ret;
309 }
310
311
312 /***********************************************************************
313  *              get_hook_proc
314  *
315  * Retrieve the hook procedure real value for a module-relative proc
316  */
317 static void *get_hook_proc( void *proc, const WCHAR *module )
318 {
319     HMODULE mod;
320
321     if (!(mod = GetModuleHandleW(module)))
322     {
323         TRACE( "loading %s\n", debugstr_w(module) );
324         /* FIXME: the library will never be freed */
325         if (!(mod = LoadLibraryW(module))) return NULL;
326     }
327     return (char *)mod + (ULONG_PTR)proc;
328 }
329
330 /***********************************************************************
331  *              call_hook
332  *
333  * Call hook either in current thread or send message to the destination
334  * thread.
335  */
336 static LRESULT call_hook( struct hook_info *info, INT code, WPARAM wparam, LPARAM lparam )
337 {
338     DWORD_PTR ret = 0;
339
340     if (info->tid)
341     {
342         struct hook_extra_info h_extra;
343         h_extra.handle = info->handle;
344         h_extra.lparam = lparam;
345
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 );
348
349         switch(info->id)
350         {
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;
365         }
366     }
367     else if (info->proc)
368     {
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) );
372
373         if (!info->module[0] ||
374             (info->proc = get_hook_proc( info->proc, info->module )) != NULL)
375         {
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;
379
380             thread_info->hook = info->handle;
381             thread_info->hook_unicode = info->next_unicode;
382             ret = call_hook_proc( (HOOKPROC)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;
386         }
387     }
388     return ret;
389 }
390
391 /***********************************************************************
392  *              HOOK_CallHooks
393  */
394 LRESULT HOOK_CallHooks( INT id, INT code, WPARAM wparam, LPARAM lparam, BOOL unicode )
395 {
396     struct user_thread_info *thread_info = get_user_thread_info();
397     struct hook_info info;
398     DWORD_PTR ret = 0;
399
400     USER_CheckNotLock();
401
402     if (!HOOK_IsHooked( id ))
403     {
404         TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
405         return 0;
406     }
407
408     ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
409     info.prev_unicode = unicode;
410     info.id = id;
411
412     SERVER_START_REQ( start_hook_chain )
413     {
414         req->id = info.id;
415         req->event = EVENT_MIN;
416         wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
417         if (!wine_server_call( req ))
418         {
419             info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
420             info.handle       = reply->handle;
421             info.pid          = reply->pid;
422             info.tid          = reply->tid;
423             info.proc         = reply->proc;
424             info.next_unicode = reply->unicode;
425             thread_info->active_hooks = reply->active_hooks;
426         }
427     }
428     SERVER_END_REQ;
429
430     if (!info.tid && !info.proc) return 0;
431     ret = call_hook( &info, code, wparam, lparam );
432
433     SERVER_START_REQ( finish_hook_chain )
434     {
435         req->id = id;
436         wine_server_call( req );
437     }
438     SERVER_END_REQ;
439     return ret;
440 }
441
442
443 /***********************************************************************
444  *           HOOK_IsHooked
445  */
446 BOOL HOOK_IsHooked( INT id )
447 {
448     struct user_thread_info *thread_info = get_user_thread_info();
449
450     if (!thread_info->active_hooks) return TRUE;
451     return (thread_info->active_hooks & (1 << (id - WH_MINHOOK))) != 0;
452 }
453
454
455 /***********************************************************************
456  *              SetWindowsHookA (USER32.@)
457  */
458 HHOOK WINAPI SetWindowsHookA( INT id, HOOKPROC proc )
459 {
460     return SetWindowsHookExA( id, proc, 0, GetCurrentThreadId() );
461 }
462
463
464 /***********************************************************************
465  *              SetWindowsHookW (USER32.@)
466  */
467 HHOOK WINAPI SetWindowsHookW( INT id, HOOKPROC proc )
468 {
469     return SetWindowsHookExW( id, proc, 0, GetCurrentThreadId() );
470 }
471
472
473 /***********************************************************************
474  *              SetWindowsHookExA (USER32.@)
475  */
476 HHOOK WINAPI SetWindowsHookExA( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
477 {
478     return set_windows_hook( id, proc, inst, tid, FALSE );
479 }
480
481 /***********************************************************************
482  *              SetWindowsHookExW (USER32.@)
483  */
484 HHOOK WINAPI SetWindowsHookExW( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
485 {
486     return set_windows_hook( id, proc, inst, tid, TRUE );
487 }
488
489
490 /***********************************************************************
491  *              UnhookWindowsHook (USER32.@)
492  */
493 BOOL WINAPI UnhookWindowsHook( INT id, HOOKPROC proc )
494 {
495     BOOL ret;
496
497     TRACE( "%s %p\n", hook_names[id-WH_MINHOOK], proc );
498
499     SERVER_START_REQ( remove_hook )
500     {
501         req->handle = 0;
502         req->id   = id;
503         req->proc = proc;
504         ret = !wine_server_call_err( req );
505         if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
506     }
507     SERVER_END_REQ;
508     if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
509     return ret;
510 }
511
512
513
514 /***********************************************************************
515  *              UnhookWindowsHookEx (USER32.@)
516  */
517 BOOL WINAPI UnhookWindowsHookEx( HHOOK hhook )
518 {
519     BOOL ret;
520
521     TRACE( "%p\n", hhook );
522
523     SERVER_START_REQ( remove_hook )
524     {
525         req->handle = hhook;
526         req->id     = 0;
527         ret = !wine_server_call_err( req );
528         if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
529     }
530     SERVER_END_REQ;
531     if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
532     return ret;
533 }
534
535
536 /***********************************************************************
537  *              CallNextHookEx (USER32.@)
538  */
539 LRESULT WINAPI CallNextHookEx( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
540 {
541     struct user_thread_info *thread_info = get_user_thread_info();
542     struct hook_info info;
543
544     ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
545
546     SERVER_START_REQ( get_hook_info )
547     {
548         req->handle = thread_info->hook;
549         req->get_next = 1;
550         req->event = EVENT_MIN;
551         wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
552         if (!wine_server_call_err( req ))
553         {
554             info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
555             info.handle       = reply->handle;
556             info.id           = reply->id;
557             info.pid          = reply->pid;
558             info.tid          = reply->tid;
559             info.proc         = reply->proc;
560             info.next_unicode = reply->unicode;
561         }
562     }
563     SERVER_END_REQ;
564
565     info.prev_unicode = thread_info->hook_unicode;
566     return call_hook( &info, code, wparam, lparam );
567 }
568
569
570 LRESULT call_current_hook( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
571 {
572     struct hook_info info;
573
574     ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
575
576     SERVER_START_REQ( get_hook_info )
577     {
578         req->handle = hhook;
579         req->get_next = 0;
580         req->event = EVENT_MIN;
581         wine_server_set_reply( req, info.module, sizeof(info.module)-sizeof(WCHAR) );
582         if (!wine_server_call_err( req ))
583         {
584             info.module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
585             info.handle       = reply->handle;
586             info.id           = reply->id;
587             info.pid          = reply->pid;
588             info.tid          = reply->tid;
589             info.proc         = reply->proc;
590             info.next_unicode = reply->unicode;
591         }
592     }
593     SERVER_END_REQ;
594
595     info.prev_unicode = TRUE;  /* assume Unicode for this function */
596     return call_hook( &info, code, wparam, lparam );
597 }
598
599 /***********************************************************************
600  *              CallMsgFilterA (USER32.@)
601  */
602 BOOL WINAPI CallMsgFilterA( LPMSG msg, INT code )
603 {
604     if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, FALSE )) return TRUE;
605     return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, FALSE );
606 }
607
608
609 /***********************************************************************
610  *              CallMsgFilterW (USER32.@)
611  */
612 BOOL WINAPI CallMsgFilterW( LPMSG msg, INT code )
613 {
614     if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, TRUE )) return TRUE;
615     return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, TRUE );
616 }
617
618
619 /***********************************************************************
620  *           SetWinEventHook                            [USER32.@]
621  *
622  * Set up an event hook for a set of events.
623  *
624  * PARAMS
625  *  event_min [I] Lowest event handled by pfnProc
626  *  event_max [I] Highest event handled by pfnProc
627  *  inst      [I] DLL containing pfnProc
628  *  proc      [I] Callback event hook function
629  *  pid       [I] Process to get events from, or 0 for all processes
630  *  tid       [I] Thread to get events from, or 0 for all threads
631  *  flags     [I] Flags indicating the status of pfnProc
632  *
633  * RETURNS
634  *  Success: A handle representing the hook.
635  *  Failure: A NULL handle.
636  */
637 HWINEVENTHOOK WINAPI SetWinEventHook(DWORD event_min, DWORD event_max,
638                                      HMODULE inst, WINEVENTPROC proc,
639                                      DWORD pid, DWORD tid, DWORD flags)
640 {
641     HWINEVENTHOOK handle = 0;
642     WCHAR module[MAX_PATH];
643     DWORD len;
644
645     TRACE("%d,%d,%p,%p,%08x,%04x,%08x\n", event_min, event_max, inst,
646           proc, pid, tid, flags);
647
648     if (inst)
649     {
650         if (!(len = GetModuleFileNameW(inst, module, MAX_PATH)) || len >= MAX_PATH)
651             inst = 0;
652     }
653
654     if ((flags & WINEVENT_INCONTEXT) && !inst)
655     {
656         SetLastError(ERROR_HOOK_NEEDS_HMOD);
657         return 0;
658     }
659
660     if (event_min > event_max)
661     {
662         SetLastError(ERROR_INVALID_HOOK_FILTER);
663         return 0;
664     }
665
666     /* FIXME: what if the tid or pid belongs to another process? */
667     if (tid)  /* thread-local hook */
668         inst = 0;
669
670     SERVER_START_REQ( set_hook )
671     {
672         req->id        = WH_WINEVENT;
673         req->pid       = pid;
674         req->tid       = tid;
675         req->event_min = event_min;
676         req->event_max = event_max;
677         req->flags     = flags;
678         req->unicode   = 1;
679         if (inst) /* make proc relative to the module base */
680         {
681             req->proc = (void *)((char *)proc - (char *)inst);
682             wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
683         }
684         else req->proc = proc;
685
686         if (!wine_server_call_err( req ))
687         {
688             handle = reply->handle;
689             get_user_thread_info()->active_hooks = reply->active_hooks;
690         }
691     }
692     SERVER_END_REQ;
693
694     TRACE("-> %p\n", handle);
695     return handle;
696 }
697
698
699 /***********************************************************************
700  *           UnhookWinEvent                             [USER32.@]
701  *
702  * Remove an event hook for a set of events.
703  *
704  * PARAMS
705  *  hEventHook [I] Event hook to remove
706  *
707  * RETURNS
708  *  Success: TRUE. The event hook has been removed.
709  *  Failure: FALSE, if hEventHook is invalid.
710  */
711 BOOL WINAPI UnhookWinEvent(HWINEVENTHOOK hEventHook)
712 {
713     BOOL ret;
714
715     TRACE( "%p\n", hEventHook );
716
717     SERVER_START_REQ( remove_hook )
718     {
719         req->handle = hEventHook;
720         req->id     = WH_WINEVENT;
721         ret = !wine_server_call_err( req );
722         if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
723     }
724     SERVER_END_REQ;
725     return ret;
726 }
727
728 static inline BOOL find_first_hook(DWORD id, DWORD event, HWND hwnd, LONG object_id,
729                                    LONG child_id, struct hook_info *info)
730 {
731     struct user_thread_info *thread_info = get_user_thread_info();
732     BOOL ret;
733
734     if (!HOOK_IsHooked( id ))
735     {
736         TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
737         return FALSE;
738     }
739
740     SERVER_START_REQ( start_hook_chain )
741     {
742         req->id = id;
743         req->event = event;
744         req->window = hwnd;
745         req->object_id = object_id;
746         req->child_id = child_id;
747         wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
748         ret = !wine_server_call( req );
749         if (ret)
750         {
751             info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
752             info->handle    = reply->handle;
753             info->proc      = reply->proc;
754             info->tid       = reply->tid;
755             thread_info->active_hooks = reply->active_hooks;
756         }
757     }
758     SERVER_END_REQ;
759     return ret && (info->tid || info->proc);
760 }
761
762 static inline BOOL find_next_hook(DWORD event, HWND hwnd, LONG object_id,
763                                   LONG child_id, struct hook_info *info)
764 {
765     BOOL ret;
766
767     SERVER_START_REQ( get_hook_info )
768     {
769         req->handle = info->handle;
770         req->get_next = 1;
771         req->event = event;
772         req->window = hwnd;
773         req->object_id = object_id;
774         req->child_id = child_id;
775         wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
776         ret = !wine_server_call( req );
777         if (ret)
778         {
779             info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
780             info->handle    = reply->handle;
781             info->proc      = reply->proc;
782             info->tid       = reply->tid;
783         }
784     }
785     SERVER_END_REQ;
786     return ret;
787 }
788
789 static inline void find_hook_close(DWORD id)
790 {
791     SERVER_START_REQ( finish_hook_chain )
792     {
793         req->id = id;
794         wine_server_call( req );
795     }
796     SERVER_END_REQ;
797 }
798
799 /***********************************************************************
800  *           NotifyWinEvent                             [USER32.@]
801  *
802  * Inform the OS that an event has occurred.
803  *
804  * PARAMS
805  *  event     [I] Id of the event
806  *  hwnd      [I] Window holding the object that created the event
807  *  object_id [I] Type of object that created the event
808  *  child_id  [I] Child object of nId, or CHILDID_SELF.
809  *
810  * RETURNS
811  *  Nothing.
812  */
813 void WINAPI NotifyWinEvent(DWORD event, HWND hwnd, LONG object_id, LONG child_id)
814 {
815     struct hook_info info;
816
817     TRACE("%04x,%p,%d,%d\n", event, hwnd, object_id, child_id);
818
819     if (!hwnd)
820     {
821         SetLastError(ERROR_INVALID_WINDOW_HANDLE);
822         return;
823     }
824
825     USER_CheckNotLock();
826
827 #if 0
828     if (event & 0x80000000)
829     {
830         /* FIXME: on 64-bit platforms we need to invent some other way for
831          * passing parameters, nId and nChildId can't hold full [W|L]PARAM.
832          * struct call_hook *hook = (LRESULT *)hWnd;
833          * wparam = hook->wparam;
834          * lparam = hook->lparam;
835          */
836         LRESULT *ret = (LRESULT *)hwnd;
837         INT id, code, unicode;
838
839         id = (dwEvent & 0x7fff0000) >> 16;
840         code = event & 0x7fff;
841         unicode = event & 0x8000;
842         *ret = HOOK_CallHooks(id, code, object_id, child_id, unicode);
843         return;
844     }
845 #endif
846
847     if (!find_first_hook(WH_WINEVENT, event, hwnd, object_id, child_id, &info)) return;
848
849     do
850     {
851         if (info.proc)
852         {
853             TRACE( "calling WH_WINEVENT hook %p event %x hwnd %p %x %x module %s\n",
854                    info.proc, event, hwnd, object_id, child_id, debugstr_w(info.module) );
855
856             if (!info.module[0] || (info.proc = get_hook_proc( info.proc, info.module )) != NULL)
857             {
858                 if (TRACE_ON(relay))
859                     DPRINTF( "%04x:Call winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
860                              GetCurrentThreadId(), info.proc, info.handle, event, hwnd, object_id,
861                              child_id, GetCurrentThreadId(), GetCurrentTime());
862
863                 info.proc(info.handle, event, hwnd, object_id, child_id,
864                           GetCurrentThreadId(), GetCurrentTime());
865
866                 if (TRACE_ON(relay))
867                     DPRINTF( "%04x:Ret  winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
868                              GetCurrentThreadId(), info.proc, info.handle, event, hwnd, object_id,
869                              child_id, GetCurrentThreadId(), GetCurrentTime());
870             }
871         }
872         else
873             break;
874     }
875     while (find_next_hook(event, hwnd, object_id, child_id, &info));
876
877     find_hook_close(WH_WINEVENT);
878 }
879
880
881 /***********************************************************************
882  *           IsWinEventHookInstalled                       [USER32.@]
883  *
884  * Determine if an event hook is installed for an event.
885  *
886  * PARAMS
887  *  dwEvent  [I] Id of the event
888  *
889  * RETURNS
890  *  TRUE,  If there are any hooks installed for the event.
891  *  FALSE, Otherwise.
892  *
893  * BUGS
894  *  Not implemented.
895  */
896 BOOL WINAPI IsWinEventHookInstalled(DWORD dwEvent)
897 {
898     FIXME("(%d)-stub!\n", dwEvent);
899     return TRUE;
900 }