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