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