Do not invalidate the window before the first paint job.
[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 <stdio.h>
26 #include <stdlib.h>
27
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31
32 #include "handle.h"
33 #include "thread.h"
34 #include "process.h"
35 #include "request.h"
36 #include "user.h"
37
38 enum message_kind { SEND_MESSAGE, POST_MESSAGE, COOKED_HW_MESSAGE, RAW_HW_MESSAGE };
39 #define NB_MSG_KINDS (RAW_HW_MESSAGE+1)
40
41
42 struct message_result
43 {
44     struct message_result *send_next;   /* next in sender list */
45     struct message_result *recv_next;   /* next in receiver list */
46     struct msg_queue      *sender;      /* sender queue */
47     struct msg_queue      *receiver;    /* receiver queue */
48     int                    replied;     /* has it been replied to? */
49     unsigned int           result;      /* reply result */
50     unsigned int           error;       /* error code to pass back to sender */
51     void                  *data;        /* message reply data */
52     unsigned int           data_size;   /* size of message reply data */
53     struct timeout_user   *timeout;     /* result timeout */
54 };
55
56 struct message
57 {
58     struct message        *next;      /* next message in list */
59     struct message        *prev;      /* prev message in list */
60     enum message_type      type;      /* message type */
61     user_handle_t          win;       /* window handle */
62     unsigned int           msg;       /* message code */
63     unsigned int           wparam;    /* parameters */
64     unsigned int           lparam;    /* parameters */
65     int                    x;         /* x position */
66     int                    y;         /* y position */
67     unsigned int           time;      /* message time */
68     unsigned int           info;      /* extra info */
69     void                  *data;      /* message data for sent messages */
70     unsigned int           data_size; /* size of message data */
71     struct message_result *result;    /* result in sender queue */
72 };
73
74 struct message_list
75 {
76     struct message *first;     /* head of list */
77     struct message *last;      /* tail of list */
78 };
79
80 struct timer
81 {
82     struct timer   *next;      /* next timer in list */
83     struct timer   *prev;      /* prev timer in list */
84     struct timeval  when;      /* next expiration */
85     unsigned int    rate;      /* timer rate in ms */
86     user_handle_t   win;       /* window handle */
87     unsigned int    msg;       /* message to post */
88     unsigned int    id;        /* timer id */
89     unsigned int    lparam;    /* lparam for message */
90 };
91
92 struct thread_input
93 {
94     struct object          obj;           /* object header */
95     user_handle_t          focus;         /* focus window */
96     user_handle_t          capture;       /* capture window */
97     user_handle_t          active;        /* active window */
98     user_handle_t          menu_owner;    /* current menu owner window */
99     user_handle_t          move_size;     /* current moving/resizing window */
100     user_handle_t          caret;         /* caret window */
101     rectangle_t            caret_rect;    /* caret rectangle */
102     int                    caret_hide;    /* caret hide count */
103     int                    caret_state;   /* caret on/off state */
104     unsigned char          keystate[256]; /* state of each key */
105 };
106
107 struct msg_queue
108 {
109     struct object          obj;           /* object header */
110     unsigned int           wake_bits;     /* wakeup bits */
111     unsigned int           wake_mask;     /* wakeup mask */
112     unsigned int           changed_bits;  /* changed wakeup bits */
113     unsigned int           changed_mask;  /* changed wakeup mask */
114     int                    paint_count;   /* pending paint messages count */
115     struct message_list    msg_list[NB_MSG_KINDS];  /* lists of messages */
116     struct message_result *send_result;   /* stack of sent messages waiting for result */
117     struct message_result *recv_result;   /* stack of received messages waiting for result */
118     struct message        *last_msg;      /* last msg returned to the app and not removed */
119     enum message_kind      last_msg_kind; /* message kind of last_msg */
120     struct timer          *first_timer;   /* head of timer list */
121     struct timer          *last_timer;    /* tail of timer list */
122     struct timer          *next_timer;    /* next timer to expire */
123     struct timeout_user   *timeout;       /* timeout for next timer to expire */
124     struct thread_input   *input;         /* thread input descriptor */
125 };
126
127 static void msg_queue_dump( struct object *obj, int verbose );
128 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
129 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
130 static int msg_queue_signaled( struct object *obj, struct thread *thread );
131 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
132 static void msg_queue_destroy( struct object *obj );
133 static void thread_input_dump( struct object *obj, int verbose );
134 static void thread_input_destroy( struct object *obj );
135 static void timer_callback( void *private );
136
137 static const struct object_ops msg_queue_ops =
138 {
139     sizeof(struct msg_queue),  /* size */
140     msg_queue_dump,            /* dump */
141     msg_queue_add_queue,       /* add_queue */
142     msg_queue_remove_queue,    /* remove_queue */
143     msg_queue_signaled,        /* signaled */
144     msg_queue_satisfied,       /* satisfied */
145     NULL,                      /* get_poll_events */
146     NULL,                      /* poll_event */
147     no_get_fd,                 /* get_fd */
148     no_flush,                  /* flush */
149     no_get_file_info,          /* get_file_info */
150     NULL,                      /* queue_async */
151     msg_queue_destroy          /* destroy */
152 };
153
154
155 static const struct object_ops thread_input_ops =
156 {
157     sizeof(struct thread_input),  /* size */
158     thread_input_dump,            /* dump */
159     no_add_queue,                 /* add_queue */
160     NULL,                         /* remove_queue */
161     NULL,                         /* signaled */
162     NULL,                         /* satisfied */
163     NULL,                         /* get_poll_events */
164     NULL,                         /* poll_event */
165     no_get_fd,                    /* get_fd */
166     no_flush,                     /* flush */
167     no_get_file_info,             /* get_file_info */
168     NULL,                         /* queue_async */
169     thread_input_destroy          /* destroy */
170 };
171
172
173 /* set the caret window in a given thread input */
174 static void set_caret_window( struct thread_input *input, user_handle_t win )
175 {
176     input->caret             = win;
177     input->caret_rect.left   = 0;
178     input->caret_rect.top    = 0;
179     input->caret_rect.right  = 0;
180     input->caret_rect.bottom = 0;
181     input->caret_hide        = 1;
182     input->caret_state       = 0;
183 }
184
185 /* create a thread input object */
186 static struct thread_input *create_thread_input(void)
187 {
188     struct thread_input *input;
189
190     if ((input = alloc_object( &thread_input_ops, -1 )))
191     {
192         input->focus       = 0;
193         input->capture     = 0;
194         input->active      = 0;
195         input->menu_owner  = 0;
196         input->move_size   = 0;
197         set_caret_window( input, 0 );
198         memset( input->keystate, 0, sizeof(input->keystate) );
199     }
200     return input;
201 }
202
203 /* pointer to input structure of foreground thread */
204 static struct thread_input *foreground_input;
205
206 /* create a message queue object */
207 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
208 {
209     struct msg_queue *queue;
210     int i;
211
212     if (!input && !(input = create_thread_input())) return NULL;
213     if ((queue = alloc_object( &msg_queue_ops, -1 )))
214     {
215         queue->wake_bits       = 0;
216         queue->wake_mask       = 0;
217         queue->changed_bits    = 0;
218         queue->changed_mask    = 0;
219         queue->paint_count     = 0;
220         queue->send_result     = NULL;
221         queue->recv_result     = NULL;
222         queue->last_msg        = NULL;
223         queue->first_timer     = NULL;
224         queue->last_timer      = NULL;
225         queue->next_timer      = NULL;
226         queue->timeout         = NULL;
227         queue->input           = (struct thread_input *)grab_object( input );
228         for (i = 0; i < NB_MSG_KINDS; i++)
229             queue->msg_list[i].first = queue->msg_list[i].last = NULL;
230
231         thread->queue = queue;
232         if (!thread->process->queue)
233             thread->process->queue = (struct msg_queue *)grab_object( queue );
234     }
235     release_object( input );
236     return queue;
237 }
238
239 /* free the message queue of a thread at thread exit */
240 void free_msg_queue( struct thread *thread )
241 {
242     struct process *process = thread->process;
243
244     if (!thread->queue) return;
245     if (process->queue == thread->queue)  /* is it the process main queue? */
246     {
247         release_object( process->queue );
248         process->queue = NULL;
249         if (process->idle_event)
250         {
251             set_event( process->idle_event );
252             release_object( process->idle_event );
253             process->idle_event = NULL;
254         }
255     }
256     release_object( thread->queue );
257     thread->queue = NULL;
258 }
259
260 /* check the queue status */
261 inline static int is_signaled( struct msg_queue *queue )
262 {
263     return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
264 }
265
266 /* set some queue bits */
267 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
268 {
269     queue->wake_bits |= bits;
270     queue->changed_bits |= bits;
271     if (is_signaled( queue )) wake_up( &queue->obj, 0 );
272 }
273
274 /* clear some queue bits */
275 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
276 {
277     queue->wake_bits &= ~bits;
278     queue->changed_bits &= ~bits;
279 }
280
281 /* get the QS_* bit corresponding to a given hardware message */
282 inline static int get_hardware_msg_bit( struct message *msg )
283 {
284     if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
285     if (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST) return QS_KEY;
286     return QS_MOUSEBUTTON;
287 }
288
289 /* get the current thread queue, creating it if needed */
290 inline static struct msg_queue *get_current_queue(void)
291 {
292     struct msg_queue *queue = current->queue;
293     if (!queue) queue = create_msg_queue( current, NULL );
294     return queue;
295 }
296
297 /* append a message to the end of a list */
298 inline static void append_message( struct message_list *list, struct message *msg )
299 {
300     msg->next = NULL;
301     if ((msg->prev = list->last)) msg->prev->next = msg;
302     else list->first = msg;
303     list->last = msg;
304 }
305
306 /* unlink a message from a list it */
307 inline static void unlink_message( struct message_list *list, struct message *msg )
308 {
309     if (msg->next) msg->next->prev = msg->prev;
310     else list->last = msg->prev;
311     if (msg->prev) msg->prev->next = msg->next;
312     else list->first = msg->next;
313 }
314
315 /* try to merge a message with the last in the list; return 1 if successful */
316 static int merge_message( struct message_list *list, const struct message *msg )
317 {
318     struct message *prev = list->last;
319
320     if (!prev) return 0;
321     if (prev->result) return 0;
322     if (prev->win != msg->win) return 0;
323     if (prev->msg != msg->msg) return 0;
324     if (prev->type != msg->type) return 0;
325     /* now we can merge it */
326     prev->wparam  = msg->wparam;
327     prev->lparam  = msg->lparam;
328     prev->x       = msg->x;
329     prev->y       = msg->y;
330     prev->time    = msg->time;
331     prev->info    = msg->info;
332     return 1;
333 }
334
335 /* free a result structure */
336 static void free_result( struct message_result *result )
337 {
338     if (result->timeout) remove_timeout_user( result->timeout );
339     if (result->data) free( result->data );
340     free( result );
341 }
342
343 /* store the message result in the appropriate structure */
344 static void store_message_result( struct message_result *res, unsigned int result,
345                                   unsigned int error )
346 {
347     res->result  = result;
348     res->error   = error;
349     res->replied = 1;
350     if (res->timeout)
351     {
352         remove_timeout_user( res->timeout );
353         res->timeout = NULL;
354     }
355     /* wake sender queue if waiting on this result */
356     if (res->sender && res->sender->send_result == res)
357         set_queue_bits( res->sender, QS_SMRESULT );
358 }
359
360 /* free a message when deleting a queue or window */
361 static void free_message( struct message *msg )
362 {
363     struct message_result *result = msg->result;
364     if (result)
365     {
366         if (result->sender)
367         {
368             result->receiver = NULL;
369             store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
370         }
371         else free_result( result );
372     }
373     if (msg->data) free( msg->data );
374     free( msg );
375 }
376
377 /* remove (and free) a message from a message list */
378 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
379                                   enum message_kind kind )
380 {
381     int clr_bit;
382     struct message *other;
383
384     if (queue->last_msg == msg) queue->last_msg = NULL;
385     unlink_message( &queue->msg_list[kind], msg );
386     switch(kind)
387     {
388     case SEND_MESSAGE:
389         if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_SENDMESSAGE );
390         break;
391     case POST_MESSAGE:
392         if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_POSTMESSAGE );
393         break;
394     case COOKED_HW_MESSAGE:
395     case RAW_HW_MESSAGE:
396         clr_bit = get_hardware_msg_bit( msg );
397         for (other = queue->msg_list[kind].first; other; other = other->next)
398             if (get_hardware_msg_bit( other ) == clr_bit) break;
399         if (!other) clear_queue_bits( queue, clr_bit );
400         break;
401     }
402     free_message( msg );
403 }
404
405 /* message timed out without getting a reply */
406 static void result_timeout( void *private )
407 {
408     struct message_result *result = private;
409
410     assert( !result->replied );
411
412     result->timeout = NULL;
413     store_message_result( result, 0, STATUS_TIMEOUT );
414 }
415
416 /* allocate and fill a message result structure */
417 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
418                                                     struct msg_queue *recv_queue,
419                                                     unsigned int timeout )
420 {
421     struct message_result *result = mem_alloc( sizeof(*result) );
422     if (result)
423     {
424         /* put the result on the sender result stack */
425         result->sender    = send_queue;
426         result->receiver  = recv_queue;
427         result->replied   = 0;
428         result->data      = NULL;
429         result->data_size = 0;
430         result->timeout   = NULL;
431         result->send_next = send_queue->send_result;
432         send_queue->send_result = result;
433         if (timeout != -1)
434         {
435             struct timeval when;
436             gettimeofday( &when, 0 );
437             add_timeout( &when, timeout );
438             result->timeout = add_timeout_user( &when, result_timeout, result );
439         }
440     }
441     return result;
442 }
443
444 /* receive a message, removing it from the sent queue */
445 static void receive_message( struct msg_queue *queue, struct message *msg,
446                              struct get_message_reply *reply )
447 {
448     struct message_result *result = msg->result;
449
450     reply->total = msg->data_size;
451     if (msg->data_size > get_reply_max_size())
452     {
453         set_error( STATUS_BUFFER_OVERFLOW );
454         return;
455     }
456     reply->type   = msg->type;
457     reply->win    = msg->win;
458     reply->msg    = msg->msg;
459     reply->wparam = msg->wparam;
460     reply->lparam = msg->lparam;
461     reply->x      = msg->x;
462     reply->y      = msg->y;
463     reply->time   = msg->time;
464     reply->info   = msg->info;
465
466     if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
467
468     unlink_message( &queue->msg_list[SEND_MESSAGE], msg );
469     /* put the result on the receiver result stack */
470     if (result)
471     {
472         result->recv_next  = queue->recv_result;
473         queue->recv_result = result;
474     }
475     free( msg );
476     if (!queue->msg_list[SEND_MESSAGE].first) clear_queue_bits( queue, QS_SENDMESSAGE );
477 }
478
479 /* set the result of the current received message */
480 static void reply_message( struct msg_queue *queue, unsigned int result,
481                            unsigned int error, int remove, const void *data, size_t len )
482 {
483     struct message_result *res = queue->recv_result;
484
485     if (remove)
486     {
487         queue->recv_result = res->recv_next;
488         res->receiver = NULL;
489         if (!res->sender)  /* no one waiting for it */
490         {
491             free_result( res );
492             return;
493         }
494     }
495     if (!res->replied)
496     {
497         if (len && (res->data = memdup( data, len ))) res->data_size = len;
498         store_message_result( res, result, error );
499     }
500 }
501
502 /* empty a message list and free all the messages */
503 static void empty_msg_list( struct message_list *list )
504 {
505     struct message *msg = list->first;
506     while (msg)
507     {
508         struct message *next = msg->next;
509         free_message( msg );
510         msg = next;
511     }
512 }
513
514 /* cleanup all pending results when deleting a queue */
515 static void cleanup_results( struct msg_queue *queue )
516 {
517     struct message_result *result, *next;
518
519     result = queue->send_result;
520     while (result)
521     {
522         next = result->send_next;
523         result->sender = NULL;
524         if (!result->receiver) free_result( result );
525         result = next;
526     }
527
528     while (queue->recv_result)
529         reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
530 }
531
532 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
533 {
534     struct msg_queue *queue = (struct msg_queue *)obj;
535     struct process *process = entry->thread->process;
536
537     /* a thread can only wait on its own queue */
538     if (entry->thread->queue != queue)
539     {
540         set_error( STATUS_ACCESS_DENIED );
541         return 0;
542     }
543     /* if waiting on the main process queue, set the idle event */
544     if (process->queue == queue)
545     {
546         if (process->idle_event) set_event( process->idle_event );
547     }
548     add_queue( obj, entry );
549     return 1;
550 }
551
552 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
553 {
554     struct msg_queue *queue = (struct msg_queue *)obj;
555     struct process *process = entry->thread->process;
556
557     remove_queue( obj, entry );
558
559     assert( entry->thread->queue == queue );
560
561     /* if waiting on the main process queue, reset the idle event */
562     if (process->queue == queue)
563     {
564         if (process->idle_event) reset_event( process->idle_event );
565     }
566 }
567
568 static void msg_queue_dump( struct object *obj, int verbose )
569 {
570     struct msg_queue *queue = (struct msg_queue *)obj;
571     fprintf( stderr, "Msg queue bits=%x mask=%x\n",
572              queue->wake_bits, queue->wake_mask );
573 }
574
575 static int msg_queue_signaled( struct object *obj, struct thread *thread )
576 {
577     struct msg_queue *queue = (struct msg_queue *)obj;
578     return is_signaled( queue );
579 }
580
581 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
582 {
583     struct msg_queue *queue = (struct msg_queue *)obj;
584     queue->wake_mask = 0;
585     queue->changed_mask = 0;
586     return 0;  /* Not abandoned */
587 }
588
589 static void msg_queue_destroy( struct object *obj )
590 {
591     struct msg_queue *queue = (struct msg_queue *)obj;
592     struct timer *timer = queue->first_timer;
593     int i;
594
595     cleanup_results( queue );
596     for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
597
598     while (timer)
599     {
600         struct timer *next = timer->next;
601         free( timer );
602         timer = next;
603     }
604     if (queue->timeout) remove_timeout_user( queue->timeout );
605     if (queue->input) release_object( queue->input );
606 }
607
608 static void thread_input_dump( struct object *obj, int verbose )
609 {
610     struct thread_input *input = (struct thread_input *)obj;
611     fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
612              input->focus, input->capture, input->active );
613 }
614
615 static void thread_input_destroy( struct object *obj )
616 {
617     struct thread_input *input = (struct thread_input *)obj;
618
619     if (foreground_input == input) foreground_input = NULL;
620 }
621
622 /* fix the thread input data when a window is destroyed */
623 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
624 {
625     struct thread_input *input = queue->input;
626
627     if (window == input->focus) input->focus = 0;
628     if (window == input->capture) input->capture = 0;
629     if (window == input->active) input->active = 0;
630     if (window == input->menu_owner) input->menu_owner = 0;
631     if (window == input->move_size) input->move_size = 0;
632     if (window == input->caret) set_caret_window( input, 0 );
633 }
634
635 /* check if the specified window can be set in the input data of a given queue */
636 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
637 {
638     struct thread *thread;
639     int ret = 0;
640
641     if (!window) return 1;  /* we can always clear the data */
642
643     if ((thread = get_window_thread( window )))
644     {
645         ret = (queue->input == thread->queue->input);
646         if (!ret) set_error( STATUS_ACCESS_DENIED );
647         release_object( thread );
648     }
649     else set_error( STATUS_INVALID_HANDLE );
650
651     return ret;
652 }
653
654 /* attach two thread input data structures */
655 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
656 {
657     struct thread_input *input;
658
659     if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
660     input = (struct thread_input *)grab_object( thread_to->queue->input );
661
662     if (thread_from->queue)
663     {
664         release_object( thread_from->queue->input );
665         thread_from->queue->input = input;
666     }
667     else
668     {
669         if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
670     }
671     memset( input->keystate, 0, sizeof(input->keystate) );
672     return 1;
673 }
674
675 /* detach two thread input data structures */
676 static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
677 {
678     struct thread_input *input;
679
680     if (!thread_from->queue || !thread_to->queue ||
681         thread_from->queue->input != thread_to->queue->input)
682     {
683         set_error( STATUS_ACCESS_DENIED );
684         return;
685     }
686     if ((input = create_thread_input()))
687     {
688         release_object( thread_from->queue->input );
689         thread_from->queue->input = input;
690     }
691 }
692
693
694 /* set the next timer to expire */
695 static void set_next_timer( struct msg_queue *queue, struct timer *timer )
696 {
697     if (queue->timeout)
698     {
699         remove_timeout_user( queue->timeout );
700         queue->timeout = NULL;
701     }
702     if ((queue->next_timer = timer))
703         queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
704
705     /* set/clear QS_TIMER bit */
706     if (queue->next_timer == queue->first_timer)
707         clear_queue_bits( queue, QS_TIMER );
708     else
709         set_queue_bits( queue, QS_TIMER );
710 }
711
712 /* callback for the next timer expiration */
713 static void timer_callback( void *private )
714 {
715     struct msg_queue *queue = private;
716
717     queue->timeout = NULL;
718     /* move on to the next timer */
719     set_next_timer( queue, queue->next_timer->next );
720 }
721
722 /* link a timer at its rightful place in the queue list */
723 static void link_timer( struct msg_queue *queue, struct timer *timer )
724 {
725     struct timer *pos = queue->next_timer;
726
727     while (pos && time_before( &pos->when, &timer->when )) pos = pos->next;
728
729     if (pos) /* insert before pos */
730     {
731         if ((timer->prev = pos->prev)) timer->prev->next = timer;
732         else queue->first_timer = timer;
733         timer->next = pos;
734         pos->prev = timer;
735     }
736     else  /* insert at end */
737     {
738         timer->next = NULL;
739         timer->prev = queue->last_timer;
740         if (queue->last_timer) queue->last_timer->next = timer;
741         else queue->first_timer = timer;
742         queue->last_timer = timer;
743     }
744     /* check if we replaced the next timer */
745     if (pos == queue->next_timer) set_next_timer( queue, timer );
746 }
747
748 /* remove a timer from the queue timer list */
749 static void unlink_timer( struct msg_queue *queue, struct timer *timer )
750 {
751     if (timer->next) timer->next->prev = timer->prev;
752     else queue->last_timer = timer->prev;
753     if (timer->prev) timer->prev->next = timer->next;
754     else queue->first_timer = timer->next;
755     /* check if we removed the next timer */
756     if (queue->next_timer == timer) set_next_timer( queue, timer->next );
757     else if (queue->next_timer == queue->first_timer) clear_queue_bits( queue, QS_TIMER );
758 }
759
760 /* restart an expired timer */
761 static void restart_timer( struct msg_queue *queue, struct timer *timer )
762 {
763     struct timeval now;
764     unlink_timer( queue, timer );
765     gettimeofday( &now, 0 );
766     while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
767     link_timer( queue, timer );
768 }
769
770 /* find an expired timer matching the filtering parameters */
771 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
772                                          unsigned int get_first, unsigned int get_last,
773                                          int remove )
774 {
775     struct timer *timer;
776     for (timer = queue->first_timer; (timer && timer != queue->next_timer); timer = timer->next)
777     {
778         if (win && timer->win != win) continue;
779         if (timer->msg >= get_first && timer->msg <= get_last)
780         {
781             if (remove) restart_timer( queue, timer );
782             return timer;
783         }
784     }
785     return NULL;
786 }
787
788 /* kill a timer */
789 static int kill_timer( struct msg_queue *queue, user_handle_t win,
790                        unsigned int msg, unsigned int id )
791 {
792     struct timer *timer;
793
794     for (timer = queue->first_timer; timer; timer = timer->next)
795     {
796         if (timer->win != win || timer->msg != msg || timer->id != id) continue;
797         unlink_timer( queue, timer );
798         free( timer );
799         return 1;
800     }
801     return 0;
802 }
803
804 /* add a timer */
805 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
806 {
807     struct timer *timer = mem_alloc( sizeof(*timer) );
808     if (timer)
809     {
810         timer->rate  = rate;
811         gettimeofday( &timer->when, 0 );
812         add_timeout( &timer->when, rate );
813         link_timer( queue, timer );
814     }
815     return timer;
816 }
817
818
819 /* increment (or decrement if 'incr' is negative) the queue paint count */
820 void inc_queue_paint_count( struct thread *thread, int incr )
821 {
822     struct msg_queue *queue = thread->queue;
823
824     assert( queue );
825
826     if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
827
828     if (queue->paint_count)
829         set_queue_bits( queue, QS_PAINT );
830     else
831         clear_queue_bits( queue, QS_PAINT );
832 }
833
834
835 /* remove all messages and timers belonging to a certain window */
836 void queue_cleanup_window( struct thread *thread, user_handle_t win )
837 {
838     struct msg_queue *queue = thread->queue;
839     struct timer *timer;
840     struct message *msg;
841     int i;
842
843     if (!queue) return;
844
845     /* remove timers */
846     timer = queue->first_timer;
847     while (timer)
848     {
849         struct timer *next = timer->next;
850         if (timer->win == win)
851         {
852             unlink_timer( queue, timer );
853             free( timer );
854         }
855         timer = next;
856     }
857
858     /* remove messages */
859     for (i = 0; i < NB_MSG_KINDS; i++)
860     {
861         msg = queue->msg_list[i].first;
862         while (msg)
863         {
864             struct message *next = msg->next;
865             if (msg->win == win) remove_queue_message( queue, msg, i );
866             msg = next;
867         }
868     }
869
870     thread_input_cleanup_window( queue, win );
871 }
872
873 /* post a message to a window; used by socket handling */
874 void post_message( user_handle_t win, unsigned int message,
875                    unsigned int wparam, unsigned int lparam )
876 {
877     struct message *msg;
878     struct thread *thread = get_window_thread( win );
879
880     if (!thread) return;
881
882     if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
883     {
884         msg->type      = MSG_POSTED;
885         msg->win       = get_user_full_handle( win );
886         msg->msg       = message;
887         msg->wparam    = wparam;
888         msg->lparam    = lparam;
889         msg->time      = get_tick_count();
890         msg->x         = 0;
891         msg->y         = 0;
892         msg->info      = 0;
893         msg->result    = NULL;
894         msg->data      = NULL;
895         msg->data_size = 0;
896
897         append_message( &thread->queue->msg_list[POST_MESSAGE], msg );
898         set_queue_bits( thread->queue, QS_POSTMESSAGE );
899     }
900     release_object( thread );
901 }
902
903
904 /* get the message queue of the current thread */
905 DECL_HANDLER(get_msg_queue)
906 {
907     struct msg_queue *queue = get_current_queue();
908
909     reply->handle = 0;
910     if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
911 }
912
913
914 /* set the current message queue wakeup mask */
915 DECL_HANDLER(set_queue_mask)
916 {
917     struct msg_queue *queue = get_current_queue();
918
919     if (queue)
920     {
921         queue->wake_mask    = req->wake_mask;
922         queue->changed_mask = req->changed_mask;
923         reply->wake_bits    = queue->wake_bits;
924         reply->changed_bits = queue->changed_bits;
925         if (is_signaled( queue ))
926         {
927             /* if skip wait is set, do what would have been done in the subsequent wait */
928             if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
929             else wake_up( &queue->obj, 0 );
930         }
931     }
932 }
933
934
935 /* get the current message queue status */
936 DECL_HANDLER(get_queue_status)
937 {
938     struct msg_queue *queue = current->queue;
939     if (queue)
940     {
941         reply->wake_bits    = queue->wake_bits;
942         reply->changed_bits = queue->changed_bits;
943         if (req->clear) queue->changed_bits = 0;
944     }
945     else reply->wake_bits = reply->changed_bits = 0;
946 }
947
948
949 /* send a message to a thread queue */
950 DECL_HANDLER(send_message)
951 {
952     struct message *msg;
953     struct msg_queue *send_queue = get_current_queue();
954     struct msg_queue *recv_queue;
955     struct thread *thread = get_thread_from_id( req->id );
956
957     if (!thread) return;
958
959     if (!(recv_queue = thread->queue))
960     {
961         set_error( STATUS_INVALID_PARAMETER );
962         release_object( thread );
963         return;
964     }
965
966     if ((msg = mem_alloc( sizeof(*msg) )))
967     {
968         msg->type      = req->type;
969         msg->win       = get_user_full_handle( req->win );
970         msg->msg       = req->msg;
971         msg->wparam    = req->wparam;
972         msg->lparam    = req->lparam;
973         msg->time      = req->time;
974         msg->x         = req->x;
975         msg->y         = req->y;
976         msg->info      = req->info;
977         msg->result    = NULL;
978         msg->data      = NULL;
979         msg->data_size = 0;
980
981         switch(msg->type)
982         {
983         case MSG_OTHER_PROCESS:
984             msg->data_size = get_req_data_size();
985             if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
986             {
987                 free( msg );
988                 break;
989             }
990             /* fall through */
991         case MSG_ASCII:
992         case MSG_UNICODE:
993         case MSG_CALLBACK:
994             if (!(msg->result = alloc_message_result( send_queue, recv_queue, req->timeout )))
995             {
996                 free( msg );
997                 break;
998             }
999             /* fall through */
1000         case MSG_NOTIFY:
1001             append_message( &recv_queue->msg_list[SEND_MESSAGE], msg );
1002             set_queue_bits( recv_queue, QS_SENDMESSAGE );
1003             break;
1004         case MSG_POSTED:
1005             /* needed for posted DDE messages */
1006             msg->data_size = get_req_data_size();
1007             if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1008             {
1009                 free( msg );
1010                 break;
1011             }
1012             append_message( &recv_queue->msg_list[POST_MESSAGE], msg );
1013             set_queue_bits( recv_queue, QS_POSTMESSAGE );
1014             break;
1015         case MSG_HARDWARE_RAW:
1016         case MSG_HARDWARE_COOKED:
1017             {
1018                 struct message_list *list = ((msg->type == MSG_HARDWARE_RAW) ?
1019                                              &recv_queue->msg_list[RAW_HW_MESSAGE] :
1020                                              &recv_queue->msg_list[COOKED_HW_MESSAGE]);
1021                 if (msg->msg == WM_MOUSEMOVE && merge_message( list, msg ))
1022                 {
1023                     free( msg );
1024                     break;
1025                 }
1026                 append_message( list, msg );
1027                 set_queue_bits( recv_queue, get_hardware_msg_bit(msg) );
1028                 break;
1029             }
1030         default:
1031             set_error( STATUS_INVALID_PARAMETER );
1032             free( msg );
1033             break;
1034         }
1035     }
1036     release_object( thread );
1037 }
1038
1039 /* return a message to the application, removing it from the queue if needed */
1040 static void return_message_to_app( struct msg_queue *queue, int flags,
1041                                    struct get_message_reply *reply,
1042                                    struct message *msg, enum message_kind kind )
1043 {
1044     reply->total = msg->data_size;
1045     if (msg->data_size > get_reply_max_size())
1046     {
1047         set_error( STATUS_BUFFER_OVERFLOW );
1048         return;
1049     }
1050     reply->type   = msg->type;
1051     reply->win    = msg->win;
1052     reply->msg    = msg->msg;
1053     reply->wparam = msg->wparam;
1054     reply->lparam = msg->lparam;
1055     reply->x      = msg->x;
1056     reply->y      = msg->y;
1057     reply->time   = msg->time;
1058     reply->info   = msg->info;
1059
1060     /* raw messages always get removed */
1061     if ((msg->type == MSG_HARDWARE_RAW) || (flags & GET_MSG_REMOVE))
1062     {
1063         queue->last_msg = NULL;
1064         if (msg->data)
1065         {
1066             set_reply_data_ptr( msg->data, msg->data_size );
1067             msg->data = NULL;
1068             msg->data_size = 0;
1069         }
1070         remove_queue_message( queue, msg, kind );
1071     }
1072     else  /* remember it as the last returned message */
1073     {
1074         if (msg->data) set_reply_data( msg->data, msg->data_size );
1075         queue->last_msg = msg;
1076         queue->last_msg_kind = kind;
1077     }
1078 }
1079
1080
1081 inline static struct message *find_matching_message( const struct message_list *list,
1082                                                      user_handle_t win,
1083                                                      unsigned int first, unsigned int last )
1084 {
1085     struct message *msg;
1086
1087     for (msg = list->first; msg; msg = msg->next)
1088     {
1089         /* check against the filters */
1090         if (msg->msg == WM_QUIT) break;  /* WM_QUIT is never filtered */
1091         if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
1092         if (msg->msg < first) continue;
1093         if (msg->msg > last) continue;
1094         break; /* found one */
1095     }
1096     return msg;
1097 }
1098
1099
1100 /* get a message from the current queue */
1101 DECL_HANDLER(get_message)
1102 {
1103     struct timer *timer;
1104     struct message *msg;
1105     struct msg_queue *queue = get_current_queue();
1106     user_handle_t get_win = get_user_full_handle( req->get_win );
1107
1108     if (!queue) return;
1109
1110     /* first check for sent messages */
1111     if ((msg = queue->msg_list[SEND_MESSAGE].first))
1112     {
1113         receive_message( queue, msg, reply );
1114         return;
1115     }
1116     if (req->flags & GET_MSG_SENT_ONLY) goto done;  /* nothing else to check */
1117
1118     /* if requested, remove the last returned but not yet removed message */
1119     if ((req->flags & GET_MSG_REMOVE_LAST) && queue->last_msg)
1120         remove_queue_message( queue, queue->last_msg, queue->last_msg_kind );
1121     queue->last_msg = NULL;
1122
1123     /* clear changed bits so we can wait on them if we don't find a message */
1124     queue->changed_bits = 0;
1125
1126     /* then check for posted messages */
1127     if ((msg = find_matching_message( &queue->msg_list[POST_MESSAGE], get_win,
1128                                       req->get_first, req->get_last )))
1129     {
1130         return_message_to_app( queue, req->flags, reply, msg, POST_MESSAGE );
1131         return;
1132     }
1133
1134     /* then check for cooked hardware messages */
1135     if ((msg = find_matching_message( &queue->msg_list[COOKED_HW_MESSAGE], get_win,
1136                                       req->get_first, req->get_last )))
1137     {
1138         return_message_to_app( queue, req->flags, reply, msg, COOKED_HW_MESSAGE );
1139         return;
1140     }
1141
1142     /* then check for any raw hardware message */
1143     if ((msg = queue->msg_list[RAW_HW_MESSAGE].first))
1144     {
1145         return_message_to_app( queue, req->flags, reply, msg, RAW_HW_MESSAGE );
1146         return;
1147     }
1148
1149     /* now check for WM_PAINT */
1150     if (queue->paint_count &&
1151         (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
1152         (reply->win = find_window_to_repaint( get_win, current )))
1153     {
1154         reply->type   = MSG_POSTED;
1155         reply->msg    = WM_PAINT;
1156         reply->wparam = 0;
1157         reply->lparam = 0;
1158         reply->x      = 0;
1159         reply->y      = 0;
1160         reply->time   = get_tick_count();
1161         reply->info   = 0;
1162         return;
1163     }
1164
1165     /* now check for timer */
1166     if ((timer = find_expired_timer( queue, get_win, req->get_first,
1167                                      req->get_last, (req->flags & GET_MSG_REMOVE) )))
1168     {
1169         reply->type   = MSG_POSTED;
1170         reply->win    = timer->win;
1171         reply->msg    = timer->msg;
1172         reply->wparam = timer->id;
1173         reply->lparam = timer->lparam;
1174         reply->x      = 0;
1175         reply->y      = 0;
1176         reply->time   = get_tick_count();
1177         reply->info   = 0;
1178         return;
1179     }
1180
1181  done:
1182     set_error( STATUS_PENDING );  /* FIXME */
1183 }
1184
1185
1186 /* reply to a sent message */
1187 DECL_HANDLER(reply_message)
1188 {
1189     if (current->queue && current->queue->recv_result)
1190         reply_message( current->queue, req->result, 0, req->remove,
1191                        get_req_data(), get_req_data_size() );
1192     else
1193         set_error( STATUS_ACCESS_DENIED );
1194 }
1195
1196
1197 /* retrieve the reply for the last message sent */
1198 DECL_HANDLER(get_message_reply)
1199 {
1200     struct msg_queue *queue = current->queue;
1201
1202     if (queue)
1203     {
1204         struct message_result *result = queue->send_result;
1205
1206         set_error( STATUS_PENDING );
1207         reply->result = 0;
1208
1209         if (result && (result->replied || req->cancel))
1210         {
1211             if (result->replied)
1212             {
1213                 reply->result = result->result;
1214                 set_error( result->error );
1215                 if (result->data)
1216                 {
1217                     size_t data_len = min( result->data_size, get_reply_max_size() );
1218                     set_reply_data_ptr( result->data, data_len );
1219                     result->data = NULL;
1220                     result->data_size = 0;
1221                 }
1222             }
1223             queue->send_result = result->send_next;
1224             result->sender = NULL;
1225             if (!result->receiver) free_result( result );
1226             if (!queue->send_result || !queue->send_result->replied)
1227                 clear_queue_bits( queue, QS_SMRESULT );
1228         }
1229     }
1230     else set_error( STATUS_ACCESS_DENIED );
1231 }
1232
1233
1234 /* set a window timer */
1235 DECL_HANDLER(set_win_timer)
1236 {
1237     struct timer *timer;
1238     struct msg_queue *queue = get_current_queue();
1239     user_handle_t win = get_user_full_handle( req->win );
1240
1241     if (!queue) return;
1242
1243     /* remove it if it existed already */
1244     if (win) kill_timer( queue, win, req->msg, req->id );
1245
1246     if ((timer = set_timer( queue, req->rate )))
1247     {
1248         timer->win    = win;
1249         timer->msg    = req->msg;
1250         timer->id     = req->id;
1251         timer->lparam = req->lparam;
1252     }
1253 }
1254
1255 /* kill a window timer */
1256 DECL_HANDLER(kill_win_timer)
1257 {
1258     struct msg_queue *queue = current->queue;
1259
1260     if (!queue || !kill_timer( queue, get_user_full_handle(req->win), req->msg, req->id ))
1261         set_error( STATUS_INVALID_PARAMETER );
1262 }
1263
1264
1265 /* attach (or detach) thread inputs */
1266 DECL_HANDLER(attach_thread_input)
1267 {
1268     struct thread *thread_from = get_thread_from_id( req->tid_from );
1269     struct thread *thread_to = get_thread_from_id( req->tid_to );
1270
1271     if (!thread_from || !thread_to)
1272     {
1273         if (thread_from) release_object( thread_from );
1274         if (thread_to) release_object( thread_to );
1275         return;
1276     }
1277     if (thread_from != thread_to)
1278     {
1279         if (req->attach) attach_thread_input( thread_from, thread_to );
1280         else detach_thread_input( thread_from, thread_to );
1281     }
1282     else set_error( STATUS_ACCESS_DENIED );
1283     release_object( thread_from );
1284     release_object( thread_to );
1285 }
1286
1287
1288 /* get thread input data */
1289 DECL_HANDLER(get_thread_input)
1290 {
1291     struct thread *thread = NULL;
1292     struct thread_input *input;
1293
1294     if (req->tid)
1295     {
1296         if (!(thread = get_thread_from_id( req->tid ))) return;
1297         input = thread->queue ? thread->queue->input : NULL;
1298     }
1299     else input = foreground_input;  /* get the foreground thread info */
1300
1301     if (input)
1302     {
1303         reply->focus      = input->focus;
1304         reply->capture    = input->capture;
1305         reply->active     = input->active;
1306         reply->menu_owner = input->menu_owner;
1307         reply->move_size  = input->move_size;
1308         reply->caret      = input->caret;
1309         reply->rect       = input->caret_rect;
1310     }
1311     else
1312     {
1313         reply->focus      = 0;
1314         reply->capture    = 0;
1315         reply->active     = 0;
1316         reply->menu_owner = 0;
1317         reply->move_size  = 0;
1318         reply->caret      = 0;
1319         reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1320     }
1321     /* foreground window is active window of foreground thread */
1322     reply->foreground = foreground_input ? foreground_input->active : 0;
1323     if (thread) release_object( thread );
1324 }
1325
1326
1327 /* set the system foreground window */
1328 DECL_HANDLER(set_foreground_window)
1329 {
1330     struct msg_queue *queue = get_current_queue();
1331
1332     reply->previous = foreground_input ? foreground_input->active : 0;
1333     reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1334     reply->send_msg_new = FALSE;
1335
1336     if (req->handle)
1337     {
1338         struct thread *thread;
1339
1340         if (is_top_level_window( req->handle ) &&
1341             ((thread = get_window_thread( req->handle ))))
1342         {
1343             foreground_input = thread->queue->input;
1344             reply->send_msg_new = (foreground_input != queue->input);
1345             release_object( thread );
1346         }
1347         else set_error( STATUS_INVALID_HANDLE );
1348     }
1349     else foreground_input = NULL;
1350 }
1351
1352
1353 /* set the current thread focus window */
1354 DECL_HANDLER(set_focus_window)
1355 {
1356     struct msg_queue *queue = get_current_queue();
1357
1358     reply->previous = 0;
1359     if (queue && check_queue_input_window( queue, req->handle ))
1360     {
1361         reply->previous = queue->input->focus;
1362         queue->input->focus = get_user_full_handle( req->handle );
1363     }
1364 }
1365
1366
1367 /* set the current thread active window */
1368 DECL_HANDLER(set_active_window)
1369 {
1370     struct msg_queue *queue = get_current_queue();
1371
1372     reply->previous = 0;
1373     if (queue && check_queue_input_window( queue, req->handle ))
1374     {
1375         if (!req->handle || make_window_active( req->handle ))
1376         {
1377             reply->previous = queue->input->active;
1378             queue->input->active = get_user_full_handle( req->handle );
1379         }
1380         else set_error( STATUS_INVALID_HANDLE );
1381     }
1382 }
1383
1384
1385 /* set the current thread capture window */
1386 DECL_HANDLER(set_capture_window)
1387 {
1388     struct msg_queue *queue = get_current_queue();
1389
1390     reply->previous = reply->full_handle = 0;
1391     if (queue && check_queue_input_window( queue, req->handle ))
1392     {
1393         struct thread_input *input = queue->input;
1394
1395         reply->previous = input->capture;
1396         input->capture = get_user_full_handle( req->handle );
1397         input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1398         input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1399         reply->full_handle = input->capture;
1400     }
1401 }
1402
1403
1404 /* Set the current thread caret window */
1405 DECL_HANDLER(set_caret_window)
1406 {
1407     struct msg_queue *queue = get_current_queue();
1408
1409     reply->previous = 0;
1410     if (queue && check_queue_input_window( queue, req->handle ))
1411     {
1412         struct thread_input *input = queue->input;
1413
1414         reply->previous  = input->caret;
1415         reply->old_rect  = input->caret_rect;
1416         reply->old_hide  = input->caret_hide;
1417         reply->old_state = input->caret_state;
1418
1419         set_caret_window( input, get_user_full_handle(req->handle) );
1420         input->caret_rect.right  = req->width;
1421         input->caret_rect.bottom = req->height;
1422     }
1423 }
1424
1425
1426 /* Set the current thread caret information */
1427 DECL_HANDLER(set_caret_info)
1428 {
1429     struct msg_queue *queue = get_current_queue();
1430     struct thread_input *input;
1431
1432     if (!queue) return;
1433     input = queue->input;
1434     reply->full_handle = input->caret;
1435     reply->old_rect    = input->caret_rect;
1436     reply->old_hide    = input->caret_hide;
1437     reply->old_state   = input->caret_state;
1438
1439     if (req->handle && get_user_full_handle(req->handle) != input->caret)
1440     {
1441         set_error( STATUS_ACCESS_DENIED );
1442         return;
1443     }
1444     if (req->flags & SET_CARET_POS)
1445     {
1446         input->caret_rect.right  += req->x - input->caret_rect.left;
1447         input->caret_rect.bottom += req->y - input->caret_rect.top;
1448         input->caret_rect.left = req->x;
1449         input->caret_rect.top  = req->y;
1450     }
1451     if (req->flags & SET_CARET_HIDE)
1452     {
1453         input->caret_hide += req->hide;
1454         if (input->caret_hide < 0) input->caret_hide = 0;
1455     }
1456     if (req->flags & SET_CARET_STATE)
1457     {
1458         if (req->state == -1) input->caret_state = !input->caret_state;
1459         else input->caret_state = !!req->state;
1460     }
1461 }