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