d3d10core/tests: Add test for creating RTV with DXGI_FORMAT_UNKNOWN.
[wine.git] / server / hook.c
blob3a0e4b4d1d3982ffcf7485bfd0b95be6300fce68
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 hook_table_dump, /* dump */
80 no_get_type, /* get_type */
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 no_map_access, /* map_access */
88 default_get_sd, /* get_sd */
89 default_set_sd, /* set_sd */
90 no_lookup_name, /* lookup_name */
91 no_link_name, /* link_name */
92 NULL, /* unlink_name */
93 no_open_file, /* open_file */
94 no_close_handle, /* close_handle */
95 hook_table_destroy /* destroy */
99 /* create a new hook table */
100 static struct hook_table *alloc_hook_table(void)
102 struct hook_table *table;
103 int i;
105 if ((table = alloc_object( &hook_table_ops )))
107 for (i = 0; i < NB_HOOKS; i++)
109 list_init( &table->hooks[i] );
110 table->counts[i] = 0;
113 return table;
116 static struct hook_table *get_global_hooks( struct thread *thread )
118 struct hook_table *table;
119 struct desktop *desktop;
121 if (!thread->desktop) return NULL;
122 if (!(desktop = get_thread_desktop( thread, 0 ))) return NULL;
123 table = desktop->global_hooks;
124 release_object( desktop );
125 return table;
128 /* create a new hook and add it to the specified table */
129 static struct hook *add_hook( struct desktop *desktop, struct thread *thread, int index, int global )
131 struct hook *hook;
132 struct hook_table *table = global ? desktop->global_hooks : get_queue_hooks(thread);
134 if (!table)
136 if (!(table = alloc_hook_table())) return NULL;
137 if (global) desktop->global_hooks = table;
138 else set_queue_hooks( thread, table );
140 if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
142 if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
144 free( hook );
145 return NULL;
147 hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
148 hook->table = table;
149 hook->index = index;
150 list_add_head( &table->hooks[index], &hook->chain );
151 if (thread) thread->desktop_users++;
152 return hook;
155 /* free a hook, removing it from its chain */
156 static void free_hook( struct hook *hook )
158 free_user_handle( hook->handle );
159 free( hook->module );
160 if (hook->thread)
162 assert( hook->thread->desktop_users > 0 );
163 hook->thread->desktop_users--;
164 release_object( hook->thread );
166 if (hook->process) release_object( hook->process );
167 release_object( hook->owner );
168 list_remove( &hook->chain );
169 free( hook );
172 /* find a hook from its index and proc */
173 static struct hook *find_hook( struct thread *thread, int index, client_ptr_t proc )
175 struct list *p;
176 struct hook_table *table = get_queue_hooks( thread );
178 if (table)
180 LIST_FOR_EACH( p, &table->hooks[index] )
182 struct hook *hook = HOOK_ENTRY( p );
183 if (hook->proc == proc) return hook;
186 return NULL;
189 /* get the first hook in the chain */
190 static inline struct hook *get_first_hook( struct hook_table *table, int index )
192 struct list *elem = list_head( &table->hooks[index] );
193 return elem ? HOOK_ENTRY( elem ) : NULL;
196 /* check if a given hook should run in the owner thread instead of the current thread */
197 static inline int run_hook_in_owner_thread( struct hook *hook )
199 if ((hook->index == WH_MOUSE_LL - WH_MINHOOK ||
200 hook->index == WH_KEYBOARD_LL - WH_MINHOOK))
201 return hook->owner != current;
202 return 0;
205 /* check if a given hook should run in the current thread */
206 static inline int run_hook_in_current_thread( struct hook *hook )
208 if (hook->process && hook->process != current->process) return 0;
209 if ((hook->flags & WINEVENT_SKIPOWNPROCESS) && hook->process == current->process) return 0;
210 if (hook->thread && hook->thread != current) return 0;
211 if ((hook->flags & WINEVENT_SKIPOWNTHREAD) && hook->thread == current) return 0;
212 /* don't run low-level hooks in processes suspended for debugging */
213 if (run_hook_in_owner_thread( hook ) && hook->owner->process->suspend) return 0;
214 return 1;
217 /* find the first non-deleted hook in the chain */
218 static inline struct hook *get_first_valid_hook( struct hook_table *table, int index,
219 int event, user_handle_t win,
220 int object_id, int child_id )
222 struct hook *hook = get_first_hook( table, index );
224 while (hook)
226 if (hook->proc && run_hook_in_current_thread( hook ))
228 if (event >= hook->event_min && event <= hook->event_max)
230 if (hook->flags & WINEVENT_INCONTEXT) return hook;
232 /* only winevent hooks may be out of context */
233 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
234 post_win_event( hook->owner, event, win, object_id, child_id,
235 hook->proc, hook->module, hook->module_size,
236 hook->handle );
239 hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
241 return hook;
244 /* find the next hook in the chain, skipping the deleted ones */
245 static struct hook *get_next_hook( struct thread *thread, struct hook *hook, int event,
246 user_handle_t win, int object_id, int child_id )
248 struct hook_table *global_hooks, *table = hook->table;
249 int index = hook->index;
251 while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
253 if (hook->proc && run_hook_in_current_thread( hook ))
255 if (event >= hook->event_min && event <= hook->event_max)
257 if (hook->flags & WINEVENT_INCONTEXT) return hook;
259 /* only winevent hooks may be out of context */
260 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
261 post_win_event( hook->owner, event, win, object_id, child_id,
262 hook->proc, hook->module, hook->module_size,
263 hook->handle );
267 global_hooks = get_global_hooks( thread );
268 if (global_hooks && table != global_hooks) /* now search through the global table */
270 hook = get_first_valid_hook( global_hooks, index, event, win, object_id, child_id );
272 return hook;
275 static void hook_table_dump( struct object *obj, int verbose )
277 /* struct hook_table *table = (struct hook_table *)obj; */
278 fprintf( stderr, "Hook table\n" );
281 static void hook_table_destroy( struct object *obj )
283 int i;
284 struct hook *hook;
285 struct hook_table *table = (struct hook_table *)obj;
287 for (i = 0; i < NB_HOOKS; i++)
289 while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
293 /* remove a hook, freeing it if the chain is not in use */
294 static void remove_hook( struct hook *hook )
296 if (hook->table->counts[hook->index])
297 hook->proc = 0; /* chain is in use, just mark it and return */
298 else
299 free_hook( hook );
302 /* release a hook chain, removing deleted hooks if the use count drops to 0 */
303 static void release_hook_chain( struct hook_table *table, int index )
305 if (!table->counts[index]) /* use count shouldn't already be 0 */
307 set_error( STATUS_INVALID_PARAMETER );
308 return;
310 if (!--table->counts[index])
312 struct hook *hook = get_first_hook( table, index );
313 while (hook)
315 struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
316 if (!hook->proc) free_hook( hook );
317 hook = next;
322 /* remove all global hooks owned by a given thread */
323 void remove_thread_hooks( struct thread *thread )
325 struct hook_table *global_hooks = get_global_hooks( thread );
326 int index;
328 if (!global_hooks) return;
330 /* only low-level keyboard/mouse global hooks can be owned by a thread */
331 for (index = WH_KEYBOARD_LL - WH_MINHOOK; index <= WH_MOUSE_LL - WH_MINHOOK; index++)
333 struct hook *hook = get_first_hook( global_hooks, index );
334 while (hook)
336 struct hook *next = HOOK_ENTRY( list_next( &global_hooks->hooks[index], &hook->chain ) );
337 if (hook->thread == thread) remove_hook( hook );
338 hook = next;
343 /* get a bitmap of active hooks in a hook table */
344 static int is_hook_active( struct hook_table *table, int index )
346 struct hook *hook = get_first_hook( table, index );
348 while (hook)
350 if (hook->proc && run_hook_in_current_thread( hook )) return 1;
351 hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
353 return 0;
356 /* get a bitmap of all active hooks for the current thread */
357 unsigned int get_active_hooks(void)
359 struct hook_table *table = get_queue_hooks( current );
360 struct hook_table *global_hooks = get_global_hooks( current );
361 unsigned int ret = 1u << 31; /* set high bit to indicate that the bitmap is valid */
362 int id;
364 for (id = WH_MINHOOK; id <= WH_WINEVENT; id++)
366 if ((table && is_hook_active( table, id - WH_MINHOOK )) ||
367 (global_hooks && is_hook_active( global_hooks, id - WH_MINHOOK )))
368 ret |= 1 << (id - WH_MINHOOK);
370 return ret;
373 /* return the thread that owns the first global hook */
374 struct thread *get_first_global_hook( int id )
376 struct hook *hook;
377 struct hook_table *global_hooks = get_global_hooks( current );
379 if (!global_hooks) return NULL;
380 if (!(hook = get_first_valid_hook( global_hooks, id - WH_MINHOOK, EVENT_MIN, 0, 0, 0 ))) return NULL;
381 return hook->owner;
384 /* set a window hook */
385 DECL_HANDLER(set_hook)
387 struct process *process = NULL;
388 struct thread *thread = NULL;
389 struct desktop *desktop;
390 struct hook *hook;
391 WCHAR *module;
392 int global;
393 data_size_t module_size = get_req_data_size();
395 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
397 set_error( STATUS_INVALID_PARAMETER );
398 return;
401 if (!(desktop = get_thread_desktop( current, DESKTOP_HOOKCONTROL ))) return;
403 if (req->pid && !(process = get_process_from_id( req->pid ))) goto done;
405 if (req->tid)
407 if (!(thread = get_thread_from_id( req->tid ))) goto done;
408 if (process && process != thread->process)
410 set_error( STATUS_INVALID_PARAMETER );
411 goto done;
415 if (req->id == WH_KEYBOARD_LL || req->id == WH_MOUSE_LL)
417 /* low-level hardware hooks are special: always global, but without a module */
418 if (thread)
420 set_error( STATUS_INVALID_PARAMETER );
421 goto done;
423 module = NULL;
424 global = 1;
426 else if (!req->tid)
428 /* out of context hooks do not need a module handle */
429 if (!module_size && (req->flags & WINEVENT_INCONTEXT))
431 set_error( STATUS_INVALID_PARAMETER );
432 goto done;
434 if (!(module = memdup( get_req_data(), module_size ))) goto done;
435 global = 1;
437 else
439 /* module is optional only if hook is in current process */
440 if (!module_size)
442 module = NULL;
443 if (thread->process != current->process)
445 set_error( STATUS_INVALID_PARAMETER );
446 goto done;
449 else if (!(module = memdup( get_req_data(), module_size ))) goto done;
450 global = 0;
453 if ((hook = add_hook( desktop, thread, req->id - WH_MINHOOK, global )))
455 hook->owner = (struct thread *)grab_object( current );
456 hook->process = process ? (struct process *)grab_object( process ) : NULL;
457 hook->event_min = req->event_min;
458 hook->event_max = req->event_max;
459 hook->flags = req->flags;
460 hook->proc = req->proc;
461 hook->unicode = req->unicode;
462 hook->module = module;
463 hook->module_size = module_size;
464 reply->handle = hook->handle;
465 reply->active_hooks = get_active_hooks();
467 else free( module );
469 done:
470 if (process) release_object( process );
471 if (thread) release_object( thread );
472 release_object( desktop );
476 /* remove a window hook */
477 DECL_HANDLER(remove_hook)
479 struct hook *hook;
481 if (req->handle)
483 if (!(hook = get_user_object( req->handle, USER_HOOK )))
485 set_error( STATUS_INVALID_HANDLE );
486 return;
489 else
491 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
493 set_error( STATUS_INVALID_PARAMETER );
494 return;
496 if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
498 set_error( STATUS_INVALID_PARAMETER );
499 return;
502 remove_hook( hook );
503 reply->active_hooks = get_active_hooks();
507 /* start calling a hook chain */
508 DECL_HANDLER(start_hook_chain)
510 struct hook *hook;
511 struct hook_table *table = get_queue_hooks( current );
512 struct hook_table *global_table = get_global_hooks( current );
514 if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
516 set_error( STATUS_INVALID_PARAMETER );
517 return;
520 reply->active_hooks = get_active_hooks();
522 if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK, req->event,
523 req->window, req->object_id, req->child_id )))
525 /* try global table */
526 if (!global_table || !(hook = get_first_valid_hook( global_table, req->id - WH_MINHOOK, req->event,
527 req->window, req->object_id, req->child_id )))
528 return; /* no hook set */
531 if (run_hook_in_owner_thread( hook ))
533 reply->pid = get_process_id( hook->owner->process );
534 reply->tid = get_thread_id( hook->owner );
536 else
538 reply->pid = 0;
539 reply->tid = 0;
541 reply->proc = hook->proc;
542 reply->handle = hook->handle;
543 reply->unicode = hook->unicode;
544 if (table) table->counts[hook->index]++;
545 if (global_table) global_table->counts[hook->index]++;
546 if (hook->module) set_reply_data( hook->module, hook->module_size );
550 /* finished calling a hook chain */
551 DECL_HANDLER(finish_hook_chain)
553 struct hook_table *table = get_queue_hooks( current );
554 struct hook_table *global_hooks = get_global_hooks( current );
555 int index = req->id - WH_MINHOOK;
557 if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
559 set_error( STATUS_INVALID_PARAMETER );
560 return;
562 if (table) release_hook_chain( table, index );
563 if (global_hooks) release_hook_chain( global_hooks, index );
567 /* get the hook information */
568 DECL_HANDLER(get_hook_info)
570 struct hook *hook;
572 if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
573 if (hook->thread && (hook->thread != current))
575 set_error( STATUS_INVALID_HANDLE );
576 return;
578 if (req->get_next && !(hook = get_next_hook( current, hook, req->event, req->window,
579 req->object_id, req->child_id )))
580 return;
582 reply->handle = hook->handle;
583 reply->id = hook->index + WH_MINHOOK;
584 reply->unicode = hook->unicode;
585 if (hook->module) set_reply_data( hook->module, min(hook->module_size,get_reply_max_size()) );
586 if (run_hook_in_owner_thread( hook ))
588 reply->pid = get_process_id( hook->owner->process );
589 reply->tid = get_thread_id( hook->owner );
591 else
593 reply->pid = 0;
594 reply->tid = 0;
596 reply->proc = hook->proc;