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