hook.c 18.6 KB
Newer Older
1 2 3 4
/*
 * Server-side window hooks support
 *
 * Copyright (C) 2002 Alexandre Julliard
5
 * Copyright (C) 2005 Dmitry Timoshkov
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
20 21 22 23 24 25
 */

#include "config.h"
#include "wine/port.h"

#include <assert.h>
26
#include <stdarg.h>
27 28
#include <stdio.h>

29 30
#include "ntstatus.h"
#define WIN32_NO_STATUS
31
#include "windef.h"
32 33
#include "winbase.h"
#include "winuser.h"
34
#include "winternl.h"
35 36

#include "object.h"
37
#include "process.h"
38 39 40 41 42 43 44 45 46
#include "request.h"
#include "user.h"

struct hook_table;

struct hook
{
    struct list         chain;    /* hook chain entry */
    user_handle_t       handle;   /* user handle for this hook */
47 48 49
    struct process     *process;  /* process the hook is set to */
    struct thread      *thread;   /* thread the hook is set to */
    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 53 54
    int                 event_min;
    int                 event_max;
    int                 flags;
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;
59 60
};

61 62 63
#define WH_WINEVENT (WH_MAXHOOK+1)

#define NB_HOOKS (WH_WINEVENT-WH_MINHOOK+1)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#define HOOK_ENTRY(p)  LIST_ENTRY( (p), struct hook, chain )

struct hook_table
{
    struct object obj;              /* object header */
    struct list   hooks[NB_HOOKS];  /* array of hook chains */
    int           counts[NB_HOOKS]; /* use counts for each hook chain */
};

static void hook_table_dump( struct object *obj, int verbose );
static void hook_table_destroy( struct object *obj );

static const struct object_ops hook_table_ops =
{
    sizeof(struct hook_table),    /* size */
79
    &no_type,                     /* type */
80 81 82 83 84
    hook_table_dump,              /* dump */
    no_add_queue,                 /* add_queue */
    NULL,                         /* remove_queue */
    NULL,                         /* signaled */
    NULL,                         /* satisfied */
85
    no_signal,                    /* signal */
86
    no_get_fd,                    /* get_fd */
87
    default_map_access,           /* map_access */
88 89
    default_get_sd,               /* get_sd */
    default_set_sd,               /* set_sd */
90
    no_get_full_name,             /* get_full_name */
91
    no_lookup_name,               /* lookup_name */
92 93
    no_link_name,                 /* link_name */
    NULL,                         /* unlink_name */
94
    no_open_file,                 /* open_file */
95
    no_kernel_obj_list,           /* get_kernel_obj_list */
96
    no_close_handle,              /* close_handle */
97 98 99 100 101 102 103 104 105 106
    hook_table_destroy            /* destroy */
};


/* create a new hook table */
static struct hook_table *alloc_hook_table(void)
{
    struct hook_table *table;
    int i;

107
    if ((table = alloc_object( &hook_table_ops )))
108 109 110 111 112 113 114 115 116 117
    {
        for (i = 0; i < NB_HOOKS; i++)
        {
            list_init( &table->hooks[i] );
            table->counts[i] = 0;
        }
    }
    return table;
}

118 119 120
static struct hook_table *get_global_hooks( struct thread *thread )
{
    struct hook_table *table;
121
    struct desktop *desktop;
122

123 124
    if (!thread->desktop) return NULL;
    if (!(desktop = get_thread_desktop( thread, 0 ))) return NULL;
125 126 127 128 129
    table = desktop->global_hooks;
    release_object( desktop );
    return table;
}

130
/* create a new hook and add it to the specified table */
131
static struct hook *add_hook( struct desktop *desktop, struct thread *thread, int index, int global )
132 133
{
    struct hook *hook;
134
    struct hook_table *table = global ? desktop->global_hooks : get_queue_hooks(thread);
135 136 137 138

    if (!table)
    {
        if (!(table = alloc_hook_table())) return NULL;
139
        if (global) desktop->global_hooks = table;
140
        else set_queue_hooks( thread, table );
141 142 143 144 145 146 147 148 149
    }
    if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;

    if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
    {
        free( hook );
        return NULL;
    }
    hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
150
    hook->table  = table;
151 152
    hook->index  = index;
    list_add_head( &table->hooks[index], &hook->chain );
153
    if (thread) thread->desktop_users++;
154 155 156 157 158 159 160
    return hook;
}

/* free a hook, removing it from its chain */
static void free_hook( struct hook *hook )
{
    free_user_handle( hook->handle );
161
    free( hook->module );
162 163 164 165 166 167
    if (hook->thread)
    {
        assert( hook->thread->desktop_users > 0 );
        hook->thread->desktop_users--;
        release_object( hook->thread );
    }
168 169
    if (hook->process) release_object( hook->process );
    release_object( hook->owner );
170 171 172 173 174
    list_remove( &hook->chain );
    free( hook );
}

/* find a hook from its index and proc */
175
static struct hook *find_hook( struct thread *thread, int index, client_ptr_t proc )
176 177
{
    struct list *p;
178
    struct hook_table *table = get_queue_hooks( thread );
179 180 181 182 183 184 185 186 187 188 189 190 191

    if (table)
    {
        LIST_FOR_EACH( p, &table->hooks[index] )
        {
            struct hook *hook = HOOK_ENTRY( p );
            if (hook->proc == proc) return hook;
        }
    }
    return NULL;
}

/* get the first hook in the chain */
192
static inline struct hook *get_first_hook( struct hook_table *table, int index )
193 194 195 196 197
{
    struct list *elem = list_head( &table->hooks[index] );
    return elem ? HOOK_ENTRY( elem ) : NULL;
}

198
/* check if a given hook should run in the owner thread instead of the current thread */
199
static inline int run_hook_in_owner_thread( struct hook *hook )
200 201 202 203 204 205 206
{
    if ((hook->index == WH_MOUSE_LL - WH_MINHOOK ||
         hook->index == WH_KEYBOARD_LL - WH_MINHOOK))
        return hook->owner != current;
    return 0;
}

207 208 209 210 211 212 213
/* check if a given hook should run in the current thread */
static inline int run_hook_in_current_thread( struct hook *hook )
{
    if (hook->process && hook->process != current->process) return 0;
    if ((hook->flags & WINEVENT_SKIPOWNPROCESS) && hook->process == current->process) return 0;
    if (hook->thread && hook->thread != current) return 0;
    if ((hook->flags & WINEVENT_SKIPOWNTHREAD) && hook->thread == current) return 0;
214 215
    /* don't run low-level hooks in processes suspended for debugging */
    if (run_hook_in_owner_thread( hook ) && hook->owner->process->suspend) return 0;
216 217 218
    return 1;
}

219
/* find the first non-deleted hook in the chain */
220
static inline struct hook *get_first_valid_hook( struct hook_table *table, int index,
221 222
                                                 int event, user_handle_t win,
                                                 int object_id, int child_id )
223 224
{
    struct hook *hook = get_first_hook( table, index );
225 226 227

    while (hook)
    {
228
        if (hook->proc && run_hook_in_current_thread( hook ))
229
        {
230
            if (event >= hook->event_min && event <= hook->event_max)
231
            {
232 233 234 235 236 237 238
                if (hook->flags & WINEVENT_INCONTEXT) return hook;

                /* only winevent hooks may be out of context */
                assert(hook->index + WH_MINHOOK == WH_WINEVENT);
                post_win_event( hook->owner, event, win, object_id, child_id,
                                hook->proc, hook->module, hook->module_size,
                                hook->handle );
239 240
            }
        }
241
        hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
242
    }
243 244 245
    return hook;
}

246
/* find the next hook in the chain, skipping the deleted ones */
247 248
static struct hook *get_next_hook( struct thread *thread, struct hook *hook, int event,
                                   user_handle_t win, int object_id, int child_id )
249
{
250
    struct hook_table *global_hooks, *table = hook->table;
251
    int index = hook->index;
252

253
    while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
254
    {
255
        if (hook->proc && run_hook_in_current_thread( hook ))
256
        {
257
            if (event >= hook->event_min && event <= hook->event_max)
258
            {
259 260 261 262 263 264 265
                if (hook->flags & WINEVENT_INCONTEXT) return hook;

                /* only winevent hooks may be out of context */
                assert(hook->index + WH_MINHOOK == WH_WINEVENT);
                post_win_event( hook->owner, event, win, object_id, child_id,
                                hook->proc, hook->module, hook->module_size,
                                hook->handle );
266 267
            }
        }
268
    }
269
    global_hooks = get_global_hooks( thread );
270 271
    if (global_hooks && table != global_hooks)  /* now search through the global table */
    {
272
        hook = get_first_valid_hook( global_hooks, index, event, win, object_id, child_id );
273 274
    }
    return hook;
275 276 277 278
}

static void hook_table_dump( struct object *obj, int verbose )
{
279 280
    /* struct hook_table *table = (struct hook_table *)obj; */
    fprintf( stderr, "Hook table\n" );
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
}

static void hook_table_destroy( struct object *obj )
{
    int i;
    struct hook *hook;
    struct hook_table *table = (struct hook_table *)obj;

    for (i = 0; i < NB_HOOKS; i++)
    {
        while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
    }
}

/* remove a hook, freeing it if the chain is not in use */
static void remove_hook( struct hook *hook )
{
298
    if (hook->table->counts[hook->index])
299
        hook->proc = 0; /* chain is in use, just mark it and return */
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    else
        free_hook( hook );
}

/* release a hook chain, removing deleted hooks if the use count drops to 0 */
static void release_hook_chain( struct hook_table *table, int index )
{
    if (!table->counts[index])  /* use count shouldn't already be 0 */
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }
    if (!--table->counts[index])
    {
        struct hook *hook = get_first_hook( table, index );
        while (hook)
        {
            struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
            if (!hook->proc) free_hook( hook );
            hook = next;
        }
    }
}

324 325 326
/* remove all global hooks owned by a given thread */
void remove_thread_hooks( struct thread *thread )
{
327
    struct hook_table *global_hooks = get_global_hooks( thread );
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    int index;

    if (!global_hooks) return;

    /* only low-level keyboard/mouse global hooks can be owned by a thread */
    for (index = WH_KEYBOARD_LL - WH_MINHOOK; index <= WH_MOUSE_LL - WH_MINHOOK; index++)
    {
        struct hook *hook = get_first_hook( global_hooks, index );
        while (hook)
        {
            struct hook *next = HOOK_ENTRY( list_next( &global_hooks->hooks[index], &hook->chain ) );
            if (hook->thread == thread) remove_hook( hook );
            hook = next;
        }
    }
}
344

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
/* get a bitmap of active hooks in a hook table */
static int is_hook_active( struct hook_table *table, int index )
{
    struct hook *hook = get_first_hook( table, index );

    while (hook)
    {
        if (hook->proc && run_hook_in_current_thread( hook )) return 1;
        hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
    }
    return 0;
}

/* get a bitmap of all active hooks for the current thread */
unsigned int get_active_hooks(void)
{
    struct hook_table *table = get_queue_hooks( current );
362
    struct hook_table *global_hooks = get_global_hooks( current );
363
    unsigned int ret = 1u << 31;  /* set high bit to indicate that the bitmap is valid */
364 365 366 367 368 369 370 371 372 373 374
    int id;

    for (id = WH_MINHOOK; id <= WH_WINEVENT; id++)
    {
        if ((table && is_hook_active( table, id - WH_MINHOOK )) ||
            (global_hooks && is_hook_active( global_hooks, id - WH_MINHOOK )))
            ret |= 1 << (id - WH_MINHOOK);
    }
    return ret;
}

375 376 377 378 379 380 381 382 383 384 385
/* return the thread that owns the first global hook */
struct thread *get_first_global_hook( int id )
{
    struct hook *hook;
    struct hook_table *global_hooks = get_global_hooks( current );

    if (!global_hooks) return NULL;
    if (!(hook = get_first_valid_hook( global_hooks, id - WH_MINHOOK, EVENT_MIN, 0, 0, 0 ))) return NULL;
    return hook->owner;
}

386 387 388
/* set a window hook */
DECL_HANDLER(set_hook)
{
389 390
    struct process *process = NULL;
    struct thread *thread = NULL;
391
    struct desktop *desktop;
392
    struct hook *hook;
393
    WCHAR *module;
394
    int global;
395
    data_size_t module_size = get_req_data_size();
396

397
    if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
398 399 400 401
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }
402

403 404 405
    if (!(desktop = get_thread_desktop( current, DESKTOP_HOOKCONTROL ))) return;

    if (req->pid && !(process = get_process_from_id( req->pid ))) goto done;
406 407 408

    if (req->tid)
    {
409
        if (!(thread = get_thread_from_id( req->tid ))) goto done;
410 411 412
        if (process && process != thread->process)
        {
            set_error( STATUS_INVALID_PARAMETER );
413
            goto done;
414 415 416
        }
    }

417 418 419
    if (req->id == WH_KEYBOARD_LL || req->id == WH_MOUSE_LL)
    {
        /* low-level hardware hooks are special: always global, but without a module */
420 421 422 423 424
        if (thread)
        {
            set_error( STATUS_INVALID_PARAMETER );
            goto done;
        }
425 426 427 428
        module = NULL;
        global = 1;
    }
    else if (!req->tid)
429
    {
430 431
        /* out of context hooks do not need a module handle */
        if (!module_size && (req->flags & WINEVENT_INCONTEXT))
432 433
        {
            set_error( STATUS_INVALID_PARAMETER );
434
            goto done;
435
        }
436
        if (!(module = memdup( get_req_data(), module_size ))) goto done;
437
        global = 1;
438 439 440
    }
    else
    {
441 442 443 444 445 446 447 448 449 450 451
        /* module is optional only if hook is in current process */
        if (!module_size)
        {
            module = NULL;
            if (thread->process != current->process)
            {
                set_error( STATUS_INVALID_PARAMETER );
                goto done;
            }
        }
        else if (!(module = memdup( get_req_data(), module_size ))) goto done;
452
        global = 0;
453
    }
454

455
    if ((hook = add_hook( desktop, thread, req->id - WH_MINHOOK, global )))
456
    {
457 458 459 460 461
        hook->owner = (struct thread *)grab_object( current );
        hook->process = process ? (struct process *)grab_object( process ) : NULL;
        hook->event_min   = req->event_min;
        hook->event_max   = req->event_max;
        hook->flags       = req->flags;
462 463 464 465
        hook->proc        = req->proc;
        hook->unicode     = req->unicode;
        hook->module      = module;
        hook->module_size = module_size;
466
        reply->handle = hook->handle;
467
        reply->active_hooks = get_active_hooks();
468
    }
469
    else free( module );
470

471
done:
472
    if (process) release_object( process );
473
    if (thread) release_object( thread );
474
    release_object( desktop );
475 476 477 478 479 480 481 482
}


/* remove a window hook */
DECL_HANDLER(remove_hook)
{
    struct hook *hook;

483 484 485 486
    if (req->handle)
    {
        if (!(hook = get_user_object( req->handle, USER_HOOK )))
        {
487
            set_error( STATUS_INVALID_HANDLE );
488 489 490
            return;
        }
    }
491 492
    else
    {
493
        if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
494 495 496 497 498
        {
            set_error( STATUS_INVALID_PARAMETER );
            return;
        }
        if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
499
        {
500
            set_error( STATUS_INVALID_PARAMETER );
501 502
            return;
        }
503
    }
504
    remove_hook( hook );
505
    reply->active_hooks = get_active_hooks();
506 507 508 509 510 511 512
}


/* start calling a hook chain */
DECL_HANDLER(start_hook_chain)
{
    struct hook *hook;
513
    struct hook_table *table = get_queue_hooks( current );
514
    struct hook_table *global_table = get_global_hooks( current );
515

516
    if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
517 518 519 520
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }
521

522 523 524 525
    reply->active_hooks = get_active_hooks();

    if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK, req->event,
                                                 req->window, req->object_id, req->child_id )))
526 527
    {
        /* try global table */
528 529
        if (!global_table || !(hook = get_first_valid_hook( global_table, req->id - WH_MINHOOK, req->event,
                                                            req->window, req->object_id, req->child_id )))
530 531
            return;  /* no hook set */
    }
532

533
    if (run_hook_in_owner_thread( hook ))
534
    {
535 536
        reply->pid  = get_process_id( hook->owner->process );
        reply->tid  = get_thread_id( hook->owner );
537 538 539 540 541 542
    }
    else
    {
        reply->pid  = 0;
        reply->tid  = 0;
    }
543
    reply->proc    = hook->proc;
544 545
    reply->handle  = hook->handle;
    reply->unicode = hook->unicode;
546 547
    if (table) table->counts[hook->index]++;
    if (global_table) global_table->counts[hook->index]++;
548
    if (hook->module) set_reply_data( hook->module, hook->module_size );
549 550 551 552 553 554
}


/* finished calling a hook chain */
DECL_HANDLER(finish_hook_chain)
{
555
    struct hook_table *table = get_queue_hooks( current );
556
    struct hook_table *global_hooks = get_global_hooks( current );
557 558
    int index = req->id - WH_MINHOOK;

559
    if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
560 561 562 563 564
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }
    if (table) release_hook_chain( table, index );
565
    if (global_hooks) release_hook_chain( global_hooks, index );
566 567 568
}


569 570
/* get the hook information */
DECL_HANDLER(get_hook_info)
571
{
572
    struct hook *hook;
573 574

    if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
575
    if (hook->thread && (hook->thread != current))
576 577 578 579
    {
        set_error( STATUS_INVALID_HANDLE );
        return;
    }
580 581 582 583 584 585 586 587 588
    if (req->get_next && !(hook = get_next_hook( current, hook, req->event, req->window,
                                                 req->object_id, req->child_id )))
        return;

    reply->handle  = hook->handle;
    reply->id      = hook->index + WH_MINHOOK;
    reply->unicode = hook->unicode;
    if (hook->module) set_reply_data( hook->module, min(hook->module_size,get_reply_max_size()) );
    if (run_hook_in_owner_thread( hook ))
589
    {
590 591 592 593 594 595 596
        reply->pid  = get_process_id( hook->owner->process );
        reply->tid  = get_thread_id( hook->owner );
    }
    else
    {
        reply->pid  = 0;
        reply->tid  = 0;
597
    }
598
    reply->proc = hook->proc;
599
}