wineps.drv: Ignore requested resolutions not supported by device.
[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 "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"
82
83 WINE_DEFAULT_DEBUG_CHANNEL(hook);
84 WINE_DECLARE_DEBUG_CHANNEL(relay);
85
86 struct hook_info
87 {
88     INT id;
89     void *proc;
90     void *handle;
91     DWORD pid, tid;
92     BOOL prev_unicode, next_unicode;
93     WCHAR module[MAX_PATH];
94 };
95
96 #define WH_WINEVENT (WH_MAXHOOK+1)
97
98 static const char * const hook_names[WH_WINEVENT - WH_MINHOOK + 1] =
99 {
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"
117 };
118
119
120 /***********************************************************************
121  *              get_ll_hook_timeout
122  *
123  */
124 static UINT get_ll_hook_timeout(void)
125 {
126     /* FIXME: should retrieve LowLevelHooksTimeout in HKEY_CURRENT_USER\Control Panel\Desktop */
127     return 2000;
128 }
129
130
131 /***********************************************************************
132  *              set_windows_hook
133  *
134  * Implementation of SetWindowsHookExA and SetWindowsHookExW.
135  */
136 static HHOOK set_windows_hook( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid, BOOL unicode )
137 {
138     HHOOK handle = 0;
139     WCHAR module[MAX_PATH];
140     DWORD len;
141
142     if (!proc)
143     {
144         SetLastError( ERROR_INVALID_FILTER_PROC );
145         return 0;
146     }
147
148     if (tid)  /* thread-local hook */
149     {
150         if (id == WH_JOURNALRECORD ||
151             id == WH_JOURNALPLAYBACK ||
152             id == WH_KEYBOARD_LL ||
153             id == WH_MOUSE_LL ||
154             id == WH_SYSMSGFILTER)
155         {
156             /* these can only be global */
157             SetLastError( ERROR_INVALID_PARAMETER );
158             return 0;
159         }
160     }
161     else  /* system-global hook */
162     {
163         if (id == WH_KEYBOARD_LL || id == WH_MOUSE_LL) inst = 0;
164         else if (!inst)
165         {
166             SetLastError( ERROR_HOOK_NEEDS_HMOD );
167             return 0;
168         }
169     }
170
171     if (inst && (!(len = GetModuleFileNameW( inst, module, MAX_PATH )) || len >= MAX_PATH))
172     {
173         SetLastError( ERROR_INVALID_PARAMETER );
174         return 0;
175     }
176
177     SERVER_START_REQ( set_hook )
178     {
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 */
187         {
188             req->proc = wine_server_client_ptr( (void *)((char *)proc - (char *)inst) );
189             wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
190         }
191         else req->proc = wine_server_client_ptr( proc );
192
193         if (!wine_server_call_err( req ))
194         {
195             handle = wine_server_ptr_handle( reply->handle );
196             get_user_thread_info()->active_hooks = reply->active_hooks;
197         }
198     }
199     SERVER_END_REQ;
200
201     TRACE( "%s %p %x -> %p\n", hook_names[id-WH_MINHOOK], proc, tid, handle );
202     return handle;
203 }
204
205
206 /***********************************************************************
207  *              call_hook_AtoW
208  */
209 static LRESULT call_hook_AtoW( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
210 {
211     LRESULT ret;
212     UNICODE_STRING usBuffer;
213     if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
214     else
215     {
216         CBT_CREATEWNDA *cbtcwA = (CBT_CREATEWNDA *)lparam;
217         CBT_CREATEWNDW cbtcwW;
218         CREATESTRUCTW csW;
219         LPWSTR nameW = NULL;
220         LPWSTR classW = NULL;
221
222         cbtcwW.lpcs = &csW;
223         cbtcwW.hwndInsertAfter = cbtcwA->hwndInsertAfter;
224         csW = *(CREATESTRUCTW *)cbtcwA->lpcs;
225
226         if (!IS_INTRESOURCE(cbtcwA->lpcs->lpszName))
227         {
228             RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszName);
229             csW.lpszName = nameW = usBuffer.Buffer;
230         }
231         if (!IS_INTRESOURCE(cbtcwA->lpcs->lpszClass))
232         {
233             RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszClass);
234             csW.lpszClass = classW = usBuffer.Buffer;
235         }
236         ret = proc( code, wparam, (LPARAM)&cbtcwW );
237         cbtcwA->hwndInsertAfter = cbtcwW.hwndInsertAfter;
238         HeapFree( GetProcessHeap(), 0, nameW );
239         HeapFree( GetProcessHeap(), 0, classW );
240     }
241     return ret;
242 }
243
244
245 /***********************************************************************
246  *              call_hook_WtoA
247  */
248 static LRESULT call_hook_WtoA( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
249 {
250     LRESULT ret;
251
252     if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
253     else
254     {
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;
261
262         cbtcwA.lpcs = &csA;
263         cbtcwA.hwndInsertAfter = cbtcwW->hwndInsertAfter;
264         csA = *(CREATESTRUCTA *)cbtcwW->lpcs;
265
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;
271         }
272
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;
278         }
279
280         ret = proc( code, wparam, (LPARAM)&cbtcwA );
281         cbtcwW->hwndInsertAfter = cbtcwA.hwndInsertAfter;
282         HeapFree( GetProcessHeap(), 0, nameA );
283         HeapFree( GetProcessHeap(), 0, classA );
284     }
285     return ret;
286 }
287
288
289 /***********************************************************************
290  *              call_hook_proc
291  */
292 static LRESULT call_hook_proc( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam,
293                                BOOL prev_unicode, BOOL next_unicode )
294 {
295     LRESULT ret;
296
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 );
300
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 );
304
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 );
308
309     return ret;
310 }
311
312
313 /***********************************************************************
314  *              get_hook_proc
315  *
316  * Retrieve the hook procedure real value for a module-relative proc
317  */
318 void *get_hook_proc( void *proc, const WCHAR *module )
319 {
320     HMODULE mod;
321
322     if (!(mod = GetModuleHandleW(module)))
323     {
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;
327     }
328     return (char *)mod + (ULONG_PTR)proc;
329 }
330
331 /***********************************************************************
332  *              call_hook
333  *
334  * Call hook either in current thread or send message to the destination
335  * thread.
336  */
337 static LRESULT call_hook( struct hook_info *info, INT code, WPARAM wparam, LPARAM lparam )
338 {
339     DWORD_PTR ret = 0;
340
341     if (info->tid)
342     {
343         struct hook_extra_info h_extra;
344         h_extra.handle = info->handle;
345         h_extra.lparam = lparam;
346
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 );
349
350         switch(info->id)
351         {
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;
366         }
367     }
368     else if (info->proc)
369     {
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) );
373
374         if (!info->module[0] ||
375             (info->proc = get_hook_proc( info->proc, info->module )) != NULL)
376         {
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;
380
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;
387         }
388     }
389
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 */
392
393     return ret;
394 }
395
396
397 /***********************************************************************
398  *           HOOK_IsHooked
399  */
400 static BOOL HOOK_IsHooked( INT id )
401 {
402     struct user_thread_info *thread_info = get_user_thread_info();
403
404     if (!thread_info->active_hooks) return TRUE;
405     return (thread_info->active_hooks & (1 << (id - WH_MINHOOK))) != 0;
406 }
407
408
409 /***********************************************************************
410  *              HOOK_CallHooks
411  */
412 LRESULT HOOK_CallHooks( INT id, INT code, WPARAM wparam, LPARAM lparam, BOOL unicode )
413 {
414     struct user_thread_info *thread_info = get_user_thread_info();
415     struct hook_info info;
416     DWORD_PTR ret = 0;
417
418     USER_CheckNotLock();
419
420     if (!HOOK_IsHooked( id ))
421     {
422         TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
423         return 0;
424     }
425
426     ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
427     info.prev_unicode = unicode;
428     info.id = id;
429
430     SERVER_START_REQ( start_hook_chain )
431     {
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 ))
436         {
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;
444         }
445     }
446     SERVER_END_REQ;
447
448     if (!info.tid && !info.proc) return 0;
449     ret = call_hook( &info, code, wparam, lparam );
450
451     SERVER_START_REQ( finish_hook_chain )
452     {
453         req->id = id;
454         wine_server_call( req );
455     }
456     SERVER_END_REQ;
457     return ret;
458 }
459
460
461 /***********************************************************************
462  *              SetWindowsHookA (USER32.@)
463  */
464 HHOOK WINAPI SetWindowsHookA( INT id, HOOKPROC proc )
465 {
466     return SetWindowsHookExA( id, proc, 0, GetCurrentThreadId() );
467 }
468
469
470 /***********************************************************************
471  *              SetWindowsHookW (USER32.@)
472  */
473 HHOOK WINAPI SetWindowsHookW( INT id, HOOKPROC proc )
474 {
475     return SetWindowsHookExW( id, proc, 0, GetCurrentThreadId() );
476 }
477
478
479 /***********************************************************************
480  *              SetWindowsHookExA (USER32.@)
481  */
482 HHOOK WINAPI SetWindowsHookExA( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
483 {
484     return set_windows_hook( id, proc, inst, tid, FALSE );
485 }
486
487 /***********************************************************************
488  *              SetWindowsHookExW (USER32.@)
489  */
490 HHOOK WINAPI SetWindowsHookExW( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
491 {
492     return set_windows_hook( id, proc, inst, tid, TRUE );
493 }
494
495
496 /***********************************************************************
497  *              UnhookWindowsHook (USER32.@)
498  */
499 BOOL WINAPI UnhookWindowsHook( INT id, HOOKPROC proc )
500 {
501     BOOL ret;
502
503     TRACE( "%s %p\n", hook_names[id-WH_MINHOOK], proc );
504
505     SERVER_START_REQ( remove_hook )
506     {
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;
512     }
513     SERVER_END_REQ;
514     if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
515     return ret;
516 }
517
518
519
520 /***********************************************************************
521  *              UnhookWindowsHookEx (USER32.@)
522  */
523 BOOL WINAPI UnhookWindowsHookEx( HHOOK hhook )
524 {
525     BOOL ret;
526
527     SERVER_START_REQ( remove_hook )
528     {
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;
533     }
534     SERVER_END_REQ;
535     if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
536     return ret;
537 }
538
539
540 /***********************************************************************
541  *              CallNextHookEx (USER32.@)
542  */
543 LRESULT WINAPI CallNextHookEx( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
544 {
545     struct user_thread_info *thread_info = get_user_thread_info();
546     struct hook_info info;
547
548     ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
549
550     SERVER_START_REQ( get_hook_info )
551     {
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 ))
557         {
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;
565         }
566     }
567     SERVER_END_REQ;
568
569     info.prev_unicode = thread_info->hook_unicode;
570     return call_hook( &info, code, wparam, lparam );
571 }
572
573
574 LRESULT call_current_hook( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
575 {
576     struct hook_info info;
577
578     ZeroMemory( &info, sizeof(info) - sizeof(info.module) );
579
580     SERVER_START_REQ( get_hook_info )
581     {
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 ))
587         {
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;
595         }
596     }
597     SERVER_END_REQ;
598
599     info.prev_unicode = TRUE;  /* assume Unicode for this function */
600     return call_hook( &info, code, wparam, lparam );
601 }
602
603 /***********************************************************************
604  *              CallMsgFilterA (USER32.@)
605  */
606 BOOL WINAPI CallMsgFilterA( LPMSG msg, INT code )
607 {
608     if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, FALSE )) return TRUE;
609     return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, FALSE );
610 }
611
612
613 /***********************************************************************
614  *              CallMsgFilterW (USER32.@)
615  */
616 BOOL WINAPI CallMsgFilterW( LPMSG msg, INT code )
617 {
618     if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, TRUE )) return TRUE;
619     return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, TRUE );
620 }
621
622
623 /***********************************************************************
624  *           SetWinEventHook                            [USER32.@]
625  *
626  * Set up an event hook for a set of events.
627  *
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
636  *
637  * RETURNS
638  *  Success: A handle representing the hook.
639  *  Failure: A NULL handle.
640  */
641 HWINEVENTHOOK WINAPI SetWinEventHook(DWORD event_min, DWORD event_max,
642                                      HMODULE inst, WINEVENTPROC proc,
643                                      DWORD pid, DWORD tid, DWORD flags)
644 {
645     HWINEVENTHOOK handle = 0;
646     WCHAR module[MAX_PATH];
647     DWORD len;
648
649     TRACE("%d,%d,%p,%p,%08x,%04x,%08x\n", event_min, event_max, inst,
650           proc, pid, tid, flags);
651
652     if (inst)
653     {
654         if (!(len = GetModuleFileNameW(inst, module, MAX_PATH)) || len >= MAX_PATH)
655             inst = 0;
656     }
657
658     if ((flags & WINEVENT_INCONTEXT) && !inst)
659     {
660         SetLastError(ERROR_HOOK_NEEDS_HMOD);
661         return 0;
662     }
663
664     if (event_min > event_max)
665     {
666         SetLastError(ERROR_INVALID_HOOK_FILTER);
667         return 0;
668     }
669
670     /* FIXME: what if the tid or pid belongs to another process? */
671     if (tid)  /* thread-local hook */
672         inst = 0;
673
674     SERVER_START_REQ( set_hook )
675     {
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 */
684         {
685             req->proc = wine_server_client_ptr( (void *)((char *)proc - (char *)inst) );
686             wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
687         }
688         else req->proc = wine_server_client_ptr( proc );
689
690         if (!wine_server_call_err( req ))
691         {
692             handle = wine_server_ptr_handle( reply->handle );
693             get_user_thread_info()->active_hooks = reply->active_hooks;
694         }
695     }
696     SERVER_END_REQ;
697
698     TRACE("-> %p\n", handle);
699     return handle;
700 }
701
702
703 /***********************************************************************
704  *           UnhookWinEvent                             [USER32.@]
705  *
706  * Remove an event hook for a set of events.
707  *
708  * PARAMS
709  *  hEventHook [I] Event hook to remove
710  *
711  * RETURNS
712  *  Success: TRUE. The event hook has been removed.
713  *  Failure: FALSE, if hEventHook is invalid.
714  */
715 BOOL WINAPI UnhookWinEvent(HWINEVENTHOOK hEventHook)
716 {
717     BOOL ret;
718
719     SERVER_START_REQ( remove_hook )
720     {
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;
725     }
726     SERVER_END_REQ;
727     return ret;
728 }
729
730 static inline BOOL find_first_hook(DWORD id, DWORD event, HWND hwnd, LONG object_id,
731                                    LONG child_id, struct hook_info *info)
732 {
733     struct user_thread_info *thread_info = get_user_thread_info();
734     BOOL ret;
735
736     if (!HOOK_IsHooked( id ))
737     {
738         TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
739         return FALSE;
740     }
741
742     SERVER_START_REQ( start_hook_chain )
743     {
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)
752         {
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;
758         }
759     }
760     SERVER_END_REQ;
761     return ret && (info->tid || info->proc);
762 }
763
764 static inline BOOL find_next_hook(DWORD event, HWND hwnd, LONG object_id,
765                                   LONG child_id, struct hook_info *info)
766 {
767     BOOL ret;
768
769     SERVER_START_REQ( get_hook_info )
770     {
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)
780         {
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;
785         }
786     }
787     SERVER_END_REQ;
788     return ret;
789 }
790
791 static inline void find_hook_close(DWORD id)
792 {
793     SERVER_START_REQ( finish_hook_chain )
794     {
795         req->id = id;
796         wine_server_call( req );
797     }
798     SERVER_END_REQ;
799 }
800
801 /***********************************************************************
802  *           NotifyWinEvent                             [USER32.@]
803  *
804  * Inform the OS that an event has occurred.
805  *
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.
811  *
812  * RETURNS
813  *  Nothing.
814  */
815 void WINAPI NotifyWinEvent(DWORD event, HWND hwnd, LONG object_id, LONG child_id)
816 {
817     struct hook_info info;
818
819     TRACE("%04x,%p,%d,%d\n", event, hwnd, object_id, child_id);
820
821     if (!hwnd)
822     {
823         SetLastError(ERROR_INVALID_WINDOW_HANDLE);
824         return;
825     }
826
827     USER_CheckNotLock();
828
829 #if 0
830     if (event & 0x80000000)
831     {
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;
837          */
838         LRESULT *ret = (LRESULT *)hwnd;
839         INT id, code, unicode;
840
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;
846     }
847 #endif
848
849     if (!find_first_hook(WH_WINEVENT, event, hwnd, object_id, child_id, &info)) return;
850
851     do
852     {
853         WINEVENTPROC proc = info.proc;
854         if (proc)
855         {
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) );
858
859             if (!info.module[0] || (proc = get_hook_proc( proc, info.module )) != NULL)
860             {
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());
865
866                 proc( info.handle, event, hwnd, object_id, child_id,
867                       GetCurrentThreadId(), GetCurrentTime());
868
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());
873             }
874         }
875         else
876             break;
877     }
878     while (find_next_hook(event, hwnd, object_id, child_id, &info));
879
880     find_hook_close(WH_WINEVENT);
881 }
882
883
884 /***********************************************************************
885  *           IsWinEventHookInstalled                       [USER32.@]
886  *
887  * Determine if an event hook is installed for an event.
888  *
889  * PARAMS
890  *  dwEvent  [I] Id of the event
891  *
892  * RETURNS
893  *  TRUE,  If there are any hooks installed for the event.
894  *  FALSE, Otherwise.
895  *
896  * BUGS
897  *  Not implemented.
898  */
899 BOOL WINAPI IsWinEventHookInstalled(DWORD dwEvent)
900 {
901     /* FIXME: Needed by Office 2007 installer */
902     WARN("(%d)-stub!\n", dwEvent);
903     return TRUE;
904 }