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