Store the windows and system directories as long path names.
[wine.git] / server / hook.c
blobab868659ea2264dc13776fc9b1ccbb1800f76152
1 /*
2 * Server-side window hooks support
4 * Copyright (C) 2002 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
32 #include "object.h"
33 #include "process.h"
34 #include "request.h"
35 #include "user.h"
37 struct hook_table;
39 struct hook
41 struct list chain; /* hook chain entry */
42 user_handle_t handle; /* user handle for this hook */
43 struct thread *thread; /* thread owning the hook */
44 int index; /* hook table index */
45 void *proc; /* hook function */
46 int unicode; /* is it a unicode hook? */
47 WCHAR *module; /* module name for global hooks */
48 size_t module_size;
51 #define NB_HOOKS (WH_MAXHOOK-WH_MINHOOK+1)
52 #define HOOK_ENTRY(p) LIST_ENTRY( (p), struct hook, chain )
54 struct hook_table
56 struct object obj; /* object header */
57 struct list hooks[NB_HOOKS]; /* array of hook chains */
58 int counts[NB_HOOKS]; /* use counts for each hook chain */
61 static void hook_table_dump( struct object *obj, int verbose );
62 static void hook_table_destroy( struct object *obj );
64 static const struct object_ops hook_table_ops =
66 sizeof(struct hook_table), /* size */
67 hook_table_dump, /* dump */
68 no_add_queue, /* add_queue */
69 NULL, /* remove_queue */
70 NULL, /* signaled */
71 NULL, /* satisfied */
72 no_get_fd, /* get_fd */
73 hook_table_destroy /* destroy */
77 static struct hook_table *global_hooks;
79 /* create a new hook table */
80 static struct hook_table *alloc_hook_table(void)
82 struct hook_table *table;
83 int i;
85 if ((table = alloc_object( &hook_table_ops )))
87 for (i = 0; i < NB_HOOKS; i++)
89 list_init( &table->hooks[i] );
90 table->counts[i] = 0;
93 return table;
96 /* create a new hook and add it to the specified table */
97 static struct hook *add_hook( struct thread *thread, int index, int global )
99 struct hook *hook;
100 struct hook_table *table = global ? global_hooks : get_queue_hooks(thread);
102 if (!table)
104 if (!(table = alloc_hook_table())) return NULL;
105 if (global) global_hooks = table;
106 else set_queue_hooks( thread, table );
108 if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
110 if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
112 free( hook );
113 return NULL;
115 hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
116 hook->index = index;
117 list_add_head( &table->hooks[index], &hook->chain );
118 return hook;
121 /* free a hook, removing it from its chain */
122 static void free_hook( struct hook *hook )
124 free_user_handle( hook->handle );
125 if (hook->module) free( hook->module );
126 if (hook->thread) release_object( hook->thread );
127 list_remove( &hook->chain );
128 free( hook );
131 /* find a hook from its index and proc */
132 static struct hook *find_hook( struct thread *thread, int index, void *proc )
134 struct list *p;
135 struct hook_table *table = get_queue_hooks( thread );
137 if (table)
139 LIST_FOR_EACH( p, &table->hooks[index] )
141 struct hook *hook = HOOK_ENTRY( p );
142 if (hook->proc == proc) return hook;
145 return NULL;
148 /* get the hook table that a given hook belongs to */
149 inline static struct hook_table *get_table( struct hook *hook )
151 if (!hook->thread) return global_hooks;
152 if (hook->index + WH_MINHOOK == WH_KEYBOARD_LL) return global_hooks;
153 if (hook->index + WH_MINHOOK == WH_MOUSE_LL) return global_hooks;
154 return get_queue_hooks(hook->thread);
157 /* get the first hook in the chain */
158 inline static struct hook *get_first_hook( struct hook_table *table, int index )
160 struct list *elem = list_head( &table->hooks[index] );
161 return elem ? HOOK_ENTRY( elem ) : NULL;
164 /* find the first non-deleted hook in the chain */
165 inline static struct hook *get_first_valid_hook( struct hook_table *table, int index )
167 struct hook *hook = get_first_hook( table, index );
168 while (hook && !hook->proc)
169 hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
170 return hook;
173 /* find the next hook in the chain, skipping the deleted ones */
174 static struct hook *get_next_hook( struct hook *hook )
176 struct hook_table *table = get_table( hook );
177 int index = hook->index;
179 while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
181 if (hook->proc) return hook;
183 if (global_hooks && table != global_hooks) /* now search through the global table */
185 hook = get_first_valid_hook( global_hooks, index );
187 return hook;
190 static void hook_table_dump( struct object *obj, int verbose )
192 struct hook_table *table = (struct hook_table *)obj;
193 if (table == global_hooks) fprintf( stderr, "Global hook table\n" );
194 else fprintf( stderr, "Hook table\n" );
197 static void hook_table_destroy( struct object *obj )
199 int i;
200 struct hook *hook;
201 struct hook_table *table = (struct hook_table *)obj;
203 for (i = 0; i < NB_HOOKS; i++)
205 while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
209 /* free the global hooks table */
210 void close_global_hooks(void)
212 if (global_hooks) release_object( global_hooks );
215 /* remove a hook, freeing it if the chain is not in use */
216 static void remove_hook( struct hook *hook )
218 struct hook_table *table = get_table( hook );
219 if (table->counts[hook->index])
220 hook->proc = NULL; /* chain is in use, just mark it and return */
221 else
222 free_hook( hook );
225 /* release a hook chain, removing deleted hooks if the use count drops to 0 */
226 static void release_hook_chain( struct hook_table *table, int index )
228 if (!table->counts[index]) /* use count shouldn't already be 0 */
230 set_error( STATUS_INVALID_PARAMETER );
231 return;
233 if (!--table->counts[index])
235 struct hook *hook = get_first_hook( table, index );
236 while (hook)
238 struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
239 if (!hook->proc) free_hook( hook );
240 hook = next;
245 /* remove all global hooks owned by a given thread */
246 void remove_thread_hooks( struct thread *thread )
248 int index;
250 if (!global_hooks) return;
252 /* only low-level keyboard/mouse global hooks can be owned by a thread */
253 for (index = WH_KEYBOARD_LL - WH_MINHOOK; index <= WH_MOUSE_LL - WH_MINHOOK; index++)
255 struct hook *hook = get_first_hook( global_hooks, index );
256 while (hook)
258 struct hook *next = HOOK_ENTRY( list_next( &global_hooks->hooks[index], &hook->chain ) );
259 if (hook->thread == thread) remove_hook( hook );
260 hook = next;
265 /* set a window hook */
266 DECL_HANDLER(set_hook)
268 struct thread *thread;
269 struct hook *hook;
270 WCHAR *module;
271 int global;
272 size_t module_size = get_req_data_size();
274 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
276 set_error( STATUS_INVALID_PARAMETER );
277 return;
279 if (req->id == WH_KEYBOARD_LL || req->id == WH_MOUSE_LL)
281 /* low-level hardware hooks are special: always global, but without a module */
282 thread = (struct thread *)grab_object( current );
283 module = NULL;
284 global = 1;
286 else if (!req->tid)
288 if (!module_size)
290 set_error( STATUS_INVALID_PARAMETER );
291 return;
293 if (!(module = memdup( get_req_data(), module_size ))) return;
294 thread = NULL;
295 global = 1;
297 else
299 module = NULL;
300 global = 0;
301 if (!(thread = get_thread_from_id( req->tid ))) return;
304 if ((hook = add_hook( thread, req->id - WH_MINHOOK, global )))
306 hook->proc = req->proc;
307 hook->unicode = req->unicode;
308 hook->module = module;
309 hook->module_size = module_size;
310 reply->handle = hook->handle;
312 else if (module) free( module );
314 if (thread) release_object( thread );
318 /* remove a window hook */
319 DECL_HANDLER(remove_hook)
321 struct hook *hook;
323 if (req->handle) hook = get_user_object( req->handle, USER_HOOK );
324 else
326 if (!req->proc || req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
328 set_error( STATUS_INVALID_PARAMETER );
329 return;
331 if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
332 set_error( STATUS_INVALID_PARAMETER );
334 if (hook) remove_hook( hook );
338 /* start calling a hook chain */
339 DECL_HANDLER(start_hook_chain)
341 struct hook *hook;
342 struct hook_table *table = get_queue_hooks( current );
344 if (req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
346 set_error( STATUS_INVALID_PARAMETER );
347 return;
350 if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK )))
352 /* try global table */
353 if (!(table = global_hooks) ||
354 !(hook = get_first_valid_hook( global_hooks, req->id - WH_MINHOOK )))
355 return; /* no hook set */
358 if (hook->thread && hook->thread != current) /* must run in other thread */
360 reply->pid = get_process_id( hook->thread->process );
361 reply->tid = get_thread_id( hook->thread );
362 reply->proc = 0;
364 else
366 reply->pid = 0;
367 reply->tid = 0;
368 reply->proc = hook->proc;
370 reply->handle = hook->handle;
371 reply->unicode = hook->unicode;
372 table->counts[hook->index]++;
373 if (hook->module) set_reply_data( hook->module, hook->module_size );
377 /* finished calling a hook chain */
378 DECL_HANDLER(finish_hook_chain)
380 struct hook_table *table = get_queue_hooks( current );
381 int index = req->id - WH_MINHOOK;
383 if (req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
385 set_error( STATUS_INVALID_PARAMETER );
386 return;
388 if (table) release_hook_chain( table, index );
389 if (global_hooks) release_hook_chain( global_hooks, index );
393 /* get the next hook to call */
394 DECL_HANDLER(get_next_hook)
396 struct hook *hook, *next;
398 if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
399 if (hook->thread && (hook->thread != current))
401 set_error( STATUS_INVALID_HANDLE );
402 return;
404 if ((next = get_next_hook( hook )))
406 reply->next = next->handle;
407 reply->id = next->index + WH_MINHOOK;
408 reply->prev_unicode = hook->unicode;
409 reply->next_unicode = next->unicode;
410 if (next->module) set_reply_data( next->module, next->module_size );
411 if (next->thread && next->thread != current)
413 reply->pid = get_process_id( next->thread->process );
414 reply->tid = get_thread_id( next->thread );
415 reply->proc = 0;
417 else
419 reply->pid = 0;
420 reply->tid = 0;
421 reply->proc = next->proc;