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 unsigned int wake_bits
; /* wakeup bits */
96 unsigned int wake_mask
; /* wakeup mask */
97 unsigned int changed_bits
; /* changed wakeup bits */
98 unsigned int changed_mask
; /* changed wakeup mask */
99 int paint_count
; /* pending paint messages count */
100 struct message_list msg_list
[NB_MSG_KINDS
]; /* lists of messages */
101 struct message_result
*send_result
; /* stack of sent messages waiting for result */
102 struct message_result
*recv_result
; /* stack of received messages waiting for result */
103 struct message
*last_msg
; /* last msg returned to the app and not removed */
104 enum message_kind last_msg_kind
; /* message kind of last_msg */
105 struct timer
*first_timer
; /* head of timer list */
106 struct timer
*last_timer
; /* tail of timer list */
107 struct timer
*next_timer
; /* next timer to expire */
108 struct timeout_user
*timeout
; /* timeout for next timer to expire */
111 static void msg_queue_dump( struct object
*obj
, int verbose
);
112 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
113 static void msg_queue_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
114 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
);
115 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
);
116 static void msg_queue_destroy( struct object
*obj
);
117 static void timer_callback( void *private );
119 static const struct object_ops msg_queue_ops
=
121 sizeof(struct msg_queue
), /* size */
122 msg_queue_dump
, /* dump */
123 msg_queue_add_queue
, /* add_queue */
124 msg_queue_remove_queue
, /* remove_queue */
125 msg_queue_signaled
, /* signaled */
126 msg_queue_satisfied
, /* satisfied */
127 NULL
, /* get_poll_events */
128 NULL
, /* poll_event */
129 no_get_fd
, /* get_fd */
130 no_flush
, /* flush */
131 no_get_file_info
, /* get_file_info */
132 NULL
, /* queue_async */
133 msg_queue_destroy
/* destroy */
137 static struct msg_queue
*create_msg_queue( struct thread
*thread
)
139 struct msg_queue
*queue
;
142 if ((queue
= alloc_object( &msg_queue_ops
, -1 )))
144 queue
->wake_bits
= 0;
145 queue
->wake_mask
= 0;
146 queue
->changed_bits
= 0;
147 queue
->changed_mask
= 0;
148 queue
->paint_count
= 0;
149 queue
->send_result
= NULL
;
150 queue
->recv_result
= NULL
;
151 queue
->last_msg
= NULL
;
152 queue
->first_timer
= NULL
;
153 queue
->last_timer
= NULL
;
154 queue
->next_timer
= NULL
;
155 queue
->timeout
= NULL
;
156 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
157 queue
->msg_list
[i
].first
= queue
->msg_list
[i
].last
= NULL
;
159 thread
->queue
= queue
;
160 if (!thread
->process
->queue
)
161 thread
->process
->queue
= (struct msg_queue
*)grab_object( queue
);
166 /* check the queue status */
167 inline static int is_signaled( struct msg_queue
*queue
)
169 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
172 /* set some queue bits */
173 inline static void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
175 queue
->wake_bits
|= bits
;
176 queue
->changed_bits
|= bits
;
177 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
180 /* clear some queue bits */
181 inline static void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
183 queue
->wake_bits
&= ~bits
;
184 queue
->changed_bits
&= ~bits
;
187 /* get the QS_* bit corresponding to a given hardware message */
188 inline static int get_hardware_msg_bit( struct message
*msg
)
190 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
191 if (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
) return QS_KEY
;
192 return QS_MOUSEBUTTON
;
195 /* get the current thread queue, creating it if needed */
196 inline static struct msg_queue
*get_current_queue(void)
198 struct msg_queue
*queue
= current
->queue
;
199 if (!queue
) queue
= create_msg_queue( current
);
203 /* append a message to the end of a list */
204 inline static void append_message( struct message_list
*list
, struct message
*msg
)
207 if ((msg
->prev
= list
->last
)) msg
->prev
->next
= msg
;
208 else list
->first
= msg
;
212 /* unlink a message from a list it */
213 inline static void unlink_message( struct message_list
*list
, struct message
*msg
)
215 if (msg
->next
) msg
->next
->prev
= msg
->prev
;
216 else list
->last
= msg
->prev
;
217 if (msg
->prev
) msg
->prev
->next
= msg
->next
;
218 else list
->first
= msg
->next
;
221 /* try to merge a message with the last in the list; return 1 if successful */
222 static int merge_message( struct message_list
*list
, const struct message
*msg
)
224 struct message
*prev
= list
->last
;
227 if (prev
->result
) return 0;
228 if (prev
->win
!= msg
->win
) return 0;
229 if (prev
->msg
!= msg
->msg
) return 0;
230 if (prev
->type
!= msg
->type
) return 0;
231 /* now we can merge it */
232 prev
->wparam
= msg
->wparam
;
233 prev
->lparam
= msg
->lparam
;
236 prev
->time
= msg
->time
;
237 prev
->info
= msg
->info
;
241 /* free a result structure */
242 static void free_result( struct message_result
*result
)
244 if (result
->timeout
) remove_timeout_user( result
->timeout
);
245 if (result
->data
) free( result
->data
);
249 /* store the message result in the appropriate structure */
250 static void store_message_result( struct message_result
*res
, unsigned int result
,
253 res
->result
= result
;
258 remove_timeout_user( res
->timeout
);
261 /* wake sender queue if waiting on this result */
262 if (res
->sender
&& res
->sender
->send_result
== res
)
263 set_queue_bits( res
->sender
, QS_SMRESULT
);
266 /* free a message when deleting a queue or window */
267 static void free_message( struct message
*msg
)
269 struct message_result
*result
= msg
->result
;
274 result
->receiver
= NULL
;
275 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
277 else free_result( result
);
279 if (msg
->data
) free( msg
->data
);
283 /* remove (and free) a message from a message list */
284 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
285 enum message_kind kind
)
288 struct message
*other
;
290 if (queue
->last_msg
== msg
) queue
->last_msg
= NULL
;
291 unlink_message( &queue
->msg_list
[kind
], msg
);
295 if (!queue
->msg_list
[kind
].first
) clear_queue_bits( queue
, QS_SENDMESSAGE
);
298 if (!queue
->msg_list
[kind
].first
) clear_queue_bits( queue
, QS_POSTMESSAGE
);
300 case COOKED_HW_MESSAGE
:
302 clr_bit
= get_hardware_msg_bit( msg
);
303 for (other
= queue
->msg_list
[kind
].first
; other
; other
= other
->next
)
304 if (get_hardware_msg_bit( other
) == clr_bit
) break;
305 if (!other
) clear_queue_bits( queue
, clr_bit
);
311 /* message timed out without getting a reply */
312 static void result_timeout( void *private )
314 struct message_result
*result
= private;
316 assert( !result
->replied
);
318 result
->timeout
= NULL
;
319 store_message_result( result
, 0, STATUS_TIMEOUT
);
322 /* allocate and fill a message result structure */
323 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
324 struct msg_queue
*recv_queue
,
325 unsigned int timeout
)
327 struct message_result
*result
= mem_alloc( sizeof(*result
) );
330 /* put the result on the sender result stack */
331 result
->sender
= send_queue
;
332 result
->receiver
= recv_queue
;
335 result
->data_size
= 0;
336 result
->timeout
= NULL
;
337 result
->send_next
= send_queue
->send_result
;
338 send_queue
->send_result
= result
;
342 gettimeofday( &when
, 0 );
343 add_timeout( &when
, timeout
);
344 result
->timeout
= add_timeout_user( &when
, result_timeout
, result
);
350 /* receive a message, removing it from the sent queue */
351 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
352 struct get_message_reply
*reply
)
354 struct message_result
*result
= msg
->result
;
356 reply
->total
= msg
->data_size
;
357 if (msg
->data_size
> get_reply_max_size())
359 set_error( STATUS_BUFFER_OVERFLOW
);
362 reply
->type
= msg
->type
;
363 reply
->win
= msg
->win
;
364 reply
->msg
= msg
->msg
;
365 reply
->wparam
= msg
->wparam
;
366 reply
->lparam
= msg
->lparam
;
369 reply
->time
= msg
->time
;
370 reply
->info
= msg
->info
;
372 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
374 unlink_message( &queue
->msg_list
[SEND_MESSAGE
], msg
);
375 /* put the result on the receiver result stack */
378 result
->recv_next
= queue
->recv_result
;
379 queue
->recv_result
= result
;
382 if (!queue
->msg_list
[SEND_MESSAGE
].first
) clear_queue_bits( queue
, QS_SENDMESSAGE
);
385 /* set the result of the current received message */
386 static void reply_message( struct msg_queue
*queue
, unsigned int result
,
387 unsigned int error
, int remove
, const void *data
, size_t len
)
389 struct message_result
*res
= queue
->recv_result
;
393 queue
->recv_result
= res
->recv_next
;
394 res
->receiver
= NULL
;
395 if (!res
->sender
) /* no one waiting for it */
403 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
404 store_message_result( res
, result
, error
);
408 /* empty a message list and free all the messages */
409 static void empty_msg_list( struct message_list
*list
)
411 struct message
*msg
= list
->first
;
414 struct message
*next
= msg
->next
;
420 /* cleanup all pending results when deleting a queue */
421 static void cleanup_results( struct msg_queue
*queue
)
423 struct message_result
*result
, *next
;
425 result
= queue
->send_result
;
428 next
= result
->send_next
;
429 result
->sender
= NULL
;
430 if (!result
->receiver
) free_result( result
);
434 while (queue
->recv_result
)
435 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
438 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
440 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
441 struct process
*process
= entry
->thread
->process
;
443 /* a thread can only wait on its own queue */
444 if (entry
->thread
->queue
!= queue
)
446 set_error( STATUS_ACCESS_DENIED
);
449 /* if waiting on the main process queue, set the idle event */
450 if (process
->queue
== queue
)
452 if (process
->idle_event
) set_event( process
->idle_event
);
454 add_queue( obj
, entry
);
458 static void msg_queue_remove_queue(struct object
*obj
, struct wait_queue_entry
*entry
)
460 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
461 struct process
*process
= entry
->thread
->process
;
463 remove_queue( obj
, entry
);
465 assert( entry
->thread
->queue
== queue
);
467 /* if waiting on the main process queue, reset the idle event */
468 if (process
->queue
== queue
)
470 if (process
->idle_event
) reset_event( process
->idle_event
);
474 static void msg_queue_dump( struct object
*obj
, int verbose
)
476 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
477 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
478 queue
->wake_bits
, queue
->wake_mask
);
481 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
)
483 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
484 return is_signaled( queue
);
487 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
)
489 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
490 queue
->wake_mask
= 0;
491 queue
->changed_mask
= 0;
492 return 0; /* Not abandoned */
495 static void msg_queue_destroy( struct object
*obj
)
497 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
498 struct timer
*timer
= queue
->first_timer
;
501 cleanup_results( queue
);
502 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
506 struct timer
*next
= timer
->next
;
510 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
513 /* set the next timer to expire */
514 static void set_next_timer( struct msg_queue
*queue
, struct timer
*timer
)
518 remove_timeout_user( queue
->timeout
);
519 queue
->timeout
= NULL
;
521 if ((queue
->next_timer
= timer
))
522 queue
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, queue
);
524 /* set/clear QS_TIMER bit */
525 if (queue
->next_timer
== queue
->first_timer
)
526 clear_queue_bits( queue
, QS_TIMER
);
528 set_queue_bits( queue
, QS_TIMER
);
531 /* callback for the next timer expiration */
532 static void timer_callback( void *private )
534 struct msg_queue
*queue
= private;
536 queue
->timeout
= NULL
;
537 /* move on to the next timer */
538 set_next_timer( queue
, queue
->next_timer
->next
);
541 /* link a timer at its rightful place in the queue list */
542 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
544 struct timer
*pos
= queue
->next_timer
;
546 while (pos
&& time_before( &pos
->when
, &timer
->when
)) pos
= pos
->next
;
548 if (pos
) /* insert before pos */
550 if ((timer
->prev
= pos
->prev
)) timer
->prev
->next
= timer
;
551 else queue
->first_timer
= timer
;
555 else /* insert at end */
558 timer
->prev
= queue
->last_timer
;
559 if (queue
->last_timer
) queue
->last_timer
->next
= timer
;
560 else queue
->first_timer
= timer
;
561 queue
->last_timer
= timer
;
563 /* check if we replaced the next timer */
564 if (pos
== queue
->next_timer
) set_next_timer( queue
, timer
);
567 /* remove a timer from the queue timer list */
568 static void unlink_timer( struct msg_queue
*queue
, struct timer
*timer
)
570 if (timer
->next
) timer
->next
->prev
= timer
->prev
;
571 else queue
->last_timer
= timer
->prev
;
572 if (timer
->prev
) timer
->prev
->next
= timer
->next
;
573 else queue
->first_timer
= timer
->next
;
574 /* check if we removed the next timer */
575 if (queue
->next_timer
== timer
) set_next_timer( queue
, timer
->next
);
576 else if (queue
->next_timer
== queue
->first_timer
) clear_queue_bits( queue
, QS_TIMER
);
579 /* restart an expired timer */
580 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
583 unlink_timer( queue
, timer
);
584 gettimeofday( &now
, 0 );
585 while (!time_before( &now
, &timer
->when
)) add_timeout( &timer
->when
, timer
->rate
);
586 link_timer( queue
, timer
);
589 /* find an expired timer matching the filtering parameters */
590 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
591 unsigned int get_first
, unsigned int get_last
,
595 for (timer
= queue
->first_timer
; (timer
&& timer
!= queue
->next_timer
); timer
= timer
->next
)
597 if (win
&& timer
->win
!= win
) continue;
598 if (timer
->msg
>= get_first
&& timer
->msg
<= get_last
)
600 if (remove
) restart_timer( queue
, timer
);
608 static int kill_timer( struct msg_queue
*queue
, user_handle_t win
,
609 unsigned int msg
, unsigned int id
)
613 for (timer
= queue
->first_timer
; timer
; timer
= timer
->next
)
615 if (timer
->win
!= win
|| timer
->msg
!= msg
|| timer
->id
!= id
) continue;
616 unlink_timer( queue
, timer
);
624 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
626 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
630 gettimeofday( &timer
->when
, 0 );
631 add_timeout( &timer
->when
, rate
);
632 link_timer( queue
, timer
);
638 /* increment (or decrement if 'incr' is negative) the queue paint count */
639 void inc_queue_paint_count( struct thread
*thread
, int incr
)
641 struct msg_queue
*queue
= thread
->queue
;
645 if ((queue
->paint_count
+= incr
) < 0) queue
->paint_count
= 0;
647 if (queue
->paint_count
)
648 set_queue_bits( queue
, QS_PAINT
);
650 clear_queue_bits( queue
, QS_PAINT
);
654 /* remove all messages and timers belonging to a certain window */
655 void queue_cleanup_window( struct thread
*thread
, user_handle_t win
)
657 struct msg_queue
*queue
= thread
->queue
;
665 timer
= queue
->first_timer
;
668 struct timer
*next
= timer
->next
;
669 if (timer
->win
== win
)
671 unlink_timer( queue
, timer
);
677 /* remove messages */
678 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
680 msg
= queue
->msg_list
[i
].first
;
683 struct message
*next
= msg
->next
;
684 if (msg
->win
== win
) remove_queue_message( queue
, msg
, i
);
690 /* post a message to a window; used by socket handling */
691 void post_message( user_handle_t win
, unsigned int message
,
692 unsigned int wparam
, unsigned int lparam
)
695 struct thread
*thread
= get_window_thread( win
);
699 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
701 msg
->type
= MSG_POSTED
;
702 msg
->win
= get_user_full_handle( win
);
704 msg
->wparam
= wparam
;
705 msg
->lparam
= lparam
;
706 msg
->time
= get_tick_count();
714 append_message( &thread
->queue
->msg_list
[POST_MESSAGE
], msg
);
715 set_queue_bits( thread
->queue
, QS_POSTMESSAGE
);
717 release_object( thread
);
721 /* get the message queue of the current thread */
722 DECL_HANDLER(get_msg_queue
)
724 struct msg_queue
*queue
= get_current_queue();
727 if (queue
) reply
->handle
= alloc_handle( current
->process
, queue
, SYNCHRONIZE
, 0 );
731 /* set the current message queue wakeup mask */
732 DECL_HANDLER(set_queue_mask
)
734 struct msg_queue
*queue
= get_current_queue();
738 queue
->wake_mask
= req
->wake_mask
;
739 queue
->changed_mask
= req
->changed_mask
;
740 reply
->wake_bits
= queue
->wake_bits
;
741 reply
->changed_bits
= queue
->changed_bits
;
742 if (is_signaled( queue
))
744 /* if skip wait is set, do what would have been done in the subsequent wait */
745 if (req
->skip_wait
) msg_queue_satisfied( &queue
->obj
, current
);
746 else wake_up( &queue
->obj
, 0 );
752 /* get the current message queue status */
753 DECL_HANDLER(get_queue_status
)
755 struct msg_queue
*queue
= current
->queue
;
758 reply
->wake_bits
= queue
->wake_bits
;
759 reply
->changed_bits
= queue
->changed_bits
;
760 if (req
->clear
) queue
->changed_bits
= 0;
762 else reply
->wake_bits
= reply
->changed_bits
= 0;
766 /* send a message to a thread queue */
767 DECL_HANDLER(send_message
)
770 struct msg_queue
*send_queue
= get_current_queue();
771 struct msg_queue
*recv_queue
;
772 struct thread
*thread
= get_thread_from_id( req
->id
);
776 if (!(recv_queue
= thread
->queue
))
778 set_error( STATUS_INVALID_PARAMETER
);
779 release_object( thread
);
783 if ((msg
= mem_alloc( sizeof(*msg
) )))
785 msg
->type
= req
->type
;
786 msg
->win
= get_user_full_handle( req
->win
);
788 msg
->wparam
= req
->wparam
;
789 msg
->lparam
= req
->lparam
;
790 msg
->time
= req
->time
;
793 msg
->info
= req
->info
;
800 case MSG_OTHER_PROCESS
:
801 msg
->data_size
= get_req_data_size();
802 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
811 if (!(msg
->result
= alloc_message_result( send_queue
, recv_queue
, req
->timeout
)))
818 append_message( &recv_queue
->msg_list
[SEND_MESSAGE
], msg
);
819 set_queue_bits( recv_queue
, QS_SENDMESSAGE
);
822 /* needed for posted DDE messages */
823 msg
->data_size
= get_req_data_size();
824 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
829 append_message( &recv_queue
->msg_list
[POST_MESSAGE
], msg
);
830 set_queue_bits( recv_queue
, QS_POSTMESSAGE
);
832 case MSG_HARDWARE_RAW
:
833 case MSG_HARDWARE_COOKED
:
835 struct message_list
*list
= ((msg
->type
== MSG_HARDWARE_RAW
) ?
836 &recv_queue
->msg_list
[RAW_HW_MESSAGE
] :
837 &recv_queue
->msg_list
[COOKED_HW_MESSAGE
]);
838 if (msg
->msg
== WM_MOUSEMOVE
&& merge_message( list
, msg
))
843 append_message( list
, msg
);
844 set_queue_bits( recv_queue
, get_hardware_msg_bit(msg
) );
848 set_error( STATUS_INVALID_PARAMETER
);
853 release_object( thread
);
856 /* return a message to the application, removing it from the queue if needed */
857 static void return_message_to_app( struct msg_queue
*queue
, int flags
,
858 struct get_message_reply
*reply
,
859 struct message
*msg
, enum message_kind kind
)
861 reply
->total
= msg
->data_size
;
862 if (msg
->data_size
> get_reply_max_size())
864 set_error( STATUS_BUFFER_OVERFLOW
);
867 reply
->type
= msg
->type
;
868 reply
->win
= msg
->win
;
869 reply
->msg
= msg
->msg
;
870 reply
->wparam
= msg
->wparam
;
871 reply
->lparam
= msg
->lparam
;
874 reply
->time
= msg
->time
;
875 reply
->info
= msg
->info
;
877 /* raw messages always get removed */
878 if ((msg
->type
== MSG_HARDWARE_RAW
) || (flags
& GET_MSG_REMOVE
))
880 queue
->last_msg
= NULL
;
883 set_reply_data_ptr( msg
->data
, msg
->data_size
);
887 remove_queue_message( queue
, msg
, kind
);
889 else /* remember it as the last returned message */
891 if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
892 queue
->last_msg
= msg
;
893 queue
->last_msg_kind
= kind
;
898 inline static struct message
*find_matching_message( const struct message_list
*list
,
900 unsigned int first
, unsigned int last
)
904 for (msg
= list
->first
; msg
; msg
= msg
->next
)
906 /* check against the filters */
907 if (msg
->msg
== WM_QUIT
) break; /* WM_QUIT is never filtered */
908 if (win
&& msg
->win
&& msg
->win
!= win
&& !is_child_window( win
, msg
->win
)) continue;
909 if (msg
->msg
< first
) continue;
910 if (msg
->msg
> last
) continue;
911 break; /* found one */
917 /* get a message from the current queue */
918 DECL_HANDLER(get_message
)
922 struct msg_queue
*queue
= get_current_queue();
923 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
927 /* first check for sent messages */
928 if ((msg
= queue
->msg_list
[SEND_MESSAGE
].first
))
930 receive_message( queue
, msg
, reply
);
933 if (req
->flags
& GET_MSG_SENT_ONLY
) goto done
; /* nothing else to check */
935 /* if requested, remove the last returned but not yet removed message */
936 if ((req
->flags
& GET_MSG_REMOVE_LAST
) && queue
->last_msg
)
937 remove_queue_message( queue
, queue
->last_msg
, queue
->last_msg_kind
);
938 queue
->last_msg
= NULL
;
940 /* clear changed bits so we can wait on them if we don't find a message */
941 queue
->changed_bits
= 0;
943 /* then check for posted messages */
944 if ((msg
= find_matching_message( &queue
->msg_list
[POST_MESSAGE
], get_win
,
945 req
->get_first
, req
->get_last
)))
947 return_message_to_app( queue
, req
->flags
, reply
, msg
, POST_MESSAGE
);
951 /* then check for cooked hardware messages */
952 if ((msg
= find_matching_message( &queue
->msg_list
[COOKED_HW_MESSAGE
], get_win
,
953 req
->get_first
, req
->get_last
)))
955 return_message_to_app( queue
, req
->flags
, reply
, msg
, COOKED_HW_MESSAGE
);
959 /* then check for any raw hardware message */
960 if ((msg
= queue
->msg_list
[RAW_HW_MESSAGE
].first
))
962 return_message_to_app( queue
, req
->flags
, reply
, msg
, RAW_HW_MESSAGE
);
966 /* now check for WM_PAINT */
967 if (queue
->paint_count
&&
968 (WM_PAINT
>= req
->get_first
) && (WM_PAINT
<= req
->get_last
) &&
969 (reply
->win
= find_window_to_repaint( get_win
, current
)))
971 reply
->type
= MSG_POSTED
;
972 reply
->msg
= WM_PAINT
;
977 reply
->time
= get_tick_count();
982 /* now check for timer */
983 if ((timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
984 req
->get_last
, (req
->flags
& GET_MSG_REMOVE
) )))
986 reply
->type
= MSG_POSTED
;
987 reply
->win
= timer
->win
;
988 reply
->msg
= timer
->msg
;
989 reply
->wparam
= timer
->id
;
990 reply
->lparam
= timer
->lparam
;
993 reply
->time
= get_tick_count();
999 set_error( STATUS_PENDING
); /* FIXME */
1003 /* reply to a sent message */
1004 DECL_HANDLER(reply_message
)
1006 if (current
->queue
&& current
->queue
->recv_result
)
1007 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
1008 get_req_data(), get_req_data_size() );
1010 set_error( STATUS_ACCESS_DENIED
);
1014 /* retrieve the reply for the last message sent */
1015 DECL_HANDLER(get_message_reply
)
1017 struct msg_queue
*queue
= current
->queue
;
1021 struct message_result
*result
= queue
->send_result
;
1023 set_error( STATUS_PENDING
);
1026 if (result
&& (result
->replied
|| req
->cancel
))
1028 if (result
->replied
)
1030 reply
->result
= result
->result
;
1031 set_error( result
->error
);
1034 size_t data_len
= min( result
->data_size
, get_reply_max_size() );
1035 set_reply_data_ptr( result
->data
, data_len
);
1036 result
->data
= NULL
;
1037 result
->data_size
= 0;
1040 queue
->send_result
= result
->send_next
;
1041 result
->sender
= NULL
;
1042 if (!result
->receiver
) free_result( result
);
1043 if (!queue
->send_result
|| !queue
->send_result
->replied
)
1044 clear_queue_bits( queue
, QS_SMRESULT
);
1047 else set_error( STATUS_ACCESS_DENIED
);
1051 /* set a window timer */
1052 DECL_HANDLER(set_win_timer
)
1054 struct timer
*timer
;
1055 struct msg_queue
*queue
= get_current_queue();
1056 user_handle_t win
= get_user_full_handle( req
->win
);
1060 /* remove it if it existed already */
1061 if (win
) kill_timer( queue
, win
, req
->msg
, req
->id
);
1063 if ((timer
= set_timer( queue
, req
->rate
)))
1066 timer
->msg
= req
->msg
;
1067 timer
->id
= req
->id
;
1068 timer
->lparam
= req
->lparam
;
1072 /* kill a window timer */
1073 DECL_HANDLER(kill_win_timer
)
1075 struct msg_queue
*queue
= current
->queue
;
1077 if (!queue
|| !kill_timer( queue
, get_user_full_handle(req
->win
), req
->msg
, req
->id
))
1078 set_error( STATUS_INVALID_PARAMETER
);