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
)
179 input
->caret_rect
.left
= 0;
180 input
->caret_rect
.top
= 0;
181 input
->caret_rect
.right
= 0;
182 input
->caret_rect
.bottom
= 0;
183 input
->caret_hide
= 1;
184 input
->caret_state
= 0;
187 /* create a thread input object */
188 static struct thread_input
*create_thread_input(void)
190 struct thread_input
*input
;
192 if ((input
= alloc_object( &thread_input_ops
)))
197 input
->menu_owner
= 0;
198 input
->move_size
= 0;
200 input
->msg_thread
= NULL
;
201 input
->msg_list
.first
= input
->msg_list
.last
= NULL
;
202 set_caret_window( input
, 0 );
203 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
208 /* release the thread input data of a given thread */
209 static void release_thread_input( struct thread
*thread
)
211 struct thread_input
*input
= thread
->queue
->input
;
214 if (input
->msg_thread
== thread
)
216 release_object( input
->msg_thread
);
217 input
->msg_thread
= NULL
;
220 release_object( input
);
221 thread
->queue
->input
= NULL
;
224 /* create a message queue object */
225 static struct msg_queue
*create_msg_queue( struct thread
*thread
, struct thread_input
*input
)
227 struct msg_queue
*queue
;
230 if (!input
&& !(input
= create_thread_input())) return NULL
;
231 if ((queue
= alloc_object( &msg_queue_ops
)))
233 queue
->wake_bits
= 0;
234 queue
->wake_mask
= 0;
235 queue
->changed_bits
= 0;
236 queue
->changed_mask
= 0;
237 queue
->paint_count
= 0;
238 queue
->recv_result
= NULL
;
239 queue
->next_timer_id
= 1;
240 queue
->timeout
= NULL
;
241 queue
->input
= (struct thread_input
*)grab_object( input
);
243 gettimeofday( &queue
->last_get_msg
, NULL
);
244 list_init( &queue
->send_result
);
245 list_init( &queue
->callback_result
);
246 list_init( &queue
->pending_timers
);
247 list_init( &queue
->expired_timers
);
248 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
249 queue
->msg_list
[i
].first
= queue
->msg_list
[i
].last
= NULL
;
251 thread
->queue
= queue
;
252 if (!thread
->process
->queue
)
253 thread
->process
->queue
= (struct msg_queue
*)grab_object( queue
);
255 release_object( input
);
259 /* free the message queue of a thread at thread exit */
260 void free_msg_queue( struct thread
*thread
)
262 struct process
*process
= thread
->process
;
264 remove_thread_hooks( thread
);
265 if (!thread
->queue
) return;
266 if (process
->queue
== thread
->queue
) /* is it the process main queue? */
268 release_object( process
->queue
);
269 process
->queue
= NULL
;
270 if (process
->idle_event
)
272 set_event( process
->idle_event
);
273 release_object( process
->idle_event
);
274 process
->idle_event
= NULL
;
277 release_thread_input( thread
);
278 release_object( thread
->queue
);
279 thread
->queue
= NULL
;
282 /* get the hook table for a given thread */
283 struct hook_table
*get_queue_hooks( struct thread
*thread
)
285 if (!thread
->queue
) return NULL
;
286 return thread
->queue
->hooks
;
289 /* set the hook table for a given thread, allocating the queue if needed */
290 void set_queue_hooks( struct thread
*thread
, struct hook_table
*hooks
)
292 struct msg_queue
*queue
= thread
->queue
;
293 if (!queue
) queue
= create_msg_queue( thread
, NULL
);
294 if (queue
->hooks
) release_object( queue
->hooks
);
295 queue
->hooks
= hooks
;
298 /* check the queue status */
299 inline static int is_signaled( struct msg_queue
*queue
)
301 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
304 /* set some queue bits */
305 inline static void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
307 queue
->wake_bits
|= bits
;
308 queue
->changed_bits
|= bits
;
309 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
312 /* clear some queue bits */
313 inline static void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
315 queue
->wake_bits
&= ~bits
;
316 queue
->changed_bits
&= ~bits
;
319 /* check whether msg is a keyboard message */
320 inline static int is_keyboard_msg( struct message
*msg
)
322 return (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
);
325 /* get the QS_* bit corresponding to a given hardware message */
326 inline static int get_hardware_msg_bit( struct message
*msg
)
328 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
329 if (is_keyboard_msg( msg
)) return QS_KEY
;
330 return QS_MOUSEBUTTON
;
333 /* get the current thread queue, creating it if needed */
334 inline static struct msg_queue
*get_current_queue(void)
336 struct msg_queue
*queue
= current
->queue
;
337 if (!queue
) queue
= create_msg_queue( current
, NULL
);
341 /* append a message to the end of a list */
342 inline static void append_message( struct message_list
*list
, struct message
*msg
)
345 if ((msg
->prev
= list
->last
)) msg
->prev
->next
= msg
;
346 else list
->first
= msg
;
350 /* unlink a message from a list it */
351 inline static void unlink_message( struct message_list
*list
, struct message
*msg
)
353 if (msg
->next
) msg
->next
->prev
= msg
->prev
;
354 else list
->last
= msg
->prev
;
355 if (msg
->prev
) msg
->prev
->next
= msg
->next
;
356 else list
->first
= msg
->next
;
359 /* try to merge a message with the last in the list; return 1 if successful */
360 static int merge_message( struct thread_input
*input
, const struct message
*msg
)
362 struct message
*prev
= input
->msg_list
.last
;
365 if (input
->msg
== prev
) return 0;
366 if (prev
->result
) return 0;
367 if (prev
->win
!= msg
->win
) return 0;
368 if (prev
->msg
!= msg
->msg
) return 0;
369 if (prev
->type
!= msg
->type
) return 0;
370 /* now we can merge it */
371 prev
->wparam
= msg
->wparam
;
372 prev
->lparam
= msg
->lparam
;
375 prev
->time
= msg
->time
;
376 prev
->info
= msg
->info
;
380 /* free a result structure */
381 static void free_result( struct message_result
*result
)
383 if (result
->timeout
) remove_timeout_user( result
->timeout
);
384 if (result
->data
) free( result
->data
);
385 if (result
->callback_msg
) free( result
->callback_msg
);
389 /* remove the result from the sender list it is on */
390 static inline void remove_result_from_sender( struct message_result
*result
)
392 assert( result
->sender
);
394 list_remove( &result
->sender_entry
);
395 result
->sender
= NULL
;
396 if (!result
->receiver
) free_result( result
);
399 /* store the message result in the appropriate structure */
400 static void store_message_result( struct message_result
*res
, unsigned int result
,
403 res
->result
= result
;
408 remove_timeout_user( res
->timeout
);
413 if (res
->callback_msg
)
415 /* queue the callback message in the sender queue */
416 res
->callback_msg
->lparam
= result
;
417 append_message( &res
->sender
->msg_list
[SEND_MESSAGE
], res
->callback_msg
);
418 set_queue_bits( res
->sender
, QS_SENDMESSAGE
);
419 res
->callback_msg
= NULL
;
420 remove_result_from_sender( res
);
424 /* wake sender queue if waiting on this result */
425 if (list_head(&res
->sender
->send_result
) == &res
->sender_entry
)
426 set_queue_bits( res
->sender
, QS_SMRESULT
);
432 /* free a message when deleting a queue or window */
433 static void free_message( struct message
*msg
)
435 struct message_result
*result
= msg
->result
;
440 result
->receiver
= NULL
;
441 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
443 else free_result( result
);
445 if (msg
->data
) free( msg
->data
);
449 /* remove (and free) a message from a message list */
450 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
451 enum message_kind kind
)
453 unlink_message( &queue
->msg_list
[kind
], msg
);
457 if (!queue
->msg_list
[kind
].first
) clear_queue_bits( queue
, QS_SENDMESSAGE
);
460 if (!queue
->msg_list
[kind
].first
) clear_queue_bits( queue
, QS_POSTMESSAGE
);
466 /* message timed out without getting a reply */
467 static void result_timeout( void *private )
469 struct message_result
*result
= private;
471 assert( !result
->replied
);
473 result
->timeout
= NULL
;
474 store_message_result( result
, 0, STATUS_TIMEOUT
);
477 /* allocate and fill a message result structure */
478 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
479 struct msg_queue
*recv_queue
,
480 struct message
*msg
, unsigned int timeout
,
481 void *callback
, unsigned int callback_data
)
483 struct message_result
*result
= mem_alloc( sizeof(*result
) );
486 result
->sender
= send_queue
;
487 result
->receiver
= recv_queue
;
490 result
->data_size
= 0;
491 result
->timeout
= NULL
;
493 if (msg
->type
== MSG_CALLBACK
)
495 struct message
*callback_msg
= mem_alloc( sizeof(*callback_msg
) );
501 callback_msg
->type
= MSG_CALLBACK_RESULT
;
502 callback_msg
->win
= msg
->win
;
503 callback_msg
->msg
= msg
->msg
;
504 callback_msg
->wparam
= (unsigned int)callback
;
505 callback_msg
->lparam
= 0;
506 callback_msg
->time
= get_tick_count();
509 callback_msg
->info
= callback_data
;
510 callback_msg
->result
= NULL
;
511 callback_msg
->data
= NULL
;
512 callback_msg
->data_size
= 0;
514 result
->callback_msg
= callback_msg
;
515 list_add_head( &send_queue
->callback_result
, &result
->sender_entry
);
519 result
->callback_msg
= NULL
;
520 list_add_head( &send_queue
->send_result
, &result
->sender_entry
);
526 gettimeofday( &when
, 0 );
527 add_timeout( &when
, timeout
);
528 result
->timeout
= add_timeout_user( &when
, result_timeout
, result
);
534 /* receive a message, removing it from the sent queue */
535 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
536 struct get_message_reply
*reply
)
538 struct message_result
*result
= msg
->result
;
540 reply
->total
= msg
->data_size
;
541 if (msg
->data_size
> get_reply_max_size())
543 set_error( STATUS_BUFFER_OVERFLOW
);
546 reply
->type
= msg
->type
;
547 reply
->win
= msg
->win
;
548 reply
->msg
= msg
->msg
;
549 reply
->wparam
= msg
->wparam
;
550 reply
->lparam
= msg
->lparam
;
553 reply
->time
= msg
->time
;
554 reply
->info
= msg
->info
;
555 reply
->hook
= msg
->hook
;
556 reply
->hook_proc
= msg
->hook_proc
;
558 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
560 unlink_message( &queue
->msg_list
[SEND_MESSAGE
], msg
);
561 /* put the result on the receiver result stack */
564 result
->recv_next
= queue
->recv_result
;
565 queue
->recv_result
= result
;
568 if (!queue
->msg_list
[SEND_MESSAGE
].first
) clear_queue_bits( queue
, QS_SENDMESSAGE
);
571 /* set the result of the current received message */
572 static void reply_message( struct msg_queue
*queue
, unsigned int result
,
573 unsigned int error
, int remove
, const void *data
, size_t len
)
575 struct message_result
*res
= queue
->recv_result
;
579 queue
->recv_result
= res
->recv_next
;
580 res
->receiver
= NULL
;
581 if (!res
->sender
) /* no one waiting for it */
589 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
590 store_message_result( res
, result
, error
);
594 /* retrieve a posted message */
595 static int get_posted_message( struct msg_queue
*queue
, user_handle_t win
,
596 unsigned int first
, unsigned int last
, unsigned int flags
,
597 struct get_message_reply
*reply
)
600 struct message_list
*list
= &queue
->msg_list
[POST_MESSAGE
];
602 /* check against the filters */
603 for (msg
= list
->first
; msg
; msg
= msg
->next
)
605 if (msg
->msg
== WM_QUIT
) break; /* WM_QUIT is never filtered */
606 if (win
&& msg
->win
&& msg
->win
!= win
&& !is_child_window( win
, msg
->win
)) continue;
607 if (msg
->msg
< first
) continue;
608 if (msg
->msg
> last
) continue;
609 break; /* found one */
613 /* return it to the app */
615 reply
->total
= msg
->data_size
;
616 if (msg
->data_size
> get_reply_max_size())
618 set_error( STATUS_BUFFER_OVERFLOW
);
621 reply
->type
= msg
->type
;
622 reply
->win
= msg
->win
;
623 reply
->msg
= msg
->msg
;
624 reply
->wparam
= msg
->wparam
;
625 reply
->lparam
= msg
->lparam
;
628 reply
->time
= msg
->time
;
629 reply
->info
= msg
->info
;
631 if (flags
& GET_MSG_REMOVE
)
635 set_reply_data_ptr( msg
->data
, msg
->data_size
);
639 remove_queue_message( queue
, msg
, POST_MESSAGE
);
641 else if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
646 /* empty a message list and free all the messages */
647 static void empty_msg_list( struct message_list
*list
)
649 struct message
*msg
= list
->first
;
652 struct message
*next
= msg
->next
;
658 /* cleanup all pending results when deleting a queue */
659 static void cleanup_results( struct msg_queue
*queue
)
663 while ((entry
= list_head( &queue
->send_result
)) != NULL
)
665 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
668 while ((entry
= list_head( &queue
->callback_result
)) != NULL
)
670 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
673 while (queue
->recv_result
)
674 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
677 /* check if the thread owning the queue is hung (not checking for messages) */
678 static int is_queue_hung( struct msg_queue
*queue
)
681 struct wait_queue_entry
*entry
;
683 gettimeofday( &now
, NULL
);
684 if (now
.tv_sec
- queue
->last_get_msg
.tv_sec
<= 5)
685 return 0; /* less than 5 seconds since last get message -> not hung */
687 for (entry
= queue
->obj
.head
; entry
; entry
= entry
->next
)
689 if (entry
->thread
->queue
== queue
)
690 return 0; /* thread is waiting on queue -> not hung */
695 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
697 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
698 struct process
*process
= entry
->thread
->process
;
700 /* a thread can only wait on its own queue */
701 if (entry
->thread
->queue
!= queue
)
703 set_error( STATUS_ACCESS_DENIED
);
706 /* if waiting on the main process queue, set the idle event */
707 if (process
->queue
== queue
)
709 if (process
->idle_event
) set_event( process
->idle_event
);
711 add_queue( obj
, entry
);
715 static void msg_queue_remove_queue(struct object
*obj
, struct wait_queue_entry
*entry
)
717 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
718 struct process
*process
= entry
->thread
->process
;
720 remove_queue( obj
, entry
);
722 assert( entry
->thread
->queue
== queue
);
724 /* if waiting on the main process queue, reset the idle event */
725 if (process
->queue
== queue
)
727 if (process
->idle_event
) reset_event( process
->idle_event
);
731 static void msg_queue_dump( struct object
*obj
, int verbose
)
733 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
734 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
735 queue
->wake_bits
, queue
->wake_mask
);
738 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
)
740 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
741 return is_signaled( queue
);
744 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
)
746 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
747 queue
->wake_mask
= 0;
748 queue
->changed_mask
= 0;
749 return 0; /* Not abandoned */
752 static void msg_queue_destroy( struct object
*obj
)
754 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
758 cleanup_results( queue
);
759 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
761 while ((ptr
= list_head( &queue
->pending_timers
)))
763 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
764 list_remove( &timer
->entry
);
767 while ((ptr
= list_head( &queue
->expired_timers
)))
769 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
770 list_remove( &timer
->entry
);
773 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
774 if (queue
->input
) release_object( queue
->input
);
775 if (queue
->hooks
) release_object( queue
->hooks
);
778 static void thread_input_dump( struct object
*obj
, int verbose
)
780 struct thread_input
*input
= (struct thread_input
*)obj
;
781 fprintf( stderr
, "Thread input focus=%p capture=%p active=%p\n",
782 input
->focus
, input
->capture
, input
->active
);
785 static void thread_input_destroy( struct object
*obj
)
787 struct thread_input
*input
= (struct thread_input
*)obj
;
789 if (foreground_input
== input
) foreground_input
= NULL
;
790 if (input
->msg_thread
) release_object( input
->msg_thread
);
791 empty_msg_list( &input
->msg_list
);
794 /* fix the thread input data when a window is destroyed */
795 inline static void thread_input_cleanup_window( struct msg_queue
*queue
, user_handle_t window
)
797 struct thread_input
*input
= queue
->input
;
799 if (window
== input
->focus
) input
->focus
= 0;
800 if (window
== input
->capture
) input
->capture
= 0;
801 if (window
== input
->active
) input
->active
= 0;
802 if (window
== input
->menu_owner
) input
->menu_owner
= 0;
803 if (window
== input
->move_size
) input
->move_size
= 0;
804 if (window
== input
->caret
) set_caret_window( input
, 0 );
807 /* check if the specified window can be set in the input data of a given queue */
808 static int check_queue_input_window( struct msg_queue
*queue
, user_handle_t window
)
810 struct thread
*thread
;
813 if (!window
) return 1; /* we can always clear the data */
815 if ((thread
= get_window_thread( window
)))
817 ret
= (queue
->input
== thread
->queue
->input
);
818 if (!ret
) set_error( STATUS_ACCESS_DENIED
);
819 release_object( thread
);
821 else set_error( STATUS_INVALID_HANDLE
);
826 /* attach two thread input data structures */
827 int attach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
829 struct thread_input
*input
;
831 if (!thread_to
->queue
&& !(thread_to
->queue
= create_msg_queue( thread_to
, NULL
))) return 0;
832 input
= (struct thread_input
*)grab_object( thread_to
->queue
->input
);
834 if (thread_from
->queue
)
836 release_thread_input( thread_from
);
837 thread_from
->queue
->input
= input
;
841 if (!(thread_from
->queue
= create_msg_queue( thread_from
, input
))) return 0;
843 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
847 /* detach two thread input data structures */
848 static void detach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
850 struct thread_input
*input
;
852 if (!thread_from
->queue
|| !thread_to
->queue
||
853 thread_from
->queue
->input
!= thread_to
->queue
->input
)
855 set_error( STATUS_ACCESS_DENIED
);
858 if ((input
= create_thread_input()))
860 release_thread_input( thread_from
);
861 thread_from
->queue
->input
= input
;
866 /* set the next timer to expire */
867 static void set_next_timer( struct msg_queue
*queue
)
873 remove_timeout_user( queue
->timeout
);
874 queue
->timeout
= NULL
;
876 if ((ptr
= list_head( &queue
->pending_timers
)))
878 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
879 queue
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, queue
);
881 /* set/clear QS_TIMER bit */
882 if (list_empty( &queue
->expired_timers
))
883 clear_queue_bits( queue
, QS_TIMER
);
885 set_queue_bits( queue
, QS_TIMER
);
888 /* find a timer from its window and id */
889 static struct timer
*find_timer( struct msg_queue
*queue
, user_handle_t win
,
890 unsigned int msg
, unsigned int id
)
894 /* we need to search both lists */
896 LIST_FOR_EACH( ptr
, &queue
->pending_timers
)
898 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
899 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
901 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
903 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
904 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
909 /* callback for the next timer expiration */
910 static void timer_callback( void *private )
912 struct msg_queue
*queue
= private;
915 queue
->timeout
= NULL
;
916 /* move on to the next timer */
917 ptr
= list_head( &queue
->pending_timers
);
919 list_add_tail( &queue
->expired_timers
, ptr
);
920 set_next_timer( queue
);
923 /* link a timer at its rightful place in the queue list */
924 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
928 for (ptr
= queue
->pending_timers
.next
; ptr
!= &queue
->pending_timers
; ptr
= ptr
->next
)
930 struct timer
*t
= LIST_ENTRY( ptr
, struct timer
, entry
);
931 if (!time_before( &t
->when
, &timer
->when
)) break;
933 list_add_before( ptr
, &timer
->entry
);
936 /* remove a timer from the queue timer list and free it */
937 static void free_timer( struct msg_queue
*queue
, struct timer
*timer
)
939 list_remove( &timer
->entry
);
941 set_next_timer( queue
);
944 /* restart an expired timer */
945 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
949 list_remove( &timer
->entry
);
950 gettimeofday( &now
, 0 );
951 while (!time_before( &now
, &timer
->when
)) add_timeout( &timer
->when
, timer
->rate
);
952 link_timer( queue
, timer
);
953 set_next_timer( queue
);
956 /* find an expired timer matching the filtering parameters */
957 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
958 unsigned int get_first
, unsigned int get_last
,
963 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
965 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
966 if (win
&& timer
->win
!= win
) continue;
967 if (timer
->msg
>= get_first
&& timer
->msg
<= get_last
)
969 if (remove
) restart_timer( queue
, timer
);
977 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
979 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
982 timer
->rate
= max( rate
, 1 );
983 gettimeofday( &timer
->when
, 0 );
984 add_timeout( &timer
->when
, rate
);
985 link_timer( queue
, timer
);
986 /* check if we replaced the next timer */
987 if (list_head( &queue
->pending_timers
) == &timer
->entry
) set_next_timer( queue
);
992 /* change the input key state for a given key */
993 static void set_input_key_state( struct thread_input
*input
, unsigned char key
, int down
)
997 if (!(input
->keystate
[key
] & 0x80)) input
->keystate
[key
] ^= 0x01;
998 input
->keystate
[key
] |= 0x80;
1000 else input
->keystate
[key
] &= ~0x80;
1003 /* update the input key state for a keyboard message */
1004 static void update_input_key_state( struct thread_input
*input
, const struct message
*msg
)
1007 int down
= 0, extended
;
1011 case WM_LBUTTONDOWN
:
1015 set_input_key_state( input
, VK_LBUTTON
, down
);
1017 case WM_MBUTTONDOWN
:
1021 set_input_key_state( input
, VK_MBUTTON
, down
);
1023 case WM_RBUTTONDOWN
:
1027 set_input_key_state( input
, VK_RBUTTON
, down
);
1035 key
= (unsigned char)msg
->wparam
;
1036 extended
= ((msg
->lparam
>> 16) & KF_EXTENDED
) != 0;
1037 set_input_key_state( input
, key
, down
);
1041 set_input_key_state( input
, extended
? VK_RSHIFT
: VK_LSHIFT
, down
);
1044 set_input_key_state( input
, extended
? VK_RCONTROL
: VK_LCONTROL
, down
);
1047 set_input_key_state( input
, extended
? VK_RMENU
: VK_LMENU
, down
);
1054 /* release the hardware message currently being processed by the given thread */
1055 static void release_hardware_message( struct thread
*thread
, int remove
)
1057 struct thread_input
*input
= thread
->queue
->input
;
1059 if (input
->msg_thread
!= thread
) return;
1062 struct message
*other
;
1065 update_input_key_state( input
, input
->msg
);
1066 unlink_message( &input
->msg_list
, input
->msg
);
1067 clr_bit
= get_hardware_msg_bit( input
->msg
);
1068 for (other
= input
->msg_list
.first
; other
; other
= other
->next
)
1069 if (get_hardware_msg_bit( other
) == clr_bit
) break;
1070 if (!other
) clear_queue_bits( thread
->queue
, clr_bit
);
1071 free_message( input
->msg
);
1073 release_object( input
->msg_thread
);
1075 input
->msg_thread
= NULL
;
1078 /* find the window that should receive a given hardware message */
1079 static user_handle_t
find_hardware_message_window( struct thread_input
*input
, struct message
*msg
,
1080 unsigned int *msg_code
)
1082 user_handle_t win
= 0;
1084 *msg_code
= msg
->msg
;
1085 if (is_keyboard_msg( msg
))
1087 if (input
&& !(win
= input
->focus
))
1089 win
= input
->active
;
1090 if (*msg_code
< WM_SYSKEYDOWN
) *msg_code
+= WM_SYSKEYDOWN
- WM_KEYDOWN
;
1093 else /* mouse message */
1095 if (!input
|| !(win
= input
->capture
))
1097 if (!(win
= msg
->win
) || !is_window_visible( win
))
1098 win
= window_from_point( msg
->x
, msg
->y
);
1104 /* queue a hardware message into a given thread input */
1105 static void queue_hardware_message( struct msg_queue
*queue
, struct message
*msg
)
1108 struct thread
*thread
;
1109 struct thread_input
*input
;
1110 unsigned int msg_code
;
1112 win
= find_hardware_message_window( queue
? queue
->input
: foreground_input
, msg
, &msg_code
);
1113 if (!win
|| !(thread
= get_window_thread(win
)))
1118 input
= thread
->queue
->input
;
1120 if (msg
->msg
== WM_MOUSEMOVE
&& merge_message( input
, msg
)) free( msg
);
1123 append_message( &input
->msg_list
, msg
);
1124 set_queue_bits( thread
->queue
, get_hardware_msg_bit(msg
) );
1126 release_object( thread
);
1129 /* find a hardware message for the given queue */
1130 static int get_hardware_message( struct thread
*thread
, struct message
*first
,
1131 user_handle_t filter_win
, struct get_message_reply
*reply
)
1133 struct thread_input
*input
= thread
->queue
->input
;
1134 struct thread
*win_thread
;
1135 struct message
*msg
;
1137 int clear_bits
, got_one
= 0;
1138 unsigned int msg_code
;
1140 if (input
->msg_thread
&& input
->msg_thread
!= thread
)
1141 return 0; /* locked by another thread */
1145 msg
= input
->msg_list
.first
;
1146 clear_bits
= QS_KEY
| QS_MOUSEMOVE
| QS_MOUSEBUTTON
;
1151 clear_bits
= 0; /* don't clear bits if we don't go through the whole list */
1156 win
= find_hardware_message_window( input
, msg
, &msg_code
);
1157 if (!win
|| !(win_thread
= get_window_thread( win
)))
1159 /* no window at all, remove it */
1160 struct message
*next
= msg
->next
;
1161 update_input_key_state( input
, msg
);
1162 unlink_message( &input
->msg_list
, msg
);
1163 free_message( msg
);
1167 if (win_thread
!= thread
)
1169 /* wake the other thread */
1170 set_queue_bits( win_thread
->queue
, get_hardware_msg_bit(msg
) );
1171 release_object( win_thread
);
1176 /* if we already got a message for another thread, or if it doesn't
1177 * match the filter we skip it (filter is only checked for keyboard
1178 * messages since the dest window for a mouse message depends on hittest)
1181 (filter_win
&& is_keyboard_msg(msg
) &&
1182 win
!= filter_win
&& !is_child_window( filter_win
, win
)))
1184 clear_bits
&= ~get_hardware_msg_bit( msg
);
1185 release_object( win_thread
);
1189 /* now we can return it */
1190 if (!input
->msg_thread
) input
->msg_thread
= win_thread
;
1191 else release_object( win_thread
);
1194 reply
->type
= MSG_HARDWARE
;
1196 reply
->msg
= msg_code
;
1197 reply
->wparam
= msg
->wparam
;
1198 reply
->lparam
= msg
->lparam
;
1201 reply
->time
= msg
->time
;
1202 reply
->info
= msg
->info
;
1205 /* nothing found, clear the hardware queue bits */
1206 clear_queue_bits( thread
->queue
, clear_bits
);
1207 if (input
->msg_thread
) release_object( input
->msg_thread
);
1209 input
->msg_thread
= NULL
;
1213 /* increment (or decrement if 'incr' is negative) the queue paint count */
1214 void inc_queue_paint_count( struct thread
*thread
, int incr
)
1216 struct msg_queue
*queue
= thread
->queue
;
1220 if ((queue
->paint_count
+= incr
) < 0) queue
->paint_count
= 0;
1222 if (queue
->paint_count
)
1223 set_queue_bits( queue
, QS_PAINT
);
1225 clear_queue_bits( queue
, QS_PAINT
);
1229 /* remove all messages and timers belonging to a certain window */
1230 void queue_cleanup_window( struct thread
*thread
, user_handle_t win
)
1232 struct msg_queue
*queue
= thread
->queue
;
1234 struct message
*msg
;
1241 ptr
= list_head( &queue
->pending_timers
);
1244 struct list
*next
= list_next( &queue
->pending_timers
, ptr
);
1245 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1246 if (timer
->win
== win
) free_timer( queue
, timer
);
1249 ptr
= list_head( &queue
->expired_timers
);
1252 struct list
*next
= list_next( &queue
->expired_timers
, ptr
);
1253 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1254 if (timer
->win
== win
) free_timer( queue
, timer
);
1258 /* remove messages */
1259 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
1261 msg
= queue
->msg_list
[i
].first
;
1264 struct message
*next
= msg
->next
;
1265 if (msg
->win
== win
) remove_queue_message( queue
, msg
, i
);
1270 thread_input_cleanup_window( queue
, win
);
1273 /* post a message to a window; used by socket handling */
1274 void post_message( user_handle_t win
, unsigned int message
,
1275 unsigned int wparam
, unsigned int lparam
)
1277 struct message
*msg
;
1278 struct thread
*thread
= get_window_thread( win
);
1280 if (!thread
) return;
1282 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1284 msg
->type
= MSG_POSTED
;
1285 msg
->win
= get_user_full_handle( win
);
1287 msg
->wparam
= wparam
;
1288 msg
->lparam
= lparam
;
1289 msg
->time
= get_tick_count();
1297 append_message( &thread
->queue
->msg_list
[POST_MESSAGE
], msg
);
1298 set_queue_bits( thread
->queue
, QS_POSTMESSAGE
);
1300 release_object( thread
);
1303 /* post a win event */
1304 void post_win_event( struct thread
*thread
, unsigned int event
,
1305 user_handle_t win
, unsigned int object_id
,
1306 unsigned int child_id
, void *hook_proc
,
1307 const WCHAR
*module
, size_t module_size
,
1310 struct message
*msg
;
1312 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1314 msg
->type
= MSG_WINEVENT
;
1315 msg
->win
= get_user_full_handle( win
);
1317 msg
->wparam
= object_id
;
1318 msg
->lparam
= child_id
;
1319 msg
->time
= get_tick_count();
1322 msg
->info
= get_thread_id( current
);
1325 msg
->hook_proc
= hook_proc
;
1327 if ((msg
->data
= malloc( module_size
)))
1329 msg
->data_size
= module_size
;
1330 memcpy( msg
->data
, module
, module_size
);
1332 if (debug_level
> 1)
1333 fprintf( stderr
, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1334 get_thread_id(thread
), event
, win
, object_id
, child_id
);
1335 append_message( &thread
->queue
->msg_list
[SEND_MESSAGE
], msg
);
1336 set_queue_bits( thread
->queue
, QS_SENDMESSAGE
);
1343 /* get the message queue of the current thread */
1344 DECL_HANDLER(get_msg_queue
)
1346 struct msg_queue
*queue
= get_current_queue();
1349 if (queue
) reply
->handle
= alloc_handle( current
->process
, queue
, SYNCHRONIZE
, 0 );
1353 /* set the current message queue wakeup mask */
1354 DECL_HANDLER(set_queue_mask
)
1356 struct msg_queue
*queue
= get_current_queue();
1360 queue
->wake_mask
= req
->wake_mask
;
1361 queue
->changed_mask
= req
->changed_mask
;
1362 reply
->wake_bits
= queue
->wake_bits
;
1363 reply
->changed_bits
= queue
->changed_bits
;
1364 if (is_signaled( queue
))
1366 /* if skip wait is set, do what would have been done in the subsequent wait */
1367 if (req
->skip_wait
) msg_queue_satisfied( &queue
->obj
, current
);
1368 else wake_up( &queue
->obj
, 0 );
1374 /* get the current message queue status */
1375 DECL_HANDLER(get_queue_status
)
1377 struct msg_queue
*queue
= current
->queue
;
1380 reply
->wake_bits
= queue
->wake_bits
;
1381 reply
->changed_bits
= queue
->changed_bits
;
1382 if (req
->clear
) queue
->changed_bits
= 0;
1384 else reply
->wake_bits
= reply
->changed_bits
= 0;
1388 /* send a message to a thread queue */
1389 DECL_HANDLER(send_message
)
1391 struct message
*msg
;
1392 struct msg_queue
*send_queue
= get_current_queue();
1393 struct msg_queue
*recv_queue
= NULL
;
1394 struct thread
*thread
= NULL
;
1398 if (!(thread
= get_thread_from_id( req
->id
))) return;
1400 else if (req
->type
!= MSG_HARDWARE
)
1402 /* only hardware messages are allowed without destination thread */
1403 set_error( STATUS_INVALID_PARAMETER
);
1407 if (thread
&& !(recv_queue
= thread
->queue
))
1409 set_error( STATUS_INVALID_PARAMETER
);
1410 release_object( thread
);
1413 if (recv_queue
&& (req
->flags
& SEND_MSG_ABORT_IF_HUNG
) && is_queue_hung(recv_queue
))
1415 set_error( STATUS_TIMEOUT
);
1416 release_object( thread
);
1420 if ((msg
= mem_alloc( sizeof(*msg
) )))
1422 msg
->type
= req
->type
;
1423 msg
->win
= get_user_full_handle( req
->win
);
1424 msg
->msg
= req
->msg
;
1425 msg
->wparam
= req
->wparam
;
1426 msg
->lparam
= req
->lparam
;
1427 msg
->time
= req
->time
;
1430 msg
->info
= req
->info
;
1437 case MSG_OTHER_PROCESS
:
1438 msg
->data_size
= get_req_data_size();
1439 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1448 if (!(msg
->result
= alloc_message_result( send_queue
, recv_queue
, msg
,
1449 req
->timeout
, req
->callback
, req
->info
)))
1451 free_message( msg
);
1456 append_message( &recv_queue
->msg_list
[SEND_MESSAGE
], msg
);
1457 set_queue_bits( recv_queue
, QS_SENDMESSAGE
);
1460 /* needed for posted DDE messages */
1461 msg
->data_size
= get_req_data_size();
1462 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1467 append_message( &recv_queue
->msg_list
[POST_MESSAGE
], msg
);
1468 set_queue_bits( recv_queue
, QS_POSTMESSAGE
);
1471 queue_hardware_message( recv_queue
, msg
);
1473 case MSG_CALLBACK_RESULT
: /* cannot send this one */
1475 set_error( STATUS_INVALID_PARAMETER
);
1480 if (thread
) release_object( thread
);
1484 /* get a message from the current queue */
1485 DECL_HANDLER(get_message
)
1487 struct timer
*timer
;
1488 struct message
*msg
;
1489 struct message
*first_hw_msg
= NULL
;
1490 struct msg_queue
*queue
= get_current_queue();
1491 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
1494 gettimeofday( &queue
->last_get_msg
, NULL
);
1496 /* first of all release the hardware input lock if we own it */
1497 /* we'll grab it again if we find a hardware message */
1498 if (queue
->input
->msg_thread
== current
)
1500 first_hw_msg
= queue
->input
->msg
;
1501 release_hardware_message( current
, 0 );
1504 /* first check for sent messages */
1505 if ((msg
= queue
->msg_list
[SEND_MESSAGE
].first
))
1507 receive_message( queue
, msg
, reply
);
1510 if (req
->flags
& GET_MSG_SENT_ONLY
) goto done
; /* nothing else to check */
1512 /* clear changed bits so we can wait on them if we don't find a message */
1513 queue
->changed_bits
= 0;
1515 /* then check for posted messages */
1516 if (get_posted_message( queue
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
1519 /* then check for any raw hardware message */
1520 if (get_hardware_message( current
, first_hw_msg
, get_win
, reply
))
1523 /* now check for WM_PAINT */
1524 if (queue
->paint_count
&&
1525 (WM_PAINT
>= req
->get_first
) && (WM_PAINT
<= req
->get_last
) &&
1526 (reply
->win
= find_window_to_repaint( get_win
, current
)))
1528 reply
->type
= MSG_POSTED
;
1529 reply
->msg
= WM_PAINT
;
1534 reply
->time
= get_tick_count();
1539 /* now check for timer */
1540 if ((timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
1541 req
->get_last
, (req
->flags
& GET_MSG_REMOVE
) )))
1543 reply
->type
= MSG_POSTED
;
1544 reply
->win
= timer
->win
;
1545 reply
->msg
= timer
->msg
;
1546 reply
->wparam
= timer
->id
;
1547 reply
->lparam
= timer
->lparam
;
1550 reply
->time
= get_tick_count();
1556 set_error( STATUS_PENDING
); /* FIXME */
1560 /* reply to a sent message */
1561 DECL_HANDLER(reply_message
)
1563 if (!current
->queue
)
1565 set_error( STATUS_ACCESS_DENIED
);
1568 if (req
->type
== MSG_HARDWARE
)
1570 struct thread_input
*input
= current
->queue
->input
;
1571 if (input
->msg_thread
== current
) release_hardware_message( current
, req
->remove
);
1572 else set_error( STATUS_ACCESS_DENIED
);
1574 else if (current
->queue
->recv_result
)
1575 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
1576 get_req_data(), get_req_data_size() );
1580 /* retrieve the reply for the last message sent */
1581 DECL_HANDLER(get_message_reply
)
1583 struct message_result
*result
;
1585 struct msg_queue
*queue
= current
->queue
;
1589 set_error( STATUS_PENDING
);
1592 if (!(entry
= list_head( &queue
->send_result
))) return; /* no reply ready */
1594 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1595 if (result
->replied
|| req
->cancel
)
1597 if (result
->replied
)
1599 reply
->result
= result
->result
;
1600 set_error( result
->error
);
1603 size_t data_len
= min( result
->data_size
, get_reply_max_size() );
1604 set_reply_data_ptr( result
->data
, data_len
);
1605 result
->data
= NULL
;
1606 result
->data_size
= 0;
1609 remove_result_from_sender( result
);
1611 entry
= list_head( &queue
->send_result
);
1612 if (!entry
) clear_queue_bits( queue
, QS_SMRESULT
);
1615 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1616 if (!result
->replied
) clear_queue_bits( queue
, QS_SMRESULT
);
1620 else set_error( STATUS_ACCESS_DENIED
);
1624 /* set a window timer */
1625 DECL_HANDLER(set_win_timer
)
1627 struct timer
*timer
;
1628 struct msg_queue
*queue
;
1629 struct thread
*thread
= NULL
;
1630 user_handle_t win
= 0;
1631 unsigned int id
= req
->id
;
1635 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1637 set_error( STATUS_INVALID_HANDLE
);
1640 if (thread
->process
!= current
->process
)
1642 release_object( thread
);
1643 set_error( STATUS_ACCESS_DENIED
);
1646 queue
= thread
->queue
;
1647 /* remove it if it existed already */
1648 if ((timer
= find_timer( queue
, win
, req
->msg
, id
))) free_timer( queue
, timer
);
1652 queue
= get_current_queue();
1653 /* find a free id for it */
1656 id
= queue
->next_timer_id
;
1657 if (++queue
->next_timer_id
>= 0x10000) queue
->next_timer_id
= 1;
1659 while (find_timer( queue
, 0, req
->msg
, id
));
1662 if ((timer
= set_timer( queue
, req
->rate
)))
1665 timer
->msg
= req
->msg
;
1667 timer
->lparam
= req
->lparam
;
1670 if (thread
) release_object( thread
);
1673 /* kill a window timer */
1674 DECL_HANDLER(kill_win_timer
)
1676 struct timer
*timer
;
1677 struct thread
*thread
;
1678 user_handle_t win
= 0;
1682 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1684 set_error( STATUS_INVALID_HANDLE
);
1687 if (thread
->process
!= current
->process
)
1689 release_object( thread
);
1690 set_error( STATUS_ACCESS_DENIED
);
1694 else thread
= (struct thread
*)grab_object( current
);
1696 if (thread
->queue
&& (timer
= find_timer( thread
->queue
, win
, req
->msg
, req
->id
)))
1697 free_timer( thread
->queue
, timer
);
1699 set_error( STATUS_INVALID_PARAMETER
);
1701 release_object( thread
);
1705 /* attach (or detach) thread inputs */
1706 DECL_HANDLER(attach_thread_input
)
1708 struct thread
*thread_from
= get_thread_from_id( req
->tid_from
);
1709 struct thread
*thread_to
= get_thread_from_id( req
->tid_to
);
1711 if (!thread_from
|| !thread_to
)
1713 if (thread_from
) release_object( thread_from
);
1714 if (thread_to
) release_object( thread_to
);
1717 if (thread_from
!= thread_to
)
1719 if (req
->attach
) attach_thread_input( thread_from
, thread_to
);
1720 else detach_thread_input( thread_from
, thread_to
);
1722 else set_error( STATUS_ACCESS_DENIED
);
1723 release_object( thread_from
);
1724 release_object( thread_to
);
1728 /* get thread input data */
1729 DECL_HANDLER(get_thread_input
)
1731 struct thread
*thread
= NULL
;
1732 struct thread_input
*input
;
1736 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1737 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1739 else input
= foreground_input
; /* get the foreground thread info */
1743 reply
->focus
= input
->focus
;
1744 reply
->capture
= input
->capture
;
1745 reply
->active
= input
->active
;
1746 reply
->menu_owner
= input
->menu_owner
;
1747 reply
->move_size
= input
->move_size
;
1748 reply
->caret
= input
->caret
;
1749 reply
->rect
= input
->caret_rect
;
1756 reply
->menu_owner
= 0;
1757 reply
->move_size
= 0;
1759 reply
->rect
.left
= reply
->rect
.top
= reply
->rect
.right
= reply
->rect
.bottom
= 0;
1761 /* foreground window is active window of foreground thread */
1762 reply
->foreground
= foreground_input
? foreground_input
->active
: 0;
1763 if (thread
) release_object( thread
);
1767 /* retrieve queue keyboard state for a given thread */
1768 DECL_HANDLER(get_key_state
)
1770 struct thread
*thread
;
1771 struct thread_input
*input
;
1773 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1774 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1777 if (req
->key
>= 0) reply
->state
= input
->keystate
[req
->key
& 0xff];
1778 set_reply_data( input
->keystate
, min( get_reply_max_size(), sizeof(input
->keystate
) ));
1780 release_object( thread
);
1784 /* set queue keyboard state for a given thread */
1785 DECL_HANDLER(set_key_state
)
1787 struct thread
*thread
= NULL
;
1788 struct thread_input
*input
;
1790 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1791 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1794 size_t size
= min( sizeof(input
->keystate
), get_req_data_size() );
1795 if (size
) memcpy( input
->keystate
, get_req_data(), size
);
1797 release_object( thread
);
1801 /* set the system foreground window */
1802 DECL_HANDLER(set_foreground_window
)
1804 struct msg_queue
*queue
= get_current_queue();
1806 reply
->previous
= foreground_input
? foreground_input
->active
: 0;
1807 reply
->send_msg_old
= (reply
->previous
&& foreground_input
!= queue
->input
);
1808 reply
->send_msg_new
= FALSE
;
1812 struct thread
*thread
;
1814 if (is_top_level_window( req
->handle
) &&
1815 ((thread
= get_window_thread( req
->handle
))))
1817 foreground_input
= thread
->queue
->input
;
1818 reply
->send_msg_new
= (foreground_input
!= queue
->input
);
1819 release_object( thread
);
1821 else set_error( STATUS_INVALID_HANDLE
);
1823 else foreground_input
= NULL
;
1827 /* set the current thread focus window */
1828 DECL_HANDLER(set_focus_window
)
1830 struct msg_queue
*queue
= get_current_queue();
1832 reply
->previous
= 0;
1833 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1835 reply
->previous
= queue
->input
->focus
;
1836 queue
->input
->focus
= get_user_full_handle( req
->handle
);
1841 /* set the current thread active window */
1842 DECL_HANDLER(set_active_window
)
1844 struct msg_queue
*queue
= get_current_queue();
1846 reply
->previous
= 0;
1847 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1849 if (!req
->handle
|| make_window_active( req
->handle
))
1851 reply
->previous
= queue
->input
->active
;
1852 queue
->input
->active
= get_user_full_handle( req
->handle
);
1854 else set_error( STATUS_INVALID_HANDLE
);
1859 /* set the current thread capture window */
1860 DECL_HANDLER(set_capture_window
)
1862 struct msg_queue
*queue
= get_current_queue();
1864 reply
->previous
= reply
->full_handle
= 0;
1865 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1867 struct thread_input
*input
= queue
->input
;
1869 reply
->previous
= input
->capture
;
1870 input
->capture
= get_user_full_handle( req
->handle
);
1871 input
->menu_owner
= (req
->flags
& CAPTURE_MENU
) ? input
->capture
: 0;
1872 input
->move_size
= (req
->flags
& CAPTURE_MOVESIZE
) ? input
->capture
: 0;
1873 reply
->full_handle
= input
->capture
;
1878 /* Set the current thread caret window */
1879 DECL_HANDLER(set_caret_window
)
1881 struct msg_queue
*queue
= get_current_queue();
1883 reply
->previous
= 0;
1884 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1886 struct thread_input
*input
= queue
->input
;
1888 reply
->previous
= input
->caret
;
1889 reply
->old_rect
= input
->caret_rect
;
1890 reply
->old_hide
= input
->caret_hide
;
1891 reply
->old_state
= input
->caret_state
;
1893 set_caret_window( input
, get_user_full_handle(req
->handle
) );
1894 input
->caret_rect
.right
= req
->width
;
1895 input
->caret_rect
.bottom
= req
->height
;
1900 /* Set the current thread caret information */
1901 DECL_HANDLER(set_caret_info
)
1903 struct msg_queue
*queue
= get_current_queue();
1904 struct thread_input
*input
;
1907 input
= queue
->input
;
1908 reply
->full_handle
= input
->caret
;
1909 reply
->old_rect
= input
->caret_rect
;
1910 reply
->old_hide
= input
->caret_hide
;
1911 reply
->old_state
= input
->caret_state
;
1913 if (req
->handle
&& get_user_full_handle(req
->handle
) != input
->caret
)
1915 set_error( STATUS_ACCESS_DENIED
);
1918 if (req
->flags
& SET_CARET_POS
)
1920 input
->caret_rect
.right
+= req
->x
- input
->caret_rect
.left
;
1921 input
->caret_rect
.bottom
+= req
->y
- input
->caret_rect
.top
;
1922 input
->caret_rect
.left
= req
->x
;
1923 input
->caret_rect
.top
= req
->y
;
1925 if (req
->flags
& SET_CARET_HIDE
)
1927 input
->caret_hide
+= req
->hide
;
1928 if (input
->caret_hide
< 0) input
->caret_hide
= 0;
1930 if (req
->flags
& SET_CARET_STATE
)
1932 if (req
->state
== -1) input
->caret_state
= !input
->caret_state
;
1933 else input
->caret_state
= !!req
->state
;