Correct a problem with parse_data if binary data was not in full bytes.
[wine.git] / server / hook.c
blob125f3467ed520ba703b3f3a28650868c8acd4d3c
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 "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
33 #include "object.h"
34 #include "process.h"
35 #include "request.h"
36 #include "user.h"
38 struct hook_table;
40 struct hook
42 struct list chain; /* hook chain entry */
43 user_handle_t handle; /* user handle for this hook */
44 struct process *process; /* process the hook is set to */
45 struct thread *thread; /* thread the hook is set to */
46 struct thread *owner; /* owner of the out of context hook */
47 int index; /* hook table index */
48 int event_min;
49 int event_max;
50 int flags;
51 void *proc; /* hook function */
52 int unicode; /* is it a unicode hook? */
53 WCHAR *module; /* module name for global hooks */
54 size_t module_size;
57 #define WH_WINEVENT (WH_MAXHOOK+1)
59 #define NB_HOOKS (WH_WINEVENT-WH_MINHOOK+1)
60 #define HOOK_ENTRY(p) LIST_ENTRY( (p), struct hook, chain )
62 struct hook_table
64 struct object obj; /* object header */
65 struct list hooks[NB_HOOKS]; /* array of hook chains */
66 int counts[NB_HOOKS]; /* use counts for each hook chain */
69 static void hook_table_dump( struct object *obj, int verbose );
70 static void hook_table_destroy( struct object *obj );
72 static const struct object_ops hook_table_ops =
74 sizeof(struct hook_table), /* size */
75 hook_table_dump, /* dump */
76 no_add_queue, /* add_queue */
77 NULL, /* remove_queue */
78 NULL, /* signaled */
79 NULL, /* satisfied */
80 no_signal, /* signal */
81 no_get_fd, /* get_fd */
82 hook_table_destroy /* destroy */
86 static struct hook_table *global_hooks;
88 /* create a new hook table */
89 static struct hook_table *alloc_hook_table(void)
91 struct hook_table *table;
92 int i;
94 if ((table = alloc_object( &hook_table_ops )))
96 for (i = 0; i < NB_HOOKS; i++)
98 list_init( &table->hooks[i] );
99 table->counts[i] = 0;
102 return table;
105 /* create a new hook and add it to the specified table */
106 static struct hook *add_hook( struct thread *thread, int index, int global )
108 struct hook *hook;
109 struct hook_table *table = global ? global_hooks : get_queue_hooks(thread);
111 if (!table)
113 if (!(table = alloc_hook_table())) return NULL;
114 if (global) global_hooks = table;
115 else set_queue_hooks( thread, table );
117 if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
119 if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
121 free( hook );
122 return NULL;
124 hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
125 hook->index = index;
126 list_add_head( &table->hooks[index], &hook->chain );
127 return hook;
130 /* free a hook, removing it from its chain */
131 static void free_hook( struct hook *hook )
133 free_user_handle( hook->handle );
134 if (hook->module) free( hook->module );
135 if (hook->thread) release_object( hook->thread );
136 if (hook->process) release_object( hook->process );
137 release_object( hook->owner );
138 list_remove( &hook->chain );
139 free( hook );
142 /* find a hook from its index and proc */
143 static struct hook *find_hook( struct thread *thread, int index, void *proc )
145 struct list *p;
146 struct hook_table *table = get_queue_hooks( thread );
148 if (table)
150 LIST_FOR_EACH( p, &table->hooks[index] )
152 struct hook *hook = HOOK_ENTRY( p );
153 if (hook->proc == proc) return hook;
156 return NULL;
159 /* get the hook table that a given hook belongs to */
160 inline static struct hook_table *get_table( struct hook *hook )
162 if (!hook->thread) return global_hooks;
163 if (hook->index + WH_MINHOOK == WH_KEYBOARD_LL) return global_hooks;
164 if (hook->index + WH_MINHOOK == WH_MOUSE_LL) return global_hooks;
165 return get_queue_hooks(hook->thread);
168 /* get the first hook in the chain */
169 inline static struct hook *get_first_hook( struct hook_table *table, int index )
171 struct list *elem = list_head( &table->hooks[index] );
172 return elem ? HOOK_ENTRY( elem ) : NULL;
175 /* check if a given hook should run in the current thread */
176 inline static int run_hook_in_current_thread( struct hook *hook )
178 if ((!hook->process || hook->process == current->process) &&
179 (!(hook->flags & WINEVENT_SKIPOWNPROCESS) || hook->process != current->process))
181 if ((!hook->thread || hook->thread == current) &&
182 (!(hook->flags & WINEVENT_SKIPOWNTHREAD) || hook->thread != current))
183 return 1;
185 return 0;
188 /* find the first non-deleted hook in the chain */
189 inline static struct hook *get_first_valid_hook( struct hook_table *table, int index,
190 int event, user_handle_t win,
191 int object_id, int child_id )
193 struct hook *hook = get_first_hook( table, index );
195 while (hook)
197 if (hook->proc && run_hook_in_current_thread( hook ))
199 if (event >= hook->event_min && event <= hook->event_max)
201 if (hook->flags & WINEVENT_INCONTEXT) return hook;
203 /* only winevent hooks may be out of context */
204 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
205 post_win_event( hook->owner, event, win, object_id, child_id,
206 hook->proc, hook->module, hook->module_size,
207 hook->handle );
210 hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
212 return hook;
215 /* find the next hook in the chain, skipping the deleted ones */
216 static struct hook *get_next_hook( struct hook *hook, int event, user_handle_t win,
217 int object_id, int child_id )
219 struct hook_table *table = get_table( hook );
220 int index = hook->index;
222 while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
224 if (hook->proc && run_hook_in_current_thread( hook ))
226 if (event >= hook->event_min && event <= hook->event_max)
228 if (hook->flags & WINEVENT_INCONTEXT) return hook;
230 /* only winevent hooks may be out of context */
231 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
232 post_win_event( hook->owner, event, win, object_id, child_id,
233 hook->proc, hook->module, hook->module_size,
234 hook->handle );
238 if (global_hooks && table != global_hooks) /* now search through the global table */
240 hook = get_first_valid_hook( global_hooks, index, event, win, object_id, child_id );
242 return hook;
245 static void hook_table_dump( struct object *obj, int verbose )
247 struct hook_table *table = (struct hook_table *)obj;
248 if (table == global_hooks) fprintf( stderr, "Global hook table\n" );
249 else fprintf( stderr, "Hook table\n" );
252 static void hook_table_destroy( struct object *obj )
254 int i;
255 struct hook *hook;
256 struct hook_table *table = (struct hook_table *)obj;
258 for (i = 0; i < NB_HOOKS; i++)
260 while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
264 /* free the global hooks table */
265 void close_global_hooks(void)
267 if (global_hooks) release_object( global_hooks );
270 /* remove a hook, freeing it if the chain is not in use */
271 static void remove_hook( struct hook *hook )
273 struct hook_table *table = get_table( hook );
274 if (table->counts[hook->index])
275 hook->proc = NULL; /* chain is in use, just mark it and return */
276 else
277 free_hook( hook );
280 /* release a hook chain, removing deleted hooks if the use count drops to 0 */
281 static void release_hook_chain( struct hook_table *table, int index )
283 if (!table->counts[index]) /* use count shouldn't already be 0 */
285 set_error( STATUS_INVALID_PARAMETER );
286 return;
288 if (!--table->counts[index])
290 struct hook *hook = get_first_hook( table, index );
291 while (hook)
293 struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
294 if (!hook->proc) free_hook( hook );
295 hook = next;
300 /* remove all global hooks owned by a given thread */
301 void remove_thread_hooks( struct thread *thread )
303 int index;
305 if (!global_hooks) return;
307 /* only low-level keyboard/mouse global hooks can be owned by a thread */
308 for (index = WH_KEYBOARD_LL - WH_MINHOOK; index <= WH_MOUSE_LL - WH_MINHOOK; index++)
310 struct hook *hook = get_first_hook( global_hooks, index );
311 while (hook)
313 struct hook *next = HOOK_ENTRY( list_next( &global_hooks->hooks[index], &hook->chain ) );
314 if (hook->thread == thread) remove_hook( hook );
315 hook = next;
320 /* get a bitmap of active hooks in a hook table */
321 static int is_hook_active( struct hook_table *table, int index )
323 struct hook *hook = get_first_hook( table, index );
325 while (hook)
327 if (hook->proc && run_hook_in_current_thread( hook )) return 1;
328 hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
330 return 0;
333 /* get a bitmap of all active hooks for the current thread */
334 unsigned int get_active_hooks(void)
336 struct hook_table *table = get_queue_hooks( current );
337 unsigned int ret = 1 << 31; /* set high bit to indicate that the bitmap is valid */
338 int id;
340 for (id = WH_MINHOOK; id <= WH_WINEVENT; id++)
342 if ((table && is_hook_active( table, id - WH_MINHOOK )) ||
343 (global_hooks && is_hook_active( global_hooks, id - WH_MINHOOK )))
344 ret |= 1 << (id - WH_MINHOOK);
346 return ret;
349 /* set a window hook */
350 DECL_HANDLER(set_hook)
352 struct process *process = NULL;
353 struct thread *thread = NULL;
354 struct hook *hook;
355 WCHAR *module;
356 int global;
357 size_t module_size = get_req_data_size();
359 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
361 set_error( STATUS_INVALID_PARAMETER );
362 return;
365 if (req->pid && !(process = get_process_from_id( req->pid ))) return;
367 if (req->tid)
369 if (!(thread = get_thread_from_id( req->tid )))
371 if (process) release_object( process );
372 return;
374 if (process && process != thread->process)
376 release_object( process );
377 release_object( thread );
378 set_error( STATUS_INVALID_PARAMETER );
379 return;
383 if (req->id == WH_KEYBOARD_LL || req->id == WH_MOUSE_LL)
385 /* low-level hardware hooks are special: always global, but without a module */
386 thread = (struct thread *)grab_object( current );
387 module = NULL;
388 global = 1;
390 else if (!req->tid)
392 /* out of context hooks do not need a module handle */
393 if (!module_size && (req->flags & WINEVENT_INCONTEXT))
395 if (process) release_object( process );
396 if (thread) release_object( thread );
397 set_error( STATUS_INVALID_PARAMETER );
398 return;
401 if (!(module = memdup( get_req_data(), module_size ))) return;
402 thread = NULL;
403 global = 1;
405 else
407 module = NULL;
408 global = 0;
411 if ((hook = add_hook( thread, req->id - WH_MINHOOK, global )))
413 hook->owner = (struct thread *)grab_object( current );
414 hook->process = process ? (struct process *)grab_object( process ) : NULL;
415 hook->event_min = req->event_min;
416 hook->event_max = req->event_max;
417 hook->flags = req->flags;
418 hook->proc = req->proc;
419 hook->unicode = req->unicode;
420 hook->module = module;
421 hook->module_size = module_size;
422 reply->handle = hook->handle;
423 reply->active_hooks = get_active_hooks();
425 else if (module) free( module );
427 if (process) release_object( process );
428 if (thread) release_object( thread );
432 /* remove a window hook */
433 DECL_HANDLER(remove_hook)
435 struct hook *hook;
437 if (req->handle)
439 if (!(hook = get_user_object( req->handle, USER_HOOK )))
441 set_error( STATUS_INVALID_HANDLE );
442 return;
445 else
447 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
449 set_error( STATUS_INVALID_PARAMETER );
450 return;
452 if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
454 set_error( STATUS_INVALID_PARAMETER );
455 return;
458 remove_hook( hook );
459 reply->active_hooks = get_active_hooks();
463 /* start calling a hook chain */
464 DECL_HANDLER(start_hook_chain)
466 struct hook *hook;
467 struct hook_table *table = get_queue_hooks( current );
469 if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
471 set_error( STATUS_INVALID_PARAMETER );
472 return;
475 reply->active_hooks = get_active_hooks();
477 if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK, req->event,
478 req->window, req->object_id, req->child_id )))
480 /* try global table */
481 if (!(table = global_hooks) ||
482 !(hook = get_first_valid_hook( global_hooks, req->id - WH_MINHOOK, req->event,
483 req->window, req->object_id, req->child_id )))
484 return; /* no hook set */
487 if (hook->thread && hook->thread != current) /* must run in other thread */
489 reply->pid = get_process_id( hook->thread->process );
490 reply->tid = get_thread_id( hook->thread );
492 else
494 reply->pid = 0;
495 reply->tid = 0;
497 reply->proc = hook->proc;
498 reply->handle = hook->handle;
499 reply->unicode = hook->unicode;
500 table->counts[hook->index]++;
501 if (hook->module) set_reply_data( hook->module, hook->module_size );
505 /* finished calling a hook chain */
506 DECL_HANDLER(finish_hook_chain)
508 struct hook_table *table = get_queue_hooks( current );
509 int index = req->id - WH_MINHOOK;
511 if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
513 set_error( STATUS_INVALID_PARAMETER );
514 return;
516 if (table) release_hook_chain( table, index );
517 if (global_hooks) release_hook_chain( global_hooks, index );
521 /* get the next hook to call */
522 DECL_HANDLER(get_next_hook)
524 struct hook *hook, *next;
526 if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
527 if (hook->thread && (hook->thread != current))
529 set_error( STATUS_INVALID_HANDLE );
530 return;
532 if ((next = get_next_hook( hook, req->event, req->window, req->object_id, req->child_id )))
534 reply->next = next->handle;
535 reply->id = next->index + WH_MINHOOK;
536 reply->prev_unicode = hook->unicode;
537 reply->next_unicode = next->unicode;
538 if (next->module) set_reply_data( next->module, next->module_size );
539 if (next->thread && next->thread != current)
541 reply->pid = get_process_id( next->thread->process );
542 reply->tid = get_thread_id( next->thread );
544 else
546 reply->pid = 0;
547 reply->tid = 0;
549 reply->proc = next->proc;