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