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