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 caret_rect
; /* caret rectangle */
102 int caret_hide
; /* caret hide count */
103 int caret_state
; /* caret on/off state */
104 unsigned char keystate
[256]; /* state of each key */
109 struct object obj
; /* object header */
110 unsigned int wake_bits
; /* wakeup bits */
111 unsigned int wake_mask
; /* wakeup mask */
112 unsigned int changed_bits
; /* changed wakeup bits */
113 unsigned int changed_mask
; /* changed wakeup mask */
114 int paint_count
; /* pending paint messages count */
115 struct message_list msg_list
[NB_MSG_KINDS
]; /* lists of messages */
116 struct message_result
*send_result
; /* stack of sent messages waiting for result */
117 struct message_result
*recv_result
; /* stack of received messages waiting for result */
118 struct message
*last_msg
; /* last msg returned to the app and not removed */
119 enum message_kind last_msg_kind
; /* message kind of last_msg */
120 struct timer
*first_timer
; /* head of timer list */
121 struct timer
*last_timer
; /* tail of timer list */
122 struct timer
*next_timer
; /* next timer to expire */
123 struct timeout_user
*timeout
; /* timeout for next timer to expire */
124 struct thread_input
*input
; /* thread input descriptor */
127 static void msg_queue_dump( struct object
*obj
, int verbose
);
128 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
129 static void msg_queue_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
130 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
);
131 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
);
132 static void msg_queue_destroy( struct object
*obj
);
133 static void thread_input_dump( struct object
*obj
, int verbose
);
134 static void thread_input_destroy( struct object
*obj
);
135 static void timer_callback( void *private );
137 static const struct object_ops msg_queue_ops
=
139 sizeof(struct msg_queue
), /* size */
140 msg_queue_dump
, /* dump */
141 msg_queue_add_queue
, /* add_queue */
142 msg_queue_remove_queue
, /* remove_queue */
143 msg_queue_signaled
, /* signaled */
144 msg_queue_satisfied
, /* satisfied */
145 NULL
, /* get_poll_events */
146 NULL
, /* poll_event */
147 no_get_fd
, /* get_fd */
148 no_flush
, /* flush */
149 no_get_file_info
, /* get_file_info */
150 NULL
, /* queue_async */
151 msg_queue_destroy
/* destroy */
155 static const struct object_ops thread_input_ops
=
157 sizeof(struct thread_input
), /* size */
158 thread_input_dump
, /* dump */
159 no_add_queue
, /* add_queue */
160 NULL
, /* remove_queue */
162 NULL
, /* satisfied */
163 NULL
, /* get_poll_events */
164 NULL
, /* poll_event */
165 no_get_fd
, /* get_fd */
166 no_flush
, /* flush */
167 no_get_file_info
, /* get_file_info */
168 NULL
, /* queue_async */
169 thread_input_destroy
/* destroy */
173 /* set the caret window in a given thread input */
174 static void set_caret_window( struct thread_input
*input
, user_handle_t win
)
177 input
->caret_rect
.left
= 0;
178 input
->caret_rect
.top
= 0;
179 input
->caret_rect
.right
= 0;
180 input
->caret_rect
.bottom
= 0;
181 input
->caret_hide
= 1;
182 input
->caret_state
= 0;
185 /* create a thread input object */
186 static struct thread_input
*create_thread_input(void)
188 struct thread_input
*input
;
190 if ((input
= alloc_object( &thread_input_ops
, -1 )))
195 input
->menu_owner
= 0;
196 input
->move_size
= 0;
197 set_caret_window( input
, 0 );
198 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
203 /* pointer to input structure of foreground thread */
204 static struct thread_input
*foreground_input
;
206 /* create a message queue object */
207 static struct msg_queue
*create_msg_queue( struct thread
*thread
, struct thread_input
*input
)
209 struct msg_queue
*queue
;
212 if (!input
&& !(input
= create_thread_input())) return NULL
;
213 if ((queue
= alloc_object( &msg_queue_ops
, -1 )))
215 queue
->wake_bits
= 0;
216 queue
->wake_mask
= 0;
217 queue
->changed_bits
= 0;
218 queue
->changed_mask
= 0;
219 queue
->paint_count
= 0;
220 queue
->send_result
= NULL
;
221 queue
->recv_result
= NULL
;
222 queue
->last_msg
= NULL
;
223 queue
->first_timer
= NULL
;
224 queue
->last_timer
= NULL
;
225 queue
->next_timer
= NULL
;
226 queue
->timeout
= NULL
;
227 queue
->input
= (struct thread_input
*)grab_object( input
);
228 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
229 queue
->msg_list
[i
].first
= queue
->msg_list
[i
].last
= NULL
;
231 thread
->queue
= queue
;
232 if (!thread
->process
->queue
)
233 thread
->process
->queue
= (struct msg_queue
*)grab_object( queue
);
235 release_object( input
);
239 /* free the message queue of a thread at thread exit */
240 void free_msg_queue( struct thread
*thread
)
242 struct process
*process
= thread
->process
;
244 if (!thread
->queue
) return;
245 if (process
->queue
== thread
->queue
) /* is it the process main queue? */
247 release_object( process
->queue
);
248 process
->queue
= NULL
;
249 if (process
->idle_event
)
251 set_event( process
->idle_event
);
252 release_object( process
->idle_event
);
253 process
->idle_event
= NULL
;
256 release_object( thread
->queue
);
257 thread
->queue
= NULL
;
260 /* check the queue status */
261 inline static int is_signaled( struct msg_queue
*queue
)
263 return ((queue
->wake_bits
& queue
->wake_mask
) || (queue
->changed_bits
& queue
->changed_mask
));
266 /* set some queue bits */
267 inline static void set_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
269 queue
->wake_bits
|= bits
;
270 queue
->changed_bits
|= bits
;
271 if (is_signaled( queue
)) wake_up( &queue
->obj
, 0 );
274 /* clear some queue bits */
275 inline static void clear_queue_bits( struct msg_queue
*queue
, unsigned int bits
)
277 queue
->wake_bits
&= ~bits
;
278 queue
->changed_bits
&= ~bits
;
281 /* get the QS_* bit corresponding to a given hardware message */
282 inline static int get_hardware_msg_bit( struct message
*msg
)
284 if (msg
->msg
== WM_MOUSEMOVE
|| msg
->msg
== WM_NCMOUSEMOVE
) return QS_MOUSEMOVE
;
285 if (msg
->msg
>= WM_KEYFIRST
&& msg
->msg
<= WM_KEYLAST
) return QS_KEY
;
286 return QS_MOUSEBUTTON
;
289 /* get the current thread queue, creating it if needed */
290 inline static struct msg_queue
*get_current_queue(void)
292 struct msg_queue
*queue
= current
->queue
;
293 if (!queue
) queue
= create_msg_queue( current
, NULL
);
297 /* append a message to the end of a list */
298 inline static void append_message( struct message_list
*list
, struct message
*msg
)
301 if ((msg
->prev
= list
->last
)) msg
->prev
->next
= msg
;
302 else list
->first
= msg
;
306 /* unlink a message from a list it */
307 inline static void unlink_message( struct message_list
*list
, struct message
*msg
)
309 if (msg
->next
) msg
->next
->prev
= msg
->prev
;
310 else list
->last
= msg
->prev
;
311 if (msg
->prev
) msg
->prev
->next
= msg
->next
;
312 else list
->first
= msg
->next
;
315 /* try to merge a message with the last in the list; return 1 if successful */
316 static int merge_message( struct message_list
*list
, const struct message
*msg
)
318 struct message
*prev
= list
->last
;
321 if (prev
->result
) return 0;
322 if (prev
->win
!= msg
->win
) return 0;
323 if (prev
->msg
!= msg
->msg
) return 0;
324 if (prev
->type
!= msg
->type
) return 0;
325 /* now we can merge it */
326 prev
->wparam
= msg
->wparam
;
327 prev
->lparam
= msg
->lparam
;
330 prev
->time
= msg
->time
;
331 prev
->info
= msg
->info
;
335 /* free a result structure */
336 static void free_result( struct message_result
*result
)
338 if (result
->timeout
) remove_timeout_user( result
->timeout
);
339 if (result
->data
) free( result
->data
);
343 /* store the message result in the appropriate structure */
344 static void store_message_result( struct message_result
*res
, unsigned int result
,
347 res
->result
= result
;
352 remove_timeout_user( res
->timeout
);
355 /* wake sender queue if waiting on this result */
356 if (res
->sender
&& res
->sender
->send_result
== res
)
357 set_queue_bits( res
->sender
, QS_SMRESULT
);
360 /* free a message when deleting a queue or window */
361 static void free_message( struct message
*msg
)
363 struct message_result
*result
= msg
->result
;
368 result
->receiver
= NULL
;
369 store_message_result( result
, 0, STATUS_ACCESS_DENIED
/*FIXME*/ );
371 else free_result( result
);
373 if (msg
->data
) free( msg
->data
);
377 /* remove (and free) a message from a message list */
378 static void remove_queue_message( struct msg_queue
*queue
, struct message
*msg
,
379 enum message_kind kind
)
382 struct message
*other
;
384 if (queue
->last_msg
== msg
) queue
->last_msg
= NULL
;
385 unlink_message( &queue
->msg_list
[kind
], msg
);
389 if (!queue
->msg_list
[kind
].first
) clear_queue_bits( queue
, QS_SENDMESSAGE
);
392 if (!queue
->msg_list
[kind
].first
) clear_queue_bits( queue
, QS_POSTMESSAGE
);
394 case COOKED_HW_MESSAGE
:
396 clr_bit
= get_hardware_msg_bit( msg
);
397 for (other
= queue
->msg_list
[kind
].first
; other
; other
= other
->next
)
398 if (get_hardware_msg_bit( other
) == clr_bit
) break;
399 if (!other
) clear_queue_bits( queue
, clr_bit
);
405 /* message timed out without getting a reply */
406 static void result_timeout( void *private )
408 struct message_result
*result
= private;
410 assert( !result
->replied
);
412 result
->timeout
= NULL
;
413 store_message_result( result
, 0, STATUS_TIMEOUT
);
416 /* allocate and fill a message result structure */
417 static struct message_result
*alloc_message_result( struct msg_queue
*send_queue
,
418 struct msg_queue
*recv_queue
,
419 unsigned int timeout
)
421 struct message_result
*result
= mem_alloc( sizeof(*result
) );
424 /* put the result on the sender result stack */
425 result
->sender
= send_queue
;
426 result
->receiver
= recv_queue
;
429 result
->data_size
= 0;
430 result
->timeout
= NULL
;
431 result
->send_next
= send_queue
->send_result
;
432 send_queue
->send_result
= result
;
436 gettimeofday( &when
, 0 );
437 add_timeout( &when
, timeout
);
438 result
->timeout
= add_timeout_user( &when
, result_timeout
, result
);
444 /* receive a message, removing it from the sent queue */
445 static void receive_message( struct msg_queue
*queue
, struct message
*msg
,
446 struct get_message_reply
*reply
)
448 struct message_result
*result
= msg
->result
;
450 reply
->total
= msg
->data_size
;
451 if (msg
->data_size
> get_reply_max_size())
453 set_error( STATUS_BUFFER_OVERFLOW
);
456 reply
->type
= msg
->type
;
457 reply
->win
= msg
->win
;
458 reply
->msg
= msg
->msg
;
459 reply
->wparam
= msg
->wparam
;
460 reply
->lparam
= msg
->lparam
;
463 reply
->time
= msg
->time
;
464 reply
->info
= msg
->info
;
466 if (msg
->data
) set_reply_data_ptr( msg
->data
, msg
->data_size
);
468 unlink_message( &queue
->msg_list
[SEND_MESSAGE
], msg
);
469 /* put the result on the receiver result stack */
472 result
->recv_next
= queue
->recv_result
;
473 queue
->recv_result
= result
;
476 if (!queue
->msg_list
[SEND_MESSAGE
].first
) clear_queue_bits( queue
, QS_SENDMESSAGE
);
479 /* set the result of the current received message */
480 static void reply_message( struct msg_queue
*queue
, unsigned int result
,
481 unsigned int error
, int remove
, const void *data
, size_t len
)
483 struct message_result
*res
= queue
->recv_result
;
487 queue
->recv_result
= res
->recv_next
;
488 res
->receiver
= NULL
;
489 if (!res
->sender
) /* no one waiting for it */
497 if (len
&& (res
->data
= memdup( data
, len
))) res
->data_size
= len
;
498 store_message_result( res
, result
, error
);
502 /* empty a message list and free all the messages */
503 static void empty_msg_list( struct message_list
*list
)
505 struct message
*msg
= list
->first
;
508 struct message
*next
= msg
->next
;
514 /* cleanup all pending results when deleting a queue */
515 static void cleanup_results( struct msg_queue
*queue
)
517 struct message_result
*result
, *next
;
519 result
= queue
->send_result
;
522 next
= result
->send_next
;
523 result
->sender
= NULL
;
524 if (!result
->receiver
) free_result( result
);
528 while (queue
->recv_result
)
529 reply_message( queue
, 0, STATUS_ACCESS_DENIED
/*FIXME*/, 1, NULL
, 0 );
532 static int msg_queue_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
534 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
535 struct process
*process
= entry
->thread
->process
;
537 /* a thread can only wait on its own queue */
538 if (entry
->thread
->queue
!= queue
)
540 set_error( STATUS_ACCESS_DENIED
);
543 /* if waiting on the main process queue, set the idle event */
544 if (process
->queue
== queue
)
546 if (process
->idle_event
) set_event( process
->idle_event
);
548 add_queue( obj
, entry
);
552 static void msg_queue_remove_queue(struct object
*obj
, struct wait_queue_entry
*entry
)
554 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
555 struct process
*process
= entry
->thread
->process
;
557 remove_queue( obj
, entry
);
559 assert( entry
->thread
->queue
== queue
);
561 /* if waiting on the main process queue, reset the idle event */
562 if (process
->queue
== queue
)
564 if (process
->idle_event
) reset_event( process
->idle_event
);
568 static void msg_queue_dump( struct object
*obj
, int verbose
)
570 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
571 fprintf( stderr
, "Msg queue bits=%x mask=%x\n",
572 queue
->wake_bits
, queue
->wake_mask
);
575 static int msg_queue_signaled( struct object
*obj
, struct thread
*thread
)
577 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
578 return is_signaled( queue
);
581 static int msg_queue_satisfied( struct object
*obj
, struct thread
*thread
)
583 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
584 queue
->wake_mask
= 0;
585 queue
->changed_mask
= 0;
586 return 0; /* Not abandoned */
589 static void msg_queue_destroy( struct object
*obj
)
591 struct msg_queue
*queue
= (struct msg_queue
*)obj
;
592 struct timer
*timer
= queue
->first_timer
;
595 cleanup_results( queue
);
596 for (i
= 0; i
< NB_MSG_KINDS
; i
++) empty_msg_list( &queue
->msg_list
[i
] );
600 struct timer
*next
= timer
->next
;
604 if (queue
->timeout
) remove_timeout_user( queue
->timeout
);
605 if (queue
->input
) release_object( queue
->input
);
608 static void thread_input_dump( struct object
*obj
, int verbose
)
610 struct thread_input
*input
= (struct thread_input
*)obj
;
611 fprintf( stderr
, "Thread input focus=%p capture=%p active=%p\n",
612 input
->focus
, input
->capture
, input
->active
);
615 static void thread_input_destroy( struct object
*obj
)
617 struct thread_input
*input
= (struct thread_input
*)obj
;
619 if (foreground_input
== input
) foreground_input
= NULL
;
622 /* fix the thread input data when a window is destroyed */
623 inline static void thread_input_cleanup_window( struct msg_queue
*queue
, user_handle_t window
)
625 struct thread_input
*input
= queue
->input
;
627 if (window
== input
->focus
) input
->focus
= 0;
628 if (window
== input
->capture
) input
->capture
= 0;
629 if (window
== input
->active
) input
->active
= 0;
630 if (window
== input
->menu_owner
) input
->menu_owner
= 0;
631 if (window
== input
->move_size
) input
->move_size
= 0;
632 if (window
== input
->caret
) set_caret_window( input
, 0 );
635 /* check if the specified window can be set in the input data of a given queue */
636 static int check_queue_input_window( struct msg_queue
*queue
, user_handle_t window
)
638 struct thread
*thread
;
641 if (!window
) return 1; /* we can always clear the data */
643 if ((thread
= get_window_thread( window
)))
645 ret
= (queue
->input
== thread
->queue
->input
);
646 if (!ret
) set_error( STATUS_ACCESS_DENIED
);
647 release_object( thread
);
649 else set_error( STATUS_INVALID_HANDLE
);
654 /* attach two thread input data structures */
655 int attach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
657 struct thread_input
*input
;
659 if (!thread_to
->queue
&& !(thread_to
->queue
= create_msg_queue( thread_to
, NULL
))) return 0;
660 input
= (struct thread_input
*)grab_object( thread_to
->queue
->input
);
662 if (thread_from
->queue
)
664 release_object( thread_from
->queue
->input
);
665 thread_from
->queue
->input
= input
;
669 if (!(thread_from
->queue
= create_msg_queue( thread_from
, input
))) return 0;
671 memset( input
->keystate
, 0, sizeof(input
->keystate
) );
675 /* detach two thread input data structures */
676 static void detach_thread_input( struct thread
*thread_from
, struct thread
*thread_to
)
678 struct thread_input
*input
;
680 if (!thread_from
->queue
|| !thread_to
->queue
||
681 thread_from
->queue
->input
!= thread_to
->queue
->input
)
683 set_error( STATUS_ACCESS_DENIED
);
686 if ((input
= create_thread_input()))
688 release_object( thread_from
->queue
->input
);
689 thread_from
->queue
->input
= input
;
694 /* set the next timer to expire */
695 static void set_next_timer( struct msg_queue
*queue
, struct timer
*timer
)
699 remove_timeout_user( queue
->timeout
);
700 queue
->timeout
= NULL
;
702 if ((queue
->next_timer
= timer
))
703 queue
->timeout
= add_timeout_user( &timer
->when
, timer_callback
, queue
);
705 /* set/clear QS_TIMER bit */
706 if (queue
->next_timer
== queue
->first_timer
)
707 clear_queue_bits( queue
, QS_TIMER
);
709 set_queue_bits( queue
, QS_TIMER
);
712 /* callback for the next timer expiration */
713 static void timer_callback( void *private )
715 struct msg_queue
*queue
= private;
717 queue
->timeout
= NULL
;
718 /* move on to the next timer */
719 set_next_timer( queue
, queue
->next_timer
->next
);
722 /* link a timer at its rightful place in the queue list */
723 static void link_timer( struct msg_queue
*queue
, struct timer
*timer
)
725 struct timer
*pos
= queue
->next_timer
;
727 while (pos
&& time_before( &pos
->when
, &timer
->when
)) pos
= pos
->next
;
729 if (pos
) /* insert before pos */
731 if ((timer
->prev
= pos
->prev
)) timer
->prev
->next
= timer
;
732 else queue
->first_timer
= timer
;
736 else /* insert at end */
739 timer
->prev
= queue
->last_timer
;
740 if (queue
->last_timer
) queue
->last_timer
->next
= timer
;
741 else queue
->first_timer
= timer
;
742 queue
->last_timer
= timer
;
744 /* check if we replaced the next timer */
745 if (pos
== queue
->next_timer
) set_next_timer( queue
, timer
);
748 /* remove a timer from the queue timer list */
749 static void unlink_timer( struct msg_queue
*queue
, struct timer
*timer
)
751 if (timer
->next
) timer
->next
->prev
= timer
->prev
;
752 else queue
->last_timer
= timer
->prev
;
753 if (timer
->prev
) timer
->prev
->next
= timer
->next
;
754 else queue
->first_timer
= timer
->next
;
755 /* check if we removed the next timer */
756 if (queue
->next_timer
== timer
) set_next_timer( queue
, timer
->next
);
757 else if (queue
->next_timer
== queue
->first_timer
) clear_queue_bits( queue
, QS_TIMER
);
760 /* restart an expired timer */
761 static void restart_timer( struct msg_queue
*queue
, struct timer
*timer
)
764 unlink_timer( queue
, timer
);
765 gettimeofday( &now
, 0 );
766 while (!time_before( &now
, &timer
->when
)) add_timeout( &timer
->when
, timer
->rate
);
767 link_timer( queue
, timer
);
770 /* find an expired timer matching the filtering parameters */
771 static struct timer
*find_expired_timer( struct msg_queue
*queue
, user_handle_t win
,
772 unsigned int get_first
, unsigned int get_last
,
776 for (timer
= queue
->first_timer
; (timer
&& timer
!= queue
->next_timer
); timer
= timer
->next
)
778 if (win
&& timer
->win
!= win
) continue;
779 if (timer
->msg
>= get_first
&& timer
->msg
<= get_last
)
781 if (remove
) restart_timer( queue
, timer
);
789 static int kill_timer( struct msg_queue
*queue
, user_handle_t win
,
790 unsigned int msg
, unsigned int id
)
794 for (timer
= queue
->first_timer
; timer
; timer
= timer
->next
)
796 if (timer
->win
!= win
|| timer
->msg
!= msg
|| timer
->id
!= id
) continue;
797 unlink_timer( queue
, timer
);
805 static struct timer
*set_timer( struct msg_queue
*queue
, unsigned int rate
)
807 struct timer
*timer
= mem_alloc( sizeof(*timer
) );
811 gettimeofday( &timer
->when
, 0 );
812 add_timeout( &timer
->when
, rate
);
813 link_timer( queue
, timer
);
819 /* increment (or decrement if 'incr' is negative) the queue paint count */
820 void inc_queue_paint_count( struct thread
*thread
, int incr
)
822 struct msg_queue
*queue
= thread
->queue
;
826 if ((queue
->paint_count
+= incr
) < 0) queue
->paint_count
= 0;
828 if (queue
->paint_count
)
829 set_queue_bits( queue
, QS_PAINT
);
831 clear_queue_bits( queue
, QS_PAINT
);
835 /* remove all messages and timers belonging to a certain window */
836 void queue_cleanup_window( struct thread
*thread
, user_handle_t win
)
838 struct msg_queue
*queue
= thread
->queue
;
846 timer
= queue
->first_timer
;
849 struct timer
*next
= timer
->next
;
850 if (timer
->win
== win
)
852 unlink_timer( queue
, timer
);
858 /* remove messages */
859 for (i
= 0; i
< NB_MSG_KINDS
; i
++)
861 msg
= queue
->msg_list
[i
].first
;
864 struct message
*next
= msg
->next
;
865 if (msg
->win
== win
) remove_queue_message( queue
, msg
, i
);
870 thread_input_cleanup_window( queue
, win
);
873 /* post a message to a window; used by socket handling */
874 void post_message( user_handle_t win
, unsigned int message
,
875 unsigned int wparam
, unsigned int lparam
)
878 struct thread
*thread
= get_window_thread( win
);
882 if (thread
->queue
&& (msg
= mem_alloc( sizeof(*msg
) )))
884 msg
->type
= MSG_POSTED
;
885 msg
->win
= get_user_full_handle( win
);
887 msg
->wparam
= wparam
;
888 msg
->lparam
= lparam
;
889 msg
->time
= get_tick_count();
897 append_message( &thread
->queue
->msg_list
[POST_MESSAGE
], msg
);
898 set_queue_bits( thread
->queue
, QS_POSTMESSAGE
);
900 release_object( thread
);
904 /* get the message queue of the current thread */
905 DECL_HANDLER(get_msg_queue
)
907 struct msg_queue
*queue
= get_current_queue();
910 if (queue
) reply
->handle
= alloc_handle( current
->process
, queue
, SYNCHRONIZE
, 0 );
914 /* set the current message queue wakeup mask */
915 DECL_HANDLER(set_queue_mask
)
917 struct msg_queue
*queue
= get_current_queue();
921 queue
->wake_mask
= req
->wake_mask
;
922 queue
->changed_mask
= req
->changed_mask
;
923 reply
->wake_bits
= queue
->wake_bits
;
924 reply
->changed_bits
= queue
->changed_bits
;
925 if (is_signaled( queue
))
927 /* if skip wait is set, do what would have been done in the subsequent wait */
928 if (req
->skip_wait
) msg_queue_satisfied( &queue
->obj
, current
);
929 else wake_up( &queue
->obj
, 0 );
935 /* get the current message queue status */
936 DECL_HANDLER(get_queue_status
)
938 struct msg_queue
*queue
= current
->queue
;
941 reply
->wake_bits
= queue
->wake_bits
;
942 reply
->changed_bits
= queue
->changed_bits
;
943 if (req
->clear
) queue
->changed_bits
= 0;
945 else reply
->wake_bits
= reply
->changed_bits
= 0;
949 /* send a message to a thread queue */
950 DECL_HANDLER(send_message
)
953 struct msg_queue
*send_queue
= get_current_queue();
954 struct msg_queue
*recv_queue
;
955 struct thread
*thread
= get_thread_from_id( req
->id
);
959 if (!(recv_queue
= thread
->queue
))
961 set_error( STATUS_INVALID_PARAMETER
);
962 release_object( thread
);
966 if ((msg
= mem_alloc( sizeof(*msg
) )))
968 msg
->type
= req
->type
;
969 msg
->win
= get_user_full_handle( req
->win
);
971 msg
->wparam
= req
->wparam
;
972 msg
->lparam
= req
->lparam
;
973 msg
->time
= req
->time
;
976 msg
->info
= req
->info
;
983 case MSG_OTHER_PROCESS
:
984 msg
->data_size
= get_req_data_size();
985 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
994 if (!(msg
->result
= alloc_message_result( send_queue
, recv_queue
, req
->timeout
)))
1001 append_message( &recv_queue
->msg_list
[SEND_MESSAGE
], msg
);
1002 set_queue_bits( recv_queue
, QS_SENDMESSAGE
);
1005 /* needed for posted DDE messages */
1006 msg
->data_size
= get_req_data_size();
1007 if (msg
->data_size
&& !(msg
->data
= memdup( get_req_data(), msg
->data_size
)))
1012 append_message( &recv_queue
->msg_list
[POST_MESSAGE
], msg
);
1013 set_queue_bits( recv_queue
, QS_POSTMESSAGE
);
1015 case MSG_HARDWARE_RAW
:
1016 case MSG_HARDWARE_COOKED
:
1018 struct message_list
*list
= ((msg
->type
== MSG_HARDWARE_RAW
) ?
1019 &recv_queue
->msg_list
[RAW_HW_MESSAGE
] :
1020 &recv_queue
->msg_list
[COOKED_HW_MESSAGE
]);
1021 if (msg
->msg
== WM_MOUSEMOVE
&& merge_message( list
, msg
))
1026 append_message( list
, msg
);
1027 set_queue_bits( recv_queue
, get_hardware_msg_bit(msg
) );
1031 set_error( STATUS_INVALID_PARAMETER
);
1036 release_object( thread
);
1039 /* return a message to the application, removing it from the queue if needed */
1040 static void return_message_to_app( struct msg_queue
*queue
, int flags
,
1041 struct get_message_reply
*reply
,
1042 struct message
*msg
, enum message_kind kind
)
1044 reply
->total
= msg
->data_size
;
1045 if (msg
->data_size
> get_reply_max_size())
1047 set_error( STATUS_BUFFER_OVERFLOW
);
1050 reply
->type
= msg
->type
;
1051 reply
->win
= msg
->win
;
1052 reply
->msg
= msg
->msg
;
1053 reply
->wparam
= msg
->wparam
;
1054 reply
->lparam
= msg
->lparam
;
1057 reply
->time
= msg
->time
;
1058 reply
->info
= msg
->info
;
1060 /* raw messages always get removed */
1061 if ((msg
->type
== MSG_HARDWARE_RAW
) || (flags
& GET_MSG_REMOVE
))
1063 queue
->last_msg
= NULL
;
1066 set_reply_data_ptr( msg
->data
, msg
->data_size
);
1070 remove_queue_message( queue
, msg
, kind
);
1072 else /* remember it as the last returned message */
1074 if (msg
->data
) set_reply_data( msg
->data
, msg
->data_size
);
1075 queue
->last_msg
= msg
;
1076 queue
->last_msg_kind
= kind
;
1081 inline static struct message
*find_matching_message( const struct message_list
*list
,
1083 unsigned int first
, unsigned int last
)
1085 struct message
*msg
;
1087 for (msg
= list
->first
; msg
; msg
= msg
->next
)
1089 /* check against the filters */
1090 if (msg
->msg
== WM_QUIT
) break; /* WM_QUIT is never filtered */
1091 if (win
&& msg
->win
&& msg
->win
!= win
&& !is_child_window( win
, msg
->win
)) continue;
1092 if (msg
->msg
< first
) continue;
1093 if (msg
->msg
> last
) continue;
1094 break; /* found one */
1100 /* get a message from the current queue */
1101 DECL_HANDLER(get_message
)
1103 struct timer
*timer
;
1104 struct message
*msg
;
1105 struct msg_queue
*queue
= get_current_queue();
1106 user_handle_t get_win
= get_user_full_handle( req
->get_win
);
1110 /* first check for sent messages */
1111 if ((msg
= queue
->msg_list
[SEND_MESSAGE
].first
))
1113 receive_message( queue
, msg
, reply
);
1116 if (req
->flags
& GET_MSG_SENT_ONLY
) goto done
; /* nothing else to check */
1118 /* if requested, remove the last returned but not yet removed message */
1119 if ((req
->flags
& GET_MSG_REMOVE_LAST
) && queue
->last_msg
)
1120 remove_queue_message( queue
, queue
->last_msg
, queue
->last_msg_kind
);
1121 queue
->last_msg
= NULL
;
1123 /* clear changed bits so we can wait on them if we don't find a message */
1124 queue
->changed_bits
= 0;
1126 /* then check for posted messages */
1127 if ((msg
= find_matching_message( &queue
->msg_list
[POST_MESSAGE
], get_win
,
1128 req
->get_first
, req
->get_last
)))
1130 return_message_to_app( queue
, req
->flags
, reply
, msg
, POST_MESSAGE
);
1134 /* then check for cooked hardware messages */
1135 if ((msg
= find_matching_message( &queue
->msg_list
[COOKED_HW_MESSAGE
], get_win
,
1136 req
->get_first
, req
->get_last
)))
1138 return_message_to_app( queue
, req
->flags
, reply
, msg
, COOKED_HW_MESSAGE
);
1142 /* then check for any raw hardware message */
1143 if ((msg
= queue
->msg_list
[RAW_HW_MESSAGE
].first
))
1145 return_message_to_app( queue
, req
->flags
, reply
, msg
, RAW_HW_MESSAGE
);
1149 /* now check for WM_PAINT */
1150 if (queue
->paint_count
&&
1151 (WM_PAINT
>= req
->get_first
) && (WM_PAINT
<= req
->get_last
) &&
1152 (reply
->win
= find_window_to_repaint( get_win
, current
)))
1154 reply
->type
= MSG_POSTED
;
1155 reply
->msg
= WM_PAINT
;
1160 reply
->time
= get_tick_count();
1165 /* now check for timer */
1166 if ((timer
= find_expired_timer( queue
, get_win
, req
->get_first
,
1167 req
->get_last
, (req
->flags
& GET_MSG_REMOVE
) )))
1169 reply
->type
= MSG_POSTED
;
1170 reply
->win
= timer
->win
;
1171 reply
->msg
= timer
->msg
;
1172 reply
->wparam
= timer
->id
;
1173 reply
->lparam
= timer
->lparam
;
1176 reply
->time
= get_tick_count();
1182 set_error( STATUS_PENDING
); /* FIXME */
1186 /* reply to a sent message */
1187 DECL_HANDLER(reply_message
)
1189 if (current
->queue
&& current
->queue
->recv_result
)
1190 reply_message( current
->queue
, req
->result
, 0, req
->remove
,
1191 get_req_data(), get_req_data_size() );
1193 set_error( STATUS_ACCESS_DENIED
);
1197 /* retrieve the reply for the last message sent */
1198 DECL_HANDLER(get_message_reply
)
1200 struct msg_queue
*queue
= current
->queue
;
1204 struct message_result
*result
= queue
->send_result
;
1206 set_error( STATUS_PENDING
);
1209 if (result
&& (result
->replied
|| req
->cancel
))
1211 if (result
->replied
)
1213 reply
->result
= result
->result
;
1214 set_error( result
->error
);
1217 size_t data_len
= min( result
->data_size
, get_reply_max_size() );
1218 set_reply_data_ptr( result
->data
, data_len
);
1219 result
->data
= NULL
;
1220 result
->data_size
= 0;
1223 queue
->send_result
= result
->send_next
;
1224 result
->sender
= NULL
;
1225 if (!result
->receiver
) free_result( result
);
1226 if (!queue
->send_result
|| !queue
->send_result
->replied
)
1227 clear_queue_bits( queue
, QS_SMRESULT
);
1230 else set_error( STATUS_ACCESS_DENIED
);
1234 /* set a window timer */
1235 DECL_HANDLER(set_win_timer
)
1237 struct timer
*timer
;
1238 struct msg_queue
*queue
= get_current_queue();
1239 user_handle_t win
= get_user_full_handle( req
->win
);
1243 /* remove it if it existed already */
1244 if (win
) kill_timer( queue
, win
, req
->msg
, req
->id
);
1246 if ((timer
= set_timer( queue
, req
->rate
)))
1249 timer
->msg
= req
->msg
;
1250 timer
->id
= req
->id
;
1251 timer
->lparam
= req
->lparam
;
1255 /* kill a window timer */
1256 DECL_HANDLER(kill_win_timer
)
1258 struct msg_queue
*queue
= current
->queue
;
1260 if (!queue
|| !kill_timer( queue
, get_user_full_handle(req
->win
), req
->msg
, req
->id
))
1261 set_error( STATUS_INVALID_PARAMETER
);
1265 /* attach (or detach) thread inputs */
1266 DECL_HANDLER(attach_thread_input
)
1268 struct thread
*thread_from
= get_thread_from_id( req
->tid_from
);
1269 struct thread
*thread_to
= get_thread_from_id( req
->tid_to
);
1271 if (!thread_from
|| !thread_to
)
1273 if (thread_from
) release_object( thread_from
);
1274 if (thread_to
) release_object( thread_to
);
1277 if (thread_from
!= thread_to
)
1279 if (req
->attach
) attach_thread_input( thread_from
, thread_to
);
1280 else detach_thread_input( thread_from
, thread_to
);
1282 else set_error( STATUS_ACCESS_DENIED
);
1283 release_object( thread_from
);
1284 release_object( thread_to
);
1288 /* get thread input data */
1289 DECL_HANDLER(get_thread_input
)
1291 struct thread
*thread
= NULL
;
1292 struct thread_input
*input
;
1296 if (!(thread
= get_thread_from_id( req
->tid
))) return;
1297 input
= thread
->queue
? thread
->queue
->input
: NULL
;
1299 else input
= foreground_input
; /* get the foreground thread info */
1303 reply
->focus
= input
->focus
;
1304 reply
->capture
= input
->capture
;
1305 reply
->active
= input
->active
;
1306 reply
->menu_owner
= input
->menu_owner
;
1307 reply
->move_size
= input
->move_size
;
1308 reply
->caret
= input
->caret
;
1309 reply
->rect
= input
->caret_rect
;
1316 reply
->menu_owner
= 0;
1317 reply
->move_size
= 0;
1319 reply
->rect
.left
= reply
->rect
.top
= reply
->rect
.right
= reply
->rect
.bottom
= 0;
1321 /* foreground window is active window of foreground thread */
1322 reply
->foreground
= foreground_input
? foreground_input
->active
: 0;
1323 if (thread
) release_object( thread
);
1327 /* set the system foreground window */
1328 DECL_HANDLER(set_foreground_window
)
1330 struct msg_queue
*queue
= get_current_queue();
1332 reply
->previous
= foreground_input
? foreground_input
->active
: 0;
1333 reply
->send_msg_old
= (reply
->previous
&& foreground_input
!= queue
->input
);
1334 reply
->send_msg_new
= FALSE
;
1338 struct thread
*thread
;
1340 if (is_top_level_window( req
->handle
) &&
1341 ((thread
= get_window_thread( req
->handle
))))
1343 foreground_input
= thread
->queue
->input
;
1344 reply
->send_msg_new
= (foreground_input
!= queue
->input
);
1345 release_object( thread
);
1347 else set_error( STATUS_INVALID_HANDLE
);
1349 else foreground_input
= NULL
;
1353 /* set the current thread focus window */
1354 DECL_HANDLER(set_focus_window
)
1356 struct msg_queue
*queue
= get_current_queue();
1358 reply
->previous
= 0;
1359 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1361 reply
->previous
= queue
->input
->focus
;
1362 queue
->input
->focus
= get_user_full_handle( req
->handle
);
1367 /* set the current thread active window */
1368 DECL_HANDLER(set_active_window
)
1370 struct msg_queue
*queue
= get_current_queue();
1372 reply
->previous
= 0;
1373 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1375 if (!req
->handle
|| make_window_active( req
->handle
))
1377 reply
->previous
= queue
->input
->active
;
1378 queue
->input
->active
= get_user_full_handle( req
->handle
);
1380 else set_error( STATUS_INVALID_HANDLE
);
1385 /* set the current thread capture window */
1386 DECL_HANDLER(set_capture_window
)
1388 struct msg_queue
*queue
= get_current_queue();
1390 reply
->previous
= reply
->full_handle
= 0;
1391 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1393 struct thread_input
*input
= queue
->input
;
1395 reply
->previous
= input
->capture
;
1396 input
->capture
= get_user_full_handle( req
->handle
);
1397 input
->menu_owner
= (req
->flags
& CAPTURE_MENU
) ? input
->capture
: 0;
1398 input
->move_size
= (req
->flags
& CAPTURE_MOVESIZE
) ? input
->capture
: 0;
1399 reply
->full_handle
= input
->capture
;
1404 /* Set the current thread caret window */
1405 DECL_HANDLER(set_caret_window
)
1407 struct msg_queue
*queue
= get_current_queue();
1409 reply
->previous
= 0;
1410 if (queue
&& check_queue_input_window( queue
, req
->handle
))
1412 struct thread_input
*input
= queue
->input
;
1414 reply
->previous
= input
->caret
;
1415 reply
->old_rect
= input
->caret_rect
;
1416 reply
->old_hide
= input
->caret_hide
;
1417 reply
->old_state
= input
->caret_state
;
1419 set_caret_window( input
, get_user_full_handle(req
->handle
) );
1420 input
->caret_rect
.right
= req
->width
;
1421 input
->caret_rect
.bottom
= req
->height
;
1426 /* Set the current thread caret information */
1427 DECL_HANDLER(set_caret_info
)
1429 struct msg_queue
*queue
= get_current_queue();
1430 struct thread_input
*input
;
1433 input
= queue
->input
;
1434 reply
->full_handle
= input
->caret
;
1435 reply
->old_rect
= input
->caret_rect
;
1436 reply
->old_hide
= input
->caret_hide
;
1437 reply
->old_state
= input
->caret_state
;
1439 if (req
->handle
&& get_user_full_handle(req
->handle
) != input
->caret
)
1441 set_error( STATUS_ACCESS_DENIED
);
1444 if (req
->flags
& SET_CARET_POS
)
1446 input
->caret_rect
.right
+= req
->x
- input
->caret_rect
.left
;
1447 input
->caret_rect
.bottom
+= req
->y
- input
->caret_rect
.top
;
1448 input
->caret_rect
.left
= req
->x
;
1449 input
->caret_rect
.top
= req
->y
;
1451 if (req
->flags
& SET_CARET_HIDE
)
1453 input
->caret_hide
+= req
->hide
;
1454 if (input
->caret_hide
< 0) input
->caret_hide
= 0;
1456 if (req
->flags
& SET_CARET_STATE
)
1458 if (req
->state
== -1) input
->caret_state
= !input
->caret_state
;
1459 else input
->caret_state
= !!req
->state
;