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