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
23 #include "wine/port.h"
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 */
51 void *proc
; /* hook function */
52 int unicode
; /* is it a unicode hook? */
53 WCHAR
*module
; /* module name for global hooks */
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 )
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 */
80 no_get_fd
, /* get_fd */
81 hook_table_destroy
/* destroy */
85 static struct hook_table
*global_hooks
;
87 /* create a new hook table */
88 static struct hook_table
*alloc_hook_table(void)
90 struct hook_table
*table
;
93 if ((table
= alloc_object( &hook_table_ops
)))
95 for (i
= 0; i
< NB_HOOKS
; i
++)
97 list_init( &table
->hooks
[i
] );
104 /* create a new hook and add it to the specified table */
105 static struct hook
*add_hook( struct thread
*thread
, int index
, int global
)
108 struct hook_table
*table
= global
? global_hooks
: get_queue_hooks(thread
);
112 if (!(table
= alloc_hook_table())) return NULL
;
113 if (global
) global_hooks
= table
;
114 else set_queue_hooks( thread
, table
);
116 if (!(hook
= mem_alloc( sizeof(*hook
) ))) return NULL
;
118 if (!(hook
->handle
= alloc_user_handle( hook
, USER_HOOK
)))
123 hook
->thread
= thread
? (struct thread
*)grab_object( thread
) : NULL
;
125 list_add_head( &table
->hooks
[index
], &hook
->chain
);
129 /* free a hook, removing it from its chain */
130 static void free_hook( struct hook
*hook
)
132 free_user_handle( hook
->handle
);
133 if (hook
->module
) free( hook
->module
);
134 if (hook
->thread
) release_object( hook
->thread
);
135 if (hook
->process
) release_object( hook
->process
);
136 release_object( hook
->owner
);
137 list_remove( &hook
->chain
);
141 /* find a hook from its index and proc */
142 static struct hook
*find_hook( struct thread
*thread
, int index
, void *proc
)
145 struct hook_table
*table
= get_queue_hooks( thread
);
149 LIST_FOR_EACH( p
, &table
->hooks
[index
] )
151 struct hook
*hook
= HOOK_ENTRY( p
);
152 if (hook
->proc
== proc
) return hook
;
158 /* get the hook table that a given hook belongs to */
159 inline static struct hook_table
*get_table( struct hook
*hook
)
161 if (!hook
->thread
) return global_hooks
;
162 if (hook
->index
+ WH_MINHOOK
== WH_KEYBOARD_LL
) return global_hooks
;
163 if (hook
->index
+ WH_MINHOOK
== WH_MOUSE_LL
) return global_hooks
;
164 return get_queue_hooks(hook
->thread
);
167 /* get the first hook in the chain */
168 inline static struct hook
*get_first_hook( struct hook_table
*table
, int index
)
170 struct list
*elem
= list_head( &table
->hooks
[index
] );
171 return elem
? HOOK_ENTRY( elem
) : NULL
;
174 /* find the first non-deleted hook in the chain */
175 inline static struct hook
*get_first_valid_hook( struct hook_table
*table
, int index
,
176 int event
, user_handle_t win
,
177 int object_id
, int child_id
)
179 struct hook
*hook
= get_first_hook( table
, index
);
183 if ((!hook
->process
|| hook
->process
== current
->process
) &&
184 (!(hook
->flags
& WINEVENT_SKIPOWNPROCESS
) || hook
->process
!= current
->process
))
186 if ((!hook
->thread
|| hook
->thread
== current
) &&
187 (!(hook
->flags
& WINEVENT_SKIPOWNTHREAD
) || hook
->thread
!= current
))
189 if (hook
->proc
&& event
>= hook
->event_min
&& event
<= hook
->event_max
)
191 if (hook
->flags
& WINEVENT_INCONTEXT
) return hook
;
193 /* only winevent hooks may be out of context */
194 assert(hook
->index
+ WH_MINHOOK
== WH_WINEVENT
);
195 post_win_event( hook
->owner
, event
, win
, object_id
, child_id
,
196 hook
->proc
, hook
->module
, hook
->module_size
,
201 hook
= HOOK_ENTRY( list_next( &table
->hooks
[index
], &hook
->chain
) );
206 /* find the next hook in the chain, skipping the deleted ones */
207 static struct hook
*get_next_hook( struct hook
*hook
, int event
, user_handle_t win
,
208 int object_id
, int child_id
)
210 struct hook_table
*table
= get_table( hook
);
211 int index
= hook
->index
;
213 while ((hook
= HOOK_ENTRY( list_next( &table
->hooks
[index
], &hook
->chain
) )))
215 if ((!hook
->process
|| hook
->process
== current
->process
) &&
216 (!(hook
->flags
& WINEVENT_SKIPOWNPROCESS
) || hook
->process
!= current
->process
))
218 if ((!hook
->thread
|| hook
->thread
== current
) &&
219 (!(hook
->flags
& WINEVENT_SKIPOWNTHREAD
) || hook
->thread
!= current
))
221 if (hook
->proc
&& event
>= hook
->event_min
&& event
<= hook
->event_max
)
223 if (hook
->flags
& WINEVENT_INCONTEXT
) return hook
;
225 /* only winevent hooks may be out of context */
226 assert(hook
->index
+ WH_MINHOOK
== WH_WINEVENT
);
227 post_win_event( hook
->owner
, event
, win
, object_id
, child_id
,
228 hook
->proc
, hook
->module
, hook
->module_size
,
234 if (global_hooks
&& table
!= global_hooks
) /* now search through the global table */
236 hook
= get_first_valid_hook( global_hooks
, index
, event
, win
, object_id
, child_id
);
241 static void hook_table_dump( struct object
*obj
, int verbose
)
243 struct hook_table
*table
= (struct hook_table
*)obj
;
244 if (table
== global_hooks
) fprintf( stderr
, "Global hook table\n" );
245 else fprintf( stderr
, "Hook table\n" );
248 static void hook_table_destroy( struct object
*obj
)
252 struct hook_table
*table
= (struct hook_table
*)obj
;
254 for (i
= 0; i
< NB_HOOKS
; i
++)
256 while ((hook
= get_first_hook( table
, i
)) != NULL
) free_hook( hook
);
260 /* free the global hooks table */
261 void close_global_hooks(void)
263 if (global_hooks
) release_object( global_hooks
);
266 /* remove a hook, freeing it if the chain is not in use */
267 static void remove_hook( struct hook
*hook
)
269 struct hook_table
*table
= get_table( hook
);
270 if (table
->counts
[hook
->index
])
271 hook
->proc
= NULL
; /* chain is in use, just mark it and return */
276 /* release a hook chain, removing deleted hooks if the use count drops to 0 */
277 static void release_hook_chain( struct hook_table
*table
, int index
)
279 if (!table
->counts
[index
]) /* use count shouldn't already be 0 */
281 set_error( STATUS_INVALID_PARAMETER
);
284 if (!--table
->counts
[index
])
286 struct hook
*hook
= get_first_hook( table
, index
);
289 struct hook
*next
= HOOK_ENTRY( list_next( &table
->hooks
[hook
->index
], &hook
->chain
) );
290 if (!hook
->proc
) free_hook( hook
);
296 /* remove all global hooks owned by a given thread */
297 void remove_thread_hooks( struct thread
*thread
)
301 if (!global_hooks
) return;
303 /* only low-level keyboard/mouse global hooks can be owned by a thread */
304 for (index
= WH_KEYBOARD_LL
- WH_MINHOOK
; index
<= WH_MOUSE_LL
- WH_MINHOOK
; index
++)
306 struct hook
*hook
= get_first_hook( global_hooks
, index
);
309 struct hook
*next
= HOOK_ENTRY( list_next( &global_hooks
->hooks
[index
], &hook
->chain
) );
310 if (hook
->thread
== thread
) remove_hook( hook
);
316 /* set a window hook */
317 DECL_HANDLER(set_hook
)
319 struct process
*process
= NULL
;
320 struct thread
*thread
= NULL
;
324 size_t module_size
= get_req_data_size();
326 if (!req
->proc
|| req
->id
< WH_MINHOOK
|| req
->id
> WH_WINEVENT
)
328 set_error( STATUS_INVALID_PARAMETER
);
332 if (req
->pid
&& !(process
= get_process_from_id( req
->pid
))) return;
336 if (!(thread
= get_thread_from_id( req
->tid
)))
338 if (process
) release_object( process
);
341 if (process
&& process
!= thread
->process
)
343 release_object( process
);
344 release_object( thread
);
345 set_error( STATUS_INVALID_PARAMETER
);
350 if (req
->id
== WH_KEYBOARD_LL
|| req
->id
== WH_MOUSE_LL
)
352 /* low-level hardware hooks are special: always global, but without a module */
353 thread
= (struct thread
*)grab_object( current
);
359 /* out of context hooks do not need a module handle */
360 if (!module_size
&& (req
->flags
& WINEVENT_INCONTEXT
))
362 if (process
) release_object( process
);
363 if (thread
) release_object( thread
);
364 set_error( STATUS_INVALID_PARAMETER
);
368 if (!(module
= memdup( get_req_data(), module_size
))) return;
378 if ((hook
= add_hook( thread
, req
->id
- WH_MINHOOK
, global
)))
380 hook
->owner
= (struct thread
*)grab_object( current
);
381 hook
->process
= process
? (struct process
*)grab_object( process
) : NULL
;
382 hook
->event_min
= req
->event_min
;
383 hook
->event_max
= req
->event_max
;
384 hook
->flags
= req
->flags
;
385 hook
->proc
= req
->proc
;
386 hook
->unicode
= req
->unicode
;
387 hook
->module
= module
;
388 hook
->module_size
= module_size
;
389 reply
->handle
= hook
->handle
;
391 else if (module
) free( module
);
393 if (process
) release_object( process
);
394 if (thread
) release_object( thread
);
398 /* remove a window hook */
399 DECL_HANDLER(remove_hook
)
405 if (!(hook
= get_user_object( req
->handle
, USER_HOOK
)))
407 set_error( STATUS_INVALID_HANDLE
);
413 if (!req
->proc
|| req
->id
< WH_MINHOOK
|| req
->id
> WH_WINEVENT
)
415 set_error( STATUS_INVALID_PARAMETER
);
418 if (!(hook
= find_hook( current
, req
->id
- WH_MINHOOK
, req
->proc
)))
420 set_error( STATUS_INVALID_PARAMETER
);
428 /* start calling a hook chain */
429 DECL_HANDLER(start_hook_chain
)
432 struct hook_table
*table
= get_queue_hooks( current
);
434 if (req
->id
< WH_MINHOOK
|| req
->id
> WH_WINEVENT
)
436 set_error( STATUS_INVALID_PARAMETER
);
440 if (!table
|| !(hook
= get_first_valid_hook( table
, req
->id
- WH_MINHOOK
, req
->event
, req
->window
, req
->object_id
, req
->child_id
)))
442 /* try global table */
443 if (!(table
= global_hooks
) ||
444 !(hook
= get_first_valid_hook( global_hooks
, req
->id
- WH_MINHOOK
, req
->event
, req
->window
, req
->object_id
, req
->child_id
)))
445 return; /* no hook set */
448 if (hook
->thread
&& hook
->thread
!= current
) /* must run in other thread */
450 reply
->pid
= get_process_id( hook
->thread
->process
);
451 reply
->tid
= get_thread_id( hook
->thread
);
458 reply
->proc
= hook
->proc
;
459 reply
->handle
= hook
->handle
;
460 reply
->unicode
= hook
->unicode
;
461 table
->counts
[hook
->index
]++;
462 if (hook
->module
) set_reply_data( hook
->module
, hook
->module_size
);
466 /* finished calling a hook chain */
467 DECL_HANDLER(finish_hook_chain
)
469 struct hook_table
*table
= get_queue_hooks( current
);
470 int index
= req
->id
- WH_MINHOOK
;
472 if (req
->id
< WH_MINHOOK
|| req
->id
> WH_WINEVENT
)
474 set_error( STATUS_INVALID_PARAMETER
);
477 if (table
) release_hook_chain( table
, index
);
478 if (global_hooks
) release_hook_chain( global_hooks
, index
);
482 /* get the next hook to call */
483 DECL_HANDLER(get_next_hook
)
485 struct hook
*hook
, *next
;
487 if (!(hook
= get_user_object( req
->handle
, USER_HOOK
))) return;
488 if (hook
->thread
&& (hook
->thread
!= current
))
490 set_error( STATUS_INVALID_HANDLE
);
493 if ((next
= get_next_hook( hook
, req
->event
, req
->window
, req
->object_id
, req
->child_id
)))
495 reply
->next
= next
->handle
;
496 reply
->id
= next
->index
+ WH_MINHOOK
;
497 reply
->prev_unicode
= hook
->unicode
;
498 reply
->next_unicode
= next
->unicode
;
499 if (next
->module
) set_reply_data( next
->module
, next
->module_size
);
500 if (next
->thread
&& next
->thread
!= current
)
502 reply
->pid
= get_process_id( next
->thread
->process
);
503 reply
->tid
= get_thread_id( next
->thread
);
510 reply
->proc
= next
->proc
;