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