Added ctrl-c support.
[wine.git] / server / queue.c
blobae2486ecd9e12f8d4b33f302bde8694e87e0d18c
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
32 #include "handle.h"
33 #include "thread.h"
34 #include "process.h"
35 #include "request.h"
36 #include "user.h"
38 enum message_kind { SEND_MESSAGE, POST_MESSAGE, COOKED_HW_MESSAGE, RAW_HW_MESSAGE };
39 #define NB_MSG_KINDS (RAW_HW_MESSAGE+1)
42 struct message_result
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 */
56 struct message
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 */
74 struct message_list
76 struct message *first; /* head of list */
77 struct message *last; /* tail of list */
80 struct timer
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 */
92 struct msg_queue
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;
140 int i;
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 );
163 return 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 );
200 return queue;
203 /* append a message to the end of a list */
204 inline static void append_message( struct message_list *list, struct message *msg )
206 msg->next = NULL;
207 if ((msg->prev = list->last)) msg->prev->next = msg;
208 else list->first = msg;
209 list->last = 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;
226 if (!prev) return 0;
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;
234 prev->x = msg->x;
235 prev->y = msg->y;
236 prev->time = msg->time;
237 prev->info = msg->info;
238 return 1;
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 );
246 free( result );
249 /* store the message result in the appropriate structure */
250 static void store_message_result( struct message_result *res, unsigned int result,
251 unsigned int error )
253 res->result = result;
254 res->error = error;
255 res->replied = 1;
256 if (res->timeout)
258 remove_timeout_user( res->timeout );
259 res->timeout = NULL;
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;
270 if (result)
272 if (result->sender)
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 );
280 free( msg );
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 )
287 int clr_bit;
288 struct message *other;
290 if (queue->last_msg == msg) queue->last_msg = NULL;
291 unlink_message( &queue->msg_list[kind], msg );
292 switch(kind)
294 case SEND_MESSAGE:
295 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_SENDMESSAGE );
296 break;
297 case POST_MESSAGE:
298 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_POSTMESSAGE );
299 break;
300 case COOKED_HW_MESSAGE:
301 case RAW_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 );
306 break;
308 free_message( msg );
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) );
328 if (result)
330 /* put the result on the sender result stack */
331 result->sender = send_queue;
332 result->receiver = recv_queue;
333 result->replied = 0;
334 result->data = NULL;
335 result->data_size = 0;
336 result->timeout = NULL;
337 result->send_next = send_queue->send_result;
338 send_queue->send_result = result;
339 if (timeout != -1)
341 struct timeval when;
342 gettimeofday( &when, 0 );
343 add_timeout( &when, timeout );
344 result->timeout = add_timeout_user( &when, result_timeout, result );
347 return 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 );
360 return;
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;
367 reply->x = msg->x;
368 reply->y = msg->y;
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 */
376 if (result)
378 result->recv_next = queue->recv_result;
379 queue->recv_result = result;
381 free( msg );
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;
391 if (remove)
393 queue->recv_result = res->recv_next;
394 res->receiver = NULL;
395 if (!res->sender) /* no one waiting for it */
397 free_result( res );
398 return;
401 if (!res->replied)
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;
412 while (msg)
414 struct message *next = msg->next;
415 free_message( msg );
416 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;
426 while (result)
428 next = result->send_next;
429 result->sender = NULL;
430 if (!result->receiver) free_result( result );
431 result = next;
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 );
447 return 0;
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 );
455 return 1;
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;
499 int i;
501 cleanup_results( queue );
502 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
504 while (timer)
506 struct timer *next = timer->next;
507 free( timer );
508 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 )
516 if (queue->timeout)
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 );
527 else
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;
552 timer->next = pos;
553 pos->prev = timer;
555 else /* insert at end */
557 timer->next = NULL;
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 )
582 struct timeval now;
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,
592 int remove )
594 struct timer *timer;
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 );
601 return timer;
604 return NULL;
607 /* kill a timer */
608 static int kill_timer( struct msg_queue *queue, user_handle_t win,
609 unsigned int msg, unsigned int id )
611 struct timer *timer;
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 );
617 free( timer );
618 return 1;
620 return 0;
623 /* add a timer */
624 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
626 struct timer *timer = mem_alloc( sizeof(*timer) );
627 if (timer)
629 timer->rate = rate;
630 gettimeofday( &timer->when, 0 );
631 add_timeout( &timer->when, rate );
632 link_timer( queue, timer );
634 return 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;
643 assert( queue );
645 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
647 if (queue->paint_count)
648 set_queue_bits( queue, QS_PAINT );
649 else
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;
658 struct timer *timer;
659 struct message *msg;
660 int i;
662 if (!queue) return;
664 /* remove timers */
665 timer = queue->first_timer;
666 while (timer)
668 struct timer *next = timer->next;
669 if (timer->win == win)
671 unlink_timer( queue, timer );
672 free( timer );
674 timer = next;
677 /* remove messages */
678 for (i = 0; i < NB_MSG_KINDS; i++)
680 msg = queue->msg_list[i].first;
681 while (msg)
683 struct message *next = msg->next;
684 if (msg->win == win) remove_queue_message( queue, msg, i );
685 msg = next;
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 )
694 struct message *msg;
695 struct thread *thread = get_window_thread( win );
697 if (!thread) return;
699 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
701 msg->type = MSG_POSTED;
702 msg->win = get_user_full_handle( win );
703 msg->msg = message;
704 msg->wparam = wparam;
705 msg->lparam = lparam;
706 msg->time = get_tick_count();
707 msg->x = 0;
708 msg->y = 0;
709 msg->info = 0;
710 msg->result = NULL;
711 msg->data = NULL;
712 msg->data_size = 0;
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();
726 reply->handle = 0;
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();
736 if (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;
756 if (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)
769 struct message *msg;
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 );
774 if (!thread) return;
776 if (!(recv_queue = thread->queue))
778 set_error( STATUS_INVALID_PARAMETER );
779 release_object( thread );
780 return;
783 if ((msg = mem_alloc( sizeof(*msg) )))
785 msg->type = req->type;
786 msg->win = get_user_full_handle( req->win );
787 msg->msg = req->msg;
788 msg->wparam = req->wparam;
789 msg->lparam = req->lparam;
790 msg->time = req->time;
791 msg->x = req->x;
792 msg->y = req->y;
793 msg->info = req->info;
794 msg->result = NULL;
795 msg->data = NULL;
796 msg->data_size = 0;
798 switch(msg->type)
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 )))
804 free( msg );
805 break;
807 /* fall through */
808 case MSG_ASCII:
809 case MSG_UNICODE:
810 case MSG_CALLBACK:
811 if (!(msg->result = alloc_message_result( send_queue, recv_queue, req->timeout )))
813 free( msg );
814 break;
816 /* fall through */
817 case MSG_NOTIFY:
818 append_message( &recv_queue->msg_list[SEND_MESSAGE], msg );
819 set_queue_bits( recv_queue, QS_SENDMESSAGE );
820 break;
821 case MSG_POSTED:
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 )))
826 free( msg );
827 break;
829 append_message( &recv_queue->msg_list[POST_MESSAGE], msg );
830 set_queue_bits( recv_queue, QS_POSTMESSAGE );
831 break;
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 ))
840 free( msg );
841 break;
843 append_message( list, msg );
844 set_queue_bits( recv_queue, get_hardware_msg_bit(msg) );
845 break;
847 default:
848 set_error( STATUS_INVALID_PARAMETER );
849 free( msg );
850 break;
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 );
865 return;
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;
872 reply->x = msg->x;
873 reply->y = msg->y;
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;
881 if (msg->data)
883 set_reply_data_ptr( msg->data, msg->data_size );
884 msg->data = NULL;
885 msg->data_size = 0;
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,
899 user_handle_t win,
900 unsigned int first, unsigned int last )
902 struct message *msg;
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 */
913 return msg;
917 /* get a message from the current queue */
918 DECL_HANDLER(get_message)
920 struct timer *timer;
921 struct message *msg;
922 struct msg_queue *queue = get_current_queue();
923 user_handle_t get_win = get_user_full_handle( req->get_win );
925 if (!queue) return;
927 /* first check for sent messages */
928 if ((msg = queue->msg_list[SEND_MESSAGE].first))
930 receive_message( queue, msg, reply );
931 return;
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 );
948 return;
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 );
956 return;
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 );
963 return;
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;
973 reply->wparam = 0;
974 reply->lparam = 0;
975 reply->x = 0;
976 reply->y = 0;
977 reply->time = get_tick_count();
978 reply->info = 0;
979 return;
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;
991 reply->x = 0;
992 reply->y = 0;
993 reply->time = get_tick_count();
994 reply->info = 0;
995 return;
998 done:
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() );
1009 else
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;
1019 if (queue)
1021 struct message_result *result = queue->send_result;
1023 set_error( STATUS_PENDING );
1024 reply->result = 0;
1026 if (result && (result->replied || req->cancel))
1028 if (result->replied)
1030 reply->result = result->result;
1031 set_error( result->error );
1032 if (result->data)
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 );
1058 if (!queue) return;
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 )))
1065 timer->win = win;
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 );