server: Return the cursor information for a thread input, and use it for GetCursorInfo.
[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 thread_input *input, struct message *msg,
1266                                     struct hardware_msg_data *data )
1267 {
1268     user_handle_t win;
1269     struct thread *thread;
1270     unsigned int msg_code;
1271
1272     last_input_time = get_tick_count();
1273     win = find_hardware_message_window( input, msg, data, &msg_code );
1274     if (!win || !(thread = get_window_thread(win)))
1275     {
1276         if (input) update_input_key_state( input, msg );
1277         free( msg );
1278         return;
1279     }
1280     input = thread->queue->input;
1281
1282     if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1283     else
1284     {
1285         msg->unique_id = 0;  /* will be set once we return it to the app */
1286         list_add_tail( &input->msg_list, &msg->entry );
1287         set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1288     }
1289     release_object( thread );
1290 }
1291
1292 /* check message filter for a hardware message */
1293 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1294                                     user_handle_t filter_win, unsigned int first, unsigned int last )
1295 {
1296     if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1297     {
1298         /* we can only test the window for a keyboard message since the
1299          * dest window for a mouse message depends on hittest */
1300         if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1301             return 0;
1302         /* the message code is final for a keyboard message, we can simply check it */
1303         return check_msg_filter( msg_code, first, last );
1304     }
1305     else  /* mouse message */
1306     {
1307         /* we need to check all possible values that the message can have in the end */
1308
1309         if (check_msg_filter( msg_code, first, last )) return 1;
1310         if (msg_code == WM_MOUSEWHEEL) return 0;  /* no other possible value for this one */
1311
1312         /* all other messages can become non-client messages */
1313         if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1314
1315         /* clicks can become double-clicks or non-client double-clicks */
1316         if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1317             msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1318         {
1319             if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1320             if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1321         }
1322         return 0;
1323     }
1324 }
1325
1326
1327 /* find a hardware message for the given queue */
1328 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1329                                  unsigned int first, unsigned int last, struct get_message_reply *reply )
1330 {
1331     struct thread_input *input = thread->queue->input;
1332     struct thread *win_thread;
1333     struct list *ptr;
1334     user_handle_t win;
1335     int clear_bits, got_one = 0;
1336     unsigned int msg_code;
1337
1338     ptr = list_head( &input->msg_list );
1339     if (hw_id)
1340     {
1341         while (ptr)
1342         {
1343             struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1344             if (msg->unique_id == hw_id) break;
1345             ptr = list_next( &input->msg_list, ptr );
1346         }
1347         if (!ptr) ptr = list_head( &input->msg_list );
1348         else ptr = list_next( &input->msg_list, ptr );  /* start from the next one */
1349     }
1350
1351     if (ptr == list_head( &input->msg_list ))
1352         clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1353     else
1354         clear_bits = 0;  /* don't clear bits if we don't go through the whole list */
1355
1356     while (ptr)
1357     {
1358         struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1359         struct hardware_msg_data *data = msg->data;
1360
1361         ptr = list_next( &input->msg_list, ptr );
1362         win = find_hardware_message_window( input, msg, data, &msg_code );
1363         if (!win || !(win_thread = get_window_thread( win )))
1364         {
1365             /* no window at all, remove it */
1366             update_input_key_state( input, msg );
1367             list_remove( &msg->entry );
1368             free_message( msg );
1369             continue;
1370         }
1371         if (win_thread != thread)
1372         {
1373             if (win_thread->queue->input == input)
1374             {
1375                 /* wake the other thread */
1376                 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1377                 got_one = 1;
1378             }
1379             else
1380             {
1381                 /* for another thread input, drop it */
1382                 update_input_key_state( input, msg );
1383                 list_remove( &msg->entry );
1384                 free_message( msg );
1385             }
1386             release_object( win_thread );
1387             continue;
1388         }
1389         release_object( win_thread );
1390
1391         /* if we already got a message for another thread, or if it doesn't
1392          * match the filter we skip it */
1393         if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1394         {
1395             clear_bits &= ~get_hardware_msg_bit( msg );
1396             continue;
1397         }
1398         /* now we can return it */
1399         if (!msg->unique_id) msg->unique_id = get_unique_id();
1400         reply->type   = MSG_HARDWARE;
1401         reply->win    = win;
1402         reply->msg    = msg_code;
1403         reply->wparam = msg->wparam;
1404         reply->lparam = msg->lparam;
1405         reply->time   = msg->time;
1406
1407         data->hw_id = msg->unique_id;
1408         set_reply_data( msg->data, msg->data_size );
1409         return 1;
1410     }
1411     /* nothing found, clear the hardware queue bits */
1412     clear_queue_bits( thread->queue, clear_bits );
1413     return 0;
1414 }
1415
1416 /* increment (or decrement if 'incr' is negative) the queue paint count */
1417 void inc_queue_paint_count( struct thread *thread, int incr )
1418 {
1419     struct msg_queue *queue = thread->queue;
1420
1421     assert( queue );
1422
1423     if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1424
1425     if (queue->paint_count)
1426         set_queue_bits( queue, QS_PAINT );
1427     else
1428         clear_queue_bits( queue, QS_PAINT );
1429 }
1430
1431
1432 /* remove all messages and timers belonging to a certain window */
1433 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1434 {
1435     struct msg_queue *queue = thread->queue;
1436     struct list *ptr;
1437     int i;
1438
1439     if (!queue) return;
1440
1441     /* remove timers */
1442
1443     ptr = list_head( &queue->pending_timers );
1444     while (ptr)
1445     {
1446         struct list *next = list_next( &queue->pending_timers, ptr );
1447         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1448         if (timer->win == win) free_timer( queue, timer );
1449         ptr = next;
1450     }
1451     ptr = list_head( &queue->expired_timers );
1452     while (ptr)
1453     {
1454         struct list *next = list_next( &queue->expired_timers, ptr );
1455         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1456         if (timer->win == win) free_timer( queue, timer );
1457         ptr = next;
1458     }
1459
1460     /* remove messages */
1461     for (i = 0; i < NB_MSG_KINDS; i++)
1462     {
1463         struct list *ptr, *next;
1464
1465         LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1466         {
1467             struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1468             if (msg->win == win) remove_queue_message( queue, msg, i );
1469         }
1470     }
1471
1472     thread_input_cleanup_window( queue, win );
1473 }
1474
1475 /* post a message to a window; used by socket handling */
1476 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
1477 {
1478     struct message *msg;
1479     struct thread *thread = get_window_thread( win );
1480
1481     if (!thread) return;
1482
1483     if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1484     {
1485         msg->type      = MSG_POSTED;
1486         msg->win       = get_user_full_handle( win );
1487         msg->msg       = message;
1488         msg->wparam    = wparam;
1489         msg->lparam    = lparam;
1490         msg->time      = get_tick_count();
1491         msg->result    = NULL;
1492         msg->data      = NULL;
1493         msg->data_size = 0;
1494
1495         list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1496         set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1497     }
1498     release_object( thread );
1499 }
1500
1501 /* post a win event */
1502 void post_win_event( struct thread *thread, unsigned int event,
1503                      user_handle_t win, unsigned int object_id,
1504                      unsigned int child_id, client_ptr_t hook_proc,
1505                      const WCHAR *module, data_size_t module_size,
1506                      user_handle_t hook)
1507 {
1508     struct message *msg;
1509
1510     if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1511     {
1512         struct winevent_msg_data *data;
1513
1514         msg->type      = MSG_WINEVENT;
1515         msg->win       = get_user_full_handle( win );
1516         msg->msg       = event;
1517         msg->wparam    = object_id;
1518         msg->lparam    = child_id;
1519         msg->time      = get_tick_count();
1520         msg->result    = NULL;
1521
1522         if ((data = malloc( sizeof(*data) + module_size )))
1523         {
1524             data->hook = hook;
1525             data->tid  = get_thread_id( current );
1526             data->hook_proc = hook_proc;
1527             memcpy( data + 1, module, module_size );
1528
1529             msg->data = data;
1530             msg->data_size = sizeof(*data) + module_size;
1531
1532             if (debug_level > 1)
1533                 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
1534                          get_thread_id(thread), event, win, object_id, child_id );
1535             list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1536             set_queue_bits( thread->queue, QS_SENDMESSAGE );
1537         }
1538         else
1539             free( msg );
1540     }
1541 }
1542
1543
1544 /* check if the thread owning the window is hung */
1545 DECL_HANDLER(is_window_hung)
1546 {
1547     struct thread *thread;
1548
1549     thread = get_window_thread( req->win );
1550     if (thread)
1551     {
1552         reply->is_hung = is_queue_hung( thread->queue );
1553         release_object( thread );
1554     }
1555     else reply->is_hung = 0;
1556 }
1557
1558
1559 /* get the message queue of the current thread */
1560 DECL_HANDLER(get_msg_queue)
1561 {
1562     struct msg_queue *queue = get_current_queue();
1563
1564     reply->handle = 0;
1565     if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1566 }
1567
1568
1569 /* set the file descriptor associated to the current thread queue */
1570 DECL_HANDLER(set_queue_fd)
1571 {
1572     struct msg_queue *queue = get_current_queue();
1573     struct file *file;
1574     int unix_fd;
1575
1576     if (queue->fd)  /* fd can only be set once */
1577     {
1578         set_error( STATUS_ACCESS_DENIED );
1579         return;
1580     }
1581     if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
1582
1583     if ((unix_fd = get_file_unix_fd( file )) != -1)
1584     {
1585         if ((unix_fd = dup( unix_fd )) != -1)
1586             queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
1587         else
1588             file_set_error();
1589     }
1590     release_object( file );
1591 }
1592
1593
1594 /* set the current message queue wakeup mask */
1595 DECL_HANDLER(set_queue_mask)
1596 {
1597     struct msg_queue *queue = get_current_queue();
1598
1599     if (queue)
1600     {
1601         queue->wake_mask    = req->wake_mask;
1602         queue->changed_mask = req->changed_mask;
1603         reply->wake_bits    = queue->wake_bits;
1604         reply->changed_bits = queue->changed_bits;
1605         if (is_signaled( queue ))
1606         {
1607             /* if skip wait is set, do what would have been done in the subsequent wait */
1608             if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1609             else wake_up( &queue->obj, 0 );
1610         }
1611     }
1612 }
1613
1614
1615 /* get the current message queue status */
1616 DECL_HANDLER(get_queue_status)
1617 {
1618     struct msg_queue *queue = current->queue;
1619     if (queue)
1620     {
1621         reply->wake_bits    = queue->wake_bits;
1622         reply->changed_bits = queue->changed_bits;
1623         if (req->clear) queue->changed_bits = 0;
1624     }
1625     else reply->wake_bits = reply->changed_bits = 0;
1626 }
1627
1628
1629 /* send a message to a thread queue */
1630 DECL_HANDLER(send_message)
1631 {
1632     struct message *msg;
1633     struct msg_queue *send_queue = get_current_queue();
1634     struct msg_queue *recv_queue = NULL;
1635     struct thread *thread = NULL;
1636
1637     if (!(thread = get_thread_from_id( req->id ))) return;
1638
1639     if (!(recv_queue = thread->queue))
1640     {
1641         set_error( STATUS_INVALID_PARAMETER );
1642         release_object( thread );
1643         return;
1644     }
1645     if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1646     {
1647         set_error( STATUS_TIMEOUT );
1648         release_object( thread );
1649         return;
1650     }
1651
1652     if ((msg = mem_alloc( sizeof(*msg) )))
1653     {
1654         msg->type      = req->type;
1655         msg->win       = get_user_full_handle( req->win );
1656         msg->msg       = req->msg;
1657         msg->wparam    = req->wparam;
1658         msg->lparam    = req->lparam;
1659         msg->time      = get_tick_count();
1660         msg->result    = NULL;
1661         msg->data      = NULL;
1662         msg->data_size = get_req_data_size();
1663
1664         if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1665         {
1666             free( msg );
1667             release_object( thread );
1668             return;
1669         }
1670
1671         switch(msg->type)
1672         {
1673         case MSG_OTHER_PROCESS:
1674         case MSG_ASCII:
1675         case MSG_UNICODE:
1676         case MSG_CALLBACK:
1677             if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
1678             {
1679                 free_message( msg );
1680                 break;
1681             }
1682             /* fall through */
1683         case MSG_NOTIFY:
1684             list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1685             set_queue_bits( recv_queue, QS_SENDMESSAGE );
1686             break;
1687         case MSG_POSTED:
1688             list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1689             set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1690             break;
1691         case MSG_HARDWARE:  /* should use send_hardware_message instead */
1692         case MSG_CALLBACK_RESULT:  /* cannot send this one */
1693         default:
1694             set_error( STATUS_INVALID_PARAMETER );
1695             free( msg );
1696             break;
1697         }
1698     }
1699     release_object( thread );
1700 }
1701
1702 /* send a hardware message to a thread queue */
1703 DECL_HANDLER(send_hardware_message)
1704 {
1705     struct message *msg;
1706     struct thread *thread = NULL;
1707     struct hardware_msg_data *data;
1708     struct thread_input *input = foreground_input;
1709
1710     if (req->id)
1711     {
1712         if (!(thread = get_thread_from_id( req->id ))) return;
1713         if (!thread->queue)
1714         {
1715             set_error( STATUS_INVALID_PARAMETER );
1716             release_object( thread );
1717             return;
1718         }
1719         input = thread->queue->input;
1720     }
1721
1722     if (!(data = mem_alloc( sizeof(*data) )))
1723     {
1724         if (thread) release_object( thread );
1725         return;
1726     }
1727     memset( data, 0, sizeof(*data) );
1728     data->x    = req->x;
1729     data->y    = req->y;
1730     data->info = req->info;
1731
1732     if ((msg = mem_alloc( sizeof(*msg) )))
1733     {
1734         msg->type      = MSG_HARDWARE;
1735         msg->win       = get_user_full_handle( req->win );
1736         msg->msg       = req->msg;
1737         msg->wparam    = req->wparam;
1738         msg->lparam    = req->lparam;
1739         msg->time      = req->time;
1740         msg->result    = NULL;
1741         msg->data      = data;
1742         msg->data_size = sizeof(*data);
1743         queue_hardware_message( input, msg, data );
1744     }
1745     else free( data );
1746
1747     if (thread)
1748     {
1749         reply->cursor = input->cursor;
1750         reply->count  = input->cursor_count;
1751         release_object( thread );
1752     }
1753 }
1754
1755 /* post a quit message to the current queue */
1756 DECL_HANDLER(post_quit_message)
1757 {
1758     struct msg_queue *queue = get_current_queue();
1759
1760     if (!queue)
1761         return;
1762
1763     queue->quit_message = 1;
1764     queue->exit_code = req->exit_code;
1765     set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1766 }
1767
1768 /* get a message from the current queue */
1769 DECL_HANDLER(get_message)
1770 {
1771     struct timer *timer;
1772     struct list *ptr;
1773     struct msg_queue *queue = get_current_queue();
1774     user_handle_t get_win = get_user_full_handle( req->get_win );
1775     unsigned int filter = req->flags >> 16;
1776
1777     reply->active_hooks = get_active_hooks();
1778
1779     if (!queue) return;
1780     queue->last_get_msg = current_time;
1781     if (!filter) filter = QS_ALLINPUT;
1782
1783     /* first check for sent messages */
1784     if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1785     {
1786         struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1787         receive_message( queue, msg, reply );
1788         return;
1789     }
1790
1791     /* clear changed bits so we can wait on them if we don't find a message */
1792     if (filter & QS_POSTMESSAGE)
1793     {
1794         queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
1795         if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
1796     }
1797     if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
1798     if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
1799
1800     /* then check for posted messages */
1801     if ((filter & QS_POSTMESSAGE) &&
1802         get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1803         return;
1804
1805     /* only check for quit messages if not posted messages pending.
1806      * note: the quit message isn't filtered */
1807     if (get_quit_message( queue, req->flags, reply ))
1808         return;
1809
1810     /* then check for any raw hardware message */
1811     if ((filter & QS_INPUT) &&
1812         filter_contains_hw_range( req->get_first, req->get_last ) &&
1813         get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1814         return;
1815
1816     /* now check for WM_PAINT */
1817     if ((filter & QS_PAINT) &&
1818         queue->paint_count &&
1819         check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1820         (reply->win = find_window_to_repaint( get_win, current )))
1821     {
1822         reply->type   = MSG_POSTED;
1823         reply->msg    = WM_PAINT;
1824         reply->wparam = 0;
1825         reply->lparam = 0;
1826         reply->time   = get_tick_count();
1827         return;
1828     }
1829
1830     /* now check for timer */
1831     if ((filter & QS_TIMER) &&
1832         (timer = find_expired_timer( queue, get_win, req->get_first,
1833                                      req->get_last, (req->flags & PM_REMOVE) )))
1834     {
1835         reply->type   = MSG_POSTED;
1836         reply->win    = timer->win;
1837         reply->msg    = timer->msg;
1838         reply->wparam = timer->id;
1839         reply->lparam = timer->lparam;
1840         reply->time   = get_tick_count();
1841         if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
1842             set_event( current->process->idle_event );
1843         return;
1844     }
1845
1846     if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
1847     queue->wake_mask = req->wake_mask;
1848     queue->changed_mask = req->changed_mask;
1849     set_error( STATUS_PENDING );  /* FIXME */
1850 }
1851
1852
1853 /* reply to a sent message */
1854 DECL_HANDLER(reply_message)
1855 {
1856     if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1857     else if (current->queue->recv_result)
1858         reply_message( current->queue, req->result, 0, req->remove,
1859                        get_req_data(), get_req_data_size() );
1860 }
1861
1862
1863 /* accept the current hardware message */
1864 DECL_HANDLER(accept_hardware_message)
1865 {
1866     if (current->queue)
1867         release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1868     else
1869         set_error( STATUS_ACCESS_DENIED );
1870 }
1871
1872
1873 /* retrieve the reply for the last message sent */
1874 DECL_HANDLER(get_message_reply)
1875 {
1876     struct message_result *result;
1877     struct list *entry;
1878     struct msg_queue *queue = current->queue;
1879
1880     if (queue)
1881     {
1882         set_error( STATUS_PENDING );
1883         reply->result = 0;
1884
1885         if (!(entry = list_head( &queue->send_result ))) return;  /* no reply ready */
1886
1887         result = LIST_ENTRY( entry, struct message_result, sender_entry );
1888         if (result->replied || req->cancel)
1889         {
1890             if (result->replied)
1891             {
1892                 reply->result = result->result;
1893                 set_error( result->error );
1894                 if (result->data)
1895                 {
1896                     data_size_t data_len = min( result->data_size, get_reply_max_size() );
1897                     set_reply_data_ptr( result->data, data_len );
1898                     result->data = NULL;
1899                     result->data_size = 0;
1900                 }
1901             }
1902             remove_result_from_sender( result );
1903
1904             entry = list_head( &queue->send_result );
1905             if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1906             else
1907             {
1908                 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1909                 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1910             }
1911         }
1912     }
1913     else set_error( STATUS_ACCESS_DENIED );
1914 }
1915
1916
1917 /* set a window timer */
1918 DECL_HANDLER(set_win_timer)
1919 {
1920     struct timer *timer;
1921     struct msg_queue *queue;
1922     struct thread *thread = NULL;
1923     user_handle_t win = 0;
1924     lparam_t id = req->id;
1925
1926     if (req->win)
1927     {
1928         if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1929         {
1930             set_error( STATUS_INVALID_HANDLE );
1931             return;
1932         }
1933         if (thread->process != current->process)
1934         {
1935             release_object( thread );
1936             set_error( STATUS_ACCESS_DENIED );
1937             return;
1938         }
1939         queue = thread->queue;
1940         /* remove it if it existed already */
1941         if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1942     }
1943     else
1944     {
1945         queue = get_current_queue();
1946         /* look for a timer with this id */
1947         if (id && (timer = find_timer( queue, 0, req->msg, id )))
1948         {
1949             /* free and reuse id */
1950             free_timer( queue, timer );
1951         }
1952         else
1953         {
1954             /* find a free id for it */
1955             do
1956             {
1957                 id = queue->next_timer_id;
1958                 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
1959             }
1960             while (find_timer( queue, 0, req->msg, id ));
1961         }
1962     }
1963
1964     if ((timer = set_timer( queue, req->rate )))
1965     {
1966         timer->win    = win;
1967         timer->msg    = req->msg;
1968         timer->id     = id;
1969         timer->lparam = req->lparam;
1970         reply->id     = id;
1971     }
1972     if (thread) release_object( thread );
1973 }
1974
1975 /* kill a window timer */
1976 DECL_HANDLER(kill_win_timer)
1977 {
1978     struct timer *timer;
1979     struct thread *thread;
1980     user_handle_t win = 0;
1981
1982     if (req->win)
1983     {
1984         if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1985         {
1986             set_error( STATUS_INVALID_HANDLE );
1987             return;
1988         }
1989         if (thread->process != current->process)
1990         {
1991             release_object( thread );
1992             set_error( STATUS_ACCESS_DENIED );
1993             return;
1994         }
1995     }
1996     else thread = (struct thread *)grab_object( current );
1997
1998     if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1999         free_timer( thread->queue, timer );
2000     else
2001         set_error( STATUS_INVALID_PARAMETER );
2002
2003     release_object( thread );
2004 }
2005
2006
2007 /* attach (or detach) thread inputs */
2008 DECL_HANDLER(attach_thread_input)
2009 {
2010     struct thread *thread_from = get_thread_from_id( req->tid_from );
2011     struct thread *thread_to = get_thread_from_id( req->tid_to );
2012
2013     if (!thread_from || !thread_to)
2014     {
2015         if (thread_from) release_object( thread_from );
2016         if (thread_to) release_object( thread_to );
2017         return;
2018     }
2019     if (thread_from != thread_to)
2020     {
2021         if (req->attach) attach_thread_input( thread_from, thread_to );
2022         else
2023         {
2024             if (thread_from->queue && thread_to->queue &&
2025                 thread_from->queue->input == thread_to->queue->input)
2026                 detach_thread_input( thread_from );
2027             else
2028                 set_error( STATUS_ACCESS_DENIED );
2029         }
2030     }
2031     else set_error( STATUS_ACCESS_DENIED );
2032     release_object( thread_from );
2033     release_object( thread_to );
2034 }
2035
2036
2037 /* get thread input data */
2038 DECL_HANDLER(get_thread_input)
2039 {
2040     struct thread *thread = NULL;
2041     struct thread_input *input;
2042
2043     if (req->tid)
2044     {
2045         if (!(thread = get_thread_from_id( req->tid ))) return;
2046         input = thread->queue ? thread->queue->input : NULL;
2047     }
2048     else input = foreground_input;  /* get the foreground thread info */
2049
2050     if (input)
2051     {
2052         reply->focus      = input->focus;
2053         reply->capture    = input->capture;
2054         reply->active     = input->active;
2055         reply->menu_owner = input->menu_owner;
2056         reply->move_size  = input->move_size;
2057         reply->caret      = input->caret;
2058         reply->cursor     = input->cursor;
2059         reply->show_count = input->cursor_count;
2060         reply->rect       = input->caret_rect;
2061     }
2062
2063     /* foreground window is active window of foreground thread */
2064     reply->foreground = foreground_input ? foreground_input->active : 0;
2065     if (thread) release_object( thread );
2066 }
2067
2068
2069 /* retrieve queue keyboard state for a given thread */
2070 DECL_HANDLER(get_key_state)
2071 {
2072     struct thread *thread;
2073     struct thread_input *input;
2074
2075     if (!(thread = get_thread_from_id( req->tid ))) return;
2076     input = thread->queue ? thread->queue->input : NULL;
2077     if (input)
2078     {
2079         if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
2080         set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
2081     }
2082     release_object( thread );
2083 }
2084
2085
2086 /* set queue keyboard state for a given thread */
2087 DECL_HANDLER(set_key_state)
2088 {
2089     struct thread *thread = NULL;
2090     struct thread_input *input;
2091
2092     if (!(thread = get_thread_from_id( req->tid ))) return;
2093     input = thread->queue ? thread->queue->input : NULL;
2094     if (input)
2095     {
2096         data_size_t size = min( sizeof(input->keystate), get_req_data_size() );
2097         if (size) memcpy( input->keystate, get_req_data(), size );
2098     }
2099     release_object( thread );
2100 }
2101
2102
2103 /* set the system foreground window */
2104 DECL_HANDLER(set_foreground_window)
2105 {
2106     struct thread *thread;
2107     struct msg_queue *queue = get_current_queue();
2108
2109     reply->previous = foreground_input ? foreground_input->active : 0;
2110     reply->send_msg_old = (reply->previous && foreground_input != queue->input);
2111     reply->send_msg_new = FALSE;
2112
2113     if (is_top_level_window( req->handle ) &&
2114         ((thread = get_window_thread( req->handle ))))
2115     {
2116         foreground_input = thread->queue->input;
2117         reply->send_msg_new = (foreground_input != queue->input);
2118         release_object( thread );
2119     }
2120     else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2121 }
2122
2123
2124 /* set the current thread focus window */
2125 DECL_HANDLER(set_focus_window)
2126 {
2127     struct msg_queue *queue = get_current_queue();
2128
2129     reply->previous = 0;
2130     if (queue && check_queue_input_window( queue, req->handle ))
2131     {
2132         reply->previous = queue->input->focus;
2133         queue->input->focus = get_user_full_handle( req->handle );
2134     }
2135 }
2136
2137
2138 /* set the current thread active window */
2139 DECL_HANDLER(set_active_window)
2140 {
2141     struct msg_queue *queue = get_current_queue();
2142
2143     reply->previous = 0;
2144     if (queue && check_queue_input_window( queue, req->handle ))
2145     {
2146         if (!req->handle || make_window_active( req->handle ))
2147         {
2148             reply->previous = queue->input->active;
2149             queue->input->active = get_user_full_handle( req->handle );
2150         }
2151         else set_error( STATUS_INVALID_HANDLE );
2152     }
2153 }
2154
2155
2156 /* set the current thread capture window */
2157 DECL_HANDLER(set_capture_window)
2158 {
2159     struct msg_queue *queue = get_current_queue();
2160
2161     reply->previous = reply->full_handle = 0;
2162     if (queue && check_queue_input_window( queue, req->handle ))
2163     {
2164         struct thread_input *input = queue->input;
2165
2166         /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2167         if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2168         {
2169             set_error(STATUS_ACCESS_DENIED);
2170             return;
2171         }
2172         reply->previous = input->capture;
2173         input->capture = get_user_full_handle( req->handle );
2174         input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2175         input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2176         reply->full_handle = input->capture;
2177     }
2178 }
2179
2180
2181 /* Set the current thread caret window */
2182 DECL_HANDLER(set_caret_window)
2183 {
2184     struct msg_queue *queue = get_current_queue();
2185
2186     reply->previous = 0;
2187     if (queue && check_queue_input_window( queue, req->handle ))
2188     {
2189         struct thread_input *input = queue->input;
2190
2191         reply->previous  = input->caret;
2192         reply->old_rect  = input->caret_rect;
2193         reply->old_hide  = input->caret_hide;
2194         reply->old_state = input->caret_state;
2195
2196         set_caret_window( input, get_user_full_handle(req->handle) );
2197         input->caret_rect.right  = input->caret_rect.left + req->width;
2198         input->caret_rect.bottom = input->caret_rect.top + req->height;
2199     }
2200 }
2201
2202
2203 /* Set the current thread caret information */
2204 DECL_HANDLER(set_caret_info)
2205 {
2206     struct msg_queue *queue = get_current_queue();
2207     struct thread_input *input;
2208
2209     if (!queue) return;
2210     input = queue->input;
2211     reply->full_handle = input->caret;
2212     reply->old_rect    = input->caret_rect;
2213     reply->old_hide    = input->caret_hide;
2214     reply->old_state   = input->caret_state;
2215
2216     if (req->handle && get_user_full_handle(req->handle) != input->caret)
2217     {
2218         set_error( STATUS_ACCESS_DENIED );
2219         return;
2220     }
2221     if (req->flags & SET_CARET_POS)
2222     {
2223         input->caret_rect.right  += req->x - input->caret_rect.left;
2224         input->caret_rect.bottom += req->y - input->caret_rect.top;
2225         input->caret_rect.left = req->x;
2226         input->caret_rect.top  = req->y;
2227     }
2228     if (req->flags & SET_CARET_HIDE)
2229     {
2230         input->caret_hide += req->hide;
2231         if (input->caret_hide < 0) input->caret_hide = 0;
2232     }
2233     if (req->flags & SET_CARET_STATE)
2234     {
2235         if (req->state == -1) input->caret_state = !input->caret_state;
2236         else input->caret_state = !!req->state;
2237     }
2238 }
2239
2240
2241 /* get the time of the last input event */
2242 DECL_HANDLER(get_last_input_time)
2243 {
2244     reply->time = last_input_time;
2245 }
2246
2247 /* set/get the current cursor */
2248 DECL_HANDLER(set_cursor)
2249 {
2250     struct msg_queue *queue = get_current_queue();
2251     struct thread_input *input;
2252
2253     if (!queue) return;
2254     input = queue->input;
2255
2256     reply->prev_handle = input->cursor;
2257     reply->prev_count  = input->cursor_count;
2258
2259     if (req->flags & SET_CURSOR_HANDLE)
2260     {
2261         if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
2262         {
2263             set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
2264             return;
2265         }
2266         input->cursor = req->handle;
2267     }
2268
2269     if (req->flags & SET_CURSOR_COUNT)
2270     {
2271         queue->cursor_count += req->show_count;
2272         input->cursor_count += req->show_count;
2273     }
2274 }