ntdll: Fix heap management for 64-bit.
[wine] / server / queue.c
1 /*
2  * Server-side message queues
3  *
4  * Copyright (C) 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "winternl.h"
36
37 #include "handle.h"
38 #include "file.h"
39 #include "thread.h"
40 #include "process.h"
41 #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 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
48 #define NB_MSG_KINDS (POST_MESSAGE+1)
49
50
51 struct message_result
52 {
53     struct list            sender_entry;  /* entry in sender list */
54     struct message        *msg;           /* message the result is for */
55     struct message_result *recv_next;     /* next in receiver list */
56     struct msg_queue      *sender;        /* sender queue */
57     struct msg_queue      *receiver;      /* receiver queue */
58     int                    replied;       /* has it been replied to? */
59     unsigned int           error;         /* error code to pass back to sender */
60     lparam_t               result;        /* reply result */
61     struct message        *callback_msg;  /* message to queue for callback */
62     void                  *data;          /* message reply data */
63     unsigned int           data_size;     /* size of message reply data */
64     struct timeout_user   *timeout;       /* result timeout */
65 };
66
67 struct message
68 {
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     lparam_t               wparam;    /* parameters */
74     lparam_t               lparam;    /* parameters */
75     lparam_t               info;      /* extra info */
76     int                    x;         /* x position */
77     int                    y;         /* y position */
78     unsigned int           time;      /* message time */
79     void                  *data;      /* message data for sent messages */
80     unsigned int           data_size; /* size of message data */
81     unsigned int           unique_id; /* unique id for nested hw message waits */
82     struct message_result *result;    /* result in sender queue */
83 };
84
85 struct timer
86 {
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     lparam_t        id;        /* timer id */
93     lparam_t        lparam;    /* lparam for message */
94 };
95
96 struct thread_input
97 {
98     struct object          obj;           /* object header */
99     struct desktop        *desktop;       /* desktop that this thread input belongs to */
100     user_handle_t          focus;         /* focus window */
101     user_handle_t          capture;       /* capture window */
102     user_handle_t          active;        /* active window */
103     user_handle_t          menu_owner;    /* current menu owner window */
104     user_handle_t          move_size;     /* current moving/resizing window */
105     user_handle_t          caret;         /* caret window */
106     rectangle_t            caret_rect;    /* caret rectangle */
107     int                    caret_hide;    /* caret hide count */
108     int                    caret_state;   /* caret on/off state */
109     struct list            msg_list;      /* list of hardware messages */
110     unsigned char          keystate[256]; /* state of each key */
111 };
112
113 struct msg_queue
114 {
115     struct object          obj;             /* object header */
116     struct fd             *fd;              /* optional file descriptor to poll */
117     unsigned int           wake_bits;       /* wakeup bits */
118     unsigned int           wake_mask;       /* wakeup mask */
119     unsigned int           changed_bits;    /* changed wakeup bits */
120     unsigned int           changed_mask;    /* changed wakeup mask */
121     int                    paint_count;     /* pending paint messages count */
122     int                    quit_message;    /* is there a pending quit message? */
123     int                    exit_code;       /* exit code of pending quit message */
124     struct list            msg_list[NB_MSG_KINDS];  /* lists of messages */
125     struct list            send_result;     /* stack of sent messages waiting for result */
126     struct list            callback_result; /* list of callback messages waiting for result */
127     struct message_result *recv_result;     /* stack of received messages waiting for result */
128     struct list            pending_timers;  /* list of pending timers */
129     struct list            expired_timers;  /* list of expired timers */
130     lparam_t               next_timer_id;   /* id for the next timer with a 0 window */
131     struct timeout_user   *timeout;         /* timeout for next timer to expire */
132     struct thread_input   *input;           /* thread input descriptor */
133     struct hook_table     *hooks;           /* hook table */
134     timeout_t              last_get_msg;    /* time of last get message call */
135 };
136
137 static void msg_queue_dump( struct object *obj, int verbose );
138 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
139 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
140 static int msg_queue_signaled( struct object *obj, struct thread *thread );
141 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 static void thread_input_dump( struct object *obj, int verbose );
145 static void thread_input_destroy( struct object *obj );
146 static void timer_callback( void *private );
147
148 static const struct object_ops msg_queue_ops =
149 {
150     sizeof(struct msg_queue),  /* size */
151     msg_queue_dump,            /* dump */
152     no_get_type,               /* get_type */
153     msg_queue_add_queue,       /* add_queue */
154     msg_queue_remove_queue,    /* remove_queue */
155     msg_queue_signaled,        /* signaled */
156     msg_queue_satisfied,       /* satisfied */
157     no_signal,                 /* signal */
158     no_get_fd,                 /* get_fd */
159     no_map_access,             /* map_access */
160     default_get_sd,            /* get_sd */
161     default_set_sd,            /* set_sd */
162     no_lookup_name,            /* lookup_name */
163     no_open_file,              /* open_file */
164     no_close_handle,           /* close_handle */
165     msg_queue_destroy          /* destroy */
166 };
167
168 static const struct fd_ops msg_queue_fd_ops =
169 {
170     NULL,                        /* get_poll_events */
171     msg_queue_poll_event,        /* poll_event */
172     NULL,                        /* flush */
173     NULL,                        /* get_fd_type */
174     NULL,                        /* ioctl */
175     NULL,                        /* queue_async */
176     NULL,                        /* reselect_async */
177     NULL                         /* cancel async */
178 };
179
180
181 static const struct object_ops thread_input_ops =
182 {
183     sizeof(struct thread_input),  /* size */
184     thread_input_dump,            /* dump */
185     no_get_type,                  /* get_type */
186     no_add_queue,                 /* add_queue */
187     NULL,                         /* remove_queue */
188     NULL,                         /* signaled */
189     NULL,                         /* satisfied */
190     no_signal,                    /* signal */
191     no_get_fd,                    /* get_fd */
192     no_map_access,                /* map_access */
193     default_get_sd,               /* get_sd */
194     default_set_sd,               /* set_sd */
195     no_lookup_name,               /* lookup_name */
196     no_open_file,                 /* open_file */
197     no_close_handle,              /* close_handle */
198     thread_input_destroy          /* destroy */
199 };
200
201 /* pointer to input structure of foreground thread */
202 static struct thread_input *foreground_input;
203 static unsigned int last_input_time;
204
205 static void free_message( struct message *msg );
206
207 /* set the caret window in a given thread input */
208 static void set_caret_window( struct thread_input *input, user_handle_t win )
209 {
210     if (!win || win != input->caret)
211     {
212         input->caret_rect.left   = 0;
213         input->caret_rect.top    = 0;
214         input->caret_rect.right  = 0;
215         input->caret_rect.bottom = 0;
216     }
217     input->caret             = win;
218     input->caret_hide        = 1;
219     input->caret_state       = 0;
220 }
221
222 /* create a thread input object */
223 static struct thread_input *create_thread_input( struct thread *thread )
224 {
225     struct thread_input *input;
226
227     if ((input = alloc_object( &thread_input_ops )))
228     {
229         input->focus       = 0;
230         input->capture     = 0;
231         input->active      = 0;
232         input->menu_owner  = 0;
233         input->move_size   = 0;
234         list_init( &input->msg_list );
235         set_caret_window( input, 0 );
236         memset( input->keystate, 0, sizeof(input->keystate) );
237
238         if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
239         {
240             release_object( input );
241             return NULL;
242         }
243     }
244     return input;
245 }
246
247 /* release the thread input data of a given thread */
248 static inline void release_thread_input( struct thread *thread )
249 {
250     struct thread_input *input = thread->queue->input;
251
252     if (!input) return;
253     release_object( input );
254     thread->queue->input = NULL;
255 }
256
257 /* create a message queue object */
258 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
259 {
260     struct msg_queue *queue;
261     int i;
262
263     if (!input && !(input = create_thread_input( thread ))) return NULL;
264     if ((queue = alloc_object( &msg_queue_ops )))
265     {
266         queue->fd              = NULL;
267         queue->wake_bits       = 0;
268         queue->wake_mask       = 0;
269         queue->changed_bits    = 0;
270         queue->changed_mask    = 0;
271         queue->paint_count     = 0;
272         queue->quit_message    = 0;
273         queue->recv_result     = NULL;
274         queue->next_timer_id   = 0x7fff;
275         queue->timeout         = NULL;
276         queue->input           = (struct thread_input *)grab_object( input );
277         queue->hooks           = NULL;
278         queue->last_get_msg    = current_time;
279         list_init( &queue->send_result );
280         list_init( &queue->callback_result );
281         list_init( &queue->pending_timers );
282         list_init( &queue->expired_timers );
283         for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
284
285         thread->queue = queue;
286         if (!thread->process->queue)
287             thread->process->queue = (struct msg_queue *)grab_object( queue );
288     }
289     release_object( input );
290     return queue;
291 }
292
293 /* free the message queue of a thread at thread exit */
294 void free_msg_queue( struct thread *thread )
295 {
296     struct process *process = thread->process;
297
298     remove_thread_hooks( thread );
299     if (!thread->queue) return;
300     if (process->queue == thread->queue)  /* is it the process main queue? */
301     {
302         release_object( process->queue );
303         process->queue = NULL;
304         if (process->idle_event)
305         {
306             set_event( process->idle_event );
307             release_object( process->idle_event );
308             process->idle_event = NULL;
309         }
310     }
311     release_object( thread->queue );
312     thread->queue = NULL;
313 }
314
315 /* get the hook table for a given thread */
316 struct hook_table *get_queue_hooks( struct thread *thread )
317 {
318     if (!thread->queue) return NULL;
319     return thread->queue->hooks;
320 }
321
322 /* set the hook table for a given thread, allocating the queue if needed */
323 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
324 {
325     struct msg_queue *queue = thread->queue;
326     if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
327     if (queue->hooks) release_object( queue->hooks );
328     queue->hooks = hooks;
329 }
330
331 /* check the queue status */
332 static inline int is_signaled( struct msg_queue *queue )
333 {
334     return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
335 }
336
337 /* set some queue bits */
338 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
339 {
340     queue->wake_bits |= bits;
341     queue->changed_bits |= bits;
342     if (is_signaled( queue )) wake_up( &queue->obj, 0 );
343 }
344
345 /* clear some queue bits */
346 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
347 {
348     queue->wake_bits &= ~bits;
349     queue->changed_bits &= ~bits;
350 }
351
352 /* check whether msg is a keyboard message */
353 static inline int is_keyboard_msg( struct message *msg )
354 {
355     return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
356 }
357
358 /* check if message is matched by the filter */
359 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
360 {
361     return (msg >= first && msg <= last);
362 }
363
364 /* check whether a message filter contains at least one potential hardware message */
365 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
366 {
367     /* hardware message ranges are (in numerical order):
368      *   WM_NCMOUSEFIRST .. WM_NCMOUSELAST
369      *   WM_KEYFIRST .. WM_KEYLAST
370      *   WM_MOUSEFIRST .. WM_MOUSELAST
371      */
372     if (last < WM_NCMOUSEFIRST) return 0;
373     if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
374     if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
375     if (first > WM_MOUSELAST) return 0;
376     return 1;
377 }
378
379 /* get the QS_* bit corresponding to a given hardware message */
380 static inline int get_hardware_msg_bit( struct message *msg )
381 {
382     if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
383     if (is_keyboard_msg( msg )) return QS_KEY;
384     return QS_MOUSEBUTTON;
385 }
386
387 /* get the current thread queue, creating it if needed */
388 static inline struct msg_queue *get_current_queue(void)
389 {
390     struct msg_queue *queue = current->queue;
391     if (!queue) queue = create_msg_queue( current, NULL );
392     return queue;
393 }
394
395 /* get a (pseudo-)unique id to tag hardware messages */
396 static inline unsigned int get_unique_id(void)
397 {
398     static unsigned int id;
399     if (!++id) id = 1;  /* avoid an id of 0 */
400     return id;
401 }
402
403 /* try to merge a message with the last in the list; return 1 if successful */
404 static int merge_message( struct thread_input *input, const struct message *msg )
405 {
406     struct message *prev;
407     struct list *ptr = list_tail( &input->msg_list );
408
409     if (!ptr) return 0;
410     prev = LIST_ENTRY( ptr, struct message, entry );
411     if (prev->result) return 0;
412     if (prev->win && msg->win && prev->win != msg->win) return 0;
413     if (prev->msg != msg->msg) return 0;
414     if (prev->type != msg->type) return 0;
415     /* now we can merge it */
416     prev->wparam  = msg->wparam;
417     prev->lparam  = msg->lparam;
418     prev->x       = msg->x;
419     prev->y       = msg->y;
420     prev->time    = msg->time;
421     prev->info    = msg->info;
422     return 1;
423 }
424
425 /* free a result structure */
426 static void free_result( struct message_result *result )
427 {
428     if (result->timeout) remove_timeout_user( result->timeout );
429     free( result->data );
430     if (result->callback_msg) free_message( result->callback_msg );
431     free( result );
432 }
433
434 /* remove the result from the sender list it is on */
435 static inline void remove_result_from_sender( struct message_result *result )
436 {
437     assert( result->sender );
438
439     list_remove( &result->sender_entry );
440     result->sender = NULL;
441     if (!result->receiver) free_result( result );
442 }
443
444 /* store the message result in the appropriate structure */
445 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
446 {
447     res->result  = result;
448     res->error   = error;
449     res->replied = 1;
450     if (res->timeout)
451     {
452         remove_timeout_user( res->timeout );
453         res->timeout = NULL;
454     }
455     if (res->sender)
456     {
457         if (res->callback_msg)
458         {
459             /* queue the callback message in the sender queue */
460             struct callback_msg_data *data = res->callback_msg->data;
461             data->result = result;
462             list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
463             set_queue_bits( res->sender, QS_SENDMESSAGE );
464             res->callback_msg = NULL;
465             remove_result_from_sender( res );
466         }
467         else
468         {
469             /* wake sender queue if waiting on this result */
470             if (list_head(&res->sender->send_result) == &res->sender_entry)
471                 set_queue_bits( res->sender, QS_SMRESULT );
472         }
473     }
474
475 }
476
477 /* free a message when deleting a queue or window */
478 static void free_message( struct message *msg )
479 {
480     struct message_result *result = msg->result;
481     if (result)
482     {
483         result->msg = NULL;
484         if (result->sender)
485         {
486             result->receiver = NULL;
487             store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
488         }
489         else free_result( result );
490     }
491     free( msg->data );
492     free( msg );
493 }
494
495 /* remove (and free) a message from a message list */
496 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
497                                   enum message_kind kind )
498 {
499     list_remove( &msg->entry );
500     switch(kind)
501     {
502     case SEND_MESSAGE:
503         if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
504         break;
505     case POST_MESSAGE:
506         if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
507             clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
508         break;
509     }
510     free_message( msg );
511 }
512
513 /* message timed out without getting a reply */
514 static void result_timeout( void *private )
515 {
516     struct message_result *result = private;
517
518     assert( !result->replied );
519
520     result->timeout = NULL;
521
522     if (result->msg)  /* not received yet */
523     {
524         struct message *msg = result->msg;
525
526         result->msg = NULL;
527         msg->result = NULL;
528         remove_queue_message( result->receiver, msg, SEND_MESSAGE );
529         result->receiver = NULL;
530         if (!result->sender)
531         {
532             free_result( result );
533             return;
534         }
535     }
536
537     store_message_result( result, 0, STATUS_TIMEOUT );
538 }
539
540 /* allocate and fill a message result structure */
541 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
542                                                     struct msg_queue *recv_queue,
543                                                     struct message *msg, timeout_t timeout )
544 {
545     struct message_result *result = mem_alloc( sizeof(*result) );
546     if (result)
547     {
548         result->msg       = msg;
549         result->sender    = send_queue;
550         result->receiver  = recv_queue;
551         result->replied   = 0;
552         result->data      = NULL;
553         result->data_size = 0;
554         result->timeout   = NULL;
555
556         if (msg->type == MSG_CALLBACK)
557         {
558             struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
559
560             if (!callback_msg)
561             {
562                 free( result );
563                 return NULL;
564             }
565             callback_msg->type      = MSG_CALLBACK_RESULT;
566             callback_msg->win       = msg->win;
567             callback_msg->msg       = msg->msg;
568             callback_msg->wparam    = 0;
569             callback_msg->lparam    = 0;
570             callback_msg->time      = get_tick_count();
571             callback_msg->x         = 0;
572             callback_msg->y         = 0;
573             callback_msg->info      = 0;
574             callback_msg->result    = NULL;
575             /* steal the data from the original message */
576             callback_msg->data      = msg->data;
577             callback_msg->data_size = msg->data_size;
578             msg->data = NULL;
579             msg->data_size = 0;
580
581             result->callback_msg = callback_msg;
582             list_add_head( &send_queue->callback_result, &result->sender_entry );
583         }
584         else
585         {
586             result->callback_msg = NULL;
587             list_add_head( &send_queue->send_result, &result->sender_entry );
588         }
589
590         if (timeout != TIMEOUT_INFINITE)
591             result->timeout = add_timeout_user( timeout, result_timeout, result );
592     }
593     return result;
594 }
595
596 /* receive a message, removing it from the sent queue */
597 static void receive_message( struct msg_queue *queue, struct message *msg,
598                              struct get_message_reply *reply )
599 {
600     struct message_result *result = msg->result;
601
602     reply->total = msg->data_size;
603     if (msg->data_size > get_reply_max_size())
604     {
605         set_error( STATUS_BUFFER_OVERFLOW );
606         return;
607     }
608     reply->type   = msg->type;
609     reply->win    = msg->win;
610     reply->msg    = msg->msg;
611     reply->wparam = msg->wparam;
612     reply->lparam = msg->lparam;
613     reply->x      = msg->x;
614     reply->y      = msg->y;
615     reply->time   = msg->time;
616     reply->info   = msg->info;
617
618     if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
619
620     list_remove( &msg->entry );
621     /* put the result on the receiver result stack */
622     if (result)
623     {
624         result->msg = NULL;
625         result->recv_next  = queue->recv_result;
626         queue->recv_result = result;
627     }
628     free( msg );
629     if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
630 }
631
632 /* set the result of the current received message */
633 static void reply_message( struct msg_queue *queue, lparam_t result,
634                            unsigned int error, int remove, const void *data, data_size_t len )
635 {
636     struct message_result *res = queue->recv_result;
637
638     if (remove)
639     {
640         queue->recv_result = res->recv_next;
641         res->receiver = NULL;
642         if (!res->sender)  /* no one waiting for it */
643         {
644             free_result( res );
645             return;
646         }
647     }
648     if (!res->replied)
649     {
650         if (len && (res->data = memdup( data, len ))) res->data_size = len;
651         store_message_result( res, result, error );
652     }
653 }
654
655 /* retrieve a posted message */
656 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
657                                unsigned int first, unsigned int last, unsigned int flags,
658                                struct get_message_reply *reply )
659 {
660     struct message *msg;
661
662     /* check against the filters */
663     LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
664     {
665         if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
666         if (!check_msg_filter( msg->msg, first, last )) continue;
667         goto found; /* found one */
668     }
669     return 0;
670
671     /* return it to the app */
672 found:
673     reply->total = msg->data_size;
674     if (msg->data_size > get_reply_max_size())
675     {
676         set_error( STATUS_BUFFER_OVERFLOW );
677         return 1;
678     }
679     reply->type   = msg->type;
680     reply->win    = msg->win;
681     reply->msg    = msg->msg;
682     reply->wparam = msg->wparam;
683     reply->lparam = msg->lparam;
684     reply->x      = msg->x;
685     reply->y      = msg->y;
686     reply->time   = msg->time;
687     reply->info   = msg->info;
688
689     if (flags & PM_REMOVE)
690     {
691         if (msg->data)
692         {
693             set_reply_data_ptr( msg->data, msg->data_size );
694             msg->data = NULL;
695             msg->data_size = 0;
696         }
697         remove_queue_message( queue, msg, POST_MESSAGE );
698     }
699     else if (msg->data) set_reply_data( msg->data, msg->data_size );
700
701     return 1;
702 }
703
704 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
705                              struct get_message_reply *reply )
706 {
707     if (queue->quit_message)
708     {
709         reply->total  = 0;
710         reply->type   = MSG_POSTED;
711         reply->win    = 0;
712         reply->msg    = WM_QUIT;
713         reply->wparam = queue->exit_code;
714         reply->lparam = 0;
715         reply->x      = 0;
716         reply->y      = 0;
717         reply->time   = get_tick_count();
718         reply->info   = 0;
719
720         if (flags & PM_REMOVE)
721         {
722             queue->quit_message = 0;
723             if (list_empty( &queue->msg_list[POST_MESSAGE] ))
724                 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
725         }
726         return 1;
727     }
728     else
729         return 0;
730 }
731
732 /* empty a message list and free all the messages */
733 static void empty_msg_list( struct list *list )
734 {
735     struct list *ptr;
736
737     while ((ptr = list_head( list )) != NULL)
738     {
739         struct message *msg = LIST_ENTRY( ptr, struct message, entry );
740         list_remove( &msg->entry );
741         free_message( msg );
742     }
743 }
744
745 /* cleanup all pending results when deleting a queue */
746 static void cleanup_results( struct msg_queue *queue )
747 {
748     struct list *entry;
749
750     while ((entry = list_head( &queue->send_result )) != NULL)
751     {
752         remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
753     }
754
755     while ((entry = list_head( &queue->callback_result )) != NULL)
756     {
757         remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
758     }
759
760     while (queue->recv_result)
761         reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
762 }
763
764 /* check if the thread owning the queue is hung (not checking for messages) */
765 static int is_queue_hung( struct msg_queue *queue )
766 {
767     struct wait_queue_entry *entry;
768
769     if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
770         return 0;  /* less than 5 seconds since last get message -> not hung */
771
772     LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
773     {
774         if (entry->thread->queue == queue)
775             return 0;  /* thread is waiting on queue -> not hung */
776     }
777     return 1;
778 }
779
780 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
781 {
782     struct msg_queue *queue = (struct msg_queue *)obj;
783     struct process *process = entry->thread->process;
784
785     /* a thread can only wait on its own queue */
786     if (entry->thread->queue != queue)
787     {
788         set_error( STATUS_ACCESS_DENIED );
789         return 0;
790     }
791     /* if waiting on the main process queue, set the idle event */
792     if (process->queue == queue)
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 */
797         set_fd_events( queue->fd, POLLIN );
798     add_queue( obj, entry );
799     return 1;
800 }
801
802 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
803 {
804     struct msg_queue *queue = (struct msg_queue *)obj;
805     struct process *process = entry->thread->process;
806
807     remove_queue( obj, entry );
808     if (queue->fd && list_empty( &obj->wait_queue ))  /* last on the queue is gone */
809         set_fd_events( queue->fd, 0 );
810
811     assert( entry->thread->queue == queue );
812
813     /* if waiting on the main process queue, reset the idle event */
814     if (process->queue == queue)
815     {
816         if (process->idle_event) reset_event( process->idle_event );
817     }
818 }
819
820 static void msg_queue_dump( struct object *obj, int verbose )
821 {
822     struct msg_queue *queue = (struct msg_queue *)obj;
823     fprintf( stderr, "Msg queue bits=%x mask=%x\n",
824              queue->wake_bits, queue->wake_mask );
825 }
826
827 static int msg_queue_signaled( struct object *obj, struct thread *thread )
828 {
829     struct msg_queue *queue = (struct msg_queue *)obj;
830     int ret = 0;
831
832     if (queue->fd)
833     {
834         if ((ret = check_fd_events( queue->fd, POLLIN )))
835             /* stop waiting on select() if we are signaled */
836             set_fd_events( queue->fd, 0 );
837         else if (!list_empty( &obj->wait_queue ))
838             /* restart waiting on poll() if we are no longer signaled */
839             set_fd_events( queue->fd, POLLIN );
840     }
841     return ret || is_signaled( queue );
842 }
843
844 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
845 {
846     struct msg_queue *queue = (struct msg_queue *)obj;
847     queue->wake_mask = 0;
848     queue->changed_mask = 0;
849     return 0;  /* Not abandoned */
850 }
851
852 static void msg_queue_destroy( struct object *obj )
853 {
854     struct msg_queue *queue = (struct msg_queue *)obj;
855     struct list *ptr;
856     int i;
857
858     cleanup_results( queue );
859     for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
860
861     while ((ptr = list_head( &queue->pending_timers )))
862     {
863         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
864         list_remove( &timer->entry );
865         free( timer );
866     }
867     while ((ptr = list_head( &queue->expired_timers )))
868     {
869         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
870         list_remove( &timer->entry );
871         free( timer );
872     }
873     if (queue->timeout) remove_timeout_user( queue->timeout );
874     if (queue->input) release_object( queue->input );
875     if (queue->hooks) release_object( queue->hooks );
876     if (queue->fd) release_object( queue->fd );
877 }
878
879 static void msg_queue_poll_event( struct fd *fd, int event )
880 {
881     struct msg_queue *queue = get_fd_user( fd );
882     assert( queue->obj.ops == &msg_queue_ops );
883
884     if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
885     wake_up( &queue->obj, 0 );
886 }
887
888 static void thread_input_dump( struct object *obj, int verbose )
889 {
890     struct thread_input *input = (struct thread_input *)obj;
891     fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
892              input->focus, input->capture, input->active );
893 }
894
895 static void thread_input_destroy( struct object *obj )
896 {
897     struct thread_input *input = (struct thread_input *)obj;
898
899     if (foreground_input == input) foreground_input = NULL;
900     empty_msg_list( &input->msg_list );
901     if (input->desktop) release_object( input->desktop );
902 }
903
904 /* fix the thread input data when a window is destroyed */
905 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
906 {
907     struct thread_input *input = queue->input;
908
909     if (window == input->focus) input->focus = 0;
910     if (window == input->capture) input->capture = 0;
911     if (window == input->active) input->active = 0;
912     if (window == input->menu_owner) input->menu_owner = 0;
913     if (window == input->move_size) input->move_size = 0;
914     if (window == input->caret) set_caret_window( input, 0 );
915 }
916
917 /* check if the specified window can be set in the input data of a given queue */
918 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
919 {
920     struct thread *thread;
921     int ret = 0;
922
923     if (!window) return 1;  /* we can always clear the data */
924
925     if ((thread = get_window_thread( window )))
926     {
927         ret = (queue->input == thread->queue->input);
928         if (!ret) set_error( STATUS_ACCESS_DENIED );
929         release_object( thread );
930     }
931     else set_error( STATUS_INVALID_HANDLE );
932
933     return ret;
934 }
935
936 /* make sure the specified thread has a queue */
937 int init_thread_queue( struct thread *thread )
938 {
939     if (thread->queue) return 1;
940     return (create_msg_queue( thread, NULL ) != NULL);
941 }
942
943 /* attach two thread input data structures */
944 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
945 {
946     struct desktop *desktop;
947     struct thread_input *input;
948
949     if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
950     if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
951     input = (struct thread_input *)grab_object( thread_to->queue->input );
952     if (input->desktop != desktop)
953     {
954         set_error( STATUS_ACCESS_DENIED );
955         release_object( input );
956         release_object( desktop );
957         return 0;
958     }
959     release_object( desktop );
960
961     if (thread_from->queue)
962     {
963         release_thread_input( thread_from );
964         thread_from->queue->input = input;
965     }
966     else
967     {
968         if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
969     }
970     memset( input->keystate, 0, sizeof(input->keystate) );
971     return 1;
972 }
973
974 /* detach two thread input data structures */
975 void detach_thread_input( struct thread *thread_from )
976 {
977     struct thread_input *input;
978
979     if ((input = create_thread_input( thread_from )))
980     {
981         release_thread_input( thread_from );
982         thread_from->queue->input = input;
983     }
984 }
985
986
987 /* set the next timer to expire */
988 static void set_next_timer( struct msg_queue *queue )
989 {
990     struct list *ptr;
991
992     if (queue->timeout)
993     {
994         remove_timeout_user( queue->timeout );
995         queue->timeout = NULL;
996     }
997     if ((ptr = list_head( &queue->pending_timers )))
998     {
999         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1000         queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1001     }
1002     /* set/clear QS_TIMER bit */
1003     if (list_empty( &queue->expired_timers ))
1004         clear_queue_bits( queue, QS_TIMER );
1005     else
1006         set_queue_bits( queue, QS_TIMER );
1007 }
1008
1009 /* find a timer from its window and id */
1010 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1011                                  unsigned int msg, lparam_t id )
1012 {
1013     struct list *ptr;
1014
1015     /* we need to search both lists */
1016
1017     LIST_FOR_EACH( ptr, &queue->pending_timers )
1018     {
1019         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1020         if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1021     }
1022     LIST_FOR_EACH( ptr, &queue->expired_timers )
1023     {
1024         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1025         if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1026     }
1027     return NULL;
1028 }
1029
1030 /* callback for the next timer expiration */
1031 static void timer_callback( void *private )
1032 {
1033     struct msg_queue *queue = private;
1034     struct list *ptr;
1035
1036     queue->timeout = NULL;
1037     /* move on to the next timer */
1038     ptr = list_head( &queue->pending_timers );
1039     list_remove( ptr );
1040     list_add_tail( &queue->expired_timers, ptr );
1041     set_next_timer( queue );
1042 }
1043
1044 /* link a timer at its rightful place in the queue list */
1045 static void link_timer( struct msg_queue *queue, struct timer *timer )
1046 {
1047     struct list *ptr;
1048
1049     for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1050     {
1051         struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1052         if (t->when >= timer->when) break;
1053     }
1054     list_add_before( ptr, &timer->entry );
1055 }
1056
1057 /* remove a timer from the queue timer list and free it */
1058 static void free_timer( struct msg_queue *queue, struct timer *timer )
1059 {
1060     list_remove( &timer->entry );
1061     free( timer );
1062     set_next_timer( queue );
1063 }
1064
1065 /* restart an expired timer */
1066 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1067 {
1068     list_remove( &timer->entry );
1069     while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1070     link_timer( queue, timer );
1071     set_next_timer( queue );
1072 }
1073
1074 /* find an expired timer matching the filtering parameters */
1075 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1076                                          unsigned int get_first, unsigned int get_last,
1077                                          int remove )
1078 {
1079     struct list *ptr;
1080
1081     LIST_FOR_EACH( ptr, &queue->expired_timers )
1082     {
1083         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1084         if (win && timer->win != win) continue;
1085         if (check_msg_filter( timer->msg, get_first, get_last ))
1086         {
1087             if (remove) restart_timer( queue, timer );
1088             return timer;
1089         }
1090     }
1091     return NULL;
1092 }
1093
1094 /* add a timer */
1095 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1096 {
1097     struct timer *timer = mem_alloc( sizeof(*timer) );
1098     if (timer)
1099     {
1100         timer->rate = max( rate, 1 );
1101         timer->when = current_time + (timeout_t)timer->rate * 10000;
1102         link_timer( queue, timer );
1103         /* check if we replaced the next timer */
1104         if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1105     }
1106     return timer;
1107 }
1108
1109 /* change the input key state for a given key */
1110 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1111 {
1112     if (down)
1113     {
1114         if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1115         input->keystate[key] |= 0x80;
1116     }
1117     else input->keystate[key] &= ~0x80;
1118 }
1119
1120 /* update the input key state for a keyboard message */
1121 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1122 {
1123     unsigned char key;
1124     int down = 0;
1125
1126     switch (msg->msg)
1127     {
1128     case WM_LBUTTONDOWN:
1129         down = 1;
1130         /* fall through */
1131     case WM_LBUTTONUP:
1132         set_input_key_state( input, VK_LBUTTON, down );
1133         break;
1134     case WM_MBUTTONDOWN:
1135         down = 1;
1136         /* fall through */
1137     case WM_MBUTTONUP:
1138         set_input_key_state( input, VK_MBUTTON, down );
1139         break;
1140     case WM_RBUTTONDOWN:
1141         down = 1;
1142         /* fall through */
1143     case WM_RBUTTONUP:
1144         set_input_key_state( input, VK_RBUTTON, down );
1145         break;
1146     case WM_XBUTTONDOWN:
1147         down = 1;
1148         /* fall through */
1149     case WM_XBUTTONUP:
1150         if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1151         else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1152         break;
1153     case WM_KEYDOWN:
1154     case WM_SYSKEYDOWN:
1155         down = 1;
1156         /* fall through */
1157     case WM_KEYUP:
1158     case WM_SYSKEYUP:
1159         key = (unsigned char)msg->wparam;
1160         set_input_key_state( input, key, down );
1161         switch(key)
1162         {
1163         case VK_LCONTROL:
1164         case VK_RCONTROL:
1165             down = (input->keystate[VK_LCONTROL] | input->keystate[VK_RCONTROL]) & 0x80;
1166             set_input_key_state( input, VK_CONTROL, down );
1167             break;
1168         case VK_LMENU:
1169         case VK_RMENU:
1170             down = (input->keystate[VK_LMENU] | input->keystate[VK_RMENU]) & 0x80;
1171             set_input_key_state( input, VK_MENU, down );
1172             break;
1173         case VK_LSHIFT:
1174         case VK_RSHIFT:
1175             down = (input->keystate[VK_LSHIFT] | input->keystate[VK_RSHIFT]) & 0x80;
1176             set_input_key_state( input, VK_SHIFT, down );
1177             break;
1178         }
1179         break;
1180     }
1181 }
1182
1183 /* release the hardware message currently being processed by the given thread */
1184 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1185                                       int remove, user_handle_t new_win )
1186 {
1187     struct thread_input *input = queue->input;
1188     struct message *msg;
1189
1190     LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1191     {
1192         if (msg->unique_id == hw_id) break;
1193     }
1194     if (&msg->entry == &input->msg_list) return;  /* not found */
1195
1196     /* clear the queue bit for that message */
1197     if (remove || new_win)
1198     {
1199         struct message *other;
1200         int clr_bit;
1201
1202         clr_bit = get_hardware_msg_bit( msg );
1203         LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1204         {
1205             if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1206             {
1207                 clr_bit = 0;
1208                 break;
1209             }
1210         }
1211         if (clr_bit) clear_queue_bits( queue, clr_bit );
1212     }
1213
1214     if (new_win)  /* set the new window */
1215     {
1216         struct thread *owner = get_window_thread( new_win );
1217         if (owner)
1218         {
1219             msg->win = new_win;
1220             if (owner->queue->input != input)
1221             {
1222                 list_remove( &msg->entry );
1223                 if (msg->msg == WM_MOUSEMOVE && merge_message( owner->queue->input, msg ))
1224                 {
1225                     free_message( msg );
1226                     release_object( owner );
1227                     return;
1228                 }
1229                 list_add_tail( &owner->queue->input->msg_list, &msg->entry );
1230             }
1231             set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1232             remove = 0;
1233             release_object( owner );
1234         }
1235     }
1236     if (remove)
1237     {
1238         update_input_key_state( input, msg );
1239         list_remove( &msg->entry );
1240         free_message( msg );
1241     }
1242 }
1243
1244 /* find the window that should receive a given hardware message */
1245 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1246                                                    unsigned int *msg_code )
1247 {
1248     user_handle_t win = 0;
1249
1250     *msg_code = msg->msg;
1251     if (is_keyboard_msg( msg ))
1252     {
1253         if (input && !(win = input->focus))
1254         {
1255             win = input->active;
1256             if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1257         }
1258     }
1259     else  /* mouse message */
1260     {
1261         if (!input || !(win = input->capture))
1262         {
1263             if (!(win = msg->win) || !is_window_visible( win ))
1264             {
1265                 if (input) win = window_from_point( input->desktop, msg->x, msg->y );
1266             }
1267         }
1268     }
1269     return win;
1270 }
1271
1272 /* queue a hardware message into a given thread input */
1273 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1274 {
1275     user_handle_t win;
1276     struct thread *thread;
1277     struct thread_input *input = queue ? queue->input : foreground_input;
1278     unsigned int msg_code;
1279
1280     last_input_time = get_tick_count();
1281     win = find_hardware_message_window( input, msg, &msg_code );
1282     if (!win || !(thread = get_window_thread(win)))
1283     {
1284         if (input) update_input_key_state( input, msg );
1285         free( msg );
1286         return;
1287     }
1288     input = thread->queue->input;
1289
1290     if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1291     else
1292     {
1293         msg->unique_id = 0;  /* will be set once we return it to the app */
1294         list_add_tail( &input->msg_list, &msg->entry );
1295         set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1296     }
1297     release_object( thread );
1298 }
1299
1300 /* check message filter for a hardware message */
1301 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1302                                     user_handle_t filter_win, unsigned int first, unsigned int last )
1303 {
1304     if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1305     {
1306         /* we can only test the window for a keyboard message since the
1307          * dest window for a mouse message depends on hittest */
1308         if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1309             return 0;
1310         /* the message code is final for a keyboard message, we can simply check it */
1311         return check_msg_filter( msg_code, first, last );
1312     }
1313     else  /* mouse message */
1314     {
1315         /* we need to check all possible values that the message can have in the end */
1316
1317         if (check_msg_filter( msg_code, first, last )) return 1;
1318         if (msg_code == WM_MOUSEWHEEL) return 0;  /* no other possible value for this one */
1319
1320         /* all other messages can become non-client messages */
1321         if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1322
1323         /* clicks can become double-clicks or non-client double-clicks */
1324         if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1325             msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1326         {
1327             if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1328             if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1329         }
1330         return 0;
1331     }
1332 }
1333
1334
1335 /* find a hardware message for the given queue */
1336 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1337                                  unsigned int first, unsigned int last, struct get_message_reply *reply )
1338 {
1339     struct thread_input *input = thread->queue->input;
1340     struct thread *win_thread;
1341     struct list *ptr;
1342     user_handle_t win;
1343     int clear_bits, got_one = 0;
1344     unsigned int msg_code;
1345
1346     ptr = list_head( &input->msg_list );
1347     if (hw_id)
1348     {
1349         while (ptr)
1350         {
1351             struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1352             if (msg->unique_id == hw_id) break;
1353             ptr = list_next( &input->msg_list, ptr );
1354         }
1355         if (!ptr) ptr = list_head( &input->msg_list );
1356         else ptr = list_next( &input->msg_list, ptr );  /* start from the next one */
1357     }
1358
1359     if (ptr == list_head( &input->msg_list ))
1360         clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1361     else
1362         clear_bits = 0;  /* don't clear bits if we don't go through the whole list */
1363
1364     while (ptr)
1365     {
1366         struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1367         ptr = list_next( &input->msg_list, ptr );
1368         win = find_hardware_message_window( input, msg, &msg_code );
1369         if (!win || !(win_thread = get_window_thread( win )))
1370         {
1371             /* no window at all, remove it */
1372             update_input_key_state( input, msg );
1373             list_remove( &msg->entry );
1374             free_message( msg );
1375             continue;
1376         }
1377         if (win_thread != thread)
1378         {
1379             if (win_thread->queue->input == input)
1380             {
1381                 /* wake the other thread */
1382                 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1383                 got_one = 1;
1384             }
1385             else
1386             {
1387                 /* for another thread input, drop it */
1388                 update_input_key_state( input, msg );
1389                 list_remove( &msg->entry );
1390                 free_message( msg );
1391             }
1392             release_object( win_thread );
1393             continue;
1394         }
1395         release_object( win_thread );
1396
1397         /* if we already got a message for another thread, or if it doesn't
1398          * match the filter we skip it */
1399         if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1400         {
1401             clear_bits &= ~get_hardware_msg_bit( msg );
1402             continue;
1403         }
1404         /* now we can return it */
1405         if (!msg->unique_id) msg->unique_id = get_unique_id();
1406         reply->type   = MSG_HARDWARE;
1407         reply->win    = win;
1408         reply->msg    = msg_code;
1409         reply->wparam = msg->wparam;
1410         reply->lparam = msg->lparam;
1411         reply->x      = msg->x;
1412         reply->y      = msg->y;
1413         reply->time   = msg->time;
1414         reply->info   = msg->info;
1415         reply->hw_id  = msg->unique_id;
1416         return 1;
1417     }
1418     /* nothing found, clear the hardware queue bits */
1419     clear_queue_bits( thread->queue, clear_bits );
1420     return 0;
1421 }
1422
1423 /* increment (or decrement if 'incr' is negative) the queue paint count */
1424 void inc_queue_paint_count( struct thread *thread, int incr )
1425 {
1426     struct msg_queue *queue = thread->queue;
1427
1428     assert( queue );
1429
1430     if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1431
1432     if (queue->paint_count)
1433         set_queue_bits( queue, QS_PAINT );
1434     else
1435         clear_queue_bits( queue, QS_PAINT );
1436 }
1437
1438
1439 /* remove all messages and timers belonging to a certain window */
1440 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1441 {
1442     struct msg_queue *queue = thread->queue;
1443     struct list *ptr;
1444     int i;
1445
1446     if (!queue) return;
1447
1448     /* remove timers */
1449
1450     ptr = list_head( &queue->pending_timers );
1451     while (ptr)
1452     {
1453         struct list *next = list_next( &queue->pending_timers, ptr );
1454         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1455         if (timer->win == win) free_timer( queue, timer );
1456         ptr = next;
1457     }
1458     ptr = list_head( &queue->expired_timers );
1459     while (ptr)
1460     {
1461         struct list *next = list_next( &queue->expired_timers, ptr );
1462         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1463         if (timer->win == win) free_timer( queue, timer );
1464         ptr = next;
1465     }
1466
1467     /* remove messages */
1468     for (i = 0; i < NB_MSG_KINDS; i++)
1469     {
1470         struct list *ptr, *next;
1471
1472         LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1473         {
1474             struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1475             if (msg->win == win) remove_queue_message( queue, msg, i );
1476         }
1477     }
1478
1479     thread_input_cleanup_window( queue, win );
1480 }
1481
1482 /* post a message to a window; used by socket handling */
1483 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
1484 {
1485     struct message *msg;
1486     struct thread *thread = get_window_thread( win );
1487
1488     if (!thread) return;
1489
1490     if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1491     {
1492         msg->type      = MSG_POSTED;
1493         msg->win       = get_user_full_handle( win );
1494         msg->msg       = message;
1495         msg->wparam    = wparam;
1496         msg->lparam    = lparam;
1497         msg->time      = get_tick_count();
1498         msg->x         = 0;
1499         msg->y         = 0;
1500         msg->info      = 0;
1501         msg->result    = NULL;
1502         msg->data      = NULL;
1503         msg->data_size = 0;
1504
1505         list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1506         set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1507     }
1508     release_object( thread );
1509 }
1510
1511 /* post a win event */
1512 void post_win_event( struct thread *thread, unsigned int event,
1513                      user_handle_t win, unsigned int object_id,
1514                      unsigned int child_id, void *hook_proc,
1515                      const WCHAR *module, data_size_t module_size,
1516                      user_handle_t hook)
1517 {
1518     struct message *msg;
1519
1520     if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1521     {
1522         struct winevent_msg_data *data;
1523
1524         msg->type      = MSG_WINEVENT;
1525         msg->win       = get_user_full_handle( win );
1526         msg->msg       = event;
1527         msg->wparam    = object_id;
1528         msg->lparam    = child_id;
1529         msg->time      = get_tick_count();
1530         msg->x         = 0;
1531         msg->y         = 0;
1532         msg->info      = 0;
1533         msg->result    = NULL;
1534
1535         if ((data = malloc( sizeof(*data) + module_size )))
1536         {
1537             data->hook = hook;
1538             data->tid  = get_thread_id( current );
1539             data->hook_proc = hook_proc;
1540             memcpy( data + 1, module, module_size );
1541
1542             msg->data = data;
1543             msg->data_size = sizeof(*data) + module_size;
1544
1545             if (debug_level > 1)
1546                 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
1547                          get_thread_id(thread), event, win, object_id, child_id );
1548             list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1549             set_queue_bits( thread->queue, QS_SENDMESSAGE );
1550         }
1551         else
1552             free( msg );
1553     }
1554 }
1555
1556
1557 /* check if the thread owning the window is hung */
1558 DECL_HANDLER(is_window_hung)
1559 {
1560     struct thread *thread;
1561
1562     thread = get_window_thread( req->win );
1563     if (thread)
1564     {
1565         reply->is_hung = is_queue_hung( thread->queue );
1566         release_object( thread );
1567     }
1568     else reply->is_hung = 0;
1569 }
1570
1571
1572 /* get the message queue of the current thread */
1573 DECL_HANDLER(get_msg_queue)
1574 {
1575     struct msg_queue *queue = get_current_queue();
1576
1577     reply->handle = 0;
1578     if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1579 }
1580
1581
1582 /* set the file descriptor associated to the current thread queue */
1583 DECL_HANDLER(set_queue_fd)
1584 {
1585     struct msg_queue *queue = get_current_queue();
1586     struct file *file;
1587     int unix_fd;
1588
1589     if (queue->fd)  /* fd can only be set once */
1590     {
1591         set_error( STATUS_ACCESS_DENIED );
1592         return;
1593     }
1594     if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
1595
1596     if ((unix_fd = get_file_unix_fd( file )) != -1)
1597     {
1598         if ((unix_fd = dup( unix_fd )) != -1)
1599             queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
1600         else
1601             file_set_error();
1602     }
1603     release_object( file );
1604 }
1605
1606
1607 /* set the current message queue wakeup mask */
1608 DECL_HANDLER(set_queue_mask)
1609 {
1610     struct msg_queue *queue = get_current_queue();
1611
1612     if (queue)
1613     {
1614         queue->wake_mask    = req->wake_mask;
1615         queue->changed_mask = req->changed_mask;
1616         reply->wake_bits    = queue->wake_bits;
1617         reply->changed_bits = queue->changed_bits;
1618         if (is_signaled( queue ))
1619         {
1620             /* if skip wait is set, do what would have been done in the subsequent wait */
1621             if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1622             else wake_up( &queue->obj, 0 );
1623         }
1624     }
1625 }
1626
1627
1628 /* get the current message queue status */
1629 DECL_HANDLER(get_queue_status)
1630 {
1631     struct msg_queue *queue = current->queue;
1632     if (queue)
1633     {
1634         reply->wake_bits    = queue->wake_bits;
1635         reply->changed_bits = queue->changed_bits;
1636         if (req->clear) queue->changed_bits = 0;
1637     }
1638     else reply->wake_bits = reply->changed_bits = 0;
1639 }
1640
1641
1642 /* send a message to a thread queue */
1643 DECL_HANDLER(send_message)
1644 {
1645     struct message *msg;
1646     struct msg_queue *send_queue = get_current_queue();
1647     struct msg_queue *recv_queue = NULL;
1648     struct thread *thread = NULL;
1649
1650     if (!(thread = get_thread_from_id( req->id ))) return;
1651
1652     if (!(recv_queue = thread->queue))
1653     {
1654         set_error( STATUS_INVALID_PARAMETER );
1655         release_object( thread );
1656         return;
1657     }
1658     if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1659     {
1660         set_error( STATUS_TIMEOUT );
1661         release_object( thread );
1662         return;
1663     }
1664
1665     if ((msg = mem_alloc( sizeof(*msg) )))
1666     {
1667         msg->type      = req->type;
1668         msg->win       = get_user_full_handle( req->win );
1669         msg->msg       = req->msg;
1670         msg->wparam    = req->wparam;
1671         msg->lparam    = req->lparam;
1672         msg->time      = get_tick_count();
1673         msg->x         = 0;
1674         msg->y         = 0;
1675         msg->info      = 0;
1676         msg->result    = NULL;
1677         msg->data      = NULL;
1678         msg->data_size = get_req_data_size();
1679
1680         if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1681         {
1682             free( msg );
1683             release_object( thread );
1684             return;
1685         }
1686
1687         switch(msg->type)
1688         {
1689         case MSG_OTHER_PROCESS:
1690         case MSG_ASCII:
1691         case MSG_UNICODE:
1692         case MSG_CALLBACK:
1693             if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
1694             {
1695                 free_message( msg );
1696                 break;
1697             }
1698             /* fall through */
1699         case MSG_NOTIFY:
1700             list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1701             set_queue_bits( recv_queue, QS_SENDMESSAGE );
1702             break;
1703         case MSG_POSTED:
1704             list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1705             set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1706             break;
1707         case MSG_HARDWARE:  /* should use send_hardware_message instead */
1708         case MSG_CALLBACK_RESULT:  /* cannot send this one */
1709         default:
1710             set_error( STATUS_INVALID_PARAMETER );
1711             free( msg );
1712             break;
1713         }
1714     }
1715     release_object( thread );
1716 }
1717
1718 /* send a hardware message to a thread queue */
1719 DECL_HANDLER(send_hardware_message)
1720 {
1721     struct message *msg;
1722     struct msg_queue *recv_queue = NULL;
1723     struct thread *thread = NULL;
1724
1725     if (req->id)
1726     {
1727         if (!(thread = get_thread_from_id( req->id ))) return;
1728     }
1729
1730     if (thread && !(recv_queue = thread->queue))
1731     {
1732         set_error( STATUS_INVALID_PARAMETER );
1733         release_object( thread );
1734         return;
1735     }
1736
1737     if ((msg = mem_alloc( sizeof(*msg) )))
1738     {
1739         msg->type      = MSG_HARDWARE;
1740         msg->win       = get_user_full_handle( req->win );
1741         msg->msg       = req->msg;
1742         msg->wparam    = req->wparam;
1743         msg->lparam    = req->lparam;
1744         msg->time      = req->time;
1745         msg->x         = req->x;
1746         msg->y         = req->y;
1747         msg->info      = req->info;
1748         msg->result    = NULL;
1749         msg->data      = NULL;
1750         msg->data_size = 0;
1751         queue_hardware_message( recv_queue, msg );
1752     }
1753     if (thread) release_object( thread );
1754 }
1755
1756 /* post a quit message to the current queue */
1757 DECL_HANDLER(post_quit_message)
1758 {
1759     struct msg_queue *queue = get_current_queue();
1760
1761     if (!queue)
1762         return;
1763
1764     queue->quit_message = 1;
1765     queue->exit_code = req->exit_code;
1766     set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1767 }
1768
1769 /* get a message from the current queue */
1770 DECL_HANDLER(get_message)
1771 {
1772     struct timer *timer;
1773     struct list *ptr;
1774     struct msg_queue *queue = get_current_queue();
1775     user_handle_t get_win = get_user_full_handle( req->get_win );
1776     unsigned int filter = req->flags >> 16;
1777
1778     reply->active_hooks = get_active_hooks();
1779
1780     if (!queue) return;
1781     queue->last_get_msg = current_time;
1782     if (!filter) filter = QS_ALLINPUT;
1783
1784     /* first check for sent messages */
1785     if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1786     {
1787         struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1788         receive_message( queue, msg, reply );
1789         return;
1790     }
1791
1792     /* clear changed bits so we can wait on them if we don't find a message */
1793     if (filter & QS_POSTMESSAGE)
1794     {
1795         queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
1796         if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
1797     }
1798     if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
1799     if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
1800
1801     /* then check for posted messages */
1802     if ((filter & QS_POSTMESSAGE) &&
1803         get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1804         return;
1805
1806     /* only check for quit messages if not posted messages pending.
1807      * note: the quit message isn't filtered */
1808     if (get_quit_message( queue, req->flags, reply ))
1809         return;
1810
1811     /* then check for any raw hardware message */
1812     if ((filter & QS_INPUT) &&
1813         filter_contains_hw_range( req->get_first, req->get_last ) &&
1814         get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1815         return;
1816
1817     /* now check for WM_PAINT */
1818     if ((filter & QS_PAINT) &&
1819         queue->paint_count &&
1820         check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1821         (reply->win = find_window_to_repaint( get_win, current )))
1822     {
1823         reply->type   = MSG_POSTED;
1824         reply->msg    = WM_PAINT;
1825         reply->wparam = 0;
1826         reply->lparam = 0;
1827         reply->x      = 0;
1828         reply->y      = 0;
1829         reply->time   = get_tick_count();
1830         reply->info   = 0;
1831         return;
1832     }
1833
1834     /* now check for timer */
1835     if ((filter & QS_TIMER) &&
1836         (timer = find_expired_timer( queue, get_win, req->get_first,
1837                                      req->get_last, (req->flags & PM_REMOVE) )))
1838     {
1839         reply->type   = MSG_POSTED;
1840         reply->win    = timer->win;
1841         reply->msg    = timer->msg;
1842         reply->wparam = timer->id;
1843         reply->lparam = timer->lparam;
1844         reply->x      = 0;
1845         reply->y      = 0;
1846         reply->time   = get_tick_count();
1847         reply->info   = 0;
1848         return;
1849     }
1850
1851     queue->wake_mask = req->wake_mask;
1852     queue->changed_mask = req->changed_mask;
1853     set_error( STATUS_PENDING );  /* FIXME */
1854 }
1855
1856
1857 /* reply to a sent message */
1858 DECL_HANDLER(reply_message)
1859 {
1860     if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1861     else if (current->queue->recv_result)
1862         reply_message( current->queue, req->result, 0, req->remove,
1863                        get_req_data(), get_req_data_size() );
1864 }
1865
1866
1867 /* accept the current hardware message */
1868 DECL_HANDLER(accept_hardware_message)
1869 {
1870     if (current->queue)
1871         release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1872     else
1873         set_error( STATUS_ACCESS_DENIED );
1874 }
1875
1876
1877 /* retrieve the reply for the last message sent */
1878 DECL_HANDLER(get_message_reply)
1879 {
1880     struct message_result *result;
1881     struct list *entry;
1882     struct msg_queue *queue = current->queue;
1883
1884     if (queue)
1885     {
1886         set_error( STATUS_PENDING );
1887         reply->result = 0;
1888
1889         if (!(entry = list_head( &queue->send_result ))) return;  /* no reply ready */
1890
1891         result = LIST_ENTRY( entry, struct message_result, sender_entry );
1892         if (result->replied || req->cancel)
1893         {
1894             if (result->replied)
1895             {
1896                 reply->result = result->result;
1897                 set_error( result->error );
1898                 if (result->data)
1899                 {
1900                     data_size_t data_len = min( result->data_size, get_reply_max_size() );
1901                     set_reply_data_ptr( result->data, data_len );
1902                     result->data = NULL;
1903                     result->data_size = 0;
1904                 }
1905             }
1906             remove_result_from_sender( result );
1907
1908             entry = list_head( &queue->send_result );
1909             if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1910             else
1911             {
1912                 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1913                 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1914             }
1915         }
1916     }
1917     else set_error( STATUS_ACCESS_DENIED );
1918 }
1919
1920
1921 /* set a window timer */
1922 DECL_HANDLER(set_win_timer)
1923 {
1924     struct timer *timer;
1925     struct msg_queue *queue;
1926     struct thread *thread = NULL;
1927     user_handle_t win = 0;
1928     lparam_t id = req->id;
1929
1930     if (req->win)
1931     {
1932         if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1933         {
1934             set_error( STATUS_INVALID_HANDLE );
1935             return;
1936         }
1937         if (thread->process != current->process)
1938         {
1939             release_object( thread );
1940             set_error( STATUS_ACCESS_DENIED );
1941             return;
1942         }
1943         queue = thread->queue;
1944         /* remove it if it existed already */
1945         if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1946     }
1947     else
1948     {
1949         queue = get_current_queue();
1950         /* look for a timer with this id */
1951         if (id && (timer = find_timer( queue, 0, req->msg, id )))
1952         {
1953             /* free and reuse id */
1954             free_timer( queue, timer );
1955         }
1956         else
1957         {
1958             /* find a free id for it */
1959             do
1960             {
1961                 id = queue->next_timer_id;
1962                 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
1963             }
1964             while (find_timer( queue, 0, req->msg, id ));
1965         }
1966     }
1967
1968     if ((timer = set_timer( queue, req->rate )))
1969     {
1970         timer->win    = win;
1971         timer->msg    = req->msg;
1972         timer->id     = id;
1973         timer->lparam = req->lparam;
1974         reply->id     = id;
1975     }
1976     if (thread) release_object( thread );
1977 }
1978
1979 /* kill a window timer */
1980 DECL_HANDLER(kill_win_timer)
1981 {
1982     struct timer *timer;
1983     struct thread *thread;
1984     user_handle_t win = 0;
1985
1986     if (req->win)
1987     {
1988         if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1989         {
1990             set_error( STATUS_INVALID_HANDLE );
1991             return;
1992         }
1993         if (thread->process != current->process)
1994         {
1995             release_object( thread );
1996             set_error( STATUS_ACCESS_DENIED );
1997             return;
1998         }
1999     }
2000     else thread = (struct thread *)grab_object( current );
2001
2002     if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2003         free_timer( thread->queue, timer );
2004     else
2005         set_error( STATUS_INVALID_PARAMETER );
2006
2007     release_object( thread );
2008 }
2009
2010
2011 /* attach (or detach) thread inputs */
2012 DECL_HANDLER(attach_thread_input)
2013 {
2014     struct thread *thread_from = get_thread_from_id( req->tid_from );
2015     struct thread *thread_to = get_thread_from_id( req->tid_to );
2016
2017     if (!thread_from || !thread_to)
2018     {
2019         if (thread_from) release_object( thread_from );
2020         if (thread_to) release_object( thread_to );
2021         return;
2022     }
2023     if (thread_from != thread_to)
2024     {
2025         if (req->attach) attach_thread_input( thread_from, thread_to );
2026         else
2027         {
2028             if (thread_from->queue && thread_to->queue &&
2029                 thread_from->queue->input == thread_to->queue->input)
2030                 detach_thread_input( thread_from );
2031             else
2032                 set_error( STATUS_ACCESS_DENIED );
2033         }
2034     }
2035     else set_error( STATUS_ACCESS_DENIED );
2036     release_object( thread_from );
2037     release_object( thread_to );
2038 }
2039
2040
2041 /* get thread input data */
2042 DECL_HANDLER(get_thread_input)
2043 {
2044     struct thread *thread = NULL;
2045     struct thread_input *input;
2046
2047     if (req->tid)
2048     {
2049         if (!(thread = get_thread_from_id( req->tid ))) return;
2050         input = thread->queue ? thread->queue->input : NULL;
2051     }
2052     else input = foreground_input;  /* get the foreground thread info */
2053
2054     if (input)
2055     {
2056         reply->focus      = input->focus;
2057         reply->capture    = input->capture;
2058         reply->active     = input->active;
2059         reply->menu_owner = input->menu_owner;
2060         reply->move_size  = input->move_size;
2061         reply->caret      = input->caret;
2062         reply->rect       = input->caret_rect;
2063     }
2064     else
2065     {
2066         reply->focus      = 0;
2067         reply->capture    = 0;
2068         reply->active     = 0;
2069         reply->menu_owner = 0;
2070         reply->move_size  = 0;
2071         reply->caret      = 0;
2072         reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
2073     }
2074     /* foreground window is active window of foreground thread */
2075     reply->foreground = foreground_input ? foreground_input->active : 0;
2076     if (thread) release_object( thread );
2077 }
2078
2079
2080 /* retrieve queue keyboard state for a given thread */
2081 DECL_HANDLER(get_key_state)
2082 {
2083     struct thread *thread;
2084     struct thread_input *input;
2085
2086     if (!(thread = get_thread_from_id( req->tid ))) return;
2087     input = thread->queue ? thread->queue->input : NULL;
2088     if (input)
2089     {
2090         if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
2091         set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
2092     }
2093     release_object( thread );
2094 }
2095
2096
2097 /* set queue keyboard state for a given thread */
2098 DECL_HANDLER(set_key_state)
2099 {
2100     struct thread *thread = NULL;
2101     struct thread_input *input;
2102
2103     if (!(thread = get_thread_from_id( req->tid ))) return;
2104     input = thread->queue ? thread->queue->input : NULL;
2105     if (input)
2106     {
2107         data_size_t size = min( sizeof(input->keystate), get_req_data_size() );
2108         if (size) memcpy( input->keystate, get_req_data(), size );
2109     }
2110     release_object( thread );
2111 }
2112
2113
2114 /* set the system foreground window */
2115 DECL_HANDLER(set_foreground_window)
2116 {
2117     struct thread *thread;
2118     struct msg_queue *queue = get_current_queue();
2119
2120     reply->previous = foreground_input ? foreground_input->active : 0;
2121     reply->send_msg_old = (reply->previous && foreground_input != queue->input);
2122     reply->send_msg_new = FALSE;
2123
2124     if (is_top_level_window( req->handle ) &&
2125         ((thread = get_window_thread( req->handle ))))
2126     {
2127         foreground_input = thread->queue->input;
2128         reply->send_msg_new = (foreground_input != queue->input);
2129         release_object( thread );
2130     }
2131     else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2132 }
2133
2134
2135 /* set the current thread focus window */
2136 DECL_HANDLER(set_focus_window)
2137 {
2138     struct msg_queue *queue = get_current_queue();
2139
2140     reply->previous = 0;
2141     if (queue && check_queue_input_window( queue, req->handle ))
2142     {
2143         reply->previous = queue->input->focus;
2144         queue->input->focus = get_user_full_handle( req->handle );
2145     }
2146 }
2147
2148
2149 /* set the current thread active window */
2150 DECL_HANDLER(set_active_window)
2151 {
2152     struct msg_queue *queue = get_current_queue();
2153
2154     reply->previous = 0;
2155     if (queue && check_queue_input_window( queue, req->handle ))
2156     {
2157         if (!req->handle || make_window_active( req->handle ))
2158         {
2159             reply->previous = queue->input->active;
2160             queue->input->active = get_user_full_handle( req->handle );
2161         }
2162         else set_error( STATUS_INVALID_HANDLE );
2163     }
2164 }
2165
2166
2167 /* set the current thread capture window */
2168 DECL_HANDLER(set_capture_window)
2169 {
2170     struct msg_queue *queue = get_current_queue();
2171
2172     reply->previous = reply->full_handle = 0;
2173     if (queue && check_queue_input_window( queue, req->handle ))
2174     {
2175         struct thread_input *input = queue->input;
2176
2177         reply->previous = input->capture;
2178         input->capture = get_user_full_handle( req->handle );
2179         input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2180         input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2181         reply->full_handle = input->capture;
2182     }
2183 }
2184
2185
2186 /* Set the current thread caret window */
2187 DECL_HANDLER(set_caret_window)
2188 {
2189     struct msg_queue *queue = get_current_queue();
2190
2191     reply->previous = 0;
2192     if (queue && check_queue_input_window( queue, req->handle ))
2193     {
2194         struct thread_input *input = queue->input;
2195
2196         reply->previous  = input->caret;
2197         reply->old_rect  = input->caret_rect;
2198         reply->old_hide  = input->caret_hide;
2199         reply->old_state = input->caret_state;
2200
2201         set_caret_window( input, get_user_full_handle(req->handle) );
2202         input->caret_rect.right  = input->caret_rect.left + req->width;
2203         input->caret_rect.bottom = input->caret_rect.top + req->height;
2204     }
2205 }
2206
2207
2208 /* Set the current thread caret information */
2209 DECL_HANDLER(set_caret_info)
2210 {
2211     struct msg_queue *queue = get_current_queue();
2212     struct thread_input *input;
2213
2214     if (!queue) return;
2215     input = queue->input;
2216     reply->full_handle = input->caret;
2217     reply->old_rect    = input->caret_rect;
2218     reply->old_hide    = input->caret_hide;
2219     reply->old_state   = input->caret_state;
2220
2221     if (req->handle && get_user_full_handle(req->handle) != input->caret)
2222     {
2223         set_error( STATUS_ACCESS_DENIED );
2224         return;
2225     }
2226     if (req->flags & SET_CARET_POS)
2227     {
2228         input->caret_rect.right  += req->x - input->caret_rect.left;
2229         input->caret_rect.bottom += req->y - input->caret_rect.top;
2230         input->caret_rect.left = req->x;
2231         input->caret_rect.top  = req->y;
2232     }
2233     if (req->flags & SET_CARET_HIDE)
2234     {
2235         input->caret_hide += req->hide;
2236         if (input->caret_hide < 0) input->caret_hide = 0;
2237     }
2238     if (req->flags & SET_CARET_STATE)
2239     {
2240         if (req->state == -1) input->caret_state = !input->caret_state;
2241         else input->caret_state = !!req->state;
2242     }
2243 }
2244
2245
2246 /* get the time of the last input event */
2247 DECL_HANDLER(get_last_input_time)
2248 {
2249     reply->time = last_input_time;
2250 }