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"
38 enum message_kind { SEND_MESSAGE, POST_MESSAGE, COOKED_HW_MESSAGE, RAW_HW_MESSAGE };
39 #define NB_MSG_KINDS (RAW_HW_MESSAGE+1)
44 struct message_result *send_next; /* next in sender list */
45 struct message_result *recv_next; /* next in receiver list */
46 struct msg_queue *sender; /* sender queue */
47 struct msg_queue *receiver; /* receiver queue */
48 int replied; /* has it been replied to? */
49 unsigned int result; /* reply result */
50 unsigned int error; /* error code to pass back to sender */
51 void *data; /* message reply data */
52 unsigned int data_size; /* size of message reply data */
53 struct timeout_user *timeout; /* result timeout */
58 struct message *next; /* next message in list */
59 struct message *prev; /* prev message in list */
60 enum message_type type; /* message type */
61 user_handle_t win; /* window handle */
62 unsigned int msg; /* message code */
63 unsigned int wparam; /* parameters */
64 unsigned int lparam; /* parameters */
65 int x; /* x position */
66 int y; /* y position */
67 unsigned int time; /* message time */
68 unsigned int info; /* extra info */
69 void *data; /* message data for sent messages */
70 unsigned int data_size; /* size of message data */
71 struct message_result *result; /* result in sender queue */
76 struct message *first; /* head of list */
77 struct message *last; /* tail of list */
82 struct timer *next; /* next timer in list */
83 struct timer *prev; /* prev timer in list */
84 struct timeval when; /* next expiration */
85 unsigned int rate; /* timer rate in ms */
86 user_handle_t win; /* window handle */
87 unsigned int msg; /* message to post */
88 unsigned int id; /* timer id */
89 unsigned int lparam; /* lparam for message */
94 struct object obj; /* object header */
95 user_handle_t focus; /* focus window */
96 user_handle_t capture; /* capture window */
97 user_handle_t active; /* active window */
98 user_handle_t menu_owner; /* current menu owner window */
99 user_handle_t move_size; /* current moving/resizing window */
100 user_handle_t caret; /* caret window */
101 rectangle_t rect; /* caret rectangle */
102 unsigned char keystate[256]; /* state of each key */
107 struct object obj; /* object header */
108 unsigned int wake_bits; /* wakeup bits */
109 unsigned int wake_mask; /* wakeup mask */
110 unsigned int changed_bits; /* changed wakeup bits */
111 unsigned int changed_mask; /* changed wakeup mask */
112 int paint_count; /* pending paint messages count */
113 struct message_list msg_list[NB_MSG_KINDS]; /* lists of messages */
114 struct message_result *send_result; /* stack of sent messages waiting for result */
115 struct message_result *recv_result; /* stack of received messages waiting for result */
116 struct message *last_msg; /* last msg returned to the app and not removed */
117 enum message_kind last_msg_kind; /* message kind of last_msg */
118 struct timer *first_timer; /* head of timer list */
119 struct timer *last_timer; /* tail of timer list */
120 struct timer *next_timer; /* next timer to expire */
121 struct timeout_user *timeout; /* timeout for next timer to expire */
122 struct thread_input *input; /* thread input descriptor */
125 static void msg_queue_dump( struct object *obj, int verbose );
126 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
127 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
128 static int msg_queue_signaled( struct object *obj, struct thread *thread );
129 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
130 static void msg_queue_destroy( struct object *obj );
131 static void thread_input_dump( struct object *obj, int verbose );
132 static void thread_input_destroy( struct object *obj );
133 static void timer_callback( void *private );
135 static const struct object_ops msg_queue_ops =
137 sizeof(struct msg_queue), /* size */
138 msg_queue_dump, /* dump */
139 msg_queue_add_queue, /* add_queue */
140 msg_queue_remove_queue, /* remove_queue */
141 msg_queue_signaled, /* signaled */
142 msg_queue_satisfied, /* satisfied */
143 NULL, /* get_poll_events */
144 NULL, /* poll_event */
145 no_get_fd, /* get_fd */
146 no_flush, /* flush */
147 no_get_file_info, /* get_file_info */
148 NULL, /* queue_async */
149 msg_queue_destroy /* destroy */
153 static const struct object_ops thread_input_ops =
155 sizeof(struct thread_input), /* size */
156 thread_input_dump, /* dump */
157 no_add_queue, /* add_queue */
158 NULL, /* remove_queue */
160 NULL, /* satisfied */
161 NULL, /* get_poll_events */
162 NULL, /* poll_event */
163 no_get_fd, /* get_fd */
164 no_flush, /* flush */
165 no_get_file_info, /* get_file_info */
166 NULL, /* queue_async */
167 thread_input_destroy /* destroy */
170 /* create a thread input object */
171 static struct thread_input *create_thread_input(void)
173 struct thread_input *input;
175 if ((input = alloc_object( &thread_input_ops, -1 )))
180 input->menu_owner = 0;
181 input->move_size = 0;
183 input->rect.left = 0;
185 input->rect.right = 0;
186 input->rect.bottom = 0;
187 memset( input->keystate, 0, sizeof(input->keystate) );
192 /* pointer to input structure of foreground thread */
193 static struct thread_input *foreground_input;
195 /* create a message queue object */
196 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
198 struct msg_queue *queue;
201 if (!input && !(input = create_thread_input())) return NULL;
202 if ((queue = alloc_object( &msg_queue_ops, -1 )))
204 queue->wake_bits = 0;
205 queue->wake_mask = 0;
206 queue->changed_bits = 0;
207 queue->changed_mask = 0;
208 queue->paint_count = 0;
209 queue->send_result = NULL;
210 queue->recv_result = NULL;
211 queue->last_msg = NULL;
212 queue->first_timer = NULL;
213 queue->last_timer = NULL;
214 queue->next_timer = NULL;
215 queue->timeout = NULL;
216 queue->input = (struct thread_input *)grab_object( input );
217 for (i = 0; i < NB_MSG_KINDS; i++)
218 queue->msg_list[i].first = queue->msg_list[i].last = NULL;
220 thread->queue = queue;
221 if (!thread->process->queue)
222 thread->process->queue = (struct msg_queue *)grab_object( queue );
224 release_object( input );
228 /* free the message queue of a thread at thread exit */
229 void free_msg_queue( struct thread *thread )
231 struct process *process = thread->process;
233 if (!thread->queue) return;
234 if (process->queue == thread->queue) /* is it the process main queue? */
236 release_object( process->queue );
237 process->queue = NULL;
238 if (process->idle_event)
240 set_event( process->idle_event );
241 release_object( process->idle_event );
242 process->idle_event = NULL;
245 release_object( thread->queue );
246 thread->queue = NULL;
249 /* check the queue status */
250 inline static int is_signaled( struct msg_queue *queue )
252 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
255 /* set some queue bits */
256 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
258 queue->wake_bits |= bits;
259 queue->changed_bits |= bits;
260 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
263 /* clear some queue bits */
264 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
266 queue->wake_bits &= ~bits;
267 queue->changed_bits &= ~bits;
270 /* get the QS_* bit corresponding to a given hardware message */
271 inline static int get_hardware_msg_bit( struct message *msg )
273 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
274 if (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST) return QS_KEY;
275 return QS_MOUSEBUTTON;
278 /* get the current thread queue, creating it if needed */
279 inline static struct msg_queue *get_current_queue(void)
281 struct msg_queue *queue = current->queue;
282 if (!queue) queue = create_msg_queue( current, NULL );
286 /* append a message to the end of a list */
287 inline static void append_message( struct message_list *list, struct message *msg )
290 if ((msg->prev = list->last)) msg->prev->next = msg;
291 else list->first = msg;
295 /* unlink a message from a list it */
296 inline static void unlink_message( struct message_list *list, struct message *msg )
298 if (msg->next) msg->next->prev = msg->prev;
299 else list->last = msg->prev;
300 if (msg->prev) msg->prev->next = msg->next;
301 else list->first = msg->next;
304 /* try to merge a message with the last in the list; return 1 if successful */
305 static int merge_message( struct message_list *list, const struct message *msg )
307 struct message *prev = list->last;
310 if (prev->result) return 0;
311 if (prev->win != msg->win) return 0;
312 if (prev->msg != msg->msg) return 0;
313 if (prev->type != msg->type) return 0;
314 /* now we can merge it */
315 prev->wparam = msg->wparam;
316 prev->lparam = msg->lparam;
319 prev->time = msg->time;
320 prev->info = msg->info;
324 /* free a result structure */
325 static void free_result( struct message_result *result )
327 if (result->timeout) remove_timeout_user( result->timeout );
328 if (result->data) free( result->data );
332 /* store the message result in the appropriate structure */
333 static void store_message_result( struct message_result *res, unsigned int result,
336 res->result = result;
341 remove_timeout_user( res->timeout );
344 /* wake sender queue if waiting on this result */
345 if (res->sender && res->sender->send_result == res)
346 set_queue_bits( res->sender, QS_SMRESULT );
349 /* free a message when deleting a queue or window */
350 static void free_message( struct message *msg )
352 struct message_result *result = msg->result;
357 result->receiver = NULL;
358 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
360 else free_result( result );
362 if (msg->data) free( msg->data );
366 /* remove (and free) a message from a message list */
367 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
368 enum message_kind kind )
371 struct message *other;
373 if (queue->last_msg == msg) queue->last_msg = NULL;
374 unlink_message( &queue->msg_list[kind], msg );
378 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_SENDMESSAGE );
381 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_POSTMESSAGE );
383 case COOKED_HW_MESSAGE:
385 clr_bit = get_hardware_msg_bit( msg );
386 for (other = queue->msg_list[kind].first; other; other = other->next)
387 if (get_hardware_msg_bit( other ) == clr_bit) break;
388 if (!other) clear_queue_bits( queue, clr_bit );
394 /* message timed out without getting a reply */
395 static void result_timeout( void *private )
397 struct message_result *result = private;
399 assert( !result->replied );
401 result->timeout = NULL;
402 store_message_result( result, 0, STATUS_TIMEOUT );
405 /* allocate and fill a message result structure */
406 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
407 struct msg_queue *recv_queue,
408 unsigned int timeout )
410 struct message_result *result = mem_alloc( sizeof(*result) );
413 /* put the result on the sender result stack */
414 result->sender = send_queue;
415 result->receiver = recv_queue;
418 result->data_size = 0;
419 result->timeout = NULL;
420 result->send_next = send_queue->send_result;
421 send_queue->send_result = result;
425 gettimeofday( &when, 0 );
426 add_timeout( &when, timeout );
427 result->timeout = add_timeout_user( &when, result_timeout, result );
433 /* receive a message, removing it from the sent queue */
434 static void receive_message( struct msg_queue *queue, struct message *msg,
435 struct get_message_reply *reply )
437 struct message_result *result = msg->result;
439 reply->total = msg->data_size;
440 if (msg->data_size > get_reply_max_size())
442 set_error( STATUS_BUFFER_OVERFLOW );
445 reply->type = msg->type;
446 reply->win = msg->win;
447 reply->msg = msg->msg;
448 reply->wparam = msg->wparam;
449 reply->lparam = msg->lparam;
452 reply->time = msg->time;
453 reply->info = msg->info;
455 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
457 unlink_message( &queue->msg_list[SEND_MESSAGE], msg );
458 /* put the result on the receiver result stack */
461 result->recv_next = queue->recv_result;
462 queue->recv_result = result;
465 if (!queue->msg_list[SEND_MESSAGE].first) clear_queue_bits( queue, QS_SENDMESSAGE );
468 /* set the result of the current received message */
469 static void reply_message( struct msg_queue *queue, unsigned int result,
470 unsigned int error, int remove, const void *data, size_t len )
472 struct message_result *res = queue->recv_result;
476 queue->recv_result = res->recv_next;
477 res->receiver = NULL;
478 if (!res->sender) /* no one waiting for it */
486 if (len && (res->data = memdup( data, len ))) res->data_size = len;
487 store_message_result( res, result, error );
491 /* empty a message list and free all the messages */
492 static void empty_msg_list( struct message_list *list )
494 struct message *msg = list->first;
497 struct message *next = msg->next;
503 /* cleanup all pending results when deleting a queue */
504 static void cleanup_results( struct msg_queue *queue )
506 struct message_result *result, *next;
508 result = queue->send_result;
511 next = result->send_next;
512 result->sender = NULL;
513 if (!result->receiver) free_result( result );
517 while (queue->recv_result)
518 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
521 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
523 struct msg_queue *queue = (struct msg_queue *)obj;
524 struct process *process = entry->thread->process;
526 /* a thread can only wait on its own queue */
527 if (entry->thread->queue != queue)
529 set_error( STATUS_ACCESS_DENIED );
532 /* if waiting on the main process queue, set the idle event */
533 if (process->queue == queue)
535 if (process->idle_event) set_event( process->idle_event );
537 add_queue( obj, entry );
541 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
543 struct msg_queue *queue = (struct msg_queue *)obj;
544 struct process *process = entry->thread->process;
546 remove_queue( obj, entry );
548 assert( entry->thread->queue == queue );
550 /* if waiting on the main process queue, reset the idle event */
551 if (process->queue == queue)
553 if (process->idle_event) reset_event( process->idle_event );
557 static void msg_queue_dump( struct object *obj, int verbose )
559 struct msg_queue *queue = (struct msg_queue *)obj;
560 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
561 queue->wake_bits, queue->wake_mask );
564 static int msg_queue_signaled( struct object *obj, struct thread *thread )
566 struct msg_queue *queue = (struct msg_queue *)obj;
567 return is_signaled( queue );
570 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
572 struct msg_queue *queue = (struct msg_queue *)obj;
573 queue->wake_mask = 0;
574 queue->changed_mask = 0;
575 return 0; /* Not abandoned */
578 static void msg_queue_destroy( struct object *obj )
580 struct msg_queue *queue = (struct msg_queue *)obj;
581 struct timer *timer = queue->first_timer;
584 cleanup_results( queue );
585 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
589 struct timer *next = timer->next;
593 if (queue->timeout) remove_timeout_user( queue->timeout );
594 if (queue->input) release_object( queue->input );
597 static void thread_input_dump( struct object *obj, int verbose )
599 struct thread_input *input = (struct thread_input *)obj;
600 fprintf( stderr, "Thread input focus=%x capture=%x active=%x\n",
601 input->focus, input->capture, input->active );
604 static void thread_input_destroy( struct object *obj )
606 struct thread_input *input = (struct thread_input *)obj;
608 if (foreground_input == input) foreground_input = NULL;
611 /* fix the thread input data when a window is destroyed */
612 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
614 struct thread_input *input = queue->input;
616 if (window == input->focus) input->focus = 0;
617 if (window == input->capture) input->capture = 0;
618 if (window == input->active) input->active = 0;
619 if (window == input->menu_owner) input->menu_owner = 0;
620 if (window == input->move_size) input->move_size = 0;
621 if (window == input->caret) input->caret = 0;
624 /* check if the specified window can be set in the input data of a given queue */
625 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
627 struct thread *thread;
630 if (!window) return 1; /* we can always clear the data */
632 if ((thread = get_window_thread( window )))
634 ret = (queue->input == thread->queue->input);
635 if (!ret) set_error( STATUS_ACCESS_DENIED );
636 release_object( thread );
638 else set_error( STATUS_INVALID_HANDLE );
643 /* attach two thread input data structures */
644 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
646 struct thread_input *input;
648 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
649 input = (struct thread_input *)grab_object( thread_to->queue->input );
651 if (thread_from->queue)
653 release_object( thread_from->queue->input );
654 thread_from->queue->input = input;
658 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
660 memset( input->keystate, 0, sizeof(input->keystate) );
664 /* detach two thread input data structures */
665 static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
667 struct thread_input *input;
669 if (!thread_from->queue || !thread_to->queue ||
670 thread_from->queue->input != thread_to->queue->input)
672 set_error( STATUS_ACCESS_DENIED );
675 if ((input = create_thread_input()))
677 release_object( thread_from->queue->input );
678 thread_from->queue->input = input;
683 /* set the next timer to expire */
684 static void set_next_timer( struct msg_queue *queue, struct timer *timer )
688 remove_timeout_user( queue->timeout );
689 queue->timeout = NULL;
691 if ((queue->next_timer = timer))
692 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
694 /* set/clear QS_TIMER bit */
695 if (queue->next_timer == queue->first_timer)
696 clear_queue_bits( queue, QS_TIMER );
698 set_queue_bits( queue, QS_TIMER );
701 /* callback for the next timer expiration */
702 static void timer_callback( void *private )
704 struct msg_queue *queue = private;
706 queue->timeout = NULL;
707 /* move on to the next timer */
708 set_next_timer( queue, queue->next_timer->next );
711 /* link a timer at its rightful place in the queue list */
712 static void link_timer( struct msg_queue *queue, struct timer *timer )
714 struct timer *pos = queue->next_timer;
716 while (pos && time_before( &pos->when, &timer->when )) pos = pos->next;
718 if (pos) /* insert before pos */
720 if ((timer->prev = pos->prev)) timer->prev->next = timer;
721 else queue->first_timer = timer;
725 else /* insert at end */
728 timer->prev = queue->last_timer;
729 if (queue->last_timer) queue->last_timer->next = timer;
730 else queue->first_timer = timer;
731 queue->last_timer = timer;
733 /* check if we replaced the next timer */
734 if (pos == queue->next_timer) set_next_timer( queue, timer );
737 /* remove a timer from the queue timer list */
738 static void unlink_timer( struct msg_queue *queue, struct timer *timer )
740 if (timer->next) timer->next->prev = timer->prev;
741 else queue->last_timer = timer->prev;
742 if (timer->prev) timer->prev->next = timer->next;
743 else queue->first_timer = timer->next;
744 /* check if we removed the next timer */
745 if (queue->next_timer == timer) set_next_timer( queue, timer->next );
746 else if (queue->next_timer == queue->first_timer) clear_queue_bits( queue, QS_TIMER );
749 /* restart an expired timer */
750 static void restart_timer( struct msg_queue *queue, struct timer *timer )
753 unlink_timer( queue, timer );
754 gettimeofday( &now, 0 );
755 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
756 link_timer( queue, timer );
759 /* find an expired timer matching the filtering parameters */
760 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
761 unsigned int get_first, unsigned int get_last,
765 for (timer = queue->first_timer; (timer && timer != queue->next_timer); timer = timer->next)
767 if (win && timer->win != win) continue;
768 if (timer->msg >= get_first && timer->msg <= get_last)
770 if (remove) restart_timer( queue, timer );
778 static int kill_timer( struct msg_queue *queue, user_handle_t win,
779 unsigned int msg, unsigned int id )
783 for (timer = queue->first_timer; timer; timer = timer->next)
785 if (timer->win != win || timer->msg != msg || timer->id != id) continue;
786 unlink_timer( queue, timer );
794 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
796 struct timer *timer = mem_alloc( sizeof(*timer) );
800 gettimeofday( &timer->when, 0 );
801 add_timeout( &timer->when, rate );
802 link_timer( queue, timer );
808 /* increment (or decrement if 'incr' is negative) the queue paint count */
809 void inc_queue_paint_count( struct thread *thread, int incr )
811 struct msg_queue *queue = thread->queue;
815 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
817 if (queue->paint_count)
818 set_queue_bits( queue, QS_PAINT );
820 clear_queue_bits( queue, QS_PAINT );
824 /* remove all messages and timers belonging to a certain window */
825 void queue_cleanup_window( struct thread *thread, user_handle_t win )
827 struct msg_queue *queue = thread->queue;
835 timer = queue->first_timer;
838 struct timer *next = timer->next;
839 if (timer->win == win)
841 unlink_timer( queue, timer );
847 /* remove messages */
848 for (i = 0; i < NB_MSG_KINDS; i++)
850 msg = queue->msg_list[i].first;
853 struct message *next = msg->next;
854 if (msg->win == win) remove_queue_message( queue, msg, i );
859 thread_input_cleanup_window( queue, win );
862 /* post a message to a window; used by socket handling */
863 void post_message( user_handle_t win, unsigned int message,
864 unsigned int wparam, unsigned int lparam )
867 struct thread *thread = get_window_thread( win );
871 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
873 msg->type = MSG_POSTED;
874 msg->win = get_user_full_handle( win );
876 msg->wparam = wparam;
877 msg->lparam = lparam;
878 msg->time = get_tick_count();
886 append_message( &thread->queue->msg_list[POST_MESSAGE], msg );
887 set_queue_bits( thread->queue, QS_POSTMESSAGE );
889 release_object( thread );
893 /* get the message queue of the current thread */
894 DECL_HANDLER(get_msg_queue)
896 struct msg_queue *queue = get_current_queue();
899 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
903 /* set the current message queue wakeup mask */
904 DECL_HANDLER(set_queue_mask)
906 struct msg_queue *queue = get_current_queue();
910 queue->wake_mask = req->wake_mask;
911 queue->changed_mask = req->changed_mask;
912 reply->wake_bits = queue->wake_bits;
913 reply->changed_bits = queue->changed_bits;
914 if (is_signaled( queue ))
916 /* if skip wait is set, do what would have been done in the subsequent wait */
917 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
918 else wake_up( &queue->obj, 0 );
924 /* get the current message queue status */
925 DECL_HANDLER(get_queue_status)
927 struct msg_queue *queue = current->queue;
930 reply->wake_bits = queue->wake_bits;
931 reply->changed_bits = queue->changed_bits;
932 if (req->clear) queue->changed_bits = 0;
934 else reply->wake_bits = reply->changed_bits = 0;
938 /* send a message to a thread queue */
939 DECL_HANDLER(send_message)
942 struct msg_queue *send_queue = get_current_queue();
943 struct msg_queue *recv_queue;
944 struct thread *thread = get_thread_from_id( req->id );
948 if (!(recv_queue = thread->queue))
950 set_error( STATUS_INVALID_PARAMETER );
951 release_object( thread );
955 if ((msg = mem_alloc( sizeof(*msg) )))
957 msg->type = req->type;
958 msg->win = get_user_full_handle( req->win );
960 msg->wparam = req->wparam;
961 msg->lparam = req->lparam;
962 msg->time = req->time;
965 msg->info = req->info;
972 case MSG_OTHER_PROCESS:
973 msg->data_size = get_req_data_size();
974 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
983 if (!(msg->result = alloc_message_result( send_queue, recv_queue, req->timeout )))
990 append_message( &recv_queue->msg_list[SEND_MESSAGE], msg );
991 set_queue_bits( recv_queue, QS_SENDMESSAGE );
994 /* needed for posted DDE messages */
995 msg->data_size = get_req_data_size();
996 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1001 append_message( &recv_queue->msg_list[POST_MESSAGE], msg );
1002 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1004 case MSG_HARDWARE_RAW:
1005 case MSG_HARDWARE_COOKED:
1007 struct message_list *list = ((msg->type == MSG_HARDWARE_RAW) ?
1008 &recv_queue->msg_list[RAW_HW_MESSAGE] :
1009 &recv_queue->msg_list[COOKED_HW_MESSAGE]);
1010 if (msg->msg == WM_MOUSEMOVE && merge_message( list, msg ))
1015 append_message( list, msg );
1016 set_queue_bits( recv_queue, get_hardware_msg_bit(msg) );
1020 set_error( STATUS_INVALID_PARAMETER );
1025 release_object( thread );
1028 /* return a message to the application, removing it from the queue if needed */
1029 static void return_message_to_app( struct msg_queue *queue, int flags,
1030 struct get_message_reply *reply,
1031 struct message *msg, enum message_kind kind )
1033 reply->total = msg->data_size;
1034 if (msg->data_size > get_reply_max_size())
1036 set_error( STATUS_BUFFER_OVERFLOW );
1039 reply->type = msg->type;
1040 reply->win = msg->win;
1041 reply->msg = msg->msg;
1042 reply->wparam = msg->wparam;
1043 reply->lparam = msg->lparam;
1046 reply->time = msg->time;
1047 reply->info = msg->info;
1049 /* raw messages always get removed */
1050 if ((msg->type == MSG_HARDWARE_RAW) || (flags & GET_MSG_REMOVE))
1052 queue->last_msg = NULL;
1055 set_reply_data_ptr( msg->data, msg->data_size );
1059 remove_queue_message( queue, msg, kind );
1061 else /* remember it as the last returned message */
1063 if (msg->data) set_reply_data( msg->data, msg->data_size );
1064 queue->last_msg = msg;
1065 queue->last_msg_kind = kind;
1070 inline static struct message *find_matching_message( const struct message_list *list,
1072 unsigned int first, unsigned int last )
1074 struct message *msg;
1076 for (msg = list->first; msg; msg = msg->next)
1078 /* check against the filters */
1079 if (msg->msg == WM_QUIT) break; /* WM_QUIT is never filtered */
1080 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
1081 if (msg->msg < first) continue;
1082 if (msg->msg > last) continue;
1083 break; /* found one */
1089 /* get a message from the current queue */
1090 DECL_HANDLER(get_message)
1092 struct timer *timer;
1093 struct message *msg;
1094 struct msg_queue *queue = get_current_queue();
1095 user_handle_t get_win = get_user_full_handle( req->get_win );
1099 /* first check for sent messages */
1100 if ((msg = queue->msg_list[SEND_MESSAGE].first))
1102 receive_message( queue, msg, reply );
1105 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1107 /* if requested, remove the last returned but not yet removed message */
1108 if ((req->flags & GET_MSG_REMOVE_LAST) && queue->last_msg)
1109 remove_queue_message( queue, queue->last_msg, queue->last_msg_kind );
1110 queue->last_msg = NULL;
1112 /* clear changed bits so we can wait on them if we don't find a message */
1113 queue->changed_bits = 0;
1115 /* then check for posted messages */
1116 if ((msg = find_matching_message( &queue->msg_list[POST_MESSAGE], get_win,
1117 req->get_first, req->get_last )))
1119 return_message_to_app( queue, req->flags, reply, msg, POST_MESSAGE );
1123 /* then check for cooked hardware messages */
1124 if ((msg = find_matching_message( &queue->msg_list[COOKED_HW_MESSAGE], get_win,
1125 req->get_first, req->get_last )))
1127 return_message_to_app( queue, req->flags, reply, msg, COOKED_HW_MESSAGE );
1131 /* then check for any raw hardware message */
1132 if ((msg = queue->msg_list[RAW_HW_MESSAGE].first))
1134 return_message_to_app( queue, req->flags, reply, msg, RAW_HW_MESSAGE );
1138 /* now check for WM_PAINT */
1139 if (queue->paint_count &&
1140 (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
1141 (reply->win = find_window_to_repaint( get_win, current )))
1143 reply->type = MSG_POSTED;
1144 reply->msg = WM_PAINT;
1149 reply->time = get_tick_count();
1154 /* now check for timer */
1155 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1156 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1158 reply->type = MSG_POSTED;
1159 reply->win = timer->win;
1160 reply->msg = timer->msg;
1161 reply->wparam = timer->id;
1162 reply->lparam = timer->lparam;
1165 reply->time = get_tick_count();
1171 set_error( STATUS_PENDING ); /* FIXME */
1175 /* reply to a sent message */
1176 DECL_HANDLER(reply_message)
1178 if (current->queue && current->queue->recv_result)
1179 reply_message( current->queue, req->result, 0, req->remove,
1180 get_req_data(), get_req_data_size() );
1182 set_error( STATUS_ACCESS_DENIED );
1186 /* retrieve the reply for the last message sent */
1187 DECL_HANDLER(get_message_reply)
1189 struct msg_queue *queue = current->queue;
1193 struct message_result *result = queue->send_result;
1195 set_error( STATUS_PENDING );
1198 if (result && (result->replied || req->cancel))
1200 if (result->replied)
1202 reply->result = result->result;
1203 set_error( result->error );
1206 size_t data_len = min( result->data_size, get_reply_max_size() );
1207 set_reply_data_ptr( result->data, data_len );
1208 result->data = NULL;
1209 result->data_size = 0;
1212 queue->send_result = result->send_next;
1213 result->sender = NULL;
1214 if (!result->receiver) free_result( result );
1215 if (!queue->send_result || !queue->send_result->replied)
1216 clear_queue_bits( queue, QS_SMRESULT );
1219 else set_error( STATUS_ACCESS_DENIED );
1223 /* set a window timer */
1224 DECL_HANDLER(set_win_timer)
1226 struct timer *timer;
1227 struct msg_queue *queue = get_current_queue();
1228 user_handle_t win = get_user_full_handle( req->win );
1232 /* remove it if it existed already */
1233 if (win) kill_timer( queue, win, req->msg, req->id );
1235 if ((timer = set_timer( queue, req->rate )))
1238 timer->msg = req->msg;
1239 timer->id = req->id;
1240 timer->lparam = req->lparam;
1244 /* kill a window timer */
1245 DECL_HANDLER(kill_win_timer)
1247 struct msg_queue *queue = current->queue;
1249 if (!queue || !kill_timer( queue, get_user_full_handle(req->win), req->msg, req->id ))
1250 set_error( STATUS_INVALID_PARAMETER );
1254 /* attach (or detach) thread inputs */
1255 DECL_HANDLER(attach_thread_input)
1257 struct thread *thread_from = get_thread_from_id( req->tid_from );
1258 struct thread *thread_to = get_thread_from_id( req->tid_to );
1260 if (!thread_from || !thread_to)
1262 if (thread_from) release_object( thread_from );
1263 if (thread_to) release_object( thread_to );
1266 if (thread_from != thread_to)
1268 if (req->attach) attach_thread_input( thread_from, thread_to );
1269 else detach_thread_input( thread_from, thread_to );
1271 else set_error( STATUS_ACCESS_DENIED );
1272 release_object( thread_from );
1273 release_object( thread_to );
1277 /* get thread input data */
1278 DECL_HANDLER(get_thread_input)
1280 struct thread *thread = NULL;
1281 struct thread_input *input;
1285 if (!(thread = get_thread_from_id( req->tid ))) return;
1286 input = thread->queue ? thread->queue->input : NULL;
1288 else input = foreground_input; /* get the foreground thread info */
1292 reply->focus = input->focus;
1293 reply->capture = input->capture;
1294 reply->active = input->active;
1295 reply->menu_owner = input->menu_owner;
1296 reply->move_size = input->move_size;
1297 reply->caret = input->caret;
1298 reply->rect = input->rect;
1305 reply->menu_owner = 0;
1306 reply->move_size = 0;
1308 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1310 /* foreground window is active window of foreground thread */
1311 reply->foreground = foreground_input ? foreground_input->active : 0;
1312 if (thread) release_object( thread );
1316 /* set the system foreground window */
1317 DECL_HANDLER(set_foreground_window)
1319 struct msg_queue *queue = get_current_queue();
1321 reply->previous = foreground_input ? foreground_input->active : 0;
1322 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1323 reply->send_msg_new = FALSE;
1327 struct thread *thread;
1329 if (is_top_level_window( req->handle ) &&
1330 ((thread = get_window_thread( req->handle ))))
1332 foreground_input = thread->queue->input;
1333 reply->send_msg_new = (foreground_input != queue->input);
1334 release_object( thread );
1336 else set_error( STATUS_INVALID_HANDLE );
1338 else foreground_input = NULL;
1342 /* set the current thread focus window */
1343 DECL_HANDLER(set_focus_window)
1345 struct msg_queue *queue = get_current_queue();
1347 reply->previous = 0;
1348 if (queue && check_queue_input_window( queue, req->handle ))
1350 reply->previous = queue->input->focus;
1351 queue->input->focus = get_user_full_handle( req->handle );
1356 /* set the current thread active window */
1357 DECL_HANDLER(set_active_window)
1359 struct msg_queue *queue = get_current_queue();
1361 reply->previous = 0;
1362 if (queue && check_queue_input_window( queue, req->handle ))
1364 if (!req->handle || make_window_active( req->handle ))
1366 reply->previous = queue->input->active;
1367 queue->input->active = get_user_full_handle( req->handle );
1369 else set_error( STATUS_INVALID_HANDLE );
1374 /* set the current thread capture window */
1375 DECL_HANDLER(set_capture_window)
1377 struct msg_queue *queue = get_current_queue();
1379 reply->previous = reply->full_handle = 0;
1380 if (queue && check_queue_input_window( queue, req->handle ))
1382 struct thread_input *input = queue->input;
1384 reply->previous = input->capture;
1385 input->capture = get_user_full_handle( req->handle );
1386 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1387 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1388 reply->full_handle = input->capture;