gdi: better implementation for GetCharABCWidthsFloat{A,W}.
[wine/multimedia.git] / server / hook.c
blobdd7b177ef6645b48f0532c20aa0f1f121239853a
1 /*
2 * Server-side window hooks support
4 * Copyright (C) 2002 Alexandre Julliard
5 * Copyright (C) 2005 Dmitry Timoshkov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdio.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "winternl.h"
36 #include "object.h"
37 #include "process.h"
38 #include "request.h"
39 #include "user.h"
41 struct hook_table;
43 struct hook
45 struct list chain; /* hook chain entry */
46 user_handle_t handle; /* user handle for this hook */
47 struct process *process; /* process the hook is set to */
48 struct thread *thread; /* thread the hook is set to */
49 struct thread *owner; /* owner of the out of context hook */
50 struct hook_table *table; /* hook table that contains this hook */
51 int index; /* hook table index */
52 int event_min;
53 int event_max;
54 int flags;
55 void *proc; /* hook function */
56 int unicode; /* is it a unicode hook? */
57 WCHAR *module; /* module name for global hooks */
58 size_t module_size;
61 #define WH_WINEVENT (WH_MAXHOOK+1)
63 #define NB_HOOKS (WH_WINEVENT-WH_MINHOOK+1)
64 #define HOOK_ENTRY(p) LIST_ENTRY( (p), struct hook, chain )
66 struct hook_table
68 struct object obj; /* object header */
69 struct list hooks[NB_HOOKS]; /* array of hook chains */
70 int counts[NB_HOOKS]; /* use counts for each hook chain */
73 static void hook_table_dump( struct object *obj, int verbose );
74 static void hook_table_destroy( struct object *obj );
76 static const struct object_ops hook_table_ops =
78 sizeof(struct hook_table), /* size */
79 hook_table_dump, /* dump */
80 no_add_queue, /* add_queue */
81 NULL, /* remove_queue */
82 NULL, /* signaled */
83 NULL, /* satisfied */
84 no_signal, /* signal */
85 no_get_fd, /* get_fd */
86 no_map_access, /* map_access */
87 no_lookup_name, /* lookup_name */
88 no_close_handle, /* close_handle */
89 hook_table_destroy /* destroy */
93 /* create a new hook table */
94 static struct hook_table *alloc_hook_table(void)
96 struct hook_table *table;
97 int i;
99 if ((table = alloc_object( &hook_table_ops )))
101 for (i = 0; i < NB_HOOKS; i++)
103 list_init( &table->hooks[i] );
104 table->counts[i] = 0;
107 return table;
110 static struct hook_table *get_global_hooks( struct thread *thread )
112 struct hook_table *table;
113 struct desktop *desktop = get_thread_desktop( thread, 0 );
115 if (!desktop) return NULL;
116 table = desktop->global_hooks;
117 release_object( desktop );
118 return table;
121 /* create a new hook and add it to the specified table */
122 static struct hook *add_hook( struct desktop *desktop, struct thread *thread, int index, int global )
124 struct hook *hook;
125 struct hook_table *table = global ? desktop->global_hooks : get_queue_hooks(thread);
127 if (!table)
129 if (!(table = alloc_hook_table())) return NULL;
130 if (global) desktop->global_hooks = table;
131 else set_queue_hooks( thread, table );
133 if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
135 if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
137 free( hook );
138 return NULL;
140 hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
141 hook->table = table;
142 hook->index = index;
143 list_add_head( &table->hooks[index], &hook->chain );
144 if (thread) thread->desktop_users++;
145 return hook;
148 /* free a hook, removing it from its chain */
149 static void free_hook( struct hook *hook )
151 free_user_handle( hook->handle );
152 if (hook->module) free( hook->module );
153 if (hook->thread)
155 assert( hook->thread->desktop_users > 0 );
156 hook->thread->desktop_users--;
157 release_object( hook->thread );
159 if (hook->process) release_object( hook->process );
160 release_object( hook->owner );
161 list_remove( &hook->chain );
162 free( hook );
165 /* find a hook from its index and proc */
166 static struct hook *find_hook( struct thread *thread, int index, void *proc )
168 struct list *p;
169 struct hook_table *table = get_queue_hooks( thread );
171 if (table)
173 LIST_FOR_EACH( p, &table->hooks[index] )
175 struct hook *hook = HOOK_ENTRY( p );
176 if (hook->proc == proc) return hook;
179 return NULL;
182 /* get the first hook in the chain */
183 inline static struct hook *get_first_hook( struct hook_table *table, int index )
185 struct list *elem = list_head( &table->hooks[index] );
186 return elem ? HOOK_ENTRY( elem ) : NULL;
189 /* check if a given hook should run in the current thread */
190 inline static int run_hook_in_current_thread( struct hook *hook )
192 if ((!hook->process || hook->process == current->process) &&
193 (!(hook->flags & WINEVENT_SKIPOWNPROCESS) || hook->process != current->process))
195 if ((!hook->thread || hook->thread == current) &&
196 (!(hook->flags & WINEVENT_SKIPOWNTHREAD) || hook->thread != current))
197 return 1;
199 return 0;
202 /* find the first non-deleted hook in the chain */
203 inline static struct hook *get_first_valid_hook( struct hook_table *table, int index,
204 int event, user_handle_t win,
205 int object_id, int child_id )
207 struct hook *hook = get_first_hook( table, index );
209 while (hook)
211 if (hook->proc && run_hook_in_current_thread( hook ))
213 if (event >= hook->event_min && event <= hook->event_max)
215 if (hook->flags & WINEVENT_INCONTEXT) return hook;
217 /* only winevent hooks may be out of context */
218 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
219 post_win_event( hook->owner, event, win, object_id, child_id,
220 hook->proc, hook->module, hook->module_size,
221 hook->handle );
224 hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
226 return hook;
229 /* find the next hook in the chain, skipping the deleted ones */
230 static struct hook *get_next_hook( struct thread *thread, struct hook *hook, int event,
231 user_handle_t win, int object_id, int child_id )
233 struct hook_table *global_hooks, *table = hook->table;
234 int index = hook->index;
236 while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
238 if (hook->proc && run_hook_in_current_thread( hook ))
240 if (event >= hook->event_min && event <= hook->event_max)
242 if (hook->flags & WINEVENT_INCONTEXT) return hook;
244 /* only winevent hooks may be out of context */
245 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
246 post_win_event( hook->owner, event, win, object_id, child_id,
247 hook->proc, hook->module, hook->module_size,
248 hook->handle );
252 global_hooks = get_global_hooks( thread );
253 if (global_hooks && table != global_hooks) /* now search through the global table */
255 hook = get_first_valid_hook( global_hooks, index, event, win, object_id, child_id );
257 return hook;
260 static void hook_table_dump( struct object *obj, int verbose )
262 /* struct hook_table *table = (struct hook_table *)obj; */
263 fprintf( stderr, "Hook table\n" );
266 static void hook_table_destroy( struct object *obj )
268 int i;
269 struct hook *hook;
270 struct hook_table *table = (struct hook_table *)obj;
272 for (i = 0; i < NB_HOOKS; i++)
274 while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
278 /* remove a hook, freeing it if the chain is not in use */
279 static void remove_hook( struct hook *hook )
281 if (hook->table->counts[hook->index])
282 hook->proc = NULL; /* chain is in use, just mark it and return */
283 else
284 free_hook( hook );
287 /* release a hook chain, removing deleted hooks if the use count drops to 0 */
288 static void release_hook_chain( struct hook_table *table, int index )
290 if (!table->counts[index]) /* use count shouldn't already be 0 */
292 set_error( STATUS_INVALID_PARAMETER );
293 return;
295 if (!--table->counts[index])
297 struct hook *hook = get_first_hook( table, index );
298 while (hook)
300 struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
301 if (!hook->proc) free_hook( hook );
302 hook = next;
307 /* remove all global hooks owned by a given thread */
308 void remove_thread_hooks( struct thread *thread )
310 struct hook_table *global_hooks = get_global_hooks( thread );
311 int index;
313 if (!global_hooks) return;
315 /* only low-level keyboard/mouse global hooks can be owned by a thread */
316 for (index = WH_KEYBOARD_LL - WH_MINHOOK; index <= WH_MOUSE_LL - WH_MINHOOK; index++)
318 struct hook *hook = get_first_hook( global_hooks, index );
319 while (hook)
321 struct hook *next = HOOK_ENTRY( list_next( &global_hooks->hooks[index], &hook->chain ) );
322 if (hook->thread == thread) remove_hook( hook );
323 hook = next;
328 /* get a bitmap of active hooks in a hook table */
329 static int is_hook_active( struct hook_table *table, int index )
331 struct hook *hook = get_first_hook( table, index );
333 while (hook)
335 if (hook->proc && run_hook_in_current_thread( hook )) return 1;
336 hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
338 return 0;
341 /* get a bitmap of all active hooks for the current thread */
342 unsigned int get_active_hooks(void)
344 struct hook_table *table = get_queue_hooks( current );
345 struct hook_table *global_hooks = get_global_hooks( current );
346 unsigned int ret = 1 << 31; /* set high bit to indicate that the bitmap is valid */
347 int id;
349 for (id = WH_MINHOOK; id <= WH_WINEVENT; id++)
351 if ((table && is_hook_active( table, id - WH_MINHOOK )) ||
352 (global_hooks && is_hook_active( global_hooks, id - WH_MINHOOK )))
353 ret |= 1 << (id - WH_MINHOOK);
355 return ret;
358 /* set a window hook */
359 DECL_HANDLER(set_hook)
361 struct process *process = NULL;
362 struct thread *thread = NULL;
363 struct desktop *desktop;
364 struct hook *hook;
365 WCHAR *module;
366 int global;
367 size_t module_size = get_req_data_size();
369 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
371 set_error( STATUS_INVALID_PARAMETER );
372 return;
375 if (!(desktop = get_thread_desktop( current, DESKTOP_HOOKCONTROL ))) return;
377 if (req->pid && !(process = get_process_from_id( req->pid ))) goto done;
379 if (req->tid)
381 if (!(thread = get_thread_from_id( req->tid ))) goto done;
382 if (process && process != thread->process)
384 set_error( STATUS_INVALID_PARAMETER );
385 goto done;
389 if (req->id == WH_KEYBOARD_LL || req->id == WH_MOUSE_LL)
391 /* low-level hardware hooks are special: always global, but without a module */
392 thread = (struct thread *)grab_object( current );
393 module = NULL;
394 global = 1;
396 else if (!req->tid)
398 /* out of context hooks do not need a module handle */
399 if (!module_size && (req->flags & WINEVENT_INCONTEXT))
401 set_error( STATUS_INVALID_PARAMETER );
402 goto done;
404 if (!(module = memdup( get_req_data(), module_size ))) goto done;
405 global = 1;
407 else
409 module = NULL;
410 global = 0;
413 if ((hook = add_hook( desktop, thread, req->id - WH_MINHOOK, global )))
415 hook->owner = (struct thread *)grab_object( current );
416 hook->process = process ? (struct process *)grab_object( process ) : NULL;
417 hook->event_min = req->event_min;
418 hook->event_max = req->event_max;
419 hook->flags = req->flags;
420 hook->proc = req->proc;
421 hook->unicode = req->unicode;
422 hook->module = module;
423 hook->module_size = module_size;
424 reply->handle = hook->handle;
425 reply->active_hooks = get_active_hooks();
427 else if (module) free( module );
429 done:
430 if (process) release_object( process );
431 if (thread) release_object( thread );
432 release_object( desktop );
436 /* remove a window hook */
437 DECL_HANDLER(remove_hook)
439 struct hook *hook;
441 if (req->handle)
443 if (!(hook = get_user_object( req->handle, USER_HOOK )))
445 set_error( STATUS_INVALID_HANDLE );
446 return;
449 else
451 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
453 set_error( STATUS_INVALID_PARAMETER );
454 return;
456 if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
458 set_error( STATUS_INVALID_PARAMETER );
459 return;
462 remove_hook( hook );
463 reply->active_hooks = get_active_hooks();
467 /* start calling a hook chain */
468 DECL_HANDLER(start_hook_chain)
470 struct hook *hook;
471 struct hook_table *table = get_queue_hooks( current );
473 if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
475 set_error( STATUS_INVALID_PARAMETER );
476 return;
479 reply->active_hooks = get_active_hooks();
481 if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK, req->event,
482 req->window, req->object_id, req->child_id )))
484 /* try global table */
485 if (!(table = get_global_hooks( current )) ||
486 !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK, req->event,
487 req->window, req->object_id, req->child_id )))
488 return; /* no hook set */
491 if (hook->thread && hook->thread != current) /* must run in other thread */
493 reply->pid = get_process_id( hook->thread->process );
494 reply->tid = get_thread_id( hook->thread );
496 else
498 reply->pid = 0;
499 reply->tid = 0;
501 reply->proc = hook->proc;
502 reply->handle = hook->handle;
503 reply->unicode = hook->unicode;
504 table->counts[hook->index]++;
505 if (hook->module) set_reply_data( hook->module, hook->module_size );
509 /* finished calling a hook chain */
510 DECL_HANDLER(finish_hook_chain)
512 struct hook_table *table = get_queue_hooks( current );
513 struct hook_table *global_hooks = get_global_hooks( current );
514 int index = req->id - WH_MINHOOK;
516 if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
518 set_error( STATUS_INVALID_PARAMETER );
519 return;
521 if (table) release_hook_chain( table, index );
522 if (global_hooks) release_hook_chain( global_hooks, index );
526 /* get the next hook to call */
527 DECL_HANDLER(get_next_hook)
529 struct hook *hook, *next;
531 if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
532 if (hook->thread && (hook->thread != current))
534 set_error( STATUS_INVALID_HANDLE );
535 return;
537 if ((next = get_next_hook( current, hook, req->event, req->window, req->object_id, req->child_id )))
539 reply->next = next->handle;
540 reply->id = next->index + WH_MINHOOK;
541 reply->prev_unicode = hook->unicode;
542 reply->next_unicode = next->unicode;
543 if (next->module) set_reply_data( next->module, next->module_size );
544 if (next->thread && next->thread != current)
546 reply->pid = get_process_id( next->thread->process );
547 reply->tid = get_thread_id( next->thread );
549 else
551 reply->pid = 0;
552 reply->tid = 0;
554 reply->proc = next->proc;