Moved all references to file descriptors out of the generic object
[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     hook_table_destroy            /* destroy */
71 };
72
73
74 static struct hook_table *global_hooks;
75
76 /* create a new hook table */
77 static struct hook_table *alloc_hook_table(void)
78 {
79     struct hook_table *table;
80     int i;
81
82     if ((table = alloc_object( &hook_table_ops )))
83     {
84         for (i = 0; i < NB_HOOKS; i++)
85         {
86             list_init( &table->hooks[i] );
87             table->counts[i] = 0;
88         }
89     }
90     return table;
91 }
92
93 /* create a new hook and add it to the specified table */
94 static struct hook *add_hook( struct thread *thread, int index )
95 {
96     struct hook *hook;
97     struct hook_table *table = thread ? thread->hooks : global_hooks;
98
99     if (!table)
100     {
101         if (!(table = alloc_hook_table())) return NULL;
102         if (thread) thread->hooks = table;
103         else global_hooks = table;
104     }
105     if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
106
107     if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
108     {
109         free( hook );
110         return NULL;
111     }
112     hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
113     hook->index  = index;
114     list_add_head( &table->hooks[index], &hook->chain );
115     return hook;
116 }
117
118 /* free a hook, removing it from its chain */
119 static void free_hook( struct hook *hook )
120 {
121     free_user_handle( hook->handle );
122     if (hook->module) free( hook->module );
123     if (hook->thread) release_object( hook->thread );
124     list_remove( &hook->chain );
125     free( hook );
126 }
127
128 /* find a hook from its index and proc */
129 static struct hook *find_hook( struct thread *thread, int index, void *proc )
130 {
131     struct list *p;
132     struct hook_table *table = thread->hooks;
133
134     if (table)
135     {
136         LIST_FOR_EACH( p, &table->hooks[index] )
137         {
138             struct hook *hook = HOOK_ENTRY( p );
139             if (hook->proc == proc) return hook;
140         }
141     }
142     return NULL;
143 }
144
145 /* get the hook table that a given hook belongs to */
146 inline static struct hook_table *get_table( struct hook *hook )
147 {
148     return hook->thread ? hook->thread->hooks : global_hooks;
149 }
150
151 /* get the first hook in the chain */
152 inline static struct hook *get_first_hook( struct hook_table *table, int index )
153 {
154     struct list *elem = list_head( &table->hooks[index] );
155     return elem ? HOOK_ENTRY( elem ) : NULL;
156 }
157
158 /* find the first non-deleted hook in the chain */
159 inline static struct hook *get_first_valid_hook( struct hook_table *table, int index )
160 {
161     struct hook *hook = get_first_hook( table, index );
162     while (hook && !hook->proc)
163         hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
164     return hook;
165 }
166
167 /* find the next hook in the chain, skipping the deleted ones */
168 static struct hook *get_next_hook( struct hook *hook )
169 {
170     struct hook_table *table = get_table( hook );
171     int index = hook->index;
172
173     while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
174     {
175         if (hook->proc) return hook;
176     }
177     if (global_hooks && table != global_hooks)  /* now search through the global table */
178     {
179         hook = get_first_valid_hook( global_hooks, index );
180     }
181     return hook;
182 }
183
184 static void hook_table_dump( struct object *obj, int verbose )
185 {
186     struct hook_table *table = (struct hook_table *)obj;
187     if (table == global_hooks) fprintf( stderr, "Global hook table\n" );
188     else fprintf( stderr, "Hook table\n" );
189 }
190
191 static void hook_table_destroy( struct object *obj )
192 {
193     int i;
194     struct hook *hook;
195     struct hook_table *table = (struct hook_table *)obj;
196
197     for (i = 0; i < NB_HOOKS; i++)
198     {
199         while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
200     }
201 }
202
203 /* free the global hooks table */
204 void close_global_hooks(void)
205 {
206     if (global_hooks) release_object( global_hooks );
207 }
208
209 /* remove a hook, freeing it if the chain is not in use */
210 static void remove_hook( struct hook *hook )
211 {
212     struct hook_table *table = get_table( hook );
213     if (table->counts[hook->index])
214         hook->proc = NULL; /* chain is in use, just mark it and return */
215     else
216         free_hook( hook );
217 }
218
219 /* release a hook chain, removing deleted hooks if the use count drops to 0 */
220 static void release_hook_chain( struct hook_table *table, int index )
221 {
222     if (!table->counts[index])  /* use count shouldn't already be 0 */
223     {
224         set_error( STATUS_INVALID_PARAMETER );
225         return;
226     }
227     if (!--table->counts[index])
228     {
229         struct hook *hook = get_first_hook( table, index );
230         while (hook)
231         {
232             struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
233             if (!hook->proc) free_hook( hook );
234             hook = next;
235         }
236     }
237 }
238
239
240 /* set a window hook */
241 DECL_HANDLER(set_hook)
242 {
243     struct thread *thread;
244     struct hook *hook;
245     WCHAR *module;
246     size_t module_size = get_req_data_size();
247
248     if (!req->proc || req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
249     {
250         set_error( STATUS_INVALID_PARAMETER );
251         return;
252     }
253     if (!req->tid)
254     {
255         if (!module_size)
256         {
257             set_error( STATUS_INVALID_PARAMETER );
258             return;
259         }
260         if (!(module = memdup( get_req_data(), module_size ))) return;
261         thread = NULL;
262     }
263     else
264     {
265         module = NULL;
266         if (!(thread = get_thread_from_id( req->tid ))) return;
267     }
268
269     if ((hook = add_hook( thread, req->id - WH_MINHOOK )))
270     {
271         hook->proc        = req->proc;
272         hook->unicode     = req->unicode;
273         hook->module      = module;
274         hook->module_size = module_size;
275         reply->handle = hook->handle;
276     }
277     else if (module) free( module );
278
279     if (thread) release_object( thread );
280 }
281
282
283 /* remove a window hook */
284 DECL_HANDLER(remove_hook)
285 {
286     struct hook *hook;
287
288     if (req->handle) hook = get_user_object( req->handle, USER_HOOK );
289     else
290     {
291         if (!req->proc || req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
292         {
293             set_error( STATUS_INVALID_PARAMETER );
294             return;
295         }
296         if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
297             set_error( STATUS_INVALID_PARAMETER );
298     }
299     if (hook) remove_hook( hook );
300 }
301
302
303 /* start calling a hook chain */
304 DECL_HANDLER(start_hook_chain)
305 {
306     struct hook *hook;
307     struct hook_table *table = current->hooks;
308
309     if (req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
310     {
311         set_error( STATUS_INVALID_PARAMETER );
312         return;
313     }
314
315     if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK )))
316     {
317         /* try global table */
318         if (!(table = global_hooks) ||
319             !(hook = get_first_valid_hook( global_hooks, req->id - WH_MINHOOK )))
320             return;  /* no hook set */
321     }
322     reply->handle  = hook->handle;
323     reply->proc    = hook->proc;
324     reply->unicode = hook->unicode;
325     table->counts[hook->index]++;
326     if (hook->module) set_reply_data( hook->module, hook->module_size );
327 }
328
329
330 /* finished calling a hook chain */
331 DECL_HANDLER(finish_hook_chain)
332 {
333     struct hook_table *table = current->hooks;
334     int index = req->id - WH_MINHOOK;
335
336     if (req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
337     {
338         set_error( STATUS_INVALID_PARAMETER );
339         return;
340     }
341     if (table) release_hook_chain( table, index );
342     if (global_hooks) release_hook_chain( global_hooks, index );
343 }
344
345
346 /* get the next hook to call */
347 DECL_HANDLER(get_next_hook)
348 {
349     struct hook *hook, *next;
350
351     if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
352     if (hook->thread && (hook->thread != current))
353     {
354         set_error( STATUS_INVALID_HANDLE );
355         return;
356     }
357     if ((next = get_next_hook( hook )))
358     {
359         reply->next = next->handle;
360         reply->id   = next->index + WH_MINHOOK;
361         reply->proc = next->proc;
362         reply->prev_unicode = hook->unicode;
363         reply->next_unicode = next->unicode;
364         if (next->module) set_reply_data( next->module, next->module_size );
365     }
366 }