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