- VarCmp: handle comparision of VT_EMPTY with an integer
[wine.git] / server / hook.c
blobc3a0244895c82b7ce8c0c8aa3d7291eeea743edc
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 no_close_handle, /* close_handle */
83 hook_table_destroy /* destroy */
87 static struct hook_table *global_hooks;
89 /* create a new hook table */
90 static struct hook_table *alloc_hook_table(void)
92 struct hook_table *table;
93 int i;
95 if ((table = alloc_object( &hook_table_ops )))
97 for (i = 0; i < NB_HOOKS; i++)
99 list_init( &table->hooks[i] );
100 table->counts[i] = 0;
103 return table;
106 /* create a new hook and add it to the specified table */
107 static struct hook *add_hook( struct thread *thread, int index, int global )
109 struct hook *hook;
110 struct hook_table *table = global ? global_hooks : get_queue_hooks(thread);
112 if (!table)
114 if (!(table = alloc_hook_table())) return NULL;
115 if (global) global_hooks = table;
116 else set_queue_hooks( thread, table );
118 if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
120 if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
122 free( hook );
123 return NULL;
125 hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
126 hook->index = index;
127 list_add_head( &table->hooks[index], &hook->chain );
128 return hook;
131 /* free a hook, removing it from its chain */
132 static void free_hook( struct hook *hook )
134 free_user_handle( hook->handle );
135 if (hook->module) free( hook->module );
136 if (hook->thread) release_object( hook->thread );
137 if (hook->process) release_object( hook->process );
138 release_object( hook->owner );
139 list_remove( &hook->chain );
140 free( hook );
143 /* find a hook from its index and proc */
144 static struct hook *find_hook( struct thread *thread, int index, void *proc )
146 struct list *p;
147 struct hook_table *table = get_queue_hooks( thread );
149 if (table)
151 LIST_FOR_EACH( p, &table->hooks[index] )
153 struct hook *hook = HOOK_ENTRY( p );
154 if (hook->proc == proc) return hook;
157 return NULL;
160 /* get the hook table that a given hook belongs to */
161 inline static struct hook_table *get_table( struct hook *hook )
163 if (!hook->thread) return global_hooks;
164 if (hook->index + WH_MINHOOK == WH_KEYBOARD_LL) return global_hooks;
165 if (hook->index + WH_MINHOOK == WH_MOUSE_LL) return global_hooks;
166 return get_queue_hooks(hook->thread);
169 /* get the first hook in the chain */
170 inline static struct hook *get_first_hook( struct hook_table *table, int index )
172 struct list *elem = list_head( &table->hooks[index] );
173 return elem ? HOOK_ENTRY( elem ) : NULL;
176 /* check if a given hook should run in the current thread */
177 inline static int run_hook_in_current_thread( struct hook *hook )
179 if ((!hook->process || hook->process == current->process) &&
180 (!(hook->flags & WINEVENT_SKIPOWNPROCESS) || hook->process != current->process))
182 if ((!hook->thread || hook->thread == current) &&
183 (!(hook->flags & WINEVENT_SKIPOWNTHREAD) || hook->thread != current))
184 return 1;
186 return 0;
189 /* find the first non-deleted hook in the chain */
190 inline static struct hook *get_first_valid_hook( struct hook_table *table, int index,
191 int event, user_handle_t win,
192 int object_id, int child_id )
194 struct hook *hook = get_first_hook( table, index );
196 while (hook)
198 if (hook->proc && run_hook_in_current_thread( hook ))
200 if (event >= hook->event_min && event <= hook->event_max)
202 if (hook->flags & WINEVENT_INCONTEXT) return hook;
204 /* only winevent hooks may be out of context */
205 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
206 post_win_event( hook->owner, event, win, object_id, child_id,
207 hook->proc, hook->module, hook->module_size,
208 hook->handle );
211 hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
213 return hook;
216 /* find the next hook in the chain, skipping the deleted ones */
217 static struct hook *get_next_hook( struct hook *hook, int event, user_handle_t win,
218 int object_id, int child_id )
220 struct hook_table *table = get_table( hook );
221 int index = hook->index;
223 while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
225 if (hook->proc && run_hook_in_current_thread( hook ))
227 if (event >= hook->event_min && event <= hook->event_max)
229 if (hook->flags & WINEVENT_INCONTEXT) return hook;
231 /* only winevent hooks may be out of context */
232 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
233 post_win_event( hook->owner, event, win, object_id, child_id,
234 hook->proc, hook->module, hook->module_size,
235 hook->handle );
239 if (global_hooks && table != global_hooks) /* now search through the global table */
241 hook = get_first_valid_hook( global_hooks, index, event, win, object_id, child_id );
243 return hook;
246 static void hook_table_dump( struct object *obj, int verbose )
248 struct hook_table *table = (struct hook_table *)obj;
249 if (table == global_hooks) fprintf( stderr, "Global hook table\n" );
250 else fprintf( stderr, "Hook table\n" );
253 static void hook_table_destroy( struct object *obj )
255 int i;
256 struct hook *hook;
257 struct hook_table *table = (struct hook_table *)obj;
259 for (i = 0; i < NB_HOOKS; i++)
261 while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
265 /* free the global hooks table */
266 void close_global_hooks(void)
268 if (global_hooks) release_object( global_hooks );
271 /* remove a hook, freeing it if the chain is not in use */
272 static void remove_hook( struct hook *hook )
274 struct hook_table *table = get_table( hook );
275 if (table->counts[hook->index])
276 hook->proc = NULL; /* chain is in use, just mark it and return */
277 else
278 free_hook( hook );
281 /* release a hook chain, removing deleted hooks if the use count drops to 0 */
282 static void release_hook_chain( struct hook_table *table, int index )
284 if (!table->counts[index]) /* use count shouldn't already be 0 */
286 set_error( STATUS_INVALID_PARAMETER );
287 return;
289 if (!--table->counts[index])
291 struct hook *hook = get_first_hook( table, index );
292 while (hook)
294 struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
295 if (!hook->proc) free_hook( hook );
296 hook = next;
301 /* remove all global hooks owned by a given thread */
302 void remove_thread_hooks( struct thread *thread )
304 int index;
306 if (!global_hooks) return;
308 /* only low-level keyboard/mouse global hooks can be owned by a thread */
309 for (index = WH_KEYBOARD_LL - WH_MINHOOK; index <= WH_MOUSE_LL - WH_MINHOOK; index++)
311 struct hook *hook = get_first_hook( global_hooks, index );
312 while (hook)
314 struct hook *next = HOOK_ENTRY( list_next( &global_hooks->hooks[index], &hook->chain ) );
315 if (hook->thread == thread) remove_hook( hook );
316 hook = next;
321 /* get a bitmap of active hooks in a hook table */
322 static int is_hook_active( struct hook_table *table, int index )
324 struct hook *hook = get_first_hook( table, index );
326 while (hook)
328 if (hook->proc && run_hook_in_current_thread( hook )) return 1;
329 hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
331 return 0;
334 /* get a bitmap of all active hooks for the current thread */
335 unsigned int get_active_hooks(void)
337 struct hook_table *table = get_queue_hooks( current );
338 unsigned int ret = 1 << 31; /* set high bit to indicate that the bitmap is valid */
339 int id;
341 for (id = WH_MINHOOK; id <= WH_WINEVENT; id++)
343 if ((table && is_hook_active( table, id - WH_MINHOOK )) ||
344 (global_hooks && is_hook_active( global_hooks, id - WH_MINHOOK )))
345 ret |= 1 << (id - WH_MINHOOK);
347 return ret;
350 /* set a window hook */
351 DECL_HANDLER(set_hook)
353 struct process *process = NULL;
354 struct thread *thread = NULL;
355 struct hook *hook;
356 WCHAR *module;
357 int global;
358 size_t module_size = get_req_data_size();
360 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
362 set_error( STATUS_INVALID_PARAMETER );
363 return;
366 if (req->pid && !(process = get_process_from_id( req->pid ))) return;
368 if (req->tid)
370 if (!(thread = get_thread_from_id( req->tid )))
372 if (process) release_object( process );
373 return;
375 if (process && process != thread->process)
377 release_object( process );
378 release_object( thread );
379 set_error( STATUS_INVALID_PARAMETER );
380 return;
384 if (req->id == WH_KEYBOARD_LL || req->id == WH_MOUSE_LL)
386 /* low-level hardware hooks are special: always global, but without a module */
387 thread = (struct thread *)grab_object( current );
388 module = NULL;
389 global = 1;
391 else if (!req->tid)
393 /* out of context hooks do not need a module handle */
394 if (!module_size && (req->flags & WINEVENT_INCONTEXT))
396 if (process) release_object( process );
397 if (thread) release_object( thread );
398 set_error( STATUS_INVALID_PARAMETER );
399 return;
402 if (!(module = memdup( get_req_data(), module_size ))) return;
403 thread = NULL;
404 global = 1;
406 else
408 module = NULL;
409 global = 0;
412 if ((hook = add_hook( thread, req->id - WH_MINHOOK, global )))
414 hook->owner = (struct thread *)grab_object( current );
415 hook->process = process ? (struct process *)grab_object( process ) : NULL;
416 hook->event_min = req->event_min;
417 hook->event_max = req->event_max;
418 hook->flags = req->flags;
419 hook->proc = req->proc;
420 hook->unicode = req->unicode;
421 hook->module = module;
422 hook->module_size = module_size;
423 reply->handle = hook->handle;
424 reply->active_hooks = get_active_hooks();
426 else if (module) free( module );
428 if (process) release_object( process );
429 if (thread) release_object( thread );
433 /* remove a window hook */
434 DECL_HANDLER(remove_hook)
436 struct hook *hook;
438 if (req->handle)
440 if (!(hook = get_user_object( req->handle, USER_HOOK )))
442 set_error( STATUS_INVALID_HANDLE );
443 return;
446 else
448 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
450 set_error( STATUS_INVALID_PARAMETER );
451 return;
453 if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
455 set_error( STATUS_INVALID_PARAMETER );
456 return;
459 remove_hook( hook );
460 reply->active_hooks = get_active_hooks();
464 /* start calling a hook chain */
465 DECL_HANDLER(start_hook_chain)
467 struct hook *hook;
468 struct hook_table *table = get_queue_hooks( current );
470 if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
472 set_error( STATUS_INVALID_PARAMETER );
473 return;
476 reply->active_hooks = get_active_hooks();
478 if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK, req->event,
479 req->window, req->object_id, req->child_id )))
481 /* try global table */
482 if (!(table = global_hooks) ||
483 !(hook = get_first_valid_hook( global_hooks, req->id - WH_MINHOOK, req->event,
484 req->window, req->object_id, req->child_id )))
485 return; /* no hook set */
488 if (hook->thread && hook->thread != current) /* must run in other thread */
490 reply->pid = get_process_id( hook->thread->process );
491 reply->tid = get_thread_id( hook->thread );
493 else
495 reply->pid = 0;
496 reply->tid = 0;
498 reply->proc = hook->proc;
499 reply->handle = hook->handle;
500 reply->unicode = hook->unicode;
501 table->counts[hook->index]++;
502 if (hook->module) set_reply_data( hook->module, hook->module_size );
506 /* finished calling a hook chain */
507 DECL_HANDLER(finish_hook_chain)
509 struct hook_table *table = get_queue_hooks( current );
510 int index = req->id - WH_MINHOOK;
512 if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
514 set_error( STATUS_INVALID_PARAMETER );
515 return;
517 if (table) release_hook_chain( table, index );
518 if (global_hooks) release_hook_chain( global_hooks, index );
522 /* get the next hook to call */
523 DECL_HANDLER(get_next_hook)
525 struct hook *hook, *next;
527 if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
528 if (hook->thread && (hook->thread != current))
530 set_error( STATUS_INVALID_HANDLE );
531 return;
533 if ((next = get_next_hook( hook, req->event, req->window, req->object_id, req->child_id )))
535 reply->next = next->handle;
536 reply->id = next->index + WH_MINHOOK;
537 reply->prev_unicode = hook->unicode;
538 reply->next_unicode = next->unicode;
539 if (next->module) set_reply_data( next->module, next->module_size );
540 if (next->thread && next->thread != current)
542 reply->pid = get_process_id( next->thread->process );
543 reply->tid = get_thread_id( next->thread );
545 else
547 reply->pid = 0;
548 reply->tid = 0;
550 reply->proc = next->proc;