blob: ab868659ea2264dc13776fc9b1ccbb1800f76152 [file] [log] [blame]
Alexandre Julliard02861352002-10-29 00:41:42 +00001/*
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>
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000025#include <stdarg.h>
Alexandre Julliard02861352002-10-29 00:41:42 +000026#include <stdio.h>
27
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000028#include "windef.h"
Alexandre Julliard02861352002-10-29 00:41:42 +000029#include "winbase.h"
30#include "winuser.h"
31
32#include "object.h"
Alexandre Julliardca3ac8f2003-07-11 21:55:58 +000033#include "process.h"
Alexandre Julliard02861352002-10-29 00:41:42 +000034#include "request.h"
35#include "user.h"
36
37struct hook_table;
38
39struct hook
40{
41 struct list chain; /* hook chain entry */
42 user_handle_t handle; /* user handle for this hook */
43 struct thread *thread; /* thread owning the hook */
44 int index; /* hook table index */
45 void *proc; /* hook function */
46 int unicode; /* is it a unicode hook? */
Alexandre Julliard14e68ba2002-11-20 19:54:32 +000047 WCHAR *module; /* module name for global hooks */
48 size_t module_size;
Alexandre Julliard02861352002-10-29 00:41:42 +000049};
50
51#define NB_HOOKS (WH_MAXHOOK-WH_MINHOOK+1)
52#define HOOK_ENTRY(p) LIST_ENTRY( (p), struct hook, chain )
53
54struct hook_table
55{
56 struct object obj; /* object header */
57 struct list hooks[NB_HOOKS]; /* array of hook chains */
58 int counts[NB_HOOKS]; /* use counts for each hook chain */
59};
60
61static void hook_table_dump( struct object *obj, int verbose );
62static void hook_table_destroy( struct object *obj );
63
64static const struct object_ops hook_table_ops =
65{
66 sizeof(struct hook_table), /* size */
67 hook_table_dump, /* dump */
68 no_add_queue, /* add_queue */
69 NULL, /* remove_queue */
70 NULL, /* signaled */
71 NULL, /* satisfied */
Alexandre Julliard02861352002-10-29 00:41:42 +000072 no_get_fd, /* get_fd */
Alexandre Julliard02861352002-10-29 00:41:42 +000073 hook_table_destroy /* destroy */
74};
75
76
Alexandre Julliard14e68ba2002-11-20 19:54:32 +000077static struct hook_table *global_hooks;
78
Alexandre Julliard02861352002-10-29 00:41:42 +000079/* create a new hook table */
80static struct hook_table *alloc_hook_table(void)
81{
82 struct hook_table *table;
83 int i;
84
Alexandre Julliarde66207e2003-02-19 00:33:32 +000085 if ((table = alloc_object( &hook_table_ops )))
Alexandre Julliard02861352002-10-29 00:41:42 +000086 {
87 for (i = 0; i < NB_HOOKS; i++)
88 {
89 list_init( &table->hooks[i] );
90 table->counts[i] = 0;
91 }
92 }
93 return table;
94}
95
96/* create a new hook and add it to the specified table */
Alexandre Julliardca3ac8f2003-07-11 21:55:58 +000097static struct hook *add_hook( struct thread *thread, int index, int global )
Alexandre Julliard02861352002-10-29 00:41:42 +000098{
99 struct hook *hook;
Alexandre Julliardca3ac8f2003-07-11 21:55:58 +0000100 struct hook_table *table = global ? global_hooks : get_queue_hooks(thread);
Alexandre Julliard02861352002-10-29 00:41:42 +0000101
102 if (!table)
103 {
104 if (!(table = alloc_hook_table())) return NULL;
Alexandre Julliardca3ac8f2003-07-11 21:55:58 +0000105 if (global) global_hooks = table;
106 else set_queue_hooks( thread, table );
Alexandre Julliard02861352002-10-29 00:41:42 +0000107 }
108 if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
109
110 if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
111 {
112 free( hook );
113 return NULL;
114 }
115 hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
116 hook->index = index;
117 list_add_head( &table->hooks[index], &hook->chain );
118 return hook;
119}
120
121/* free a hook, removing it from its chain */
122static void free_hook( struct hook *hook )
123{
124 free_user_handle( hook->handle );
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000125 if (hook->module) free( hook->module );
Alexandre Julliard02861352002-10-29 00:41:42 +0000126 if (hook->thread) release_object( hook->thread );
127 list_remove( &hook->chain );
128 free( hook );
129}
130
131/* find a hook from its index and proc */
132static struct hook *find_hook( struct thread *thread, int index, void *proc )
133{
134 struct list *p;
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000135 struct hook_table *table = get_queue_hooks( thread );
Alexandre Julliard02861352002-10-29 00:41:42 +0000136
137 if (table)
138 {
139 LIST_FOR_EACH( p, &table->hooks[index] )
140 {
141 struct hook *hook = HOOK_ENTRY( p );
142 if (hook->proc == proc) return hook;
143 }
144 }
145 return NULL;
146}
147
148/* get the hook table that a given hook belongs to */
149inline static struct hook_table *get_table( struct hook *hook )
150{
Alexandre Julliardca3ac8f2003-07-11 21:55:58 +0000151 if (!hook->thread) return global_hooks;
152 if (hook->index + WH_MINHOOK == WH_KEYBOARD_LL) return global_hooks;
153 if (hook->index + WH_MINHOOK == WH_MOUSE_LL) return global_hooks;
154 return get_queue_hooks(hook->thread);
Alexandre Julliard02861352002-10-29 00:41:42 +0000155}
156
157/* get the first hook in the chain */
158inline static struct hook *get_first_hook( struct hook_table *table, int index )
159{
160 struct list *elem = list_head( &table->hooks[index] );
161 return elem ? HOOK_ENTRY( elem ) : NULL;
162}
163
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000164/* find the first non-deleted hook in the chain */
165inline static struct hook *get_first_valid_hook( struct hook_table *table, int index )
166{
167 struct hook *hook = get_first_hook( table, index );
168 while (hook && !hook->proc)
169 hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
170 return hook;
171}
172
Alexandre Julliard02861352002-10-29 00:41:42 +0000173/* find the next hook in the chain, skipping the deleted ones */
174static struct hook *get_next_hook( struct hook *hook )
175{
176 struct hook_table *table = get_table( hook );
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000177 int index = hook->index;
Alexandre Julliard02861352002-10-29 00:41:42 +0000178
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000179 while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
Alexandre Julliard02861352002-10-29 00:41:42 +0000180 {
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000181 if (hook->proc) return hook;
Alexandre Julliard02861352002-10-29 00:41:42 +0000182 }
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000183 if (global_hooks && table != global_hooks) /* now search through the global table */
184 {
185 hook = get_first_valid_hook( global_hooks, index );
186 }
187 return hook;
Alexandre Julliard02861352002-10-29 00:41:42 +0000188}
189
190static void hook_table_dump( struct object *obj, int verbose )
191{
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000192 struct hook_table *table = (struct hook_table *)obj;
193 if (table == global_hooks) fprintf( stderr, "Global hook table\n" );
194 else fprintf( stderr, "Hook table\n" );
Alexandre Julliard02861352002-10-29 00:41:42 +0000195}
196
197static void hook_table_destroy( struct object *obj )
198{
199 int i;
200 struct hook *hook;
201 struct hook_table *table = (struct hook_table *)obj;
202
203 for (i = 0; i < NB_HOOKS; i++)
204 {
205 while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
206 }
207}
208
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000209/* free the global hooks table */
210void close_global_hooks(void)
211{
212 if (global_hooks) release_object( global_hooks );
213}
214
Alexandre Julliard02861352002-10-29 00:41:42 +0000215/* remove a hook, freeing it if the chain is not in use */
216static void remove_hook( struct hook *hook )
217{
218 struct hook_table *table = get_table( hook );
219 if (table->counts[hook->index])
220 hook->proc = NULL; /* chain is in use, just mark it and return */
221 else
222 free_hook( hook );
223}
224
225/* release a hook chain, removing deleted hooks if the use count drops to 0 */
226static void release_hook_chain( struct hook_table *table, int index )
227{
228 if (!table->counts[index]) /* use count shouldn't already be 0 */
229 {
230 set_error( STATUS_INVALID_PARAMETER );
231 return;
232 }
233 if (!--table->counts[index])
234 {
235 struct hook *hook = get_first_hook( table, index );
236 while (hook)
237 {
238 struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
239 if (!hook->proc) free_hook( hook );
240 hook = next;
241 }
242 }
243}
244
Alexandre Julliardca3ac8f2003-07-11 21:55:58 +0000245/* remove all global hooks owned by a given thread */
246void remove_thread_hooks( struct thread *thread )
247{
248 int index;
249
250 if (!global_hooks) return;
251
252 /* only low-level keyboard/mouse global hooks can be owned by a thread */
253 for (index = WH_KEYBOARD_LL - WH_MINHOOK; index <= WH_MOUSE_LL - WH_MINHOOK; index++)
254 {
255 struct hook *hook = get_first_hook( global_hooks, index );
256 while (hook)
257 {
258 struct hook *next = HOOK_ENTRY( list_next( &global_hooks->hooks[index], &hook->chain ) );
259 if (hook->thread == thread) remove_hook( hook );
260 hook = next;
261 }
262 }
263}
Alexandre Julliard02861352002-10-29 00:41:42 +0000264
265/* set a window hook */
266DECL_HANDLER(set_hook)
267{
268 struct thread *thread;
269 struct hook *hook;
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000270 WCHAR *module;
Alexandre Julliardca3ac8f2003-07-11 21:55:58 +0000271 int global;
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000272 size_t module_size = get_req_data_size();
Alexandre Julliard02861352002-10-29 00:41:42 +0000273
274 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
275 {
276 set_error( STATUS_INVALID_PARAMETER );
277 return;
278 }
Alexandre Julliardca3ac8f2003-07-11 21:55:58 +0000279 if (req->id == WH_KEYBOARD_LL || req->id == WH_MOUSE_LL)
280 {
281 /* low-level hardware hooks are special: always global, but without a module */
282 thread = (struct thread *)grab_object( current );
283 module = NULL;
284 global = 1;
285 }
286 else if (!req->tid)
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000287 {
288 if (!module_size)
289 {
290 set_error( STATUS_INVALID_PARAMETER );
291 return;
292 }
293 if (!(module = memdup( get_req_data(), module_size ))) return;
294 thread = NULL;
Alexandre Julliardca3ac8f2003-07-11 21:55:58 +0000295 global = 1;
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000296 }
297 else
298 {
299 module = NULL;
Alexandre Julliardca3ac8f2003-07-11 21:55:58 +0000300 global = 0;
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000301 if (!(thread = get_thread_from_id( req->tid ))) return;
302 }
Alexandre Julliard02861352002-10-29 00:41:42 +0000303
Alexandre Julliardca3ac8f2003-07-11 21:55:58 +0000304 if ((hook = add_hook( thread, req->id - WH_MINHOOK, global )))
Alexandre Julliard02861352002-10-29 00:41:42 +0000305 {
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000306 hook->proc = req->proc;
307 hook->unicode = req->unicode;
308 hook->module = module;
309 hook->module_size = module_size;
Alexandre Julliard02861352002-10-29 00:41:42 +0000310 reply->handle = hook->handle;
311 }
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000312 else if (module) free( module );
313
314 if (thread) release_object( thread );
Alexandre Julliard02861352002-10-29 00:41:42 +0000315}
316
317
318/* remove a window hook */
319DECL_HANDLER(remove_hook)
320{
321 struct hook *hook;
322
323 if (req->handle) hook = get_user_object( req->handle, USER_HOOK );
324 else
325 {
326 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
327 {
328 set_error( STATUS_INVALID_PARAMETER );
329 return;
330 }
331 if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
332 set_error( STATUS_INVALID_PARAMETER );
333 }
334 if (hook) remove_hook( hook );
335}
336
337
338/* start calling a hook chain */
339DECL_HANDLER(start_hook_chain)
340{
341 struct hook *hook;
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000342 struct hook_table *table = get_queue_hooks( current );
Alexandre Julliard02861352002-10-29 00:41:42 +0000343
344 if (req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
345 {
346 set_error( STATUS_INVALID_PARAMETER );
347 return;
348 }
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000349
350 if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK )))
351 {
352 /* try global table */
353 if (!(table = global_hooks) ||
354 !(hook = get_first_valid_hook( global_hooks, req->id - WH_MINHOOK )))
355 return; /* no hook set */
356 }
Alexandre Julliardca3ac8f2003-07-11 21:55:58 +0000357
358 if (hook->thread && hook->thread != current) /* must run in other thread */
359 {
360 reply->pid = get_process_id( hook->thread->process );
361 reply->tid = get_thread_id( hook->thread );
362 reply->proc = 0;
363 }
364 else
365 {
366 reply->pid = 0;
367 reply->tid = 0;
368 reply->proc = hook->proc;
369 }
Alexandre Julliard02861352002-10-29 00:41:42 +0000370 reply->handle = hook->handle;
Alexandre Julliard02861352002-10-29 00:41:42 +0000371 reply->unicode = hook->unicode;
372 table->counts[hook->index]++;
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000373 if (hook->module) set_reply_data( hook->module, hook->module_size );
Alexandre Julliard02861352002-10-29 00:41:42 +0000374}
375
376
377/* finished calling a hook chain */
378DECL_HANDLER(finish_hook_chain)
379{
Alexandre Julliardd55e7f12003-07-03 18:16:48 +0000380 struct hook_table *table = get_queue_hooks( current );
Alexandre Julliard02861352002-10-29 00:41:42 +0000381 int index = req->id - WH_MINHOOK;
382
383 if (req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
384 {
385 set_error( STATUS_INVALID_PARAMETER );
386 return;
387 }
388 if (table) release_hook_chain( table, index );
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000389 if (global_hooks) release_hook_chain( global_hooks, index );
Alexandre Julliard02861352002-10-29 00:41:42 +0000390}
391
392
393/* get the next hook to call */
394DECL_HANDLER(get_next_hook)
395{
396 struct hook *hook, *next;
397
398 if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
Alexandre Julliard14e68ba2002-11-20 19:54:32 +0000399 if (hook->thread && (hook->thread != current))
Alexandre Julliard02861352002-10-29 00:41:42 +0000400 {
401 set_error( STATUS_INVALID_HANDLE );
402 return;
403 }
404 if ((next = get_next_hook( hook )))
405 {
406 reply->next = next->handle;
407 reply->id = next->index + WH_MINHOOK;
Alexandre Julliard02861352002-10-29 00:41:42 +0000408 reply->prev_unicode = hook->unicode;
409 reply->next_unicode = next->unicode;
Alberto Massari66da6c82002-12-17 21:01:01 +0000410 if (next->module) set_reply_data( next->module, next->module_size );
Alexandre Julliardca3ac8f2003-07-11 21:55:58 +0000411 if (next->thread && next->thread != current)
412 {
413 reply->pid = get_process_id( next->thread->process );
414 reply->tid = get_thread_id( next->thread );
415 reply->proc = 0;
416 }
417 else
418 {
419 reply->pid = 0;
420 reply->tid = 0;
421 reply->proc = next->proc;
422 }
Alexandre Julliard02861352002-10-29 00:41:42 +0000423 }
424}