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 user_handle_t focus
; /* focus window */
98 user_handle_t capture
; /* capture window */
99 user_handle_t active
; /* active window */
100 user_handle_t menu_owner
; /* current menu owner window */
101 user_handle_t move_size
; /* current moving/resizing window */
102 user_handle_t caret
; /* caret window */
103 rectangle_t caret_rect
; /* caret rectangle */
104 int caret_hide
; /* caret hide count */
105 int caret_state
; /* caret on/off state */
106 struct list msg_list
; /* list of hardware messages */
107 unsigned char keystate
[256]; /* state of each key */
112 struct object obj
; /* object header */
113 unsigned int wake_bits
; /* wakeup bits */
114 unsigned int wake_mask
; /* wakeup mask */
115 unsigned int changed_bits
; /* changed wakeup bits */
116 unsigned int changed_mask
; /* changed wakeup mask */
117 int paint_count
; /* pending paint messages count */
118 struct list msg_list
[NB_MSG_KINDS
]; /* lists of messages */
119 struct list send_result
; /* stack of sent messages waiting for result */
120 struct list callback_result
; /* list of callback messages waiting for result */
121 struct message_result
*recv_result
; /* stack of received messages waiting for result */
122 struct list pending_timers
; /* list of pending timers */
123 struct list expired_timers
; /* list of expired timers */
124 unsigned int next_timer_id
; /* id for the next timer with a 0 window */
125 struct timeout_user
*timeout
; /* timeout for next timer to expire */
126 struct thread_input
*input
; /* thread input descriptor */
127 struct hook_table
*hooks
; /* hook table */
128 struct timeval last_get_msg
; /* time of last get message call */
131 static void msg_queue_dump( struct object
*obj
, int verbose
);
132 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
133 static void msg_queue_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
134 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
);
135 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
);
136 static void msg_queue_destroy( struct object
*obj
);
137 static void thread_input_dump( struct object
*obj
, int verbose
);
138 static void thread_input_destroy( struct object
*obj
);
139 static void timer_callback( void *private );
141 static const struct object_ops msg_queue_ops
=
143 sizeof(struct msg_queue
), /* size */
144 msg_queue_dump
, /* dump */
145 msg_queue_add_queue
, /* add_queue */
146 msg_queue_remove_queue
, /* remove_queue */
147 msg_queue_signaled
, /* signaled */
148 msg_queue_satisfied
, /* satisfied */
149 no_signal
, /* signal */
150 no_get_fd
, /* get_fd */
151 msg_queue_destroy
/* destroy */
155 static const struct object_ops thread_input_ops
=
157 sizeof(struct thread_input
), /* size */
158 thread_input_dump
, /* dump */
159 no_add_queue
, /* add_queue */
160 NULL
, /* remove_queue */
162 NULL
, /* satisfied */
163 no_signal
, /* signal */
164 no_get_fd
, /* get_fd */
165 thread_input_destroy
/* destroy */
168 /* pointer to input structure of foreground thread */
169 static struct thread_input
*foreground_input
;
172 /* set the caret window in a given thread input */
173 static void set_caret_window( struct thread_input
*input
, user_handle_t win
)
175 if (!win
|| win
!= input
->caret
)
177 input
->caret_rect
.left
= 0;
178 input
->caret_rect
.top
= 0;
179 input
->caret_rect
.right
= 0;
180 input
->caret_rect
.bottom
= 0;
183 input
->caret_hide
= 1;
184 input
->caret_state
= 0;
187 /* create a thread input object */
188 static struct thread_input
*create_thread_input(void)
190 struct thread_input
*input
;
192 if ((input
= alloc_object( &thread_input_ops
)))
197 input
->menu_owner
= 0;
198 input
->move_size
= 0;
199 list_init( &input
->msg_list
);
200 set_caret_window( input
, 0 );
201 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
206 /* release the thread input data of a given thread */
207 static inline void release_thread_input( struct thread
*thread
)
209 struct thread_input
*input
= thread
->queue
->input
;
212 release_object( input
);
213 thread
->queue
->input
= NULL
;
216 /* create a message queue object */
217 static struct msg_queue
*create_msg_queue( struct thread
*thread
, struct thread_input
*input
)
219 struct msg_queue
*queue
;
222 if (!input
&& !(input
= create_thread_input())) return NULL
;
223 if ((queue
= alloc_object( &msg_queue_ops
)))
225 queue
->wake_bits
= 0;
226 queue
->wake_mask
= 0;
227 queue
->changed_bits
= 0;
228 queue
->changed_mask
= 0;
229 queue
->paint_count
= 0;
230 queue
->recv_result
= NULL
;
231 queue
->next_timer_id
= 1;
232 queue
->timeout
= NULL
;
233 queue
->input
= (struct thread_input
*)grab_object( input
);
235 gettimeofday( &queue
->last_get_msg
, NULL
);
236 list_init( &queue
->send_result
);
237 list_init( &queue
->callback_result
);
238 list_init( &queue
->pending_timers
);
239 list_init( &queue
->expired_timers
);
240 for (i
= 0; i
< NB_MSG_KINDS
; i
++) list_init( &queue
->msg_list
[i
] );
242 thread
->queue
= queue
;
243 if (!thread
->process
->queue
)
244 thread
->process
->queue
= (struct msg_queue
*)grab_object( queue
);
246 release_object( input
);
250 /* free the message queue of a thread at thread exit */
251 void free_msg_queue( struct thread
*thread
)
253 struct process
*process
= thread
->process
;
254 struct thread_input
*input
;
256 remove_thread_hooks( thread
);
257 if (!thread
->queue
) return;
258 if (process
->queue
== thread
->queue
) /* is it the process main queue? */
260 release_object( process
->queue
);
261 process
->queue
= NULL
;
262 if (process
->idle_event
)
264 set_event( process
->idle_event
);
265 release_object( process
->idle_event
);
266 process
->idle_event
= NULL
;
269 input
= thread
->queue
->input
;
270 release_object( thread
->queue
);
271 thread
->queue
= NULL
;
274 /* get the hook table for a given thread */
275 struct hook_table
*get_queue_hooks( struct thread
*thread
)
277 if (!thread
->queue
) return NULL
;
278 return thread
->queue
->hooks
;
281 /* set the hook table for a given thread, allocating the queue if needed */
282 void set_queue_hooks( struct thread
*thread
, struct hook_table
*hooks
)
284 struct msg_queue
*queue
= thread
->queue
;
285 if (!queue
) queue
= create_msg_queue( thread
, NULL
);
286 if (queue
->hooks
) release_object( queue
->hooks
);
287 queue
->hooks
= hooks
;
290 /* check the queue status */
291 inline static int is_signaled( struct msg_queue
*queue
)
293 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
296 /* set some queue bits */
297 inline static void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
299 queue
->wake_bits
|= bits
;
300 queue
->changed_bits
|= bits
;
301 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
304 /* clear some queue bits */
305 inline static void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
307 queue
->wake_bits
&= ~bits
;
308 queue
->changed_bits
&= ~bits
;
311 /* check whether msg is a keyboard message */
312 inline static int is_keyboard_msg( struct message
*msg
)
314 return (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
);
317 /* check if message is matched by the filter */
318 inline static int check_msg_filter( unsigned int msg
, unsigned int first
, unsigned int last
)
320 return (msg
>= first
&& msg
<= last
);
323 /* check whether a message filter contains at least one potential hardware message */
324 inline static int filter_contains_hw_range( unsigned int first
, unsigned int last
)
326 /* hardware message ranges are (in numerical order):
327 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
328 * WM_KEYFIRST .. WM_KEYLAST
329 * WM_MOUSEFIRST .. WM_MOUSELAST
331 if (last
< WM_NCMOUSEFIRST
) return 0;
332 if (first
> WM_NCMOUSELAST
&& last
< WM_KEYFIRST
) return 0;
333 if (first
> WM_KEYLAST
&& last
< WM_MOUSEFIRST
) return 0;
334 if (first
> WM_MOUSELAST
) return 0;
338 /* get the QS_* bit corresponding to a given hardware message */
339 inline static int get_hardware_msg_bit( struct message
*msg
)
341 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
342 if (is_keyboard_msg( msg
)) return QS_KEY
;
343 return QS_MOUSEBUTTON
;
346 /* get the current thread queue, creating it if needed */
347 inline static struct msg_queue
*get_current_queue(void)
349 struct msg_queue
*queue
= current
->queue
;
350 if (!queue
) queue
= create_msg_queue( current
, NULL
);
354 /* get a (pseudo-)unique id to tag hardware messages */
355 inline static unsigned int get_unique_id(void)
357 static unsigned int id
;
358 if (!++id
) id
= 1; /* avoid an id of 0 */
362 /* try to merge a message with the last in the list; return 1 if successful */
363 static int merge_message( struct thread_input
*input
, const struct message
*msg
)
365 struct message
*prev
;
366 struct list
*ptr
= list_tail( &input
->msg_list
);
369 prev
= LIST_ENTRY( ptr
, struct message
, entry
);
370 if (prev
->unique_id
) return 0;
371 if (prev
->result
) return 0;
372 if (prev
->win
!= msg
->win
) return 0;
373 if (prev
->msg
!= msg
->msg
) return 0;
374 if (prev
->type
!= msg
->type
) return 0;
375 /* now we can merge it */
376 prev
->wparam
= msg
->wparam
;
377 prev
->lparam
= msg
->lparam
;
380 prev
->time
= msg
->time
;
381 prev
->info
= msg
->info
;
385 /* free a result structure */
386 static void free_result( struct message_result
*result
)
388 if (result
->timeout
) remove_timeout_user( result
->timeout
);
389 if (result
->data
) free( result
->data
);
390 if (result
->callback_msg
) free( result
->callback_msg
);
394 /* remove the result from the sender list it is on */
395 static inline void remove_result_from_sender( struct message_result
*result
)
397 assert( result
->sender
);
399 list_remove( &result
->sender_entry
);
400 result
->sender
= NULL
;
401 if (!result
->receiver
) free_result( result
);
404 /* store the message result in the appropriate structure */
405 static void store_message_result( struct message_result
*res
, unsigned int result
,
408 res
->result
= result
;
413 remove_timeout_user( res
->timeout
);
418 if (res
->callback_msg
)
420 /* queue the callback message in the sender queue */
421 res
->callback_msg
->lparam
= result
;
422 list_add_tail( &res
->sender
->msg_list
[SEND_MESSAGE
], &res
->callback_msg
->entry
);
423 set_queue_bits( res
->sender
, QS_SENDMESSAGE
);
424 res
->callback_msg
= NULL
;
425 remove_result_from_sender( res
);
429 /* wake sender queue if waiting on this result */
430 if (list_head(&res
->sender
->send_result
) == &res
->sender_entry
)
431 set_queue_bits( res
->sender
, QS_SMRESULT
);
437 /* free a message when deleting a queue or window */
438 static void free_message( struct message
*msg
)
440 struct message_result
*result
= msg
->result
;
445 result
->receiver
= NULL
;
446 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
448 else free_result( result
);
450 if (msg
->data
) free( msg
->data
);
454 /* remove (and free) a message from a message list */
455 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
456 enum message_kind kind
)
458 list_remove( &msg
->entry
);
462 if (list_empty( &queue
->msg_list
[kind
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
465 if (list_empty( &queue
->msg_list
[kind
] )) clear_queue_bits( queue
, QS_POSTMESSAGE
);
471 /* message timed out without getting a reply */
472 static void result_timeout( void *private )
474 struct message_result
*result
= private;
476 assert( !result
->replied
);
478 result
->timeout
= NULL
;
479 store_message_result( result
, 0, STATUS_TIMEOUT
);
482 /* allocate and fill a message result structure */
483 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
484 struct msg_queue
*recv_queue
,
485 struct message
*msg
, unsigned int timeout
,
486 void *callback
, unsigned int callback_data
)
488 struct message_result
*result
= mem_alloc( sizeof(*result
) );
491 result
->sender
= send_queue
;
492 result
->receiver
= recv_queue
;
495 result
->data_size
= 0;
496 result
->timeout
= NULL
;
498 if (msg
->type
== MSG_CALLBACK
)
500 struct message
*callback_msg
= mem_alloc( sizeof(*callback_msg
) );
506 callback_msg
->type
= MSG_CALLBACK_RESULT
;
507 callback_msg
->win
= msg
->win
;
508 callback_msg
->msg
= msg
->msg
;
509 callback_msg
->wparam
= (unsigned int)callback
;
510 callback_msg
->lparam
= 0;
511 callback_msg
->time
= get_tick_count();
514 callback_msg
->info
= callback_data
;
515 callback_msg
->hook
= 0;
516 callback_msg
->hook_proc
= NULL
;
517 callback_msg
->result
= NULL
;
518 callback_msg
->data
= NULL
;
519 callback_msg
->data_size
= 0;
521 result
->callback_msg
= callback_msg
;
522 list_add_head( &send_queue
->callback_result
, &result
->sender_entry
);
526 result
->callback_msg
= NULL
;
527 list_add_head( &send_queue
->send_result
, &result
->sender_entry
);
533 gettimeofday( &when
, 0 );
534 add_timeout( &when
, timeout
);
535 result
->timeout
= add_timeout_user( &when
, result_timeout
, result
);
541 /* receive a message, removing it from the sent queue */
542 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
543 struct get_message_reply
*reply
)
545 struct message_result
*result
= msg
->result
;
547 reply
->total
= msg
->data_size
;
548 if (msg
->data_size
> get_reply_max_size())
550 set_error( STATUS_BUFFER_OVERFLOW
);
553 reply
->type
= msg
->type
;
554 reply
->win
= msg
->win
;
555 reply
->msg
= msg
->msg
;
556 reply
->wparam
= msg
->wparam
;
557 reply
->lparam
= msg
->lparam
;
560 reply
->time
= msg
->time
;
561 reply
->info
= msg
->info
;
562 reply
->hook
= msg
->hook
;
563 reply
->hook_proc
= msg
->hook_proc
;
565 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
567 list_remove( &msg
->entry
);
568 /* put the result on the receiver result stack */
571 result
->recv_next
= queue
->recv_result
;
572 queue
->recv_result
= result
;
575 if (list_empty( &queue
->msg_list
[SEND_MESSAGE
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
578 /* set the result of the current received message */
579 static void reply_message( struct msg_queue
*queue
, unsigned int result
,
580 unsigned int error
, int remove
, const void *data
, size_t len
)
582 struct message_result
*res
= queue
->recv_result
;
586 queue
->recv_result
= res
->recv_next
;
587 res
->receiver
= NULL
;
588 if (!res
->sender
) /* no one waiting for it */
596 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
597 store_message_result( res
, result
, error
);
601 /* retrieve a posted message */
602 static int get_posted_message( struct msg_queue
*queue
, user_handle_t win
,
603 unsigned int first
, unsigned int last
, unsigned int flags
,
604 struct get_message_reply
*reply
)
608 /* check against the filters */
609 LIST_FOR_EACH_ENTRY( msg
, &queue
->msg_list
[POST_MESSAGE
], struct message
, entry
)
611 if (msg
->msg
== WM_QUIT
) goto found
; /* WM_QUIT is never filtered */
612 if (win
&& msg
->win
&& msg
->win
!= win
&& !is_child_window( win
, msg
->win
)) continue;
613 if (!check_msg_filter( msg
->msg
, first
, last
)) continue;
614 goto found
; /* found one */
618 /* return it to the app */
620 reply
->total
= msg
->data_size
;
621 if (msg
->data_size
> get_reply_max_size())
623 set_error( STATUS_BUFFER_OVERFLOW
);
626 reply
->type
= msg
->type
;
627 reply
->win
= msg
->win
;
628 reply
->msg
= msg
->msg
;
629 reply
->wparam
= msg
->wparam
;
630 reply
->lparam
= msg
->lparam
;
633 reply
->time
= msg
->time
;
634 reply
->info
= msg
->info
;
636 if (flags
& GET_MSG_REMOVE
)
640 set_reply_data_ptr( msg
->data
, msg
->data_size
);
644 remove_queue_message( queue
, msg
, POST_MESSAGE
);
646 else if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
651 /* empty a message list and free all the messages */
652 static void empty_msg_list( struct list
*list
)
656 while ((ptr
= list_head( list
)) != NULL
)
658 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
659 list_remove( &msg
->entry
);
664 /* cleanup all pending results when deleting a queue */
665 static void cleanup_results( struct msg_queue
*queue
)
669 while ((entry
= list_head( &queue
->send_result
)) != NULL
)
671 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
674 while ((entry
= list_head( &queue
->callback_result
)) != NULL
)
676 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
679 while (queue
->recv_result
)
680 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
683 /* check if the thread owning the queue is hung (not checking for messages) */
684 static int is_queue_hung( struct msg_queue
*queue
)
687 struct wait_queue_entry
*entry
;
689 gettimeofday( &now
, NULL
);
690 if (now
.tv_sec
- queue
->last_get_msg
.tv_sec
<= 5)
691 return 0; /* less than 5 seconds since last get message -> not hung */
693 LIST_FOR_EACH_ENTRY( entry
, &queue
->obj
.wait_queue
, struct wait_queue_entry
, entry
)
695 if (entry
->thread
->queue
== queue
)
696 return 0; /* thread is waiting on queue -> not hung */
701 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
703 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
704 struct process
*process
= entry
->thread
->process
;
706 /* a thread can only wait on its own queue */
707 if (entry
->thread
->queue
!= queue
)
709 set_error( STATUS_ACCESS_DENIED
);
712 /* if waiting on the main process queue, set the idle event */
713 if (process
->queue
== queue
)
715 if (process
->idle_event
) set_event( process
->idle_event
);
717 add_queue( obj
, entry
);
721 static void msg_queue_remove_queue(struct object
*obj
, struct wait_queue_entry
*entry
)
723 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
724 struct process
*process
= entry
->thread
->process
;
726 remove_queue( obj
, entry
);
728 assert( entry
->thread
->queue
== queue
);
730 /* if waiting on the main process queue, reset the idle event */
731 if (process
->queue
== queue
)
733 if (process
->idle_event
) reset_event( process
->idle_event
);
737 static void msg_queue_dump( struct object
*obj
, int verbose
)
739 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
740 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
741 queue
->wake_bits
, queue
->wake_mask
);
744 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
)
746 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
747 return is_signaled( queue
);
750 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
)
752 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
753 queue
->wake_mask
= 0;
754 queue
->changed_mask
= 0;
755 return 0; /* Not abandoned */
758 static void msg_queue_destroy( struct object
*obj
)
760 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
764 cleanup_results( queue
);
765 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
767 while ((ptr
= list_head( &queue
->pending_timers
)))
769 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
770 list_remove( &timer
->entry
);
773 while ((ptr
= list_head( &queue
->expired_timers
)))
775 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
776 list_remove( &timer
->entry
);
779 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
780 if (queue
->input
) release_object( queue
->input
);
781 if (queue
->hooks
) release_object( queue
->hooks
);
784 static void thread_input_dump( struct object
*obj
, int verbose
)
786 struct thread_input
*input
= (struct thread_input
*)obj
;
787 fprintf( stderr
, "Thread input focus=%p capture=%p active=%p\n",
788 input
->focus
, input
->capture
, input
->active
);
791 static void thread_input_destroy( struct object
*obj
)
793 struct thread_input
*input
= (struct thread_input
*)obj
;
795 if (foreground_input
== input
) foreground_input
= NULL
;
796 empty_msg_list( &input
->msg_list
);
799 /* fix the thread input data when a window is destroyed */
800 inline static void thread_input_cleanup_window( struct msg_queue
*queue
, user_handle_t window
)
802 struct thread_input
*input
= queue
->input
;
804 if (window
== input
->focus
) input
->focus
= 0;
805 if (window
== input
->capture
) input
->capture
= 0;
806 if (window
== input
->active
) input
->active
= 0;
807 if (window
== input
->menu_owner
) input
->menu_owner
= 0;
808 if (window
== input
->move_size
) input
->move_size
= 0;
809 if (window
== input
->caret
) set_caret_window( input
, 0 );
812 /* check if the specified window can be set in the input data of a given queue */
813 static int check_queue_input_window( struct msg_queue
*queue
, user_handle_t window
)
815 struct thread
*thread
;
818 if (!window
) return 1; /* we can always clear the data */
820 if ((thread
= get_window_thread( window
)))
822 ret
= (queue
->input
== thread
->queue
->input
);
823 if (!ret
) set_error( STATUS_ACCESS_DENIED
);
824 release_object( thread
);
826 else set_error( STATUS_INVALID_HANDLE
);
831 /* attach two thread input data structures */
832 int attach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
834 struct thread_input
*input
;
836 if (!thread_to
->queue
&& !(thread_to
->queue
= create_msg_queue( thread_to
, NULL
))) return 0;
837 input
= (struct thread_input
*)grab_object( thread_to
->queue
->input
);
839 if (thread_from
->queue
)
841 release_thread_input( thread_from
);
842 thread_from
->queue
->input
= input
;
846 if (!(thread_from
->queue
= create_msg_queue( thread_from
, input
))) return 0;
848 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
852 /* detach two thread input data structures */
853 static void detach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
855 struct thread_input
*input
;
857 if (!thread_from
->queue
|| !thread_to
->queue
||
858 thread_from
->queue
->input
!= thread_to
->queue
->input
)
860 set_error( STATUS_ACCESS_DENIED
);
863 if ((input
= create_thread_input()))
865 release_thread_input( thread_from
);
866 thread_from
->queue
->input
= input
;
871 /* set the next timer to expire */
872 static void set_next_timer( struct msg_queue
*queue
)
878 remove_timeout_user( queue
->timeout
);
879 queue
->timeout
= NULL
;
881 if ((ptr
= list_head( &queue
->pending_timers
)))
883 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
884 queue
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, queue
);
886 /* set/clear QS_TIMER bit */
887 if (list_empty( &queue
->expired_timers
))
888 clear_queue_bits( queue
, QS_TIMER
);
890 set_queue_bits( queue
, QS_TIMER
);
893 /* find a timer from its window and id */
894 static struct timer
*find_timer( struct msg_queue
*queue
, user_handle_t win
,
895 unsigned int msg
, unsigned int id
)
899 /* we need to search both lists */
901 LIST_FOR_EACH( ptr
, &queue
->pending_timers
)
903 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
904 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
906 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
908 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
909 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
914 /* callback for the next timer expiration */
915 static void timer_callback( void *private )
917 struct msg_queue
*queue
= private;
920 queue
->timeout
= NULL
;
921 /* move on to the next timer */
922 ptr
= list_head( &queue
->pending_timers
);
924 list_add_tail( &queue
->expired_timers
, ptr
);
925 set_next_timer( queue
);
928 /* link a timer at its rightful place in the queue list */
929 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
933 for (ptr
= queue
->pending_timers
.next
; ptr
!= &queue
->pending_timers
; ptr
= ptr
->next
)
935 struct timer
*t
= LIST_ENTRY( ptr
, struct timer
, entry
);
936 if (!time_before( &t
->when
, &timer
->when
)) break;
938 list_add_before( ptr
, &timer
->entry
);
941 /* remove a timer from the queue timer list and free it */
942 static void free_timer( struct msg_queue
*queue
, struct timer
*timer
)
944 list_remove( &timer
->entry
);
946 set_next_timer( queue
);
949 /* restart an expired timer */
950 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
954 list_remove( &timer
->entry
);
955 gettimeofday( &now
, 0 );
956 while (!time_before( &now
, &timer
->when
)) add_timeout( &timer
->when
, timer
->rate
);
957 link_timer( queue
, timer
);
958 set_next_timer( queue
);
961 /* find an expired timer matching the filtering parameters */
962 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
963 unsigned int get_first
, unsigned int get_last
,
968 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
970 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
971 if (win
&& timer
->win
!= win
) continue;
972 if (check_msg_filter( timer
->msg
, get_first
, get_last
))
974 if (remove
) restart_timer( queue
, timer
);
982 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
984 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
987 timer
->rate
= max( rate
, 1 );
988 gettimeofday( &timer
->when
, 0 );
989 add_timeout( &timer
->when
, rate
);
990 link_timer( queue
, timer
);
991 /* check if we replaced the next timer */
992 if (list_head( &queue
->pending_timers
) == &timer
->entry
) set_next_timer( queue
);
997 /* change the input key state for a given key */
998 static void set_input_key_state( struct thread_input
*input
, unsigned char key
, int down
)
1002 if (!(input
->keystate
[key
] & 0x80)) input
->keystate
[key
] ^= 0x01;
1003 input
->keystate
[key
] |= 0x80;
1005 else input
->keystate
[key
] &= ~0x80;
1008 /* update the input key state for a keyboard message */
1009 static void update_input_key_state( struct thread_input
*input
, const struct message
*msg
)
1012 int down
= 0, extended
;
1016 case WM_LBUTTONDOWN
:
1020 set_input_key_state( input
, VK_LBUTTON
, down
);
1022 case WM_MBUTTONDOWN
:
1026 set_input_key_state( input
, VK_MBUTTON
, down
);
1028 case WM_RBUTTONDOWN
:
1032 set_input_key_state( input
, VK_RBUTTON
, down
);
1034 case WM_XBUTTONDOWN
:
1038 if (msg
->wparam
== XBUTTON1
) set_input_key_state( input
, VK_XBUTTON1
, down
);
1039 else if (msg
->wparam
== XBUTTON2
) set_input_key_state( input
, VK_XBUTTON2
, down
);
1047 key
= (unsigned char)msg
->wparam
;
1048 extended
= ((msg
->lparam
>> 16) & KF_EXTENDED
) != 0;
1049 set_input_key_state( input
, key
, down
);
1053 set_input_key_state( input
, extended
? VK_RSHIFT
: VK_LSHIFT
, down
);
1056 set_input_key_state( input
, extended
? VK_RCONTROL
: VK_LCONTROL
, down
);
1059 set_input_key_state( input
, extended
? VK_RMENU
: VK_LMENU
, down
);
1066 /* release the hardware message currently being processed by the given thread */
1067 static void release_hardware_message( struct msg_queue
*queue
, unsigned int hw_id
,
1068 int remove
, user_handle_t new_win
)
1070 struct thread_input
*input
= queue
->input
;
1071 struct message
*msg
;
1073 LIST_FOR_EACH_ENTRY( msg
, &input
->msg_list
, struct message
, entry
)
1075 if (msg
->unique_id
== hw_id
) break;
1077 if (&msg
->entry
== &input
->msg_list
) return; /* not found */
1079 /* clear the queue bit for that message */
1080 if (remove
|| new_win
)
1082 struct message
*other
;
1085 clr_bit
= get_hardware_msg_bit( msg
);
1086 LIST_FOR_EACH_ENTRY( other
, &input
->msg_list
, struct message
, entry
)
1088 if (other
!= msg
&& get_hardware_msg_bit( other
) == clr_bit
)
1094 if (clr_bit
) clear_queue_bits( queue
, clr_bit
);
1097 if (new_win
) /* set the new window */
1099 struct thread
*owner
= get_window_thread( new_win
);
1103 set_queue_bits( owner
->queue
, get_hardware_msg_bit( msg
));
1104 release_object( owner
);
1106 if (!remove
) return; /* don't release the message */
1110 update_input_key_state( input
, msg
);
1111 list_remove( &msg
->entry
);
1112 free_message( msg
);
1116 /* find the window that should receive a given hardware message */
1117 static user_handle_t
find_hardware_message_window( struct thread_input
*input
, struct message
*msg
,
1118 unsigned int *msg_code
)
1120 user_handle_t win
= 0;
1122 *msg_code
= msg
->msg
;
1123 if (is_keyboard_msg( msg
))
1125 if (input
&& !(win
= input
->focus
))
1127 win
= input
->active
;
1128 if (*msg_code
< WM_SYSKEYDOWN
) *msg_code
+= WM_SYSKEYDOWN
- WM_KEYDOWN
;
1131 else /* mouse message */
1133 if (!input
|| !(win
= input
->capture
))
1135 if (!(win
= msg
->win
) || !is_window_visible( win
))
1136 win
= window_from_point( msg
->x
, msg
->y
);
1142 /* queue a hardware message into a given thread input */
1143 static void queue_hardware_message( struct msg_queue
*queue
, struct message
*msg
)
1146 struct thread
*thread
;
1147 struct thread_input
*input
;
1148 unsigned int msg_code
;
1150 win
= find_hardware_message_window( queue
? queue
->input
: foreground_input
, msg
, &msg_code
);
1151 if (!win
|| !(thread
= get_window_thread(win
)))
1156 input
= thread
->queue
->input
;
1158 if (msg
->msg
== WM_MOUSEMOVE
&& merge_message( input
, msg
)) free( msg
);
1161 msg
->unique_id
= 0; /* will be set once we return it to the app */
1162 list_add_tail( &input
->msg_list
, &msg
->entry
);
1163 set_queue_bits( thread
->queue
, get_hardware_msg_bit(msg
) );
1165 release_object( thread
);
1168 /* check message filter for a hardware message */
1169 static int check_hw_message_filter( user_handle_t win
, unsigned int msg_code
,
1170 user_handle_t filter_win
, unsigned int first
, unsigned int last
)
1172 if (msg_code
>= WM_KEYFIRST
&& msg_code
<= WM_KEYLAST
)
1174 /* we can only test the window for a keyboard message since the
1175 * dest window for a mouse message depends on hittest */
1176 if (filter_win
&& win
!= filter_win
&& !is_child_window( filter_win
, win
))
1178 /* the message code is final for a keyboard message, we can simply check it */
1179 return check_msg_filter( msg_code
, first
, last
);
1181 else /* mouse message */
1183 /* we need to check all possible values that the message can have in the end */
1185 if (check_msg_filter( msg_code
, first
, last
)) return 1;
1186 if (msg_code
== WM_MOUSEWHEEL
) return 0; /* no other possible value for this one */
1188 /* all other messages can become non-client messages */
1189 if (check_msg_filter( msg_code
+ (WM_NCMOUSEFIRST
- WM_MOUSEFIRST
), first
, last
)) return 1;
1191 /* clicks can become double-clicks or non-client double-clicks */
1192 if (msg_code
== WM_LBUTTONDOWN
|| msg_code
== WM_MBUTTONDOWN
||
1193 msg_code
== WM_RBUTTONDOWN
|| msg_code
== WM_XBUTTONDOWN
)
1195 if (check_msg_filter( msg_code
+ (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1196 if (check_msg_filter( msg_code
+ (WM_NCLBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1203 /* find a hardware message for the given queue */
1204 static int get_hardware_message( struct thread
*thread
, int hw_id
, user_handle_t filter_win
,
1205 unsigned int first
, unsigned int last
, struct get_message_reply
*reply
)
1207 struct thread_input
*input
= thread
->queue
->input
;
1208 struct thread
*win_thread
;
1211 int clear_bits
, got_one
= 0;
1212 unsigned int msg_code
;
1214 ptr
= list_head( &input
->msg_list
);
1219 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1220 if (msg
->unique_id
== hw_id
) break;
1221 ptr
= list_next( &input
->msg_list
, ptr
);
1223 if (!ptr
) ptr
= list_head( &input
->msg_list
);
1224 else ptr
= list_next( &input
->msg_list
, ptr
); /* start from the next one */
1227 if (ptr
== list_head( &input
->msg_list
))
1228 clear_bits
= QS_KEY
| QS_MOUSEMOVE
| QS_MOUSEBUTTON
;
1230 clear_bits
= 0; /* don't clear bits if we don't go through the whole list */
1234 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1235 win
= find_hardware_message_window( input
, msg
, &msg_code
);
1236 if (!win
|| !(win_thread
= get_window_thread( win
)))
1238 /* no window at all, remove it */
1239 ptr
= list_next( &input
->msg_list
, ptr
);
1240 update_input_key_state( input
, msg
);
1241 list_remove( &msg
->entry
);
1242 free_message( msg
);
1245 if (win_thread
!= thread
)
1247 /* wake the other thread */
1248 set_queue_bits( win_thread
->queue
, get_hardware_msg_bit(msg
) );
1249 release_object( win_thread
);
1251 ptr
= list_next( &input
->msg_list
, ptr
);
1254 release_object( win_thread
);
1256 /* if we already got a message for another thread, or if it doesn't
1257 * match the filter we skip it */
1258 if (got_one
|| !check_hw_message_filter( win
, msg_code
, filter_win
, first
, last
))
1260 clear_bits
&= ~get_hardware_msg_bit( msg
);
1261 ptr
= list_next( &input
->msg_list
, ptr
);
1264 /* now we can return it */
1265 if (!msg
->unique_id
) msg
->unique_id
= get_unique_id();
1266 reply
->type
= MSG_HARDWARE
;
1268 reply
->msg
= msg_code
;
1269 reply
->wparam
= msg
->wparam
;
1270 reply
->lparam
= msg
->lparam
;
1273 reply
->time
= msg
->time
;
1274 reply
->info
= msg
->info
;
1275 reply
->hw_id
= msg
->unique_id
;
1278 /* nothing found, clear the hardware queue bits */
1279 clear_queue_bits( thread
->queue
, clear_bits
);
1283 /* increment (or decrement if 'incr' is negative) the queue paint count */
1284 void inc_queue_paint_count( struct thread
*thread
, int incr
)
1286 struct msg_queue
*queue
= thread
->queue
;
1290 if ((queue
->paint_count
+= incr
) < 0) queue
->paint_count
= 0;
1292 if (queue
->paint_count
)
1293 set_queue_bits( queue
, QS_PAINT
);
1295 clear_queue_bits( queue
, QS_PAINT
);
1299 /* remove all messages and timers belonging to a certain window */
1300 void queue_cleanup_window( struct thread
*thread
, user_handle_t win
)
1302 struct msg_queue
*queue
= thread
->queue
;
1310 ptr
= list_head( &queue
->pending_timers
);
1313 struct list
*next
= list_next( &queue
->pending_timers
, ptr
);
1314 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1315 if (timer
->win
== win
) free_timer( queue
, timer
);
1318 ptr
= list_head( &queue
->expired_timers
);
1321 struct list
*next
= list_next( &queue
->expired_timers
, ptr
);
1322 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1323 if (timer
->win
== win
) free_timer( queue
, timer
);
1327 /* remove messages */
1328 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
1330 struct list
*ptr
, *next
;
1332 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->msg_list
[i
] )
1334 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1335 if (msg
->win
== win
) remove_queue_message( queue
, msg
, i
);
1339 thread_input_cleanup_window( queue
, win
);
1342 /* post a message to a window; used by socket handling */
1343 void post_message( user_handle_t win
, unsigned int message
,
1344 unsigned int wparam
, unsigned int lparam
)
1346 struct message
*msg
;
1347 struct thread
*thread
= get_window_thread( win
);
1349 if (!thread
) return;
1351 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1353 msg
->type
= MSG_POSTED
;
1354 msg
->win
= get_user_full_handle( win
);
1356 msg
->wparam
= wparam
;
1357 msg
->lparam
= lparam
;
1358 msg
->time
= get_tick_count();
1363 msg
->hook_proc
= NULL
;
1368 list_add_tail( &thread
->queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1369 set_queue_bits( thread
->queue
, QS_POSTMESSAGE
);
1371 release_object( thread
);
1374 /* post a win event */
1375 void post_win_event( struct thread
*thread
, unsigned int event
,
1376 user_handle_t win
, unsigned int object_id
,
1377 unsigned int child_id
, void *hook_proc
,
1378 const WCHAR
*module
, size_t module_size
,
1381 struct message
*msg
;
1383 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1385 msg
->type
= MSG_WINEVENT
;
1386 msg
->win
= get_user_full_handle( win
);
1388 msg
->wparam
= object_id
;
1389 msg
->lparam
= child_id
;
1390 msg
->time
= get_tick_count();
1393 msg
->info
= get_thread_id( current
);
1396 msg
->hook_proc
= hook_proc
;
1398 if ((msg
->data
= malloc( module_size
)))
1400 msg
->data_size
= module_size
;
1401 memcpy( msg
->data
, module
, module_size
);
1403 if (debug_level
> 1)
1404 fprintf( stderr
, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1405 get_thread_id(thread
), event
, win
, object_id
, child_id
);
1406 list_add_tail( &thread
->queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1407 set_queue_bits( thread
->queue
, QS_SENDMESSAGE
);
1414 /* get the message queue of the current thread */
1415 DECL_HANDLER(get_msg_queue
)
1417 struct msg_queue
*queue
= get_current_queue();
1420 if (queue
) reply
->handle
= alloc_handle( current
->process
, queue
, SYNCHRONIZE
, 0 );
1424 /* set the current message queue wakeup mask */
1425 DECL_HANDLER(set_queue_mask
)
1427 struct msg_queue
*queue
= get_current_queue();
1431 queue
->wake_mask
= req
->wake_mask
;
1432 queue
->changed_mask
= req
->changed_mask
;
1433 reply
->wake_bits
= queue
->wake_bits
;
1434 reply
->changed_bits
= queue
->changed_bits
;
1435 if (is_signaled( queue
))
1437 /* if skip wait is set, do what would have been done in the subsequent wait */
1438 if (req
->skip_wait
) msg_queue_satisfied( &queue
->obj
, current
);
1439 else wake_up( &queue
->obj
, 0 );
1445 /* get the current message queue status */
1446 DECL_HANDLER(get_queue_status
)
1448 struct msg_queue
*queue
= current
->queue
;
1451 reply
->wake_bits
= queue
->wake_bits
;
1452 reply
->changed_bits
= queue
->changed_bits
;
1453 if (req
->clear
) queue
->changed_bits
= 0;
1455 else reply
->wake_bits
= reply
->changed_bits
= 0;
1459 /* send a message to a thread queue */
1460 DECL_HANDLER(send_message
)
1462 struct message
*msg
;
1463 struct msg_queue
*send_queue
= get_current_queue();
1464 struct msg_queue
*recv_queue
= NULL
;
1465 struct thread
*thread
= NULL
;
1469 if (!(thread
= get_thread_from_id( req
->id
))) return;
1471 else if (req
->type
!= MSG_HARDWARE
)
1473 /* only hardware messages are allowed without destination thread */
1474 set_error( STATUS_INVALID_PARAMETER
);
1478 if (thread
&& !(recv_queue
= thread
->queue
))
1480 set_error( STATUS_INVALID_PARAMETER
);
1481 release_object( thread
);
1484 if (recv_queue
&& (req
->flags
& SEND_MSG_ABORT_IF_HUNG
) && is_queue_hung(recv_queue
))
1486 set_error( STATUS_TIMEOUT
);
1487 release_object( thread
);
1491 if ((msg
= mem_alloc( sizeof(*msg
) )))
1493 msg
->type
= req
->type
;
1494 msg
->win
= get_user_full_handle( req
->win
);
1495 msg
->msg
= req
->msg
;
1496 msg
->wparam
= req
->wparam
;
1497 msg
->lparam
= req
->lparam
;
1498 msg
->time
= req
->time
;
1501 msg
->info
= req
->info
;
1503 msg
->hook_proc
= NULL
;
1510 case MSG_OTHER_PROCESS
:
1511 msg
->data_size
= get_req_data_size();
1512 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1521 if (!(msg
->result
= alloc_message_result( send_queue
, recv_queue
, msg
,
1522 req
->timeout
, req
->callback
, req
->info
)))
1524 free_message( msg
);
1529 list_add_tail( &recv_queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1530 set_queue_bits( recv_queue
, QS_SENDMESSAGE
);
1533 /* needed for posted DDE messages */
1534 msg
->data_size
= get_req_data_size();
1535 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1540 list_add_tail( &recv_queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1541 set_queue_bits( recv_queue
, QS_POSTMESSAGE
);
1544 queue_hardware_message( recv_queue
, msg
);
1546 case MSG_CALLBACK_RESULT
: /* cannot send this one */
1548 set_error( STATUS_INVALID_PARAMETER
);
1553 if (thread
) release_object( thread
);
1557 /* get a message from the current queue */
1558 DECL_HANDLER(get_message
)
1560 struct timer
*timer
;
1562 struct msg_queue
*queue
= get_current_queue();
1563 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
1566 gettimeofday( &queue
->last_get_msg
, NULL
);
1568 /* first check for sent messages */
1569 if ((ptr
= list_head( &queue
->msg_list
[SEND_MESSAGE
] )))
1571 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1572 receive_message( queue
, msg
, reply
);
1575 if (req
->flags
& GET_MSG_SENT_ONLY
) goto done
; /* nothing else to check */
1577 /* clear changed bits so we can wait on them if we don't find a message */
1578 queue
->changed_bits
= 0;
1580 /* then check for posted messages */
1581 if (get_posted_message( queue
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
1584 /* then check for any raw hardware message */
1585 if (filter_contains_hw_range( req
->get_first
, req
->get_last
) &&
1586 get_hardware_message( current
, req
->hw_id
, get_win
, req
->get_first
, req
->get_last
, reply
))
1589 /* now check for WM_PAINT */
1590 if (queue
->paint_count
&&
1591 check_msg_filter( WM_PAINT
, req
->get_first
, req
->get_last
) &&
1592 (reply
->win
= find_window_to_repaint( get_win
, current
)))
1594 reply
->type
= MSG_POSTED
;
1595 reply
->msg
= WM_PAINT
;
1600 reply
->time
= get_tick_count();
1605 /* now check for timer */
1606 if ((timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
1607 req
->get_last
, (req
->flags
& GET_MSG_REMOVE
) )))
1609 reply
->type
= MSG_POSTED
;
1610 reply
->win
= timer
->win
;
1611 reply
->msg
= timer
->msg
;
1612 reply
->wparam
= timer
->id
;
1613 reply
->lparam
= timer
->lparam
;
1616 reply
->time
= get_tick_count();
1622 set_error( STATUS_PENDING
); /* FIXME */
1626 /* reply to a sent message */
1627 DECL_HANDLER(reply_message
)
1629 if (!current
->queue
) set_error( STATUS_ACCESS_DENIED
);
1630 else if (current
->queue
->recv_result
)
1631 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
1632 get_req_data(), get_req_data_size() );
1636 /* accept the current hardware message */
1637 DECL_HANDLER(accept_hardware_message
)
1640 release_hardware_message( current
->queue
, req
->hw_id
, req
->remove
, req
->new_win
);
1642 set_error( STATUS_ACCESS_DENIED
);
1646 /* retrieve the reply for the last message sent */
1647 DECL_HANDLER(get_message_reply
)
1649 struct message_result
*result
;
1651 struct msg_queue
*queue
= current
->queue
;
1655 set_error( STATUS_PENDING
);
1658 if (!(entry
= list_head( &queue
->send_result
))) return; /* no reply ready */
1660 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1661 if (result
->replied
|| req
->cancel
)
1663 if (result
->replied
)
1665 reply
->result
= result
->result
;
1666 set_error( result
->error
);
1669 size_t data_len
= min( result
->data_size
, get_reply_max_size() );
1670 set_reply_data_ptr( result
->data
, data_len
);
1671 result
->data
= NULL
;
1672 result
->data_size
= 0;
1675 remove_result_from_sender( result
);
1677 entry
= list_head( &queue
->send_result
);
1678 if (!entry
) clear_queue_bits( queue
, QS_SMRESULT
);
1681 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1682 if (!result
->replied
) clear_queue_bits( queue
, QS_SMRESULT
);
1686 else set_error( STATUS_ACCESS_DENIED
);
1690 /* set a window timer */
1691 DECL_HANDLER(set_win_timer
)
1693 struct timer
*timer
;
1694 struct msg_queue
*queue
;
1695 struct thread
*thread
= NULL
;
1696 user_handle_t win
= 0;
1697 unsigned int id
= req
->id
;
1701 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1703 set_error( STATUS_INVALID_HANDLE
);
1706 if (thread
->process
!= current
->process
)
1708 release_object( thread
);
1709 set_error( STATUS_ACCESS_DENIED
);
1712 queue
= thread
->queue
;
1713 /* remove it if it existed already */
1714 if ((timer
= find_timer( queue
, win
, req
->msg
, id
))) free_timer( queue
, timer
);
1718 queue
= get_current_queue();
1719 /* find a free id for it */
1722 id
= queue
->next_timer_id
;
1723 if (++queue
->next_timer_id
>= 0x10000) queue
->next_timer_id
= 1;
1725 while (find_timer( queue
, 0, req
->msg
, id
));
1728 if ((timer
= set_timer( queue
, req
->rate
)))
1731 timer
->msg
= req
->msg
;
1733 timer
->lparam
= req
->lparam
;
1736 if (thread
) release_object( thread
);
1739 /* kill a window timer */
1740 DECL_HANDLER(kill_win_timer
)
1742 struct timer
*timer
;
1743 struct thread
*thread
;
1744 user_handle_t win
= 0;
1748 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1750 set_error( STATUS_INVALID_HANDLE
);
1753 if (thread
->process
!= current
->process
)
1755 release_object( thread
);
1756 set_error( STATUS_ACCESS_DENIED
);
1760 else thread
= (struct thread
*)grab_object( current
);
1762 if (thread
->queue
&& (timer
= find_timer( thread
->queue
, win
, req
->msg
, req
->id
)))
1763 free_timer( thread
->queue
, timer
);
1765 set_error( STATUS_INVALID_PARAMETER
);
1767 release_object( thread
);
1771 /* attach (or detach) thread inputs */
1772 DECL_HANDLER(attach_thread_input
)
1774 struct thread
*thread_from
= get_thread_from_id( req
->tid_from
);
1775 struct thread
*thread_to
= get_thread_from_id( req
->tid_to
);
1777 if (!thread_from
|| !thread_to
)
1779 if (thread_from
) release_object( thread_from
);
1780 if (thread_to
) release_object( thread_to
);
1783 if (thread_from
!= thread_to
)
1785 if (req
->attach
) attach_thread_input( thread_from
, thread_to
);
1786 else detach_thread_input( thread_from
, thread_to
);
1788 else set_error( STATUS_ACCESS_DENIED
);
1789 release_object( thread_from
);
1790 release_object( thread_to
);
1794 /* get thread input data */
1795 DECL_HANDLER(get_thread_input
)
1797 struct thread
*thread
= NULL
;
1798 struct thread_input
*input
;
1802 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1803 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1805 else input
= foreground_input
; /* get the foreground thread info */
1809 reply
->focus
= input
->focus
;
1810 reply
->capture
= input
->capture
;
1811 reply
->active
= input
->active
;
1812 reply
->menu_owner
= input
->menu_owner
;
1813 reply
->move_size
= input
->move_size
;
1814 reply
->caret
= input
->caret
;
1815 reply
->rect
= input
->caret_rect
;
1822 reply
->menu_owner
= 0;
1823 reply
->move_size
= 0;
1825 reply
->rect
.left
= reply
->rect
.top
= reply
->rect
.right
= reply
->rect
.bottom
= 0;
1827 /* foreground window is active window of foreground thread */
1828 reply
->foreground
= foreground_input
? foreground_input
->active
: 0;
1829 if (thread
) release_object( thread
);
1833 /* retrieve queue keyboard state for a given thread */
1834 DECL_HANDLER(get_key_state
)
1836 struct thread
*thread
;
1837 struct thread_input
*input
;
1839 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1840 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1843 if (req
->key
>= 0) reply
->state
= input
->keystate
[req
->key
& 0xff];
1844 set_reply_data( input
->keystate
, min( get_reply_max_size(), sizeof(input
->keystate
) ));
1846 release_object( thread
);
1850 /* set queue keyboard state for a given thread */
1851 DECL_HANDLER(set_key_state
)
1853 struct thread
*thread
= NULL
;
1854 struct thread_input
*input
;
1856 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1857 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1860 size_t size
= min( sizeof(input
->keystate
), get_req_data_size() );
1861 if (size
) memcpy( input
->keystate
, get_req_data(), size
);
1863 release_object( thread
);
1867 /* set the system foreground window */
1868 DECL_HANDLER(set_foreground_window
)
1870 struct msg_queue
*queue
= get_current_queue();
1872 reply
->previous
= foreground_input
? foreground_input
->active
: 0;
1873 reply
->send_msg_old
= (reply
->previous
&& foreground_input
!= queue
->input
);
1874 reply
->send_msg_new
= FALSE
;
1878 struct thread
*thread
;
1880 if (is_top_level_window( req
->handle
) &&
1881 ((thread
= get_window_thread( req
->handle
))))
1883 foreground_input
= thread
->queue
->input
;
1884 reply
->send_msg_new
= (foreground_input
!= queue
->input
);
1885 release_object( thread
);
1887 else set_error( STATUS_INVALID_HANDLE
);
1889 else foreground_input
= NULL
;
1893 /* set the current thread focus window */
1894 DECL_HANDLER(set_focus_window
)
1896 struct msg_queue
*queue
= get_current_queue();
1898 reply
->previous
= 0;
1899 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1901 reply
->previous
= queue
->input
->focus
;
1902 queue
->input
->focus
= get_user_full_handle( req
->handle
);
1907 /* set the current thread active window */
1908 DECL_HANDLER(set_active_window
)
1910 struct msg_queue
*queue
= get_current_queue();
1912 reply
->previous
= 0;
1913 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1915 if (!req
->handle
|| make_window_active( req
->handle
))
1917 reply
->previous
= queue
->input
->active
;
1918 queue
->input
->active
= get_user_full_handle( req
->handle
);
1920 else set_error( STATUS_INVALID_HANDLE
);
1925 /* set the current thread capture window */
1926 DECL_HANDLER(set_capture_window
)
1928 struct msg_queue
*queue
= get_current_queue();
1930 reply
->previous
= reply
->full_handle
= 0;
1931 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1933 struct thread_input
*input
= queue
->input
;
1935 reply
->previous
= input
->capture
;
1936 input
->capture
= get_user_full_handle( req
->handle
);
1937 input
->menu_owner
= (req
->flags
& CAPTURE_MENU
) ? input
->capture
: 0;
1938 input
->move_size
= (req
->flags
& CAPTURE_MOVESIZE
) ? input
->capture
: 0;
1939 reply
->full_handle
= input
->capture
;
1944 /* Set the current thread caret window */
1945 DECL_HANDLER(set_caret_window
)
1947 struct msg_queue
*queue
= get_current_queue();
1949 reply
->previous
= 0;
1950 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1952 struct thread_input
*input
= queue
->input
;
1954 reply
->previous
= input
->caret
;
1955 reply
->old_rect
= input
->caret_rect
;
1956 reply
->old_hide
= input
->caret_hide
;
1957 reply
->old_state
= input
->caret_state
;
1959 set_caret_window( input
, get_user_full_handle(req
->handle
) );
1960 input
->caret_rect
.right
= input
->caret_rect
.left
+ req
->width
;
1961 input
->caret_rect
.bottom
= input
->caret_rect
.top
+ req
->height
;
1966 /* Set the current thread caret information */
1967 DECL_HANDLER(set_caret_info
)
1969 struct msg_queue
*queue
= get_current_queue();
1970 struct thread_input
*input
;
1973 input
= queue
->input
;
1974 reply
->full_handle
= input
->caret
;
1975 reply
->old_rect
= input
->caret_rect
;
1976 reply
->old_hide
= input
->caret_hide
;
1977 reply
->old_state
= input
->caret_state
;
1979 if (req
->handle
&& get_user_full_handle(req
->handle
) != input
->caret
)
1981 set_error( STATUS_ACCESS_DENIED
);
1984 if (req
->flags
& SET_CARET_POS
)
1986 input
->caret_rect
.right
+= req
->x
- input
->caret_rect
.left
;
1987 input
->caret_rect
.bottom
+= req
->y
- input
->caret_rect
.top
;
1988 input
->caret_rect
.left
= req
->x
;
1989 input
->caret_rect
.top
= req
->y
;
1991 if (req
->flags
& SET_CARET_HIDE
)
1993 input
->caret_hide
+= req
->hide
;
1994 if (input
->caret_hide
< 0) input
->caret_hide
= 0;
1996 if (req
->flags
& SET_CARET_STATE
)
1998 if (req
->state
== -1) input
->caret_state
= !input
->caret_state
;
1999 else input
->caret_state
= !!req
->state
;