GetCPInfo should return FALSE if called with NULL info buffer.
[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_NCMBUTTONDBLCLK
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_KEYDOWN:
1036     case WM_SYSKEYDOWN:
1037         down = 1;
1038         /* fall through */
1039     case WM_KEYUP:
1040     case WM_SYSKEYUP:
1041         key = (unsigned char)msg->wparam;
1042         extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1043         set_input_key_state( input, key, down );
1044         switch(key)
1045         {
1046         case VK_SHIFT:
1047             set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1048             break;
1049         case VK_CONTROL:
1050             set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1051             break;
1052         case VK_MENU:
1053             set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1054             break;
1055         }
1056         break;
1057     }
1058 }
1059
1060 /* release the hardware message currently being processed by the given thread */
1061 static void release_hardware_message( struct thread *thread, int remove )
1062 {
1063     struct thread_input *input = thread->queue->input;
1064
1065     if (input->msg_thread != thread) return;
1066     if (remove)
1067     {
1068         struct message *other;
1069         int clr_bit;
1070
1071         update_input_key_state( input, input->msg );
1072         list_remove( &input->msg->entry );
1073         clr_bit = get_hardware_msg_bit( input->msg );
1074         LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1075         {
1076             if (get_hardware_msg_bit( other ) == clr_bit)
1077             {
1078                 clr_bit = 0;
1079                 break;
1080             }
1081         }
1082         if (clr_bit) clear_queue_bits( thread->queue, clr_bit );
1083         free_message( input->msg );
1084     }
1085     release_object( input->msg_thread );
1086     input->msg = NULL;
1087     input->msg_thread = NULL;
1088 }
1089
1090 /* find the window that should receive a given hardware message */
1091 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1092                                                    unsigned int *msg_code )
1093 {
1094     user_handle_t win = 0;
1095
1096     *msg_code = msg->msg;
1097     if (is_keyboard_msg( msg ))
1098     {
1099         if (input && !(win = input->focus))
1100         {
1101             win = input->active;
1102             if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1103         }
1104     }
1105     else  /* mouse message */
1106     {
1107         if (!input || !(win = input->capture))
1108         {
1109             if (!(win = msg->win) || !is_window_visible( win ))
1110                 win = window_from_point( msg->x, msg->y );
1111         }
1112     }
1113     return win;
1114 }
1115
1116 /* queue a hardware message into a given thread input */
1117 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1118 {
1119     user_handle_t win;
1120     struct thread *thread;
1121     struct thread_input *input;
1122     unsigned int msg_code;
1123
1124     win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1125     if (!win || !(thread = get_window_thread(win)))
1126     {
1127         free( msg );
1128         return;
1129     }
1130     input = thread->queue->input;
1131
1132     if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1133     else
1134     {
1135         list_add_tail( &input->msg_list, &msg->entry );
1136         set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1137     }
1138     release_object( thread );
1139 }
1140
1141 /* find a hardware message for the given queue */
1142 static int get_hardware_message( struct thread *thread, int get_next_hw, struct message *first,
1143                                  user_handle_t filter_win, struct get_message_reply *reply )
1144 {
1145     struct thread_input *input = thread->queue->input;
1146     struct thread *win_thread;
1147     struct list *ptr;
1148     user_handle_t win;
1149     int clear_bits, got_one = 0;
1150     unsigned int msg_code;
1151
1152     if (input->msg_thread && ((input->msg_thread != thread) || !get_next_hw))
1153         return 0;  /* locked by another thread, or another recursion level of the same thread */
1154
1155     if (!first)
1156     {
1157         ptr = list_head( &input->msg_list );
1158         clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1159     }
1160     else
1161     {
1162         ptr = list_next( &input->msg_list, &first->entry );
1163         clear_bits = 0;  /* don't clear bits if we don't go through the whole list */
1164     }
1165
1166     while (ptr)
1167     {
1168         struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1169         win = find_hardware_message_window( input, msg, &msg_code );
1170         if (!win || !(win_thread = get_window_thread( win )))
1171         {
1172             /* no window at all, remove it */
1173             ptr = list_next( &input->msg_list, ptr );
1174             update_input_key_state( input, msg );
1175             list_remove( &msg->entry );
1176             free_message( msg );
1177             continue;
1178         }
1179         if (win_thread != thread)
1180         {
1181             /* wake the other thread */
1182             set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1183             release_object( win_thread );
1184             got_one = 1;
1185             ptr = list_next( &input->msg_list, ptr );
1186             continue;
1187         }
1188         /* if we already got a message for another thread, or if it doesn't
1189          * match the filter we skip it (filter is only checked for keyboard
1190          * messages since the dest window for a mouse message depends on hittest)
1191          */
1192         if (got_one ||
1193             (filter_win && is_keyboard_msg(msg) &&
1194              win != filter_win && !is_child_window( filter_win, win )))
1195         {
1196             clear_bits &= ~get_hardware_msg_bit( msg );
1197             release_object( win_thread );
1198             ptr = list_next( &input->msg_list, ptr );
1199             continue;
1200         }
1201         /* now we can return it */
1202         if (!input->msg_thread) input->msg_thread = win_thread;
1203         else release_object( win_thread );
1204         input->msg = msg;
1205
1206         reply->type   = MSG_HARDWARE;
1207         reply->win    = win;
1208         reply->msg    = msg_code;
1209         reply->wparam = msg->wparam;
1210         reply->lparam = msg->lparam;
1211         reply->x      = msg->x;
1212         reply->y      = msg->y;
1213         reply->time   = msg->time;
1214         reply->info   = msg->info;
1215         return 1;
1216     }
1217     /* nothing found, clear the hardware queue bits */
1218     clear_queue_bits( thread->queue, clear_bits );
1219     if (input->msg_thread) release_object( input->msg_thread );
1220     input->msg = NULL;
1221     input->msg_thread = NULL;
1222     return 0;
1223 }
1224
1225 /* increment (or decrement if 'incr' is negative) the queue paint count */
1226 void inc_queue_paint_count( struct thread *thread, int incr )
1227 {
1228     struct msg_queue *queue = thread->queue;
1229
1230     assert( queue );
1231
1232     if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1233
1234     if (queue->paint_count)
1235         set_queue_bits( queue, QS_PAINT );
1236     else
1237         clear_queue_bits( queue, QS_PAINT );
1238 }
1239
1240
1241 /* remove all messages and timers belonging to a certain window */
1242 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1243 {
1244     struct msg_queue *queue = thread->queue;
1245     struct list *ptr;
1246     int i;
1247
1248     if (!queue) return;
1249
1250     /* remove timers */
1251
1252     ptr = list_head( &queue->pending_timers );
1253     while (ptr)
1254     {
1255         struct list *next = list_next( &queue->pending_timers, ptr );
1256         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1257         if (timer->win == win) free_timer( queue, timer );
1258         ptr = next;
1259     }
1260     ptr = list_head( &queue->expired_timers );
1261     while (ptr)
1262     {
1263         struct list *next = list_next( &queue->expired_timers, ptr );
1264         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1265         if (timer->win == win) free_timer( queue, timer );
1266         ptr = next;
1267     }
1268
1269     /* remove messages */
1270     for (i = 0; i < NB_MSG_KINDS; i++)
1271     {
1272         struct list *ptr, *next;
1273
1274         LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1275         {
1276             struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1277             if (msg->win == win) remove_queue_message( queue, msg, i );
1278         }
1279     }
1280
1281     thread_input_cleanup_window( queue, win );
1282 }
1283
1284 /* post a message to a window; used by socket handling */
1285 void post_message( user_handle_t win, unsigned int message,
1286                    unsigned int wparam, unsigned int lparam )
1287 {
1288     struct message *msg;
1289     struct thread *thread = get_window_thread( win );
1290
1291     if (!thread) return;
1292
1293     if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1294     {
1295         msg->type      = MSG_POSTED;
1296         msg->win       = get_user_full_handle( win );
1297         msg->msg       = message;
1298         msg->wparam    = wparam;
1299         msg->lparam    = lparam;
1300         msg->time      = get_tick_count();
1301         msg->x         = 0;
1302         msg->y         = 0;
1303         msg->info      = 0;
1304         msg->hook      = 0;
1305         msg->hook_proc = NULL;
1306         msg->result    = NULL;
1307         msg->data      = NULL;
1308         msg->data_size = 0;
1309
1310         list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1311         set_queue_bits( thread->queue, QS_POSTMESSAGE );
1312     }
1313     release_object( thread );
1314 }
1315
1316 /* post a win event */
1317 void post_win_event( struct thread *thread, unsigned int event,
1318                      user_handle_t win, unsigned int object_id,
1319                      unsigned int child_id, void *hook_proc,
1320                      const WCHAR *module, size_t module_size,
1321                      user_handle_t hook)
1322 {
1323     struct message *msg;
1324
1325     if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1326     {
1327         msg->type      = MSG_WINEVENT;
1328         msg->win       = get_user_full_handle( win );
1329         msg->msg       = event;
1330         msg->wparam    = object_id;
1331         msg->lparam    = child_id;
1332         msg->time      = get_tick_count();
1333         msg->x         = 0;
1334         msg->y         = 0;
1335         msg->info      = get_thread_id( current );
1336         msg->result    = NULL;
1337         msg->hook      = hook;
1338         msg->hook_proc = hook_proc;
1339
1340         if ((msg->data = malloc( module_size )))
1341         {
1342             msg->data_size = module_size;
1343             memcpy( msg->data, module, module_size );
1344
1345             if (debug_level > 1)
1346                 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1347                          get_thread_id(thread), event, win, object_id, child_id );
1348             list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1349             set_queue_bits( thread->queue, QS_SENDMESSAGE );
1350         }
1351         else
1352             free( msg );
1353     }
1354 }
1355
1356 /* get the message queue of the current thread */
1357 DECL_HANDLER(get_msg_queue)
1358 {
1359     struct msg_queue *queue = get_current_queue();
1360
1361     reply->handle = 0;
1362     if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1363 }
1364
1365
1366 /* set the current message queue wakeup mask */
1367 DECL_HANDLER(set_queue_mask)
1368 {
1369     struct msg_queue *queue = get_current_queue();
1370
1371     if (queue)
1372     {
1373         queue->wake_mask    = req->wake_mask;
1374         queue->changed_mask = req->changed_mask;
1375         reply->wake_bits    = queue->wake_bits;
1376         reply->changed_bits = queue->changed_bits;
1377         if (is_signaled( queue ))
1378         {
1379             /* if skip wait is set, do what would have been done in the subsequent wait */
1380             if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1381             else wake_up( &queue->obj, 0 );
1382         }
1383     }
1384 }
1385
1386
1387 /* get the current message queue status */
1388 DECL_HANDLER(get_queue_status)
1389 {
1390     struct msg_queue *queue = current->queue;
1391     if (queue)
1392     {
1393         reply->wake_bits    = queue->wake_bits;
1394         reply->changed_bits = queue->changed_bits;
1395         if (req->clear) queue->changed_bits = 0;
1396     }
1397     else reply->wake_bits = reply->changed_bits = 0;
1398 }
1399
1400
1401 /* send a message to a thread queue */
1402 DECL_HANDLER(send_message)
1403 {
1404     struct message *msg;
1405     struct msg_queue *send_queue = get_current_queue();
1406     struct msg_queue *recv_queue = NULL;
1407     struct thread *thread = NULL;
1408
1409     if (req->id)
1410     {
1411         if (!(thread = get_thread_from_id( req->id ))) return;
1412     }
1413     else if (req->type != MSG_HARDWARE)
1414     {
1415         /* only hardware messages are allowed without destination thread */
1416         set_error( STATUS_INVALID_PARAMETER );
1417         return;
1418     }
1419
1420     if (thread && !(recv_queue = thread->queue))
1421     {
1422         set_error( STATUS_INVALID_PARAMETER );
1423         release_object( thread );
1424         return;
1425     }
1426     if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1427     {
1428         set_error( STATUS_TIMEOUT );
1429         release_object( thread );
1430         return;
1431     }
1432
1433     if ((msg = mem_alloc( sizeof(*msg) )))
1434     {
1435         msg->type      = req->type;
1436         msg->win       = get_user_full_handle( req->win );
1437         msg->msg       = req->msg;
1438         msg->wparam    = req->wparam;
1439         msg->lparam    = req->lparam;
1440         msg->time      = req->time;
1441         msg->x         = req->x;
1442         msg->y         = req->y;
1443         msg->info      = req->info;
1444         msg->hook      = 0;
1445         msg->hook_proc = NULL;
1446         msg->result    = NULL;
1447         msg->data      = NULL;
1448         msg->data_size = 0;
1449
1450         switch(msg->type)
1451         {
1452         case MSG_OTHER_PROCESS:
1453             msg->data_size = get_req_data_size();
1454             if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1455             {
1456                 free( msg );
1457                 break;
1458             }
1459             /* fall through */
1460         case MSG_ASCII:
1461         case MSG_UNICODE:
1462         case MSG_CALLBACK:
1463             if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1464                                                       req->timeout, req->callback, req->info )))
1465             {
1466                 free_message( msg );
1467                 break;
1468             }
1469             /* fall through */
1470         case MSG_NOTIFY:
1471             list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1472             set_queue_bits( recv_queue, QS_SENDMESSAGE );
1473             break;
1474         case MSG_POSTED:
1475             /* needed for posted DDE messages */
1476             msg->data_size = get_req_data_size();
1477             if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1478             {
1479                 free( msg );
1480                 break;
1481             }
1482             list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1483             set_queue_bits( recv_queue, QS_POSTMESSAGE );
1484             break;
1485         case MSG_HARDWARE:
1486             queue_hardware_message( recv_queue, msg );
1487             break;
1488         case MSG_CALLBACK_RESULT:  /* cannot send this one */
1489         default:
1490             set_error( STATUS_INVALID_PARAMETER );
1491             free( msg );
1492             break;
1493         }
1494     }
1495     if (thread) release_object( thread );
1496 }
1497
1498
1499 /* get a message from the current queue */
1500 DECL_HANDLER(get_message)
1501 {
1502     struct timer *timer;
1503     struct list *ptr;
1504     struct message *first_hw_msg = NULL;
1505     struct msg_queue *queue = get_current_queue();
1506     user_handle_t get_win = get_user_full_handle( req->get_win );
1507
1508     if (!queue) return;
1509     gettimeofday( &queue->last_get_msg, NULL );
1510
1511     /* first of all release the hardware input lock if we own it */
1512     /* we'll grab it again if we find a hardware message */
1513     if (queue->input->msg_thread == current && req->get_next_hw)
1514     {
1515         first_hw_msg = queue->input->msg;
1516         release_hardware_message( current, 0 );
1517     }
1518
1519     /* first check for sent messages */
1520     if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1521     {
1522         struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1523         receive_message( queue, msg, reply );
1524         return;
1525     }
1526     if (req->flags & GET_MSG_SENT_ONLY) goto done;  /* nothing else to check */
1527
1528     /* clear changed bits so we can wait on them if we don't find a message */
1529     queue->changed_bits = 0;
1530
1531     /* then check for posted messages */
1532     if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1533         return;
1534
1535     /* then check for any raw hardware message */
1536     if (filter_contains_hw_range( req->get_first, req->get_last ) &&
1537         get_hardware_message( current, req->get_next_hw, first_hw_msg, get_win, reply ))
1538         return;
1539
1540     /* now check for WM_PAINT */
1541     if (queue->paint_count &&
1542         (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
1543         (reply->win = find_window_to_repaint( get_win, current )))
1544     {
1545         reply->type   = MSG_POSTED;
1546         reply->msg    = WM_PAINT;
1547         reply->wparam = 0;
1548         reply->lparam = 0;
1549         reply->x      = 0;
1550         reply->y      = 0;
1551         reply->time   = get_tick_count();
1552         reply->info   = 0;
1553         return;
1554     }
1555
1556     /* now check for timer */
1557     if ((timer = find_expired_timer( queue, get_win, req->get_first,
1558                                      req->get_last, (req->flags & GET_MSG_REMOVE) )))
1559     {
1560         reply->type   = MSG_POSTED;
1561         reply->win    = timer->win;
1562         reply->msg    = timer->msg;
1563         reply->wparam = timer->id;
1564         reply->lparam = timer->lparam;
1565         reply->x      = 0;
1566         reply->y      = 0;
1567         reply->time   = get_tick_count();
1568         reply->info   = 0;
1569         return;
1570     }
1571
1572  done:
1573     set_error( STATUS_PENDING );  /* FIXME */
1574 }
1575
1576
1577 /* reply to a sent message */
1578 DECL_HANDLER(reply_message)
1579 {
1580     if (!current->queue)
1581     {
1582         set_error( STATUS_ACCESS_DENIED );
1583         return;
1584     }
1585     if (req->type == MSG_HARDWARE)
1586     {
1587         struct thread_input *input = current->queue->input;
1588         if (input->msg_thread == current) release_hardware_message( current, req->remove );
1589         else set_error( STATUS_ACCESS_DENIED );
1590     }
1591     else if (current->queue->recv_result)
1592         reply_message( current->queue, req->result, 0, req->remove,
1593                        get_req_data(), get_req_data_size() );
1594 }
1595
1596
1597 /* retrieve the reply for the last message sent */
1598 DECL_HANDLER(get_message_reply)
1599 {
1600     struct message_result *result;
1601     struct list *entry;
1602     struct msg_queue *queue = current->queue;
1603
1604     if (queue)
1605     {
1606         set_error( STATUS_PENDING );
1607         reply->result = 0;
1608
1609         if (!(entry = list_head( &queue->send_result ))) return;  /* no reply ready */
1610
1611         result = LIST_ENTRY( entry, struct message_result, sender_entry );
1612         if (result->replied || req->cancel)
1613         {
1614             if (result->replied)
1615             {
1616                 reply->result = result->result;
1617                 set_error( result->error );
1618                 if (result->data)
1619                 {
1620                     size_t data_len = min( result->data_size, get_reply_max_size() );
1621                     set_reply_data_ptr( result->data, data_len );
1622                     result->data = NULL;
1623                     result->data_size = 0;
1624                 }
1625             }
1626             remove_result_from_sender( result );
1627
1628             entry = list_head( &queue->send_result );
1629             if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1630             else
1631             {
1632                 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1633                 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1634             }
1635         }
1636     }
1637     else set_error( STATUS_ACCESS_DENIED );
1638 }
1639
1640
1641 /* set a window timer */
1642 DECL_HANDLER(set_win_timer)
1643 {
1644     struct timer *timer;
1645     struct msg_queue *queue;
1646     struct thread *thread = NULL;
1647     user_handle_t win = 0;
1648     unsigned int id = req->id;
1649
1650     if (req->win)
1651     {
1652         if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1653         {
1654             set_error( STATUS_INVALID_HANDLE );
1655             return;
1656         }
1657         if (thread->process != current->process)
1658         {
1659             release_object( thread );
1660             set_error( STATUS_ACCESS_DENIED );
1661             return;
1662         }
1663         queue = thread->queue;
1664         /* remove it if it existed already */
1665         if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1666     }
1667     else
1668     {
1669         queue = get_current_queue();
1670         /* find a free id for it */
1671         do
1672         {
1673             id = queue->next_timer_id;
1674             if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1675         }
1676         while (find_timer( queue, 0, req->msg, id ));
1677     }
1678
1679     if ((timer = set_timer( queue, req->rate )))
1680     {
1681         timer->win    = win;
1682         timer->msg    = req->msg;
1683         timer->id     = id;
1684         timer->lparam = req->lparam;
1685         reply->id     = id;
1686     }
1687     if (thread) release_object( thread );
1688 }
1689
1690 /* kill a window timer */
1691 DECL_HANDLER(kill_win_timer)
1692 {
1693     struct timer *timer;
1694     struct thread *thread;
1695     user_handle_t win = 0;
1696
1697     if (req->win)
1698     {
1699         if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1700         {
1701             set_error( STATUS_INVALID_HANDLE );
1702             return;
1703         }
1704         if (thread->process != current->process)
1705         {
1706             release_object( thread );
1707             set_error( STATUS_ACCESS_DENIED );
1708             return;
1709         }
1710     }
1711     else thread = (struct thread *)grab_object( current );
1712
1713     if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1714         free_timer( thread->queue, timer );
1715     else
1716         set_error( STATUS_INVALID_PARAMETER );
1717
1718     release_object( thread );
1719 }
1720
1721
1722 /* attach (or detach) thread inputs */
1723 DECL_HANDLER(attach_thread_input)
1724 {
1725     struct thread *thread_from = get_thread_from_id( req->tid_from );
1726     struct thread *thread_to = get_thread_from_id( req->tid_to );
1727
1728     if (!thread_from || !thread_to)
1729     {
1730         if (thread_from) release_object( thread_from );
1731         if (thread_to) release_object( thread_to );
1732         return;
1733     }
1734     if (thread_from != thread_to)
1735     {
1736         if (req->attach) attach_thread_input( thread_from, thread_to );
1737         else detach_thread_input( thread_from, thread_to );
1738     }
1739     else set_error( STATUS_ACCESS_DENIED );
1740     release_object( thread_from );
1741     release_object( thread_to );
1742 }
1743
1744
1745 /* get thread input data */
1746 DECL_HANDLER(get_thread_input)
1747 {
1748     struct thread *thread = NULL;
1749     struct thread_input *input;
1750
1751     if (req->tid)
1752     {
1753         if (!(thread = get_thread_from_id( req->tid ))) return;
1754         input = thread->queue ? thread->queue->input : NULL;
1755     }
1756     else input = foreground_input;  /* get the foreground thread info */
1757
1758     if (input)
1759     {
1760         reply->focus      = input->focus;
1761         reply->capture    = input->capture;
1762         reply->active     = input->active;
1763         reply->menu_owner = input->menu_owner;
1764         reply->move_size  = input->move_size;
1765         reply->caret      = input->caret;
1766         reply->rect       = input->caret_rect;
1767     }
1768     else
1769     {
1770         reply->focus      = 0;
1771         reply->capture    = 0;
1772         reply->active     = 0;
1773         reply->menu_owner = 0;
1774         reply->move_size  = 0;
1775         reply->caret      = 0;
1776         reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1777     }
1778     /* foreground window is active window of foreground thread */
1779     reply->foreground = foreground_input ? foreground_input->active : 0;
1780     if (thread) release_object( thread );
1781 }
1782
1783
1784 /* retrieve queue keyboard state for a given thread */
1785 DECL_HANDLER(get_key_state)
1786 {
1787     struct thread *thread;
1788     struct thread_input *input;
1789
1790     if (!(thread = get_thread_from_id( req->tid ))) return;
1791     input = thread->queue ? thread->queue->input : NULL;
1792     if (input)
1793     {
1794         if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1795         set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1796     }
1797     release_object( thread );
1798 }
1799
1800
1801 /* set queue keyboard state for a given thread */
1802 DECL_HANDLER(set_key_state)
1803 {
1804     struct thread *thread = NULL;
1805     struct thread_input *input;
1806
1807     if (!(thread = get_thread_from_id( req->tid ))) return;
1808     input = thread->queue ? thread->queue->input : NULL;
1809     if (input)
1810     {
1811         size_t size = min( sizeof(input->keystate), get_req_data_size() );
1812         if (size) memcpy( input->keystate, get_req_data(), size );
1813     }
1814     release_object( thread );
1815 }
1816
1817
1818 /* set the system foreground window */
1819 DECL_HANDLER(set_foreground_window)
1820 {
1821     struct msg_queue *queue = get_current_queue();
1822
1823     reply->previous = foreground_input ? foreground_input->active : 0;
1824     reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1825     reply->send_msg_new = FALSE;
1826
1827     if (req->handle)
1828     {
1829         struct thread *thread;
1830
1831         if (is_top_level_window( req->handle ) &&
1832             ((thread = get_window_thread( req->handle ))))
1833         {
1834             foreground_input = thread->queue->input;
1835             reply->send_msg_new = (foreground_input != queue->input);
1836             release_object( thread );
1837         }
1838         else set_error( STATUS_INVALID_HANDLE );
1839     }
1840     else foreground_input = NULL;
1841 }
1842
1843
1844 /* set the current thread focus window */
1845 DECL_HANDLER(set_focus_window)
1846 {
1847     struct msg_queue *queue = get_current_queue();
1848
1849     reply->previous = 0;
1850     if (queue && check_queue_input_window( queue, req->handle ))
1851     {
1852         reply->previous = queue->input->focus;
1853         queue->input->focus = get_user_full_handle( req->handle );
1854     }
1855 }
1856
1857
1858 /* set the current thread active window */
1859 DECL_HANDLER(set_active_window)
1860 {
1861     struct msg_queue *queue = get_current_queue();
1862
1863     reply->previous = 0;
1864     if (queue && check_queue_input_window( queue, req->handle ))
1865     {
1866         if (!req->handle || make_window_active( req->handle ))
1867         {
1868             reply->previous = queue->input->active;
1869             queue->input->active = get_user_full_handle( req->handle );
1870         }
1871         else set_error( STATUS_INVALID_HANDLE );
1872     }
1873 }
1874
1875
1876 /* set the current thread capture window */
1877 DECL_HANDLER(set_capture_window)
1878 {
1879     struct msg_queue *queue = get_current_queue();
1880
1881     reply->previous = reply->full_handle = 0;
1882     if (queue && check_queue_input_window( queue, req->handle ))
1883     {
1884         struct thread_input *input = queue->input;
1885
1886         reply->previous = input->capture;
1887         input->capture = get_user_full_handle( req->handle );
1888         input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1889         input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1890         reply->full_handle = input->capture;
1891     }
1892 }
1893
1894
1895 /* Set the current thread caret window */
1896 DECL_HANDLER(set_caret_window)
1897 {
1898     struct msg_queue *queue = get_current_queue();
1899
1900     reply->previous = 0;
1901     if (queue && check_queue_input_window( queue, req->handle ))
1902     {
1903         struct thread_input *input = queue->input;
1904
1905         reply->previous  = input->caret;
1906         reply->old_rect  = input->caret_rect;
1907         reply->old_hide  = input->caret_hide;
1908         reply->old_state = input->caret_state;
1909
1910         set_caret_window( input, get_user_full_handle(req->handle) );
1911         input->caret_rect.right  = input->caret_rect.left + req->width;
1912         input->caret_rect.bottom = input->caret_rect.top + req->height;
1913     }
1914 }
1915
1916
1917 /* Set the current thread caret information */
1918 DECL_HANDLER(set_caret_info)
1919 {
1920     struct msg_queue *queue = get_current_queue();
1921     struct thread_input *input;
1922
1923     if (!queue) return;
1924     input = queue->input;
1925     reply->full_handle = input->caret;
1926     reply->old_rect    = input->caret_rect;
1927     reply->old_hide    = input->caret_hide;
1928     reply->old_state   = input->caret_state;
1929
1930     if (req->handle && get_user_full_handle(req->handle) != input->caret)
1931     {
1932         set_error( STATUS_ACCESS_DENIED );
1933         return;
1934     }
1935     if (req->flags & SET_CARET_POS)
1936     {
1937         input->caret_rect.right  += req->x - input->caret_rect.left;
1938         input->caret_rect.bottom += req->y - input->caret_rect.top;
1939         input->caret_rect.left = req->x;
1940         input->caret_rect.top  = req->y;
1941     }
1942     if (req->flags & SET_CARET_HIDE)
1943     {
1944         input->caret_hide += req->hide;
1945         if (input->caret_hide < 0) input->caret_hide = 0;
1946     }
1947     if (req->flags & SET_CARET_STATE)
1948     {
1949         if (req->state == -1) input->caret_state = !input->caret_state;
1950         else input->caret_state = !!req->state;
1951     }
1952 }