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