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