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