Audit combo control code.
[wine] / dlls / user / hook.c
1 /*
2  * Windows hook functions
3  *
4  * Copyright 2002 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * NOTES:
21  *   Status of the various hooks:
22  *     WH_MSGFILTER                 OK
23  *     WH_JOURNALRECORD             Partially implemented
24  *     WH_JOURNALPLAYBACK           Partially implemented
25  *     WH_KEYBOARD                  OK
26  *     WH_GETMESSAGE                OK (FIXME: A/W mapping?)
27  *     WH_CALLWNDPROC               OK (FIXME: A/W mapping?)
28  *     WH_CBT
29  *       HCBT_MOVESIZE              OK
30  *       HCBT_MINMAX                OK
31  *       HCBT_QS                    OK
32  *       HCBT_CREATEWND             OK
33  *       HCBT_DESTROYWND            OK
34  *       HCBT_ACTIVATE              OK
35  *       HCBT_CLICKSKIPPED          OK
36  *       HCBT_KEYSKIPPED            OK
37  *       HCBT_SYSCOMMAND            Not implemented
38  *       HCBT_SETFOCUS              OK
39  *     WH_SYSMSGFILTER              OK
40  *     WH_MOUSE                     OK
41  *     WH_HARDWARE                  Not supported in Win32
42  *     WH_DEBUG                     Not implemented
43  *     WH_SHELL
44  *       HSHELL_WINDOWCREATED       OK
45  *       HSHELL_WINDOWDESTROYED     OK
46  *       HSHELL_ACTIVATESHELLWINDOW Not implemented
47  *       HSHELL_WINDOWACTIVATED     Not implemented
48  *       HSHELL_GETMINRECT          Not implemented
49  *       HSHELL_REDRAW              Not implemented
50  *       HSHELL_TASKMAN             Not implemented
51  *       HSHELL_LANGUAGE            Not implemented
52  *       HSHELL_SYSMENU             Not implemented
53  *       HSHELL_ENDTASK             Not implemented
54  *       HSHELL_ACCESSIBILITYSTATE  Not implemented
55  *       HSHELL_APPCOMMAND          Not implemented
56  *       HSHELL_WINDOWREPLACED      Not implemented
57  *       HSHELL_WINDOWREPLACING     Not implemented
58  *     WH_FOREGROUNDIDLE            Not implemented
59  *     WH_CALLWNDPROCRET            OK (FIXME: A/W mapping?)
60  *     WH_KEYBOARD_LL               Implemented but should use SendMessage instead
61  *     WH_MOUSE_LL                  Implemented but should use SendMessage instead
62  */
63
64 #include "config.h"
65 #include "wine/port.h"
66
67 #include <stdarg.h>
68
69 #include "windef.h"
70 #include "winbase.h"
71 #include "winuser.h"
72 #include "winerror.h"
73 #include "heap.h"
74 #include "message.h"
75 #include "win.h"
76 #include "wine/server.h"
77 #include "wine/unicode.h"
78 #include "wine/debug.h"
79 #include "winternl.h"
80
81 WINE_DEFAULT_DEBUG_CHANNEL(hook);
82 WINE_DECLARE_DEBUG_CHANNEL(relay);
83
84 static const char * const hook_names[WH_MAXHOOK - WH_MINHOOK + 1] =
85 {
86     "WH_MSGFILTER",
87     "WH_JOURNALRECORD",
88     "WH_JOURNALPLAYBACK",
89     "WH_KEYBOARD",
90     "WH_GETMESSAGE",
91     "WH_CALLWNDPROC",
92     "WH_CBT",
93     "WH_SYSMSGFILTER",
94     "WH_MOUSE",
95     "WH_HARDWARE",
96     "WH_DEBUG",
97     "WH_SHELL",
98     "WH_FOREGROUNDIDLE",
99     "WH_CALLWNDPROCRET",
100     "WH_KEYBOARD_LL",
101     "WH_MOUSE_LL"
102 };
103
104
105 /***********************************************************************
106  *              get_ll_hook_timeout
107  *
108  */
109 static UINT get_ll_hook_timeout(void)
110 {
111     /* FIXME: should retrieve LowLevelHooksTimeout in HKEY_CURRENT_USER\Control Panel\Desktop */
112     return 2000;
113 }
114
115
116 /***********************************************************************
117  *              set_windows_hook
118  *
119  * Implementation of SetWindowsHookExA and SetWindowsHookExW.
120  */
121 static HHOOK set_windows_hook( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid, BOOL unicode )
122 {
123     HHOOK handle = 0;
124     WCHAR module[MAX_PATH];
125     DWORD len;
126
127     if (tid)  /* thread-local hook */
128     {
129         if (id == WH_JOURNALRECORD ||
130             id == WH_JOURNALPLAYBACK ||
131             id == WH_KEYBOARD_LL ||
132             id == WH_MOUSE_LL ||
133             id == WH_SYSMSGFILTER)
134         {
135             /* these can only be global */
136             SetLastError( ERROR_INVALID_PARAMETER );
137             return 0;
138         }
139         inst = 0;
140     }
141     else  /* system-global hook */
142     {
143         if (id == WH_KEYBOARD_LL || id == WH_MOUSE_LL) inst = 0;
144         else if (!inst || !(len = GetModuleFileNameW( inst, module, MAX_PATH )) || len >= MAX_PATH)
145         {
146             SetLastError( ERROR_INVALID_PARAMETER );
147             return 0;
148         }
149     }
150
151     SERVER_START_REQ( set_hook )
152     {
153         req->id      = id;
154         req->tid     = tid;
155         req->unicode = unicode;
156         if (inst) /* make proc relative to the module base */
157         {
158             req->proc = (void *)((char *)proc - (char *)inst);
159             wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
160         }
161         else req->proc = proc;
162
163         if (!wine_server_call_err( req )) handle = reply->handle;
164     }
165     SERVER_END_REQ;
166
167     TRACE( "%s %p %lx -> %p\n", hook_names[id-WH_MINHOOK], proc, tid, handle );
168     return handle;
169 }
170
171
172 /***********************************************************************
173  *              call_hook_AtoW
174  */
175 static LRESULT call_hook_AtoW( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
176 {
177     LRESULT ret;
178     UNICODE_STRING usBuffer;
179     if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
180     else
181     {
182         CBT_CREATEWNDA *cbtcwA = (CBT_CREATEWNDA *)lparam;
183         CBT_CREATEWNDW cbtcwW;
184         CREATESTRUCTW csW;
185
186         cbtcwW.lpcs = &csW;
187         cbtcwW.hwndInsertAfter = cbtcwA->hwndInsertAfter;
188         csW = *(CREATESTRUCTW *)cbtcwA->lpcs;
189
190         if (HIWORD(cbtcwA->lpcs->lpszName))
191         {
192             RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszName);
193             csW.lpszName = usBuffer.Buffer;
194         }
195         if (HIWORD(cbtcwA->lpcs->lpszClass))
196         {
197             RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszClass);
198             csW.lpszClass = usBuffer.Buffer;
199         }
200         ret = proc( code, wparam, (LPARAM)&cbtcwW );
201         cbtcwA->hwndInsertAfter = cbtcwW.hwndInsertAfter;
202         if (HIWORD(csW.lpszName)) HeapFree( GetProcessHeap(), 0, (LPWSTR)csW.lpszName );
203         if (HIWORD(csW.lpszClass)) HeapFree( GetProcessHeap(), 0, (LPWSTR)csW.lpszClass );
204     }
205     return ret;
206 }
207
208
209 /***********************************************************************
210  *              call_hook_WtoA
211  */
212 static LRESULT call_hook_WtoA( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
213 {
214     LRESULT ret;
215
216     if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
217     else
218     {
219         CBT_CREATEWNDW *cbtcwW = (CBT_CREATEWNDW *)lparam;
220         CBT_CREATEWNDA cbtcwA;
221         CREATESTRUCTA csA;
222
223         cbtcwA.lpcs = &csA;
224         cbtcwA.hwndInsertAfter = cbtcwW->hwndInsertAfter;
225         csA = *(CREATESTRUCTA *)cbtcwW->lpcs;
226
227         if (HIWORD(cbtcwW->lpcs->lpszName))
228             csA.lpszName = HEAP_strdupWtoA( GetProcessHeap(), 0, cbtcwW->lpcs->lpszName );
229         if (HIWORD(cbtcwW->lpcs->lpszClass))
230             csA.lpszClass = HEAP_strdupWtoA( GetProcessHeap(), 0, cbtcwW->lpcs->lpszClass );
231         ret = proc( code, wparam, (LPARAM)&cbtcwA );
232         cbtcwW->hwndInsertAfter = cbtcwA.hwndInsertAfter;
233         if (HIWORD(csA.lpszName)) HeapFree( GetProcessHeap(), 0, (LPSTR)csA.lpszName );
234         if (HIWORD(csA.lpszClass)) HeapFree( GetProcessHeap(), 0, (LPSTR)csA.lpszClass );
235     }
236     return ret;
237 }
238
239
240 /***********************************************************************
241  *              call_hook
242  */
243 static LRESULT call_hook( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam,
244                           BOOL prev_unicode, BOOL next_unicode )
245 {
246     LRESULT ret;
247
248     if (TRACE_ON(relay))
249         DPRINTF( "%04lx:Call hook proc %p (id=%s,code=%x,wp=%08x,lp=%08lx)\n",
250                  GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam );
251
252     if (!prev_unicode == !next_unicode) ret = proc( code, wparam, lparam );
253     else if (prev_unicode) ret = call_hook_WtoA( proc, id, code, wparam, lparam );
254     else ret = call_hook_AtoW( proc, id, code, wparam, lparam );
255
256     if (TRACE_ON(relay))
257         DPRINTF( "%04lx:Ret  hook proc %p (id=%s,code=%x,wp=%08x,lp=%08lx) retval=%08lx\n",
258                  GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, ret );
259
260     return ret;
261 }
262
263
264 /***********************************************************************
265  *              get_hook_proc
266  *
267  * Retrieve the hook procedure real value for a module-relative proc
268  */
269 static HOOKPROC get_hook_proc( HOOKPROC proc, const WCHAR *module )
270 {
271     HMODULE mod;
272
273     if (!(mod = GetModuleHandleW(module)))
274     {
275         TRACE( "loading %s\n", debugstr_w(module) );
276         /* FIXME: the library will never be freed */
277         if (!(mod = LoadLibraryW(module))) return NULL;
278     }
279     return (HOOKPROC)((char *)mod + (ULONG_PTR)proc);
280 }
281
282
283 /***********************************************************************
284  *              HOOK_CallHooks
285  */
286 LRESULT HOOK_CallHooks( INT id, INT code, WPARAM wparam, LPARAM lparam, BOOL unicode )
287 {
288     MESSAGEQUEUE *queue = QUEUE_Current();
289     HOOKPROC proc = NULL;
290     HHOOK handle = 0;
291     DWORD pid = 0, tid = 0;
292     WCHAR module[MAX_PATH];
293     BOOL unicode_hook = FALSE;
294     LRESULT ret = 0;
295
296     if (!queue) return 0;
297     SERVER_START_REQ( start_hook_chain )
298     {
299         req->id = id;
300         wine_server_set_reply( req, module, sizeof(module)-sizeof(WCHAR) );
301         if (!wine_server_call( req ))
302         {
303             module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
304             handle       = reply->handle;
305             proc         = reply->proc;
306             pid          = reply->pid;
307             tid          = reply->tid;
308             unicode_hook = reply->unicode;
309         }
310     }
311     SERVER_END_REQ;
312
313     if (tid)
314     {
315         TRACE( "calling hook in thread %04lx %s code %x wp %x lp %lx\n",
316                tid, hook_names[id-WH_MINHOOK], code, wparam, lparam );
317
318         switch(id)
319         {
320         case WH_KEYBOARD_LL:
321             MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_KEYBOARD_LL_HOOK, wparam, lparam,
322                                             SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
323             break;
324         case WH_MOUSE_LL:
325             MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_MOUSE_LL_HOOK, wparam, lparam,
326                                             SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
327             break;
328         }
329     }
330     else if (proc)
331     {
332         TRACE( "calling hook %p %s code %x wp %x lp %lx module %s\n",
333                proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, debugstr_w(module) );
334
335         if (!module[0] || (proc = get_hook_proc( proc, module )) != NULL)
336         {
337             int locks = WIN_SuspendWndsLock();
338             HHOOK prev = queue->hook;
339             queue->hook = handle;
340             ret = call_hook( proc, id, code, wparam, lparam, unicode, unicode_hook );
341             queue->hook = prev;
342             WIN_RestoreWndsLock( locks );
343         }
344
345     }
346     else return 0;
347
348     SERVER_START_REQ( finish_hook_chain )
349     {
350         req->id = id;
351         wine_server_call( req );
352     }
353     SERVER_END_REQ;
354     return ret;
355 }
356
357
358 /***********************************************************************
359  *           HOOK_IsHooked
360  */
361 BOOL HOOK_IsHooked( INT id )
362 {
363     return TRUE;  /* FIXME */
364 }
365
366
367 /***********************************************************************
368  *              SetWindowsHookA (USER32.@)
369  */
370 HHOOK WINAPI SetWindowsHookA( INT id, HOOKPROC proc )
371 {
372     return SetWindowsHookExA( id, proc, 0, GetCurrentThreadId() );
373 }
374
375
376 /***********************************************************************
377  *              SetWindowsHookW (USER32.@)
378  */
379 HHOOK WINAPI SetWindowsHookW( INT id, HOOKPROC proc )
380 {
381     return SetWindowsHookExW( id, proc, 0, GetCurrentThreadId() );
382 }
383
384
385 /***********************************************************************
386  *              SetWindowsHookExA (USER32.@)
387  */
388 HHOOK WINAPI SetWindowsHookExA( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
389 {
390     return set_windows_hook( id, proc, inst, tid, FALSE );
391 }
392
393 /***********************************************************************
394  *              SetWindowsHookExW (USER32.@)
395  */
396 HHOOK WINAPI SetWindowsHookExW( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
397 {
398     return set_windows_hook( id, proc, inst, tid, TRUE );
399 }
400
401
402 /***********************************************************************
403  *              UnhookWindowsHook (USER32.@)
404  */
405 BOOL WINAPI UnhookWindowsHook( INT id, HOOKPROC proc )
406 {
407     BOOL ret;
408
409     TRACE( "%s %p\n", hook_names[id-WH_MINHOOK], proc );
410
411     SERVER_START_REQ( remove_hook )
412     {
413         req->handle = 0;
414         req->id   = id;
415         req->proc = proc;
416         ret = !wine_server_call_err( req );
417     }
418     SERVER_END_REQ;
419     return ret;
420 }
421
422
423
424 /***********************************************************************
425  *              UnhookWindowsHookEx (USER32.@)
426  */
427 BOOL WINAPI UnhookWindowsHookEx( HHOOK hhook )
428 {
429     BOOL ret;
430
431     TRACE( "%p\n", hhook );
432
433     SERVER_START_REQ( remove_hook )
434     {
435         req->handle = hhook;
436         ret = !wine_server_call_err( req );
437     }
438     SERVER_END_REQ;
439     return ret;
440 }
441
442
443 /***********************************************************************
444  *              CallNextHookEx (USER32.@)
445  */
446 LRESULT WINAPI CallNextHookEx( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
447 {
448     MESSAGEQUEUE *queue = QUEUE_Current();
449     HOOKPROC proc = NULL;
450     WCHAR module[MAX_PATH];
451     HHOOK handle = 0;
452     DWORD pid = 0, tid = 0;
453     INT id = 0;
454     BOOL prev_unicode = FALSE, next_unicode = FALSE;
455     LRESULT ret = 0;
456
457     if (!queue) return 0;
458
459     SERVER_START_REQ( get_next_hook )
460     {
461         req->handle = queue->hook;
462         wine_server_set_reply( req, module, sizeof(module)-sizeof(WCHAR) );
463         if (!wine_server_call_err( req ))
464         {
465             module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
466             handle       = reply->next;
467             id           = reply->id;
468             pid          = reply->pid;
469             tid          = reply->tid;
470             proc         = reply->proc;
471             prev_unicode = reply->prev_unicode;
472             next_unicode = reply->next_unicode;
473         }
474     }
475     SERVER_END_REQ;
476
477     if (tid)
478     {
479         TRACE( "calling hook in thread %04lx %s code %x wp %x lp %lx\n",
480                tid, hook_names[id-WH_MINHOOK], code, wparam, lparam );
481
482         switch(id)
483         {
484         case WH_KEYBOARD_LL:
485             MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_KEYBOARD_LL_HOOK, wparam, lparam,
486                                             SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
487             break;
488         case WH_MOUSE_LL:
489             MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_MOUSE_LL_HOOK, wparam, lparam,
490                                             SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
491             break;
492         }
493     }
494     else if (proc)
495     {
496         TRACE( "calling hook %p %s code %x wp %x lp %lx module %s\n",
497                proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, debugstr_w(module) );
498
499         if (!module[0] || (proc = get_hook_proc( proc, module )) != NULL)
500         {
501             HHOOK prev = queue->hook;
502             queue->hook = handle;
503             ret = call_hook( proc, id, code, wparam, lparam, prev_unicode, next_unicode );
504             queue->hook = prev;
505         }
506     }
507     return ret;
508 }
509
510
511 /***********************************************************************
512  *              CallMsgFilterA (USER32.@)
513  */
514 BOOL WINAPI CallMsgFilterA( LPMSG msg, INT code )
515 {
516     if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, FALSE )) return TRUE;
517     return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, FALSE );
518 }
519
520
521 /***********************************************************************
522  *              CallMsgFilterW (USER32.@)
523  */
524 BOOL WINAPI CallMsgFilterW( LPMSG msg, INT code )
525 {
526     if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, TRUE )) return TRUE;
527     return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, TRUE );
528 }
529
530
531 /***********************************************************************
532  *           SetWinEventHook                            [USER32.@]
533  *
534  * Set up an event hook for a set of events.
535  *
536  * PARAMS
537  *  dwMin     [I] Lowest event handled by pfnProc
538  *  dwMax     [I] Highest event handled by pfnProc
539  *  hModule   [I] DLL containing pfnProc
540  *  pfnProc   [I] Callback event hook function
541  *  dwProcess [I] Process to get events from, or 0 for all processes
542  *  dwThread  [I] Thread to get events from, or 0 for all threads
543  *  dwFlags   [I] Flags indicating the status of pfnProc
544  *
545  * RETURNS
546  *  Success: A handle representing the hook.
547  *  Failure: A NULL handle.
548  *
549  * BUGS
550  *  Not implemented.
551  */
552 HWINEVENTHOOK WINAPI SetWinEventHook(DWORD dwMin, DWORD dwMax, HMODULE hModule,
553                                      WINEVENTPROC pfnProc, DWORD dwProcess,
554                                      DWORD dwThread, DWORD dwFlags)
555 {
556     FIXME("(%ld,%ld,%p,%p,%ld,%ld,0x%08lx)-stub!\n", dwMin, dwMax, hModule,
557           pfnProc, dwProcess, dwThread, dwFlags);
558     return 0;
559 }
560
561
562 /***********************************************************************
563  *           UnhookWinEvent                             [USER32.@]
564  *
565  * Remove an event hook for a set of events.
566  *
567  * PARAMS
568  *  hEventHook [I] Event hook to remove
569  *
570  * RETURNS
571  *  Success: TRUE. The event hook has been removed.
572  *  Failure: FALSE, if hEventHook is invalid.
573  *
574  * BUGS
575  *  Not implemented.
576  */
577 BOOL WINAPI UnhookWinEvent(HWINEVENTHOOK hEventHook)
578 {
579     FIXME("(%p)-stub!\n", hEventHook);
580
581     return (hEventHook != 0);
582 }
583
584
585 /***********************************************************************
586  *           NotifyWinEvent                             [USER32.@]
587  *
588  * Inform the OS that an event has occurred.
589  *
590  * PARAMS
591  *  dwEvent  [I] Id of the event
592  *  hWnd     [I] Window holding the object that created the event
593  *  nId      [I] Type of object that created the event
594  *  nChildId [I] Child object of nId, or CHILDID_SELF.
595  *
596  * RETURNS
597  *  Nothing.
598  *
599  * BUGS
600  *  Not implemented.
601  */
602 void WINAPI NotifyWinEvent(DWORD dwEvent, HWND hWnd, LONG nId, LONG nChildId)
603 {
604     FIXME("(%ld,%p,%ld,%ld)-stub!\n", dwEvent, hWnd, nId, nChildId);
605 }
606
607
608 /***********************************************************************
609  *           IsWinEventHookInstalled                       [USER32.@]
610  *
611  * Determine if an event hook is installed for an event.
612  *
613  * PARAMS
614  *  dwEvent  [I] Id of the event
615  *
616  * RETURNS
617  *  TRUE,  If there are any hooks installed for the event.
618  *  FALSE, Otherwise.
619  *
620  * BUGS
621  *  Not implemented.
622  */
623 BOOL WINAPI IsWinEventHookInstalled(DWORD dwEvent)
624 {
625     FIXME("(%ld)-stub!\n", dwEvent);
626     return TRUE;
627 }