d3d10.idl: Added missing D3D10_RESOURCE_MISC_FLAG values.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "winternl.h"
36
37 #include "handle.h"
38 #include "file.h"
39 #include "thread.h"
40 #include "process.h"
41 #include "request.h"
42 #include "user.h"
43
44 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
45 #define WM_NCMOUSELAST  (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
46
47 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
48 #define NB_MSG_KINDS (POST_MESSAGE+1)
49
50
51 struct message_result
52 {
53     struct list            sender_entry;  /* entry in sender list */
54     struct message        *msg;           /* message the result is for */
55     struct message_result *recv_next;     /* next in receiver list */
56     struct msg_queue      *sender;        /* sender queue */
57     struct msg_queue      *receiver;      /* receiver queue */
58     int                    replied;       /* has it been replied to? */
59     unsigned int           error;         /* error code to pass back to sender */
60     lparam_t               result;        /* reply result */
61     struct message        *hardware_msg;  /* hardware message if low-level hook result */
62     struct desktop        *desktop;       /* desktop for hardware message */
63     struct message        *callback_msg;  /* message to queue for callback */
64     void                  *data;          /* message reply data */
65     unsigned int           data_size;     /* size of message reply data */
66     struct timeout_user   *timeout;       /* result timeout */
67 };
68
69 struct message
70 {
71     struct list            entry;     /* entry in message list */
72     enum message_type      type;      /* message type */
73     user_handle_t          win;       /* window handle */
74     unsigned int           msg;       /* message code */
75     lparam_t               wparam;    /* parameters */
76     lparam_t               lparam;    /* parameters */
77     unsigned int           time;      /* message time */
78     void                  *data;      /* message data for sent messages */
79     unsigned int           data_size; /* size of message data */
80     unsigned int           unique_id; /* unique id for nested hw message waits */
81     struct message_result *result;    /* result in sender queue */
82 };
83
84 struct timer
85 {
86     struct list     entry;     /* entry in timer list */
87     timeout_t       when;      /* next expiration */
88     unsigned int    rate;      /* timer rate in ms */
89     user_handle_t   win;       /* window handle */
90     unsigned int    msg;       /* message to post */
91     lparam_t        id;        /* timer id */
92     lparam_t        lparam;    /* lparam for message */
93 };
94
95 struct thread_input
96 {
97     struct object          obj;           /* object header */
98     struct desktop        *desktop;       /* desktop that this thread input belongs to */
99     user_handle_t          focus;         /* focus window */
100     user_handle_t          capture;       /* capture window */
101     user_handle_t          active;        /* active window */
102     user_handle_t          menu_owner;    /* current menu owner window */
103     user_handle_t          move_size;     /* current moving/resizing window */
104     user_handle_t          caret;         /* caret window */
105     rectangle_t            caret_rect;    /* caret rectangle */
106     int                    caret_hide;    /* caret hide count */
107     int                    caret_state;   /* caret on/off state */
108     user_handle_t          cursor;        /* current cursor */
109     int                    cursor_count;  /* cursor show count */
110     struct list            msg_list;      /* list of hardware messages */
111     unsigned char          keystate[256]; /* state of each key */
112 };
113
114 struct msg_queue
115 {
116     struct object          obj;             /* object header */
117     struct fd             *fd;              /* optional file descriptor to poll */
118     unsigned int           wake_bits;       /* wakeup bits */
119     unsigned int           wake_mask;       /* wakeup mask */
120     unsigned int           changed_bits;    /* changed wakeup bits */
121     unsigned int           changed_mask;    /* changed wakeup mask */
122     int                    paint_count;     /* pending paint messages count */
123     int                    quit_message;    /* is there a pending quit message? */
124     int                    exit_code;       /* exit code of pending quit message */
125     int                    cursor_count;    /* per-queue cursor show count */
126     struct list            msg_list[NB_MSG_KINDS];  /* lists of messages */
127     struct list            send_result;     /* stack of sent messages waiting for result */
128     struct list            callback_result; /* list of callback messages waiting for result */
129     struct message_result *recv_result;     /* stack of received messages waiting for result */
130     struct list            pending_timers;  /* list of pending timers */
131     struct list            expired_timers;  /* list of expired timers */
132     lparam_t               next_timer_id;   /* id for the next timer with a 0 window */
133     struct timeout_user   *timeout;         /* timeout for next timer to expire */
134     struct thread_input   *input;           /* thread input descriptor */
135     struct hook_table     *hooks;           /* hook table */
136     timeout_t              last_get_msg;    /* time of last get message call */
137 };
138
139 struct hotkey
140 {
141     struct list         entry;        /* entry in desktop hotkey list */
142     struct msg_queue   *queue;        /* queue owning this hotkey */
143     user_handle_t       win;          /* window handle */
144     int                 id;           /* hotkey id */
145     unsigned int        vkey;         /* virtual key code */
146     unsigned int        flags;        /* key modifiers */
147 };
148
149 static void msg_queue_dump( struct object *obj, int verbose );
150 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
151 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
152 static int msg_queue_signaled( struct object *obj, struct thread *thread );
153 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
154 static void msg_queue_destroy( struct object *obj );
155 static void msg_queue_poll_event( struct fd *fd, int event );
156 static void thread_input_dump( struct object *obj, int verbose );
157 static void thread_input_destroy( struct object *obj );
158 static void timer_callback( void *private );
159
160 static const struct object_ops msg_queue_ops =
161 {
162     sizeof(struct msg_queue),  /* size */
163     msg_queue_dump,            /* dump */
164     no_get_type,               /* get_type */
165     msg_queue_add_queue,       /* add_queue */
166     msg_queue_remove_queue,    /* remove_queue */
167     msg_queue_signaled,        /* signaled */
168     msg_queue_satisfied,       /* satisfied */
169     no_signal,                 /* signal */
170     no_get_fd,                 /* get_fd */
171     no_map_access,             /* map_access */
172     default_get_sd,            /* get_sd */
173     default_set_sd,            /* set_sd */
174     no_lookup_name,            /* lookup_name */
175     no_open_file,              /* open_file */
176     no_close_handle,           /* close_handle */
177     msg_queue_destroy          /* destroy */
178 };
179
180 static const struct fd_ops msg_queue_fd_ops =
181 {
182     NULL,                        /* get_poll_events */
183     msg_queue_poll_event,        /* poll_event */
184     NULL,                        /* flush */
185     NULL,                        /* get_fd_type */
186     NULL,                        /* ioctl */
187     NULL,                        /* queue_async */
188     NULL,                        /* reselect_async */
189     NULL                         /* cancel async */
190 };
191
192
193 static const struct object_ops thread_input_ops =
194 {
195     sizeof(struct thread_input),  /* size */
196     thread_input_dump,            /* dump */
197     no_get_type,                  /* get_type */
198     no_add_queue,                 /* add_queue */
199     NULL,                         /* remove_queue */
200     NULL,                         /* signaled */
201     NULL,                         /* satisfied */
202     no_signal,                    /* signal */
203     no_get_fd,                    /* get_fd */
204     no_map_access,                /* map_access */
205     default_get_sd,               /* get_sd */
206     default_set_sd,               /* set_sd */
207     no_lookup_name,               /* lookup_name */
208     no_open_file,                 /* open_file */
209     no_close_handle,              /* close_handle */
210     thread_input_destroy          /* destroy */
211 };
212
213 /* pointer to input structure of foreground thread */
214 static unsigned int last_input_time;
215
216 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
217 static void free_message( struct message *msg );
218
219 /* set the caret window in a given thread input */
220 static void set_caret_window( struct thread_input *input, user_handle_t win )
221 {
222     if (!win || win != input->caret)
223     {
224         input->caret_rect.left   = 0;
225         input->caret_rect.top    = 0;
226         input->caret_rect.right  = 0;
227         input->caret_rect.bottom = 0;
228     }
229     input->caret             = win;
230     input->caret_hide        = 1;
231     input->caret_state       = 0;
232 }
233
234 /* create a thread input object */
235 static struct thread_input *create_thread_input( struct thread *thread )
236 {
237     struct thread_input *input;
238
239     if ((input = alloc_object( &thread_input_ops )))
240     {
241         input->focus        = 0;
242         input->capture      = 0;
243         input->active       = 0;
244         input->menu_owner   = 0;
245         input->move_size    = 0;
246         input->cursor       = 0;
247         input->cursor_count = 0;
248         list_init( &input->msg_list );
249         set_caret_window( input, 0 );
250         memset( input->keystate, 0, sizeof(input->keystate) );
251
252         if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
253         {
254             release_object( input );
255             return NULL;
256         }
257     }
258     return input;
259 }
260
261 /* create a message queue object */
262 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
263 {
264     struct thread_input *new_input = NULL;
265     struct msg_queue *queue;
266     int i;
267
268     if (!input)
269     {
270         if (!(new_input = create_thread_input( thread ))) return NULL;
271         input = new_input;
272     }
273
274     if ((queue = alloc_object( &msg_queue_ops )))
275     {
276         queue->fd              = NULL;
277         queue->wake_bits       = 0;
278         queue->wake_mask       = 0;
279         queue->changed_bits    = 0;
280         queue->changed_mask    = 0;
281         queue->paint_count     = 0;
282         queue->quit_message    = 0;
283         queue->cursor_count    = 0;
284         queue->recv_result     = NULL;
285         queue->next_timer_id   = 0x7fff;
286         queue->timeout         = NULL;
287         queue->input           = (struct thread_input *)grab_object( input );
288         queue->hooks           = NULL;
289         queue->last_get_msg    = current_time;
290         list_init( &queue->send_result );
291         list_init( &queue->callback_result );
292         list_init( &queue->pending_timers );
293         list_init( &queue->expired_timers );
294         for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
295
296         thread->queue = queue;
297     }
298     if (new_input) release_object( new_input );
299     return queue;
300 }
301
302 /* free the message queue of a thread at thread exit */
303 void free_msg_queue( struct thread *thread )
304 {
305     remove_thread_hooks( thread );
306     if (!thread->queue) return;
307     release_object( thread->queue );
308     thread->queue = NULL;
309 }
310
311 /* change the thread input data of a given thread */
312 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
313 {
314     struct msg_queue *queue = thread->queue;
315
316     if (!queue)
317     {
318         thread->queue = create_msg_queue( thread, new_input );
319         return thread->queue != NULL;
320     }
321     if (queue->input)
322     {
323         queue->input->cursor_count -= queue->cursor_count;
324         release_object( queue->input );
325     }
326     queue->input = (struct thread_input *)grab_object( new_input );
327     new_input->cursor_count += queue->cursor_count;
328     return 1;
329 }
330
331 /* set the cursor position and queue the corresponding mouse message */
332 static void set_cursor_pos( struct desktop *desktop, int x, int y )
333 {
334     struct hardware_msg_data *msg_data;
335     struct message *msg;
336
337     if (!(msg = mem_alloc( sizeof(*msg) ))) return;
338     if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
339     {
340         free( msg );
341         return;
342     }
343     memset( msg_data, 0, sizeof(*msg_data) );
344
345     msg->type      = MSG_HARDWARE;
346     msg->win       = 0;
347     msg->msg       = WM_MOUSEMOVE;
348     msg->wparam    = 0;
349     msg->lparam    = 0;
350     msg->time      = get_tick_count();
351     msg->result    = NULL;
352     msg->data      = msg_data;
353     msg->data_size = sizeof(*msg_data);
354     msg_data->x    = x;
355     msg_data->y    = y;
356     queue_hardware_message( desktop, msg, 1 );
357 }
358
359 /* set the cursor clip rectangle */
360 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect )
361 {
362     rectangle_t top_rect;
363     int x, y;
364
365     get_top_window_rectangle( desktop, &top_rect );
366     if (rect)
367     {
368         rectangle_t new_rect = *rect;
369         if (new_rect.left   < top_rect.left)   new_rect.left   = top_rect.left;
370         if (new_rect.right  > top_rect.right)  new_rect.right  = top_rect.right;
371         if (new_rect.top    < top_rect.top)    new_rect.top    = top_rect.top;
372         if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
373         if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
374         desktop->cursor.clip = new_rect;
375     }
376     else desktop->cursor.clip = top_rect;
377
378     if (desktop->cursor.clip_msg)
379         post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
380
381     /* warp the mouse to be inside the clip rect */
382     x = min( max( desktop->cursor.x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
383     y = min( max( desktop->cursor.y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
384     if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
385 }
386
387 /* change the foreground input and reset the cursor clip rect */
388 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
389 {
390     if (desktop->foreground_input == input) return;
391     set_clip_rectangle( desktop, NULL );
392     desktop->foreground_input = input;
393 }
394
395 /* get the hook table for a given thread */
396 struct hook_table *get_queue_hooks( struct thread *thread )
397 {
398     if (!thread->queue) return NULL;
399     return thread->queue->hooks;
400 }
401
402 /* set the hook table for a given thread, allocating the queue if needed */
403 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
404 {
405     struct msg_queue *queue = thread->queue;
406     if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
407     if (queue->hooks) release_object( queue->hooks );
408     queue->hooks = hooks;
409 }
410
411 /* check the queue status */
412 static inline int is_signaled( struct msg_queue *queue )
413 {
414     return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
415 }
416
417 /* set some queue bits */
418 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
419 {
420     queue->wake_bits |= bits;
421     queue->changed_bits |= bits;
422     if (is_signaled( queue )) wake_up( &queue->obj, 0 );
423 }
424
425 /* clear some queue bits */
426 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
427 {
428     queue->wake_bits &= ~bits;
429     queue->changed_bits &= ~bits;
430 }
431
432 /* check whether msg is a keyboard message */
433 static inline int is_keyboard_msg( struct message *msg )
434 {
435     return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
436 }
437
438 /* check if message is matched by the filter */
439 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
440 {
441     return (msg >= first && msg <= last);
442 }
443
444 /* check whether a message filter contains at least one potential hardware message */
445 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
446 {
447     /* hardware message ranges are (in numerical order):
448      *   WM_NCMOUSEFIRST .. WM_NCMOUSELAST
449      *   WM_KEYFIRST .. WM_KEYLAST
450      *   WM_MOUSEFIRST .. WM_MOUSELAST
451      */
452     if (last < WM_NCMOUSEFIRST) return 0;
453     if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
454     if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
455     if (first > WM_MOUSELAST) return 0;
456     return 1;
457 }
458
459 /* get the QS_* bit corresponding to a given hardware message */
460 static inline int get_hardware_msg_bit( struct message *msg )
461 {
462     if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
463     if (is_keyboard_msg( msg )) return QS_KEY;
464     return QS_MOUSEBUTTON;
465 }
466
467 /* get the current thread queue, creating it if needed */
468 static inline struct msg_queue *get_current_queue(void)
469 {
470     struct msg_queue *queue = current->queue;
471     if (!queue) queue = create_msg_queue( current, NULL );
472     return queue;
473 }
474
475 /* get a (pseudo-)unique id to tag hardware messages */
476 static inline unsigned int get_unique_id(void)
477 {
478     static unsigned int id;
479     if (!++id) id = 1;  /* avoid an id of 0 */
480     return id;
481 }
482
483 /* try to merge a message with the last in the list; return 1 if successful */
484 static int merge_message( struct thread_input *input, const struct message *msg )
485 {
486     struct message *prev;
487     struct list *ptr;
488
489     if (msg->msg != WM_MOUSEMOVE) return 0;
490     if (!(ptr = list_tail( &input->msg_list ))) return 0;
491     prev = LIST_ENTRY( ptr, struct message, entry );
492     if (prev->result) return 0;
493     if (prev->win && msg->win && prev->win != msg->win) return 0;
494     if (prev->msg != msg->msg) return 0;
495     if (prev->type != msg->type) return 0;
496     /* now we can merge it */
497     prev->wparam  = msg->wparam;
498     prev->lparam  = msg->lparam;
499     prev->time    = msg->time;
500     if (msg->type == MSG_HARDWARE && prev->data && msg->data)
501     {
502         struct hardware_msg_data *prev_data = prev->data;
503         struct hardware_msg_data *msg_data = msg->data;
504         prev_data->x     = msg_data->x;
505         prev_data->y     = msg_data->y;
506         prev_data->info  = msg_data->info;
507     }
508     return 1;
509 }
510
511 /* free a result structure */
512 static void free_result( struct message_result *result )
513 {
514     if (result->timeout) remove_timeout_user( result->timeout );
515     free( result->data );
516     if (result->callback_msg) free_message( result->callback_msg );
517     if (result->hardware_msg) free_message( result->hardware_msg );
518     if (result->desktop) release_object( result->desktop );
519     free( result );
520 }
521
522 /* remove the result from the sender list it is on */
523 static inline void remove_result_from_sender( struct message_result *result )
524 {
525     assert( result->sender );
526
527     list_remove( &result->sender_entry );
528     result->sender = NULL;
529     if (!result->receiver) free_result( result );
530 }
531
532 /* store the message result in the appropriate structure */
533 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
534 {
535     res->result  = result;
536     res->error   = error;
537     res->replied = 1;
538     if (res->timeout)
539     {
540         remove_timeout_user( res->timeout );
541         res->timeout = NULL;
542     }
543
544     if (res->hardware_msg)
545     {
546         if (!error && result)  /* rejected by the hook */
547             free_message( res->hardware_msg );
548         else
549             queue_hardware_message( res->desktop, res->hardware_msg, 0 );
550
551         res->hardware_msg = NULL;
552     }
553
554     if (res->sender)
555     {
556         if (res->callback_msg)
557         {
558             /* queue the callback message in the sender queue */
559             struct callback_msg_data *data = res->callback_msg->data;
560             data->result = result;
561             list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
562             set_queue_bits( res->sender, QS_SENDMESSAGE );
563             res->callback_msg = NULL;
564             remove_result_from_sender( res );
565         }
566         else
567         {
568             /* wake sender queue if waiting on this result */
569             if (list_head(&res->sender->send_result) == &res->sender_entry)
570                 set_queue_bits( res->sender, QS_SMRESULT );
571         }
572     }
573     else if (!res->receiver) free_result( res );
574 }
575
576 /* free a message when deleting a queue or window */
577 static void free_message( struct message *msg )
578 {
579     struct message_result *result = msg->result;
580     if (result)
581     {
582         result->msg = NULL;
583         result->receiver = NULL;
584         store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
585     }
586     free( msg->data );
587     free( msg );
588 }
589
590 /* remove (and free) a message from a message list */
591 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
592                                   enum message_kind kind )
593 {
594     list_remove( &msg->entry );
595     switch(kind)
596     {
597     case SEND_MESSAGE:
598         if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
599         break;
600     case POST_MESSAGE:
601         if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
602             clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
603         break;
604     }
605     free_message( msg );
606 }
607
608 /* message timed out without getting a reply */
609 static void result_timeout( void *private )
610 {
611     struct message_result *result = private;
612
613     assert( !result->replied );
614
615     result->timeout = NULL;
616
617     if (result->msg)  /* not received yet */
618     {
619         struct message *msg = result->msg;
620
621         result->msg = NULL;
622         msg->result = NULL;
623         remove_queue_message( result->receiver, msg, SEND_MESSAGE );
624         result->receiver = NULL;
625     }
626     store_message_result( result, 0, STATUS_TIMEOUT );
627 }
628
629 /* allocate and fill a message result structure */
630 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
631                                                     struct msg_queue *recv_queue,
632                                                     struct message *msg, timeout_t timeout )
633 {
634     struct message_result *result = mem_alloc( sizeof(*result) );
635     if (result)
636     {
637         result->msg          = msg;
638         result->sender       = send_queue;
639         result->receiver     = recv_queue;
640         result->replied      = 0;
641         result->data         = NULL;
642         result->data_size    = 0;
643         result->timeout      = NULL;
644         result->hardware_msg = NULL;
645         result->desktop      = NULL;
646         result->callback_msg = NULL;
647
648         if (msg->type == MSG_CALLBACK)
649         {
650             struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
651
652             if (!callback_msg)
653             {
654                 free( result );
655                 return NULL;
656             }
657             callback_msg->type      = MSG_CALLBACK_RESULT;
658             callback_msg->win       = msg->win;
659             callback_msg->msg       = msg->msg;
660             callback_msg->wparam    = 0;
661             callback_msg->lparam    = 0;
662             callback_msg->time      = get_tick_count();
663             callback_msg->result    = NULL;
664             /* steal the data from the original message */
665             callback_msg->data      = msg->data;
666             callback_msg->data_size = msg->data_size;
667             msg->data = NULL;
668             msg->data_size = 0;
669
670             result->callback_msg = callback_msg;
671             list_add_head( &send_queue->callback_result, &result->sender_entry );
672         }
673         else if (send_queue) list_add_head( &send_queue->send_result, &result->sender_entry );
674
675         if (timeout != TIMEOUT_INFINITE)
676             result->timeout = add_timeout_user( timeout, result_timeout, result );
677     }
678     return result;
679 }
680
681 /* receive a message, removing it from the sent queue */
682 static void receive_message( struct msg_queue *queue, struct message *msg,
683                              struct get_message_reply *reply )
684 {
685     struct message_result *result = msg->result;
686
687     reply->total = msg->data_size;
688     if (msg->data_size > get_reply_max_size())
689     {
690         set_error( STATUS_BUFFER_OVERFLOW );
691         return;
692     }
693     reply->type   = msg->type;
694     reply->win    = msg->win;
695     reply->msg    = msg->msg;
696     reply->wparam = msg->wparam;
697     reply->lparam = msg->lparam;
698     reply->time   = msg->time;
699
700     if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
701
702     list_remove( &msg->entry );
703     /* put the result on the receiver result stack */
704     if (result)
705     {
706         result->msg = NULL;
707         result->recv_next  = queue->recv_result;
708         queue->recv_result = result;
709     }
710     free( msg );
711     if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
712 }
713
714 /* set the result of the current received message */
715 static void reply_message( struct msg_queue *queue, lparam_t result,
716                            unsigned int error, int remove, const void *data, data_size_t len )
717 {
718     struct message_result *res = queue->recv_result;
719
720     if (remove)
721     {
722         queue->recv_result = res->recv_next;
723         res->receiver = NULL;
724         if (!res->sender && !res->hardware_msg)  /* no one waiting for it */
725         {
726             free_result( res );
727             return;
728         }
729     }
730     if (!res->replied)
731     {
732         if (len && (res->data = memdup( data, len ))) res->data_size = len;
733         store_message_result( res, result, error );
734     }
735 }
736
737 static int match_window( user_handle_t win, user_handle_t msg_win )
738 {
739     if (!win) return 1;
740     if (win == -1 || win == 1) return !msg_win;
741     if (msg_win == win) return 1;
742     return is_child_window( win, msg_win );
743 }
744
745 /* retrieve a posted message */
746 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
747                                unsigned int first, unsigned int last, unsigned int flags,
748                                struct get_message_reply *reply )
749 {
750     struct message *msg;
751
752     /* check against the filters */
753     LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
754     {
755         if (!match_window( win, msg->win )) continue;
756         if (!check_msg_filter( msg->msg, first, last )) continue;
757         goto found; /* found one */
758     }
759     return 0;
760
761     /* return it to the app */
762 found:
763     reply->total = msg->data_size;
764     if (msg->data_size > get_reply_max_size())
765     {
766         set_error( STATUS_BUFFER_OVERFLOW );
767         return 1;
768     }
769     reply->type   = msg->type;
770     reply->win    = msg->win;
771     reply->msg    = msg->msg;
772     reply->wparam = msg->wparam;
773     reply->lparam = msg->lparam;
774     reply->time   = msg->time;
775
776     if (flags & PM_REMOVE)
777     {
778         if (msg->data)
779         {
780             set_reply_data_ptr( msg->data, msg->data_size );
781             msg->data = NULL;
782             msg->data_size = 0;
783         }
784         remove_queue_message( queue, msg, POST_MESSAGE );
785     }
786     else if (msg->data) set_reply_data( msg->data, msg->data_size );
787
788     return 1;
789 }
790
791 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
792                              struct get_message_reply *reply )
793 {
794     if (queue->quit_message)
795     {
796         reply->total  = 0;
797         reply->type   = MSG_POSTED;
798         reply->win    = 0;
799         reply->msg    = WM_QUIT;
800         reply->wparam = queue->exit_code;
801         reply->lparam = 0;
802         reply->time   = get_tick_count();
803
804         if (flags & PM_REMOVE)
805         {
806             queue->quit_message = 0;
807             if (list_empty( &queue->msg_list[POST_MESSAGE] ))
808                 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
809         }
810         return 1;
811     }
812     else
813         return 0;
814 }
815
816 /* empty a message list and free all the messages */
817 static void empty_msg_list( struct list *list )
818 {
819     struct list *ptr;
820
821     while ((ptr = list_head( list )) != NULL)
822     {
823         struct message *msg = LIST_ENTRY( ptr, struct message, entry );
824         list_remove( &msg->entry );
825         free_message( msg );
826     }
827 }
828
829 /* cleanup all pending results when deleting a queue */
830 static void cleanup_results( struct msg_queue *queue )
831 {
832     struct list *entry;
833
834     while ((entry = list_head( &queue->send_result )) != NULL)
835     {
836         remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
837     }
838
839     while ((entry = list_head( &queue->callback_result )) != NULL)
840     {
841         remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
842     }
843
844     while (queue->recv_result)
845         reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
846 }
847
848 /* check if the thread owning the queue is hung (not checking for messages) */
849 static int is_queue_hung( struct msg_queue *queue )
850 {
851     struct wait_queue_entry *entry;
852
853     if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
854         return 0;  /* less than 5 seconds since last get message -> not hung */
855
856     LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
857     {
858         if (entry->thread->queue == queue)
859             return 0;  /* thread is waiting on queue -> not hung */
860     }
861     return 1;
862 }
863
864 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
865 {
866     struct msg_queue *queue = (struct msg_queue *)obj;
867     struct process *process = entry->thread->process;
868
869     /* a thread can only wait on its own queue */
870     if (entry->thread->queue != queue)
871     {
872         set_error( STATUS_ACCESS_DENIED );
873         return 0;
874     }
875     if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
876
877     if (queue->fd && list_empty( &obj->wait_queue ))  /* first on the queue */
878         set_fd_events( queue->fd, POLLIN );
879     add_queue( obj, entry );
880     return 1;
881 }
882
883 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
884 {
885     struct msg_queue *queue = (struct msg_queue *)obj;
886
887     remove_queue( obj, entry );
888     if (queue->fd && list_empty( &obj->wait_queue ))  /* last on the queue is gone */
889         set_fd_events( queue->fd, 0 );
890 }
891
892 static void msg_queue_dump( struct object *obj, int verbose )
893 {
894     struct msg_queue *queue = (struct msg_queue *)obj;
895     fprintf( stderr, "Msg queue bits=%x mask=%x\n",
896              queue->wake_bits, queue->wake_mask );
897 }
898
899 static int msg_queue_signaled( struct object *obj, struct thread *thread )
900 {
901     struct msg_queue *queue = (struct msg_queue *)obj;
902     int ret = 0;
903
904     if (queue->fd)
905     {
906         if ((ret = check_fd_events( queue->fd, POLLIN )))
907             /* stop waiting on select() if we are signaled */
908             set_fd_events( queue->fd, 0 );
909         else if (!list_empty( &obj->wait_queue ))
910             /* restart waiting on poll() if we are no longer signaled */
911             set_fd_events( queue->fd, POLLIN );
912     }
913     return ret || is_signaled( queue );
914 }
915
916 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
917 {
918     struct msg_queue *queue = (struct msg_queue *)obj;
919     queue->wake_mask = 0;
920     queue->changed_mask = 0;
921     return 0;  /* Not abandoned */
922 }
923
924 static void msg_queue_destroy( struct object *obj )
925 {
926     struct msg_queue *queue = (struct msg_queue *)obj;
927     struct list *ptr;
928     struct hotkey *hotkey, *hotkey2;
929     int i;
930
931     cleanup_results( queue );
932     for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
933
934     LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
935     {
936         if (hotkey->queue == queue)
937         {
938             list_remove( &hotkey->entry );
939             free( hotkey );
940         }
941     }
942
943     while ((ptr = list_head( &queue->pending_timers )))
944     {
945         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
946         list_remove( &timer->entry );
947         free( timer );
948     }
949     while ((ptr = list_head( &queue->expired_timers )))
950     {
951         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
952         list_remove( &timer->entry );
953         free( timer );
954     }
955     if (queue->timeout) remove_timeout_user( queue->timeout );
956     if (queue->input)
957     {
958         queue->input->cursor_count -= queue->cursor_count;
959         release_object( queue->input );
960     }
961     if (queue->hooks) release_object( queue->hooks );
962     if (queue->fd) release_object( queue->fd );
963 }
964
965 static void msg_queue_poll_event( struct fd *fd, int event )
966 {
967     struct msg_queue *queue = get_fd_user( fd );
968     assert( queue->obj.ops == &msg_queue_ops );
969
970     if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
971     else set_fd_events( queue->fd, 0 );
972     wake_up( &queue->obj, 0 );
973 }
974
975 static void thread_input_dump( struct object *obj, int verbose )
976 {
977     struct thread_input *input = (struct thread_input *)obj;
978     fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
979              input->focus, input->capture, input->active );
980 }
981
982 static void thread_input_destroy( struct object *obj )
983 {
984     struct thread_input *input = (struct thread_input *)obj;
985
986     empty_msg_list( &input->msg_list );
987     if (input->desktop)
988     {
989         if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
990         release_object( input->desktop );
991     }
992 }
993
994 /* fix the thread input data when a window is destroyed */
995 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
996 {
997     struct thread_input *input = queue->input;
998
999     if (window == input->focus) input->focus = 0;
1000     if (window == input->capture) input->capture = 0;
1001     if (window == input->active) input->active = 0;
1002     if (window == input->menu_owner) input->menu_owner = 0;
1003     if (window == input->move_size) input->move_size = 0;
1004     if (window == input->caret) set_caret_window( input, 0 );
1005 }
1006
1007 /* check if the specified window can be set in the input data of a given queue */
1008 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1009 {
1010     struct thread *thread;
1011     int ret = 0;
1012
1013     if (!window) return 1;  /* we can always clear the data */
1014
1015     if ((thread = get_window_thread( window )))
1016     {
1017         ret = (queue->input == thread->queue->input);
1018         if (!ret) set_error( STATUS_ACCESS_DENIED );
1019         release_object( thread );
1020     }
1021     else set_error( STATUS_INVALID_HANDLE );
1022
1023     return ret;
1024 }
1025
1026 /* make sure the specified thread has a queue */
1027 int init_thread_queue( struct thread *thread )
1028 {
1029     if (thread->queue) return 1;
1030     return (create_msg_queue( thread, NULL ) != NULL);
1031 }
1032
1033 /* attach two thread input data structures */
1034 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1035 {
1036     struct desktop *desktop;
1037     struct thread_input *input;
1038     int ret;
1039
1040     if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1041     if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1042     input = (struct thread_input *)grab_object( thread_to->queue->input );
1043     if (input->desktop != desktop)
1044     {
1045         set_error( STATUS_ACCESS_DENIED );
1046         release_object( input );
1047         release_object( desktop );
1048         return 0;
1049     }
1050     release_object( desktop );
1051
1052     ret = assign_thread_input( thread_from, input );
1053     if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1054     release_object( input );
1055     return ret;
1056 }
1057
1058 /* detach two thread input data structures */
1059 void detach_thread_input( struct thread *thread_from )
1060 {
1061     struct thread_input *input;
1062
1063     if ((input = create_thread_input( thread_from )))
1064     {
1065         assign_thread_input( thread_from, input );
1066         release_object( input );
1067     }
1068 }
1069
1070
1071 /* set the next timer to expire */
1072 static void set_next_timer( struct msg_queue *queue )
1073 {
1074     struct list *ptr;
1075
1076     if (queue->timeout)
1077     {
1078         remove_timeout_user( queue->timeout );
1079         queue->timeout = NULL;
1080     }
1081     if ((ptr = list_head( &queue->pending_timers )))
1082     {
1083         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1084         queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1085     }
1086     /* set/clear QS_TIMER bit */
1087     if (list_empty( &queue->expired_timers ))
1088         clear_queue_bits( queue, QS_TIMER );
1089     else
1090         set_queue_bits( queue, QS_TIMER );
1091 }
1092
1093 /* find a timer from its window and id */
1094 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1095                                  unsigned int msg, lparam_t id )
1096 {
1097     struct list *ptr;
1098
1099     /* we need to search both lists */
1100
1101     LIST_FOR_EACH( ptr, &queue->pending_timers )
1102     {
1103         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1104         if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1105     }
1106     LIST_FOR_EACH( ptr, &queue->expired_timers )
1107     {
1108         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1109         if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1110     }
1111     return NULL;
1112 }
1113
1114 /* callback for the next timer expiration */
1115 static void timer_callback( void *private )
1116 {
1117     struct msg_queue *queue = private;
1118     struct list *ptr;
1119
1120     queue->timeout = NULL;
1121     /* move on to the next timer */
1122     ptr = list_head( &queue->pending_timers );
1123     list_remove( ptr );
1124     list_add_tail( &queue->expired_timers, ptr );
1125     set_next_timer( queue );
1126 }
1127
1128 /* link a timer at its rightful place in the queue list */
1129 static void link_timer( struct msg_queue *queue, struct timer *timer )
1130 {
1131     struct list *ptr;
1132
1133     for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1134     {
1135         struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1136         if (t->when >= timer->when) break;
1137     }
1138     list_add_before( ptr, &timer->entry );
1139 }
1140
1141 /* remove a timer from the queue timer list and free it */
1142 static void free_timer( struct msg_queue *queue, struct timer *timer )
1143 {
1144     list_remove( &timer->entry );
1145     free( timer );
1146     set_next_timer( queue );
1147 }
1148
1149 /* restart an expired timer */
1150 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1151 {
1152     list_remove( &timer->entry );
1153     while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1154     link_timer( queue, timer );
1155     set_next_timer( queue );
1156 }
1157
1158 /* find an expired timer matching the filtering parameters */
1159 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1160                                          unsigned int get_first, unsigned int get_last,
1161                                          int remove )
1162 {
1163     struct list *ptr;
1164
1165     LIST_FOR_EACH( ptr, &queue->expired_timers )
1166     {
1167         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1168         if (win && timer->win != win) continue;
1169         if (check_msg_filter( timer->msg, get_first, get_last ))
1170         {
1171             if (remove) restart_timer( queue, timer );
1172             return timer;
1173         }
1174     }
1175     return NULL;
1176 }
1177
1178 /* add a timer */
1179 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1180 {
1181     struct timer *timer = mem_alloc( sizeof(*timer) );
1182     if (timer)
1183     {
1184         timer->rate = max( rate, 1 );
1185         timer->when = current_time + (timeout_t)timer->rate * 10000;
1186         link_timer( queue, timer );
1187         /* check if we replaced the next timer */
1188         if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1189     }
1190     return timer;
1191 }
1192
1193 /* change the input key state for a given key */
1194 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1195 {
1196     if (down)
1197     {
1198         if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1199         keystate[key] |= down;
1200     }
1201     else keystate[key] &= ~0x80;
1202 }
1203
1204 /* update the input key state for a keyboard message */
1205 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1206                                     const struct message *msg )
1207 {
1208     unsigned char key;
1209     int down = 0;
1210
1211     switch (msg->msg)
1212     {
1213     case WM_LBUTTONDOWN:
1214         down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1215         /* fall through */
1216     case WM_LBUTTONUP:
1217         set_input_key_state( keystate, VK_LBUTTON, down );
1218         break;
1219     case WM_MBUTTONDOWN:
1220         down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1221         /* fall through */
1222     case WM_MBUTTONUP:
1223         set_input_key_state( keystate, VK_MBUTTON, down );
1224         break;
1225     case WM_RBUTTONDOWN:
1226         down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1227         /* fall through */
1228     case WM_RBUTTONUP:
1229         set_input_key_state( keystate, VK_RBUTTON, down );
1230         break;
1231     case WM_XBUTTONDOWN:
1232         down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1233         /* fall through */
1234     case WM_XBUTTONUP:
1235         if (msg->wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1236         else if (msg->wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1237         break;
1238     case WM_KEYDOWN:
1239     case WM_SYSKEYDOWN:
1240         down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1241         /* fall through */
1242     case WM_KEYUP:
1243     case WM_SYSKEYUP:
1244         key = (unsigned char)msg->wparam;
1245         set_input_key_state( keystate, key, down );
1246         switch(key)
1247         {
1248         case VK_LCONTROL:
1249         case VK_RCONTROL:
1250             down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1251             set_input_key_state( keystate, VK_CONTROL, down );
1252             break;
1253         case VK_LMENU:
1254         case VK_RMENU:
1255             down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1256             set_input_key_state( keystate, VK_MENU, down );
1257             break;
1258         case VK_LSHIFT:
1259         case VK_RSHIFT:
1260             down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1261             set_input_key_state( keystate, VK_SHIFT, down );
1262             break;
1263         }
1264         break;
1265     }
1266 }
1267
1268 /* release the hardware message currently being processed by the given thread */
1269 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1270                                       int remove, user_handle_t new_win )
1271 {
1272     struct thread_input *input = queue->input;
1273     struct message *msg;
1274
1275     LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1276     {
1277         if (msg->unique_id == hw_id) break;
1278     }
1279     if (&msg->entry == &input->msg_list) return;  /* not found */
1280
1281     /* clear the queue bit for that message */
1282     if (remove || new_win)
1283     {
1284         struct message *other;
1285         int clr_bit;
1286
1287         clr_bit = get_hardware_msg_bit( msg );
1288         LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1289         {
1290             if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1291             {
1292                 clr_bit = 0;
1293                 break;
1294             }
1295         }
1296         if (clr_bit) clear_queue_bits( queue, clr_bit );
1297     }
1298
1299     if (new_win)  /* set the new window */
1300     {
1301         struct thread *owner = get_window_thread( new_win );
1302         if (owner)
1303         {
1304             msg->win = new_win;
1305             if (owner->queue->input != input)
1306             {
1307                 list_remove( &msg->entry );
1308                 if (merge_message( owner->queue->input, msg ))
1309                 {
1310                     free_message( msg );
1311                     release_object( owner );
1312                     return;
1313                 }
1314                 list_add_tail( &owner->queue->input->msg_list, &msg->entry );
1315             }
1316             set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1317             remove = 0;
1318             release_object( owner );
1319         }
1320     }
1321     if (remove)
1322     {
1323         update_input_key_state( input->desktop, input->keystate, msg );
1324         list_remove( &msg->entry );
1325         free_message( msg );
1326     }
1327 }
1328
1329 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1330 {
1331     struct hotkey *hotkey;
1332     unsigned int modifiers = 0;
1333
1334     if (msg->msg != WM_KEYDOWN) return 0;
1335
1336     if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1337     if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1338     if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1339     if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1340
1341     LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1342     {
1343         if (hotkey->vkey != msg->wparam) continue;
1344         if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1345     }
1346
1347     return 0;
1348
1349 found:
1350     msg->type      = MSG_POSTED;
1351     msg->win       = hotkey->win;
1352     msg->msg       = WM_HOTKEY;
1353     msg->wparam    = hotkey->id;
1354     msg->lparam    = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1355
1356     free( msg->data );
1357     msg->data      = NULL;
1358     msg->data_size = 0;
1359
1360     list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1361     set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1362     return 1;
1363 }
1364
1365 /* find the window that should receive a given hardware message */
1366 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1367                                                    struct message *msg, unsigned int *msg_code )
1368 {
1369     struct hardware_msg_data *data = msg->data;
1370     user_handle_t win = 0;
1371
1372     *msg_code = msg->msg;
1373     if (is_keyboard_msg( msg ))
1374     {
1375         if (input && !(win = input->focus))
1376         {
1377             win = input->active;
1378             if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1379         }
1380     }
1381     else  /* mouse message */
1382     {
1383         if (!input || !(win = input->capture))
1384         {
1385             if (!(win = msg->win) || !is_window_visible( win ) || is_window_transparent( win ))
1386                 win = window_from_point( desktop, data->x, data->y );
1387         }
1388     }
1389     return win;
1390 }
1391
1392 /* queue a hardware message into a given thread input */
1393 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1394 {
1395     user_handle_t win;
1396     struct thread *thread;
1397     struct thread_input *input;
1398     unsigned int msg_code;
1399     struct hardware_msg_data *data = msg->data;
1400
1401     update_input_key_state( desktop, desktop->keystate, msg );
1402     last_input_time = get_tick_count();
1403     if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1404
1405     if (is_keyboard_msg( msg ))
1406     {
1407         if (queue_hotkey_message( desktop, msg )) return;
1408         if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1409         if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1410             msg->lparam &= ~(KF_EXTENDED << 16);
1411     }
1412     else
1413     {
1414         if (msg->msg == WM_MOUSEMOVE)
1415         {
1416             int x = min( max( data->x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
1417             int y = min( max( data->y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
1418             if (desktop->cursor.x != x || desktop->cursor.y != y) always_queue = 1;
1419             desktop->cursor.x = x;
1420             desktop->cursor.y = y;
1421             desktop->cursor.last_change = get_tick_count();
1422         }
1423         if (desktop->keystate[VK_LBUTTON] & 0x80)  msg->wparam |= MK_LBUTTON;
1424         if (desktop->keystate[VK_MBUTTON] & 0x80)  msg->wparam |= MK_MBUTTON;
1425         if (desktop->keystate[VK_RBUTTON] & 0x80)  msg->wparam |= MK_RBUTTON;
1426         if (desktop->keystate[VK_SHIFT] & 0x80)    msg->wparam |= MK_SHIFT;
1427         if (desktop->keystate[VK_CONTROL] & 0x80)  msg->wparam |= MK_CONTROL;
1428         if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1429         if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1430     }
1431     data->x = desktop->cursor.x;
1432     data->y = desktop->cursor.y;
1433
1434     if (msg->win && (thread = get_window_thread( msg->win )))
1435     {
1436         input = thread->queue->input;
1437         release_object( thread );
1438     }
1439     else input = desktop->foreground_input;
1440
1441     win = find_hardware_message_window( desktop, input, msg, &msg_code );
1442     if (!win || !(thread = get_window_thread(win)))
1443     {
1444         if (input) update_input_key_state( input->desktop, input->keystate, msg );
1445         free_message( msg );
1446         return;
1447     }
1448     input = thread->queue->input;
1449
1450     if (win != desktop->cursor.win) always_queue = 1;
1451     desktop->cursor.win = win;
1452
1453     if (!always_queue || merge_message( input, msg )) free_message( msg );
1454     else
1455     {
1456         msg->unique_id = 0;  /* will be set once we return it to the app */
1457         list_add_tail( &input->msg_list, &msg->entry );
1458         set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1459     }
1460     release_object( thread );
1461 }
1462
1463 /* send the low-level hook message for a given hardware message */
1464 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1465                                  const hw_input_t *input, struct msg_queue *sender )
1466 {
1467     struct thread *hook_thread;
1468     struct msg_queue *queue;
1469     struct message *msg;
1470     timeout_t timeout = 2000 * -10000;  /* FIXME: load from registry */
1471     int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1472
1473     if (!(hook_thread = get_first_global_hook( id ))) return 0;
1474     if (!(queue = hook_thread->queue)) return 0;
1475     if (is_queue_hung( queue )) return 0;
1476
1477     if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1478
1479     msg->type      = MSG_HOOK_LL;
1480     msg->win       = 0;
1481     msg->msg       = id;
1482     msg->wparam    = hardware_msg->msg;
1483     msg->time      = hardware_msg->time;
1484     msg->data_size = hardware_msg->data_size;
1485     msg->result    = NULL;
1486
1487     if (input->type == INPUT_KEYBOARD)
1488     {
1489         unsigned short vkey = input->kbd.vkey;
1490         if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1491         msg->lparam = (input->kbd.scan << 16) | vkey;
1492     }
1493     else msg->lparam = input->mouse.data << 16;
1494
1495     if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1496         !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1497     {
1498         free_message( msg );
1499         return 0;
1500     }
1501     msg->result->hardware_msg = hardware_msg;
1502     msg->result->desktop = (struct desktop *)grab_object( desktop );
1503     list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1504     set_queue_bits( queue, QS_SENDMESSAGE );
1505     return 1;
1506 }
1507
1508 /* queue a hardware message for a mouse event */
1509 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1510                                 unsigned int hook_flags, struct msg_queue *sender )
1511 {
1512     struct hardware_msg_data *msg_data;
1513     struct message *msg;
1514     unsigned int i, time, flags;
1515     int wait = 0, x, y;
1516
1517     static const unsigned int messages[] =
1518     {
1519         WM_MOUSEMOVE,    /* 0x0001 = MOUSEEVENTF_MOVE */
1520         WM_LBUTTONDOWN,  /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1521         WM_LBUTTONUP,    /* 0x0004 = MOUSEEVENTF_LEFTUP */
1522         WM_RBUTTONDOWN,  /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1523         WM_RBUTTONUP,    /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1524         WM_MBUTTONDOWN,  /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1525         WM_MBUTTONUP,    /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1526         WM_XBUTTONDOWN,  /* 0x0080 = MOUSEEVENTF_XDOWN */
1527         WM_XBUTTONUP,    /* 0x0100 = MOUSEEVENTF_XUP */
1528         0,               /* 0x0200 = unused */
1529         0,               /* 0x0400 = unused */
1530         WM_MOUSEWHEEL,   /* 0x0800 = MOUSEEVENTF_WHEEL */
1531         WM_MOUSEHWHEEL   /* 0x1000 = MOUSEEVENTF_HWHEEL */
1532     };
1533
1534     desktop->cursor.last_change = get_tick_count();
1535     flags = input->mouse.flags;
1536     time  = input->mouse.time;
1537     if (!time) time = desktop->cursor.last_change;
1538
1539     if (flags & MOUSEEVENTF_MOVE)
1540     {
1541         if (flags & MOUSEEVENTF_ABSOLUTE)
1542         {
1543             x = input->mouse.x;
1544             y = input->mouse.y;
1545             if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1546                 x == desktop->cursor.x && y == desktop->cursor.y)
1547                 flags &= ~MOUSEEVENTF_MOVE;
1548         }
1549         else
1550         {
1551             x = desktop->cursor.x + input->mouse.x;
1552             y = desktop->cursor.y + input->mouse.y;
1553         }
1554     }
1555     else
1556     {
1557         x = desktop->cursor.x;
1558         y = desktop->cursor.y;
1559     }
1560
1561     for (i = 0; i < sizeof(messages)/sizeof(messages[0]); i++)
1562     {
1563         if (!messages[i]) continue;
1564         if (!(flags & (1 << i))) continue;
1565         flags &= ~(1 << i);
1566
1567         if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1568         if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1569         {
1570             free( msg );
1571             return 0;
1572         }
1573         memset( msg_data, 0, sizeof(*msg_data) );
1574
1575         msg->type      = MSG_HARDWARE;
1576         msg->win       = get_user_full_handle( win );
1577         msg->msg       = messages[i];
1578         msg->wparam    = input->mouse.data << 16;
1579         msg->lparam    = 0;
1580         msg->time      = time;
1581         msg->result    = NULL;
1582         msg->data      = msg_data;
1583         msg->data_size = sizeof(*msg_data);
1584         msg_data->x    = x;
1585         msg_data->y    = y;
1586         msg_data->info = input->mouse.info;
1587         if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLMHF_INJECTED;
1588
1589         /* specify a sender only when sending the last message */
1590         if (!(flags & ((1 << sizeof(messages)/sizeof(messages[0])) - 1)))
1591         {
1592             if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1593                 queue_hardware_message( desktop, msg, 0 );
1594         }
1595         else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1596             queue_hardware_message( desktop, msg, 0 );
1597     }
1598     return wait;
1599 }
1600
1601 /* queue a hardware message for a keyboard event */
1602 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1603                                    unsigned int hook_flags, struct msg_queue *sender )
1604 {
1605     struct hardware_msg_data *msg_data;
1606     struct message *msg;
1607     unsigned char vkey = input->kbd.vkey;
1608     int wait;
1609
1610     if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1611     if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1612     {
1613         free( msg );
1614         return 0;
1615     }
1616     memset( msg_data, 0, sizeof(*msg_data) );
1617
1618     msg->type      = MSG_HARDWARE;
1619     msg->win       = get_user_full_handle( win );
1620     msg->lparam    = (input->kbd.scan << 16) | 1u; /* repeat count */
1621     msg->time      = input->kbd.time;
1622     msg->result    = NULL;
1623     msg->data      = msg_data;
1624     msg->data_size = sizeof(*msg_data);
1625     msg_data->info = input->kbd.info;
1626     if (!msg->time) msg->time = get_tick_count();
1627     if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLKHF_INJECTED;
1628
1629     if (input->kbd.flags & KEYEVENTF_UNICODE)
1630     {
1631         msg->wparam = VK_PACKET;
1632     }
1633     else
1634     {
1635         unsigned int flags = 0;
1636         switch (vkey)
1637         {
1638         case VK_MENU:
1639         case VK_LMENU:
1640         case VK_RMENU:
1641             vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1642             break;
1643         case VK_CONTROL:
1644         case VK_LCONTROL:
1645         case VK_RCONTROL:
1646             vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1647             break;
1648         case VK_SHIFT:
1649         case VK_LSHIFT:
1650         case VK_RSHIFT:
1651             vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1652             break;
1653         }
1654         if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1655         /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1656         if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1657         else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1658
1659         msg->wparam = vkey;
1660         msg->lparam |= flags << 16;
1661         msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1662     }
1663
1664     msg->msg = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1665
1666     switch (vkey)
1667     {
1668     case VK_LMENU:
1669     case VK_RMENU:
1670         if (input->kbd.flags & KEYEVENTF_KEYUP)
1671         {
1672             /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1673             /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1674             if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1675             msg->msg = WM_SYSKEYUP;
1676             desktop->keystate[VK_MENU] &= ~0x02;
1677         }
1678         else
1679         {
1680             /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1681             if (desktop->keystate[VK_CONTROL] & 0x80) break;
1682             msg->msg = WM_SYSKEYDOWN;
1683             desktop->keystate[VK_MENU] |= 0x02;
1684         }
1685         break;
1686
1687     case VK_LCONTROL:
1688     case VK_RCONTROL:
1689         /* send WM_SYSKEYUP on release if Alt still pressed */
1690         if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1691         if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1692         msg->msg = WM_SYSKEYUP;
1693         desktop->keystate[VK_MENU] &= ~0x02;
1694         break;
1695
1696     default:
1697         /* send WM_SYSKEY for Alt-anykey and for F10 */
1698         if (desktop->keystate[VK_CONTROL] & 0x80) break;
1699         if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1700         /* fall through */
1701     case VK_F10:
1702         msg->msg = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1703         desktop->keystate[VK_MENU] &= ~0x02;
1704         break;
1705     }
1706     if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1707         queue_hardware_message( desktop, msg, 1 );
1708
1709     return wait;
1710 }
1711
1712 /* queue a hardware message for a custom type of event */
1713 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1714                                            const hw_input_t *input )
1715 {
1716     struct hardware_msg_data *msg_data;
1717     struct message *msg;
1718
1719     if (!(msg = mem_alloc( sizeof(*msg) ))) return;
1720     if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1721     {
1722         free( msg );
1723         return;
1724     }
1725     memset( msg_data, 0, sizeof(*msg_data) );
1726
1727     msg->type      = MSG_HARDWARE;
1728     msg->win       = get_user_full_handle( win );
1729     msg->msg       = input->hw.msg;
1730     msg->wparam    = 0;
1731     msg->lparam    = input->hw.lparam;
1732     msg->time      = get_tick_count();
1733     msg->result    = NULL;
1734     msg->data      = msg_data;
1735     msg->data_size = sizeof(*msg_data);
1736
1737     queue_hardware_message( desktop, msg, 1 );
1738 }
1739
1740 /* check message filter for a hardware message */
1741 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1742                                     user_handle_t filter_win, unsigned int first, unsigned int last )
1743 {
1744     if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1745     {
1746         /* we can only test the window for a keyboard message since the
1747          * dest window for a mouse message depends on hittest */
1748         if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1749             return 0;
1750         /* the message code is final for a keyboard message, we can simply check it */
1751         return check_msg_filter( msg_code, first, last );
1752     }
1753     else  /* mouse message */
1754     {
1755         /* we need to check all possible values that the message can have in the end */
1756
1757         if (check_msg_filter( msg_code, first, last )) return 1;
1758         if (msg_code == WM_MOUSEWHEEL) return 0;  /* no other possible value for this one */
1759
1760         /* all other messages can become non-client messages */
1761         if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1762
1763         /* clicks can become double-clicks or non-client double-clicks */
1764         if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1765             msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1766         {
1767             if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1768             if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1769         }
1770         return 0;
1771     }
1772 }
1773
1774
1775 /* find a hardware message for the given queue */
1776 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1777                                  unsigned int first, unsigned int last, struct get_message_reply *reply )
1778 {
1779     struct thread_input *input = thread->queue->input;
1780     struct thread *win_thread;
1781     struct list *ptr;
1782     user_handle_t win;
1783     int clear_bits, got_one = 0;
1784     unsigned int msg_code;
1785
1786     ptr = list_head( &input->msg_list );
1787     if (hw_id)
1788     {
1789         while (ptr)
1790         {
1791             struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1792             if (msg->unique_id == hw_id) break;
1793             ptr = list_next( &input->msg_list, ptr );
1794         }
1795         if (!ptr) ptr = list_head( &input->msg_list );
1796         else ptr = list_next( &input->msg_list, ptr );  /* start from the next one */
1797     }
1798
1799     if (ptr == list_head( &input->msg_list ))
1800         clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1801     else
1802         clear_bits = 0;  /* don't clear bits if we don't go through the whole list */
1803
1804     while (ptr)
1805     {
1806         struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1807         struct hardware_msg_data *data = msg->data;
1808
1809         ptr = list_next( &input->msg_list, ptr );
1810         win = find_hardware_message_window( input->desktop, input, msg, &msg_code );
1811         if (!win || !(win_thread = get_window_thread( win )))
1812         {
1813             /* no window at all, remove it */
1814             update_input_key_state( input->desktop, input->keystate, msg );
1815             list_remove( &msg->entry );
1816             free_message( msg );
1817             continue;
1818         }
1819         if (win_thread != thread)
1820         {
1821             if (win_thread->queue->input == input)
1822             {
1823                 /* wake the other thread */
1824                 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1825                 got_one = 1;
1826             }
1827             else
1828             {
1829                 /* for another thread input, drop it */
1830                 update_input_key_state( input->desktop, input->keystate, msg );
1831                 list_remove( &msg->entry );
1832                 free_message( msg );
1833             }
1834             release_object( win_thread );
1835             continue;
1836         }
1837         release_object( win_thread );
1838
1839         /* if we already got a message for another thread, or if it doesn't
1840          * match the filter we skip it */
1841         if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1842         {
1843             clear_bits &= ~get_hardware_msg_bit( msg );
1844             continue;
1845         }
1846         /* now we can return it */
1847         if (!msg->unique_id) msg->unique_id = get_unique_id();
1848         reply->type   = MSG_HARDWARE;
1849         reply->win    = win;
1850         reply->msg    = msg_code;
1851         reply->wparam = msg->wparam;
1852         reply->lparam = msg->lparam;
1853         reply->time   = msg->time;
1854
1855         data->hw_id = msg->unique_id;
1856         set_reply_data( msg->data, msg->data_size );
1857         return 1;
1858     }
1859     /* nothing found, clear the hardware queue bits */
1860     clear_queue_bits( thread->queue, clear_bits );
1861     return 0;
1862 }
1863
1864 /* increment (or decrement if 'incr' is negative) the queue paint count */
1865 void inc_queue_paint_count( struct thread *thread, int incr )
1866 {
1867     struct msg_queue *queue = thread->queue;
1868
1869     assert( queue );
1870
1871     if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1872
1873     if (queue->paint_count)
1874         set_queue_bits( queue, QS_PAINT );
1875     else
1876         clear_queue_bits( queue, QS_PAINT );
1877 }
1878
1879
1880 /* remove all messages and timers belonging to a certain window */
1881 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1882 {
1883     struct msg_queue *queue = thread->queue;
1884     struct list *ptr;
1885     int i;
1886
1887     if (!queue) return;
1888
1889     /* remove timers */
1890
1891     ptr = list_head( &queue->pending_timers );
1892     while (ptr)
1893     {
1894         struct list *next = list_next( &queue->pending_timers, ptr );
1895         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1896         if (timer->win == win) free_timer( queue, timer );
1897         ptr = next;
1898     }
1899     ptr = list_head( &queue->expired_timers );
1900     while (ptr)
1901     {
1902         struct list *next = list_next( &queue->expired_timers, ptr );
1903         struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1904         if (timer->win == win) free_timer( queue, timer );
1905         ptr = next;
1906     }
1907
1908     /* remove messages */
1909     for (i = 0; i < NB_MSG_KINDS; i++)
1910     {
1911         struct list *ptr, *next;
1912
1913         LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1914         {
1915             struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1916             if (msg->win == win) remove_queue_message( queue, msg, i );
1917         }
1918     }
1919
1920     thread_input_cleanup_window( queue, win );
1921 }
1922
1923 /* post a message to a window; used by socket handling */
1924 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
1925 {
1926     struct message *msg;
1927     struct thread *thread = get_window_thread( win );
1928
1929     if (!thread) return;
1930
1931     if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1932     {
1933         msg->type      = MSG_POSTED;
1934         msg->win       = get_user_full_handle( win );
1935         msg->msg       = message;
1936         msg->wparam    = wparam;
1937         msg->lparam    = lparam;
1938         msg->time      = get_tick_count();
1939         msg->result    = NULL;
1940         msg->data      = NULL;
1941         msg->data_size = 0;
1942
1943         list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1944         set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1945     }
1946     release_object( thread );
1947 }
1948
1949 /* post a win event */
1950 void post_win_event( struct thread *thread, unsigned int event,
1951                      user_handle_t win, unsigned int object_id,
1952                      unsigned int child_id, client_ptr_t hook_proc,
1953                      const WCHAR *module, data_size_t module_size,
1954                      user_handle_t hook)
1955 {
1956     struct message *msg;
1957
1958     if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1959     {
1960         struct winevent_msg_data *data;
1961
1962         msg->type      = MSG_WINEVENT;
1963         msg->win       = get_user_full_handle( win );
1964         msg->msg       = event;
1965         msg->wparam    = object_id;
1966         msg->lparam    = child_id;
1967         msg->time      = get_tick_count();
1968         msg->result    = NULL;
1969
1970         if ((data = malloc( sizeof(*data) + module_size )))
1971         {
1972             data->hook = hook;
1973             data->tid  = get_thread_id( current );
1974             data->hook_proc = hook_proc;
1975             memcpy( data + 1, module, module_size );
1976
1977             msg->data = data;
1978             msg->data_size = sizeof(*data) + module_size;
1979
1980             if (debug_level > 1)
1981                 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
1982                          get_thread_id(thread), event, win, object_id, child_id );
1983             list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1984             set_queue_bits( thread->queue, QS_SENDMESSAGE );
1985         }
1986         else
1987             free( msg );
1988     }
1989 }
1990
1991 /* free all hotkeys on a desktop, optionally filtering by window */
1992 void free_hotkeys( struct desktop *desktop, user_handle_t window )
1993 {
1994     struct hotkey *hotkey, *hotkey2;
1995
1996     LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
1997     {
1998         if (!window || hotkey->win == window)
1999         {
2000             list_remove( &hotkey->entry );
2001             free( hotkey );
2002         }
2003     }
2004 }
2005
2006
2007 /* check if the thread owning the window is hung */
2008 DECL_HANDLER(is_window_hung)
2009 {
2010     struct thread *thread;
2011
2012     thread = get_window_thread( req->win );
2013     if (thread)
2014     {
2015         reply->is_hung = is_queue_hung( thread->queue );
2016         release_object( thread );
2017     }
2018     else reply->is_hung = 0;
2019 }
2020
2021
2022 /* get the message queue of the current thread */
2023 DECL_HANDLER(get_msg_queue)
2024 {
2025     struct msg_queue *queue = get_current_queue();
2026
2027     reply->handle = 0;
2028     if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2029 }
2030
2031
2032 /* set the file descriptor associated to the current thread queue */
2033 DECL_HANDLER(set_queue_fd)
2034 {
2035     struct msg_queue *queue = get_current_queue();
2036     struct file *file;
2037     int unix_fd;
2038
2039     if (queue->fd)  /* fd can only be set once */
2040     {
2041         set_error( STATUS_ACCESS_DENIED );
2042         return;
2043     }
2044     if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2045
2046     if ((unix_fd = get_file_unix_fd( file )) != -1)
2047     {
2048         if ((unix_fd = dup( unix_fd )) != -1)
2049             queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2050         else
2051             file_set_error();
2052     }
2053     release_object( file );
2054 }
2055
2056
2057 /* set the current message queue wakeup mask */
2058 DECL_HANDLER(set_queue_mask)
2059 {
2060     struct msg_queue *queue = get_current_queue();
2061
2062     if (queue)
2063     {
2064         queue->wake_mask    = req->wake_mask;
2065         queue->changed_mask = req->changed_mask;
2066         reply->wake_bits    = queue->wake_bits;
2067         reply->changed_bits = queue->changed_bits;
2068         if (is_signaled( queue ))
2069         {
2070             /* if skip wait is set, do what would have been done in the subsequent wait */
2071             if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
2072             else wake_up( &queue->obj, 0 );
2073         }
2074     }
2075 }
2076
2077
2078 /* get the current message queue status */
2079 DECL_HANDLER(get_queue_status)
2080 {
2081     struct msg_queue *queue = current->queue;
2082     if (queue)
2083     {
2084         reply->wake_bits    = queue->wake_bits;
2085         reply->changed_bits = queue->changed_bits;
2086         if (req->clear) queue->changed_bits = 0;
2087     }
2088     else reply->wake_bits = reply->changed_bits = 0;
2089 }
2090
2091
2092 /* send a message to a thread queue */
2093 DECL_HANDLER(send_message)
2094 {
2095     struct message *msg;
2096     struct msg_queue *send_queue = get_current_queue();
2097     struct msg_queue *recv_queue = NULL;
2098     struct thread *thread = NULL;
2099
2100     if (!(thread = get_thread_from_id( req->id ))) return;
2101
2102     if (!(recv_queue = thread->queue))
2103     {
2104         set_error( STATUS_INVALID_PARAMETER );
2105         release_object( thread );
2106         return;
2107     }
2108     if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2109     {
2110         set_error( STATUS_TIMEOUT );
2111         release_object( thread );
2112         return;
2113     }
2114
2115     if ((msg = mem_alloc( sizeof(*msg) )))
2116     {
2117         msg->type      = req->type;
2118         msg->win       = get_user_full_handle( req->win );
2119         msg->msg       = req->msg;
2120         msg->wparam    = req->wparam;
2121         msg->lparam    = req->lparam;
2122         msg->time      = get_tick_count();
2123         msg->result    = NULL;
2124         msg->data      = NULL;
2125         msg->data_size = get_req_data_size();
2126
2127         if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2128         {
2129             free( msg );
2130             release_object( thread );
2131             return;
2132         }
2133
2134         switch(msg->type)
2135         {
2136         case MSG_OTHER_PROCESS:
2137         case MSG_ASCII:
2138         case MSG_UNICODE:
2139         case MSG_CALLBACK:
2140             if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2141             {
2142                 free_message( msg );
2143                 break;
2144             }
2145             /* fall through */
2146         case MSG_NOTIFY:
2147             list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2148             set_queue_bits( recv_queue, QS_SENDMESSAGE );
2149             break;
2150         case MSG_POSTED:
2151             list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2152             set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2153             break;
2154         case MSG_HARDWARE:  /* should use send_hardware_message instead */
2155         case MSG_CALLBACK_RESULT:  /* cannot send this one */
2156         case MSG_HOOK_LL:  /* generated internally */
2157         default:
2158             set_error( STATUS_INVALID_PARAMETER );
2159             free( msg );
2160             break;
2161         }
2162     }
2163     release_object( thread );
2164 }
2165
2166 /* send a hardware message to a thread queue */
2167 DECL_HANDLER(send_hardware_message)
2168 {
2169     struct thread *thread = NULL;
2170     struct desktop *desktop;
2171     struct msg_queue *sender = get_current_queue();
2172
2173     if (req->win)
2174     {
2175         if (!(thread = get_window_thread( req->win ))) return;
2176         desktop = (struct desktop *)grab_object( thread->queue->input->desktop );
2177     }
2178     else if (!(desktop = get_thread_desktop( current, 0 ))) return;
2179
2180     switch (req->input.type)
2181     {
2182     case INPUT_MOUSE:
2183         reply->wait = queue_mouse_message( desktop, req->win, &req->input, req->flags, sender );
2184         break;
2185     case INPUT_KEYBOARD:
2186         reply->wait = queue_keyboard_message( desktop, req->win, &req->input, req->flags, sender );
2187         break;
2188     case INPUT_HARDWARE:
2189         queue_custom_hardware_message( desktop, req->win, &req->input );
2190         break;
2191     default:
2192         set_error( STATUS_INVALID_PARAMETER );
2193     }
2194     if (thread) release_object( thread );
2195     release_object( desktop );
2196 }
2197
2198 /* post a quit message to the current queue */
2199 DECL_HANDLER(post_quit_message)
2200 {
2201     struct msg_queue *queue = get_current_queue();
2202
2203     if (!queue)
2204         return;
2205
2206     queue->quit_message = 1;
2207     queue->exit_code = req->exit_code;
2208     set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2209 }
2210
2211 /* get a message from the current queue */
2212 DECL_HANDLER(get_message)
2213 {
2214     struct timer *timer;
2215     struct list *ptr;
2216     struct msg_queue *queue = get_current_queue();
2217     user_handle_t get_win = get_user_full_handle( req->get_win );
2218     unsigned int filter = req->flags >> 16;
2219
2220     reply->active_hooks = get_active_hooks();
2221
2222     if (!queue) return;
2223     queue->last_get_msg = current_time;
2224     if (!filter) filter = QS_ALLINPUT;
2225
2226     /* first check for sent messages */
2227     if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2228     {
2229         struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2230         receive_message( queue, msg, reply );
2231         return;
2232     }
2233
2234     /* clear changed bits so we can wait on them if we don't find a message */
2235     if (filter & QS_POSTMESSAGE)
2236     {
2237         queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2238         if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2239     }
2240     if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2241     if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2242
2243     /* then check for posted messages */
2244     if ((filter & QS_POSTMESSAGE) &&
2245         get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2246         return;
2247
2248     /* only check for quit messages if not posted messages pending.
2249      * note: the quit message isn't filtered */
2250     if (get_quit_message( queue, req->flags, reply ))
2251         return;
2252
2253     /* then check for any raw hardware message */
2254     if ((filter & QS_INPUT) &&
2255         filter_contains_hw_range( req->get_first, req->get_last ) &&
2256         get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
2257         return;
2258
2259     /* now check for WM_PAINT */
2260     if ((filter & QS_PAINT) &&
2261         queue->paint_count &&
2262         check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2263         (reply->win = find_window_to_repaint( get_win, current )))
2264     {
2265         reply->type   = MSG_POSTED;
2266         reply->msg    = WM_PAINT;
2267         reply->wparam = 0;
2268         reply->lparam = 0;
2269         reply->time   = get_tick_count();
2270         return;
2271     }
2272
2273     /* now check for timer */
2274     if ((filter & QS_TIMER) &&
2275         (timer = find_expired_timer( queue, get_win, req->get_first,
2276                                      req->get_last, (req->flags & PM_REMOVE) )))
2277     {
2278         reply->type   = MSG_POSTED;
2279         reply->win    = timer->win;
2280         reply->msg    = timer->msg;
2281         reply->wparam = timer->id;
2282         reply->lparam = timer->lparam;
2283         reply->time   = get_tick_count();
2284         if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2285             set_event( current->process->idle_event );
2286         return;
2287     }
2288
2289     if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2290     queue->wake_mask = req->wake_mask;
2291     queue->changed_mask = req->changed_mask;
2292     set_error( STATUS_PENDING );  /* FIXME */
2293 }
2294
2295
2296 /* reply to a sent message */
2297 DECL_HANDLER(reply_message)
2298 {
2299     if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2300     else if (current->queue->recv_result)
2301         reply_message( current->queue, req->result, 0, req->remove,
2302                        get_req_data(), get_req_data_size() );
2303 }
2304
2305
2306 /* accept the current hardware message */
2307 DECL_HANDLER(accept_hardware_message)
2308 {
2309     if (current->queue)
2310         release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
2311     else
2312         set_error( STATUS_ACCESS_DENIED );
2313 }
2314
2315
2316 /* retrieve the reply for the last message sent */
2317 DECL_HANDLER(get_message_reply)
2318 {
2319     struct message_result *result;
2320     struct list *entry;
2321     struct msg_queue *queue = current->queue;
2322
2323     if (queue)
2324     {
2325         set_error( STATUS_PENDING );
2326         reply->result = 0;
2327
2328         if (!(entry = list_head( &queue->send_result ))) return;  /* no reply ready */
2329
2330         result = LIST_ENTRY( entry, struct message_result, sender_entry );
2331         if (result->replied || req->cancel)
2332         {
2333             if (result->replied)
2334             {
2335                 reply->result = result->result;
2336                 set_error( result->error );
2337                 if (result->data)
2338                 {
2339                     data_size_t data_len = min( result->data_size, get_reply_max_size() );
2340                     set_reply_data_ptr( result->data, data_len );
2341                     result->data = NULL;
2342                     result->data_size = 0;
2343                 }
2344             }
2345             remove_result_from_sender( result );
2346
2347             entry = list_head( &queue->send_result );
2348             if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2349             else
2350             {
2351                 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2352                 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
2353             }
2354         }
2355     }
2356     else set_error( STATUS_ACCESS_DENIED );
2357 }
2358
2359
2360 /* set a window timer */
2361 DECL_HANDLER(set_win_timer)
2362 {
2363     struct timer *timer;
2364     struct msg_queue *queue;
2365     struct thread *thread = NULL;
2366     user_handle_t win = 0;
2367     lparam_t id = req->id;
2368
2369     if (req->win)
2370     {
2371         if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2372         {
2373             set_error( STATUS_INVALID_HANDLE );
2374             return;
2375         }
2376         if (thread->process != current->process)
2377         {
2378             release_object( thread );
2379             set_error( STATUS_ACCESS_DENIED );
2380             return;
2381         }
2382         queue = thread->queue;
2383         /* remove it if it existed already */
2384         if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2385     }
2386     else
2387     {
2388         queue = get_current_queue();
2389         /* look for a timer with this id */
2390         if (id && (timer = find_timer( queue, 0, req->msg, id )))
2391         {
2392             /* free and reuse id */
2393             free_timer( queue, timer );
2394         }
2395         else
2396         {
2397             /* find a free id for it */
2398             do
2399             {
2400                 id = queue->next_timer_id;
2401                 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2402             }
2403             while (find_timer( queue, 0, req->msg, id ));
2404         }
2405     }
2406
2407     if ((timer = set_timer( queue, req->rate )))
2408     {
2409         timer->win    = win;
2410         timer->msg    = req->msg;
2411         timer->id     = id;
2412         timer->lparam = req->lparam;
2413         reply->id     = id;
2414     }
2415     if (thread) release_object( thread );
2416 }
2417
2418 /* kill a window timer */
2419 DECL_HANDLER(kill_win_timer)
2420 {
2421     struct timer *timer;
2422     struct thread *thread;
2423     user_handle_t win = 0;
2424
2425     if (req->win)
2426     {
2427         if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2428         {
2429             set_error( STATUS_INVALID_HANDLE );
2430             return;
2431         }
2432         if (thread->process != current->process)
2433         {
2434             release_object( thread );
2435             set_error( STATUS_ACCESS_DENIED );
2436             return;
2437         }
2438     }
2439     else thread = (struct thread *)grab_object( current );
2440
2441     if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2442         free_timer( thread->queue, timer );
2443     else
2444         set_error( STATUS_INVALID_PARAMETER );
2445
2446     release_object( thread );
2447 }
2448
2449 DECL_HANDLER(register_hotkey)
2450 {
2451     struct desktop *desktop;
2452     user_handle_t win_handle = req->window;
2453     struct hotkey *hotkey;
2454     struct hotkey *new_hotkey = NULL;
2455     struct thread *thread;
2456     const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2457
2458     if (!(desktop = get_thread_desktop( current, 0 ))) return;
2459
2460     if (win_handle)
2461     {
2462         if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2463         {
2464             release_object( desktop );
2465             set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2466             return;
2467         }
2468
2469         thread = get_window_thread( win_handle );
2470         if (thread) release_object( thread );
2471
2472         if (thread != current)
2473         {
2474             release_object( desktop );
2475             set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2476             return;
2477         }
2478     }
2479
2480     LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2481     {
2482         if (req->vkey == hotkey->vkey &&
2483             (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2484         {
2485             release_object( desktop );
2486             set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2487             return;
2488         }
2489         if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2490             new_hotkey = hotkey;
2491     }
2492
2493     if (new_hotkey)
2494     {
2495         reply->replaced = 1;
2496         reply->flags    = new_hotkey->flags;
2497         reply->vkey     = new_hotkey->vkey;
2498     }
2499     else
2500     {
2501         new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2502         if (new_hotkey)
2503         {
2504             list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2505             new_hotkey->queue  = current->queue;
2506             new_hotkey->win    = win_handle;
2507             new_hotkey->id     = req->id;
2508         }
2509     }
2510
2511     if (new_hotkey)
2512     {
2513         new_hotkey->flags = req->flags;
2514         new_hotkey->vkey  = req->vkey;
2515     }
2516
2517     release_object( desktop );
2518 }
2519
2520 DECL_HANDLER(unregister_hotkey)
2521 {
2522     struct desktop *desktop;
2523     user_handle_t win_handle = req->window;
2524     struct hotkey *hotkey;
2525     struct thread *thread;
2526
2527     if (!(desktop = get_thread_desktop( current, 0 ))) return;
2528
2529     if (win_handle)
2530     {
2531         if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2532         {
2533             release_object( desktop );
2534             set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2535             return;
2536         }
2537
2538         thread = get_window_thread( win_handle );
2539         if (thread) release_object( thread );
2540
2541         if (thread != current)
2542         {
2543             release_object( desktop );
2544             set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2545             return;
2546         }
2547     }
2548
2549     LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2550     {
2551         if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2552             goto found;
2553     }
2554
2555     release_object( desktop );
2556     set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2557     return;
2558
2559 found:
2560     reply->flags = hotkey->flags;
2561     reply->vkey  = hotkey->vkey;
2562     list_remove( &hotkey->entry );
2563     free( hotkey );
2564     release_object( desktop );
2565 }
2566
2567 /* attach (or detach) thread inputs */
2568 DECL_HANDLER(attach_thread_input)
2569 {
2570     struct thread *thread_from = get_thread_from_id( req->tid_from );
2571     struct thread *thread_to = get_thread_from_id( req->tid_to );
2572
2573     if (!thread_from || !thread_to)
2574     {
2575         if (thread_from) release_object( thread_from );
2576         if (thread_to) release_object( thread_to );
2577         return;
2578     }
2579     if (thread_from != thread_to)
2580     {
2581         if (req->attach) attach_thread_input( thread_from, thread_to );
2582         else
2583         {
2584             if (thread_from->queue && thread_to->queue &&
2585                 thread_from->queue->input == thread_to->queue->input)
2586                 detach_thread_input( thread_from );
2587             else
2588                 set_error( STATUS_ACCESS_DENIED );
2589         }
2590     }
2591     else set_error( STATUS_ACCESS_DENIED );
2592     release_object( thread_from );
2593     release_object( thread_to );
2594 }
2595
2596
2597 /* get thread input data */
2598 DECL_HANDLER(get_thread_input)
2599 {
2600     struct thread *thread = NULL;
2601     struct desktop *desktop;
2602     struct thread_input *input;
2603
2604     if (req->tid)
2605     {
2606         if (!(thread = get_thread_from_id( req->tid ))) return;
2607         if (!(desktop = get_thread_desktop( thread, 0 )))
2608         {
2609             release_object( thread );
2610             return;
2611         }
2612         input = thread->queue ? thread->queue->input : NULL;
2613     }
2614     else
2615     {
2616         if (!(desktop = get_thread_desktop( current, 0 ))) return;
2617         input = desktop->foreground_input;  /* get the foreground thread info */
2618     }
2619
2620     if (input)
2621     {
2622         reply->focus      = input->focus;
2623         reply->capture    = input->capture;
2624         reply->active     = input->active;
2625         reply->menu_owner = input->menu_owner;
2626         reply->move_size  = input->move_size;
2627         reply->caret      = input->caret;
2628         reply->cursor     = input->cursor;
2629         reply->show_count = input->cursor_count;
2630         reply->rect       = input->caret_rect;
2631     }
2632
2633     /* foreground window is active window of foreground thread */
2634     reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2635     if (thread) release_object( thread );
2636     release_object( desktop );
2637 }
2638
2639
2640 /* retrieve queue keyboard state for a given thread */
2641 DECL_HANDLER(get_key_state)
2642 {
2643     struct thread *thread;
2644     struct desktop *desktop;
2645     data_size_t size = min( 256, get_reply_max_size() );
2646
2647     if (!req->tid)  /* get global async key state */
2648     {
2649         if (!(desktop = get_thread_desktop( current, 0 ))) return;
2650         if (req->key >= 0)
2651         {
2652             reply->state = desktop->keystate[req->key & 0xff];
2653             desktop->keystate[req->key & 0xff] &= ~0x40;
2654         }
2655         set_reply_data( desktop->keystate, size );
2656         release_object( desktop );
2657     }
2658     else
2659     {
2660         if (!(thread = get_thread_from_id( req->tid ))) return;
2661         if (thread->queue)
2662         {
2663             if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2664             set_reply_data( thread->queue->input->keystate, size );
2665         }
2666         release_object( thread );
2667     }
2668 }
2669
2670
2671 /* set queue keyboard state for a given thread */
2672 DECL_HANDLER(set_key_state)
2673 {
2674     struct thread *thread;
2675     struct desktop *desktop;
2676     data_size_t size = min( 256, get_req_data_size() );
2677
2678     if (!req->tid)  /* set global async key state */
2679     {
2680         if (!(desktop = get_thread_desktop( current, 0 ))) return;
2681         memcpy( desktop->keystate, get_req_data(), size );
2682         release_object( desktop );
2683     }
2684     else
2685     {
2686         if (!(thread = get_thread_from_id( req->tid ))) return;
2687         if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
2688         release_object( thread );
2689     }
2690 }
2691
2692
2693 /* set the system foreground window */
2694 DECL_HANDLER(set_foreground_window)
2695 {
2696     struct thread *thread = NULL;
2697     struct desktop *desktop;
2698     struct msg_queue *queue = get_current_queue();
2699
2700     if (!(desktop = get_thread_desktop( current, 0 ))) return;
2701     reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
2702     reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
2703     reply->send_msg_new = FALSE;
2704
2705     if (is_top_level_window( req->handle ) &&
2706         ((thread = get_window_thread( req->handle ))) &&
2707         (thread->queue->input->desktop == desktop))
2708     {
2709         set_foreground_input( desktop, thread->queue->input );
2710         reply->send_msg_new = (desktop->foreground_input != queue->input);
2711     }
2712     else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2713
2714     if (thread) release_object( thread );
2715     release_object( desktop );
2716 }
2717
2718
2719 /* set the current thread focus window */
2720 DECL_HANDLER(set_focus_window)
2721 {
2722     struct msg_queue *queue = get_current_queue();
2723
2724     reply->previous = 0;
2725     if (queue && check_queue_input_window( queue, req->handle ))
2726     {
2727         reply->previous = queue->input->focus;
2728         queue->input->focus = get_user_full_handle( req->handle );
2729     }
2730 }
2731
2732
2733 /* set the current thread active window */
2734 DECL_HANDLER(set_active_window)
2735 {
2736     struct msg_queue *queue = get_current_queue();
2737
2738     reply->previous = 0;
2739     if (queue && check_queue_input_window( queue, req->handle ))
2740     {
2741         if (!req->handle || make_window_active( req->handle ))
2742         {
2743             reply->previous = queue->input->active;
2744             queue->input->active = get_user_full_handle( req->handle );
2745         }
2746         else set_error( STATUS_INVALID_HANDLE );
2747     }
2748 }
2749
2750
2751 /* set the current thread capture window */
2752 DECL_HANDLER(set_capture_window)
2753 {
2754     struct msg_queue *queue = get_current_queue();
2755
2756     reply->previous = reply->full_handle = 0;
2757     if (queue && check_queue_input_window( queue, req->handle ))
2758     {
2759         struct thread_input *input = queue->input;
2760
2761         /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2762         if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2763         {
2764             set_error(STATUS_ACCESS_DENIED);
2765             return;
2766         }
2767         reply->previous = input->capture;
2768         input->capture = get_user_full_handle( req->handle );
2769         input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2770         input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2771         reply->full_handle = input->capture;
2772     }
2773 }
2774
2775
2776 /* Set the current thread caret window */
2777 DECL_HANDLER(set_caret_window)
2778 {
2779     struct msg_queue *queue = get_current_queue();
2780
2781     reply->previous = 0;
2782     if (queue && check_queue_input_window( queue, req->handle ))
2783     {
2784         struct thread_input *input = queue->input;
2785
2786         reply->previous  = input->caret;
2787         reply->old_rect  = input->caret_rect;
2788         reply->old_hide  = input->caret_hide;
2789         reply->old_state = input->caret_state;
2790
2791         set_caret_window( input, get_user_full_handle(req->handle) );
2792         input->caret_rect.right  = input->caret_rect.left + req->width;
2793         input->caret_rect.bottom = input->caret_rect.top + req->height;
2794     }
2795 }
2796
2797
2798 /* Set the current thread caret information */
2799 DECL_HANDLER(set_caret_info)
2800 {
2801     struct msg_queue *queue = get_current_queue();
2802     struct thread_input *input;
2803
2804     if (!queue) return;
2805     input = queue->input;
2806     reply->full_handle = input->caret;
2807     reply->old_rect    = input->caret_rect;
2808     reply->old_hide    = input->caret_hide;
2809     reply->old_state   = input->caret_state;
2810
2811     if (req->handle && get_user_full_handle(req->handle) != input->caret)
2812     {
2813         set_error( STATUS_ACCESS_DENIED );
2814         return;
2815     }
2816     if (req->flags & SET_CARET_POS)
2817     {
2818         input->caret_rect.right  += req->x - input->caret_rect.left;
2819         input->caret_rect.bottom += req->y - input->caret_rect.top;
2820         input->caret_rect.left = req->x;
2821         input->caret_rect.top  = req->y;
2822     }
2823     if (req->flags & SET_CARET_HIDE)
2824     {
2825         input->caret_hide += req->hide;
2826         if (input->caret_hide < 0) input->caret_hide = 0;
2827     }
2828     if (req->flags & SET_CARET_STATE)
2829     {
2830         if (req->state == -1) input->caret_state = !input->caret_state;
2831         else input->caret_state = !!req->state;
2832     }
2833 }
2834
2835
2836 /* get the time of the last input event */
2837 DECL_HANDLER(get_last_input_time)
2838 {
2839     reply->time = last_input_time;
2840 }
2841
2842 /* set/get the current cursor */
2843 DECL_HANDLER(set_cursor)
2844 {
2845     struct msg_queue *queue = get_current_queue();
2846     struct thread_input *input;
2847
2848     if (!queue) return;
2849     input = queue->input;
2850
2851     reply->prev_handle = input->cursor;
2852     reply->prev_count  = input->cursor_count;
2853     reply->prev_x      = input->desktop->cursor.x;
2854     reply->prev_y      = input->desktop->cursor.y;
2855
2856     if (req->flags & SET_CURSOR_HANDLE)
2857     {
2858         if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
2859         {
2860             set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
2861             return;
2862         }
2863         input->cursor = req->handle;
2864     }
2865     if (req->flags & SET_CURSOR_COUNT)
2866     {
2867         queue->cursor_count += req->show_count;
2868         input->cursor_count += req->show_count;
2869     }
2870     if (req->flags & SET_CURSOR_POS)
2871     {
2872         set_cursor_pos( input->desktop, req->x, req->y );
2873     }
2874     if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
2875     {
2876         struct desktop *desktop = input->desktop;
2877
2878         /* only the desktop owner can set the message */
2879         if (req->clip_msg && get_top_window_owner(desktop) == current->process)
2880             desktop->cursor.clip_msg = req->clip_msg;
2881
2882         set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip );
2883     }
2884
2885     reply->new_x       = input->desktop->cursor.x;
2886     reply->new_y       = input->desktop->cursor.y;
2887     reply->new_clip    = input->desktop->cursor.clip;
2888     reply->last_change = input->desktop->cursor.last_change;
2889 }