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