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