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