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