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 list entry; /* entry in message list */
63 enum message_type type; /* message type */
64 user_handle_t win; /* window handle */
65 unsigned int msg; /* message code */
66 unsigned int wparam; /* parameters */
67 unsigned int lparam; /* parameters */
68 int x; /* x position */
69 int y; /* y position */
70 unsigned int time; /* message time */
71 unsigned int info; /* extra info */
72 user_handle_t hook; /* winevent hook handle */
73 void *hook_proc; /* winevent hook proc address */
74 void *data; /* message data for sent messages */
75 unsigned int data_size; /* size of message data */
76 struct message_result *result; /* result in sender queue */
81 struct list entry; /* entry in timer list */
82 struct timeval when; /* next expiration */
83 unsigned int rate; /* timer rate in ms */
84 user_handle_t win; /* window handle */
85 unsigned int msg; /* message to post */
86 unsigned int id; /* timer id */
87 unsigned int lparam; /* lparam for message */
92 struct object obj; /* object header */
93 user_handle_t focus; /* focus window */
94 user_handle_t capture; /* capture window */
95 user_handle_t active; /* active window */
96 user_handle_t menu_owner; /* current menu owner window */
97 user_handle_t move_size; /* current moving/resizing window */
98 user_handle_t caret; /* caret window */
99 rectangle_t caret_rect; /* caret rectangle */
100 int caret_hide; /* caret hide count */
101 int caret_state; /* caret on/off state */
102 struct message *msg; /* message currently processed */
103 struct thread *msg_thread; /* thread processing the message */
104 struct list msg_list; /* list of hardware messages */
105 unsigned char keystate[256]; /* state of each key */
110 struct object obj; /* object header */
111 unsigned int wake_bits; /* wakeup bits */
112 unsigned int wake_mask; /* wakeup mask */
113 unsigned int changed_bits; /* changed wakeup bits */
114 unsigned int changed_mask; /* changed wakeup mask */
115 int paint_count; /* pending paint messages count */
116 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
117 struct list send_result; /* stack of sent messages waiting for result */
118 struct list callback_result; /* list of callback messages waiting for result */
119 struct message_result *recv_result; /* stack of received messages waiting for result */
120 struct list pending_timers; /* list of pending timers */
121 struct list expired_timers; /* list of expired timers */
122 unsigned int next_timer_id; /* id for the next timer with a 0 window */
123 struct timeout_user *timeout; /* timeout for next timer to expire */
124 struct thread_input *input; /* thread input descriptor */
125 struct hook_table *hooks; /* hook table */
126 struct timeval last_get_msg; /* time of last get message call */
129 static void msg_queue_dump( struct object *obj, int verbose );
130 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
131 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
132 static int msg_queue_signaled( struct object *obj, struct thread *thread );
133 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
134 static void msg_queue_destroy( struct object *obj );
135 static void thread_input_dump( struct object *obj, int verbose );
136 static void thread_input_destroy( struct object *obj );
137 static void timer_callback( void *private );
139 static const struct object_ops msg_queue_ops =
141 sizeof(struct msg_queue), /* size */
142 msg_queue_dump, /* dump */
143 msg_queue_add_queue, /* add_queue */
144 msg_queue_remove_queue, /* remove_queue */
145 msg_queue_signaled, /* signaled */
146 msg_queue_satisfied, /* satisfied */
147 no_get_fd, /* get_fd */
148 msg_queue_destroy /* destroy */
152 static const struct object_ops thread_input_ops =
154 sizeof(struct thread_input), /* size */
155 thread_input_dump, /* dump */
156 no_add_queue, /* add_queue */
157 NULL, /* remove_queue */
159 NULL, /* satisfied */
160 no_get_fd, /* get_fd */
161 thread_input_destroy /* destroy */
164 /* pointer to input structure of foreground thread */
165 static struct thread_input *foreground_input;
168 /* set the caret window in a given thread input */
169 static void set_caret_window( struct thread_input *input, user_handle_t win )
171 if (!win || win != input->caret)
173 input->caret_rect.left = 0;
174 input->caret_rect.top = 0;
175 input->caret_rect.right = 0;
176 input->caret_rect.bottom = 0;
179 input->caret_hide = 1;
180 input->caret_state = 0;
183 /* create a thread input object */
184 static struct thread_input *create_thread_input(void)
186 struct thread_input *input;
188 if ((input = alloc_object( &thread_input_ops )))
193 input->menu_owner = 0;
194 input->move_size = 0;
196 input->msg_thread = NULL;
197 list_init( &input->msg_list );
198 set_caret_window( input, 0 );
199 memset( input->keystate, 0, sizeof(input->keystate) );
204 /* release the thread input data of a given thread */
205 static void release_thread_input( struct thread *thread )
207 struct thread_input *input = thread->queue->input;
210 if (input->msg_thread == thread)
212 release_object( input->msg_thread );
213 input->msg_thread = NULL;
216 release_object( input );
217 thread->queue->input = NULL;
220 /* create a message queue object */
221 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
223 struct msg_queue *queue;
226 if (!input && !(input = create_thread_input())) return NULL;
227 if ((queue = alloc_object( &msg_queue_ops )))
229 queue->wake_bits = 0;
230 queue->wake_mask = 0;
231 queue->changed_bits = 0;
232 queue->changed_mask = 0;
233 queue->paint_count = 0;
234 queue->recv_result = NULL;
235 queue->next_timer_id = 1;
236 queue->timeout = NULL;
237 queue->input = (struct thread_input *)grab_object( input );
239 gettimeofday( &queue->last_get_msg, NULL );
240 list_init( &queue->send_result );
241 list_init( &queue->callback_result );
242 list_init( &queue->pending_timers );
243 list_init( &queue->expired_timers );
244 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
246 thread->queue = queue;
247 if (!thread->process->queue)
248 thread->process->queue = (struct msg_queue *)grab_object( queue );
250 release_object( input );
254 /* free the message queue of a thread at thread exit */
255 void free_msg_queue( struct thread *thread )
257 struct process *process = thread->process;
259 remove_thread_hooks( thread );
260 if (!thread->queue) return;
261 if (process->queue == thread->queue) /* is it the process main queue? */
263 release_object( process->queue );
264 process->queue = NULL;
265 if (process->idle_event)
267 set_event( process->idle_event );
268 release_object( process->idle_event );
269 process->idle_event = NULL;
272 release_thread_input( thread );
273 release_object( thread->queue );
274 thread->queue = NULL;
277 /* get the hook table for a given thread */
278 struct hook_table *get_queue_hooks( struct thread *thread )
280 if (!thread->queue) return NULL;
281 return thread->queue->hooks;
284 /* set the hook table for a given thread, allocating the queue if needed */
285 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
287 struct msg_queue *queue = thread->queue;
288 if (!queue) queue = create_msg_queue( thread, NULL );
289 if (queue->hooks) release_object( queue->hooks );
290 queue->hooks = hooks;
293 /* check the queue status */
294 inline static int is_signaled( struct msg_queue *queue )
296 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
299 /* set some queue bits */
300 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
302 queue->wake_bits |= bits;
303 queue->changed_bits |= bits;
304 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
307 /* clear some queue bits */
308 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
310 queue->wake_bits &= ~bits;
311 queue->changed_bits &= ~bits;
314 /* check whether msg is a keyboard message */
315 inline static int is_keyboard_msg( struct message *msg )
317 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
320 /* get the QS_* bit corresponding to a given hardware message */
321 inline static int get_hardware_msg_bit( struct message *msg )
323 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
324 if (is_keyboard_msg( msg )) return QS_KEY;
325 return QS_MOUSEBUTTON;
328 /* get the current thread queue, creating it if needed */
329 inline static struct msg_queue *get_current_queue(void)
331 struct msg_queue *queue = current->queue;
332 if (!queue) queue = create_msg_queue( current, NULL );
336 /* try to merge a message with the last in the list; return 1 if successful */
337 static int merge_message( struct thread_input *input, const struct message *msg )
339 struct message *prev;
340 struct list *ptr = list_tail( &input->msg_list );
343 prev = LIST_ENTRY( ptr, struct message, entry );
344 if (input->msg == prev) return 0;
345 if (prev->result) return 0;
346 if (prev->win != msg->win) return 0;
347 if (prev->msg != msg->msg) return 0;
348 if (prev->type != msg->type) return 0;
349 /* now we can merge it */
350 prev->wparam = msg->wparam;
351 prev->lparam = msg->lparam;
354 prev->time = msg->time;
355 prev->info = msg->info;
359 /* free a result structure */
360 static void free_result( struct message_result *result )
362 if (result->timeout) remove_timeout_user( result->timeout );
363 if (result->data) free( result->data );
364 if (result->callback_msg) free( result->callback_msg );
368 /* remove the result from the sender list it is on */
369 static inline void remove_result_from_sender( struct message_result *result )
371 assert( result->sender );
373 list_remove( &result->sender_entry );
374 result->sender = NULL;
375 if (!result->receiver) free_result( result );
378 /* store the message result in the appropriate structure */
379 static void store_message_result( struct message_result *res, unsigned int result,
382 res->result = result;
387 remove_timeout_user( res->timeout );
392 if (res->callback_msg)
394 /* queue the callback message in the sender queue */
395 res->callback_msg->lparam = result;
396 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
397 set_queue_bits( res->sender, QS_SENDMESSAGE );
398 res->callback_msg = NULL;
399 remove_result_from_sender( res );
403 /* wake sender queue if waiting on this result */
404 if (list_head(&res->sender->send_result) == &res->sender_entry)
405 set_queue_bits( res->sender, QS_SMRESULT );
411 /* free a message when deleting a queue or window */
412 static void free_message( struct message *msg )
414 struct message_result *result = msg->result;
419 result->receiver = NULL;
420 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
422 else free_result( result );
424 if (msg->data) free( msg->data );
428 /* remove (and free) a message from a message list */
429 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
430 enum message_kind kind )
432 list_remove( &msg->entry );
436 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
439 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_POSTMESSAGE );
445 /* message timed out without getting a reply */
446 static void result_timeout( void *private )
448 struct message_result *result = private;
450 assert( !result->replied );
452 result->timeout = NULL;
453 store_message_result( result, 0, STATUS_TIMEOUT );
456 /* allocate and fill a message result structure */
457 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
458 struct msg_queue *recv_queue,
459 struct message *msg, unsigned int timeout,
460 void *callback, unsigned int callback_data )
462 struct message_result *result = mem_alloc( sizeof(*result) );
465 result->sender = send_queue;
466 result->receiver = recv_queue;
469 result->data_size = 0;
470 result->timeout = NULL;
472 if (msg->type == MSG_CALLBACK)
474 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
480 callback_msg->type = MSG_CALLBACK_RESULT;
481 callback_msg->win = msg->win;
482 callback_msg->msg = msg->msg;
483 callback_msg->wparam = (unsigned int)callback;
484 callback_msg->lparam = 0;
485 callback_msg->time = get_tick_count();
488 callback_msg->info = callback_data;
489 callback_msg->result = NULL;
490 callback_msg->data = NULL;
491 callback_msg->data_size = 0;
493 result->callback_msg = callback_msg;
494 list_add_head( &send_queue->callback_result, &result->sender_entry );
498 result->callback_msg = NULL;
499 list_add_head( &send_queue->send_result, &result->sender_entry );
505 gettimeofday( &when, 0 );
506 add_timeout( &when, timeout );
507 result->timeout = add_timeout_user( &when, result_timeout, result );
513 /* receive a message, removing it from the sent queue */
514 static void receive_message( struct msg_queue *queue, struct message *msg,
515 struct get_message_reply *reply )
517 struct message_result *result = msg->result;
519 reply->total = msg->data_size;
520 if (msg->data_size > get_reply_max_size())
522 set_error( STATUS_BUFFER_OVERFLOW );
525 reply->type = msg->type;
526 reply->win = msg->win;
527 reply->msg = msg->msg;
528 reply->wparam = msg->wparam;
529 reply->lparam = msg->lparam;
532 reply->time = msg->time;
533 reply->info = msg->info;
534 reply->hook = msg->hook;
535 reply->hook_proc = msg->hook_proc;
537 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
539 list_remove( &msg->entry );
540 /* put the result on the receiver result stack */
543 result->recv_next = queue->recv_result;
544 queue->recv_result = result;
547 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
550 /* set the result of the current received message */
551 static void reply_message( struct msg_queue *queue, unsigned int result,
552 unsigned int error, int remove, const void *data, size_t len )
554 struct message_result *res = queue->recv_result;
558 queue->recv_result = res->recv_next;
559 res->receiver = NULL;
560 if (!res->sender) /* no one waiting for it */
568 if (len && (res->data = memdup( data, len ))) res->data_size = len;
569 store_message_result( res, result, error );
573 /* retrieve a posted message */
574 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
575 unsigned int first, unsigned int last, unsigned int flags,
576 struct get_message_reply *reply )
580 /* check against the filters */
581 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
583 if (msg->msg == WM_QUIT) goto found; /* WM_QUIT is never filtered */
584 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
585 if (msg->msg < first) continue;
586 if (msg->msg > last) continue;
587 goto found; /* found one */
591 /* return it to the app */
593 reply->total = msg->data_size;
594 if (msg->data_size > get_reply_max_size())
596 set_error( STATUS_BUFFER_OVERFLOW );
599 reply->type = msg->type;
600 reply->win = msg->win;
601 reply->msg = msg->msg;
602 reply->wparam = msg->wparam;
603 reply->lparam = msg->lparam;
606 reply->time = msg->time;
607 reply->info = msg->info;
609 if (flags & GET_MSG_REMOVE)
613 set_reply_data_ptr( msg->data, msg->data_size );
617 remove_queue_message( queue, msg, POST_MESSAGE );
619 else if (msg->data) set_reply_data( msg->data, msg->data_size );
624 /* empty a message list and free all the messages */
625 static void empty_msg_list( struct list *list )
629 while ((ptr = list_head( list )) != NULL)
631 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
632 list_remove( &msg->entry );
637 /* cleanup all pending results when deleting a queue */
638 static void cleanup_results( struct msg_queue *queue )
642 while ((entry = list_head( &queue->send_result )) != NULL)
644 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
647 while ((entry = list_head( &queue->callback_result )) != NULL)
649 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
652 while (queue->recv_result)
653 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
656 /* check if the thread owning the queue is hung (not checking for messages) */
657 static int is_queue_hung( struct msg_queue *queue )
660 struct wait_queue_entry *entry;
662 gettimeofday( &now, NULL );
663 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
664 return 0; /* less than 5 seconds since last get message -> not hung */
666 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
668 if (entry->thread->queue == queue)
669 return 0; /* thread is waiting on queue -> not hung */
674 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
676 struct msg_queue *queue = (struct msg_queue *)obj;
677 struct process *process = entry->thread->process;
679 /* a thread can only wait on its own queue */
680 if (entry->thread->queue != queue)
682 set_error( STATUS_ACCESS_DENIED );
685 /* if waiting on the main process queue, set the idle event */
686 if (process->queue == queue)
688 if (process->idle_event) set_event( process->idle_event );
690 add_queue( obj, entry );
694 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
696 struct msg_queue *queue = (struct msg_queue *)obj;
697 struct process *process = entry->thread->process;
699 remove_queue( obj, entry );
701 assert( entry->thread->queue == queue );
703 /* if waiting on the main process queue, reset the idle event */
704 if (process->queue == queue)
706 if (process->idle_event) reset_event( process->idle_event );
710 static void msg_queue_dump( struct object *obj, int verbose )
712 struct msg_queue *queue = (struct msg_queue *)obj;
713 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
714 queue->wake_bits, queue->wake_mask );
717 static int msg_queue_signaled( struct object *obj, struct thread *thread )
719 struct msg_queue *queue = (struct msg_queue *)obj;
720 return is_signaled( queue );
723 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
725 struct msg_queue *queue = (struct msg_queue *)obj;
726 queue->wake_mask = 0;
727 queue->changed_mask = 0;
728 return 0; /* Not abandoned */
731 static void msg_queue_destroy( struct object *obj )
733 struct msg_queue *queue = (struct msg_queue *)obj;
737 cleanup_results( queue );
738 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
740 while ((ptr = list_head( &queue->pending_timers )))
742 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
743 list_remove( &timer->entry );
746 while ((ptr = list_head( &queue->expired_timers )))
748 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
749 list_remove( &timer->entry );
752 if (queue->timeout) remove_timeout_user( queue->timeout );
753 if (queue->input) release_object( queue->input );
754 if (queue->hooks) release_object( queue->hooks );
757 static void thread_input_dump( struct object *obj, int verbose )
759 struct thread_input *input = (struct thread_input *)obj;
760 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
761 input->focus, input->capture, input->active );
764 static void thread_input_destroy( struct object *obj )
766 struct thread_input *input = (struct thread_input *)obj;
768 if (foreground_input == input) foreground_input = NULL;
769 if (input->msg_thread) release_object( input->msg_thread );
770 empty_msg_list( &input->msg_list );
773 /* fix the thread input data when a window is destroyed */
774 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
776 struct thread_input *input = queue->input;
778 if (window == input->focus) input->focus = 0;
779 if (window == input->capture) input->capture = 0;
780 if (window == input->active) input->active = 0;
781 if (window == input->menu_owner) input->menu_owner = 0;
782 if (window == input->move_size) input->move_size = 0;
783 if (window == input->caret) set_caret_window( input, 0 );
786 /* check if the specified window can be set in the input data of a given queue */
787 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
789 struct thread *thread;
792 if (!window) return 1; /* we can always clear the data */
794 if ((thread = get_window_thread( window )))
796 ret = (queue->input == thread->queue->input);
797 if (!ret) set_error( STATUS_ACCESS_DENIED );
798 release_object( thread );
800 else set_error( STATUS_INVALID_HANDLE );
805 /* attach two thread input data structures */
806 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
808 struct thread_input *input;
810 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
811 input = (struct thread_input *)grab_object( thread_to->queue->input );
813 if (thread_from->queue)
815 release_thread_input( thread_from );
816 thread_from->queue->input = input;
820 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
822 memset( input->keystate, 0, sizeof(input->keystate) );
826 /* detach two thread input data structures */
827 static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
829 struct thread_input *input;
831 if (!thread_from->queue || !thread_to->queue ||
832 thread_from->queue->input != thread_to->queue->input)
834 set_error( STATUS_ACCESS_DENIED );
837 if ((input = create_thread_input()))
839 release_thread_input( thread_from );
840 thread_from->queue->input = input;
845 /* set the next timer to expire */
846 static void set_next_timer( struct msg_queue *queue )
852 remove_timeout_user( queue->timeout );
853 queue->timeout = NULL;
855 if ((ptr = list_head( &queue->pending_timers )))
857 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
858 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
860 /* set/clear QS_TIMER bit */
861 if (list_empty( &queue->expired_timers ))
862 clear_queue_bits( queue, QS_TIMER );
864 set_queue_bits( queue, QS_TIMER );
867 /* find a timer from its window and id */
868 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
869 unsigned int msg, unsigned int id )
873 /* we need to search both lists */
875 LIST_FOR_EACH( ptr, &queue->pending_timers )
877 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
878 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
880 LIST_FOR_EACH( ptr, &queue->expired_timers )
882 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
883 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
888 /* callback for the next timer expiration */
889 static void timer_callback( void *private )
891 struct msg_queue *queue = private;
894 queue->timeout = NULL;
895 /* move on to the next timer */
896 ptr = list_head( &queue->pending_timers );
898 list_add_tail( &queue->expired_timers, ptr );
899 set_next_timer( queue );
902 /* link a timer at its rightful place in the queue list */
903 static void link_timer( struct msg_queue *queue, struct timer *timer )
907 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
909 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
910 if (!time_before( &t->when, &timer->when )) break;
912 list_add_before( ptr, &timer->entry );
915 /* remove a timer from the queue timer list and free it */
916 static void free_timer( struct msg_queue *queue, struct timer *timer )
918 list_remove( &timer->entry );
920 set_next_timer( queue );
923 /* restart an expired timer */
924 static void restart_timer( struct msg_queue *queue, struct timer *timer )
928 list_remove( &timer->entry );
929 gettimeofday( &now, 0 );
930 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
931 link_timer( queue, timer );
932 set_next_timer( queue );
935 /* find an expired timer matching the filtering parameters */
936 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
937 unsigned int get_first, unsigned int get_last,
942 LIST_FOR_EACH( ptr, &queue->expired_timers )
944 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
945 if (win && timer->win != win) continue;
946 if (timer->msg >= get_first && timer->msg <= get_last)
948 if (remove) restart_timer( queue, timer );
956 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
958 struct timer *timer = mem_alloc( sizeof(*timer) );
961 timer->rate = max( rate, 1 );
962 gettimeofday( &timer->when, 0 );
963 add_timeout( &timer->when, rate );
964 link_timer( queue, timer );
965 /* check if we replaced the next timer */
966 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
971 /* change the input key state for a given key */
972 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
976 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
977 input->keystate[key] |= 0x80;
979 else input->keystate[key] &= ~0x80;
982 /* update the input key state for a keyboard message */
983 static void update_input_key_state( struct thread_input *input, const struct message *msg )
986 int down = 0, extended;
994 set_input_key_state( input, VK_LBUTTON, down );
1000 set_input_key_state( input, VK_MBUTTON, down );
1002 case WM_RBUTTONDOWN:
1006 set_input_key_state( input, VK_RBUTTON, down );
1014 key = (unsigned char)msg->wparam;
1015 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1016 set_input_key_state( input, key, down );
1020 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1023 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1026 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1033 /* release the hardware message currently being processed by the given thread */
1034 static void release_hardware_message( struct thread *thread, int remove )
1036 struct thread_input *input = thread->queue->input;
1038 if (input->msg_thread != thread) return;
1041 struct message *other;
1044 update_input_key_state( input, input->msg );
1045 list_remove( &input->msg->entry );
1046 clr_bit = get_hardware_msg_bit( input->msg );
1047 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1049 if (get_hardware_msg_bit( other ) == clr_bit)
1055 if (clr_bit) clear_queue_bits( thread->queue, clr_bit );
1056 free_message( input->msg );
1058 release_object( input->msg_thread );
1060 input->msg_thread = NULL;
1063 /* find the window that should receive a given hardware message */
1064 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1065 unsigned int *msg_code )
1067 user_handle_t win = 0;
1069 *msg_code = msg->msg;
1070 if (is_keyboard_msg( msg ))
1072 if (input && !(win = input->focus))
1074 win = input->active;
1075 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1078 else /* mouse message */
1080 if (!input || !(win = input->capture))
1082 if (!(win = msg->win) || !is_window_visible( win ))
1083 win = window_from_point( msg->x, msg->y );
1089 /* queue a hardware message into a given thread input */
1090 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1093 struct thread *thread;
1094 struct thread_input *input;
1095 unsigned int msg_code;
1097 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1098 if (!win || !(thread = get_window_thread(win)))
1103 input = thread->queue->input;
1105 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1108 list_add_tail( &input->msg_list, &msg->entry );
1109 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1111 release_object( thread );
1114 /* find a hardware message for the given queue */
1115 static int get_hardware_message( struct thread *thread, struct message *first,
1116 user_handle_t filter_win, struct get_message_reply *reply )
1118 struct thread_input *input = thread->queue->input;
1119 struct thread *win_thread;
1122 int clear_bits, got_one = 0;
1123 unsigned int msg_code;
1125 if (input->msg_thread && input->msg_thread != thread)
1126 return 0; /* locked by another thread */
1130 ptr = list_head( &input->msg_list );
1131 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1135 ptr = list_next( &input->msg_list, &first->entry );
1136 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1141 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1142 win = find_hardware_message_window( input, msg, &msg_code );
1143 if (!win || !(win_thread = get_window_thread( win )))
1145 /* no window at all, remove it */
1146 ptr = list_next( &input->msg_list, ptr );
1147 update_input_key_state( input, msg );
1148 list_remove( &msg->entry );
1149 free_message( msg );
1152 if (win_thread != thread)
1154 /* wake the other thread */
1155 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1156 release_object( win_thread );
1158 ptr = list_next( &input->msg_list, ptr );
1161 /* if we already got a message for another thread, or if it doesn't
1162 * match the filter we skip it (filter is only checked for keyboard
1163 * messages since the dest window for a mouse message depends on hittest)
1166 (filter_win && is_keyboard_msg(msg) &&
1167 win != filter_win && !is_child_window( filter_win, win )))
1169 clear_bits &= ~get_hardware_msg_bit( msg );
1170 release_object( win_thread );
1171 ptr = list_next( &input->msg_list, ptr );
1174 /* now we can return it */
1175 if (!input->msg_thread) input->msg_thread = win_thread;
1176 else release_object( win_thread );
1179 reply->type = MSG_HARDWARE;
1181 reply->msg = msg_code;
1182 reply->wparam = msg->wparam;
1183 reply->lparam = msg->lparam;
1186 reply->time = msg->time;
1187 reply->info = msg->info;
1190 /* nothing found, clear the hardware queue bits */
1191 clear_queue_bits( thread->queue, clear_bits );
1192 if (input->msg_thread) release_object( input->msg_thread );
1194 input->msg_thread = NULL;
1198 /* increment (or decrement if 'incr' is negative) the queue paint count */
1199 void inc_queue_paint_count( struct thread *thread, int incr )
1201 struct msg_queue *queue = thread->queue;
1205 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1207 if (queue->paint_count)
1208 set_queue_bits( queue, QS_PAINT );
1210 clear_queue_bits( queue, QS_PAINT );
1214 /* remove all messages and timers belonging to a certain window */
1215 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1217 struct msg_queue *queue = thread->queue;
1225 ptr = list_head( &queue->pending_timers );
1228 struct list *next = list_next( &queue->pending_timers, ptr );
1229 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1230 if (timer->win == win) free_timer( queue, timer );
1233 ptr = list_head( &queue->expired_timers );
1236 struct list *next = list_next( &queue->expired_timers, ptr );
1237 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1238 if (timer->win == win) free_timer( queue, timer );
1242 /* remove messages */
1243 for (i = 0; i < NB_MSG_KINDS; i++)
1245 struct list *ptr, *next;
1247 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1249 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1250 if (msg->win == win) remove_queue_message( queue, msg, i );
1254 thread_input_cleanup_window( queue, win );
1257 /* post a message to a window; used by socket handling */
1258 void post_message( user_handle_t win, unsigned int message,
1259 unsigned int wparam, unsigned int lparam )
1261 struct message *msg;
1262 struct thread *thread = get_window_thread( win );
1264 if (!thread) return;
1266 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1268 msg->type = MSG_POSTED;
1269 msg->win = get_user_full_handle( win );
1271 msg->wparam = wparam;
1272 msg->lparam = lparam;
1273 msg->time = get_tick_count();
1281 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1282 set_queue_bits( thread->queue, QS_POSTMESSAGE );
1284 release_object( thread );
1287 /* post a win event */
1288 void post_win_event( struct thread *thread, unsigned int event,
1289 user_handle_t win, unsigned int object_id,
1290 unsigned int child_id, void *hook_proc,
1291 const WCHAR *module, size_t module_size,
1294 struct message *msg;
1296 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1298 msg->type = MSG_WINEVENT;
1299 msg->win = get_user_full_handle( win );
1301 msg->wparam = object_id;
1302 msg->lparam = child_id;
1303 msg->time = get_tick_count();
1306 msg->info = get_thread_id( current );
1309 msg->hook_proc = hook_proc;
1311 if ((msg->data = malloc( module_size )))
1313 msg->data_size = module_size;
1314 memcpy( msg->data, module, module_size );
1316 if (debug_level > 1)
1317 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1318 get_thread_id(thread), event, win, object_id, child_id );
1319 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1320 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1327 /* get the message queue of the current thread */
1328 DECL_HANDLER(get_msg_queue)
1330 struct msg_queue *queue = get_current_queue();
1333 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1337 /* set the current message queue wakeup mask */
1338 DECL_HANDLER(set_queue_mask)
1340 struct msg_queue *queue = get_current_queue();
1344 queue->wake_mask = req->wake_mask;
1345 queue->changed_mask = req->changed_mask;
1346 reply->wake_bits = queue->wake_bits;
1347 reply->changed_bits = queue->changed_bits;
1348 if (is_signaled( queue ))
1350 /* if skip wait is set, do what would have been done in the subsequent wait */
1351 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1352 else wake_up( &queue->obj, 0 );
1358 /* get the current message queue status */
1359 DECL_HANDLER(get_queue_status)
1361 struct msg_queue *queue = current->queue;
1364 reply->wake_bits = queue->wake_bits;
1365 reply->changed_bits = queue->changed_bits;
1366 if (req->clear) queue->changed_bits = 0;
1368 else reply->wake_bits = reply->changed_bits = 0;
1372 /* send a message to a thread queue */
1373 DECL_HANDLER(send_message)
1375 struct message *msg;
1376 struct msg_queue *send_queue = get_current_queue();
1377 struct msg_queue *recv_queue = NULL;
1378 struct thread *thread = NULL;
1382 if (!(thread = get_thread_from_id( req->id ))) return;
1384 else if (req->type != MSG_HARDWARE)
1386 /* only hardware messages are allowed without destination thread */
1387 set_error( STATUS_INVALID_PARAMETER );
1391 if (thread && !(recv_queue = thread->queue))
1393 set_error( STATUS_INVALID_PARAMETER );
1394 release_object( thread );
1397 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1399 set_error( STATUS_TIMEOUT );
1400 release_object( thread );
1404 if ((msg = mem_alloc( sizeof(*msg) )))
1406 msg->type = req->type;
1407 msg->win = get_user_full_handle( req->win );
1408 msg->msg = req->msg;
1409 msg->wparam = req->wparam;
1410 msg->lparam = req->lparam;
1411 msg->time = req->time;
1414 msg->info = req->info;
1421 case MSG_OTHER_PROCESS:
1422 msg->data_size = get_req_data_size();
1423 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1432 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1433 req->timeout, req->callback, req->info )))
1435 free_message( msg );
1440 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1441 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1444 /* needed for posted DDE messages */
1445 msg->data_size = get_req_data_size();
1446 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1451 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1452 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1455 queue_hardware_message( recv_queue, msg );
1457 case MSG_CALLBACK_RESULT: /* cannot send this one */
1459 set_error( STATUS_INVALID_PARAMETER );
1464 if (thread) release_object( thread );
1468 /* get a message from the current queue */
1469 DECL_HANDLER(get_message)
1471 struct timer *timer;
1473 struct message *first_hw_msg = NULL;
1474 struct msg_queue *queue = get_current_queue();
1475 user_handle_t get_win = get_user_full_handle( req->get_win );
1478 gettimeofday( &queue->last_get_msg, NULL );
1480 /* first of all release the hardware input lock if we own it */
1481 /* we'll grab it again if we find a hardware message */
1482 if (queue->input->msg_thread == current)
1484 first_hw_msg = queue->input->msg;
1485 release_hardware_message( current, 0 );
1488 /* first check for sent messages */
1489 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1491 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1492 receive_message( queue, msg, reply );
1495 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1497 /* clear changed bits so we can wait on them if we don't find a message */
1498 queue->changed_bits = 0;
1500 /* then check for posted messages */
1501 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1504 /* then check for any raw hardware message */
1505 if (get_hardware_message( current, first_hw_msg, get_win, reply ))
1508 /* now check for WM_PAINT */
1509 if (queue->paint_count &&
1510 (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
1511 (reply->win = find_window_to_repaint( get_win, current )))
1513 reply->type = MSG_POSTED;
1514 reply->msg = WM_PAINT;
1519 reply->time = get_tick_count();
1524 /* now check for timer */
1525 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1526 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1528 reply->type = MSG_POSTED;
1529 reply->win = timer->win;
1530 reply->msg = timer->msg;
1531 reply->wparam = timer->id;
1532 reply->lparam = timer->lparam;
1535 reply->time = get_tick_count();
1541 set_error( STATUS_PENDING ); /* FIXME */
1545 /* reply to a sent message */
1546 DECL_HANDLER(reply_message)
1548 if (!current->queue)
1550 set_error( STATUS_ACCESS_DENIED );
1553 if (req->type == MSG_HARDWARE)
1555 struct thread_input *input = current->queue->input;
1556 if (input->msg_thread == current) release_hardware_message( current, req->remove );
1557 else set_error( STATUS_ACCESS_DENIED );
1559 else if (current->queue->recv_result)
1560 reply_message( current->queue, req->result, 0, req->remove,
1561 get_req_data(), get_req_data_size() );
1565 /* retrieve the reply for the last message sent */
1566 DECL_HANDLER(get_message_reply)
1568 struct message_result *result;
1570 struct msg_queue *queue = current->queue;
1574 set_error( STATUS_PENDING );
1577 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1579 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1580 if (result->replied || req->cancel)
1582 if (result->replied)
1584 reply->result = result->result;
1585 set_error( result->error );
1588 size_t data_len = min( result->data_size, get_reply_max_size() );
1589 set_reply_data_ptr( result->data, data_len );
1590 result->data = NULL;
1591 result->data_size = 0;
1594 remove_result_from_sender( result );
1596 entry = list_head( &queue->send_result );
1597 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1600 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1601 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1605 else set_error( STATUS_ACCESS_DENIED );
1609 /* set a window timer */
1610 DECL_HANDLER(set_win_timer)
1612 struct timer *timer;
1613 struct msg_queue *queue;
1614 struct thread *thread = NULL;
1615 user_handle_t win = 0;
1616 unsigned int id = req->id;
1620 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1622 set_error( STATUS_INVALID_HANDLE );
1625 if (thread->process != current->process)
1627 release_object( thread );
1628 set_error( STATUS_ACCESS_DENIED );
1631 queue = thread->queue;
1632 /* remove it if it existed already */
1633 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1637 queue = get_current_queue();
1638 /* find a free id for it */
1641 id = queue->next_timer_id;
1642 if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1644 while (find_timer( queue, 0, req->msg, id ));
1647 if ((timer = set_timer( queue, req->rate )))
1650 timer->msg = req->msg;
1652 timer->lparam = req->lparam;
1655 if (thread) release_object( thread );
1658 /* kill a window timer */
1659 DECL_HANDLER(kill_win_timer)
1661 struct timer *timer;
1662 struct thread *thread;
1663 user_handle_t win = 0;
1667 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1669 set_error( STATUS_INVALID_HANDLE );
1672 if (thread->process != current->process)
1674 release_object( thread );
1675 set_error( STATUS_ACCESS_DENIED );
1679 else thread = (struct thread *)grab_object( current );
1681 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1682 free_timer( thread->queue, timer );
1684 set_error( STATUS_INVALID_PARAMETER );
1686 release_object( thread );
1690 /* attach (or detach) thread inputs */
1691 DECL_HANDLER(attach_thread_input)
1693 struct thread *thread_from = get_thread_from_id( req->tid_from );
1694 struct thread *thread_to = get_thread_from_id( req->tid_to );
1696 if (!thread_from || !thread_to)
1698 if (thread_from) release_object( thread_from );
1699 if (thread_to) release_object( thread_to );
1702 if (thread_from != thread_to)
1704 if (req->attach) attach_thread_input( thread_from, thread_to );
1705 else detach_thread_input( thread_from, thread_to );
1707 else set_error( STATUS_ACCESS_DENIED );
1708 release_object( thread_from );
1709 release_object( thread_to );
1713 /* get thread input data */
1714 DECL_HANDLER(get_thread_input)
1716 struct thread *thread = NULL;
1717 struct thread_input *input;
1721 if (!(thread = get_thread_from_id( req->tid ))) return;
1722 input = thread->queue ? thread->queue->input : NULL;
1724 else input = foreground_input; /* get the foreground thread info */
1728 reply->focus = input->focus;
1729 reply->capture = input->capture;
1730 reply->active = input->active;
1731 reply->menu_owner = input->menu_owner;
1732 reply->move_size = input->move_size;
1733 reply->caret = input->caret;
1734 reply->rect = input->caret_rect;
1741 reply->menu_owner = 0;
1742 reply->move_size = 0;
1744 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1746 /* foreground window is active window of foreground thread */
1747 reply->foreground = foreground_input ? foreground_input->active : 0;
1748 if (thread) release_object( thread );
1752 /* retrieve queue keyboard state for a given thread */
1753 DECL_HANDLER(get_key_state)
1755 struct thread *thread;
1756 struct thread_input *input;
1758 if (!(thread = get_thread_from_id( req->tid ))) return;
1759 input = thread->queue ? thread->queue->input : NULL;
1762 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1763 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1765 release_object( thread );
1769 /* set queue keyboard state for a given thread */
1770 DECL_HANDLER(set_key_state)
1772 struct thread *thread = NULL;
1773 struct thread_input *input;
1775 if (!(thread = get_thread_from_id( req->tid ))) return;
1776 input = thread->queue ? thread->queue->input : NULL;
1779 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1780 if (size) memcpy( input->keystate, get_req_data(), size );
1782 release_object( thread );
1786 /* set the system foreground window */
1787 DECL_HANDLER(set_foreground_window)
1789 struct msg_queue *queue = get_current_queue();
1791 reply->previous = foreground_input ? foreground_input->active : 0;
1792 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1793 reply->send_msg_new = FALSE;
1797 struct thread *thread;
1799 if (is_top_level_window( req->handle ) &&
1800 ((thread = get_window_thread( req->handle ))))
1802 foreground_input = thread->queue->input;
1803 reply->send_msg_new = (foreground_input != queue->input);
1804 release_object( thread );
1806 else set_error( STATUS_INVALID_HANDLE );
1808 else foreground_input = NULL;
1812 /* set the current thread focus window */
1813 DECL_HANDLER(set_focus_window)
1815 struct msg_queue *queue = get_current_queue();
1817 reply->previous = 0;
1818 if (queue && check_queue_input_window( queue, req->handle ))
1820 reply->previous = queue->input->focus;
1821 queue->input->focus = get_user_full_handle( req->handle );
1826 /* set the current thread active window */
1827 DECL_HANDLER(set_active_window)
1829 struct msg_queue *queue = get_current_queue();
1831 reply->previous = 0;
1832 if (queue && check_queue_input_window( queue, req->handle ))
1834 if (!req->handle || make_window_active( req->handle ))
1836 reply->previous = queue->input->active;
1837 queue->input->active = get_user_full_handle( req->handle );
1839 else set_error( STATUS_INVALID_HANDLE );
1844 /* set the current thread capture window */
1845 DECL_HANDLER(set_capture_window)
1847 struct msg_queue *queue = get_current_queue();
1849 reply->previous = reply->full_handle = 0;
1850 if (queue && check_queue_input_window( queue, req->handle ))
1852 struct thread_input *input = queue->input;
1854 reply->previous = input->capture;
1855 input->capture = get_user_full_handle( req->handle );
1856 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1857 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1858 reply->full_handle = input->capture;
1863 /* Set the current thread caret window */
1864 DECL_HANDLER(set_caret_window)
1866 struct msg_queue *queue = get_current_queue();
1868 reply->previous = 0;
1869 if (queue && check_queue_input_window( queue, req->handle ))
1871 struct thread_input *input = queue->input;
1873 reply->previous = input->caret;
1874 reply->old_rect = input->caret_rect;
1875 reply->old_hide = input->caret_hide;
1876 reply->old_state = input->caret_state;
1878 set_caret_window( input, get_user_full_handle(req->handle) );
1879 input->caret_rect.right = input->caret_rect.left + req->width;
1880 input->caret_rect.bottom = input->caret_rect.top + req->height;
1885 /* Set the current thread caret information */
1886 DECL_HANDLER(set_caret_info)
1888 struct msg_queue *queue = get_current_queue();
1889 struct thread_input *input;
1892 input = queue->input;
1893 reply->full_handle = input->caret;
1894 reply->old_rect = input->caret_rect;
1895 reply->old_hide = input->caret_hide;
1896 reply->old_state = input->caret_state;
1898 if (req->handle && get_user_full_handle(req->handle) != input->caret)
1900 set_error( STATUS_ACCESS_DENIED );
1903 if (req->flags & SET_CARET_POS)
1905 input->caret_rect.right += req->x - input->caret_rect.left;
1906 input->caret_rect.bottom += req->y - input->caret_rect.top;
1907 input->caret_rect.left = req->x;
1908 input->caret_rect.top = req->y;
1910 if (req->flags & SET_CARET_HIDE)
1912 input->caret_hide += req->hide;
1913 if (input->caret_hide < 0) input->caret_hide = 0;
1915 if (req->flags & SET_CARET_STATE)
1917 if (req->state == -1) input->caret_state = !input->caret_state;
1918 else input->caret_state = !!req->state;