Skip to content
Newer
Older
100644 596 lines (526 sloc) 18.3 KB
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
1 /*
2 * Server-side window hooks support
3 *
4 * Copyright (C) 2002 Alexandre Julliard
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
5 * Copyright (C) 2005 Dmitry Timoshkov
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
6 *
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.
11 *
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.
16 *
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
360a3f9 @jonathanernst Update the address of the Free Software Foundation.
jonathanernst authored
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
20 */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
e37c6e1 @julliard Fixed header dependencies to be fully compatible with the Windows
julliard authored
26 #include <stdarg.h>
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
27 #include <stdio.h>
28
1a1583a Match PSDK STATUS_* definitions.
Ge van Geldorp authored
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
e37c6e1 @julliard Fixed header dependencies to be fully compatible with the Windows
julliard authored
31 #include "windef.h"
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
32 #include "winbase.h"
33 #include "winuser.h"
1a1583a Match PSDK STATUS_* definitions.
Ge van Geldorp authored
34 #include "winternl.h"
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
35
36 #include "object.h"
ca3ac8f @julliard Run WH_KEYBOARD_LL and WH_MOUSE_LL hooks in the context of the thread
julliard authored
37 #include "process.h"
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
38 #include "request.h"
39 #include "user.h"
40
41 struct hook_table;
42
43 struct hook
44 {
45 struct list chain; /* hook chain entry */
46 user_handle_t handle; /* user handle for this hook */
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
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 */
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
50 struct hook_table *table; /* hook table that contains this hook */
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
51 int index; /* hook table index */
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
52 int event_min;
53 int event_max;
54 int flags;
cc55fd3 @julliard server: Make hook procedures client_ptr_t instead of void pointers.
julliard authored
55 client_ptr_t proc; /* hook function */
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
56 int unicode; /* is it a unicode hook? */
14e68ba @julliard Added support for system-wide hooks.
julliard authored
57 WCHAR *module; /* module name for global hooks */
0f273c1 @julliard server: Added data_size_t type to represent sizes in the server proto…
julliard authored
58 data_size_t module_size;
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
59 };
60
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
61 #define WH_WINEVENT (WH_MAXHOOK+1)
62
63 #define NB_HOOKS (WH_WINEVENT-WH_MINHOOK+1)
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
64 #define HOOK_ENTRY(p) LIST_ENTRY( (p), struct hook, chain )
65
66 struct hook_table
67 {
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 */
71 };
72
73 static void hook_table_dump( struct object *obj, int verbose );
74 static void hook_table_destroy( struct object *obj );
75
76 static const struct object_ops hook_table_ops =
77 {
78 sizeof(struct hook_table), /* size */
79 hook_table_dump, /* dump */
8382eb0 @julliard server: Return correct object types in the get_directory_entry request.
julliard authored
80 no_get_type, /* get_type */
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
81 no_add_queue, /* add_queue */
82 NULL, /* remove_queue */
83 NULL, /* signaled */
84 NULL, /* satisfied */
f92fff6 Implemented NtSignalAndWaitForSingleObject.
Mike McCormack authored
85 no_signal, /* signal */
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
86 no_get_fd, /* get_fd */
28beba3 @julliard server: Added infrastructure for access rights mapping.
julliard authored
87 no_map_access, /* map_access */
c1707d8 server: Add get_sd and set_sd object operations to allow the security…
Rob Shearman authored
88 default_get_sd, /* get_sd */
89 default_set_sd, /* set_sd */
baffcb9 Add name_lookup function in object_ops.
Vitaliy Margolen authored
90 no_lookup_name, /* lookup_name */
7e71c1d @julliard server: Add an open_file() function to the object operations.
julliard authored
91 no_open_file, /* open_file */
b9b1ea9 @julliard Added a close_handle method to the object operations, and use it to
julliard authored
92 no_close_handle, /* close_handle */
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
93 hook_table_destroy /* destroy */
94 };
95
96
97 /* create a new hook table */
98 static struct hook_table *alloc_hook_table(void)
99 {
100 struct hook_table *table;
101 int i;
102
e66207e @julliard Moved all references to file descriptors out of the generic object
julliard authored
103 if ((table = alloc_object( &hook_table_ops )))
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
104 {
105 for (i = 0; i < NB_HOOKS; i++)
106 {
107 list_init( &table->hooks[i] );
108 table->counts[i] = 0;
109 }
110 }
111 return table;
112 }
113
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
114 static struct hook_table *get_global_hooks( struct thread *thread )
115 {
116 struct hook_table *table;
e4faa12 @julliard server: Make sure that last error doesn't get set during thread cleanup.
julliard authored
117 struct desktop *desktop;
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
118
e4faa12 @julliard server: Make sure that last error doesn't get set during thread cleanup.
julliard authored
119 if (!thread->desktop) return NULL;
120 if (!(desktop = get_thread_desktop( thread, 0 ))) return NULL;
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
121 table = desktop->global_hooks;
122 release_object( desktop );
123 return table;
124 }
125
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
126 /* create a new hook and add it to the specified table */
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
127 static struct hook *add_hook( struct desktop *desktop, struct thread *thread, int index, int global )
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
128 {
129 struct hook *hook;
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
130 struct hook_table *table = global ? desktop->global_hooks : get_queue_hooks(thread);
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
131
132 if (!table)
133 {
134 if (!(table = alloc_hook_table())) return NULL;
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
135 if (global) desktop->global_hooks = table;
ca3ac8f @julliard Run WH_KEYBOARD_LL and WH_MOUSE_LL hooks in the context of the thread
julliard authored
136 else set_queue_hooks( thread, table );
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
137 }
138 if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
139
140 if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
141 {
142 free( hook );
143 return NULL;
144 }
145 hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
146 hook->table = table;
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
147 hook->index = index;
148 list_add_head( &table->hooks[index], &hook->chain );
92fec7b @julliard Keep track of the windows and hooks used by a thread to properly
julliard authored
149 if (thread) thread->desktop_users++;
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
150 return hook;
151 }
152
153 /* free a hook, removing it from its chain */
154 static void free_hook( struct hook *hook )
155 {
156 free_user_handle( hook->handle );
5ceccec server: Remove redundant NULL check before free() (found by Smatch).
Michael Stefaniuc authored
157 free( hook->module );
92fec7b @julliard Keep track of the windows and hooks used by a thread to properly
julliard authored
158 if (hook->thread)
159 {
160 assert( hook->thread->desktop_users > 0 );
161 hook->thread->desktop_users--;
162 release_object( hook->thread );
163 }
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
164 if (hook->process) release_object( hook->process );
165 release_object( hook->owner );
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
166 list_remove( &hook->chain );
167 free( hook );
168 }
169
170 /* find a hook from its index and proc */
cc55fd3 @julliard server: Make hook procedures client_ptr_t instead of void pointers.
julliard authored
171 static struct hook *find_hook( struct thread *thread, int index, client_ptr_t proc )
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
172 {
173 struct list *p;
d55e7f1 @julliard Store the hook table in the thread queue instead of the thread
julliard authored
174 struct hook_table *table = get_queue_hooks( thread );
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
175
176 if (table)
177 {
178 LIST_FOR_EACH( p, &table->hooks[index] )
179 {
180 struct hook *hook = HOOK_ENTRY( p );
181 if (hook->proc == proc) return hook;
182 }
183 }
184 return NULL;
185 }
186
187 /* get the first hook in the chain */
b1788c8 server: Replace inline static with static inline.
Andrew Talbot authored
188 static inline struct hook *get_first_hook( struct hook_table *table, int index )
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
189 {
190 struct list *elem = list_head( &table->hooks[index] );
191 return elem ? HOOK_ENTRY( elem ) : NULL;
192 }
193
6d85f3b @julliard server: Fixed low-level hardware hooks.
julliard authored
194 /* check if a given hook should run in the owner thread instead of the current thread */
b1788c8 server: Replace inline static with static inline.
Andrew Talbot authored
195 static inline int run_hook_in_owner_thread( struct hook *hook )
6d85f3b @julliard server: Fixed low-level hardware hooks.
julliard authored
196 {
197 if ((hook->index == WH_MOUSE_LL - WH_MINHOOK ||
198 hook->index == WH_KEYBOARD_LL - WH_MINHOOK))
199 return hook->owner != current;
200 return 0;
201 }
202
437a838 @julliard server: Don't run low-level hooks in debugged processes.
julliard authored
203 /* check if a given hook should run in the current thread */
204 static inline int run_hook_in_current_thread( struct hook *hook )
205 {
206 if (hook->process && hook->process != current->process) return 0;
207 if ((hook->flags & WINEVENT_SKIPOWNPROCESS) && hook->process == current->process) return 0;
208 if (hook->thread && hook->thread != current) return 0;
209 if ((hook->flags & WINEVENT_SKIPOWNTHREAD) && hook->thread == current) return 0;
457a847 @julliard server: Only avoid running low-level hooks in processes currently sus…
julliard authored
210 /* don't run low-level hooks in processes suspended for debugging */
211 if (run_hook_in_owner_thread( hook ) && hook->owner->process->suspend) return 0;
437a838 @julliard server: Don't run low-level hooks in debugged processes.
julliard authored
212 return 1;
213 }
214
14e68ba @julliard Added support for system-wide hooks.
julliard authored
215 /* find the first non-deleted hook in the chain */
b1788c8 server: Replace inline static with static inline.
Andrew Talbot authored
216 static inline struct hook *get_first_valid_hook( struct hook_table *table, int index,
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
217 int event, user_handle_t win,
218 int object_id, int child_id )
14e68ba @julliard Added support for system-wide hooks.
julliard authored
219 {
220 struct hook *hook = get_first_hook( table, index );
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
221
222 while (hook)
223 {
6334235 @julliard Store a bitmap of active hooks on the client side to try to avoid
julliard authored
224 if (hook->proc && run_hook_in_current_thread( hook ))
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
225 {
6334235 @julliard Store a bitmap of active hooks on the client side to try to avoid
julliard authored
226 if (event >= hook->event_min && event <= hook->event_max)
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
227 {
6334235 @julliard Store a bitmap of active hooks on the client side to try to avoid
julliard authored
228 if (hook->flags & WINEVENT_INCONTEXT) return hook;
229
230 /* only winevent hooks may be out of context */
231 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
232 post_win_event( hook->owner, event, win, object_id, child_id,
233 hook->proc, hook->module, hook->module_size,
234 hook->handle );
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
235 }
236 }
14e68ba @julliard Added support for system-wide hooks.
julliard authored
237 hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
238 }
14e68ba @julliard Added support for system-wide hooks.
julliard authored
239 return hook;
240 }
241
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
242 /* find the next hook in the chain, skipping the deleted ones */
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
243 static struct hook *get_next_hook( struct thread *thread, struct hook *hook, int event,
244 user_handle_t win, int object_id, int child_id )
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
245 {
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
246 struct hook_table *global_hooks, *table = hook->table;
14e68ba @julliard Added support for system-wide hooks.
julliard authored
247 int index = hook->index;
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
248
14e68ba @julliard Added support for system-wide hooks.
julliard authored
249 while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
250 {
6334235 @julliard Store a bitmap of active hooks on the client side to try to avoid
julliard authored
251 if (hook->proc && run_hook_in_current_thread( hook ))
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
252 {
6334235 @julliard Store a bitmap of active hooks on the client side to try to avoid
julliard authored
253 if (event >= hook->event_min && event <= hook->event_max)
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
254 {
6334235 @julliard Store a bitmap of active hooks on the client side to try to avoid
julliard authored
255 if (hook->flags & WINEVENT_INCONTEXT) return hook;
256
257 /* only winevent hooks may be out of context */
258 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
259 post_win_event( hook->owner, event, win, object_id, child_id,
260 hook->proc, hook->module, hook->module_size,
261 hook->handle );
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
262 }
263 }
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
264 }
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
265 global_hooks = get_global_hooks( thread );
14e68ba @julliard Added support for system-wide hooks.
julliard authored
266 if (global_hooks && table != global_hooks) /* now search through the global table */
267 {
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
268 hook = get_first_valid_hook( global_hooks, index, event, win, object_id, child_id );
14e68ba @julliard Added support for system-wide hooks.
julliard authored
269 }
270 return hook;
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
271 }
272
273 static void hook_table_dump( struct object *obj, int verbose )
274 {
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
275 /* struct hook_table *table = (struct hook_table *)obj; */
276 fprintf( stderr, "Hook table\n" );
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
277 }
278
279 static void hook_table_destroy( struct object *obj )
280 {
281 int i;
282 struct hook *hook;
283 struct hook_table *table = (struct hook_table *)obj;
284
285 for (i = 0; i < NB_HOOKS; i++)
286 {
287 while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
288 }
289 }
290
291 /* remove a hook, freeing it if the chain is not in use */
292 static void remove_hook( struct hook *hook )
293 {
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
294 if (hook->table->counts[hook->index])
cc55fd3 @julliard server: Make hook procedures client_ptr_t instead of void pointers.
julliard authored
295 hook->proc = 0; /* chain is in use, just mark it and return */
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
296 else
297 free_hook( hook );
298 }
299
300 /* release a hook chain, removing deleted hooks if the use count drops to 0 */
301 static void release_hook_chain( struct hook_table *table, int index )
302 {
303 if (!table->counts[index]) /* use count shouldn't already be 0 */
304 {
305 set_error( STATUS_INVALID_PARAMETER );
306 return;
307 }
308 if (!--table->counts[index])
309 {
310 struct hook *hook = get_first_hook( table, index );
311 while (hook)
312 {
313 struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
314 if (!hook->proc) free_hook( hook );
315 hook = next;
316 }
317 }
318 }
319
ca3ac8f @julliard Run WH_KEYBOARD_LL and WH_MOUSE_LL hooks in the context of the thread
julliard authored
320 /* remove all global hooks owned by a given thread */
321 void remove_thread_hooks( struct thread *thread )
322 {
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
323 struct hook_table *global_hooks = get_global_hooks( thread );
ca3ac8f @julliard Run WH_KEYBOARD_LL and WH_MOUSE_LL hooks in the context of the thread
julliard authored
324 int index;
325
326 if (!global_hooks) return;
327
328 /* only low-level keyboard/mouse global hooks can be owned by a thread */
329 for (index = WH_KEYBOARD_LL - WH_MINHOOK; index <= WH_MOUSE_LL - WH_MINHOOK; index++)
330 {
331 struct hook *hook = get_first_hook( global_hooks, index );
332 while (hook)
333 {
334 struct hook *next = HOOK_ENTRY( list_next( &global_hooks->hooks[index], &hook->chain ) );
335 if (hook->thread == thread) remove_hook( hook );
336 hook = next;
337 }
338 }
339 }
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
340
6334235 @julliard Store a bitmap of active hooks on the client side to try to avoid
julliard authored
341 /* get a bitmap of active hooks in a hook table */
342 static int is_hook_active( struct hook_table *table, int index )
343 {
344 struct hook *hook = get_first_hook( table, index );
345
346 while (hook)
347 {
348 if (hook->proc && run_hook_in_current_thread( hook )) return 1;
349 hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
350 }
351 return 0;
352 }
353
354 /* get a bitmap of all active hooks for the current thread */
355 unsigned int get_active_hooks(void)
356 {
357 struct hook_table *table = get_queue_hooks( current );
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
358 struct hook_table *global_hooks = get_global_hooks( current );
6334235 @julliard Store a bitmap of active hooks on the client side to try to avoid
julliard authored
359 unsigned int ret = 1 << 31; /* set high bit to indicate that the bitmap is valid */
360 int id;
361
362 for (id = WH_MINHOOK; id <= WH_WINEVENT; id++)
363 {
364 if ((table && is_hook_active( table, id - WH_MINHOOK )) ||
365 (global_hooks && is_hook_active( global_hooks, id - WH_MINHOOK )))
366 ret |= 1 << (id - WH_MINHOOK);
367 }
368 return ret;
369 }
370
c7efa29 @julliard server: Invoke low-level hardware hooks directly from the server side.
julliard authored
371 /* return the thread that owns the first global hook */
372 struct thread *get_first_global_hook( int id )
373 {
374 struct hook *hook;
375 struct hook_table *global_hooks = get_global_hooks( current );
376
377 if (!global_hooks) return NULL;
378 if (!(hook = get_first_valid_hook( global_hooks, id - WH_MINHOOK, EVENT_MIN, 0, 0, 0 ))) return NULL;
379 return hook->owner;
380 }
381
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
382 /* set a window hook */
383 DECL_HANDLER(set_hook)
384 {
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
385 struct process *process = NULL;
386 struct thread *thread = NULL;
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
387 struct desktop *desktop;
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
388 struct hook *hook;
14e68ba @julliard Added support for system-wide hooks.
julliard authored
389 WCHAR *module;
ca3ac8f @julliard Run WH_KEYBOARD_LL and WH_MOUSE_LL hooks in the context of the thread
julliard authored
390 int global;
0f273c1 @julliard server: Added data_size_t type to represent sizes in the server proto…
julliard authored
391 data_size_t module_size = get_req_data_size();
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
392
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
393 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
394 {
395 set_error( STATUS_INVALID_PARAMETER );
396 return;
397 }
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
398
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
399 if (!(desktop = get_thread_desktop( current, DESKTOP_HOOKCONTROL ))) return;
400
401 if (req->pid && !(process = get_process_from_id( req->pid ))) goto done;
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
402
403 if (req->tid)
404 {
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
405 if (!(thread = get_thread_from_id( req->tid ))) goto done;
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
406 if (process && process != thread->process)
407 {
408 set_error( STATUS_INVALID_PARAMETER );
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
409 goto done;
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
410 }
411 }
412
ca3ac8f @julliard Run WH_KEYBOARD_LL and WH_MOUSE_LL hooks in the context of the thread
julliard authored
413 if (req->id == WH_KEYBOARD_LL || req->id == WH_MOUSE_LL)
414 {
415 /* low-level hardware hooks are special: always global, but without a module */
6d85f3b @julliard server: Fixed low-level hardware hooks.
julliard authored
416 if (thread)
417 {
418 set_error( STATUS_INVALID_PARAMETER );
419 goto done;
420 }
ca3ac8f @julliard Run WH_KEYBOARD_LL and WH_MOUSE_LL hooks in the context of the thread
julliard authored
421 module = NULL;
422 global = 1;
423 }
424 else if (!req->tid)
14e68ba @julliard Added support for system-wide hooks.
julliard authored
425 {
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
426 /* out of context hooks do not need a module handle */
427 if (!module_size && (req->flags & WINEVENT_INCONTEXT))
14e68ba @julliard Added support for system-wide hooks.
julliard authored
428 {
429 set_error( STATUS_INVALID_PARAMETER );
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
430 goto done;
14e68ba @julliard Added support for system-wide hooks.
julliard authored
431 }
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
432 if (!(module = memdup( get_req_data(), module_size ))) goto done;
ca3ac8f @julliard Run WH_KEYBOARD_LL and WH_MOUSE_LL hooks in the context of the thread
julliard authored
433 global = 1;
14e68ba @julliard Added support for system-wide hooks.
julliard authored
434 }
435 else
436 {
04a9f93 @julliard user32: Thread-local hooks need a module if they don't belong to the …
julliard authored
437 /* module is optional only if hook is in current process */
438 if (!module_size)
439 {
440 module = NULL;
441 if (thread->process != current->process)
442 {
443 set_error( STATUS_INVALID_PARAMETER );
444 goto done;
445 }
446 }
447 else if (!(module = memdup( get_req_data(), module_size ))) goto done;
ca3ac8f @julliard Run WH_KEYBOARD_LL and WH_MOUSE_LL hooks in the context of the thread
julliard authored
448 global = 0;
14e68ba @julliard Added support for system-wide hooks.
julliard authored
449 }
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
450
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
451 if ((hook = add_hook( desktop, thread, req->id - WH_MINHOOK, global )))
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
452 {
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
453 hook->owner = (struct thread *)grab_object( current );
454 hook->process = process ? (struct process *)grab_object( process ) : NULL;
455 hook->event_min = req->event_min;
456 hook->event_max = req->event_max;
457 hook->flags = req->flags;
14e68ba @julliard Added support for system-wide hooks.
julliard authored
458 hook->proc = req->proc;
459 hook->unicode = req->unicode;
460 hook->module = module;
461 hook->module_size = module_size;
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
462 reply->handle = hook->handle;
6334235 @julliard Store a bitmap of active hooks on the client side to try to avoid
julliard authored
463 reply->active_hooks = get_active_hooks();
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
464 }
5ceccec server: Remove redundant NULL check before free() (found by Smatch).
Michael Stefaniuc authored
465 else free( module );
14e68ba @julliard Added support for system-wide hooks.
julliard authored
466
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
467 done:
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
468 if (process) release_object( process );
14e68ba @julliard Added support for system-wide hooks.
julliard authored
469 if (thread) release_object( thread );
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
470 release_object( desktop );
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
471 }
472
473
474 /* remove a window hook */
475 DECL_HANDLER(remove_hook)
476 {
477 struct hook *hook;
478
ed04d36 Add more tests for old style hooks and winevent hooks, make them pass
Dmitry Timoshkov authored
479 if (req->handle)
480 {
481 if (!(hook = get_user_object( req->handle, USER_HOOK )))
482 {
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
483 set_error( STATUS_INVALID_HANDLE );
ed04d36 Add more tests for old style hooks and winevent hooks, make them pass
Dmitry Timoshkov authored
484 return;
485 }
486 }
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
487 else
488 {
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
489 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
490 {
491 set_error( STATUS_INVALID_PARAMETER );
492 return;
493 }
494 if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
ed04d36 Add more tests for old style hooks and winevent hooks, make them pass
Dmitry Timoshkov authored
495 {
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
496 set_error( STATUS_INVALID_PARAMETER );
ed04d36 Add more tests for old style hooks and winevent hooks, make them pass
Dmitry Timoshkov authored
497 return;
498 }
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
499 }
ed04d36 Add more tests for old style hooks and winevent hooks, make them pass
Dmitry Timoshkov authored
500 remove_hook( hook );
6334235 @julliard Store a bitmap of active hooks on the client side to try to avoid
julliard authored
501 reply->active_hooks = get_active_hooks();
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
502 }
503
504
505 /* start calling a hook chain */
506 DECL_HANDLER(start_hook_chain)
507 {
508 struct hook *hook;
d55e7f1 @julliard Store the hook table in the thread queue instead of the thread
julliard authored
509 struct hook_table *table = get_queue_hooks( current );
f039648 @julliard server: Increment the usage count of the global table also when start…
julliard authored
510 struct hook_table *global_table = get_global_hooks( current );
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
511
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
512 if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
513 {
514 set_error( STATUS_INVALID_PARAMETER );
515 return;
516 }
14e68ba @julliard Added support for system-wide hooks.
julliard authored
517
6334235 @julliard Store a bitmap of active hooks on the client side to try to avoid
julliard authored
518 reply->active_hooks = get_active_hooks();
519
520 if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK, req->event,
521 req->window, req->object_id, req->child_id )))
14e68ba @julliard Added support for system-wide hooks.
julliard authored
522 {
523 /* try global table */
f039648 @julliard server: Increment the usage count of the global table also when start…
julliard authored
524 if (!global_table || !(hook = get_first_valid_hook( global_table, req->id - WH_MINHOOK, req->event,
525 req->window, req->object_id, req->child_id )))
14e68ba @julliard Added support for system-wide hooks.
julliard authored
526 return; /* no hook set */
527 }
ca3ac8f @julliard Run WH_KEYBOARD_LL and WH_MOUSE_LL hooks in the context of the thread
julliard authored
528
6d85f3b @julliard server: Fixed low-level hardware hooks.
julliard authored
529 if (run_hook_in_owner_thread( hook ))
ca3ac8f @julliard Run WH_KEYBOARD_LL and WH_MOUSE_LL hooks in the context of the thread
julliard authored
530 {
6d85f3b @julliard server: Fixed low-level hardware hooks.
julliard authored
531 reply->pid = get_process_id( hook->owner->process );
532 reply->tid = get_thread_id( hook->owner );
ca3ac8f @julliard Run WH_KEYBOARD_LL and WH_MOUSE_LL hooks in the context of the thread
julliard authored
533 }
534 else
535 {
536 reply->pid = 0;
537 reply->tid = 0;
538 }
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
539 reply->proc = hook->proc;
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
540 reply->handle = hook->handle;
541 reply->unicode = hook->unicode;
f039648 @julliard server: Increment the usage count of the global table also when start…
julliard authored
542 if (table) table->counts[hook->index]++;
543 if (global_table) global_table->counts[hook->index]++;
14e68ba @julliard Added support for system-wide hooks.
julliard authored
544 if (hook->module) set_reply_data( hook->module, hook->module_size );
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
545 }
546
547
548 /* finished calling a hook chain */
549 DECL_HANDLER(finish_hook_chain)
550 {
d55e7f1 @julliard Store the hook table in the thread queue instead of the thread
julliard authored
551 struct hook_table *table = get_queue_hooks( current );
4a40b2e @julliard Store the global hooks table in the desktop object.
julliard authored
552 struct hook_table *global_hooks = get_global_hooks( current );
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
553 int index = req->id - WH_MINHOOK;
554
6dba0a7 Add support for winevent hooks.
Dmitry Timoshkov authored
555 if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
556 {
557 set_error( STATUS_INVALID_PARAMETER );
558 return;
559 }
560 if (table) release_hook_chain( table, index );
14e68ba @julliard Added support for system-wide hooks.
julliard authored
561 if (global_hooks) release_hook_chain( global_hooks, index );
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
562 }
563
564
2f80fcd @julliard server: Changed the get_next_hook request to allow retrieving the cur…
julliard authored
565 /* get the hook information */
566 DECL_HANDLER(get_hook_info)
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
567 {
2f80fcd @julliard server: Changed the get_next_hook request to allow retrieving the cur…
julliard authored
568 struct hook *hook;
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
569
570 if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
14e68ba @julliard Added support for system-wide hooks.
julliard authored
571 if (hook->thread && (hook->thread != current))
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
572 {
573 set_error( STATUS_INVALID_HANDLE );
574 return;
575 }
2f80fcd @julliard server: Changed the get_next_hook request to allow retrieving the cur…
julliard authored
576 if (req->get_next && !(hook = get_next_hook( current, hook, req->event, req->window,
577 req->object_id, req->child_id )))
578 return;
579
580 reply->handle = hook->handle;
581 reply->id = hook->index + WH_MINHOOK;
582 reply->unicode = hook->unicode;
583 if (hook->module) set_reply_data( hook->module, min(hook->module_size,get_reply_max_size()) );
584 if (run_hook_in_owner_thread( hook ))
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
585 {
2f80fcd @julliard server: Changed the get_next_hook request to allow retrieving the cur…
julliard authored
586 reply->pid = get_process_id( hook->owner->process );
587 reply->tid = get_thread_id( hook->owner );
588 }
589 else
590 {
591 reply->pid = 0;
592 reply->tid = 0;
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
593 }
2f80fcd @julliard server: Changed the get_next_hook request to allow retrieving the cur…
julliard authored
594 reply->proc = hook->proc;
0286135 @julliard Rewrote hook support to store the hook chain in the server.
julliard authored
595 }
Something went wrong with that request. Please try again.