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 user_handle_t cursor
; /* current cursor */
107 int cursor_count
; /* cursor show count */
108 struct list msg_list
; /* list of hardware messages */
109 unsigned char keystate
[256]; /* state of each key */
114 struct object obj
; /* object header */
115 struct fd
*fd
; /* optional file descriptor to poll */
116 unsigned int wake_bits
; /* wakeup bits */
117 unsigned int wake_mask
; /* wakeup mask */
118 unsigned int changed_bits
; /* changed wakeup bits */
119 unsigned int changed_mask
; /* changed wakeup mask */
120 int paint_count
; /* pending paint messages count */
121 int quit_message
; /* is there a pending quit message? */
122 int exit_code
; /* exit code of pending quit message */
123 int cursor_count
; /* per-queue cursor show count */
124 struct list msg_list
[NB_MSG_KINDS
]; /* lists of messages */
125 struct list send_result
; /* stack of sent messages waiting for result */
126 struct list callback_result
; /* list of callback messages waiting for result */
127 struct message_result
*recv_result
; /* stack of received messages waiting for result */
128 struct list pending_timers
; /* list of pending timers */
129 struct list expired_timers
; /* list of expired timers */
130 lparam_t next_timer_id
; /* id for the next timer with a 0 window */
131 struct timeout_user
*timeout
; /* timeout for next timer to expire */
132 struct thread_input
*input
; /* thread input descriptor */
133 struct hook_table
*hooks
; /* hook table */
134 timeout_t last_get_msg
; /* time of last get message call */
137 static void msg_queue_dump( struct object
*obj
, int verbose
);
138 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
139 static void msg_queue_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
140 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
);
141 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
);
142 static void msg_queue_destroy( struct object
*obj
);
143 static void msg_queue_poll_event( struct fd
*fd
, int event
);
144 static void thread_input_dump( struct object
*obj
, int verbose
);
145 static void thread_input_destroy( struct object
*obj
);
146 static void timer_callback( void *private );
148 static const struct object_ops msg_queue_ops
=
150 sizeof(struct msg_queue
), /* size */
151 msg_queue_dump
, /* dump */
152 no_get_type
, /* get_type */
153 msg_queue_add_queue
, /* add_queue */
154 msg_queue_remove_queue
, /* remove_queue */
155 msg_queue_signaled
, /* signaled */
156 msg_queue_satisfied
, /* satisfied */
157 no_signal
, /* signal */
158 no_get_fd
, /* get_fd */
159 no_map_access
, /* map_access */
160 default_get_sd
, /* get_sd */
161 default_set_sd
, /* set_sd */
162 no_lookup_name
, /* lookup_name */
163 no_open_file
, /* open_file */
164 no_close_handle
, /* close_handle */
165 msg_queue_destroy
/* destroy */
168 static const struct fd_ops msg_queue_fd_ops
=
170 NULL
, /* get_poll_events */
171 msg_queue_poll_event
, /* poll_event */
173 NULL
, /* get_fd_type */
174 NULL
, /* removable */
176 NULL
, /* queue_async */
177 NULL
, /* async_event */
178 NULL
, /* async_terminated */
179 NULL
/* cancel async */
183 static const struct object_ops thread_input_ops
=
185 sizeof(struct thread_input
), /* size */
186 thread_input_dump
, /* dump */
187 no_get_type
, /* get_type */
188 no_add_queue
, /* add_queue */
189 NULL
, /* remove_queue */
191 NULL
, /* satisfied */
192 no_signal
, /* signal */
193 no_get_fd
, /* get_fd */
194 no_map_access
, /* map_access */
195 default_get_sd
, /* get_sd */
196 default_set_sd
, /* set_sd */
197 no_lookup_name
, /* lookup_name */
198 no_open_file
, /* open_file */
199 no_close_handle
, /* close_handle */
200 thread_input_destroy
/* destroy */
203 /* pointer to input structure of foreground thread */
204 static struct thread_input
*foreground_input
;
205 static unsigned int last_input_time
;
207 static void free_message( struct message
*msg
);
209 /* set the caret window in a given thread input */
210 static void set_caret_window( struct thread_input
*input
, user_handle_t win
)
212 if (!win
|| win
!= input
->caret
)
214 input
->caret_rect
.left
= 0;
215 input
->caret_rect
.top
= 0;
216 input
->caret_rect
.right
= 0;
217 input
->caret_rect
.bottom
= 0;
220 input
->caret_hide
= 1;
221 input
->caret_state
= 0;
224 /* create a thread input object */
225 static struct thread_input
*create_thread_input( struct thread
*thread
)
227 struct thread_input
*input
;
229 if ((input
= alloc_object( &thread_input_ops
)))
234 input
->menu_owner
= 0;
235 input
->move_size
= 0;
237 input
->cursor_count
= 0;
238 list_init( &input
->msg_list
);
239 set_caret_window( input
, 0 );
240 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
242 if (!(input
->desktop
= get_thread_desktop( thread
, 0 /* FIXME: access rights */ )))
244 release_object( input
);
251 /* create a message queue object */
252 static struct msg_queue
*create_msg_queue( struct thread
*thread
, struct thread_input
*input
)
254 struct thread_input
*new_input
= NULL
;
255 struct msg_queue
*queue
;
260 if (!(new_input
= create_thread_input( thread
))) return NULL
;
264 if ((queue
= alloc_object( &msg_queue_ops
)))
267 queue
->wake_bits
= 0;
268 queue
->wake_mask
= 0;
269 queue
->changed_bits
= 0;
270 queue
->changed_mask
= 0;
271 queue
->paint_count
= 0;
272 queue
->quit_message
= 0;
273 queue
->cursor_count
= 0;
274 queue
->recv_result
= NULL
;
275 queue
->next_timer_id
= 0x7fff;
276 queue
->timeout
= NULL
;
277 queue
->input
= (struct thread_input
*)grab_object( input
);
279 queue
->last_get_msg
= current_time
;
280 list_init( &queue
->send_result
);
281 list_init( &queue
->callback_result
);
282 list_init( &queue
->pending_timers
);
283 list_init( &queue
->expired_timers
);
284 for (i
= 0; i
< NB_MSG_KINDS
; i
++) list_init( &queue
->msg_list
[i
] );
286 thread
->queue
= queue
;
288 if (new_input
) release_object( new_input
);
292 /* free the message queue of a thread at thread exit */
293 void free_msg_queue( struct thread
*thread
)
295 remove_thread_hooks( thread
);
296 if (!thread
->queue
) return;
297 release_object( thread
->queue
);
298 thread
->queue
= NULL
;
301 /* change the thread input data of a given thread */
302 static int assign_thread_input( struct thread
*thread
, struct thread_input
*new_input
)
304 struct msg_queue
*queue
= thread
->queue
;
308 thread
->queue
= create_msg_queue( thread
, new_input
);
309 return thread
->queue
!= NULL
;
313 queue
->input
->cursor_count
-= queue
->cursor_count
;
314 release_object( queue
->input
);
316 queue
->input
= (struct thread_input
*)grab_object( new_input
);
317 new_input
->cursor_count
+= queue
->cursor_count
;
321 /* get the hook table for a given thread */
322 struct hook_table
*get_queue_hooks( struct thread
*thread
)
324 if (!thread
->queue
) return NULL
;
325 return thread
->queue
->hooks
;
328 /* set the hook table for a given thread, allocating the queue if needed */
329 void set_queue_hooks( struct thread
*thread
, struct hook_table
*hooks
)
331 struct msg_queue
*queue
= thread
->queue
;
332 if (!queue
&& !(queue
= create_msg_queue( thread
, NULL
))) return;
333 if (queue
->hooks
) release_object( queue
->hooks
);
334 queue
->hooks
= hooks
;
337 /* check the queue status */
338 static inline int is_signaled( struct msg_queue
*queue
)
340 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
343 /* set some queue bits */
344 static inline void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
346 queue
->wake_bits
|= bits
;
347 queue
->changed_bits
|= bits
;
348 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
351 /* clear some queue bits */
352 static inline void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
354 queue
->wake_bits
&= ~bits
;
355 queue
->changed_bits
&= ~bits
;
358 /* check whether msg is a keyboard message */
359 static inline int is_keyboard_msg( struct message
*msg
)
361 return (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
);
364 /* check if message is matched by the filter */
365 static inline int check_msg_filter( unsigned int msg
, unsigned int first
, unsigned int last
)
367 return (msg
>= first
&& msg
<= last
);
370 /* check whether a message filter contains at least one potential hardware message */
371 static inline int filter_contains_hw_range( unsigned int first
, unsigned int last
)
373 /* hardware message ranges are (in numerical order):
374 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
375 * WM_KEYFIRST .. WM_KEYLAST
376 * WM_MOUSEFIRST .. WM_MOUSELAST
378 if (last
< WM_NCMOUSEFIRST
) return 0;
379 if (first
> WM_NCMOUSELAST
&& last
< WM_KEYFIRST
) return 0;
380 if (first
> WM_KEYLAST
&& last
< WM_MOUSEFIRST
) return 0;
381 if (first
> WM_MOUSELAST
) return 0;
385 /* get the QS_* bit corresponding to a given hardware message */
386 static inline int get_hardware_msg_bit( struct message
*msg
)
388 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
389 if (is_keyboard_msg( msg
)) return QS_KEY
;
390 return QS_MOUSEBUTTON
;
393 /* get the current thread queue, creating it if needed */
394 static inline struct msg_queue
*get_current_queue(void)
396 struct msg_queue
*queue
= current
->queue
;
397 if (!queue
) queue
= create_msg_queue( current
, NULL
);
401 /* get a (pseudo-)unique id to tag hardware messages */
402 static inline unsigned int get_unique_id(void)
404 static unsigned int id
;
405 if (!++id
) id
= 1; /* avoid an id of 0 */
409 /* try to merge a message with the last in the list; return 1 if successful */
410 static int merge_message( struct thread_input
*input
, const struct message
*msg
)
412 struct message
*prev
;
413 struct list
*ptr
= list_tail( &input
->msg_list
);
416 prev
= LIST_ENTRY( ptr
, struct message
, entry
);
417 if (prev
->result
) return 0;
418 if (prev
->win
&& msg
->win
&& prev
->win
!= msg
->win
) return 0;
419 if (prev
->msg
!= msg
->msg
) return 0;
420 if (prev
->type
!= msg
->type
) return 0;
421 /* now we can merge it */
422 prev
->wparam
= msg
->wparam
;
423 prev
->lparam
= msg
->lparam
;
424 prev
->time
= msg
->time
;
425 if (msg
->type
== MSG_HARDWARE
&& prev
->data
&& msg
->data
)
427 struct hardware_msg_data
*prev_data
= prev
->data
;
428 struct hardware_msg_data
*msg_data
= msg
->data
;
429 prev_data
->x
= msg_data
->x
;
430 prev_data
->y
= msg_data
->y
;
431 prev_data
->info
= msg_data
->info
;
436 /* free a result structure */
437 static void free_result( struct message_result
*result
)
439 if (result
->timeout
) remove_timeout_user( result
->timeout
);
440 free( result
->data
);
441 if (result
->callback_msg
) free_message( result
->callback_msg
);
445 /* remove the result from the sender list it is on */
446 static inline void remove_result_from_sender( struct message_result
*result
)
448 assert( result
->sender
);
450 list_remove( &result
->sender_entry
);
451 result
->sender
= NULL
;
452 if (!result
->receiver
) free_result( result
);
455 /* store the message result in the appropriate structure */
456 static void store_message_result( struct message_result
*res
, lparam_t result
, unsigned int error
)
458 res
->result
= result
;
463 remove_timeout_user( res
->timeout
);
468 if (res
->callback_msg
)
470 /* queue the callback message in the sender queue */
471 struct callback_msg_data
*data
= res
->callback_msg
->data
;
472 data
->result
= result
;
473 list_add_tail( &res
->sender
->msg_list
[SEND_MESSAGE
], &res
->callback_msg
->entry
);
474 set_queue_bits( res
->sender
, QS_SENDMESSAGE
);
475 res
->callback_msg
= NULL
;
476 remove_result_from_sender( res
);
480 /* wake sender queue if waiting on this result */
481 if (list_head(&res
->sender
->send_result
) == &res
->sender_entry
)
482 set_queue_bits( res
->sender
, QS_SMRESULT
);
488 /* free a message when deleting a queue or window */
489 static void free_message( struct message
*msg
)
491 struct message_result
*result
= msg
->result
;
497 result
->receiver
= NULL
;
498 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
500 else free_result( result
);
506 /* remove (and free) a message from a message list */
507 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
508 enum message_kind kind
)
510 list_remove( &msg
->entry
);
514 if (list_empty( &queue
->msg_list
[kind
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
517 if (list_empty( &queue
->msg_list
[kind
] ) && !queue
->quit_message
)
518 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
524 /* message timed out without getting a reply */
525 static void result_timeout( void *private )
527 struct message_result
*result
= private;
529 assert( !result
->replied
);
531 result
->timeout
= NULL
;
533 if (result
->msg
) /* not received yet */
535 struct message
*msg
= result
->msg
;
539 remove_queue_message( result
->receiver
, msg
, SEND_MESSAGE
);
540 result
->receiver
= NULL
;
543 free_result( result
);
548 store_message_result( result
, 0, STATUS_TIMEOUT
);
551 /* allocate and fill a message result structure */
552 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
553 struct msg_queue
*recv_queue
,
554 struct message
*msg
, timeout_t timeout
)
556 struct message_result
*result
= mem_alloc( sizeof(*result
) );
560 result
->sender
= send_queue
;
561 result
->receiver
= recv_queue
;
564 result
->data_size
= 0;
565 result
->timeout
= NULL
;
567 if (msg
->type
== MSG_CALLBACK
)
569 struct message
*callback_msg
= mem_alloc( sizeof(*callback_msg
) );
576 callback_msg
->type
= MSG_CALLBACK_RESULT
;
577 callback_msg
->win
= msg
->win
;
578 callback_msg
->msg
= msg
->msg
;
579 callback_msg
->wparam
= 0;
580 callback_msg
->lparam
= 0;
581 callback_msg
->time
= get_tick_count();
582 callback_msg
->result
= NULL
;
583 /* steal the data from the original message */
584 callback_msg
->data
= msg
->data
;
585 callback_msg
->data_size
= msg
->data_size
;
589 result
->callback_msg
= callback_msg
;
590 list_add_head( &send_queue
->callback_result
, &result
->sender_entry
);
594 result
->callback_msg
= NULL
;
595 list_add_head( &send_queue
->send_result
, &result
->sender_entry
);
598 if (timeout
!= TIMEOUT_INFINITE
)
599 result
->timeout
= add_timeout_user( timeout
, result_timeout
, result
);
604 /* receive a message, removing it from the sent queue */
605 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
606 struct get_message_reply
*reply
)
608 struct message_result
*result
= msg
->result
;
610 reply
->total
= msg
->data_size
;
611 if (msg
->data_size
> get_reply_max_size())
613 set_error( STATUS_BUFFER_OVERFLOW
);
616 reply
->type
= msg
->type
;
617 reply
->win
= msg
->win
;
618 reply
->msg
= msg
->msg
;
619 reply
->wparam
= msg
->wparam
;
620 reply
->lparam
= msg
->lparam
;
621 reply
->time
= msg
->time
;
623 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
625 list_remove( &msg
->entry
);
626 /* put the result on the receiver result stack */
630 result
->recv_next
= queue
->recv_result
;
631 queue
->recv_result
= result
;
634 if (list_empty( &queue
->msg_list
[SEND_MESSAGE
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
637 /* set the result of the current received message */
638 static void reply_message( struct msg_queue
*queue
, lparam_t result
,
639 unsigned int error
, int remove
, const void *data
, data_size_t len
)
641 struct message_result
*res
= queue
->recv_result
;
645 queue
->recv_result
= res
->recv_next
;
646 res
->receiver
= NULL
;
647 if (!res
->sender
) /* no one waiting for it */
655 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
656 store_message_result( res
, result
, error
);
660 static int match_window( user_handle_t win
, user_handle_t msg_win
)
663 if (win
== -1 || win
== 1) return !msg_win
;
664 if (msg_win
== win
) return 1;
665 return is_child_window( win
, msg_win
);
668 /* retrieve a posted message */
669 static int get_posted_message( struct msg_queue
*queue
, user_handle_t win
,
670 unsigned int first
, unsigned int last
, unsigned int flags
,
671 struct get_message_reply
*reply
)
675 /* check against the filters */
676 LIST_FOR_EACH_ENTRY( msg
, &queue
->msg_list
[POST_MESSAGE
], struct message
, entry
)
678 if (!match_window( win
, msg
->win
)) continue;
679 if (!check_msg_filter( msg
->msg
, first
, last
)) continue;
680 goto found
; /* found one */
684 /* return it to the app */
686 reply
->total
= msg
->data_size
;
687 if (msg
->data_size
> get_reply_max_size())
689 set_error( STATUS_BUFFER_OVERFLOW
);
692 reply
->type
= msg
->type
;
693 reply
->win
= msg
->win
;
694 reply
->msg
= msg
->msg
;
695 reply
->wparam
= msg
->wparam
;
696 reply
->lparam
= msg
->lparam
;
697 reply
->time
= msg
->time
;
699 if (flags
& PM_REMOVE
)
703 set_reply_data_ptr( msg
->data
, msg
->data_size
);
707 remove_queue_message( queue
, msg
, POST_MESSAGE
);
709 else if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
714 static int get_quit_message( struct msg_queue
*queue
, unsigned int flags
,
715 struct get_message_reply
*reply
)
717 if (queue
->quit_message
)
720 reply
->type
= MSG_POSTED
;
722 reply
->msg
= WM_QUIT
;
723 reply
->wparam
= queue
->exit_code
;
725 reply
->time
= get_tick_count();
727 if (flags
& PM_REMOVE
)
729 queue
->quit_message
= 0;
730 if (list_empty( &queue
->msg_list
[POST_MESSAGE
] ))
731 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
739 /* empty a message list and free all the messages */
740 static void empty_msg_list( struct list
*list
)
744 while ((ptr
= list_head( list
)) != NULL
)
746 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
747 list_remove( &msg
->entry
);
752 /* cleanup all pending results when deleting a queue */
753 static void cleanup_results( struct msg_queue
*queue
)
757 while ((entry
= list_head( &queue
->send_result
)) != NULL
)
759 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
762 while ((entry
= list_head( &queue
->callback_result
)) != NULL
)
764 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
767 while (queue
->recv_result
)
768 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
771 /* check if the thread owning the queue is hung (not checking for messages) */
772 static int is_queue_hung( struct msg_queue
*queue
)
774 struct wait_queue_entry
*entry
;
776 if (current_time
- queue
->last_get_msg
<= 5 * TICKS_PER_SEC
)
777 return 0; /* less than 5 seconds since last get message -> not hung */
779 LIST_FOR_EACH_ENTRY( entry
, &queue
->obj
.wait_queue
, struct wait_queue_entry
, entry
)
781 if (entry
->thread
->queue
== queue
)
782 return 0; /* thread is waiting on queue -> not hung */
787 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
789 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
790 struct process
*process
= entry
->thread
->process
;
792 /* a thread can only wait on its own queue */
793 if (entry
->thread
->queue
!= queue
)
795 set_error( STATUS_ACCESS_DENIED
);
798 if (process
->idle_event
&& !(queue
->wake_mask
& QS_SMRESULT
)) set_event( process
->idle_event
);
800 if (queue
->fd
&& list_empty( &obj
->wait_queue
)) /* first on the queue */
801 set_fd_events( queue
->fd
, POLLIN
);
802 add_queue( obj
, entry
);
806 static void msg_queue_remove_queue(struct object
*obj
, struct wait_queue_entry
*entry
)
808 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
810 remove_queue( obj
, entry
);
811 if (queue
->fd
&& list_empty( &obj
->wait_queue
)) /* last on the queue is gone */
812 set_fd_events( queue
->fd
, 0 );
815 static void msg_queue_dump( struct object
*obj
, int verbose
)
817 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
818 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
819 queue
->wake_bits
, queue
->wake_mask
);
822 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
)
824 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
829 if ((ret
= check_fd_events( queue
->fd
, POLLIN
)))
830 /* stop waiting on select() if we are signaled */
831 set_fd_events( queue
->fd
, 0 );
832 else if (!list_empty( &obj
->wait_queue
))
833 /* restart waiting on poll() if we are no longer signaled */
834 set_fd_events( queue
->fd
, POLLIN
);
836 return ret
|| is_signaled( queue
);
839 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
)
841 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
842 queue
->wake_mask
= 0;
843 queue
->changed_mask
= 0;
844 return 0; /* Not abandoned */
847 static void msg_queue_destroy( struct object
*obj
)
849 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
853 cleanup_results( queue
);
854 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
856 while ((ptr
= list_head( &queue
->pending_timers
)))
858 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
859 list_remove( &timer
->entry
);
862 while ((ptr
= list_head( &queue
->expired_timers
)))
864 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
865 list_remove( &timer
->entry
);
868 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
871 queue
->input
->cursor_count
-= queue
->cursor_count
;
872 release_object( queue
->input
);
874 if (queue
->hooks
) release_object( queue
->hooks
);
875 if (queue
->fd
) release_object( queue
->fd
);
878 static void msg_queue_poll_event( struct fd
*fd
, int event
)
880 struct msg_queue
*queue
= get_fd_user( fd
);
881 assert( queue
->obj
.ops
== &msg_queue_ops
);
883 if (event
& (POLLERR
| POLLHUP
)) set_fd_events( fd
, -1 );
884 else set_fd_events( queue
->fd
, 0 );
885 wake_up( &queue
->obj
, 0 );
888 static void thread_input_dump( struct object
*obj
, int verbose
)
890 struct thread_input
*input
= (struct thread_input
*)obj
;
891 fprintf( stderr
, "Thread input focus=%08x capture=%08x active=%08x\n",
892 input
->focus
, input
->capture
, input
->active
);
895 static void thread_input_destroy( struct object
*obj
)
897 struct thread_input
*input
= (struct thread_input
*)obj
;
899 if (foreground_input
== input
) foreground_input
= NULL
;
900 empty_msg_list( &input
->msg_list
);
901 if (input
->desktop
) release_object( input
->desktop
);
904 /* fix the thread input data when a window is destroyed */
905 static inline void thread_input_cleanup_window( struct msg_queue
*queue
, user_handle_t window
)
907 struct thread_input
*input
= queue
->input
;
909 if (window
== input
->focus
) input
->focus
= 0;
910 if (window
== input
->capture
) input
->capture
= 0;
911 if (window
== input
->active
) input
->active
= 0;
912 if (window
== input
->menu_owner
) input
->menu_owner
= 0;
913 if (window
== input
->move_size
) input
->move_size
= 0;
914 if (window
== input
->caret
) set_caret_window( input
, 0 );
917 /* check if the specified window can be set in the input data of a given queue */
918 static int check_queue_input_window( struct msg_queue
*queue
, user_handle_t window
)
920 struct thread
*thread
;
923 if (!window
) return 1; /* we can always clear the data */
925 if ((thread
= get_window_thread( window
)))
927 ret
= (queue
->input
== thread
->queue
->input
);
928 if (!ret
) set_error( STATUS_ACCESS_DENIED
);
929 release_object( thread
);
931 else set_error( STATUS_INVALID_HANDLE
);
936 /* make sure the specified thread has a queue */
937 int init_thread_queue( struct thread
*thread
)
939 if (thread
->queue
) return 1;
940 return (create_msg_queue( thread
, NULL
) != NULL
);
943 /* attach two thread input data structures */
944 int attach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
946 struct desktop
*desktop
;
947 struct thread_input
*input
;
950 if (!thread_to
->queue
&& !(thread_to
->queue
= create_msg_queue( thread_to
, NULL
))) return 0;
951 if (!(desktop
= get_thread_desktop( thread_from
, 0 ))) return 0;
952 input
= (struct thread_input
*)grab_object( thread_to
->queue
->input
);
953 if (input
->desktop
!= desktop
)
955 set_error( STATUS_ACCESS_DENIED
);
956 release_object( input
);
957 release_object( desktop
);
960 release_object( desktop
);
962 ret
= assign_thread_input( thread_from
, input
);
963 if (ret
) memset( input
->keystate
, 0, sizeof(input
->keystate
) );
964 release_object( input
);
968 /* detach two thread input data structures */
969 void detach_thread_input( struct thread
*thread_from
)
971 struct thread_input
*input
;
973 if ((input
= create_thread_input( thread_from
)))
975 assign_thread_input( thread_from
, input
);
976 release_object( input
);
981 /* set the next timer to expire */
982 static void set_next_timer( struct msg_queue
*queue
)
988 remove_timeout_user( queue
->timeout
);
989 queue
->timeout
= NULL
;
991 if ((ptr
= list_head( &queue
->pending_timers
)))
993 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
994 queue
->timeout
= add_timeout_user( timer
->when
, timer_callback
, queue
);
996 /* set/clear QS_TIMER bit */
997 if (list_empty( &queue
->expired_timers
))
998 clear_queue_bits( queue
, QS_TIMER
);
1000 set_queue_bits( queue
, QS_TIMER
);
1003 /* find a timer from its window and id */
1004 static struct timer
*find_timer( struct msg_queue
*queue
, user_handle_t win
,
1005 unsigned int msg
, lparam_t id
)
1009 /* we need to search both lists */
1011 LIST_FOR_EACH( ptr
, &queue
->pending_timers
)
1013 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1014 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
1016 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
1018 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1019 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
1024 /* callback for the next timer expiration */
1025 static void timer_callback( void *private )
1027 struct msg_queue
*queue
= private;
1030 queue
->timeout
= NULL
;
1031 /* move on to the next timer */
1032 ptr
= list_head( &queue
->pending_timers
);
1034 list_add_tail( &queue
->expired_timers
, ptr
);
1035 set_next_timer( queue
);
1038 /* link a timer at its rightful place in the queue list */
1039 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
1043 for (ptr
= queue
->pending_timers
.next
; ptr
!= &queue
->pending_timers
; ptr
= ptr
->next
)
1045 struct timer
*t
= LIST_ENTRY( ptr
, struct timer
, entry
);
1046 if (t
->when
>= timer
->when
) break;
1048 list_add_before( ptr
, &timer
->entry
);
1051 /* remove a timer from the queue timer list and free it */
1052 static void free_timer( struct msg_queue
*queue
, struct timer
*timer
)
1054 list_remove( &timer
->entry
);
1056 set_next_timer( queue
);
1059 /* restart an expired timer */
1060 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
1062 list_remove( &timer
->entry
);
1063 while (timer
->when
<= current_time
) timer
->when
+= (timeout_t
)timer
->rate
* 10000;
1064 link_timer( queue
, timer
);
1065 set_next_timer( queue
);
1068 /* find an expired timer matching the filtering parameters */
1069 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
1070 unsigned int get_first
, unsigned int get_last
,
1075 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
1077 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1078 if (win
&& timer
->win
!= win
) continue;
1079 if (check_msg_filter( timer
->msg
, get_first
, get_last
))
1081 if (remove
) restart_timer( queue
, timer
);
1089 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
1091 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
1094 timer
->rate
= max( rate
, 1 );
1095 timer
->when
= current_time
+ (timeout_t
)timer
->rate
* 10000;
1096 link_timer( queue
, timer
);
1097 /* check if we replaced the next timer */
1098 if (list_head( &queue
->pending_timers
) == &timer
->entry
) set_next_timer( queue
);
1103 /* change the input key state for a given key */
1104 static void set_input_key_state( struct thread_input
*input
, unsigned char key
, int down
)
1108 if (!(input
->keystate
[key
] & 0x80)) input
->keystate
[key
] ^= 0x01;
1109 input
->keystate
[key
] |= 0x80;
1111 else input
->keystate
[key
] &= ~0x80;
1114 /* update the input key state for a keyboard message */
1115 static void update_input_key_state( struct thread_input
*input
, const struct message
*msg
)
1122 case WM_LBUTTONDOWN
:
1126 set_input_key_state( input
, VK_LBUTTON
, down
);
1128 case WM_MBUTTONDOWN
:
1132 set_input_key_state( input
, VK_MBUTTON
, down
);
1134 case WM_RBUTTONDOWN
:
1138 set_input_key_state( input
, VK_RBUTTON
, down
);
1140 case WM_XBUTTONDOWN
:
1144 if (msg
->wparam
== XBUTTON1
) set_input_key_state( input
, VK_XBUTTON1
, down
);
1145 else if (msg
->wparam
== XBUTTON2
) set_input_key_state( input
, VK_XBUTTON2
, down
);
1153 key
= (unsigned char)msg
->wparam
;
1154 set_input_key_state( input
, key
, down
);
1159 down
= (input
->keystate
[VK_LCONTROL
] | input
->keystate
[VK_RCONTROL
]) & 0x80;
1160 set_input_key_state( input
, VK_CONTROL
, down
);
1164 down
= (input
->keystate
[VK_LMENU
] | input
->keystate
[VK_RMENU
]) & 0x80;
1165 set_input_key_state( input
, VK_MENU
, down
);
1169 down
= (input
->keystate
[VK_LSHIFT
] | input
->keystate
[VK_RSHIFT
]) & 0x80;
1170 set_input_key_state( input
, VK_SHIFT
, down
);
1177 /* release the hardware message currently being processed by the given thread */
1178 static void release_hardware_message( struct msg_queue
*queue
, unsigned int hw_id
,
1179 int remove
, user_handle_t new_win
)
1181 struct thread_input
*input
= queue
->input
;
1182 struct message
*msg
;
1184 LIST_FOR_EACH_ENTRY( msg
, &input
->msg_list
, struct message
, entry
)
1186 if (msg
->unique_id
== hw_id
) break;
1188 if (&msg
->entry
== &input
->msg_list
) return; /* not found */
1190 /* clear the queue bit for that message */
1191 if (remove
|| new_win
)
1193 struct message
*other
;
1196 clr_bit
= get_hardware_msg_bit( msg
);
1197 LIST_FOR_EACH_ENTRY( other
, &input
->msg_list
, struct message
, entry
)
1199 if (other
!= msg
&& get_hardware_msg_bit( other
) == clr_bit
)
1205 if (clr_bit
) clear_queue_bits( queue
, clr_bit
);
1208 if (new_win
) /* set the new window */
1210 struct thread
*owner
= get_window_thread( new_win
);
1214 if (owner
->queue
->input
!= input
)
1216 list_remove( &msg
->entry
);
1217 if (msg
->msg
== WM_MOUSEMOVE
&& merge_message( owner
->queue
->input
, msg
))
1219 free_message( msg
);
1220 release_object( owner
);
1223 list_add_tail( &owner
->queue
->input
->msg_list
, &msg
->entry
);
1225 set_queue_bits( owner
->queue
, get_hardware_msg_bit( msg
));
1227 release_object( owner
);
1232 update_input_key_state( input
, msg
);
1233 list_remove( &msg
->entry
);
1234 free_message( msg
);
1238 /* find the window that should receive a given hardware message */
1239 static user_handle_t
find_hardware_message_window( struct thread_input
*input
, struct message
*msg
,
1240 struct hardware_msg_data
*data
, unsigned int *msg_code
)
1242 user_handle_t win
= 0;
1244 *msg_code
= msg
->msg
;
1245 if (is_keyboard_msg( msg
))
1247 if (input
&& !(win
= input
->focus
))
1249 win
= input
->active
;
1250 if (*msg_code
< WM_SYSKEYDOWN
) *msg_code
+= WM_SYSKEYDOWN
- WM_KEYDOWN
;
1253 else /* mouse message */
1255 if (!input
|| !(win
= input
->capture
))
1257 if (!(win
= msg
->win
) || !is_window_visible( win
) || is_window_transparent( win
))
1259 if (input
) win
= window_from_point( input
->desktop
, data
->x
, data
->y
);
1266 /* queue a hardware message into a given thread input */
1267 static void queue_hardware_message( struct thread_input
*input
, struct message
*msg
,
1268 struct hardware_msg_data
*data
)
1271 struct thread
*thread
;
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 thread
*thread
= NULL
;
1709 struct hardware_msg_data
*data
;
1710 struct thread_input
*input
= foreground_input
;
1714 if (!(thread
= get_thread_from_id( req
->id
))) return;
1717 set_error( STATUS_INVALID_PARAMETER
);
1718 release_object( thread
);
1721 input
= thread
->queue
->input
;
1722 reply
->cursor
= input
->cursor
;
1723 reply
->count
= input
->cursor_count
;
1726 if (!req
->msg
|| !(data
= mem_alloc( sizeof(*data
) )))
1728 if (thread
) release_object( thread
);
1731 memset( data
, 0, sizeof(*data
) );
1734 data
->info
= req
->info
;
1736 if ((msg
= mem_alloc( sizeof(*msg
) )))
1738 msg
->type
= MSG_HARDWARE
;
1739 msg
->win
= get_user_full_handle( req
->win
);
1740 msg
->msg
= req
->msg
;
1741 msg
->wparam
= req
->wparam
;
1742 msg
->lparam
= req
->lparam
;
1743 msg
->time
= req
->time
;
1746 msg
->data_size
= sizeof(*data
);
1747 queue_hardware_message( input
, msg
, data
);
1751 if (thread
) release_object( thread
);
1754 /* post a quit message to the current queue */
1755 DECL_HANDLER(post_quit_message
)
1757 struct msg_queue
*queue
= get_current_queue();
1762 queue
->quit_message
= 1;
1763 queue
->exit_code
= req
->exit_code
;
1764 set_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
1767 /* get a message from the current queue */
1768 DECL_HANDLER(get_message
)
1770 struct timer
*timer
;
1772 struct msg_queue
*queue
= get_current_queue();
1773 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
1774 unsigned int filter
= req
->flags
>> 16;
1776 reply
->active_hooks
= get_active_hooks();
1779 queue
->last_get_msg
= current_time
;
1780 if (!filter
) filter
= QS_ALLINPUT
;
1782 /* first check for sent messages */
1783 if ((ptr
= list_head( &queue
->msg_list
[SEND_MESSAGE
] )))
1785 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1786 receive_message( queue
, msg
, reply
);
1790 /* clear changed bits so we can wait on them if we don't find a message */
1791 if (filter
& QS_POSTMESSAGE
)
1793 queue
->changed_bits
&= ~(QS_POSTMESSAGE
| QS_HOTKEY
| QS_TIMER
);
1794 if (req
->get_first
== 0 && req
->get_last
== ~0U) queue
->changed_bits
&= ~QS_ALLPOSTMESSAGE
;
1796 if (filter
& QS_INPUT
) queue
->changed_bits
&= ~QS_INPUT
;
1797 if (filter
& QS_PAINT
) queue
->changed_bits
&= ~QS_PAINT
;
1799 /* then check for posted messages */
1800 if ((filter
& QS_POSTMESSAGE
) &&
1801 get_posted_message( queue
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
1804 /* only check for quit messages if not posted messages pending.
1805 * note: the quit message isn't filtered */
1806 if (get_quit_message( queue
, req
->flags
, reply
))
1809 /* then check for any raw hardware message */
1810 if ((filter
& QS_INPUT
) &&
1811 filter_contains_hw_range( req
->get_first
, req
->get_last
) &&
1812 get_hardware_message( current
, req
->hw_id
, get_win
, req
->get_first
, req
->get_last
, reply
))
1815 /* now check for WM_PAINT */
1816 if ((filter
& QS_PAINT
) &&
1817 queue
->paint_count
&&
1818 check_msg_filter( WM_PAINT
, req
->get_first
, req
->get_last
) &&
1819 (reply
->win
= find_window_to_repaint( get_win
, current
)))
1821 reply
->type
= MSG_POSTED
;
1822 reply
->msg
= WM_PAINT
;
1825 reply
->time
= get_tick_count();
1829 /* now check for timer */
1830 if ((filter
& QS_TIMER
) &&
1831 (timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
1832 req
->get_last
, (req
->flags
& PM_REMOVE
) )))
1834 reply
->type
= MSG_POSTED
;
1835 reply
->win
= timer
->win
;
1836 reply
->msg
= timer
->msg
;
1837 reply
->wparam
= timer
->id
;
1838 reply
->lparam
= timer
->lparam
;
1839 reply
->time
= get_tick_count();
1840 if (!(req
->flags
& PM_NOYIELD
) && current
->process
->idle_event
)
1841 set_event( current
->process
->idle_event
);
1845 if (get_win
== -1 && current
->process
->idle_event
) set_event( current
->process
->idle_event
);
1846 queue
->wake_mask
= req
->wake_mask
;
1847 queue
->changed_mask
= req
->changed_mask
;
1848 set_error( STATUS_PENDING
); /* FIXME */
1852 /* reply to a sent message */
1853 DECL_HANDLER(reply_message
)
1855 if (!current
->queue
) set_error( STATUS_ACCESS_DENIED
);
1856 else if (current
->queue
->recv_result
)
1857 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
1858 get_req_data(), get_req_data_size() );
1862 /* accept the current hardware message */
1863 DECL_HANDLER(accept_hardware_message
)
1866 release_hardware_message( current
->queue
, req
->hw_id
, req
->remove
, req
->new_win
);
1868 set_error( STATUS_ACCESS_DENIED
);
1872 /* retrieve the reply for the last message sent */
1873 DECL_HANDLER(get_message_reply
)
1875 struct message_result
*result
;
1877 struct msg_queue
*queue
= current
->queue
;
1881 set_error( STATUS_PENDING
);
1884 if (!(entry
= list_head( &queue
->send_result
))) return; /* no reply ready */
1886 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1887 if (result
->replied
|| req
->cancel
)
1889 if (result
->replied
)
1891 reply
->result
= result
->result
;
1892 set_error( result
->error
);
1895 data_size_t data_len
= min( result
->data_size
, get_reply_max_size() );
1896 set_reply_data_ptr( result
->data
, data_len
);
1897 result
->data
= NULL
;
1898 result
->data_size
= 0;
1901 remove_result_from_sender( result
);
1903 entry
= list_head( &queue
->send_result
);
1904 if (!entry
) clear_queue_bits( queue
, QS_SMRESULT
);
1907 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1908 if (!result
->replied
) clear_queue_bits( queue
, QS_SMRESULT
);
1912 else set_error( STATUS_ACCESS_DENIED
);
1916 /* set a window timer */
1917 DECL_HANDLER(set_win_timer
)
1919 struct timer
*timer
;
1920 struct msg_queue
*queue
;
1921 struct thread
*thread
= NULL
;
1922 user_handle_t win
= 0;
1923 lparam_t id
= req
->id
;
1927 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1929 set_error( STATUS_INVALID_HANDLE
);
1932 if (thread
->process
!= current
->process
)
1934 release_object( thread
);
1935 set_error( STATUS_ACCESS_DENIED
);
1938 queue
= thread
->queue
;
1939 /* remove it if it existed already */
1940 if ((timer
= find_timer( queue
, win
, req
->msg
, id
))) free_timer( queue
, timer
);
1944 queue
= get_current_queue();
1945 /* look for a timer with this id */
1946 if (id
&& (timer
= find_timer( queue
, 0, req
->msg
, id
)))
1948 /* free and reuse id */
1949 free_timer( queue
, timer
);
1953 /* find a free id for it */
1956 id
= queue
->next_timer_id
;
1957 if (--queue
->next_timer_id
<= 0x100) queue
->next_timer_id
= 0x7fff;
1959 while (find_timer( queue
, 0, req
->msg
, id
));
1963 if ((timer
= set_timer( queue
, req
->rate
)))
1966 timer
->msg
= req
->msg
;
1968 timer
->lparam
= req
->lparam
;
1971 if (thread
) release_object( thread
);
1974 /* kill a window timer */
1975 DECL_HANDLER(kill_win_timer
)
1977 struct timer
*timer
;
1978 struct thread
*thread
;
1979 user_handle_t win
= 0;
1983 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1985 set_error( STATUS_INVALID_HANDLE
);
1988 if (thread
->process
!= current
->process
)
1990 release_object( thread
);
1991 set_error( STATUS_ACCESS_DENIED
);
1995 else thread
= (struct thread
*)grab_object( current
);
1997 if (thread
->queue
&& (timer
= find_timer( thread
->queue
, win
, req
->msg
, req
->id
)))
1998 free_timer( thread
->queue
, timer
);
2000 set_error( STATUS_INVALID_PARAMETER
);
2002 release_object( thread
);
2006 /* attach (or detach) thread inputs */
2007 DECL_HANDLER(attach_thread_input
)
2009 struct thread
*thread_from
= get_thread_from_id( req
->tid_from
);
2010 struct thread
*thread_to
= get_thread_from_id( req
->tid_to
);
2012 if (!thread_from
|| !thread_to
)
2014 if (thread_from
) release_object( thread_from
);
2015 if (thread_to
) release_object( thread_to
);
2018 if (thread_from
!= thread_to
)
2020 if (req
->attach
) attach_thread_input( thread_from
, thread_to
);
2023 if (thread_from
->queue
&& thread_to
->queue
&&
2024 thread_from
->queue
->input
== thread_to
->queue
->input
)
2025 detach_thread_input( thread_from
);
2027 set_error( STATUS_ACCESS_DENIED
);
2030 else set_error( STATUS_ACCESS_DENIED
);
2031 release_object( thread_from
);
2032 release_object( thread_to
);
2036 /* get thread input data */
2037 DECL_HANDLER(get_thread_input
)
2039 struct thread
*thread
= NULL
;
2040 struct thread_input
*input
;
2044 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2045 input
= thread
->queue
? thread
->queue
->input
: NULL
;
2047 else input
= foreground_input
; /* get the foreground thread info */
2051 reply
->focus
= input
->focus
;
2052 reply
->capture
= input
->capture
;
2053 reply
->active
= input
->active
;
2054 reply
->menu_owner
= input
->menu_owner
;
2055 reply
->move_size
= input
->move_size
;
2056 reply
->caret
= input
->caret
;
2057 reply
->cursor
= input
->cursor
;
2058 reply
->show_count
= input
->cursor_count
;
2059 reply
->rect
= input
->caret_rect
;
2062 /* foreground window is active window of foreground thread */
2063 reply
->foreground
= foreground_input
? foreground_input
->active
: 0;
2064 if (thread
) release_object( thread
);
2068 /* retrieve queue keyboard state for a given thread */
2069 DECL_HANDLER(get_key_state
)
2071 struct thread
*thread
;
2072 struct thread_input
*input
;
2074 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2075 input
= thread
->queue
? thread
->queue
->input
: NULL
;
2078 if (req
->key
>= 0) reply
->state
= input
->keystate
[req
->key
& 0xff];
2079 set_reply_data( input
->keystate
, min( get_reply_max_size(), sizeof(input
->keystate
) ));
2081 release_object( thread
);
2085 /* set queue keyboard state for a given thread */
2086 DECL_HANDLER(set_key_state
)
2088 struct thread
*thread
= NULL
;
2089 struct thread_input
*input
;
2091 if (!(thread
= get_thread_from_id( req
->tid
))) return;
2092 input
= thread
->queue
? thread
->queue
->input
: NULL
;
2095 data_size_t size
= min( sizeof(input
->keystate
), get_req_data_size() );
2096 if (size
) memcpy( input
->keystate
, get_req_data(), size
);
2098 release_object( thread
);
2102 /* set the system foreground window */
2103 DECL_HANDLER(set_foreground_window
)
2105 struct thread
*thread
;
2106 struct msg_queue
*queue
= get_current_queue();
2108 reply
->previous
= foreground_input
? foreground_input
->active
: 0;
2109 reply
->send_msg_old
= (reply
->previous
&& foreground_input
!= queue
->input
);
2110 reply
->send_msg_new
= FALSE
;
2112 if (is_top_level_window( req
->handle
) &&
2113 ((thread
= get_window_thread( req
->handle
))))
2115 foreground_input
= thread
->queue
->input
;
2116 reply
->send_msg_new
= (foreground_input
!= queue
->input
);
2117 release_object( thread
);
2119 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE
);
2123 /* set the current thread focus window */
2124 DECL_HANDLER(set_focus_window
)
2126 struct msg_queue
*queue
= get_current_queue();
2128 reply
->previous
= 0;
2129 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2131 reply
->previous
= queue
->input
->focus
;
2132 queue
->input
->focus
= get_user_full_handle( req
->handle
);
2137 /* set the current thread active window */
2138 DECL_HANDLER(set_active_window
)
2140 struct msg_queue
*queue
= get_current_queue();
2142 reply
->previous
= 0;
2143 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2145 if (!req
->handle
|| make_window_active( req
->handle
))
2147 reply
->previous
= queue
->input
->active
;
2148 queue
->input
->active
= get_user_full_handle( req
->handle
);
2150 else set_error( STATUS_INVALID_HANDLE
);
2155 /* set the current thread capture window */
2156 DECL_HANDLER(set_capture_window
)
2158 struct msg_queue
*queue
= get_current_queue();
2160 reply
->previous
= reply
->full_handle
= 0;
2161 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2163 struct thread_input
*input
= queue
->input
;
2165 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2166 if (input
->menu_owner
&& !(req
->flags
& CAPTURE_MENU
))
2168 set_error(STATUS_ACCESS_DENIED
);
2171 reply
->previous
= input
->capture
;
2172 input
->capture
= get_user_full_handle( req
->handle
);
2173 input
->menu_owner
= (req
->flags
& CAPTURE_MENU
) ? input
->capture
: 0;
2174 input
->move_size
= (req
->flags
& CAPTURE_MOVESIZE
) ? input
->capture
: 0;
2175 reply
->full_handle
= input
->capture
;
2180 /* Set the current thread caret window */
2181 DECL_HANDLER(set_caret_window
)
2183 struct msg_queue
*queue
= get_current_queue();
2185 reply
->previous
= 0;
2186 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2188 struct thread_input
*input
= queue
->input
;
2190 reply
->previous
= input
->caret
;
2191 reply
->old_rect
= input
->caret_rect
;
2192 reply
->old_hide
= input
->caret_hide
;
2193 reply
->old_state
= input
->caret_state
;
2195 set_caret_window( input
, get_user_full_handle(req
->handle
) );
2196 input
->caret_rect
.right
= input
->caret_rect
.left
+ req
->width
;
2197 input
->caret_rect
.bottom
= input
->caret_rect
.top
+ req
->height
;
2202 /* Set the current thread caret information */
2203 DECL_HANDLER(set_caret_info
)
2205 struct msg_queue
*queue
= get_current_queue();
2206 struct thread_input
*input
;
2209 input
= queue
->input
;
2210 reply
->full_handle
= input
->caret
;
2211 reply
->old_rect
= input
->caret_rect
;
2212 reply
->old_hide
= input
->caret_hide
;
2213 reply
->old_state
= input
->caret_state
;
2215 if (req
->handle
&& get_user_full_handle(req
->handle
) != input
->caret
)
2217 set_error( STATUS_ACCESS_DENIED
);
2220 if (req
->flags
& SET_CARET_POS
)
2222 input
->caret_rect
.right
+= req
->x
- input
->caret_rect
.left
;
2223 input
->caret_rect
.bottom
+= req
->y
- input
->caret_rect
.top
;
2224 input
->caret_rect
.left
= req
->x
;
2225 input
->caret_rect
.top
= req
->y
;
2227 if (req
->flags
& SET_CARET_HIDE
)
2229 input
->caret_hide
+= req
->hide
;
2230 if (input
->caret_hide
< 0) input
->caret_hide
= 0;
2232 if (req
->flags
& SET_CARET_STATE
)
2234 if (req
->state
== -1) input
->caret_state
= !input
->caret_state
;
2235 else input
->caret_state
= !!req
->state
;
2240 /* get the time of the last input event */
2241 DECL_HANDLER(get_last_input_time
)
2243 reply
->time
= last_input_time
;
2246 /* set/get the current cursor */
2247 DECL_HANDLER(set_cursor
)
2249 struct msg_queue
*queue
= get_current_queue();
2250 struct thread_input
*input
;
2253 input
= queue
->input
;
2255 reply
->prev_handle
= input
->cursor
;
2256 reply
->prev_count
= input
->cursor_count
;
2258 if (req
->flags
& SET_CURSOR_HANDLE
)
2260 if (req
->handle
&& !get_user_object( req
->handle
, USER_CLIENT
))
2262 set_win32_error( ERROR_INVALID_CURSOR_HANDLE
);
2265 input
->cursor
= req
->handle
;
2268 if (req
->flags
& SET_CURSOR_COUNT
)
2270 queue
->cursor_count
+= req
->show_count
;
2271 input
->cursor_count
+= req
->show_count
;