2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
30 #define WIN32_NO_STATUS
44 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
45 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
47 enum message_kind
{ SEND_MESSAGE
, POST_MESSAGE
};
48 #define NB_MSG_KINDS (POST_MESSAGE+1)
53 struct list sender_entry
; /* entry in sender list */
54 struct message
*msg
; /* message the result is for */
55 struct message_result
*recv_next
; /* next in receiver list */
56 struct msg_queue
*sender
; /* sender queue */
57 struct msg_queue
*receiver
; /* receiver queue */
58 int replied
; /* has it been replied to? */
59 unsigned int result
; /* reply result */
60 unsigned int error
; /* error code to pass back to sender */
61 struct message
*callback_msg
; /* message to queue for callback */
62 void *data
; /* message reply data */
63 unsigned int data_size
; /* size of message reply data */
64 struct timeout_user
*timeout
; /* result timeout */
69 struct list entry
; /* entry in message list */
70 enum message_type type
; /* message type */
71 user_handle_t win
; /* window handle */
72 unsigned int msg
; /* message code */
73 unsigned long wparam
; /* parameters */
74 unsigned long lparam
; /* parameters */
75 int x
; /* x position */
76 int y
; /* y position */
77 unsigned int time
; /* message time */
78 unsigned int info
; /* extra info */
79 user_handle_t hook
; /* winevent hook handle */
80 void *hook_proc
; /* winevent hook proc address */
81 void *data
; /* message data for sent messages */
82 unsigned int data_size
; /* size of message data */
83 unsigned int unique_id
; /* unique id for nested hw message waits */
84 struct message_result
*result
; /* result in sender queue */
89 struct list entry
; /* entry in timer list */
90 struct timeval when
; /* next expiration */
91 unsigned int rate
; /* timer rate in ms */
92 user_handle_t win
; /* window handle */
93 unsigned int msg
; /* message to post */
94 unsigned int id
; /* timer id */
95 unsigned int lparam
; /* lparam for message */
100 struct object obj
; /* object header */
101 struct desktop
*desktop
; /* desktop that this thread input belongs to */
102 user_handle_t focus
; /* focus window */
103 user_handle_t capture
; /* capture window */
104 user_handle_t active
; /* active window */
105 user_handle_t menu_owner
; /* current menu owner window */
106 user_handle_t move_size
; /* current moving/resizing window */
107 user_handle_t caret
; /* caret window */
108 rectangle_t caret_rect
; /* caret rectangle */
109 int caret_hide
; /* caret hide count */
110 int caret_state
; /* caret on/off state */
111 struct list msg_list
; /* list of hardware messages */
112 unsigned char keystate
[256]; /* state of each key */
117 struct object obj
; /* object header */
118 unsigned int wake_bits
; /* wakeup bits */
119 unsigned int wake_mask
; /* wakeup mask */
120 unsigned int changed_bits
; /* changed wakeup bits */
121 unsigned int changed_mask
; /* changed wakeup mask */
122 int paint_count
; /* pending paint messages count */
123 int quit_message
; /* is there a pending quit message? */
124 int exit_code
; /* exit code of pending quit message */
125 struct list msg_list
[NB_MSG_KINDS
]; /* lists of messages */
126 struct list send_result
; /* stack of sent messages waiting for result */
127 struct list callback_result
; /* list of callback messages waiting for result */
128 struct message_result
*recv_result
; /* stack of received messages waiting for result */
129 struct list pending_timers
; /* list of pending timers */
130 struct list expired_timers
; /* list of expired timers */
131 unsigned int next_timer_id
; /* id for the next timer with a 0 window */
132 struct timeout_user
*timeout
; /* timeout for next timer to expire */
133 struct thread_input
*input
; /* thread input descriptor */
134 struct hook_table
*hooks
; /* hook table */
135 struct timeval last_get_msg
; /* time of last get message call */
138 static void msg_queue_dump( struct object
*obj
, int verbose
);
139 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
140 static void msg_queue_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
141 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
);
142 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
);
143 static void msg_queue_destroy( struct object
*obj
);
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 msg_queue_add_queue
, /* add_queue */
153 msg_queue_remove_queue
, /* remove_queue */
154 msg_queue_signaled
, /* signaled */
155 msg_queue_satisfied
, /* satisfied */
156 no_signal
, /* signal */
157 no_get_fd
, /* get_fd */
158 no_map_access
, /* map_access */
159 no_lookup_name
, /* lookup_name */
160 no_close_handle
, /* close_handle */
161 msg_queue_destroy
/* destroy */
165 static const struct object_ops thread_input_ops
=
167 sizeof(struct thread_input
), /* size */
168 thread_input_dump
, /* dump */
169 no_add_queue
, /* add_queue */
170 NULL
, /* remove_queue */
172 NULL
, /* satisfied */
173 no_signal
, /* signal */
174 no_get_fd
, /* get_fd */
175 no_map_access
, /* map_access */
176 no_lookup_name
, /* lookup_name */
177 no_close_handle
, /* close_handle */
178 thread_input_destroy
/* destroy */
181 /* pointer to input structure of foreground thread */
182 static struct thread_input
*foreground_input
;
183 static unsigned int last_input_time
;
186 /* set the caret window in a given thread input */
187 static void set_caret_window( struct thread_input
*input
, user_handle_t win
)
189 if (!win
|| win
!= input
->caret
)
191 input
->caret_rect
.left
= 0;
192 input
->caret_rect
.top
= 0;
193 input
->caret_rect
.right
= 0;
194 input
->caret_rect
.bottom
= 0;
197 input
->caret_hide
= 1;
198 input
->caret_state
= 0;
201 /* create a thread input object */
202 static struct thread_input
*create_thread_input( struct thread
*thread
)
204 struct thread_input
*input
;
206 if ((input
= alloc_object( &thread_input_ops
)))
211 input
->menu_owner
= 0;
212 input
->move_size
= 0;
213 list_init( &input
->msg_list
);
214 set_caret_window( input
, 0 );
215 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
217 if (!(input
->desktop
= get_thread_desktop( thread
, 0 /* FIXME: access rights */ )))
219 release_object( input
);
226 /* release the thread input data of a given thread */
227 static inline void release_thread_input( struct thread
*thread
)
229 struct thread_input
*input
= thread
->queue
->input
;
232 release_object( input
);
233 thread
->queue
->input
= NULL
;
236 /* create a message queue object */
237 static struct msg_queue
*create_msg_queue( struct thread
*thread
, struct thread_input
*input
)
239 struct msg_queue
*queue
;
242 if (!input
&& !(input
= create_thread_input( thread
))) return NULL
;
243 if ((queue
= alloc_object( &msg_queue_ops
)))
245 queue
->wake_bits
= 0;
246 queue
->wake_mask
= 0;
247 queue
->changed_bits
= 0;
248 queue
->changed_mask
= 0;
249 queue
->paint_count
= 0;
250 queue
->quit_message
= 0;
251 queue
->recv_result
= NULL
;
252 queue
->next_timer_id
= 1;
253 queue
->timeout
= NULL
;
254 queue
->input
= (struct thread_input
*)grab_object( input
);
256 gettimeofday( &queue
->last_get_msg
, NULL
);
257 list_init( &queue
->send_result
);
258 list_init( &queue
->callback_result
);
259 list_init( &queue
->pending_timers
);
260 list_init( &queue
->expired_timers
);
261 for (i
= 0; i
< NB_MSG_KINDS
; i
++) list_init( &queue
->msg_list
[i
] );
263 thread
->queue
= queue
;
264 if (!thread
->process
->queue
)
265 thread
->process
->queue
= (struct msg_queue
*)grab_object( queue
);
267 release_object( input
);
271 /* free the message queue of a thread at thread exit */
272 void free_msg_queue( struct thread
*thread
)
274 struct process
*process
= thread
->process
;
276 remove_thread_hooks( thread
);
277 if (!thread
->queue
) return;
278 if (process
->queue
== thread
->queue
) /* is it the process main queue? */
280 release_object( process
->queue
);
281 process
->queue
= NULL
;
282 if (process
->idle_event
)
284 set_event( process
->idle_event
);
285 release_object( process
->idle_event
);
286 process
->idle_event
= NULL
;
289 release_object( thread
->queue
);
290 thread
->queue
= NULL
;
293 /* get the hook table for a given thread */
294 struct hook_table
*get_queue_hooks( struct thread
*thread
)
296 if (!thread
->queue
) return NULL
;
297 return thread
->queue
->hooks
;
300 /* set the hook table for a given thread, allocating the queue if needed */
301 void set_queue_hooks( struct thread
*thread
, struct hook_table
*hooks
)
303 struct msg_queue
*queue
= thread
->queue
;
304 if (!queue
&& !(queue
= create_msg_queue( thread
, NULL
))) return;
305 if (queue
->hooks
) release_object( queue
->hooks
);
306 queue
->hooks
= hooks
;
309 /* check the queue status */
310 inline static int is_signaled( struct msg_queue
*queue
)
312 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
315 /* set some queue bits */
316 inline static void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
318 queue
->wake_bits
|= bits
;
319 queue
->changed_bits
|= bits
;
320 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
323 /* clear some queue bits */
324 inline static void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
326 queue
->wake_bits
&= ~bits
;
327 queue
->changed_bits
&= ~bits
;
330 /* check whether msg is a keyboard message */
331 inline static int is_keyboard_msg( struct message
*msg
)
333 return (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
);
336 /* check if message is matched by the filter */
337 inline static int check_msg_filter( unsigned int msg
, unsigned int first
, unsigned int last
)
339 return (msg
>= first
&& msg
<= last
);
342 /* check whether a message filter contains at least one potential hardware message */
343 inline static int filter_contains_hw_range( unsigned int first
, unsigned int last
)
345 /* hardware message ranges are (in numerical order):
346 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
347 * WM_KEYFIRST .. WM_KEYLAST
348 * WM_MOUSEFIRST .. WM_MOUSELAST
350 if (last
< WM_NCMOUSEFIRST
) return 0;
351 if (first
> WM_NCMOUSELAST
&& last
< WM_KEYFIRST
) return 0;
352 if (first
> WM_KEYLAST
&& last
< WM_MOUSEFIRST
) return 0;
353 if (first
> WM_MOUSELAST
) return 0;
357 /* get the QS_* bit corresponding to a given hardware message */
358 inline static int get_hardware_msg_bit( struct message
*msg
)
360 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
361 if (is_keyboard_msg( msg
)) return QS_KEY
;
362 return QS_MOUSEBUTTON
;
365 /* get the current thread queue, creating it if needed */
366 inline static struct msg_queue
*get_current_queue(void)
368 struct msg_queue
*queue
= current
->queue
;
369 if (!queue
) queue
= create_msg_queue( current
, NULL
);
373 /* get a (pseudo-)unique id to tag hardware messages */
374 inline static unsigned int get_unique_id(void)
376 static unsigned int id
;
377 if (!++id
) id
= 1; /* avoid an id of 0 */
381 /* try to merge a message with the last in the list; return 1 if successful */
382 static int merge_message( struct thread_input
*input
, const struct message
*msg
)
384 struct message
*prev
;
385 struct list
*ptr
= list_tail( &input
->msg_list
);
388 prev
= LIST_ENTRY( ptr
, struct message
, entry
);
389 if (prev
->unique_id
) return 0;
390 if (prev
->result
) return 0;
391 if (prev
->win
!= msg
->win
) return 0;
392 if (prev
->msg
!= msg
->msg
) return 0;
393 if (prev
->type
!= msg
->type
) return 0;
394 /* now we can merge it */
395 prev
->wparam
= msg
->wparam
;
396 prev
->lparam
= msg
->lparam
;
399 prev
->time
= msg
->time
;
400 prev
->info
= msg
->info
;
404 /* free a result structure */
405 static void free_result( struct message_result
*result
)
407 if (result
->timeout
) remove_timeout_user( result
->timeout
);
408 if (result
->data
) free( result
->data
);
409 if (result
->callback_msg
) free( result
->callback_msg
);
413 /* remove the result from the sender list it is on */
414 static inline void remove_result_from_sender( struct message_result
*result
)
416 assert( result
->sender
);
418 list_remove( &result
->sender_entry
);
419 result
->sender
= NULL
;
420 if (!result
->receiver
) free_result( result
);
423 /* store the message result in the appropriate structure */
424 static void store_message_result( struct message_result
*res
, unsigned int result
,
427 res
->result
= result
;
432 remove_timeout_user( res
->timeout
);
437 if (res
->callback_msg
)
439 /* queue the callback message in the sender queue */
440 res
->callback_msg
->lparam
= result
;
441 list_add_tail( &res
->sender
->msg_list
[SEND_MESSAGE
], &res
->callback_msg
->entry
);
442 set_queue_bits( res
->sender
, QS_SENDMESSAGE
);
443 res
->callback_msg
= NULL
;
444 remove_result_from_sender( res
);
448 /* wake sender queue if waiting on this result */
449 if (list_head(&res
->sender
->send_result
) == &res
->sender_entry
)
450 set_queue_bits( res
->sender
, QS_SMRESULT
);
456 /* free a message when deleting a queue or window */
457 static void free_message( struct message
*msg
)
459 struct message_result
*result
= msg
->result
;
465 result
->receiver
= NULL
;
466 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
468 else free_result( result
);
470 if (msg
->data
) free( msg
->data
);
474 /* remove (and free) a message from a message list */
475 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
476 enum message_kind kind
)
478 list_remove( &msg
->entry
);
482 if (list_empty( &queue
->msg_list
[kind
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
485 if (list_empty( &queue
->msg_list
[kind
] ) && !queue
->quit_message
)
486 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
492 /* message timed out without getting a reply */
493 static void result_timeout( void *private )
495 struct message_result
*result
= private;
497 assert( !result
->replied
);
499 result
->timeout
= NULL
;
501 if (result
->msg
) /* not received yet */
503 struct message
*msg
= result
->msg
;
507 remove_queue_message( result
->receiver
, msg
, SEND_MESSAGE
);
508 result
->receiver
= NULL
;
511 free_result( result
);
516 store_message_result( result
, 0, STATUS_TIMEOUT
);
519 /* allocate and fill a message result structure */
520 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
521 struct msg_queue
*recv_queue
,
522 struct message
*msg
, int timeout
,
523 void *callback
, unsigned int callback_data
)
525 struct message_result
*result
= mem_alloc( sizeof(*result
) );
529 result
->sender
= send_queue
;
530 result
->receiver
= recv_queue
;
533 result
->data_size
= 0;
534 result
->timeout
= NULL
;
536 if (msg
->type
== MSG_CALLBACK
)
538 struct message
*callback_msg
= mem_alloc( sizeof(*callback_msg
) );
544 callback_msg
->type
= MSG_CALLBACK_RESULT
;
545 callback_msg
->win
= msg
->win
;
546 callback_msg
->msg
= msg
->msg
;
547 callback_msg
->wparam
= (unsigned long)callback
;
548 callback_msg
->lparam
= 0;
549 callback_msg
->time
= get_tick_count();
552 callback_msg
->info
= callback_data
;
553 callback_msg
->hook
= 0;
554 callback_msg
->hook_proc
= NULL
;
555 callback_msg
->result
= NULL
;
556 callback_msg
->data
= NULL
;
557 callback_msg
->data_size
= 0;
559 result
->callback_msg
= callback_msg
;
560 list_add_head( &send_queue
->callback_result
, &result
->sender_entry
);
564 result
->callback_msg
= NULL
;
565 list_add_head( &send_queue
->send_result
, &result
->sender_entry
);
571 gettimeofday( &when
, NULL
);
572 add_timeout( &when
, timeout
);
573 result
->timeout
= add_timeout_user( &when
, result_timeout
, result
);
579 /* receive a message, removing it from the sent queue */
580 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
581 struct get_message_reply
*reply
)
583 struct message_result
*result
= msg
->result
;
585 reply
->total
= msg
->data_size
;
586 if (msg
->data_size
> get_reply_max_size())
588 set_error( STATUS_BUFFER_OVERFLOW
);
591 reply
->type
= msg
->type
;
592 reply
->win
= msg
->win
;
593 reply
->msg
= msg
->msg
;
594 reply
->wparam
= msg
->wparam
;
595 reply
->lparam
= msg
->lparam
;
598 reply
->time
= msg
->time
;
599 reply
->info
= msg
->info
;
600 reply
->hook
= msg
->hook
;
601 reply
->hook_proc
= msg
->hook_proc
;
603 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
605 list_remove( &msg
->entry
);
606 /* put the result on the receiver result stack */
610 result
->recv_next
= queue
->recv_result
;
611 queue
->recv_result
= result
;
614 if (list_empty( &queue
->msg_list
[SEND_MESSAGE
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
617 /* set the result of the current received message */
618 static void reply_message( struct msg_queue
*queue
, unsigned int result
,
619 unsigned int error
, int remove
, const void *data
, size_t len
)
621 struct message_result
*res
= queue
->recv_result
;
625 queue
->recv_result
= res
->recv_next
;
626 res
->receiver
= NULL
;
627 if (!res
->sender
) /* no one waiting for it */
635 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
636 store_message_result( res
, result
, error
);
640 /* retrieve a posted message */
641 static int get_posted_message( struct msg_queue
*queue
, user_handle_t win
,
642 unsigned int first
, unsigned int last
, unsigned int flags
,
643 struct get_message_reply
*reply
)
647 /* check against the filters */
648 LIST_FOR_EACH_ENTRY( msg
, &queue
->msg_list
[POST_MESSAGE
], struct message
, entry
)
650 if (win
&& msg
->win
&& msg
->win
!= win
&& !is_child_window( win
, msg
->win
)) continue;
651 if (!check_msg_filter( msg
->msg
, first
, last
)) continue;
652 goto found
; /* found one */
656 /* return it to the app */
658 reply
->total
= msg
->data_size
;
659 if (msg
->data_size
> get_reply_max_size())
661 set_error( STATUS_BUFFER_OVERFLOW
);
664 reply
->type
= msg
->type
;
665 reply
->win
= msg
->win
;
666 reply
->msg
= msg
->msg
;
667 reply
->wparam
= msg
->wparam
;
668 reply
->lparam
= msg
->lparam
;
671 reply
->time
= msg
->time
;
672 reply
->info
= msg
->info
;
674 if (flags
& GET_MSG_REMOVE
)
678 set_reply_data_ptr( msg
->data
, msg
->data_size
);
682 remove_queue_message( queue
, msg
, POST_MESSAGE
);
684 else if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
689 static int get_quit_message( struct msg_queue
*queue
, unsigned int flags
,
690 struct get_message_reply
*reply
)
692 if (queue
->quit_message
)
695 reply
->type
= MSG_POSTED
;
697 reply
->msg
= WM_QUIT
;
698 reply
->wparam
= queue
->exit_code
;
702 reply
->time
= get_tick_count();
705 if (flags
& GET_MSG_REMOVE
)
707 queue
->quit_message
= 0;
708 if (list_empty( &queue
->msg_list
[POST_MESSAGE
] ))
709 clear_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
717 /* empty a message list and free all the messages */
718 static void empty_msg_list( struct list
*list
)
722 while ((ptr
= list_head( list
)) != NULL
)
724 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
725 list_remove( &msg
->entry
);
730 /* cleanup all pending results when deleting a queue */
731 static void cleanup_results( struct msg_queue
*queue
)
735 while ((entry
= list_head( &queue
->send_result
)) != NULL
)
737 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
740 while ((entry
= list_head( &queue
->callback_result
)) != NULL
)
742 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
745 while (queue
->recv_result
)
746 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
749 /* check if the thread owning the queue is hung (not checking for messages) */
750 static int is_queue_hung( struct msg_queue
*queue
)
753 struct wait_queue_entry
*entry
;
755 gettimeofday( &now
, NULL
);
756 if (now
.tv_sec
- queue
->last_get_msg
.tv_sec
<= 5)
757 return 0; /* less than 5 seconds since last get message -> not hung */
759 LIST_FOR_EACH_ENTRY( entry
, &queue
->obj
.wait_queue
, struct wait_queue_entry
, entry
)
761 if (entry
->thread
->queue
== queue
)
762 return 0; /* thread is waiting on queue -> not hung */
767 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
769 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
770 struct process
*process
= entry
->thread
->process
;
772 /* a thread can only wait on its own queue */
773 if (entry
->thread
->queue
!= queue
)
775 set_error( STATUS_ACCESS_DENIED
);
778 /* if waiting on the main process queue, set the idle event */
779 if (process
->queue
== queue
)
781 if (process
->idle_event
) set_event( process
->idle_event
);
783 add_queue( obj
, entry
);
787 static void msg_queue_remove_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 remove_queue( obj
, entry
);
794 assert( entry
->thread
->queue
== queue
);
796 /* if waiting on the main process queue, reset the idle event */
797 if (process
->queue
== queue
)
799 if (process
->idle_event
) reset_event( process
->idle_event
);
803 static void msg_queue_dump( struct object
*obj
, int verbose
)
805 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
806 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
807 queue
->wake_bits
, queue
->wake_mask
);
810 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
)
812 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
813 return is_signaled( queue
);
816 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
)
818 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
819 queue
->wake_mask
= 0;
820 queue
->changed_mask
= 0;
821 return 0; /* Not abandoned */
824 static void msg_queue_destroy( struct object
*obj
)
826 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
830 cleanup_results( queue
);
831 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
833 while ((ptr
= list_head( &queue
->pending_timers
)))
835 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
836 list_remove( &timer
->entry
);
839 while ((ptr
= list_head( &queue
->expired_timers
)))
841 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
842 list_remove( &timer
->entry
);
845 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
846 if (queue
->input
) release_object( queue
->input
);
847 if (queue
->hooks
) release_object( queue
->hooks
);
850 static void thread_input_dump( struct object
*obj
, int verbose
)
852 struct thread_input
*input
= (struct thread_input
*)obj
;
853 fprintf( stderr
, "Thread input focus=%p capture=%p active=%p\n",
854 input
->focus
, input
->capture
, input
->active
);
857 static void thread_input_destroy( struct object
*obj
)
859 struct thread_input
*input
= (struct thread_input
*)obj
;
861 if (foreground_input
== input
) foreground_input
= NULL
;
862 empty_msg_list( &input
->msg_list
);
863 if (input
->desktop
) release_object( input
->desktop
);
866 /* fix the thread input data when a window is destroyed */
867 inline static void thread_input_cleanup_window( struct msg_queue
*queue
, user_handle_t window
)
869 struct thread_input
*input
= queue
->input
;
871 if (window
== input
->focus
) input
->focus
= 0;
872 if (window
== input
->capture
) input
->capture
= 0;
873 if (window
== input
->active
) input
->active
= 0;
874 if (window
== input
->menu_owner
) input
->menu_owner
= 0;
875 if (window
== input
->move_size
) input
->move_size
= 0;
876 if (window
== input
->caret
) set_caret_window( input
, 0 );
879 /* check if the specified window can be set in the input data of a given queue */
880 static int check_queue_input_window( struct msg_queue
*queue
, user_handle_t window
)
882 struct thread
*thread
;
885 if (!window
) return 1; /* we can always clear the data */
887 if ((thread
= get_window_thread( window
)))
889 ret
= (queue
->input
== thread
->queue
->input
);
890 if (!ret
) set_error( STATUS_ACCESS_DENIED
);
891 release_object( thread
);
893 else set_error( STATUS_INVALID_HANDLE
);
898 /* make sure the specified thread has a queue */
899 int init_thread_queue( struct thread
*thread
)
901 if (thread
->queue
) return 1;
902 return (create_msg_queue( thread
, NULL
) != NULL
);
905 /* attach two thread input data structures */
906 int attach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
908 struct desktop
*desktop
;
909 struct thread_input
*input
;
911 if (!thread_to
->queue
&& !(thread_to
->queue
= create_msg_queue( thread_to
, NULL
))) return 0;
912 if (!(desktop
= get_thread_desktop( thread_from
, 0 ))) return 0;
913 input
= (struct thread_input
*)grab_object( thread_to
->queue
->input
);
914 if (input
->desktop
!= desktop
)
916 set_error( STATUS_ACCESS_DENIED
);
917 release_object( input
);
918 release_object( desktop
);
921 release_object( desktop
);
923 if (thread_from
->queue
)
925 release_thread_input( thread_from
);
926 thread_from
->queue
->input
= input
;
930 if (!(thread_from
->queue
= create_msg_queue( thread_from
, input
))) return 0;
932 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
936 /* detach two thread input data structures */
937 void detach_thread_input( struct thread
*thread_from
)
939 struct thread_input
*input
;
941 if ((input
= create_thread_input( thread_from
)))
943 release_thread_input( thread_from
);
944 thread_from
->queue
->input
= input
;
949 /* set the next timer to expire */
950 static void set_next_timer( struct msg_queue
*queue
)
956 remove_timeout_user( queue
->timeout
);
957 queue
->timeout
= NULL
;
959 if ((ptr
= list_head( &queue
->pending_timers
)))
961 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
962 queue
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, queue
);
964 /* set/clear QS_TIMER bit */
965 if (list_empty( &queue
->expired_timers
))
966 clear_queue_bits( queue
, QS_TIMER
);
968 set_queue_bits( queue
, QS_TIMER
);
971 /* find a timer from its window and id */
972 static struct timer
*find_timer( struct msg_queue
*queue
, user_handle_t win
,
973 unsigned int msg
, unsigned int id
)
977 /* we need to search both lists */
979 LIST_FOR_EACH( ptr
, &queue
->pending_timers
)
981 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
982 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
984 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
986 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
987 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
992 /* callback for the next timer expiration */
993 static void timer_callback( void *private )
995 struct msg_queue
*queue
= private;
998 queue
->timeout
= NULL
;
999 /* move on to the next timer */
1000 ptr
= list_head( &queue
->pending_timers
);
1002 list_add_tail( &queue
->expired_timers
, ptr
);
1003 set_next_timer( queue
);
1006 /* link a timer at its rightful place in the queue list */
1007 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
1011 for (ptr
= queue
->pending_timers
.next
; ptr
!= &queue
->pending_timers
; ptr
= ptr
->next
)
1013 struct timer
*t
= LIST_ENTRY( ptr
, struct timer
, entry
);
1014 if (!time_before( &t
->when
, &timer
->when
)) break;
1016 list_add_before( ptr
, &timer
->entry
);
1019 /* remove a timer from the queue timer list and free it */
1020 static void free_timer( struct msg_queue
*queue
, struct timer
*timer
)
1022 list_remove( &timer
->entry
);
1024 set_next_timer( queue
);
1027 /* restart an expired timer */
1028 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
1032 list_remove( &timer
->entry
);
1033 gettimeofday( &now
, NULL
);
1034 while (!time_before( &now
, &timer
->when
)) add_timeout( &timer
->when
, timer
->rate
);
1035 link_timer( queue
, timer
);
1036 set_next_timer( queue
);
1039 /* find an expired timer matching the filtering parameters */
1040 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
1041 unsigned int get_first
, unsigned int get_last
,
1046 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
1048 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1049 if (win
&& timer
->win
!= win
) continue;
1050 if (check_msg_filter( timer
->msg
, get_first
, get_last
))
1052 if (remove
) restart_timer( queue
, timer
);
1060 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
1062 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
1065 timer
->rate
= max( rate
, 1 );
1066 gettimeofday( &timer
->when
, NULL
);
1067 add_timeout( &timer
->when
, rate
);
1068 link_timer( queue
, timer
);
1069 /* check if we replaced the next timer */
1070 if (list_head( &queue
->pending_timers
) == &timer
->entry
) set_next_timer( queue
);
1075 /* change the input key state for a given key */
1076 static void set_input_key_state( struct thread_input
*input
, unsigned char key
, int down
)
1080 if (!(input
->keystate
[key
] & 0x80)) input
->keystate
[key
] ^= 0x01;
1081 input
->keystate
[key
] |= 0x80;
1083 else input
->keystate
[key
] &= ~0x80;
1086 /* update the input key state for a keyboard message */
1087 static void update_input_key_state( struct thread_input
*input
, const struct message
*msg
)
1090 int down
= 0, extended
;
1094 case WM_LBUTTONDOWN
:
1098 set_input_key_state( input
, VK_LBUTTON
, down
);
1100 case WM_MBUTTONDOWN
:
1104 set_input_key_state( input
, VK_MBUTTON
, down
);
1106 case WM_RBUTTONDOWN
:
1110 set_input_key_state( input
, VK_RBUTTON
, down
);
1112 case WM_XBUTTONDOWN
:
1116 if (msg
->wparam
== XBUTTON1
) set_input_key_state( input
, VK_XBUTTON1
, down
);
1117 else if (msg
->wparam
== XBUTTON2
) set_input_key_state( input
, VK_XBUTTON2
, down
);
1125 key
= (unsigned char)msg
->wparam
;
1126 extended
= ((msg
->lparam
>> 16) & KF_EXTENDED
) != 0;
1127 set_input_key_state( input
, key
, down
);
1131 set_input_key_state( input
, extended
? VK_RSHIFT
: VK_LSHIFT
, down
);
1134 set_input_key_state( input
, extended
? VK_RCONTROL
: VK_LCONTROL
, down
);
1137 set_input_key_state( input
, extended
? VK_RMENU
: VK_LMENU
, down
);
1141 set_input_key_state( input
, VK_CONTROL
, down
);
1145 set_input_key_state( input
, VK_MENU
, down
);
1149 set_input_key_state( input
, VK_SHIFT
, down
);
1156 /* release the hardware message currently being processed by the given thread */
1157 static void release_hardware_message( struct msg_queue
*queue
, unsigned int hw_id
,
1158 int remove
, user_handle_t new_win
)
1160 struct thread_input
*input
= queue
->input
;
1161 struct message
*msg
;
1163 LIST_FOR_EACH_ENTRY( msg
, &input
->msg_list
, struct message
, entry
)
1165 if (msg
->unique_id
== hw_id
) break;
1167 if (&msg
->entry
== &input
->msg_list
) return; /* not found */
1169 /* clear the queue bit for that message */
1170 if (remove
|| new_win
)
1172 struct message
*other
;
1175 clr_bit
= get_hardware_msg_bit( msg
);
1176 LIST_FOR_EACH_ENTRY( other
, &input
->msg_list
, struct message
, entry
)
1178 if (other
!= msg
&& get_hardware_msg_bit( other
) == clr_bit
)
1184 if (clr_bit
) clear_queue_bits( queue
, clr_bit
);
1187 if (new_win
) /* set the new window */
1189 struct thread
*owner
= get_window_thread( new_win
);
1192 if (owner
->queue
->input
== input
)
1195 set_queue_bits( owner
->queue
, get_hardware_msg_bit( msg
));
1198 release_object( owner
);
1203 update_input_key_state( input
, msg
);
1204 list_remove( &msg
->entry
);
1205 free_message( msg
);
1209 /* find the window that should receive a given hardware message */
1210 static user_handle_t
find_hardware_message_window( struct thread_input
*input
, struct message
*msg
,
1211 unsigned int *msg_code
)
1213 user_handle_t win
= 0;
1215 *msg_code
= msg
->msg
;
1216 if (is_keyboard_msg( msg
))
1218 if (input
&& !(win
= input
->focus
))
1220 win
= input
->active
;
1221 if (*msg_code
< WM_SYSKEYDOWN
) *msg_code
+= WM_SYSKEYDOWN
- WM_KEYDOWN
;
1224 else /* mouse message */
1226 if (!input
|| !(win
= input
->capture
))
1228 if (!(win
= msg
->win
) || !is_window_visible( win
))
1230 if (input
) win
= window_from_point( input
->desktop
, msg
->x
, msg
->y
);
1237 /* queue a hardware message into a given thread input */
1238 static void queue_hardware_message( struct msg_queue
*queue
, struct message
*msg
)
1241 struct thread
*thread
;
1242 struct thread_input
*input
= queue
? queue
->input
: foreground_input
;
1243 unsigned int msg_code
;
1245 last_input_time
= get_tick_count();
1246 win
= find_hardware_message_window( input
, msg
, &msg_code
);
1247 if (!win
|| !(thread
= get_window_thread(win
)))
1249 if (input
) update_input_key_state( input
, msg
);
1253 input
= thread
->queue
->input
;
1255 if (msg
->msg
== WM_MOUSEMOVE
&& merge_message( input
, msg
)) free( msg
);
1258 msg
->unique_id
= 0; /* will be set once we return it to the app */
1259 list_add_tail( &input
->msg_list
, &msg
->entry
);
1260 set_queue_bits( thread
->queue
, get_hardware_msg_bit(msg
) );
1262 release_object( thread
);
1265 /* check message filter for a hardware message */
1266 static int check_hw_message_filter( user_handle_t win
, unsigned int msg_code
,
1267 user_handle_t filter_win
, unsigned int first
, unsigned int last
)
1269 if (msg_code
>= WM_KEYFIRST
&& msg_code
<= WM_KEYLAST
)
1271 /* we can only test the window for a keyboard message since the
1272 * dest window for a mouse message depends on hittest */
1273 if (filter_win
&& win
!= filter_win
&& !is_child_window( filter_win
, win
))
1275 /* the message code is final for a keyboard message, we can simply check it */
1276 return check_msg_filter( msg_code
, first
, last
);
1278 else /* mouse message */
1280 /* we need to check all possible values that the message can have in the end */
1282 if (check_msg_filter( msg_code
, first
, last
)) return 1;
1283 if (msg_code
== WM_MOUSEWHEEL
) return 0; /* no other possible value for this one */
1285 /* all other messages can become non-client messages */
1286 if (check_msg_filter( msg_code
+ (WM_NCMOUSEFIRST
- WM_MOUSEFIRST
), first
, last
)) return 1;
1288 /* clicks can become double-clicks or non-client double-clicks */
1289 if (msg_code
== WM_LBUTTONDOWN
|| msg_code
== WM_MBUTTONDOWN
||
1290 msg_code
== WM_RBUTTONDOWN
|| msg_code
== WM_XBUTTONDOWN
)
1292 if (check_msg_filter( msg_code
+ (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1293 if (check_msg_filter( msg_code
+ (WM_NCLBUTTONDBLCLK
- WM_LBUTTONDOWN
), first
, last
)) return 1;
1300 /* find a hardware message for the given queue */
1301 static int get_hardware_message( struct thread
*thread
, int hw_id
, user_handle_t filter_win
,
1302 unsigned int first
, unsigned int last
, struct get_message_reply
*reply
)
1304 struct thread_input
*input
= thread
->queue
->input
;
1305 struct thread
*win_thread
;
1308 int clear_bits
, got_one
= 0;
1309 unsigned int msg_code
;
1311 ptr
= list_head( &input
->msg_list
);
1316 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1317 if (msg
->unique_id
== hw_id
) break;
1318 ptr
= list_next( &input
->msg_list
, ptr
);
1320 if (!ptr
) ptr
= list_head( &input
->msg_list
);
1321 else ptr
= list_next( &input
->msg_list
, ptr
); /* start from the next one */
1324 if (ptr
== list_head( &input
->msg_list
))
1325 clear_bits
= QS_KEY
| QS_MOUSEMOVE
| QS_MOUSEBUTTON
;
1327 clear_bits
= 0; /* don't clear bits if we don't go through the whole list */
1331 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1332 ptr
= list_next( &input
->msg_list
, ptr
);
1333 win
= find_hardware_message_window( input
, msg
, &msg_code
);
1334 if (!win
|| !(win_thread
= get_window_thread( win
)))
1336 /* no window at all, remove it */
1337 update_input_key_state( input
, msg
);
1338 list_remove( &msg
->entry
);
1339 free_message( msg
);
1342 if (win_thread
!= thread
)
1344 if (win_thread
->queue
->input
== input
)
1346 /* wake the other thread */
1347 set_queue_bits( win_thread
->queue
, get_hardware_msg_bit(msg
) );
1352 /* for another thread input, drop it */
1353 update_input_key_state( input
, msg
);
1354 list_remove( &msg
->entry
);
1355 free_message( msg
);
1357 release_object( win_thread
);
1360 release_object( win_thread
);
1362 /* if we already got a message for another thread, or if it doesn't
1363 * match the filter we skip it */
1364 if (got_one
|| !check_hw_message_filter( win
, msg_code
, filter_win
, first
, last
))
1366 clear_bits
&= ~get_hardware_msg_bit( msg
);
1369 /* now we can return it */
1370 if (!msg
->unique_id
) msg
->unique_id
= get_unique_id();
1371 reply
->type
= MSG_HARDWARE
;
1373 reply
->msg
= msg_code
;
1374 reply
->wparam
= msg
->wparam
;
1375 reply
->lparam
= msg
->lparam
;
1378 reply
->time
= msg
->time
;
1379 reply
->info
= msg
->info
;
1380 reply
->hw_id
= msg
->unique_id
;
1383 /* nothing found, clear the hardware queue bits */
1384 clear_queue_bits( thread
->queue
, clear_bits
);
1388 /* increment (or decrement if 'incr' is negative) the queue paint count */
1389 void inc_queue_paint_count( struct thread
*thread
, int incr
)
1391 struct msg_queue
*queue
= thread
->queue
;
1395 if ((queue
->paint_count
+= incr
) < 0) queue
->paint_count
= 0;
1397 if (queue
->paint_count
)
1398 set_queue_bits( queue
, QS_PAINT
);
1400 clear_queue_bits( queue
, QS_PAINT
);
1404 /* remove all messages and timers belonging to a certain window */
1405 void queue_cleanup_window( struct thread
*thread
, user_handle_t win
)
1407 struct msg_queue
*queue
= thread
->queue
;
1415 ptr
= list_head( &queue
->pending_timers
);
1418 struct list
*next
= list_next( &queue
->pending_timers
, ptr
);
1419 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1420 if (timer
->win
== win
) free_timer( queue
, timer
);
1423 ptr
= list_head( &queue
->expired_timers
);
1426 struct list
*next
= list_next( &queue
->expired_timers
, ptr
);
1427 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1428 if (timer
->win
== win
) free_timer( queue
, timer
);
1432 /* remove messages */
1433 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
1435 struct list
*ptr
, *next
;
1437 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->msg_list
[i
] )
1439 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1440 if (msg
->win
== win
) remove_queue_message( queue
, msg
, i
);
1444 thread_input_cleanup_window( queue
, win
);
1447 /* post a message to a window; used by socket handling */
1448 void post_message( user_handle_t win
, unsigned int message
,
1449 unsigned long wparam
, unsigned long lparam
)
1451 struct message
*msg
;
1452 struct thread
*thread
= get_window_thread( win
);
1454 if (!thread
) return;
1456 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1458 msg
->type
= MSG_POSTED
;
1459 msg
->win
= get_user_full_handle( win
);
1461 msg
->wparam
= wparam
;
1462 msg
->lparam
= lparam
;
1463 msg
->time
= get_tick_count();
1468 msg
->hook_proc
= NULL
;
1473 list_add_tail( &thread
->queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1474 set_queue_bits( thread
->queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
1476 release_object( thread
);
1479 /* post a win event */
1480 void post_win_event( struct thread
*thread
, unsigned int event
,
1481 user_handle_t win
, unsigned int object_id
,
1482 unsigned int child_id
, void *hook_proc
,
1483 const WCHAR
*module
, size_t module_size
,
1486 struct message
*msg
;
1488 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1490 msg
->type
= MSG_WINEVENT
;
1491 msg
->win
= get_user_full_handle( win
);
1493 msg
->wparam
= object_id
;
1494 msg
->lparam
= child_id
;
1495 msg
->time
= get_tick_count();
1498 msg
->info
= get_thread_id( current
);
1501 msg
->hook_proc
= hook_proc
;
1503 if ((msg
->data
= malloc( module_size
)))
1505 msg
->data_size
= module_size
;
1506 memcpy( msg
->data
, module
, module_size
);
1508 if (debug_level
> 1)
1509 fprintf( stderr
, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1510 get_thread_id(thread
), event
, win
, object_id
, child_id
);
1511 list_add_tail( &thread
->queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1512 set_queue_bits( thread
->queue
, QS_SENDMESSAGE
);
1519 /* get the message queue of the current thread */
1520 DECL_HANDLER(get_msg_queue
)
1522 struct msg_queue
*queue
= get_current_queue();
1525 if (queue
) reply
->handle
= alloc_handle( current
->process
, queue
, SYNCHRONIZE
, 0 );
1529 /* set the current message queue wakeup mask */
1530 DECL_HANDLER(set_queue_mask
)
1532 struct msg_queue
*queue
= get_current_queue();
1536 queue
->wake_mask
= req
->wake_mask
;
1537 queue
->changed_mask
= req
->changed_mask
;
1538 reply
->wake_bits
= queue
->wake_bits
;
1539 reply
->changed_bits
= queue
->changed_bits
;
1540 if (is_signaled( queue
))
1542 /* if skip wait is set, do what would have been done in the subsequent wait */
1543 if (req
->skip_wait
) msg_queue_satisfied( &queue
->obj
, current
);
1544 else wake_up( &queue
->obj
, 0 );
1550 /* get the current message queue status */
1551 DECL_HANDLER(get_queue_status
)
1553 struct msg_queue
*queue
= current
->queue
;
1556 reply
->wake_bits
= queue
->wake_bits
;
1557 reply
->changed_bits
= queue
->changed_bits
;
1558 if (req
->clear
) queue
->changed_bits
= 0;
1560 else reply
->wake_bits
= reply
->changed_bits
= 0;
1564 /* send a message to a thread queue */
1565 DECL_HANDLER(send_message
)
1567 struct message
*msg
;
1568 struct msg_queue
*send_queue
= get_current_queue();
1569 struct msg_queue
*recv_queue
= NULL
;
1570 struct thread
*thread
= NULL
;
1574 if (!(thread
= get_thread_from_id( req
->id
))) return;
1576 else if (req
->type
!= MSG_HARDWARE
)
1578 /* only hardware messages are allowed without destination thread */
1579 set_error( STATUS_INVALID_PARAMETER
);
1583 if (thread
&& !(recv_queue
= thread
->queue
))
1585 set_error( STATUS_INVALID_PARAMETER
);
1586 release_object( thread
);
1589 if (recv_queue
&& (req
->flags
& SEND_MSG_ABORT_IF_HUNG
) && is_queue_hung(recv_queue
))
1591 set_error( STATUS_TIMEOUT
);
1592 release_object( thread
);
1596 if ((msg
= mem_alloc( sizeof(*msg
) )))
1598 msg
->type
= req
->type
;
1599 msg
->win
= get_user_full_handle( req
->win
);
1600 msg
->msg
= req
->msg
;
1601 msg
->wparam
= req
->wparam
;
1602 msg
->lparam
= req
->lparam
;
1603 msg
->time
= req
->time
;
1606 msg
->info
= req
->info
;
1608 msg
->hook_proc
= NULL
;
1615 case MSG_OTHER_PROCESS
:
1616 msg
->data_size
= get_req_data_size();
1617 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1626 if (!(msg
->result
= alloc_message_result( send_queue
, recv_queue
, msg
,
1627 req
->timeout
, req
->callback
, req
->info
)))
1629 free_message( msg
);
1634 list_add_tail( &recv_queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1635 set_queue_bits( recv_queue
, QS_SENDMESSAGE
);
1638 /* needed for posted DDE messages */
1639 msg
->data_size
= get_req_data_size();
1640 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1645 list_add_tail( &recv_queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1646 set_queue_bits( recv_queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
1649 queue_hardware_message( recv_queue
, msg
);
1651 case MSG_CALLBACK_RESULT
: /* cannot send this one */
1653 set_error( STATUS_INVALID_PARAMETER
);
1658 if (thread
) release_object( thread
);
1661 /* post a quit message to the current queue */
1662 DECL_HANDLER(post_quit_message
)
1664 struct msg_queue
*queue
= get_current_queue();
1669 queue
->quit_message
= 1;
1670 queue
->exit_code
= req
->exit_code
;
1671 set_queue_bits( queue
, QS_POSTMESSAGE
|QS_ALLPOSTMESSAGE
);
1674 /* get a message from the current queue */
1675 DECL_HANDLER(get_message
)
1677 struct timer
*timer
;
1679 struct msg_queue
*queue
= get_current_queue();
1680 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
1682 reply
->active_hooks
= get_active_hooks();
1685 gettimeofday( &queue
->last_get_msg
, NULL
);
1687 /* first check for sent messages */
1688 if ((ptr
= list_head( &queue
->msg_list
[SEND_MESSAGE
] )))
1690 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1691 receive_message( queue
, msg
, reply
);
1694 if (req
->flags
& GET_MSG_SENT_ONLY
) goto done
; /* nothing else to check */
1696 /* clear changed bits so we can wait on them if we don't find a message */
1697 if (req
->get_first
== 0 && req
->get_last
== ~0U) queue
->changed_bits
= 0;
1698 else queue
->changed_bits
&= QS_ALLPOSTMESSAGE
;
1700 /* then check for posted messages */
1701 if (get_posted_message( queue
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
1704 /* only check for quit messages if not posted messages pending.
1705 * note: the quit message isn't filtered */
1706 if (get_quit_message( queue
, req
->flags
, reply
))
1709 /* then check for any raw hardware message */
1710 if (filter_contains_hw_range( req
->get_first
, req
->get_last
) &&
1711 get_hardware_message( current
, req
->hw_id
, get_win
, req
->get_first
, req
->get_last
, reply
))
1714 /* now check for WM_PAINT */
1715 if (queue
->paint_count
&&
1716 check_msg_filter( WM_PAINT
, req
->get_first
, req
->get_last
) &&
1717 (reply
->win
= find_window_to_repaint( get_win
, current
)))
1719 reply
->type
= MSG_POSTED
;
1720 reply
->msg
= WM_PAINT
;
1725 reply
->time
= get_tick_count();
1730 /* now check for timer */
1731 if ((timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
1732 req
->get_last
, (req
->flags
& GET_MSG_REMOVE
) )))
1734 reply
->type
= MSG_POSTED
;
1735 reply
->win
= timer
->win
;
1736 reply
->msg
= timer
->msg
;
1737 reply
->wparam
= timer
->id
;
1738 reply
->lparam
= timer
->lparam
;
1741 reply
->time
= get_tick_count();
1747 set_error( STATUS_PENDING
); /* FIXME */
1751 /* reply to a sent message */
1752 DECL_HANDLER(reply_message
)
1754 if (!current
->queue
) set_error( STATUS_ACCESS_DENIED
);
1755 else if (current
->queue
->recv_result
)
1756 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
1757 get_req_data(), get_req_data_size() );
1761 /* accept the current hardware message */
1762 DECL_HANDLER(accept_hardware_message
)
1765 release_hardware_message( current
->queue
, req
->hw_id
, req
->remove
, req
->new_win
);
1767 set_error( STATUS_ACCESS_DENIED
);
1771 /* retrieve the reply for the last message sent */
1772 DECL_HANDLER(get_message_reply
)
1774 struct message_result
*result
;
1776 struct msg_queue
*queue
= current
->queue
;
1780 set_error( STATUS_PENDING
);
1783 if (!(entry
= list_head( &queue
->send_result
))) return; /* no reply ready */
1785 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1786 if (result
->replied
|| req
->cancel
)
1788 if (result
->replied
)
1790 reply
->result
= result
->result
;
1791 set_error( result
->error
);
1794 size_t data_len
= min( result
->data_size
, get_reply_max_size() );
1795 set_reply_data_ptr( result
->data
, data_len
);
1796 result
->data
= NULL
;
1797 result
->data_size
= 0;
1800 remove_result_from_sender( result
);
1802 entry
= list_head( &queue
->send_result
);
1803 if (!entry
) clear_queue_bits( queue
, QS_SMRESULT
);
1806 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1807 if (!result
->replied
) clear_queue_bits( queue
, QS_SMRESULT
);
1811 else set_error( STATUS_ACCESS_DENIED
);
1815 /* set a window timer */
1816 DECL_HANDLER(set_win_timer
)
1818 struct timer
*timer
;
1819 struct msg_queue
*queue
;
1820 struct thread
*thread
= NULL
;
1821 user_handle_t win
= 0;
1822 unsigned int id
= req
->id
;
1826 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1828 set_error( STATUS_INVALID_HANDLE
);
1831 if (thread
->process
!= current
->process
)
1833 release_object( thread
);
1834 set_error( STATUS_ACCESS_DENIED
);
1837 queue
= thread
->queue
;
1838 /* remove it if it existed already */
1839 if ((timer
= find_timer( queue
, win
, req
->msg
, id
))) free_timer( queue
, timer
);
1843 queue
= get_current_queue();
1844 /* find a free id for it */
1847 id
= queue
->next_timer_id
;
1848 if (++queue
->next_timer_id
>= 0x10000) queue
->next_timer_id
= 1;
1850 while (find_timer( queue
, 0, req
->msg
, id
));
1853 if ((timer
= set_timer( queue
, req
->rate
)))
1856 timer
->msg
= req
->msg
;
1858 timer
->lparam
= req
->lparam
;
1861 if (thread
) release_object( thread
);
1864 /* kill a window timer */
1865 DECL_HANDLER(kill_win_timer
)
1867 struct timer
*timer
;
1868 struct thread
*thread
;
1869 user_handle_t win
= 0;
1873 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1875 set_error( STATUS_INVALID_HANDLE
);
1878 if (thread
->process
!= current
->process
)
1880 release_object( thread
);
1881 set_error( STATUS_ACCESS_DENIED
);
1885 else thread
= (struct thread
*)grab_object( current
);
1887 if (thread
->queue
&& (timer
= find_timer( thread
->queue
, win
, req
->msg
, req
->id
)))
1888 free_timer( thread
->queue
, timer
);
1890 set_error( STATUS_INVALID_PARAMETER
);
1892 release_object( thread
);
1896 /* attach (or detach) thread inputs */
1897 DECL_HANDLER(attach_thread_input
)
1899 struct thread
*thread_from
= get_thread_from_id( req
->tid_from
);
1900 struct thread
*thread_to
= get_thread_from_id( req
->tid_to
);
1902 if (!thread_from
|| !thread_to
)
1904 if (thread_from
) release_object( thread_from
);
1905 if (thread_to
) release_object( thread_to
);
1908 if (thread_from
!= thread_to
)
1910 if (req
->attach
) attach_thread_input( thread_from
, thread_to
);
1913 if (thread_from
->queue
&& thread_to
->queue
&&
1914 thread_from
->queue
->input
== thread_to
->queue
->input
)
1915 detach_thread_input( thread_from
);
1917 set_error( STATUS_ACCESS_DENIED
);
1920 else set_error( STATUS_ACCESS_DENIED
);
1921 release_object( thread_from
);
1922 release_object( thread_to
);
1926 /* get thread input data */
1927 DECL_HANDLER(get_thread_input
)
1929 struct thread
*thread
= NULL
;
1930 struct thread_input
*input
;
1934 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1935 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1937 else input
= foreground_input
; /* get the foreground thread info */
1941 reply
->focus
= input
->focus
;
1942 reply
->capture
= input
->capture
;
1943 reply
->active
= input
->active
;
1944 reply
->menu_owner
= input
->menu_owner
;
1945 reply
->move_size
= input
->move_size
;
1946 reply
->caret
= input
->caret
;
1947 reply
->rect
= input
->caret_rect
;
1954 reply
->menu_owner
= 0;
1955 reply
->move_size
= 0;
1957 reply
->rect
.left
= reply
->rect
.top
= reply
->rect
.right
= reply
->rect
.bottom
= 0;
1959 /* foreground window is active window of foreground thread */
1960 reply
->foreground
= foreground_input
? foreground_input
->active
: 0;
1961 if (thread
) release_object( thread
);
1965 /* retrieve queue keyboard state for a given thread */
1966 DECL_HANDLER(get_key_state
)
1968 struct thread
*thread
;
1969 struct thread_input
*input
;
1971 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1972 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1975 if (req
->key
>= 0) reply
->state
= input
->keystate
[req
->key
& 0xff];
1976 set_reply_data( input
->keystate
, min( get_reply_max_size(), sizeof(input
->keystate
) ));
1978 release_object( thread
);
1982 /* set queue keyboard state for a given thread */
1983 DECL_HANDLER(set_key_state
)
1985 struct thread
*thread
= NULL
;
1986 struct thread_input
*input
;
1988 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1989 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1992 size_t size
= min( sizeof(input
->keystate
), get_req_data_size() );
1993 if (size
) memcpy( input
->keystate
, get_req_data(), size
);
1995 release_object( thread
);
1999 /* set the system foreground window */
2000 DECL_HANDLER(set_foreground_window
)
2002 struct msg_queue
*queue
= get_current_queue();
2004 reply
->previous
= foreground_input
? foreground_input
->active
: 0;
2005 reply
->send_msg_old
= (reply
->previous
&& foreground_input
!= queue
->input
);
2006 reply
->send_msg_new
= FALSE
;
2010 struct thread
*thread
;
2012 if (is_top_level_window( req
->handle
) &&
2013 ((thread
= get_window_thread( req
->handle
))))
2015 foreground_input
= thread
->queue
->input
;
2016 reply
->send_msg_new
= (foreground_input
!= queue
->input
);
2017 release_object( thread
);
2019 else set_error( STATUS_INVALID_HANDLE
);
2021 else foreground_input
= NULL
;
2025 /* set the current thread focus window */
2026 DECL_HANDLER(set_focus_window
)
2028 struct msg_queue
*queue
= get_current_queue();
2030 reply
->previous
= 0;
2031 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2033 reply
->previous
= queue
->input
->focus
;
2034 queue
->input
->focus
= get_user_full_handle( req
->handle
);
2039 /* set the current thread active window */
2040 DECL_HANDLER(set_active_window
)
2042 struct msg_queue
*queue
= get_current_queue();
2044 reply
->previous
= 0;
2045 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2047 if (!req
->handle
|| make_window_active( req
->handle
))
2049 reply
->previous
= queue
->input
->active
;
2050 queue
->input
->active
= get_user_full_handle( req
->handle
);
2052 else set_error( STATUS_INVALID_HANDLE
);
2057 /* set the current thread capture window */
2058 DECL_HANDLER(set_capture_window
)
2060 struct msg_queue
*queue
= get_current_queue();
2062 reply
->previous
= reply
->full_handle
= 0;
2063 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2065 struct thread_input
*input
= queue
->input
;
2067 reply
->previous
= input
->capture
;
2068 input
->capture
= get_user_full_handle( req
->handle
);
2069 input
->menu_owner
= (req
->flags
& CAPTURE_MENU
) ? input
->capture
: 0;
2070 input
->move_size
= (req
->flags
& CAPTURE_MOVESIZE
) ? input
->capture
: 0;
2071 reply
->full_handle
= input
->capture
;
2076 /* Set the current thread caret window */
2077 DECL_HANDLER(set_caret_window
)
2079 struct msg_queue
*queue
= get_current_queue();
2081 reply
->previous
= 0;
2082 if (queue
&& check_queue_input_window( queue
, req
->handle
))
2084 struct thread_input
*input
= queue
->input
;
2086 reply
->previous
= input
->caret
;
2087 reply
->old_rect
= input
->caret_rect
;
2088 reply
->old_hide
= input
->caret_hide
;
2089 reply
->old_state
= input
->caret_state
;
2091 set_caret_window( input
, get_user_full_handle(req
->handle
) );
2092 input
->caret_rect
.right
= input
->caret_rect
.left
+ req
->width
;
2093 input
->caret_rect
.bottom
= input
->caret_rect
.top
+ req
->height
;
2098 /* Set the current thread caret information */
2099 DECL_HANDLER(set_caret_info
)
2101 struct msg_queue
*queue
= get_current_queue();
2102 struct thread_input
*input
;
2105 input
= queue
->input
;
2106 reply
->full_handle
= input
->caret
;
2107 reply
->old_rect
= input
->caret_rect
;
2108 reply
->old_hide
= input
->caret_hide
;
2109 reply
->old_state
= input
->caret_state
;
2111 if (req
->handle
&& get_user_full_handle(req
->handle
) != input
->caret
)
2113 set_error( STATUS_ACCESS_DENIED
);
2116 if (req
->flags
& SET_CARET_POS
)
2118 input
->caret_rect
.right
+= req
->x
- input
->caret_rect
.left
;
2119 input
->caret_rect
.bottom
+= req
->y
- input
->caret_rect
.top
;
2120 input
->caret_rect
.left
= req
->x
;
2121 input
->caret_rect
.top
= req
->y
;
2123 if (req
->flags
& SET_CARET_HIDE
)
2125 input
->caret_hide
+= req
->hide
;
2126 if (input
->caret_hide
< 0) input
->caret_hide
= 0;
2128 if (req
->flags
& SET_CARET_STATE
)
2130 if (req
->state
== -1) input
->caret_state
= !input
->caret_state
;
2131 else input
->caret_state
= !!req
->state
;
2136 /* get the time of the last input event */
2137 DECL_HANDLER(get_last_input_time
)
2139 reply
->time
= last_input_time
;