winex11: Process all types of messages while moving/resizing a window.
[wine/multimedia.git] / server / queue.c
blob1cda33c6e418ceefb0df52d8debd6dbe4d361952
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "winternl.h"
37 #include "handle.h"
38 #include "file.h"
39 #include "thread.h"
40 #include "process.h"
41 #include "request.h"
42 #include "user.h"
44 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
45 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
47 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
48 #define NB_MSG_KINDS (POST_MESSAGE+1)
51 struct message_result
53 struct list sender_entry; /* entry in sender list */
54 struct message *msg; /* message the result is for */
55 struct message_result *recv_next; /* next in receiver list */
56 struct msg_queue *sender; /* sender queue */
57 struct msg_queue *receiver; /* receiver queue */
58 int replied; /* has it been replied to? */
59 unsigned int error; /* error code to pass back to sender */
60 unsigned long result; /* reply result */
61 struct message *callback_msg; /* message to queue for callback */
62 void *data; /* message reply data */
63 unsigned int data_size; /* size of message reply data */
64 struct timeout_user *timeout; /* result timeout */
67 struct message
69 struct list entry; /* entry in message list */
70 enum message_type type; /* message type */
71 user_handle_t win; /* window handle */
72 unsigned int msg; /* message code */
73 unsigned long wparam; /* parameters */
74 unsigned long lparam; /* parameters */
75 unsigned long info; /* extra info */
76 int x; /* x position */
77 int y; /* y position */
78 unsigned int time; /* message time */
79 void *data; /* message data for sent messages */
80 unsigned int data_size; /* size of message data */
81 unsigned int unique_id; /* unique id for nested hw message waits */
82 struct message_result *result; /* result in sender queue */
85 struct timer
87 struct list entry; /* entry in timer list */
88 timeout_t when; /* next expiration */
89 unsigned int rate; /* timer rate in ms */
90 user_handle_t win; /* window handle */
91 unsigned int msg; /* message to post */
92 unsigned long id; /* timer id */
93 unsigned long lparam; /* lparam for message */
96 struct thread_input
98 struct object obj; /* object header */
99 struct desktop *desktop; /* desktop that this thread input belongs to */
100 user_handle_t focus; /* focus window */
101 user_handle_t capture; /* capture window */
102 user_handle_t active; /* active window */
103 user_handle_t menu_owner; /* current menu owner window */
104 user_handle_t move_size; /* current moving/resizing window */
105 user_handle_t caret; /* caret window */
106 rectangle_t caret_rect; /* caret rectangle */
107 int caret_hide; /* caret hide count */
108 int caret_state; /* caret on/off state */
109 struct list msg_list; /* list of hardware messages */
110 unsigned char keystate[256]; /* state of each key */
113 struct msg_queue
115 struct object obj; /* object header */
116 struct fd *fd; /* optional file descriptor to poll */
117 unsigned int wake_bits; /* wakeup bits */
118 unsigned int wake_mask; /* wakeup mask */
119 unsigned int changed_bits; /* changed wakeup bits */
120 unsigned int changed_mask; /* changed wakeup mask */
121 int paint_count; /* pending paint messages count */
122 int quit_message; /* is there a pending quit message? */
123 int exit_code; /* exit code of pending quit message */
124 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
125 struct list send_result; /* stack of sent messages waiting for result */
126 struct list callback_result; /* list of callback messages waiting for result */
127 struct message_result *recv_result; /* stack of received messages waiting for result */
128 struct list pending_timers; /* list of pending timers */
129 struct list expired_timers; /* list of expired timers */
130 unsigned long next_timer_id; /* id for the next timer with a 0 window */
131 struct timeout_user *timeout; /* timeout for next timer to expire */
132 struct thread_input *input; /* thread input descriptor */
133 struct hook_table *hooks; /* hook table */
134 timeout_t last_get_msg; /* time of last get message call */
137 static void msg_queue_dump( struct object *obj, int verbose );
138 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
139 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
140 static int msg_queue_signaled( struct object *obj, struct thread *thread );
141 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
142 static void msg_queue_destroy( struct object *obj );
143 static void msg_queue_poll_event( struct fd *fd, int event );
144 static void thread_input_dump( struct object *obj, int verbose );
145 static void thread_input_destroy( struct object *obj );
146 static void timer_callback( void *private );
148 static const struct object_ops msg_queue_ops =
150 sizeof(struct msg_queue), /* size */
151 msg_queue_dump, /* dump */
152 msg_queue_add_queue, /* add_queue */
153 msg_queue_remove_queue, /* remove_queue */
154 msg_queue_signaled, /* signaled */
155 msg_queue_satisfied, /* satisfied */
156 no_signal, /* signal */
157 no_get_fd, /* get_fd */
158 no_map_access, /* map_access */
159 default_get_sd, /* get_sd */
160 default_set_sd, /* set_sd */
161 no_lookup_name, /* lookup_name */
162 no_open_file, /* open_file */
163 no_close_handle, /* close_handle */
164 msg_queue_destroy /* destroy */
167 static const struct fd_ops msg_queue_fd_ops =
169 NULL, /* get_poll_events */
170 msg_queue_poll_event, /* poll_event */
171 NULL, /* flush */
172 NULL, /* get_fd_type */
173 NULL, /* ioctl */
174 NULL, /* queue_async */
175 NULL, /* reselect_async */
176 NULL /* cancel async */
180 static const struct object_ops thread_input_ops =
182 sizeof(struct thread_input), /* size */
183 thread_input_dump, /* dump */
184 no_add_queue, /* add_queue */
185 NULL, /* remove_queue */
186 NULL, /* signaled */
187 NULL, /* satisfied */
188 no_signal, /* signal */
189 no_get_fd, /* get_fd */
190 no_map_access, /* map_access */
191 default_get_sd, /* get_sd */
192 default_set_sd, /* set_sd */
193 no_lookup_name, /* lookup_name */
194 no_open_file, /* open_file */
195 no_close_handle, /* close_handle */
196 thread_input_destroy /* destroy */
199 /* pointer to input structure of foreground thread */
200 static struct thread_input *foreground_input;
201 static unsigned int last_input_time;
203 static void free_message( struct message *msg );
205 /* set the caret window in a given thread input */
206 static void set_caret_window( struct thread_input *input, user_handle_t win )
208 if (!win || win != input->caret)
210 input->caret_rect.left = 0;
211 input->caret_rect.top = 0;
212 input->caret_rect.right = 0;
213 input->caret_rect.bottom = 0;
215 input->caret = win;
216 input->caret_hide = 1;
217 input->caret_state = 0;
220 /* create a thread input object */
221 static struct thread_input *create_thread_input( struct thread *thread )
223 struct thread_input *input;
225 if ((input = alloc_object( &thread_input_ops )))
227 input->focus = 0;
228 input->capture = 0;
229 input->active = 0;
230 input->menu_owner = 0;
231 input->move_size = 0;
232 list_init( &input->msg_list );
233 set_caret_window( input, 0 );
234 memset( input->keystate, 0, sizeof(input->keystate) );
236 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
238 release_object( input );
239 return NULL;
242 return input;
245 /* release the thread input data of a given thread */
246 static inline void release_thread_input( struct thread *thread )
248 struct thread_input *input = thread->queue->input;
250 if (!input) return;
251 release_object( input );
252 thread->queue->input = NULL;
255 /* create a message queue object */
256 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
258 struct msg_queue *queue;
259 int i;
261 if (!input && !(input = create_thread_input( thread ))) return NULL;
262 if ((queue = alloc_object( &msg_queue_ops )))
264 queue->fd = NULL;
265 queue->wake_bits = 0;
266 queue->wake_mask = 0;
267 queue->changed_bits = 0;
268 queue->changed_mask = 0;
269 queue->paint_count = 0;
270 queue->quit_message = 0;
271 queue->recv_result = NULL;
272 queue->next_timer_id = 1;
273 queue->timeout = NULL;
274 queue->input = (struct thread_input *)grab_object( input );
275 queue->hooks = NULL;
276 queue->last_get_msg = current_time;
277 list_init( &queue->send_result );
278 list_init( &queue->callback_result );
279 list_init( &queue->pending_timers );
280 list_init( &queue->expired_timers );
281 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
283 thread->queue = queue;
284 if (!thread->process->queue)
285 thread->process->queue = (struct msg_queue *)grab_object( queue );
287 release_object( input );
288 return queue;
291 /* free the message queue of a thread at thread exit */
292 void free_msg_queue( struct thread *thread )
294 struct process *process = thread->process;
296 remove_thread_hooks( thread );
297 if (!thread->queue) return;
298 if (process->queue == thread->queue) /* is it the process main queue? */
300 release_object( process->queue );
301 process->queue = NULL;
302 if (process->idle_event)
304 set_event( process->idle_event );
305 release_object( process->idle_event );
306 process->idle_event = NULL;
309 release_object( thread->queue );
310 thread->queue = NULL;
313 /* get the hook table for a given thread */
314 struct hook_table *get_queue_hooks( struct thread *thread )
316 if (!thread->queue) return NULL;
317 return thread->queue->hooks;
320 /* set the hook table for a given thread, allocating the queue if needed */
321 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
323 struct msg_queue *queue = thread->queue;
324 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
325 if (queue->hooks) release_object( queue->hooks );
326 queue->hooks = hooks;
329 /* check the queue status */
330 static inline int is_signaled( struct msg_queue *queue )
332 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
335 /* set some queue bits */
336 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
338 queue->wake_bits |= bits;
339 queue->changed_bits |= bits;
340 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
343 /* clear some queue bits */
344 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
346 queue->wake_bits &= ~bits;
347 queue->changed_bits &= ~bits;
350 /* check whether msg is a keyboard message */
351 static inline int is_keyboard_msg( struct message *msg )
353 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
356 /* check if message is matched by the filter */
357 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
359 return (msg >= first && msg <= last);
362 /* check whether a message filter contains at least one potential hardware message */
363 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
365 /* hardware message ranges are (in numerical order):
366 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
367 * WM_KEYFIRST .. WM_KEYLAST
368 * WM_MOUSEFIRST .. WM_MOUSELAST
370 if (last < WM_NCMOUSEFIRST) return 0;
371 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
372 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
373 if (first > WM_MOUSELAST) return 0;
374 return 1;
377 /* get the QS_* bit corresponding to a given hardware message */
378 static inline int get_hardware_msg_bit( struct message *msg )
380 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
381 if (is_keyboard_msg( msg )) return QS_KEY;
382 return QS_MOUSEBUTTON;
385 /* get the current thread queue, creating it if needed */
386 static inline struct msg_queue *get_current_queue(void)
388 struct msg_queue *queue = current->queue;
389 if (!queue) queue = create_msg_queue( current, NULL );
390 return queue;
393 /* get a (pseudo-)unique id to tag hardware messages */
394 static inline unsigned int get_unique_id(void)
396 static unsigned int id;
397 if (!++id) id = 1; /* avoid an id of 0 */
398 return id;
401 /* try to merge a message with the last in the list; return 1 if successful */
402 static int merge_message( struct thread_input *input, const struct message *msg )
404 struct message *prev;
405 struct list *ptr = list_tail( &input->msg_list );
407 if (!ptr) return 0;
408 prev = LIST_ENTRY( ptr, struct message, entry );
409 if (prev->result) return 0;
410 if (prev->win != msg->win) return 0;
411 if (prev->msg != msg->msg) return 0;
412 if (prev->type != msg->type) return 0;
413 /* now we can merge it */
414 prev->wparam = msg->wparam;
415 prev->lparam = msg->lparam;
416 prev->x = msg->x;
417 prev->y = msg->y;
418 prev->time = msg->time;
419 prev->info = msg->info;
420 return 1;
423 /* free a result structure */
424 static void free_result( struct message_result *result )
426 if (result->timeout) remove_timeout_user( result->timeout );
427 free( result->data );
428 if (result->callback_msg) free_message( result->callback_msg );
429 free( result );
432 /* remove the result from the sender list it is on */
433 static inline void remove_result_from_sender( struct message_result *result )
435 assert( result->sender );
437 list_remove( &result->sender_entry );
438 result->sender = NULL;
439 if (!result->receiver) free_result( result );
442 /* store the message result in the appropriate structure */
443 static void store_message_result( struct message_result *res, unsigned long result,
444 unsigned int error )
446 res->result = result;
447 res->error = error;
448 res->replied = 1;
449 if (res->timeout)
451 remove_timeout_user( res->timeout );
452 res->timeout = NULL;
454 if (res->sender)
456 if (res->callback_msg)
458 /* queue the callback message in the sender queue */
459 struct callback_msg_data *data = res->callback_msg->data;
460 data->result = result;
461 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
462 set_queue_bits( res->sender, QS_SENDMESSAGE );
463 res->callback_msg = NULL;
464 remove_result_from_sender( res );
466 else
468 /* wake sender queue if waiting on this result */
469 if (list_head(&res->sender->send_result) == &res->sender_entry)
470 set_queue_bits( res->sender, QS_SMRESULT );
476 /* free a message when deleting a queue or window */
477 static void free_message( struct message *msg )
479 struct message_result *result = msg->result;
480 if (result)
482 result->msg = NULL;
483 if (result->sender)
485 result->receiver = NULL;
486 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
488 else free_result( result );
490 free( msg->data );
491 free( msg );
494 /* remove (and free) a message from a message list */
495 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
496 enum message_kind kind )
498 list_remove( &msg->entry );
499 switch(kind)
501 case SEND_MESSAGE:
502 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
503 break;
504 case POST_MESSAGE:
505 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
506 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
507 break;
509 free_message( msg );
512 /* message timed out without getting a reply */
513 static void result_timeout( void *private )
515 struct message_result *result = private;
517 assert( !result->replied );
519 result->timeout = NULL;
521 if (result->msg) /* not received yet */
523 struct message *msg = result->msg;
525 result->msg = NULL;
526 msg->result = NULL;
527 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
528 result->receiver = NULL;
529 if (!result->sender)
531 free_result( result );
532 return;
536 store_message_result( result, 0, STATUS_TIMEOUT );
539 /* allocate and fill a message result structure */
540 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
541 struct msg_queue *recv_queue,
542 struct message *msg, timeout_t timeout )
544 struct message_result *result = mem_alloc( sizeof(*result) );
545 if (result)
547 result->msg = msg;
548 result->sender = send_queue;
549 result->receiver = recv_queue;
550 result->replied = 0;
551 result->data = NULL;
552 result->data_size = 0;
553 result->timeout = NULL;
555 if (msg->type == MSG_CALLBACK)
557 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
559 if (!callback_msg)
561 free( result );
562 return NULL;
564 callback_msg->type = MSG_CALLBACK_RESULT;
565 callback_msg->win = msg->win;
566 callback_msg->msg = msg->msg;
567 callback_msg->wparam = 0;
568 callback_msg->lparam = 0;
569 callback_msg->time = get_tick_count();
570 callback_msg->x = 0;
571 callback_msg->y = 0;
572 callback_msg->info = 0;
573 callback_msg->result = NULL;
574 /* steal the data from the original message */
575 callback_msg->data = msg->data;
576 callback_msg->data_size = msg->data_size;
577 msg->data = NULL;
578 msg->data_size = 0;
580 result->callback_msg = callback_msg;
581 list_add_head( &send_queue->callback_result, &result->sender_entry );
583 else
585 result->callback_msg = NULL;
586 list_add_head( &send_queue->send_result, &result->sender_entry );
589 if (timeout != TIMEOUT_INFINITE)
590 result->timeout = add_timeout_user( timeout, result_timeout, result );
592 return result;
595 /* receive a message, removing it from the sent queue */
596 static void receive_message( struct msg_queue *queue, struct message *msg,
597 struct get_message_reply *reply )
599 struct message_result *result = msg->result;
601 reply->total = msg->data_size;
602 if (msg->data_size > get_reply_max_size())
604 set_error( STATUS_BUFFER_OVERFLOW );
605 return;
607 reply->type = msg->type;
608 reply->win = msg->win;
609 reply->msg = msg->msg;
610 reply->wparam = msg->wparam;
611 reply->lparam = msg->lparam;
612 reply->x = msg->x;
613 reply->y = msg->y;
614 reply->time = msg->time;
615 reply->info = msg->info;
617 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
619 list_remove( &msg->entry );
620 /* put the result on the receiver result stack */
621 if (result)
623 result->msg = NULL;
624 result->recv_next = queue->recv_result;
625 queue->recv_result = result;
627 free( msg );
628 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
631 /* set the result of the current received message */
632 static void reply_message( struct msg_queue *queue, unsigned long result,
633 unsigned int error, int remove, const void *data, data_size_t len )
635 struct message_result *res = queue->recv_result;
637 if (remove)
639 queue->recv_result = res->recv_next;
640 res->receiver = NULL;
641 if (!res->sender) /* no one waiting for it */
643 free_result( res );
644 return;
647 if (!res->replied)
649 if (len && (res->data = memdup( data, len ))) res->data_size = len;
650 store_message_result( res, result, error );
654 /* retrieve a posted message */
655 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
656 unsigned int first, unsigned int last, unsigned int flags,
657 struct get_message_reply *reply )
659 struct message *msg;
661 /* check against the filters */
662 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
664 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
665 if (!check_msg_filter( msg->msg, first, last )) continue;
666 goto found; /* found one */
668 return 0;
670 /* return it to the app */
671 found:
672 reply->total = msg->data_size;
673 if (msg->data_size > get_reply_max_size())
675 set_error( STATUS_BUFFER_OVERFLOW );
676 return 1;
678 reply->type = msg->type;
679 reply->win = msg->win;
680 reply->msg = msg->msg;
681 reply->wparam = msg->wparam;
682 reply->lparam = msg->lparam;
683 reply->x = msg->x;
684 reply->y = msg->y;
685 reply->time = msg->time;
686 reply->info = msg->info;
688 if (flags & PM_REMOVE)
690 if (msg->data)
692 set_reply_data_ptr( msg->data, msg->data_size );
693 msg->data = NULL;
694 msg->data_size = 0;
696 remove_queue_message( queue, msg, POST_MESSAGE );
698 else if (msg->data) set_reply_data( msg->data, msg->data_size );
700 return 1;
703 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
704 struct get_message_reply *reply )
706 if (queue->quit_message)
708 reply->total = 0;
709 reply->type = MSG_POSTED;
710 reply->win = NULL;
711 reply->msg = WM_QUIT;
712 reply->wparam = queue->exit_code;
713 reply->lparam = 0;
714 reply->x = 0;
715 reply->y = 0;
716 reply->time = get_tick_count();
717 reply->info = 0;
719 if (flags & PM_REMOVE)
721 queue->quit_message = 0;
722 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
723 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
725 return 1;
727 else
728 return 0;
731 /* empty a message list and free all the messages */
732 static void empty_msg_list( struct list *list )
734 struct list *ptr;
736 while ((ptr = list_head( list )) != NULL)
738 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
739 list_remove( &msg->entry );
740 free_message( msg );
744 /* cleanup all pending results when deleting a queue */
745 static void cleanup_results( struct msg_queue *queue )
747 struct list *entry;
749 while ((entry = list_head( &queue->send_result )) != NULL)
751 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
754 while ((entry = list_head( &queue->callback_result )) != NULL)
756 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
759 while (queue->recv_result)
760 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
763 /* check if the thread owning the queue is hung (not checking for messages) */
764 static int is_queue_hung( struct msg_queue *queue )
766 struct wait_queue_entry *entry;
768 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
769 return 0; /* less than 5 seconds since last get message -> not hung */
771 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
773 if (entry->thread->queue == queue)
774 return 0; /* thread is waiting on queue -> not hung */
776 return 1;
779 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
781 struct msg_queue *queue = (struct msg_queue *)obj;
782 struct process *process = entry->thread->process;
784 /* a thread can only wait on its own queue */
785 if (entry->thread->queue != queue)
787 set_error( STATUS_ACCESS_DENIED );
788 return 0;
790 /* if waiting on the main process queue, set the idle event */
791 if (process->queue == queue)
793 if (process->idle_event) set_event( process->idle_event );
795 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
796 set_fd_events( queue->fd, POLLIN );
797 add_queue( obj, entry );
798 return 1;
801 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
803 struct msg_queue *queue = (struct msg_queue *)obj;
804 struct process *process = entry->thread->process;
806 remove_queue( obj, entry );
807 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
808 set_fd_events( queue->fd, 0 );
810 assert( entry->thread->queue == queue );
812 /* if waiting on the main process queue, reset the idle event */
813 if (process->queue == queue)
815 if (process->idle_event) reset_event( process->idle_event );
819 static void msg_queue_dump( struct object *obj, int verbose )
821 struct msg_queue *queue = (struct msg_queue *)obj;
822 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
823 queue->wake_bits, queue->wake_mask );
826 static int msg_queue_signaled( struct object *obj, struct thread *thread )
828 struct msg_queue *queue = (struct msg_queue *)obj;
829 int ret = 0;
831 if (queue->fd)
833 if ((ret = check_fd_events( queue->fd, POLLIN )))
834 /* stop waiting on select() if we are signaled */
835 set_fd_events( queue->fd, 0 );
836 else if (!list_empty( &obj->wait_queue ))
837 /* restart waiting on poll() if we are no longer signaled */
838 set_fd_events( queue->fd, POLLIN );
840 return ret || is_signaled( queue );
843 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
845 struct msg_queue *queue = (struct msg_queue *)obj;
846 queue->wake_mask = 0;
847 queue->changed_mask = 0;
848 return 0; /* Not abandoned */
851 static void msg_queue_destroy( struct object *obj )
853 struct msg_queue *queue = (struct msg_queue *)obj;
854 struct list *ptr;
855 int i;
857 cleanup_results( queue );
858 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
860 while ((ptr = list_head( &queue->pending_timers )))
862 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
863 list_remove( &timer->entry );
864 free( timer );
866 while ((ptr = list_head( &queue->expired_timers )))
868 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
869 list_remove( &timer->entry );
870 free( timer );
872 if (queue->timeout) remove_timeout_user( queue->timeout );
873 if (queue->input) release_object( queue->input );
874 if (queue->hooks) release_object( queue->hooks );
875 if (queue->fd) release_object( queue->fd );
878 static void msg_queue_poll_event( struct fd *fd, int event )
880 struct msg_queue *queue = get_fd_user( fd );
881 assert( queue->obj.ops == &msg_queue_ops );
883 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
884 wake_up( &queue->obj, 0 );
887 static void thread_input_dump( struct object *obj, int verbose )
889 struct thread_input *input = (struct thread_input *)obj;
890 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
891 input->focus, input->capture, input->active );
894 static void thread_input_destroy( struct object *obj )
896 struct thread_input *input = (struct thread_input *)obj;
898 if (foreground_input == input) foreground_input = NULL;
899 empty_msg_list( &input->msg_list );
900 if (input->desktop) release_object( input->desktop );
903 /* fix the thread input data when a window is destroyed */
904 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
906 struct thread_input *input = queue->input;
908 if (window == input->focus) input->focus = 0;
909 if (window == input->capture) input->capture = 0;
910 if (window == input->active) input->active = 0;
911 if (window == input->menu_owner) input->menu_owner = 0;
912 if (window == input->move_size) input->move_size = 0;
913 if (window == input->caret) set_caret_window( input, 0 );
916 /* check if the specified window can be set in the input data of a given queue */
917 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
919 struct thread *thread;
920 int ret = 0;
922 if (!window) return 1; /* we can always clear the data */
924 if ((thread = get_window_thread( window )))
926 ret = (queue->input == thread->queue->input);
927 if (!ret) set_error( STATUS_ACCESS_DENIED );
928 release_object( thread );
930 else set_error( STATUS_INVALID_HANDLE );
932 return ret;
935 /* make sure the specified thread has a queue */
936 int init_thread_queue( struct thread *thread )
938 if (thread->queue) return 1;
939 return (create_msg_queue( thread, NULL ) != NULL);
942 /* attach two thread input data structures */
943 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
945 struct desktop *desktop;
946 struct thread_input *input;
948 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
949 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
950 input = (struct thread_input *)grab_object( thread_to->queue->input );
951 if (input->desktop != desktop)
953 set_error( STATUS_ACCESS_DENIED );
954 release_object( input );
955 release_object( desktop );
956 return 0;
958 release_object( desktop );
960 if (thread_from->queue)
962 release_thread_input( thread_from );
963 thread_from->queue->input = input;
965 else
967 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
969 memset( input->keystate, 0, sizeof(input->keystate) );
970 return 1;
973 /* detach two thread input data structures */
974 void detach_thread_input( struct thread *thread_from )
976 struct thread_input *input;
978 if ((input = create_thread_input( thread_from )))
980 release_thread_input( thread_from );
981 thread_from->queue->input = input;
986 /* set the next timer to expire */
987 static void set_next_timer( struct msg_queue *queue )
989 struct list *ptr;
991 if (queue->timeout)
993 remove_timeout_user( queue->timeout );
994 queue->timeout = NULL;
996 if ((ptr = list_head( &queue->pending_timers )))
998 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
999 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1001 /* set/clear QS_TIMER bit */
1002 if (list_empty( &queue->expired_timers ))
1003 clear_queue_bits( queue, QS_TIMER );
1004 else
1005 set_queue_bits( queue, QS_TIMER );
1008 /* find a timer from its window and id */
1009 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1010 unsigned int msg, unsigned long id )
1012 struct list *ptr;
1014 /* we need to search both lists */
1016 LIST_FOR_EACH( ptr, &queue->pending_timers )
1018 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1019 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1021 LIST_FOR_EACH( ptr, &queue->expired_timers )
1023 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1024 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1026 return NULL;
1029 /* callback for the next timer expiration */
1030 static void timer_callback( void *private )
1032 struct msg_queue *queue = private;
1033 struct list *ptr;
1035 queue->timeout = NULL;
1036 /* move on to the next timer */
1037 ptr = list_head( &queue->pending_timers );
1038 list_remove( ptr );
1039 list_add_tail( &queue->expired_timers, ptr );
1040 set_next_timer( queue );
1043 /* link a timer at its rightful place in the queue list */
1044 static void link_timer( struct msg_queue *queue, struct timer *timer )
1046 struct list *ptr;
1048 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1050 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1051 if (t->when >= timer->when) break;
1053 list_add_before( ptr, &timer->entry );
1056 /* remove a timer from the queue timer list and free it */
1057 static void free_timer( struct msg_queue *queue, struct timer *timer )
1059 list_remove( &timer->entry );
1060 free( timer );
1061 set_next_timer( queue );
1064 /* restart an expired timer */
1065 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1067 list_remove( &timer->entry );
1068 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1069 link_timer( queue, timer );
1070 set_next_timer( queue );
1073 /* find an expired timer matching the filtering parameters */
1074 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1075 unsigned int get_first, unsigned int get_last,
1076 int remove )
1078 struct list *ptr;
1080 LIST_FOR_EACH( ptr, &queue->expired_timers )
1082 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1083 if (win && timer->win != win) continue;
1084 if (check_msg_filter( timer->msg, get_first, get_last ))
1086 if (remove) restart_timer( queue, timer );
1087 return timer;
1090 return NULL;
1093 /* add a timer */
1094 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1096 struct timer *timer = mem_alloc( sizeof(*timer) );
1097 if (timer)
1099 timer->rate = max( rate, 1 );
1100 timer->when = current_time + (timeout_t)timer->rate * 10000;
1101 link_timer( queue, timer );
1102 /* check if we replaced the next timer */
1103 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1105 return timer;
1108 /* change the input key state for a given key */
1109 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1111 if (down)
1113 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1114 input->keystate[key] |= 0x80;
1116 else input->keystate[key] &= ~0x80;
1119 /* update the input key state for a keyboard message */
1120 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1122 unsigned char key;
1123 int down = 0, extended;
1125 switch (msg->msg)
1127 case WM_LBUTTONDOWN:
1128 down = 1;
1129 /* fall through */
1130 case WM_LBUTTONUP:
1131 set_input_key_state( input, VK_LBUTTON, down );
1132 break;
1133 case WM_MBUTTONDOWN:
1134 down = 1;
1135 /* fall through */
1136 case WM_MBUTTONUP:
1137 set_input_key_state( input, VK_MBUTTON, down );
1138 break;
1139 case WM_RBUTTONDOWN:
1140 down = 1;
1141 /* fall through */
1142 case WM_RBUTTONUP:
1143 set_input_key_state( input, VK_RBUTTON, down );
1144 break;
1145 case WM_XBUTTONDOWN:
1146 down = 1;
1147 /* fall through */
1148 case WM_XBUTTONUP:
1149 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1150 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1151 break;
1152 case WM_KEYDOWN:
1153 case WM_SYSKEYDOWN:
1154 down = 1;
1155 /* fall through */
1156 case WM_KEYUP:
1157 case WM_SYSKEYUP:
1158 key = (unsigned char)msg->wparam;
1159 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1160 set_input_key_state( input, key, down );
1161 switch(key)
1163 case VK_SHIFT:
1164 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1165 break;
1166 case VK_CONTROL:
1167 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1168 break;
1169 case VK_MENU:
1170 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1171 break;
1172 case VK_LCONTROL:
1173 case VK_RCONTROL:
1174 set_input_key_state( input, VK_CONTROL, down );
1175 break;
1176 case VK_LMENU:
1177 case VK_RMENU:
1178 set_input_key_state( input, VK_MENU, down );
1179 break;
1180 case VK_LSHIFT:
1181 case VK_RSHIFT:
1182 set_input_key_state( input, VK_SHIFT, down );
1183 break;
1185 break;
1189 /* release the hardware message currently being processed by the given thread */
1190 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1191 int remove, user_handle_t new_win )
1193 struct thread_input *input = queue->input;
1194 struct message *msg;
1196 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1198 if (msg->unique_id == hw_id) break;
1200 if (&msg->entry == &input->msg_list) return; /* not found */
1202 /* clear the queue bit for that message */
1203 if (remove || new_win)
1205 struct message *other;
1206 int clr_bit;
1208 clr_bit = get_hardware_msg_bit( msg );
1209 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1211 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1213 clr_bit = 0;
1214 break;
1217 if (clr_bit) clear_queue_bits( queue, clr_bit );
1220 if (new_win) /* set the new window */
1222 struct thread *owner = get_window_thread( new_win );
1223 if (owner)
1225 if (owner->queue->input == input)
1227 msg->win = new_win;
1228 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1229 remove = 0;
1231 release_object( owner );
1234 if (remove)
1236 update_input_key_state( input, msg );
1237 list_remove( &msg->entry );
1238 free_message( msg );
1242 /* find the window that should receive a given hardware message */
1243 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1244 unsigned int *msg_code )
1246 user_handle_t win = 0;
1248 *msg_code = msg->msg;
1249 if (is_keyboard_msg( msg ))
1251 if (input && !(win = input->focus))
1253 win = input->active;
1254 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1257 else /* mouse message */
1259 if (!input || !(win = input->capture))
1261 if (!(win = msg->win) || !is_window_visible( win ))
1263 if (input) win = window_from_point( input->desktop, msg->x, msg->y );
1267 return win;
1270 /* queue a hardware message into a given thread input */
1271 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1273 user_handle_t win;
1274 struct thread *thread;
1275 struct thread_input *input = queue ? queue->input : foreground_input;
1276 unsigned int msg_code;
1278 last_input_time = get_tick_count();
1279 win = find_hardware_message_window( input, msg, &msg_code );
1280 if (!win || !(thread = get_window_thread(win)))
1282 if (input) update_input_key_state( input, msg );
1283 free( msg );
1284 return;
1286 input = thread->queue->input;
1288 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1289 else
1291 msg->unique_id = 0; /* will be set once we return it to the app */
1292 list_add_tail( &input->msg_list, &msg->entry );
1293 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1295 release_object( thread );
1298 /* check message filter for a hardware message */
1299 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1300 user_handle_t filter_win, unsigned int first, unsigned int last )
1302 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1304 /* we can only test the window for a keyboard message since the
1305 * dest window for a mouse message depends on hittest */
1306 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1307 return 0;
1308 /* the message code is final for a keyboard message, we can simply check it */
1309 return check_msg_filter( msg_code, first, last );
1311 else /* mouse message */
1313 /* we need to check all possible values that the message can have in the end */
1315 if (check_msg_filter( msg_code, first, last )) return 1;
1316 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1318 /* all other messages can become non-client messages */
1319 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1321 /* clicks can become double-clicks or non-client double-clicks */
1322 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1323 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1325 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1326 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1328 return 0;
1333 /* find a hardware message for the given queue */
1334 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1335 unsigned int first, unsigned int last, struct get_message_reply *reply )
1337 struct thread_input *input = thread->queue->input;
1338 struct thread *win_thread;
1339 struct list *ptr;
1340 user_handle_t win;
1341 int clear_bits, got_one = 0;
1342 unsigned int msg_code;
1344 ptr = list_head( &input->msg_list );
1345 if (hw_id)
1347 while (ptr)
1349 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1350 if (msg->unique_id == hw_id) break;
1351 ptr = list_next( &input->msg_list, ptr );
1353 if (!ptr) ptr = list_head( &input->msg_list );
1354 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1357 if (ptr == list_head( &input->msg_list ))
1358 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1359 else
1360 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1362 while (ptr)
1364 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1365 ptr = list_next( &input->msg_list, ptr );
1366 win = find_hardware_message_window( input, msg, &msg_code );
1367 if (!win || !(win_thread = get_window_thread( win )))
1369 /* no window at all, remove it */
1370 update_input_key_state( input, msg );
1371 list_remove( &msg->entry );
1372 free_message( msg );
1373 continue;
1375 if (win_thread != thread)
1377 if (win_thread->queue->input == input)
1379 /* wake the other thread */
1380 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1381 got_one = 1;
1383 else
1385 /* for another thread input, drop it */
1386 update_input_key_state( input, msg );
1387 list_remove( &msg->entry );
1388 free_message( msg );
1390 release_object( win_thread );
1391 continue;
1393 release_object( win_thread );
1395 /* if we already got a message for another thread, or if it doesn't
1396 * match the filter we skip it */
1397 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1399 clear_bits &= ~get_hardware_msg_bit( msg );
1400 continue;
1402 /* now we can return it */
1403 if (!msg->unique_id) msg->unique_id = get_unique_id();
1404 reply->type = MSG_HARDWARE;
1405 reply->win = win;
1406 reply->msg = msg_code;
1407 reply->wparam = msg->wparam;
1408 reply->lparam = msg->lparam;
1409 reply->x = msg->x;
1410 reply->y = msg->y;
1411 reply->time = msg->time;
1412 reply->info = msg->info;
1413 reply->hw_id = msg->unique_id;
1414 return 1;
1416 /* nothing found, clear the hardware queue bits */
1417 clear_queue_bits( thread->queue, clear_bits );
1418 return 0;
1421 /* increment (or decrement if 'incr' is negative) the queue paint count */
1422 void inc_queue_paint_count( struct thread *thread, int incr )
1424 struct msg_queue *queue = thread->queue;
1426 assert( queue );
1428 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1430 if (queue->paint_count)
1431 set_queue_bits( queue, QS_PAINT );
1432 else
1433 clear_queue_bits( queue, QS_PAINT );
1437 /* remove all messages and timers belonging to a certain window */
1438 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1440 struct msg_queue *queue = thread->queue;
1441 struct list *ptr;
1442 int i;
1444 if (!queue) return;
1446 /* remove timers */
1448 ptr = list_head( &queue->pending_timers );
1449 while (ptr)
1451 struct list *next = list_next( &queue->pending_timers, ptr );
1452 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1453 if (timer->win == win) free_timer( queue, timer );
1454 ptr = next;
1456 ptr = list_head( &queue->expired_timers );
1457 while (ptr)
1459 struct list *next = list_next( &queue->expired_timers, ptr );
1460 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1461 if (timer->win == win) free_timer( queue, timer );
1462 ptr = next;
1465 /* remove messages */
1466 for (i = 0; i < NB_MSG_KINDS; i++)
1468 struct list *ptr, *next;
1470 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1472 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1473 if (msg->win == win) remove_queue_message( queue, msg, i );
1477 thread_input_cleanup_window( queue, win );
1480 /* post a message to a window; used by socket handling */
1481 void post_message( user_handle_t win, unsigned int message,
1482 unsigned long wparam, unsigned long lparam )
1484 struct message *msg;
1485 struct thread *thread = get_window_thread( win );
1487 if (!thread) return;
1489 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1491 msg->type = MSG_POSTED;
1492 msg->win = get_user_full_handle( win );
1493 msg->msg = message;
1494 msg->wparam = wparam;
1495 msg->lparam = lparam;
1496 msg->time = get_tick_count();
1497 msg->x = 0;
1498 msg->y = 0;
1499 msg->info = 0;
1500 msg->result = NULL;
1501 msg->data = NULL;
1502 msg->data_size = 0;
1504 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1505 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1507 release_object( thread );
1510 /* post a win event */
1511 void post_win_event( struct thread *thread, unsigned int event,
1512 user_handle_t win, unsigned int object_id,
1513 unsigned int child_id, void *hook_proc,
1514 const WCHAR *module, data_size_t module_size,
1515 user_handle_t hook)
1517 struct message *msg;
1519 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1521 struct winevent_msg_data *data;
1523 msg->type = MSG_WINEVENT;
1524 msg->win = get_user_full_handle( win );
1525 msg->msg = event;
1526 msg->wparam = object_id;
1527 msg->lparam = child_id;
1528 msg->time = get_tick_count();
1529 msg->x = 0;
1530 msg->y = 0;
1531 msg->info = 0;
1532 msg->result = NULL;
1534 if ((data = malloc( sizeof(*data) + module_size )))
1536 data->hook = hook;
1537 data->tid = get_thread_id( current );
1538 data->hook_proc = hook_proc;
1539 memcpy( data + 1, module, module_size );
1541 msg->data = data;
1542 msg->data_size = sizeof(*data) + module_size;
1544 if (debug_level > 1)
1545 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1546 get_thread_id(thread), event, win, object_id, child_id );
1547 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1548 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1550 else
1551 free( msg );
1555 /* get the message queue of the current thread */
1556 DECL_HANDLER(get_msg_queue)
1558 struct msg_queue *queue = get_current_queue();
1560 reply->handle = 0;
1561 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1565 /* set the file descriptor associated to the current thread queue */
1566 DECL_HANDLER(set_queue_fd)
1568 struct msg_queue *queue = get_current_queue();
1569 struct file *file;
1570 int unix_fd;
1572 if (queue->fd) /* fd can only be set once */
1574 set_error( STATUS_ACCESS_DENIED );
1575 return;
1577 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
1579 if ((unix_fd = get_file_unix_fd( file )) != -1)
1581 if ((unix_fd = dup( unix_fd )) != -1)
1582 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
1583 else
1584 file_set_error();
1586 release_object( file );
1590 /* set the current message queue wakeup mask */
1591 DECL_HANDLER(set_queue_mask)
1593 struct msg_queue *queue = get_current_queue();
1595 if (queue)
1597 queue->wake_mask = req->wake_mask;
1598 queue->changed_mask = req->changed_mask;
1599 reply->wake_bits = queue->wake_bits;
1600 reply->changed_bits = queue->changed_bits;
1601 if (is_signaled( queue ))
1603 /* if skip wait is set, do what would have been done in the subsequent wait */
1604 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1605 else wake_up( &queue->obj, 0 );
1611 /* get the current message queue status */
1612 DECL_HANDLER(get_queue_status)
1614 struct msg_queue *queue = current->queue;
1615 if (queue)
1617 reply->wake_bits = queue->wake_bits;
1618 reply->changed_bits = queue->changed_bits;
1619 if (req->clear) queue->changed_bits = 0;
1621 else reply->wake_bits = reply->changed_bits = 0;
1625 /* send a message to a thread queue */
1626 DECL_HANDLER(send_message)
1628 struct message *msg;
1629 struct msg_queue *send_queue = get_current_queue();
1630 struct msg_queue *recv_queue = NULL;
1631 struct thread *thread = NULL;
1633 if (!(thread = get_thread_from_id( req->id ))) return;
1635 if (!(recv_queue = thread->queue))
1637 set_error( STATUS_INVALID_PARAMETER );
1638 release_object( thread );
1639 return;
1641 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1643 set_error( STATUS_TIMEOUT );
1644 release_object( thread );
1645 return;
1648 if ((msg = mem_alloc( sizeof(*msg) )))
1650 msg->type = req->type;
1651 msg->win = get_user_full_handle( req->win );
1652 msg->msg = req->msg;
1653 msg->wparam = req->wparam;
1654 msg->lparam = req->lparam;
1655 msg->time = get_tick_count();
1656 msg->x = 0;
1657 msg->y = 0;
1658 msg->info = 0;
1659 msg->result = NULL;
1660 msg->data = NULL;
1661 msg->data_size = get_req_data_size();
1663 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1665 free( msg );
1666 release_object( thread );
1667 return;
1670 switch(msg->type)
1672 case MSG_OTHER_PROCESS:
1673 case MSG_ASCII:
1674 case MSG_UNICODE:
1675 case MSG_CALLBACK:
1676 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
1678 free_message( msg );
1679 break;
1681 /* fall through */
1682 case MSG_NOTIFY:
1683 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1684 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1685 break;
1686 case MSG_POSTED:
1687 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1688 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1689 break;
1690 case MSG_HARDWARE: /* should use send_hardware_message instead */
1691 case MSG_CALLBACK_RESULT: /* cannot send this one */
1692 default:
1693 set_error( STATUS_INVALID_PARAMETER );
1694 free( msg );
1695 break;
1698 release_object( thread );
1701 /* send a hardware message to a thread queue */
1702 DECL_HANDLER(send_hardware_message)
1704 struct message *msg;
1705 struct msg_queue *recv_queue = NULL;
1706 struct thread *thread = NULL;
1708 if (req->id)
1710 if (!(thread = get_thread_from_id( req->id ))) return;
1713 if (thread && !(recv_queue = thread->queue))
1715 set_error( STATUS_INVALID_PARAMETER );
1716 release_object( thread );
1717 return;
1720 if ((msg = mem_alloc( sizeof(*msg) )))
1722 msg->type = MSG_HARDWARE;
1723 msg->win = get_user_full_handle( req->win );
1724 msg->msg = req->msg;
1725 msg->wparam = req->wparam;
1726 msg->lparam = req->lparam;
1727 msg->time = req->time;
1728 msg->x = req->x;
1729 msg->y = req->y;
1730 msg->info = req->info;
1731 msg->result = NULL;
1732 msg->data = NULL;
1733 msg->data_size = 0;
1734 queue_hardware_message( recv_queue, msg );
1736 if (thread) release_object( thread );
1739 /* post a quit message to the current queue */
1740 DECL_HANDLER(post_quit_message)
1742 struct msg_queue *queue = get_current_queue();
1744 if (!queue)
1745 return;
1747 queue->quit_message = 1;
1748 queue->exit_code = req->exit_code;
1749 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1752 /* get a message from the current queue */
1753 DECL_HANDLER(get_message)
1755 struct timer *timer;
1756 struct list *ptr;
1757 struct msg_queue *queue = get_current_queue();
1758 user_handle_t get_win = get_user_full_handle( req->get_win );
1759 unsigned int filter = req->flags >> 16;
1761 reply->active_hooks = get_active_hooks();
1763 if (!queue) return;
1764 queue->last_get_msg = current_time;
1765 if (!filter) filter = QS_ALLINPUT;
1767 /* first check for sent messages */
1768 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1770 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1771 receive_message( queue, msg, reply );
1772 return;
1775 /* clear changed bits so we can wait on them if we don't find a message */
1776 if (filter & QS_POSTMESSAGE)
1778 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
1779 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
1781 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
1782 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
1784 /* then check for posted messages */
1785 if ((filter & QS_POSTMESSAGE) &&
1786 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1787 return;
1789 /* only check for quit messages if not posted messages pending.
1790 * note: the quit message isn't filtered */
1791 if (get_quit_message( queue, req->flags, reply ))
1792 return;
1794 /* then check for any raw hardware message */
1795 if ((filter & QS_INPUT) &&
1796 filter_contains_hw_range( req->get_first, req->get_last ) &&
1797 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1798 return;
1800 /* now check for WM_PAINT */
1801 if ((filter & QS_PAINT) &&
1802 queue->paint_count &&
1803 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1804 (reply->win = find_window_to_repaint( get_win, current )))
1806 reply->type = MSG_POSTED;
1807 reply->msg = WM_PAINT;
1808 reply->wparam = 0;
1809 reply->lparam = 0;
1810 reply->x = 0;
1811 reply->y = 0;
1812 reply->time = get_tick_count();
1813 reply->info = 0;
1814 return;
1817 /* now check for timer */
1818 if ((filter & QS_TIMER) &&
1819 (timer = find_expired_timer( queue, get_win, req->get_first,
1820 req->get_last, (req->flags & PM_REMOVE) )))
1822 reply->type = MSG_POSTED;
1823 reply->win = timer->win;
1824 reply->msg = timer->msg;
1825 reply->wparam = timer->id;
1826 reply->lparam = timer->lparam;
1827 reply->x = 0;
1828 reply->y = 0;
1829 reply->time = get_tick_count();
1830 reply->info = 0;
1831 return;
1834 queue->wake_mask = req->wake_mask;
1835 queue->changed_mask = req->changed_mask;
1836 set_error( STATUS_PENDING ); /* FIXME */
1840 /* reply to a sent message */
1841 DECL_HANDLER(reply_message)
1843 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1844 else if (current->queue->recv_result)
1845 reply_message( current->queue, req->result, 0, req->remove,
1846 get_req_data(), get_req_data_size() );
1850 /* accept the current hardware message */
1851 DECL_HANDLER(accept_hardware_message)
1853 if (current->queue)
1854 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1855 else
1856 set_error( STATUS_ACCESS_DENIED );
1860 /* retrieve the reply for the last message sent */
1861 DECL_HANDLER(get_message_reply)
1863 struct message_result *result;
1864 struct list *entry;
1865 struct msg_queue *queue = current->queue;
1867 if (queue)
1869 set_error( STATUS_PENDING );
1870 reply->result = 0;
1872 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1874 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1875 if (result->replied || req->cancel)
1877 if (result->replied)
1879 reply->result = result->result;
1880 set_error( result->error );
1881 if (result->data)
1883 data_size_t data_len = min( result->data_size, get_reply_max_size() );
1884 set_reply_data_ptr( result->data, data_len );
1885 result->data = NULL;
1886 result->data_size = 0;
1889 remove_result_from_sender( result );
1891 entry = list_head( &queue->send_result );
1892 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1893 else
1895 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1896 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1900 else set_error( STATUS_ACCESS_DENIED );
1904 /* set a window timer */
1905 DECL_HANDLER(set_win_timer)
1907 struct timer *timer;
1908 struct msg_queue *queue;
1909 struct thread *thread = NULL;
1910 user_handle_t win = 0;
1911 unsigned long id = req->id;
1913 if (req->win)
1915 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1917 set_error( STATUS_INVALID_HANDLE );
1918 return;
1920 if (thread->process != current->process)
1922 release_object( thread );
1923 set_error( STATUS_ACCESS_DENIED );
1924 return;
1926 queue = thread->queue;
1927 /* remove it if it existed already */
1928 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1930 else
1932 queue = get_current_queue();
1933 /* look for a timer with this id */
1934 if (id && (timer = find_timer( queue, NULL, req->msg, id )))
1936 /* free and reuse id */
1937 free_timer( queue, timer );
1939 else
1941 /* find a free id for it */
1944 id = queue->next_timer_id;
1945 if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1947 while (find_timer( queue, 0, req->msg, id ));
1951 if ((timer = set_timer( queue, req->rate )))
1953 timer->win = win;
1954 timer->msg = req->msg;
1955 timer->id = id;
1956 timer->lparam = req->lparam;
1957 reply->id = id;
1959 if (thread) release_object( thread );
1962 /* kill a window timer */
1963 DECL_HANDLER(kill_win_timer)
1965 struct timer *timer;
1966 struct thread *thread;
1967 user_handle_t win = 0;
1969 if (req->win)
1971 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1973 set_error( STATUS_INVALID_HANDLE );
1974 return;
1976 if (thread->process != current->process)
1978 release_object( thread );
1979 set_error( STATUS_ACCESS_DENIED );
1980 return;
1983 else thread = (struct thread *)grab_object( current );
1985 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1986 free_timer( thread->queue, timer );
1987 else
1988 set_error( STATUS_INVALID_PARAMETER );
1990 release_object( thread );
1994 /* attach (or detach) thread inputs */
1995 DECL_HANDLER(attach_thread_input)
1997 struct thread *thread_from = get_thread_from_id( req->tid_from );
1998 struct thread *thread_to = get_thread_from_id( req->tid_to );
2000 if (!thread_from || !thread_to)
2002 if (thread_from) release_object( thread_from );
2003 if (thread_to) release_object( thread_to );
2004 return;
2006 if (thread_from != thread_to)
2008 if (req->attach) attach_thread_input( thread_from, thread_to );
2009 else
2011 if (thread_from->queue && thread_to->queue &&
2012 thread_from->queue->input == thread_to->queue->input)
2013 detach_thread_input( thread_from );
2014 else
2015 set_error( STATUS_ACCESS_DENIED );
2018 else set_error( STATUS_ACCESS_DENIED );
2019 release_object( thread_from );
2020 release_object( thread_to );
2024 /* get thread input data */
2025 DECL_HANDLER(get_thread_input)
2027 struct thread *thread = NULL;
2028 struct thread_input *input;
2030 if (req->tid)
2032 if (!(thread = get_thread_from_id( req->tid ))) return;
2033 input = thread->queue ? thread->queue->input : NULL;
2035 else input = foreground_input; /* get the foreground thread info */
2037 if (input)
2039 reply->focus = input->focus;
2040 reply->capture = input->capture;
2041 reply->active = input->active;
2042 reply->menu_owner = input->menu_owner;
2043 reply->move_size = input->move_size;
2044 reply->caret = input->caret;
2045 reply->rect = input->caret_rect;
2047 else
2049 reply->focus = 0;
2050 reply->capture = 0;
2051 reply->active = 0;
2052 reply->menu_owner = 0;
2053 reply->move_size = 0;
2054 reply->caret = 0;
2055 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
2057 /* foreground window is active window of foreground thread */
2058 reply->foreground = foreground_input ? foreground_input->active : 0;
2059 if (thread) release_object( thread );
2063 /* retrieve queue keyboard state for a given thread */
2064 DECL_HANDLER(get_key_state)
2066 struct thread *thread;
2067 struct thread_input *input;
2069 if (!(thread = get_thread_from_id( req->tid ))) return;
2070 input = thread->queue ? thread->queue->input : NULL;
2071 if (input)
2073 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
2074 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
2076 release_object( thread );
2080 /* set queue keyboard state for a given thread */
2081 DECL_HANDLER(set_key_state)
2083 struct thread *thread = NULL;
2084 struct thread_input *input;
2086 if (!(thread = get_thread_from_id( req->tid ))) return;
2087 input = thread->queue ? thread->queue->input : NULL;
2088 if (input)
2090 data_size_t size = min( sizeof(input->keystate), get_req_data_size() );
2091 if (size) memcpy( input->keystate, get_req_data(), size );
2093 release_object( thread );
2097 /* set the system foreground window */
2098 DECL_HANDLER(set_foreground_window)
2100 struct thread *thread;
2101 struct msg_queue *queue = get_current_queue();
2103 reply->previous = foreground_input ? foreground_input->active : 0;
2104 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
2105 reply->send_msg_new = FALSE;
2107 if (is_top_level_window( req->handle ) &&
2108 ((thread = get_window_thread( req->handle ))))
2110 foreground_input = thread->queue->input;
2111 reply->send_msg_new = (foreground_input != queue->input);
2112 release_object( thread );
2114 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2118 /* set the current thread focus window */
2119 DECL_HANDLER(set_focus_window)
2121 struct msg_queue *queue = get_current_queue();
2123 reply->previous = 0;
2124 if (queue && check_queue_input_window( queue, req->handle ))
2126 reply->previous = queue->input->focus;
2127 queue->input->focus = get_user_full_handle( req->handle );
2132 /* set the current thread active window */
2133 DECL_HANDLER(set_active_window)
2135 struct msg_queue *queue = get_current_queue();
2137 reply->previous = 0;
2138 if (queue && check_queue_input_window( queue, req->handle ))
2140 if (!req->handle || make_window_active( req->handle ))
2142 reply->previous = queue->input->active;
2143 queue->input->active = get_user_full_handle( req->handle );
2145 else set_error( STATUS_INVALID_HANDLE );
2150 /* set the current thread capture window */
2151 DECL_HANDLER(set_capture_window)
2153 struct msg_queue *queue = get_current_queue();
2155 reply->previous = reply->full_handle = 0;
2156 if (queue && check_queue_input_window( queue, req->handle ))
2158 struct thread_input *input = queue->input;
2160 reply->previous = input->capture;
2161 input->capture = get_user_full_handle( req->handle );
2162 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2163 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2164 reply->full_handle = input->capture;
2169 /* Set the current thread caret window */
2170 DECL_HANDLER(set_caret_window)
2172 struct msg_queue *queue = get_current_queue();
2174 reply->previous = 0;
2175 if (queue && check_queue_input_window( queue, req->handle ))
2177 struct thread_input *input = queue->input;
2179 reply->previous = input->caret;
2180 reply->old_rect = input->caret_rect;
2181 reply->old_hide = input->caret_hide;
2182 reply->old_state = input->caret_state;
2184 set_caret_window( input, get_user_full_handle(req->handle) );
2185 input->caret_rect.right = input->caret_rect.left + req->width;
2186 input->caret_rect.bottom = input->caret_rect.top + req->height;
2191 /* Set the current thread caret information */
2192 DECL_HANDLER(set_caret_info)
2194 struct msg_queue *queue = get_current_queue();
2195 struct thread_input *input;
2197 if (!queue) return;
2198 input = queue->input;
2199 reply->full_handle = input->caret;
2200 reply->old_rect = input->caret_rect;
2201 reply->old_hide = input->caret_hide;
2202 reply->old_state = input->caret_state;
2204 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2206 set_error( STATUS_ACCESS_DENIED );
2207 return;
2209 if (req->flags & SET_CARET_POS)
2211 input->caret_rect.right += req->x - input->caret_rect.left;
2212 input->caret_rect.bottom += req->y - input->caret_rect.top;
2213 input->caret_rect.left = req->x;
2214 input->caret_rect.top = req->y;
2216 if (req->flags & SET_CARET_HIDE)
2218 input->caret_hide += req->hide;
2219 if (input->caret_hide < 0) input->caret_hide = 0;
2221 if (req->flags & SET_CARET_STATE)
2223 if (req->state == -1) input->caret_state = !input->caret_state;
2224 else input->caret_state = !!req->state;
2229 /* get the time of the last input event */
2230 DECL_HANDLER(get_last_input_time)
2232 reply->time = last_input_time;