winex11.drv: Make wglMakeCurrent return the correct error when the drawable is invalid.
[wine/multimedia.git] / server / hook.c
blobc9df5d37d3948b8dafa410212e72c0e5aa7a30d1
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_open_file, /* open_file */
92 no_close_handle, /* close_handle */
93 hook_table_destroy /* destroy */
97 /* create a new hook table */
98 static struct hook_table *alloc_hook_table(void)
100 struct hook_table *table;
101 int i;
103 if ((table = alloc_object( &hook_table_ops )))
105 for (i = 0; i < NB_HOOKS; i++)
107 list_init( &table->hooks[i] );
108 table->counts[i] = 0;
111 return table;
114 static struct hook_table *get_global_hooks( struct thread *thread )
116 struct hook_table *table;
117 struct desktop *desktop;
119 if (!thread->desktop) return NULL;
120 if (!(desktop = get_thread_desktop( thread, 0 ))) return NULL;
121 table = desktop->global_hooks;
122 release_object( desktop );
123 return table;
126 /* create a new hook and add it to the specified table */
127 static struct hook *add_hook( struct desktop *desktop, struct thread *thread, int index, int global )
129 struct hook *hook;
130 struct hook_table *table = global ? desktop->global_hooks : get_queue_hooks(thread);
132 if (!table)
134 if (!(table = alloc_hook_table())) return NULL;
135 if (global) desktop->global_hooks = table;
136 else set_queue_hooks( thread, table );
138 if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
140 if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
142 free( hook );
143 return NULL;
145 hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
146 hook->table = table;
147 hook->index = index;
148 list_add_head( &table->hooks[index], &hook->chain );
149 if (thread) thread->desktop_users++;
150 return hook;
153 /* free a hook, removing it from its chain */
154 static void free_hook( struct hook *hook )
156 free_user_handle( hook->handle );
157 free( hook->module );
158 if (hook->thread)
160 assert( hook->thread->desktop_users > 0 );
161 hook->thread->desktop_users--;
162 release_object( hook->thread );
164 if (hook->process) release_object( hook->process );
165 release_object( hook->owner );
166 list_remove( &hook->chain );
167 free( hook );
170 /* find a hook from its index and proc */
171 static struct hook *find_hook( struct thread *thread, int index, client_ptr_t proc )
173 struct list *p;
174 struct hook_table *table = get_queue_hooks( thread );
176 if (table)
178 LIST_FOR_EACH( p, &table->hooks[index] )
180 struct hook *hook = HOOK_ENTRY( p );
181 if (hook->proc == proc) return hook;
184 return NULL;
187 /* get the first hook in the chain */
188 static inline struct hook *get_first_hook( struct hook_table *table, int index )
190 struct list *elem = list_head( &table->hooks[index] );
191 return elem ? HOOK_ENTRY( elem ) : NULL;
194 /* check if a given hook should run in the current thread */
195 static inline int run_hook_in_current_thread( struct hook *hook )
197 if ((!hook->process || hook->process == current->process) &&
198 (!(hook->flags & WINEVENT_SKIPOWNPROCESS) || hook->process != current->process))
200 if ((!hook->thread || hook->thread == current) &&
201 (!(hook->flags & WINEVENT_SKIPOWNTHREAD) || hook->thread != current))
202 return 1;
204 return 0;
207 /* check if a given hook should run in the owner thread instead of the current thread */
208 static inline int run_hook_in_owner_thread( struct hook *hook )
210 if ((hook->index == WH_MOUSE_LL - WH_MINHOOK ||
211 hook->index == WH_KEYBOARD_LL - WH_MINHOOK))
212 return hook->owner != current;
213 return 0;
216 /* find the first non-deleted hook in the chain */
217 static inline struct hook *get_first_valid_hook( struct hook_table *table, int index,
218 int event, user_handle_t win,
219 int object_id, int child_id )
221 struct hook *hook = get_first_hook( table, index );
223 while (hook)
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 );
238 hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
240 return hook;
243 /* find the next hook in the chain, skipping the deleted ones */
244 static struct hook *get_next_hook( struct thread *thread, struct hook *hook, int event,
245 user_handle_t win, int object_id, int child_id )
247 struct hook_table *global_hooks, *table = hook->table;
248 int index = hook->index;
250 while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
252 if (hook->proc && run_hook_in_current_thread( hook ))
254 if (event >= hook->event_min && event <= hook->event_max)
256 if (hook->flags & WINEVENT_INCONTEXT) return hook;
258 /* only winevent hooks may be out of context */
259 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
260 post_win_event( hook->owner, event, win, object_id, child_id,
261 hook->proc, hook->module, hook->module_size,
262 hook->handle );
266 global_hooks = get_global_hooks( thread );
267 if (global_hooks && table != global_hooks) /* now search through the global table */
269 hook = get_first_valid_hook( global_hooks, index, event, win, object_id, child_id );
271 return hook;
274 static void hook_table_dump( struct object *obj, int verbose )
276 /* struct hook_table *table = (struct hook_table *)obj; */
277 fprintf( stderr, "Hook table\n" );
280 static void hook_table_destroy( struct object *obj )
282 int i;
283 struct hook *hook;
284 struct hook_table *table = (struct hook_table *)obj;
286 for (i = 0; i < NB_HOOKS; i++)
288 while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
292 /* remove a hook, freeing it if the chain is not in use */
293 static void remove_hook( struct hook *hook )
295 if (hook->table->counts[hook->index])
296 hook->proc = 0; /* chain is in use, just mark it and return */
297 else
298 free_hook( hook );
301 /* release a hook chain, removing deleted hooks if the use count drops to 0 */
302 static void release_hook_chain( struct hook_table *table, int index )
304 if (!table->counts[index]) /* use count shouldn't already be 0 */
306 set_error( STATUS_INVALID_PARAMETER );
307 return;
309 if (!--table->counts[index])
311 struct hook *hook = get_first_hook( table, index );
312 while (hook)
314 struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
315 if (!hook->proc) free_hook( hook );
316 hook = next;
321 /* remove all global hooks owned by a given thread */
322 void remove_thread_hooks( struct thread *thread )
324 struct hook_table *global_hooks = get_global_hooks( thread );
325 int index;
327 if (!global_hooks) return;
329 /* only low-level keyboard/mouse global hooks can be owned by a thread */
330 for (index = WH_KEYBOARD_LL - WH_MINHOOK; index <= WH_MOUSE_LL - WH_MINHOOK; index++)
332 struct hook *hook = get_first_hook( global_hooks, index );
333 while (hook)
335 struct hook *next = HOOK_ENTRY( list_next( &global_hooks->hooks[index], &hook->chain ) );
336 if (hook->thread == thread) remove_hook( hook );
337 hook = next;
342 /* get a bitmap of active hooks in a hook table */
343 static int is_hook_active( struct hook_table *table, int index )
345 struct hook *hook = get_first_hook( table, index );
347 while (hook)
349 if (hook->proc && run_hook_in_current_thread( hook )) return 1;
350 hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
352 return 0;
355 /* get a bitmap of all active hooks for the current thread */
356 unsigned int get_active_hooks(void)
358 struct hook_table *table = get_queue_hooks( current );
359 struct hook_table *global_hooks = get_global_hooks( current );
360 unsigned int ret = 1 << 31; /* set high bit to indicate that the bitmap is valid */
361 int id;
363 for (id = WH_MINHOOK; id <= WH_WINEVENT; id++)
365 if ((table && is_hook_active( table, id - WH_MINHOOK )) ||
366 (global_hooks && is_hook_active( global_hooks, id - WH_MINHOOK )))
367 ret |= 1 << (id - WH_MINHOOK);
369 return ret;
372 /* return the thread that owns the first global hook */
373 struct thread *get_first_global_hook( int id )
375 struct hook *hook;
376 struct hook_table *global_hooks = get_global_hooks( current );
378 if (!global_hooks) return NULL;
379 if (!(hook = get_first_valid_hook( global_hooks, id - WH_MINHOOK, EVENT_MIN, 0, 0, 0 ))) return NULL;
380 return hook->owner;
383 /* set a window hook */
384 DECL_HANDLER(set_hook)
386 struct process *process = NULL;
387 struct thread *thread = NULL;
388 struct desktop *desktop;
389 struct hook *hook;
390 WCHAR *module;
391 int global;
392 data_size_t module_size = get_req_data_size();
394 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
396 set_error( STATUS_INVALID_PARAMETER );
397 return;
400 if (!(desktop = get_thread_desktop( current, DESKTOP_HOOKCONTROL ))) return;
402 if (req->pid && !(process = get_process_from_id( req->pid ))) goto done;
404 if (req->tid)
406 if (!(thread = get_thread_from_id( req->tid ))) goto done;
407 if (process && process != thread->process)
409 set_error( STATUS_INVALID_PARAMETER );
410 goto done;
414 if (req->id == WH_KEYBOARD_LL || req->id == WH_MOUSE_LL)
416 /* low-level hardware hooks are special: always global, but without a module */
417 if (thread)
419 set_error( STATUS_INVALID_PARAMETER );
420 goto done;
422 module = NULL;
423 global = 1;
425 else if (!req->tid)
427 /* out of context hooks do not need a module handle */
428 if (!module_size && (req->flags & WINEVENT_INCONTEXT))
430 set_error( STATUS_INVALID_PARAMETER );
431 goto done;
433 if (!(module = memdup( get_req_data(), module_size ))) goto done;
434 global = 1;
436 else
438 /* module is optional only if hook is in current process */
439 if (!module_size)
441 module = NULL;
442 if (thread->process != current->process)
444 set_error( STATUS_INVALID_PARAMETER );
445 goto done;
448 else if (!(module = memdup( get_req_data(), module_size ))) goto done;
449 global = 0;
452 if ((hook = add_hook( desktop, thread, req->id - WH_MINHOOK, global )))
454 hook->owner = (struct thread *)grab_object( current );
455 hook->process = process ? (struct process *)grab_object( process ) : NULL;
456 hook->event_min = req->event_min;
457 hook->event_max = req->event_max;
458 hook->flags = req->flags;
459 hook->proc = req->proc;
460 hook->unicode = req->unicode;
461 hook->module = module;
462 hook->module_size = module_size;
463 reply->handle = hook->handle;
464 reply->active_hooks = get_active_hooks();
466 else free( module );
468 done:
469 if (process) release_object( process );
470 if (thread) release_object( thread );
471 release_object( desktop );
475 /* remove a window hook */
476 DECL_HANDLER(remove_hook)
478 struct hook *hook;
480 if (req->handle)
482 if (!(hook = get_user_object( req->handle, USER_HOOK )))
484 set_error( STATUS_INVALID_HANDLE );
485 return;
488 else
490 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
492 set_error( STATUS_INVALID_PARAMETER );
493 return;
495 if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
497 set_error( STATUS_INVALID_PARAMETER );
498 return;
501 remove_hook( hook );
502 reply->active_hooks = get_active_hooks();
506 /* start calling a hook chain */
507 DECL_HANDLER(start_hook_chain)
509 struct hook *hook;
510 struct hook_table *table = get_queue_hooks( current );
511 struct hook_table *global_table = get_global_hooks( current );
513 if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
515 set_error( STATUS_INVALID_PARAMETER );
516 return;
519 reply->active_hooks = get_active_hooks();
521 if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK, req->event,
522 req->window, req->object_id, req->child_id )))
524 /* try global table */
525 if (!global_table || !(hook = get_first_valid_hook( global_table, req->id - WH_MINHOOK, req->event,
526 req->window, req->object_id, req->child_id )))
527 return; /* no hook set */
530 if (run_hook_in_owner_thread( hook ))
532 reply->pid = get_process_id( hook->owner->process );
533 reply->tid = get_thread_id( hook->owner );
535 else
537 reply->pid = 0;
538 reply->tid = 0;
540 reply->proc = hook->proc;
541 reply->handle = hook->handle;
542 reply->unicode = hook->unicode;
543 if (table) table->counts[hook->index]++;
544 if (global_table) global_table->counts[hook->index]++;
545 if (hook->module) set_reply_data( hook->module, hook->module_size );
549 /* finished calling a hook chain */
550 DECL_HANDLER(finish_hook_chain)
552 struct hook_table *table = get_queue_hooks( current );
553 struct hook_table *global_hooks = get_global_hooks( current );
554 int index = req->id - WH_MINHOOK;
556 if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
558 set_error( STATUS_INVALID_PARAMETER );
559 return;
561 if (table) release_hook_chain( table, index );
562 if (global_hooks) release_hook_chain( global_hooks, index );
566 /* get the hook information */
567 DECL_HANDLER(get_hook_info)
569 struct hook *hook;
571 if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
572 if (hook->thread && (hook->thread != current))
574 set_error( STATUS_INVALID_HANDLE );
575 return;
577 if (req->get_next && !(hook = get_next_hook( current, hook, req->event, req->window,
578 req->object_id, req->child_id )))
579 return;
581 reply->handle = hook->handle;
582 reply->id = hook->index + WH_MINHOOK;
583 reply->unicode = hook->unicode;
584 if (hook->module) set_reply_data( hook->module, min(hook->module_size,get_reply_max_size()) );
585 if (run_hook_in_owner_thread( hook ))
587 reply->pid = get_process_id( hook->owner->process );
588 reply->tid = get_thread_id( hook->owner );
590 else
592 reply->pid = 0;
593 reply->tid = 0;
595 reply->proc = hook->proc;