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