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