Match PSDK STATUS_* definitions.
[wine] / server / hook.c
1 /*
2  * Server-side window hooks support
3  *
4  * Copyright (C) 2002 Alexandre Julliard
5  * Copyright (C) 2005 Dmitry Timoshkov
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
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28
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"
35
36 #include "object.h"
37 #include "process.h"
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 */
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     void               *proc;     /* hook function */
56     int                 unicode;  /* is it a unicode hook? */
57     WCHAR              *module;   /* module name for global hooks */
58     size_t              module_size;
59 };
60
61 #define WH_WINEVENT (WH_MAXHOOK+1)
62
63 #define NB_HOOKS (WH_WINEVENT-WH_MINHOOK+1)
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 */
80     no_add_queue,                 /* add_queue */
81     NULL,                         /* remove_queue */
82     NULL,                         /* signaled */
83     NULL,                         /* satisfied */
84     no_signal,                    /* signal */
85     no_get_fd,                    /* get_fd */
86     no_lookup_name,               /* lookup_name */
87     no_close_handle,              /* close_handle */
88     hook_table_destroy            /* destroy */
89 };
90
91
92 /* create a new hook table */
93 static struct hook_table *alloc_hook_table(void)
94 {
95     struct hook_table *table;
96     int i;
97
98     if ((table = alloc_object( &hook_table_ops )))
99     {
100         for (i = 0; i < NB_HOOKS; i++)
101         {
102             list_init( &table->hooks[i] );
103             table->counts[i] = 0;
104         }
105     }
106     return table;
107 }
108
109 static struct hook_table *get_global_hooks( struct thread *thread )
110 {
111     struct hook_table *table;
112     struct desktop *desktop = get_thread_desktop( thread, 0 );
113
114     if (!desktop) return NULL;
115     table = desktop->global_hooks;
116     release_object( desktop );
117     return table;
118 }
119
120 /* create a new hook and add it to the specified table */
121 static struct hook *add_hook( struct desktop *desktop, struct thread *thread, int index, int global )
122 {
123     struct hook *hook;
124     struct hook_table *table = global ? desktop->global_hooks : get_queue_hooks(thread);
125
126     if (!table)
127     {
128         if (!(table = alloc_hook_table())) return NULL;
129         if (global) desktop->global_hooks = table;
130         else set_queue_hooks( thread, table );
131     }
132     if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
133
134     if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
135     {
136         free( hook );
137         return NULL;
138     }
139     hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
140     hook->table  = table;
141     hook->index  = index;
142     list_add_head( &table->hooks[index], &hook->chain );
143     if (thread) thread->desktop_users++;
144     return hook;
145 }
146
147 /* free a hook, removing it from its chain */
148 static void free_hook( struct hook *hook )
149 {
150     free_user_handle( hook->handle );
151     if (hook->module) free( hook->module );
152     if (hook->thread)
153     {
154         assert( hook->thread->desktop_users > 0 );
155         hook->thread->desktop_users--;
156         release_object( hook->thread );
157     }
158     if (hook->process) release_object( hook->process );
159     release_object( hook->owner );
160     list_remove( &hook->chain );
161     free( hook );
162 }
163
164 /* find a hook from its index and proc */
165 static struct hook *find_hook( struct thread *thread, int index, void *proc )
166 {
167     struct list *p;
168     struct hook_table *table = get_queue_hooks( thread );
169
170     if (table)
171     {
172         LIST_FOR_EACH( p, &table->hooks[index] )
173         {
174             struct hook *hook = HOOK_ENTRY( p );
175             if (hook->proc == proc) return hook;
176         }
177     }
178     return NULL;
179 }
180
181 /* get the first hook in the chain */
182 inline static struct hook *get_first_hook( struct hook_table *table, int index )
183 {
184     struct list *elem = list_head( &table->hooks[index] );
185     return elem ? HOOK_ENTRY( elem ) : NULL;
186 }
187
188 /* check if a given hook should run in the current thread */
189 inline static int run_hook_in_current_thread( struct hook *hook )
190 {
191     if ((!hook->process || hook->process == current->process) &&
192         (!(hook->flags & WINEVENT_SKIPOWNPROCESS) || hook->process != current->process))
193     {
194         if ((!hook->thread || hook->thread == current) &&
195             (!(hook->flags & WINEVENT_SKIPOWNTHREAD) || hook->thread != current))
196             return 1;
197     }
198     return 0;
199 }
200
201 /* find the first non-deleted hook in the chain */
202 inline static struct hook *get_first_valid_hook( struct hook_table *table, int index,
203                                                  int event, user_handle_t win,
204                                                  int object_id, int child_id )
205 {
206     struct hook *hook = get_first_hook( table, index );
207
208     while (hook)
209     {
210         if (hook->proc && run_hook_in_current_thread( hook ))
211         {
212             if (event >= hook->event_min && event <= hook->event_max)
213             {
214                 if (hook->flags & WINEVENT_INCONTEXT) return hook;
215
216                 /* only winevent hooks may be out of context */
217                 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
218                 post_win_event( hook->owner, event, win, object_id, child_id,
219                                 hook->proc, hook->module, hook->module_size,
220                                 hook->handle );
221             }
222         }
223         hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
224     }
225     return hook;
226 }
227
228 /* find the next hook in the chain, skipping the deleted ones */
229 static struct hook *get_next_hook( struct thread *thread, struct hook *hook, int event,
230                                    user_handle_t win, int object_id, int child_id )
231 {
232     struct hook_table *global_hooks, *table = hook->table;
233     int index = hook->index;
234
235     while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
236     {
237         if (hook->proc && run_hook_in_current_thread( hook ))
238         {
239             if (event >= hook->event_min && event <= hook->event_max)
240             {
241                 if (hook->flags & WINEVENT_INCONTEXT) return hook;
242
243                 /* only winevent hooks may be out of context */
244                 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
245                 post_win_event( hook->owner, event, win, object_id, child_id,
246                                 hook->proc, hook->module, hook->module_size,
247                                 hook->handle );
248             }
249         }
250     }
251     global_hooks = get_global_hooks( thread );
252     if (global_hooks && table != global_hooks)  /* now search through the global table */
253     {
254         hook = get_first_valid_hook( global_hooks, index, event, win, object_id, child_id );
255     }
256     return hook;
257 }
258
259 static void hook_table_dump( struct object *obj, int verbose )
260 {
261     /* struct hook_table *table = (struct hook_table *)obj; */
262     fprintf( stderr, "Hook table\n" );
263 }
264
265 static void hook_table_destroy( struct object *obj )
266 {
267     int i;
268     struct hook *hook;
269     struct hook_table *table = (struct hook_table *)obj;
270
271     for (i = 0; i < NB_HOOKS; i++)
272     {
273         while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
274     }
275 }
276
277 /* remove a hook, freeing it if the chain is not in use */
278 static void remove_hook( struct hook *hook )
279 {
280     if (hook->table->counts[hook->index])
281         hook->proc = NULL; /* chain is in use, just mark it and return */
282     else
283         free_hook( hook );
284 }
285
286 /* release a hook chain, removing deleted hooks if the use count drops to 0 */
287 static void release_hook_chain( struct hook_table *table, int index )
288 {
289     if (!table->counts[index])  /* use count shouldn't already be 0 */
290     {
291         set_error( STATUS_INVALID_PARAMETER );
292         return;
293     }
294     if (!--table->counts[index])
295     {
296         struct hook *hook = get_first_hook( table, index );
297         while (hook)
298         {
299             struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
300             if (!hook->proc) free_hook( hook );
301             hook = next;
302         }
303     }
304 }
305
306 /* remove all global hooks owned by a given thread */
307 void remove_thread_hooks( struct thread *thread )
308 {
309     struct hook_table *global_hooks = get_global_hooks( thread );
310     int index;
311
312     if (!global_hooks) return;
313
314     /* only low-level keyboard/mouse global hooks can be owned by a thread */
315     for (index = WH_KEYBOARD_LL - WH_MINHOOK; index <= WH_MOUSE_LL - WH_MINHOOK; index++)
316     {
317         struct hook *hook = get_first_hook( global_hooks, index );
318         while (hook)
319         {
320             struct hook *next = HOOK_ENTRY( list_next( &global_hooks->hooks[index], &hook->chain ) );
321             if (hook->thread == thread) remove_hook( hook );
322             hook = next;
323         }
324     }
325 }
326
327 /* get a bitmap of active hooks in a hook table */
328 static int is_hook_active( struct hook_table *table, int index )
329 {
330     struct hook *hook = get_first_hook( table, index );
331
332     while (hook)
333     {
334         if (hook->proc && run_hook_in_current_thread( hook )) return 1;
335         hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
336     }
337     return 0;
338 }
339
340 /* get a bitmap of all active hooks for the current thread */
341 unsigned int get_active_hooks(void)
342 {
343     struct hook_table *table = get_queue_hooks( current );
344     struct hook_table *global_hooks = get_global_hooks( current );
345     unsigned int ret = 1 << 31;  /* set high bit to indicate that the bitmap is valid */
346     int id;
347
348     for (id = WH_MINHOOK; id <= WH_WINEVENT; id++)
349     {
350         if ((table && is_hook_active( table, id - WH_MINHOOK )) ||
351             (global_hooks && is_hook_active( global_hooks, id - WH_MINHOOK )))
352             ret |= 1 << (id - WH_MINHOOK);
353     }
354     return ret;
355 }
356
357 /* set a window hook */
358 DECL_HANDLER(set_hook)
359 {
360     struct process *process = NULL;
361     struct thread *thread = NULL;
362     struct desktop *desktop;
363     struct hook *hook;
364     WCHAR *module;
365     int global;
366     size_t module_size = get_req_data_size();
367
368     if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
369     {
370         set_error( STATUS_INVALID_PARAMETER );
371         return;
372     }
373
374     if (!(desktop = get_thread_desktop( current, DESKTOP_HOOKCONTROL ))) return;
375
376     if (req->pid && !(process = get_process_from_id( req->pid ))) goto done;
377
378     if (req->tid)
379     {
380         if (!(thread = get_thread_from_id( req->tid ))) goto done;
381         if (process && process != thread->process)
382         {
383             set_error( STATUS_INVALID_PARAMETER );
384             goto done;
385         }
386     }
387
388     if (req->id == WH_KEYBOARD_LL || req->id == WH_MOUSE_LL)
389     {
390         /* low-level hardware hooks are special: always global, but without a module */
391         thread = (struct thread *)grab_object( current );
392         module = NULL;
393         global = 1;
394     }
395     else if (!req->tid)
396     {
397         /* out of context hooks do not need a module handle */
398         if (!module_size && (req->flags & WINEVENT_INCONTEXT))
399         {
400             set_error( STATUS_INVALID_PARAMETER );
401             goto done;
402         }
403         if (!(module = memdup( get_req_data(), module_size ))) goto done;
404         global = 1;
405     }
406     else
407     {
408         module = NULL;
409         global = 0;
410     }
411
412     if ((hook = add_hook( desktop, thread, req->id - WH_MINHOOK, global )))
413     {
414         hook->owner = (struct thread *)grab_object( current );
415         hook->process = process ? (struct process *)grab_object( process ) : NULL;
416         hook->event_min   = req->event_min;
417         hook->event_max   = req->event_max;
418         hook->flags       = req->flags;
419         hook->proc        = req->proc;
420         hook->unicode     = req->unicode;
421         hook->module      = module;
422         hook->module_size = module_size;
423         reply->handle = hook->handle;
424         reply->active_hooks = get_active_hooks();
425     }
426     else if (module) free( module );
427
428 done:
429     if (process) release_object( process );
430     if (thread) release_object( thread );
431     release_object( desktop );
432 }
433
434
435 /* remove a window hook */
436 DECL_HANDLER(remove_hook)
437 {
438     struct hook *hook;
439
440     if (req->handle)
441     {
442         if (!(hook = get_user_object( req->handle, USER_HOOK )))
443         {
444             set_error( STATUS_INVALID_HANDLE );
445             return;
446         }
447     }
448     else
449     {
450         if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
451         {
452             set_error( STATUS_INVALID_PARAMETER );
453             return;
454         }
455         if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
456         {
457             set_error( STATUS_INVALID_PARAMETER );
458             return;
459         }
460     }
461     remove_hook( hook );
462     reply->active_hooks = get_active_hooks();
463 }
464
465
466 /* start calling a hook chain */
467 DECL_HANDLER(start_hook_chain)
468 {
469     struct hook *hook;
470     struct hook_table *table = get_queue_hooks( current );
471
472     if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
473     {
474         set_error( STATUS_INVALID_PARAMETER );
475         return;
476     }
477
478     reply->active_hooks = get_active_hooks();
479
480     if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK, req->event,
481                                                  req->window, req->object_id, req->child_id )))
482     {
483         /* try global table */
484         if (!(table = get_global_hooks( current )) ||
485             !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK, req->event,
486                                            req->window, req->object_id, req->child_id )))
487             return;  /* no hook set */
488     }
489
490     if (hook->thread && hook->thread != current) /* must run in other thread */
491     {
492         reply->pid  = get_process_id( hook->thread->process );
493         reply->tid  = get_thread_id( hook->thread );
494     }
495     else
496     {
497         reply->pid  = 0;
498         reply->tid  = 0;
499     }
500     reply->proc    = hook->proc;
501     reply->handle  = hook->handle;
502     reply->unicode = hook->unicode;
503     table->counts[hook->index]++;
504     if (hook->module) set_reply_data( hook->module, hook->module_size );
505 }
506
507
508 /* finished calling a hook chain */
509 DECL_HANDLER(finish_hook_chain)
510 {
511     struct hook_table *table = get_queue_hooks( current );
512     struct hook_table *global_hooks = get_global_hooks( current );
513     int index = req->id - WH_MINHOOK;
514
515     if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
516     {
517         set_error( STATUS_INVALID_PARAMETER );
518         return;
519     }
520     if (table) release_hook_chain( table, index );
521     if (global_hooks) release_hook_chain( global_hooks, index );
522 }
523
524
525 /* get the next hook to call */
526 DECL_HANDLER(get_next_hook)
527 {
528     struct hook *hook, *next;
529
530     if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
531     if (hook->thread && (hook->thread != current))
532     {
533         set_error( STATUS_INVALID_HANDLE );
534         return;
535     }
536     if ((next = get_next_hook( current, hook, req->event, req->window, req->object_id, req->child_id )))
537     {
538         reply->next = next->handle;
539         reply->id   = next->index + WH_MINHOOK;
540         reply->prev_unicode = hook->unicode;
541         reply->next_unicode = next->unicode;
542         if (next->module) set_reply_data( next->module, next->module_size );
543         if (next->thread && next->thread != current)
544         {
545             reply->pid  = get_process_id( next->thread->process );
546             reply->tid  = get_thread_id( next->thread );
547         }
548         else
549         {
550             reply->pid  = 0;
551             reply->tid  = 0;
552         }
553         reply->proc = next->proc;
554     }
555 }