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
23 #include "wine/port.h"
30 #define WIN32_NO_STATUS
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 */
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 )
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 */
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_kernel_obj_list
, /* get_kernel_obj_list */
95 no_close_handle
, /* close_handle */
96 hook_table_destroy
/* destroy */
100 /* create a new hook table */
101 static struct hook_table
*alloc_hook_table(void)
103 struct hook_table
*table
;
106 if ((table
= alloc_object( &hook_table_ops
)))
108 for (i
= 0; i
< NB_HOOKS
; i
++)
110 list_init( &table
->hooks
[i
] );
111 table
->counts
[i
] = 0;
117 static struct hook_table
*get_global_hooks( struct thread
*thread
)
119 struct hook_table
*table
;
120 struct desktop
*desktop
;
122 if (!thread
->desktop
) return NULL
;
123 if (!(desktop
= get_thread_desktop( thread
, 0 ))) return NULL
;
124 table
= desktop
->global_hooks
;
125 release_object( desktop
);
129 /* create a new hook and add it to the specified table */
130 static struct hook
*add_hook( struct desktop
*desktop
, struct thread
*thread
, int index
, int global
)
133 struct hook_table
*table
= global
? desktop
->global_hooks
: get_queue_hooks(thread
);
137 if (!(table
= alloc_hook_table())) return NULL
;
138 if (global
) desktop
->global_hooks
= table
;
139 else set_queue_hooks( thread
, table
);
141 if (!(hook
= mem_alloc( sizeof(*hook
) ))) return NULL
;
143 if (!(hook
->handle
= alloc_user_handle( hook
, USER_HOOK
)))
148 hook
->thread
= thread
? (struct thread
*)grab_object( thread
) : NULL
;
151 list_add_head( &table
->hooks
[index
], &hook
->chain
);
152 if (thread
) thread
->desktop_users
++;
156 /* free a hook, removing it from its chain */
157 static void free_hook( struct hook
*hook
)
159 free_user_handle( hook
->handle
);
160 free( hook
->module
);
163 assert( hook
->thread
->desktop_users
> 0 );
164 hook
->thread
->desktop_users
--;
165 release_object( hook
->thread
);
167 if (hook
->process
) release_object( hook
->process
);
168 release_object( hook
->owner
);
169 list_remove( &hook
->chain
);
173 /* find a hook from its index and proc */
174 static struct hook
*find_hook( struct thread
*thread
, int index
, client_ptr_t proc
)
177 struct hook_table
*table
= get_queue_hooks( thread
);
181 LIST_FOR_EACH( p
, &table
->hooks
[index
] )
183 struct hook
*hook
= HOOK_ENTRY( p
);
184 if (hook
->proc
== proc
) return hook
;
190 /* get the first hook in the chain */
191 static inline struct hook
*get_first_hook( struct hook_table
*table
, int index
)
193 struct list
*elem
= list_head( &table
->hooks
[index
] );
194 return elem
? HOOK_ENTRY( elem
) : NULL
;
197 /* check if a given hook should run in the owner thread instead of the current thread */
198 static inline int run_hook_in_owner_thread( struct hook
*hook
)
200 if ((hook
->index
== WH_MOUSE_LL
- WH_MINHOOK
||
201 hook
->index
== WH_KEYBOARD_LL
- WH_MINHOOK
))
202 return hook
->owner
!= current
;
206 /* check if a given hook should run in the current thread */
207 static inline int run_hook_in_current_thread( struct hook
*hook
)
209 if (hook
->process
&& hook
->process
!= current
->process
) return 0;
210 if ((hook
->flags
& WINEVENT_SKIPOWNPROCESS
) && hook
->process
== current
->process
) return 0;
211 if (hook
->thread
&& hook
->thread
!= current
) return 0;
212 if ((hook
->flags
& WINEVENT_SKIPOWNTHREAD
) && hook
->thread
== current
) return 0;
213 /* don't run low-level hooks in processes suspended for debugging */
214 if (run_hook_in_owner_thread( hook
) && hook
->owner
->process
->suspend
) return 0;
218 /* find the first non-deleted hook in the chain */
219 static inline struct hook
*get_first_valid_hook( struct hook_table
*table
, int index
,
220 int event
, user_handle_t win
,
221 int object_id
, int child_id
)
223 struct hook
*hook
= get_first_hook( table
, index
);
227 if (hook
->proc
&& run_hook_in_current_thread( hook
))
229 if (event
>= hook
->event_min
&& event
<= hook
->event_max
)
231 if (hook
->flags
& WINEVENT_INCONTEXT
) return hook
;
233 /* only winevent hooks may be out of context */
234 assert(hook
->index
+ WH_MINHOOK
== WH_WINEVENT
);
235 post_win_event( hook
->owner
, event
, win
, object_id
, child_id
,
236 hook
->proc
, hook
->module
, hook
->module_size
,
240 hook
= HOOK_ENTRY( list_next( &table
->hooks
[index
], &hook
->chain
) );
245 /* find the next hook in the chain, skipping the deleted ones */
246 static struct hook
*get_next_hook( struct thread
*thread
, struct hook
*hook
, int event
,
247 user_handle_t win
, int object_id
, int child_id
)
249 struct hook_table
*global_hooks
, *table
= hook
->table
;
250 int index
= hook
->index
;
252 while ((hook
= HOOK_ENTRY( list_next( &table
->hooks
[index
], &hook
->chain
) )))
254 if (hook
->proc
&& run_hook_in_current_thread( hook
))
256 if (event
>= hook
->event_min
&& event
<= hook
->event_max
)
258 if (hook
->flags
& WINEVENT_INCONTEXT
) return hook
;
260 /* only winevent hooks may be out of context */
261 assert(hook
->index
+ WH_MINHOOK
== WH_WINEVENT
);
262 post_win_event( hook
->owner
, event
, win
, object_id
, child_id
,
263 hook
->proc
, hook
->module
, hook
->module_size
,
268 global_hooks
= get_global_hooks( thread
);
269 if (global_hooks
&& table
!= global_hooks
) /* now search through the global table */
271 hook
= get_first_valid_hook( global_hooks
, index
, event
, win
, object_id
, child_id
);
276 static void hook_table_dump( struct object
*obj
, int verbose
)
278 /* struct hook_table *table = (struct hook_table *)obj; */
279 fprintf( stderr
, "Hook table\n" );
282 static void hook_table_destroy( struct object
*obj
)
286 struct hook_table
*table
= (struct hook_table
*)obj
;
288 for (i
= 0; i
< NB_HOOKS
; i
++)
290 while ((hook
= get_first_hook( table
, i
)) != NULL
) free_hook( hook
);
294 /* remove a hook, freeing it if the chain is not in use */
295 static void remove_hook( struct hook
*hook
)
297 if (hook
->table
->counts
[hook
->index
])
298 hook
->proc
= 0; /* chain is in use, just mark it and return */
303 /* release a hook chain, removing deleted hooks if the use count drops to 0 */
304 static void release_hook_chain( struct hook_table
*table
, int index
)
306 if (!table
->counts
[index
]) /* use count shouldn't already be 0 */
308 set_error( STATUS_INVALID_PARAMETER
);
311 if (!--table
->counts
[index
])
313 struct hook
*hook
= get_first_hook( table
, index
);
316 struct hook
*next
= HOOK_ENTRY( list_next( &table
->hooks
[hook
->index
], &hook
->chain
) );
317 if (!hook
->proc
) free_hook( hook
);
323 /* remove all global hooks owned by a given thread */
324 void remove_thread_hooks( struct thread
*thread
)
326 struct hook_table
*global_hooks
= get_global_hooks( thread
);
329 if (!global_hooks
) return;
331 /* only low-level keyboard/mouse global hooks can be owned by a thread */
332 for (index
= WH_KEYBOARD_LL
- WH_MINHOOK
; index
<= WH_MOUSE_LL
- WH_MINHOOK
; index
++)
334 struct hook
*hook
= get_first_hook( global_hooks
, index
);
337 struct hook
*next
= HOOK_ENTRY( list_next( &global_hooks
->hooks
[index
], &hook
->chain
) );
338 if (hook
->thread
== thread
) remove_hook( hook
);
344 /* get a bitmap of active hooks in a hook table */
345 static int is_hook_active( struct hook_table
*table
, int index
)
347 struct hook
*hook
= get_first_hook( table
, index
);
351 if (hook
->proc
&& run_hook_in_current_thread( hook
)) return 1;
352 hook
= HOOK_ENTRY( list_next( &table
->hooks
[index
], &hook
->chain
) );
357 /* get a bitmap of all active hooks for the current thread */
358 unsigned int get_active_hooks(void)
360 struct hook_table
*table
= get_queue_hooks( current
);
361 struct hook_table
*global_hooks
= get_global_hooks( current
);
362 unsigned int ret
= 1u << 31; /* set high bit to indicate that the bitmap is valid */
365 for (id
= WH_MINHOOK
; id
<= WH_WINEVENT
; id
++)
367 if ((table
&& is_hook_active( table
, id
- WH_MINHOOK
)) ||
368 (global_hooks
&& is_hook_active( global_hooks
, id
- WH_MINHOOK
)))
369 ret
|= 1 << (id
- WH_MINHOOK
);
374 /* return the thread that owns the first global hook */
375 struct thread
*get_first_global_hook( int id
)
378 struct hook_table
*global_hooks
= get_global_hooks( current
);
380 if (!global_hooks
) return NULL
;
381 if (!(hook
= get_first_valid_hook( global_hooks
, id
- WH_MINHOOK
, EVENT_MIN
, 0, 0, 0 ))) return NULL
;
385 /* set a window hook */
386 DECL_HANDLER(set_hook
)
388 struct process
*process
= NULL
;
389 struct thread
*thread
= NULL
;
390 struct desktop
*desktop
;
394 data_size_t module_size
= get_req_data_size();
396 if (!req
->proc
|| req
->id
< WH_MINHOOK
|| req
->id
> WH_WINEVENT
)
398 set_error( STATUS_INVALID_PARAMETER
);
402 if (!(desktop
= get_thread_desktop( current
, DESKTOP_HOOKCONTROL
))) return;
404 if (req
->pid
&& !(process
= get_process_from_id( req
->pid
))) goto done
;
408 if (!(thread
= get_thread_from_id( req
->tid
))) goto done
;
409 if (process
&& process
!= thread
->process
)
411 set_error( STATUS_INVALID_PARAMETER
);
416 if (req
->id
== WH_KEYBOARD_LL
|| req
->id
== WH_MOUSE_LL
)
418 /* low-level hardware hooks are special: always global, but without a module */
421 set_error( STATUS_INVALID_PARAMETER
);
429 /* out of context hooks do not need a module handle */
430 if (!module_size
&& (req
->flags
& WINEVENT_INCONTEXT
))
432 set_error( STATUS_INVALID_PARAMETER
);
435 if (!(module
= memdup( get_req_data(), module_size
))) goto done
;
440 /* module is optional only if hook is in current process */
444 if (thread
->process
!= current
->process
)
446 set_error( STATUS_INVALID_PARAMETER
);
450 else if (!(module
= memdup( get_req_data(), module_size
))) goto done
;
454 if ((hook
= add_hook( desktop
, thread
, req
->id
- WH_MINHOOK
, global
)))
456 hook
->owner
= (struct thread
*)grab_object( current
);
457 hook
->process
= process
? (struct process
*)grab_object( process
) : NULL
;
458 hook
->event_min
= req
->event_min
;
459 hook
->event_max
= req
->event_max
;
460 hook
->flags
= req
->flags
;
461 hook
->proc
= req
->proc
;
462 hook
->unicode
= req
->unicode
;
463 hook
->module
= module
;
464 hook
->module_size
= module_size
;
465 reply
->handle
= hook
->handle
;
466 reply
->active_hooks
= get_active_hooks();
471 if (process
) release_object( process
);
472 if (thread
) release_object( thread
);
473 release_object( desktop
);
477 /* remove a window hook */
478 DECL_HANDLER(remove_hook
)
484 if (!(hook
= get_user_object( req
->handle
, USER_HOOK
)))
486 set_error( STATUS_INVALID_HANDLE
);
492 if (!req
->proc
|| req
->id
< WH_MINHOOK
|| req
->id
> WH_WINEVENT
)
494 set_error( STATUS_INVALID_PARAMETER
);
497 if (!(hook
= find_hook( current
, req
->id
- WH_MINHOOK
, req
->proc
)))
499 set_error( STATUS_INVALID_PARAMETER
);
504 reply
->active_hooks
= get_active_hooks();
508 /* start calling a hook chain */
509 DECL_HANDLER(start_hook_chain
)
512 struct hook_table
*table
= get_queue_hooks( current
);
513 struct hook_table
*global_table
= get_global_hooks( current
);
515 if (req
->id
< WH_MINHOOK
|| req
->id
> WH_WINEVENT
)
517 set_error( STATUS_INVALID_PARAMETER
);
521 reply
->active_hooks
= get_active_hooks();
523 if (!table
|| !(hook
= get_first_valid_hook( table
, req
->id
- WH_MINHOOK
, req
->event
,
524 req
->window
, req
->object_id
, req
->child_id
)))
526 /* try global table */
527 if (!global_table
|| !(hook
= get_first_valid_hook( global_table
, req
->id
- WH_MINHOOK
, req
->event
,
528 req
->window
, req
->object_id
, req
->child_id
)))
529 return; /* no hook set */
532 if (run_hook_in_owner_thread( hook
))
534 reply
->pid
= get_process_id( hook
->owner
->process
);
535 reply
->tid
= get_thread_id( hook
->owner
);
542 reply
->proc
= hook
->proc
;
543 reply
->handle
= hook
->handle
;
544 reply
->unicode
= hook
->unicode
;
545 if (table
) table
->counts
[hook
->index
]++;
546 if (global_table
) global_table
->counts
[hook
->index
]++;
547 if (hook
->module
) set_reply_data( hook
->module
, hook
->module_size
);
551 /* finished calling a hook chain */
552 DECL_HANDLER(finish_hook_chain
)
554 struct hook_table
*table
= get_queue_hooks( current
);
555 struct hook_table
*global_hooks
= get_global_hooks( current
);
556 int index
= req
->id
- WH_MINHOOK
;
558 if (req
->id
< WH_MINHOOK
|| req
->id
> WH_WINEVENT
)
560 set_error( STATUS_INVALID_PARAMETER
);
563 if (table
) release_hook_chain( table
, index
);
564 if (global_hooks
) release_hook_chain( global_hooks
, index
);
568 /* get the hook information */
569 DECL_HANDLER(get_hook_info
)
573 if (!(hook
= get_user_object( req
->handle
, USER_HOOK
))) return;
574 if (hook
->thread
&& (hook
->thread
!= current
))
576 set_error( STATUS_INVALID_HANDLE
);
579 if (req
->get_next
&& !(hook
= get_next_hook( current
, hook
, req
->event
, req
->window
,
580 req
->object_id
, req
->child_id
)))
583 reply
->handle
= hook
->handle
;
584 reply
->id
= hook
->index
+ WH_MINHOOK
;
585 reply
->unicode
= hook
->unicode
;
586 if (hook
->module
) set_reply_data( hook
->module
, min(hook
->module_size
,get_reply_max_size()) );
587 if (run_hook_in_owner_thread( hook
))
589 reply
->pid
= get_process_id( hook
->owner
->process
);
590 reply
->tid
= get_thread_id( hook
->owner
);
597 reply
->proc
= hook
->proc
;