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
;
301 if (process
->idle_event
)
303 set_event( process
->idle_event
);
304 release_object( process
->idle_event
);
305 process
->idle_event
= NULL
;
308 release_object( thread
->queue
);
309 thread
->queue
= NULL
;
312 /* get the hook table for a given thread */
313 struct hook_table
*get_queue_hooks( struct thread
*thread
)
315 if (!thread
->queue
) return NULL
;
316 return thread
->queue
->hooks
;
319 /* set the hook table for a given thread, allocating the queue if needed */
320 void set_queue_hooks( struct thread
*thread
, struct hook_table
*hooks
)
322 struct msg_queue
*queue
= thread
->queue
;
323 if (!queue
&& !(queue
= create_msg_queue( thread
, NULL
))) return;
324 if (queue
->hooks
) release_object( queue
->hooks
);
325 queue
->hooks
= hooks
;
328 /* check the queue status */
329 static inline int is_signaled( struct msg_queue
*queue
)
331 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
334 /* set some queue bits */
335 static inline void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
337 queue
->wake_bits
|= bits
;
338 queue
->changed_bits
|= bits
;
339 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
342 /* clear some queue bits */
343 static inline void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
345 queue
->wake_bits
&= ~bits
;
346 queue
->changed_bits
&= ~bits
;
349 /* check whether msg is a keyboard message */
350 static inline int is_keyboard_msg( struct message
*msg
)
352 return (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
);
355 /* check if message is matched by the filter */
356 static inline int check_msg_filter( unsigned int msg
, unsigned int first
, unsigned int last
)
358 return (msg
>= first
&& msg
<= last
);
361 /* check whether a message filter contains at least one potential hardware message */
362 static inline int filter_contains_hw_range( unsigned int first
, unsigned int last
)
364 /* hardware message ranges are (in numerical order):
365 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
366 * WM_KEYFIRST .. WM_KEYLAST
367 * WM_MOUSEFIRST .. WM_MOUSELAST
369 if (last
< WM_NCMOUSEFIRST
) return 0;
370 if (first
> WM_NCMOUSELAST
&& last
< WM_KEYFIRST
) return 0;
371 if (first
> WM_KEYLAST
&& last
< WM_MOUSEFIRST
) return 0;
372 if (first
> WM_MOUSELAST
) return 0;
376 /* get the QS_* bit corresponding to a given hardware message */
377 static inline int get_hardware_msg_bit( struct message
*msg
)
379 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
380 if (is_keyboard_msg( msg
)) return QS_KEY
;
381 return QS_MOUSEBUTTON
;
384 /* get the current thread queue, creating it if needed */
385 static inline struct msg_queue
*get_current_queue(void)
387 struct msg_queue
*queue
= current
->queue
;
388 if (!queue
) queue
= create_msg_queue( current
, NULL
);
392 /* get a (pseudo-)unique id to tag hardware messages */
393 static inline unsigned int get_unique_id(void)
395 static unsigned int id
;
396 if (!++id
) id
= 1; /* avoid an id of 0 */
400 /* try to merge a message with the last in the list; return 1 if successful */
401 static int merge_message( struct thread_input
*input
, const struct message
*msg
)
403 struct message
*prev
;
404 struct list
*ptr
= list_tail( &input
->msg_list
);
407 prev
= LIST_ENTRY( ptr
, struct message
, entry
);
408 if (prev
->result
) return 0;
409 if (prev
->win
&& msg
->win
&& prev
->win
!= msg
->win
) return 0;
410 if (prev
->msg
!= msg
->msg
) return 0;
411 if (prev
->type
!= msg
->type
) return 0;
412 /* now we can merge it */
413 prev
->wparam
= msg
->wparam
;
414 prev
->lparam
= msg
->lparam
;
415 prev
->time
= msg
->time
;
416 if (msg
->type
== MSG_HARDWARE
&& prev
->data
&& msg
->data
)
418 struct hardware_msg_data
*prev_data
= prev
->data
;
419 struct hardware_msg_data
*msg_data
= msg
->data
;
420 prev_data
->x
= msg_data
->x
;
421 prev_data
->y
= msg_data
->y
;
422 prev_data
->info
= msg_data
->info
;
427 /* free a result structure */
428 static void free_result( struct message_result
*result
)
430 if (result
->timeout
) remove_timeout_user( result
->timeout
);
431 free( result
->data
);
432 if (result
->callback_msg
) free_message( result
->callback_msg
);
436 /* remove the result from the sender list it is on */
437 static inline void remove_result_from_sender( struct message_result
*result
)
439 assert( result
->sender
);
441 list_remove( &result
->sender_entry
);
442 result
->sender
= NULL
;
443 if (!result
->receiver
) free_result( result
);
446 /* store the message result in the appropriate structure */
447 static void store_message_result( struct message_result
*res
, lparam_t result
, unsigned int error
)
449 res
->result
= result
;
454 remove_timeout_user( res
->timeout
);
459 if (res
->callback_msg
)
461 /* queue the callback message in the sender queue */
462 struct callback_msg_data
*data
= res
->callback_msg
->data
;
463 data
->result
= result
;
464 list_add_tail( &res
->sender
->msg_list
[SEND_MESSAGE
], &res
->callback_msg
->entry
);
465 set_queue_bits( res
->sender
, QS_SENDMESSAGE
);
466 res
->callback_msg
= NULL
;
467 remove_result_from_sender( res
);
471 /* wake sender queue if waiting on this result */
472 if (list_head(&res
->sender
->send_result
) == &res
->sender_entry
)
473 set_queue_bits( res
->sender
, QS_SMRESULT
);
479 /* free a message when deleting a queue or window */
480 static void free_message( struct message
*msg
)
482 struct message_result
*result
= msg
->result
;
488 result
->receiver
= NULL
;
489 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
491 else free_result( result
);
497 /* remove (and free) a message from a message list */
498 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
499 enum message_kind kind
)
501 list_remove( &msg
->entry
);
505 if (list_empty( &queue
->msg_list
[kind
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
508 if (list_empty( &queue
->msg_list
[kind
] ) && !queue
->quit_message
)
509 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
515 /* message timed out without getting a reply */
516 static void result_timeout( void *private )
518 struct message_result
*result
= private;
520 assert( !result
->replied
);
522 result
->timeout
= NULL
;
524 if (result
->msg
) /* not received yet */
526 struct message
*msg
= result
->msg
;
530 remove_queue_message( result
->receiver
, msg
, SEND_MESSAGE
);
531 result
->receiver
= NULL
;
534 free_result( result
);
539 store_message_result( result
, 0, STATUS_TIMEOUT
);
542 /* allocate and fill a message result structure */
543 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
544 struct msg_queue
*recv_queue
,
545 struct message
*msg
, timeout_t timeout
)
547 struct message_result
*result
= mem_alloc( sizeof(*result
) );
551 result
->sender
= send_queue
;
552 result
->receiver
= recv_queue
;
555 result
->data_size
= 0;
556 result
->timeout
= NULL
;
558 if (msg
->type
== MSG_CALLBACK
)
560 struct message
*callback_msg
= mem_alloc( sizeof(*callback_msg
) );
567 callback_msg
->type
= MSG_CALLBACK_RESULT
;
568 callback_msg
->win
= msg
->win
;
569 callback_msg
->msg
= msg
->msg
;
570 callback_msg
->wparam
= 0;
571 callback_msg
->lparam
= 0;
572 callback_msg
->time
= get_tick_count();
573 callback_msg
->result
= NULL
;
574 /* steal the data from the original message */
575 callback_msg
->data
= msg
->data
;
576 callback_msg
->data_size
= msg
->data_size
;
580 result
->callback_msg
= callback_msg
;
581 list_add_head( &send_queue
->callback_result
, &result
->sender_entry
);
585 result
->callback_msg
= NULL
;
586 list_add_head( &send_queue
->send_result
, &result
->sender_entry
);
589 if (timeout
!= TIMEOUT_INFINITE
)
590 result
->timeout
= add_timeout_user( timeout
, result_timeout
, result
);
595 /* receive a message, removing it from the sent queue */
596 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
597 struct get_message_reply
*reply
)
599 struct message_result
*result
= msg
->result
;
601 reply
->total
= msg
->data_size
;
602 if (msg
->data_size
> get_reply_max_size())
604 set_error( STATUS_BUFFER_OVERFLOW
);
607 reply
->type
= msg
->type
;
608 reply
->win
= msg
->win
;
609 reply
->msg
= msg
->msg
;
610 reply
->wparam
= msg
->wparam
;
611 reply
->lparam
= msg
->lparam
;
612 reply
->time
= msg
->time
;
614 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
616 list_remove( &msg
->entry
);
617 /* put the result on the receiver result stack */
621 result
->recv_next
= queue
->recv_result
;
622 queue
->recv_result
= result
;
625 if (list_empty( &queue
->msg_list
[SEND_MESSAGE
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
628 /* set the result of the current received message */
629 static void reply_message( struct msg_queue
*queue
, lparam_t result
,
630 unsigned int error
, int remove
, const void *data
, data_size_t len
)
632 struct message_result
*res
= queue
->recv_result
;
636 queue
->recv_result
= res
->recv_next
;
637 res
->receiver
= NULL
;
638 if (!res
->sender
) /* no one waiting for it */
646 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
647 store_message_result( res
, result
, error
);
651 /* retrieve a posted message */
652 static int get_posted_message( struct msg_queue
*queue
, user_handle_t win
,
653 unsigned int first
, unsigned int last
, unsigned int flags
,
654 struct get_message_reply
*reply
)
658 /* check against the filters */
659 LIST_FOR_EACH_ENTRY( msg
, &queue
->msg_list
[POST_MESSAGE
], struct message
, entry
)
661 if (win
&& msg
->win
&& msg
->win
!= win
&& !is_child_window( win
, msg
->win
)) continue;
662 if (!check_msg_filter( msg
->msg
, first
, last
)) continue;
663 goto found
; /* found one */
667 /* return it to the app */
669 reply
->total
= msg
->data_size
;
670 if (msg
->data_size
> get_reply_max_size())
672 set_error( STATUS_BUFFER_OVERFLOW
);
675 reply
->type
= msg
->type
;
676 reply
->win
= msg
->win
;
677 reply
->msg
= msg
->msg
;
678 reply
->wparam
= msg
->wparam
;
679 reply
->lparam
= msg
->lparam
;
680 reply
->time
= msg
->time
;
682 if (flags
& PM_REMOVE
)
686 set_reply_data_ptr( msg
->data
, msg
->data_size
);
690 remove_queue_message( queue
, msg
, POST_MESSAGE
);
692 else if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
697 static int get_quit_message( struct msg_queue
*queue
, unsigned int flags
,
698 struct get_message_reply
*reply
)
700 if (queue
->quit_message
)
703 reply
->type
= MSG_POSTED
;
705 reply
->msg
= WM_QUIT
;
706 reply
->wparam
= queue
->exit_code
;
708 reply
->time
= get_tick_count();
710 if (flags
& PM_REMOVE
)
712 queue
->quit_message
= 0;
713 if (list_empty( &queue
->msg_list
[POST_MESSAGE
] ))
714 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
722 /* empty a message list and free all the messages */
723 static void empty_msg_list( struct list
*list
)
727 while ((ptr
= list_head( list
)) != NULL
)
729 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
730 list_remove( &msg
->entry
);
735 /* cleanup all pending results when deleting a queue */
736 static void cleanup_results( struct msg_queue
*queue
)
740 while ((entry
= list_head( &queue
->send_result
)) != NULL
)
742 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
745 while ((entry
= list_head( &queue
->callback_result
)) != NULL
)
747 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
750 while (queue
->recv_result
)
751 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
754 /* check if the thread owning the queue is hung (not checking for messages) */
755 static int is_queue_hung( struct msg_queue
*queue
)
757 struct wait_queue_entry
*entry
;
759 if (current_time
- queue
->last_get_msg
<= 5 * TICKS_PER_SEC
)
760 return 0; /* less than 5 seconds since last get message -> not hung */
762 LIST_FOR_EACH_ENTRY( entry
, &queue
->obj
.wait_queue
, struct wait_queue_entry
, entry
)
764 if (entry
->thread
->queue
== queue
)
765 return 0; /* thread is waiting on queue -> not hung */
770 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
772 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
773 struct process
*process
= entry
->thread
->process
;
775 /* a thread can only wait on its own queue */
776 if (entry
->thread
->queue
!= queue
)
778 set_error( STATUS_ACCESS_DENIED
);
781 /* if waiting on the main process queue, set the idle event */
782 if (process
->queue
== queue
)
784 if (process
->idle_event
) set_event( process
->idle_event
);
786 if (queue
->fd
&& list_empty( &obj
->wait_queue
)) /* first on the queue */
787 set_fd_events( queue
->fd
, POLLIN
);
788 add_queue( obj
, entry
);
792 static void msg_queue_remove_queue(struct object
*obj
, struct wait_queue_entry
*entry
)
794 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
795 struct process
*process
= entry
->thread
->process
;
797 remove_queue( obj
, entry
);
798 if (queue
->fd
&& list_empty( &obj
->wait_queue
)) /* last on the queue is gone */
799 set_fd_events( queue
->fd
, 0 );
801 assert( entry
->thread
->queue
== queue
);
803 /* if waiting on the main process queue, reset the idle event */
804 if (process
->queue
== queue
)
806 if (process
->idle_event
) reset_event( process
->idle_event
);
810 static void msg_queue_dump( struct object
*obj
, int verbose
)
812 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
813 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
814 queue
->wake_bits
, queue
->wake_mask
);
817 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
)
819 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
824 if ((ret
= check_fd_events( queue
->fd
, POLLIN
)))
825 /* stop waiting on select() if we are signaled */
826 set_fd_events( queue
->fd
, 0 );
827 else if (!list_empty( &obj
->wait_queue
))
828 /* restart waiting on poll() if we are no longer signaled */
829 set_fd_events( queue
->fd
, POLLIN
);
831 return ret
|| is_signaled( queue
);
834 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
)
836 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
837 queue
->wake_mask
= 0;
838 queue
->changed_mask
= 0;
839 return 0; /* Not abandoned */
842 static void msg_queue_destroy( struct object
*obj
)
844 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
848 cleanup_results( queue
);
849 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
851 while ((ptr
= list_head( &queue
->pending_timers
)))
853 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
854 list_remove( &timer
->entry
);
857 while ((ptr
= list_head( &queue
->expired_timers
)))
859 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
860 list_remove( &timer
->entry
);
863 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
864 if (queue
->input
) release_object( queue
->input
);
865 if (queue
->hooks
) release_object( queue
->hooks
);
866 if (queue
->fd
) release_object( queue
->fd
);
869 static void msg_queue_poll_event( struct fd
*fd
, int event
)
871 struct msg_queue
*queue
= get_fd_user( fd
);
872 assert( queue
->obj
.ops
== &msg_queue_ops
);
874 if (event
& (POLLERR
| POLLHUP
)) set_fd_events( fd
, -1 );
875 wake_up( &queue
->obj
, 0 );
878 static void thread_input_dump( struct object
*obj
, int verbose
)
880 struct thread_input
*input
= (struct thread_input
*)obj
;
881 fprintf( stderr
, "Thread input focus=%08x capture=%08x active=%08x\n",
882 input
->focus
, input
->capture
, input
->active
);
885 static void thread_input_destroy( struct object
*obj
)
887 struct thread_input
*input
= (struct thread_input
*)obj
;
889 if (foreground_input
== input
) foreground_input
= NULL
;
890 empty_msg_list( &input
->msg_list
);
891 if (input
->desktop
) release_object( input
->desktop
);
894 /* fix the thread input data when a window is destroyed */
895 static inline void thread_input_cleanup_window( struct msg_queue
*queue
, user_handle_t window
)
897 struct thread_input
*input
= queue
->input
;
899 if (window
== input
->focus
) input
->focus
= 0;
900 if (window
== input
->capture
) input
->capture
= 0;
901 if (window
== input
->active
) input
->active
= 0;
902 if (window
== input
->menu_owner
) input
->menu_owner
= 0;
903 if (window
== input
->move_size
) input
->move_size
= 0;
904 if (window
== input
->caret
) set_caret_window( input
, 0 );
907 /* check if the specified window can be set in the input data of a given queue */
908 static int check_queue_input_window( struct msg_queue
*queue
, user_handle_t window
)
910 struct thread
*thread
;
913 if (!window
) return 1; /* we can always clear the data */
915 if ((thread
= get_window_thread( window
)))
917 ret
= (queue
->input
== thread
->queue
->input
);
918 if (!ret
) set_error( STATUS_ACCESS_DENIED
);
919 release_object( thread
);
921 else set_error( STATUS_INVALID_HANDLE
);
926 /* make sure the specified thread has a queue */
927 int init_thread_queue( struct thread
*thread
)
929 if (thread
->queue
) return 1;
930 return (create_msg_queue( thread
, NULL
) != NULL
);
933 /* attach two thread input data structures */
934 int attach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
936 struct desktop
*desktop
;
937 struct thread_input
*input
;
939 if (!thread_to
->queue
&& !(thread_to
->queue
= create_msg_queue( thread_to
, NULL
))) return 0;
940 if (!(desktop
= get_thread_desktop( thread_from
, 0 ))) return 0;
941 input
= (struct thread_input
*)grab_object( thread_to
->queue
->input
);
942 if (input
->desktop
!= desktop
)
944 set_error( STATUS_ACCESS_DENIED
);
945 release_object( input
);
946 release_object( desktop
);
949 release_object( desktop
);
951 if (thread_from
->queue
)
953 release_thread_input( thread_from
);
954 thread_from
->queue
->input
= input
;
958 if (!(thread_from
->queue
= create_msg_queue( thread_from
, input
))) return 0;
960 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
964 /* detach two thread input data structures */
965 void detach_thread_input( struct thread
*thread_from
)
967 struct thread_input
*input
;
969 if ((input
= create_thread_input( thread_from
)))
971 release_thread_input( thread_from
);
972 thread_from
->queue
->input
= input
;
977 /* set the next timer to expire */
978 static void set_next_timer( struct msg_queue
*queue
)
984 remove_timeout_user( queue
->timeout
);
985 queue
->timeout
= NULL
;
987 if ((ptr
= list_head( &queue
->pending_timers
)))
989 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
990 queue
->timeout
= add_timeout_user( timer
->when
, timer_callback
, queue
);
992 /* set/clear QS_TIMER bit */
993 if (list_empty( &queue
->expired_timers
))
994 clear_queue_bits( queue
, QS_TIMER
);
996 set_queue_bits( queue
, QS_TIMER
);
999 /* find a timer from its window and id */
1000 static struct timer
*find_timer( struct msg_queue
*queue
, user_handle_t win
,
1001 unsigned int msg
, lparam_t id
)
1005 /* we need to search both lists */
1007 LIST_FOR_EACH( ptr
, &queue
->pending_timers
)
1009 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1010 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
1012 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
1014 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1015 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
1020 /* callback for the next timer expiration */
1021 static void timer_callback( void *private )
1023 struct msg_queue
*queue
= private;
1026 queue
->timeout
= NULL
;
1027 /* move on to the next timer */
1028 ptr
= list_head( &queue
->pending_timers
);
1030 list_add_tail( &queue
->expired_timers
, ptr
);
1031 set_next_timer( queue
);
1034 /* link a timer at its rightful place in the queue list */
1035 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
1039 for (ptr
= queue
->pending_timers
.next
; ptr
!= &queue
->pending_timers
; ptr
= ptr
->next
)
1041 struct timer
*t
= LIST_ENTRY( ptr
, struct timer
, entry
);
1042 if (t
->when
>= timer
->when
) break;
1044 list_add_before( ptr
, &timer
->entry
);
1047 /* remove a timer from the queue timer list and free it */
1048 static void free_timer( struct msg_queue
*queue
, struct timer
*timer
)
1050 list_remove( &timer
->entry
);
1052 set_next_timer( queue
);
1055 /* restart an expired timer */
1056 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
1058 list_remove( &timer
->entry
);
1059 while (timer
->when
<= current_time
) timer
->when
+= (timeout_t
)timer
->rate
* 10000;
1060 link_timer( queue
, timer
);
1061 set_next_timer( queue
);
1064 /* find an expired timer matching the filtering parameters */
1065 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
1066 unsigned int get_first
, unsigned int get_last
,
1071 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
1073 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1074 if (win
&& timer
->win
!= win
) continue;
1075 if (check_msg_filter( timer
->msg
, get_first
, get_last
))
1077 if (remove
) restart_timer( queue
, timer
);
1085 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
1087 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
1090 timer
->rate
= max( rate
, 1 );
1091 timer
->when
= current_time
+ (timeout_t
)timer
->rate
* 10000;
1092 link_timer( queue
, timer
);
1093 /* check if we replaced the next timer */
1094 if (list_head( &queue
->pending_timers
) == &timer
->entry
) set_next_timer( queue
);
1099 /* change the input key state for a given key */
1100 static void set_input_key_state( struct thread_input
*input
, unsigned char key
, int down
)
1104 if (!(input
->keystate
[key
] & 0x80)) input
->keystate
[key
] ^= 0x01;
1105 input
->keystate
[key
] |= 0x80;
1107 else input
->keystate
[key
] &= ~0x80;
1110 /* update the input key state for a keyboard message */
1111 static void update_input_key_state( struct thread_input
*input
, const struct message
*msg
)
1118 case WM_LBUTTONDOWN
:
1122 set_input_key_state( input
, VK_LBUTTON
, down
);
1124 case WM_MBUTTONDOWN
:
1128 set_input_key_state( input
, VK_MBUTTON
, down
);
1130 case WM_RBUTTONDOWN
:
1134 set_input_key_state( input
, VK_RBUTTON
, down
);
1136 case WM_XBUTTONDOWN
:
1140 if (msg
->wparam
== XBUTTON1
) set_input_key_state( input
, VK_XBUTTON1
, down
);
1141 else if (msg
->wparam
== XBUTTON2
) set_input_key_state( input
, VK_XBUTTON2
, down
);
1149 key
= (unsigned char)msg
->wparam
;
1150 set_input_key_state( input
, key
, down
);
1155 down
= (input
->keystate
[VK_LCONTROL
] | input
->keystate
[VK_RCONTROL
]) & 0x80;
1156 set_input_key_state( input
, VK_CONTROL
, down
);
1160 down
= (input
->keystate
[VK_LMENU
] | input
->keystate
[VK_RMENU
]) & 0x80;
1161 set_input_key_state( input
, VK_MENU
, down
);
1165 down
= (input
->keystate
[VK_LSHIFT
] | input
->keystate
[VK_RSHIFT
]) & 0x80;
1166 set_input_key_state( input
, VK_SHIFT
, down
);
1173 /* release the hardware message currently being processed by the given thread */
1174 static void release_hardware_message( struct msg_queue
*queue
, unsigned int hw_id
,
1175 int remove
, user_handle_t new_win
)
1177 struct thread_input
*input
= queue
->input
;
1178 struct message
*msg
;
1180 LIST_FOR_EACH_ENTRY( msg
, &input
->msg_list
, struct message
, entry
)
1182 if (msg
->unique_id
== hw_id
) break;
1184 if (&msg
->entry
== &input
->msg_list
) return; /* not found */
1186 /* clear the queue bit for that message */
1187 if (remove
|| new_win
)
1189 struct message
*other
;
1192 clr_bit
= get_hardware_msg_bit( msg
);
1193 LIST_FOR_EACH_ENTRY( other
, &input
->msg_list
, struct message
, entry
)
1195 if (other
!= msg
&& get_hardware_msg_bit( other
) == clr_bit
)
1201 if (clr_bit
) clear_queue_bits( queue
, clr_bit
);
1204 if (new_win
) /* set the new window */
1206 struct thread
*owner
= get_window_thread( new_win
);
1210 if (owner
->queue
->input
!= input
)
1212 list_remove( &msg
->entry
);
1213 if (msg
->msg
== WM_MOUSEMOVE
&& merge_message( owner
->queue
->input
, msg
))
1215 free_message( msg
);
1216 release_object( owner
);
1219 list_add_tail( &owner
->queue
->input
->msg_list
, &msg
->entry
);
1221 set_queue_bits( owner
->queue
, get_hardware_msg_bit( msg
));
1223 release_object( owner
);
1228 update_input_key_state( input
, msg
);
1229 list_remove( &msg
->entry
);
1230 free_message( msg
);
1234 /* find the window that should receive a given hardware message */
1235 static user_handle_t
find_hardware_message_window( struct thread_input
*input
, struct message
*msg
,
1236 struct hardware_msg_data
*data
, unsigned int *msg_code
)
1238 user_handle_t win
= 0;
1240 *msg_code
= msg
->msg
;
1241 if (is_keyboard_msg( msg
))
1243 if (input
&& !(win
= input
->focus
))
1245 win
= input
->active
;
1246 if (*msg_code
< WM_SYSKEYDOWN
) *msg_code
+= WM_SYSKEYDOWN
- WM_KEYDOWN
;
1249 else /* mouse message */
1251 if (!input
|| !(win
= input
->capture
))
1253 if (!(win
= msg
->win
) || !is_window_visible( win
))
1255 if (input
) win
= window_from_point( input
->desktop
, data
->x
, data
->y
);
1262 /* queue a hardware message into a given thread input */
1263 static void queue_hardware_message( struct msg_queue
*queue
, struct message
*msg
,
1264 struct hardware_msg_data
*data
)
1267 struct thread
*thread
;
1268 struct thread_input
*input
= queue
? queue
->input
: foreground_input
;
1269 unsigned int msg_code
;
1271 last_input_time
= get_tick_count();
1272 win
= find_hardware_message_window( input
, msg
, data
, &msg_code
);
1273 if (!win
|| !(thread
= get_window_thread(win
)))
1275 if (input
) update_input_key_state( input
, msg
);
1279 input
= thread
->queue
->input
;
1281 if (msg
->msg
== WM_MOUSEMOVE
&& merge_message( input
, msg
)) free( msg
);
1284 msg
->unique_id
= 0; /* will be set once we return it to the app */
1285 list_add_tail( &input
->msg_list
, &msg
->entry
);
1286 set_queue_bits( thread
->queue
, get_hardware_msg_bit(msg
) );
1288 release_object( thread
);
1291 /* check message filter for a hardware message */
1292 static int check_hw_message_filter( user_handle_t win
, unsigned int msg_code
,
1293 user_handle_t filter_win
, unsigned int first
, unsigned int last
)
1295 if (msg_code
>= WM_KEYFIRST
&& msg_code
<= WM_KEYLAST
)
1297 /* we can only test the window for a keyboard message since the
1298 * dest window for a mouse message depends on hittest */
1299 if (filter_win
&& win
!= filter_win
&& !is_child_window( filter_win
, win
))
1301 /* the message code is final for a keyboard message, we can simply check it */
1302 return check_msg_filter( msg_code
, first
, last
);
1304 else /* mouse message */
1306 /* we need to check all possible values that the message can have in the end */
1308 if (check_msg_filter( msg_code
, first
, last
)) return 1;
1309 if (msg_code
== WM_MOUSEWHEEL
) return 0; /* no other possible value for this one */
1311 /* all other messages can become non-client messages */
1312 if (check_msg_filter( msg_code
+ (WM_NCMOUSEFIRST
- WM_MOUSEFIRST
), first
, last
)) return 1;
1314 /* clicks can become double-clicks or non-client double-clicks */
1315 if (msg_code
== WM_LBUTTONDOWN
|| msg_code
== WM_MBUTTONDOWN
||
1316 msg_code
== WM_RBUTTONDOWN
|| msg_code
== WM_XBUTTONDOWN
)
1318 if (check_msg_filter( msg_code
+ (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1319 if (check_msg_filter( msg_code
+ (WM_NCLBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1326 /* find a hardware message for the given queue */
1327 static int get_hardware_message( struct thread
*thread
, unsigned int hw_id
, user_handle_t filter_win
,
1328 unsigned int first
, unsigned int last
, struct get_message_reply
*reply
)
1330 struct thread_input
*input
= thread
->queue
->input
;
1331 struct thread
*win_thread
;
1334 int clear_bits
, got_one
= 0;
1335 unsigned int msg_code
;
1337 ptr
= list_head( &input
->msg_list
);
1342 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1343 if (msg
->unique_id
== hw_id
) break;
1344 ptr
= list_next( &input
->msg_list
, ptr
);
1346 if (!ptr
) ptr
= list_head( &input
->msg_list
);
1347 else ptr
= list_next( &input
->msg_list
, ptr
); /* start from the next one */
1350 if (ptr
== list_head( &input
->msg_list
))
1351 clear_bits
= QS_KEY
| QS_MOUSEMOVE
| QS_MOUSEBUTTON
;
1353 clear_bits
= 0; /* don't clear bits if we don't go through the whole list */
1357 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1358 struct hardware_msg_data
*data
= msg
->data
;
1360 ptr
= list_next( &input
->msg_list
, ptr
);
1361 win
= find_hardware_message_window( input
, msg
, data
, &msg_code
);
1362 if (!win
|| !(win_thread
= get_window_thread( win
)))
1364 /* no window at all, remove it */
1365 update_input_key_state( input
, msg
);
1366 list_remove( &msg
->entry
);
1367 free_message( msg
);
1370 if (win_thread
!= thread
)
1372 if (win_thread
->queue
->input
== input
)
1374 /* wake the other thread */
1375 set_queue_bits( win_thread
->queue
, get_hardware_msg_bit(msg
) );
1380 /* for another thread input, drop it */
1381 update_input_key_state( input
, msg
);
1382 list_remove( &msg
->entry
);
1383 free_message( msg
);
1385 release_object( win_thread
);
1388 release_object( win_thread
);
1390 /* if we already got a message for another thread, or if it doesn't
1391 * match the filter we skip it */
1392 if (got_one
|| !check_hw_message_filter( win
, msg_code
, filter_win
, first
, last
))
1394 clear_bits
&= ~get_hardware_msg_bit( msg
);
1397 /* now we can return it */
1398 if (!msg
->unique_id
) msg
->unique_id
= get_unique_id();
1399 reply
->type
= MSG_HARDWARE
;
1401 reply
->msg
= msg_code
;
1402 reply
->wparam
= msg
->wparam
;
1403 reply
->lparam
= msg
->lparam
;
1404 reply
->time
= msg
->time
;
1406 data
->hw_id
= msg
->unique_id
;
1407 set_reply_data( msg
->data
, msg
->data_size
);
1410 /* nothing found, clear the hardware queue bits */
1411 clear_queue_bits( thread
->queue
, clear_bits
);
1415 /* increment (or decrement if 'incr' is negative) the queue paint count */
1416 void inc_queue_paint_count( struct thread
*thread
, int incr
)
1418 struct msg_queue
*queue
= thread
->queue
;
1422 if ((queue
->paint_count
+= incr
) < 0) queue
->paint_count
= 0;
1424 if (queue
->paint_count
)
1425 set_queue_bits( queue
, QS_PAINT
);
1427 clear_queue_bits( queue
, QS_PAINT
);
1431 /* remove all messages and timers belonging to a certain window */
1432 void queue_cleanup_window( struct thread
*thread
, user_handle_t win
)
1434 struct msg_queue
*queue
= thread
->queue
;
1442 ptr
= list_head( &queue
->pending_timers
);
1445 struct list
*next
= list_next( &queue
->pending_timers
, ptr
);
1446 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1447 if (timer
->win
== win
) free_timer( queue
, timer
);
1450 ptr
= list_head( &queue
->expired_timers
);
1453 struct list
*next
= list_next( &queue
->expired_timers
, ptr
);
1454 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1455 if (timer
->win
== win
) free_timer( queue
, timer
);
1459 /* remove messages */
1460 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
1462 struct list
*ptr
, *next
;
1464 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->msg_list
[i
] )
1466 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1467 if (msg
->win
== win
) remove_queue_message( queue
, msg
, i
);
1471 thread_input_cleanup_window( queue
, win
);
1474 /* post a message to a window; used by socket handling */
1475 void post_message( user_handle_t win
, unsigned int message
, lparam_t wparam
, lparam_t lparam
)
1477 struct message
*msg
;
1478 struct thread
*thread
= get_window_thread( win
);
1480 if (!thread
) return;
1482 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1484 msg
->type
= MSG_POSTED
;
1485 msg
->win
= get_user_full_handle( win
);
1487 msg
->wparam
= wparam
;
1488 msg
->lparam
= lparam
;
1489 msg
->time
= get_tick_count();
1494 list_add_tail( &thread
->queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1495 set_queue_bits( thread
->queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
1497 release_object( thread
);
1500 /* post a win event */
1501 void post_win_event( struct thread
*thread
, unsigned int event
,
1502 user_handle_t win
, unsigned int object_id
,
1503 unsigned int child_id
, client_ptr_t hook_proc
,
1504 const WCHAR
*module
, data_size_t module_size
,
1507 struct message
*msg
;
1509 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1511 struct winevent_msg_data
*data
;
1513 msg
->type
= MSG_WINEVENT
;
1514 msg
->win
= get_user_full_handle( win
);
1516 msg
->wparam
= object_id
;
1517 msg
->lparam
= child_id
;
1518 msg
->time
= get_tick_count();
1521 if ((data
= malloc( sizeof(*data
) + module_size
)))
1524 data
->tid
= get_thread_id( current
);
1525 data
->hook_proc
= hook_proc
;
1526 memcpy( data
+ 1, module
, module_size
);
1529 msg
->data_size
= sizeof(*data
) + module_size
;
1531 if (debug_level
> 1)
1532 fprintf( stderr
, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
1533 get_thread_id(thread
), event
, win
, object_id
, child_id
);
1534 list_add_tail( &thread
->queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1535 set_queue_bits( thread
->queue
, QS_SENDMESSAGE
);
1543 /* check if the thread owning the window is hung */
1544 DECL_HANDLER(is_window_hung
)
1546 struct thread
*thread
;
1548 thread
= get_window_thread( req
->win
);
1551 reply
->is_hung
= is_queue_hung( thread
->queue
);
1552 release_object( thread
);
1554 else reply
->is_hung
= 0;
1558 /* get the message queue of the current thread */
1559 DECL_HANDLER(get_msg_queue
)
1561 struct msg_queue
*queue
= get_current_queue();
1564 if (queue
) reply
->handle
= alloc_handle( current
->process
, queue
, SYNCHRONIZE
, 0 );
1568 /* set the file descriptor associated to the current thread queue */
1569 DECL_HANDLER(set_queue_fd
)
1571 struct msg_queue
*queue
= get_current_queue();
1575 if (queue
->fd
) /* fd can only be set once */
1577 set_error( STATUS_ACCESS_DENIED
);
1580 if (!(file
= get_file_obj( current
->process
, req
->handle
, SYNCHRONIZE
))) return;
1582 if ((unix_fd
= get_file_unix_fd( file
)) != -1)
1584 if ((unix_fd
= dup( unix_fd
)) != -1)
1585 queue
->fd
= create_anonymous_fd( &msg_queue_fd_ops
, unix_fd
, &queue
->obj
, 0 );
1589 release_object( file
);
1593 /* set the current message queue wakeup mask */
1594 DECL_HANDLER(set_queue_mask
)
1596 struct msg_queue
*queue
= get_current_queue();
1600 queue
->wake_mask
= req
->wake_mask
;
1601 queue
->changed_mask
= req
->changed_mask
;
1602 reply
->wake_bits
= queue
->wake_bits
;
1603 reply
->changed_bits
= queue
->changed_bits
;
1604 if (is_signaled( queue
))
1606 /* if skip wait is set, do what would have been done in the subsequent wait */
1607 if (req
->skip_wait
) msg_queue_satisfied( &queue
->obj
, current
);
1608 else wake_up( &queue
->obj
, 0 );
1614 /* get the current message queue status */
1615 DECL_HANDLER(get_queue_status
)
1617 struct msg_queue
*queue
= current
->queue
;
1620 reply
->wake_bits
= queue
->wake_bits
;
1621 reply
->changed_bits
= queue
->changed_bits
;
1622 if (req
->clear
) queue
->changed_bits
= 0;
1624 else reply
->wake_bits
= reply
->changed_bits
= 0;
1628 /* send a message to a thread queue */
1629 DECL_HANDLER(send_message
)
1631 struct message
*msg
;
1632 struct msg_queue
*send_queue
= get_current_queue();
1633 struct msg_queue
*recv_queue
= NULL
;
1634 struct thread
*thread
= NULL
;
1636 if (!(thread
= get_thread_from_id( req
->id
))) return;
1638 if (!(recv_queue
= thread
->queue
))
1640 set_error( STATUS_INVALID_PARAMETER
);
1641 release_object( thread
);
1644 if ((req
->flags
& SEND_MSG_ABORT_IF_HUNG
) && is_queue_hung(recv_queue
))
1646 set_error( STATUS_TIMEOUT
);
1647 release_object( thread
);
1651 if ((msg
= mem_alloc( sizeof(*msg
) )))
1653 msg
->type
= req
->type
;
1654 msg
->win
= get_user_full_handle( req
->win
);
1655 msg
->msg
= req
->msg
;
1656 msg
->wparam
= req
->wparam
;
1657 msg
->lparam
= req
->lparam
;
1658 msg
->time
= get_tick_count();
1661 msg
->data_size
= get_req_data_size();
1663 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1666 release_object( thread
);
1672 case MSG_OTHER_PROCESS
:
1676 if (!(msg
->result
= alloc_message_result( send_queue
, recv_queue
, msg
, req
->timeout
)))
1678 free_message( msg
);
1683 list_add_tail( &recv_queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1684 set_queue_bits( recv_queue
, QS_SENDMESSAGE
);
1687 list_add_tail( &recv_queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1688 set_queue_bits( recv_queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
1690 case MSG_HARDWARE
: /* should use send_hardware_message instead */
1691 case MSG_CALLBACK_RESULT
: /* cannot send this one */
1693 set_error( STATUS_INVALID_PARAMETER
);
1698 release_object( thread
);
1701 /* send a hardware message to a thread queue */
1702 DECL_HANDLER(send_hardware_message
)
1704 struct message
*msg
;
1705 struct msg_queue
*recv_queue
= NULL
;
1706 struct thread
*thread
= NULL
;
1707 struct hardware_msg_data
*data
;
1711 if (!(thread
= get_thread_from_id( req
->id
))) return;
1714 if (thread
&& !(recv_queue
= thread
->queue
))
1716 set_error( STATUS_INVALID_PARAMETER
);
1717 release_object( thread
);
1721 if (!(data
= mem_alloc( sizeof(*data
) )))
1723 release_object( thread
);
1726 memset( data
, 0, sizeof(*data
) );
1729 data
->info
= req
->info
;
1731 if ((msg
= mem_alloc( sizeof(*msg
) )))
1733 msg
->type
= MSG_HARDWARE
;
1734 msg
->win
= get_user_full_handle( req
->win
);
1735 msg
->msg
= req
->msg
;
1736 msg
->wparam
= req
->wparam
;
1737 msg
->lparam
= req
->lparam
;
1738 msg
->time
= req
->time
;
1741 msg
->data_size
= sizeof(*data
);
1742 queue_hardware_message( recv_queue
, msg
, data
);
1746 if (thread
) release_object( thread
);
1749 /* post a quit message to the current queue */
1750 DECL_HANDLER(post_quit_message
)
1752 struct msg_queue
*queue
= get_current_queue();
1757 queue
->quit_message
= 1;
1758 queue
->exit_code
= req
->exit_code
;
1759 set_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
1762 /* get a message from the current queue */
1763 DECL_HANDLER(get_message
)
1765 struct timer
*timer
;
1767 struct msg_queue
*queue
= get_current_queue();
1768 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
1769 unsigned int filter
= req
->flags
>> 16;
1771 reply
->active_hooks
= get_active_hooks();
1774 queue
->last_get_msg
= current_time
;
1775 if (!filter
) filter
= QS_ALLINPUT
;
1777 /* first check for sent messages */
1778 if ((ptr
= list_head( &queue
->msg_list
[SEND_MESSAGE
] )))
1780 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1781 receive_message( queue
, msg
, reply
);
1785 /* clear changed bits so we can wait on them if we don't find a message */
1786 if (filter
& QS_POSTMESSAGE
)
1788 queue
->changed_bits
&= ~(QS_POSTMESSAGE
| QS_HOTKEY
| QS_TIMER
);
1789 if (req
->get_first
== 0 && req
->get_last
== ~0U) queue
->changed_bits
&= ~QS_ALLPOSTMESSAGE
;
1791 if (filter
& QS_INPUT
) queue
->changed_bits
&= ~QS_INPUT
;
1792 if (filter
& QS_PAINT
) queue
->changed_bits
&= ~QS_PAINT
;
1794 /* then check for posted messages */
1795 if ((filter
& QS_POSTMESSAGE
) &&
1796 get_posted_message( queue
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
1799 /* only check for quit messages if not posted messages pending.
1800 * note: the quit message isn't filtered */
1801 if (get_quit_message( queue
, req
->flags
, reply
))
1804 /* then check for any raw hardware message */
1805 if ((filter
& QS_INPUT
) &&
1806 filter_contains_hw_range( req
->get_first
, req
->get_last
) &&
1807 get_hardware_message( current
, req
->hw_id
, get_win
, req
->get_first
, req
->get_last
, reply
))
1810 /* now check for WM_PAINT */
1811 if ((filter
& QS_PAINT
) &&
1812 queue
->paint_count
&&
1813 check_msg_filter( WM_PAINT
, req
->get_first
, req
->get_last
) &&
1814 (reply
->win
= find_window_to_repaint( get_win
, current
)))
1816 reply
->type
= MSG_POSTED
;
1817 reply
->msg
= WM_PAINT
;
1820 reply
->time
= get_tick_count();
1824 /* now check for timer */
1825 if ((filter
& QS_TIMER
) &&
1826 (timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
1827 req
->get_last
, (req
->flags
& PM_REMOVE
) )))
1829 reply
->type
= MSG_POSTED
;
1830 reply
->win
= timer
->win
;
1831 reply
->msg
= timer
->msg
;
1832 reply
->wparam
= timer
->id
;
1833 reply
->lparam
= timer
->lparam
;
1834 reply
->time
= get_tick_count();
1838 queue
->wake_mask
= req
->wake_mask
;
1839 queue
->changed_mask
= req
->changed_mask
;
1840 set_error( STATUS_PENDING
); /* FIXME */
1844 /* reply to a sent message */
1845 DECL_HANDLER(reply_message
)
1847 if (!current
->queue
) set_error( STATUS_ACCESS_DENIED
);
1848 else if (current
->queue
->recv_result
)
1849 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
1850 get_req_data(), get_req_data_size() );
1854 /* accept the current hardware message */
1855 DECL_HANDLER(accept_hardware_message
)
1858 release_hardware_message( current
->queue
, req
->hw_id
, req
->remove
, req
->new_win
);
1860 set_error( STATUS_ACCESS_DENIED
);
1864 /* retrieve the reply for the last message sent */
1865 DECL_HANDLER(get_message_reply
)
1867 struct message_result
*result
;
1869 struct msg_queue
*queue
= current
->queue
;
1873 set_error( STATUS_PENDING
);
1876 if (!(entry
= list_head( &queue
->send_result
))) return; /* no reply ready */
1878 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1879 if (result
->replied
|| req
->cancel
)
1881 if (result
->replied
)
1883 reply
->result
= result
->result
;
1884 set_error( result
->error
);
1887 data_size_t data_len
= min( result
->data_size
, get_reply_max_size() );
1888 set_reply_data_ptr( result
->data
, data_len
);
1889 result
->data
= NULL
;
1890 result
->data_size
= 0;
1893 remove_result_from_sender( result
);
1895 entry
= list_head( &queue
->send_result
);
1896 if (!entry
) clear_queue_bits( queue
, QS_SMRESULT
);
1899 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1900 if (!result
->replied
) clear_queue_bits( queue
, QS_SMRESULT
);
1904 else set_error( STATUS_ACCESS_DENIED
);
1908 /* set a window timer */
1909 DECL_HANDLER(set_win_timer
)
1911 struct timer
*timer
;
1912 struct msg_queue
*queue
;
1913 struct thread
*thread
= NULL
;
1914 user_handle_t win
= 0;
1915 lparam_t id
= req
->id
;
1919 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1921 set_error( STATUS_INVALID_HANDLE
);
1924 if (thread
->process
!= current
->process
)
1926 release_object( thread
);
1927 set_error( STATUS_ACCESS_DENIED
);
1930 queue
= thread
->queue
;
1931 /* remove it if it existed already */
1932 if ((timer
= find_timer( queue
, win
, req
->msg
, id
))) free_timer( queue
, timer
);
1936 queue
= get_current_queue();
1937 /* look for a timer with this id */
1938 if (id
&& (timer
= find_timer( queue
, 0, req
->msg
, id
)))
1940 /* free and reuse id */
1941 free_timer( queue
, timer
);
1945 /* find a free id for it */
1948 id
= queue
->next_timer_id
;
1949 if (--queue
->next_timer_id
<= 0x100) queue
->next_timer_id
= 0x7fff;
1951 while (find_timer( queue
, 0, req
->msg
, id
));
1955 if ((timer
= set_timer( queue
, req
->rate
)))
1958 timer
->msg
= req
->msg
;
1960 timer
->lparam
= req
->lparam
;
1963 if (thread
) release_object( thread
);
1966 /* kill a window timer */
1967 DECL_HANDLER(kill_win_timer
)
1969 struct timer
*timer
;
1970 struct thread
*thread
;
1971 user_handle_t win
= 0;
1975 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1977 set_error( STATUS_INVALID_HANDLE
);
1980 if (thread
->process
!= current
->process
)
1982 release_object( thread
);
1983 set_error( STATUS_ACCESS_DENIED
);
1987 else thread
= (struct thread
*)grab_object( current
);
1989 if (thread
->queue
&& (timer
= find_timer( thread
->queue
, win
, req
->msg
, req
->id
)))
1990 free_timer( thread
->queue
, timer
);
1992 set_error( STATUS_INVALID_PARAMETER
);
1994 release_object( thread
);
1998 /* attach (or detach) thread inputs */
1999 DECL_HANDLER(attach_thread_input
)
2001 struct thread
*thread_from
= get_thread_from_id( req
->tid_from
);
2002 struct thread
*thread_to
= get_thread_from_id( req
->tid_to
);
2004 if (!thread_from
|| !thread_to
)
2006 if (thread_from
) release_object( thread_from
);
2007 if (thread_to
) release_object( thread_to
);
2010 if (thread_from
!= thread_to
)
2012 if (req
->attach
) attach_thread_input( thread_from
, thread_to
);
2015 if (thread_from
->queue
&& thread_to
->queue
&&
2016 thread_from
->queue
->input
== thread_to
->queue
->input
)
2017 detach_thread_input( thread_from
);
2019 set_error( STATUS_ACCESS_DENIED
);
2022 else set_error( STATUS_ACCESS_DENIED
);
2023 release_object( thread_from
);
2024 release_object( thread_to
);
2028 /* get thread input data */
2029 DECL_HANDLER(get_thread_input
)
2031 struct thread
*thread
= NULL
;
2032 struct thread_input
*input
;
2036 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2037 input
= thread
->queue
? thread
->queue
->input
: NULL
;
2039 else input
= foreground_input
; /* get the foreground thread info */
2043 reply
->focus
= input
->focus
;
2044 reply
->capture
= input
->capture
;
2045 reply
->active
= input
->active
;
2046 reply
->menu_owner
= input
->menu_owner
;
2047 reply
->move_size
= input
->move_size
;
2048 reply
->caret
= input
->caret
;
2049 reply
->rect
= input
->caret_rect
;
2056 reply
->menu_owner
= 0;
2057 reply
->move_size
= 0;
2059 reply
->rect
.left
= reply
->rect
.top
= reply
->rect
.right
= reply
->rect
.bottom
= 0;
2061 /* foreground window is active window of foreground thread */
2062 reply
->foreground
= foreground_input
? foreground_input
->active
: 0;
2063 if (thread
) release_object( thread
);
2067 /* retrieve queue keyboard state for a given thread */
2068 DECL_HANDLER(get_key_state
)
2070 struct thread
*thread
;
2071 struct thread_input
*input
;
2073 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2074 input
= thread
->queue
? thread
->queue
->input
: NULL
;
2077 if (req
->key
>= 0) reply
->state
= input
->keystate
[req
->key
& 0xff];
2078 set_reply_data( input
->keystate
, min( get_reply_max_size(), sizeof(input
->keystate
) ));
2080 release_object( thread
);
2084 /* set queue keyboard state for a given thread */
2085 DECL_HANDLER(set_key_state
)
2087 struct thread
*thread
= NULL
;
2088 struct thread_input
*input
;
2090 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2091 input
= thread
->queue
? thread
->queue
->input
: NULL
;
2094 data_size_t size
= min( sizeof(input
->keystate
), get_req_data_size() );
2095 if (size
) memcpy( input
->keystate
, get_req_data(), size
);
2097 release_object( thread
);
2101 /* set the system foreground window */
2102 DECL_HANDLER(set_foreground_window
)
2104 struct thread
*thread
;
2105 struct msg_queue
*queue
= get_current_queue();
2107 reply
->previous
= foreground_input
? foreground_input
->active
: 0;
2108 reply
->send_msg_old
= (reply
->previous
&& foreground_input
!= queue
->input
);
2109 reply
->send_msg_new
= FALSE
;
2111 if (is_top_level_window( req
->handle
) &&
2112 ((thread
= get_window_thread( req
->handle
))))
2114 foreground_input
= thread
->queue
->input
;
2115 reply
->send_msg_new
= (foreground_input
!= queue
->input
);
2116 release_object( thread
);
2118 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE
);
2122 /* set the current thread focus window */
2123 DECL_HANDLER(set_focus_window
)
2125 struct msg_queue
*queue
= get_current_queue();
2127 reply
->previous
= 0;
2128 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2130 reply
->previous
= queue
->input
->focus
;
2131 queue
->input
->focus
= get_user_full_handle( req
->handle
);
2136 /* set the current thread active window */
2137 DECL_HANDLER(set_active_window
)
2139 struct msg_queue
*queue
= get_current_queue();
2141 reply
->previous
= 0;
2142 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2144 if (!req
->handle
|| make_window_active( req
->handle
))
2146 reply
->previous
= queue
->input
->active
;
2147 queue
->input
->active
= get_user_full_handle( req
->handle
);
2149 else set_error( STATUS_INVALID_HANDLE
);
2154 /* set the current thread capture window */
2155 DECL_HANDLER(set_capture_window
)
2157 struct msg_queue
*queue
= get_current_queue();
2159 reply
->previous
= reply
->full_handle
= 0;
2160 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2162 struct thread_input
*input
= queue
->input
;
2164 reply
->previous
= input
->capture
;
2165 input
->capture
= get_user_full_handle( req
->handle
);
2166 input
->menu_owner
= (req
->flags
& CAPTURE_MENU
) ? input
->capture
: 0;
2167 input
->move_size
= (req
->flags
& CAPTURE_MOVESIZE
) ? input
->capture
: 0;
2168 reply
->full_handle
= input
->capture
;
2173 /* Set the current thread caret window */
2174 DECL_HANDLER(set_caret_window
)
2176 struct msg_queue
*queue
= get_current_queue();
2178 reply
->previous
= 0;
2179 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2181 struct thread_input
*input
= queue
->input
;
2183 reply
->previous
= input
->caret
;
2184 reply
->old_rect
= input
->caret_rect
;
2185 reply
->old_hide
= input
->caret_hide
;
2186 reply
->old_state
= input
->caret_state
;
2188 set_caret_window( input
, get_user_full_handle(req
->handle
) );
2189 input
->caret_rect
.right
= input
->caret_rect
.left
+ req
->width
;
2190 input
->caret_rect
.bottom
= input
->caret_rect
.top
+ req
->height
;
2195 /* Set the current thread caret information */
2196 DECL_HANDLER(set_caret_info
)
2198 struct msg_queue
*queue
= get_current_queue();
2199 struct thread_input
*input
;
2202 input
= queue
->input
;
2203 reply
->full_handle
= input
->caret
;
2204 reply
->old_rect
= input
->caret_rect
;
2205 reply
->old_hide
= input
->caret_hide
;
2206 reply
->old_state
= input
->caret_state
;
2208 if (req
->handle
&& get_user_full_handle(req
->handle
) != input
->caret
)
2210 set_error( STATUS_ACCESS_DENIED
);
2213 if (req
->flags
& SET_CARET_POS
)
2215 input
->caret_rect
.right
+= req
->x
- input
->caret_rect
.left
;
2216 input
->caret_rect
.bottom
+= req
->y
- input
->caret_rect
.top
;
2217 input
->caret_rect
.left
= req
->x
;
2218 input
->caret_rect
.top
= req
->y
;
2220 if (req
->flags
& SET_CARET_HIDE
)
2222 input
->caret_hide
+= req
->hide
;
2223 if (input
->caret_hide
< 0) input
->caret_hide
= 0;
2225 if (req
->flags
& SET_CARET_STATE
)
2227 if (req
->state
== -1) input
->caret_state
= !input
->caret_state
;
2228 else input
->caret_state
= !!req
->state
;
2233 /* get the time of the last input event */
2234 DECL_HANDLER(get_last_input_time
)
2236 reply
->time
= last_input_time
;