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