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