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