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