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