queue.c 69.7 KB
Newer Older
1 2 3 4
/*
 * Server-side message queues
 *
 * Copyright (C) 2000 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21 22 23
#include "config.h"
#include "wine/port.h"

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

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

37
#include "handle.h"
38
#include "file.h"
39 40 41
#include "thread.h"
#include "process.h"
#include "request.h"
42
#include "user.h"
43

44
#define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
45
#define WM_NCMOUSELAST  (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
46

47 48
enum message_kind { SEND_MESSAGE, POST_MESSAGE };
#define NB_MSG_KINDS (POST_MESSAGE+1)
49 50


51 52
struct message_result
{
53
    struct list            sender_entry;  /* entry in sender list */
54
    struct message        *msg;           /* message the result is for */
55 56 57 58 59
    struct message_result *recv_next;     /* next in receiver list */
    struct msg_queue      *sender;        /* sender queue */
    struct msg_queue      *receiver;      /* receiver queue */
    int                    replied;       /* has it been replied to? */
    unsigned int           error;         /* error code to pass back to sender */
60
    unsigned long          result;        /* reply result */
61 62 63 64
    struct message        *callback_msg;  /* message to queue for callback */
    void                  *data;          /* message reply data */
    unsigned int           data_size;     /* size of message reply data */
    struct timeout_user   *timeout;       /* result timeout */
65 66 67 68
};

struct message
{
69
    struct list            entry;     /* entry in message list */
70
    enum message_type      type;      /* message type */
71
    user_handle_t          win;       /* window handle */
72
    unsigned int           msg;       /* message code */
73 74
    unsigned long          wparam;    /* parameters */
    unsigned long          lparam;    /* parameters */
75
    unsigned long          info;      /* extra info */
76 77
    int                    x;         /* x position */
    int                    y;         /* y position */
78
    unsigned int           time;      /* message time */
79 80
    void                  *data;      /* message data for sent messages */
    unsigned int           data_size; /* size of message data */
81
    unsigned int           unique_id; /* unique id for nested hw message waits */
82 83 84 85 86
    struct message_result *result;    /* result in sender queue */
};

struct timer
{
87
    struct list     entry;     /* entry in timer list */
88
    timeout_t       when;      /* next expiration */
89
    unsigned int    rate;      /* timer rate in ms */
90
    user_handle_t   win;       /* window handle */
91
    unsigned int    msg;       /* message to post */
92
    unsigned long   id;        /* timer id */
93
    unsigned long   lparam;    /* lparam for message */
94 95
};

96 97 98
struct thread_input
{
    struct object          obj;           /* object header */
99
    struct desktop        *desktop;       /* desktop that this thread input belongs to */
100 101 102 103 104 105
    user_handle_t          focus;         /* focus window */
    user_handle_t          capture;       /* capture window */
    user_handle_t          active;        /* active window */
    user_handle_t          menu_owner;    /* current menu owner window */
    user_handle_t          move_size;     /* current moving/resizing window */
    user_handle_t          caret;         /* caret window */
106 107 108
    rectangle_t            caret_rect;    /* caret rectangle */
    int                    caret_hide;    /* caret hide count */
    int                    caret_state;   /* caret on/off state */
109
    struct list            msg_list;      /* list of hardware messages */
110 111 112
    unsigned char          keystate[256]; /* state of each key */
};

113 114
struct msg_queue
{
115
    struct object          obj;             /* object header */
116
    struct fd             *fd;              /* optional file descriptor to poll */
117 118 119 120 121
    unsigned int           wake_bits;       /* wakeup bits */
    unsigned int           wake_mask;       /* wakeup mask */
    unsigned int           changed_bits;    /* changed wakeup bits */
    unsigned int           changed_mask;    /* changed wakeup mask */
    int                    paint_count;     /* pending paint messages count */
122 123
    int                    quit_message;    /* is there a pending quit message? */
    int                    exit_code;       /* exit code of pending quit message */
124
    struct list            msg_list[NB_MSG_KINDS];  /* lists of messages */
125 126 127
    struct list            send_result;     /* stack of sent messages waiting for result */
    struct list            callback_result; /* list of callback messages waiting for result */
    struct message_result *recv_result;     /* stack of received messages waiting for result */
128 129
    struct list            pending_timers;  /* list of pending timers */
    struct list            expired_timers;  /* list of expired timers */
130
    unsigned long          next_timer_id;   /* id for the next timer with a 0 window */
131 132 133
    struct timeout_user   *timeout;         /* timeout for next timer to expire */
    struct thread_input   *input;           /* thread input descriptor */
    struct hook_table     *hooks;           /* hook table */
134
    timeout_t              last_get_msg;    /* time of last get message call */
135 136 137 138 139 140 141
};

static void msg_queue_dump( struct object *obj, int verbose );
static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
static int msg_queue_signaled( struct object *obj, struct thread *thread );
static int msg_queue_satisfied( struct object *obj, struct thread *thread );
142
static void msg_queue_destroy( struct object *obj );
143
static void msg_queue_poll_event( struct fd *fd, int event );
144 145
static void thread_input_dump( struct object *obj, int verbose );
static void thread_input_destroy( struct object *obj );
146
static void timer_callback( void *private );
147 148 149 150 151 152 153 154 155

static const struct object_ops msg_queue_ops =
{
    sizeof(struct msg_queue),  /* size */
    msg_queue_dump,            /* dump */
    msg_queue_add_queue,       /* add_queue */
    msg_queue_remove_queue,    /* remove_queue */
    msg_queue_signaled,        /* signaled */
    msg_queue_satisfied,       /* satisfied */
156
    no_signal,                 /* signal */
157
    no_get_fd,                 /* get_fd */
158
    no_map_access,             /* map_access */
159 160
    default_get_sd,            /* get_sd */
    default_set_sd,            /* set_sd */
161
    no_lookup_name,            /* lookup_name */
162
    no_open_file,              /* open_file */
163
    no_close_handle,           /* close_handle */
164
    msg_queue_destroy          /* destroy */
165 166
};

167 168 169 170
static const struct fd_ops msg_queue_fd_ops =
{
    NULL,                        /* get_poll_events */
    msg_queue_poll_event,        /* poll_event */
171 172
    NULL,                        /* flush */
    NULL,                        /* get_fd_type */
173
    NULL,                        /* ioctl */
174
    NULL,                        /* queue_async */
175
    NULL,                        /* reselect_async */
176
    NULL                         /* cancel async */
177 178
};

179

180 181 182 183 184 185 186 187
static const struct object_ops thread_input_ops =
{
    sizeof(struct thread_input),  /* size */
    thread_input_dump,            /* dump */
    no_add_queue,                 /* add_queue */
    NULL,                         /* remove_queue */
    NULL,                         /* signaled */
    NULL,                         /* satisfied */
188
    no_signal,                    /* signal */
189
    no_get_fd,                    /* get_fd */
190
    no_map_access,                /* map_access */
191 192
    default_get_sd,               /* get_sd */
    default_set_sd,               /* set_sd */
193
    no_lookup_name,               /* lookup_name */
194
    no_open_file,                 /* open_file */
195
    no_close_handle,              /* close_handle */
196 197 198
    thread_input_destroy          /* destroy */
};

199 200
/* pointer to input structure of foreground thread */
static struct thread_input *foreground_input;
201
static unsigned int last_input_time;
202

203
static void free_message( struct message *msg );
204 205 206 207

/* set the caret window in a given thread input */
static void set_caret_window( struct thread_input *input, user_handle_t win )
{
208 209 210 211 212 213 214
    if (!win || win != input->caret)
    {
        input->caret_rect.left   = 0;
        input->caret_rect.top    = 0;
        input->caret_rect.right  = 0;
        input->caret_rect.bottom = 0;
    }
215 216 217 218 219
    input->caret             = win;
    input->caret_hide        = 1;
    input->caret_state       = 0;
}

220
/* create a thread input object */
221
static struct thread_input *create_thread_input( struct thread *thread )
222 223 224
{
    struct thread_input *input;

225
    if ((input = alloc_object( &thread_input_ops )))
226 227 228 229 230 231
    {
        input->focus       = 0;
        input->capture     = 0;
        input->active      = 0;
        input->menu_owner  = 0;
        input->move_size   = 0;
232
        list_init( &input->msg_list );
233
        set_caret_window( input, 0 );
234
        memset( input->keystate, 0, sizeof(input->keystate) );
235 236 237 238 239 240

        if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
        {
            release_object( input );
            return NULL;
        }
241 242 243 244
    }
    return input;
}

245
/* release the thread input data of a given thread */
246
static inline void release_thread_input( struct thread *thread )
247 248 249 250 251 252 253 254
{
    struct thread_input *input = thread->queue->input;

    if (!input) return;
    release_object( input );
    thread->queue->input = NULL;
}

255 256
/* create a message queue object */
static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
257 258
{
    struct msg_queue *queue;
259
    int i;
260

261
    if (!input && !(input = create_thread_input( thread ))) return NULL;
262
    if ((queue = alloc_object( &msg_queue_ops )))
263
    {
264
        queue->fd              = NULL;
265 266 267 268
        queue->wake_bits       = 0;
        queue->wake_mask       = 0;
        queue->changed_bits    = 0;
        queue->changed_mask    = 0;
269
        queue->paint_count     = 0;
270
        queue->quit_message    = 0;
271
        queue->recv_result     = NULL;
272
        queue->next_timer_id   = 1;
273
        queue->timeout         = NULL;
274
        queue->input           = (struct thread_input *)grab_object( input );
275
        queue->hooks           = NULL;
276
        queue->last_get_msg    = current_time;
277 278
        list_init( &queue->send_result );
        list_init( &queue->callback_result );
279 280
        list_init( &queue->pending_timers );
        list_init( &queue->expired_timers );
281
        for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
282

283 284 285 286
        thread->queue = queue;
        if (!thread->process->queue)
            thread->process->queue = (struct msg_queue *)grab_object( queue );
    }
287
    release_object( input );
288 289 290
    return queue;
}

291 292 293 294 295
/* free the message queue of a thread at thread exit */
void free_msg_queue( struct thread *thread )
{
    struct process *process = thread->process;

296
    remove_thread_hooks( thread );
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
    if (!thread->queue) return;
    if (process->queue == thread->queue)  /* is it the process main queue? */
    {
        release_object( process->queue );
        process->queue = NULL;
        if (process->idle_event)
        {
            set_event( process->idle_event );
            release_object( process->idle_event );
            process->idle_event = NULL;
        }
    }
    release_object( thread->queue );
    thread->queue = NULL;
}

313 314 315 316 317 318 319 320 321 322 323
/* get the hook table for a given thread */
struct hook_table *get_queue_hooks( struct thread *thread )
{
    if (!thread->queue) return NULL;
    return thread->queue->hooks;
}

/* set the hook table for a given thread, allocating the queue if needed */
void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
{
    struct msg_queue *queue = thread->queue;
324
    if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
325 326 327 328
    if (queue->hooks) release_object( queue->hooks );
    queue->hooks = hooks;
}

329
/* check the queue status */
330
static inline int is_signaled( struct msg_queue *queue )
331 332 333 334
{
    return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
}

335
/* set some queue bits */
336
static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
337
{
338 339
    queue->wake_bits |= bits;
    queue->changed_bits |= bits;
340 341 342
    if (is_signaled( queue )) wake_up( &queue->obj, 0 );
}

343
/* clear some queue bits */
344
static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
345 346 347 348 349
{
    queue->wake_bits &= ~bits;
    queue->changed_bits &= ~bits;
}

350
/* check whether msg is a keyboard message */
351
static inline int is_keyboard_msg( struct message *msg )
352 353 354 355
{
    return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
}

356
/* check if message is matched by the filter */
357
static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
358 359 360 361
{
    return (msg >= first && msg <= last);
}

362
/* check whether a message filter contains at least one potential hardware message */
363
static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
364 365 366 367 368 369 370 371 372 373 374 375 376
{
    /* hardware message ranges are (in numerical order):
     *   WM_NCMOUSEFIRST .. WM_NCMOUSELAST
     *   WM_KEYFIRST .. WM_KEYLAST
     *   WM_MOUSEFIRST .. WM_MOUSELAST
     */
    if (last < WM_NCMOUSEFIRST) return 0;
    if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
    if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
    if (first > WM_MOUSELAST) return 0;
    return 1;
}

377
/* get the QS_* bit corresponding to a given hardware message */
378
static inline int get_hardware_msg_bit( struct message *msg )
379 380
{
    if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
381
    if (is_keyboard_msg( msg )) return QS_KEY;
382 383 384
    return QS_MOUSEBUTTON;
}

385
/* get the current thread queue, creating it if needed */
386
static inline struct msg_queue *get_current_queue(void)
387 388
{
    struct msg_queue *queue = current->queue;
389
    if (!queue) queue = create_msg_queue( current, NULL );
390 391 392
    return queue;
}

393
/* get a (pseudo-)unique id to tag hardware messages */
394
static inline unsigned int get_unique_id(void)
395 396 397 398 399 400
{
    static unsigned int id;
    if (!++id) id = 1;  /* avoid an id of 0 */
    return id;
}

401
/* try to merge a message with the last in the list; return 1 if successful */
402
static int merge_message( struct thread_input *input, const struct message *msg )
403
{
404 405
    struct message *prev;
    struct list *ptr = list_tail( &input->msg_list );
406

407 408
    if (!ptr) return 0;
    prev = LIST_ENTRY( ptr, struct message, entry );
409 410 411 412 413 414 415 416 417 418 419 420 421 422
    if (prev->result) return 0;
    if (prev->win != msg->win) return 0;
    if (prev->msg != msg->msg) return 0;
    if (prev->type != msg->type) return 0;
    /* now we can merge it */
    prev->wparam  = msg->wparam;
    prev->lparam  = msg->lparam;
    prev->x       = msg->x;
    prev->y       = msg->y;
    prev->time    = msg->time;
    prev->info    = msg->info;
    return 1;
}

423 424 425 426
/* free a result structure */
static void free_result( struct message_result *result )
{
    if (result->timeout) remove_timeout_user( result->timeout );
427
    free( result->data );
428
    if (result->callback_msg) free_message( result->callback_msg );
429 430 431
    free( result );
}

432 433 434 435 436 437 438 439 440 441
/* remove the result from the sender list it is on */
static inline void remove_result_from_sender( struct message_result *result )
{
    assert( result->sender );

    list_remove( &result->sender_entry );
    result->sender = NULL;
    if (!result->receiver) free_result( result );
}

442
/* store the message result in the appropriate structure */
443
static void store_message_result( struct message_result *res, unsigned long result,
444 445 446 447 448 449 450 451 452 453
                                  unsigned int error )
{
    res->result  = result;
    res->error   = error;
    res->replied = 1;
    if (res->timeout)
    {
        remove_timeout_user( res->timeout );
        res->timeout = NULL;
    }
454 455 456 457 458
    if (res->sender)
    {
        if (res->callback_msg)
        {
            /* queue the callback message in the sender queue */
459 460
            struct callback_msg_data *data = res->callback_msg->data;
            data->result = result;
461
            list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
462 463 464 465 466 467 468 469 470 471 472 473
            set_queue_bits( res->sender, QS_SENDMESSAGE );
            res->callback_msg = NULL;
            remove_result_from_sender( res );
        }
        else
        {
            /* wake sender queue if waiting on this result */
            if (list_head(&res->sender->send_result) == &res->sender_entry)
                set_queue_bits( res->sender, QS_SMRESULT );
        }
    }

474 475
}

476 477 478 479 480 481
/* free a message when deleting a queue or window */
static void free_message( struct message *msg )
{
    struct message_result *result = msg->result;
    if (result)
    {
482
        result->msg = NULL;
483 484 485
        if (result->sender)
        {
            result->receiver = NULL;
486
            store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
487
        }
488
        else free_result( result );
489
    }
490
    free( msg->data );
491 492 493
    free( msg );
}

494 495 496
/* remove (and free) a message from a message list */
static void remove_queue_message( struct msg_queue *queue, struct message *msg,
                                  enum message_kind kind )
497
{
498
    list_remove( &msg->entry );
499 500 501
    switch(kind)
    {
    case SEND_MESSAGE:
502
        if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
503 504
        break;
    case POST_MESSAGE:
505 506
        if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
            clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
507 508
        break;
    }
509 510 511
    free_message( msg );
}

512 513 514 515 516 517 518 519
/* message timed out without getting a reply */
static void result_timeout( void *private )
{
    struct message_result *result = private;

    assert( !result->replied );

    result->timeout = NULL;
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535

    if (result->msg)  /* not received yet */
    {
        struct message *msg = result->msg;

        result->msg = NULL;
        msg->result = NULL;
        remove_queue_message( result->receiver, msg, SEND_MESSAGE );
        result->receiver = NULL;
        if (!result->sender)
        {
            free_result( result );
            return;
        }
    }

536 537 538 539 540 541
    store_message_result( result, 0, STATUS_TIMEOUT );
}

/* allocate and fill a message result structure */
static struct message_result *alloc_message_result( struct msg_queue *send_queue,
                                                    struct msg_queue *recv_queue,
542
                                                    struct message *msg, timeout_t timeout )
543 544
{
    struct message_result *result = mem_alloc( sizeof(*result) );
545 546
    if (result)
    {
547
        result->msg       = msg;
548 549 550 551 552 553
        result->sender    = send_queue;
        result->receiver  = recv_queue;
        result->replied   = 0;
        result->data      = NULL;
        result->data_size = 0;
        result->timeout   = NULL;
554 555 556 557

        if (msg->type == MSG_CALLBACK)
        {
            struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
558

559 560 561 562 563 564 565 566
            if (!callback_msg)
            {
                free( result );
                return NULL;
            }
            callback_msg->type      = MSG_CALLBACK_RESULT;
            callback_msg->win       = msg->win;
            callback_msg->msg       = msg->msg;
567
            callback_msg->wparam    = 0;
568 569 570 571
            callback_msg->lparam    = 0;
            callback_msg->time      = get_tick_count();
            callback_msg->x         = 0;
            callback_msg->y         = 0;
572
            callback_msg->info      = 0;
573
            callback_msg->result    = NULL;
574 575 576 577 578
            /* steal the data from the original message */
            callback_msg->data      = msg->data;
            callback_msg->data_size = msg->data_size;
            msg->data = NULL;
            msg->data_size = 0;
579 580 581 582 583 584 585 586 587 588

            result->callback_msg = callback_msg;
            list_add_head( &send_queue->callback_result, &result->sender_entry );
        }
        else
        {
            result->callback_msg = NULL;
            list_add_head( &send_queue->send_result, &result->sender_entry );
        }

589 590
        if (timeout != TIMEOUT_INFINITE)
            result->timeout = add_timeout_user( timeout, result_timeout, result );
591 592
    }
    return result;
593 594 595
}

/* receive a message, removing it from the sent queue */
596 597
static void receive_message( struct msg_queue *queue, struct message *msg,
                             struct get_message_reply *reply )
598 599 600
{
    struct message_result *result = msg->result;

601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
    reply->total = msg->data_size;
    if (msg->data_size > get_reply_max_size())
    {
        set_error( STATUS_BUFFER_OVERFLOW );
        return;
    }
    reply->type   = msg->type;
    reply->win    = msg->win;
    reply->msg    = msg->msg;
    reply->wparam = msg->wparam;
    reply->lparam = msg->lparam;
    reply->x      = msg->x;
    reply->y      = msg->y;
    reply->time   = msg->time;
    reply->info   = msg->info;

    if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );

619
    list_remove( &msg->entry );
620
    /* put the result on the receiver result stack */
621 622
    if (result)
    {
623
        result->msg = NULL;
624 625 626
        result->recv_next  = queue->recv_result;
        queue->recv_result = result;
    }
627
    free( msg );
628
    if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
629 630 631
}

/* set the result of the current received message */
632
static void reply_message( struct msg_queue *queue, unsigned long result,
633
                           unsigned int error, int remove, const void *data, data_size_t len )
634 635 636 637 638 639 640 641 642
{
    struct message_result *res = queue->recv_result;

    if (remove)
    {
        queue->recv_result = res->recv_next;
        res->receiver = NULL;
        if (!res->sender)  /* no one waiting for it */
        {
643
            free_result( res );
644 645 646 647 648
            return;
        }
    }
    if (!res->replied)
    {
649 650
        if (len && (res->data = memdup( data, len ))) res->data_size = len;
        store_message_result( res, result, error );
651 652 653
    }
}

654 655 656 657 658 659 660 661
/* retrieve a posted message */
static int get_posted_message( struct msg_queue *queue, user_handle_t win,
                               unsigned int first, unsigned int last, unsigned int flags,
                               struct get_message_reply *reply )
{
    struct message *msg;

    /* check against the filters */
662
    LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
663 664
    {
        if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
665
        if (!check_msg_filter( msg->msg, first, last )) continue;
666
        goto found; /* found one */
667
    }
668
    return 0;
669 670

    /* return it to the app */
671
found:
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
    reply->total = msg->data_size;
    if (msg->data_size > get_reply_max_size())
    {
        set_error( STATUS_BUFFER_OVERFLOW );
        return 1;
    }
    reply->type   = msg->type;
    reply->win    = msg->win;
    reply->msg    = msg->msg;
    reply->wparam = msg->wparam;
    reply->lparam = msg->lparam;
    reply->x      = msg->x;
    reply->y      = msg->y;
    reply->time   = msg->time;
    reply->info   = msg->info;

688
    if (flags & PM_REMOVE)
689 690 691 692 693 694 695 696 697 698 699 700 701 702
    {
        if (msg->data)
        {
            set_reply_data_ptr( msg->data, msg->data_size );
            msg->data = NULL;
            msg->data_size = 0;
        }
        remove_queue_message( queue, msg, POST_MESSAGE );
    }
    else if (msg->data) set_reply_data( msg->data, msg->data_size );

    return 1;
}

703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
static int get_quit_message( struct msg_queue *queue, unsigned int flags,
                             struct get_message_reply *reply )
{
    if (queue->quit_message)
    {
        reply->total  = 0;
        reply->type   = MSG_POSTED;
        reply->win    = NULL;
        reply->msg    = WM_QUIT;
        reply->wparam = queue->exit_code;
        reply->lparam = 0;
        reply->x      = 0;
        reply->y      = 0;
        reply->time   = get_tick_count();
        reply->info   = 0;

719
        if (flags & PM_REMOVE)
720
        {
721
            queue->quit_message = 0;
722 723 724
            if (list_empty( &queue->msg_list[POST_MESSAGE] ))
                clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
        }
725 726 727 728 729 730
        return 1;
    }
    else
        return 0;
}

731
/* empty a message list and free all the messages */
732
static void empty_msg_list( struct list *list )
733
{
734 735 736
    struct list *ptr;

    while ((ptr = list_head( list )) != NULL)
737
    {
738 739
        struct message *msg = LIST_ENTRY( ptr, struct message, entry );
        list_remove( &msg->entry );
740 741 742 743 744 745 746
        free_message( msg );
    }
}

/* cleanup all pending results when deleting a queue */
static void cleanup_results( struct msg_queue *queue )
{
747 748 749 750 751 752
    struct list *entry;

    while ((entry = list_head( &queue->send_result )) != NULL)
    {
        remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
    }
753

754
    while ((entry = list_head( &queue->callback_result )) != NULL)
755
    {
756
        remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
757 758
    }

759 760
    while (queue->recv_result)
        reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
761 762
}

763 764 765 766 767
/* check if the thread owning the queue is hung (not checking for messages) */
static int is_queue_hung( struct msg_queue *queue )
{
    struct wait_queue_entry *entry;

768
    if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
769 770
        return 0;  /* less than 5 seconds since last get message -> not hung */

771
    LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
772 773 774 775 776 777 778
    {
        if (entry->thread->queue == queue)
            return 0;  /* thread is waiting on queue -> not hung */
    }
    return 1;
}

779 780 781 782 783
static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
{
    struct msg_queue *queue = (struct msg_queue *)obj;
    struct process *process = entry->thread->process;

784 785 786 787 788 789
    /* a thread can only wait on its own queue */
    if (entry->thread->queue != queue)
    {
        set_error( STATUS_ACCESS_DENIED );
        return 0;
    }
790
    /* if waiting on the main process queue, set the idle event */
791
    if (process->queue == queue)
792 793 794
    {
        if (process->idle_event) set_event( process->idle_event );
    }
795 796
    if (queue->fd && list_empty( &obj->wait_queue ))  /* first on the queue */
        set_fd_events( queue->fd, POLLIN );
797 798 799 800 801 802 803 804 805 806
    add_queue( obj, entry );
    return 1;
}

static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
{
    struct msg_queue *queue = (struct msg_queue *)obj;
    struct process *process = entry->thread->process;

    remove_queue( obj, entry );
807 808
    if (queue->fd && list_empty( &obj->wait_queue ))  /* last on the queue is gone */
        set_fd_events( queue->fd, 0 );
809

810 811
    assert( entry->thread->queue == queue );

812
    /* if waiting on the main process queue, reset the idle event */
813
    if (process->queue == queue)
814 815 816 817 818 819 820 821
    {
        if (process->idle_event) reset_event( process->idle_event );
    }
}

static void msg_queue_dump( struct object *obj, int verbose )
{
    struct msg_queue *queue = (struct msg_queue *)obj;
822 823
    fprintf( stderr, "Msg queue bits=%x mask=%x\n",
             queue->wake_bits, queue->wake_mask );
824 825 826 827 828
}

static int msg_queue_signaled( struct object *obj, struct thread *thread )
{
    struct msg_queue *queue = (struct msg_queue *)obj;
829 830 831 832 833 834 835 836 837 838 839 840
    int ret = 0;

    if (queue->fd)
    {
        if ((ret = check_fd_events( queue->fd, POLLIN )))
            /* stop waiting on select() if we are signaled */
            set_fd_events( queue->fd, 0 );
        else if (!list_empty( &obj->wait_queue ))
            /* restart waiting on poll() if we are no longer signaled */
            set_fd_events( queue->fd, POLLIN );
    }
    return ret || is_signaled( queue );
841 842 843 844 845
}

static int msg_queue_satisfied( struct object *obj, struct thread *thread )
{
    struct msg_queue *queue = (struct msg_queue *)obj;
846 847
    queue->wake_mask = 0;
    queue->changed_mask = 0;
848 849 850
    return 0;  /* Not abandoned */
}

851 852 853
static void msg_queue_destroy( struct object *obj )
{
    struct msg_queue *queue = (struct msg_queue *)obj;
854
    struct list *ptr;
855
    int i;
856 857

    cleanup_results( queue );
858
    for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
859

860
    while ((ptr = list_head( &queue->pending_timers )))
861
    {
862 863 864 865 866 867 868 869
        struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
        list_remove( &timer->entry );
        free( timer );
    }
    while ((ptr = list_head( &queue->expired_timers )))
    {
        struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
        list_remove( &timer->entry );
870 871 872
        free( timer );
    }
    if (queue->timeout) remove_timeout_user( queue->timeout );
873
    if (queue->input) release_object( queue->input );
874
    if (queue->hooks) release_object( queue->hooks );
875 876 877 878 879 880 881 882 883 884
    if (queue->fd) release_object( queue->fd );
}

static void msg_queue_poll_event( struct fd *fd, int event )
{
    struct msg_queue *queue = get_fd_user( fd );
    assert( queue->obj.ops == &msg_queue_ops );

    if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
    wake_up( &queue->obj, 0 );
885 886 887 888 889
}

static void thread_input_dump( struct object *obj, int verbose )
{
    struct thread_input *input = (struct thread_input *)obj;
890
    fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
891 892 893 894 895 896 897 898
             input->focus, input->capture, input->active );
}

static void thread_input_destroy( struct object *obj )
{
    struct thread_input *input = (struct thread_input *)obj;

    if (foreground_input == input) foreground_input = NULL;
899
    empty_msg_list( &input->msg_list );
900
    if (input->desktop) release_object( input->desktop );
901 902 903
}

/* fix the thread input data when a window is destroyed */
904
static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
905 906 907 908 909 910 911 912
{
    struct thread_input *input = queue->input;

    if (window == input->focus) input->focus = 0;
    if (window == input->capture) input->capture = 0;
    if (window == input->active) input->active = 0;
    if (window == input->menu_owner) input->menu_owner = 0;
    if (window == input->move_size) input->move_size = 0;
913
    if (window == input->caret) set_caret_window( input, 0 );
914 915
}

916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
/* check if the specified window can be set in the input data of a given queue */
static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
{
    struct thread *thread;
    int ret = 0;

    if (!window) return 1;  /* we can always clear the data */

    if ((thread = get_window_thread( window )))
    {
        ret = (queue->input == thread->queue->input);
        if (!ret) set_error( STATUS_ACCESS_DENIED );
        release_object( thread );
    }
    else set_error( STATUS_INVALID_HANDLE );

    return ret;
}

935 936 937 938 939 940 941
/* make sure the specified thread has a queue */
int init_thread_queue( struct thread *thread )
{
    if (thread->queue) return 1;
    return (create_msg_queue( thread, NULL ) != NULL);
}

942 943 944
/* attach two thread input data structures */
int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
{
945
    struct desktop *desktop;
946 947 948
    struct thread_input *input;

    if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
949
    if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
950
    input = (struct thread_input *)grab_object( thread_to->queue->input );
951 952 953 954 955 956 957 958
    if (input->desktop != desktop)
    {
        set_error( STATUS_ACCESS_DENIED );
        release_object( input );
        release_object( desktop );
        return 0;
    }
    release_object( desktop );
959 960 961

    if (thread_from->queue)
    {
962
        release_thread_input( thread_from );
963 964 965 966 967 968 969 970
        thread_from->queue->input = input;
    }
    else
    {
        if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
    }
    memset( input->keystate, 0, sizeof(input->keystate) );
    return 1;
971 972
}

973
/* detach two thread input data structures */
974
void detach_thread_input( struct thread *thread_from )
975 976 977
{
    struct thread_input *input;

978
    if ((input = create_thread_input( thread_from )))
979
    {
980
        release_thread_input( thread_from );
981 982 983 984 985
        thread_from->queue->input = input;
    }
}


986
/* set the next timer to expire */
987
static void set_next_timer( struct msg_queue *queue )
988
{
989 990
    struct list *ptr;

991 992 993 994 995
    if (queue->timeout)
    {
        remove_timeout_user( queue->timeout );
        queue->timeout = NULL;
    }
996 997 998
    if ((ptr = list_head( &queue->pending_timers )))
    {
        struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
999
        queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1000
    }
1001
    /* set/clear QS_TIMER bit */
1002
    if (list_empty( &queue->expired_timers ))
1003
        clear_queue_bits( queue, QS_TIMER );
1004
    else
1005
        set_queue_bits( queue, QS_TIMER );
1006 1007
}

1008 1009
/* find a timer from its window and id */
static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1010
                                 unsigned int msg, unsigned long id )
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
{
    struct list *ptr;

    /* we need to search both lists */

    LIST_FOR_EACH( ptr, &queue->pending_timers )
    {
        struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
        if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
    }
    LIST_FOR_EACH( ptr, &queue->expired_timers )
    {
        struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
        if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
    }
    return NULL;
}

1029 1030 1031 1032
/* callback for the next timer expiration */
static void timer_callback( void *private )
{
    struct msg_queue *queue = private;
1033
    struct list *ptr;
1034 1035 1036

    queue->timeout = NULL;
    /* move on to the next timer */
1037 1038 1039 1040
    ptr = list_head( &queue->pending_timers );
    list_remove( ptr );
    list_add_tail( &queue->expired_timers, ptr );
    set_next_timer( queue );
1041 1042 1043 1044 1045
}

/* link a timer at its rightful place in the queue list */
static void link_timer( struct msg_queue *queue, struct timer *timer )
{
1046
    struct list *ptr;
1047

1048
    for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1049
    {
1050
        struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1051
        if (t->when >= timer->when) break;
1052
    }
1053
    list_add_before( ptr, &timer->entry );
1054 1055
}

1056 1057
/* remove a timer from the queue timer list and free it */
static void free_timer( struct msg_queue *queue, struct timer *timer )
1058
{
1059 1060 1061
    list_remove( &timer->entry );
    free( timer );
    set_next_timer( queue );
1062 1063 1064 1065 1066
}

/* restart an expired timer */
static void restart_timer( struct msg_queue *queue, struct timer *timer )
{
1067
    list_remove( &timer->entry );
1068
    while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1069
    link_timer( queue, timer );
1070
    set_next_timer( queue );
1071 1072 1073
}

/* find an expired timer matching the filtering parameters */
1074
static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1075 1076 1077
                                         unsigned int get_first, unsigned int get_last,
                                         int remove )
{
1078 1079 1080
    struct list *ptr;

    LIST_FOR_EACH( ptr, &queue->expired_timers )
1081
    {
1082
        struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1083
        if (win && timer->win != win) continue;
1084
        if (check_msg_filter( timer->msg, get_first, get_last ))
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
        {
            if (remove) restart_timer( queue, timer );
            return timer;
        }
    }
    return NULL;
}

/* add a timer */
static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
{
    struct timer *timer = mem_alloc( sizeof(*timer) );
    if (timer)
    {
1099
        timer->rate = max( rate, 1 );
1100
        timer->when = current_time + (timeout_t)timer->rate * 10000;
1101
        link_timer( queue, timer );
1102 1103
        /* check if we replaced the next timer */
        if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1104 1105 1106 1107
    }
    return timer;
}

1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
/* change the input key state for a given key */
static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
{
    if (down)
    {
        if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
        input->keystate[key] |= 0x80;
    }
    else input->keystate[key] &= ~0x80;
}

/* update the input key state for a keyboard message */
static void update_input_key_state( struct thread_input *input, const struct message *msg )
{
    unsigned char key;
    int down = 0, extended;

    switch (msg->msg)
    {
    case WM_LBUTTONDOWN:
        down = 1;
        /* fall through */
    case WM_LBUTTONUP:
        set_input_key_state( input, VK_LBUTTON, down );
        break;
    case WM_MBUTTONDOWN:
        down = 1;
        /* fall through */
    case WM_MBUTTONUP:
        set_input_key_state( input, VK_MBUTTON, down );
        break;
    case WM_RBUTTONDOWN:
        down = 1;
        /* fall through */
    case WM_RBUTTONUP:
        set_input_key_state( input, VK_RBUTTON, down );
        break;
1145 1146 1147 1148 1149 1150 1151
    case WM_XBUTTONDOWN:
        down = 1;
        /* fall through */
    case WM_XBUTTONUP:
        if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
        else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
        break;
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
    case WM_KEYDOWN:
    case WM_SYSKEYDOWN:
        down = 1;
        /* fall through */
    case WM_KEYUP:
    case WM_SYSKEYUP:
        key = (unsigned char)msg->wparam;
        extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
        set_input_key_state( input, key, down );
        switch(key)
        {
        case VK_SHIFT:
            set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
            break;
        case VK_CONTROL:
            set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
            break;
        case VK_MENU:
            set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
            break;
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
        case VK_LCONTROL:
        case VK_RCONTROL:
            set_input_key_state( input, VK_CONTROL, down );
            break;
        case VK_LMENU:
        case VK_RMENU:
            set_input_key_state( input, VK_MENU, down );
            break;
        case VK_LSHIFT:
        case VK_RSHIFT:
            set_input_key_state( input, VK_SHIFT, down );
            break;
1184 1185 1186 1187 1188
        }
        break;
    }
}

1189
/* release the hardware message currently being processed by the given thread */
1190 1191
static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
                                      int remove, user_handle_t new_win )
1192
{
1193 1194
    struct thread_input *input = queue->input;
    struct message *msg;
1195

1196 1197 1198 1199 1200
    LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
    {
        if (msg->unique_id == hw_id) break;
    }
    if (&msg->entry == &input->msg_list) return;  /* not found */
1201 1202 1203

    /* clear the queue bit for that message */
    if (remove || new_win)
1204 1205 1206 1207
    {
        struct message *other;
        int clr_bit;

1208
        clr_bit = get_hardware_msg_bit( msg );
1209 1210
        LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
        {
1211
            if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1212 1213 1214 1215 1216
            {
                clr_bit = 0;
                break;
            }
        }
1217
        if (clr_bit) clear_queue_bits( queue, clr_bit );
1218 1219 1220 1221 1222 1223 1224
    }

    if (new_win)  /* set the new window */
    {
        struct thread *owner = get_window_thread( new_win );
        if (owner)
        {
1225 1226 1227 1228 1229 1230
            if (owner->queue->input == input)
            {
                msg->win = new_win;
                set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
                remove = 0;
            }
1231 1232 1233
            release_object( owner );
        }
    }
1234
    if (remove)
1235
    {
1236 1237 1238
        update_input_key_state( input, msg );
        list_remove( &msg->entry );
        free_message( msg );
1239 1240 1241 1242
    }
}

/* find the window that should receive a given hardware message */
1243 1244
static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
                                                   unsigned int *msg_code )
1245 1246 1247
{
    user_handle_t win = 0;

1248
    *msg_code = msg->msg;
1249 1250
    if (is_keyboard_msg( msg ))
    {
1251 1252 1253 1254 1255
        if (input && !(win = input->focus))
        {
            win = input->active;
            if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
        }
1256 1257 1258 1259 1260
    }
    else  /* mouse message */
    {
        if (!input || !(win = input->capture))
        {
1261
            if (!(win = msg->win) || !is_window_visible( win ))
1262 1263 1264
            {
                if (input) win = window_from_point( input->desktop, msg->x, msg->y );
            }
1265 1266 1267 1268 1269 1270 1271 1272 1273
        }
    }
    return win;
}

/* queue a hardware message into a given thread input */
static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
{
    user_handle_t win;
1274
    struct thread *thread;
1275
    struct thread_input *input = queue ? queue->input : foreground_input;
1276
    unsigned int msg_code;
1277

1278
    last_input_time = get_tick_count();
1279
    win = find_hardware_message_window( input, msg, &msg_code );
1280
    if (!win || !(thread = get_window_thread(win)))
1281
    {
1282
        if (input) update_input_key_state( input, msg );
1283 1284 1285
        free( msg );
        return;
    }
1286
    input = thread->queue->input;
1287 1288 1289 1290

    if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
    else
    {
1291
        msg->unique_id = 0;  /* will be set once we return it to the app */
1292
        list_add_tail( &input->msg_list, &msg->entry );
1293 1294
        set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
    }
1295
    release_object( thread );
1296 1297
}

1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
/* check message filter for a hardware message */
static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
                                    user_handle_t filter_win, unsigned int first, unsigned int last )
{
    if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
    {
        /* we can only test the window for a keyboard message since the
         * dest window for a mouse message depends on hittest */
        if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
            return 0;
        /* the message code is final for a keyboard message, we can simply check it */
        return check_msg_filter( msg_code, first, last );
    }
    else  /* mouse message */
    {
        /* we need to check all possible values that the message can have in the end */

        if (check_msg_filter( msg_code, first, last )) return 1;
        if (msg_code == WM_MOUSEWHEEL) return 0;  /* no other possible value for this one */

        /* all other messages can become non-client messages */
        if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;

        /* clicks can become double-clicks or non-client double-clicks */
        if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
            msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
        {
            if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
            if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
        }
        return 0;
    }
}


1333
/* find a hardware message for the given queue */
1334
static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1335
                                 unsigned int first, unsigned int last, struct get_message_reply *reply )
1336 1337 1338
{
    struct thread_input *input = thread->queue->input;
    struct thread *win_thread;
1339
    struct list *ptr;
1340 1341
    user_handle_t win;
    int clear_bits, got_one = 0;
1342
    unsigned int msg_code;
1343

1344 1345
    ptr = list_head( &input->msg_list );
    if (hw_id)
1346
    {
1347 1348 1349 1350 1351 1352 1353 1354
        while (ptr)
        {
            struct message *msg = LIST_ENTRY( ptr, struct message, entry );
            if (msg->unique_id == hw_id) break;
            ptr = list_next( &input->msg_list, ptr );
        }
        if (!ptr) ptr = list_head( &input->msg_list );
        else ptr = list_next( &input->msg_list, ptr );  /* start from the next one */
1355
    }
1356 1357 1358

    if (ptr == list_head( &input->msg_list ))
        clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1359 1360 1361
    else
        clear_bits = 0;  /* don't clear bits if we don't go through the whole list */

1362
    while (ptr)
1363
    {
1364
        struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1365
        ptr = list_next( &input->msg_list, ptr );
1366
        win = find_hardware_message_window( input, msg, &msg_code );
1367 1368 1369
        if (!win || !(win_thread = get_window_thread( win )))
        {
            /* no window at all, remove it */
1370
            update_input_key_state( input, msg );
1371
            list_remove( &msg->entry );
1372 1373 1374 1375 1376
            free_message( msg );
            continue;
        }
        if (win_thread != thread)
        {
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
            if (win_thread->queue->input == input)
            {
                /* wake the other thread */
                set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
                got_one = 1;
            }
            else
            {
                /* for another thread input, drop it */
                update_input_key_state( input, msg );
                list_remove( &msg->entry );
                free_message( msg );
            }
1390 1391 1392
            release_object( win_thread );
            continue;
        }
1393 1394
        release_object( win_thread );

1395
        /* if we already got a message for another thread, or if it doesn't
1396 1397
         * match the filter we skip it */
        if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1398 1399 1400 1401 1402
        {
            clear_bits &= ~get_hardware_msg_bit( msg );
            continue;
        }
        /* now we can return it */
1403
        if (!msg->unique_id) msg->unique_id = get_unique_id();
1404 1405
        reply->type   = MSG_HARDWARE;
        reply->win    = win;
1406
        reply->msg    = msg_code;
1407 1408 1409 1410 1411 1412
        reply->wparam = msg->wparam;
        reply->lparam = msg->lparam;
        reply->x      = msg->x;
        reply->y      = msg->y;
        reply->time   = msg->time;
        reply->info   = msg->info;
1413
        reply->hw_id  = msg->unique_id;
1414 1415 1416 1417 1418 1419
        return 1;
    }
    /* nothing found, clear the hardware queue bits */
    clear_queue_bits( thread->queue, clear_bits );
    return 0;
}
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436

/* increment (or decrement if 'incr' is negative) the queue paint count */
void inc_queue_paint_count( struct thread *thread, int incr )
{
    struct msg_queue *queue = thread->queue;

    assert( queue );

    if ((queue->paint_count += incr) < 0) queue->paint_count = 0;

    if (queue->paint_count)
        set_queue_bits( queue, QS_PAINT );
    else
        clear_queue_bits( queue, QS_PAINT );
}


1437
/* remove all messages and timers belonging to a certain window */
1438
void queue_cleanup_window( struct thread *thread, user_handle_t win )
1439
{
1440
    struct msg_queue *queue = thread->queue;
1441
    struct list *ptr;
1442
    int i;
1443

1444 1445
    if (!queue) return;

1446
    /* remove timers */
1447 1448 1449

    ptr = list_head( &queue->pending_timers );
    while (ptr)
1450
    {
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
        struct list *next = list_next( &queue->pending_timers, ptr );
        struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
        if (timer->win == win) free_timer( queue, timer );
        ptr = next;
    }
    ptr = list_head( &queue->expired_timers );
    while (ptr)
    {
        struct list *next = list_next( &queue->expired_timers, ptr );
        struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
        if (timer->win == win) free_timer( queue, timer );
        ptr = next;
1463 1464
    }

1465 1466
    /* remove messages */
    for (i = 0; i < NB_MSG_KINDS; i++)
1467
    {
1468 1469 1470
        struct list *ptr, *next;

        LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1471
        {
1472
            struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1473 1474
            if (msg->win == win) remove_queue_message( queue, msg, i );
        }
1475
    }
1476 1477

    thread_input_cleanup_window( queue, win );
1478 1479
}

1480 1481
/* post a message to a window; used by socket handling */
void post_message( user_handle_t win, unsigned int message,
1482
                   unsigned long wparam, unsigned long lparam )
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
{
    struct message *msg;
    struct thread *thread = get_window_thread( win );

    if (!thread) return;

    if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
    {
        msg->type      = MSG_POSTED;
        msg->win       = get_user_full_handle( win );
        msg->msg       = message;
        msg->wparam    = wparam;
        msg->lparam    = lparam;
        msg->time      = get_tick_count();
        msg->x         = 0;
        msg->y         = 0;
        msg->info      = 0;
        msg->result    = NULL;
        msg->data      = NULL;
        msg->data_size = 0;

1504
        list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1505
        set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1506 1507 1508 1509
    }
    release_object( thread );
}

1510 1511 1512 1513
/* post a win event */
void post_win_event( struct thread *thread, unsigned int event,
                     user_handle_t win, unsigned int object_id,
                     unsigned int child_id, void *hook_proc,
1514
                     const WCHAR *module, data_size_t module_size,
1515 1516 1517 1518 1519 1520
                     user_handle_t hook)
{
    struct message *msg;

    if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
    {
1521 1522
        struct winevent_msg_data *data;

1523 1524 1525 1526 1527 1528 1529 1530
        msg->type      = MSG_WINEVENT;
        msg->win       = get_user_full_handle( win );
        msg->msg       = event;
        msg->wparam    = object_id;
        msg->lparam    = child_id;
        msg->time      = get_tick_count();
        msg->x         = 0;
        msg->y         = 0;
1531
        msg->info      = 0;
1532 1533
        msg->result    = NULL;

1534
        if ((data = malloc( sizeof(*data) + module_size )))
1535
        {
1536 1537 1538 1539 1540 1541 1542
            data->hook = hook;
            data->tid  = get_thread_id( current );
            data->hook_proc = hook_proc;
            memcpy( data + 1, module, module_size );

            msg->data = data;
            msg->data_size = sizeof(*data) + module_size;
1543 1544 1545 1546

            if (debug_level > 1)
                fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
                         get_thread_id(thread), event, win, object_id, child_id );
1547
            list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1548 1549 1550 1551 1552 1553
            set_queue_bits( thread->queue, QS_SENDMESSAGE );
        }
        else
            free( msg );
    }
}
1554

1555 1556 1557
/* get the message queue of the current thread */
DECL_HANDLER(get_msg_queue)
{
1558
    struct msg_queue *queue = get_current_queue();
1559

1560 1561
    reply->handle = 0;
    if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1562 1563
}

1564

1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
/* set the file descriptor associated to the current thread queue */
DECL_HANDLER(set_queue_fd)
{
    struct msg_queue *queue = get_current_queue();
    struct file *file;
    int unix_fd;

    if (queue->fd)  /* fd can only be set once */
    {
        set_error( STATUS_ACCESS_DENIED );
        return;
    }
    if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;

    if ((unix_fd = get_file_unix_fd( file )) != -1)
    {
        if ((unix_fd = dup( unix_fd )) != -1)
1582
            queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
1583 1584 1585 1586 1587 1588 1589
        else
            file_set_error();
    }
    release_object( file );
}


1590 1591 1592 1593 1594 1595 1596 1597 1598
/* set the current message queue wakeup mask */
DECL_HANDLER(set_queue_mask)
{
    struct msg_queue *queue = get_current_queue();

    if (queue)
    {
        queue->wake_mask    = req->wake_mask;
        queue->changed_mask = req->changed_mask;
1599 1600
        reply->wake_bits    = queue->wake_bits;
        reply->changed_bits = queue->changed_bits;
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
        if (is_signaled( queue ))
        {
            /* if skip wait is set, do what would have been done in the subsequent wait */
            if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
            else wake_up( &queue->obj, 0 );
        }
    }
}


/* get the current message queue status */
DECL_HANDLER(get_queue_status)
{
    struct msg_queue *queue = current->queue;
    if (queue)
    {
1617 1618
        reply->wake_bits    = queue->wake_bits;
        reply->changed_bits = queue->changed_bits;
1619 1620
        if (req->clear) queue->changed_bits = 0;
    }
1621
    else reply->wake_bits = reply->changed_bits = 0;
1622 1623 1624 1625 1626 1627 1628 1629
}


/* send a message to a thread queue */
DECL_HANDLER(send_message)
{
    struct message *msg;
    struct msg_queue *send_queue = get_current_queue();
1630 1631
    struct msg_queue *recv_queue = NULL;
    struct thread *thread = NULL;
1632

1633
    if (!(thread = get_thread_from_id( req->id ))) return;
1634

1635
    if (!(recv_queue = thread->queue))
1636 1637 1638 1639 1640
    {
        set_error( STATUS_INVALID_PARAMETER );
        release_object( thread );
        return;
    }
1641
    if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1642 1643 1644 1645 1646
    {
        set_error( STATUS_TIMEOUT );
        release_object( thread );
        return;
    }
1647 1648 1649

    if ((msg = mem_alloc( sizeof(*msg) )))
    {
1650
        msg->type      = req->type;
1651
        msg->win       = get_user_full_handle( req->win );
1652 1653 1654
        msg->msg       = req->msg;
        msg->wparam    = req->wparam;
        msg->lparam    = req->lparam;
1655 1656 1657
        msg->time      = get_tick_count();
        msg->x         = 0;
        msg->y         = 0;
1658
        msg->info      = 0;
1659 1660
        msg->result    = NULL;
        msg->data      = NULL;
1661 1662 1663 1664 1665 1666 1667 1668
        msg->data_size = get_req_data_size();

        if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
        {
            free( msg );
            release_object( thread );
            return;
        }
1669 1670

        switch(msg->type)
1671
        {
1672
        case MSG_OTHER_PROCESS:
1673 1674 1675
        case MSG_ASCII:
        case MSG_UNICODE:
        case MSG_CALLBACK:
1676
            if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
1677
            {
1678
                free_message( msg );
1679
                break;
1680
            }
1681 1682
            /* fall through */
        case MSG_NOTIFY:
1683
            list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1684 1685 1686
            set_queue_bits( recv_queue, QS_SENDMESSAGE );
            break;
        case MSG_POSTED:
1687
            list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1688
            set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1689
            break;
1690
        case MSG_HARDWARE:  /* should use send_hardware_message instead */
1691
        case MSG_CALLBACK_RESULT:  /* cannot send this one */
1692 1693
        default:
            set_error( STATUS_INVALID_PARAMETER );
1694
            free( msg );
1695
            break;
1696 1697
        }
    }
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
    release_object( thread );
}

/* send a hardware message to a thread queue */
DECL_HANDLER(send_hardware_message)
{
    struct message *msg;
    struct msg_queue *recv_queue = NULL;
    struct thread *thread = NULL;

    if (req->id)
    {
        if (!(thread = get_thread_from_id( req->id ))) return;
    }

    if (thread && !(recv_queue = thread->queue))
    {
        set_error( STATUS_INVALID_PARAMETER );
        release_object( thread );
        return;
    }

    if ((msg = mem_alloc( sizeof(*msg) )))
    {
        msg->type      = MSG_HARDWARE;
        msg->win       = get_user_full_handle( req->win );
        msg->msg       = req->msg;
        msg->wparam    = req->wparam;
        msg->lparam    = req->lparam;
        msg->time      = req->time;
        msg->x         = req->x;
        msg->y         = req->y;
        msg->info      = req->info;
        msg->result    = NULL;
        msg->data      = NULL;
        msg->data_size = 0;
        queue_hardware_message( recv_queue, msg );
    }
1736
    if (thread) release_object( thread );
1737 1738
}

1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
/* post a quit message to the current queue */
DECL_HANDLER(post_quit_message)
{
    struct msg_queue *queue = get_current_queue();

    if (!queue)
        return;

    queue->quit_message = 1;
    queue->exit_code = req->exit_code;
    set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
}
1751

1752 1753 1754 1755
/* get a message from the current queue */
DECL_HANDLER(get_message)
{
    struct timer *timer;
1756
    struct list *ptr;
1757
    struct msg_queue *queue = get_current_queue();
1758
    user_handle_t get_win = get_user_full_handle( req->get_win );
1759
    unsigned int filter = req->flags >> 16;
1760

1761 1762
    reply->active_hooks = get_active_hooks();

1763
    if (!queue) return;
1764
    queue->last_get_msg = current_time;
1765
    if (!filter) filter = QS_ALLINPUT;
1766 1767

    /* first check for sent messages */
1768
    if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1769
    {
1770
        struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1771
        receive_message( queue, msg, reply );
1772 1773 1774
        return;
    }

1775
    /* clear changed bits so we can wait on them if we don't find a message */
1776 1777 1778 1779 1780 1781 1782
    if (filter & QS_POSTMESSAGE)
    {
        queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
        if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
    }
    if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
    if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
1783

1784
    /* then check for posted messages */
1785 1786
    if ((filter & QS_POSTMESSAGE) &&
        get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1787 1788
        return;

1789 1790 1791 1792 1793
    /* only check for quit messages if not posted messages pending.
     * note: the quit message isn't filtered */
    if (get_quit_message( queue, req->flags, reply ))
        return;

1794
    /* then check for any raw hardware message */
1795 1796
    if ((filter & QS_INPUT) &&
        filter_contains_hw_range( req->get_first, req->get_last ) &&
1797
        get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1798
        return;
1799 1800

    /* now check for WM_PAINT */
1801 1802
    if ((filter & QS_PAINT) &&
        queue->paint_count &&
1803
        check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
        (reply->win = find_window_to_repaint( get_win, current )))
    {
        reply->type   = MSG_POSTED;
        reply->msg    = WM_PAINT;
        reply->wparam = 0;
        reply->lparam = 0;
        reply->x      = 0;
        reply->y      = 0;
        reply->time   = get_tick_count();
        reply->info   = 0;
1814 1815 1816 1817
        return;
    }

    /* now check for timer */
1818 1819 1820
    if ((filter & QS_TIMER) &&
        (timer = find_expired_timer( queue, get_win, req->get_first,
                                     req->get_last, (req->flags & PM_REMOVE) )))
1821
    {
1822 1823 1824 1825 1826 1827 1828 1829 1830
        reply->type   = MSG_POSTED;
        reply->win    = timer->win;
        reply->msg    = timer->msg;
        reply->wparam = timer->id;
        reply->lparam = timer->lparam;
        reply->x      = 0;
        reply->y      = 0;
        reply->time   = get_tick_count();
        reply->info   = 0;
1831 1832 1833
        return;
    }

1834 1835
    queue->wake_mask = req->wake_mask;
    queue->changed_mask = req->changed_mask;
1836 1837 1838 1839 1840 1841 1842
    set_error( STATUS_PENDING );  /* FIXME */
}


/* reply to a sent message */
DECL_HANDLER(reply_message)
{
1843
    if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1844 1845 1846
    else if (current->queue->recv_result)
        reply_message( current->queue, req->result, 0, req->remove,
                       get_req_data(), get_req_data_size() );
1847 1848 1849
}


1850 1851 1852 1853
/* accept the current hardware message */
DECL_HANDLER(accept_hardware_message)
{
    if (current->queue)
1854 1855 1856
        release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
    else
        set_error( STATUS_ACCESS_DENIED );
1857 1858 1859
}


1860 1861 1862
/* retrieve the reply for the last message sent */
DECL_HANDLER(get_message_reply)
{
1863 1864
    struct message_result *result;
    struct list *entry;
1865
    struct msg_queue *queue = current->queue;
1866

1867 1868 1869
    if (queue)
    {
        set_error( STATUS_PENDING );
1870
        reply->result = 0;
1871

1872 1873 1874 1875
        if (!(entry = list_head( &queue->send_result ))) return;  /* no reply ready */

        result = LIST_ENTRY( entry, struct message_result, sender_entry );
        if (result->replied || req->cancel)
1876
        {
1877 1878
            if (result->replied)
            {
1879
                reply->result = result->result;
1880 1881 1882
                set_error( result->error );
                if (result->data)
                {
1883
                    data_size_t data_len = min( result->data_size, get_reply_max_size() );
1884
                    set_reply_data_ptr( result->data, data_len );
1885 1886 1887 1888
                    result->data = NULL;
                    result->data_size = 0;
                }
            }
1889 1890 1891 1892 1893 1894 1895 1896 1897
            remove_result_from_sender( result );

            entry = list_head( &queue->send_result );
            if (!entry) clear_queue_bits( queue, QS_SMRESULT );
            else
            {
                result = LIST_ENTRY( entry, struct message_result, sender_entry );
                if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
            }
1898 1899
        }
    }
1900
    else set_error( STATUS_ACCESS_DENIED );
1901 1902 1903 1904 1905 1906 1907
}


/* set a window timer */
DECL_HANDLER(set_win_timer)
{
    struct timer *timer;
1908 1909 1910
    struct msg_queue *queue;
    struct thread *thread = NULL;
    user_handle_t win = 0;
1911
    unsigned long id = req->id;
1912

1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
    if (req->win)
    {
        if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
        {
            set_error( STATUS_INVALID_HANDLE );
            return;
        }
        if (thread->process != current->process)
        {
            release_object( thread );
            set_error( STATUS_ACCESS_DENIED );
            return;
        }
        queue = thread->queue;
        /* remove it if it existed already */
        if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
    }
    else
    {
        queue = get_current_queue();
1933 1934
        /* look for a timer with this id */
        if (id && (timer = find_timer( queue, NULL, req->msg, id )))
1935
        {
1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
            /* free and reuse id */
            free_timer( queue, timer );
        }
        else
        {
            /* find a free id for it */
            do
            {
                id = queue->next_timer_id;
                if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
            }
            while (find_timer( queue, 0, req->msg, id ));
1948 1949
        }
    }
1950 1951 1952

    if ((timer = set_timer( queue, req->rate )))
    {
1953
        timer->win    = win;
1954
        timer->msg    = req->msg;
1955
        timer->id     = id;
1956
        timer->lparam = req->lparam;
1957
        reply->id     = id;
1958
    }
1959
    if (thread) release_object( thread );
1960 1961 1962 1963 1964
}

/* kill a window timer */
DECL_HANDLER(kill_win_timer)
{
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
    struct timer *timer;
    struct thread *thread;
    user_handle_t win = 0;

    if (req->win)
    {
        if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
        {
            set_error( STATUS_INVALID_HANDLE );
            return;
        }
        if (thread->process != current->process)
        {
            release_object( thread );
            set_error( STATUS_ACCESS_DENIED );
            return;
        }
    }
    else thread = (struct thread *)grab_object( current );
1984

1985 1986 1987
    if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
        free_timer( thread->queue, timer );
    else
1988
        set_error( STATUS_INVALID_PARAMETER );
1989 1990

    release_object( thread );
1991
}
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008


/* attach (or detach) thread inputs */
DECL_HANDLER(attach_thread_input)
{
    struct thread *thread_from = get_thread_from_id( req->tid_from );
    struct thread *thread_to = get_thread_from_id( req->tid_to );

    if (!thread_from || !thread_to)
    {
        if (thread_from) release_object( thread_from );
        if (thread_to) release_object( thread_to );
        return;
    }
    if (thread_from != thread_to)
    {
        if (req->attach) attach_thread_input( thread_from, thread_to );
2009 2010 2011 2012 2013 2014 2015 2016
        else
        {
            if (thread_from->queue && thread_to->queue &&
                thread_from->queue->input == thread_to->queue->input)
                detach_thread_input( thread_from );
            else
                set_error( STATUS_ACCESS_DENIED );
        }
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
    }
    else set_error( STATUS_ACCESS_DENIED );
    release_object( thread_from );
    release_object( thread_to );
}


/* get thread input data */
DECL_HANDLER(get_thread_input)
{
    struct thread *thread = NULL;
    struct thread_input *input;

    if (req->tid)
    {
        if (!(thread = get_thread_from_id( req->tid ))) return;
        input = thread->queue ? thread->queue->input : NULL;
    }
    else input = foreground_input;  /* get the foreground thread info */

    if (input)
    {
        reply->focus      = input->focus;
        reply->capture    = input->capture;
        reply->active     = input->active;
        reply->menu_owner = input->menu_owner;
        reply->move_size  = input->move_size;
        reply->caret      = input->caret;
2045
        reply->rect       = input->caret_rect;
2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060
    }
    else
    {
        reply->focus      = 0;
        reply->capture    = 0;
        reply->active     = 0;
        reply->menu_owner = 0;
        reply->move_size  = 0;
        reply->caret      = 0;
        reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
    }
    /* foreground window is active window of foreground thread */
    reply->foreground = foreground_input ? foreground_input->active : 0;
    if (thread) release_object( thread );
}
2061 2062


2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
/* retrieve queue keyboard state for a given thread */
DECL_HANDLER(get_key_state)
{
    struct thread *thread;
    struct thread_input *input;

    if (!(thread = get_thread_from_id( req->tid ))) return;
    input = thread->queue ? thread->queue->input : NULL;
    if (input)
    {
        if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
        set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
    }
    release_object( thread );
}


/* set queue keyboard state for a given thread */
DECL_HANDLER(set_key_state)
{
    struct thread *thread = NULL;
    struct thread_input *input;

    if (!(thread = get_thread_from_id( req->tid ))) return;
    input = thread->queue ? thread->queue->input : NULL;
    if (input)
    {
2090
        data_size_t size = min( sizeof(input->keystate), get_req_data_size() );
2091 2092 2093 2094 2095 2096
        if (size) memcpy( input->keystate, get_req_data(), size );
    }
    release_object( thread );
}


2097 2098 2099
/* set the system foreground window */
DECL_HANDLER(set_foreground_window)
{
2100
    struct thread *thread;
2101 2102 2103 2104 2105 2106
    struct msg_queue *queue = get_current_queue();

    reply->previous = foreground_input ? foreground_input->active : 0;
    reply->send_msg_old = (reply->previous && foreground_input != queue->input);
    reply->send_msg_new = FALSE;

2107 2108
    if (is_top_level_window( req->handle ) &&
        ((thread = get_window_thread( req->handle ))))
2109
    {
2110 2111 2112
        foreground_input = thread->queue->input;
        reply->send_msg_new = (foreground_input != queue->input);
        release_object( thread );
2113
    }
2114
    else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147
}


/* set the current thread focus window */
DECL_HANDLER(set_focus_window)
{
    struct msg_queue *queue = get_current_queue();

    reply->previous = 0;
    if (queue && check_queue_input_window( queue, req->handle ))
    {
        reply->previous = queue->input->focus;
        queue->input->focus = get_user_full_handle( req->handle );
    }
}


/* set the current thread active window */
DECL_HANDLER(set_active_window)
{
    struct msg_queue *queue = get_current_queue();

    reply->previous = 0;
    if (queue && check_queue_input_window( queue, req->handle ))
    {
        if (!req->handle || make_window_active( req->handle ))
        {
            reply->previous = queue->input->active;
            queue->input->active = get_user_full_handle( req->handle );
        }
        else set_error( STATUS_INVALID_HANDLE );
    }
}
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166


/* set the current thread capture window */
DECL_HANDLER(set_capture_window)
{
    struct msg_queue *queue = get_current_queue();

    reply->previous = reply->full_handle = 0;
    if (queue && check_queue_input_window( queue, req->handle ))
    {
        struct thread_input *input = queue->input;

        reply->previous = input->capture;
        input->capture = get_user_full_handle( req->handle );
        input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
        input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
        reply->full_handle = input->capture;
    }
}
2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184


/* Set the current thread caret window */
DECL_HANDLER(set_caret_window)
{
    struct msg_queue *queue = get_current_queue();

    reply->previous = 0;
    if (queue && check_queue_input_window( queue, req->handle ))
    {
        struct thread_input *input = queue->input;

        reply->previous  = input->caret;
        reply->old_rect  = input->caret_rect;
        reply->old_hide  = input->caret_hide;
        reply->old_state = input->caret_state;

        set_caret_window( input, get_user_full_handle(req->handle) );
2185 2186
        input->caret_rect.right  = input->caret_rect.left + req->width;
        input->caret_rect.bottom = input->caret_rect.top + req->height;
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
    }
}


/* Set the current thread caret information */
DECL_HANDLER(set_caret_info)
{
    struct msg_queue *queue = get_current_queue();
    struct thread_input *input;

    if (!queue) return;
    input = queue->input;
    reply->full_handle = input->caret;
    reply->old_rect    = input->caret_rect;
    reply->old_hide    = input->caret_hide;
    reply->old_state   = input->caret_state;

    if (req->handle && get_user_full_handle(req->handle) != input->caret)
    {
        set_error( STATUS_ACCESS_DENIED );
        return;
    }
    if (req->flags & SET_CARET_POS)
    {
        input->caret_rect.right  += req->x - input->caret_rect.left;
        input->caret_rect.bottom += req->y - input->caret_rect.top;
        input->caret_rect.left = req->x;
        input->caret_rect.top  = req->y;
    }
    if (req->flags & SET_CARET_HIDE)
    {
        input->caret_hide += req->hide;
        if (input->caret_hide < 0) input->caret_hide = 0;
    }
    if (req->flags & SET_CARET_STATE)
    {
        if (req->state == -1) input->caret_state = !input->caret_state;
        else input->caret_state = !!req->state;
    }
}
2227 2228 2229 2230 2231 2232 2233


/* get the time of the last input event */
DECL_HANDLER(get_last_input_time)
{
    reply->time = last_input_time;
}