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 timer
*next
; /* next timer in list */
87 struct timer
*prev
; /* prev timer in list */
88 struct timeval when
; /* next expiration */
89 unsigned int rate
; /* timer rate in ms */
90 user_handle_t win
; /* window handle */
91 unsigned int msg
; /* message to post */
92 unsigned int id
; /* timer id */
93 unsigned int lparam
; /* lparam for message */
98 struct object obj
; /* object header */
99 user_handle_t focus
; /* focus window */
100 user_handle_t capture
; /* capture window */
101 user_handle_t active
; /* active window */
102 user_handle_t menu_owner
; /* current menu owner window */
103 user_handle_t move_size
; /* current moving/resizing window */
104 user_handle_t caret
; /* caret window */
105 rectangle_t caret_rect
; /* caret rectangle */
106 int caret_hide
; /* caret hide count */
107 int caret_state
; /* caret on/off state */
108 struct message
*msg
; /* message currently processed */
109 struct thread
*msg_thread
; /* thread processing the message */
110 struct message_list msg_list
; /* list of hardware messages */
111 unsigned char keystate
[256]; /* state of each key */
116 struct object obj
; /* object header */
117 unsigned int wake_bits
; /* wakeup bits */
118 unsigned int wake_mask
; /* wakeup mask */
119 unsigned int changed_bits
; /* changed wakeup bits */
120 unsigned int changed_mask
; /* changed wakeup mask */
121 int paint_count
; /* pending paint messages count */
122 struct message_list msg_list
[NB_MSG_KINDS
]; /* lists of messages */
123 struct list send_result
; /* stack of sent messages waiting for result */
124 struct list callback_result
; /* list of callback messages waiting for result */
125 struct message_result
*recv_result
; /* stack of received messages waiting for result */
126 struct timer
*first_timer
; /* head of timer list */
127 struct timer
*last_timer
; /* tail of timer list */
128 struct timer
*next_timer
; /* next timer to expire */
129 struct timeout_user
*timeout
; /* timeout for next timer to expire */
130 struct thread_input
*input
; /* thread input descriptor */
131 struct hook_table
*hooks
; /* hook table */
132 struct timeval last_get_msg
; /* time of last get message call */
135 static void msg_queue_dump( struct object
*obj
, int verbose
);
136 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
137 static void msg_queue_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
138 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
);
139 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
);
140 static void msg_queue_destroy( struct object
*obj
);
141 static void thread_input_dump( struct object
*obj
, int verbose
);
142 static void thread_input_destroy( struct object
*obj
);
143 static void timer_callback( void *private );
145 static const struct object_ops msg_queue_ops
=
147 sizeof(struct msg_queue
), /* size */
148 msg_queue_dump
, /* dump */
149 msg_queue_add_queue
, /* add_queue */
150 msg_queue_remove_queue
, /* remove_queue */
151 msg_queue_signaled
, /* signaled */
152 msg_queue_satisfied
, /* satisfied */
153 no_get_fd
, /* get_fd */
154 msg_queue_destroy
/* destroy */
158 static const struct object_ops thread_input_ops
=
160 sizeof(struct thread_input
), /* size */
161 thread_input_dump
, /* dump */
162 no_add_queue
, /* add_queue */
163 NULL
, /* remove_queue */
165 NULL
, /* satisfied */
166 no_get_fd
, /* get_fd */
167 thread_input_destroy
/* destroy */
170 /* pointer to input structure of foreground thread */
171 static struct thread_input
*foreground_input
;
174 /* set the caret window in a given thread input */
175 static void set_caret_window( struct thread_input
*input
, user_handle_t win
)
178 input
->caret_rect
.left
= 0;
179 input
->caret_rect
.top
= 0;
180 input
->caret_rect
.right
= 0;
181 input
->caret_rect
.bottom
= 0;
182 input
->caret_hide
= 1;
183 input
->caret_state
= 0;
186 /* create a thread input object */
187 static struct thread_input
*create_thread_input(void)
189 struct thread_input
*input
;
191 if ((input
= alloc_object( &thread_input_ops
)))
196 input
->menu_owner
= 0;
197 input
->move_size
= 0;
199 input
->msg_thread
= NULL
;
200 input
->msg_list
.first
= input
->msg_list
.last
= NULL
;
201 set_caret_window( input
, 0 );
202 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
207 /* release the thread input data of a given thread */
208 static void release_thread_input( struct thread
*thread
)
210 struct thread_input
*input
= thread
->queue
->input
;
213 if (input
->msg_thread
== thread
)
215 release_object( input
->msg_thread
);
216 input
->msg_thread
= NULL
;
219 release_object( input
);
220 thread
->queue
->input
= NULL
;
223 /* create a message queue object */
224 static struct msg_queue
*create_msg_queue( struct thread
*thread
, struct thread_input
*input
)
226 struct msg_queue
*queue
;
229 if (!input
&& !(input
= create_thread_input())) return NULL
;
230 if ((queue
= alloc_object( &msg_queue_ops
)))
232 queue
->wake_bits
= 0;
233 queue
->wake_mask
= 0;
234 queue
->changed_bits
= 0;
235 queue
->changed_mask
= 0;
236 queue
->paint_count
= 0;
237 queue
->recv_result
= NULL
;
238 queue
->first_timer
= NULL
;
239 queue
->last_timer
= NULL
;
240 queue
->next_timer
= NULL
;
241 queue
->timeout
= NULL
;
242 queue
->input
= (struct thread_input
*)grab_object( input
);
244 gettimeofday( &queue
->last_get_msg
, NULL
);
245 list_init( &queue
->send_result
);
246 list_init( &queue
->callback_result
);
247 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
248 queue
->msg_list
[i
].first
= queue
->msg_list
[i
].last
= NULL
;
250 thread
->queue
= queue
;
251 if (!thread
->process
->queue
)
252 thread
->process
->queue
= (struct msg_queue
*)grab_object( queue
);
254 release_object( input
);
258 /* free the message queue of a thread at thread exit */
259 void free_msg_queue( struct thread
*thread
)
261 struct process
*process
= thread
->process
;
263 remove_thread_hooks( thread
);
264 if (!thread
->queue
) return;
265 if (process
->queue
== thread
->queue
) /* is it the process main queue? */
267 release_object( process
->queue
);
268 process
->queue
= NULL
;
269 if (process
->idle_event
)
271 set_event( process
->idle_event
);
272 release_object( process
->idle_event
);
273 process
->idle_event
= NULL
;
276 release_thread_input( thread
);
277 release_object( thread
->queue
);
278 thread
->queue
= NULL
;
281 /* get the hook table for a given thread */
282 struct hook_table
*get_queue_hooks( struct thread
*thread
)
284 if (!thread
->queue
) return NULL
;
285 return thread
->queue
->hooks
;
288 /* set the hook table for a given thread, allocating the queue if needed */
289 void set_queue_hooks( struct thread
*thread
, struct hook_table
*hooks
)
291 struct msg_queue
*queue
= thread
->queue
;
292 if (!queue
) queue
= create_msg_queue( thread
, NULL
);
293 if (queue
->hooks
) release_object( queue
->hooks
);
294 queue
->hooks
= hooks
;
297 /* check the queue status */
298 inline static int is_signaled( struct msg_queue
*queue
)
300 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
303 /* set some queue bits */
304 inline static void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
306 queue
->wake_bits
|= bits
;
307 queue
->changed_bits
|= bits
;
308 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
311 /* clear some queue bits */
312 inline static void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
314 queue
->wake_bits
&= ~bits
;
315 queue
->changed_bits
&= ~bits
;
318 /* check whether msg is a keyboard message */
319 inline static int is_keyboard_msg( struct message
*msg
)
321 return (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
);
324 /* get the QS_* bit corresponding to a given hardware message */
325 inline static int get_hardware_msg_bit( struct message
*msg
)
327 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
328 if (is_keyboard_msg( msg
)) return QS_KEY
;
329 return QS_MOUSEBUTTON
;
332 /* get the current thread queue, creating it if needed */
333 inline static struct msg_queue
*get_current_queue(void)
335 struct msg_queue
*queue
= current
->queue
;
336 if (!queue
) queue
= create_msg_queue( current
, NULL
);
340 /* append a message to the end of a list */
341 inline static void append_message( struct message_list
*list
, struct message
*msg
)
344 if ((msg
->prev
= list
->last
)) msg
->prev
->next
= msg
;
345 else list
->first
= msg
;
349 /* unlink a message from a list it */
350 inline static void unlink_message( struct message_list
*list
, struct message
*msg
)
352 if (msg
->next
) msg
->next
->prev
= msg
->prev
;
353 else list
->last
= msg
->prev
;
354 if (msg
->prev
) msg
->prev
->next
= msg
->next
;
355 else list
->first
= msg
->next
;
358 /* try to merge a message with the last in the list; return 1 if successful */
359 static int merge_message( struct thread_input
*input
, const struct message
*msg
)
361 struct message
*prev
= input
->msg_list
.last
;
364 if (input
->msg
== prev
) return 0;
365 if (prev
->result
) return 0;
366 if (prev
->win
!= msg
->win
) return 0;
367 if (prev
->msg
!= msg
->msg
) return 0;
368 if (prev
->type
!= msg
->type
) return 0;
369 /* now we can merge it */
370 prev
->wparam
= msg
->wparam
;
371 prev
->lparam
= msg
->lparam
;
374 prev
->time
= msg
->time
;
375 prev
->info
= msg
->info
;
379 /* free a result structure */
380 static void free_result( struct message_result
*result
)
382 if (result
->timeout
) remove_timeout_user( result
->timeout
);
383 if (result
->data
) free( result
->data
);
384 if (result
->callback_msg
) free( result
->callback_msg
);
388 /* remove the result from the sender list it is on */
389 static inline void remove_result_from_sender( struct message_result
*result
)
391 assert( result
->sender
);
393 list_remove( &result
->sender_entry
);
394 result
->sender
= NULL
;
395 if (!result
->receiver
) free_result( result
);
398 /* store the message result in the appropriate structure */
399 static void store_message_result( struct message_result
*res
, unsigned int result
,
402 res
->result
= result
;
407 remove_timeout_user( res
->timeout
);
412 if (res
->callback_msg
)
414 /* queue the callback message in the sender queue */
415 res
->callback_msg
->lparam
= result
;
416 append_message( &res
->sender
->msg_list
[SEND_MESSAGE
], res
->callback_msg
);
417 set_queue_bits( res
->sender
, QS_SENDMESSAGE
);
418 res
->callback_msg
= NULL
;
419 remove_result_from_sender( res
);
423 /* wake sender queue if waiting on this result */
424 if (list_head(&res
->sender
->send_result
) == &res
->sender_entry
)
425 set_queue_bits( res
->sender
, QS_SMRESULT
);
431 /* free a message when deleting a queue or window */
432 static void free_message( struct message
*msg
)
434 struct message_result
*result
= msg
->result
;
439 result
->receiver
= NULL
;
440 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
442 else free_result( result
);
444 if (msg
->data
) free( msg
->data
);
448 /* remove (and free) a message from a message list */
449 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
450 enum message_kind kind
)
452 unlink_message( &queue
->msg_list
[kind
], msg
);
456 if (!queue
->msg_list
[kind
].first
) clear_queue_bits( queue
, QS_SENDMESSAGE
);
459 if (!queue
->msg_list
[kind
].first
) clear_queue_bits( queue
, QS_POSTMESSAGE
);
465 /* message timed out without getting a reply */
466 static void result_timeout( void *private )
468 struct message_result
*result
= private;
470 assert( !result
->replied
);
472 result
->timeout
= NULL
;
473 store_message_result( result
, 0, STATUS_TIMEOUT
);
476 /* allocate and fill a message result structure */
477 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
478 struct msg_queue
*recv_queue
,
479 struct message
*msg
, unsigned int timeout
,
480 void *callback
, unsigned int callback_data
)
482 struct message_result
*result
= mem_alloc( sizeof(*result
) );
485 result
->sender
= send_queue
;
486 result
->receiver
= recv_queue
;
489 result
->data_size
= 0;
490 result
->timeout
= NULL
;
492 if (msg
->type
== MSG_CALLBACK
)
494 struct message
*callback_msg
= mem_alloc( sizeof(*callback_msg
) );
500 callback_msg
->type
= MSG_CALLBACK_RESULT
;
501 callback_msg
->win
= msg
->win
;
502 callback_msg
->msg
= msg
->msg
;
503 callback_msg
->wparam
= (unsigned int)callback
;
504 callback_msg
->lparam
= 0;
505 callback_msg
->time
= get_tick_count();
508 callback_msg
->info
= callback_data
;
509 callback_msg
->result
= NULL
;
510 callback_msg
->data
= NULL
;
511 callback_msg
->data_size
= 0;
513 result
->callback_msg
= callback_msg
;
514 list_add_head( &send_queue
->callback_result
, &result
->sender_entry
);
518 result
->callback_msg
= NULL
;
519 list_add_head( &send_queue
->send_result
, &result
->sender_entry
);
525 gettimeofday( &when
, 0 );
526 add_timeout( &when
, timeout
);
527 result
->timeout
= add_timeout_user( &when
, result_timeout
, result
);
533 /* receive a message, removing it from the sent queue */
534 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
535 struct get_message_reply
*reply
)
537 struct message_result
*result
= msg
->result
;
539 reply
->total
= msg
->data_size
;
540 if (msg
->data_size
> get_reply_max_size())
542 set_error( STATUS_BUFFER_OVERFLOW
);
545 reply
->type
= msg
->type
;
546 reply
->win
= msg
->win
;
547 reply
->msg
= msg
->msg
;
548 reply
->wparam
= msg
->wparam
;
549 reply
->lparam
= msg
->lparam
;
552 reply
->time
= msg
->time
;
553 reply
->info
= msg
->info
;
555 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
557 unlink_message( &queue
->msg_list
[SEND_MESSAGE
], msg
);
558 /* put the result on the receiver result stack */
561 result
->recv_next
= queue
->recv_result
;
562 queue
->recv_result
= result
;
565 if (!queue
->msg_list
[SEND_MESSAGE
].first
) clear_queue_bits( queue
, QS_SENDMESSAGE
);
568 /* set the result of the current received message */
569 static void reply_message( struct msg_queue
*queue
, unsigned int result
,
570 unsigned int error
, int remove
, const void *data
, size_t len
)
572 struct message_result
*res
= queue
->recv_result
;
576 queue
->recv_result
= res
->recv_next
;
577 res
->receiver
= NULL
;
578 if (!res
->sender
) /* no one waiting for it */
586 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
587 store_message_result( res
, result
, error
);
591 /* retrieve a posted message */
592 static int get_posted_message( struct msg_queue
*queue
, user_handle_t win
,
593 unsigned int first
, unsigned int last
, unsigned int flags
,
594 struct get_message_reply
*reply
)
597 struct message_list
*list
= &queue
->msg_list
[POST_MESSAGE
];
599 /* check against the filters */
600 for (msg
= list
->first
; msg
; msg
= msg
->next
)
602 if (msg
->msg
== WM_QUIT
) break; /* WM_QUIT is never filtered */
603 if (win
&& msg
->win
&& msg
->win
!= win
&& !is_child_window( win
, msg
->win
)) continue;
604 if (msg
->msg
< first
) continue;
605 if (msg
->msg
> last
) continue;
606 break; /* found one */
610 /* return it to the app */
612 reply
->total
= msg
->data_size
;
613 if (msg
->data_size
> get_reply_max_size())
615 set_error( STATUS_BUFFER_OVERFLOW
);
618 reply
->type
= msg
->type
;
619 reply
->win
= msg
->win
;
620 reply
->msg
= msg
->msg
;
621 reply
->wparam
= msg
->wparam
;
622 reply
->lparam
= msg
->lparam
;
625 reply
->time
= msg
->time
;
626 reply
->info
= msg
->info
;
628 if (flags
& GET_MSG_REMOVE
)
632 set_reply_data_ptr( msg
->data
, msg
->data_size
);
636 remove_queue_message( queue
, msg
, POST_MESSAGE
);
638 else if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
643 /* empty a message list and free all the messages */
644 static void empty_msg_list( struct message_list
*list
)
646 struct message
*msg
= list
->first
;
649 struct message
*next
= msg
->next
;
655 /* cleanup all pending results when deleting a queue */
656 static void cleanup_results( struct msg_queue
*queue
)
660 while ((entry
= list_head( &queue
->send_result
)) != NULL
)
662 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
665 while ((entry
= list_head( &queue
->callback_result
)) != NULL
)
667 remove_result_from_sender( LIST_ENTRY( entry
, struct message_result
, sender_entry
) );
670 while (queue
->recv_result
)
671 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
674 /* check if the thread owning the queue is hung (not checking for messages) */
675 static int is_queue_hung( struct msg_queue
*queue
)
678 struct wait_queue_entry
*entry
;
680 gettimeofday( &now
, NULL
);
681 if (now
.tv_sec
- queue
->last_get_msg
.tv_sec
<= 5)
682 return 0; /* less than 5 seconds since last get message -> not hung */
684 for (entry
= queue
->obj
.head
; entry
; entry
= entry
->next
)
686 if (entry
->thread
->queue
== queue
)
687 return 0; /* thread is waiting on queue -> not hung */
692 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
694 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
695 struct process
*process
= entry
->thread
->process
;
697 /* a thread can only wait on its own queue */
698 if (entry
->thread
->queue
!= queue
)
700 set_error( STATUS_ACCESS_DENIED
);
703 /* if waiting on the main process queue, set the idle event */
704 if (process
->queue
== queue
)
706 if (process
->idle_event
) set_event( process
->idle_event
);
708 add_queue( obj
, entry
);
712 static void msg_queue_remove_queue(struct object
*obj
, struct wait_queue_entry
*entry
)
714 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
715 struct process
*process
= entry
->thread
->process
;
717 remove_queue( obj
, entry
);
719 assert( entry
->thread
->queue
== queue
);
721 /* if waiting on the main process queue, reset the idle event */
722 if (process
->queue
== queue
)
724 if (process
->idle_event
) reset_event( process
->idle_event
);
728 static void msg_queue_dump( struct object
*obj
, int verbose
)
730 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
731 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
732 queue
->wake_bits
, queue
->wake_mask
);
735 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
)
737 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
738 return is_signaled( queue
);
741 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
)
743 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
744 queue
->wake_mask
= 0;
745 queue
->changed_mask
= 0;
746 return 0; /* Not abandoned */
749 static void msg_queue_destroy( struct object
*obj
)
751 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
752 struct timer
*timer
= queue
->first_timer
;
755 cleanup_results( queue
);
756 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
760 struct timer
*next
= timer
->next
;
764 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
765 if (queue
->input
) release_object( queue
->input
);
766 if (queue
->hooks
) release_object( queue
->hooks
);
769 static void thread_input_dump( struct object
*obj
, int verbose
)
771 struct thread_input
*input
= (struct thread_input
*)obj
;
772 fprintf( stderr
, "Thread input focus=%p capture=%p active=%p\n",
773 input
->focus
, input
->capture
, input
->active
);
776 static void thread_input_destroy( struct object
*obj
)
778 struct thread_input
*input
= (struct thread_input
*)obj
;
780 if (foreground_input
== input
) foreground_input
= NULL
;
781 if (input
->msg_thread
) release_object( input
->msg_thread
);
782 empty_msg_list( &input
->msg_list
);
785 /* fix the thread input data when a window is destroyed */
786 inline static void thread_input_cleanup_window( struct msg_queue
*queue
, user_handle_t window
)
788 struct thread_input
*input
= queue
->input
;
790 if (window
== input
->focus
) input
->focus
= 0;
791 if (window
== input
->capture
) input
->capture
= 0;
792 if (window
== input
->active
) input
->active
= 0;
793 if (window
== input
->menu_owner
) input
->menu_owner
= 0;
794 if (window
== input
->move_size
) input
->move_size
= 0;
795 if (window
== input
->caret
) set_caret_window( input
, 0 );
798 /* check if the specified window can be set in the input data of a given queue */
799 static int check_queue_input_window( struct msg_queue
*queue
, user_handle_t window
)
801 struct thread
*thread
;
804 if (!window
) return 1; /* we can always clear the data */
806 if ((thread
= get_window_thread( window
)))
808 ret
= (queue
->input
== thread
->queue
->input
);
809 if (!ret
) set_error( STATUS_ACCESS_DENIED
);
810 release_object( thread
);
812 else set_error( STATUS_INVALID_HANDLE
);
817 /* attach two thread input data structures */
818 int attach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
820 struct thread_input
*input
;
822 if (!thread_to
->queue
&& !(thread_to
->queue
= create_msg_queue( thread_to
, NULL
))) return 0;
823 input
= (struct thread_input
*)grab_object( thread_to
->queue
->input
);
825 if (thread_from
->queue
)
827 release_thread_input( thread_from
);
828 thread_from
->queue
->input
= input
;
832 if (!(thread_from
->queue
= create_msg_queue( thread_from
, input
))) return 0;
834 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
838 /* detach two thread input data structures */
839 static void detach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
841 struct thread_input
*input
;
843 if (!thread_from
->queue
|| !thread_to
->queue
||
844 thread_from
->queue
->input
!= thread_to
->queue
->input
)
846 set_error( STATUS_ACCESS_DENIED
);
849 if ((input
= create_thread_input()))
851 release_thread_input( thread_from
);
852 thread_from
->queue
->input
= input
;
857 /* set the next timer to expire */
858 static void set_next_timer( struct msg_queue
*queue
, struct timer
*timer
)
862 remove_timeout_user( queue
->timeout
);
863 queue
->timeout
= NULL
;
865 if ((queue
->next_timer
= timer
))
866 queue
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, queue
);
868 /* set/clear QS_TIMER bit */
869 if (queue
->next_timer
== queue
->first_timer
)
870 clear_queue_bits( queue
, QS_TIMER
);
872 set_queue_bits( queue
, QS_TIMER
);
875 /* callback for the next timer expiration */
876 static void timer_callback( void *private )
878 struct msg_queue
*queue
= private;
880 queue
->timeout
= NULL
;
881 /* move on to the next timer */
882 set_next_timer( queue
, queue
->next_timer
->next
);
885 /* link a timer at its rightful place in the queue list */
886 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
888 struct timer
*pos
= queue
->next_timer
;
890 while (pos
&& time_before( &pos
->when
, &timer
->when
)) pos
= pos
->next
;
892 if (pos
) /* insert before pos */
894 if ((timer
->prev
= pos
->prev
)) timer
->prev
->next
= timer
;
895 else queue
->first_timer
= timer
;
899 else /* insert at end */
902 timer
->prev
= queue
->last_timer
;
903 if (queue
->last_timer
) queue
->last_timer
->next
= timer
;
904 else queue
->first_timer
= timer
;
905 queue
->last_timer
= timer
;
907 /* check if we replaced the next timer */
908 if (pos
== queue
->next_timer
) set_next_timer( queue
, timer
);
911 /* remove a timer from the queue timer list */
912 static void unlink_timer( struct msg_queue
*queue
, struct timer
*timer
)
914 if (timer
->next
) timer
->next
->prev
= timer
->prev
;
915 else queue
->last_timer
= timer
->prev
;
916 if (timer
->prev
) timer
->prev
->next
= timer
->next
;
917 else queue
->first_timer
= timer
->next
;
918 /* check if we removed the next timer */
919 if (queue
->next_timer
== timer
) set_next_timer( queue
, timer
->next
);
920 else if (queue
->next_timer
== queue
->first_timer
) clear_queue_bits( queue
, QS_TIMER
);
923 /* restart an expired timer */
924 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
927 unlink_timer( queue
, timer
);
928 gettimeofday( &now
, 0 );
929 while (!time_before( &now
, &timer
->when
)) add_timeout( &timer
->when
, timer
->rate
);
930 link_timer( queue
, timer
);
933 /* find an expired timer matching the filtering parameters */
934 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
935 unsigned int get_first
, unsigned int get_last
,
939 for (timer
= queue
->first_timer
; (timer
&& timer
!= queue
->next_timer
); timer
= timer
->next
)
941 if (win
&& timer
->win
!= win
) continue;
942 if (timer
->msg
>= get_first
&& timer
->msg
<= get_last
)
944 if (remove
) restart_timer( queue
, timer
);
952 static int kill_timer( struct msg_queue
*queue
, user_handle_t win
,
953 unsigned int msg
, unsigned int id
)
957 for (timer
= queue
->first_timer
; timer
; timer
= timer
->next
)
959 if (timer
->win
!= win
|| timer
->msg
!= msg
|| timer
->id
!= id
) continue;
960 unlink_timer( queue
, timer
);
968 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
970 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
974 gettimeofday( &timer
->when
, 0 );
975 add_timeout( &timer
->when
, rate
);
976 link_timer( queue
, timer
);
981 /* change the input key state for a given key */
982 static void set_input_key_state( struct thread_input
*input
, unsigned char key
, int down
)
986 if (!(input
->keystate
[key
] & 0x80)) input
->keystate
[key
] ^= 0x01;
987 input
->keystate
[key
] |= 0x80;
989 else input
->keystate
[key
] &= ~0x80;
992 /* update the input key state for a keyboard message */
993 static void update_input_key_state( struct thread_input
*input
, const struct message
*msg
)
996 int down
= 0, extended
;
1000 case WM_LBUTTONDOWN
:
1004 set_input_key_state( input
, VK_LBUTTON
, down
);
1006 case WM_MBUTTONDOWN
:
1010 set_input_key_state( input
, VK_MBUTTON
, down
);
1012 case WM_RBUTTONDOWN
:
1016 set_input_key_state( input
, VK_RBUTTON
, down
);
1024 key
= (unsigned char)msg
->wparam
;
1025 extended
= ((msg
->lparam
>> 16) & KF_EXTENDED
) != 0;
1026 set_input_key_state( input
, key
, down
);
1030 set_input_key_state( input
, extended
? VK_RSHIFT
: VK_LSHIFT
, down
);
1033 set_input_key_state( input
, extended
? VK_RCONTROL
: VK_LCONTROL
, down
);
1036 set_input_key_state( input
, extended
? VK_RMENU
: VK_LMENU
, down
);
1043 /* release the hardware message currently being processed by the given thread */
1044 static void release_hardware_message( struct thread
*thread
, int remove
)
1046 struct thread_input
*input
= thread
->queue
->input
;
1048 if (input
->msg_thread
!= thread
) return;
1051 struct message
*other
;
1054 update_input_key_state( input
, input
->msg
);
1055 unlink_message( &input
->msg_list
, input
->msg
);
1056 clr_bit
= get_hardware_msg_bit( input
->msg
);
1057 for (other
= input
->msg_list
.first
; other
; other
= other
->next
)
1058 if (get_hardware_msg_bit( other
) == clr_bit
) break;
1059 if (!other
) clear_queue_bits( thread
->queue
, clr_bit
);
1060 free_message( input
->msg
);
1062 release_object( input
->msg_thread
);
1064 input
->msg_thread
= NULL
;
1067 /* find the window that should receive a given hardware message */
1068 static user_handle_t
find_hardware_message_window( struct thread_input
*input
, struct message
*msg
,
1069 unsigned int *msg_code
)
1071 user_handle_t win
= 0;
1073 *msg_code
= msg
->msg
;
1074 if (is_keyboard_msg( msg
))
1076 if (input
&& !(win
= input
->focus
))
1078 win
= input
->active
;
1079 if (*msg_code
< WM_SYSKEYDOWN
) *msg_code
+= WM_SYSKEYDOWN
- WM_KEYDOWN
;
1082 else /* mouse message */
1084 if (!input
|| !(win
= input
->capture
))
1086 if (!(win
= msg
->win
) || !is_window_visible( win
))
1087 win
= window_from_point( msg
->x
, msg
->y
);
1093 /* queue a hardware message into a given thread input */
1094 static void queue_hardware_message( struct msg_queue
*queue
, struct message
*msg
)
1097 struct thread
*thread
;
1098 struct thread_input
*input
;
1099 unsigned int msg_code
;
1101 win
= find_hardware_message_window( queue
? queue
->input
: foreground_input
, msg
, &msg_code
);
1102 if (!win
|| !(thread
= get_window_thread(win
)))
1107 input
= thread
->queue
->input
;
1109 if (msg
->msg
== WM_MOUSEMOVE
&& merge_message( input
, msg
)) free( msg
);
1112 append_message( &input
->msg_list
, msg
);
1113 set_queue_bits( thread
->queue
, get_hardware_msg_bit(msg
) );
1115 release_object( thread
);
1118 /* find a hardware message for the given queue */
1119 static int get_hardware_message( struct thread
*thread
, struct message
*first
,
1120 user_handle_t filter_win
, struct get_message_reply
*reply
)
1122 struct thread_input
*input
= thread
->queue
->input
;
1123 struct thread
*win_thread
;
1124 struct message
*msg
;
1126 int clear_bits
, got_one
= 0;
1127 unsigned int msg_code
;
1129 if (input
->msg_thread
&& input
->msg_thread
!= thread
)
1130 return 0; /* locked by another thread */
1134 msg
= input
->msg_list
.first
;
1135 clear_bits
= QS_KEY
| QS_MOUSEMOVE
| QS_MOUSEBUTTON
;
1140 clear_bits
= 0; /* don't clear bits if we don't go through the whole list */
1145 win
= find_hardware_message_window( input
, msg
, &msg_code
);
1146 if (!win
|| !(win_thread
= get_window_thread( win
)))
1148 /* no window at all, remove it */
1149 struct message
*next
= msg
->next
;
1150 update_input_key_state( input
, msg
);
1151 unlink_message( &input
->msg_list
, msg
);
1152 free_message( msg
);
1156 if (win_thread
!= thread
)
1158 /* wake the other thread */
1159 set_queue_bits( win_thread
->queue
, get_hardware_msg_bit(msg
) );
1160 release_object( win_thread
);
1165 /* if we already got a message for another thread, or if it doesn't
1166 * match the filter we skip it (filter is only checked for keyboard
1167 * messages since the dest window for a mouse message depends on hittest)
1170 (filter_win
&& is_keyboard_msg(msg
) &&
1171 win
!= filter_win
&& !is_child_window( filter_win
, win
)))
1173 clear_bits
&= ~get_hardware_msg_bit( msg
);
1174 release_object( win_thread
);
1178 /* now we can return it */
1179 if (!input
->msg_thread
) input
->msg_thread
= win_thread
;
1180 else release_object( win_thread
);
1183 reply
->type
= MSG_HARDWARE
;
1185 reply
->msg
= msg_code
;
1186 reply
->wparam
= msg
->wparam
;
1187 reply
->lparam
= msg
->lparam
;
1190 reply
->time
= msg
->time
;
1191 reply
->info
= msg
->info
;
1194 /* nothing found, clear the hardware queue bits */
1195 clear_queue_bits( thread
->queue
, clear_bits
);
1196 if (input
->msg_thread
) release_object( input
->msg_thread
);
1198 input
->msg_thread
= NULL
;
1202 /* increment (or decrement if 'incr' is negative) the queue paint count */
1203 void inc_queue_paint_count( struct thread
*thread
, int incr
)
1205 struct msg_queue
*queue
= thread
->queue
;
1209 if ((queue
->paint_count
+= incr
) < 0) queue
->paint_count
= 0;
1211 if (queue
->paint_count
)
1212 set_queue_bits( queue
, QS_PAINT
);
1214 clear_queue_bits( queue
, QS_PAINT
);
1218 /* remove all messages and timers belonging to a certain window */
1219 void queue_cleanup_window( struct thread
*thread
, user_handle_t win
)
1221 struct msg_queue
*queue
= thread
->queue
;
1222 struct timer
*timer
;
1223 struct message
*msg
;
1229 timer
= queue
->first_timer
;
1232 struct timer
*next
= timer
->next
;
1233 if (timer
->win
== win
)
1235 unlink_timer( queue
, timer
);
1241 /* remove messages */
1242 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
1244 msg
= queue
->msg_list
[i
].first
;
1247 struct message
*next
= msg
->next
;
1248 if (msg
->win
== win
) remove_queue_message( queue
, msg
, i
);
1253 thread_input_cleanup_window( queue
, win
);
1256 /* post a message to a window; used by socket handling */
1257 void post_message( user_handle_t win
, unsigned int message
,
1258 unsigned int wparam
, unsigned int lparam
)
1260 struct message
*msg
;
1261 struct thread
*thread
= get_window_thread( win
);
1263 if (!thread
) return;
1265 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
1267 msg
->type
= MSG_POSTED
;
1268 msg
->win
= get_user_full_handle( win
);
1270 msg
->wparam
= wparam
;
1271 msg
->lparam
= lparam
;
1272 msg
->time
= get_tick_count();
1280 append_message( &thread
->queue
->msg_list
[POST_MESSAGE
], msg
);
1281 set_queue_bits( thread
->queue
, QS_POSTMESSAGE
);
1283 release_object( thread
);
1287 /* get the message queue of the current thread */
1288 DECL_HANDLER(get_msg_queue
)
1290 struct msg_queue
*queue
= get_current_queue();
1293 if (queue
) reply
->handle
= alloc_handle( current
->process
, queue
, SYNCHRONIZE
, 0 );
1297 /* set the current message queue wakeup mask */
1298 DECL_HANDLER(set_queue_mask
)
1300 struct msg_queue
*queue
= get_current_queue();
1304 queue
->wake_mask
= req
->wake_mask
;
1305 queue
->changed_mask
= req
->changed_mask
;
1306 reply
->wake_bits
= queue
->wake_bits
;
1307 reply
->changed_bits
= queue
->changed_bits
;
1308 if (is_signaled( queue
))
1310 /* if skip wait is set, do what would have been done in the subsequent wait */
1311 if (req
->skip_wait
) msg_queue_satisfied( &queue
->obj
, current
);
1312 else wake_up( &queue
->obj
, 0 );
1318 /* get the current message queue status */
1319 DECL_HANDLER(get_queue_status
)
1321 struct msg_queue
*queue
= current
->queue
;
1324 reply
->wake_bits
= queue
->wake_bits
;
1325 reply
->changed_bits
= queue
->changed_bits
;
1326 if (req
->clear
) queue
->changed_bits
= 0;
1328 else reply
->wake_bits
= reply
->changed_bits
= 0;
1332 /* send a message to a thread queue */
1333 DECL_HANDLER(send_message
)
1335 struct message
*msg
;
1336 struct msg_queue
*send_queue
= get_current_queue();
1337 struct msg_queue
*recv_queue
= NULL
;
1338 struct thread
*thread
= NULL
;
1342 if (!(thread
= get_thread_from_id( req
->id
))) return;
1344 else if (req
->type
!= MSG_HARDWARE
)
1346 /* only hardware messages are allowed without destination thread */
1347 set_error( STATUS_INVALID_PARAMETER
);
1351 if (thread
&& !(recv_queue
= thread
->queue
))
1353 set_error( STATUS_INVALID_PARAMETER
);
1354 release_object( thread
);
1357 if (recv_queue
&& (req
->flags
& SEND_MSG_ABORT_IF_HUNG
) && is_queue_hung(recv_queue
))
1359 set_error( STATUS_TIMEOUT
);
1360 release_object( thread
);
1364 if ((msg
= mem_alloc( sizeof(*msg
) )))
1366 msg
->type
= req
->type
;
1367 msg
->win
= get_user_full_handle( req
->win
);
1368 msg
->msg
= req
->msg
;
1369 msg
->wparam
= req
->wparam
;
1370 msg
->lparam
= req
->lparam
;
1371 msg
->time
= req
->time
;
1374 msg
->info
= req
->info
;
1381 case MSG_OTHER_PROCESS
:
1382 msg
->data_size
= get_req_data_size();
1383 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1392 if (!(msg
->result
= alloc_message_result( send_queue
, recv_queue
, msg
,
1393 req
->timeout
, req
->callback
, req
->info
)))
1395 free_message( msg
);
1400 append_message( &recv_queue
->msg_list
[SEND_MESSAGE
], msg
);
1401 set_queue_bits( recv_queue
, QS_SENDMESSAGE
);
1404 /* needed for posted DDE messages */
1405 msg
->data_size
= get_req_data_size();
1406 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1411 append_message( &recv_queue
->msg_list
[POST_MESSAGE
], msg
);
1412 set_queue_bits( recv_queue
, QS_POSTMESSAGE
);
1415 queue_hardware_message( recv_queue
, msg
);
1417 case MSG_CALLBACK_RESULT
: /* cannot send this one */
1419 set_error( STATUS_INVALID_PARAMETER
);
1424 if (thread
) release_object( thread
);
1428 /* get a message from the current queue */
1429 DECL_HANDLER(get_message
)
1431 struct timer
*timer
;
1432 struct message
*msg
;
1433 struct message
*first_hw_msg
= NULL
;
1434 struct msg_queue
*queue
= get_current_queue();
1435 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
1438 gettimeofday( &queue
->last_get_msg
, NULL
);
1440 /* first of all release the hardware input lock if we own it */
1441 /* we'll grab it again if we find a hardware message */
1442 if (queue
->input
->msg_thread
== current
)
1444 first_hw_msg
= queue
->input
->msg
;
1445 release_hardware_message( current
, 0 );
1448 /* first check for sent messages */
1449 if ((msg
= queue
->msg_list
[SEND_MESSAGE
].first
))
1451 receive_message( queue
, msg
, reply
);
1454 if (req
->flags
& GET_MSG_SENT_ONLY
) goto done
; /* nothing else to check */
1456 /* clear changed bits so we can wait on them if we don't find a message */
1457 queue
->changed_bits
= 0;
1459 /* then check for posted messages */
1460 if (get_posted_message( queue
, get_win
, req
->get_first
, req
->get_last
, req
->flags
, reply
))
1463 /* then check for any raw hardware message */
1464 if (get_hardware_message( current
, first_hw_msg
, get_win
, reply
))
1467 /* now check for WM_PAINT */
1468 if (queue
->paint_count
&&
1469 (WM_PAINT
>= req
->get_first
) && (WM_PAINT
<= req
->get_last
) &&
1470 (reply
->win
= find_window_to_repaint( get_win
, current
)))
1472 reply
->type
= MSG_POSTED
;
1473 reply
->msg
= WM_PAINT
;
1478 reply
->time
= get_tick_count();
1483 /* now check for timer */
1484 if ((timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
1485 req
->get_last
, (req
->flags
& GET_MSG_REMOVE
) )))
1487 reply
->type
= MSG_POSTED
;
1488 reply
->win
= timer
->win
;
1489 reply
->msg
= timer
->msg
;
1490 reply
->wparam
= timer
->id
;
1491 reply
->lparam
= timer
->lparam
;
1494 reply
->time
= get_tick_count();
1500 set_error( STATUS_PENDING
); /* FIXME */
1504 /* reply to a sent message */
1505 DECL_HANDLER(reply_message
)
1507 if (!current
->queue
)
1509 set_error( STATUS_ACCESS_DENIED
);
1512 if (req
->type
== MSG_HARDWARE
)
1514 struct thread_input
*input
= current
->queue
->input
;
1515 if (input
->msg_thread
== current
) release_hardware_message( current
, req
->remove
);
1516 else set_error( STATUS_ACCESS_DENIED
);
1518 else if (current
->queue
->recv_result
)
1519 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
1520 get_req_data(), get_req_data_size() );
1524 /* retrieve the reply for the last message sent */
1525 DECL_HANDLER(get_message_reply
)
1527 struct message_result
*result
;
1529 struct msg_queue
*queue
= current
->queue
;
1533 set_error( STATUS_PENDING
);
1536 if (!(entry
= list_head( &queue
->send_result
))) return; /* no reply ready */
1538 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1539 if (result
->replied
|| req
->cancel
)
1541 if (result
->replied
)
1543 reply
->result
= result
->result
;
1544 set_error( result
->error
);
1547 size_t data_len
= min( result
->data_size
, get_reply_max_size() );
1548 set_reply_data_ptr( result
->data
, data_len
);
1549 result
->data
= NULL
;
1550 result
->data_size
= 0;
1553 remove_result_from_sender( result
);
1555 entry
= list_head( &queue
->send_result
);
1556 if (!entry
) clear_queue_bits( queue
, QS_SMRESULT
);
1559 result
= LIST_ENTRY( entry
, struct message_result
, sender_entry
);
1560 if (!result
->replied
) clear_queue_bits( queue
, QS_SMRESULT
);
1564 else set_error( STATUS_ACCESS_DENIED
);
1568 /* set a window timer */
1569 DECL_HANDLER(set_win_timer
)
1571 struct timer
*timer
;
1572 struct msg_queue
*queue
= get_current_queue();
1573 user_handle_t win
= get_user_full_handle( req
->win
);
1577 /* remove it if it existed already */
1578 if (win
) kill_timer( queue
, win
, req
->msg
, req
->id
);
1580 if ((timer
= set_timer( queue
, req
->rate
)))
1583 timer
->msg
= req
->msg
;
1584 timer
->id
= req
->id
;
1585 timer
->lparam
= req
->lparam
;
1589 /* kill a window timer */
1590 DECL_HANDLER(kill_win_timer
)
1592 struct msg_queue
*queue
= current
->queue
;
1594 if (!queue
|| !kill_timer( queue
, get_user_full_handle(req
->win
), req
->msg
, req
->id
))
1595 set_error( STATUS_INVALID_PARAMETER
);
1599 /* attach (or detach) thread inputs */
1600 DECL_HANDLER(attach_thread_input
)
1602 struct thread
*thread_from
= get_thread_from_id( req
->tid_from
);
1603 struct thread
*thread_to
= get_thread_from_id( req
->tid_to
);
1605 if (!thread_from
|| !thread_to
)
1607 if (thread_from
) release_object( thread_from
);
1608 if (thread_to
) release_object( thread_to
);
1611 if (thread_from
!= thread_to
)
1613 if (req
->attach
) attach_thread_input( thread_from
, thread_to
);
1614 else detach_thread_input( thread_from
, thread_to
);
1616 else set_error( STATUS_ACCESS_DENIED
);
1617 release_object( thread_from
);
1618 release_object( thread_to
);
1622 /* get thread input data */
1623 DECL_HANDLER(get_thread_input
)
1625 struct thread
*thread
= NULL
;
1626 struct thread_input
*input
;
1630 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1631 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1633 else input
= foreground_input
; /* get the foreground thread info */
1637 reply
->focus
= input
->focus
;
1638 reply
->capture
= input
->capture
;
1639 reply
->active
= input
->active
;
1640 reply
->menu_owner
= input
->menu_owner
;
1641 reply
->move_size
= input
->move_size
;
1642 reply
->caret
= input
->caret
;
1643 reply
->rect
= input
->caret_rect
;
1650 reply
->menu_owner
= 0;
1651 reply
->move_size
= 0;
1653 reply
->rect
.left
= reply
->rect
.top
= reply
->rect
.right
= reply
->rect
.bottom
= 0;
1655 /* foreground window is active window of foreground thread */
1656 reply
->foreground
= foreground_input
? foreground_input
->active
: 0;
1657 if (thread
) release_object( thread
);
1661 /* retrieve queue keyboard state for a given thread */
1662 DECL_HANDLER(get_key_state
)
1664 struct thread
*thread
;
1665 struct thread_input
*input
;
1667 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1668 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1671 if (req
->key
>= 0) reply
->state
= input
->keystate
[req
->key
& 0xff];
1672 set_reply_data( input
->keystate
, min( get_reply_max_size(), sizeof(input
->keystate
) ));
1674 release_object( thread
);
1678 /* set queue keyboard state for a given thread */
1679 DECL_HANDLER(set_key_state
)
1681 struct thread
*thread
= NULL
;
1682 struct thread_input
*input
;
1684 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1685 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1688 size_t size
= min( sizeof(input
->keystate
), get_req_data_size() );
1689 if (size
) memcpy( input
->keystate
, get_req_data(), size
);
1691 release_object( thread
);
1695 /* set the system foreground window */
1696 DECL_HANDLER(set_foreground_window
)
1698 struct msg_queue
*queue
= get_current_queue();
1700 reply
->previous
= foreground_input
? foreground_input
->active
: 0;
1701 reply
->send_msg_old
= (reply
->previous
&& foreground_input
!= queue
->input
);
1702 reply
->send_msg_new
= FALSE
;
1706 struct thread
*thread
;
1708 if (is_top_level_window( req
->handle
) &&
1709 ((thread
= get_window_thread( req
->handle
))))
1711 foreground_input
= thread
->queue
->input
;
1712 reply
->send_msg_new
= (foreground_input
!= queue
->input
);
1713 release_object( thread
);
1715 else set_error( STATUS_INVALID_HANDLE
);
1717 else foreground_input
= NULL
;
1721 /* set the current thread focus window */
1722 DECL_HANDLER(set_focus_window
)
1724 struct msg_queue
*queue
= get_current_queue();
1726 reply
->previous
= 0;
1727 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1729 reply
->previous
= queue
->input
->focus
;
1730 queue
->input
->focus
= get_user_full_handle( req
->handle
);
1735 /* set the current thread active window */
1736 DECL_HANDLER(set_active_window
)
1738 struct msg_queue
*queue
= get_current_queue();
1740 reply
->previous
= 0;
1741 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1743 if (!req
->handle
|| make_window_active( req
->handle
))
1745 reply
->previous
= queue
->input
->active
;
1746 queue
->input
->active
= get_user_full_handle( req
->handle
);
1748 else set_error( STATUS_INVALID_HANDLE
);
1753 /* set the current thread capture window */
1754 DECL_HANDLER(set_capture_window
)
1756 struct msg_queue
*queue
= get_current_queue();
1758 reply
->previous
= reply
->full_handle
= 0;
1759 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1761 struct thread_input
*input
= queue
->input
;
1763 reply
->previous
= input
->capture
;
1764 input
->capture
= get_user_full_handle( req
->handle
);
1765 input
->menu_owner
= (req
->flags
& CAPTURE_MENU
) ? input
->capture
: 0;
1766 input
->move_size
= (req
->flags
& CAPTURE_MOVESIZE
) ? input
->capture
: 0;
1767 reply
->full_handle
= input
->capture
;
1772 /* Set the current thread caret window */
1773 DECL_HANDLER(set_caret_window
)
1775 struct msg_queue
*queue
= get_current_queue();
1777 reply
->previous
= 0;
1778 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1780 struct thread_input
*input
= queue
->input
;
1782 reply
->previous
= input
->caret
;
1783 reply
->old_rect
= input
->caret_rect
;
1784 reply
->old_hide
= input
->caret_hide
;
1785 reply
->old_state
= input
->caret_state
;
1787 set_caret_window( input
, get_user_full_handle(req
->handle
) );
1788 input
->caret_rect
.right
= req
->width
;
1789 input
->caret_rect
.bottom
= req
->height
;
1794 /* Set the current thread caret information */
1795 DECL_HANDLER(set_caret_info
)
1797 struct msg_queue
*queue
= get_current_queue();
1798 struct thread_input
*input
;
1801 input
= queue
->input
;
1802 reply
->full_handle
= input
->caret
;
1803 reply
->old_rect
= input
->caret_rect
;
1804 reply
->old_hide
= input
->caret_hide
;
1805 reply
->old_state
= input
->caret_state
;
1807 if (req
->handle
&& get_user_full_handle(req
->handle
) != input
->caret
)
1809 set_error( STATUS_ACCESS_DENIED
);
1812 if (req
->flags
& SET_CARET_POS
)
1814 input
->caret_rect
.right
+= req
->x
- input
->caret_rect
.left
;
1815 input
->caret_rect
.bottom
+= req
->y
- input
->caret_rect
.top
;
1816 input
->caret_rect
.left
= req
->x
;
1817 input
->caret_rect
.top
= req
->y
;
1819 if (req
->flags
& SET_CARET_HIDE
)
1821 input
->caret_hide
+= req
->hide
;
1822 if (input
->caret_hide
< 0) input
->caret_hide
= 0;
1824 if (req
->flags
& SET_CARET_STATE
)
1826 if (req
->state
== -1) input
->caret_state
= !input
->caret_state
;
1827 else input
->caret_state
= !!req
->state
;