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