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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
41 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
42 #define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK
44 enum message_kind
{ SEND_MESSAGE
, POST_MESSAGE
};
45 #define NB_MSG_KINDS (POST_MESSAGE+1)
50 struct list sender_entry
; /* entry in sender list */
51 struct message_result
*recv_next
; /* next in receiver list */
52 struct msg_queue
*sender
; /* sender queue */
53 struct msg_queue
*receiver
; /* receiver queue */
54 int replied
; /* has it been replied to? */
55 unsigned int result
; /* reply result */
56 unsigned int error
; /* error code to pass back to sender */
57 struct message
*callback_msg
; /* message to queue for callback */
58 void *data
; /* message reply data */
59 unsigned int data_size
; /* size of message reply data */
60 struct timeout_user
*timeout
; /* result timeout */
65 struct list entry
; /* entry in message list */
66 enum message_type type
; /* message type */
67 user_handle_t win
; /* window handle */
68 unsigned int msg
; /* message code */
69 unsigned int wparam
; /* parameters */
70 unsigned int lparam
; /* parameters */
71 int x
; /* x position */
72 int y
; /* y position */
73 unsigned int time
; /* message time */
74 unsigned int info
; /* extra info */
75 user_handle_t hook
; /* winevent hook handle */
76 void *hook_proc
; /* winevent hook proc address */
77 void *data
; /* message data for sent messages */
78 unsigned int data_size
; /* size of message data */
79 struct message_result
*result
; /* result in sender queue */
84 struct list entry
; /* entry in timer list */
85 struct timeval 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 unsigned int id
; /* timer id */
90 unsigned int lparam
; /* lparam for message */
95 struct object obj
; /* object header */
96 user_handle_t focus
; /* focus window */
97 user_handle_t capture
; /* capture window */
98 user_handle_t active
; /* active window */
99 user_handle_t menu_owner
; /* current menu owner window */
100 user_handle_t move_size
; /* current moving/resizing window */
101 user_handle_t caret
; /* caret window */
102 rectangle_t caret_rect
; /* caret rectangle */
103 int caret_hide
; /* caret hide count */
104 int caret_state
; /* caret on/off state */
105 struct message
*msg
; /* message currently processed */
106 struct thread
*msg_thread
; /* thread processing the message */
107 struct list msg_list
; /* list of hardware messages */
108 unsigned char keystate
[256]; /* state of each key */
113 struct object obj
; /* object header */
114 unsigned int wake_bits
; /* wakeup bits */
115 unsigned int wake_mask
; /* wakeup mask */
116 unsigned int changed_bits
; /* changed wakeup bits */
117 unsigned int changed_mask
; /* changed wakeup mask */
118 int paint_count
; /* pending paint messages count */
119 struct list msg_list
[NB_MSG_KINDS
]; /* lists of messages */
120 struct list send_result
; /* stack of sent messages waiting for result */
121 struct list callback_result
; /* list of callback messages waiting for result */
122 struct message_result
*recv_result
; /* stack of received messages waiting for result */
123 struct list pending_timers
; /* list of pending timers */
124 struct list expired_timers
; /* list of expired timers */
125 unsigned int next_timer_id
; /* id for the next timer with a 0 window */
126 struct timeout_user
*timeout
; /* timeout for next timer to expire */
127 struct thread_input
*input
; /* thread input descriptor */
128 struct hook_table
*hooks
; /* hook table */
129 struct timeval last_get_msg
; /* time of last get message call */
132 static void msg_queue_dump( struct object
*obj
, int verbose
);
133 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
134 static void msg_queue_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
135 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
);
136 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
);
137 static void msg_queue_destroy( struct object
*obj
);
138 static void thread_input_dump( struct object
*obj
, int verbose
);
139 static void thread_input_destroy( struct object
*obj
);
140 static void timer_callback( void *private );
142 static const struct object_ops msg_queue_ops
=
144 sizeof(struct msg_queue
), /* size */
145 msg_queue_dump
, /* dump */
146 msg_queue_add_queue
, /* add_queue */
147 msg_queue_remove_queue
, /* remove_queue */
148 msg_queue_signaled
, /* signaled */
149 msg_queue_satisfied
, /* satisfied */
150 no_get_fd
, /* get_fd */
151 msg_queue_destroy
/* destroy */
155 static const struct object_ops thread_input_ops
=
157 sizeof(struct thread_input
), /* size */
158 thread_input_dump
, /* dump */
159 no_add_queue
, /* add_queue */
160 NULL
, /* remove_queue */
162 NULL
, /* satisfied */
163 no_get_fd
, /* get_fd */
164 thread_input_destroy
/* destroy */
167 /* pointer to input structure of foreground thread */
168 static struct thread_input
*foreground_input
;
171 /* set the caret window in a given thread input */
172 static void set_caret_window( struct thread_input
*input
, user_handle_t win
)
174 if (!win
|| win
!= input
->caret
)
176 input
->caret_rect
.left
= 0;
177 input
->caret_rect
.top
= 0;
178 input
->caret_rect
.right
= 0;
179 input
->caret_rect
.bottom
= 0;
182 input
->caret_hide
= 1;
183 input
->caret_state
= 0;
186 /* create a thread input object */
187 static struct thread_input
*create_thread_input(void)
189 struct thread_input
*input
;
191 if ((input
= alloc_object( &thread_input_ops
)))
196 input
->menu_owner
= 0;
197 input
->move_size
= 0;
199 input
->msg_thread
= NULL
;
200 list_init( &input
->msg_list
);
201 set_caret_window( input
, 0 );
202 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
207 /* release the thread input data of a given thread */
208 static void release_thread_input( struct thread
*thread
)
210 struct thread_input
*input
= thread
->queue
->input
;
213 if (input
->msg_thread
== thread
)
215 release_object( input
->msg_thread
);
216 input
->msg_thread
= NULL
;
219 release_object( input
);
220 thread
->queue
->input
= NULL
;
223 /* create a message queue object */
224 static struct msg_queue
*create_msg_queue( struct thread
*thread
, struct thread_input
*input
)
226 struct msg_queue
*queue
;
229 if (!input
&& !(input
= create_thread_input())) return NULL
;
230 if ((queue
= alloc_object( &msg_queue_ops
)))
232 queue
->wake_bits
= 0;
233 queue
->wake_mask
= 0;
234 queue
->changed_bits
= 0;
235 queue
->changed_mask
= 0;
236 queue
->paint_count
= 0;
237 queue
->recv_result
= NULL
;
238 queue
->next_timer_id
= 1;
239 queue
->timeout
= NULL
;
240 queue
->input
= (struct thread_input
*)grab_object( input
);
242 gettimeofday( &queue
->last_get_msg
, NULL
);
243 list_init( &queue
->send_result
);
244 list_init( &queue
->callback_result
);
245 list_init( &queue
->pending_timers
);
246 list_init( &queue
->expired_timers
);
247 for (i
= 0; i
< NB_MSG_KINDS
; i
++) list_init( &queue
->msg_list
[i
] );
249 thread
->queue
= queue
;
250 if (!thread
->process
->queue
)
251 thread
->process
->queue
= (struct msg_queue
*)grab_object( queue
);
253 release_object( input
);
257 /* free the message queue of a thread at thread exit */
258 void free_msg_queue( struct thread
*thread
)
260 struct process
*process
= thread
->process
;
261 struct thread_input
*input
;
263 remove_thread_hooks( thread
);
264 if (!thread
->queue
) return;
265 if (process
->queue
== thread
->queue
) /* is it the process main queue? */
267 release_object( process
->queue
);
268 process
->queue
= NULL
;
269 if (process
->idle_event
)
271 set_event( process
->idle_event
);
272 release_object( process
->idle_event
);
273 process
->idle_event
= NULL
;
276 input
= thread
->queue
->input
;
277 if (input
->msg_thread
== thread
)
279 release_object( input
->msg_thread
);
280 input
->msg_thread
= NULL
;
283 release_object( thread
->queue
);
284 thread
->queue
= NULL
;
287 /* get the hook table for a given thread */
288 struct hook_table
*get_queue_hooks( struct thread
*thread
)
290 if (!thread
->queue
) return NULL
;
291 return thread
->queue
->hooks
;
294 /* set the hook table for a given thread, allocating the queue if needed */
295 void set_queue_hooks( struct thread
*thread
, struct hook_table
*hooks
)
297 struct msg_queue
*queue
= thread
->queue
;
298 if (!queue
) queue
= create_msg_queue( thread
, NULL
);
299 if (queue
->hooks
) release_object( queue
->hooks
);
300 queue
->hooks
= hooks
;
303 /* check the queue status */
304 inline static int is_signaled( struct msg_queue
*queue
)
306 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
309 /* set some queue bits */
310 inline static void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
312 queue
->wake_bits
|= bits
;
313 queue
->changed_bits
|= bits
;
314 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
317 /* clear some queue bits */
318 inline static void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
320 queue
->wake_bits
&= ~bits
;
321 queue
->changed_bits
&= ~bits
;
324 /* check whether msg is a keyboard message */
325 inline static int is_keyboard_msg( struct message
*msg
)
327 return (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
);
330 /* check whether a message filter contains at least one potential hardware message */
331 inline static int filter_contains_hw_range( unsigned int first
, unsigned int last
)
333 /* hardware message ranges are (in numerical order):
334 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
335 * WM_KEYFIRST .. WM_KEYLAST
336 * WM_MOUSEFIRST .. WM_MOUSELAST
338 if (last
< WM_NCMOUSEFIRST
) return 0;
339 if (first
> WM_NCMOUSELAST
&& last
< WM_KEYFIRST
) return 0;
340 if (first
> WM_KEYLAST
&& last
< WM_MOUSEFIRST
) return 0;
341 if (first
> WM_MOUSELAST
) return 0;
345 /* get the QS_* bit corresponding to a given hardware message */
346 inline static int get_hardware_msg_bit( struct message
*msg
)
348 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
349 if (is_keyboard_msg( msg
)) return QS_KEY
;
350 return QS_MOUSEBUTTON
;
353 /* get the current thread queue, creating it if needed */
354 inline static struct msg_queue
*get_current_queue(void)
356 struct msg_queue
*queue
= current
->queue
;
357 if (!queue
) queue
= create_msg_queue( current
, NULL
);
361 /* try to merge a message with the last in the list; return 1 if successful */
362 static int merge_message( struct thread_input
*input
, const struct message
*msg
)
364 struct message
*prev
;
365 struct list
*ptr
= list_tail( &input
->msg_list
);
368 prev
= LIST_ENTRY( ptr
, struct message
, entry
);
369 if (input
->msg
== prev
) return 0;
370 if (prev
->result
) return 0;
371 if (prev
->win
!= msg
->win
) return 0;
372 if (prev
->msg
!= msg
->msg
) return 0;
373 if (prev
->type
!= msg
->type
) return 0;
374 /* now we can merge it */
375 prev
->wparam
= msg
->wparam
;
376 prev
->lparam
= msg
->lparam
;
379 prev
->time
= msg
->time
;
380 prev
->info
= msg
->info
;
384 /* free a result structure */
385 static void free_result( struct message_result
*result
)
387 if (result
->timeout
) remove_timeout_user( result
->timeout
);
388 if (result
->data
) free( result
->data
);
389 if (result
->callback_msg
) free( result
->callback_msg
);
393 /* remove the result from the sender list it is on */
394 static inline void remove_result_from_sender( struct message_result
*result
)
396 assert( result
->sender
);
398 list_remove( &result
->sender_entry
);
399 result
->sender
= NULL
;
400 if (!result
->receiver
) free_result( result
);
403 /* store the message result in the appropriate structure */
404 static void store_message_result( struct message_result
*res
, unsigned int result
,
407 res
->result
= result
;
412 remove_timeout_user( res
->timeout
);
417 if (res
->callback_msg
)
419 /* queue the callback message in the sender queue */
420 res
->callback_msg
->lparam
= result
;
421 list_add_tail( &res
->sender
->msg_list
[SEND_MESSAGE
], &res
->callback_msg
->entry
);
422 set_queue_bits( res
->sender
, QS_SENDMESSAGE
);
423 res
->callback_msg
= NULL
;
424 remove_result_from_sender( res
);
428 /* wake sender queue if waiting on this result */
429 if (list_head(&res
->sender
->send_result
) == &res
->sender_entry
)
430 set_queue_bits( res
->sender
, QS_SMRESULT
);
436 /* free a message when deleting a queue or window */
437 static void free_message( struct message
*msg
)
439 struct message_result
*result
= msg
->result
;
444 result
->receiver
= NULL
;
445 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
447 else free_result( result
);
449 if (msg
->data
) free( msg
->data
);
453 /* remove (and free) a message from a message list */
454 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
455 enum message_kind kind
)
457 list_remove( &msg
->entry
);
461 if (list_empty( &queue
->msg_list
[kind
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
464 if (list_empty( &queue
->msg_list
[kind
] )) clear_queue_bits( queue
, QS_POSTMESSAGE
);
470 /* message timed out without getting a reply */
471 static void result_timeout( void *private )
473 struct message_result
*result
= private;
475 assert( !result
->replied
);
477 result
->timeout
= NULL
;
478 store_message_result( result
, 0, STATUS_TIMEOUT
);
481 /* allocate and fill a message result structure */
482 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
483 struct msg_queue
*recv_queue
,
484 struct message
*msg
, unsigned int timeout
,
485 void *callback
, unsigned int callback_data
)
487 struct message_result
*result
= mem_alloc( sizeof(*result
) );
490 result
->sender
= send_queue
;
491 result
->receiver
= recv_queue
;
494 result
->data_size
= 0;
495 result
->timeout
= NULL
;
497 if (msg
->type
== MSG_CALLBACK
)
499 struct message
*callback_msg
= mem_alloc( sizeof(*callback_msg
) );
505 callback_msg
->type
= MSG_CALLBACK_RESULT
;
506 callback_msg
->win
= msg
->win
;
507 callback_msg
->msg
= msg
->msg
;
508 callback_msg
->wparam
= (unsigned int)callback
;
509 callback_msg
->lparam
= 0;
510 callback_msg
->time
= get_tick_count();
513 callback_msg
->info
= callback_data
;
514 callback_msg
->hook
= 0;
515 callback_msg
->hook_proc
= NULL
;
516 callback_msg
->result
= NULL
;
517 callback_msg
->data
= NULL
;
518 callback_msg
->data_size
= 0;
520 result
->callback_msg
= callback_msg
;
521 list_add_head( &send_queue
->callback_result
, &result
->sender_entry
);
525 result
->callback_msg
= NULL
;
526 list_add_head( &send_queue
->send_result
, &result
->sender_entry
);
532 gettimeofday( &when
, 0 );
533 add_timeout( &when
, timeout
);
534 result
->timeout
= add_timeout_user( &when
, result_timeout
, result
);
540 /* receive a message, removing it from the sent queue */
541 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
542 struct get_message_reply
*reply
)
544 struct message_result
*result
= msg
->result
;
546 reply
->total
= msg
->data_size
;
547 if (msg
->data_size
> get_reply_max_size())
549 set_error( STATUS_BUFFER_OVERFLOW
);
552 reply
->type
= msg
->type
;
553 reply
->win
= msg
->win
;
554 reply
->msg
= msg
->msg
;
555 reply
->wparam
= msg
->wparam
;
556 reply
->lparam
= msg
->lparam
;
559 reply
->time
= msg
->time
;
560 reply
->info
= msg
->info
;
561 reply
->hook
= msg
->hook
;
562 reply
->hook_proc
= msg
->hook_proc
;
564 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
566 list_remove( &msg
->entry
);
567 /* put the result on the receiver result stack */
570 result
->recv_next
= queue
->recv_result
;
571 queue
->recv_result
= result
;
574 if (list_empty( &queue
->msg_list
[SEND_MESSAGE
] )) clear_queue_bits( queue
, QS_SENDMESSAGE
);
577 /* set the result of the current received message */
578 static void reply_message( struct msg_queue
*queue
, unsigned int result
,
579 unsigned int error
, int remove
, const void *data
, size_t len
)
581 struct message_result
*res
= queue
->recv_result
;
585 queue
->recv_result
= res
->recv_next
;
586 res
->receiver
= NULL
;
587 if (!res
->sender
) /* no one waiting for it */
595 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
596 store_message_result( res
, result
, error
);
600 /* retrieve a posted message */
601 static int get_posted_message( struct msg_queue
*queue
, user_handle_t win
,
602 unsigned int first
, unsigned int last
, unsigned int flags
,
603 struct get_message_reply
*reply
)
607 /* check against the filters */
608 LIST_FOR_EACH_ENTRY( msg
, &queue
->msg_list
[POST_MESSAGE
], struct message
, entry
)
610 if (msg
->msg
== WM_QUIT
) goto found
; /* WM_QUIT is never filtered */
611 if (win
&& msg
->win
&& msg
->win
!= win
&& !is_child_window( win
, msg
->win
)) continue;
612 if (msg
->msg
< first
) continue;
613 if (msg
->msg
> last
) continue;
614 goto found
; /* found one */
618 /* return it to the app */
620 reply
->total
= msg
->data_size
;
621 if (msg
->data_size
> get_reply_max_size())
623 set_error( STATUS_BUFFER_OVERFLOW
);
626 reply
->type
= msg
->type
;
627 reply
->win
= msg
->win
;
628 reply
->msg
= msg
->msg
;
629 reply
->wparam
= msg
->wparam
;
630 reply
->lparam
= msg
->lparam
;
633 reply
->time
= msg
->time
;
634 reply
->info
= msg
->info
;
636 if (flags
& GET_MSG_REMOVE
)
640 set_reply_data_ptr( msg
->data
, msg
->data_size
);
644 remove_queue_message( queue
, msg
, POST_MESSAGE
);
646 else if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
651 /* empty a message list and free all the messages */
652 static void empty_msg_list( struct list
*list
)
656 while ((ptr
= list_head( list
)) != NULL
)
658 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
659 list_remove( &msg
->entry
);
664 /* cleanup all pending results when deleting a queue */
665 static void cleanup_results( struct msg_queue
*queue
)
669 while ((entry
= list_head( &queue
->send_result
)) != NULL
)
671 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
674 while ((entry
= list_head( &queue
->callback_result
)) != NULL
)
676 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
679 while (queue
->recv_result
)
680 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
683 /* check if the thread owning the queue is hung (not checking for messages) */
684 static int is_queue_hung( struct msg_queue
*queue
)
687 struct wait_queue_entry
*entry
;
689 gettimeofday( &now
, NULL
);
690 if (now
.tv_sec
- queue
->last_get_msg
.tv_sec
<= 5)
691 return 0; /* less than 5 seconds since last get message -> not hung */
693 LIST_FOR_EACH_ENTRY( entry
, &queue
->obj
.wait_queue
, struct wait_queue_entry
, entry
)
695 if (entry
->thread
->queue
== queue
)
696 return 0; /* thread is waiting on queue -> not hung */
701 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
703 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
704 struct process
*process
= entry
->thread
->process
;
706 /* a thread can only wait on its own queue */
707 if (entry
->thread
->queue
!= queue
)
709 set_error( STATUS_ACCESS_DENIED
);
712 /* if waiting on the main process queue, set the idle event */
713 if (process
->queue
== queue
)
715 if (process
->idle_event
) set_event( process
->idle_event
);
717 add_queue( obj
, entry
);
721 static void msg_queue_remove_queue(struct object
*obj
, struct wait_queue_entry
*entry
)
723 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
724 struct process
*process
= entry
->thread
->process
;
726 remove_queue( obj
, entry
);
728 assert( entry
->thread
->queue
== queue
);
730 /* if waiting on the main process queue, reset the idle event */
731 if (process
->queue
== queue
)
733 if (process
->idle_event
) reset_event( process
->idle_event
);
737 static void msg_queue_dump( struct object
*obj
, int verbose
)
739 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
740 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
741 queue
->wake_bits
, queue
->wake_mask
);
744 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
)
746 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
747 return is_signaled( queue
);
750 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
)
752 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
753 queue
->wake_mask
= 0;
754 queue
->changed_mask
= 0;
755 return 0; /* Not abandoned */
758 static void msg_queue_destroy( struct object
*obj
)
760 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
764 cleanup_results( queue
);
765 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
767 while ((ptr
= list_head( &queue
->pending_timers
)))
769 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
770 list_remove( &timer
->entry
);
773 while ((ptr
= list_head( &queue
->expired_timers
)))
775 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
776 list_remove( &timer
->entry
);
779 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
780 if (queue
->input
) release_object( queue
->input
);
781 if (queue
->hooks
) release_object( queue
->hooks
);
784 static void thread_input_dump( struct object
*obj
, int verbose
)
786 struct thread_input
*input
= (struct thread_input
*)obj
;
787 fprintf( stderr
, "Thread input focus=%p capture=%p active=%p\n",
788 input
->focus
, input
->capture
, input
->active
);
791 static void thread_input_destroy( struct object
*obj
)
793 struct thread_input
*input
= (struct thread_input
*)obj
;
795 if (foreground_input
== input
) foreground_input
= NULL
;
796 if (input
->msg_thread
) release_object( input
->msg_thread
);
797 empty_msg_list( &input
->msg_list
);
800 /* fix the thread input data when a window is destroyed */
801 inline static void thread_input_cleanup_window( struct msg_queue
*queue
, user_handle_t window
)
803 struct thread_input
*input
= queue
->input
;
805 if (window
== input
->focus
) input
->focus
= 0;
806 if (window
== input
->capture
) input
->capture
= 0;
807 if (window
== input
->active
) input
->active
= 0;
808 if (window
== input
->menu_owner
) input
->menu_owner
= 0;
809 if (window
== input
->move_size
) input
->move_size
= 0;
810 if (window
== input
->caret
) set_caret_window( input
, 0 );
813 /* check if the specified window can be set in the input data of a given queue */
814 static int check_queue_input_window( struct msg_queue
*queue
, user_handle_t window
)
816 struct thread
*thread
;
819 if (!window
) return 1; /* we can always clear the data */
821 if ((thread
= get_window_thread( window
)))
823 ret
= (queue
->input
== thread
->queue
->input
);
824 if (!ret
) set_error( STATUS_ACCESS_DENIED
);
825 release_object( thread
);
827 else set_error( STATUS_INVALID_HANDLE
);
832 /* attach two thread input data structures */
833 int attach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
835 struct thread_input
*input
;
837 if (!thread_to
->queue
&& !(thread_to
->queue
= create_msg_queue( thread_to
, NULL
))) return 0;
838 input
= (struct thread_input
*)grab_object( thread_to
->queue
->input
);
840 if (thread_from
->queue
)
842 release_thread_input( thread_from
);
843 thread_from
->queue
->input
= input
;
847 if (!(thread_from
->queue
= create_msg_queue( thread_from
, input
))) return 0;
849 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
853 /* detach two thread input data structures */
854 static void detach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
856 struct thread_input
*input
;
858 if (!thread_from
->queue
|| !thread_to
->queue
||
859 thread_from
->queue
->input
!= thread_to
->queue
->input
)
861 set_error( STATUS_ACCESS_DENIED
);
864 if ((input
= create_thread_input()))
866 release_thread_input( thread_from
);
867 thread_from
->queue
->input
= input
;
872 /* set the next timer to expire */
873 static void set_next_timer( struct msg_queue
*queue
)
879 remove_timeout_user( queue
->timeout
);
880 queue
->timeout
= NULL
;
882 if ((ptr
= list_head( &queue
->pending_timers
)))
884 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
885 queue
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, queue
);
887 /* set/clear QS_TIMER bit */
888 if (list_empty( &queue
->expired_timers
))
889 clear_queue_bits( queue
, QS_TIMER
);
891 set_queue_bits( queue
, QS_TIMER
);
894 /* find a timer from its window and id */
895 static struct timer
*find_timer( struct msg_queue
*queue
, user_handle_t win
,
896 unsigned int msg
, unsigned int id
)
900 /* we need to search both lists */
902 LIST_FOR_EACH( ptr
, &queue
->pending_timers
)
904 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
905 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
907 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
909 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
910 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
915 /* callback for the next timer expiration */
916 static void timer_callback( void *private )
918 struct msg_queue
*queue
= private;
921 queue
->timeout
= NULL
;
922 /* move on to the next timer */
923 ptr
= list_head( &queue
->pending_timers
);
925 list_add_tail( &queue
->expired_timers
, ptr
);
926 set_next_timer( queue
);
929 /* link a timer at its rightful place in the queue list */
930 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
934 for (ptr
= queue
->pending_timers
.next
; ptr
!= &queue
->pending_timers
; ptr
= ptr
->next
)
936 struct timer
*t
= LIST_ENTRY( ptr
, struct timer
, entry
);
937 if (!time_before( &t
->when
, &timer
->when
)) break;
939 list_add_before( ptr
, &timer
->entry
);
942 /* remove a timer from the queue timer list and free it */
943 static void free_timer( struct msg_queue
*queue
, struct timer
*timer
)
945 list_remove( &timer
->entry
);
947 set_next_timer( queue
);
950 /* restart an expired timer */
951 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
955 list_remove( &timer
->entry
);
956 gettimeofday( &now
, 0 );
957 while (!time_before( &now
, &timer
->when
)) add_timeout( &timer
->when
, timer
->rate
);
958 link_timer( queue
, timer
);
959 set_next_timer( queue
);
962 /* find an expired timer matching the filtering parameters */
963 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
964 unsigned int get_first
, unsigned int get_last
,
969 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
971 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
972 if (win
&& timer
->win
!= win
) continue;
973 if (timer
->msg
>= get_first
&& timer
->msg
<= get_last
)
975 if (remove
) restart_timer( queue
, timer
);
983 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
985 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
988 timer
->rate
= max( rate
, 1 );
989 gettimeofday( &timer
->when
, 0 );
990 add_timeout( &timer
->when
, rate
);
991 link_timer( queue
, timer
);
992 /* check if we replaced the next timer */
993 if (list_head( &queue
->pending_timers
) == &timer
->entry
) set_next_timer( queue
);
998 /* change the input key state for a given key */
999 static void set_input_key_state( struct thread_input
*input
, unsigned char key
, int down
)
1003 if (!(input
->keystate
[key
] & 0x80)) input
->keystate
[key
] ^= 0x01;
1004 input
->keystate
[key
] |= 0x80;
1006 else input
->keystate
[key
] &= ~0x80;
1009 /* update the input key state for a keyboard message */
1010 static void update_input_key_state( struct thread_input
*input
, const struct message
*msg
)
1013 int down
= 0, extended
;
1017 case WM_LBUTTONDOWN
:
1021 set_input_key_state( input
, VK_LBUTTON
, down
);
1023 case WM_MBUTTONDOWN
:
1027 set_input_key_state( input
, VK_MBUTTON
, down
);
1029 case WM_RBUTTONDOWN
:
1033 set_input_key_state( input
, VK_RBUTTON
, down
);
1041 key
= (unsigned char)msg
->wparam
;
1042 extended
= ((msg
->lparam
>> 16) & KF_EXTENDED
) != 0;
1043 set_input_key_state( input
, key
, down
);
1047 set_input_key_state( input
, extended
? VK_RSHIFT
: VK_LSHIFT
, down
);
1050 set_input_key_state( input
, extended
? VK_RCONTROL
: VK_LCONTROL
, down
);
1053 set_input_key_state( input
, extended
? VK_RMENU
: VK_LMENU
, down
);
1060 /* release the hardware message currently being processed by the given thread */
1061 static void release_hardware_message( struct thread
*thread
, int remove
, user_handle_t new_win
)
1063 struct thread_input
*input
= thread
->queue
->input
;
1065 if (input
->msg_thread
!= thread
) return;
1067 /* clear the queue bit for that message */
1068 if (remove
|| new_win
)
1070 struct message
*other
;
1073 clr_bit
= get_hardware_msg_bit( input
->msg
);
1074 LIST_FOR_EACH_ENTRY( other
, &input
->msg_list
, struct message
, entry
)
1076 if (other
!= input
->msg
&& get_hardware_msg_bit( other
) == clr_bit
)
1082 if (clr_bit
) clear_queue_bits( thread
->queue
, clr_bit
);
1085 if (new_win
) /* set the new window */
1087 struct thread
*owner
= get_window_thread( new_win
);
1090 input
->msg
->win
= new_win
;
1091 set_queue_bits( owner
->queue
, get_hardware_msg_bit( input
->msg
));
1092 release_object( owner
);
1094 if (!remove
) return; /* don't release the message */
1098 update_input_key_state( input
, input
->msg
);
1099 list_remove( &input
->msg
->entry
);
1100 free_message( input
->msg
);
1102 release_object( input
->msg_thread
);
1104 input
->msg_thread
= NULL
;
1107 /* find the window that should receive a given hardware message */
1108 static user_handle_t
find_hardware_message_window( struct thread_input
*input
, struct message
*msg
,
1109 unsigned int *msg_code
)
1111 user_handle_t win
= 0;
1113 *msg_code
= msg
->msg
;
1114 if (is_keyboard_msg( msg
))
1116 if (input
&& !(win
= input
->focus
))
1118 win
= input
->active
;
1119 if (*msg_code
< WM_SYSKEYDOWN
) *msg_code
+= WM_SYSKEYDOWN
- WM_KEYDOWN
;
1122 else /* mouse message */
1124 if (!input
|| !(win
= input
->capture
))
1126 if (!(win
= msg
->win
) || !is_window_visible( win
))
1127 win
= window_from_point( msg
->x
, msg
->y
);
1133 /* queue a hardware message into a given thread input */
1134 static void queue_hardware_message( struct msg_queue
*queue
, struct message
*msg
)
1137 struct thread
*thread
;
1138 struct thread_input
*input
;
1139 unsigned int msg_code
;
1141 win
= find_hardware_message_window( queue
? queue
->input
: foreground_input
, msg
, &msg_code
);
1142 if (!win
|| !(thread
= get_window_thread(win
)))
1147 input
= thread
->queue
->input
;
1149 if (msg
->msg
== WM_MOUSEMOVE
&& merge_message( input
, msg
)) free( msg
);
1152 list_add_tail( &input
->msg_list
, &msg
->entry
);
1153 set_queue_bits( thread
->queue
, get_hardware_msg_bit(msg
) );
1155 release_object( thread
);
1158 /* find a hardware message for the given queue */
1159 static int get_hardware_message( struct thread
*thread
, int get_next_hw
, struct message
*first
,
1160 user_handle_t filter_win
, struct get_message_reply
*reply
)
1162 struct thread_input
*input
= thread
->queue
->input
;
1163 struct thread
*win_thread
;
1166 int clear_bits
, got_one
= 0;
1167 unsigned int msg_code
;
1169 if (input
->msg_thread
&& ((input
->msg_thread
!= thread
) || !get_next_hw
))
1170 return 0; /* locked by another thread, or another recursion level of the same thread */
1174 ptr
= list_head( &input
->msg_list
);
1175 clear_bits
= QS_KEY
| QS_MOUSEMOVE
| QS_MOUSEBUTTON
;
1179 ptr
= list_next( &input
->msg_list
, &first
->entry
);
1180 clear_bits
= 0; /* don't clear bits if we don't go through the whole list */
1185 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1186 win
= find_hardware_message_window( input
, msg
, &msg_code
);
1187 if (!win
|| !(win_thread
= get_window_thread( win
)))
1189 /* no window at all, remove it */
1190 ptr
= list_next( &input
->msg_list
, ptr
);
1191 update_input_key_state( input
, msg
);
1192 list_remove( &msg
->entry
);
1193 free_message( msg
);
1196 if (win_thread
!= thread
)
1198 /* wake the other thread */
1199 set_queue_bits( win_thread
->queue
, get_hardware_msg_bit(msg
) );
1200 release_object( win_thread
);
1202 ptr
= list_next( &input
->msg_list
, ptr
);
1205 /* if we already got a message for another thread, or if it doesn't
1206 * match the filter we skip it (filter is only checked for keyboard
1207 * messages since the dest window for a mouse message depends on hittest)
1210 (filter_win
&& is_keyboard_msg(msg
) &&
1211 win
!= filter_win
&& !is_child_window( filter_win
, win
)))
1213 clear_bits
&= ~get_hardware_msg_bit( msg
);
1214 release_object( win_thread
);
1215 ptr
= list_next( &input
->msg_list
, ptr
);
1218 /* now we can return it */
1219 if (!input
->msg_thread
) input
->msg_thread
= win_thread
;
1220 else release_object( win_thread
);
1223 reply
->type
= MSG_HARDWARE
;
1225 reply
->msg
= msg_code
;
1226 reply
->wparam
= msg
->wparam
;
1227 reply
->lparam
= msg
->lparam
;
1230 reply
->time
= msg
->time
;
1231 reply
->info
= msg
->info
;
1234 /* nothing found, clear the hardware queue bits */
1235 clear_queue_bits( thread
->queue
, clear_bits
);
1236 if (input
->msg_thread
) release_object( input
->msg_thread
);
1238 input
->msg_thread
= NULL
;
1242 /* increment (or decrement if 'incr' is negative) the queue paint count */
1243 void inc_queue_paint_count( struct thread
*thread
, int incr
)
1245 struct msg_queue
*queue
= thread
->queue
;
1249 if ((queue
->paint_count
+= incr
) < 0) queue
->paint_count
= 0;
1251 if (queue
->paint_count
)
1252 set_queue_bits( queue
, QS_PAINT
);
1254 clear_queue_bits( queue
, QS_PAINT
);
1258 /* remove all messages and timers belonging to a certain window */
1259 void queue_cleanup_window( struct thread
*thread
, user_handle_t win
)
1261 struct msg_queue
*queue
= thread
->queue
;
1269 ptr
= list_head( &queue
->pending_timers
);
1272 struct list
*next
= list_next( &queue
->pending_timers
, ptr
);
1273 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1274 if (timer
->win
== win
) free_timer( queue
, timer
);
1277 ptr
= list_head( &queue
->expired_timers
);
1280 struct list
*next
= list_next( &queue
->expired_timers
, ptr
);
1281 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1282 if (timer
->win
== win
) free_timer( queue
, timer
);
1286 /* remove messages */
1287 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
1289 struct list
*ptr
, *next
;
1291 LIST_FOR_EACH_SAFE( ptr
, next
, &queue
->msg_list
[i
] )
1293 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1294 if (msg
->win
== win
) remove_queue_message( queue
, msg
, i
);
1298 thread_input_cleanup_window( queue
, win
);
1301 /* post a message to a window; used by socket handling */
1302 void post_message( user_handle_t win
, unsigned int message
,
1303 unsigned int wparam
, unsigned int lparam
)
1305 struct message
*msg
;
1306 struct thread
*thread
= get_window_thread( win
);
1308 if (!thread
) return;
1310 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1312 msg
->type
= MSG_POSTED
;
1313 msg
->win
= get_user_full_handle( win
);
1315 msg
->wparam
= wparam
;
1316 msg
->lparam
= lparam
;
1317 msg
->time
= get_tick_count();
1322 msg
->hook_proc
= NULL
;
1327 list_add_tail( &thread
->queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1328 set_queue_bits( thread
->queue
, QS_POSTMESSAGE
);
1330 release_object( thread
);
1333 /* post a win event */
1334 void post_win_event( struct thread
*thread
, unsigned int event
,
1335 user_handle_t win
, unsigned int object_id
,
1336 unsigned int child_id
, void *hook_proc
,
1337 const WCHAR
*module
, size_t module_size
,
1340 struct message
*msg
;
1342 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1344 msg
->type
= MSG_WINEVENT
;
1345 msg
->win
= get_user_full_handle( win
);
1347 msg
->wparam
= object_id
;
1348 msg
->lparam
= child_id
;
1349 msg
->time
= get_tick_count();
1352 msg
->info
= get_thread_id( current
);
1355 msg
->hook_proc
= hook_proc
;
1357 if ((msg
->data
= malloc( module_size
)))
1359 msg
->data_size
= module_size
;
1360 memcpy( msg
->data
, module
, module_size
);
1362 if (debug_level
> 1)
1363 fprintf( stderr
, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1364 get_thread_id(thread
), event
, win
, object_id
, child_id
);
1365 list_add_tail( &thread
->queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1366 set_queue_bits( thread
->queue
, QS_SENDMESSAGE
);
1373 /* get the message queue of the current thread */
1374 DECL_HANDLER(get_msg_queue
)
1376 struct msg_queue
*queue
= get_current_queue();
1379 if (queue
) reply
->handle
= alloc_handle( current
->process
, queue
, SYNCHRONIZE
, 0 );
1383 /* set the current message queue wakeup mask */
1384 DECL_HANDLER(set_queue_mask
)
1386 struct msg_queue
*queue
= get_current_queue();
1390 queue
->wake_mask
= req
->wake_mask
;
1391 queue
->changed_mask
= req
->changed_mask
;
1392 reply
->wake_bits
= queue
->wake_bits
;
1393 reply
->changed_bits
= queue
->changed_bits
;
1394 if (is_signaled( queue
))
1396 /* if skip wait is set, do what would have been done in the subsequent wait */
1397 if (req
->skip_wait
) msg_queue_satisfied( &queue
->obj
, current
);
1398 else wake_up( &queue
->obj
, 0 );
1404 /* get the current message queue status */
1405 DECL_HANDLER(get_queue_status
)
1407 struct msg_queue
*queue
= current
->queue
;
1410 reply
->wake_bits
= queue
->wake_bits
;
1411 reply
->changed_bits
= queue
->changed_bits
;
1412 if (req
->clear
) queue
->changed_bits
= 0;
1414 else reply
->wake_bits
= reply
->changed_bits
= 0;
1418 /* send a message to a thread queue */
1419 DECL_HANDLER(send_message
)
1421 struct message
*msg
;
1422 struct msg_queue
*send_queue
= get_current_queue();
1423 struct msg_queue
*recv_queue
= NULL
;
1424 struct thread
*thread
= NULL
;
1428 if (!(thread
= get_thread_from_id( req
->id
))) return;
1430 else if (req
->type
!= MSG_HARDWARE
)
1432 /* only hardware messages are allowed without destination thread */
1433 set_error( STATUS_INVALID_PARAMETER
);
1437 if (thread
&& !(recv_queue
= thread
->queue
))
1439 set_error( STATUS_INVALID_PARAMETER
);
1440 release_object( thread
);
1443 if (recv_queue
&& (req
->flags
& SEND_MSG_ABORT_IF_HUNG
) && is_queue_hung(recv_queue
))
1445 set_error( STATUS_TIMEOUT
);
1446 release_object( thread
);
1450 if ((msg
= mem_alloc( sizeof(*msg
) )))
1452 msg
->type
= req
->type
;
1453 msg
->win
= get_user_full_handle( req
->win
);
1454 msg
->msg
= req
->msg
;
1455 msg
->wparam
= req
->wparam
;
1456 msg
->lparam
= req
->lparam
;
1457 msg
->time
= req
->time
;
1460 msg
->info
= req
->info
;
1462 msg
->hook_proc
= NULL
;
1469 case MSG_OTHER_PROCESS
:
1470 msg
->data_size
= get_req_data_size();
1471 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1480 if (!(msg
->result
= alloc_message_result( send_queue
, recv_queue
, msg
,
1481 req
->timeout
, req
->callback
, req
->info
)))
1483 free_message( msg
);
1488 list_add_tail( &recv_queue
->msg_list
[SEND_MESSAGE
], &msg
->entry
);
1489 set_queue_bits( recv_queue
, QS_SENDMESSAGE
);
1492 /* needed for posted DDE messages */
1493 msg
->data_size
= get_req_data_size();
1494 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1499 list_add_tail( &recv_queue
->msg_list
[POST_MESSAGE
], &msg
->entry
);
1500 set_queue_bits( recv_queue
, QS_POSTMESSAGE
);
1503 queue_hardware_message( recv_queue
, msg
);
1505 case MSG_CALLBACK_RESULT
: /* cannot send this one */
1507 set_error( STATUS_INVALID_PARAMETER
);
1512 if (thread
) release_object( thread
);
1516 /* get a message from the current queue */
1517 DECL_HANDLER(get_message
)
1519 struct timer
*timer
;
1521 struct message
*first_hw_msg
= NULL
;
1522 struct msg_queue
*queue
= get_current_queue();
1523 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
1526 gettimeofday( &queue
->last_get_msg
, NULL
);
1528 /* first of all release the hardware input lock if we own it */
1529 /* we'll grab it again if we find a hardware message */
1530 if (queue
->input
->msg_thread
== current
&& req
->get_next_hw
)
1532 first_hw_msg
= queue
->input
->msg
;
1533 release_hardware_message( current
, 0, 0 );
1536 /* first check for sent messages */
1537 if ((ptr
= list_head( &queue
->msg_list
[SEND_MESSAGE
] )))
1539 struct message
*msg
= LIST_ENTRY( ptr
, struct message
, entry
);
1540 receive_message( queue
, msg
, reply
);
1543 if (req
->flags
& GET_MSG_SENT_ONLY
) goto done
; /* nothing else to check */
1545 /* clear changed bits so we can wait on them if we don't find a message */
1546 queue
->changed_bits
= 0;
1548 /* then check for posted messages */
1549 if (get_posted_message( queue
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
1552 /* then check for any raw hardware message */
1553 if (filter_contains_hw_range( req
->get_first
, req
->get_last
) &&
1554 get_hardware_message( current
, req
->get_next_hw
, first_hw_msg
, get_win
, reply
))
1557 /* now check for WM_PAINT */
1558 if (queue
->paint_count
&&
1559 (WM_PAINT
>= req
->get_first
) && (WM_PAINT
<= req
->get_last
) &&
1560 (reply
->win
= find_window_to_repaint( get_win
, current
)))
1562 reply
->type
= MSG_POSTED
;
1563 reply
->msg
= WM_PAINT
;
1568 reply
->time
= get_tick_count();
1573 /* now check for timer */
1574 if ((timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
1575 req
->get_last
, (req
->flags
& GET_MSG_REMOVE
) )))
1577 reply
->type
= MSG_POSTED
;
1578 reply
->win
= timer
->win
;
1579 reply
->msg
= timer
->msg
;
1580 reply
->wparam
= timer
->id
;
1581 reply
->lparam
= timer
->lparam
;
1584 reply
->time
= get_tick_count();
1590 set_error( STATUS_PENDING
); /* FIXME */
1594 /* reply to a sent message */
1595 DECL_HANDLER(reply_message
)
1597 if (!current
->queue
) set_error( STATUS_ACCESS_DENIED
);
1598 else if (current
->queue
->recv_result
)
1599 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
1600 get_req_data(), get_req_data_size() );
1604 /* accept the current hardware message */
1605 DECL_HANDLER(accept_hardware_message
)
1609 struct thread_input
*input
= current
->queue
->input
;
1610 if (input
->msg_thread
== current
)
1612 release_hardware_message( current
, req
->remove
, req
->new_win
);
1616 set_error( STATUS_ACCESS_DENIED
);
1620 /* retrieve the reply for the last message sent */
1621 DECL_HANDLER(get_message_reply
)
1623 struct message_result
*result
;
1625 struct msg_queue
*queue
= current
->queue
;
1629 set_error( STATUS_PENDING
);
1632 if (!(entry
= list_head( &queue
->send_result
))) return; /* no reply ready */
1634 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1635 if (result
->replied
|| req
->cancel
)
1637 if (result
->replied
)
1639 reply
->result
= result
->result
;
1640 set_error( result
->error
);
1643 size_t data_len
= min( result
->data_size
, get_reply_max_size() );
1644 set_reply_data_ptr( result
->data
, data_len
);
1645 result
->data
= NULL
;
1646 result
->data_size
= 0;
1649 remove_result_from_sender( result
);
1651 entry
= list_head( &queue
->send_result
);
1652 if (!entry
) clear_queue_bits( queue
, QS_SMRESULT
);
1655 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1656 if (!result
->replied
) clear_queue_bits( queue
, QS_SMRESULT
);
1660 else set_error( STATUS_ACCESS_DENIED
);
1664 /* set a window timer */
1665 DECL_HANDLER(set_win_timer
)
1667 struct timer
*timer
;
1668 struct msg_queue
*queue
;
1669 struct thread
*thread
= NULL
;
1670 user_handle_t win
= 0;
1671 unsigned int id
= req
->id
;
1675 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1677 set_error( STATUS_INVALID_HANDLE
);
1680 if (thread
->process
!= current
->process
)
1682 release_object( thread
);
1683 set_error( STATUS_ACCESS_DENIED
);
1686 queue
= thread
->queue
;
1687 /* remove it if it existed already */
1688 if ((timer
= find_timer( queue
, win
, req
->msg
, id
))) free_timer( queue
, timer
);
1692 queue
= get_current_queue();
1693 /* find a free id for it */
1696 id
= queue
->next_timer_id
;
1697 if (++queue
->next_timer_id
>= 0x10000) queue
->next_timer_id
= 1;
1699 while (find_timer( queue
, 0, req
->msg
, id
));
1702 if ((timer
= set_timer( queue
, req
->rate
)))
1705 timer
->msg
= req
->msg
;
1707 timer
->lparam
= req
->lparam
;
1710 if (thread
) release_object( thread
);
1713 /* kill a window timer */
1714 DECL_HANDLER(kill_win_timer
)
1716 struct timer
*timer
;
1717 struct thread
*thread
;
1718 user_handle_t win
= 0;
1722 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1724 set_error( STATUS_INVALID_HANDLE
);
1727 if (thread
->process
!= current
->process
)
1729 release_object( thread
);
1730 set_error( STATUS_ACCESS_DENIED
);
1734 else thread
= (struct thread
*)grab_object( current
);
1736 if (thread
->queue
&& (timer
= find_timer( thread
->queue
, win
, req
->msg
, req
->id
)))
1737 free_timer( thread
->queue
, timer
);
1739 set_error( STATUS_INVALID_PARAMETER
);
1741 release_object( thread
);
1745 /* attach (or detach) thread inputs */
1746 DECL_HANDLER(attach_thread_input
)
1748 struct thread
*thread_from
= get_thread_from_id( req
->tid_from
);
1749 struct thread
*thread_to
= get_thread_from_id( req
->tid_to
);
1751 if (!thread_from
|| !thread_to
)
1753 if (thread_from
) release_object( thread_from
);
1754 if (thread_to
) release_object( thread_to
);
1757 if (thread_from
!= thread_to
)
1759 if (req
->attach
) attach_thread_input( thread_from
, thread_to
);
1760 else detach_thread_input( thread_from
, thread_to
);
1762 else set_error( STATUS_ACCESS_DENIED
);
1763 release_object( thread_from
);
1764 release_object( thread_to
);
1768 /* get thread input data */
1769 DECL_HANDLER(get_thread_input
)
1771 struct thread
*thread
= NULL
;
1772 struct thread_input
*input
;
1776 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1777 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1779 else input
= foreground_input
; /* get the foreground thread info */
1783 reply
->focus
= input
->focus
;
1784 reply
->capture
= input
->capture
;
1785 reply
->active
= input
->active
;
1786 reply
->menu_owner
= input
->menu_owner
;
1787 reply
->move_size
= input
->move_size
;
1788 reply
->caret
= input
->caret
;
1789 reply
->rect
= input
->caret_rect
;
1796 reply
->menu_owner
= 0;
1797 reply
->move_size
= 0;
1799 reply
->rect
.left
= reply
->rect
.top
= reply
->rect
.right
= reply
->rect
.bottom
= 0;
1801 /* foreground window is active window of foreground thread */
1802 reply
->foreground
= foreground_input
? foreground_input
->active
: 0;
1803 if (thread
) release_object( thread
);
1807 /* retrieve queue keyboard state for a given thread */
1808 DECL_HANDLER(get_key_state
)
1810 struct thread
*thread
;
1811 struct thread_input
*input
;
1813 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1814 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1817 if (req
->key
>= 0) reply
->state
= input
->keystate
[req
->key
& 0xff];
1818 set_reply_data( input
->keystate
, min( get_reply_max_size(), sizeof(input
->keystate
) ));
1820 release_object( thread
);
1824 /* set queue keyboard state for a given thread */
1825 DECL_HANDLER(set_key_state
)
1827 struct thread
*thread
= NULL
;
1828 struct thread_input
*input
;
1830 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1831 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1834 size_t size
= min( sizeof(input
->keystate
), get_req_data_size() );
1835 if (size
) memcpy( input
->keystate
, get_req_data(), size
);
1837 release_object( thread
);
1841 /* set the system foreground window */
1842 DECL_HANDLER(set_foreground_window
)
1844 struct msg_queue
*queue
= get_current_queue();
1846 reply
->previous
= foreground_input
? foreground_input
->active
: 0;
1847 reply
->send_msg_old
= (reply
->previous
&& foreground_input
!= queue
->input
);
1848 reply
->send_msg_new
= FALSE
;
1852 struct thread
*thread
;
1854 if (is_top_level_window( req
->handle
) &&
1855 ((thread
= get_window_thread( req
->handle
))))
1857 foreground_input
= thread
->queue
->input
;
1858 reply
->send_msg_new
= (foreground_input
!= queue
->input
);
1859 release_object( thread
);
1861 else set_error( STATUS_INVALID_HANDLE
);
1863 else foreground_input
= NULL
;
1867 /* set the current thread focus window */
1868 DECL_HANDLER(set_focus_window
)
1870 struct msg_queue
*queue
= get_current_queue();
1872 reply
->previous
= 0;
1873 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1875 reply
->previous
= queue
->input
->focus
;
1876 queue
->input
->focus
= get_user_full_handle( req
->handle
);
1881 /* set the current thread active window */
1882 DECL_HANDLER(set_active_window
)
1884 struct msg_queue
*queue
= get_current_queue();
1886 reply
->previous
= 0;
1887 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1889 if (!req
->handle
|| make_window_active( req
->handle
))
1891 reply
->previous
= queue
->input
->active
;
1892 queue
->input
->active
= get_user_full_handle( req
->handle
);
1894 else set_error( STATUS_INVALID_HANDLE
);
1899 /* set the current thread capture window */
1900 DECL_HANDLER(set_capture_window
)
1902 struct msg_queue
*queue
= get_current_queue();
1904 reply
->previous
= reply
->full_handle
= 0;
1905 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1907 struct thread_input
*input
= queue
->input
;
1909 reply
->previous
= input
->capture
;
1910 input
->capture
= get_user_full_handle( req
->handle
);
1911 input
->menu_owner
= (req
->flags
& CAPTURE_MENU
) ? input
->capture
: 0;
1912 input
->move_size
= (req
->flags
& CAPTURE_MOVESIZE
) ? input
->capture
: 0;
1913 reply
->full_handle
= input
->capture
;
1918 /* Set the current thread caret window */
1919 DECL_HANDLER(set_caret_window
)
1921 struct msg_queue
*queue
= get_current_queue();
1923 reply
->previous
= 0;
1924 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1926 struct thread_input
*input
= queue
->input
;
1928 reply
->previous
= input
->caret
;
1929 reply
->old_rect
= input
->caret_rect
;
1930 reply
->old_hide
= input
->caret_hide
;
1931 reply
->old_state
= input
->caret_state
;
1933 set_caret_window( input
, get_user_full_handle(req
->handle
) );
1934 input
->caret_rect
.right
= input
->caret_rect
.left
+ req
->width
;
1935 input
->caret_rect
.bottom
= input
->caret_rect
.top
+ req
->height
;
1940 /* Set the current thread caret information */
1941 DECL_HANDLER(set_caret_info
)
1943 struct msg_queue
*queue
= get_current_queue();
1944 struct thread_input
*input
;
1947 input
= queue
->input
;
1948 reply
->full_handle
= input
->caret
;
1949 reply
->old_rect
= input
->caret_rect
;
1950 reply
->old_hide
= input
->caret_hide
;
1951 reply
->old_state
= input
->caret_state
;
1953 if (req
->handle
&& get_user_full_handle(req
->handle
) != input
->caret
)
1955 set_error( STATUS_ACCESS_DENIED
);
1958 if (req
->flags
& SET_CARET_POS
)
1960 input
->caret_rect
.right
+= req
->x
- input
->caret_rect
.left
;
1961 input
->caret_rect
.bottom
+= req
->y
- input
->caret_rect
.top
;
1962 input
->caret_rect
.left
= req
->x
;
1963 input
->caret_rect
.top
= req
->y
;
1965 if (req
->flags
& SET_CARET_HIDE
)
1967 input
->caret_hide
+= req
->hide
;
1968 if (input
->caret_hide
< 0) input
->caret_hide
= 0;
1970 if (req
->flags
& SET_CARET_STATE
)
1972 if (req
->state
== -1) input
->caret_state
= !input
->caret_state
;
1973 else input
->caret_state
= !!req
->state
;