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 error
; /* error code to pass back to sender */
60 lparam_t result
; /* reply result */
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 lparam_t wparam
; /* parameters */
74 lparam_t lparam
; /* parameters */
75 unsigned int time
; /* message time */
76 void *data
; /* message data for sent messages */
77 unsigned int data_size
; /* size of message data */
78 unsigned int unique_id
; /* unique id for nested hw message waits */
79 struct message_result
*result
; /* result in sender queue */
84 struct list entry
; /* entry in timer list */
85 timeout_t when
; /* next expiration */
86 unsigned int rate
; /* timer rate in ms */
87 user_handle_t win
; /* window handle */
88 unsigned int msg
; /* message to post */
89 lparam_t id
; /* timer id */
90 lparam_t lparam
; /* lparam for message */
95 struct object obj
; /* object header */
96 struct desktop
*desktop
; /* desktop that this thread input belongs to */
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 struct fd
*fd
; /* optional file descriptor to poll */
114 unsigned int wake_bits
; /* wakeup bits */
115 unsigned int wake_mask
; /* wakeup mask */
116 unsigned int changed_bits
; /* changed wakeup bits */
117 unsigned int changed_mask
; /* changed wakeup mask */
118 int paint_count
; /* pending paint messages count */
119 int quit_message
; /* is there a pending quit message? */
120 int exit_code
; /* exit code of pending quit message */
121 struct list msg_list
[NB_MSG_KINDS
]; /* lists of messages */
122 struct list send_result
; /* stack of sent messages waiting for result */
123 struct list callback_result
; /* list of callback messages waiting for result */
124 struct message_result
*recv_result
; /* stack of received messages waiting for result */
125 struct list pending_timers
; /* list of pending timers */
126 struct list expired_timers
; /* list of expired timers */
127 lparam_t next_timer_id
; /* id for the next timer with a 0 window */
128 struct timeout_user
*timeout
; /* timeout for next timer to expire */
129 struct thread_input
*input
; /* thread input descriptor */
130 struct hook_table
*hooks
; /* hook table */
131 timeout_t last_get_msg
; /* time of last get message call */
134 static void msg_queue_dump( struct object
*obj
, int verbose
);
135 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
136 static void msg_queue_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
137 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
);
138 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
);
139 static void msg_queue_destroy( struct object
*obj
);
140 static void msg_queue_poll_event( struct fd
*fd
, int event
);
141 static void thread_input_dump( struct object
*obj
, int verbose
);
142 static void thread_input_destroy( struct object
*obj
);
143 static void timer_callback( void *private );
145 static const struct object_ops msg_queue_ops
=
147 sizeof(struct msg_queue
), /* size */
148 msg_queue_dump
, /* dump */
149 no_get_type
, /* get_type */
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 default_get_sd
, /* get_sd */
158 default_set_sd
, /* set_sd */
159 no_lookup_name
, /* lookup_name */
160 no_open_file
, /* open_file */
161 no_close_handle
, /* close_handle */
162 msg_queue_destroy
/* destroy */
165 static const struct fd_ops msg_queue_fd_ops
=
167 NULL
, /* get_poll_events */
168 msg_queue_poll_event
, /* poll_event */
170 NULL
, /* get_fd_type */
172 NULL
, /* queue_async */
173 NULL
, /* reselect_async */
174 NULL
/* cancel async */
178 static const struct object_ops thread_input_ops
=
180 sizeof(struct thread_input
), /* size */
181 thread_input_dump
, /* dump */
182 no_get_type
, /* get_type */
183 no_add_queue
, /* add_queue */
184 NULL
, /* remove_queue */
186 NULL
, /* satisfied */
187 no_signal
, /* signal */
188 no_get_fd
, /* get_fd */
189 no_map_access
, /* map_access */
190 default_get_sd
, /* get_sd */
191 default_set_sd
, /* set_sd */
192 no_lookup_name
, /* lookup_name */
193 no_open_file
, /* open_file */
194 no_close_handle
, /* close_handle */
195 thread_input_destroy
/* destroy */
198 /* pointer to input structure of foreground thread */
199 static struct thread_input
*foreground_input
;
200 static unsigned int last_input_time
;
202 static void free_message( struct message
*msg
);
204 /* set the caret window in a given thread input */
205 static void set_caret_window( struct thread_input
*input
, user_handle_t win
)
207 if (!win
|| win
!= input
->caret
)
209 input
->caret_rect
.left
= 0;
210 input
->caret_rect
.top
= 0;
211 input
->caret_rect
.right
= 0;
212 input
->caret_rect
.bottom
= 0;
215 input
->caret_hide
= 1;
216 input
->caret_state
= 0;
219 /* create a thread input object */
220 static struct thread_input
*create_thread_input( struct thread
*thread
)
222 struct thread_input
*input
;
224 if ((input
= alloc_object( &thread_input_ops
)))
229 input
->menu_owner
= 0;
230 input
->move_size
= 0;
231 list_init( &input
->msg_list
);
232 set_caret_window( input
, 0 );
233 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
235 if (!(input
->desktop
= get_thread_desktop( thread
, 0 /* FIXME: access rights */ )))
237 release_object( input
);
244 /* release the thread input data of a given thread */
245 static inline void release_thread_input( struct thread
*thread
)
247 struct thread_input
*input
= thread
->queue
->input
;
250 release_object( input
);
251 thread
->queue
->input
= NULL
;
254 /* create a message queue object */
255 static struct msg_queue
*create_msg_queue( struct thread
*thread
, struct thread_input
*input
)
257 struct msg_queue
*queue
;
260 if (!input
&& !(input
= create_thread_input( thread
))) return NULL
;
261 if ((queue
= alloc_object( &msg_queue_ops
)))
264 queue
->wake_bits
= 0;
265 queue
->wake_mask
= 0;
266 queue
->changed_bits
= 0;
267 queue
->changed_mask
= 0;
268 queue
->paint_count
= 0;
269 queue
->quit_message
= 0;
270 queue
->recv_result
= NULL
;
271 queue
->next_timer_id
= 0x7fff;
272 queue
->timeout
= NULL
;
273 queue
->input
= (struct thread_input
*)grab_object( input
);
275 queue
->last_get_msg
= current_time
;
276 list_init( &queue
->send_result
);
277 list_init( &queue
->callback_result
);
278 list_init( &queue
->pending_timers
);
279 list_init( &queue
->expired_timers
);
280 for (i
= 0; i
< NB_MSG_KINDS
; i
++) list_init( &queue
->msg_list
[i
] );
282 thread
->queue
= queue
;
283 if (!thread
->process
->queue
)
284 thread
->process
->queue
= (struct msg_queue
*)grab_object( queue
);
286 release_object( input
);
290 /* free the message queue of a thread at thread exit */
291 void free_msg_queue( struct thread
*thread
)
293 struct process
*process
= thread
->process
;
295 remove_thread_hooks( thread
);
296 if (!thread
->queue
) return;
297 if (process
->queue
== thread
->queue
) /* is it the process main queue? */
299 release_object( process
->queue
);
300 process
->queue
= NULL
;
302 release_object( thread
->queue
);
303 thread
->queue
= NULL
;
306 /* get the hook table for a given thread */
307 struct hook_table
*get_queue_hooks( struct thread
*thread
)
309 if (!thread
->queue
) return NULL
;
310 return thread
->queue
->hooks
;
313 /* set the hook table for a given thread, allocating the queue if needed */
314 void set_queue_hooks( struct thread
*thread
, struct hook_table
*hooks
)
316 struct msg_queue
*queue
= thread
->queue
;
317 if (!queue
&& !(queue
= create_msg_queue( thread
, NULL
))) return;
318 if (queue
->hooks
) release_object( queue
->hooks
);
319 queue
->hooks
= hooks
;
322 /* check the queue status */
323 static inline int is_signaled( struct msg_queue
*queue
)
325 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
328 /* set some queue bits */
329 static inline void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
331 queue
->wake_bits
|= bits
;
332 queue
->changed_bits
|= bits
;
333 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
336 /* clear some queue bits */
337 static inline void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
339 queue
->wake_bits
&= ~bits
;
340 queue
->changed_bits
&= ~bits
;
343 /* check whether msg is a keyboard message */
344 static inline int is_keyboard_msg( struct message
*msg
)
346 return (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
);
349 /* check if message is matched by the filter */
350 static inline int check_msg_filter( unsigned int msg
, unsigned int first
, unsigned int last
)
352 return (msg
>= first
&& msg
<= last
);
355 /* check whether a message filter contains at least one potential hardware message */
356 static inline int filter_contains_hw_range( unsigned int first
, unsigned int last
)
358 /* hardware message ranges are (in numerical order):
359 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
360 * WM_KEYFIRST .. WM_KEYLAST
361 * WM_MOUSEFIRST .. WM_MOUSELAST
363 if (last
< WM_NCMOUSEFIRST
) return 0;
364 if (first
> WM_NCMOUSELAST
&& last
< WM_KEYFIRST
) return 0;
365 if (first
> WM_KEYLAST
&& last
< WM_MOUSEFIRST
) return 0;
366 if (first
> WM_MOUSELAST
) return 0;
370 /* get the QS_* bit corresponding to a given hardware message */
371 static inline int get_hardware_msg_bit( struct message
*msg
)
373 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
374 if (is_keyboard_msg( msg
)) return QS_KEY
;
375 return QS_MOUSEBUTTON
;
378 /* get the current thread queue, creating it if needed */
379 static inline struct msg_queue
*get_current_queue(void)
381 struct msg_queue
*queue
= current
->queue
;
382 if (!queue
) queue
= create_msg_queue( current
, NULL
);
386 /* get a (pseudo-)unique id to tag hardware messages */
387 static inline unsigned int get_unique_id(void)
389 static unsigned int id
;
390 if (!++id
) id
= 1; /* avoid an id of 0 */
394 /* try to merge a message with the last in the list; return 1 if successful */
395 static int merge_message( struct thread_input
*input
, const struct message
*msg
)
397 struct message
*prev
;
398 struct list
*ptr
= list_tail( &input
->msg_list
);
401 prev
= LIST_ENTRY( ptr
, struct message
, entry
);
402 if (prev
->result
) return 0;
403 if (prev
->win
&& msg
->win
&& prev
->win
!= msg
->win
) return 0;
404 if (prev
->msg
!= msg
->msg
) return 0;
405 if (prev
->type
!= msg
->type
) return 0;
406 /* now we can merge it */
407 prev
->wparam
= msg
->wparam
;
408 prev
->lparam
= msg
->lparam
;
409 prev
->time
= msg
->time
;
410 if (msg
->type
== MSG_HARDWARE
&& prev
->data
&& msg
->data
)
412 struct hardware_msg_data
*prev_data
= prev
->data
;
413 struct hardware_msg_data
*msg_data
= msg
->data
;
414 prev_data
->x
= msg_data
->x
;
415 prev_data
->y
= msg_data
->y
;
416 prev_data
->info
= msg_data
->info
;
421 /* free a result structure */
422 static void free_result( struct message_result
*result
)
424 if (result
->timeout
) remove_timeout_user( result
->timeout
);
425 free( result
->data
);
426 if (result
->callback_msg
) free_message( result
->callback_msg
);
430 /* remove the result from the sender list it is on */
431 static inline void remove_result_from_sender( struct message_result
*result
)
433 assert( result
->sender
);
435 list_remove( &result
->sender_entry
);
436 result
->sender
= NULL
;
437 if (!result
->receiver
) free_result( result
);
440 /* store the message result in the appropriate structure */
441 static void store_message_result( struct message_result
*res
, lparam_t result
, unsigned int error
)
443 res
->result
= result
;
448 remove_timeout_user( res
->timeout
);
453 if (res
->callback_msg
)
455 /* queue the callback message in the sender queue */
456 struct callback_msg_data
*data
= res
->callback_msg
->data
;
457 data
->result
= result
;
458 list_add_tail( &res
->sender
->msg_list
[SEND_MESSAGE
], &res
->callback_msg
->entry
);
459 set_queue_bits( res
->sender
, QS_SENDMESSAGE
);
460 res
->callback_msg
= NULL
;
461 remove_result_from_sender( res
);
465 /* wake sender queue if waiting on this result */
466 if (list_head(&res
->sender
->send_result
) == &res
->sender_entry
)
467 set_queue_bits( res
->sender
, QS_SMRESULT
);
473 /* free a message when deleting a queue or window */
474 static void free_message( struct message
*msg
)
476 struct message_result
*result
= msg
->result
;
482 result
->receiver
= NULL
;
483 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
485 else free_result( result
);
491 /* remove (and free) a message from a message list */
492 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
493 enum message_kind kind
)
495 list_remove( &msg
->entry
);
499 if (list_empty( &queue
->msg_list
[kind
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
502 if (list_empty( &queue
->msg_list
[kind
] ) && !queue
->quit_message
)
503 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
509 /* message timed out without getting a reply */
510 static void result_timeout( void *private )
512 struct message_result
*result
= private;
514 assert( !result
->replied
);
516 result
->timeout
= NULL
;
518 if (result
->msg
) /* not received yet */
520 struct message
*msg
= result
->msg
;
524 remove_queue_message( result
->receiver
, msg
, SEND_MESSAGE
);
525 result
->receiver
= NULL
;
528 free_result( result
);
533 store_message_result( result
, 0, STATUS_TIMEOUT
);
536 /* allocate and fill a message result structure */
537 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
538 struct msg_queue
*recv_queue
,
539 struct message
*msg
, timeout_t timeout
)
541 struct message_result
*result
= mem_alloc( sizeof(*result
) );
545 result
->sender
= send_queue
;
546 result
->receiver
= recv_queue
;
549 result
->data_size
= 0;
550 result
->timeout
= NULL
;
552 if (msg
->type
== MSG_CALLBACK
)
554 struct message
*callback_msg
= mem_alloc( sizeof(*callback_msg
) );
561 callback_msg
->type
= MSG_CALLBACK_RESULT
;
562 callback_msg
->win
= msg
->win
;
563 callback_msg
->msg
= msg
->msg
;
564 callback_msg
->wparam
= 0;
565 callback_msg
->lparam
= 0;
566 callback_msg
->time
= get_tick_count();
567 callback_msg
->result
= NULL
;
568 /* steal the data from the original message */
569 callback_msg
->data
= msg
->data
;
570 callback_msg
->data_size
= msg
->data_size
;
574 result
->callback_msg
= callback_msg
;
575 list_add_head( &send_queue
->callback_result
, &result
->sender_entry
);
579 result
->callback_msg
= NULL
;
580 list_add_head( &send_queue
->send_result
, &result
->sender_entry
);
583 if (timeout
!= TIMEOUT_INFINITE
)
584 result
->timeout
= add_timeout_user( timeout
, result_timeout
, result
);
589 /* receive a message, removing it from the sent queue */
590 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
591 struct get_message_reply
*reply
)
593 struct message_result
*result
= msg
->result
;
595 reply
->total
= msg
->data_size
;
596 if (msg
->data_size
> get_reply_max_size())
598 set_error( STATUS_BUFFER_OVERFLOW
);
601 reply
->type
= msg
->type
;
602 reply
->win
= msg
->win
;
603 reply
->msg
= msg
->msg
;
604 reply
->wparam
= msg
->wparam
;
605 reply
->lparam
= msg
->lparam
;
606 reply
->time
= msg
->time
;
608 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
610 list_remove( &msg
->entry
);
611 /* put the result on the receiver result stack */
615 result
->recv_next
= queue
->recv_result
;
616 queue
->recv_result
= result
;
619 if (list_empty( &queue
->msg_list
[SEND_MESSAGE
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
622 /* set the result of the current received message */
623 static void reply_message( struct msg_queue
*queue
, lparam_t result
,
624 unsigned int error
, int remove
, const void *data
, data_size_t len
)
626 struct message_result
*res
= queue
->recv_result
;
630 queue
->recv_result
= res
->recv_next
;
631 res
->receiver
= NULL
;
632 if (!res
->sender
) /* no one waiting for it */
640 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
641 store_message_result( res
, result
, error
);
645 static int match_window( user_handle_t win
, user_handle_t msg_win
)
648 if (win
== (user_handle_t
)-1) return !msg_win
;
649 if (msg_win
== win
) return 1;
650 return is_child_window( win
, msg_win
);
653 /* retrieve a posted message */
654 static int get_posted_message( struct msg_queue
*queue
, user_handle_t win
,
655 unsigned int first
, unsigned int last
, unsigned int flags
,
656 struct get_message_reply
*reply
)
660 /* check against the filters */
661 LIST_FOR_EACH_ENTRY( msg
, &queue
->msg_list
[POST_MESSAGE
], struct message
, entry
)
663 if (!match_window( win
, msg
->win
)) continue;
664 if (!check_msg_filter( msg
->msg
, first
, last
)) continue;
665 goto found
; /* found one */
669 /* return it to the app */
671 reply
->total
= msg
->data_size
;
672 if (msg
->data_size
> get_reply_max_size())
674 set_error( STATUS_BUFFER_OVERFLOW
);
677 reply
->type
= msg
->type
;
678 reply
->win
= msg
->win
;
679 reply
->msg
= msg
->msg
;
680 reply
->wparam
= msg
->wparam
;
681 reply
->lparam
= msg
->lparam
;
682 reply
->time
= msg
->time
;
684 if (flags
& PM_REMOVE
)
688 set_reply_data_ptr( msg
->data
, msg
->data_size
);
692 remove_queue_message( queue
, msg
, POST_MESSAGE
);
694 else if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
699 static int get_quit_message( struct msg_queue
*queue
, unsigned int flags
,
700 struct get_message_reply
*reply
)
702 if (queue
->quit_message
)
705 reply
->type
= MSG_POSTED
;
707 reply
->msg
= WM_QUIT
;
708 reply
->wparam
= queue
->exit_code
;
710 reply
->time
= get_tick_count();
712 if (flags
& PM_REMOVE
)
714 queue
->quit_message
= 0;
715 if (list_empty( &queue
->msg_list
[POST_MESSAGE
] ))
716 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
724 /* empty a message list and free all the messages */
725 static void empty_msg_list( struct list
*list
)
729 while ((ptr
= list_head( list
)) != NULL
)
731 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
732 list_remove( &msg
->entry
);
737 /* cleanup all pending results when deleting a queue */
738 static void cleanup_results( struct msg_queue
*queue
)
742 while ((entry
= list_head( &queue
->send_result
)) != NULL
)
744 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
747 while ((entry
= list_head( &queue
->callback_result
)) != NULL
)
749 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
752 while (queue
->recv_result
)
753 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
756 /* check if the thread owning the queue is hung (not checking for messages) */
757 static int is_queue_hung( struct msg_queue
*queue
)
759 struct wait_queue_entry
*entry
;
761 if (current_time
- queue
->last_get_msg
<= 5 * TICKS_PER_SEC
)
762 return 0; /* less than 5 seconds since last get message -> not hung */
764 LIST_FOR_EACH_ENTRY( entry
, &queue
->obj
.wait_queue
, struct wait_queue_entry
, entry
)
766 if (entry
->thread
->queue
== queue
)
767 return 0; /* thread is waiting on queue -> not hung */
772 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
774 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
775 struct process
*process
= entry
->thread
->process
;
777 /* a thread can only wait on its own queue */
778 if (entry
->thread
->queue
!= queue
)
780 set_error( STATUS_ACCESS_DENIED
);
783 /* if waiting on the main process queue, set the idle event */
784 if (process
->queue
== queue
)
786 if (process
->idle_event
) set_event( process
->idle_event
);
788 if (queue
->fd
&& list_empty( &obj
->wait_queue
)) /* first on the queue */
789 set_fd_events( queue
->fd
, POLLIN
);
790 add_queue( obj
, entry
);
794 static void msg_queue_remove_queue(struct object
*obj
, struct wait_queue_entry
*entry
)
796 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
797 struct process
*process
= entry
->thread
->process
;
799 remove_queue( obj
, entry
);
800 if (queue
->fd
&& list_empty( &obj
->wait_queue
)) /* last on the queue is gone */
801 set_fd_events( queue
->fd
, 0 );
803 assert( entry
->thread
->queue
== queue
);
805 /* if waiting on the main process queue, reset the idle event */
806 if (process
->queue
== queue
)
808 if (process
->idle_event
) reset_event( process
->idle_event
);
812 static void msg_queue_dump( struct object
*obj
, int verbose
)
814 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
815 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
816 queue
->wake_bits
, queue
->wake_mask
);
819 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
)
821 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
826 if ((ret
= check_fd_events( queue
->fd
, POLLIN
)))
827 /* stop waiting on select() if we are signaled */
828 set_fd_events( queue
->fd
, 0 );
829 else if (!list_empty( &obj
->wait_queue
))
830 /* restart waiting on poll() if we are no longer signaled */
831 set_fd_events( queue
->fd
, POLLIN
);
833 return ret
|| is_signaled( queue
);
836 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
)
838 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
839 queue
->wake_mask
= 0;
840 queue
->changed_mask
= 0;
841 return 0; /* Not abandoned */
844 static void msg_queue_destroy( struct object
*obj
)
846 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
850 cleanup_results( queue
);
851 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
853 while ((ptr
= list_head( &queue
->pending_timers
)))
855 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
856 list_remove( &timer
->entry
);
859 while ((ptr
= list_head( &queue
->expired_timers
)))
861 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
862 list_remove( &timer
->entry
);
865 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
866 if (queue
->input
) release_object( queue
->input
);
867 if (queue
->hooks
) release_object( queue
->hooks
);
868 if (queue
->fd
) release_object( queue
->fd
);
871 static void msg_queue_poll_event( struct fd
*fd
, int event
)
873 struct msg_queue
*queue
= get_fd_user( fd
);
874 assert( queue
->obj
.ops
== &msg_queue_ops
);
876 if (event
& (POLLERR
| POLLHUP
)) set_fd_events( fd
, -1 );
877 else set_fd_events( queue
->fd
, 0 );
878 wake_up( &queue
->obj
, 0 );
881 static void thread_input_dump( struct object
*obj
, int verbose
)
883 struct thread_input
*input
= (struct thread_input
*)obj
;
884 fprintf( stderr
, "Thread input focus=%08x capture=%08x active=%08x\n",
885 input
->focus
, input
->capture
, input
->active
);
888 static void thread_input_destroy( struct object
*obj
)
890 struct thread_input
*input
= (struct thread_input
*)obj
;
892 if (foreground_input
== input
) foreground_input
= NULL
;
893 empty_msg_list( &input
->msg_list
);
894 if (input
->desktop
) release_object( input
->desktop
);
897 /* fix the thread input data when a window is destroyed */
898 static inline void thread_input_cleanup_window( struct msg_queue
*queue
, user_handle_t window
)
900 struct thread_input
*input
= queue
->input
;
902 if (window
== input
->focus
) input
->focus
= 0;
903 if (window
== input
->capture
) input
->capture
= 0;
904 if (window
== input
->active
) input
->active
= 0;
905 if (window
== input
->menu_owner
) input
->menu_owner
= 0;
906 if (window
== input
->move_size
) input
->move_size
= 0;
907 if (window
== input
->caret
) set_caret_window( input
, 0 );
910 /* check if the specified window can be set in the input data of a given queue */
911 static int check_queue_input_window( struct msg_queue
*queue
, user_handle_t window
)
913 struct thread
*thread
;
916 if (!window
) return 1; /* we can always clear the data */
918 if ((thread
= get_window_thread( window
)))
920 ret
= (queue
->input
== thread
->queue
->input
);
921 if (!ret
) set_error( STATUS_ACCESS_DENIED
);
922 release_object( thread
);
924 else set_error( STATUS_INVALID_HANDLE
);
929 /* make sure the specified thread has a queue */
930 int init_thread_queue( struct thread
*thread
)
932 if (thread
->queue
) return 1;
933 return (create_msg_queue( thread
, NULL
) != NULL
);
936 /* attach two thread input data structures */
937 int attach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
939 struct desktop
*desktop
;
940 struct thread_input
*input
;
942 if (!thread_to
->queue
&& !(thread_to
->queue
= create_msg_queue( thread_to
, NULL
))) return 0;
943 if (!(desktop
= get_thread_desktop( thread_from
, 0 ))) return 0;
944 input
= (struct thread_input
*)grab_object( thread_to
->queue
->input
);
945 if (input
->desktop
!= desktop
)
947 set_error( STATUS_ACCESS_DENIED
);
948 release_object( input
);
949 release_object( desktop
);
952 release_object( desktop
);
954 if (thread_from
->queue
)
956 release_thread_input( thread_from
);
957 thread_from
->queue
->input
= input
;
961 if (!(thread_from
->queue
= create_msg_queue( thread_from
, input
))) return 0;
963 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
967 /* detach two thread input data structures */
968 void detach_thread_input( struct thread
*thread_from
)
970 struct thread_input
*input
;
972 if ((input
= create_thread_input( thread_from
)))
974 release_thread_input( thread_from
);
975 thread_from
->queue
->input
= input
;
980 /* set the next timer to expire */
981 static void set_next_timer( struct msg_queue
*queue
)
987 remove_timeout_user( queue
->timeout
);
988 queue
->timeout
= NULL
;
990 if ((ptr
= list_head( &queue
->pending_timers
)))
992 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
993 queue
->timeout
= add_timeout_user( timer
->when
, timer_callback
, queue
);
995 /* set/clear QS_TIMER bit */
996 if (list_empty( &queue
->expired_timers
))
997 clear_queue_bits( queue
, QS_TIMER
);
999 set_queue_bits( queue
, QS_TIMER
);
1002 /* find a timer from its window and id */
1003 static struct timer
*find_timer( struct msg_queue
*queue
, user_handle_t win
,
1004 unsigned int msg
, lparam_t id
)
1008 /* we need to search both lists */
1010 LIST_FOR_EACH( ptr
, &queue
->pending_timers
)
1012 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1013 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
1015 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
1017 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1018 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
1023 /* callback for the next timer expiration */
1024 static void timer_callback( void *private )
1026 struct msg_queue
*queue
= private;
1029 queue
->timeout
= NULL
;
1030 /* move on to the next timer */
1031 ptr
= list_head( &queue
->pending_timers
);
1033 list_add_tail( &queue
->expired_timers
, ptr
);
1034 set_next_timer( queue
);
1037 /* link a timer at its rightful place in the queue list */
1038 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
1042 for (ptr
= queue
->pending_timers
.next
; ptr
!= &queue
->pending_timers
; ptr
= ptr
->next
)
1044 struct timer
*t
= LIST_ENTRY( ptr
, struct timer
, entry
);
1045 if (t
->when
>= timer
->when
) break;
1047 list_add_before( ptr
, &timer
->entry
);
1050 /* remove a timer from the queue timer list and free it */
1051 static void free_timer( struct msg_queue
*queue
, struct timer
*timer
)
1053 list_remove( &timer
->entry
);
1055 set_next_timer( queue
);
1058 /* restart an expired timer */
1059 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
1061 list_remove( &timer
->entry
);
1062 while (timer
->when
<= current_time
) timer
->when
+= (timeout_t
)timer
->rate
* 10000;
1063 link_timer( queue
, timer
);
1064 set_next_timer( queue
);
1067 /* find an expired timer matching the filtering parameters */
1068 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
1069 unsigned int get_first
, unsigned int get_last
,
1074 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
1076 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1077 if (win
&& timer
->win
!= win
) continue;
1078 if (check_msg_filter( timer
->msg
, get_first
, get_last
))
1080 if (remove
) restart_timer( queue
, timer
);
1088 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
1090 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
1093 timer
->rate
= max( rate
, 1 );
1094 timer
->when
= current_time
+ (timeout_t
)timer
->rate
* 10000;
1095 link_timer( queue
, timer
);
1096 /* check if we replaced the next timer */
1097 if (list_head( &queue
->pending_timers
) == &timer
->entry
) set_next_timer( queue
);
1102 /* change the input key state for a given key */
1103 static void set_input_key_state( struct thread_input
*input
, unsigned char key
, int down
)
1107 if (!(input
->keystate
[key
] & 0x80)) input
->keystate
[key
] ^= 0x01;
1108 input
->keystate
[key
] |= 0x80;
1110 else input
->keystate
[key
] &= ~0x80;
1113 /* update the input key state for a keyboard message */
1114 static void update_input_key_state( struct thread_input
*input
, const struct message
*msg
)
1121 case WM_LBUTTONDOWN
:
1125 set_input_key_state( input
, VK_LBUTTON
, down
);
1127 case WM_MBUTTONDOWN
:
1131 set_input_key_state( input
, VK_MBUTTON
, down
);
1133 case WM_RBUTTONDOWN
:
1137 set_input_key_state( input
, VK_RBUTTON
, down
);
1139 case WM_XBUTTONDOWN
:
1143 if (msg
->wparam
== XBUTTON1
) set_input_key_state( input
, VK_XBUTTON1
, down
);
1144 else if (msg
->wparam
== XBUTTON2
) set_input_key_state( input
, VK_XBUTTON2
, down
);
1152 key
= (unsigned char)msg
->wparam
;
1153 set_input_key_state( input
, key
, down
);
1158 down
= (input
->keystate
[VK_LCONTROL
] | input
->keystate
[VK_RCONTROL
]) & 0x80;
1159 set_input_key_state( input
, VK_CONTROL
, down
);
1163 down
= (input
->keystate
[VK_LMENU
] | input
->keystate
[VK_RMENU
]) & 0x80;
1164 set_input_key_state( input
, VK_MENU
, down
);
1168 down
= (input
->keystate
[VK_LSHIFT
] | input
->keystate
[VK_RSHIFT
]) & 0x80;
1169 set_input_key_state( input
, VK_SHIFT
, down
);
1176 /* release the hardware message currently being processed by the given thread */
1177 static void release_hardware_message( struct msg_queue
*queue
, unsigned int hw_id
,
1178 int remove
, user_handle_t new_win
)
1180 struct thread_input
*input
= queue
->input
;
1181 struct message
*msg
;
1183 LIST_FOR_EACH_ENTRY( msg
, &input
->msg_list
, struct message
, entry
)
1185 if (msg
->unique_id
== hw_id
) break;
1187 if (&msg
->entry
== &input
->msg_list
) return; /* not found */
1189 /* clear the queue bit for that message */
1190 if (remove
|| new_win
)
1192 struct message
*other
;
1195 clr_bit
= get_hardware_msg_bit( msg
);
1196 LIST_FOR_EACH_ENTRY( other
, &input
->msg_list
, struct message
, entry
)
1198 if (other
!= msg
&& get_hardware_msg_bit( other
) == clr_bit
)
1204 if (clr_bit
) clear_queue_bits( queue
, clr_bit
);
1207 if (new_win
) /* set the new window */
1209 struct thread
*owner
= get_window_thread( new_win
);
1213 if (owner
->queue
->input
!= input
)
1215 list_remove( &msg
->entry
);
1216 if (msg
->msg
== WM_MOUSEMOVE
&& merge_message( owner
->queue
->input
, msg
))
1218 free_message( msg
);
1219 release_object( owner
);
1222 list_add_tail( &owner
->queue
->input
->msg_list
, &msg
->entry
);
1224 set_queue_bits( owner
->queue
, get_hardware_msg_bit( msg
));
1226 release_object( owner
);
1231 update_input_key_state( input
, msg
);
1232 list_remove( &msg
->entry
);
1233 free_message( msg
);
1237 /* find the window that should receive a given hardware message */
1238 static user_handle_t
find_hardware_message_window( struct thread_input
*input
, struct message
*msg
,
1239 struct hardware_msg_data
*data
, unsigned int *msg_code
)
1241 user_handle_t win
= 0;
1243 *msg_code
= msg
->msg
;
1244 if (is_keyboard_msg( msg
))
1246 if (input
&& !(win
= input
->focus
))
1248 win
= input
->active
;
1249 if (*msg_code
< WM_SYSKEYDOWN
) *msg_code
+= WM_SYSKEYDOWN
- WM_KEYDOWN
;
1252 else /* mouse message */
1254 if (!input
|| !(win
= input
->capture
))
1256 if (!(win
= msg
->win
) || !is_window_visible( win
))
1258 if (input
) win
= window_from_point( input
->desktop
, data
->x
, data
->y
);
1265 /* queue a hardware message into a given thread input */
1266 static void queue_hardware_message( struct msg_queue
*queue
, struct message
*msg
,
1267 struct hardware_msg_data
*data
)
1270 struct thread
*thread
;
1271 struct thread_input
*input
= queue
? queue
->input
: foreground_input
;
1272 unsigned int msg_code
;
1274 last_input_time
= get_tick_count();
1275 win
= find_hardware_message_window( input
, msg
, data
, &msg_code
);
1276 if (!win
|| !(thread
= get_window_thread(win
)))
1278 if (input
) update_input_key_state( input
, msg
);
1282 input
= thread
->queue
->input
;
1284 if (msg
->msg
== WM_MOUSEMOVE
&& merge_message( input
, msg
)) free( msg
);
1287 msg
->unique_id
= 0; /* will be set once we return it to the app */
1288 list_add_tail( &input
->msg_list
, &msg
->entry
);
1289 set_queue_bits( thread
->queue
, get_hardware_msg_bit(msg
) );
1291 release_object( thread
);
1294 /* check message filter for a hardware message */
1295 static int check_hw_message_filter( user_handle_t win
, unsigned int msg_code
,
1296 user_handle_t filter_win
, unsigned int first
, unsigned int last
)
1298 if (msg_code
>= WM_KEYFIRST
&& msg_code
<= WM_KEYLAST
)
1300 /* we can only test the window for a keyboard message since the
1301 * dest window for a mouse message depends on hittest */
1302 if (filter_win
&& win
!= filter_win
&& !is_child_window( filter_win
, win
))
1304 /* the message code is final for a keyboard message, we can simply check it */
1305 return check_msg_filter( msg_code
, first
, last
);
1307 else /* mouse message */
1309 /* we need to check all possible values that the message can have in the end */
1311 if (check_msg_filter( msg_code
, first
, last
)) return 1;
1312 if (msg_code
== WM_MOUSEWHEEL
) return 0; /* no other possible value for this one */
1314 /* all other messages can become non-client messages */
1315 if (check_msg_filter( msg_code
+ (WM_NCMOUSEFIRST
- WM_MOUSEFIRST
), first
, last
)) return 1;
1317 /* clicks can become double-clicks or non-client double-clicks */
1318 if (msg_code
== WM_LBUTTONDOWN
|| msg_code
== WM_MBUTTONDOWN
||
1319 msg_code
== WM_RBUTTONDOWN
|| msg_code
== WM_XBUTTONDOWN
)
1321 if (check_msg_filter( msg_code
+ (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1322 if (check_msg_filter( msg_code
+ (WM_NCLBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1329 /* find a hardware message for the given queue */
1330 static int get_hardware_message( struct thread
*thread
, unsigned int hw_id
, user_handle_t filter_win
,
1331 unsigned int first
, unsigned int last
, struct get_message_reply
*reply
)
1333 struct thread_input
*input
= thread
->queue
->input
;
1334 struct thread
*win_thread
;
1337 int clear_bits
, got_one
= 0;
1338 unsigned int msg_code
;
1340 ptr
= list_head( &input
->msg_list
);
1345 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1346 if (msg
->unique_id
== hw_id
) break;
1347 ptr
= list_next( &input
->msg_list
, ptr
);
1349 if (!ptr
) ptr
= list_head( &input
->msg_list
);
1350 else ptr
= list_next( &input
->msg_list
, ptr
); /* start from the next one */
1353 if (ptr
== list_head( &input
->msg_list
))
1354 clear_bits
= QS_KEY
| QS_MOUSEMOVE
| QS_MOUSEBUTTON
;
1356 clear_bits
= 0; /* don't clear bits if we don't go through the whole list */
1360 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1361 struct hardware_msg_data
*data
= msg
->data
;
1363 ptr
= list_next( &input
->msg_list
, ptr
);
1364 win
= find_hardware_message_window( input
, msg
, data
, &msg_code
);
1365 if (!win
|| !(win_thread
= get_window_thread( win
)))
1367 /* no window at all, remove it */
1368 update_input_key_state( input
, msg
);
1369 list_remove( &msg
->entry
);
1370 free_message( msg
);
1373 if (win_thread
!= thread
)
1375 if (win_thread
->queue
->input
== input
)
1377 /* wake the other thread */
1378 set_queue_bits( win_thread
->queue
, get_hardware_msg_bit(msg
) );
1383 /* for another thread input, drop it */
1384 update_input_key_state( input
, msg
);
1385 list_remove( &msg
->entry
);
1386 free_message( msg
);
1388 release_object( win_thread
);
1391 release_object( win_thread
);
1393 /* if we already got a message for another thread, or if it doesn't
1394 * match the filter we skip it */
1395 if (got_one
|| !check_hw_message_filter( win
, msg_code
, filter_win
, first
, last
))
1397 clear_bits
&= ~get_hardware_msg_bit( msg
);
1400 /* now we can return it */
1401 if (!msg
->unique_id
) msg
->unique_id
= get_unique_id();
1402 reply
->type
= MSG_HARDWARE
;
1404 reply
->msg
= msg_code
;
1405 reply
->wparam
= msg
->wparam
;
1406 reply
->lparam
= msg
->lparam
;
1407 reply
->time
= msg
->time
;
1409 data
->hw_id
= msg
->unique_id
;
1410 set_reply_data( msg
->data
, msg
->data_size
);
1413 /* nothing found, clear the hardware queue bits */
1414 clear_queue_bits( thread
->queue
, clear_bits
);
1418 /* increment (or decrement if 'incr' is negative) the queue paint count */
1419 void inc_queue_paint_count( struct thread
*thread
, int incr
)
1421 struct msg_queue
*queue
= thread
->queue
;
1425 if ((queue
->paint_count
+= incr
) < 0) queue
->paint_count
= 0;
1427 if (queue
->paint_count
)
1428 set_queue_bits( queue
, QS_PAINT
);
1430 clear_queue_bits( queue
, QS_PAINT
);
1434 /* remove all messages and timers belonging to a certain window */
1435 void queue_cleanup_window( struct thread
*thread
, user_handle_t win
)
1437 struct msg_queue
*queue
= thread
->queue
;
1445 ptr
= list_head( &queue
->pending_timers
);
1448 struct list
*next
= list_next( &queue
->pending_timers
, ptr
);
1449 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1450 if (timer
->win
== win
) free_timer( queue
, timer
);
1453 ptr
= list_head( &queue
->expired_timers
);
1456 struct list
*next
= list_next( &queue
->expired_timers
, ptr
);
1457 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1458 if (timer
->win
== win
) free_timer( queue
, timer
);
1462 /* remove messages */
1463 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
1465 struct list
*ptr
, *next
;
1467 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->msg_list
[i
] )
1469 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1470 if (msg
->win
== win
) remove_queue_message( queue
, msg
, i
);
1474 thread_input_cleanup_window( queue
, win
);
1477 /* post a message to a window; used by socket handling */
1478 void post_message( user_handle_t win
, unsigned int message
, lparam_t wparam
, lparam_t lparam
)
1480 struct message
*msg
;
1481 struct thread
*thread
= get_window_thread( win
);
1483 if (!thread
) return;
1485 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1487 msg
->type
= MSG_POSTED
;
1488 msg
->win
= get_user_full_handle( win
);
1490 msg
->wparam
= wparam
;
1491 msg
->lparam
= lparam
;
1492 msg
->time
= get_tick_count();
1497 list_add_tail( &thread
->queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1498 set_queue_bits( thread
->queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
1500 release_object( thread
);
1503 /* post a win event */
1504 void post_win_event( struct thread
*thread
, unsigned int event
,
1505 user_handle_t win
, unsigned int object_id
,
1506 unsigned int child_id
, client_ptr_t hook_proc
,
1507 const WCHAR
*module
, data_size_t module_size
,
1510 struct message
*msg
;
1512 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1514 struct winevent_msg_data
*data
;
1516 msg
->type
= MSG_WINEVENT
;
1517 msg
->win
= get_user_full_handle( win
);
1519 msg
->wparam
= object_id
;
1520 msg
->lparam
= child_id
;
1521 msg
->time
= get_tick_count();
1524 if ((data
= malloc( sizeof(*data
) + module_size
)))
1527 data
->tid
= get_thread_id( current
);
1528 data
->hook_proc
= hook_proc
;
1529 memcpy( data
+ 1, module
, module_size
);
1532 msg
->data_size
= sizeof(*data
) + module_size
;
1534 if (debug_level
> 1)
1535 fprintf( stderr
, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
1536 get_thread_id(thread
), event
, win
, object_id
, child_id
);
1537 list_add_tail( &thread
->queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1538 set_queue_bits( thread
->queue
, QS_SENDMESSAGE
);
1546 /* check if the thread owning the window is hung */
1547 DECL_HANDLER(is_window_hung
)
1549 struct thread
*thread
;
1551 thread
= get_window_thread( req
->win
);
1554 reply
->is_hung
= is_queue_hung( thread
->queue
);
1555 release_object( thread
);
1557 else reply
->is_hung
= 0;
1561 /* get the message queue of the current thread */
1562 DECL_HANDLER(get_msg_queue
)
1564 struct msg_queue
*queue
= get_current_queue();
1567 if (queue
) reply
->handle
= alloc_handle( current
->process
, queue
, SYNCHRONIZE
, 0 );
1571 /* set the file descriptor associated to the current thread queue */
1572 DECL_HANDLER(set_queue_fd
)
1574 struct msg_queue
*queue
= get_current_queue();
1578 if (queue
->fd
) /* fd can only be set once */
1580 set_error( STATUS_ACCESS_DENIED
);
1583 if (!(file
= get_file_obj( current
->process
, req
->handle
, SYNCHRONIZE
))) return;
1585 if ((unix_fd
= get_file_unix_fd( file
)) != -1)
1587 if ((unix_fd
= dup( unix_fd
)) != -1)
1588 queue
->fd
= create_anonymous_fd( &msg_queue_fd_ops
, unix_fd
, &queue
->obj
, 0 );
1592 release_object( file
);
1596 /* set the current message queue wakeup mask */
1597 DECL_HANDLER(set_queue_mask
)
1599 struct msg_queue
*queue
= get_current_queue();
1603 queue
->wake_mask
= req
->wake_mask
;
1604 queue
->changed_mask
= req
->changed_mask
;
1605 reply
->wake_bits
= queue
->wake_bits
;
1606 reply
->changed_bits
= queue
->changed_bits
;
1607 if (is_signaled( queue
))
1609 /* if skip wait is set, do what would have been done in the subsequent wait */
1610 if (req
->skip_wait
) msg_queue_satisfied( &queue
->obj
, current
);
1611 else wake_up( &queue
->obj
, 0 );
1617 /* get the current message queue status */
1618 DECL_HANDLER(get_queue_status
)
1620 struct msg_queue
*queue
= current
->queue
;
1623 reply
->wake_bits
= queue
->wake_bits
;
1624 reply
->changed_bits
= queue
->changed_bits
;
1625 if (req
->clear
) queue
->changed_bits
= 0;
1627 else reply
->wake_bits
= reply
->changed_bits
= 0;
1631 /* send a message to a thread queue */
1632 DECL_HANDLER(send_message
)
1634 struct message
*msg
;
1635 struct msg_queue
*send_queue
= get_current_queue();
1636 struct msg_queue
*recv_queue
= NULL
;
1637 struct thread
*thread
= NULL
;
1639 if (!(thread
= get_thread_from_id( req
->id
))) return;
1641 if (!(recv_queue
= thread
->queue
))
1643 set_error( STATUS_INVALID_PARAMETER
);
1644 release_object( thread
);
1647 if ((req
->flags
& SEND_MSG_ABORT_IF_HUNG
) && is_queue_hung(recv_queue
))
1649 set_error( STATUS_TIMEOUT
);
1650 release_object( thread
);
1654 if ((msg
= mem_alloc( sizeof(*msg
) )))
1656 msg
->type
= req
->type
;
1657 msg
->win
= get_user_full_handle( req
->win
);
1658 msg
->msg
= req
->msg
;
1659 msg
->wparam
= req
->wparam
;
1660 msg
->lparam
= req
->lparam
;
1661 msg
->time
= get_tick_count();
1664 msg
->data_size
= get_req_data_size();
1666 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1669 release_object( thread
);
1675 case MSG_OTHER_PROCESS
:
1679 if (!(msg
->result
= alloc_message_result( send_queue
, recv_queue
, msg
, req
->timeout
)))
1681 free_message( msg
);
1686 list_add_tail( &recv_queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1687 set_queue_bits( recv_queue
, QS_SENDMESSAGE
);
1690 list_add_tail( &recv_queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1691 set_queue_bits( recv_queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
1693 case MSG_HARDWARE
: /* should use send_hardware_message instead */
1694 case MSG_CALLBACK_RESULT
: /* cannot send this one */
1696 set_error( STATUS_INVALID_PARAMETER
);
1701 release_object( thread
);
1704 /* send a hardware message to a thread queue */
1705 DECL_HANDLER(send_hardware_message
)
1707 struct message
*msg
;
1708 struct msg_queue
*recv_queue
= NULL
;
1709 struct thread
*thread
= NULL
;
1710 struct hardware_msg_data
*data
;
1714 if (!(thread
= get_thread_from_id( req
->id
))) return;
1717 if (thread
&& !(recv_queue
= thread
->queue
))
1719 set_error( STATUS_INVALID_PARAMETER
);
1720 release_object( thread
);
1724 if (!(data
= mem_alloc( sizeof(*data
) )))
1726 if (thread
) release_object( thread
);
1729 memset( data
, 0, sizeof(*data
) );
1732 data
->info
= req
->info
;
1734 if ((msg
= mem_alloc( sizeof(*msg
) )))
1736 msg
->type
= MSG_HARDWARE
;
1737 msg
->win
= get_user_full_handle( req
->win
);
1738 msg
->msg
= req
->msg
;
1739 msg
->wparam
= req
->wparam
;
1740 msg
->lparam
= req
->lparam
;
1741 msg
->time
= req
->time
;
1744 msg
->data_size
= sizeof(*data
);
1745 queue_hardware_message( recv_queue
, msg
, data
);
1749 if (thread
) release_object( thread
);
1752 /* post a quit message to the current queue */
1753 DECL_HANDLER(post_quit_message
)
1755 struct msg_queue
*queue
= get_current_queue();
1760 queue
->quit_message
= 1;
1761 queue
->exit_code
= req
->exit_code
;
1762 set_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
1765 /* get a message from the current queue */
1766 DECL_HANDLER(get_message
)
1768 struct timer
*timer
;
1770 struct msg_queue
*queue
= get_current_queue();
1771 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
1772 unsigned int filter
= req
->flags
>> 16;
1774 reply
->active_hooks
= get_active_hooks();
1777 queue
->last_get_msg
= current_time
;
1778 if (!filter
) filter
= QS_ALLINPUT
;
1780 /* first check for sent messages */
1781 if ((ptr
= list_head( &queue
->msg_list
[SEND_MESSAGE
] )))
1783 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1784 receive_message( queue
, msg
, reply
);
1788 /* clear changed bits so we can wait on them if we don't find a message */
1789 if (filter
& QS_POSTMESSAGE
)
1791 queue
->changed_bits
&= ~(QS_POSTMESSAGE
| QS_HOTKEY
| QS_TIMER
);
1792 if (req
->get_first
== 0 && req
->get_last
== ~0U) queue
->changed_bits
&= ~QS_ALLPOSTMESSAGE
;
1794 if (filter
& QS_INPUT
) queue
->changed_bits
&= ~QS_INPUT
;
1795 if (filter
& QS_PAINT
) queue
->changed_bits
&= ~QS_PAINT
;
1797 /* then check for posted messages */
1798 if ((filter
& QS_POSTMESSAGE
) &&
1799 get_posted_message( queue
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
1802 /* only check for quit messages if not posted messages pending.
1803 * note: the quit message isn't filtered */
1804 if (get_quit_message( queue
, req
->flags
, reply
))
1807 /* then check for any raw hardware message */
1808 if ((filter
& QS_INPUT
) &&
1809 filter_contains_hw_range( req
->get_first
, req
->get_last
) &&
1810 get_hardware_message( current
, req
->hw_id
, get_win
, req
->get_first
, req
->get_last
, reply
))
1813 /* now check for WM_PAINT */
1814 if ((filter
& QS_PAINT
) &&
1815 queue
->paint_count
&&
1816 check_msg_filter( WM_PAINT
, req
->get_first
, req
->get_last
) &&
1817 (reply
->win
= find_window_to_repaint( get_win
, current
)))
1819 reply
->type
= MSG_POSTED
;
1820 reply
->msg
= WM_PAINT
;
1823 reply
->time
= get_tick_count();
1827 /* now check for timer */
1828 if ((filter
& QS_TIMER
) &&
1829 (timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
1830 req
->get_last
, (req
->flags
& PM_REMOVE
) )))
1832 reply
->type
= MSG_POSTED
;
1833 reply
->win
= timer
->win
;
1834 reply
->msg
= timer
->msg
;
1835 reply
->wparam
= timer
->id
;
1836 reply
->lparam
= timer
->lparam
;
1837 reply
->time
= get_tick_count();
1841 queue
->wake_mask
= req
->wake_mask
;
1842 queue
->changed_mask
= req
->changed_mask
;
1843 set_error( STATUS_PENDING
); /* FIXME */
1847 /* reply to a sent message */
1848 DECL_HANDLER(reply_message
)
1850 if (!current
->queue
) set_error( STATUS_ACCESS_DENIED
);
1851 else if (current
->queue
->recv_result
)
1852 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
1853 get_req_data(), get_req_data_size() );
1857 /* accept the current hardware message */
1858 DECL_HANDLER(accept_hardware_message
)
1861 release_hardware_message( current
->queue
, req
->hw_id
, req
->remove
, req
->new_win
);
1863 set_error( STATUS_ACCESS_DENIED
);
1867 /* retrieve the reply for the last message sent */
1868 DECL_HANDLER(get_message_reply
)
1870 struct message_result
*result
;
1872 struct msg_queue
*queue
= current
->queue
;
1876 set_error( STATUS_PENDING
);
1879 if (!(entry
= list_head( &queue
->send_result
))) return; /* no reply ready */
1881 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1882 if (result
->replied
|| req
->cancel
)
1884 if (result
->replied
)
1886 reply
->result
= result
->result
;
1887 set_error( result
->error
);
1890 data_size_t data_len
= min( result
->data_size
, get_reply_max_size() );
1891 set_reply_data_ptr( result
->data
, data_len
);
1892 result
->data
= NULL
;
1893 result
->data_size
= 0;
1896 remove_result_from_sender( result
);
1898 entry
= list_head( &queue
->send_result
);
1899 if (!entry
) clear_queue_bits( queue
, QS_SMRESULT
);
1902 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1903 if (!result
->replied
) clear_queue_bits( queue
, QS_SMRESULT
);
1907 else set_error( STATUS_ACCESS_DENIED
);
1911 /* set a window timer */
1912 DECL_HANDLER(set_win_timer
)
1914 struct timer
*timer
;
1915 struct msg_queue
*queue
;
1916 struct thread
*thread
= NULL
;
1917 user_handle_t win
= 0;
1918 lparam_t id
= req
->id
;
1922 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1924 set_error( STATUS_INVALID_HANDLE
);
1927 if (thread
->process
!= current
->process
)
1929 release_object( thread
);
1930 set_error( STATUS_ACCESS_DENIED
);
1933 queue
= thread
->queue
;
1934 /* remove it if it existed already */
1935 if ((timer
= find_timer( queue
, win
, req
->msg
, id
))) free_timer( queue
, timer
);
1939 queue
= get_current_queue();
1940 /* look for a timer with this id */
1941 if (id
&& (timer
= find_timer( queue
, 0, req
->msg
, id
)))
1943 /* free and reuse id */
1944 free_timer( queue
, timer
);
1948 /* find a free id for it */
1951 id
= queue
->next_timer_id
;
1952 if (--queue
->next_timer_id
<= 0x100) queue
->next_timer_id
= 0x7fff;
1954 while (find_timer( queue
, 0, req
->msg
, id
));
1958 if ((timer
= set_timer( queue
, req
->rate
)))
1961 timer
->msg
= req
->msg
;
1963 timer
->lparam
= req
->lparam
;
1966 if (thread
) release_object( thread
);
1969 /* kill a window timer */
1970 DECL_HANDLER(kill_win_timer
)
1972 struct timer
*timer
;
1973 struct thread
*thread
;
1974 user_handle_t win
= 0;
1978 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1980 set_error( STATUS_INVALID_HANDLE
);
1983 if (thread
->process
!= current
->process
)
1985 release_object( thread
);
1986 set_error( STATUS_ACCESS_DENIED
);
1990 else thread
= (struct thread
*)grab_object( current
);
1992 if (thread
->queue
&& (timer
= find_timer( thread
->queue
, win
, req
->msg
, req
->id
)))
1993 free_timer( thread
->queue
, timer
);
1995 set_error( STATUS_INVALID_PARAMETER
);
1997 release_object( thread
);
2001 /* attach (or detach) thread inputs */
2002 DECL_HANDLER(attach_thread_input
)
2004 struct thread
*thread_from
= get_thread_from_id( req
->tid_from
);
2005 struct thread
*thread_to
= get_thread_from_id( req
->tid_to
);
2007 if (!thread_from
|| !thread_to
)
2009 if (thread_from
) release_object( thread_from
);
2010 if (thread_to
) release_object( thread_to
);
2013 if (thread_from
!= thread_to
)
2015 if (req
->attach
) attach_thread_input( thread_from
, thread_to
);
2018 if (thread_from
->queue
&& thread_to
->queue
&&
2019 thread_from
->queue
->input
== thread_to
->queue
->input
)
2020 detach_thread_input( thread_from
);
2022 set_error( STATUS_ACCESS_DENIED
);
2025 else set_error( STATUS_ACCESS_DENIED
);
2026 release_object( thread_from
);
2027 release_object( thread_to
);
2031 /* get thread input data */
2032 DECL_HANDLER(get_thread_input
)
2034 struct thread
*thread
= NULL
;
2035 struct thread_input
*input
;
2039 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2040 input
= thread
->queue
? thread
->queue
->input
: NULL
;
2042 else input
= foreground_input
; /* get the foreground thread info */
2046 reply
->focus
= input
->focus
;
2047 reply
->capture
= input
->capture
;
2048 reply
->active
= input
->active
;
2049 reply
->menu_owner
= input
->menu_owner
;
2050 reply
->move_size
= input
->move_size
;
2051 reply
->caret
= input
->caret
;
2052 reply
->rect
= input
->caret_rect
;
2059 reply
->menu_owner
= 0;
2060 reply
->move_size
= 0;
2062 reply
->rect
.left
= reply
->rect
.top
= reply
->rect
.right
= reply
->rect
.bottom
= 0;
2064 /* foreground window is active window of foreground thread */
2065 reply
->foreground
= foreground_input
? foreground_input
->active
: 0;
2066 if (thread
) release_object( thread
);
2070 /* retrieve queue keyboard state for a given thread */
2071 DECL_HANDLER(get_key_state
)
2073 struct thread
*thread
;
2074 struct thread_input
*input
;
2076 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2077 input
= thread
->queue
? thread
->queue
->input
: NULL
;
2080 if (req
->key
>= 0) reply
->state
= input
->keystate
[req
->key
& 0xff];
2081 set_reply_data( input
->keystate
, min( get_reply_max_size(), sizeof(input
->keystate
) ));
2083 release_object( thread
);
2087 /* set queue keyboard state for a given thread */
2088 DECL_HANDLER(set_key_state
)
2090 struct thread
*thread
= NULL
;
2091 struct thread_input
*input
;
2093 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2094 input
= thread
->queue
? thread
->queue
->input
: NULL
;
2097 data_size_t size
= min( sizeof(input
->keystate
), get_req_data_size() );
2098 if (size
) memcpy( input
->keystate
, get_req_data(), size
);
2100 release_object( thread
);
2104 /* set the system foreground window */
2105 DECL_HANDLER(set_foreground_window
)
2107 struct thread
*thread
;
2108 struct msg_queue
*queue
= get_current_queue();
2110 reply
->previous
= foreground_input
? foreground_input
->active
: 0;
2111 reply
->send_msg_old
= (reply
->previous
&& foreground_input
!= queue
->input
);
2112 reply
->send_msg_new
= FALSE
;
2114 if (is_top_level_window( req
->handle
) &&
2115 ((thread
= get_window_thread( req
->handle
))))
2117 foreground_input
= thread
->queue
->input
;
2118 reply
->send_msg_new
= (foreground_input
!= queue
->input
);
2119 release_object( thread
);
2121 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE
);
2125 /* set the current thread focus window */
2126 DECL_HANDLER(set_focus_window
)
2128 struct msg_queue
*queue
= get_current_queue();
2130 reply
->previous
= 0;
2131 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2133 reply
->previous
= queue
->input
->focus
;
2134 queue
->input
->focus
= get_user_full_handle( req
->handle
);
2139 /* set the current thread active window */
2140 DECL_HANDLER(set_active_window
)
2142 struct msg_queue
*queue
= get_current_queue();
2144 reply
->previous
= 0;
2145 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2147 if (!req
->handle
|| make_window_active( req
->handle
))
2149 reply
->previous
= queue
->input
->active
;
2150 queue
->input
->active
= get_user_full_handle( req
->handle
);
2152 else set_error( STATUS_INVALID_HANDLE
);
2157 /* set the current thread capture window */
2158 DECL_HANDLER(set_capture_window
)
2160 struct msg_queue
*queue
= get_current_queue();
2162 reply
->previous
= reply
->full_handle
= 0;
2163 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2165 struct thread_input
*input
= queue
->input
;
2167 reply
->previous
= input
->capture
;
2168 input
->capture
= get_user_full_handle( req
->handle
);
2169 input
->menu_owner
= (req
->flags
& CAPTURE_MENU
) ? input
->capture
: 0;
2170 input
->move_size
= (req
->flags
& CAPTURE_MOVESIZE
) ? input
->capture
: 0;
2171 reply
->full_handle
= input
->capture
;
2176 /* Set the current thread caret window */
2177 DECL_HANDLER(set_caret_window
)
2179 struct msg_queue
*queue
= get_current_queue();
2181 reply
->previous
= 0;
2182 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2184 struct thread_input
*input
= queue
->input
;
2186 reply
->previous
= input
->caret
;
2187 reply
->old_rect
= input
->caret_rect
;
2188 reply
->old_hide
= input
->caret_hide
;
2189 reply
->old_state
= input
->caret_state
;
2191 set_caret_window( input
, get_user_full_handle(req
->handle
) );
2192 input
->caret_rect
.right
= input
->caret_rect
.left
+ req
->width
;
2193 input
->caret_rect
.bottom
= input
->caret_rect
.top
+ req
->height
;
2198 /* Set the current thread caret information */
2199 DECL_HANDLER(set_caret_info
)
2201 struct msg_queue
*queue
= get_current_queue();
2202 struct thread_input
*input
;
2205 input
= queue
->input
;
2206 reply
->full_handle
= input
->caret
;
2207 reply
->old_rect
= input
->caret_rect
;
2208 reply
->old_hide
= input
->caret_hide
;
2209 reply
->old_state
= input
->caret_state
;
2211 if (req
->handle
&& get_user_full_handle(req
->handle
) != input
->caret
)
2213 set_error( STATUS_ACCESS_DENIED
);
2216 if (req
->flags
& SET_CARET_POS
)
2218 input
->caret_rect
.right
+= req
->x
- input
->caret_rect
.left
;
2219 input
->caret_rect
.bottom
+= req
->y
- input
->caret_rect
.top
;
2220 input
->caret_rect
.left
= req
->x
;
2221 input
->caret_rect
.top
= req
->y
;
2223 if (req
->flags
& SET_CARET_HIDE
)
2225 input
->caret_hide
+= req
->hide
;
2226 if (input
->caret_hide
< 0) input
->caret_hide
= 0;
2228 if (req
->flags
& SET_CARET_STATE
)
2230 if (req
->state
== -1) input
->caret_state
= !input
->caret_state
;
2231 else input
->caret_state
= !!req
->state
;
2236 /* get the time of the last input event */
2237 DECL_HANDLER(get_last_input_time
)
2239 reply
->time
= last_input_time
;