Rewrote hook support to store the hook chain in the server.
[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 };
45
46 #define NB_HOOKS (WH_MAXHOOK-WH_MINHOOK+1)
47 #define HOOK_ENTRY(p)  LIST_ENTRY( (p), struct hook, chain )
48
49 struct hook_table
50 {
51     struct object obj;              /* object header */
52     struct list   hooks[NB_HOOKS];  /* array of hook chains */
53     int           counts[NB_HOOKS]; /* use counts for each hook chain */
54 };
55
56 static void hook_table_dump( struct object *obj, int verbose );
57 static void hook_table_destroy( struct object *obj );
58
59 static const struct object_ops hook_table_ops =
60 {
61     sizeof(struct hook_table),    /* size */
62     hook_table_dump,              /* dump */
63     no_add_queue,                 /* add_queue */
64     NULL,                         /* remove_queue */
65     NULL,                         /* signaled */
66     NULL,                         /* satisfied */
67     NULL,                         /* get_poll_events */
68     NULL,                         /* poll_event */
69     no_get_fd,                    /* get_fd */
70     no_flush,                     /* flush */
71     no_get_file_info,             /* get_file_info */
72     NULL,                         /* queue_async */
73     hook_table_destroy            /* destroy */
74 };
75
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->hooks;
99
100     if (!table)
101     {
102         if (!(table = alloc_hook_table())) return NULL;
103         thread->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->thread) release_object( hook->thread );
123     list_remove( &hook->chain );
124     free( hook );
125 }
126
127 /* find a hook from its index and proc */
128 static struct hook *find_hook( struct thread *thread, int index, void *proc )
129 {
130     struct list *p;
131     struct hook_table *table = thread->hooks;
132
133     if (table)
134     {
135         LIST_FOR_EACH( p, &table->hooks[index] )
136         {
137             struct hook *hook = HOOK_ENTRY( p );
138             if (hook->proc == proc) return hook;
139         }
140     }
141     return NULL;
142 }
143
144 /* get the hook table that a given hook belongs to */
145 inline static struct hook_table *get_table( struct hook *hook )
146 {
147     return hook->thread->hooks;
148 }
149
150 /* get the first hook in the chain */
151 inline static struct hook *get_first_hook( struct hook_table *table, int index )
152 {
153     struct list *elem = list_head( &table->hooks[index] );
154     return elem ? HOOK_ENTRY( elem ) : NULL;
155 }
156
157 /* find the next hook in the chain, skipping the deleted ones */
158 static struct hook *get_next_hook( struct hook *hook )
159 {
160     struct hook_table *table = get_table( hook );
161     struct hook *next;
162
163     while ((next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) )))
164     {
165         if (next->proc) break;
166     }
167     return next;
168 }
169
170 static void hook_table_dump( struct object *obj, int verbose )
171 {
172 /*    struct hook_table *table = (struct hook_table *)obj; */
173     fprintf( stderr, "Hook table\n" );
174 }
175
176 static void hook_table_destroy( struct object *obj )
177 {
178     int i;
179     struct hook *hook;
180     struct hook_table *table = (struct hook_table *)obj;
181
182     for (i = 0; i < NB_HOOKS; i++)
183     {
184         while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
185     }
186 }
187
188 /* remove a hook, freeing it if the chain is not in use */
189 static void remove_hook( struct hook *hook )
190 {
191     struct hook_table *table = get_table( hook );
192     if (table->counts[hook->index])
193         hook->proc = NULL; /* chain is in use, just mark it and return */
194     else
195         free_hook( hook );
196 }
197
198 /* release a hook chain, removing deleted hooks if the use count drops to 0 */
199 static void release_hook_chain( struct hook_table *table, int index )
200 {
201     if (!table->counts[index])  /* use count shouldn't already be 0 */
202     {
203         set_error( STATUS_INVALID_PARAMETER );
204         return;
205     }
206     if (!--table->counts[index])
207     {
208         struct hook *hook = get_first_hook( table, index );
209         while (hook)
210         {
211             struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
212             if (!hook->proc) free_hook( hook );
213             hook = next;
214         }
215     }
216 }
217
218
219 /* set a window hook */
220 DECL_HANDLER(set_hook)
221 {
222     struct thread *thread;
223     struct hook *hook;
224
225     if (!req->proc || req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
226     {
227         set_error( STATUS_INVALID_PARAMETER );
228         return;
229     }
230     if (!(thread = get_thread_from_id( req->tid ))) return;
231
232     if ((hook = add_hook( thread, req->id - WH_MINHOOK )))
233     {
234         hook->proc    = req->proc;
235         hook->unicode = req->unicode;
236         reply->handle = hook->handle;
237     }
238     release_object( thread );
239 }
240
241
242 /* remove a window hook */
243 DECL_HANDLER(remove_hook)
244 {
245     struct hook *hook;
246
247     if (req->handle) hook = get_user_object( req->handle, USER_HOOK );
248     else
249     {
250         if (!req->proc || req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
251         {
252             set_error( STATUS_INVALID_PARAMETER );
253             return;
254         }
255         if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
256             set_error( STATUS_INVALID_PARAMETER );
257     }
258     if (hook) remove_hook( hook );
259 }
260
261
262 /* start calling a hook chain */
263 DECL_HANDLER(start_hook_chain)
264 {
265     struct hook *hook;
266     struct hook_table *table = current->hooks;
267
268     if (req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
269     {
270         set_error( STATUS_INVALID_PARAMETER );
271         return;
272     }
273     if (!table) return;  /* no hook set */
274     if (!(hook = get_first_hook( table, req->id - WH_MINHOOK ))) return;  /* no hook set */
275     reply->handle  = hook->handle;
276     reply->proc    = hook->proc;
277     reply->unicode = hook->unicode;
278     table->counts[hook->index]++;
279 }
280
281
282 /* finished calling a hook chain */
283 DECL_HANDLER(finish_hook_chain)
284 {
285     struct hook_table *table = current->hooks;
286     int index = req->id - WH_MINHOOK;
287
288     if (req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
289     {
290         set_error( STATUS_INVALID_PARAMETER );
291         return;
292     }
293     if (table) release_hook_chain( table, index );
294 }
295
296
297 /* get the next hook to call */
298 DECL_HANDLER(get_next_hook)
299 {
300     struct hook *hook, *next;
301
302     if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
303     if (hook->thread != current)
304     {
305         set_error( STATUS_INVALID_HANDLE );
306         return;
307     }
308     if ((next = get_next_hook( hook )))
309     {
310         reply->next = next->handle;
311         reply->id   = next->index + WH_MINHOOK;
312         reply->proc = next->proc;
313         reply->prev_unicode = hook->unicode;
314         reply->next_unicode = next->unicode;
315     }
316 }