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 void *data
; /* message data for sent messages */
74 unsigned int data_size
; /* size of message data */
75 struct message_result
*result
; /* result in sender queue */
80 struct message
*first
; /* head of list */
81 struct message
*last
; /* tail of list */
86 struct list entry
; /* entry in timer list */
87 struct timeval when
; /* next expiration */
88 unsigned int rate
; /* timer rate in ms */
89 user_handle_t win
; /* window handle */
90 unsigned int msg
; /* message to post */
91 unsigned int id
; /* timer id */
92 unsigned int lparam
; /* lparam for message */
97 struct object obj
; /* object header */
98 user_handle_t focus
; /* focus window */
99 user_handle_t capture
; /* capture window */
100 user_handle_t active
; /* active window */
101 user_handle_t menu_owner
; /* current menu owner window */
102 user_handle_t move_size
; /* current moving/resizing window */
103 user_handle_t caret
; /* caret window */
104 rectangle_t caret_rect
; /* caret rectangle */
105 int caret_hide
; /* caret hide count */
106 int caret_state
; /* caret on/off state */
107 struct message
*msg
; /* message currently processed */
108 struct thread
*msg_thread
; /* thread processing the message */
109 struct message_list msg_list
; /* list of hardware messages */
110 unsigned char keystate
[256]; /* state of each key */
115 struct object obj
; /* object header */
116 unsigned int wake_bits
; /* wakeup bits */
117 unsigned int wake_mask
; /* wakeup mask */
118 unsigned int changed_bits
; /* changed wakeup bits */
119 unsigned int changed_mask
; /* changed wakeup mask */
120 int paint_count
; /* pending paint messages count */
121 struct message_list msg_list
[NB_MSG_KINDS
]; /* lists of messages */
122 struct list send_result
; /* stack of sent messages waiting for result */
123 struct list callback_result
; /* list of callback messages waiting for result */
124 struct message_result
*recv_result
; /* stack of received messages waiting for result */
125 struct list pending_timers
; /* list of pending timers */
126 struct list expired_timers
; /* list of expired timers */
127 unsigned int next_timer_id
; /* id for the next timer with a 0 window */
128 struct timeout_user
*timeout
; /* timeout for next timer to expire */
129 struct thread_input
*input
; /* thread input descriptor */
130 struct hook_table
*hooks
; /* hook table */
131 struct timeval last_get_msg
; /* time of last get message call */
134 static void msg_queue_dump( struct object
*obj
, int verbose
);
135 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
136 static void msg_queue_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
137 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
);
138 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
);
139 static void msg_queue_destroy( struct object
*obj
);
140 static void thread_input_dump( struct object
*obj
, int verbose
);
141 static void thread_input_destroy( struct object
*obj
);
142 static void timer_callback( void *private );
144 static const struct object_ops msg_queue_ops
=
146 sizeof(struct msg_queue
), /* size */
147 msg_queue_dump
, /* dump */
148 msg_queue_add_queue
, /* add_queue */
149 msg_queue_remove_queue
, /* remove_queue */
150 msg_queue_signaled
, /* signaled */
151 msg_queue_satisfied
, /* satisfied */
152 no_get_fd
, /* get_fd */
153 msg_queue_destroy
/* destroy */
157 static const struct object_ops thread_input_ops
=
159 sizeof(struct thread_input
), /* size */
160 thread_input_dump
, /* dump */
161 no_add_queue
, /* add_queue */
162 NULL
, /* remove_queue */
164 NULL
, /* satisfied */
165 no_get_fd
, /* get_fd */
166 thread_input_destroy
/* destroy */
169 /* pointer to input structure of foreground thread */
170 static struct thread_input
*foreground_input
;
173 /* set the caret window in a given thread input */
174 static void set_caret_window( struct thread_input
*input
, user_handle_t win
)
177 input
->caret_rect
.left
= 0;
178 input
->caret_rect
.top
= 0;
179 input
->caret_rect
.right
= 0;
180 input
->caret_rect
.bottom
= 0;
181 input
->caret_hide
= 1;
182 input
->caret_state
= 0;
185 /* create a thread input object */
186 static struct thread_input
*create_thread_input(void)
188 struct thread_input
*input
;
190 if ((input
= alloc_object( &thread_input_ops
)))
195 input
->menu_owner
= 0;
196 input
->move_size
= 0;
198 input
->msg_thread
= NULL
;
199 input
->msg_list
.first
= input
->msg_list
.last
= NULL
;
200 set_caret_window( input
, 0 );
201 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
206 /* release the thread input data of a given thread */
207 static void release_thread_input( struct thread
*thread
)
209 struct thread_input
*input
= thread
->queue
->input
;
212 if (input
->msg_thread
== thread
)
214 release_object( input
->msg_thread
);
215 input
->msg_thread
= NULL
;
218 release_object( input
);
219 thread
->queue
->input
= NULL
;
222 /* create a message queue object */
223 static struct msg_queue
*create_msg_queue( struct thread
*thread
, struct thread_input
*input
)
225 struct msg_queue
*queue
;
228 if (!input
&& !(input
= create_thread_input())) return NULL
;
229 if ((queue
= alloc_object( &msg_queue_ops
)))
231 queue
->wake_bits
= 0;
232 queue
->wake_mask
= 0;
233 queue
->changed_bits
= 0;
234 queue
->changed_mask
= 0;
235 queue
->paint_count
= 0;
236 queue
->recv_result
= NULL
;
237 queue
->next_timer_id
= 1;
238 queue
->timeout
= NULL
;
239 queue
->input
= (struct thread_input
*)grab_object( input
);
241 gettimeofday( &queue
->last_get_msg
, NULL
);
242 list_init( &queue
->send_result
);
243 list_init( &queue
->callback_result
);
244 list_init( &queue
->pending_timers
);
245 list_init( &queue
->expired_timers
);
246 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
247 queue
->msg_list
[i
].first
= queue
->msg_list
[i
].last
= NULL
;
249 thread
->queue
= queue
;
250 if (!thread
->process
->queue
)
251 thread
->process
->queue
= (struct msg_queue
*)grab_object( queue
);
253 release_object( input
);
257 /* free the message queue of a thread at thread exit */
258 void free_msg_queue( struct thread
*thread
)
260 struct process
*process
= thread
->process
;
262 remove_thread_hooks( thread
);
263 if (!thread
->queue
) return;
264 if (process
->queue
== thread
->queue
) /* is it the process main queue? */
266 release_object( process
->queue
);
267 process
->queue
= NULL
;
268 if (process
->idle_event
)
270 set_event( process
->idle_event
);
271 release_object( process
->idle_event
);
272 process
->idle_event
= NULL
;
275 release_thread_input( thread
);
276 release_object( thread
->queue
);
277 thread
->queue
= NULL
;
280 /* get the hook table for a given thread */
281 struct hook_table
*get_queue_hooks( struct thread
*thread
)
283 if (!thread
->queue
) return NULL
;
284 return thread
->queue
->hooks
;
287 /* set the hook table for a given thread, allocating the queue if needed */
288 void set_queue_hooks( struct thread
*thread
, struct hook_table
*hooks
)
290 struct msg_queue
*queue
= thread
->queue
;
291 if (!queue
) queue
= create_msg_queue( thread
, NULL
);
292 if (queue
->hooks
) release_object( queue
->hooks
);
293 queue
->hooks
= hooks
;
296 /* check the queue status */
297 inline static int is_signaled( struct msg_queue
*queue
)
299 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
302 /* set some queue bits */
303 inline static void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
305 queue
->wake_bits
|= bits
;
306 queue
->changed_bits
|= bits
;
307 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
310 /* clear some queue bits */
311 inline static void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
313 queue
->wake_bits
&= ~bits
;
314 queue
->changed_bits
&= ~bits
;
317 /* check whether msg is a keyboard message */
318 inline static int is_keyboard_msg( struct message
*msg
)
320 return (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
);
323 /* get the QS_* bit corresponding to a given hardware message */
324 inline static int get_hardware_msg_bit( struct message
*msg
)
326 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
327 if (is_keyboard_msg( msg
)) return QS_KEY
;
328 return QS_MOUSEBUTTON
;
331 /* get the current thread queue, creating it if needed */
332 inline static struct msg_queue
*get_current_queue(void)
334 struct msg_queue
*queue
= current
->queue
;
335 if (!queue
) queue
= create_msg_queue( current
, NULL
);
339 /* append a message to the end of a list */
340 inline static void append_message( struct message_list
*list
, struct message
*msg
)
343 if ((msg
->prev
= list
->last
)) msg
->prev
->next
= msg
;
344 else list
->first
= msg
;
348 /* unlink a message from a list it */
349 inline static void unlink_message( struct message_list
*list
, struct message
*msg
)
351 if (msg
->next
) msg
->next
->prev
= msg
->prev
;
352 else list
->last
= msg
->prev
;
353 if (msg
->prev
) msg
->prev
->next
= msg
->next
;
354 else list
->first
= msg
->next
;
357 /* try to merge a message with the last in the list; return 1 if successful */
358 static int merge_message( struct thread_input
*input
, const struct message
*msg
)
360 struct message
*prev
= input
->msg_list
.last
;
363 if (input
->msg
== prev
) return 0;
364 if (prev
->result
) return 0;
365 if (prev
->win
!= msg
->win
) return 0;
366 if (prev
->msg
!= msg
->msg
) return 0;
367 if (prev
->type
!= msg
->type
) return 0;
368 /* now we can merge it */
369 prev
->wparam
= msg
->wparam
;
370 prev
->lparam
= msg
->lparam
;
373 prev
->time
= msg
->time
;
374 prev
->info
= msg
->info
;
378 /* free a result structure */
379 static void free_result( struct message_result
*result
)
381 if (result
->timeout
) remove_timeout_user( result
->timeout
);
382 if (result
->data
) free( result
->data
);
383 if (result
->callback_msg
) free( result
->callback_msg
);
387 /* remove the result from the sender list it is on */
388 static inline void remove_result_from_sender( struct message_result
*result
)
390 assert( result
->sender
);
392 list_remove( &result
->sender_entry
);
393 result
->sender
= NULL
;
394 if (!result
->receiver
) free_result( result
);
397 /* store the message result in the appropriate structure */
398 static void store_message_result( struct message_result
*res
, unsigned int result
,
401 res
->result
= result
;
406 remove_timeout_user( res
->timeout
);
411 if (res
->callback_msg
)
413 /* queue the callback message in the sender queue */
414 res
->callback_msg
->lparam
= result
;
415 append_message( &res
->sender
->msg_list
[SEND_MESSAGE
], res
->callback_msg
);
416 set_queue_bits( res
->sender
, QS_SENDMESSAGE
);
417 res
->callback_msg
= NULL
;
418 remove_result_from_sender( res
);
422 /* wake sender queue if waiting on this result */
423 if (list_head(&res
->sender
->send_result
) == &res
->sender_entry
)
424 set_queue_bits( res
->sender
, QS_SMRESULT
);
430 /* free a message when deleting a queue or window */
431 static void free_message( struct message
*msg
)
433 struct message_result
*result
= msg
->result
;
438 result
->receiver
= NULL
;
439 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
441 else free_result( result
);
443 if (msg
->data
) free( msg
->data
);
447 /* remove (and free) a message from a message list */
448 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
449 enum message_kind kind
)
451 unlink_message( &queue
->msg_list
[kind
], msg
);
455 if (!queue
->msg_list
[kind
].first
) clear_queue_bits( queue
, QS_SENDMESSAGE
);
458 if (!queue
->msg_list
[kind
].first
) clear_queue_bits( queue
, QS_POSTMESSAGE
);
464 /* message timed out without getting a reply */
465 static void result_timeout( void *private )
467 struct message_result
*result
= private;
469 assert( !result
->replied
);
471 result
->timeout
= NULL
;
472 store_message_result( result
, 0, STATUS_TIMEOUT
);
475 /* allocate and fill a message result structure */
476 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
477 struct msg_queue
*recv_queue
,
478 struct message
*msg
, unsigned int timeout
,
479 void *callback
, unsigned int callback_data
)
481 struct message_result
*result
= mem_alloc( sizeof(*result
) );
484 result
->sender
= send_queue
;
485 result
->receiver
= recv_queue
;
488 result
->data_size
= 0;
489 result
->timeout
= NULL
;
491 if (msg
->type
== MSG_CALLBACK
)
493 struct message
*callback_msg
= mem_alloc( sizeof(*callback_msg
) );
499 callback_msg
->type
= MSG_CALLBACK_RESULT
;
500 callback_msg
->win
= msg
->win
;
501 callback_msg
->msg
= msg
->msg
;
502 callback_msg
->wparam
= (unsigned int)callback
;
503 callback_msg
->lparam
= 0;
504 callback_msg
->time
= get_tick_count();
507 callback_msg
->info
= callback_data
;
508 callback_msg
->result
= NULL
;
509 callback_msg
->data
= NULL
;
510 callback_msg
->data_size
= 0;
512 result
->callback_msg
= callback_msg
;
513 list_add_head( &send_queue
->callback_result
, &result
->sender_entry
);
517 result
->callback_msg
= NULL
;
518 list_add_head( &send_queue
->send_result
, &result
->sender_entry
);
524 gettimeofday( &when
, 0 );
525 add_timeout( &when
, timeout
);
526 result
->timeout
= add_timeout_user( &when
, result_timeout
, result
);
532 /* receive a message, removing it from the sent queue */
533 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
534 struct get_message_reply
*reply
)
536 struct message_result
*result
= msg
->result
;
538 reply
->total
= msg
->data_size
;
539 if (msg
->data_size
> get_reply_max_size())
541 set_error( STATUS_BUFFER_OVERFLOW
);
544 reply
->type
= msg
->type
;
545 reply
->win
= msg
->win
;
546 reply
->msg
= msg
->msg
;
547 reply
->wparam
= msg
->wparam
;
548 reply
->lparam
= msg
->lparam
;
551 reply
->time
= msg
->time
;
552 reply
->info
= msg
->info
;
554 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
556 unlink_message( &queue
->msg_list
[SEND_MESSAGE
], msg
);
557 /* put the result on the receiver result stack */
560 result
->recv_next
= queue
->recv_result
;
561 queue
->recv_result
= result
;
564 if (!queue
->msg_list
[SEND_MESSAGE
].first
) clear_queue_bits( queue
, QS_SENDMESSAGE
);
567 /* set the result of the current received message */
568 static void reply_message( struct msg_queue
*queue
, unsigned int result
,
569 unsigned int error
, int remove
, const void *data
, size_t len
)
571 struct message_result
*res
= queue
->recv_result
;
575 queue
->recv_result
= res
->recv_next
;
576 res
->receiver
= NULL
;
577 if (!res
->sender
) /* no one waiting for it */
585 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
586 store_message_result( res
, result
, error
);
590 /* retrieve a posted message */
591 static int get_posted_message( struct msg_queue
*queue
, user_handle_t win
,
592 unsigned int first
, unsigned int last
, unsigned int flags
,
593 struct get_message_reply
*reply
)
596 struct message_list
*list
= &queue
->msg_list
[POST_MESSAGE
];
598 /* check against the filters */
599 for (msg
= list
->first
; msg
; msg
= msg
->next
)
601 if (msg
->msg
== WM_QUIT
) break; /* WM_QUIT is never filtered */
602 if (win
&& msg
->win
&& msg
->win
!= win
&& !is_child_window( win
, msg
->win
)) continue;
603 if (msg
->msg
< first
) continue;
604 if (msg
->msg
> last
) continue;
605 break; /* found one */
609 /* return it to the app */
611 reply
->total
= msg
->data_size
;
612 if (msg
->data_size
> get_reply_max_size())
614 set_error( STATUS_BUFFER_OVERFLOW
);
617 reply
->type
= msg
->type
;
618 reply
->win
= msg
->win
;
619 reply
->msg
= msg
->msg
;
620 reply
->wparam
= msg
->wparam
;
621 reply
->lparam
= msg
->lparam
;
624 reply
->time
= msg
->time
;
625 reply
->info
= msg
->info
;
627 if (flags
& GET_MSG_REMOVE
)
631 set_reply_data_ptr( msg
->data
, msg
->data_size
);
635 remove_queue_message( queue
, msg
, POST_MESSAGE
);
637 else if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
642 /* empty a message list and free all the messages */
643 static void empty_msg_list( struct message_list
*list
)
645 struct message
*msg
= list
->first
;
648 struct message
*next
= msg
->next
;
654 /* cleanup all pending results when deleting a queue */
655 static void cleanup_results( struct msg_queue
*queue
)
659 while ((entry
= list_head( &queue
->send_result
)) != NULL
)
661 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
664 while ((entry
= list_head( &queue
->callback_result
)) != NULL
)
666 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
669 while (queue
->recv_result
)
670 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
673 /* check if the thread owning the queue is hung (not checking for messages) */
674 static int is_queue_hung( struct msg_queue
*queue
)
677 struct wait_queue_entry
*entry
;
679 gettimeofday( &now
, NULL
);
680 if (now
.tv_sec
- queue
->last_get_msg
.tv_sec
<= 5)
681 return 0; /* less than 5 seconds since last get message -> not hung */
683 for (entry
= queue
->obj
.head
; entry
; entry
= entry
->next
)
685 if (entry
->thread
->queue
== queue
)
686 return 0; /* thread is waiting on queue -> not hung */
691 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
693 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
694 struct process
*process
= entry
->thread
->process
;
696 /* a thread can only wait on its own queue */
697 if (entry
->thread
->queue
!= queue
)
699 set_error( STATUS_ACCESS_DENIED
);
702 /* if waiting on the main process queue, set the idle event */
703 if (process
->queue
== queue
)
705 if (process
->idle_event
) set_event( process
->idle_event
);
707 add_queue( obj
, entry
);
711 static void msg_queue_remove_queue(struct object
*obj
, struct wait_queue_entry
*entry
)
713 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
714 struct process
*process
= entry
->thread
->process
;
716 remove_queue( obj
, entry
);
718 assert( entry
->thread
->queue
== queue
);
720 /* if waiting on the main process queue, reset the idle event */
721 if (process
->queue
== queue
)
723 if (process
->idle_event
) reset_event( process
->idle_event
);
727 static void msg_queue_dump( struct object
*obj
, int verbose
)
729 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
730 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
731 queue
->wake_bits
, queue
->wake_mask
);
734 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
)
736 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
737 return is_signaled( queue
);
740 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
)
742 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
743 queue
->wake_mask
= 0;
744 queue
->changed_mask
= 0;
745 return 0; /* Not abandoned */
748 static void msg_queue_destroy( struct object
*obj
)
750 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
754 cleanup_results( queue
);
755 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
757 while ((ptr
= list_head( &queue
->pending_timers
)))
759 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
760 list_remove( &timer
->entry
);
763 while ((ptr
= list_head( &queue
->expired_timers
)))
765 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
766 list_remove( &timer
->entry
);
769 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
770 if (queue
->input
) release_object( queue
->input
);
771 if (queue
->hooks
) release_object( queue
->hooks
);
774 static void thread_input_dump( struct object
*obj
, int verbose
)
776 struct thread_input
*input
= (struct thread_input
*)obj
;
777 fprintf( stderr
, "Thread input focus=%p capture=%p active=%p\n",
778 input
->focus
, input
->capture
, input
->active
);
781 static void thread_input_destroy( struct object
*obj
)
783 struct thread_input
*input
= (struct thread_input
*)obj
;
785 if (foreground_input
== input
) foreground_input
= NULL
;
786 if (input
->msg_thread
) release_object( input
->msg_thread
);
787 empty_msg_list( &input
->msg_list
);
790 /* fix the thread input data when a window is destroyed */
791 inline static void thread_input_cleanup_window( struct msg_queue
*queue
, user_handle_t window
)
793 struct thread_input
*input
= queue
->input
;
795 if (window
== input
->focus
) input
->focus
= 0;
796 if (window
== input
->capture
) input
->capture
= 0;
797 if (window
== input
->active
) input
->active
= 0;
798 if (window
== input
->menu_owner
) input
->menu_owner
= 0;
799 if (window
== input
->move_size
) input
->move_size
= 0;
800 if (window
== input
->caret
) set_caret_window( input
, 0 );
803 /* check if the specified window can be set in the input data of a given queue */
804 static int check_queue_input_window( struct msg_queue
*queue
, user_handle_t window
)
806 struct thread
*thread
;
809 if (!window
) return 1; /* we can always clear the data */
811 if ((thread
= get_window_thread( window
)))
813 ret
= (queue
->input
== thread
->queue
->input
);
814 if (!ret
) set_error( STATUS_ACCESS_DENIED
);
815 release_object( thread
);
817 else set_error( STATUS_INVALID_HANDLE
);
822 /* attach two thread input data structures */
823 int attach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
825 struct thread_input
*input
;
827 if (!thread_to
->queue
&& !(thread_to
->queue
= create_msg_queue( thread_to
, NULL
))) return 0;
828 input
= (struct thread_input
*)grab_object( thread_to
->queue
->input
);
830 if (thread_from
->queue
)
832 release_thread_input( thread_from
);
833 thread_from
->queue
->input
= input
;
837 if (!(thread_from
->queue
= create_msg_queue( thread_from
, input
))) return 0;
839 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
843 /* detach two thread input data structures */
844 static void detach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
846 struct thread_input
*input
;
848 if (!thread_from
->queue
|| !thread_to
->queue
||
849 thread_from
->queue
->input
!= thread_to
->queue
->input
)
851 set_error( STATUS_ACCESS_DENIED
);
854 if ((input
= create_thread_input()))
856 release_thread_input( thread_from
);
857 thread_from
->queue
->input
= input
;
862 /* set the next timer to expire */
863 static void set_next_timer( struct msg_queue
*queue
)
869 remove_timeout_user( queue
->timeout
);
870 queue
->timeout
= NULL
;
872 if ((ptr
= list_head( &queue
->pending_timers
)))
874 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
875 queue
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, queue
);
877 /* set/clear QS_TIMER bit */
878 if (list_empty( &queue
->expired_timers
))
879 clear_queue_bits( queue
, QS_TIMER
);
881 set_queue_bits( queue
, QS_TIMER
);
884 /* find a timer from its window and id */
885 static struct timer
*find_timer( struct msg_queue
*queue
, user_handle_t win
,
886 unsigned int msg
, unsigned int id
)
890 /* we need to search both lists */
892 LIST_FOR_EACH( ptr
, &queue
->pending_timers
)
894 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
895 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
897 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
899 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
900 if (timer
->win
== win
&& timer
->msg
== msg
&& timer
->id
== id
) return timer
;
905 /* callback for the next timer expiration */
906 static void timer_callback( void *private )
908 struct msg_queue
*queue
= private;
911 queue
->timeout
= NULL
;
912 /* move on to the next timer */
913 ptr
= list_head( &queue
->pending_timers
);
915 list_add_tail( &queue
->expired_timers
, ptr
);
916 set_next_timer( queue
);
919 /* link a timer at its rightful place in the queue list */
920 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
924 for (ptr
= queue
->pending_timers
.next
; ptr
!= &queue
->pending_timers
; ptr
= ptr
->next
)
926 struct timer
*t
= LIST_ENTRY( ptr
, struct timer
, entry
);
927 if (!time_before( &t
->when
, &timer
->when
)) break;
929 list_add_before( ptr
, &timer
->entry
);
932 /* remove a timer from the queue timer list and free it */
933 static void free_timer( struct msg_queue
*queue
, struct timer
*timer
)
935 list_remove( &timer
->entry
);
937 set_next_timer( queue
);
940 /* restart an expired timer */
941 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
945 list_remove( &timer
->entry
);
946 gettimeofday( &now
, 0 );
947 while (!time_before( &now
, &timer
->when
)) add_timeout( &timer
->when
, timer
->rate
);
948 link_timer( queue
, timer
);
949 set_next_timer( queue
);
952 /* find an expired timer matching the filtering parameters */
953 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
954 unsigned int get_first
, unsigned int get_last
,
959 LIST_FOR_EACH( ptr
, &queue
->expired_timers
)
961 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
962 if (win
&& timer
->win
!= win
) continue;
963 if (timer
->msg
>= get_first
&& timer
->msg
<= get_last
)
965 if (remove
) restart_timer( queue
, timer
);
973 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
975 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
978 timer
->rate
= max( rate
, 1 );
979 gettimeofday( &timer
->when
, 0 );
980 add_timeout( &timer
->when
, rate
);
981 link_timer( queue
, timer
);
982 /* check if we replaced the next timer */
983 if (list_head( &queue
->pending_timers
) == &timer
->entry
) set_next_timer( queue
);
988 /* change the input key state for a given key */
989 static void set_input_key_state( struct thread_input
*input
, unsigned char key
, int down
)
993 if (!(input
->keystate
[key
] & 0x80)) input
->keystate
[key
] ^= 0x01;
994 input
->keystate
[key
] |= 0x80;
996 else input
->keystate
[key
] &= ~0x80;
999 /* update the input key state for a keyboard message */
1000 static void update_input_key_state( struct thread_input
*input
, const struct message
*msg
)
1003 int down
= 0, extended
;
1007 case WM_LBUTTONDOWN
:
1011 set_input_key_state( input
, VK_LBUTTON
, down
);
1013 case WM_MBUTTONDOWN
:
1017 set_input_key_state( input
, VK_MBUTTON
, down
);
1019 case WM_RBUTTONDOWN
:
1023 set_input_key_state( input
, VK_RBUTTON
, down
);
1031 key
= (unsigned char)msg
->wparam
;
1032 extended
= ((msg
->lparam
>> 16) & KF_EXTENDED
) != 0;
1033 set_input_key_state( input
, key
, down
);
1037 set_input_key_state( input
, extended
? VK_RSHIFT
: VK_LSHIFT
, down
);
1040 set_input_key_state( input
, extended
? VK_RCONTROL
: VK_LCONTROL
, down
);
1043 set_input_key_state( input
, extended
? VK_RMENU
: VK_LMENU
, down
);
1050 /* release the hardware message currently being processed by the given thread */
1051 static void release_hardware_message( struct thread
*thread
, int remove
)
1053 struct thread_input
*input
= thread
->queue
->input
;
1055 if (input
->msg_thread
!= thread
) return;
1058 struct message
*other
;
1061 update_input_key_state( input
, input
->msg
);
1062 unlink_message( &input
->msg_list
, input
->msg
);
1063 clr_bit
= get_hardware_msg_bit( input
->msg
);
1064 for (other
= input
->msg_list
.first
; other
; other
= other
->next
)
1065 if (get_hardware_msg_bit( other
) == clr_bit
) break;
1066 if (!other
) clear_queue_bits( thread
->queue
, clr_bit
);
1067 free_message( input
->msg
);
1069 release_object( input
->msg_thread
);
1071 input
->msg_thread
= NULL
;
1074 /* find the window that should receive a given hardware message */
1075 static user_handle_t
find_hardware_message_window( struct thread_input
*input
, struct message
*msg
,
1076 unsigned int *msg_code
)
1078 user_handle_t win
= 0;
1080 *msg_code
= msg
->msg
;
1081 if (is_keyboard_msg( msg
))
1083 if (input
&& !(win
= input
->focus
))
1085 win
= input
->active
;
1086 if (*msg_code
< WM_SYSKEYDOWN
) *msg_code
+= WM_SYSKEYDOWN
- WM_KEYDOWN
;
1089 else /* mouse message */
1091 if (!input
|| !(win
= input
->capture
))
1093 if (!(win
= msg
->win
) || !is_window_visible( win
))
1094 win
= window_from_point( msg
->x
, msg
->y
);
1100 /* queue a hardware message into a given thread input */
1101 static void queue_hardware_message( struct msg_queue
*queue
, struct message
*msg
)
1104 struct thread
*thread
;
1105 struct thread_input
*input
;
1106 unsigned int msg_code
;
1108 win
= find_hardware_message_window( queue
? queue
->input
: foreground_input
, msg
, &msg_code
);
1109 if (!win
|| !(thread
= get_window_thread(win
)))
1114 input
= thread
->queue
->input
;
1116 if (msg
->msg
== WM_MOUSEMOVE
&& merge_message( input
, msg
)) free( msg
);
1119 append_message( &input
->msg_list
, msg
);
1120 set_queue_bits( thread
->queue
, get_hardware_msg_bit(msg
) );
1122 release_object( thread
);
1125 /* find a hardware message for the given queue */
1126 static int get_hardware_message( struct thread
*thread
, struct message
*first
,
1127 user_handle_t filter_win
, struct get_message_reply
*reply
)
1129 struct thread_input
*input
= thread
->queue
->input
;
1130 struct thread
*win_thread
;
1131 struct message
*msg
;
1133 int clear_bits
, got_one
= 0;
1134 unsigned int msg_code
;
1136 if (input
->msg_thread
&& input
->msg_thread
!= thread
)
1137 return 0; /* locked by another thread */
1141 msg
= input
->msg_list
.first
;
1142 clear_bits
= QS_KEY
| QS_MOUSEMOVE
| QS_MOUSEBUTTON
;
1147 clear_bits
= 0; /* don't clear bits if we don't go through the whole list */
1152 win
= find_hardware_message_window( input
, msg
, &msg_code
);
1153 if (!win
|| !(win_thread
= get_window_thread( win
)))
1155 /* no window at all, remove it */
1156 struct message
*next
= msg
->next
;
1157 update_input_key_state( input
, msg
);
1158 unlink_message( &input
->msg_list
, msg
);
1159 free_message( msg
);
1163 if (win_thread
!= thread
)
1165 /* wake the other thread */
1166 set_queue_bits( win_thread
->queue
, get_hardware_msg_bit(msg
) );
1167 release_object( win_thread
);
1172 /* if we already got a message for another thread, or if it doesn't
1173 * match the filter we skip it (filter is only checked for keyboard
1174 * messages since the dest window for a mouse message depends on hittest)
1177 (filter_win
&& is_keyboard_msg(msg
) &&
1178 win
!= filter_win
&& !is_child_window( filter_win
, win
)))
1180 clear_bits
&= ~get_hardware_msg_bit( msg
);
1181 release_object( win_thread
);
1185 /* now we can return it */
1186 if (!input
->msg_thread
) input
->msg_thread
= win_thread
;
1187 else release_object( win_thread
);
1190 reply
->type
= MSG_HARDWARE
;
1192 reply
->msg
= msg_code
;
1193 reply
->wparam
= msg
->wparam
;
1194 reply
->lparam
= msg
->lparam
;
1197 reply
->time
= msg
->time
;
1198 reply
->info
= msg
->info
;
1201 /* nothing found, clear the hardware queue bits */
1202 clear_queue_bits( thread
->queue
, clear_bits
);
1203 if (input
->msg_thread
) release_object( input
->msg_thread
);
1205 input
->msg_thread
= NULL
;
1209 /* increment (or decrement if 'incr' is negative) the queue paint count */
1210 void inc_queue_paint_count( struct thread
*thread
, int incr
)
1212 struct msg_queue
*queue
= thread
->queue
;
1216 if ((queue
->paint_count
+= incr
) < 0) queue
->paint_count
= 0;
1218 if (queue
->paint_count
)
1219 set_queue_bits( queue
, QS_PAINT
);
1221 clear_queue_bits( queue
, QS_PAINT
);
1225 /* remove all messages and timers belonging to a certain window */
1226 void queue_cleanup_window( struct thread
*thread
, user_handle_t win
)
1228 struct msg_queue
*queue
= thread
->queue
;
1230 struct message
*msg
;
1237 ptr
= list_head( &queue
->pending_timers
);
1240 struct list
*next
= list_next( &queue
->pending_timers
, ptr
);
1241 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1242 if (timer
->win
== win
) free_timer( queue
, timer
);
1245 ptr
= list_head( &queue
->expired_timers
);
1248 struct list
*next
= list_next( &queue
->expired_timers
, ptr
);
1249 struct timer
*timer
= LIST_ENTRY( ptr
, struct timer
, entry
);
1250 if (timer
->win
== win
) free_timer( queue
, timer
);
1254 /* remove messages */
1255 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
1257 msg
= queue
->msg_list
[i
].first
;
1260 struct message
*next
= msg
->next
;
1261 if (msg
->win
== win
) remove_queue_message( queue
, msg
, i
);
1266 thread_input_cleanup_window( queue
, win
);
1269 /* post a message to a window; used by socket handling */
1270 void post_message( user_handle_t win
, unsigned int message
,
1271 unsigned int wparam
, unsigned int lparam
)
1273 struct message
*msg
;
1274 struct thread
*thread
= get_window_thread( win
);
1276 if (!thread
) return;
1278 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1280 msg
->type
= MSG_POSTED
;
1281 msg
->win
= get_user_full_handle( win
);
1283 msg
->wparam
= wparam
;
1284 msg
->lparam
= lparam
;
1285 msg
->time
= get_tick_count();
1293 append_message( &thread
->queue
->msg_list
[POST_MESSAGE
], msg
);
1294 set_queue_bits( thread
->queue
, QS_POSTMESSAGE
);
1296 release_object( thread
);
1300 /* get the message queue of the current thread */
1301 DECL_HANDLER(get_msg_queue
)
1303 struct msg_queue
*queue
= get_current_queue();
1306 if (queue
) reply
->handle
= alloc_handle( current
->process
, queue
, SYNCHRONIZE
, 0 );
1310 /* set the current message queue wakeup mask */
1311 DECL_HANDLER(set_queue_mask
)
1313 struct msg_queue
*queue
= get_current_queue();
1317 queue
->wake_mask
= req
->wake_mask
;
1318 queue
->changed_mask
= req
->changed_mask
;
1319 reply
->wake_bits
= queue
->wake_bits
;
1320 reply
->changed_bits
= queue
->changed_bits
;
1321 if (is_signaled( queue
))
1323 /* if skip wait is set, do what would have been done in the subsequent wait */
1324 if (req
->skip_wait
) msg_queue_satisfied( &queue
->obj
, current
);
1325 else wake_up( &queue
->obj
, 0 );
1331 /* get the current message queue status */
1332 DECL_HANDLER(get_queue_status
)
1334 struct msg_queue
*queue
= current
->queue
;
1337 reply
->wake_bits
= queue
->wake_bits
;
1338 reply
->changed_bits
= queue
->changed_bits
;
1339 if (req
->clear
) queue
->changed_bits
= 0;
1341 else reply
->wake_bits
= reply
->changed_bits
= 0;
1345 /* send a message to a thread queue */
1346 DECL_HANDLER(send_message
)
1348 struct message
*msg
;
1349 struct msg_queue
*send_queue
= get_current_queue();
1350 struct msg_queue
*recv_queue
= NULL
;
1351 struct thread
*thread
= NULL
;
1355 if (!(thread
= get_thread_from_id( req
->id
))) return;
1357 else if (req
->type
!= MSG_HARDWARE
)
1359 /* only hardware messages are allowed without destination thread */
1360 set_error( STATUS_INVALID_PARAMETER
);
1364 if (thread
&& !(recv_queue
= thread
->queue
))
1366 set_error( STATUS_INVALID_PARAMETER
);
1367 release_object( thread
);
1370 if (recv_queue
&& (req
->flags
& SEND_MSG_ABORT_IF_HUNG
) && is_queue_hung(recv_queue
))
1372 set_error( STATUS_TIMEOUT
);
1373 release_object( thread
);
1377 if ((msg
= mem_alloc( sizeof(*msg
) )))
1379 msg
->type
= req
->type
;
1380 msg
->win
= get_user_full_handle( req
->win
);
1381 msg
->msg
= req
->msg
;
1382 msg
->wparam
= req
->wparam
;
1383 msg
->lparam
= req
->lparam
;
1384 msg
->time
= req
->time
;
1387 msg
->info
= req
->info
;
1394 case MSG_OTHER_PROCESS
:
1395 msg
->data_size
= get_req_data_size();
1396 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1405 if (!(msg
->result
= alloc_message_result( send_queue
, recv_queue
, msg
,
1406 req
->timeout
, req
->callback
, req
->info
)))
1408 free_message( msg
);
1413 append_message( &recv_queue
->msg_list
[SEND_MESSAGE
], msg
);
1414 set_queue_bits( recv_queue
, QS_SENDMESSAGE
);
1417 /* needed for posted DDE messages */
1418 msg
->data_size
= get_req_data_size();
1419 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1424 append_message( &recv_queue
->msg_list
[POST_MESSAGE
], msg
);
1425 set_queue_bits( recv_queue
, QS_POSTMESSAGE
);
1428 queue_hardware_message( recv_queue
, msg
);
1430 case MSG_CALLBACK_RESULT
: /* cannot send this one */
1432 set_error( STATUS_INVALID_PARAMETER
);
1437 if (thread
) release_object( thread
);
1441 /* get a message from the current queue */
1442 DECL_HANDLER(get_message
)
1444 struct timer
*timer
;
1445 struct message
*msg
;
1446 struct message
*first_hw_msg
= NULL
;
1447 struct msg_queue
*queue
= get_current_queue();
1448 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
1451 gettimeofday( &queue
->last_get_msg
, NULL
);
1453 /* first of all release the hardware input lock if we own it */
1454 /* we'll grab it again if we find a hardware message */
1455 if (queue
->input
->msg_thread
== current
)
1457 first_hw_msg
= queue
->input
->msg
;
1458 release_hardware_message( current
, 0 );
1461 /* first check for sent messages */
1462 if ((msg
= queue
->msg_list
[SEND_MESSAGE
].first
))
1464 receive_message( queue
, msg
, reply
);
1467 if (req
->flags
& GET_MSG_SENT_ONLY
) goto done
; /* nothing else to check */
1469 /* clear changed bits so we can wait on them if we don't find a message */
1470 queue
->changed_bits
= 0;
1472 /* then check for posted messages */
1473 if (get_posted_message( queue
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
1476 /* then check for any raw hardware message */
1477 if (get_hardware_message( current
, first_hw_msg
, get_win
, reply
))
1480 /* now check for WM_PAINT */
1481 if (queue
->paint_count
&&
1482 (WM_PAINT
>= req
->get_first
) && (WM_PAINT
<= req
->get_last
) &&
1483 (reply
->win
= find_window_to_repaint( get_win
, current
)))
1485 reply
->type
= MSG_POSTED
;
1486 reply
->msg
= WM_PAINT
;
1491 reply
->time
= get_tick_count();
1496 /* now check for timer */
1497 if ((timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
1498 req
->get_last
, (req
->flags
& GET_MSG_REMOVE
) )))
1500 reply
->type
= MSG_POSTED
;
1501 reply
->win
= timer
->win
;
1502 reply
->msg
= timer
->msg
;
1503 reply
->wparam
= timer
->id
;
1504 reply
->lparam
= timer
->lparam
;
1507 reply
->time
= get_tick_count();
1513 set_error( STATUS_PENDING
); /* FIXME */
1517 /* reply to a sent message */
1518 DECL_HANDLER(reply_message
)
1520 if (!current
->queue
)
1522 set_error( STATUS_ACCESS_DENIED
);
1525 if (req
->type
== MSG_HARDWARE
)
1527 struct thread_input
*input
= current
->queue
->input
;
1528 if (input
->msg_thread
== current
) release_hardware_message( current
, req
->remove
);
1529 else set_error( STATUS_ACCESS_DENIED
);
1531 else if (current
->queue
->recv_result
)
1532 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
1533 get_req_data(), get_req_data_size() );
1537 /* retrieve the reply for the last message sent */
1538 DECL_HANDLER(get_message_reply
)
1540 struct message_result
*result
;
1542 struct msg_queue
*queue
= current
->queue
;
1546 set_error( STATUS_PENDING
);
1549 if (!(entry
= list_head( &queue
->send_result
))) return; /* no reply ready */
1551 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1552 if (result
->replied
|| req
->cancel
)
1554 if (result
->replied
)
1556 reply
->result
= result
->result
;
1557 set_error( result
->error
);
1560 size_t data_len
= min( result
->data_size
, get_reply_max_size() );
1561 set_reply_data_ptr( result
->data
, data_len
);
1562 result
->data
= NULL
;
1563 result
->data_size
= 0;
1566 remove_result_from_sender( result
);
1568 entry
= list_head( &queue
->send_result
);
1569 if (!entry
) clear_queue_bits( queue
, QS_SMRESULT
);
1572 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1573 if (!result
->replied
) clear_queue_bits( queue
, QS_SMRESULT
);
1577 else set_error( STATUS_ACCESS_DENIED
);
1581 /* set a window timer */
1582 DECL_HANDLER(set_win_timer
)
1584 struct timer
*timer
;
1585 struct msg_queue
*queue
;
1586 struct thread
*thread
= NULL
;
1587 user_handle_t win
= 0;
1588 unsigned int id
= req
->id
;
1592 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1594 set_error( STATUS_INVALID_HANDLE
);
1597 if (thread
->process
!= current
->process
)
1599 release_object( thread
);
1600 set_error( STATUS_ACCESS_DENIED
);
1603 queue
= thread
->queue
;
1604 /* remove it if it existed already */
1605 if ((timer
= find_timer( queue
, win
, req
->msg
, id
))) free_timer( queue
, timer
);
1609 queue
= get_current_queue();
1610 /* find a free id for it */
1613 id
= queue
->next_timer_id
;
1614 if (++queue
->next_timer_id
>= 0x10000) queue
->next_timer_id
= 1;
1616 while (find_timer( queue
, 0, req
->msg
, id
));
1619 if ((timer
= set_timer( queue
, req
->rate
)))
1622 timer
->msg
= req
->msg
;
1624 timer
->lparam
= req
->lparam
;
1627 if (thread
) release_object( thread
);
1630 /* kill a window timer */
1631 DECL_HANDLER(kill_win_timer
)
1633 struct timer
*timer
;
1634 struct thread
*thread
;
1635 user_handle_t win
= 0;
1639 if (!(win
= get_user_full_handle( req
->win
)) || !(thread
= get_window_thread( win
)))
1641 set_error( STATUS_INVALID_HANDLE
);
1644 if (thread
->process
!= current
->process
)
1646 release_object( thread
);
1647 set_error( STATUS_ACCESS_DENIED
);
1651 else thread
= (struct thread
*)grab_object( current
);
1653 if (thread
->queue
&& (timer
= find_timer( thread
->queue
, win
, req
->msg
, req
->id
)))
1654 free_timer( thread
->queue
, timer
);
1656 set_error( STATUS_INVALID_PARAMETER
);
1658 release_object( thread
);
1662 /* attach (or detach) thread inputs */
1663 DECL_HANDLER(attach_thread_input
)
1665 struct thread
*thread_from
= get_thread_from_id( req
->tid_from
);
1666 struct thread
*thread_to
= get_thread_from_id( req
->tid_to
);
1668 if (!thread_from
|| !thread_to
)
1670 if (thread_from
) release_object( thread_from
);
1671 if (thread_to
) release_object( thread_to
);
1674 if (thread_from
!= thread_to
)
1676 if (req
->attach
) attach_thread_input( thread_from
, thread_to
);
1677 else detach_thread_input( thread_from
, thread_to
);
1679 else set_error( STATUS_ACCESS_DENIED
);
1680 release_object( thread_from
);
1681 release_object( thread_to
);
1685 /* get thread input data */
1686 DECL_HANDLER(get_thread_input
)
1688 struct thread
*thread
= NULL
;
1689 struct thread_input
*input
;
1693 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1694 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1696 else input
= foreground_input
; /* get the foreground thread info */
1700 reply
->focus
= input
->focus
;
1701 reply
->capture
= input
->capture
;
1702 reply
->active
= input
->active
;
1703 reply
->menu_owner
= input
->menu_owner
;
1704 reply
->move_size
= input
->move_size
;
1705 reply
->caret
= input
->caret
;
1706 reply
->rect
= input
->caret_rect
;
1713 reply
->menu_owner
= 0;
1714 reply
->move_size
= 0;
1716 reply
->rect
.left
= reply
->rect
.top
= reply
->rect
.right
= reply
->rect
.bottom
= 0;
1718 /* foreground window is active window of foreground thread */
1719 reply
->foreground
= foreground_input
? foreground_input
->active
: 0;
1720 if (thread
) release_object( thread
);
1724 /* retrieve queue keyboard state for a given thread */
1725 DECL_HANDLER(get_key_state
)
1727 struct thread
*thread
;
1728 struct thread_input
*input
;
1730 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1731 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1734 if (req
->key
>= 0) reply
->state
= input
->keystate
[req
->key
& 0xff];
1735 set_reply_data( input
->keystate
, min( get_reply_max_size(), sizeof(input
->keystate
) ));
1737 release_object( thread
);
1741 /* set queue keyboard state for a given thread */
1742 DECL_HANDLER(set_key_state
)
1744 struct thread
*thread
= NULL
;
1745 struct thread_input
*input
;
1747 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1748 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1751 size_t size
= min( sizeof(input
->keystate
), get_req_data_size() );
1752 if (size
) memcpy( input
->keystate
, get_req_data(), size
);
1754 release_object( thread
);
1758 /* set the system foreground window */
1759 DECL_HANDLER(set_foreground_window
)
1761 struct msg_queue
*queue
= get_current_queue();
1763 reply
->previous
= foreground_input
? foreground_input
->active
: 0;
1764 reply
->send_msg_old
= (reply
->previous
&& foreground_input
!= queue
->input
);
1765 reply
->send_msg_new
= FALSE
;
1769 struct thread
*thread
;
1771 if (is_top_level_window( req
->handle
) &&
1772 ((thread
= get_window_thread( req
->handle
))))
1774 foreground_input
= thread
->queue
->input
;
1775 reply
->send_msg_new
= (foreground_input
!= queue
->input
);
1776 release_object( thread
);
1778 else set_error( STATUS_INVALID_HANDLE
);
1780 else foreground_input
= NULL
;
1784 /* set the current thread focus window */
1785 DECL_HANDLER(set_focus_window
)
1787 struct msg_queue
*queue
= get_current_queue();
1789 reply
->previous
= 0;
1790 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1792 reply
->previous
= queue
->input
->focus
;
1793 queue
->input
->focus
= get_user_full_handle( req
->handle
);
1798 /* set the current thread active window */
1799 DECL_HANDLER(set_active_window
)
1801 struct msg_queue
*queue
= get_current_queue();
1803 reply
->previous
= 0;
1804 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1806 if (!req
->handle
|| make_window_active( req
->handle
))
1808 reply
->previous
= queue
->input
->active
;
1809 queue
->input
->active
= get_user_full_handle( req
->handle
);
1811 else set_error( STATUS_INVALID_HANDLE
);
1816 /* set the current thread capture window */
1817 DECL_HANDLER(set_capture_window
)
1819 struct msg_queue
*queue
= get_current_queue();
1821 reply
->previous
= reply
->full_handle
= 0;
1822 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1824 struct thread_input
*input
= queue
->input
;
1826 reply
->previous
= input
->capture
;
1827 input
->capture
= get_user_full_handle( req
->handle
);
1828 input
->menu_owner
= (req
->flags
& CAPTURE_MENU
) ? input
->capture
: 0;
1829 input
->move_size
= (req
->flags
& CAPTURE_MOVESIZE
) ? input
->capture
: 0;
1830 reply
->full_handle
= input
->capture
;
1835 /* Set the current thread caret window */
1836 DECL_HANDLER(set_caret_window
)
1838 struct msg_queue
*queue
= get_current_queue();
1840 reply
->previous
= 0;
1841 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1843 struct thread_input
*input
= queue
->input
;
1845 reply
->previous
= input
->caret
;
1846 reply
->old_rect
= input
->caret_rect
;
1847 reply
->old_hide
= input
->caret_hide
;
1848 reply
->old_state
= input
->caret_state
;
1850 set_caret_window( input
, get_user_full_handle(req
->handle
) );
1851 input
->caret_rect
.right
= req
->width
;
1852 input
->caret_rect
.bottom
= req
->height
;
1857 /* Set the current thread caret information */
1858 DECL_HANDLER(set_caret_info
)
1860 struct msg_queue
*queue
= get_current_queue();
1861 struct thread_input
*input
;
1864 input
= queue
->input
;
1865 reply
->full_handle
= input
->caret
;
1866 reply
->old_rect
= input
->caret_rect
;
1867 reply
->old_hide
= input
->caret_hide
;
1868 reply
->old_state
= input
->caret_state
;
1870 if (req
->handle
&& get_user_full_handle(req
->handle
) != input
->caret
)
1872 set_error( STATUS_ACCESS_DENIED
);
1875 if (req
->flags
& SET_CARET_POS
)
1877 input
->caret_rect
.right
+= req
->x
- input
->caret_rect
.left
;
1878 input
->caret_rect
.bottom
+= req
->y
- input
->caret_rect
.top
;
1879 input
->caret_rect
.left
= req
->x
;
1880 input
->caret_rect
.top
= req
->y
;
1882 if (req
->flags
& SET_CARET_HIDE
)
1884 input
->caret_hide
+= req
->hide
;
1885 if (input
->caret_hide
< 0) input
->caret_hide
= 0;
1887 if (req
->flags
& SET_CARET_STATE
)
1889 if (req
->state
== -1) input
->caret_state
= !input
->caret_state
;
1890 else input
->caret_state
= !!req
->state
;