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