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