2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
41 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
42 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
44 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
45 #define NB_MSG_KINDS (POST_MESSAGE+1)
50 struct list sender_entry; /* entry in sender list */
51 struct message_result *recv_next; /* next in receiver list */
52 struct msg_queue *sender; /* sender queue */
53 struct msg_queue *receiver; /* receiver queue */
54 int replied; /* has it been replied to? */
55 unsigned int result; /* reply result */
56 unsigned int error; /* error code to pass back to sender */
57 struct message *callback_msg; /* message to queue for callback */
58 void *data; /* message reply data */
59 unsigned int data_size; /* size of message reply data */
60 struct timeout_user *timeout; /* result timeout */
65 struct list entry; /* entry in message list */
66 enum message_type type; /* message type */
67 user_handle_t win; /* window handle */
68 unsigned int msg; /* message code */
69 unsigned int wparam; /* parameters */
70 unsigned int lparam; /* parameters */
71 int x; /* x position */
72 int y; /* y position */
73 unsigned int time; /* message time */
74 unsigned int info; /* extra info */
75 user_handle_t hook; /* winevent hook handle */
76 void *hook_proc; /* winevent hook proc address */
77 void *data; /* message data for sent messages */
78 unsigned int data_size; /* size of message data */
79 struct message_result *result; /* result in sender queue */
84 struct list entry; /* entry in timer list */
85 struct timeval when; /* next expiration */
86 unsigned int rate; /* timer rate in ms */
87 user_handle_t win; /* window handle */
88 unsigned int msg; /* message to post */
89 unsigned int id; /* timer id */
90 unsigned int lparam; /* lparam for message */
95 struct object obj; /* object header */
96 user_handle_t focus; /* focus window */
97 user_handle_t capture; /* capture window */
98 user_handle_t active; /* active window */
99 user_handle_t menu_owner; /* current menu owner window */
100 user_handle_t move_size; /* current moving/resizing window */
101 user_handle_t caret; /* caret window */
102 rectangle_t caret_rect; /* caret rectangle */
103 int caret_hide; /* caret hide count */
104 int caret_state; /* caret on/off state */
105 struct message *msg; /* message currently processed */
106 struct thread *msg_thread; /* thread processing the message */
107 struct list msg_list; /* list of hardware messages */
108 unsigned char keystate[256]; /* state of each key */
113 struct object obj; /* object header */
114 unsigned int wake_bits; /* wakeup bits */
115 unsigned int wake_mask; /* wakeup mask */
116 unsigned int changed_bits; /* changed wakeup bits */
117 unsigned int changed_mask; /* changed wakeup mask */
118 int paint_count; /* pending paint messages count */
119 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
120 struct list send_result; /* stack of sent messages waiting for result */
121 struct list callback_result; /* list of callback messages waiting for result */
122 struct message_result *recv_result; /* stack of received messages waiting for result */
123 struct list pending_timers; /* list of pending timers */
124 struct list expired_timers; /* list of expired timers */
125 unsigned int next_timer_id; /* id for the next timer with a 0 window */
126 struct timeout_user *timeout; /* timeout for next timer to expire */
127 struct thread_input *input; /* thread input descriptor */
128 struct hook_table *hooks; /* hook table */
129 struct timeval last_get_msg; /* time of last get message call */
132 static void msg_queue_dump( struct object *obj, int verbose );
133 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
134 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
135 static int msg_queue_signaled( struct object *obj, struct thread *thread );
136 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
137 static void msg_queue_destroy( struct object *obj );
138 static void thread_input_dump( struct object *obj, int verbose );
139 static void thread_input_destroy( struct object *obj );
140 static void timer_callback( void *private );
142 static const struct object_ops msg_queue_ops =
144 sizeof(struct msg_queue), /* size */
145 msg_queue_dump, /* dump */
146 msg_queue_add_queue, /* add_queue */
147 msg_queue_remove_queue, /* remove_queue */
148 msg_queue_signaled, /* signaled */
149 msg_queue_satisfied, /* satisfied */
150 no_get_fd, /* get_fd */
151 msg_queue_destroy /* destroy */
155 static const struct object_ops thread_input_ops =
157 sizeof(struct thread_input), /* size */
158 thread_input_dump, /* dump */
159 no_add_queue, /* add_queue */
160 NULL, /* remove_queue */
162 NULL, /* satisfied */
163 no_get_fd, /* get_fd */
164 thread_input_destroy /* destroy */
167 /* pointer to input structure of foreground thread */
168 static struct thread_input *foreground_input;
171 /* set the caret window in a given thread input */
172 static void set_caret_window( struct thread_input *input, user_handle_t win )
174 if (!win || win != input->caret)
176 input->caret_rect.left = 0;
177 input->caret_rect.top = 0;
178 input->caret_rect.right = 0;
179 input->caret_rect.bottom = 0;
182 input->caret_hide = 1;
183 input->caret_state = 0;
186 /* create a thread input object */
187 static struct thread_input *create_thread_input(void)
189 struct thread_input *input;
191 if ((input = alloc_object( &thread_input_ops )))
196 input->menu_owner = 0;
197 input->move_size = 0;
199 input->msg_thread = NULL;
200 list_init( &input->msg_list );
201 set_caret_window( input, 0 );
202 memset( input->keystate, 0, sizeof(input->keystate) );
207 /* release the thread input data of a given thread */
208 static void release_thread_input( struct thread *thread )
210 struct thread_input *input = thread->queue->input;
213 if (input->msg_thread == thread)
215 release_object( input->msg_thread );
216 input->msg_thread = NULL;
219 release_object( input );
220 thread->queue->input = NULL;
223 /* create a message queue object */
224 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
226 struct msg_queue *queue;
229 if (!input && !(input = create_thread_input())) return NULL;
230 if ((queue = alloc_object( &msg_queue_ops )))
232 queue->wake_bits = 0;
233 queue->wake_mask = 0;
234 queue->changed_bits = 0;
235 queue->changed_mask = 0;
236 queue->paint_count = 0;
237 queue->recv_result = NULL;
238 queue->next_timer_id = 1;
239 queue->timeout = NULL;
240 queue->input = (struct thread_input *)grab_object( input );
242 gettimeofday( &queue->last_get_msg, NULL );
243 list_init( &queue->send_result );
244 list_init( &queue->callback_result );
245 list_init( &queue->pending_timers );
246 list_init( &queue->expired_timers );
247 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
249 thread->queue = queue;
250 if (!thread->process->queue)
251 thread->process->queue = (struct msg_queue *)grab_object( queue );
253 release_object( input );
257 /* free the message queue of a thread at thread exit */
258 void free_msg_queue( struct thread *thread )
260 struct process *process = thread->process;
261 struct thread_input *input;
263 remove_thread_hooks( thread );
264 if (!thread->queue) return;
265 if (process->queue == thread->queue) /* is it the process main queue? */
267 release_object( process->queue );
268 process->queue = NULL;
269 if (process->idle_event)
271 set_event( process->idle_event );
272 release_object( process->idle_event );
273 process->idle_event = NULL;
276 input = thread->queue->input;
277 if (input->msg_thread == thread)
279 release_object( input->msg_thread );
280 input->msg_thread = NULL;
283 release_object( thread->queue );
284 thread->queue = NULL;
287 /* get the hook table for a given thread */
288 struct hook_table *get_queue_hooks( struct thread *thread )
290 if (!thread->queue) return NULL;
291 return thread->queue->hooks;
294 /* set the hook table for a given thread, allocating the queue if needed */
295 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
297 struct msg_queue *queue = thread->queue;
298 if (!queue) queue = create_msg_queue( thread, NULL );
299 if (queue->hooks) release_object( queue->hooks );
300 queue->hooks = hooks;
303 /* check the queue status */
304 inline static int is_signaled( struct msg_queue *queue )
306 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
309 /* set some queue bits */
310 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
312 queue->wake_bits |= bits;
313 queue->changed_bits |= bits;
314 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
317 /* clear some queue bits */
318 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
320 queue->wake_bits &= ~bits;
321 queue->changed_bits &= ~bits;
324 /* check whether msg is a keyboard message */
325 inline static int is_keyboard_msg( struct message *msg )
327 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
330 /* check whether a message filter contains at least one potential hardware message */
331 inline static int filter_contains_hw_range( unsigned int first, unsigned int last )
333 /* hardware message ranges are (in numerical order):
334 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
335 * WM_KEYFIRST .. WM_KEYLAST
336 * WM_MOUSEFIRST .. WM_MOUSELAST
338 if (last < WM_NCMOUSEFIRST) return 0;
339 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
340 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
341 if (first > WM_MOUSELAST) return 0;
345 /* get the QS_* bit corresponding to a given hardware message */
346 inline static int get_hardware_msg_bit( struct message *msg )
348 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
349 if (is_keyboard_msg( msg )) return QS_KEY;
350 return QS_MOUSEBUTTON;
353 /* get the current thread queue, creating it if needed */
354 inline static struct msg_queue *get_current_queue(void)
356 struct msg_queue *queue = current->queue;
357 if (!queue) queue = create_msg_queue( current, NULL );
361 /* try to merge a message with the last in the list; return 1 if successful */
362 static int merge_message( struct thread_input *input, const struct message *msg )
364 struct message *prev;
365 struct list *ptr = list_tail( &input->msg_list );
368 prev = LIST_ENTRY( ptr, struct message, entry );
369 if (input->msg == prev) return 0;
370 if (prev->result) return 0;
371 if (prev->win != msg->win) return 0;
372 if (prev->msg != msg->msg) return 0;
373 if (prev->type != msg->type) return 0;
374 /* now we can merge it */
375 prev->wparam = msg->wparam;
376 prev->lparam = msg->lparam;
379 prev->time = msg->time;
380 prev->info = msg->info;
384 /* free a result structure */
385 static void free_result( struct message_result *result )
387 if (result->timeout) remove_timeout_user( result->timeout );
388 if (result->data) free( result->data );
389 if (result->callback_msg) free( result->callback_msg );
393 /* remove the result from the sender list it is on */
394 static inline void remove_result_from_sender( struct message_result *result )
396 assert( result->sender );
398 list_remove( &result->sender_entry );
399 result->sender = NULL;
400 if (!result->receiver) free_result( result );
403 /* store the message result in the appropriate structure */
404 static void store_message_result( struct message_result *res, unsigned int result,
407 res->result = result;
412 remove_timeout_user( res->timeout );
417 if (res->callback_msg)
419 /* queue the callback message in the sender queue */
420 res->callback_msg->lparam = result;
421 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
422 set_queue_bits( res->sender, QS_SENDMESSAGE );
423 res->callback_msg = NULL;
424 remove_result_from_sender( res );
428 /* wake sender queue if waiting on this result */
429 if (list_head(&res->sender->send_result) == &res->sender_entry)
430 set_queue_bits( res->sender, QS_SMRESULT );
436 /* free a message when deleting a queue or window */
437 static void free_message( struct message *msg )
439 struct message_result *result = msg->result;
444 result->receiver = NULL;
445 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
447 else free_result( result );
449 if (msg->data) free( msg->data );
453 /* remove (and free) a message from a message list */
454 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
455 enum message_kind kind )
457 list_remove( &msg->entry );
461 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
464 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_POSTMESSAGE );
470 /* message timed out without getting a reply */
471 static void result_timeout( void *private )
473 struct message_result *result = private;
475 assert( !result->replied );
477 result->timeout = NULL;
478 store_message_result( result, 0, STATUS_TIMEOUT );
481 /* allocate and fill a message result structure */
482 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
483 struct msg_queue *recv_queue,
484 struct message *msg, unsigned int timeout,
485 void *callback, unsigned int callback_data )
487 struct message_result *result = mem_alloc( sizeof(*result) );
490 result->sender = send_queue;
491 result->receiver = recv_queue;
494 result->data_size = 0;
495 result->timeout = NULL;
497 if (msg->type == MSG_CALLBACK)
499 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
505 callback_msg->type = MSG_CALLBACK_RESULT;
506 callback_msg->win = msg->win;
507 callback_msg->msg = msg->msg;
508 callback_msg->wparam = (unsigned int)callback;
509 callback_msg->lparam = 0;
510 callback_msg->time = get_tick_count();
513 callback_msg->info = callback_data;
514 callback_msg->hook = 0;
515 callback_msg->hook_proc = NULL;
516 callback_msg->result = NULL;
517 callback_msg->data = NULL;
518 callback_msg->data_size = 0;
520 result->callback_msg = callback_msg;
521 list_add_head( &send_queue->callback_result, &result->sender_entry );
525 result->callback_msg = NULL;
526 list_add_head( &send_queue->send_result, &result->sender_entry );
532 gettimeofday( &when, 0 );
533 add_timeout( &when, timeout );
534 result->timeout = add_timeout_user( &when, result_timeout, result );
540 /* receive a message, removing it from the sent queue */
541 static void receive_message( struct msg_queue *queue, struct message *msg,
542 struct get_message_reply *reply )
544 struct message_result *result = msg->result;
546 reply->total = msg->data_size;
547 if (msg->data_size > get_reply_max_size())
549 set_error( STATUS_BUFFER_OVERFLOW );
552 reply->type = msg->type;
553 reply->win = msg->win;
554 reply->msg = msg->msg;
555 reply->wparam = msg->wparam;
556 reply->lparam = msg->lparam;
559 reply->time = msg->time;
560 reply->info = msg->info;
561 reply->hook = msg->hook;
562 reply->hook_proc = msg->hook_proc;
564 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
566 list_remove( &msg->entry );
567 /* put the result on the receiver result stack */
570 result->recv_next = queue->recv_result;
571 queue->recv_result = result;
574 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
577 /* set the result of the current received message */
578 static void reply_message( struct msg_queue *queue, unsigned int result,
579 unsigned int error, int remove, const void *data, size_t len )
581 struct message_result *res = queue->recv_result;
585 queue->recv_result = res->recv_next;
586 res->receiver = NULL;
587 if (!res->sender) /* no one waiting for it */
595 if (len && (res->data = memdup( data, len ))) res->data_size = len;
596 store_message_result( res, result, error );
600 /* retrieve a posted message */
601 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
602 unsigned int first, unsigned int last, unsigned int flags,
603 struct get_message_reply *reply )
607 /* check against the filters */
608 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
610 if (msg->msg == WM_QUIT) goto found; /* WM_QUIT is never filtered */
611 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
612 if (msg->msg < first) continue;
613 if (msg->msg > last) continue;
614 goto found; /* found one */
618 /* return it to the app */
620 reply->total = msg->data_size;
621 if (msg->data_size > get_reply_max_size())
623 set_error( STATUS_BUFFER_OVERFLOW );
626 reply->type = msg->type;
627 reply->win = msg->win;
628 reply->msg = msg->msg;
629 reply->wparam = msg->wparam;
630 reply->lparam = msg->lparam;
633 reply->time = msg->time;
634 reply->info = msg->info;
636 if (flags & GET_MSG_REMOVE)
640 set_reply_data_ptr( msg->data, msg->data_size );
644 remove_queue_message( queue, msg, POST_MESSAGE );
646 else if (msg->data) set_reply_data( msg->data, msg->data_size );
651 /* empty a message list and free all the messages */
652 static void empty_msg_list( struct list *list )
656 while ((ptr = list_head( list )) != NULL)
658 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
659 list_remove( &msg->entry );
664 /* cleanup all pending results when deleting a queue */
665 static void cleanup_results( struct msg_queue *queue )
669 while ((entry = list_head( &queue->send_result )) != NULL)
671 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
674 while ((entry = list_head( &queue->callback_result )) != NULL)
676 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
679 while (queue->recv_result)
680 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
683 /* check if the thread owning the queue is hung (not checking for messages) */
684 static int is_queue_hung( struct msg_queue *queue )
687 struct wait_queue_entry *entry;
689 gettimeofday( &now, NULL );
690 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
691 return 0; /* less than 5 seconds since last get message -> not hung */
693 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
695 if (entry->thread->queue == queue)
696 return 0; /* thread is waiting on queue -> not hung */
701 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
703 struct msg_queue *queue = (struct msg_queue *)obj;
704 struct process *process = entry->thread->process;
706 /* a thread can only wait on its own queue */
707 if (entry->thread->queue != queue)
709 set_error( STATUS_ACCESS_DENIED );
712 /* if waiting on the main process queue, set the idle event */
713 if (process->queue == queue)
715 if (process->idle_event) set_event( process->idle_event );
717 add_queue( obj, entry );
721 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
723 struct msg_queue *queue = (struct msg_queue *)obj;
724 struct process *process = entry->thread->process;
726 remove_queue( obj, entry );
728 assert( entry->thread->queue == queue );
730 /* if waiting on the main process queue, reset the idle event */
731 if (process->queue == queue)
733 if (process->idle_event) reset_event( process->idle_event );
737 static void msg_queue_dump( struct object *obj, int verbose )
739 struct msg_queue *queue = (struct msg_queue *)obj;
740 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
741 queue->wake_bits, queue->wake_mask );
744 static int msg_queue_signaled( struct object *obj, struct thread *thread )
746 struct msg_queue *queue = (struct msg_queue *)obj;
747 return is_signaled( queue );
750 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
752 struct msg_queue *queue = (struct msg_queue *)obj;
753 queue->wake_mask = 0;
754 queue->changed_mask = 0;
755 return 0; /* Not abandoned */
758 static void msg_queue_destroy( struct object *obj )
760 struct msg_queue *queue = (struct msg_queue *)obj;
764 cleanup_results( queue );
765 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
767 while ((ptr = list_head( &queue->pending_timers )))
769 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
770 list_remove( &timer->entry );
773 while ((ptr = list_head( &queue->expired_timers )))
775 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
776 list_remove( &timer->entry );
779 if (queue->timeout) remove_timeout_user( queue->timeout );
780 if (queue->input) release_object( queue->input );
781 if (queue->hooks) release_object( queue->hooks );
784 static void thread_input_dump( struct object *obj, int verbose )
786 struct thread_input *input = (struct thread_input *)obj;
787 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
788 input->focus, input->capture, input->active );
791 static void thread_input_destroy( struct object *obj )
793 struct thread_input *input = (struct thread_input *)obj;
795 if (foreground_input == input) foreground_input = NULL;
796 if (input->msg_thread) release_object( input->msg_thread );
797 empty_msg_list( &input->msg_list );
800 /* fix the thread input data when a window is destroyed */
801 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
803 struct thread_input *input = queue->input;
805 if (window == input->focus) input->focus = 0;
806 if (window == input->capture) input->capture = 0;
807 if (window == input->active) input->active = 0;
808 if (window == input->menu_owner) input->menu_owner = 0;
809 if (window == input->move_size) input->move_size = 0;
810 if (window == input->caret) set_caret_window( input, 0 );
813 /* check if the specified window can be set in the input data of a given queue */
814 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
816 struct thread *thread;
819 if (!window) return 1; /* we can always clear the data */
821 if ((thread = get_window_thread( window )))
823 ret = (queue->input == thread->queue->input);
824 if (!ret) set_error( STATUS_ACCESS_DENIED );
825 release_object( thread );
827 else set_error( STATUS_INVALID_HANDLE );
832 /* attach two thread input data structures */
833 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
835 struct thread_input *input;
837 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
838 input = (struct thread_input *)grab_object( thread_to->queue->input );
840 if (thread_from->queue)
842 release_thread_input( thread_from );
843 thread_from->queue->input = input;
847 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
849 memset( input->keystate, 0, sizeof(input->keystate) );
853 /* detach two thread input data structures */
854 static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
856 struct thread_input *input;
858 if (!thread_from->queue || !thread_to->queue ||
859 thread_from->queue->input != thread_to->queue->input)
861 set_error( STATUS_ACCESS_DENIED );
864 if ((input = create_thread_input()))
866 release_thread_input( thread_from );
867 thread_from->queue->input = input;
872 /* set the next timer to expire */
873 static void set_next_timer( struct msg_queue *queue )
879 remove_timeout_user( queue->timeout );
880 queue->timeout = NULL;
882 if ((ptr = list_head( &queue->pending_timers )))
884 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
885 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
887 /* set/clear QS_TIMER bit */
888 if (list_empty( &queue->expired_timers ))
889 clear_queue_bits( queue, QS_TIMER );
891 set_queue_bits( queue, QS_TIMER );
894 /* find a timer from its window and id */
895 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
896 unsigned int msg, unsigned int id )
900 /* we need to search both lists */
902 LIST_FOR_EACH( ptr, &queue->pending_timers )
904 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
905 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
907 LIST_FOR_EACH( ptr, &queue->expired_timers )
909 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
910 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
915 /* callback for the next timer expiration */
916 static void timer_callback( void *private )
918 struct msg_queue *queue = private;
921 queue->timeout = NULL;
922 /* move on to the next timer */
923 ptr = list_head( &queue->pending_timers );
925 list_add_tail( &queue->expired_timers, ptr );
926 set_next_timer( queue );
929 /* link a timer at its rightful place in the queue list */
930 static void link_timer( struct msg_queue *queue, struct timer *timer )
934 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
936 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
937 if (!time_before( &t->when, &timer->when )) break;
939 list_add_before( ptr, &timer->entry );
942 /* remove a timer from the queue timer list and free it */
943 static void free_timer( struct msg_queue *queue, struct timer *timer )
945 list_remove( &timer->entry );
947 set_next_timer( queue );
950 /* restart an expired timer */
951 static void restart_timer( struct msg_queue *queue, struct timer *timer )
955 list_remove( &timer->entry );
956 gettimeofday( &now, 0 );
957 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
958 link_timer( queue, timer );
959 set_next_timer( queue );
962 /* find an expired timer matching the filtering parameters */
963 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
964 unsigned int get_first, unsigned int get_last,
969 LIST_FOR_EACH( ptr, &queue->expired_timers )
971 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
972 if (win && timer->win != win) continue;
973 if (timer->msg >= get_first && timer->msg <= get_last)
975 if (remove) restart_timer( queue, timer );
983 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
985 struct timer *timer = mem_alloc( sizeof(*timer) );
988 timer->rate = max( rate, 1 );
989 gettimeofday( &timer->when, 0 );
990 add_timeout( &timer->when, rate );
991 link_timer( queue, timer );
992 /* check if we replaced the next timer */
993 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
998 /* change the input key state for a given key */
999 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1003 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1004 input->keystate[key] |= 0x80;
1006 else input->keystate[key] &= ~0x80;
1009 /* update the input key state for a keyboard message */
1010 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1013 int down = 0, extended;
1017 case WM_LBUTTONDOWN:
1021 set_input_key_state( input, VK_LBUTTON, down );
1023 case WM_MBUTTONDOWN:
1027 set_input_key_state( input, VK_MBUTTON, down );
1029 case WM_RBUTTONDOWN:
1033 set_input_key_state( input, VK_RBUTTON, down );
1035 case WM_XBUTTONDOWN:
1039 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1040 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1048 key = (unsigned char)msg->wparam;
1049 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1050 set_input_key_state( input, key, down );
1054 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1057 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1060 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1067 /* release the hardware message currently being processed by the given thread */
1068 static void release_hardware_message( struct thread *thread, int remove, user_handle_t new_win )
1070 struct thread_input *input = thread->queue->input;
1072 if (input->msg_thread != thread) return;
1074 /* clear the queue bit for that message */
1075 if (remove || new_win)
1077 struct message *other;
1080 clr_bit = get_hardware_msg_bit( input->msg );
1081 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1083 if (other != input->msg && get_hardware_msg_bit( other ) == clr_bit)
1089 if (clr_bit) clear_queue_bits( thread->queue, clr_bit );
1092 if (new_win) /* set the new window */
1094 struct thread *owner = get_window_thread( new_win );
1097 input->msg->win = new_win;
1098 set_queue_bits( owner->queue, get_hardware_msg_bit( input->msg ));
1099 release_object( owner );
1101 if (!remove) return; /* don't release the message */
1105 update_input_key_state( input, input->msg );
1106 list_remove( &input->msg->entry );
1107 free_message( input->msg );
1109 release_object( input->msg_thread );
1111 input->msg_thread = NULL;
1114 /* find the window that should receive a given hardware message */
1115 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1116 unsigned int *msg_code )
1118 user_handle_t win = 0;
1120 *msg_code = msg->msg;
1121 if (is_keyboard_msg( msg ))
1123 if (input && !(win = input->focus))
1125 win = input->active;
1126 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1129 else /* mouse message */
1131 if (!input || !(win = input->capture))
1133 if (!(win = msg->win) || !is_window_visible( win ))
1134 win = window_from_point( msg->x, msg->y );
1140 /* queue a hardware message into a given thread input */
1141 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1144 struct thread *thread;
1145 struct thread_input *input;
1146 unsigned int msg_code;
1148 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1149 if (!win || !(thread = get_window_thread(win)))
1154 input = thread->queue->input;
1156 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1159 list_add_tail( &input->msg_list, &msg->entry );
1160 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1162 release_object( thread );
1165 /* find a hardware message for the given queue */
1166 static int get_hardware_message( struct thread *thread, int get_next_hw, struct message *first,
1167 user_handle_t filter_win, struct get_message_reply *reply )
1169 struct thread_input *input = thread->queue->input;
1170 struct thread *win_thread;
1173 int clear_bits, got_one = 0;
1174 unsigned int msg_code;
1176 if (input->msg_thread && ((input->msg_thread != thread) || !get_next_hw))
1177 return 0; /* locked by another thread, or another recursion level of the same thread */
1181 ptr = list_head( &input->msg_list );
1182 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1186 ptr = list_next( &input->msg_list, &first->entry );
1187 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1192 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1193 win = find_hardware_message_window( input, msg, &msg_code );
1194 if (!win || !(win_thread = get_window_thread( win )))
1196 /* no window at all, remove it */
1197 ptr = list_next( &input->msg_list, ptr );
1198 update_input_key_state( input, msg );
1199 list_remove( &msg->entry );
1200 free_message( msg );
1203 if (win_thread != thread)
1205 /* wake the other thread */
1206 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1207 release_object( win_thread );
1209 ptr = list_next( &input->msg_list, ptr );
1212 /* if we already got a message for another thread, or if it doesn't
1213 * match the filter we skip it (filter is only checked for keyboard
1214 * messages since the dest window for a mouse message depends on hittest)
1217 (filter_win && is_keyboard_msg(msg) &&
1218 win != filter_win && !is_child_window( filter_win, win )))
1220 clear_bits &= ~get_hardware_msg_bit( msg );
1221 release_object( win_thread );
1222 ptr = list_next( &input->msg_list, ptr );
1225 /* now we can return it */
1226 if (!input->msg_thread) input->msg_thread = win_thread;
1227 else release_object( win_thread );
1230 reply->type = MSG_HARDWARE;
1232 reply->msg = msg_code;
1233 reply->wparam = msg->wparam;
1234 reply->lparam = msg->lparam;
1237 reply->time = msg->time;
1238 reply->info = msg->info;
1241 /* nothing found, clear the hardware queue bits */
1242 clear_queue_bits( thread->queue, clear_bits );
1243 if (input->msg_thread) release_object( input->msg_thread );
1245 input->msg_thread = NULL;
1249 /* increment (or decrement if 'incr' is negative) the queue paint count */
1250 void inc_queue_paint_count( struct thread *thread, int incr )
1252 struct msg_queue *queue = thread->queue;
1256 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1258 if (queue->paint_count)
1259 set_queue_bits( queue, QS_PAINT );
1261 clear_queue_bits( queue, QS_PAINT );
1265 /* remove all messages and timers belonging to a certain window */
1266 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1268 struct msg_queue *queue = thread->queue;
1276 ptr = list_head( &queue->pending_timers );
1279 struct list *next = list_next( &queue->pending_timers, ptr );
1280 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1281 if (timer->win == win) free_timer( queue, timer );
1284 ptr = list_head( &queue->expired_timers );
1287 struct list *next = list_next( &queue->expired_timers, ptr );
1288 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1289 if (timer->win == win) free_timer( queue, timer );
1293 /* remove messages */
1294 for (i = 0; i < NB_MSG_KINDS; i++)
1296 struct list *ptr, *next;
1298 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1300 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1301 if (msg->win == win) remove_queue_message( queue, msg, i );
1305 thread_input_cleanup_window( queue, win );
1308 /* post a message to a window; used by socket handling */
1309 void post_message( user_handle_t win, unsigned int message,
1310 unsigned int wparam, unsigned int lparam )
1312 struct message *msg;
1313 struct thread *thread = get_window_thread( win );
1315 if (!thread) return;
1317 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1319 msg->type = MSG_POSTED;
1320 msg->win = get_user_full_handle( win );
1322 msg->wparam = wparam;
1323 msg->lparam = lparam;
1324 msg->time = get_tick_count();
1329 msg->hook_proc = NULL;
1334 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1335 set_queue_bits( thread->queue, QS_POSTMESSAGE );
1337 release_object( thread );
1340 /* post a win event */
1341 void post_win_event( struct thread *thread, unsigned int event,
1342 user_handle_t win, unsigned int object_id,
1343 unsigned int child_id, void *hook_proc,
1344 const WCHAR *module, size_t module_size,
1347 struct message *msg;
1349 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1351 msg->type = MSG_WINEVENT;
1352 msg->win = get_user_full_handle( win );
1354 msg->wparam = object_id;
1355 msg->lparam = child_id;
1356 msg->time = get_tick_count();
1359 msg->info = get_thread_id( current );
1362 msg->hook_proc = hook_proc;
1364 if ((msg->data = malloc( module_size )))
1366 msg->data_size = module_size;
1367 memcpy( msg->data, module, module_size );
1369 if (debug_level > 1)
1370 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1371 get_thread_id(thread), event, win, object_id, child_id );
1372 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1373 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1380 /* get the message queue of the current thread */
1381 DECL_HANDLER(get_msg_queue)
1383 struct msg_queue *queue = get_current_queue();
1386 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1390 /* set the current message queue wakeup mask */
1391 DECL_HANDLER(set_queue_mask)
1393 struct msg_queue *queue = get_current_queue();
1397 queue->wake_mask = req->wake_mask;
1398 queue->changed_mask = req->changed_mask;
1399 reply->wake_bits = queue->wake_bits;
1400 reply->changed_bits = queue->changed_bits;
1401 if (is_signaled( queue ))
1403 /* if skip wait is set, do what would have been done in the subsequent wait */
1404 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1405 else wake_up( &queue->obj, 0 );
1411 /* get the current message queue status */
1412 DECL_HANDLER(get_queue_status)
1414 struct msg_queue *queue = current->queue;
1417 reply->wake_bits = queue->wake_bits;
1418 reply->changed_bits = queue->changed_bits;
1419 if (req->clear) queue->changed_bits = 0;
1421 else reply->wake_bits = reply->changed_bits = 0;
1425 /* send a message to a thread queue */
1426 DECL_HANDLER(send_message)
1428 struct message *msg;
1429 struct msg_queue *send_queue = get_current_queue();
1430 struct msg_queue *recv_queue = NULL;
1431 struct thread *thread = NULL;
1435 if (!(thread = get_thread_from_id( req->id ))) return;
1437 else if (req->type != MSG_HARDWARE)
1439 /* only hardware messages are allowed without destination thread */
1440 set_error( STATUS_INVALID_PARAMETER );
1444 if (thread && !(recv_queue = thread->queue))
1446 set_error( STATUS_INVALID_PARAMETER );
1447 release_object( thread );
1450 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1452 set_error( STATUS_TIMEOUT );
1453 release_object( thread );
1457 if ((msg = mem_alloc( sizeof(*msg) )))
1459 msg->type = req->type;
1460 msg->win = get_user_full_handle( req->win );
1461 msg->msg = req->msg;
1462 msg->wparam = req->wparam;
1463 msg->lparam = req->lparam;
1464 msg->time = req->time;
1467 msg->info = req->info;
1469 msg->hook_proc = NULL;
1476 case MSG_OTHER_PROCESS:
1477 msg->data_size = get_req_data_size();
1478 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1487 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1488 req->timeout, req->callback, req->info )))
1490 free_message( msg );
1495 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1496 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1499 /* needed for posted DDE messages */
1500 msg->data_size = get_req_data_size();
1501 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1506 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1507 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1510 queue_hardware_message( recv_queue, msg );
1512 case MSG_CALLBACK_RESULT: /* cannot send this one */
1514 set_error( STATUS_INVALID_PARAMETER );
1519 if (thread) release_object( thread );
1523 /* get a message from the current queue */
1524 DECL_HANDLER(get_message)
1526 struct timer *timer;
1528 struct message *first_hw_msg = NULL;
1529 struct msg_queue *queue = get_current_queue();
1530 user_handle_t get_win = get_user_full_handle( req->get_win );
1533 gettimeofday( &queue->last_get_msg, NULL );
1535 /* first of all release the hardware input lock if we own it */
1536 /* we'll grab it again if we find a hardware message */
1537 if (queue->input->msg_thread == current && req->get_next_hw)
1539 first_hw_msg = queue->input->msg;
1540 release_hardware_message( current, 0, 0 );
1543 /* first check for sent messages */
1544 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1546 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1547 receive_message( queue, msg, reply );
1550 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1552 /* clear changed bits so we can wait on them if we don't find a message */
1553 queue->changed_bits = 0;
1555 /* then check for posted messages */
1556 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1559 /* then check for any raw hardware message */
1560 if (filter_contains_hw_range( req->get_first, req->get_last ) &&
1561 get_hardware_message( current, req->get_next_hw, first_hw_msg, get_win, reply ))
1564 /* now check for WM_PAINT */
1565 if (queue->paint_count &&
1566 (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
1567 (reply->win = find_window_to_repaint( get_win, current )))
1569 reply->type = MSG_POSTED;
1570 reply->msg = WM_PAINT;
1575 reply->time = get_tick_count();
1580 /* now check for timer */
1581 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1582 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1584 reply->type = MSG_POSTED;
1585 reply->win = timer->win;
1586 reply->msg = timer->msg;
1587 reply->wparam = timer->id;
1588 reply->lparam = timer->lparam;
1591 reply->time = get_tick_count();
1597 set_error( STATUS_PENDING ); /* FIXME */
1601 /* reply to a sent message */
1602 DECL_HANDLER(reply_message)
1604 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1605 else if (current->queue->recv_result)
1606 reply_message( current->queue, req->result, 0, req->remove,
1607 get_req_data(), get_req_data_size() );
1611 /* accept the current hardware message */
1612 DECL_HANDLER(accept_hardware_message)
1616 struct thread_input *input = current->queue->input;
1617 if (input->msg_thread == current)
1619 release_hardware_message( current, req->remove, req->new_win );
1623 set_error( STATUS_ACCESS_DENIED );
1627 /* retrieve the reply for the last message sent */
1628 DECL_HANDLER(get_message_reply)
1630 struct message_result *result;
1632 struct msg_queue *queue = current->queue;
1636 set_error( STATUS_PENDING );
1639 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1641 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1642 if (result->replied || req->cancel)
1644 if (result->replied)
1646 reply->result = result->result;
1647 set_error( result->error );
1650 size_t data_len = min( result->data_size, get_reply_max_size() );
1651 set_reply_data_ptr( result->data, data_len );
1652 result->data = NULL;
1653 result->data_size = 0;
1656 remove_result_from_sender( result );
1658 entry = list_head( &queue->send_result );
1659 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1662 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1663 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1667 else set_error( STATUS_ACCESS_DENIED );
1671 /* set a window timer */
1672 DECL_HANDLER(set_win_timer)
1674 struct timer *timer;
1675 struct msg_queue *queue;
1676 struct thread *thread = NULL;
1677 user_handle_t win = 0;
1678 unsigned int id = req->id;
1682 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1684 set_error( STATUS_INVALID_HANDLE );
1687 if (thread->process != current->process)
1689 release_object( thread );
1690 set_error( STATUS_ACCESS_DENIED );
1693 queue = thread->queue;
1694 /* remove it if it existed already */
1695 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1699 queue = get_current_queue();
1700 /* find a free id for it */
1703 id = queue->next_timer_id;
1704 if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1706 while (find_timer( queue, 0, req->msg, id ));
1709 if ((timer = set_timer( queue, req->rate )))
1712 timer->msg = req->msg;
1714 timer->lparam = req->lparam;
1717 if (thread) release_object( thread );
1720 /* kill a window timer */
1721 DECL_HANDLER(kill_win_timer)
1723 struct timer *timer;
1724 struct thread *thread;
1725 user_handle_t win = 0;
1729 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1731 set_error( STATUS_INVALID_HANDLE );
1734 if (thread->process != current->process)
1736 release_object( thread );
1737 set_error( STATUS_ACCESS_DENIED );
1741 else thread = (struct thread *)grab_object( current );
1743 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1744 free_timer( thread->queue, timer );
1746 set_error( STATUS_INVALID_PARAMETER );
1748 release_object( thread );
1752 /* attach (or detach) thread inputs */
1753 DECL_HANDLER(attach_thread_input)
1755 struct thread *thread_from = get_thread_from_id( req->tid_from );
1756 struct thread *thread_to = get_thread_from_id( req->tid_to );
1758 if (!thread_from || !thread_to)
1760 if (thread_from) release_object( thread_from );
1761 if (thread_to) release_object( thread_to );
1764 if (thread_from != thread_to)
1766 if (req->attach) attach_thread_input( thread_from, thread_to );
1767 else detach_thread_input( thread_from, thread_to );
1769 else set_error( STATUS_ACCESS_DENIED );
1770 release_object( thread_from );
1771 release_object( thread_to );
1775 /* get thread input data */
1776 DECL_HANDLER(get_thread_input)
1778 struct thread *thread = NULL;
1779 struct thread_input *input;
1783 if (!(thread = get_thread_from_id( req->tid ))) return;
1784 input = thread->queue ? thread->queue->input : NULL;
1786 else input = foreground_input; /* get the foreground thread info */
1790 reply->focus = input->focus;
1791 reply->capture = input->capture;
1792 reply->active = input->active;
1793 reply->menu_owner = input->menu_owner;
1794 reply->move_size = input->move_size;
1795 reply->caret = input->caret;
1796 reply->rect = input->caret_rect;
1803 reply->menu_owner = 0;
1804 reply->move_size = 0;
1806 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1808 /* foreground window is active window of foreground thread */
1809 reply->foreground = foreground_input ? foreground_input->active : 0;
1810 if (thread) release_object( thread );
1814 /* retrieve queue keyboard state for a given thread */
1815 DECL_HANDLER(get_key_state)
1817 struct thread *thread;
1818 struct thread_input *input;
1820 if (!(thread = get_thread_from_id( req->tid ))) return;
1821 input = thread->queue ? thread->queue->input : NULL;
1824 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1825 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1827 release_object( thread );
1831 /* set queue keyboard state for a given thread */
1832 DECL_HANDLER(set_key_state)
1834 struct thread *thread = NULL;
1835 struct thread_input *input;
1837 if (!(thread = get_thread_from_id( req->tid ))) return;
1838 input = thread->queue ? thread->queue->input : NULL;
1841 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1842 if (size) memcpy( input->keystate, get_req_data(), size );
1844 release_object( thread );
1848 /* set the system foreground window */
1849 DECL_HANDLER(set_foreground_window)
1851 struct msg_queue *queue = get_current_queue();
1853 reply->previous = foreground_input ? foreground_input->active : 0;
1854 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1855 reply->send_msg_new = FALSE;
1859 struct thread *thread;
1861 if (is_top_level_window( req->handle ) &&
1862 ((thread = get_window_thread( req->handle ))))
1864 foreground_input = thread->queue->input;
1865 reply->send_msg_new = (foreground_input != queue->input);
1866 release_object( thread );
1868 else set_error( STATUS_INVALID_HANDLE );
1870 else foreground_input = NULL;
1874 /* set the current thread focus window */
1875 DECL_HANDLER(set_focus_window)
1877 struct msg_queue *queue = get_current_queue();
1879 reply->previous = 0;
1880 if (queue && check_queue_input_window( queue, req->handle ))
1882 reply->previous = queue->input->focus;
1883 queue->input->focus = get_user_full_handle( req->handle );
1888 /* set the current thread active window */
1889 DECL_HANDLER(set_active_window)
1891 struct msg_queue *queue = get_current_queue();
1893 reply->previous = 0;
1894 if (queue && check_queue_input_window( queue, req->handle ))
1896 if (!req->handle || make_window_active( req->handle ))
1898 reply->previous = queue->input->active;
1899 queue->input->active = get_user_full_handle( req->handle );
1901 else set_error( STATUS_INVALID_HANDLE );
1906 /* set the current thread capture window */
1907 DECL_HANDLER(set_capture_window)
1909 struct msg_queue *queue = get_current_queue();
1911 reply->previous = reply->full_handle = 0;
1912 if (queue && check_queue_input_window( queue, req->handle ))
1914 struct thread_input *input = queue->input;
1916 reply->previous = input->capture;
1917 input->capture = get_user_full_handle( req->handle );
1918 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1919 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1920 reply->full_handle = input->capture;
1925 /* Set the current thread caret window */
1926 DECL_HANDLER(set_caret_window)
1928 struct msg_queue *queue = get_current_queue();
1930 reply->previous = 0;
1931 if (queue && check_queue_input_window( queue, req->handle ))
1933 struct thread_input *input = queue->input;
1935 reply->previous = input->caret;
1936 reply->old_rect = input->caret_rect;
1937 reply->old_hide = input->caret_hide;
1938 reply->old_state = input->caret_state;
1940 set_caret_window( input, get_user_full_handle(req->handle) );
1941 input->caret_rect.right = input->caret_rect.left + req->width;
1942 input->caret_rect.bottom = input->caret_rect.top + req->height;
1947 /* Set the current thread caret information */
1948 DECL_HANDLER(set_caret_info)
1950 struct msg_queue *queue = get_current_queue();
1951 struct thread_input *input;
1954 input = queue->input;
1955 reply->full_handle = input->caret;
1956 reply->old_rect = input->caret_rect;
1957 reply->old_hide = input->caret_hide;
1958 reply->old_state = input->caret_state;
1960 if (req->handle && get_user_full_handle(req->handle) != input->caret)
1962 set_error( STATUS_ACCESS_DENIED );
1965 if (req->flags & SET_CARET_POS)
1967 input->caret_rect.right += req->x - input->caret_rect.left;
1968 input->caret_rect.bottom += req->y - input->caret_rect.top;
1969 input->caret_rect.left = req->x;
1970 input->caret_rect.top = req->y;
1972 if (req->flags & SET_CARET_HIDE)
1974 input->caret_hide += req->hide;
1975 if (input->caret_hide < 0) input->caret_hide = 0;
1977 if (req->flags & SET_CARET_STATE)
1979 if (req->state == -1) input->caret_state = !input->caret_state;
1980 else input->caret_state = !!req->state;