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