2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
41 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
42 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
44 enum message_kind
{ SEND_MESSAGE
, POST_MESSAGE
};
45 #define NB_MSG_KINDS (POST_MESSAGE+1)
50 struct list sender_entry
; /* entry in sender list */
51 struct message_result
*recv_next
; /* next in receiver list */
52 struct msg_queue
*sender
; /* sender queue */
53 struct msg_queue
*receiver
; /* receiver queue */
54 int replied
; /* has it been replied to? */
55 unsigned int result
; /* reply result */
56 unsigned int error
; /* error code to pass back to sender */
57 struct message
*callback_msg
; /* message to queue for callback */
58 void *data
; /* message reply data */
59 unsigned int data_size
; /* size of message reply data */
60 struct timeout_user
*timeout
; /* result timeout */
65 struct list entry
; /* entry in message list */
66 enum message_type type
; /* message type */
67 user_handle_t win
; /* window handle */
68 unsigned int msg
; /* message code */
69 unsigned int wparam
; /* parameters */
70 unsigned int lparam
; /* parameters */
71 int x
; /* x position */
72 int y
; /* y position */
73 unsigned int time
; /* message time */
74 unsigned int info
; /* extra info */
75 user_handle_t hook
; /* winevent hook handle */
76 void *hook_proc
; /* winevent hook proc address */
77 void *data
; /* message data for sent messages */
78 unsigned int data_size
; /* size of message data */
79 unsigned int unique_id
; /* unique id for nested hw message waits */
80 struct message_result
*result
; /* result in sender queue */
85 struct list entry
; /* entry in timer list */
86 struct timeval when
; /* next expiration */
87 unsigned int rate
; /* timer rate in ms */
88 user_handle_t win
; /* window handle */
89 unsigned int msg
; /* message to post */
90 unsigned int id
; /* timer id */
91 unsigned int lparam
; /* lparam for message */
96 struct object obj
; /* object header */
97 struct desktop
*desktop
; /* desktop that this thread input belongs to */
98 user_handle_t focus
; /* focus window */
99 user_handle_t capture
; /* capture window */
100 user_handle_t active
; /* active window */
101 user_handle_t menu_owner
; /* current menu owner window */
102 user_handle_t move_size
; /* current moving/resizing window */
103 user_handle_t caret
; /* caret window */
104 rectangle_t caret_rect
; /* caret rectangle */
105 int caret_hide
; /* caret hide count */
106 int caret_state
; /* caret on/off state */
107 struct list msg_list
; /* list of hardware messages */
108 unsigned char keystate
[256]; /* state of each key */
113 struct object obj
; /* object header */
114 unsigned int wake_bits
; /* wakeup bits */
115 unsigned int wake_mask
; /* wakeup mask */
116 unsigned int changed_bits
; /* changed wakeup bits */
117 unsigned int changed_mask
; /* changed wakeup mask */
118 int paint_count
; /* pending paint messages count */
119 struct list msg_list
[NB_MSG_KINDS
]; /* lists of messages */
120 struct list send_result
; /* stack of sent messages waiting for result */
121 struct list callback_result
; /* list of callback messages waiting for result */
122 struct message_result
*recv_result
; /* stack of received messages waiting for result */
123 struct list pending_timers
; /* list of pending timers */
124 struct list expired_timers
; /* list of expired timers */
125 unsigned int next_timer_id
; /* id for the next timer with a 0 window */
126 struct timeout_user
*timeout
; /* timeout for next timer to expire */
127 struct thread_input
*input
; /* thread input descriptor */
128 struct hook_table
*hooks
; /* hook table */
129 struct timeval last_get_msg
; /* time of last get message call */
132 static void msg_queue_dump( struct object
*obj
, int verbose
);
133 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
134 static void msg_queue_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
135 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
);
136 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
);
137 static void msg_queue_destroy( struct object
*obj
);
138 static void thread_input_dump( struct object
*obj
, int verbose
);
139 static void thread_input_destroy( struct object
*obj
);
140 static void timer_callback( void *private );
142 static const struct object_ops msg_queue_ops
=
144 sizeof(struct msg_queue
), /* size */
145 msg_queue_dump
, /* dump */
146 msg_queue_add_queue
, /* add_queue */
147 msg_queue_remove_queue
, /* remove_queue */
148 msg_queue_signaled
, /* signaled */
149 msg_queue_satisfied
, /* satisfied */
150 no_signal
, /* signal */
151 no_get_fd
, /* get_fd */
152 no_close_handle
, /* close_handle */
153 msg_queue_destroy
/* destroy */
157 static const struct object_ops thread_input_ops
=
159 sizeof(struct thread_input
), /* size */
160 thread_input_dump
, /* dump */
161 no_add_queue
, /* add_queue */
162 NULL
, /* remove_queue */
164 NULL
, /* satisfied */
165 no_signal
, /* signal */
166 no_get_fd
, /* get_fd */
167 no_close_handle
, /* close_handle */
168 thread_input_destroy
/* destroy */
171 /* pointer to input structure of foreground thread */
172 static struct thread_input
*foreground_input
;
173 static unsigned int last_input_time
;
176 /* set the caret window in a given thread input */
177 static void set_caret_window( struct thread_input
*input
, user_handle_t win
)
179 if (!win
|| win
!= input
->caret
)
181 input
->caret_rect
.left
= 0;
182 input
->caret_rect
.top
= 0;
183 input
->caret_rect
.right
= 0;
184 input
->caret_rect
.bottom
= 0;
187 input
->caret_hide
= 1;
188 input
->caret_state
= 0;
191 /* create a thread input object */
192 static struct thread_input
*create_thread_input( struct thread
*thread
)
194 struct thread_input
*input
;
196 if ((input
= alloc_object( &thread_input_ops
)))
198 if (!(input
->desktop
= get_thread_desktop( thread
, 0 /* FIXME: access rights */ )))
206 input
->menu_owner
= 0;
207 input
->move_size
= 0;
208 list_init( &input
->msg_list
);
209 set_caret_window( input
, 0 );
210 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
215 /* release the thread input data of a given thread */
216 static inline void release_thread_input( struct thread
*thread
)
218 struct thread_input
*input
= thread
->queue
->input
;
221 release_object( input
);
222 thread
->queue
->input
= NULL
;
225 /* create a message queue object */
226 static struct msg_queue
*create_msg_queue( struct thread
*thread
, struct thread_input
*input
)
228 struct msg_queue
*queue
;
231 if (!input
&& !(input
= create_thread_input( thread
))) return NULL
;
232 if ((queue
= alloc_object( &msg_queue_ops
)))
234 queue
->wake_bits
= 0;
235 queue
->wake_mask
= 0;
236 queue
->changed_bits
= 0;
237 queue
->changed_mask
= 0;
238 queue
->paint_count
= 0;
239 queue
->recv_result
= NULL
;
240 queue
->next_timer_id
= 1;
241 queue
->timeout
= NULL
;
242 queue
->input
= (struct thread_input
*)grab_object( input
);
244 gettimeofday( &queue
->last_get_msg
, NULL
);
245 list_init( &queue
->send_result
);
246 list_init( &queue
->callback_result
);
247 list_init( &queue
->pending_timers
);
248 list_init( &queue
->expired_timers
);
249 for (i
= 0; i
< NB_MSG_KINDS
; i
++) list_init( &queue
->msg_list
[i
] );
251 thread
->queue
= queue
;
252 if (!thread
->process
->queue
)
253 thread
->process
->queue
= (struct msg_queue
*)grab_object( queue
);
255 release_object( input
);
259 /* free the message queue of a thread at thread exit */
260 void free_msg_queue( struct thread
*thread
)
262 struct process
*process
= thread
->process
;
264 remove_thread_hooks( thread
);
265 if (!thread
->queue
) return;
266 if (process
->queue
== thread
->queue
) /* is it the process main queue? */
268 release_object( process
->queue
);
269 process
->queue
= NULL
;
270 if (process
->idle_event
)
272 set_event( process
->idle_event
);
273 release_object( process
->idle_event
);
274 process
->idle_event
= NULL
;
277 release_object( thread
->queue
);
278 thread
->queue
= NULL
;
281 /* get the hook table for a given thread */
282 struct hook_table
*get_queue_hooks( struct thread
*thread
)
284 if (!thread
->queue
) return NULL
;
285 return thread
->queue
->hooks
;
288 /* set the hook table for a given thread, allocating the queue if needed */
289 void set_queue_hooks( struct thread
*thread
, struct hook_table
*hooks
)
291 struct msg_queue
*queue
= thread
->queue
;
292 if (!queue
&& !(queue
= create_msg_queue( thread
, NULL
))) return;
293 if (queue
->hooks
) release_object( queue
->hooks
);
294 queue
->hooks
= hooks
;
297 /* check the queue status */
298 inline static int is_signaled( struct msg_queue
*queue
)
300 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
303 /* set some queue bits */
304 inline static void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
306 queue
->wake_bits
|= bits
;
307 queue
->changed_bits
|= bits
;
308 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
311 /* clear some queue bits */
312 inline static void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
314 queue
->wake_bits
&= ~bits
;
315 queue
->changed_bits
&= ~bits
;
318 /* check whether msg is a keyboard message */
319 inline static int is_keyboard_msg( struct message
*msg
)
321 return (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
);
324 /* check if message is matched by the filter */
325 inline static int check_msg_filter( unsigned int msg
, unsigned int first
, unsigned int last
)
327 return (msg
>= first
&& msg
<= last
);
330 /* check whether a message filter contains at least one potential hardware message */
331 inline static int filter_contains_hw_range( unsigned int first
, unsigned int last
)
333 /* hardware message ranges are (in numerical order):
334 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
335 * WM_KEYFIRST .. WM_KEYLAST
336 * WM_MOUSEFIRST .. WM_MOUSELAST
338 if (last
< WM_NCMOUSEFIRST
) return 0;
339 if (first
> WM_NCMOUSELAST
&& last
< WM_KEYFIRST
) return 0;
340 if (first
> WM_KEYLAST
&& last
< WM_MOUSEFIRST
) return 0;
341 if (first
> WM_MOUSELAST
) return 0;
345 /* get the QS_* bit corresponding to a given hardware message */
346 inline static int get_hardware_msg_bit( struct message
*msg
)
348 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
349 if (is_keyboard_msg( msg
)) return QS_KEY
;
350 return QS_MOUSEBUTTON
;
353 /* get the current thread queue, creating it if needed */
354 inline static struct msg_queue
*get_current_queue(void)
356 struct msg_queue
*queue
= current
->queue
;
357 if (!queue
) queue
= create_msg_queue( current
, NULL
);
361 /* get a (pseudo-)unique id to tag hardware messages */
362 inline static unsigned int get_unique_id(void)
364 static unsigned int id
;
365 if (!++id
) id
= 1; /* avoid an id of 0 */
369 /* try to merge a message with the last in the list; return 1 if successful */
370 static int merge_message( struct thread_input
*input
, const struct message
*msg
)
372 struct message
*prev
;
373 struct list
*ptr
= list_tail( &input
->msg_list
);
376 prev
= LIST_ENTRY( ptr
, struct message
, entry
);
377 if (prev
->unique_id
) return 0;
378 if (prev
->result
) return 0;
379 if (prev
->win
!= msg
->win
) return 0;
380 if (prev
->msg
!= msg
->msg
) return 0;
381 if (prev
->type
!= msg
->type
) return 0;
382 /* now we can merge it */
383 prev
->wparam
= msg
->wparam
;
384 prev
->lparam
= msg
->lparam
;
387 prev
->time
= msg
->time
;
388 prev
->info
= msg
->info
;
392 /* free a result structure */
393 static void free_result( struct message_result
*result
)
395 if (result
->timeout
) remove_timeout_user( result
->timeout
);
396 if (result
->data
) free( result
->data
);
397 if (result
->callback_msg
) free( result
->callback_msg
);
401 /* remove the result from the sender list it is on */
402 static inline void remove_result_from_sender( struct message_result
*result
)
404 assert( result
->sender
);
406 list_remove( &result
->sender_entry
);
407 result
->sender
= NULL
;
408 if (!result
->receiver
) free_result( result
);
411 /* store the message result in the appropriate structure */
412 static void store_message_result( struct message_result
*res
, unsigned int result
,
415 res
->result
= result
;
420 remove_timeout_user( res
->timeout
);
425 if (res
->callback_msg
)
427 /* queue the callback message in the sender queue */
428 res
->callback_msg
->lparam
= result
;
429 list_add_tail( &res
->sender
->msg_list
[SEND_MESSAGE
], &res
->callback_msg
->entry
);
430 set_queue_bits( res
->sender
, QS_SENDMESSAGE
);
431 res
->callback_msg
= NULL
;
432 remove_result_from_sender( res
);
436 /* wake sender queue if waiting on this result */
437 if (list_head(&res
->sender
->send_result
) == &res
->sender_entry
)
438 set_queue_bits( res
->sender
, QS_SMRESULT
);
444 /* free a message when deleting a queue or window */
445 static void free_message( struct message
*msg
)
447 struct message_result
*result
= msg
->result
;
452 result
->receiver
= NULL
;
453 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
455 else free_result( result
);
457 if (msg
->data
) free( msg
->data
);
461 /* remove (and free) a message from a message list */
462 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
463 enum message_kind kind
)
465 list_remove( &msg
->entry
);
469 if (list_empty( &queue
->msg_list
[kind
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
472 if (list_empty( &queue
->msg_list
[kind
] )) clear_queue_bits( queue
, QS_POSTMESSAGE
);
478 /* message timed out without getting a reply */
479 static void result_timeout( void *private )
481 struct message_result
*result
= private;
483 assert( !result
->replied
);
485 result
->timeout
= NULL
;
486 store_message_result( result
, 0, STATUS_TIMEOUT
);
489 /* allocate and fill a message result structure */
490 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
491 struct msg_queue
*recv_queue
,
492 struct message
*msg
, unsigned int timeout
,
493 void *callback
, unsigned int callback_data
)
495 struct message_result
*result
= mem_alloc( sizeof(*result
) );
498 result
->sender
= send_queue
;
499 result
->receiver
= recv_queue
;
502 result
->data_size
= 0;
503 result
->timeout
= NULL
;
505 if (msg
->type
== MSG_CALLBACK
)
507 struct message
*callback_msg
= mem_alloc( sizeof(*callback_msg
) );
513 callback_msg
->type
= MSG_CALLBACK_RESULT
;
514 callback_msg
->win
= msg
->win
;
515 callback_msg
->msg
= msg
->msg
;
516 callback_msg
->wparam
= (unsigned int)callback
;
517 callback_msg
->lparam
= 0;
518 callback_msg
->time
= get_tick_count();
521 callback_msg
->info
= callback_data
;
522 callback_msg
->hook
= 0;
523 callback_msg
->hook_proc
= NULL
;
524 callback_msg
->result
= NULL
;
525 callback_msg
->data
= NULL
;
526 callback_msg
->data_size
= 0;
528 result
->callback_msg
= callback_msg
;
529 list_add_head( &send_queue
->callback_result
, &result
->sender_entry
);
533 result
->callback_msg
= NULL
;
534 list_add_head( &send_queue
->send_result
, &result
->sender_entry
);
540 gettimeofday( &when
, NULL
);
541 add_timeout( &when
, timeout
);
542 result
->timeout
= add_timeout_user( &when
, result_timeout
, result
);
548 /* receive a message, removing it from the sent queue */
549 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
550 struct get_message_reply
*reply
)
552 struct message_result
*result
= msg
->result
;
554 reply
->total
= msg
->data_size
;
555 if (msg
->data_size
> get_reply_max_size())
557 set_error( STATUS_BUFFER_OVERFLOW
);
560 reply
->type
= msg
->type
;
561 reply
->win
= msg
->win
;
562 reply
->msg
= msg
->msg
;
563 reply
->wparam
= msg
->wparam
;
564 reply
->lparam
= msg
->lparam
;
567 reply
->time
= msg
->time
;
568 reply
->info
= msg
->info
;
569 reply
->hook
= msg
->hook
;
570 reply
->hook_proc
= msg
->hook_proc
;
572 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
574 list_remove( &msg
->entry
);
575 /* put the result on the receiver result stack */
578 result
->recv_next
= queue
->recv_result
;
579 queue
->recv_result
= result
;
582 if (list_empty( &queue
->msg_list
[SEND_MESSAGE
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
585 /* set the result of the current received message */
586 static void reply_message( struct msg_queue
*queue
, unsigned int result
,
587 unsigned int error
, int remove
, const void *data
, size_t len
)
589 struct message_result
*res
= queue
->recv_result
;
593 queue
->recv_result
= res
->recv_next
;
594 res
->receiver
= NULL
;
595 if (!res
->sender
) /* no one waiting for it */
603 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
604 store_message_result( res
, result
, error
);
608 /* retrieve a posted message */
609 static int get_posted_message( struct msg_queue
*queue
, user_handle_t win
,
610 unsigned int first
, unsigned int last
, unsigned int flags
,
611 struct get_message_reply
*reply
)
615 /* check against the filters */
616 LIST_FOR_EACH_ENTRY( msg
, &queue
->msg_list
[POST_MESSAGE
], struct message
, entry
)
618 if (msg
->msg
== WM_QUIT
) goto found
; /* WM_QUIT is never filtered */
619 if (win
&& msg
->win
&& msg
->win
!= win
&& !is_child_window( win
, msg
->win
)) continue;
620 if (!check_msg_filter( msg
->msg
, first
, last
)) continue;
621 goto found
; /* found one */
625 /* return it to the app */
627 reply
->total
= msg
->data_size
;
628 if (msg
->data_size
> get_reply_max_size())
630 set_error( STATUS_BUFFER_OVERFLOW
);
633 reply
->type
= msg
->type
;
634 reply
->win
= msg
->win
;
635 reply
->msg
= msg
->msg
;
636 reply
->wparam
= msg
->wparam
;
637 reply
->lparam
= msg
->lparam
;
640 reply
->time
= msg
->time
;
641 reply
->info
= msg
->info
;
643 if (flags
& GET_MSG_REMOVE
)
647 set_reply_data_ptr( msg
->data
, msg
->data_size
);
651 remove_queue_message( queue
, msg
, POST_MESSAGE
);
653 else if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
658 /* empty a message list and free all the messages */
659 static void empty_msg_list( struct list
*list
)
663 while ((ptr
= list_head( list
)) != NULL
)
665 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
666 list_remove( &msg
->entry
);
671 /* cleanup all pending results when deleting a queue */
672 static void cleanup_results( struct msg_queue
*queue
)
676 while ((entry
= list_head( &queue
->send_result
)) != NULL
)
678 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
681 while ((entry
= list_head( &queue
->callback_result
)) != NULL
)
683 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
686 while (queue
->recv_result
)
687 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
690 /* check if the thread owning the queue is hung (not checking for messages) */
691 static int is_queue_hung( struct msg_queue
*queue
)
694 struct wait_queue_entry
*entry
;
696 gettimeofday( &now
, NULL
);
697 if (now
.tv_sec
- queue
->last_get_msg
.tv_sec
<= 5)
698 return 0; /* less than 5 seconds since last get message -> not hung */
700 LIST_FOR_EACH_ENTRY( entry
, &queue
->obj
.wait_queue
, struct wait_queue_entry
, entry
)
702 if (entry
->thread
->queue
== queue
)
703 return 0; /* thread is waiting on queue -> not hung */
708 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
710 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
711 struct process
*process
= entry
->thread
->process
;
713 /* a thread can only wait on its own queue */
714 if (entry
->thread
->queue
!= queue
)
716 set_error( STATUS_ACCESS_DENIED
);
719 /* if waiting on the main process queue, set the idle event */
720 if (process
->queue
== queue
)
722 if (process
->idle_event
) set_event( process
->idle_event
);
724 add_queue( obj
, entry
);
728 static void msg_queue_remove_queue(struct object
*obj
, struct wait_queue_entry
*entry
)
730 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
731 struct process
*process
= entry
->thread
->process
;
733 remove_queue( obj
, entry
);
735 assert( entry
->thread
->queue
== queue
);
737 /* if waiting on the main process queue, reset the idle event */
738 if (process
->queue
== queue
)
740 if (process
->idle_event
) reset_event( process
->idle_event
);
744 static void msg_queue_dump( struct object
*obj
, int verbose
)
746 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
747 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
748 queue
->wake_bits
, queue
->wake_mask
);
751 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
)
753 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
754 return is_signaled( queue
);
757 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
)
759 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
760 queue
->wake_mask
= 0;
761 queue
->changed_mask
= 0;
762 return 0; /* Not abandoned */
765 static void msg_queue_destroy( struct object
*obj
)
767 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
771 cleanup_results( queue
);
772 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
774 while ((ptr
= list_head( &queue
->pending_timers
)))
776 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
777 list_remove( &timer
->entry
);
780 while ((ptr
= list_head( &queue
->expired_timers
)))
782 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
783 list_remove( &timer
->entry
);
786 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
787 if (queue
->input
) release_object( queue
->input
);
788 if (queue
->hooks
) release_object( queue
->hooks
);
791 static void thread_input_dump( struct object
*obj
, int verbose
)
793 struct thread_input
*input
= (struct thread_input
*)obj
;
794 fprintf( stderr
, "Thread input focus=%p capture=%p active=%p\n",
795 input
->focus
, input
->capture
, input
->active
);
798 static void thread_input_destroy( struct object
*obj
)
800 struct thread_input
*input
= (struct thread_input
*)obj
;
802 if (foreground_input
== input
) foreground_input
= NULL
;
803 empty_msg_list( &input
->msg_list
);
804 release_object( input
->desktop
);
807 /* fix the thread input data when a window is destroyed */
808 inline static void thread_input_cleanup_window( struct msg_queue
*queue
, user_handle_t window
)
810 struct thread_input
*input
= queue
->input
;
812 if (window
== input
->focus
) input
->focus
= 0;
813 if (window
== input
->capture
) input
->capture
= 0;
814 if (window
== input
->active
) input
->active
= 0;
815 if (window
== input
->menu_owner
) input
->menu_owner
= 0;
816 if (window
== input
->move_size
) input
->move_size
= 0;
817 if (window
== input
->caret
) set_caret_window( input
, 0 );
820 /* check if the specified window can be set in the input data of a given queue */
821 static int check_queue_input_window( struct msg_queue
*queue
, user_handle_t window
)
823 struct thread
*thread
;
826 if (!window
) return 1; /* we can always clear the data */
828 if ((thread
= get_window_thread( window
)))
830 ret
= (queue
->input
== thread
->queue
->input
);
831 if (!ret
) set_error( STATUS_ACCESS_DENIED
);
832 release_object( thread
);
834 else set_error( STATUS_INVALID_HANDLE
);
839 /* make sure the specified thread has a queue */
840 int init_thread_queue( struct thread
*thread
)
842 if (thread
->queue
) return 1;
843 return (create_msg_queue( thread
, NULL
) != NULL
);
846 /* attach two thread input data structures */
847 int attach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
849 struct desktop
*desktop
;
850 struct thread_input
*input
;
852 if (!thread_to
->queue
&& !(thread_to
->queue
= create_msg_queue( thread_to
, NULL
))) return 0;
853 if (!(desktop
= get_thread_desktop( thread_from
, 0 ))) return 0;
854 input
= (struct thread_input
*)grab_object( thread_to
->queue
->input
);
855 if (input
->desktop
!= desktop
)
857 set_error( STATUS_ACCESS_DENIED
);
858 release_object( input
);
859 release_object( desktop
);
862 release_object( desktop
);
864 if (thread_from
->queue
)
866 release_thread_input( thread_from
);
867 thread_from
->queue
->input
= input
;
871 if (!(thread_from
->queue
= create_msg_queue( thread_from
, input
))) return 0;
873 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
877 /* detach two thread input data structures */
878 void detach_thread_input( struct thread
*thread_from
)
880 struct thread_input
*input
;
882 if ((input
= create_thread_input( thread_from
)))
884 release_thread_input( thread_from
);
885 thread_from
->queue
->input
= input
;
890 /* set the next timer to expire */
891 static void set_next_timer( struct msg_queue
*queue
)
897 remove_timeout_user( queue
->timeout
);
898 queue
->timeout
= NULL
;
900 if ((ptr
= list_head( &queue
->pending_timers
)))
902 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
903 queue
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, queue
);
905 /* set/clear QS_TIMER bit */
906 if (list_empty( &queue
->expired_timers
))
907 clear_queue_bits( queue
, QS_TIMER
);
909 set_queue_bits( queue
, QS_TIMER
);
912 /* find a timer from its window and id */
913 static struct timer
*find_timer( struct msg_queue
*queue
, user_handle_t win
,
914 unsigned int msg
, unsigned int id
)
918 /* we need to search both lists */
920 LIST_FOR_EACH( ptr
, &queue
->pending_timers
)
922 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
923 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
925 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
927 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
928 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
933 /* callback for the next timer expiration */
934 static void timer_callback( void *private )
936 struct msg_queue
*queue
= private;
939 queue
->timeout
= NULL
;
940 /* move on to the next timer */
941 ptr
= list_head( &queue
->pending_timers
);
943 list_add_tail( &queue
->expired_timers
, ptr
);
944 set_next_timer( queue
);
947 /* link a timer at its rightful place in the queue list */
948 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
952 for (ptr
= queue
->pending_timers
.next
; ptr
!= &queue
->pending_timers
; ptr
= ptr
->next
)
954 struct timer
*t
= LIST_ENTRY( ptr
, struct timer
, entry
);
955 if (!time_before( &t
->when
, &timer
->when
)) break;
957 list_add_before( ptr
, &timer
->entry
);
960 /* remove a timer from the queue timer list and free it */
961 static void free_timer( struct msg_queue
*queue
, struct timer
*timer
)
963 list_remove( &timer
->entry
);
965 set_next_timer( queue
);
968 /* restart an expired timer */
969 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
973 list_remove( &timer
->entry
);
974 gettimeofday( &now
, NULL
);
975 while (!time_before( &now
, &timer
->when
)) add_timeout( &timer
->when
, timer
->rate
);
976 link_timer( queue
, timer
);
977 set_next_timer( queue
);
980 /* find an expired timer matching the filtering parameters */
981 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
982 unsigned int get_first
, unsigned int get_last
,
987 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
989 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
990 if (win
&& timer
->win
!= win
) continue;
991 if (check_msg_filter( timer
->msg
, get_first
, get_last
))
993 if (remove
) restart_timer( queue
, timer
);
1001 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
1003 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
1006 timer
->rate
= max( rate
, 1 );
1007 gettimeofday( &timer
->when
, NULL
);
1008 add_timeout( &timer
->when
, rate
);
1009 link_timer( queue
, timer
);
1010 /* check if we replaced the next timer */
1011 if (list_head( &queue
->pending_timers
) == &timer
->entry
) set_next_timer( queue
);
1016 /* change the input key state for a given key */
1017 static void set_input_key_state( struct thread_input
*input
, unsigned char key
, int down
)
1021 if (!(input
->keystate
[key
] & 0x80)) input
->keystate
[key
] ^= 0x01;
1022 input
->keystate
[key
] |= 0x80;
1024 else input
->keystate
[key
] &= ~0x80;
1027 /* update the input key state for a keyboard message */
1028 static void update_input_key_state( struct thread_input
*input
, const struct message
*msg
)
1031 int down
= 0, extended
;
1035 case WM_LBUTTONDOWN
:
1039 set_input_key_state( input
, VK_LBUTTON
, down
);
1041 case WM_MBUTTONDOWN
:
1045 set_input_key_state( input
, VK_MBUTTON
, down
);
1047 case WM_RBUTTONDOWN
:
1051 set_input_key_state( input
, VK_RBUTTON
, down
);
1053 case WM_XBUTTONDOWN
:
1057 if (msg
->wparam
== XBUTTON1
) set_input_key_state( input
, VK_XBUTTON1
, down
);
1058 else if (msg
->wparam
== XBUTTON2
) set_input_key_state( input
, VK_XBUTTON2
, down
);
1066 key
= (unsigned char)msg
->wparam
;
1067 extended
= ((msg
->lparam
>> 16) & KF_EXTENDED
) != 0;
1068 set_input_key_state( input
, key
, down
);
1072 set_input_key_state( input
, extended
? VK_RSHIFT
: VK_LSHIFT
, down
);
1075 set_input_key_state( input
, extended
? VK_RCONTROL
: VK_LCONTROL
, down
);
1078 set_input_key_state( input
, extended
? VK_RMENU
: VK_LMENU
, down
);
1085 /* release the hardware message currently being processed by the given thread */
1086 static void release_hardware_message( struct msg_queue
*queue
, unsigned int hw_id
,
1087 int remove
, user_handle_t new_win
)
1089 struct thread_input
*input
= queue
->input
;
1090 struct message
*msg
;
1092 LIST_FOR_EACH_ENTRY( msg
, &input
->msg_list
, struct message
, entry
)
1094 if (msg
->unique_id
== hw_id
) break;
1096 if (&msg
->entry
== &input
->msg_list
) return; /* not found */
1098 /* clear the queue bit for that message */
1099 if (remove
|| new_win
)
1101 struct message
*other
;
1104 clr_bit
= get_hardware_msg_bit( msg
);
1105 LIST_FOR_EACH_ENTRY( other
, &input
->msg_list
, struct message
, entry
)
1107 if (other
!= msg
&& get_hardware_msg_bit( other
) == clr_bit
)
1113 if (clr_bit
) clear_queue_bits( queue
, clr_bit
);
1116 if (new_win
) /* set the new window */
1118 struct thread
*owner
= get_window_thread( new_win
);
1121 if (owner
->queue
->input
== input
)
1124 set_queue_bits( owner
->queue
, get_hardware_msg_bit( msg
));
1127 release_object( owner
);
1132 update_input_key_state( input
, msg
);
1133 list_remove( &msg
->entry
);
1134 free_message( msg
);
1138 /* find the window that should receive a given hardware message */
1139 static user_handle_t
find_hardware_message_window( struct thread_input
*input
, struct message
*msg
,
1140 unsigned int *msg_code
)
1142 user_handle_t win
= 0;
1144 *msg_code
= msg
->msg
;
1145 if (is_keyboard_msg( msg
))
1147 if (input
&& !(win
= input
->focus
))
1149 win
= input
->active
;
1150 if (*msg_code
< WM_SYSKEYDOWN
) *msg_code
+= WM_SYSKEYDOWN
- WM_KEYDOWN
;
1153 else /* mouse message */
1155 if (!input
|| !(win
= input
->capture
))
1157 if (!(win
= msg
->win
) || !is_window_visible( win
))
1159 if (input
) win
= window_from_point( input
->desktop
, msg
->x
, msg
->y
);
1166 /* queue a hardware message into a given thread input */
1167 static void queue_hardware_message( struct msg_queue
*queue
, struct message
*msg
)
1170 struct thread
*thread
;
1171 struct thread_input
*input
;
1172 unsigned int msg_code
;
1174 last_input_time
= get_tick_count();
1176 win
= find_hardware_message_window( queue
? queue
->input
: foreground_input
, msg
, &msg_code
);
1177 if (!win
|| !(thread
= get_window_thread(win
)))
1182 input
= thread
->queue
->input
;
1184 if (msg
->msg
== WM_MOUSEMOVE
&& merge_message( input
, msg
)) free( msg
);
1187 msg
->unique_id
= 0; /* will be set once we return it to the app */
1188 list_add_tail( &input
->msg_list
, &msg
->entry
);
1189 set_queue_bits( thread
->queue
, get_hardware_msg_bit(msg
) );
1191 release_object( thread
);
1194 /* check message filter for a hardware message */
1195 static int check_hw_message_filter( user_handle_t win
, unsigned int msg_code
,
1196 user_handle_t filter_win
, unsigned int first
, unsigned int last
)
1198 if (msg_code
>= WM_KEYFIRST
&& msg_code
<= WM_KEYLAST
)
1200 /* we can only test the window for a keyboard message since the
1201 * dest window for a mouse message depends on hittest */
1202 if (filter_win
&& win
!= filter_win
&& !is_child_window( filter_win
, win
))
1204 /* the message code is final for a keyboard message, we can simply check it */
1205 return check_msg_filter( msg_code
, first
, last
);
1207 else /* mouse message */
1209 /* we need to check all possible values that the message can have in the end */
1211 if (check_msg_filter( msg_code
, first
, last
)) return 1;
1212 if (msg_code
== WM_MOUSEWHEEL
) return 0; /* no other possible value for this one */
1214 /* all other messages can become non-client messages */
1215 if (check_msg_filter( msg_code
+ (WM_NCMOUSEFIRST
- WM_MOUSEFIRST
), first
, last
)) return 1;
1217 /* clicks can become double-clicks or non-client double-clicks */
1218 if (msg_code
== WM_LBUTTONDOWN
|| msg_code
== WM_MBUTTONDOWN
||
1219 msg_code
== WM_RBUTTONDOWN
|| msg_code
== WM_XBUTTONDOWN
)
1221 if (check_msg_filter( msg_code
+ (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1222 if (check_msg_filter( msg_code
+ (WM_NCLBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1229 /* find a hardware message for the given queue */
1230 static int get_hardware_message( struct thread
*thread
, int hw_id
, user_handle_t filter_win
,
1231 unsigned int first
, unsigned int last
, struct get_message_reply
*reply
)
1233 struct thread_input
*input
= thread
->queue
->input
;
1234 struct thread
*win_thread
;
1237 int clear_bits
, got_one
= 0;
1238 unsigned int msg_code
;
1240 ptr
= list_head( &input
->msg_list
);
1245 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1246 if (msg
->unique_id
== hw_id
) break;
1247 ptr
= list_next( &input
->msg_list
, ptr
);
1249 if (!ptr
) ptr
= list_head( &input
->msg_list
);
1250 else ptr
= list_next( &input
->msg_list
, ptr
); /* start from the next one */
1253 if (ptr
== list_head( &input
->msg_list
))
1254 clear_bits
= QS_KEY
| QS_MOUSEMOVE
| QS_MOUSEBUTTON
;
1256 clear_bits
= 0; /* don't clear bits if we don't go through the whole list */
1260 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1261 ptr
= list_next( &input
->msg_list
, ptr
);
1262 win
= find_hardware_message_window( input
, msg
, &msg_code
);
1263 if (!win
|| !(win_thread
= get_window_thread( win
)))
1265 /* no window at all, remove it */
1266 update_input_key_state( input
, msg
);
1267 list_remove( &msg
->entry
);
1268 free_message( msg
);
1271 if (win_thread
!= thread
)
1273 if (win_thread
->queue
->input
== input
)
1275 /* wake the other thread */
1276 set_queue_bits( win_thread
->queue
, get_hardware_msg_bit(msg
) );
1281 /* for another thread input, drop it */
1282 update_input_key_state( input
, msg
);
1283 list_remove( &msg
->entry
);
1284 free_message( msg
);
1286 release_object( win_thread
);
1289 release_object( win_thread
);
1291 /* if we already got a message for another thread, or if it doesn't
1292 * match the filter we skip it */
1293 if (got_one
|| !check_hw_message_filter( win
, msg_code
, filter_win
, first
, last
))
1295 clear_bits
&= ~get_hardware_msg_bit( msg
);
1298 /* now we can return it */
1299 if (!msg
->unique_id
) msg
->unique_id
= get_unique_id();
1300 reply
->type
= MSG_HARDWARE
;
1302 reply
->msg
= msg_code
;
1303 reply
->wparam
= msg
->wparam
;
1304 reply
->lparam
= msg
->lparam
;
1307 reply
->time
= msg
->time
;
1308 reply
->info
= msg
->info
;
1309 reply
->hw_id
= msg
->unique_id
;
1312 /* nothing found, clear the hardware queue bits */
1313 clear_queue_bits( thread
->queue
, clear_bits
);
1317 /* increment (or decrement if 'incr' is negative) the queue paint count */
1318 void inc_queue_paint_count( struct thread
*thread
, int incr
)
1320 struct msg_queue
*queue
= thread
->queue
;
1324 if ((queue
->paint_count
+= incr
) < 0) queue
->paint_count
= 0;
1326 if (queue
->paint_count
)
1327 set_queue_bits( queue
, QS_PAINT
);
1329 clear_queue_bits( queue
, QS_PAINT
);
1333 /* remove all messages and timers belonging to a certain window */
1334 void queue_cleanup_window( struct thread
*thread
, user_handle_t win
)
1336 struct msg_queue
*queue
= thread
->queue
;
1344 ptr
= list_head( &queue
->pending_timers
);
1347 struct list
*next
= list_next( &queue
->pending_timers
, ptr
);
1348 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1349 if (timer
->win
== win
) free_timer( queue
, timer
);
1352 ptr
= list_head( &queue
->expired_timers
);
1355 struct list
*next
= list_next( &queue
->expired_timers
, ptr
);
1356 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1357 if (timer
->win
== win
) free_timer( queue
, timer
);
1361 /* remove messages */
1362 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
1364 struct list
*ptr
, *next
;
1366 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->msg_list
[i
] )
1368 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1369 if (msg
->win
== win
) remove_queue_message( queue
, msg
, i
);
1373 thread_input_cleanup_window( queue
, win
);
1376 /* post a message to a window; used by socket handling */
1377 void post_message( user_handle_t win
, unsigned int message
,
1378 unsigned int wparam
, unsigned int lparam
)
1380 struct message
*msg
;
1381 struct thread
*thread
= get_window_thread( win
);
1383 if (!thread
) return;
1385 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1387 msg
->type
= MSG_POSTED
;
1388 msg
->win
= get_user_full_handle( win
);
1390 msg
->wparam
= wparam
;
1391 msg
->lparam
= lparam
;
1392 msg
->time
= get_tick_count();
1397 msg
->hook_proc
= NULL
;
1402 list_add_tail( &thread
->queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1403 set_queue_bits( thread
->queue
, QS_POSTMESSAGE
);
1405 release_object( thread
);
1408 /* post a win event */
1409 void post_win_event( struct thread
*thread
, unsigned int event
,
1410 user_handle_t win
, unsigned int object_id
,
1411 unsigned int child_id
, void *hook_proc
,
1412 const WCHAR
*module
, size_t module_size
,
1415 struct message
*msg
;
1417 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1419 msg
->type
= MSG_WINEVENT
;
1420 msg
->win
= get_user_full_handle( win
);
1422 msg
->wparam
= object_id
;
1423 msg
->lparam
= child_id
;
1424 msg
->time
= get_tick_count();
1427 msg
->info
= get_thread_id( current
);
1430 msg
->hook_proc
= hook_proc
;
1432 if ((msg
->data
= malloc( module_size
)))
1434 msg
->data_size
= module_size
;
1435 memcpy( msg
->data
, module
, module_size
);
1437 if (debug_level
> 1)
1438 fprintf( stderr
, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1439 get_thread_id(thread
), event
, win
, object_id
, child_id
);
1440 list_add_tail( &thread
->queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1441 set_queue_bits( thread
->queue
, QS_SENDMESSAGE
);
1448 /* get the message queue of the current thread */
1449 DECL_HANDLER(get_msg_queue
)
1451 struct msg_queue
*queue
= get_current_queue();
1454 if (queue
) reply
->handle
= alloc_handle( current
->process
, queue
, SYNCHRONIZE
, 0 );
1458 /* set the current message queue wakeup mask */
1459 DECL_HANDLER(set_queue_mask
)
1461 struct msg_queue
*queue
= get_current_queue();
1465 queue
->wake_mask
= req
->wake_mask
;
1466 queue
->changed_mask
= req
->changed_mask
;
1467 reply
->wake_bits
= queue
->wake_bits
;
1468 reply
->changed_bits
= queue
->changed_bits
;
1469 if (is_signaled( queue
))
1471 /* if skip wait is set, do what would have been done in the subsequent wait */
1472 if (req
->skip_wait
) msg_queue_satisfied( &queue
->obj
, current
);
1473 else wake_up( &queue
->obj
, 0 );
1479 /* get the current message queue status */
1480 DECL_HANDLER(get_queue_status
)
1482 struct msg_queue
*queue
= current
->queue
;
1485 reply
->wake_bits
= queue
->wake_bits
;
1486 reply
->changed_bits
= queue
->changed_bits
;
1487 if (req
->clear
) queue
->changed_bits
= 0;
1489 else reply
->wake_bits
= reply
->changed_bits
= 0;
1493 /* send a message to a thread queue */
1494 DECL_HANDLER(send_message
)
1496 struct message
*msg
;
1497 struct msg_queue
*send_queue
= get_current_queue();
1498 struct msg_queue
*recv_queue
= NULL
;
1499 struct thread
*thread
= NULL
;
1503 if (!(thread
= get_thread_from_id( req
->id
))) return;
1505 else if (req
->type
!= MSG_HARDWARE
)
1507 /* only hardware messages are allowed without destination thread */
1508 set_error( STATUS_INVALID_PARAMETER
);
1512 if (thread
&& !(recv_queue
= thread
->queue
))
1514 set_error( STATUS_INVALID_PARAMETER
);
1515 release_object( thread
);
1518 if (recv_queue
&& (req
->flags
& SEND_MSG_ABORT_IF_HUNG
) && is_queue_hung(recv_queue
))
1520 set_error( STATUS_TIMEOUT
);
1521 release_object( thread
);
1525 if ((msg
= mem_alloc( sizeof(*msg
) )))
1527 msg
->type
= req
->type
;
1528 msg
->win
= get_user_full_handle( req
->win
);
1529 msg
->msg
= req
->msg
;
1530 msg
->wparam
= req
->wparam
;
1531 msg
->lparam
= req
->lparam
;
1532 msg
->time
= req
->time
;
1535 msg
->info
= req
->info
;
1537 msg
->hook_proc
= NULL
;
1544 case MSG_OTHER_PROCESS
:
1545 msg
->data_size
= get_req_data_size();
1546 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1555 if (!(msg
->result
= alloc_message_result( send_queue
, recv_queue
, msg
,
1556 req
->timeout
, req
->callback
, req
->info
)))
1558 free_message( msg
);
1563 list_add_tail( &recv_queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1564 set_queue_bits( recv_queue
, QS_SENDMESSAGE
);
1567 /* needed for posted DDE messages */
1568 msg
->data_size
= get_req_data_size();
1569 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1574 list_add_tail( &recv_queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1575 set_queue_bits( recv_queue
, QS_POSTMESSAGE
);
1578 queue_hardware_message( recv_queue
, msg
);
1580 case MSG_CALLBACK_RESULT
: /* cannot send this one */
1582 set_error( STATUS_INVALID_PARAMETER
);
1587 if (thread
) release_object( thread
);
1591 /* get a message from the current queue */
1592 DECL_HANDLER(get_message
)
1594 struct timer
*timer
;
1596 struct msg_queue
*queue
= get_current_queue();
1597 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
1599 reply
->active_hooks
= get_active_hooks();
1602 gettimeofday( &queue
->last_get_msg
, NULL
);
1604 /* first check for sent messages */
1605 if ((ptr
= list_head( &queue
->msg_list
[SEND_MESSAGE
] )))
1607 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1608 receive_message( queue
, msg
, reply
);
1611 if (req
->flags
& GET_MSG_SENT_ONLY
) goto done
; /* nothing else to check */
1613 /* clear changed bits so we can wait on them if we don't find a message */
1614 queue
->changed_bits
= 0;
1616 /* then check for posted messages */
1617 if (get_posted_message( queue
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
1620 /* then check for any raw hardware message */
1621 if (filter_contains_hw_range( req
->get_first
, req
->get_last
) &&
1622 get_hardware_message( current
, req
->hw_id
, get_win
, req
->get_first
, req
->get_last
, reply
))
1625 /* now check for WM_PAINT */
1626 if (queue
->paint_count
&&
1627 check_msg_filter( WM_PAINT
, req
->get_first
, req
->get_last
) &&
1628 (reply
->win
= find_window_to_repaint( get_win
, current
)))
1630 reply
->type
= MSG_POSTED
;
1631 reply
->msg
= WM_PAINT
;
1636 reply
->time
= get_tick_count();
1641 /* now check for timer */
1642 if ((timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
1643 req
->get_last
, (req
->flags
& GET_MSG_REMOVE
) )))
1645 reply
->type
= MSG_POSTED
;
1646 reply
->win
= timer
->win
;
1647 reply
->msg
= timer
->msg
;
1648 reply
->wparam
= timer
->id
;
1649 reply
->lparam
= timer
->lparam
;
1652 reply
->time
= get_tick_count();
1658 set_error( STATUS_PENDING
); /* FIXME */
1662 /* reply to a sent message */
1663 DECL_HANDLER(reply_message
)
1665 if (!current
->queue
) set_error( STATUS_ACCESS_DENIED
);
1666 else if (current
->queue
->recv_result
)
1667 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
1668 get_req_data(), get_req_data_size() );
1672 /* accept the current hardware message */
1673 DECL_HANDLER(accept_hardware_message
)
1676 release_hardware_message( current
->queue
, req
->hw_id
, req
->remove
, req
->new_win
);
1678 set_error( STATUS_ACCESS_DENIED
);
1682 /* retrieve the reply for the last message sent */
1683 DECL_HANDLER(get_message_reply
)
1685 struct message_result
*result
;
1687 struct msg_queue
*queue
= current
->queue
;
1691 set_error( STATUS_PENDING
);
1694 if (!(entry
= list_head( &queue
->send_result
))) return; /* no reply ready */
1696 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1697 if (result
->replied
|| req
->cancel
)
1699 if (result
->replied
)
1701 reply
->result
= result
->result
;
1702 set_error( result
->error
);
1705 size_t data_len
= min( result
->data_size
, get_reply_max_size() );
1706 set_reply_data_ptr( result
->data
, data_len
);
1707 result
->data
= NULL
;
1708 result
->data_size
= 0;
1711 remove_result_from_sender( result
);
1713 entry
= list_head( &queue
->send_result
);
1714 if (!entry
) clear_queue_bits( queue
, QS_SMRESULT
);
1717 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1718 if (!result
->replied
) clear_queue_bits( queue
, QS_SMRESULT
);
1722 else set_error( STATUS_ACCESS_DENIED
);
1726 /* set a window timer */
1727 DECL_HANDLER(set_win_timer
)
1729 struct timer
*timer
;
1730 struct msg_queue
*queue
;
1731 struct thread
*thread
= NULL
;
1732 user_handle_t win
= 0;
1733 unsigned int id
= req
->id
;
1737 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1739 set_error( STATUS_INVALID_HANDLE
);
1742 if (thread
->process
!= current
->process
)
1744 release_object( thread
);
1745 set_error( STATUS_ACCESS_DENIED
);
1748 queue
= thread
->queue
;
1749 /* remove it if it existed already */
1750 if ((timer
= find_timer( queue
, win
, req
->msg
, id
))) free_timer( queue
, timer
);
1754 queue
= get_current_queue();
1755 /* find a free id for it */
1758 id
= queue
->next_timer_id
;
1759 if (++queue
->next_timer_id
>= 0x10000) queue
->next_timer_id
= 1;
1761 while (find_timer( queue
, 0, req
->msg
, id
));
1764 if ((timer
= set_timer( queue
, req
->rate
)))
1767 timer
->msg
= req
->msg
;
1769 timer
->lparam
= req
->lparam
;
1772 if (thread
) release_object( thread
);
1775 /* kill a window timer */
1776 DECL_HANDLER(kill_win_timer
)
1778 struct timer
*timer
;
1779 struct thread
*thread
;
1780 user_handle_t win
= 0;
1784 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1786 set_error( STATUS_INVALID_HANDLE
);
1789 if (thread
->process
!= current
->process
)
1791 release_object( thread
);
1792 set_error( STATUS_ACCESS_DENIED
);
1796 else thread
= (struct thread
*)grab_object( current
);
1798 if (thread
->queue
&& (timer
= find_timer( thread
->queue
, win
, req
->msg
, req
->id
)))
1799 free_timer( thread
->queue
, timer
);
1801 set_error( STATUS_INVALID_PARAMETER
);
1803 release_object( thread
);
1807 /* attach (or detach) thread inputs */
1808 DECL_HANDLER(attach_thread_input
)
1810 struct thread
*thread_from
= get_thread_from_id( req
->tid_from
);
1811 struct thread
*thread_to
= get_thread_from_id( req
->tid_to
);
1813 if (!thread_from
|| !thread_to
)
1815 if (thread_from
) release_object( thread_from
);
1816 if (thread_to
) release_object( thread_to
);
1819 if (thread_from
!= thread_to
)
1821 if (req
->attach
) attach_thread_input( thread_from
, thread_to
);
1824 if (thread_from
->queue
&& thread_to
->queue
&&
1825 thread_from
->queue
->input
== thread_to
->queue
->input
)
1826 detach_thread_input( thread_from
);
1828 set_error( STATUS_ACCESS_DENIED
);
1831 else set_error( STATUS_ACCESS_DENIED
);
1832 release_object( thread_from
);
1833 release_object( thread_to
);
1837 /* get thread input data */
1838 DECL_HANDLER(get_thread_input
)
1840 struct thread
*thread
= NULL
;
1841 struct thread_input
*input
;
1845 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1846 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1848 else input
= foreground_input
; /* get the foreground thread info */
1852 reply
->focus
= input
->focus
;
1853 reply
->capture
= input
->capture
;
1854 reply
->active
= input
->active
;
1855 reply
->menu_owner
= input
->menu_owner
;
1856 reply
->move_size
= input
->move_size
;
1857 reply
->caret
= input
->caret
;
1858 reply
->rect
= input
->caret_rect
;
1865 reply
->menu_owner
= 0;
1866 reply
->move_size
= 0;
1868 reply
->rect
.left
= reply
->rect
.top
= reply
->rect
.right
= reply
->rect
.bottom
= 0;
1870 /* foreground window is active window of foreground thread */
1871 reply
->foreground
= foreground_input
? foreground_input
->active
: 0;
1872 if (thread
) release_object( thread
);
1876 /* retrieve queue keyboard state for a given thread */
1877 DECL_HANDLER(get_key_state
)
1879 struct thread
*thread
;
1880 struct thread_input
*input
;
1882 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1883 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1886 if (req
->key
>= 0) reply
->state
= input
->keystate
[req
->key
& 0xff];
1887 set_reply_data( input
->keystate
, min( get_reply_max_size(), sizeof(input
->keystate
) ));
1889 release_object( thread
);
1893 /* set queue keyboard state for a given thread */
1894 DECL_HANDLER(set_key_state
)
1896 struct thread
*thread
= NULL
;
1897 struct thread_input
*input
;
1899 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1900 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1903 size_t size
= min( sizeof(input
->keystate
), get_req_data_size() );
1904 if (size
) memcpy( input
->keystate
, get_req_data(), size
);
1906 release_object( thread
);
1910 /* set the system foreground window */
1911 DECL_HANDLER(set_foreground_window
)
1913 struct msg_queue
*queue
= get_current_queue();
1915 reply
->previous
= foreground_input
? foreground_input
->active
: 0;
1916 reply
->send_msg_old
= (reply
->previous
&& foreground_input
!= queue
->input
);
1917 reply
->send_msg_new
= FALSE
;
1921 struct thread
*thread
;
1923 if (is_top_level_window( req
->handle
) &&
1924 ((thread
= get_window_thread( req
->handle
))))
1926 foreground_input
= thread
->queue
->input
;
1927 reply
->send_msg_new
= (foreground_input
!= queue
->input
);
1928 release_object( thread
);
1930 else set_error( STATUS_INVALID_HANDLE
);
1932 else foreground_input
= NULL
;
1936 /* set the current thread focus window */
1937 DECL_HANDLER(set_focus_window
)
1939 struct msg_queue
*queue
= get_current_queue();
1941 reply
->previous
= 0;
1942 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1944 reply
->previous
= queue
->input
->focus
;
1945 queue
->input
->focus
= get_user_full_handle( req
->handle
);
1950 /* set the current thread active window */
1951 DECL_HANDLER(set_active_window
)
1953 struct msg_queue
*queue
= get_current_queue();
1955 reply
->previous
= 0;
1956 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1958 if (!req
->handle
|| make_window_active( req
->handle
))
1960 reply
->previous
= queue
->input
->active
;
1961 queue
->input
->active
= get_user_full_handle( req
->handle
);
1963 else set_error( STATUS_INVALID_HANDLE
);
1968 /* set the current thread capture window */
1969 DECL_HANDLER(set_capture_window
)
1971 struct msg_queue
*queue
= get_current_queue();
1973 reply
->previous
= reply
->full_handle
= 0;
1974 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1976 struct thread_input
*input
= queue
->input
;
1978 reply
->previous
= input
->capture
;
1979 input
->capture
= get_user_full_handle( req
->handle
);
1980 input
->menu_owner
= (req
->flags
& CAPTURE_MENU
) ? input
->capture
: 0;
1981 input
->move_size
= (req
->flags
& CAPTURE_MOVESIZE
) ? input
->capture
: 0;
1982 reply
->full_handle
= input
->capture
;
1987 /* Set the current thread caret window */
1988 DECL_HANDLER(set_caret_window
)
1990 struct msg_queue
*queue
= get_current_queue();
1992 reply
->previous
= 0;
1993 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1995 struct thread_input
*input
= queue
->input
;
1997 reply
->previous
= input
->caret
;
1998 reply
->old_rect
= input
->caret_rect
;
1999 reply
->old_hide
= input
->caret_hide
;
2000 reply
->old_state
= input
->caret_state
;
2002 set_caret_window( input
, get_user_full_handle(req
->handle
) );
2003 input
->caret_rect
.right
= input
->caret_rect
.left
+ req
->width
;
2004 input
->caret_rect
.bottom
= input
->caret_rect
.top
+ req
->height
;
2009 /* Set the current thread caret information */
2010 DECL_HANDLER(set_caret_info
)
2012 struct msg_queue
*queue
= get_current_queue();
2013 struct thread_input
*input
;
2016 input
= queue
->input
;
2017 reply
->full_handle
= input
->caret
;
2018 reply
->old_rect
= input
->caret_rect
;
2019 reply
->old_hide
= input
->caret_hide
;
2020 reply
->old_state
= input
->caret_state
;
2022 if (req
->handle
&& get_user_full_handle(req
->handle
) != input
->caret
)
2024 set_error( STATUS_ACCESS_DENIED
);
2027 if (req
->flags
& SET_CARET_POS
)
2029 input
->caret_rect
.right
+= req
->x
- input
->caret_rect
.left
;
2030 input
->caret_rect
.bottom
+= req
->y
- input
->caret_rect
.top
;
2031 input
->caret_rect
.left
= req
->x
;
2032 input
->caret_rect
.top
= req
->y
;
2034 if (req
->flags
& SET_CARET_HIDE
)
2036 input
->caret_hide
+= req
->hide
;
2037 if (input
->caret_hide
< 0) input
->caret_hide
= 0;
2039 if (req
->flags
& SET_CARET_STATE
)
2041 if (req
->state
== -1) input
->caret_state
= !input
->caret_state
;
2042 else input
->caret_state
= !!req
->state
;
2047 /* get the time of the last input event */
2048 DECL_HANDLER(get_last_input_time
)
2050 reply
->time
= last_input_time
;