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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
30 #define WIN32_NO_STATUS
44 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
45 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
47 enum message_kind
{ SEND_MESSAGE
, POST_MESSAGE
};
48 #define NB_MSG_KINDS (POST_MESSAGE+1)
53 struct list sender_entry
; /* entry in sender list */
54 struct message
*msg
; /* message the result is for */
55 struct message_result
*recv_next
; /* next in receiver list */
56 struct msg_queue
*sender
; /* sender queue */
57 struct msg_queue
*receiver
; /* receiver queue */
58 int replied
; /* has it been replied to? */
59 unsigned int result
; /* reply result */
60 unsigned int error
; /* error code to pass back to sender */
61 struct message
*callback_msg
; /* message to queue for callback */
62 void *data
; /* message reply data */
63 unsigned int data_size
; /* size of message reply data */
64 struct timeout_user
*timeout
; /* result timeout */
69 struct list entry
; /* entry in message list */
70 enum message_type type
; /* message type */
71 user_handle_t win
; /* window handle */
72 unsigned int msg
; /* message code */
73 unsigned long wparam
; /* parameters */
74 unsigned long lparam
; /* parameters */
75 unsigned long info
; /* extra info */
76 int x
; /* x position */
77 int y
; /* y position */
78 unsigned int time
; /* message time */
79 void *data
; /* message data for sent messages */
80 unsigned int data_size
; /* size of message data */
81 unsigned int unique_id
; /* unique id for nested hw message waits */
82 struct message_result
*result
; /* result in sender queue */
87 struct list entry
; /* entry in timer list */
88 struct timeval when
; /* next expiration */
89 unsigned int rate
; /* timer rate in ms */
90 user_handle_t win
; /* window handle */
91 unsigned int msg
; /* message to post */
92 unsigned long id
; /* timer id */
93 unsigned long lparam
; /* lparam for message */
98 struct object obj
; /* object header */
99 struct desktop
*desktop
; /* desktop that this thread input belongs to */
100 user_handle_t focus
; /* focus window */
101 user_handle_t capture
; /* capture window */
102 user_handle_t active
; /* active window */
103 user_handle_t menu_owner
; /* current menu owner window */
104 user_handle_t move_size
; /* current moving/resizing window */
105 user_handle_t caret
; /* caret window */
106 rectangle_t caret_rect
; /* caret rectangle */
107 int caret_hide
; /* caret hide count */
108 int caret_state
; /* caret on/off state */
109 struct list msg_list
; /* list of hardware messages */
110 unsigned char keystate
[256]; /* state of each key */
115 struct object obj
; /* object header */
116 unsigned int wake_bits
; /* wakeup bits */
117 unsigned int wake_mask
; /* wakeup mask */
118 unsigned int changed_bits
; /* changed wakeup bits */
119 unsigned int changed_mask
; /* changed wakeup mask */
120 int paint_count
; /* pending paint messages count */
121 int quit_message
; /* is there a pending quit message? */
122 int exit_code
; /* exit code of pending quit message */
123 struct list msg_list
[NB_MSG_KINDS
]; /* lists of messages */
124 struct list send_result
; /* stack of sent messages waiting for result */
125 struct list callback_result
; /* list of callback messages waiting for result */
126 struct message_result
*recv_result
; /* stack of received messages waiting for result */
127 struct list pending_timers
; /* list of pending timers */
128 struct list expired_timers
; /* list of expired timers */
129 unsigned long next_timer_id
; /* id for the next timer with a 0 window */
130 struct timeout_user
*timeout
; /* timeout for next timer to expire */
131 struct thread_input
*input
; /* thread input descriptor */
132 struct hook_table
*hooks
; /* hook table */
133 struct timeval last_get_msg
; /* time of last get message call */
136 static void msg_queue_dump( struct object
*obj
, int verbose
);
137 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
138 static void msg_queue_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
139 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
);
140 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
);
141 static void msg_queue_destroy( struct object
*obj
);
142 static void thread_input_dump( struct object
*obj
, int verbose
);
143 static void thread_input_destroy( struct object
*obj
);
144 static void timer_callback( void *private );
146 static const struct object_ops msg_queue_ops
=
148 sizeof(struct msg_queue
), /* size */
149 msg_queue_dump
, /* dump */
150 msg_queue_add_queue
, /* add_queue */
151 msg_queue_remove_queue
, /* remove_queue */
152 msg_queue_signaled
, /* signaled */
153 msg_queue_satisfied
, /* satisfied */
154 no_signal
, /* signal */
155 no_get_fd
, /* get_fd */
156 no_map_access
, /* map_access */
157 no_lookup_name
, /* lookup_name */
158 no_open_file
, /* open_file */
159 no_close_handle
, /* close_handle */
160 msg_queue_destroy
/* destroy */
164 static const struct object_ops thread_input_ops
=
166 sizeof(struct thread_input
), /* size */
167 thread_input_dump
, /* dump */
168 no_add_queue
, /* add_queue */
169 NULL
, /* remove_queue */
171 NULL
, /* satisfied */
172 no_signal
, /* signal */
173 no_get_fd
, /* get_fd */
174 no_map_access
, /* map_access */
175 no_lookup_name
, /* lookup_name */
176 no_open_file
, /* open_file */
177 no_close_handle
, /* close_handle */
178 thread_input_destroy
/* destroy */
181 /* pointer to input structure of foreground thread */
182 static struct thread_input
*foreground_input
;
183 static unsigned int last_input_time
;
185 static void free_message( struct message
*msg
);
187 /* set the caret window in a given thread input */
188 static void set_caret_window( struct thread_input
*input
, user_handle_t win
)
190 if (!win
|| win
!= input
->caret
)
192 input
->caret_rect
.left
= 0;
193 input
->caret_rect
.top
= 0;
194 input
->caret_rect
.right
= 0;
195 input
->caret_rect
.bottom
= 0;
198 input
->caret_hide
= 1;
199 input
->caret_state
= 0;
202 /* create a thread input object */
203 static struct thread_input
*create_thread_input( struct thread
*thread
)
205 struct thread_input
*input
;
207 if ((input
= alloc_object( &thread_input_ops
)))
212 input
->menu_owner
= 0;
213 input
->move_size
= 0;
214 list_init( &input
->msg_list
);
215 set_caret_window( input
, 0 );
216 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
218 if (!(input
->desktop
= get_thread_desktop( thread
, 0 /* FIXME: access rights */ )))
220 release_object( input
);
227 /* release the thread input data of a given thread */
228 static inline void release_thread_input( struct thread
*thread
)
230 struct thread_input
*input
= thread
->queue
->input
;
233 release_object( input
);
234 thread
->queue
->input
= NULL
;
237 /* create a message queue object */
238 static struct msg_queue
*create_msg_queue( struct thread
*thread
, struct thread_input
*input
)
240 struct msg_queue
*queue
;
243 if (!input
&& !(input
= create_thread_input( thread
))) return NULL
;
244 if ((queue
= alloc_object( &msg_queue_ops
)))
246 queue
->wake_bits
= 0;
247 queue
->wake_mask
= 0;
248 queue
->changed_bits
= 0;
249 queue
->changed_mask
= 0;
250 queue
->paint_count
= 0;
251 queue
->quit_message
= 0;
252 queue
->recv_result
= NULL
;
253 queue
->next_timer_id
= 1;
254 queue
->timeout
= NULL
;
255 queue
->input
= (struct thread_input
*)grab_object( input
);
257 queue
->last_get_msg
= current_time
;
258 list_init( &queue
->send_result
);
259 list_init( &queue
->callback_result
);
260 list_init( &queue
->pending_timers
);
261 list_init( &queue
->expired_timers
);
262 for (i
= 0; i
< NB_MSG_KINDS
; i
++) list_init( &queue
->msg_list
[i
] );
264 thread
->queue
= queue
;
265 if (!thread
->process
->queue
)
266 thread
->process
->queue
= (struct msg_queue
*)grab_object( queue
);
268 release_object( input
);
272 /* free the message queue of a thread at thread exit */
273 void free_msg_queue( struct thread
*thread
)
275 struct process
*process
= thread
->process
;
277 remove_thread_hooks( thread
);
278 if (!thread
->queue
) return;
279 if (process
->queue
== thread
->queue
) /* is it the process main queue? */
281 release_object( process
->queue
);
282 process
->queue
= NULL
;
283 if (process
->idle_event
)
285 set_event( process
->idle_event
);
286 release_object( process
->idle_event
);
287 process
->idle_event
= NULL
;
290 release_object( thread
->queue
);
291 thread
->queue
= NULL
;
294 /* get the hook table for a given thread */
295 struct hook_table
*get_queue_hooks( struct thread
*thread
)
297 if (!thread
->queue
) return NULL
;
298 return thread
->queue
->hooks
;
301 /* set the hook table for a given thread, allocating the queue if needed */
302 void set_queue_hooks( struct thread
*thread
, struct hook_table
*hooks
)
304 struct msg_queue
*queue
= thread
->queue
;
305 if (!queue
&& !(queue
= create_msg_queue( thread
, NULL
))) return;
306 if (queue
->hooks
) release_object( queue
->hooks
);
307 queue
->hooks
= hooks
;
310 /* check the queue status */
311 static inline int is_signaled( struct msg_queue
*queue
)
313 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
316 /* set some queue bits */
317 static inline void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
319 queue
->wake_bits
|= bits
;
320 queue
->changed_bits
|= bits
;
321 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
324 /* clear some queue bits */
325 static inline void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
327 queue
->wake_bits
&= ~bits
;
328 queue
->changed_bits
&= ~bits
;
331 /* check whether msg is a keyboard message */
332 static inline int is_keyboard_msg( struct message
*msg
)
334 return (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
);
337 /* check if message is matched by the filter */
338 static inline int check_msg_filter( unsigned int msg
, unsigned int first
, unsigned int last
)
340 return (msg
>= first
&& msg
<= last
);
343 /* check whether a message filter contains at least one potential hardware message */
344 static inline int filter_contains_hw_range( unsigned int first
, unsigned int last
)
346 /* hardware message ranges are (in numerical order):
347 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
348 * WM_KEYFIRST .. WM_KEYLAST
349 * WM_MOUSEFIRST .. WM_MOUSELAST
351 if (last
< WM_NCMOUSEFIRST
) return 0;
352 if (first
> WM_NCMOUSELAST
&& last
< WM_KEYFIRST
) return 0;
353 if (first
> WM_KEYLAST
&& last
< WM_MOUSEFIRST
) return 0;
354 if (first
> WM_MOUSELAST
) return 0;
358 /* get the QS_* bit corresponding to a given hardware message */
359 static inline int get_hardware_msg_bit( struct message
*msg
)
361 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
362 if (is_keyboard_msg( msg
)) return QS_KEY
;
363 return QS_MOUSEBUTTON
;
366 /* get the current thread queue, creating it if needed */
367 static inline struct msg_queue
*get_current_queue(void)
369 struct msg_queue
*queue
= current
->queue
;
370 if (!queue
) queue
= create_msg_queue( current
, NULL
);
374 /* get a (pseudo-)unique id to tag hardware messages */
375 static inline unsigned int get_unique_id(void)
377 static unsigned int id
;
378 if (!++id
) id
= 1; /* avoid an id of 0 */
382 /* try to merge a message with the last in the list; return 1 if successful */
383 static int merge_message( struct thread_input
*input
, const struct message
*msg
)
385 struct message
*prev
;
386 struct list
*ptr
= list_tail( &input
->msg_list
);
389 prev
= LIST_ENTRY( ptr
, struct message
, entry
);
390 if (prev
->unique_id
) return 0;
391 if (prev
->result
) return 0;
392 if (prev
->win
!= msg
->win
) return 0;
393 if (prev
->msg
!= msg
->msg
) return 0;
394 if (prev
->type
!= msg
->type
) return 0;
395 /* now we can merge it */
396 prev
->wparam
= msg
->wparam
;
397 prev
->lparam
= msg
->lparam
;
400 prev
->time
= msg
->time
;
401 prev
->info
= msg
->info
;
405 /* free a result structure */
406 static void free_result( struct message_result
*result
)
408 if (result
->timeout
) remove_timeout_user( result
->timeout
);
409 free( result
->data
);
410 if (result
->callback_msg
) free_message( result
->callback_msg
);
414 /* remove the result from the sender list it is on */
415 static inline void remove_result_from_sender( struct message_result
*result
)
417 assert( result
->sender
);
419 list_remove( &result
->sender_entry
);
420 result
->sender
= NULL
;
421 if (!result
->receiver
) free_result( result
);
424 /* store the message result in the appropriate structure */
425 static void store_message_result( struct message_result
*res
, unsigned int result
,
428 res
->result
= result
;
433 remove_timeout_user( res
->timeout
);
438 if (res
->callback_msg
)
440 /* queue the callback message in the sender queue */
441 struct callback_msg_data
*data
= res
->callback_msg
->data
;
442 data
->result
= result
;
443 list_add_tail( &res
->sender
->msg_list
[SEND_MESSAGE
], &res
->callback_msg
->entry
);
444 set_queue_bits( res
->sender
, QS_SENDMESSAGE
);
445 res
->callback_msg
= NULL
;
446 remove_result_from_sender( res
);
450 /* wake sender queue if waiting on this result */
451 if (list_head(&res
->sender
->send_result
) == &res
->sender_entry
)
452 set_queue_bits( res
->sender
, QS_SMRESULT
);
458 /* free a message when deleting a queue or window */
459 static void free_message( struct message
*msg
)
461 struct message_result
*result
= msg
->result
;
467 result
->receiver
= NULL
;
468 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
470 else free_result( result
);
476 /* remove (and free) a message from a message list */
477 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
478 enum message_kind kind
)
480 list_remove( &msg
->entry
);
484 if (list_empty( &queue
->msg_list
[kind
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
487 if (list_empty( &queue
->msg_list
[kind
] ) && !queue
->quit_message
)
488 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
494 /* message timed out without getting a reply */
495 static void result_timeout( void *private )
497 struct message_result
*result
= private;
499 assert( !result
->replied
);
501 result
->timeout
= NULL
;
503 if (result
->msg
) /* not received yet */
505 struct message
*msg
= result
->msg
;
509 remove_queue_message( result
->receiver
, msg
, SEND_MESSAGE
);
510 result
->receiver
= NULL
;
513 free_result( result
);
518 store_message_result( result
, 0, STATUS_TIMEOUT
);
521 /* allocate and fill a message result structure */
522 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
523 struct msg_queue
*recv_queue
,
524 struct message
*msg
, int timeout
)
526 struct message_result
*result
= mem_alloc( sizeof(*result
) );
530 result
->sender
= send_queue
;
531 result
->receiver
= recv_queue
;
534 result
->data_size
= 0;
535 result
->timeout
= NULL
;
537 if (msg
->type
== MSG_CALLBACK
)
539 struct message
*callback_msg
= mem_alloc( sizeof(*callback_msg
) );
546 callback_msg
->type
= MSG_CALLBACK_RESULT
;
547 callback_msg
->win
= msg
->win
;
548 callback_msg
->msg
= msg
->msg
;
549 callback_msg
->wparam
= 0;
550 callback_msg
->lparam
= 0;
551 callback_msg
->time
= get_tick_count();
554 callback_msg
->info
= 0;
555 callback_msg
->result
= NULL
;
556 /* steal the data from the original message */
557 callback_msg
->data
= msg
->data
;
558 callback_msg
->data_size
= msg
->data_size
;
562 result
->callback_msg
= callback_msg
;
563 list_add_head( &send_queue
->callback_result
, &result
->sender_entry
);
567 result
->callback_msg
= NULL
;
568 list_add_head( &send_queue
->send_result
, &result
->sender_entry
);
573 struct timeval when
= current_time
;
574 add_timeout( &when
, timeout
);
575 result
->timeout
= add_timeout_user( &when
, result_timeout
, result
);
581 /* receive a message, removing it from the sent queue */
582 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
583 struct get_message_reply
*reply
)
585 struct message_result
*result
= msg
->result
;
587 reply
->total
= msg
->data_size
;
588 if (msg
->data_size
> get_reply_max_size())
590 set_error( STATUS_BUFFER_OVERFLOW
);
593 reply
->type
= msg
->type
;
594 reply
->win
= msg
->win
;
595 reply
->msg
= msg
->msg
;
596 reply
->wparam
= msg
->wparam
;
597 reply
->lparam
= msg
->lparam
;
600 reply
->time
= msg
->time
;
601 reply
->info
= msg
->info
;
603 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
605 list_remove( &msg
->entry
);
606 /* put the result on the receiver result stack */
610 result
->recv_next
= queue
->recv_result
;
611 queue
->recv_result
= result
;
614 if (list_empty( &queue
->msg_list
[SEND_MESSAGE
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
617 /* set the result of the current received message */
618 static void reply_message( struct msg_queue
*queue
, unsigned int result
,
619 unsigned int error
, int remove
, const void *data
, data_size_t len
)
621 struct message_result
*res
= queue
->recv_result
;
625 queue
->recv_result
= res
->recv_next
;
626 res
->receiver
= NULL
;
627 if (!res
->sender
) /* no one waiting for it */
635 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
636 store_message_result( res
, result
, error
);
640 /* retrieve a posted message */
641 static int get_posted_message( struct msg_queue
*queue
, user_handle_t win
,
642 unsigned int first
, unsigned int last
, unsigned int flags
,
643 struct get_message_reply
*reply
)
647 /* check against the filters */
648 LIST_FOR_EACH_ENTRY( msg
, &queue
->msg_list
[POST_MESSAGE
], struct message
, entry
)
650 if (win
&& msg
->win
&& msg
->win
!= win
&& !is_child_window( win
, msg
->win
)) continue;
651 if (!check_msg_filter( msg
->msg
, first
, last
)) continue;
652 goto found
; /* found one */
656 /* return it to the app */
658 reply
->total
= msg
->data_size
;
659 if (msg
->data_size
> get_reply_max_size())
661 set_error( STATUS_BUFFER_OVERFLOW
);
664 reply
->type
= msg
->type
;
665 reply
->win
= msg
->win
;
666 reply
->msg
= msg
->msg
;
667 reply
->wparam
= msg
->wparam
;
668 reply
->lparam
= msg
->lparam
;
671 reply
->time
= msg
->time
;
672 reply
->info
= msg
->info
;
674 if (flags
& PM_REMOVE
)
678 set_reply_data_ptr( msg
->data
, msg
->data_size
);
682 remove_queue_message( queue
, msg
, POST_MESSAGE
);
684 else if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
689 static int get_quit_message( struct msg_queue
*queue
, unsigned int flags
,
690 struct get_message_reply
*reply
)
692 if (queue
->quit_message
)
695 reply
->type
= MSG_POSTED
;
697 reply
->msg
= WM_QUIT
;
698 reply
->wparam
= queue
->exit_code
;
702 reply
->time
= get_tick_count();
705 if (flags
& PM_REMOVE
)
707 queue
->quit_message
= 0;
708 if (list_empty( &queue
->msg_list
[POST_MESSAGE
] ))
709 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
717 /* empty a message list and free all the messages */
718 static void empty_msg_list( struct list
*list
)
722 while ((ptr
= list_head( list
)) != NULL
)
724 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
725 list_remove( &msg
->entry
);
730 /* cleanup all pending results when deleting a queue */
731 static void cleanup_results( struct msg_queue
*queue
)
735 while ((entry
= list_head( &queue
->send_result
)) != NULL
)
737 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
740 while ((entry
= list_head( &queue
->callback_result
)) != NULL
)
742 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
745 while (queue
->recv_result
)
746 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
749 /* check if the thread owning the queue is hung (not checking for messages) */
750 static int is_queue_hung( struct msg_queue
*queue
)
752 struct wait_queue_entry
*entry
;
754 if (current_time
.tv_sec
- queue
->last_get_msg
.tv_sec
<= 5)
755 return 0; /* less than 5 seconds since last get message -> not hung */
757 LIST_FOR_EACH_ENTRY( entry
, &queue
->obj
.wait_queue
, struct wait_queue_entry
, entry
)
759 if (entry
->thread
->queue
== queue
)
760 return 0; /* thread is waiting on queue -> not hung */
765 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
767 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
768 struct process
*process
= entry
->thread
->process
;
770 /* a thread can only wait on its own queue */
771 if (entry
->thread
->queue
!= queue
)
773 set_error( STATUS_ACCESS_DENIED
);
776 /* if waiting on the main process queue, set the idle event */
777 if (process
->queue
== queue
)
779 if (process
->idle_event
) set_event( process
->idle_event
);
781 add_queue( obj
, entry
);
785 static void msg_queue_remove_queue(struct object
*obj
, struct wait_queue_entry
*entry
)
787 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
788 struct process
*process
= entry
->thread
->process
;
790 remove_queue( obj
, entry
);
792 assert( entry
->thread
->queue
== queue
);
794 /* if waiting on the main process queue, reset the idle event */
795 if (process
->queue
== queue
)
797 if (process
->idle_event
) reset_event( process
->idle_event
);
801 static void msg_queue_dump( struct object
*obj
, int verbose
)
803 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
804 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
805 queue
->wake_bits
, queue
->wake_mask
);
808 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
)
810 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
811 return is_signaled( queue
);
814 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
)
816 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
817 queue
->wake_mask
= 0;
818 queue
->changed_mask
= 0;
819 return 0; /* Not abandoned */
822 static void msg_queue_destroy( struct object
*obj
)
824 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
828 cleanup_results( queue
);
829 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
831 while ((ptr
= list_head( &queue
->pending_timers
)))
833 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
834 list_remove( &timer
->entry
);
837 while ((ptr
= list_head( &queue
->expired_timers
)))
839 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
840 list_remove( &timer
->entry
);
843 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
844 if (queue
->input
) release_object( queue
->input
);
845 if (queue
->hooks
) release_object( queue
->hooks
);
848 static void thread_input_dump( struct object
*obj
, int verbose
)
850 struct thread_input
*input
= (struct thread_input
*)obj
;
851 fprintf( stderr
, "Thread input focus=%p capture=%p active=%p\n",
852 input
->focus
, input
->capture
, input
->active
);
855 static void thread_input_destroy( struct object
*obj
)
857 struct thread_input
*input
= (struct thread_input
*)obj
;
859 if (foreground_input
== input
) foreground_input
= NULL
;
860 empty_msg_list( &input
->msg_list
);
861 if (input
->desktop
) release_object( input
->desktop
);
864 /* fix the thread input data when a window is destroyed */
865 static inline void thread_input_cleanup_window( struct msg_queue
*queue
, user_handle_t window
)
867 struct thread_input
*input
= queue
->input
;
869 if (window
== input
->focus
) input
->focus
= 0;
870 if (window
== input
->capture
) input
->capture
= 0;
871 if (window
== input
->active
) input
->active
= 0;
872 if (window
== input
->menu_owner
) input
->menu_owner
= 0;
873 if (window
== input
->move_size
) input
->move_size
= 0;
874 if (window
== input
->caret
) set_caret_window( input
, 0 );
877 /* check if the specified window can be set in the input data of a given queue */
878 static int check_queue_input_window( struct msg_queue
*queue
, user_handle_t window
)
880 struct thread
*thread
;
883 if (!window
) return 1; /* we can always clear the data */
885 if ((thread
= get_window_thread( window
)))
887 ret
= (queue
->input
== thread
->queue
->input
);
888 if (!ret
) set_error( STATUS_ACCESS_DENIED
);
889 release_object( thread
);
891 else set_error( STATUS_INVALID_HANDLE
);
896 /* make sure the specified thread has a queue */
897 int init_thread_queue( struct thread
*thread
)
899 if (thread
->queue
) return 1;
900 return (create_msg_queue( thread
, NULL
) != NULL
);
903 /* attach two thread input data structures */
904 int attach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
906 struct desktop
*desktop
;
907 struct thread_input
*input
;
909 if (!thread_to
->queue
&& !(thread_to
->queue
= create_msg_queue( thread_to
, NULL
))) return 0;
910 if (!(desktop
= get_thread_desktop( thread_from
, 0 ))) return 0;
911 input
= (struct thread_input
*)grab_object( thread_to
->queue
->input
);
912 if (input
->desktop
!= desktop
)
914 set_error( STATUS_ACCESS_DENIED
);
915 release_object( input
);
916 release_object( desktop
);
919 release_object( desktop
);
921 if (thread_from
->queue
)
923 release_thread_input( thread_from
);
924 thread_from
->queue
->input
= input
;
928 if (!(thread_from
->queue
= create_msg_queue( thread_from
, input
))) return 0;
930 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
934 /* detach two thread input data structures */
935 void detach_thread_input( struct thread
*thread_from
)
937 struct thread_input
*input
;
939 if ((input
= create_thread_input( thread_from
)))
941 release_thread_input( thread_from
);
942 thread_from
->queue
->input
= input
;
947 /* set the next timer to expire */
948 static void set_next_timer( struct msg_queue
*queue
)
954 remove_timeout_user( queue
->timeout
);
955 queue
->timeout
= NULL
;
957 if ((ptr
= list_head( &queue
->pending_timers
)))
959 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
960 queue
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, queue
);
962 /* set/clear QS_TIMER bit */
963 if (list_empty( &queue
->expired_timers
))
964 clear_queue_bits( queue
, QS_TIMER
);
966 set_queue_bits( queue
, QS_TIMER
);
969 /* find a timer from its window and id */
970 static struct timer
*find_timer( struct msg_queue
*queue
, user_handle_t win
,
971 unsigned int msg
, unsigned long id
)
975 /* we need to search both lists */
977 LIST_FOR_EACH( ptr
, &queue
->pending_timers
)
979 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
980 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
982 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
984 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
985 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
990 /* callback for the next timer expiration */
991 static void timer_callback( void *private )
993 struct msg_queue
*queue
= private;
996 queue
->timeout
= NULL
;
997 /* move on to the next timer */
998 ptr
= list_head( &queue
->pending_timers
);
1000 list_add_tail( &queue
->expired_timers
, ptr
);
1001 set_next_timer( queue
);
1004 /* link a timer at its rightful place in the queue list */
1005 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
1009 for (ptr
= queue
->pending_timers
.next
; ptr
!= &queue
->pending_timers
; ptr
= ptr
->next
)
1011 struct timer
*t
= LIST_ENTRY( ptr
, struct timer
, entry
);
1012 if (!time_before( &t
->when
, &timer
->when
)) break;
1014 list_add_before( ptr
, &timer
->entry
);
1017 /* remove a timer from the queue timer list and free it */
1018 static void free_timer( struct msg_queue
*queue
, struct timer
*timer
)
1020 list_remove( &timer
->entry
);
1022 set_next_timer( queue
);
1025 /* restart an expired timer */
1026 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
1028 list_remove( &timer
->entry
);
1029 while (!time_before( ¤t_time
, &timer
->when
)) add_timeout( &timer
->when
, timer
->rate
);
1030 link_timer( queue
, timer
);
1031 set_next_timer( queue
);
1034 /* find an expired timer matching the filtering parameters */
1035 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
1036 unsigned int get_first
, unsigned int get_last
,
1041 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
1043 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1044 if (win
&& timer
->win
!= win
) continue;
1045 if (check_msg_filter( timer
->msg
, get_first
, get_last
))
1047 if (remove
) restart_timer( queue
, timer
);
1055 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
1057 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
1060 timer
->rate
= max( rate
, 1 );
1061 timer
->when
= current_time
;
1062 add_timeout( &timer
->when
, rate
);
1063 link_timer( queue
, timer
);
1064 /* check if we replaced the next timer */
1065 if (list_head( &queue
->pending_timers
) == &timer
->entry
) set_next_timer( queue
);
1070 /* change the input key state for a given key */
1071 static void set_input_key_state( struct thread_input
*input
, unsigned char key
, int down
)
1075 if (!(input
->keystate
[key
] & 0x80)) input
->keystate
[key
] ^= 0x01;
1076 input
->keystate
[key
] |= 0x80;
1078 else input
->keystate
[key
] &= ~0x80;
1081 /* update the input key state for a keyboard message */
1082 static void update_input_key_state( struct thread_input
*input
, const struct message
*msg
)
1085 int down
= 0, extended
;
1089 case WM_LBUTTONDOWN
:
1093 set_input_key_state( input
, VK_LBUTTON
, down
);
1095 case WM_MBUTTONDOWN
:
1099 set_input_key_state( input
, VK_MBUTTON
, down
);
1101 case WM_RBUTTONDOWN
:
1105 set_input_key_state( input
, VK_RBUTTON
, down
);
1107 case WM_XBUTTONDOWN
:
1111 if (msg
->wparam
== XBUTTON1
) set_input_key_state( input
, VK_XBUTTON1
, down
);
1112 else if (msg
->wparam
== XBUTTON2
) set_input_key_state( input
, VK_XBUTTON2
, down
);
1120 key
= (unsigned char)msg
->wparam
;
1121 extended
= ((msg
->lparam
>> 16) & KF_EXTENDED
) != 0;
1122 set_input_key_state( input
, key
, down
);
1126 set_input_key_state( input
, extended
? VK_RSHIFT
: VK_LSHIFT
, down
);
1129 set_input_key_state( input
, extended
? VK_RCONTROL
: VK_LCONTROL
, down
);
1132 set_input_key_state( input
, extended
? VK_RMENU
: VK_LMENU
, down
);
1136 set_input_key_state( input
, VK_CONTROL
, down
);
1140 set_input_key_state( input
, VK_MENU
, down
);
1144 set_input_key_state( input
, VK_SHIFT
, down
);
1151 /* release the hardware message currently being processed by the given thread */
1152 static void release_hardware_message( struct msg_queue
*queue
, unsigned int hw_id
,
1153 int remove
, user_handle_t new_win
)
1155 struct thread_input
*input
= queue
->input
;
1156 struct message
*msg
;
1158 LIST_FOR_EACH_ENTRY( msg
, &input
->msg_list
, struct message
, entry
)
1160 if (msg
->unique_id
== hw_id
) break;
1162 if (&msg
->entry
== &input
->msg_list
) return; /* not found */
1164 /* clear the queue bit for that message */
1165 if (remove
|| new_win
)
1167 struct message
*other
;
1170 clr_bit
= get_hardware_msg_bit( msg
);
1171 LIST_FOR_EACH_ENTRY( other
, &input
->msg_list
, struct message
, entry
)
1173 if (other
!= msg
&& get_hardware_msg_bit( other
) == clr_bit
)
1179 if (clr_bit
) clear_queue_bits( queue
, clr_bit
);
1182 if (new_win
) /* set the new window */
1184 struct thread
*owner
= get_window_thread( new_win
);
1187 if (owner
->queue
->input
== input
)
1190 set_queue_bits( owner
->queue
, get_hardware_msg_bit( msg
));
1193 release_object( owner
);
1198 update_input_key_state( input
, msg
);
1199 list_remove( &msg
->entry
);
1200 free_message( msg
);
1204 /* find the window that should receive a given hardware message */
1205 static user_handle_t
find_hardware_message_window( struct thread_input
*input
, struct message
*msg
,
1206 unsigned int *msg_code
)
1208 user_handle_t win
= 0;
1210 *msg_code
= msg
->msg
;
1211 if (is_keyboard_msg( msg
))
1213 if (input
&& !(win
= input
->focus
))
1215 win
= input
->active
;
1216 if (*msg_code
< WM_SYSKEYDOWN
) *msg_code
+= WM_SYSKEYDOWN
- WM_KEYDOWN
;
1219 else /* mouse message */
1221 if (!input
|| !(win
= input
->capture
))
1223 if (!(win
= msg
->win
) || !is_window_visible( win
))
1225 if (input
) win
= window_from_point( input
->desktop
, msg
->x
, msg
->y
);
1232 /* queue a hardware message into a given thread input */
1233 static void queue_hardware_message( struct msg_queue
*queue
, struct message
*msg
)
1236 struct thread
*thread
;
1237 struct thread_input
*input
= queue
? queue
->input
: foreground_input
;
1238 unsigned int msg_code
;
1240 last_input_time
= get_tick_count();
1241 win
= find_hardware_message_window( input
, msg
, &msg_code
);
1242 if (!win
|| !(thread
= get_window_thread(win
)))
1244 if (input
) update_input_key_state( input
, msg
);
1248 input
= thread
->queue
->input
;
1250 if (msg
->msg
== WM_MOUSEMOVE
&& merge_message( input
, msg
)) free( msg
);
1253 msg
->unique_id
= 0; /* will be set once we return it to the app */
1254 list_add_tail( &input
->msg_list
, &msg
->entry
);
1255 set_queue_bits( thread
->queue
, get_hardware_msg_bit(msg
) );
1257 release_object( thread
);
1260 /* check message filter for a hardware message */
1261 static int check_hw_message_filter( user_handle_t win
, unsigned int msg_code
,
1262 user_handle_t filter_win
, unsigned int first
, unsigned int last
)
1264 if (msg_code
>= WM_KEYFIRST
&& msg_code
<= WM_KEYLAST
)
1266 /* we can only test the window for a keyboard message since the
1267 * dest window for a mouse message depends on hittest */
1268 if (filter_win
&& win
!= filter_win
&& !is_child_window( filter_win
, win
))
1270 /* the message code is final for a keyboard message, we can simply check it */
1271 return check_msg_filter( msg_code
, first
, last
);
1273 else /* mouse message */
1275 /* we need to check all possible values that the message can have in the end */
1277 if (check_msg_filter( msg_code
, first
, last
)) return 1;
1278 if (msg_code
== WM_MOUSEWHEEL
) return 0; /* no other possible value for this one */
1280 /* all other messages can become non-client messages */
1281 if (check_msg_filter( msg_code
+ (WM_NCMOUSEFIRST
- WM_MOUSEFIRST
), first
, last
)) return 1;
1283 /* clicks can become double-clicks or non-client double-clicks */
1284 if (msg_code
== WM_LBUTTONDOWN
|| msg_code
== WM_MBUTTONDOWN
||
1285 msg_code
== WM_RBUTTONDOWN
|| msg_code
== WM_XBUTTONDOWN
)
1287 if (check_msg_filter( msg_code
+ (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1288 if (check_msg_filter( msg_code
+ (WM_NCLBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1295 /* find a hardware message for the given queue */
1296 static int get_hardware_message( struct thread
*thread
, unsigned int hw_id
, user_handle_t filter_win
,
1297 unsigned int first
, unsigned int last
, struct get_message_reply
*reply
)
1299 struct thread_input
*input
= thread
->queue
->input
;
1300 struct thread
*win_thread
;
1303 int clear_bits
, got_one
= 0;
1304 unsigned int msg_code
;
1306 ptr
= list_head( &input
->msg_list
);
1311 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1312 if (msg
->unique_id
== hw_id
) break;
1313 ptr
= list_next( &input
->msg_list
, ptr
);
1315 if (!ptr
) ptr
= list_head( &input
->msg_list
);
1316 else ptr
= list_next( &input
->msg_list
, ptr
); /* start from the next one */
1319 if (ptr
== list_head( &input
->msg_list
))
1320 clear_bits
= QS_KEY
| QS_MOUSEMOVE
| QS_MOUSEBUTTON
;
1322 clear_bits
= 0; /* don't clear bits if we don't go through the whole list */
1326 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1327 ptr
= list_next( &input
->msg_list
, ptr
);
1328 win
= find_hardware_message_window( input
, msg
, &msg_code
);
1329 if (!win
|| !(win_thread
= get_window_thread( win
)))
1331 /* no window at all, remove it */
1332 update_input_key_state( input
, msg
);
1333 list_remove( &msg
->entry
);
1334 free_message( msg
);
1337 if (win_thread
!= thread
)
1339 if (win_thread
->queue
->input
== input
)
1341 /* wake the other thread */
1342 set_queue_bits( win_thread
->queue
, get_hardware_msg_bit(msg
) );
1347 /* for another thread input, drop it */
1348 update_input_key_state( input
, msg
);
1349 list_remove( &msg
->entry
);
1350 free_message( msg
);
1352 release_object( win_thread
);
1355 release_object( win_thread
);
1357 /* if we already got a message for another thread, or if it doesn't
1358 * match the filter we skip it */
1359 if (got_one
|| !check_hw_message_filter( win
, msg_code
, filter_win
, first
, last
))
1361 clear_bits
&= ~get_hardware_msg_bit( msg
);
1364 /* now we can return it */
1365 if (!msg
->unique_id
) msg
->unique_id
= get_unique_id();
1366 reply
->type
= MSG_HARDWARE
;
1368 reply
->msg
= msg_code
;
1369 reply
->wparam
= msg
->wparam
;
1370 reply
->lparam
= msg
->lparam
;
1373 reply
->time
= msg
->time
;
1374 reply
->info
= msg
->info
;
1375 reply
->hw_id
= msg
->unique_id
;
1378 /* nothing found, clear the hardware queue bits */
1379 clear_queue_bits( thread
->queue
, clear_bits
);
1383 /* increment (or decrement if 'incr' is negative) the queue paint count */
1384 void inc_queue_paint_count( struct thread
*thread
, int incr
)
1386 struct msg_queue
*queue
= thread
->queue
;
1390 if ((queue
->paint_count
+= incr
) < 0) queue
->paint_count
= 0;
1392 if (queue
->paint_count
)
1393 set_queue_bits( queue
, QS_PAINT
);
1395 clear_queue_bits( queue
, QS_PAINT
);
1399 /* remove all messages and timers belonging to a certain window */
1400 void queue_cleanup_window( struct thread
*thread
, user_handle_t win
)
1402 struct msg_queue
*queue
= thread
->queue
;
1410 ptr
= list_head( &queue
->pending_timers
);
1413 struct list
*next
= list_next( &queue
->pending_timers
, ptr
);
1414 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1415 if (timer
->win
== win
) free_timer( queue
, timer
);
1418 ptr
= list_head( &queue
->expired_timers
);
1421 struct list
*next
= list_next( &queue
->expired_timers
, ptr
);
1422 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1423 if (timer
->win
== win
) free_timer( queue
, timer
);
1427 /* remove messages */
1428 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
1430 struct list
*ptr
, *next
;
1432 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->msg_list
[i
] )
1434 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1435 if (msg
->win
== win
) remove_queue_message( queue
, msg
, i
);
1439 thread_input_cleanup_window( queue
, win
);
1442 /* post a message to a window; used by socket handling */
1443 void post_message( user_handle_t win
, unsigned int message
,
1444 unsigned long wparam
, unsigned long lparam
)
1446 struct message
*msg
;
1447 struct thread
*thread
= get_window_thread( win
);
1449 if (!thread
) return;
1451 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1453 msg
->type
= MSG_POSTED
;
1454 msg
->win
= get_user_full_handle( win
);
1456 msg
->wparam
= wparam
;
1457 msg
->lparam
= lparam
;
1458 msg
->time
= get_tick_count();
1466 list_add_tail( &thread
->queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1467 set_queue_bits( thread
->queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
1469 release_object( thread
);
1472 /* post a win event */
1473 void post_win_event( struct thread
*thread
, unsigned int event
,
1474 user_handle_t win
, unsigned int object_id
,
1475 unsigned int child_id
, void *hook_proc
,
1476 const WCHAR
*module
, data_size_t module_size
,
1479 struct message
*msg
;
1481 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1483 struct winevent_msg_data
*data
;
1485 msg
->type
= MSG_WINEVENT
;
1486 msg
->win
= get_user_full_handle( win
);
1488 msg
->wparam
= object_id
;
1489 msg
->lparam
= child_id
;
1490 msg
->time
= get_tick_count();
1496 if ((data
= malloc( sizeof(*data
) + module_size
)))
1499 data
->tid
= get_thread_id( current
);
1500 data
->hook_proc
= hook_proc
;
1501 memcpy( data
+ 1, module
, module_size
);
1504 msg
->data_size
= sizeof(*data
) + module_size
;
1506 if (debug_level
> 1)
1507 fprintf( stderr
, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1508 get_thread_id(thread
), event
, win
, object_id
, child_id
);
1509 list_add_tail( &thread
->queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1510 set_queue_bits( thread
->queue
, QS_SENDMESSAGE
);
1517 /* get the message queue of the current thread */
1518 DECL_HANDLER(get_msg_queue
)
1520 struct msg_queue
*queue
= get_current_queue();
1523 if (queue
) reply
->handle
= alloc_handle( current
->process
, queue
, SYNCHRONIZE
, 0 );
1527 /* set the current message queue wakeup mask */
1528 DECL_HANDLER(set_queue_mask
)
1530 struct msg_queue
*queue
= get_current_queue();
1534 queue
->wake_mask
= req
->wake_mask
;
1535 queue
->changed_mask
= req
->changed_mask
;
1536 reply
->wake_bits
= queue
->wake_bits
;
1537 reply
->changed_bits
= queue
->changed_bits
;
1538 if (is_signaled( queue
))
1540 /* if skip wait is set, do what would have been done in the subsequent wait */
1541 if (req
->skip_wait
) msg_queue_satisfied( &queue
->obj
, current
);
1542 else wake_up( &queue
->obj
, 0 );
1548 /* get the current message queue status */
1549 DECL_HANDLER(get_queue_status
)
1551 struct msg_queue
*queue
= current
->queue
;
1554 reply
->wake_bits
= queue
->wake_bits
;
1555 reply
->changed_bits
= queue
->changed_bits
;
1556 if (req
->clear
) queue
->changed_bits
= 0;
1558 else reply
->wake_bits
= reply
->changed_bits
= 0;
1562 /* send a message to a thread queue */
1563 DECL_HANDLER(send_message
)
1565 struct message
*msg
;
1566 struct msg_queue
*send_queue
= get_current_queue();
1567 struct msg_queue
*recv_queue
= NULL
;
1568 struct thread
*thread
= NULL
;
1570 if (!(thread
= get_thread_from_id( req
->id
))) return;
1572 if (!(recv_queue
= thread
->queue
))
1574 set_error( STATUS_INVALID_PARAMETER
);
1575 release_object( thread
);
1578 if ((req
->flags
& SEND_MSG_ABORT_IF_HUNG
) && is_queue_hung(recv_queue
))
1580 set_error( STATUS_TIMEOUT
);
1581 release_object( thread
);
1585 if ((msg
= mem_alloc( sizeof(*msg
) )))
1587 msg
->type
= req
->type
;
1588 msg
->win
= get_user_full_handle( req
->win
);
1589 msg
->msg
= req
->msg
;
1590 msg
->wparam
= req
->wparam
;
1591 msg
->lparam
= req
->lparam
;
1592 msg
->time
= get_tick_count();
1598 msg
->data_size
= get_req_data_size();
1600 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1603 release_object( thread
);
1609 case MSG_OTHER_PROCESS
:
1613 if (!(msg
->result
= alloc_message_result( send_queue
, recv_queue
, msg
, req
->timeout
)))
1615 free_message( msg
);
1620 list_add_tail( &recv_queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1621 set_queue_bits( recv_queue
, QS_SENDMESSAGE
);
1624 list_add_tail( &recv_queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1625 set_queue_bits( recv_queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
1627 case MSG_HARDWARE
: /* should use send_hardware_message instead */
1628 case MSG_CALLBACK_RESULT
: /* cannot send this one */
1630 set_error( STATUS_INVALID_PARAMETER
);
1635 release_object( thread
);
1638 /* send a hardware message to a thread queue */
1639 DECL_HANDLER(send_hardware_message
)
1641 struct message
*msg
;
1642 struct msg_queue
*recv_queue
= NULL
;
1643 struct thread
*thread
= NULL
;
1647 if (!(thread
= get_thread_from_id( req
->id
))) return;
1650 if (thread
&& !(recv_queue
= thread
->queue
))
1652 set_error( STATUS_INVALID_PARAMETER
);
1653 release_object( thread
);
1657 if ((msg
= mem_alloc( sizeof(*msg
) )))
1659 msg
->type
= MSG_HARDWARE
;
1660 msg
->win
= get_user_full_handle( req
->win
);
1661 msg
->msg
= req
->msg
;
1662 msg
->wparam
= req
->wparam
;
1663 msg
->lparam
= req
->lparam
;
1664 msg
->time
= req
->time
;
1667 msg
->info
= req
->info
;
1671 queue_hardware_message( recv_queue
, msg
);
1673 if (thread
) release_object( thread
);
1676 /* post a quit message to the current queue */
1677 DECL_HANDLER(post_quit_message
)
1679 struct msg_queue
*queue
= get_current_queue();
1684 queue
->quit_message
= 1;
1685 queue
->exit_code
= req
->exit_code
;
1686 set_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
1689 /* get a message from the current queue */
1690 DECL_HANDLER(get_message
)
1692 struct timer
*timer
;
1694 struct msg_queue
*queue
= get_current_queue();
1695 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
1696 unsigned int filter
= req
->flags
>> 16;
1698 reply
->active_hooks
= get_active_hooks();
1701 queue
->last_get_msg
= current_time
;
1702 if (!filter
) filter
= QS_ALLINPUT
;
1704 /* first check for sent messages */
1705 if ((ptr
= list_head( &queue
->msg_list
[SEND_MESSAGE
] )))
1707 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1708 receive_message( queue
, msg
, reply
);
1712 /* clear changed bits so we can wait on them if we don't find a message */
1713 if (filter
& QS_POSTMESSAGE
)
1715 queue
->changed_bits
&= ~(QS_POSTMESSAGE
| QS_HOTKEY
| QS_TIMER
);
1716 if (req
->get_first
== 0 && req
->get_last
== ~0U) queue
->changed_bits
&= ~QS_ALLPOSTMESSAGE
;
1718 if (filter
& QS_INPUT
) queue
->changed_bits
&= ~QS_INPUT
;
1719 if (filter
& QS_PAINT
) queue
->changed_bits
&= ~QS_PAINT
;
1721 /* then check for posted messages */
1722 if ((filter
& QS_POSTMESSAGE
) &&
1723 get_posted_message( queue
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
1726 /* only check for quit messages if not posted messages pending.
1727 * note: the quit message isn't filtered */
1728 if (get_quit_message( queue
, req
->flags
, reply
))
1731 /* then check for any raw hardware message */
1732 if ((filter
& QS_INPUT
) &&
1733 filter_contains_hw_range( req
->get_first
, req
->get_last
) &&
1734 get_hardware_message( current
, req
->hw_id
, get_win
, req
->get_first
, req
->get_last
, reply
))
1737 /* now check for WM_PAINT */
1738 if ((filter
& QS_PAINT
) &&
1739 queue
->paint_count
&&
1740 check_msg_filter( WM_PAINT
, req
->get_first
, req
->get_last
) &&
1741 (reply
->win
= find_window_to_repaint( get_win
, current
)))
1743 reply
->type
= MSG_POSTED
;
1744 reply
->msg
= WM_PAINT
;
1749 reply
->time
= get_tick_count();
1754 /* now check for timer */
1755 if ((filter
& QS_TIMER
) &&
1756 (timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
1757 req
->get_last
, (req
->flags
& PM_REMOVE
) )))
1759 reply
->type
= MSG_POSTED
;
1760 reply
->win
= timer
->win
;
1761 reply
->msg
= timer
->msg
;
1762 reply
->wparam
= timer
->id
;
1763 reply
->lparam
= timer
->lparam
;
1766 reply
->time
= get_tick_count();
1771 set_error( STATUS_PENDING
); /* FIXME */
1775 /* reply to a sent message */
1776 DECL_HANDLER(reply_message
)
1778 if (!current
->queue
) set_error( STATUS_ACCESS_DENIED
);
1779 else if (current
->queue
->recv_result
)
1780 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
1781 get_req_data(), get_req_data_size() );
1785 /* accept the current hardware message */
1786 DECL_HANDLER(accept_hardware_message
)
1789 release_hardware_message( current
->queue
, req
->hw_id
, req
->remove
, req
->new_win
);
1791 set_error( STATUS_ACCESS_DENIED
);
1795 /* retrieve the reply for the last message sent */
1796 DECL_HANDLER(get_message_reply
)
1798 struct message_result
*result
;
1800 struct msg_queue
*queue
= current
->queue
;
1804 set_error( STATUS_PENDING
);
1807 if (!(entry
= list_head( &queue
->send_result
))) return; /* no reply ready */
1809 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1810 if (result
->replied
|| req
->cancel
)
1812 if (result
->replied
)
1814 reply
->result
= result
->result
;
1815 set_error( result
->error
);
1818 data_size_t data_len
= min( result
->data_size
, get_reply_max_size() );
1819 set_reply_data_ptr( result
->data
, data_len
);
1820 result
->data
= NULL
;
1821 result
->data_size
= 0;
1824 remove_result_from_sender( result
);
1826 entry
= list_head( &queue
->send_result
);
1827 if (!entry
) clear_queue_bits( queue
, QS_SMRESULT
);
1830 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1831 if (!result
->replied
) clear_queue_bits( queue
, QS_SMRESULT
);
1835 else set_error( STATUS_ACCESS_DENIED
);
1839 /* set a window timer */
1840 DECL_HANDLER(set_win_timer
)
1842 struct timer
*timer
;
1843 struct msg_queue
*queue
;
1844 struct thread
*thread
= NULL
;
1845 user_handle_t win
= 0;
1846 unsigned long id
= req
->id
;
1850 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1852 set_error( STATUS_INVALID_HANDLE
);
1855 if (thread
->process
!= current
->process
)
1857 release_object( thread
);
1858 set_error( STATUS_ACCESS_DENIED
);
1861 queue
= thread
->queue
;
1862 /* remove it if it existed already */
1863 if ((timer
= find_timer( queue
, win
, req
->msg
, id
))) free_timer( queue
, timer
);
1867 queue
= get_current_queue();
1868 /* find a free id for it */
1871 id
= queue
->next_timer_id
;
1872 if (++queue
->next_timer_id
>= 0x10000) queue
->next_timer_id
= 1;
1874 while (find_timer( queue
, 0, req
->msg
, id
));
1877 if ((timer
= set_timer( queue
, req
->rate
)))
1880 timer
->msg
= req
->msg
;
1882 timer
->lparam
= req
->lparam
;
1885 if (thread
) release_object( thread
);
1888 /* kill a window timer */
1889 DECL_HANDLER(kill_win_timer
)
1891 struct timer
*timer
;
1892 struct thread
*thread
;
1893 user_handle_t win
= 0;
1897 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1899 set_error( STATUS_INVALID_HANDLE
);
1902 if (thread
->process
!= current
->process
)
1904 release_object( thread
);
1905 set_error( STATUS_ACCESS_DENIED
);
1909 else thread
= (struct thread
*)grab_object( current
);
1911 if (thread
->queue
&& (timer
= find_timer( thread
->queue
, win
, req
->msg
, req
->id
)))
1912 free_timer( thread
->queue
, timer
);
1914 set_error( STATUS_INVALID_PARAMETER
);
1916 release_object( thread
);
1920 /* attach (or detach) thread inputs */
1921 DECL_HANDLER(attach_thread_input
)
1923 struct thread
*thread_from
= get_thread_from_id( req
->tid_from
);
1924 struct thread
*thread_to
= get_thread_from_id( req
->tid_to
);
1926 if (!thread_from
|| !thread_to
)
1928 if (thread_from
) release_object( thread_from
);
1929 if (thread_to
) release_object( thread_to
);
1932 if (thread_from
!= thread_to
)
1934 if (req
->attach
) attach_thread_input( thread_from
, thread_to
);
1937 if (thread_from
->queue
&& thread_to
->queue
&&
1938 thread_from
->queue
->input
== thread_to
->queue
->input
)
1939 detach_thread_input( thread_from
);
1941 set_error( STATUS_ACCESS_DENIED
);
1944 else set_error( STATUS_ACCESS_DENIED
);
1945 release_object( thread_from
);
1946 release_object( thread_to
);
1950 /* get thread input data */
1951 DECL_HANDLER(get_thread_input
)
1953 struct thread
*thread
= NULL
;
1954 struct thread_input
*input
;
1958 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1959 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1961 else input
= foreground_input
; /* get the foreground thread info */
1965 reply
->focus
= input
->focus
;
1966 reply
->capture
= input
->capture
;
1967 reply
->active
= input
->active
;
1968 reply
->menu_owner
= input
->menu_owner
;
1969 reply
->move_size
= input
->move_size
;
1970 reply
->caret
= input
->caret
;
1971 reply
->rect
= input
->caret_rect
;
1978 reply
->menu_owner
= 0;
1979 reply
->move_size
= 0;
1981 reply
->rect
.left
= reply
->rect
.top
= reply
->rect
.right
= reply
->rect
.bottom
= 0;
1983 /* foreground window is active window of foreground thread */
1984 reply
->foreground
= foreground_input
? foreground_input
->active
: 0;
1985 if (thread
) release_object( thread
);
1989 /* retrieve queue keyboard state for a given thread */
1990 DECL_HANDLER(get_key_state
)
1992 struct thread
*thread
;
1993 struct thread_input
*input
;
1995 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1996 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1999 if (req
->key
>= 0) reply
->state
= input
->keystate
[req
->key
& 0xff];
2000 set_reply_data( input
->keystate
, min( get_reply_max_size(), sizeof(input
->keystate
) ));
2002 release_object( thread
);
2006 /* set queue keyboard state for a given thread */
2007 DECL_HANDLER(set_key_state
)
2009 struct thread
*thread
= NULL
;
2010 struct thread_input
*input
;
2012 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2013 input
= thread
->queue
? thread
->queue
->input
: NULL
;
2016 data_size_t size
= min( sizeof(input
->keystate
), get_req_data_size() );
2017 if (size
) memcpy( input
->keystate
, get_req_data(), size
);
2019 release_object( thread
);
2023 /* set the system foreground window */
2024 DECL_HANDLER(set_foreground_window
)
2026 struct thread
*thread
;
2027 struct msg_queue
*queue
= get_current_queue();
2029 reply
->previous
= foreground_input
? foreground_input
->active
: 0;
2030 reply
->send_msg_old
= (reply
->previous
&& foreground_input
!= queue
->input
);
2031 reply
->send_msg_new
= FALSE
;
2033 if (is_top_level_window( req
->handle
) &&
2034 ((thread
= get_window_thread( req
->handle
))))
2036 foreground_input
= thread
->queue
->input
;
2037 reply
->send_msg_new
= (foreground_input
!= queue
->input
);
2038 release_object( thread
);
2040 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE
);
2044 /* set the current thread focus window */
2045 DECL_HANDLER(set_focus_window
)
2047 struct msg_queue
*queue
= get_current_queue();
2049 reply
->previous
= 0;
2050 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2052 reply
->previous
= queue
->input
->focus
;
2053 queue
->input
->focus
= get_user_full_handle( req
->handle
);
2058 /* set the current thread active window */
2059 DECL_HANDLER(set_active_window
)
2061 struct msg_queue
*queue
= get_current_queue();
2063 reply
->previous
= 0;
2064 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2066 if (!req
->handle
|| make_window_active( req
->handle
))
2068 reply
->previous
= queue
->input
->active
;
2069 queue
->input
->active
= get_user_full_handle( req
->handle
);
2071 else set_error( STATUS_INVALID_HANDLE
);
2076 /* set the current thread capture window */
2077 DECL_HANDLER(set_capture_window
)
2079 struct msg_queue
*queue
= get_current_queue();
2081 reply
->previous
= reply
->full_handle
= 0;
2082 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2084 struct thread_input
*input
= queue
->input
;
2086 reply
->previous
= input
->capture
;
2087 input
->capture
= get_user_full_handle( req
->handle
);
2088 input
->menu_owner
= (req
->flags
& CAPTURE_MENU
) ? input
->capture
: 0;
2089 input
->move_size
= (req
->flags
& CAPTURE_MOVESIZE
) ? input
->capture
: 0;
2090 reply
->full_handle
= input
->capture
;
2095 /* Set the current thread caret window */
2096 DECL_HANDLER(set_caret_window
)
2098 struct msg_queue
*queue
= get_current_queue();
2100 reply
->previous
= 0;
2101 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2103 struct thread_input
*input
= queue
->input
;
2105 reply
->previous
= input
->caret
;
2106 reply
->old_rect
= input
->caret_rect
;
2107 reply
->old_hide
= input
->caret_hide
;
2108 reply
->old_state
= input
->caret_state
;
2110 set_caret_window( input
, get_user_full_handle(req
->handle
) );
2111 input
->caret_rect
.right
= input
->caret_rect
.left
+ req
->width
;
2112 input
->caret_rect
.bottom
= input
->caret_rect
.top
+ req
->height
;
2117 /* Set the current thread caret information */
2118 DECL_HANDLER(set_caret_info
)
2120 struct msg_queue
*queue
= get_current_queue();
2121 struct thread_input
*input
;
2124 input
= queue
->input
;
2125 reply
->full_handle
= input
->caret
;
2126 reply
->old_rect
= input
->caret_rect
;
2127 reply
->old_hide
= input
->caret_hide
;
2128 reply
->old_state
= input
->caret_state
;
2130 if (req
->handle
&& get_user_full_handle(req
->handle
) != input
->caret
)
2132 set_error( STATUS_ACCESS_DENIED
);
2135 if (req
->flags
& SET_CARET_POS
)
2137 input
->caret_rect
.right
+= req
->x
- input
->caret_rect
.left
;
2138 input
->caret_rect
.bottom
+= req
->y
- input
->caret_rect
.top
;
2139 input
->caret_rect
.left
= req
->x
;
2140 input
->caret_rect
.top
= req
->y
;
2142 if (req
->flags
& SET_CARET_HIDE
)
2144 input
->caret_hide
+= req
->hide
;
2145 if (input
->caret_hide
< 0) input
->caret_hide
= 0;
2147 if (req
->flags
& SET_CARET_STATE
)
2149 if (req
->state
== -1) input
->caret_state
= !input
->caret_state
;
2150 else input
->caret_state
= !!req
->state
;
2155 /* get the time of the last input event */
2156 DECL_HANDLER(get_last_input_time
)
2158 reply
->time
= last_input_time
;