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 enum message_kind
{ SEND_MESSAGE
, POST_MESSAGE
};
42 #define NB_MSG_KINDS (POST_MESSAGE+1)
47 struct list sender_entry
; /* entry in sender list */
48 struct message_result
*recv_next
; /* next in receiver list */
49 struct msg_queue
*sender
; /* sender queue */
50 struct msg_queue
*receiver
; /* receiver queue */
51 int replied
; /* has it been replied to? */
52 unsigned int result
; /* reply result */
53 unsigned int error
; /* error code to pass back to sender */
54 struct message
*callback_msg
; /* message to queue for callback */
55 void *data
; /* message reply data */
56 unsigned int data_size
; /* size of message reply data */
57 struct timeout_user
*timeout
; /* result timeout */
62 struct message
*next
; /* next message in list */
63 struct message
*prev
; /* prev message in list */
64 enum message_type type
; /* message type */
65 user_handle_t win
; /* window handle */
66 unsigned int msg
; /* message code */
67 unsigned int wparam
; /* parameters */
68 unsigned int lparam
; /* parameters */
69 int x
; /* x position */
70 int y
; /* y position */
71 unsigned int time
; /* message time */
72 unsigned int info
; /* extra info */
73 user_handle_t hook
; /* winevent hook handle */
74 void *hook_proc
; /* winevent hook proc address */
75 void *data
; /* message data for sent messages */
76 unsigned int data_size
; /* size of message data */
77 struct message_result
*result
; /* result in sender queue */
82 struct message
*first
; /* head of list */
83 struct message
*last
; /* tail of list */
88 struct list entry
; /* entry in timer list */
89 struct timeval when
; /* next expiration */
90 unsigned int rate
; /* timer rate in ms */
91 user_handle_t win
; /* window handle */
92 unsigned int msg
; /* message to post */
93 unsigned int id
; /* timer id */
94 unsigned int lparam
; /* lparam for message */
99 struct object obj
; /* object header */
100 user_handle_t focus
; /* focus window */
101 user_handle_t capture
; /* capture window */
102 user_handle_t active
; /* active window */
103 user_handle_t menu_owner
; /* current menu owner window */
104 user_handle_t move_size
; /* current moving/resizing window */
105 user_handle_t caret
; /* caret window */
106 rectangle_t caret_rect
; /* caret rectangle */
107 int caret_hide
; /* caret hide count */
108 int caret_state
; /* caret on/off state */
109 struct message
*msg
; /* message currently processed */
110 struct thread
*msg_thread
; /* thread processing the message */
111 struct message_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 struct message_list msg_list
[NB_MSG_KINDS
]; /* lists of messages */
124 struct list send_result
; /* stack of sent messages waiting for result */
125 struct list callback_result
; /* list of callback messages waiting for result */
126 struct message_result
*recv_result
; /* stack of received messages waiting for result */
127 struct list pending_timers
; /* list of pending timers */
128 struct list expired_timers
; /* list of expired timers */
129 unsigned int next_timer_id
; /* id for the next timer with a 0 window */
130 struct timeout_user
*timeout
; /* timeout for next timer to expire */
131 struct thread_input
*input
; /* thread input descriptor */
132 struct hook_table
*hooks
; /* hook table */
133 struct timeval last_get_msg
; /* time of last get message call */
136 static void msg_queue_dump( struct object
*obj
, int verbose
);
137 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
138 static void msg_queue_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
139 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
);
140 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
);
141 static void msg_queue_destroy( struct object
*obj
);
142 static void thread_input_dump( struct object
*obj
, int verbose
);
143 static void thread_input_destroy( struct object
*obj
);
144 static void timer_callback( void *private );
146 static const struct object_ops msg_queue_ops
=
148 sizeof(struct msg_queue
), /* size */
149 msg_queue_dump
, /* dump */
150 msg_queue_add_queue
, /* add_queue */
151 msg_queue_remove_queue
, /* remove_queue */
152 msg_queue_signaled
, /* signaled */
153 msg_queue_satisfied
, /* satisfied */
154 no_get_fd
, /* get_fd */
155 msg_queue_destroy
/* destroy */
159 static const struct object_ops thread_input_ops
=
161 sizeof(struct thread_input
), /* size */
162 thread_input_dump
, /* dump */
163 no_add_queue
, /* add_queue */
164 NULL
, /* remove_queue */
166 NULL
, /* satisfied */
167 no_get_fd
, /* get_fd */
168 thread_input_destroy
/* destroy */
171 /* pointer to input structure of foreground thread */
172 static struct thread_input
*foreground_input
;
175 /* set the caret window in a given thread input */
176 static void set_caret_window( struct thread_input
*input
, user_handle_t win
)
178 if (!win
|| win
!= input
->caret
)
180 input
->caret_rect
.left
= 0;
181 input
->caret_rect
.top
= 0;
182 input
->caret_rect
.right
= 0;
183 input
->caret_rect
.bottom
= 0;
186 input
->caret_hide
= 1;
187 input
->caret_state
= 0;
190 /* create a thread input object */
191 static struct thread_input
*create_thread_input(void)
193 struct thread_input
*input
;
195 if ((input
= alloc_object( &thread_input_ops
)))
200 input
->menu_owner
= 0;
201 input
->move_size
= 0;
203 input
->msg_thread
= NULL
;
204 input
->msg_list
.first
= input
->msg_list
.last
= NULL
;
205 set_caret_window( input
, 0 );
206 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
211 /* release the thread input data of a given thread */
212 static void release_thread_input( struct thread
*thread
)
214 struct thread_input
*input
= thread
->queue
->input
;
217 if (input
->msg_thread
== thread
)
219 release_object( input
->msg_thread
);
220 input
->msg_thread
= NULL
;
223 release_object( input
);
224 thread
->queue
->input
= NULL
;
227 /* create a message queue object */
228 static struct msg_queue
*create_msg_queue( struct thread
*thread
, struct thread_input
*input
)
230 struct msg_queue
*queue
;
233 if (!input
&& !(input
= create_thread_input())) return NULL
;
234 if ((queue
= alloc_object( &msg_queue_ops
)))
236 queue
->wake_bits
= 0;
237 queue
->wake_mask
= 0;
238 queue
->changed_bits
= 0;
239 queue
->changed_mask
= 0;
240 queue
->paint_count
= 0;
241 queue
->recv_result
= NULL
;
242 queue
->next_timer_id
= 1;
243 queue
->timeout
= NULL
;
244 queue
->input
= (struct thread_input
*)grab_object( input
);
246 gettimeofday( &queue
->last_get_msg
, NULL
);
247 list_init( &queue
->send_result
);
248 list_init( &queue
->callback_result
);
249 list_init( &queue
->pending_timers
);
250 list_init( &queue
->expired_timers
);
251 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
252 queue
->msg_list
[i
].first
= queue
->msg_list
[i
].last
= NULL
;
254 thread
->queue
= queue
;
255 if (!thread
->process
->queue
)
256 thread
->process
->queue
= (struct msg_queue
*)grab_object( queue
);
258 release_object( input
);
262 /* free the message queue of a thread at thread exit */
263 void free_msg_queue( struct thread
*thread
)
265 struct process
*process
= thread
->process
;
267 remove_thread_hooks( thread
);
268 if (!thread
->queue
) return;
269 if (process
->queue
== thread
->queue
) /* is it the process main queue? */
271 release_object( process
->queue
);
272 process
->queue
= NULL
;
273 if (process
->idle_event
)
275 set_event( process
->idle_event
);
276 release_object( process
->idle_event
);
277 process
->idle_event
= NULL
;
280 release_thread_input( thread
);
281 release_object( thread
->queue
);
282 thread
->queue
= NULL
;
285 /* get the hook table for a given thread */
286 struct hook_table
*get_queue_hooks( struct thread
*thread
)
288 if (!thread
->queue
) return NULL
;
289 return thread
->queue
->hooks
;
292 /* set the hook table for a given thread, allocating the queue if needed */
293 void set_queue_hooks( struct thread
*thread
, struct hook_table
*hooks
)
295 struct msg_queue
*queue
= thread
->queue
;
296 if (!queue
) queue
= create_msg_queue( thread
, NULL
);
297 if (queue
->hooks
) release_object( queue
->hooks
);
298 queue
->hooks
= hooks
;
301 /* check the queue status */
302 inline static int is_signaled( struct msg_queue
*queue
)
304 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
307 /* set some queue bits */
308 inline static void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
310 queue
->wake_bits
|= bits
;
311 queue
->changed_bits
|= bits
;
312 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
315 /* clear some queue bits */
316 inline static void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
318 queue
->wake_bits
&= ~bits
;
319 queue
->changed_bits
&= ~bits
;
322 /* check whether msg is a keyboard message */
323 inline static int is_keyboard_msg( struct message
*msg
)
325 return (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
);
328 /* get the QS_* bit corresponding to a given hardware message */
329 inline static int get_hardware_msg_bit( struct message
*msg
)
331 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
332 if (is_keyboard_msg( msg
)) return QS_KEY
;
333 return QS_MOUSEBUTTON
;
336 /* get the current thread queue, creating it if needed */
337 inline static struct msg_queue
*get_current_queue(void)
339 struct msg_queue
*queue
= current
->queue
;
340 if (!queue
) queue
= create_msg_queue( current
, NULL
);
344 /* append a message to the end of a list */
345 inline static void append_message( struct message_list
*list
, struct message
*msg
)
348 if ((msg
->prev
= list
->last
)) msg
->prev
->next
= msg
;
349 else list
->first
= msg
;
353 /* unlink a message from a list it */
354 inline static void unlink_message( struct message_list
*list
, struct message
*msg
)
356 if (msg
->next
) msg
->next
->prev
= msg
->prev
;
357 else list
->last
= msg
->prev
;
358 if (msg
->prev
) msg
->prev
->next
= msg
->next
;
359 else list
->first
= msg
->next
;
362 /* try to merge a message with the last in the list; return 1 if successful */
363 static int merge_message( struct thread_input
*input
, const struct message
*msg
)
365 struct message
*prev
= input
->msg_list
.last
;
368 if (input
->msg
== prev
) return 0;
369 if (prev
->result
) return 0;
370 if (prev
->win
!= msg
->win
) return 0;
371 if (prev
->msg
!= msg
->msg
) return 0;
372 if (prev
->type
!= msg
->type
) return 0;
373 /* now we can merge it */
374 prev
->wparam
= msg
->wparam
;
375 prev
->lparam
= msg
->lparam
;
378 prev
->time
= msg
->time
;
379 prev
->info
= msg
->info
;
383 /* free a result structure */
384 static void free_result( struct message_result
*result
)
386 if (result
->timeout
) remove_timeout_user( result
->timeout
);
387 if (result
->data
) free( result
->data
);
388 if (result
->callback_msg
) free( result
->callback_msg
);
392 /* remove the result from the sender list it is on */
393 static inline void remove_result_from_sender( struct message_result
*result
)
395 assert( result
->sender
);
397 list_remove( &result
->sender_entry
);
398 result
->sender
= NULL
;
399 if (!result
->receiver
) free_result( result
);
402 /* store the message result in the appropriate structure */
403 static void store_message_result( struct message_result
*res
, unsigned int result
,
406 res
->result
= result
;
411 remove_timeout_user( res
->timeout
);
416 if (res
->callback_msg
)
418 /* queue the callback message in the sender queue */
419 res
->callback_msg
->lparam
= result
;
420 append_message( &res
->sender
->msg_list
[SEND_MESSAGE
], res
->callback_msg
);
421 set_queue_bits( res
->sender
, QS_SENDMESSAGE
);
422 res
->callback_msg
= NULL
;
423 remove_result_from_sender( res
);
427 /* wake sender queue if waiting on this result */
428 if (list_head(&res
->sender
->send_result
) == &res
->sender_entry
)
429 set_queue_bits( res
->sender
, QS_SMRESULT
);
435 /* free a message when deleting a queue or window */
436 static void free_message( struct message
*msg
)
438 struct message_result
*result
= msg
->result
;
443 result
->receiver
= NULL
;
444 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
446 else free_result( result
);
448 if (msg
->data
) free( msg
->data
);
452 /* remove (and free) a message from a message list */
453 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
454 enum message_kind kind
)
456 unlink_message( &queue
->msg_list
[kind
], msg
);
460 if (!queue
->msg_list
[kind
].first
) clear_queue_bits( queue
, QS_SENDMESSAGE
);
463 if (!queue
->msg_list
[kind
].first
) clear_queue_bits( queue
, QS_POSTMESSAGE
);
469 /* message timed out without getting a reply */
470 static void result_timeout( void *private )
472 struct message_result
*result
= private;
474 assert( !result
->replied
);
476 result
->timeout
= NULL
;
477 store_message_result( result
, 0, STATUS_TIMEOUT
);
480 /* allocate and fill a message result structure */
481 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
482 struct msg_queue
*recv_queue
,
483 struct message
*msg
, unsigned int timeout
,
484 void *callback
, unsigned int callback_data
)
486 struct message_result
*result
= mem_alloc( sizeof(*result
) );
489 result
->sender
= send_queue
;
490 result
->receiver
= recv_queue
;
493 result
->data_size
= 0;
494 result
->timeout
= NULL
;
496 if (msg
->type
== MSG_CALLBACK
)
498 struct message
*callback_msg
= mem_alloc( sizeof(*callback_msg
) );
504 callback_msg
->type
= MSG_CALLBACK_RESULT
;
505 callback_msg
->win
= msg
->win
;
506 callback_msg
->msg
= msg
->msg
;
507 callback_msg
->wparam
= (unsigned int)callback
;
508 callback_msg
->lparam
= 0;
509 callback_msg
->time
= get_tick_count();
512 callback_msg
->info
= callback_data
;
513 callback_msg
->result
= NULL
;
514 callback_msg
->data
= NULL
;
515 callback_msg
->data_size
= 0;
517 result
->callback_msg
= callback_msg
;
518 list_add_head( &send_queue
->callback_result
, &result
->sender_entry
);
522 result
->callback_msg
= NULL
;
523 list_add_head( &send_queue
->send_result
, &result
->sender_entry
);
529 gettimeofday( &when
, 0 );
530 add_timeout( &when
, timeout
);
531 result
->timeout
= add_timeout_user( &when
, result_timeout
, result
);
537 /* receive a message, removing it from the sent queue */
538 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
539 struct get_message_reply
*reply
)
541 struct message_result
*result
= msg
->result
;
543 reply
->total
= msg
->data_size
;
544 if (msg
->data_size
> get_reply_max_size())
546 set_error( STATUS_BUFFER_OVERFLOW
);
549 reply
->type
= msg
->type
;
550 reply
->win
= msg
->win
;
551 reply
->msg
= msg
->msg
;
552 reply
->wparam
= msg
->wparam
;
553 reply
->lparam
= msg
->lparam
;
556 reply
->time
= msg
->time
;
557 reply
->info
= msg
->info
;
558 reply
->hook
= msg
->hook
;
559 reply
->hook_proc
= msg
->hook_proc
;
561 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
563 unlink_message( &queue
->msg_list
[SEND_MESSAGE
], msg
);
564 /* put the result on the receiver result stack */
567 result
->recv_next
= queue
->recv_result
;
568 queue
->recv_result
= result
;
571 if (!queue
->msg_list
[SEND_MESSAGE
].first
) clear_queue_bits( queue
, QS_SENDMESSAGE
);
574 /* set the result of the current received message */
575 static void reply_message( struct msg_queue
*queue
, unsigned int result
,
576 unsigned int error
, int remove
, const void *data
, size_t len
)
578 struct message_result
*res
= queue
->recv_result
;
582 queue
->recv_result
= res
->recv_next
;
583 res
->receiver
= NULL
;
584 if (!res
->sender
) /* no one waiting for it */
592 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
593 store_message_result( res
, result
, error
);
597 /* retrieve a posted message */
598 static int get_posted_message( struct msg_queue
*queue
, user_handle_t win
,
599 unsigned int first
, unsigned int last
, unsigned int flags
,
600 struct get_message_reply
*reply
)
603 struct message_list
*list
= &queue
->msg_list
[POST_MESSAGE
];
605 /* check against the filters */
606 for (msg
= list
->first
; msg
; msg
= msg
->next
)
608 if (msg
->msg
== WM_QUIT
) break; /* WM_QUIT is never filtered */
609 if (win
&& msg
->win
&& msg
->win
!= win
&& !is_child_window( win
, msg
->win
)) continue;
610 if (msg
->msg
< first
) continue;
611 if (msg
->msg
> last
) continue;
612 break; /* found one */
616 /* return it to the app */
618 reply
->total
= msg
->data_size
;
619 if (msg
->data_size
> get_reply_max_size())
621 set_error( STATUS_BUFFER_OVERFLOW
);
624 reply
->type
= msg
->type
;
625 reply
->win
= msg
->win
;
626 reply
->msg
= msg
->msg
;
627 reply
->wparam
= msg
->wparam
;
628 reply
->lparam
= msg
->lparam
;
631 reply
->time
= msg
->time
;
632 reply
->info
= msg
->info
;
634 if (flags
& GET_MSG_REMOVE
)
638 set_reply_data_ptr( msg
->data
, msg
->data_size
);
642 remove_queue_message( queue
, msg
, POST_MESSAGE
);
644 else if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
649 /* empty a message list and free all the messages */
650 static void empty_msg_list( struct message_list
*list
)
652 struct message
*msg
= list
->first
;
655 struct message
*next
= msg
->next
;
661 /* cleanup all pending results when deleting a queue */
662 static void cleanup_results( struct msg_queue
*queue
)
666 while ((entry
= list_head( &queue
->send_result
)) != NULL
)
668 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
671 while ((entry
= list_head( &queue
->callback_result
)) != NULL
)
673 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
676 while (queue
->recv_result
)
677 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
680 /* check if the thread owning the queue is hung (not checking for messages) */
681 static int is_queue_hung( struct msg_queue
*queue
)
684 struct wait_queue_entry
*entry
;
686 gettimeofday( &now
, NULL
);
687 if (now
.tv_sec
- queue
->last_get_msg
.tv_sec
<= 5)
688 return 0; /* less than 5 seconds since last get message -> not hung */
690 for (entry
= queue
->obj
.head
; entry
; entry
= entry
->next
)
692 if (entry
->thread
->queue
== queue
)
693 return 0; /* thread is waiting on queue -> not hung */
698 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
700 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
701 struct process
*process
= entry
->thread
->process
;
703 /* a thread can only wait on its own queue */
704 if (entry
->thread
->queue
!= queue
)
706 set_error( STATUS_ACCESS_DENIED
);
709 /* if waiting on the main process queue, set the idle event */
710 if (process
->queue
== queue
)
712 if (process
->idle_event
) set_event( process
->idle_event
);
714 add_queue( obj
, entry
);
718 static void msg_queue_remove_queue(struct object
*obj
, struct wait_queue_entry
*entry
)
720 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
721 struct process
*process
= entry
->thread
->process
;
723 remove_queue( obj
, entry
);
725 assert( entry
->thread
->queue
== queue
);
727 /* if waiting on the main process queue, reset the idle event */
728 if (process
->queue
== queue
)
730 if (process
->idle_event
) reset_event( process
->idle_event
);
734 static void msg_queue_dump( struct object
*obj
, int verbose
)
736 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
737 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
738 queue
->wake_bits
, queue
->wake_mask
);
741 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
)
743 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
744 return is_signaled( queue
);
747 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
)
749 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
750 queue
->wake_mask
= 0;
751 queue
->changed_mask
= 0;
752 return 0; /* Not abandoned */
755 static void msg_queue_destroy( struct object
*obj
)
757 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
761 cleanup_results( queue
);
762 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
764 while ((ptr
= list_head( &queue
->pending_timers
)))
766 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
767 list_remove( &timer
->entry
);
770 while ((ptr
= list_head( &queue
->expired_timers
)))
772 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
773 list_remove( &timer
->entry
);
776 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
777 if (queue
->input
) release_object( queue
->input
);
778 if (queue
->hooks
) release_object( queue
->hooks
);
781 static void thread_input_dump( struct object
*obj
, int verbose
)
783 struct thread_input
*input
= (struct thread_input
*)obj
;
784 fprintf( stderr
, "Thread input focus=%p capture=%p active=%p\n",
785 input
->focus
, input
->capture
, input
->active
);
788 static void thread_input_destroy( struct object
*obj
)
790 struct thread_input
*input
= (struct thread_input
*)obj
;
792 if (foreground_input
== input
) foreground_input
= NULL
;
793 if (input
->msg_thread
) release_object( input
->msg_thread
);
794 empty_msg_list( &input
->msg_list
);
797 /* fix the thread input data when a window is destroyed */
798 inline static void thread_input_cleanup_window( struct msg_queue
*queue
, user_handle_t window
)
800 struct thread_input
*input
= queue
->input
;
802 if (window
== input
->focus
) input
->focus
= 0;
803 if (window
== input
->capture
) input
->capture
= 0;
804 if (window
== input
->active
) input
->active
= 0;
805 if (window
== input
->menu_owner
) input
->menu_owner
= 0;
806 if (window
== input
->move_size
) input
->move_size
= 0;
807 if (window
== input
->caret
) set_caret_window( input
, 0 );
810 /* check if the specified window can be set in the input data of a given queue */
811 static int check_queue_input_window( struct msg_queue
*queue
, user_handle_t window
)
813 struct thread
*thread
;
816 if (!window
) return 1; /* we can always clear the data */
818 if ((thread
= get_window_thread( window
)))
820 ret
= (queue
->input
== thread
->queue
->input
);
821 if (!ret
) set_error( STATUS_ACCESS_DENIED
);
822 release_object( thread
);
824 else set_error( STATUS_INVALID_HANDLE
);
829 /* attach two thread input data structures */
830 int attach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
832 struct thread_input
*input
;
834 if (!thread_to
->queue
&& !(thread_to
->queue
= create_msg_queue( thread_to
, NULL
))) return 0;
835 input
= (struct thread_input
*)grab_object( thread_to
->queue
->input
);
837 if (thread_from
->queue
)
839 release_thread_input( thread_from
);
840 thread_from
->queue
->input
= input
;
844 if (!(thread_from
->queue
= create_msg_queue( thread_from
, input
))) return 0;
846 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
850 /* detach two thread input data structures */
851 static void detach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
853 struct thread_input
*input
;
855 if (!thread_from
->queue
|| !thread_to
->queue
||
856 thread_from
->queue
->input
!= thread_to
->queue
->input
)
858 set_error( STATUS_ACCESS_DENIED
);
861 if ((input
= create_thread_input()))
863 release_thread_input( thread_from
);
864 thread_from
->queue
->input
= input
;
869 /* set the next timer to expire */
870 static void set_next_timer( struct msg_queue
*queue
)
876 remove_timeout_user( queue
->timeout
);
877 queue
->timeout
= NULL
;
879 if ((ptr
= list_head( &queue
->pending_timers
)))
881 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
882 queue
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, queue
);
884 /* set/clear QS_TIMER bit */
885 if (list_empty( &queue
->expired_timers
))
886 clear_queue_bits( queue
, QS_TIMER
);
888 set_queue_bits( queue
, QS_TIMER
);
891 /* find a timer from its window and id */
892 static struct timer
*find_timer( struct msg_queue
*queue
, user_handle_t win
,
893 unsigned int msg
, unsigned int id
)
897 /* we need to search both lists */
899 LIST_FOR_EACH( ptr
, &queue
->pending_timers
)
901 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
902 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
904 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
906 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
907 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
912 /* callback for the next timer expiration */
913 static void timer_callback( void *private )
915 struct msg_queue
*queue
= private;
918 queue
->timeout
= NULL
;
919 /* move on to the next timer */
920 ptr
= list_head( &queue
->pending_timers
);
922 list_add_tail( &queue
->expired_timers
, ptr
);
923 set_next_timer( queue
);
926 /* link a timer at its rightful place in the queue list */
927 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
931 for (ptr
= queue
->pending_timers
.next
; ptr
!= &queue
->pending_timers
; ptr
= ptr
->next
)
933 struct timer
*t
= LIST_ENTRY( ptr
, struct timer
, entry
);
934 if (!time_before( &t
->when
, &timer
->when
)) break;
936 list_add_before( ptr
, &timer
->entry
);
939 /* remove a timer from the queue timer list and free it */
940 static void free_timer( struct msg_queue
*queue
, struct timer
*timer
)
942 list_remove( &timer
->entry
);
944 set_next_timer( queue
);
947 /* restart an expired timer */
948 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
952 list_remove( &timer
->entry
);
953 gettimeofday( &now
, 0 );
954 while (!time_before( &now
, &timer
->when
)) add_timeout( &timer
->when
, timer
->rate
);
955 link_timer( queue
, timer
);
956 set_next_timer( queue
);
959 /* find an expired timer matching the filtering parameters */
960 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
961 unsigned int get_first
, unsigned int get_last
,
966 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
968 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
969 if (win
&& timer
->win
!= win
) continue;
970 if (timer
->msg
>= get_first
&& timer
->msg
<= get_last
)
972 if (remove
) restart_timer( queue
, timer
);
980 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
982 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
985 timer
->rate
= max( rate
, 1 );
986 gettimeofday( &timer
->when
, 0 );
987 add_timeout( &timer
->when
, rate
);
988 link_timer( queue
, timer
);
989 /* check if we replaced the next timer */
990 if (list_head( &queue
->pending_timers
) == &timer
->entry
) set_next_timer( queue
);
995 /* change the input key state for a given key */
996 static void set_input_key_state( struct thread_input
*input
, unsigned char key
, int down
)
1000 if (!(input
->keystate
[key
] & 0x80)) input
->keystate
[key
] ^= 0x01;
1001 input
->keystate
[key
] |= 0x80;
1003 else input
->keystate
[key
] &= ~0x80;
1006 /* update the input key state for a keyboard message */
1007 static void update_input_key_state( struct thread_input
*input
, const struct message
*msg
)
1010 int down
= 0, extended
;
1014 case WM_LBUTTONDOWN
:
1018 set_input_key_state( input
, VK_LBUTTON
, down
);
1020 case WM_MBUTTONDOWN
:
1024 set_input_key_state( input
, VK_MBUTTON
, down
);
1026 case WM_RBUTTONDOWN
:
1030 set_input_key_state( input
, VK_RBUTTON
, down
);
1038 key
= (unsigned char)msg
->wparam
;
1039 extended
= ((msg
->lparam
>> 16) & KF_EXTENDED
) != 0;
1040 set_input_key_state( input
, key
, down
);
1044 set_input_key_state( input
, extended
? VK_RSHIFT
: VK_LSHIFT
, down
);
1047 set_input_key_state( input
, extended
? VK_RCONTROL
: VK_LCONTROL
, down
);
1050 set_input_key_state( input
, extended
? VK_RMENU
: VK_LMENU
, down
);
1057 /* release the hardware message currently being processed by the given thread */
1058 static void release_hardware_message( struct thread
*thread
, int remove
)
1060 struct thread_input
*input
= thread
->queue
->input
;
1062 if (input
->msg_thread
!= thread
) return;
1065 struct message
*other
;
1068 update_input_key_state( input
, input
->msg
);
1069 unlink_message( &input
->msg_list
, input
->msg
);
1070 clr_bit
= get_hardware_msg_bit( input
->msg
);
1071 for (other
= input
->msg_list
.first
; other
; other
= other
->next
)
1072 if (get_hardware_msg_bit( other
) == clr_bit
) break;
1073 if (!other
) clear_queue_bits( thread
->queue
, clr_bit
);
1074 free_message( input
->msg
);
1076 release_object( input
->msg_thread
);
1078 input
->msg_thread
= NULL
;
1081 /* find the window that should receive a given hardware message */
1082 static user_handle_t
find_hardware_message_window( struct thread_input
*input
, struct message
*msg
,
1083 unsigned int *msg_code
)
1085 user_handle_t win
= 0;
1087 *msg_code
= msg
->msg
;
1088 if (is_keyboard_msg( msg
))
1090 if (input
&& !(win
= input
->focus
))
1092 win
= input
->active
;
1093 if (*msg_code
< WM_SYSKEYDOWN
) *msg_code
+= WM_SYSKEYDOWN
- WM_KEYDOWN
;
1096 else /* mouse message */
1098 if (!input
|| !(win
= input
->capture
))
1100 if (!(win
= msg
->win
) || !is_window_visible( win
))
1101 win
= window_from_point( msg
->x
, msg
->y
);
1107 /* queue a hardware message into a given thread input */
1108 static void queue_hardware_message( struct msg_queue
*queue
, struct message
*msg
)
1111 struct thread
*thread
;
1112 struct thread_input
*input
;
1113 unsigned int msg_code
;
1115 win
= find_hardware_message_window( queue
? queue
->input
: foreground_input
, msg
, &msg_code
);
1116 if (!win
|| !(thread
= get_window_thread(win
)))
1121 input
= thread
->queue
->input
;
1123 if (msg
->msg
== WM_MOUSEMOVE
&& merge_message( input
, msg
)) free( msg
);
1126 append_message( &input
->msg_list
, msg
);
1127 set_queue_bits( thread
->queue
, get_hardware_msg_bit(msg
) );
1129 release_object( thread
);
1132 /* find a hardware message for the given queue */
1133 static int get_hardware_message( struct thread
*thread
, struct message
*first
,
1134 user_handle_t filter_win
, struct get_message_reply
*reply
)
1136 struct thread_input
*input
= thread
->queue
->input
;
1137 struct thread
*win_thread
;
1138 struct message
*msg
;
1140 int clear_bits
, got_one
= 0;
1141 unsigned int msg_code
;
1143 if (input
->msg_thread
&& input
->msg_thread
!= thread
)
1144 return 0; /* locked by another thread */
1148 msg
= input
->msg_list
.first
;
1149 clear_bits
= QS_KEY
| QS_MOUSEMOVE
| QS_MOUSEBUTTON
;
1154 clear_bits
= 0; /* don't clear bits if we don't go through the whole list */
1159 win
= find_hardware_message_window( input
, msg
, &msg_code
);
1160 if (!win
|| !(win_thread
= get_window_thread( win
)))
1162 /* no window at all, remove it */
1163 struct message
*next
= msg
->next
;
1164 update_input_key_state( input
, msg
);
1165 unlink_message( &input
->msg_list
, msg
);
1166 free_message( msg
);
1170 if (win_thread
!= thread
)
1172 /* wake the other thread */
1173 set_queue_bits( win_thread
->queue
, get_hardware_msg_bit(msg
) );
1174 release_object( win_thread
);
1179 /* if we already got a message for another thread, or if it doesn't
1180 * match the filter we skip it (filter is only checked for keyboard
1181 * messages since the dest window for a mouse message depends on hittest)
1184 (filter_win
&& is_keyboard_msg(msg
) &&
1185 win
!= filter_win
&& !is_child_window( filter_win
, win
)))
1187 clear_bits
&= ~get_hardware_msg_bit( msg
);
1188 release_object( win_thread
);
1192 /* now we can return it */
1193 if (!input
->msg_thread
) input
->msg_thread
= win_thread
;
1194 else release_object( win_thread
);
1197 reply
->type
= MSG_HARDWARE
;
1199 reply
->msg
= msg_code
;
1200 reply
->wparam
= msg
->wparam
;
1201 reply
->lparam
= msg
->lparam
;
1204 reply
->time
= msg
->time
;
1205 reply
->info
= msg
->info
;
1208 /* nothing found, clear the hardware queue bits */
1209 clear_queue_bits( thread
->queue
, clear_bits
);
1210 if (input
->msg_thread
) release_object( input
->msg_thread
);
1212 input
->msg_thread
= NULL
;
1216 /* increment (or decrement if 'incr' is negative) the queue paint count */
1217 void inc_queue_paint_count( struct thread
*thread
, int incr
)
1219 struct msg_queue
*queue
= thread
->queue
;
1223 if ((queue
->paint_count
+= incr
) < 0) queue
->paint_count
= 0;
1225 if (queue
->paint_count
)
1226 set_queue_bits( queue
, QS_PAINT
);
1228 clear_queue_bits( queue
, QS_PAINT
);
1232 /* remove all messages and timers belonging to a certain window */
1233 void queue_cleanup_window( struct thread
*thread
, user_handle_t win
)
1235 struct msg_queue
*queue
= thread
->queue
;
1237 struct message
*msg
;
1244 ptr
= list_head( &queue
->pending_timers
);
1247 struct list
*next
= list_next( &queue
->pending_timers
, ptr
);
1248 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1249 if (timer
->win
== win
) free_timer( queue
, timer
);
1252 ptr
= list_head( &queue
->expired_timers
);
1255 struct list
*next
= list_next( &queue
->expired_timers
, ptr
);
1256 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1257 if (timer
->win
== win
) free_timer( queue
, timer
);
1261 /* remove messages */
1262 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
1264 msg
= queue
->msg_list
[i
].first
;
1267 struct message
*next
= msg
->next
;
1268 if (msg
->win
== win
) remove_queue_message( queue
, msg
, i
);
1273 thread_input_cleanup_window( queue
, win
);
1276 /* post a message to a window; used by socket handling */
1277 void post_message( user_handle_t win
, unsigned int message
,
1278 unsigned int wparam
, unsigned int lparam
)
1280 struct message
*msg
;
1281 struct thread
*thread
= get_window_thread( win
);
1283 if (!thread
) return;
1285 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1287 msg
->type
= MSG_POSTED
;
1288 msg
->win
= get_user_full_handle( win
);
1290 msg
->wparam
= wparam
;
1291 msg
->lparam
= lparam
;
1292 msg
->time
= get_tick_count();
1300 append_message( &thread
->queue
->msg_list
[POST_MESSAGE
], msg
);
1301 set_queue_bits( thread
->queue
, QS_POSTMESSAGE
);
1303 release_object( thread
);
1306 /* post a win event */
1307 void post_win_event( struct thread
*thread
, unsigned int event
,
1308 user_handle_t win
, unsigned int object_id
,
1309 unsigned int child_id
, void *hook_proc
,
1310 const WCHAR
*module
, size_t module_size
,
1313 struct message
*msg
;
1315 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1317 msg
->type
= MSG_WINEVENT
;
1318 msg
->win
= get_user_full_handle( win
);
1320 msg
->wparam
= object_id
;
1321 msg
->lparam
= child_id
;
1322 msg
->time
= get_tick_count();
1325 msg
->info
= get_thread_id( current
);
1328 msg
->hook_proc
= hook_proc
;
1330 if ((msg
->data
= malloc( module_size
)))
1332 msg
->data_size
= module_size
;
1333 memcpy( msg
->data
, module
, module_size
);
1335 if (debug_level
> 1)
1336 fprintf( stderr
, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1337 get_thread_id(thread
), event
, win
, object_id
, child_id
);
1338 append_message( &thread
->queue
->msg_list
[SEND_MESSAGE
], msg
);
1339 set_queue_bits( thread
->queue
, QS_SENDMESSAGE
);
1346 /* get the message queue of the current thread */
1347 DECL_HANDLER(get_msg_queue
)
1349 struct msg_queue
*queue
= get_current_queue();
1352 if (queue
) reply
->handle
= alloc_handle( current
->process
, queue
, SYNCHRONIZE
, 0 );
1356 /* set the current message queue wakeup mask */
1357 DECL_HANDLER(set_queue_mask
)
1359 struct msg_queue
*queue
= get_current_queue();
1363 queue
->wake_mask
= req
->wake_mask
;
1364 queue
->changed_mask
= req
->changed_mask
;
1365 reply
->wake_bits
= queue
->wake_bits
;
1366 reply
->changed_bits
= queue
->changed_bits
;
1367 if (is_signaled( queue
))
1369 /* if skip wait is set, do what would have been done in the subsequent wait */
1370 if (req
->skip_wait
) msg_queue_satisfied( &queue
->obj
, current
);
1371 else wake_up( &queue
->obj
, 0 );
1377 /* get the current message queue status */
1378 DECL_HANDLER(get_queue_status
)
1380 struct msg_queue
*queue
= current
->queue
;
1383 reply
->wake_bits
= queue
->wake_bits
;
1384 reply
->changed_bits
= queue
->changed_bits
;
1385 if (req
->clear
) queue
->changed_bits
= 0;
1387 else reply
->wake_bits
= reply
->changed_bits
= 0;
1391 /* send a message to a thread queue */
1392 DECL_HANDLER(send_message
)
1394 struct message
*msg
;
1395 struct msg_queue
*send_queue
= get_current_queue();
1396 struct msg_queue
*recv_queue
= NULL
;
1397 struct thread
*thread
= NULL
;
1401 if (!(thread
= get_thread_from_id( req
->id
))) return;
1403 else if (req
->type
!= MSG_HARDWARE
)
1405 /* only hardware messages are allowed without destination thread */
1406 set_error( STATUS_INVALID_PARAMETER
);
1410 if (thread
&& !(recv_queue
= thread
->queue
))
1412 set_error( STATUS_INVALID_PARAMETER
);
1413 release_object( thread
);
1416 if (recv_queue
&& (req
->flags
& SEND_MSG_ABORT_IF_HUNG
) && is_queue_hung(recv_queue
))
1418 set_error( STATUS_TIMEOUT
);
1419 release_object( thread
);
1423 if ((msg
= mem_alloc( sizeof(*msg
) )))
1425 msg
->type
= req
->type
;
1426 msg
->win
= get_user_full_handle( req
->win
);
1427 msg
->msg
= req
->msg
;
1428 msg
->wparam
= req
->wparam
;
1429 msg
->lparam
= req
->lparam
;
1430 msg
->time
= req
->time
;
1433 msg
->info
= req
->info
;
1440 case MSG_OTHER_PROCESS
:
1441 msg
->data_size
= get_req_data_size();
1442 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1451 if (!(msg
->result
= alloc_message_result( send_queue
, recv_queue
, msg
,
1452 req
->timeout
, req
->callback
, req
->info
)))
1454 free_message( msg
);
1459 append_message( &recv_queue
->msg_list
[SEND_MESSAGE
], msg
);
1460 set_queue_bits( recv_queue
, QS_SENDMESSAGE
);
1463 /* needed for posted DDE messages */
1464 msg
->data_size
= get_req_data_size();
1465 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1470 append_message( &recv_queue
->msg_list
[POST_MESSAGE
], msg
);
1471 set_queue_bits( recv_queue
, QS_POSTMESSAGE
);
1474 queue_hardware_message( recv_queue
, msg
);
1476 case MSG_CALLBACK_RESULT
: /* cannot send this one */
1478 set_error( STATUS_INVALID_PARAMETER
);
1483 if (thread
) release_object( thread
);
1487 /* get a message from the current queue */
1488 DECL_HANDLER(get_message
)
1490 struct timer
*timer
;
1491 struct message
*msg
;
1492 struct message
*first_hw_msg
= NULL
;
1493 struct msg_queue
*queue
= get_current_queue();
1494 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
1497 gettimeofday( &queue
->last_get_msg
, NULL
);
1499 /* first of all release the hardware input lock if we own it */
1500 /* we'll grab it again if we find a hardware message */
1501 if (queue
->input
->msg_thread
== current
)
1503 first_hw_msg
= queue
->input
->msg
;
1504 release_hardware_message( current
, 0 );
1507 /* first check for sent messages */
1508 if ((msg
= queue
->msg_list
[SEND_MESSAGE
].first
))
1510 receive_message( queue
, msg
, reply
);
1513 if (req
->flags
& GET_MSG_SENT_ONLY
) goto done
; /* nothing else to check */
1515 /* clear changed bits so we can wait on them if we don't find a message */
1516 queue
->changed_bits
= 0;
1518 /* then check for posted messages */
1519 if (get_posted_message( queue
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
1522 /* then check for any raw hardware message */
1523 if (get_hardware_message( current
, first_hw_msg
, get_win
, reply
))
1526 /* now check for WM_PAINT */
1527 if (queue
->paint_count
&&
1528 (WM_PAINT
>= req
->get_first
) && (WM_PAINT
<= req
->get_last
) &&
1529 (reply
->win
= find_window_to_repaint( get_win
, current
)))
1531 reply
->type
= MSG_POSTED
;
1532 reply
->msg
= WM_PAINT
;
1537 reply
->time
= get_tick_count();
1542 /* now check for timer */
1543 if ((timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
1544 req
->get_last
, (req
->flags
& GET_MSG_REMOVE
) )))
1546 reply
->type
= MSG_POSTED
;
1547 reply
->win
= timer
->win
;
1548 reply
->msg
= timer
->msg
;
1549 reply
->wparam
= timer
->id
;
1550 reply
->lparam
= timer
->lparam
;
1553 reply
->time
= get_tick_count();
1559 set_error( STATUS_PENDING
); /* FIXME */
1563 /* reply to a sent message */
1564 DECL_HANDLER(reply_message
)
1566 if (!current
->queue
)
1568 set_error( STATUS_ACCESS_DENIED
);
1571 if (req
->type
== MSG_HARDWARE
)
1573 struct thread_input
*input
= current
->queue
->input
;
1574 if (input
->msg_thread
== current
) release_hardware_message( current
, req
->remove
);
1575 else set_error( STATUS_ACCESS_DENIED
);
1577 else if (current
->queue
->recv_result
)
1578 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
1579 get_req_data(), get_req_data_size() );
1583 /* retrieve the reply for the last message sent */
1584 DECL_HANDLER(get_message_reply
)
1586 struct message_result
*result
;
1588 struct msg_queue
*queue
= current
->queue
;
1592 set_error( STATUS_PENDING
);
1595 if (!(entry
= list_head( &queue
->send_result
))) return; /* no reply ready */
1597 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1598 if (result
->replied
|| req
->cancel
)
1600 if (result
->replied
)
1602 reply
->result
= result
->result
;
1603 set_error( result
->error
);
1606 size_t data_len
= min( result
->data_size
, get_reply_max_size() );
1607 set_reply_data_ptr( result
->data
, data_len
);
1608 result
->data
= NULL
;
1609 result
->data_size
= 0;
1612 remove_result_from_sender( result
);
1614 entry
= list_head( &queue
->send_result
);
1615 if (!entry
) clear_queue_bits( queue
, QS_SMRESULT
);
1618 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1619 if (!result
->replied
) clear_queue_bits( queue
, QS_SMRESULT
);
1623 else set_error( STATUS_ACCESS_DENIED
);
1627 /* set a window timer */
1628 DECL_HANDLER(set_win_timer
)
1630 struct timer
*timer
;
1631 struct msg_queue
*queue
;
1632 struct thread
*thread
= NULL
;
1633 user_handle_t win
= 0;
1634 unsigned int id
= req
->id
;
1638 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1640 set_error( STATUS_INVALID_HANDLE
);
1643 if (thread
->process
!= current
->process
)
1645 release_object( thread
);
1646 set_error( STATUS_ACCESS_DENIED
);
1649 queue
= thread
->queue
;
1650 /* remove it if it existed already */
1651 if ((timer
= find_timer( queue
, win
, req
->msg
, id
))) free_timer( queue
, timer
);
1655 queue
= get_current_queue();
1656 /* find a free id for it */
1659 id
= queue
->next_timer_id
;
1660 if (++queue
->next_timer_id
>= 0x10000) queue
->next_timer_id
= 1;
1662 while (find_timer( queue
, 0, req
->msg
, id
));
1665 if ((timer
= set_timer( queue
, req
->rate
)))
1668 timer
->msg
= req
->msg
;
1670 timer
->lparam
= req
->lparam
;
1673 if (thread
) release_object( thread
);
1676 /* kill a window timer */
1677 DECL_HANDLER(kill_win_timer
)
1679 struct timer
*timer
;
1680 struct thread
*thread
;
1681 user_handle_t win
= 0;
1685 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1687 set_error( STATUS_INVALID_HANDLE
);
1690 if (thread
->process
!= current
->process
)
1692 release_object( thread
);
1693 set_error( STATUS_ACCESS_DENIED
);
1697 else thread
= (struct thread
*)grab_object( current
);
1699 if (thread
->queue
&& (timer
= find_timer( thread
->queue
, win
, req
->msg
, req
->id
)))
1700 free_timer( thread
->queue
, timer
);
1702 set_error( STATUS_INVALID_PARAMETER
);
1704 release_object( thread
);
1708 /* attach (or detach) thread inputs */
1709 DECL_HANDLER(attach_thread_input
)
1711 struct thread
*thread_from
= get_thread_from_id( req
->tid_from
);
1712 struct thread
*thread_to
= get_thread_from_id( req
->tid_to
);
1714 if (!thread_from
|| !thread_to
)
1716 if (thread_from
) release_object( thread_from
);
1717 if (thread_to
) release_object( thread_to
);
1720 if (thread_from
!= thread_to
)
1722 if (req
->attach
) attach_thread_input( thread_from
, thread_to
);
1723 else detach_thread_input( thread_from
, thread_to
);
1725 else set_error( STATUS_ACCESS_DENIED
);
1726 release_object( thread_from
);
1727 release_object( thread_to
);
1731 /* get thread input data */
1732 DECL_HANDLER(get_thread_input
)
1734 struct thread
*thread
= NULL
;
1735 struct thread_input
*input
;
1739 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1740 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1742 else input
= foreground_input
; /* get the foreground thread info */
1746 reply
->focus
= input
->focus
;
1747 reply
->capture
= input
->capture
;
1748 reply
->active
= input
->active
;
1749 reply
->menu_owner
= input
->menu_owner
;
1750 reply
->move_size
= input
->move_size
;
1751 reply
->caret
= input
->caret
;
1752 reply
->rect
= input
->caret_rect
;
1759 reply
->menu_owner
= 0;
1760 reply
->move_size
= 0;
1762 reply
->rect
.left
= reply
->rect
.top
= reply
->rect
.right
= reply
->rect
.bottom
= 0;
1764 /* foreground window is active window of foreground thread */
1765 reply
->foreground
= foreground_input
? foreground_input
->active
: 0;
1766 if (thread
) release_object( thread
);
1770 /* retrieve queue keyboard state for a given thread */
1771 DECL_HANDLER(get_key_state
)
1773 struct thread
*thread
;
1774 struct thread_input
*input
;
1776 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1777 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1780 if (req
->key
>= 0) reply
->state
= input
->keystate
[req
->key
& 0xff];
1781 set_reply_data( input
->keystate
, min( get_reply_max_size(), sizeof(input
->keystate
) ));
1783 release_object( thread
);
1787 /* set queue keyboard state for a given thread */
1788 DECL_HANDLER(set_key_state
)
1790 struct thread
*thread
= NULL
;
1791 struct thread_input
*input
;
1793 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1794 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1797 size_t size
= min( sizeof(input
->keystate
), get_req_data_size() );
1798 if (size
) memcpy( input
->keystate
, get_req_data(), size
);
1800 release_object( thread
);
1804 /* set the system foreground window */
1805 DECL_HANDLER(set_foreground_window
)
1807 struct msg_queue
*queue
= get_current_queue();
1809 reply
->previous
= foreground_input
? foreground_input
->active
: 0;
1810 reply
->send_msg_old
= (reply
->previous
&& foreground_input
!= queue
->input
);
1811 reply
->send_msg_new
= FALSE
;
1815 struct thread
*thread
;
1817 if (is_top_level_window( req
->handle
) &&
1818 ((thread
= get_window_thread( req
->handle
))))
1820 foreground_input
= thread
->queue
->input
;
1821 reply
->send_msg_new
= (foreground_input
!= queue
->input
);
1822 release_object( thread
);
1824 else set_error( STATUS_INVALID_HANDLE
);
1826 else foreground_input
= NULL
;
1830 /* set the current thread focus window */
1831 DECL_HANDLER(set_focus_window
)
1833 struct msg_queue
*queue
= get_current_queue();
1835 reply
->previous
= 0;
1836 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1838 reply
->previous
= queue
->input
->focus
;
1839 queue
->input
->focus
= get_user_full_handle( req
->handle
);
1844 /* set the current thread active window */
1845 DECL_HANDLER(set_active_window
)
1847 struct msg_queue
*queue
= get_current_queue();
1849 reply
->previous
= 0;
1850 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1852 if (!req
->handle
|| make_window_active( req
->handle
))
1854 reply
->previous
= queue
->input
->active
;
1855 queue
->input
->active
= get_user_full_handle( req
->handle
);
1857 else set_error( STATUS_INVALID_HANDLE
);
1862 /* set the current thread capture window */
1863 DECL_HANDLER(set_capture_window
)
1865 struct msg_queue
*queue
= get_current_queue();
1867 reply
->previous
= reply
->full_handle
= 0;
1868 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1870 struct thread_input
*input
= queue
->input
;
1872 reply
->previous
= input
->capture
;
1873 input
->capture
= get_user_full_handle( req
->handle
);
1874 input
->menu_owner
= (req
->flags
& CAPTURE_MENU
) ? input
->capture
: 0;
1875 input
->move_size
= (req
->flags
& CAPTURE_MOVESIZE
) ? input
->capture
: 0;
1876 reply
->full_handle
= input
->capture
;
1881 /* Set the current thread caret window */
1882 DECL_HANDLER(set_caret_window
)
1884 struct msg_queue
*queue
= get_current_queue();
1886 reply
->previous
= 0;
1887 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1889 struct thread_input
*input
= queue
->input
;
1891 reply
->previous
= input
->caret
;
1892 reply
->old_rect
= input
->caret_rect
;
1893 reply
->old_hide
= input
->caret_hide
;
1894 reply
->old_state
= input
->caret_state
;
1896 set_caret_window( input
, get_user_full_handle(req
->handle
) );
1897 input
->caret_rect
.right
= input
->caret_rect
.left
+ req
->width
;
1898 input
->caret_rect
.bottom
= input
->caret_rect
.top
+ req
->height
;
1903 /* Set the current thread caret information */
1904 DECL_HANDLER(set_caret_info
)
1906 struct msg_queue
*queue
= get_current_queue();
1907 struct thread_input
*input
;
1910 input
= queue
->input
;
1911 reply
->full_handle
= input
->caret
;
1912 reply
->old_rect
= input
->caret_rect
;
1913 reply
->old_hide
= input
->caret_hide
;
1914 reply
->old_state
= input
->caret_state
;
1916 if (req
->handle
&& get_user_full_handle(req
->handle
) != input
->caret
)
1918 set_error( STATUS_ACCESS_DENIED
);
1921 if (req
->flags
& SET_CARET_POS
)
1923 input
->caret_rect
.right
+= req
->x
- input
->caret_rect
.left
;
1924 input
->caret_rect
.bottom
+= req
->y
- input
->caret_rect
.top
;
1925 input
->caret_rect
.left
= req
->x
;
1926 input
->caret_rect
.top
= req
->y
;
1928 if (req
->flags
& SET_CARET_HIDE
)
1930 input
->caret_hide
+= req
->hide
;
1931 if (input
->caret_hide
< 0) input
->caret_hide
= 0;
1933 if (req
->flags
& SET_CARET_STATE
)
1935 if (req
->state
== -1) input
->caret_state
= !input
->caret_state
;
1936 else input
->caret_state
= !!req
->state
;