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