Work around to make system-global hooks at least run in the thread
[wine/multimedia.git] / server / hook.c
blobcf81fa429b84c34b110acb37e0c2c09dc8dd423f
1 /*
2 * Server-side window hooks support
4 * Copyright (C) 2002 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdio.h>
27 #include "winbase.h"
28 #include "winuser.h"
30 #include "object.h"
31 #include "request.h"
32 #include "user.h"
34 struct hook_table;
36 struct hook
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? */
46 #define NB_HOOKS (WH_MAXHOOK-WH_MINHOOK+1)
47 #define HOOK_ENTRY(p) LIST_ENTRY( (p), struct hook, chain )
49 struct hook_table
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 */
56 static void hook_table_dump( struct object *obj, int verbose );
57 static void hook_table_destroy( struct object *obj );
59 static const struct object_ops hook_table_ops =
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 */
77 /* create a new hook table */
78 static struct hook_table *alloc_hook_table(void)
80 struct hook_table *table;
81 int i;
83 if ((table = alloc_object( &hook_table_ops, -1 )))
85 for (i = 0; i < NB_HOOKS; i++)
87 list_init( &table->hooks[i] );
88 table->counts[i] = 0;
91 return table;
94 /* create a new hook and add it to the specified table */
95 static struct hook *add_hook( struct thread *thread, int index )
97 struct hook *hook;
98 struct hook_table *table = thread->hooks;
100 if (!table)
102 if (!(table = alloc_hook_table())) return NULL;
103 thread->hooks = table;
105 if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
107 if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
109 free( hook );
110 return NULL;
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;
118 /* free a hook, removing it from its chain */
119 static void free_hook( struct hook *hook )
121 free_user_handle( hook->handle );
122 if (hook->thread) release_object( hook->thread );
123 list_remove( &hook->chain );
124 free( hook );
127 /* find a hook from its index and proc */
128 static struct hook *find_hook( struct thread *thread, int index, void *proc )
130 struct list *p;
131 struct hook_table *table = thread->hooks;
133 if (table)
135 LIST_FOR_EACH( p, &table->hooks[index] )
137 struct hook *hook = HOOK_ENTRY( p );
138 if (hook->proc == proc) return hook;
141 return NULL;
144 /* get the hook table that a given hook belongs to */
145 inline static struct hook_table *get_table( struct hook *hook )
147 return hook->thread->hooks;
150 /* get the first hook in the chain */
151 inline static struct hook *get_first_hook( struct hook_table *table, int index )
153 struct list *elem = list_head( &table->hooks[index] );
154 return elem ? HOOK_ENTRY( elem ) : NULL;
157 /* find the next hook in the chain, skipping the deleted ones */
158 static struct hook *get_next_hook( struct hook *hook )
160 struct hook_table *table = get_table( hook );
161 struct hook *next;
163 while ((next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) )))
165 if (next->proc) break;
167 return next;
170 static void hook_table_dump( struct object *obj, int verbose )
172 /* struct hook_table *table = (struct hook_table *)obj; */
173 fprintf( stderr, "Hook table\n" );
176 static void hook_table_destroy( struct object *obj )
178 int i;
179 struct hook *hook;
180 struct hook_table *table = (struct hook_table *)obj;
182 for (i = 0; i < NB_HOOKS; i++)
184 while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
188 /* remove a hook, freeing it if the chain is not in use */
189 static void remove_hook( struct hook *hook )
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 );
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 )
201 if (!table->counts[index]) /* use count shouldn't already be 0 */
203 set_error( STATUS_INVALID_PARAMETER );
204 return;
206 if (!--table->counts[index])
208 struct hook *hook = get_first_hook( table, index );
209 while (hook)
211 struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
212 if (!hook->proc) free_hook( hook );
213 hook = next;
219 /* set a window hook */
220 DECL_HANDLER(set_hook)
222 struct thread *thread;
223 struct hook *hook;
225 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
227 set_error( STATUS_INVALID_PARAMETER );
228 return;
230 if (!req->tid) thread = (struct thread *)grab_object( current );
231 else if (!(thread = get_thread_from_id( req->tid ))) return;
233 if ((hook = add_hook( thread, req->id - WH_MINHOOK )))
235 hook->proc = req->proc;
236 hook->unicode = req->unicode;
237 reply->handle = hook->handle;
239 release_object( thread );
243 /* remove a window hook */
244 DECL_HANDLER(remove_hook)
246 struct hook *hook;
248 if (req->handle) hook = get_user_object( req->handle, USER_HOOK );
249 else
251 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
253 set_error( STATUS_INVALID_PARAMETER );
254 return;
256 if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
257 set_error( STATUS_INVALID_PARAMETER );
259 if (hook) remove_hook( hook );
263 /* start calling a hook chain */
264 DECL_HANDLER(start_hook_chain)
266 struct hook *hook;
267 struct hook_table *table = current->hooks;
269 if (req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
271 set_error( STATUS_INVALID_PARAMETER );
272 return;
274 if (!table) return; /* no hook set */
275 if (!(hook = get_first_hook( table, req->id - WH_MINHOOK ))) return; /* no hook set */
276 reply->handle = hook->handle;
277 reply->proc = hook->proc;
278 reply->unicode = hook->unicode;
279 table->counts[hook->index]++;
283 /* finished calling a hook chain */
284 DECL_HANDLER(finish_hook_chain)
286 struct hook_table *table = current->hooks;
287 int index = req->id - WH_MINHOOK;
289 if (req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
291 set_error( STATUS_INVALID_PARAMETER );
292 return;
294 if (table) release_hook_chain( table, index );
298 /* get the next hook to call */
299 DECL_HANDLER(get_next_hook)
301 struct hook *hook, *next;
303 if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
304 if (hook->thread != current)
306 set_error( STATUS_INVALID_HANDLE );
307 return;
309 if ((next = get_next_hook( hook )))
311 reply->next = next->handle;
312 reply->id = next->index + WH_MINHOOK;
313 reply->proc = next->proc;
314 reply->prev_unicode = hook->unicode;
315 reply->next_unicode = next->unicode;