Implemented NtSignalAndWaitForSingleObject.
[wine] / server / hook.c
1 /*
2  * Server-side window hooks support
3  *
4  * Copyright (C) 2002 Alexandre Julliard
5  * Copyright (C) 2005 Dmitry Timoshkov
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32
33 #include "object.h"
34 #include "process.h"
35 #include "request.h"
36 #include "user.h"
37
38 struct hook_table;
39
40 struct hook
41 {
42     struct list         chain;    /* hook chain entry */
43     user_handle_t       handle;   /* user handle for this hook */
44     struct process     *process;  /* process the hook is set to */
45     struct thread      *thread;   /* thread the hook is set to */
46     struct thread      *owner;    /* owner of the out of context hook */
47     int                 index;    /* hook table index */
48     int                 event_min;
49     int                 event_max;
50     int                 flags;
51     void               *proc;     /* hook function */
52     int                 unicode;  /* is it a unicode hook? */
53     WCHAR              *module;   /* module name for global hooks */
54     size_t              module_size;
55 };
56
57 #define WH_WINEVENT (WH_MAXHOOK+1)
58
59 #define NB_HOOKS (WH_WINEVENT-WH_MINHOOK+1)
60 #define HOOK_ENTRY(p)  LIST_ENTRY( (p), struct hook, chain )
61
62 struct hook_table
63 {
64     struct object obj;              /* object header */
65     struct list   hooks[NB_HOOKS];  /* array of hook chains */
66     int           counts[NB_HOOKS]; /* use counts for each hook chain */
67 };
68
69 static void hook_table_dump( struct object *obj, int verbose );
70 static void hook_table_destroy( struct object *obj );
71
72 static const struct object_ops hook_table_ops =
73 {
74     sizeof(struct hook_table),    /* size */
75     hook_table_dump,              /* dump */
76     no_add_queue,                 /* add_queue */
77     NULL,                         /* remove_queue */
78     NULL,                         /* signaled */
79     NULL,                         /* satisfied */
80     no_signal,                    /* signal */
81     no_get_fd,                    /* get_fd */
82     hook_table_destroy            /* destroy */
83 };
84
85
86 static struct hook_table *global_hooks;
87
88 /* create a new hook table */
89 static struct hook_table *alloc_hook_table(void)
90 {
91     struct hook_table *table;
92     int i;
93
94     if ((table = alloc_object( &hook_table_ops )))
95     {
96         for (i = 0; i < NB_HOOKS; i++)
97         {
98             list_init( &table->hooks[i] );
99             table->counts[i] = 0;
100         }
101     }
102     return table;
103 }
104
105 /* create a new hook and add it to the specified table */
106 static struct hook *add_hook( struct thread *thread, int index, int global )
107 {
108     struct hook *hook;
109     struct hook_table *table = global ? global_hooks : get_queue_hooks(thread);
110
111     if (!table)
112     {
113         if (!(table = alloc_hook_table())) return NULL;
114         if (global) global_hooks = table;
115         else set_queue_hooks( thread, table );
116     }
117     if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
118
119     if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
120     {
121         free( hook );
122         return NULL;
123     }
124     hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
125     hook->index  = index;
126     list_add_head( &table->hooks[index], &hook->chain );
127     return hook;
128 }
129
130 /* free a hook, removing it from its chain */
131 static void free_hook( struct hook *hook )
132 {
133     free_user_handle( hook->handle );
134     if (hook->module) free( hook->module );
135     if (hook->thread) release_object( hook->thread );
136     if (hook->process) release_object( hook->process );
137     release_object( hook->owner );
138     list_remove( &hook->chain );
139     free( hook );
140 }
141
142 /* find a hook from its index and proc */
143 static struct hook *find_hook( struct thread *thread, int index, void *proc )
144 {
145     struct list *p;
146     struct hook_table *table = get_queue_hooks( thread );
147
148     if (table)
149     {
150         LIST_FOR_EACH( p, &table->hooks[index] )
151         {
152             struct hook *hook = HOOK_ENTRY( p );
153             if (hook->proc == proc) return hook;
154         }
155     }
156     return NULL;
157 }
158
159 /* get the hook table that a given hook belongs to */
160 inline static struct hook_table *get_table( struct hook *hook )
161 {
162     if (!hook->thread) return global_hooks;
163     if (hook->index + WH_MINHOOK == WH_KEYBOARD_LL) return global_hooks;
164     if (hook->index + WH_MINHOOK == WH_MOUSE_LL) return global_hooks;
165     return get_queue_hooks(hook->thread);
166 }
167
168 /* get the first hook in the chain */
169 inline static struct hook *get_first_hook( struct hook_table *table, int index )
170 {
171     struct list *elem = list_head( &table->hooks[index] );
172     return elem ? HOOK_ENTRY( elem ) : NULL;
173 }
174
175 /* find the first non-deleted hook in the chain */
176 inline static struct hook *get_first_valid_hook( struct hook_table *table, int index,
177                                                  int event, user_handle_t win,
178                                                  int object_id, int child_id )
179 {
180     struct hook *hook = get_first_hook( table, index );
181
182     while (hook)
183     {
184         if ((!hook->process || hook->process == current->process) &&
185             (!(hook->flags & WINEVENT_SKIPOWNPROCESS) || hook->process != current->process))
186         {
187             if ((!hook->thread || hook->thread == current) &&
188                 (!(hook->flags & WINEVENT_SKIPOWNTHREAD) || hook->thread != current))
189             {
190                 if (hook->proc && event >= hook->event_min && event <= hook->event_max)
191                 {
192                     if (hook->flags & WINEVENT_INCONTEXT) return hook;
193
194                     /* only winevent hooks may be out of context */
195                     assert(hook->index + WH_MINHOOK == WH_WINEVENT);
196                     post_win_event( hook->owner, event, win, object_id, child_id,
197                                     hook->proc, hook->module, hook->module_size,
198                                     hook->handle );
199                 }
200             }
201         }
202         hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
203     }
204     return hook;
205 }
206
207 /* find the next hook in the chain, skipping the deleted ones */
208 static struct hook *get_next_hook( struct hook *hook, int event, user_handle_t win,
209                                    int object_id, int child_id )
210 {
211     struct hook_table *table = get_table( hook );
212     int index = hook->index;
213
214     while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
215     {
216         if ((!hook->process || hook->process == current->process) &&
217             (!(hook->flags & WINEVENT_SKIPOWNPROCESS) || hook->process != current->process))
218         {
219             if ((!hook->thread || hook->thread == current) &&
220                 (!(hook->flags & WINEVENT_SKIPOWNTHREAD) || hook->thread != current))
221             {
222                 if (hook->proc && event >= hook->event_min && event <= hook->event_max)
223                 {
224                     if (hook->flags & WINEVENT_INCONTEXT) return hook;
225
226                     /* only winevent hooks may be out of context */
227                     assert(hook->index + WH_MINHOOK == WH_WINEVENT);
228                     post_win_event( hook->owner, event, win, object_id, child_id,
229                                     hook->proc, hook->module, hook->module_size,
230                                     hook->handle );
231                 }
232             }
233         }
234     }
235     if (global_hooks && table != global_hooks)  /* now search through the global table */
236     {
237         hook = get_first_valid_hook( global_hooks, index, event, win, object_id, child_id );
238     }
239     return hook;
240 }
241
242 static void hook_table_dump( struct object *obj, int verbose )
243 {
244     struct hook_table *table = (struct hook_table *)obj;
245     if (table == global_hooks) fprintf( stderr, "Global hook table\n" );
246     else fprintf( stderr, "Hook table\n" );
247 }
248
249 static void hook_table_destroy( struct object *obj )
250 {
251     int i;
252     struct hook *hook;
253     struct hook_table *table = (struct hook_table *)obj;
254
255     for (i = 0; i < NB_HOOKS; i++)
256     {
257         while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
258     }
259 }
260
261 /* free the global hooks table */
262 void close_global_hooks(void)
263 {
264     if (global_hooks) release_object( global_hooks );
265 }
266
267 /* remove a hook, freeing it if the chain is not in use */
268 static void remove_hook( struct hook *hook )
269 {
270     struct hook_table *table = get_table( hook );
271     if (table->counts[hook->index])
272         hook->proc = NULL; /* chain is in use, just mark it and return */
273     else
274         free_hook( hook );
275 }
276
277 /* release a hook chain, removing deleted hooks if the use count drops to 0 */
278 static void release_hook_chain( struct hook_table *table, int index )
279 {
280     if (!table->counts[index])  /* use count shouldn't already be 0 */
281     {
282         set_error( STATUS_INVALID_PARAMETER );
283         return;
284     }
285     if (!--table->counts[index])
286     {
287         struct hook *hook = get_first_hook( table, index );
288         while (hook)
289         {
290             struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
291             if (!hook->proc) free_hook( hook );
292             hook = next;
293         }
294     }
295 }
296
297 /* remove all global hooks owned by a given thread */
298 void remove_thread_hooks( struct thread *thread )
299 {
300     int index;
301
302     if (!global_hooks) return;
303
304     /* only low-level keyboard/mouse global hooks can be owned by a thread */
305     for (index = WH_KEYBOARD_LL - WH_MINHOOK; index <= WH_MOUSE_LL - WH_MINHOOK; index++)
306     {
307         struct hook *hook = get_first_hook( global_hooks, index );
308         while (hook)
309         {
310             struct hook *next = HOOK_ENTRY( list_next( &global_hooks->hooks[index], &hook->chain ) );
311             if (hook->thread == thread) remove_hook( hook );
312             hook = next;
313         }
314     }
315 }
316
317 /* set a window hook */
318 DECL_HANDLER(set_hook)
319 {
320     struct process *process = NULL;
321     struct thread *thread = NULL;
322     struct hook *hook;
323     WCHAR *module;
324     int global;
325     size_t module_size = get_req_data_size();
326
327     if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
328     {
329         set_error( STATUS_INVALID_PARAMETER );
330         return;
331     }
332
333     if (req->pid && !(process = get_process_from_id( req->pid ))) return;
334
335     if (req->tid)
336     {
337         if (!(thread = get_thread_from_id( req->tid )))
338         {
339             if (process) release_object( process );
340             return;
341         }
342         if (process && process != thread->process)
343         {
344             release_object( process );
345             release_object( thread );
346             set_error( STATUS_INVALID_PARAMETER );
347             return;
348         }
349     }
350
351     if (req->id == WH_KEYBOARD_LL || req->id == WH_MOUSE_LL)
352     {
353         /* low-level hardware hooks are special: always global, but without a module */
354         thread = (struct thread *)grab_object( current );
355         module = NULL;
356         global = 1;
357     }
358     else if (!req->tid)
359     {
360         /* out of context hooks do not need a module handle */
361         if (!module_size && (req->flags & WINEVENT_INCONTEXT))
362         {
363             if (process) release_object( process );
364             if (thread) release_object( thread );
365             set_error( STATUS_INVALID_PARAMETER );
366             return;
367         }
368
369         if (!(module = memdup( get_req_data(), module_size ))) return;
370         thread = NULL;
371         global = 1;
372     }
373     else
374     {
375         module = NULL;
376         global = 0;
377     }
378
379     if ((hook = add_hook( thread, req->id - WH_MINHOOK, global )))
380     {
381         hook->owner = (struct thread *)grab_object( current );
382         hook->process = process ? (struct process *)grab_object( process ) : NULL;
383         hook->event_min   = req->event_min;
384         hook->event_max   = req->event_max;
385         hook->flags       = req->flags;
386         hook->proc        = req->proc;
387         hook->unicode     = req->unicode;
388         hook->module      = module;
389         hook->module_size = module_size;
390         reply->handle = hook->handle;
391     }
392     else if (module) free( module );
393
394     if (process) release_object( process );
395     if (thread) release_object( thread );
396 }
397
398
399 /* remove a window hook */
400 DECL_HANDLER(remove_hook)
401 {
402     struct hook *hook;
403
404     if (req->handle)
405     {
406         if (!(hook = get_user_object( req->handle, USER_HOOK )))
407         {
408             set_error( STATUS_INVALID_HANDLE );
409             return;
410         }
411     }
412     else
413     {
414         if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
415         {
416             set_error( STATUS_INVALID_PARAMETER );
417             return;
418         }
419         if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
420         {
421             set_error( STATUS_INVALID_PARAMETER );
422             return;
423         }
424     }
425     remove_hook( hook );
426 }
427
428
429 /* start calling a hook chain */
430 DECL_HANDLER(start_hook_chain)
431 {
432     struct hook *hook;
433     struct hook_table *table = get_queue_hooks( current );
434
435     if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
436     {
437         set_error( STATUS_INVALID_PARAMETER );
438         return;
439     }
440
441     if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK, req->event, req->window, req->object_id, req->child_id )))
442     {
443         /* try global table */
444         if (!(table = global_hooks) ||
445             !(hook = get_first_valid_hook( global_hooks, req->id - WH_MINHOOK, req->event, req->window, req->object_id, req->child_id )))
446             return;  /* no hook set */
447     }
448
449     if (hook->thread && hook->thread != current) /* must run in other thread */
450     {
451         reply->pid  = get_process_id( hook->thread->process );
452         reply->tid  = get_thread_id( hook->thread );
453     }
454     else
455     {
456         reply->pid  = 0;
457         reply->tid  = 0;
458     }
459     reply->proc    = hook->proc;
460     reply->handle  = hook->handle;
461     reply->unicode = hook->unicode;
462     table->counts[hook->index]++;
463     if (hook->module) set_reply_data( hook->module, hook->module_size );
464 }
465
466
467 /* finished calling a hook chain */
468 DECL_HANDLER(finish_hook_chain)
469 {
470     struct hook_table *table = get_queue_hooks( current );
471     int index = req->id - WH_MINHOOK;
472
473     if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
474     {
475         set_error( STATUS_INVALID_PARAMETER );
476         return;
477     }
478     if (table) release_hook_chain( table, index );
479     if (global_hooks) release_hook_chain( global_hooks, index );
480 }
481
482
483 /* get the next hook to call */
484 DECL_HANDLER(get_next_hook)
485 {
486     struct hook *hook, *next;
487
488     if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
489     if (hook->thread && (hook->thread != current))
490     {
491         set_error( STATUS_INVALID_HANDLE );
492         return;
493     }
494     if ((next = get_next_hook( hook, req->event, req->window, req->object_id, req->child_id )))
495     {
496         reply->next = next->handle;
497         reply->id   = next->index + WH_MINHOOK;
498         reply->prev_unicode = hook->unicode;
499         reply->next_unicode = next->unicode;
500         if (next->module) set_reply_data( next->module, next->module_size );
501         if (next->thread && next->thread != current)
502         {
503             reply->pid  = get_process_id( next->thread->process );
504             reply->tid  = get_thread_id( next->thread );
505         }
506         else
507         {
508             reply->pid  = 0;
509             reply->tid  = 0;
510         }
511         reply->proc = next->proc;
512     }
513 }