Started moving functions that deal with Unix file descriptors to a
[wine] / server / hook.c
1 /*
2  * Server-side window hooks support
3  *
4  * Copyright (C) 2002 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdio.h>
26
27 #include "winbase.h"
28 #include "winuser.h"
29
30 #include "object.h"
31 #include "request.h"
32 #include "user.h"
33
34 struct hook_table;
35
36 struct hook
37 {
38     struct list         chain;    /* hook chain entry */
39     user_handle_t       handle;   /* user handle for this hook */
40     struct thread      *thread;   /* thread owning the hook */
41     int                 index;    /* hook table index */
42     void               *proc;     /* hook function */
43     int                 unicode;  /* is it a unicode hook? */
44     WCHAR              *module;   /* module name for global hooks */
45     size_t              module_size;
46 };
47
48 #define NB_HOOKS (WH_MAXHOOK-WH_MINHOOK+1)
49 #define HOOK_ENTRY(p)  LIST_ENTRY( (p), struct hook, chain )
50
51 struct hook_table
52 {
53     struct object obj;              /* object header */
54     struct list   hooks[NB_HOOKS];  /* array of hook chains */
55     int           counts[NB_HOOKS]; /* use counts for each hook chain */
56 };
57
58 static void hook_table_dump( struct object *obj, int verbose );
59 static void hook_table_destroy( struct object *obj );
60
61 static const struct object_ops hook_table_ops =
62 {
63     sizeof(struct hook_table),    /* size */
64     hook_table_dump,              /* dump */
65     no_add_queue,                 /* add_queue */
66     NULL,                         /* remove_queue */
67     NULL,                         /* signaled */
68     NULL,                         /* satisfied */
69     no_get_fd,                    /* get_fd */
70     no_get_file_info,             /* get_file_info */
71     hook_table_destroy            /* destroy */
72 };
73
74
75 static struct hook_table *global_hooks;
76
77 /* create a new hook table */
78 static struct hook_table *alloc_hook_table(void)
79 {
80     struct hook_table *table;
81     int i;
82
83     if ((table = alloc_object( &hook_table_ops, -1 )))
84     {
85         for (i = 0; i < NB_HOOKS; i++)
86         {
87             list_init( &table->hooks[i] );
88             table->counts[i] = 0;
89         }
90     }
91     return table;
92 }
93
94 /* create a new hook and add it to the specified table */
95 static struct hook *add_hook( struct thread *thread, int index )
96 {
97     struct hook *hook;
98     struct hook_table *table = thread ? thread->hooks : global_hooks;
99
100     if (!table)
101     {
102         if (!(table = alloc_hook_table())) return NULL;
103         if (thread) thread->hooks = table;
104         else global_hooks = table;
105     }
106     if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
107
108     if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
109     {
110         free( hook );
111         return NULL;
112     }
113     hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
114     hook->index  = index;
115     list_add_head( &table->hooks[index], &hook->chain );
116     return hook;
117 }
118
119 /* free a hook, removing it from its chain */
120 static void free_hook( struct hook *hook )
121 {
122     free_user_handle( hook->handle );
123     if (hook->module) free( hook->module );
124     if (hook->thread) release_object( hook->thread );
125     list_remove( &hook->chain );
126     free( hook );
127 }
128
129 /* find a hook from its index and proc */
130 static struct hook *find_hook( struct thread *thread, int index, void *proc )
131 {
132     struct list *p;
133     struct hook_table *table = thread->hooks;
134
135     if (table)
136     {
137         LIST_FOR_EACH( p, &table->hooks[index] )
138         {
139             struct hook *hook = HOOK_ENTRY( p );
140             if (hook->proc == proc) return hook;
141         }
142     }
143     return NULL;
144 }
145
146 /* get the hook table that a given hook belongs to */
147 inline static struct hook_table *get_table( struct hook *hook )
148 {
149     return hook->thread ? hook->thread->hooks : global_hooks;
150 }
151
152 /* get the first hook in the chain */
153 inline static struct hook *get_first_hook( struct hook_table *table, int index )
154 {
155     struct list *elem = list_head( &table->hooks[index] );
156     return elem ? HOOK_ENTRY( elem ) : NULL;
157 }
158
159 /* find the first non-deleted hook in the chain */
160 inline static struct hook *get_first_valid_hook( struct hook_table *table, int index )
161 {
162     struct hook *hook = get_first_hook( table, index );
163     while (hook && !hook->proc)
164         hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
165     return hook;
166 }
167
168 /* find the next hook in the chain, skipping the deleted ones */
169 static struct hook *get_next_hook( struct hook *hook )
170 {
171     struct hook_table *table = get_table( hook );
172     int index = hook->index;
173
174     while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
175     {
176         if (hook->proc) return hook;
177     }
178     if (global_hooks && table != global_hooks)  /* now search through the global table */
179     {
180         hook = get_first_valid_hook( global_hooks, index );
181     }
182     return hook;
183 }
184
185 static void hook_table_dump( struct object *obj, int verbose )
186 {
187     struct hook_table *table = (struct hook_table *)obj;
188     if (table == global_hooks) fprintf( stderr, "Global hook table\n" );
189     else fprintf( stderr, "Hook table\n" );
190 }
191
192 static void hook_table_destroy( struct object *obj )
193 {
194     int i;
195     struct hook *hook;
196     struct hook_table *table = (struct hook_table *)obj;
197
198     for (i = 0; i < NB_HOOKS; i++)
199     {
200         while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
201     }
202 }
203
204 /* free the global hooks table */
205 void close_global_hooks(void)
206 {
207     if (global_hooks) release_object( global_hooks );
208 }
209
210 /* remove a hook, freeing it if the chain is not in use */
211 static void remove_hook( struct hook *hook )
212 {
213     struct hook_table *table = get_table( hook );
214     if (table->counts[hook->index])
215         hook->proc = NULL; /* chain is in use, just mark it and return */
216     else
217         free_hook( hook );
218 }
219
220 /* release a hook chain, removing deleted hooks if the use count drops to 0 */
221 static void release_hook_chain( struct hook_table *table, int index )
222 {
223     if (!table->counts[index])  /* use count shouldn't already be 0 */
224     {
225         set_error( STATUS_INVALID_PARAMETER );
226         return;
227     }
228     if (!--table->counts[index])
229     {
230         struct hook *hook = get_first_hook( table, index );
231         while (hook)
232         {
233             struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
234             if (!hook->proc) free_hook( hook );
235             hook = next;
236         }
237     }
238 }
239
240
241 /* set a window hook */
242 DECL_HANDLER(set_hook)
243 {
244     struct thread *thread;
245     struct hook *hook;
246     WCHAR *module;
247     size_t module_size = get_req_data_size();
248
249     if (!req->proc || req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
250     {
251         set_error( STATUS_INVALID_PARAMETER );
252         return;
253     }
254     if (!req->tid)
255     {
256         if (!module_size)
257         {
258             set_error( STATUS_INVALID_PARAMETER );
259             return;
260         }
261         if (!(module = memdup( get_req_data(), module_size ))) return;
262         thread = NULL;
263     }
264     else
265     {
266         module = NULL;
267         if (!(thread = get_thread_from_id( req->tid ))) return;
268     }
269
270     if ((hook = add_hook( thread, req->id - WH_MINHOOK )))
271     {
272         hook->proc        = req->proc;
273         hook->unicode     = req->unicode;
274         hook->module      = module;
275         hook->module_size = module_size;
276         reply->handle = hook->handle;
277     }
278     else if (module) free( module );
279
280     if (thread) release_object( thread );
281 }
282
283
284 /* remove a window hook */
285 DECL_HANDLER(remove_hook)
286 {
287     struct hook *hook;
288
289     if (req->handle) hook = get_user_object( req->handle, USER_HOOK );
290     else
291     {
292         if (!req->proc || req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
293         {
294             set_error( STATUS_INVALID_PARAMETER );
295             return;
296         }
297         if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
298             set_error( STATUS_INVALID_PARAMETER );
299     }
300     if (hook) remove_hook( hook );
301 }
302
303
304 /* start calling a hook chain */
305 DECL_HANDLER(start_hook_chain)
306 {
307     struct hook *hook;
308     struct hook_table *table = current->hooks;
309
310     if (req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
311     {
312         set_error( STATUS_INVALID_PARAMETER );
313         return;
314     }
315
316     if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK )))
317     {
318         /* try global table */
319         if (!(table = global_hooks) ||
320             !(hook = get_first_valid_hook( global_hooks, req->id - WH_MINHOOK )))
321             return;  /* no hook set */
322     }
323     reply->handle  = hook->handle;
324     reply->proc    = hook->proc;
325     reply->unicode = hook->unicode;
326     table->counts[hook->index]++;
327     if (hook->module) set_reply_data( hook->module, hook->module_size );
328 }
329
330
331 /* finished calling a hook chain */
332 DECL_HANDLER(finish_hook_chain)
333 {
334     struct hook_table *table = current->hooks;
335     int index = req->id - WH_MINHOOK;
336
337     if (req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
338     {
339         set_error( STATUS_INVALID_PARAMETER );
340         return;
341     }
342     if (table) release_hook_chain( table, index );
343     if (global_hooks) release_hook_chain( global_hooks, index );
344 }
345
346
347 /* get the next hook to call */
348 DECL_HANDLER(get_next_hook)
349 {
350     struct hook *hook, *next;
351
352     if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
353     if (hook->thread && (hook->thread != current))
354     {
355         set_error( STATUS_INVALID_HANDLE );
356         return;
357     }
358     if ((next = get_next_hook( hook )))
359     {
360         reply->next = next->handle;
361         reply->id   = next->index + WH_MINHOOK;
362         reply->proc = next->proc;
363         reply->prev_unicode = hook->unicode;
364         reply->next_unicode = next->unicode;
365         if (next->module) set_reply_data( next->module, next->module_size );
366     }
367 }