queue.c 55.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 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
#include "windef.h"
30 31 32 33
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"

34
#include "handle.h"
35
#include "file.h"
36 37 38
#include "thread.h"
#include "process.h"
#include "request.h"
39
#include "user.h"
40

41 42
enum message_kind { SEND_MESSAGE, POST_MESSAGE };
#define NB_MSG_KINDS (POST_MESSAGE+1)
43 44


45 46
struct message_result
{
47 48 49 50 51 52 53 54 55 56 57
    struct list            sender_entry;  /* entry in sender list */
    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           result;        /* reply result */
    unsigned int           error;         /* error code to pass back to sender */
    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 */
58 59 60 61 62 63
};

struct message
{
    struct message        *next;      /* next message in list */
    struct message        *prev;      /* prev message in list */
64
    enum message_type      type;      /* message type */
65
    user_handle_t          win;       /* window handle */
66 67 68
    unsigned int           msg;       /* message code */
    unsigned int           wparam;    /* parameters */
    unsigned int           lparam;    /* parameters */
69 70
    int                    x;         /* x position */
    int                    y;         /* y position */
71
    unsigned int           time;      /* message time */
72
    unsigned int           info;      /* extra info */
73 74
    void                  *data;      /* message data for sent messages */
    unsigned int           data_size; /* size of message data */
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    struct message_result *result;    /* result in sender queue */
};

struct message_list
{
    struct message *first;     /* head of list */
    struct message *last;      /* tail of list */
};

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

96 97 98 99 100 101 102 103 104
struct thread_input
{
    struct object          obj;           /* object header */
    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 */
105 106 107
    rectangle_t            caret_rect;    /* caret rectangle */
    int                    caret_hide;    /* caret hide count */
    int                    caret_state;   /* caret on/off state */
108 109 110
    struct message        *msg;           /* message currently processed */
    struct thread         *msg_thread;    /* thread processing the message */
    struct message_list    msg_list;      /* list of hardware messages */
111 112 113
    unsigned char          keystate[256]; /* state of each key */
};

114 115
struct msg_queue
{
116 117 118 119 120 121
    struct object          obj;             /* object header */
    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
    struct message_list    msg_list[NB_MSG_KINDS];  /* lists of messages */
123 124 125 126 127 128 129 130 131 132
    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 */
    struct timer          *first_timer;     /* head of timer list */
    struct timer          *last_timer;      /* tail of timer list */
    struct timer          *next_timer;      /* next timer to expire */
    struct timeout_user   *timeout;         /* timeout for next timer to expire */
    struct thread_input   *input;           /* thread input descriptor */
    struct hook_table     *hooks;           /* hook table */
    struct timeval         last_get_msg;    /* time of last get message call */
133 134 135 136 137 138 139
};

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 );
140
static void msg_queue_destroy( struct object *obj );
141 142
static void thread_input_dump( struct object *obj, int verbose );
static void thread_input_destroy( struct object *obj );
143
static void timer_callback( void *private );
144 145 146 147 148 149 150 151 152

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 */
153
    no_get_fd,                 /* get_fd */
154
    msg_queue_destroy          /* destroy */
155 156 157
};


158 159 160 161 162 163 164 165 166 167 168 169
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 */
    no_get_fd,                    /* get_fd */
    thread_input_destroy          /* destroy */
};

170 171 172
/* pointer to input structure of foreground thread */
static struct thread_input *foreground_input;

173 174 175 176 177 178 179 180 181 182 183 184 185

/* set the caret window in a given thread input */
static void set_caret_window( struct thread_input *input, user_handle_t win )
{
    input->caret             = win;
    input->caret_rect.left   = 0;
    input->caret_rect.top    = 0;
    input->caret_rect.right  = 0;
    input->caret_rect.bottom = 0;
    input->caret_hide        = 1;
    input->caret_state       = 0;
}

186 187 188 189 190
/* create a thread input object */
static struct thread_input *create_thread_input(void)
{
    struct thread_input *input;

191
    if ((input = alloc_object( &thread_input_ops )))
192 193 194 195 196 197
    {
        input->focus       = 0;
        input->capture     = 0;
        input->active      = 0;
        input->menu_owner  = 0;
        input->move_size   = 0;
198 199 200
        input->msg         = NULL;
        input->msg_thread  = NULL;
        input->msg_list.first = input->msg_list.last = NULL;
201
        set_caret_window( input, 0 );
202 203 204 205 206 207 208
        memset( input->keystate, 0, sizeof(input->keystate) );
    }
    return input;
}

/* create a message queue object */
static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
209 210
{
    struct msg_queue *queue;
211
    int i;
212

213
    if (!input && !(input = create_thread_input())) return NULL;
214
    if ((queue = alloc_object( &msg_queue_ops )))
215
    {
216 217 218 219
        queue->wake_bits       = 0;
        queue->wake_mask       = 0;
        queue->changed_bits    = 0;
        queue->changed_mask    = 0;
220
        queue->paint_count     = 0;
221 222 223 224 225
        queue->recv_result     = NULL;
        queue->first_timer     = NULL;
        queue->last_timer      = NULL;
        queue->next_timer      = NULL;
        queue->timeout         = NULL;
226
        queue->input           = (struct thread_input *)grab_object( input );
227
        queue->hooks           = NULL;
228
        gettimeofday( &queue->last_get_msg, NULL );
229 230
        list_init( &queue->send_result );
        list_init( &queue->callback_result );
231 232 233
        for (i = 0; i < NB_MSG_KINDS; i++)
            queue->msg_list[i].first = queue->msg_list[i].last = NULL;

234 235 236 237
        thread->queue = queue;
        if (!thread->process->queue)
            thread->process->queue = (struct msg_queue *)grab_object( queue );
    }
238
    release_object( input );
239 240 241
    return queue;
}

242 243 244 245 246
/* free the message queue of a thread at thread exit */
void free_msg_queue( struct thread *thread )
{
    struct process *process = thread->process;

247
    remove_thread_hooks( thread );
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    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;
}

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
/* 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;
    if (!queue) queue = create_msg_queue( thread, NULL );
    if (queue->hooks) release_object( queue->hooks );
    queue->hooks = hooks;
}

280 281 282 283 284 285
/* check the queue status */
inline static int is_signaled( struct msg_queue *queue )
{
    return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
}

286 287
/* set some queue bits */
inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
288
{
289 290
    queue->wake_bits |= bits;
    queue->changed_bits |= bits;
291 292 293
    if (is_signaled( queue )) wake_up( &queue->obj, 0 );
}

294 295 296 297 298 299 300
/* clear some queue bits */
inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
{
    queue->wake_bits &= ~bits;
    queue->changed_bits &= ~bits;
}

301 302 303 304 305 306
/* check whether msg is a keyboard message */
inline static int is_keyboard_msg( struct message *msg )
{
    return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
}

307 308 309 310
/* get the QS_* bit corresponding to a given hardware message */
inline static int get_hardware_msg_bit( struct message *msg )
{
    if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
311
    if (is_keyboard_msg( msg )) return QS_KEY;
312 313 314
    return QS_MOUSEBUTTON;
}

315
/* get the current thread queue, creating it if needed */
316
inline static struct msg_queue *get_current_queue(void)
317 318
{
    struct msg_queue *queue = current->queue;
319
    if (!queue) queue = create_msg_queue( current, NULL );
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    return queue;
}

/* append a message to the end of a list */
inline static void append_message( struct message_list *list, struct message *msg )
{
    msg->next = NULL;
    if ((msg->prev = list->last)) msg->prev->next = msg;
    else list->first = msg;
    list->last = msg;
}

/* unlink a message from a list it */
inline static void unlink_message( struct message_list *list, struct message *msg )
{
    if (msg->next) msg->next->prev = msg->prev;
    else list->last = msg->prev;
    if (msg->prev) msg->prev->next = msg->next;
    else list->first = msg->next;
}

341
/* try to merge a message with the last in the list; return 1 if successful */
342
static int merge_message( struct thread_input *input, const struct message *msg )
343
{
344
    struct message *prev = input->msg_list.last;
345 346

    if (!prev) return 0;
347
    if (input->msg == prev) return 0;
348 349 350 351 352 353 354 355 356 357 358 359 360 361
    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;
}

362 363 364 365 366
/* free a result structure */
static void free_result( struct message_result *result )
{
    if (result->timeout) remove_timeout_user( result->timeout );
    if (result->data) free( result->data );
367
    if (result->callback_msg) free( result->callback_msg );
368 369 370
    free( result );
}

371 372 373 374 375 376 377 378 379 380
/* 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 );
}

381 382 383 384 385 386 387 388 389 390 391 392
/* store the message result in the appropriate structure */
static void store_message_result( struct message_result *res, unsigned int result,
                                  unsigned int error )
{
    res->result  = result;
    res->error   = error;
    res->replied = 1;
    if (res->timeout)
    {
        remove_timeout_user( res->timeout );
        res->timeout = NULL;
    }
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
    if (res->sender)
    {
        if (res->callback_msg)
        {
            /* queue the callback message in the sender queue */
            res->callback_msg->lparam = result;
            append_message( &res->sender->msg_list[SEND_MESSAGE], res->callback_msg );
            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 );
        }
    }

412 413
}

414 415 416 417 418 419 420 421 422
/* free a message when deleting a queue or window */
static void free_message( struct message *msg )
{
    struct message_result *result = msg->result;
    if (result)
    {
        if (result->sender)
        {
            result->receiver = NULL;
423
            store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
424
        }
425
        else free_result( result );
426
    }
427
    if (msg->data) free( msg->data );
428 429 430
    free( msg );
}

431 432 433
/* 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 )
434
{
435 436 437 438
    unlink_message( &queue->msg_list[kind], msg );
    switch(kind)
    {
    case SEND_MESSAGE:
439
        if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_SENDMESSAGE );
440 441
        break;
    case POST_MESSAGE:
442
        if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_POSTMESSAGE );
443 444
        break;
    }
445 446 447
    free_message( msg );
}

448 449 450 451 452 453 454 455 456 457 458 459 460 461
/* message timed out without getting a reply */
static void result_timeout( void *private )
{
    struct message_result *result = private;

    assert( !result->replied );

    result->timeout = NULL;
    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,
462 463
                                                    struct message *msg, unsigned int timeout,
                                                    void *callback, unsigned int callback_data )
464 465
{
    struct message_result *result = mem_alloc( sizeof(*result) );
466 467 468 469 470 471 472 473
    if (result)
    {
        result->sender    = send_queue;
        result->receiver  = recv_queue;
        result->replied   = 0;
        result->data      = NULL;
        result->data_size = 0;
        result->timeout   = NULL;
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

        if (msg->type == MSG_CALLBACK)
        {
            struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
            if (!callback_msg)
            {
                free( result );
                return NULL;
            }
            callback_msg->type      = MSG_CALLBACK_RESULT;
            callback_msg->win       = msg->win;
            callback_msg->msg       = msg->msg;
            callback_msg->wparam    = (unsigned int)callback;
            callback_msg->lparam    = 0;
            callback_msg->time      = get_tick_count();
            callback_msg->x         = 0;
            callback_msg->y         = 0;
            callback_msg->info      = callback_data;
            callback_msg->result    = NULL;
            callback_msg->data      = NULL;
            callback_msg->data_size = 0;

            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 );
        }

505 506 507 508 509 510 511 512 513
        if (timeout != -1)
        {
            struct timeval when;
            gettimeofday( &when, 0 );
            add_timeout( &when, timeout );
            result->timeout = add_timeout_user( &when, result_timeout, result );
        }
    }
    return result;
514 515 516
}

/* receive a message, removing it from the sent queue */
517 518
static void receive_message( struct msg_queue *queue, struct message *msg,
                             struct get_message_reply *reply )
519 520 521
{
    struct message_result *result = msg->result;

522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
    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 );

540
    unlink_message( &queue->msg_list[SEND_MESSAGE], msg );
541
    /* put the result on the receiver result stack */
542 543 544 545 546
    if (result)
    {
        result->recv_next  = queue->recv_result;
        queue->recv_result = result;
    }
547
    free( msg );
548
    if (!queue->msg_list[SEND_MESSAGE].first) clear_queue_bits( queue, QS_SENDMESSAGE );
549 550 551 552
}

/* set the result of the current received message */
static void reply_message( struct msg_queue *queue, unsigned int result,
553
                           unsigned int error, int remove, const void *data, size_t len )
554 555 556 557 558 559 560 561 562
{
    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 */
        {
563
            free_result( res );
564 565 566 567 568
            return;
        }
    }
    if (!res->replied)
    {
569 570
        if (len && (res->data = memdup( data, len ))) res->data_size = len;
        store_message_result( res, result, error );
571 572 573
    }
}

574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
/* 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;
    struct message_list *list = &queue->msg_list[POST_MESSAGE];

    /* check against the filters */
    for (msg = list->first; msg; msg = msg->next)
    {
        if (msg->msg == WM_QUIT) break;  /* WM_QUIT is never filtered */
        if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
        if (msg->msg < first) continue;
        if (msg->msg > last) continue;
        break; /* found one */
    }
    if (!msg) return 0;

    /* return it to the app */

    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;

    if (flags & GET_MSG_REMOVE)
    {
        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;
}

626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
/* empty a message list and free all the messages */
static void empty_msg_list( struct message_list *list )
{
    struct message *msg = list->first;
    while (msg)
    {
        struct message *next = msg->next;
        free_message( msg );
        msg = next;
    }
}

/* cleanup all pending results when deleting a queue */
static void cleanup_results( struct msg_queue *queue )
{
641 642 643 644 645 646
    struct list *entry;

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

648
    while ((entry = list_head( &queue->callback_result )) != NULL)
649
    {
650
        remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
651 652
    }

653 654
    while (queue->recv_result)
        reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
655 656
}

657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
/* check if the thread owning the queue is hung (not checking for messages) */
static int is_queue_hung( struct msg_queue *queue )
{
    struct timeval now;
    struct wait_queue_entry *entry;

    gettimeofday( &now, NULL );
    if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
        return 0;  /* less than 5 seconds since last get message -> not hung */

    for (entry = queue->obj.head; entry; entry = entry->next)
    {
        if (entry->thread->queue == queue)
            return 0;  /* thread is waiting on queue -> not hung */
    }
    return 1;
}

675 676 677 678 679
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;

680 681 682 683 684 685
    /* a thread can only wait on its own queue */
    if (entry->thread->queue != queue)
    {
        set_error( STATUS_ACCESS_DENIED );
        return 0;
    }
686
    /* if waiting on the main process queue, set the idle event */
687
    if (process->queue == queue)
688 689 690 691 692 693 694 695 696 697 698 699 700 701
    {
        if (process->idle_event) set_event( process->idle_event );
    }
    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 );

702 703
    assert( entry->thread->queue == queue );

704
    /* if waiting on the main process queue, reset the idle event */
705
    if (process->queue == queue)
706 707 708 709 710 711 712 713
    {
        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;
714 715
    fprintf( stderr, "Msg queue bits=%x mask=%x\n",
             queue->wake_bits, queue->wake_mask );
716 717 718 719 720
}

static int msg_queue_signaled( struct object *obj, struct thread *thread )
{
    struct msg_queue *queue = (struct msg_queue *)obj;
721
    return is_signaled( queue );
722 723 724 725 726
}

static int msg_queue_satisfied( struct object *obj, struct thread *thread )
{
    struct msg_queue *queue = (struct msg_queue *)obj;
727 728
    queue->wake_mask = 0;
    queue->changed_mask = 0;
729 730 731
    return 0;  /* Not abandoned */
}

732 733 734 735
static void msg_queue_destroy( struct object *obj )
{
    struct msg_queue *queue = (struct msg_queue *)obj;
    struct timer *timer = queue->first_timer;
736
    int i;
737 738

    cleanup_results( queue );
739
    for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
740 741 742 743 744 745 746 747

    while (timer)
    {
        struct timer *next = timer->next;
        free( timer );
        timer = next;
    }
    if (queue->timeout) remove_timeout_user( queue->timeout );
748
    if (queue->input) release_object( queue->input );
749
    if (queue->hooks) release_object( queue->hooks );
750 751 752 753 754
}

static void thread_input_dump( struct object *obj, int verbose )
{
    struct thread_input *input = (struct thread_input *)obj;
755
    fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
756 757 758 759 760 761 762 763
             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;
764 765
    if (input->msg_thread) release_object( input->msg_thread );
    empty_msg_list( &input->msg_list );
766 767 768 769 770 771 772 773 774 775 776 777
}

/* fix the thread input data when a window is destroyed */
inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
{
    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;
778
    if (window == input->caret) set_caret_window( input, 0 );
779 780
}

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
/* 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;
}

800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
/* attach two thread input data structures */
int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
{
    struct thread_input *input;

    if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
    input = (struct thread_input *)grab_object( thread_to->queue->input );

    if (thread_from->queue)
    {
        release_object( thread_from->queue->input );
        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;
819 820
}

821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
/* detach two thread input data structures */
static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
{
    struct thread_input *input;

    if (!thread_from->queue || !thread_to->queue ||
        thread_from->queue->input != thread_to->queue->input)
    {
        set_error( STATUS_ACCESS_DENIED );
        return;
    }
    if ((input = create_thread_input()))
    {
        release_object( thread_from->queue->input );
        thread_from->queue->input = input;
    }
}


840 841 842 843 844 845 846 847 848 849 850 851 852
/* set the next timer to expire */
static void set_next_timer( struct msg_queue *queue, struct timer *timer )
{
    if (queue->timeout)
    {
        remove_timeout_user( queue->timeout );
        queue->timeout = NULL;
    }
    if ((queue->next_timer = timer))
        queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );

    /* set/clear QS_TIMER bit */
    if (queue->next_timer == queue->first_timer)
853
        clear_queue_bits( queue, QS_TIMER );
854
    else
855
        set_queue_bits( queue, QS_TIMER );
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
}

/* callback for the next timer expiration */
static void timer_callback( void *private )
{
    struct msg_queue *queue = private;

    queue->timeout = NULL;
    /* move on to the next timer */
    set_next_timer( queue, queue->next_timer->next );
}

/* link a timer at its rightful place in the queue list */
static void link_timer( struct msg_queue *queue, struct timer *timer )
{
    struct timer *pos = queue->next_timer;

    while (pos && time_before( &pos->when, &timer->when )) pos = pos->next;

    if (pos) /* insert before pos */
    {
        if ((timer->prev = pos->prev)) timer->prev->next = timer;
        else queue->first_timer = timer;
        timer->next = pos;
        pos->prev = timer;
    }
    else  /* insert at end */
    {
        timer->next = NULL;
        timer->prev = queue->last_timer;
        if (queue->last_timer) queue->last_timer->next = timer;
        else queue->first_timer = timer;
        queue->last_timer = timer;
    }
    /* check if we replaced the next timer */
    if (pos == queue->next_timer) set_next_timer( queue, timer );
}

/* remove a timer from the queue timer list */
static void unlink_timer( struct msg_queue *queue, struct timer *timer )
{
    if (timer->next) timer->next->prev = timer->prev;
    else queue->last_timer = timer->prev;
    if (timer->prev) timer->prev->next = timer->next;
    else queue->first_timer = timer->next;
    /* check if we removed the next timer */
    if (queue->next_timer == timer) set_next_timer( queue, timer->next );
903
    else if (queue->next_timer == queue->first_timer) clear_queue_bits( queue, QS_TIMER );
904 905 906 907 908 909 910 911 912 913 914 915 916
}

/* restart an expired timer */
static void restart_timer( struct msg_queue *queue, struct timer *timer )
{
    struct timeval now;
    unlink_timer( queue, timer );
    gettimeofday( &now, 0 );
    while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
    link_timer( queue, timer );
}

/* find an expired timer matching the filtering parameters */
917
static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
                                         unsigned int get_first, unsigned int get_last,
                                         int remove )
{
    struct timer *timer;
    for (timer = queue->first_timer; (timer && timer != queue->next_timer); timer = timer->next)
    {
        if (win && timer->win != win) continue;
        if (timer->msg >= get_first && timer->msg <= get_last)
        {
            if (remove) restart_timer( queue, timer );
            return timer;
        }
    }
    return NULL;
}

/* kill a timer */
935 936
static int kill_timer( struct msg_queue *queue, user_handle_t win,
                       unsigned int msg, unsigned int id )
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
{
    struct timer *timer;

    for (timer = queue->first_timer; timer; timer = timer->next)
    {
        if (timer->win != win || timer->msg != msg || timer->id != id) continue;
        unlink_timer( queue, timer );
        free( timer );
        return 1;
    }
    return 0;
}

/* add a timer */
static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
{
    struct timer *timer = mem_alloc( sizeof(*timer) );
    if (timer)
    {
        timer->rate  = rate;
        gettimeofday( &timer->when, 0 );
        add_timeout( &timer->when, rate );
        link_timer( queue, timer );
    }
    return timer;
}

964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
/* 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;
    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;
        }
        break;
    }
}

1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
/* release the hardware message currently being processed by the given thread */
static void release_hardware_message( struct thread *thread, int remove )
{
    struct thread_input *input = thread->queue->input;

    if (input->msg_thread != thread) return;
    if (remove)
    {
        struct message *other;
        int clr_bit;

1037
        update_input_key_state( input, input->msg );
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
        unlink_message( &input->msg_list, input->msg );
        clr_bit = get_hardware_msg_bit( input->msg );
        for (other = input->msg_list.first; other; other = other->next)
            if (get_hardware_msg_bit( other ) == clr_bit) break;
        if (!other) clear_queue_bits( thread->queue, clr_bit );
        free_message( input->msg );
    }
    release_object( input->msg_thread );
    input->msg = NULL;
    input->msg_thread = NULL;
}

/* find the window that should receive a given hardware message */
1051 1052
static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
                                                   unsigned int *msg_code )
1053 1054 1055
{
    user_handle_t win = 0;

1056
    *msg_code = msg->msg;
1057 1058
    if (is_keyboard_msg( msg ))
    {
1059 1060 1061 1062 1063
        if (input && !(win = input->focus))
        {
            win = input->active;
            if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
        }
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
    }
    else  /* mouse message */
    {
        if (!input || !(win = input->capture))
        {
            if (!(win = msg->win)) win = window_from_point( msg->x, msg->y );
        }
    }
    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;
    struct thread *thread;
    struct thread_input *input;
1081
    unsigned int msg_code;
1082

1083
    win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
    if (!win || !(thread = get_window_thread(win)))
    {
        free( msg );
        return;
    }
    input = thread->queue->input;

    if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
    else
    {
        append_message( &input->msg_list, msg );
        set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
    }
    release_object( thread );
}

/* find a hardware message for the given queue */
static int get_hardware_message( struct thread *thread, struct message *first,
                                 user_handle_t filter_win, struct get_message_reply *reply )
{
    struct thread_input *input = thread->queue->input;
    struct thread *win_thread;
    struct message *msg;
    user_handle_t win;
    int clear_bits, got_one = 0;
1109
    unsigned int msg_code;
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126

    if (input->msg_thread && input->msg_thread != thread)
        return 0;  /* locked by another thread */

    if (!first)
    {
        msg = input->msg_list.first;
        clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
    }
    else
    {
        msg = first->next;
        clear_bits = 0;  /* don't clear bits if we don't go through the whole list */
    }

    while (msg)
    {
1127
        win = find_hardware_message_window( input, msg, &msg_code );
1128 1129 1130 1131
        if (!win || !(win_thread = get_window_thread( win )))
        {
            /* no window at all, remove it */
            struct message *next = msg->next;
1132
            update_input_key_state( input, msg );
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
            unlink_message( &input->msg_list, msg );
            free_message( msg );
            msg = next;
            continue;
        }
        if (win_thread != thread)
        {
            /* wake the other thread */
            set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
            release_object( win_thread );
            got_one = 1;
            msg = msg->next;
            continue;
        }
        /* if we already got a message for another thread, or if it doesn't
         * match the filter we skip it (filter is only checked for keyboard
         * messages since the dest window for a mouse message depends on hittest)
         */
        if (got_one ||
            (filter_win && is_keyboard_msg(msg) &&
             win != filter_win && !is_child_window( filter_win, win )))
        {
            clear_bits &= ~get_hardware_msg_bit( msg );
1156
            release_object( win_thread );
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
            msg = msg->next;
            continue;
        }
        /* now we can return it */
        if (!input->msg_thread) input->msg_thread = win_thread;
        else release_object( win_thread );
        input->msg = msg;

        reply->type   = MSG_HARDWARE;
        reply->win    = win;
1167
        reply->msg    = msg_code;
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
        reply->wparam = msg->wparam;
        reply->lparam = msg->lparam;
        reply->x      = msg->x;
        reply->y      = msg->y;
        reply->time   = msg->time;
        reply->info   = msg->info;
        return 1;
    }
    /* nothing found, clear the hardware queue bits */
    clear_queue_bits( thread->queue, clear_bits );
    if (input->msg_thread) release_object( input->msg_thread );
    input->msg = NULL;
    input->msg_thread = NULL;
    return 0;
}
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199

/* 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 );
}


1200
/* remove all messages and timers belonging to a certain window */
1201
void queue_cleanup_window( struct thread *thread, user_handle_t win )
1202
{
1203
    struct msg_queue *queue = thread->queue;
1204 1205
    struct timer *timer;
    struct message *msg;
1206
    int i;
1207

1208 1209
    if (!queue) return;

1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
    /* remove timers */
    timer = queue->first_timer;
    while (timer)
    {
        struct timer *next = timer->next;
        if (timer->win == win)
        {
            unlink_timer( queue, timer );
            free( timer );
        }
        timer = next;
    }

1223 1224
    /* remove messages */
    for (i = 0; i < NB_MSG_KINDS; i++)
1225
    {
1226 1227 1228 1229 1230 1231 1232
        msg = queue->msg_list[i].first;
        while (msg)
        {
            struct message *next = msg->next;
            if (msg->win == win) remove_queue_message( queue, msg, i );
            msg = next;
        }
1233
    }
1234 1235

    thread_input_cleanup_window( queue, win );
1236 1237
}

1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
/* post a message to a window; used by socket handling */
void post_message( user_handle_t win, unsigned int message,
                   unsigned int wparam, unsigned int lparam )
{
    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;

        append_message( &thread->queue->msg_list[POST_MESSAGE], msg );
        set_queue_bits( thread->queue, QS_POSTMESSAGE );
    }
    release_object( thread );
}


1269 1270 1271
/* get the message queue of the current thread */
DECL_HANDLER(get_msg_queue)
{
1272
    struct msg_queue *queue = get_current_queue();
1273

1274 1275
    reply->handle = 0;
    if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1276 1277
}

1278 1279 1280 1281 1282 1283 1284 1285 1286 1287

/* 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;
1288 1289
        reply->wake_bits    = queue->wake_bits;
        reply->changed_bits = queue->changed_bits;
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
        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)
    {
1306 1307
        reply->wake_bits    = queue->wake_bits;
        reply->changed_bits = queue->changed_bits;
1308 1309
        if (req->clear) queue->changed_bits = 0;
    }
1310
    else reply->wake_bits = reply->changed_bits = 0;
1311 1312 1313 1314 1315 1316 1317 1318
}


/* send a message to a thread queue */
DECL_HANDLER(send_message)
{
    struct message *msg;
    struct msg_queue *send_queue = get_current_queue();
1319 1320
    struct msg_queue *recv_queue = NULL;
    struct thread *thread = NULL;
1321

1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
    if (req->id)
    {
        if (!(thread = get_thread_from_id( req->id ))) return;
    }
    else if (req->type != MSG_HARDWARE)
    {
        /* only hardware messages are allowed without destination thread */
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }
1332

1333
    if (thread && !(recv_queue = thread->queue))
1334 1335 1336 1337 1338
    {
        set_error( STATUS_INVALID_PARAMETER );
        release_object( thread );
        return;
    }
1339 1340 1341 1342 1343 1344
    if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
    {
        set_error( STATUS_TIMEOUT );
        release_object( thread );
        return;
    }
1345 1346 1347

    if ((msg = mem_alloc( sizeof(*msg) )))
    {
1348
        msg->type      = req->type;
1349
        msg->win       = get_user_full_handle( req->win );
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
        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;

        switch(msg->type)
1362
        {
1363
        case MSG_OTHER_PROCESS:
1364 1365
            msg->data_size = get_req_data_size();
            if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1366 1367
            {
                free( msg );
1368
                break;
1369
            }
1370 1371 1372 1373
            /* fall through */
        case MSG_ASCII:
        case MSG_UNICODE:
        case MSG_CALLBACK:
1374 1375
            if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
                                                      req->timeout, req->callback, req->info )))
1376
            {
1377
                free_message( msg );
1378
                break;
1379
            }
1380 1381 1382 1383 1384 1385
            /* fall through */
        case MSG_NOTIFY:
            append_message( &recv_queue->msg_list[SEND_MESSAGE], msg );
            set_queue_bits( recv_queue, QS_SENDMESSAGE );
            break;
        case MSG_POSTED:
1386 1387 1388 1389 1390 1391 1392
            /* needed for posted DDE messages */
            msg->data_size = get_req_data_size();
            if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
            {
                free( msg );
                break;
            }
1393 1394
            append_message( &recv_queue->msg_list[POST_MESSAGE], msg );
            set_queue_bits( recv_queue, QS_POSTMESSAGE );
1395
            break;
1396 1397 1398
        case MSG_HARDWARE:
            queue_hardware_message( recv_queue, msg );
            break;
1399
        case MSG_CALLBACK_RESULT:  /* cannot send this one */
1400 1401
        default:
            set_error( STATUS_INVALID_PARAMETER );
1402
            free( msg );
1403
            break;
1404 1405
        }
    }
1406
    if (thread) release_object( thread );
1407 1408 1409
}


1410 1411 1412 1413 1414
/* get a message from the current queue */
DECL_HANDLER(get_message)
{
    struct timer *timer;
    struct message *msg;
1415
    struct message *first_hw_msg = NULL;
1416
    struct msg_queue *queue = get_current_queue();
1417
    user_handle_t get_win = get_user_full_handle( req->get_win );
1418

1419
    if (!queue) return;
1420
    gettimeofday( &queue->last_get_msg, NULL );
1421

1422 1423 1424 1425 1426 1427 1428 1429
    /* first of all release the hardware input lock if we own it */
    /* we'll grab it again if we find a hardware message */
    if (queue->input->msg_thread == current)
    {
        first_hw_msg = queue->input->msg;
        release_hardware_message( current, 0 );
    }

1430
    /* first check for sent messages */
1431 1432
    if ((msg = queue->msg_list[SEND_MESSAGE].first))
    {
1433
        receive_message( queue, msg, reply );
1434 1435
        return;
    }
1436
    if (req->flags & GET_MSG_SENT_ONLY) goto done;  /* nothing else to check */
1437

1438 1439 1440
    /* clear changed bits so we can wait on them if we don't find a message */
    queue->changed_bits = 0;

1441
    /* then check for posted messages */
1442
    if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1443 1444 1445
        return;

    /* then check for any raw hardware message */
1446
    if (get_hardware_message( current, first_hw_msg, get_win, reply ))
1447
        return;
1448 1449

    /* now check for WM_PAINT */
1450 1451
    if (queue->paint_count &&
        (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
        (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;
1462 1463 1464 1465
        return;
    }

    /* now check for timer */
1466
    if ((timer = find_expired_timer( queue, get_win, req->get_first,
1467
                                     req->get_last, (req->flags & GET_MSG_REMOVE) )))
1468
    {
1469 1470 1471 1472 1473 1474 1475 1476 1477
        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;
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
        return;
    }

 done:
    set_error( STATUS_PENDING );  /* FIXME */
}


/* reply to a sent message */
DECL_HANDLER(reply_message)
{
1489 1490 1491 1492 1493
    if (!current->queue)
    {
        set_error( STATUS_ACCESS_DENIED );
        return;
    }
1494
    if (req->type == MSG_HARDWARE)
1495 1496 1497 1498 1499
    {
        struct thread_input *input = current->queue->input;
        if (input->msg_thread == current) release_hardware_message( current, req->remove );
        else set_error( STATUS_ACCESS_DENIED );
    }
1500 1501 1502
    else if (current->queue->recv_result)
        reply_message( current->queue, req->result, 0, req->remove,
                       get_req_data(), get_req_data_size() );
1503 1504 1505 1506 1507 1508
}


/* retrieve the reply for the last message sent */
DECL_HANDLER(get_message_reply)
{
1509 1510
    struct message_result *result;
    struct list *entry;
1511
    struct msg_queue *queue = current->queue;
1512

1513 1514 1515
    if (queue)
    {
        set_error( STATUS_PENDING );
1516
        reply->result = 0;
1517

1518 1519 1520 1521
        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)
1522
        {
1523 1524
            if (result->replied)
            {
1525
                reply->result = result->result;
1526 1527 1528
                set_error( result->error );
                if (result->data)
                {
1529 1530
                    size_t data_len = min( result->data_size, get_reply_max_size() );
                    set_reply_data_ptr( result->data, data_len );
1531 1532 1533 1534
                    result->data = NULL;
                    result->data_size = 0;
                }
            }
1535 1536 1537 1538 1539 1540 1541 1542 1543
            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 );
            }
1544 1545
        }
    }
1546
    else set_error( STATUS_ACCESS_DENIED );
1547 1548 1549 1550 1551 1552 1553 1554
}


/* set a window timer */
DECL_HANDLER(set_win_timer)
{
    struct timer *timer;
    struct msg_queue *queue = get_current_queue();
1555
    user_handle_t win = get_user_full_handle( req->win );
1556 1557 1558 1559

    if (!queue) return;

    /* remove it if it existed already */
1560
    if (win) kill_timer( queue, win, req->msg, req->id );
1561 1562 1563

    if ((timer = set_timer( queue, req->rate )))
    {
1564
        timer->win    = win;
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
        timer->msg    = req->msg;
        timer->id     = req->id;
        timer->lparam = req->lparam;
    }
}

/* kill a window timer */
DECL_HANDLER(kill_win_timer)
{
    struct msg_queue *queue = current->queue;

1576
    if (!queue || !kill_timer( queue, get_user_full_handle(req->win), req->msg, req->id ))
1577 1578
        set_error( STATUS_INVALID_PARAMETER );
}
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624


/* 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 );
        else detach_thread_input( thread_from, thread_to );
    }
    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;
1625
        reply->rect       = input->caret_rect;
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
    }
    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 );
}
1641 1642


1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
/* 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)
    {
        size_t size = min( sizeof(input->keystate), get_req_data_size() );
        if (size) memcpy( input->keystate, get_req_data(), size );
    }
    release_object( thread );
}


1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 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
/* set the system foreground window */
DECL_HANDLER(set_foreground_window)
{
    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;

    if (req->handle)
    {
        struct thread *thread;

        if (is_top_level_window( req->handle ) &&
            ((thread = get_window_thread( req->handle ))))
        {
            foreground_input = thread->queue->input;
            reply->send_msg_new = (foreground_input != queue->input);
            release_object( thread );
        }
        else set_error( STATUS_INVALID_HANDLE );
    }
    else foreground_input = NULL;
}


/* 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 );
    }
}
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751


/* 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;
    }
}
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811


/* 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) );
        input->caret_rect.right  = req->width;
        input->caret_rect.bottom = req->height;
    }
}


/* 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;
    }
}