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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
30 #define WIN32_NO_STATUS
44 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
45 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
47 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
48 #define NB_MSG_KINDS (POST_MESSAGE+1)
53 struct list sender_entry; /* entry in sender list */
54 struct message *msg; /* message the result is for */
55 struct message_result *recv_next; /* next in receiver list */
56 struct msg_queue *sender; /* sender queue */
57 struct msg_queue *receiver; /* receiver queue */
58 int replied; /* has it been replied to? */
59 unsigned int result; /* reply result */
60 unsigned int error; /* error code to pass back to sender */
61 struct message *callback_msg; /* message to queue for callback */
62 void *data; /* message reply data */
63 unsigned int data_size; /* size of message reply data */
64 struct timeout_user *timeout; /* result timeout */
69 struct list entry; /* entry in message list */
70 enum message_type type; /* message type */
71 user_handle_t win; /* window handle */
72 unsigned int msg; /* message code */
73 unsigned long wparam; /* parameters */
74 unsigned long lparam; /* parameters */
75 unsigned long info; /* extra info */
76 int x; /* x position */
77 int y; /* y position */
78 unsigned int time; /* message time */
79 user_handle_t hook; /* winevent hook handle */
80 void *hook_proc; /* winevent hook proc address */
81 void *data; /* message data for sent messages */
82 unsigned int data_size; /* size of message data */
83 unsigned int unique_id; /* unique id for nested hw message waits */
84 struct message_result *result; /* result in sender queue */
89 struct list entry; /* entry in timer list */
90 struct timeval when; /* next expiration */
91 unsigned int rate; /* timer rate in ms */
92 user_handle_t win; /* window handle */
93 unsigned int msg; /* message to post */
94 unsigned int id; /* timer id */
95 unsigned long lparam; /* lparam for message */
100 struct object obj; /* object header */
101 struct desktop *desktop; /* desktop that this thread input belongs to */
102 user_handle_t focus; /* focus window */
103 user_handle_t capture; /* capture window */
104 user_handle_t active; /* active window */
105 user_handle_t menu_owner; /* current menu owner window */
106 user_handle_t move_size; /* current moving/resizing window */
107 user_handle_t caret; /* caret window */
108 rectangle_t caret_rect; /* caret rectangle */
109 int caret_hide; /* caret hide count */
110 int caret_state; /* caret on/off state */
111 struct list msg_list; /* list of hardware messages */
112 unsigned char keystate[256]; /* state of each key */
117 struct object obj; /* object header */
118 unsigned int wake_bits; /* wakeup bits */
119 unsigned int wake_mask; /* wakeup mask */
120 unsigned int changed_bits; /* changed wakeup bits */
121 unsigned int changed_mask; /* changed wakeup mask */
122 int paint_count; /* pending paint messages count */
123 int quit_message; /* is there a pending quit message? */
124 int exit_code; /* exit code of pending quit message */
125 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
126 struct list send_result; /* stack of sent messages waiting for result */
127 struct list callback_result; /* list of callback messages waiting for result */
128 struct message_result *recv_result; /* stack of received messages waiting for result */
129 struct list pending_timers; /* list of pending timers */
130 struct list expired_timers; /* list of expired timers */
131 unsigned int next_timer_id; /* id for the next timer with a 0 window */
132 struct timeout_user *timeout; /* timeout for next timer to expire */
133 struct thread_input *input; /* thread input descriptor */
134 struct hook_table *hooks; /* hook table */
135 struct timeval last_get_msg; /* time of last get message call */
138 static void msg_queue_dump( struct object *obj, int verbose );
139 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
140 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
141 static int msg_queue_signaled( struct object *obj, struct thread *thread );
142 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
143 static void msg_queue_destroy( struct object *obj );
144 static void thread_input_dump( struct object *obj, int verbose );
145 static void thread_input_destroy( struct object *obj );
146 static void timer_callback( void *private );
148 static const struct object_ops msg_queue_ops =
150 sizeof(struct msg_queue), /* size */
151 msg_queue_dump, /* dump */
152 msg_queue_add_queue, /* add_queue */
153 msg_queue_remove_queue, /* remove_queue */
154 msg_queue_signaled, /* signaled */
155 msg_queue_satisfied, /* satisfied */
156 no_signal, /* signal */
157 no_get_fd, /* get_fd */
158 no_map_access, /* map_access */
159 no_lookup_name, /* lookup_name */
160 no_close_handle, /* close_handle */
161 msg_queue_destroy /* destroy */
165 static const struct object_ops thread_input_ops =
167 sizeof(struct thread_input), /* size */
168 thread_input_dump, /* dump */
169 no_add_queue, /* add_queue */
170 NULL, /* remove_queue */
172 NULL, /* satisfied */
173 no_signal, /* signal */
174 no_get_fd, /* get_fd */
175 no_map_access, /* map_access */
176 no_lookup_name, /* lookup_name */
177 no_close_handle, /* close_handle */
178 thread_input_destroy /* destroy */
181 /* pointer to input structure of foreground thread */
182 static struct thread_input *foreground_input;
183 static unsigned int last_input_time;
186 /* set the caret window in a given thread input */
187 static void set_caret_window( struct thread_input *input, user_handle_t win )
189 if (!win || win != input->caret)
191 input->caret_rect.left = 0;
192 input->caret_rect.top = 0;
193 input->caret_rect.right = 0;
194 input->caret_rect.bottom = 0;
197 input->caret_hide = 1;
198 input->caret_state = 0;
201 /* create a thread input object */
202 static struct thread_input *create_thread_input( struct thread *thread )
204 struct thread_input *input;
206 if ((input = alloc_object( &thread_input_ops )))
211 input->menu_owner = 0;
212 input->move_size = 0;
213 list_init( &input->msg_list );
214 set_caret_window( input, 0 );
215 memset( input->keystate, 0, sizeof(input->keystate) );
217 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
219 release_object( input );
226 /* release the thread input data of a given thread */
227 static inline void release_thread_input( struct thread *thread )
229 struct thread_input *input = thread->queue->input;
232 release_object( input );
233 thread->queue->input = NULL;
236 /* create a message queue object */
237 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
239 struct msg_queue *queue;
242 if (!input && !(input = create_thread_input( thread ))) return NULL;
243 if ((queue = alloc_object( &msg_queue_ops )))
245 queue->wake_bits = 0;
246 queue->wake_mask = 0;
247 queue->changed_bits = 0;
248 queue->changed_mask = 0;
249 queue->paint_count = 0;
250 queue->quit_message = 0;
251 queue->recv_result = NULL;
252 queue->next_timer_id = 1;
253 queue->timeout = NULL;
254 queue->input = (struct thread_input *)grab_object( input );
256 queue->last_get_msg = current_time;
257 list_init( &queue->send_result );
258 list_init( &queue->callback_result );
259 list_init( &queue->pending_timers );
260 list_init( &queue->expired_timers );
261 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
263 thread->queue = queue;
264 if (!thread->process->queue)
265 thread->process->queue = (struct msg_queue *)grab_object( queue );
267 release_object( input );
271 /* free the message queue of a thread at thread exit */
272 void free_msg_queue( struct thread *thread )
274 struct process *process = thread->process;
276 remove_thread_hooks( thread );
277 if (!thread->queue) return;
278 if (process->queue == thread->queue) /* is it the process main queue? */
280 release_object( process->queue );
281 process->queue = NULL;
282 if (process->idle_event)
284 set_event( process->idle_event );
285 release_object( process->idle_event );
286 process->idle_event = NULL;
289 release_object( thread->queue );
290 thread->queue = NULL;
293 /* get the hook table for a given thread */
294 struct hook_table *get_queue_hooks( struct thread *thread )
296 if (!thread->queue) return NULL;
297 return thread->queue->hooks;
300 /* set the hook table for a given thread, allocating the queue if needed */
301 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
303 struct msg_queue *queue = thread->queue;
304 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
305 if (queue->hooks) release_object( queue->hooks );
306 queue->hooks = hooks;
309 /* check the queue status */
310 inline static int is_signaled( struct msg_queue *queue )
312 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
315 /* set some queue bits */
316 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
318 queue->wake_bits |= bits;
319 queue->changed_bits |= bits;
320 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
323 /* clear some queue bits */
324 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
326 queue->wake_bits &= ~bits;
327 queue->changed_bits &= ~bits;
330 /* check whether msg is a keyboard message */
331 inline static int is_keyboard_msg( struct message *msg )
333 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
336 /* check if message is matched by the filter */
337 inline static int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
339 return (msg >= first && msg <= last);
342 /* check whether a message filter contains at least one potential hardware message */
343 inline static int filter_contains_hw_range( unsigned int first, unsigned int last )
345 /* hardware message ranges are (in numerical order):
346 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
347 * WM_KEYFIRST .. WM_KEYLAST
348 * WM_MOUSEFIRST .. WM_MOUSELAST
350 if (last < WM_NCMOUSEFIRST) return 0;
351 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
352 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
353 if (first > WM_MOUSELAST) return 0;
357 /* get the QS_* bit corresponding to a given hardware message */
358 inline static int get_hardware_msg_bit( struct message *msg )
360 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
361 if (is_keyboard_msg( msg )) return QS_KEY;
362 return QS_MOUSEBUTTON;
365 /* get the current thread queue, creating it if needed */
366 inline static struct msg_queue *get_current_queue(void)
368 struct msg_queue *queue = current->queue;
369 if (!queue) queue = create_msg_queue( current, NULL );
373 /* get a (pseudo-)unique id to tag hardware messages */
374 inline static unsigned int get_unique_id(void)
376 static unsigned int id;
377 if (!++id) id = 1; /* avoid an id of 0 */
381 /* try to merge a message with the last in the list; return 1 if successful */
382 static int merge_message( struct thread_input *input, const struct message *msg )
384 struct message *prev;
385 struct list *ptr = list_tail( &input->msg_list );
388 prev = LIST_ENTRY( ptr, struct message, entry );
389 if (prev->unique_id) return 0;
390 if (prev->result) return 0;
391 if (prev->win != msg->win) return 0;
392 if (prev->msg != msg->msg) return 0;
393 if (prev->type != msg->type) return 0;
394 /* now we can merge it */
395 prev->wparam = msg->wparam;
396 prev->lparam = msg->lparam;
399 prev->time = msg->time;
400 prev->info = msg->info;
404 /* free a result structure */
405 static void free_result( struct message_result *result )
407 if (result->timeout) remove_timeout_user( result->timeout );
408 if (result->data) free( result->data );
409 if (result->callback_msg) free( result->callback_msg );
413 /* remove the result from the sender list it is on */
414 static inline void remove_result_from_sender( struct message_result *result )
416 assert( result->sender );
418 list_remove( &result->sender_entry );
419 result->sender = NULL;
420 if (!result->receiver) free_result( result );
423 /* store the message result in the appropriate structure */
424 static void store_message_result( struct message_result *res, unsigned int result,
427 res->result = result;
432 remove_timeout_user( res->timeout );
437 if (res->callback_msg)
439 /* queue the callback message in the sender queue */
440 res->callback_msg->lparam = result;
441 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
442 set_queue_bits( res->sender, QS_SENDMESSAGE );
443 res->callback_msg = NULL;
444 remove_result_from_sender( res );
448 /* wake sender queue if waiting on this result */
449 if (list_head(&res->sender->send_result) == &res->sender_entry)
450 set_queue_bits( res->sender, QS_SMRESULT );
456 /* free a message when deleting a queue or window */
457 static void free_message( struct message *msg )
459 struct message_result *result = msg->result;
465 result->receiver = NULL;
466 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
468 else free_result( result );
470 if (msg->data) free( msg->data );
474 /* remove (and free) a message from a message list */
475 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
476 enum message_kind kind )
478 list_remove( &msg->entry );
482 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
485 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
486 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
492 /* message timed out without getting a reply */
493 static void result_timeout( void *private )
495 struct message_result *result = private;
497 assert( !result->replied );
499 result->timeout = NULL;
501 if (result->msg) /* not received yet */
503 struct message *msg = result->msg;
507 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
508 result->receiver = NULL;
511 free_result( result );
516 store_message_result( result, 0, STATUS_TIMEOUT );
519 /* allocate and fill a message result structure */
520 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
521 struct msg_queue *recv_queue,
522 struct message *msg, int timeout,
523 void *callback, unsigned long callback_data )
525 struct message_result *result = mem_alloc( sizeof(*result) );
529 result->sender = send_queue;
530 result->receiver = recv_queue;
533 result->data_size = 0;
534 result->timeout = NULL;
536 if (msg->type == MSG_CALLBACK)
538 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
544 callback_msg->type = MSG_CALLBACK_RESULT;
545 callback_msg->win = msg->win;
546 callback_msg->msg = msg->msg;
547 callback_msg->wparam = (unsigned long)callback;
548 callback_msg->lparam = 0;
549 callback_msg->time = get_tick_count();
552 callback_msg->info = callback_data;
553 callback_msg->hook = 0;
554 callback_msg->hook_proc = NULL;
555 callback_msg->result = NULL;
556 callback_msg->data = NULL;
557 callback_msg->data_size = 0;
559 result->callback_msg = callback_msg;
560 list_add_head( &send_queue->callback_result, &result->sender_entry );
564 result->callback_msg = NULL;
565 list_add_head( &send_queue->send_result, &result->sender_entry );
570 struct timeval when = current_time;
571 add_timeout( &when, timeout );
572 result->timeout = add_timeout_user( &when, result_timeout, result );
578 /* receive a message, removing it from the sent queue */
579 static void receive_message( struct msg_queue *queue, struct message *msg,
580 struct get_message_reply *reply )
582 struct message_result *result = msg->result;
584 reply->total = msg->data_size;
585 if (msg->data_size > get_reply_max_size())
587 set_error( STATUS_BUFFER_OVERFLOW );
590 reply->type = msg->type;
591 reply->win = msg->win;
592 reply->msg = msg->msg;
593 reply->wparam = msg->wparam;
594 reply->lparam = msg->lparam;
597 reply->time = msg->time;
598 reply->info = msg->info;
599 reply->hook = msg->hook;
600 reply->hook_proc = msg->hook_proc;
602 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
604 list_remove( &msg->entry );
605 /* put the result on the receiver result stack */
609 result->recv_next = queue->recv_result;
610 queue->recv_result = result;
613 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
616 /* set the result of the current received message */
617 static void reply_message( struct msg_queue *queue, unsigned int result,
618 unsigned int error, int remove, const void *data, data_size_t len )
620 struct message_result *res = queue->recv_result;
624 queue->recv_result = res->recv_next;
625 res->receiver = NULL;
626 if (!res->sender) /* no one waiting for it */
634 if (len && (res->data = memdup( data, len ))) res->data_size = len;
635 store_message_result( res, result, error );
639 /* retrieve a posted message */
640 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
641 unsigned int first, unsigned int last, unsigned int flags,
642 struct get_message_reply *reply )
646 /* check against the filters */
647 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
649 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
650 if (!check_msg_filter( msg->msg, first, last )) continue;
651 goto found; /* found one */
655 /* return it to the app */
657 reply->total = msg->data_size;
658 if (msg->data_size > get_reply_max_size())
660 set_error( STATUS_BUFFER_OVERFLOW );
663 reply->type = msg->type;
664 reply->win = msg->win;
665 reply->msg = msg->msg;
666 reply->wparam = msg->wparam;
667 reply->lparam = msg->lparam;
670 reply->time = msg->time;
671 reply->info = msg->info;
673 if (flags & GET_MSG_REMOVE)
677 set_reply_data_ptr( msg->data, msg->data_size );
681 remove_queue_message( queue, msg, POST_MESSAGE );
683 else if (msg->data) set_reply_data( msg->data, msg->data_size );
688 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
689 struct get_message_reply *reply )
691 if (queue->quit_message)
694 reply->type = MSG_POSTED;
696 reply->msg = WM_QUIT;
697 reply->wparam = queue->exit_code;
701 reply->time = get_tick_count();
704 if (flags & GET_MSG_REMOVE)
706 queue->quit_message = 0;
707 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
708 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
716 /* empty a message list and free all the messages */
717 static void empty_msg_list( struct list *list )
721 while ((ptr = list_head( list )) != NULL)
723 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
724 list_remove( &msg->entry );
729 /* cleanup all pending results when deleting a queue */
730 static void cleanup_results( struct msg_queue *queue )
734 while ((entry = list_head( &queue->send_result )) != NULL)
736 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
739 while ((entry = list_head( &queue->callback_result )) != NULL)
741 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
744 while (queue->recv_result)
745 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
748 /* check if the thread owning the queue is hung (not checking for messages) */
749 static int is_queue_hung( struct msg_queue *queue )
751 struct wait_queue_entry *entry;
753 if (current_time.tv_sec - queue->last_get_msg.tv_sec <= 5)
754 return 0; /* less than 5 seconds since last get message -> not hung */
756 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
758 if (entry->thread->queue == queue)
759 return 0; /* thread is waiting on queue -> not hung */
764 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
766 struct msg_queue *queue = (struct msg_queue *)obj;
767 struct process *process = entry->thread->process;
769 /* a thread can only wait on its own queue */
770 if (entry->thread->queue != queue)
772 set_error( STATUS_ACCESS_DENIED );
775 /* if waiting on the main process queue, set the idle event */
776 if (process->queue == queue)
778 if (process->idle_event) set_event( process->idle_event );
780 add_queue( obj, entry );
784 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
786 struct msg_queue *queue = (struct msg_queue *)obj;
787 struct process *process = entry->thread->process;
789 remove_queue( obj, entry );
791 assert( entry->thread->queue == queue );
793 /* if waiting on the main process queue, reset the idle event */
794 if (process->queue == queue)
796 if (process->idle_event) reset_event( process->idle_event );
800 static void msg_queue_dump( struct object *obj, int verbose )
802 struct msg_queue *queue = (struct msg_queue *)obj;
803 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
804 queue->wake_bits, queue->wake_mask );
807 static int msg_queue_signaled( struct object *obj, struct thread *thread )
809 struct msg_queue *queue = (struct msg_queue *)obj;
810 return is_signaled( queue );
813 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
815 struct msg_queue *queue = (struct msg_queue *)obj;
816 queue->wake_mask = 0;
817 queue->changed_mask = 0;
818 return 0; /* Not abandoned */
821 static void msg_queue_destroy( struct object *obj )
823 struct msg_queue *queue = (struct msg_queue *)obj;
827 cleanup_results( queue );
828 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
830 while ((ptr = list_head( &queue->pending_timers )))
832 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
833 list_remove( &timer->entry );
836 while ((ptr = list_head( &queue->expired_timers )))
838 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
839 list_remove( &timer->entry );
842 if (queue->timeout) remove_timeout_user( queue->timeout );
843 if (queue->input) release_object( queue->input );
844 if (queue->hooks) release_object( queue->hooks );
847 static void thread_input_dump( struct object *obj, int verbose )
849 struct thread_input *input = (struct thread_input *)obj;
850 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
851 input->focus, input->capture, input->active );
854 static void thread_input_destroy( struct object *obj )
856 struct thread_input *input = (struct thread_input *)obj;
858 if (foreground_input == input) foreground_input = NULL;
859 empty_msg_list( &input->msg_list );
860 if (input->desktop) release_object( input->desktop );
863 /* fix the thread input data when a window is destroyed */
864 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
866 struct thread_input *input = queue->input;
868 if (window == input->focus) input->focus = 0;
869 if (window == input->capture) input->capture = 0;
870 if (window == input->active) input->active = 0;
871 if (window == input->menu_owner) input->menu_owner = 0;
872 if (window == input->move_size) input->move_size = 0;
873 if (window == input->caret) set_caret_window( input, 0 );
876 /* check if the specified window can be set in the input data of a given queue */
877 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
879 struct thread *thread;
882 if (!window) return 1; /* we can always clear the data */
884 if ((thread = get_window_thread( window )))
886 ret = (queue->input == thread->queue->input);
887 if (!ret) set_error( STATUS_ACCESS_DENIED );
888 release_object( thread );
890 else set_error( STATUS_INVALID_HANDLE );
895 /* make sure the specified thread has a queue */
896 int init_thread_queue( struct thread *thread )
898 if (thread->queue) return 1;
899 return (create_msg_queue( thread, NULL ) != NULL);
902 /* attach two thread input data structures */
903 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
905 struct desktop *desktop;
906 struct thread_input *input;
908 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
909 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
910 input = (struct thread_input *)grab_object( thread_to->queue->input );
911 if (input->desktop != desktop)
913 set_error( STATUS_ACCESS_DENIED );
914 release_object( input );
915 release_object( desktop );
918 release_object( desktop );
920 if (thread_from->queue)
922 release_thread_input( thread_from );
923 thread_from->queue->input = input;
927 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
929 memset( input->keystate, 0, sizeof(input->keystate) );
933 /* detach two thread input data structures */
934 void detach_thread_input( struct thread *thread_from )
936 struct thread_input *input;
938 if ((input = create_thread_input( thread_from )))
940 release_thread_input( thread_from );
941 thread_from->queue->input = input;
946 /* set the next timer to expire */
947 static void set_next_timer( struct msg_queue *queue )
953 remove_timeout_user( queue->timeout );
954 queue->timeout = NULL;
956 if ((ptr = list_head( &queue->pending_timers )))
958 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
959 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
961 /* set/clear QS_TIMER bit */
962 if (list_empty( &queue->expired_timers ))
963 clear_queue_bits( queue, QS_TIMER );
965 set_queue_bits( queue, QS_TIMER );
968 /* find a timer from its window and id */
969 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
970 unsigned int msg, unsigned int id )
974 /* we need to search both lists */
976 LIST_FOR_EACH( ptr, &queue->pending_timers )
978 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
979 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
981 LIST_FOR_EACH( ptr, &queue->expired_timers )
983 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
984 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
989 /* callback for the next timer expiration */
990 static void timer_callback( void *private )
992 struct msg_queue *queue = private;
995 queue->timeout = NULL;
996 /* move on to the next timer */
997 ptr = list_head( &queue->pending_timers );
999 list_add_tail( &queue->expired_timers, ptr );
1000 set_next_timer( queue );
1003 /* link a timer at its rightful place in the queue list */
1004 static void link_timer( struct msg_queue *queue, struct timer *timer )
1008 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1010 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1011 if (!time_before( &t->when, &timer->when )) break;
1013 list_add_before( ptr, &timer->entry );
1016 /* remove a timer from the queue timer list and free it */
1017 static void free_timer( struct msg_queue *queue, struct timer *timer )
1019 list_remove( &timer->entry );
1021 set_next_timer( queue );
1024 /* restart an expired timer */
1025 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1027 list_remove( &timer->entry );
1028 while (!time_before( ¤t_time, &timer->when )) add_timeout( &timer->when, timer->rate );
1029 link_timer( queue, timer );
1030 set_next_timer( queue );
1033 /* find an expired timer matching the filtering parameters */
1034 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1035 unsigned int get_first, unsigned int get_last,
1040 LIST_FOR_EACH( ptr, &queue->expired_timers )
1042 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1043 if (win && timer->win != win) continue;
1044 if (check_msg_filter( timer->msg, get_first, get_last ))
1046 if (remove) restart_timer( queue, timer );
1054 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1056 struct timer *timer = mem_alloc( sizeof(*timer) );
1059 timer->rate = max( rate, 1 );
1060 timer->when = current_time;
1061 add_timeout( &timer->when, rate );
1062 link_timer( queue, timer );
1063 /* check if we replaced the next timer */
1064 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1069 /* change the input key state for a given key */
1070 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1074 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1075 input->keystate[key] |= 0x80;
1077 else input->keystate[key] &= ~0x80;
1080 /* update the input key state for a keyboard message */
1081 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1084 int down = 0, extended;
1088 case WM_LBUTTONDOWN:
1092 set_input_key_state( input, VK_LBUTTON, down );
1094 case WM_MBUTTONDOWN:
1098 set_input_key_state( input, VK_MBUTTON, down );
1100 case WM_RBUTTONDOWN:
1104 set_input_key_state( input, VK_RBUTTON, down );
1106 case WM_XBUTTONDOWN:
1110 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1111 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1119 key = (unsigned char)msg->wparam;
1120 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1121 set_input_key_state( input, key, down );
1125 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1128 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1131 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1135 set_input_key_state( input, VK_CONTROL, down );
1139 set_input_key_state( input, VK_MENU, down );
1143 set_input_key_state( input, VK_SHIFT, down );
1150 /* release the hardware message currently being processed by the given thread */
1151 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1152 int remove, user_handle_t new_win )
1154 struct thread_input *input = queue->input;
1155 struct message *msg;
1157 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1159 if (msg->unique_id == hw_id) break;
1161 if (&msg->entry == &input->msg_list) return; /* not found */
1163 /* clear the queue bit for that message */
1164 if (remove || new_win)
1166 struct message *other;
1169 clr_bit = get_hardware_msg_bit( msg );
1170 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1172 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1178 if (clr_bit) clear_queue_bits( queue, clr_bit );
1181 if (new_win) /* set the new window */
1183 struct thread *owner = get_window_thread( new_win );
1186 if (owner->queue->input == input)
1189 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1192 release_object( owner );
1197 update_input_key_state( input, msg );
1198 list_remove( &msg->entry );
1199 free_message( msg );
1203 /* find the window that should receive a given hardware message */
1204 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1205 unsigned int *msg_code )
1207 user_handle_t win = 0;
1209 *msg_code = msg->msg;
1210 if (is_keyboard_msg( msg ))
1212 if (input && !(win = input->focus))
1214 win = input->active;
1215 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1218 else /* mouse message */
1220 if (!input || !(win = input->capture))
1222 if (!(win = msg->win) || !is_window_visible( win ))
1224 if (input) win = window_from_point( input->desktop, msg->x, msg->y );
1231 /* queue a hardware message into a given thread input */
1232 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1235 struct thread *thread;
1236 struct thread_input *input = queue ? queue->input : foreground_input;
1237 unsigned int msg_code;
1239 last_input_time = get_tick_count();
1240 win = find_hardware_message_window( input, msg, &msg_code );
1241 if (!win || !(thread = get_window_thread(win)))
1243 if (input) update_input_key_state( input, msg );
1247 input = thread->queue->input;
1249 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1252 msg->unique_id = 0; /* will be set once we return it to the app */
1253 list_add_tail( &input->msg_list, &msg->entry );
1254 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1256 release_object( thread );
1259 /* check message filter for a hardware message */
1260 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1261 user_handle_t filter_win, unsigned int first, unsigned int last )
1263 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1265 /* we can only test the window for a keyboard message since the
1266 * dest window for a mouse message depends on hittest */
1267 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1269 /* the message code is final for a keyboard message, we can simply check it */
1270 return check_msg_filter( msg_code, first, last );
1272 else /* mouse message */
1274 /* we need to check all possible values that the message can have in the end */
1276 if (check_msg_filter( msg_code, first, last )) return 1;
1277 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1279 /* all other messages can become non-client messages */
1280 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1282 /* clicks can become double-clicks or non-client double-clicks */
1283 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1284 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1286 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1287 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1294 /* find a hardware message for the given queue */
1295 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1296 unsigned int first, unsigned int last, struct get_message_reply *reply )
1298 struct thread_input *input = thread->queue->input;
1299 struct thread *win_thread;
1302 int clear_bits, got_one = 0;
1303 unsigned int msg_code;
1305 ptr = list_head( &input->msg_list );
1310 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1311 if (msg->unique_id == hw_id) break;
1312 ptr = list_next( &input->msg_list, ptr );
1314 if (!ptr) ptr = list_head( &input->msg_list );
1315 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1318 if (ptr == list_head( &input->msg_list ))
1319 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1321 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1325 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1326 ptr = list_next( &input->msg_list, ptr );
1327 win = find_hardware_message_window( input, msg, &msg_code );
1328 if (!win || !(win_thread = get_window_thread( win )))
1330 /* no window at all, remove it */
1331 update_input_key_state( input, msg );
1332 list_remove( &msg->entry );
1333 free_message( msg );
1336 if (win_thread != thread)
1338 if (win_thread->queue->input == input)
1340 /* wake the other thread */
1341 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1346 /* for another thread input, drop it */
1347 update_input_key_state( input, msg );
1348 list_remove( &msg->entry );
1349 free_message( msg );
1351 release_object( win_thread );
1354 release_object( win_thread );
1356 /* if we already got a message for another thread, or if it doesn't
1357 * match the filter we skip it */
1358 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1360 clear_bits &= ~get_hardware_msg_bit( msg );
1363 /* now we can return it */
1364 if (!msg->unique_id) msg->unique_id = get_unique_id();
1365 reply->type = MSG_HARDWARE;
1367 reply->msg = msg_code;
1368 reply->wparam = msg->wparam;
1369 reply->lparam = msg->lparam;
1372 reply->time = msg->time;
1373 reply->info = msg->info;
1374 reply->hw_id = msg->unique_id;
1377 /* nothing found, clear the hardware queue bits */
1378 clear_queue_bits( thread->queue, clear_bits );
1382 /* increment (or decrement if 'incr' is negative) the queue paint count */
1383 void inc_queue_paint_count( struct thread *thread, int incr )
1385 struct msg_queue *queue = thread->queue;
1389 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1391 if (queue->paint_count)
1392 set_queue_bits( queue, QS_PAINT );
1394 clear_queue_bits( queue, QS_PAINT );
1398 /* remove all messages and timers belonging to a certain window */
1399 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1401 struct msg_queue *queue = thread->queue;
1409 ptr = list_head( &queue->pending_timers );
1412 struct list *next = list_next( &queue->pending_timers, ptr );
1413 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1414 if (timer->win == win) free_timer( queue, timer );
1417 ptr = list_head( &queue->expired_timers );
1420 struct list *next = list_next( &queue->expired_timers, ptr );
1421 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1422 if (timer->win == win) free_timer( queue, timer );
1426 /* remove messages */
1427 for (i = 0; i < NB_MSG_KINDS; i++)
1429 struct list *ptr, *next;
1431 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1433 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1434 if (msg->win == win) remove_queue_message( queue, msg, i );
1438 thread_input_cleanup_window( queue, win );
1441 /* post a message to a window; used by socket handling */
1442 void post_message( user_handle_t win, unsigned int message,
1443 unsigned long wparam, unsigned long lparam )
1445 struct message *msg;
1446 struct thread *thread = get_window_thread( win );
1448 if (!thread) return;
1450 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1452 msg->type = MSG_POSTED;
1453 msg->win = get_user_full_handle( win );
1455 msg->wparam = wparam;
1456 msg->lparam = lparam;
1457 msg->time = get_tick_count();
1462 msg->hook_proc = NULL;
1467 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1468 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1470 release_object( thread );
1473 /* post a win event */
1474 void post_win_event( struct thread *thread, unsigned int event,
1475 user_handle_t win, unsigned int object_id,
1476 unsigned int child_id, void *hook_proc,
1477 const WCHAR *module, data_size_t module_size,
1480 struct message *msg;
1482 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1484 msg->type = MSG_WINEVENT;
1485 msg->win = get_user_full_handle( win );
1487 msg->wparam = object_id;
1488 msg->lparam = child_id;
1489 msg->time = get_tick_count();
1492 msg->info = get_thread_id( current );
1495 msg->hook_proc = hook_proc;
1497 if ((msg->data = malloc( module_size )))
1499 msg->data_size = module_size;
1500 memcpy( msg->data, module, module_size );
1502 if (debug_level > 1)
1503 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1504 get_thread_id(thread), event, win, object_id, child_id );
1505 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1506 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1513 /* get the message queue of the current thread */
1514 DECL_HANDLER(get_msg_queue)
1516 struct msg_queue *queue = get_current_queue();
1519 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1523 /* set the current message queue wakeup mask */
1524 DECL_HANDLER(set_queue_mask)
1526 struct msg_queue *queue = get_current_queue();
1530 queue->wake_mask = req->wake_mask;
1531 queue->changed_mask = req->changed_mask;
1532 reply->wake_bits = queue->wake_bits;
1533 reply->changed_bits = queue->changed_bits;
1534 if (is_signaled( queue ))
1536 /* if skip wait is set, do what would have been done in the subsequent wait */
1537 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1538 else wake_up( &queue->obj, 0 );
1544 /* get the current message queue status */
1545 DECL_HANDLER(get_queue_status)
1547 struct msg_queue *queue = current->queue;
1550 reply->wake_bits = queue->wake_bits;
1551 reply->changed_bits = queue->changed_bits;
1552 if (req->clear) queue->changed_bits = 0;
1554 else reply->wake_bits = reply->changed_bits = 0;
1558 /* send a message to a thread queue */
1559 DECL_HANDLER(send_message)
1561 struct message *msg;
1562 struct msg_queue *send_queue = get_current_queue();
1563 struct msg_queue *recv_queue = NULL;
1564 struct thread *thread = NULL;
1566 if (!(thread = get_thread_from_id( req->id ))) return;
1568 if (!(recv_queue = thread->queue))
1570 set_error( STATUS_INVALID_PARAMETER );
1571 release_object( thread );
1574 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1576 set_error( STATUS_TIMEOUT );
1577 release_object( thread );
1581 if ((msg = mem_alloc( sizeof(*msg) )))
1583 msg->type = req->type;
1584 msg->win = get_user_full_handle( req->win );
1585 msg->msg = req->msg;
1586 msg->wparam = req->wparam;
1587 msg->lparam = req->lparam;
1588 msg->time = get_tick_count();
1591 msg->info = req->info;
1593 msg->hook_proc = NULL;
1600 case MSG_OTHER_PROCESS:
1601 msg->data_size = get_req_data_size();
1602 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1611 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1612 req->timeout, req->callback, req->info )))
1614 free_message( msg );
1619 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1620 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1623 /* needed for posted DDE messages */
1624 msg->data_size = get_req_data_size();
1625 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1630 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1631 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1633 case MSG_HARDWARE: /* should use send_hardware_message instead */
1634 case MSG_CALLBACK_RESULT: /* cannot send this one */
1636 set_error( STATUS_INVALID_PARAMETER );
1641 release_object( thread );
1644 /* send a hardware message to a thread queue */
1645 DECL_HANDLER(send_hardware_message)
1647 struct message *msg;
1648 struct msg_queue *recv_queue = NULL;
1649 struct thread *thread = NULL;
1653 if (!(thread = get_thread_from_id( req->id ))) return;
1656 if (thread && !(recv_queue = thread->queue))
1658 set_error( STATUS_INVALID_PARAMETER );
1659 release_object( thread );
1663 if ((msg = mem_alloc( sizeof(*msg) )))
1665 msg->type = MSG_HARDWARE;
1666 msg->win = get_user_full_handle( req->win );
1667 msg->msg = req->msg;
1668 msg->wparam = req->wparam;
1669 msg->lparam = req->lparam;
1670 msg->time = req->time;
1673 msg->info = req->info;
1675 msg->hook_proc = NULL;
1679 queue_hardware_message( recv_queue, msg );
1681 if (thread) release_object( thread );
1684 /* post a quit message to the current queue */
1685 DECL_HANDLER(post_quit_message)
1687 struct msg_queue *queue = get_current_queue();
1692 queue->quit_message = 1;
1693 queue->exit_code = req->exit_code;
1694 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1697 /* get a message from the current queue */
1698 DECL_HANDLER(get_message)
1700 struct timer *timer;
1702 struct msg_queue *queue = get_current_queue();
1703 user_handle_t get_win = get_user_full_handle( req->get_win );
1705 reply->active_hooks = get_active_hooks();
1708 queue->last_get_msg = current_time;
1710 /* first check for sent messages */
1711 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1713 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1714 receive_message( queue, msg, reply );
1717 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1719 /* clear changed bits so we can wait on them if we don't find a message */
1720 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits = 0;
1721 else queue->changed_bits &= QS_ALLPOSTMESSAGE;
1723 /* then check for posted messages */
1724 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1727 /* only check for quit messages if not posted messages pending.
1728 * note: the quit message isn't filtered */
1729 if (get_quit_message( queue, req->flags, reply ))
1732 /* then check for any raw hardware message */
1733 if (filter_contains_hw_range( req->get_first, req->get_last ) &&
1734 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1737 /* now check for WM_PAINT */
1738 if (queue->paint_count &&
1739 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1740 (reply->win = find_window_to_repaint( get_win, current )))
1742 reply->type = MSG_POSTED;
1743 reply->msg = WM_PAINT;
1748 reply->time = get_tick_count();
1753 /* now check for timer */
1754 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1755 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1757 reply->type = MSG_POSTED;
1758 reply->win = timer->win;
1759 reply->msg = timer->msg;
1760 reply->wparam = timer->id;
1761 reply->lparam = timer->lparam;
1764 reply->time = get_tick_count();
1770 set_error( STATUS_PENDING ); /* FIXME */
1774 /* reply to a sent message */
1775 DECL_HANDLER(reply_message)
1777 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1778 else if (current->queue->recv_result)
1779 reply_message( current->queue, req->result, 0, req->remove,
1780 get_req_data(), get_req_data_size() );
1784 /* accept the current hardware message */
1785 DECL_HANDLER(accept_hardware_message)
1788 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1790 set_error( STATUS_ACCESS_DENIED );
1794 /* retrieve the reply for the last message sent */
1795 DECL_HANDLER(get_message_reply)
1797 struct message_result *result;
1799 struct msg_queue *queue = current->queue;
1803 set_error( STATUS_PENDING );
1806 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1808 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1809 if (result->replied || req->cancel)
1811 if (result->replied)
1813 reply->result = result->result;
1814 set_error( result->error );
1817 data_size_t data_len = min( result->data_size, get_reply_max_size() );
1818 set_reply_data_ptr( result->data, data_len );
1819 result->data = NULL;
1820 result->data_size = 0;
1823 remove_result_from_sender( result );
1825 entry = list_head( &queue->send_result );
1826 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1829 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1830 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1834 else set_error( STATUS_ACCESS_DENIED );
1838 /* set a window timer */
1839 DECL_HANDLER(set_win_timer)
1841 struct timer *timer;
1842 struct msg_queue *queue;
1843 struct thread *thread = NULL;
1844 user_handle_t win = 0;
1845 unsigned int id = req->id;
1849 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1851 set_error( STATUS_INVALID_HANDLE );
1854 if (thread->process != current->process)
1856 release_object( thread );
1857 set_error( STATUS_ACCESS_DENIED );
1860 queue = thread->queue;
1861 /* remove it if it existed already */
1862 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1866 queue = get_current_queue();
1867 /* find a free id for it */
1870 id = queue->next_timer_id;
1871 if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1873 while (find_timer( queue, 0, req->msg, id ));
1876 if ((timer = set_timer( queue, req->rate )))
1879 timer->msg = req->msg;
1881 timer->lparam = req->lparam;
1884 if (thread) release_object( thread );
1887 /* kill a window timer */
1888 DECL_HANDLER(kill_win_timer)
1890 struct timer *timer;
1891 struct thread *thread;
1892 user_handle_t win = 0;
1896 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1898 set_error( STATUS_INVALID_HANDLE );
1901 if (thread->process != current->process)
1903 release_object( thread );
1904 set_error( STATUS_ACCESS_DENIED );
1908 else thread = (struct thread *)grab_object( current );
1910 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1911 free_timer( thread->queue, timer );
1913 set_error( STATUS_INVALID_PARAMETER );
1915 release_object( thread );
1919 /* attach (or detach) thread inputs */
1920 DECL_HANDLER(attach_thread_input)
1922 struct thread *thread_from = get_thread_from_id( req->tid_from );
1923 struct thread *thread_to = get_thread_from_id( req->tid_to );
1925 if (!thread_from || !thread_to)
1927 if (thread_from) release_object( thread_from );
1928 if (thread_to) release_object( thread_to );
1931 if (thread_from != thread_to)
1933 if (req->attach) attach_thread_input( thread_from, thread_to );
1936 if (thread_from->queue && thread_to->queue &&
1937 thread_from->queue->input == thread_to->queue->input)
1938 detach_thread_input( thread_from );
1940 set_error( STATUS_ACCESS_DENIED );
1943 else set_error( STATUS_ACCESS_DENIED );
1944 release_object( thread_from );
1945 release_object( thread_to );
1949 /* get thread input data */
1950 DECL_HANDLER(get_thread_input)
1952 struct thread *thread = NULL;
1953 struct thread_input *input;
1957 if (!(thread = get_thread_from_id( req->tid ))) return;
1958 input = thread->queue ? thread->queue->input : NULL;
1960 else input = foreground_input; /* get the foreground thread info */
1964 reply->focus = input->focus;
1965 reply->capture = input->capture;
1966 reply->active = input->active;
1967 reply->menu_owner = input->menu_owner;
1968 reply->move_size = input->move_size;
1969 reply->caret = input->caret;
1970 reply->rect = input->caret_rect;
1977 reply->menu_owner = 0;
1978 reply->move_size = 0;
1980 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1982 /* foreground window is active window of foreground thread */
1983 reply->foreground = foreground_input ? foreground_input->active : 0;
1984 if (thread) release_object( thread );
1988 /* retrieve queue keyboard state for a given thread */
1989 DECL_HANDLER(get_key_state)
1991 struct thread *thread;
1992 struct thread_input *input;
1994 if (!(thread = get_thread_from_id( req->tid ))) return;
1995 input = thread->queue ? thread->queue->input : NULL;
1998 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1999 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
2001 release_object( thread );
2005 /* set queue keyboard state for a given thread */
2006 DECL_HANDLER(set_key_state)
2008 struct thread *thread = NULL;
2009 struct thread_input *input;
2011 if (!(thread = get_thread_from_id( req->tid ))) return;
2012 input = thread->queue ? thread->queue->input : NULL;
2015 data_size_t size = min( sizeof(input->keystate), get_req_data_size() );
2016 if (size) memcpy( input->keystate, get_req_data(), size );
2018 release_object( thread );
2022 /* set the system foreground window */
2023 DECL_HANDLER(set_foreground_window)
2025 struct thread *thread;
2026 struct msg_queue *queue = get_current_queue();
2028 reply->previous = foreground_input ? foreground_input->active : 0;
2029 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
2030 reply->send_msg_new = FALSE;
2032 if (is_top_level_window( req->handle ) &&
2033 ((thread = get_window_thread( req->handle ))))
2035 foreground_input = thread->queue->input;
2036 reply->send_msg_new = (foreground_input != queue->input);
2037 release_object( thread );
2039 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2043 /* set the current thread focus window */
2044 DECL_HANDLER(set_focus_window)
2046 struct msg_queue *queue = get_current_queue();
2048 reply->previous = 0;
2049 if (queue && check_queue_input_window( queue, req->handle ))
2051 reply->previous = queue->input->focus;
2052 queue->input->focus = get_user_full_handle( req->handle );
2057 /* set the current thread active window */
2058 DECL_HANDLER(set_active_window)
2060 struct msg_queue *queue = get_current_queue();
2062 reply->previous = 0;
2063 if (queue && check_queue_input_window( queue, req->handle ))
2065 if (!req->handle || make_window_active( req->handle ))
2067 reply->previous = queue->input->active;
2068 queue->input->active = get_user_full_handle( req->handle );
2070 else set_error( STATUS_INVALID_HANDLE );
2075 /* set the current thread capture window */
2076 DECL_HANDLER(set_capture_window)
2078 struct msg_queue *queue = get_current_queue();
2080 reply->previous = reply->full_handle = 0;
2081 if (queue && check_queue_input_window( queue, req->handle ))
2083 struct thread_input *input = queue->input;
2085 reply->previous = input->capture;
2086 input->capture = get_user_full_handle( req->handle );
2087 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2088 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2089 reply->full_handle = input->capture;
2094 /* Set the current thread caret window */
2095 DECL_HANDLER(set_caret_window)
2097 struct msg_queue *queue = get_current_queue();
2099 reply->previous = 0;
2100 if (queue && check_queue_input_window( queue, req->handle ))
2102 struct thread_input *input = queue->input;
2104 reply->previous = input->caret;
2105 reply->old_rect = input->caret_rect;
2106 reply->old_hide = input->caret_hide;
2107 reply->old_state = input->caret_state;
2109 set_caret_window( input, get_user_full_handle(req->handle) );
2110 input->caret_rect.right = input->caret_rect.left + req->width;
2111 input->caret_rect.bottom = input->caret_rect.top + req->height;
2116 /* Set the current thread caret information */
2117 DECL_HANDLER(set_caret_info)
2119 struct msg_queue *queue = get_current_queue();
2120 struct thread_input *input;
2123 input = queue->input;
2124 reply->full_handle = input->caret;
2125 reply->old_rect = input->caret_rect;
2126 reply->old_hide = input->caret_hide;
2127 reply->old_state = input->caret_state;
2129 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2131 set_error( STATUS_ACCESS_DENIED );
2134 if (req->flags & SET_CARET_POS)
2136 input->caret_rect.right += req->x - input->caret_rect.left;
2137 input->caret_rect.bottom += req->y - input->caret_rect.top;
2138 input->caret_rect.left = req->x;
2139 input->caret_rect.top = req->y;
2141 if (req->flags & SET_CARET_HIDE)
2143 input->caret_hide += req->hide;
2144 if (input->caret_hide < 0) input->caret_hide = 0;
2146 if (req->flags & SET_CARET_STATE)
2148 if (req->state == -1) input->caret_state = !input->caret_state;
2149 else input->caret_state = !!req->state;
2154 /* get the time of the last input event */
2155 DECL_HANDLER(get_last_input_time)
2157 reply->time = last_input_time;