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