Keep track of the windows and hooks used by a thread to properly
[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     no_close_handle,              /* close_handle */
83     hook_table_destroy            /* destroy */
84 };
85
86
87 static struct hook_table *global_hooks;
88
89 /* create a new hook table */
90 static struct hook_table *alloc_hook_table(void)
91 {
92     struct hook_table *table;
93     int i;
94
95     if ((table = alloc_object( &hook_table_ops )))
96     {
97         for (i = 0; i < NB_HOOKS; i++)
98         {
99             list_init( &table->hooks[i] );
100             table->counts[i] = 0;
101         }
102     }
103     return table;
104 }
105
106 /* create a new hook and add it to the specified table */
107 static struct hook *add_hook( struct thread *thread, int index, int global )
108 {
109     struct hook *hook;
110     struct hook_table *table = global ? global_hooks : get_queue_hooks(thread);
111
112     if (!table)
113     {
114         if (!(table = alloc_hook_table())) return NULL;
115         if (global) global_hooks = table;
116         else set_queue_hooks( thread, table );
117     }
118     if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
119
120     if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
121     {
122         free( hook );
123         return NULL;
124     }
125     hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
126     hook->index  = index;
127     list_add_head( &table->hooks[index], &hook->chain );
128     if (thread) thread->desktop_users++;
129     return hook;
130 }
131
132 /* free a hook, removing it from its chain */
133 static void free_hook( struct hook *hook )
134 {
135     free_user_handle( hook->handle );
136     if (hook->module) free( hook->module );
137     if (hook->thread)
138     {
139         assert( hook->thread->desktop_users > 0 );
140         hook->thread->desktop_users--;
141         release_object( hook->thread );
142     }
143     if (hook->process) release_object( hook->process );
144     release_object( hook->owner );
145     list_remove( &hook->chain );
146     free( hook );
147 }
148
149 /* find a hook from its index and proc */
150 static struct hook *find_hook( struct thread *thread, int index, void *proc )
151 {
152     struct list *p;
153     struct hook_table *table = get_queue_hooks( thread );
154
155     if (table)
156     {
157         LIST_FOR_EACH( p, &table->hooks[index] )
158         {
159             struct hook *hook = HOOK_ENTRY( p );
160             if (hook->proc == proc) return hook;
161         }
162     }
163     return NULL;
164 }
165
166 /* get the hook table that a given hook belongs to */
167 inline static struct hook_table *get_table( struct hook *hook )
168 {
169     if (!hook->thread) return global_hooks;
170     if (hook->index + WH_MINHOOK == WH_KEYBOARD_LL) return global_hooks;
171     if (hook->index + WH_MINHOOK == WH_MOUSE_LL) return global_hooks;
172     return get_queue_hooks(hook->thread);
173 }
174
175 /* get the first hook in the chain */
176 inline static struct hook *get_first_hook( struct hook_table *table, int index )
177 {
178     struct list *elem = list_head( &table->hooks[index] );
179     return elem ? HOOK_ENTRY( elem ) : NULL;
180 }
181
182 /* check if a given hook should run in the current thread */
183 inline static int run_hook_in_current_thread( struct hook *hook )
184 {
185     if ((!hook->process || hook->process == current->process) &&
186         (!(hook->flags & WINEVENT_SKIPOWNPROCESS) || hook->process != current->process))
187     {
188         if ((!hook->thread || hook->thread == current) &&
189             (!(hook->flags & WINEVENT_SKIPOWNTHREAD) || hook->thread != current))
190             return 1;
191     }
192     return 0;
193 }
194
195 /* find the first non-deleted hook in the chain */
196 inline static struct hook *get_first_valid_hook( struct hook_table *table, int index,
197                                                  int event, user_handle_t win,
198                                                  int object_id, int child_id )
199 {
200     struct hook *hook = get_first_hook( table, index );
201
202     while (hook)
203     {
204         if (hook->proc && run_hook_in_current_thread( hook ))
205         {
206             if (event >= hook->event_min && event <= hook->event_max)
207             {
208                 if (hook->flags & WINEVENT_INCONTEXT) return hook;
209
210                 /* only winevent hooks may be out of context */
211                 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
212                 post_win_event( hook->owner, event, win, object_id, child_id,
213                                 hook->proc, hook->module, hook->module_size,
214                                 hook->handle );
215             }
216         }
217         hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
218     }
219     return hook;
220 }
221
222 /* find the next hook in the chain, skipping the deleted ones */
223 static struct hook *get_next_hook( struct hook *hook, int event, user_handle_t win,
224                                    int object_id, int child_id )
225 {
226     struct hook_table *table = get_table( hook );
227     int index = hook->index;
228
229     while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
230     {
231         if (hook->proc && run_hook_in_current_thread( hook ))
232         {
233             if (event >= hook->event_min && event <= hook->event_max)
234             {
235                 if (hook->flags & WINEVENT_INCONTEXT) return hook;
236
237                 /* only winevent hooks may be out of context */
238                 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
239                 post_win_event( hook->owner, event, win, object_id, child_id,
240                                 hook->proc, hook->module, hook->module_size,
241                                 hook->handle );
242             }
243         }
244     }
245     if (global_hooks && table != global_hooks)  /* now search through the global table */
246     {
247         hook = get_first_valid_hook( global_hooks, index, event, win, object_id, child_id );
248     }
249     return hook;
250 }
251
252 static void hook_table_dump( struct object *obj, int verbose )
253 {
254     struct hook_table *table = (struct hook_table *)obj;
255     if (table == global_hooks) fprintf( stderr, "Global hook table\n" );
256     else fprintf( stderr, "Hook table\n" );
257 }
258
259 static void hook_table_destroy( struct object *obj )
260 {
261     int i;
262     struct hook *hook;
263     struct hook_table *table = (struct hook_table *)obj;
264
265     for (i = 0; i < NB_HOOKS; i++)
266     {
267         while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
268     }
269 }
270
271 /* free the global hooks table */
272 void close_global_hooks(void)
273 {
274     if (global_hooks) release_object( global_hooks );
275 }
276
277 /* remove a hook, freeing it if the chain is not in use */
278 static void remove_hook( struct hook *hook )
279 {
280     struct hook_table *table = get_table( hook );
281     if (table->counts[hook->index])
282         hook->proc = NULL; /* chain is in use, just mark it and return */
283     else
284         free_hook( hook );
285 }
286
287 /* release a hook chain, removing deleted hooks if the use count drops to 0 */
288 static void release_hook_chain( struct hook_table *table, int index )
289 {
290     if (!table->counts[index])  /* use count shouldn't already be 0 */
291     {
292         set_error( STATUS_INVALID_PARAMETER );
293         return;
294     }
295     if (!--table->counts[index])
296     {
297         struct hook *hook = get_first_hook( table, index );
298         while (hook)
299         {
300             struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
301             if (!hook->proc) free_hook( hook );
302             hook = next;
303         }
304     }
305 }
306
307 /* remove all global hooks owned by a given thread */
308 void remove_thread_hooks( struct thread *thread )
309 {
310     int index;
311
312     if (!global_hooks) return;
313
314     /* only low-level keyboard/mouse global hooks can be owned by a thread */
315     for (index = WH_KEYBOARD_LL - WH_MINHOOK; index <= WH_MOUSE_LL - WH_MINHOOK; index++)
316     {
317         struct hook *hook = get_first_hook( global_hooks, index );
318         while (hook)
319         {
320             struct hook *next = HOOK_ENTRY( list_next( &global_hooks->hooks[index], &hook->chain ) );
321             if (hook->thread == thread) remove_hook( hook );
322             hook = next;
323         }
324     }
325 }
326
327 /* get a bitmap of active hooks in a hook table */
328 static int is_hook_active( struct hook_table *table, int index )
329 {
330     struct hook *hook = get_first_hook( table, index );
331
332     while (hook)
333     {
334         if (hook->proc && run_hook_in_current_thread( hook )) return 1;
335         hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
336     }
337     return 0;
338 }
339
340 /* get a bitmap of all active hooks for the current thread */
341 unsigned int get_active_hooks(void)
342 {
343     struct hook_table *table = get_queue_hooks( current );
344     unsigned int ret = 1 << 31;  /* set high bit to indicate that the bitmap is valid */
345     int id;
346
347     for (id = WH_MINHOOK; id <= WH_WINEVENT; id++)
348     {
349         if ((table && is_hook_active( table, id - WH_MINHOOK )) ||
350             (global_hooks && is_hook_active( global_hooks, id - WH_MINHOOK )))
351             ret |= 1 << (id - WH_MINHOOK);
352     }
353     return ret;
354 }
355
356 /* set a window hook */
357 DECL_HANDLER(set_hook)
358 {
359     struct process *process = NULL;
360     struct thread *thread = NULL;
361     struct hook *hook;
362     WCHAR *module;
363     int global;
364     size_t module_size = get_req_data_size();
365
366     if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
367     {
368         set_error( STATUS_INVALID_PARAMETER );
369         return;
370     }
371
372     if (req->pid && !(process = get_process_from_id( req->pid ))) return;
373
374     if (req->tid)
375     {
376         if (!(thread = get_thread_from_id( req->tid )))
377         {
378             if (process) release_object( process );
379             return;
380         }
381         if (process && process != thread->process)
382         {
383             release_object( process );
384             release_object( thread );
385             set_error( STATUS_INVALID_PARAMETER );
386             return;
387         }
388     }
389
390     if (req->id == WH_KEYBOARD_LL || req->id == WH_MOUSE_LL)
391     {
392         /* low-level hardware hooks are special: always global, but without a module */
393         thread = (struct thread *)grab_object( current );
394         module = NULL;
395         global = 1;
396     }
397     else if (!req->tid)
398     {
399         /* out of context hooks do not need a module handle */
400         if (!module_size && (req->flags & WINEVENT_INCONTEXT))
401         {
402             if (process) release_object( process );
403             if (thread) release_object( thread );
404             set_error( STATUS_INVALID_PARAMETER );
405             return;
406         }
407
408         if (!(module = memdup( get_req_data(), module_size ))) return;
409         thread = NULL;
410         global = 1;
411     }
412     else
413     {
414         module = NULL;
415         global = 0;
416     }
417
418     if ((hook = add_hook( thread, req->id - WH_MINHOOK, global )))
419     {
420         hook->owner = (struct thread *)grab_object( current );
421         hook->process = process ? (struct process *)grab_object( process ) : NULL;
422         hook->event_min   = req->event_min;
423         hook->event_max   = req->event_max;
424         hook->flags       = req->flags;
425         hook->proc        = req->proc;
426         hook->unicode     = req->unicode;
427         hook->module      = module;
428         hook->module_size = module_size;
429         reply->handle = hook->handle;
430         reply->active_hooks = get_active_hooks();
431     }
432     else if (module) free( module );
433
434     if (process) release_object( process );
435     if (thread) release_object( thread );
436 }
437
438
439 /* remove a window hook */
440 DECL_HANDLER(remove_hook)
441 {
442     struct hook *hook;
443
444     if (req->handle)
445     {
446         if (!(hook = get_user_object( req->handle, USER_HOOK )))
447         {
448             set_error( STATUS_INVALID_HANDLE );
449             return;
450         }
451     }
452     else
453     {
454         if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
455         {
456             set_error( STATUS_INVALID_PARAMETER );
457             return;
458         }
459         if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
460         {
461             set_error( STATUS_INVALID_PARAMETER );
462             return;
463         }
464     }
465     remove_hook( hook );
466     reply->active_hooks = get_active_hooks();
467 }
468
469
470 /* start calling a hook chain */
471 DECL_HANDLER(start_hook_chain)
472 {
473     struct hook *hook;
474     struct hook_table *table = get_queue_hooks( current );
475
476     if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
477     {
478         set_error( STATUS_INVALID_PARAMETER );
479         return;
480     }
481
482     reply->active_hooks = get_active_hooks();
483
484     if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK, req->event,
485                                                  req->window, req->object_id, req->child_id )))
486     {
487         /* try global table */
488         if (!(table = global_hooks) ||
489             !(hook = get_first_valid_hook( global_hooks, req->id - WH_MINHOOK, req->event,
490                                            req->window, req->object_id, req->child_id )))
491             return;  /* no hook set */
492     }
493
494     if (hook->thread && hook->thread != current) /* must run in other thread */
495     {
496         reply->pid  = get_process_id( hook->thread->process );
497         reply->tid  = get_thread_id( hook->thread );
498     }
499     else
500     {
501         reply->pid  = 0;
502         reply->tid  = 0;
503     }
504     reply->proc    = hook->proc;
505     reply->handle  = hook->handle;
506     reply->unicode = hook->unicode;
507     table->counts[hook->index]++;
508     if (hook->module) set_reply_data( hook->module, hook->module_size );
509 }
510
511
512 /* finished calling a hook chain */
513 DECL_HANDLER(finish_hook_chain)
514 {
515     struct hook_table *table = get_queue_hooks( current );
516     int index = req->id - WH_MINHOOK;
517
518     if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
519     {
520         set_error( STATUS_INVALID_PARAMETER );
521         return;
522     }
523     if (table) release_hook_chain( table, index );
524     if (global_hooks) release_hook_chain( global_hooks, index );
525 }
526
527
528 /* get the next hook to call */
529 DECL_HANDLER(get_next_hook)
530 {
531     struct hook *hook, *next;
532
533     if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
534     if (hook->thread && (hook->thread != current))
535     {
536         set_error( STATUS_INVALID_HANDLE );
537         return;
538     }
539     if ((next = get_next_hook( hook, req->event, req->window, req->object_id, req->child_id )))
540     {
541         reply->next = next->handle;
542         reply->id   = next->index + WH_MINHOOK;
543         reply->prev_unicode = hook->unicode;
544         reply->next_unicode = next->unicode;
545         if (next->module) set_reply_data( next->module, next->module_size );
546         if (next->thread && next->thread != current)
547         {
548             reply->pid  = get_process_id( next->thread->process );
549             reply->tid  = get_thread_id( next->thread );
550         }
551         else
552         {
553             reply->pid  = 0;
554             reply->tid  = 0;
555         }
556         reply->proc = next->proc;
557     }
558 }