msvcrt: Import llroundf implementation from musl.
[wine.git] / server / hook.c
blobc048908c295d9d11f68de4bd2393f2ee17c7455c
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 client_ptr_t proc; /* hook function */
56 int unicode; /* is it a unicode hook? */
57 WCHAR *module; /* module name for global hooks */
58 data_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 &no_type, /* type */
80 hook_table_dump, /* dump */
81 no_add_queue, /* add_queue */
82 NULL, /* remove_queue */
83 NULL, /* signaled */
84 NULL, /* satisfied */
85 no_signal, /* signal */
86 no_get_fd, /* get_fd */
87 default_map_access, /* map_access */
88 default_get_sd, /* get_sd */
89 default_set_sd, /* set_sd */
90 no_get_full_name, /* get_full_name */
91 no_lookup_name, /* lookup_name */
92 no_link_name, /* link_name */
93 NULL, /* unlink_name */
94 no_open_file, /* open_file */
95 no_kernel_obj_list, /* get_kernel_obj_list */
96 no_close_handle, /* close_handle */
97 hook_table_destroy /* destroy */
101 /* create a new hook table */
102 static struct hook_table *alloc_hook_table(void)
104 struct hook_table *table;
105 int i;
107 if ((table = alloc_object( &hook_table_ops )))
109 for (i = 0; i < NB_HOOKS; i++)
111 list_init( &table->hooks[i] );
112 table->counts[i] = 0;
115 return table;
118 static struct hook_table *get_global_hooks( struct thread *thread )
120 struct hook_table *table;
121 struct desktop *desktop;
123 if (!thread->desktop) return NULL;
124 if (!(desktop = get_thread_desktop( thread, 0 ))) return NULL;
125 table = desktop->global_hooks;
126 release_object( desktop );
127 return table;
130 /* create a new hook and add it to the specified table */
131 static struct hook *add_hook( struct desktop *desktop, struct thread *thread, int index, int global )
133 struct hook *hook;
134 struct hook_table *table = global ? desktop->global_hooks : get_queue_hooks(thread);
136 if (!table)
138 if (!(table = alloc_hook_table())) return NULL;
139 if (global) desktop->global_hooks = table;
140 else set_queue_hooks( thread, table );
142 if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
144 if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
146 free( hook );
147 return NULL;
149 hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
150 hook->table = table;
151 hook->index = index;
152 list_add_head( &table->hooks[index], &hook->chain );
153 if (thread) thread->desktop_users++;
154 return hook;
157 /* free a hook, removing it from its chain */
158 static void free_hook( struct hook *hook )
160 free_user_handle( hook->handle );
161 free( hook->module );
162 if (hook->thread)
164 assert( hook->thread->desktop_users > 0 );
165 hook->thread->desktop_users--;
166 release_object( hook->thread );
168 if (hook->process) release_object( hook->process );
169 release_object( hook->owner );
170 list_remove( &hook->chain );
171 free( hook );
174 /* find a hook from its index and proc */
175 static struct hook *find_hook( struct thread *thread, int index, client_ptr_t proc )
177 struct list *p;
178 struct hook_table *table = get_queue_hooks( thread );
180 if (table)
182 LIST_FOR_EACH( p, &table->hooks[index] )
184 struct hook *hook = HOOK_ENTRY( p );
185 if (hook->proc == proc) return hook;
188 return NULL;
191 /* get the first hook in the chain */
192 static inline struct hook *get_first_hook( struct hook_table *table, int index )
194 struct list *elem = list_head( &table->hooks[index] );
195 return elem ? HOOK_ENTRY( elem ) : NULL;
198 /* check if a given hook should run in the owner thread instead of the current thread */
199 static inline int run_hook_in_owner_thread( struct hook *hook )
201 if ((hook->index == WH_MOUSE_LL - WH_MINHOOK ||
202 hook->index == WH_KEYBOARD_LL - WH_MINHOOK))
203 return hook->owner != current;
204 return 0;
207 /* check if a given hook should run in the current thread */
208 static inline int run_hook_in_current_thread( struct hook *hook )
210 if (hook->process && hook->process != current->process) return 0;
211 if ((hook->flags & WINEVENT_SKIPOWNPROCESS) && hook->process == current->process) return 0;
212 if (hook->thread && hook->thread != current) return 0;
213 if ((hook->flags & WINEVENT_SKIPOWNTHREAD) && hook->thread == current) return 0;
214 /* don't run low-level hooks in processes suspended for debugging */
215 if (run_hook_in_owner_thread( hook ) && hook->owner->process->suspend) return 0;
216 return 1;
219 /* find the first non-deleted hook in the chain */
220 static inline struct hook *get_first_valid_hook( struct hook_table *table, int index,
221 int event, user_handle_t win,
222 int object_id, int child_id )
224 struct hook *hook = get_first_hook( table, index );
226 while (hook)
228 if (hook->proc && run_hook_in_current_thread( hook ))
230 if (event >= hook->event_min && event <= hook->event_max)
232 if (hook->flags & WINEVENT_INCONTEXT) return hook;
234 /* only winevent hooks may be out of context */
235 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
236 post_win_event( hook->owner, event, win, object_id, child_id,
237 hook->proc, hook->module, hook->module_size,
238 hook->handle );
241 hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
243 return hook;
246 /* find the next hook in the chain, skipping the deleted ones */
247 static struct hook *get_next_hook( struct thread *thread, struct hook *hook, int event,
248 user_handle_t win, int object_id, int child_id )
250 struct hook_table *global_hooks, *table = hook->table;
251 int index = hook->index;
253 while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
255 if (hook->proc && run_hook_in_current_thread( hook ))
257 if (event >= hook->event_min && event <= hook->event_max)
259 if (hook->flags & WINEVENT_INCONTEXT) return hook;
261 /* only winevent hooks may be out of context */
262 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
263 post_win_event( hook->owner, event, win, object_id, child_id,
264 hook->proc, hook->module, hook->module_size,
265 hook->handle );
269 global_hooks = get_global_hooks( thread );
270 if (global_hooks && table != global_hooks) /* now search through the global table */
272 hook = get_first_valid_hook( global_hooks, index, event, win, object_id, child_id );
274 return hook;
277 static void hook_table_dump( struct object *obj, int verbose )
279 /* struct hook_table *table = (struct hook_table *)obj; */
280 fprintf( stderr, "Hook table\n" );
283 static void hook_table_destroy( struct object *obj )
285 int i;
286 struct hook *hook;
287 struct hook_table *table = (struct hook_table *)obj;
289 for (i = 0; i < NB_HOOKS; i++)
291 while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
295 /* remove a hook, freeing it if the chain is not in use */
296 static void remove_hook( struct hook *hook )
298 if (hook->table->counts[hook->index])
299 hook->proc = 0; /* chain is in use, just mark it and return */
300 else
301 free_hook( hook );
304 /* release a hook chain, removing deleted hooks if the use count drops to 0 */
305 static void release_hook_chain( struct hook_table *table, int index )
307 if (!table->counts[index]) /* use count shouldn't already be 0 */
309 set_error( STATUS_INVALID_PARAMETER );
310 return;
312 if (!--table->counts[index])
314 struct hook *hook = get_first_hook( table, index );
315 while (hook)
317 struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
318 if (!hook->proc) free_hook( hook );
319 hook = next;
324 /* remove all global hooks owned by a given thread */
325 void remove_thread_hooks( struct thread *thread )
327 struct hook_table *global_hooks = get_global_hooks( thread );
328 int index;
330 if (!global_hooks) return;
332 /* only low-level keyboard/mouse global hooks can be owned by a thread */
333 for (index = WH_KEYBOARD_LL - WH_MINHOOK; index <= WH_MOUSE_LL - WH_MINHOOK; index++)
335 struct hook *hook = get_first_hook( global_hooks, index );
336 while (hook)
338 struct hook *next = HOOK_ENTRY( list_next( &global_hooks->hooks[index], &hook->chain ) );
339 if (hook->thread == thread) remove_hook( hook );
340 hook = next;
345 /* get a bitmap of active hooks in a hook table */
346 static int is_hook_active( struct hook_table *table, int index )
348 struct hook *hook = get_first_hook( table, index );
350 while (hook)
352 if (hook->proc && run_hook_in_current_thread( hook )) return 1;
353 hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
355 return 0;
358 /* get a bitmap of all active hooks for the current thread */
359 unsigned int get_active_hooks(void)
361 struct hook_table *table = get_queue_hooks( current );
362 struct hook_table *global_hooks = get_global_hooks( current );
363 unsigned int ret = 1u << 31; /* set high bit to indicate that the bitmap is valid */
364 int id;
366 for (id = WH_MINHOOK; id <= WH_WINEVENT; id++)
368 if ((table && is_hook_active( table, id - WH_MINHOOK )) ||
369 (global_hooks && is_hook_active( global_hooks, id - WH_MINHOOK )))
370 ret |= 1 << (id - WH_MINHOOK);
372 return ret;
375 /* return the thread that owns the first global hook */
376 struct thread *get_first_global_hook( int id )
378 struct hook *hook;
379 struct hook_table *global_hooks = get_global_hooks( current );
381 if (!global_hooks) return NULL;
382 if (!(hook = get_first_valid_hook( global_hooks, id - WH_MINHOOK, EVENT_MIN, 0, 0, 0 ))) return NULL;
383 return hook->owner;
386 /* set a window hook */
387 DECL_HANDLER(set_hook)
389 struct process *process = NULL;
390 struct thread *thread = NULL;
391 struct desktop *desktop;
392 struct hook *hook;
393 WCHAR *module;
394 int global;
395 data_size_t module_size = get_req_data_size();
397 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
399 set_error( STATUS_INVALID_PARAMETER );
400 return;
403 if (!(desktop = get_thread_desktop( current, DESKTOP_HOOKCONTROL ))) return;
405 if (req->pid && !(process = get_process_from_id( req->pid ))) goto done;
407 if (req->tid)
409 if (!(thread = get_thread_from_id( req->tid ))) goto done;
410 if (process && process != thread->process)
412 set_error( STATUS_INVALID_PARAMETER );
413 goto done;
417 if (req->id == WH_KEYBOARD_LL || req->id == WH_MOUSE_LL)
419 /* low-level hardware hooks are special: always global, but without a module */
420 if (thread)
422 set_error( STATUS_INVALID_PARAMETER );
423 goto done;
425 module = NULL;
426 global = 1;
428 else if (!req->tid)
430 /* out of context hooks do not need a module handle */
431 if (!module_size && (req->flags & WINEVENT_INCONTEXT))
433 set_error( STATUS_INVALID_PARAMETER );
434 goto done;
436 if (!(module = memdup( get_req_data(), module_size ))) goto done;
437 global = 1;
439 else
441 /* module is optional only if hook is in current process */
442 if (!module_size)
444 module = NULL;
445 if (thread->process != current->process)
447 set_error( STATUS_INVALID_PARAMETER );
448 goto done;
451 else if (!(module = memdup( get_req_data(), module_size ))) goto done;
452 global = 0;
455 if ((hook = add_hook( desktop, thread, req->id - WH_MINHOOK, global )))
457 hook->owner = (struct thread *)grab_object( current );
458 hook->process = process ? (struct process *)grab_object( process ) : NULL;
459 hook->event_min = req->event_min;
460 hook->event_max = req->event_max;
461 hook->flags = req->flags;
462 hook->proc = req->proc;
463 hook->unicode = req->unicode;
464 hook->module = module;
465 hook->module_size = module_size;
466 reply->handle = hook->handle;
467 reply->active_hooks = get_active_hooks();
469 else free( module );
471 done:
472 if (process) release_object( process );
473 if (thread) release_object( thread );
474 release_object( desktop );
478 /* remove a window hook */
479 DECL_HANDLER(remove_hook)
481 struct hook *hook;
483 if (req->handle)
485 if (!(hook = get_user_object( req->handle, USER_HOOK )))
487 set_error( STATUS_INVALID_HANDLE );
488 return;
491 else
493 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
495 set_error( STATUS_INVALID_PARAMETER );
496 return;
498 if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
500 set_error( STATUS_INVALID_PARAMETER );
501 return;
504 remove_hook( hook );
505 reply->active_hooks = get_active_hooks();
509 /* start calling a hook chain */
510 DECL_HANDLER(start_hook_chain)
512 struct hook *hook;
513 struct hook_table *table = get_queue_hooks( current );
514 struct hook_table *global_table = get_global_hooks( current );
516 if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
518 set_error( STATUS_INVALID_PARAMETER );
519 return;
522 reply->active_hooks = get_active_hooks();
524 if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK, req->event,
525 req->window, req->object_id, req->child_id )))
527 /* try global table */
528 if (!global_table || !(hook = get_first_valid_hook( global_table, req->id - WH_MINHOOK, req->event,
529 req->window, req->object_id, req->child_id )))
530 return; /* no hook set */
533 if (run_hook_in_owner_thread( hook ))
535 reply->pid = get_process_id( hook->owner->process );
536 reply->tid = get_thread_id( hook->owner );
538 else
540 reply->pid = 0;
541 reply->tid = 0;
543 reply->proc = hook->proc;
544 reply->handle = hook->handle;
545 reply->unicode = hook->unicode;
546 if (table) table->counts[hook->index]++;
547 if (global_table) global_table->counts[hook->index]++;
548 if (hook->module) set_reply_data( hook->module, hook->module_size );
552 /* finished calling a hook chain */
553 DECL_HANDLER(finish_hook_chain)
555 struct hook_table *table = get_queue_hooks( current );
556 struct hook_table *global_hooks = get_global_hooks( current );
557 int index = req->id - WH_MINHOOK;
559 if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
561 set_error( STATUS_INVALID_PARAMETER );
562 return;
564 if (table) release_hook_chain( table, index );
565 if (global_hooks) release_hook_chain( global_hooks, index );
569 /* get the hook information */
570 DECL_HANDLER(get_hook_info)
572 struct hook *hook;
574 if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
575 if (hook->thread && (hook->thread != current))
577 set_error( STATUS_INVALID_HANDLE );
578 return;
580 if (req->get_next && !(hook = get_next_hook( current, hook, req->event, req->window,
581 req->object_id, req->child_id )))
582 return;
584 reply->handle = hook->handle;
585 reply->id = hook->index + WH_MINHOOK;
586 reply->unicode = hook->unicode;
587 if (hook->module) set_reply_data( hook->module, min(hook->module_size,get_reply_max_size()) );
588 if (run_hook_in_owner_thread( hook ))
590 reply->pid = get_process_id( hook->owner->process );
591 reply->tid = get_thread_id( hook->owner );
593 else
595 reply->pid = 0;
596 reply->tid = 0;
598 reply->proc = hook->proc;