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