qcap: Remove unneeded casts.
[wine.git] / server / queue.c
bloba32bd7e250f062e2638ef439bb40e56c14330359
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 no_get_type, /* get_type */
153 msg_queue_add_queue, /* add_queue */
154 msg_queue_remove_queue, /* remove_queue */
155 msg_queue_signaled, /* signaled */
156 msg_queue_satisfied, /* satisfied */
157 no_signal, /* signal */
158 no_get_fd, /* get_fd */
159 no_map_access, /* map_access */
160 default_get_sd, /* get_sd */
161 default_set_sd, /* set_sd */
162 no_lookup_name, /* lookup_name */
163 no_open_file, /* open_file */
164 no_close_handle, /* close_handle */
165 msg_queue_destroy /* destroy */
168 static const struct fd_ops msg_queue_fd_ops =
170 NULL, /* get_poll_events */
171 msg_queue_poll_event, /* poll_event */
172 NULL, /* flush */
173 NULL, /* get_fd_type */
174 NULL, /* ioctl */
175 NULL, /* queue_async */
176 NULL, /* reselect_async */
177 NULL /* cancel async */
181 static const struct object_ops thread_input_ops =
183 sizeof(struct thread_input), /* size */
184 thread_input_dump, /* dump */
185 no_get_type, /* get_type */
186 no_add_queue, /* add_queue */
187 NULL, /* remove_queue */
188 NULL, /* signaled */
189 NULL, /* satisfied */
190 no_signal, /* signal */
191 no_get_fd, /* get_fd */
192 no_map_access, /* map_access */
193 default_get_sd, /* get_sd */
194 default_set_sd, /* set_sd */
195 no_lookup_name, /* lookup_name */
196 no_open_file, /* open_file */
197 no_close_handle, /* close_handle */
198 thread_input_destroy /* destroy */
201 /* pointer to input structure of foreground thread */
202 static struct thread_input *foreground_input;
203 static unsigned int last_input_time;
205 static void free_message( struct message *msg );
207 /* set the caret window in a given thread input */
208 static void set_caret_window( struct thread_input *input, user_handle_t win )
210 if (!win || win != input->caret)
212 input->caret_rect.left = 0;
213 input->caret_rect.top = 0;
214 input->caret_rect.right = 0;
215 input->caret_rect.bottom = 0;
217 input->caret = win;
218 input->caret_hide = 1;
219 input->caret_state = 0;
222 /* create a thread input object */
223 static struct thread_input *create_thread_input( struct thread *thread )
225 struct thread_input *input;
227 if ((input = alloc_object( &thread_input_ops )))
229 input->focus = 0;
230 input->capture = 0;
231 input->active = 0;
232 input->menu_owner = 0;
233 input->move_size = 0;
234 list_init( &input->msg_list );
235 set_caret_window( input, 0 );
236 memset( input->keystate, 0, sizeof(input->keystate) );
238 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
240 release_object( input );
241 return NULL;
244 return input;
247 /* release the thread input data of a given thread */
248 static inline void release_thread_input( struct thread *thread )
250 struct thread_input *input = thread->queue->input;
252 if (!input) return;
253 release_object( input );
254 thread->queue->input = NULL;
257 /* create a message queue object */
258 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
260 struct msg_queue *queue;
261 int i;
263 if (!input && !(input = create_thread_input( thread ))) return NULL;
264 if ((queue = alloc_object( &msg_queue_ops )))
266 queue->fd = NULL;
267 queue->wake_bits = 0;
268 queue->wake_mask = 0;
269 queue->changed_bits = 0;
270 queue->changed_mask = 0;
271 queue->paint_count = 0;
272 queue->quit_message = 0;
273 queue->recv_result = NULL;
274 queue->next_timer_id = 0x7fff;
275 queue->timeout = NULL;
276 queue->input = (struct thread_input *)grab_object( input );
277 queue->hooks = NULL;
278 queue->last_get_msg = current_time;
279 list_init( &queue->send_result );
280 list_init( &queue->callback_result );
281 list_init( &queue->pending_timers );
282 list_init( &queue->expired_timers );
283 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
285 thread->queue = queue;
286 if (!thread->process->queue)
287 thread->process->queue = (struct msg_queue *)grab_object( queue );
289 release_object( input );
290 return queue;
293 /* free the message queue of a thread at thread exit */
294 void free_msg_queue( struct thread *thread )
296 struct process *process = thread->process;
298 remove_thread_hooks( thread );
299 if (!thread->queue) return;
300 if (process->queue == thread->queue) /* is it the process main queue? */
302 release_object( process->queue );
303 process->queue = NULL;
304 if (process->idle_event)
306 set_event( process->idle_event );
307 release_object( process->idle_event );
308 process->idle_event = NULL;
311 release_object( thread->queue );
312 thread->queue = NULL;
315 /* get the hook table for a given thread */
316 struct hook_table *get_queue_hooks( struct thread *thread )
318 if (!thread->queue) return NULL;
319 return thread->queue->hooks;
322 /* set the hook table for a given thread, allocating the queue if needed */
323 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
325 struct msg_queue *queue = thread->queue;
326 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
327 if (queue->hooks) release_object( queue->hooks );
328 queue->hooks = hooks;
331 /* check the queue status */
332 static inline int is_signaled( struct msg_queue *queue )
334 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
337 /* set some queue bits */
338 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
340 queue->wake_bits |= bits;
341 queue->changed_bits |= bits;
342 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
345 /* clear some queue bits */
346 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
348 queue->wake_bits &= ~bits;
349 queue->changed_bits &= ~bits;
352 /* check whether msg is a keyboard message */
353 static inline int is_keyboard_msg( struct message *msg )
355 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
358 /* check if message is matched by the filter */
359 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
361 return (msg >= first && msg <= last);
364 /* check whether a message filter contains at least one potential hardware message */
365 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
367 /* hardware message ranges are (in numerical order):
368 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
369 * WM_KEYFIRST .. WM_KEYLAST
370 * WM_MOUSEFIRST .. WM_MOUSELAST
372 if (last < WM_NCMOUSEFIRST) return 0;
373 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
374 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
375 if (first > WM_MOUSELAST) return 0;
376 return 1;
379 /* get the QS_* bit corresponding to a given hardware message */
380 static inline int get_hardware_msg_bit( struct message *msg )
382 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
383 if (is_keyboard_msg( msg )) return QS_KEY;
384 return QS_MOUSEBUTTON;
387 /* get the current thread queue, creating it if needed */
388 static inline struct msg_queue *get_current_queue(void)
390 struct msg_queue *queue = current->queue;
391 if (!queue) queue = create_msg_queue( current, NULL );
392 return queue;
395 /* get a (pseudo-)unique id to tag hardware messages */
396 static inline unsigned int get_unique_id(void)
398 static unsigned int id;
399 if (!++id) id = 1; /* avoid an id of 0 */
400 return id;
403 /* try to merge a message with the last in the list; return 1 if successful */
404 static int merge_message( struct thread_input *input, const struct message *msg )
406 struct message *prev;
407 struct list *ptr = list_tail( &input->msg_list );
409 if (!ptr) return 0;
410 prev = LIST_ENTRY( ptr, struct message, entry );
411 if (prev->result) return 0;
412 if (prev->win && msg->win && prev->win != msg->win) return 0;
413 if (prev->msg != msg->msg) return 0;
414 if (prev->type != msg->type) return 0;
415 /* now we can merge it */
416 prev->wparam = msg->wparam;
417 prev->lparam = msg->lparam;
418 prev->x = msg->x;
419 prev->y = msg->y;
420 prev->time = msg->time;
421 prev->info = msg->info;
422 return 1;
425 /* free a result structure */
426 static void free_result( struct message_result *result )
428 if (result->timeout) remove_timeout_user( result->timeout );
429 free( result->data );
430 if (result->callback_msg) free_message( result->callback_msg );
431 free( result );
434 /* remove the result from the sender list it is on */
435 static inline void remove_result_from_sender( struct message_result *result )
437 assert( result->sender );
439 list_remove( &result->sender_entry );
440 result->sender = NULL;
441 if (!result->receiver) free_result( result );
444 /* store the message result in the appropriate structure */
445 static void store_message_result( struct message_result *res, unsigned long result,
446 unsigned int error )
448 res->result = result;
449 res->error = error;
450 res->replied = 1;
451 if (res->timeout)
453 remove_timeout_user( res->timeout );
454 res->timeout = NULL;
456 if (res->sender)
458 if (res->callback_msg)
460 /* queue the callback message in the sender queue */
461 struct callback_msg_data *data = res->callback_msg->data;
462 data->result = result;
463 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
464 set_queue_bits( res->sender, QS_SENDMESSAGE );
465 res->callback_msg = NULL;
466 remove_result_from_sender( res );
468 else
470 /* wake sender queue if waiting on this result */
471 if (list_head(&res->sender->send_result) == &res->sender_entry)
472 set_queue_bits( res->sender, QS_SMRESULT );
478 /* free a message when deleting a queue or window */
479 static void free_message( struct message *msg )
481 struct message_result *result = msg->result;
482 if (result)
484 result->msg = NULL;
485 if (result->sender)
487 result->receiver = NULL;
488 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
490 else free_result( result );
492 free( msg->data );
493 free( msg );
496 /* remove (and free) a message from a message list */
497 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
498 enum message_kind kind )
500 list_remove( &msg->entry );
501 switch(kind)
503 case SEND_MESSAGE:
504 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
505 break;
506 case POST_MESSAGE:
507 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
508 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
509 break;
511 free_message( msg );
514 /* message timed out without getting a reply */
515 static void result_timeout( void *private )
517 struct message_result *result = private;
519 assert( !result->replied );
521 result->timeout = NULL;
523 if (result->msg) /* not received yet */
525 struct message *msg = result->msg;
527 result->msg = NULL;
528 msg->result = NULL;
529 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
530 result->receiver = NULL;
531 if (!result->sender)
533 free_result( result );
534 return;
538 store_message_result( result, 0, STATUS_TIMEOUT );
541 /* allocate and fill a message result structure */
542 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
543 struct msg_queue *recv_queue,
544 struct message *msg, timeout_t timeout )
546 struct message_result *result = mem_alloc( sizeof(*result) );
547 if (result)
549 result->msg = msg;
550 result->sender = send_queue;
551 result->receiver = recv_queue;
552 result->replied = 0;
553 result->data = NULL;
554 result->data_size = 0;
555 result->timeout = NULL;
557 if (msg->type == MSG_CALLBACK)
559 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
561 if (!callback_msg)
563 free( result );
564 return NULL;
566 callback_msg->type = MSG_CALLBACK_RESULT;
567 callback_msg->win = msg->win;
568 callback_msg->msg = msg->msg;
569 callback_msg->wparam = 0;
570 callback_msg->lparam = 0;
571 callback_msg->time = get_tick_count();
572 callback_msg->x = 0;
573 callback_msg->y = 0;
574 callback_msg->info = 0;
575 callback_msg->result = NULL;
576 /* steal the data from the original message */
577 callback_msg->data = msg->data;
578 callback_msg->data_size = msg->data_size;
579 msg->data = NULL;
580 msg->data_size = 0;
582 result->callback_msg = callback_msg;
583 list_add_head( &send_queue->callback_result, &result->sender_entry );
585 else
587 result->callback_msg = NULL;
588 list_add_head( &send_queue->send_result, &result->sender_entry );
591 if (timeout != TIMEOUT_INFINITE)
592 result->timeout = add_timeout_user( timeout, result_timeout, result );
594 return result;
597 /* receive a message, removing it from the sent queue */
598 static void receive_message( struct msg_queue *queue, struct message *msg,
599 struct get_message_reply *reply )
601 struct message_result *result = msg->result;
603 reply->total = msg->data_size;
604 if (msg->data_size > get_reply_max_size())
606 set_error( STATUS_BUFFER_OVERFLOW );
607 return;
609 reply->type = msg->type;
610 reply->win = msg->win;
611 reply->msg = msg->msg;
612 reply->wparam = msg->wparam;
613 reply->lparam = msg->lparam;
614 reply->x = msg->x;
615 reply->y = msg->y;
616 reply->time = msg->time;
617 reply->info = msg->info;
619 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
621 list_remove( &msg->entry );
622 /* put the result on the receiver result stack */
623 if (result)
625 result->msg = NULL;
626 result->recv_next = queue->recv_result;
627 queue->recv_result = result;
629 free( msg );
630 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
633 /* set the result of the current received message */
634 static void reply_message( struct msg_queue *queue, unsigned long result,
635 unsigned int error, int remove, const void *data, data_size_t len )
637 struct message_result *res = queue->recv_result;
639 if (remove)
641 queue->recv_result = res->recv_next;
642 res->receiver = NULL;
643 if (!res->sender) /* no one waiting for it */
645 free_result( res );
646 return;
649 if (!res->replied)
651 if (len && (res->data = memdup( data, len ))) res->data_size = len;
652 store_message_result( res, result, error );
656 /* retrieve a posted message */
657 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
658 unsigned int first, unsigned int last, unsigned int flags,
659 struct get_message_reply *reply )
661 struct message *msg;
663 /* check against the filters */
664 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
666 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
667 if (!check_msg_filter( msg->msg, first, last )) continue;
668 goto found; /* found one */
670 return 0;
672 /* return it to the app */
673 found:
674 reply->total = msg->data_size;
675 if (msg->data_size > get_reply_max_size())
677 set_error( STATUS_BUFFER_OVERFLOW );
678 return 1;
680 reply->type = msg->type;
681 reply->win = msg->win;
682 reply->msg = msg->msg;
683 reply->wparam = msg->wparam;
684 reply->lparam = msg->lparam;
685 reply->x = msg->x;
686 reply->y = msg->y;
687 reply->time = msg->time;
688 reply->info = msg->info;
690 if (flags & PM_REMOVE)
692 if (msg->data)
694 set_reply_data_ptr( msg->data, msg->data_size );
695 msg->data = NULL;
696 msg->data_size = 0;
698 remove_queue_message( queue, msg, POST_MESSAGE );
700 else if (msg->data) set_reply_data( msg->data, msg->data_size );
702 return 1;
705 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
706 struct get_message_reply *reply )
708 if (queue->quit_message)
710 reply->total = 0;
711 reply->type = MSG_POSTED;
712 reply->win = NULL;
713 reply->msg = WM_QUIT;
714 reply->wparam = queue->exit_code;
715 reply->lparam = 0;
716 reply->x = 0;
717 reply->y = 0;
718 reply->time = get_tick_count();
719 reply->info = 0;
721 if (flags & PM_REMOVE)
723 queue->quit_message = 0;
724 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
725 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
727 return 1;
729 else
730 return 0;
733 /* empty a message list and free all the messages */
734 static void empty_msg_list( struct list *list )
736 struct list *ptr;
738 while ((ptr = list_head( list )) != NULL)
740 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
741 list_remove( &msg->entry );
742 free_message( msg );
746 /* cleanup all pending results when deleting a queue */
747 static void cleanup_results( struct msg_queue *queue )
749 struct list *entry;
751 while ((entry = list_head( &queue->send_result )) != NULL)
753 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
756 while ((entry = list_head( &queue->callback_result )) != NULL)
758 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
761 while (queue->recv_result)
762 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
765 /* check if the thread owning the queue is hung (not checking for messages) */
766 static int is_queue_hung( struct msg_queue *queue )
768 struct wait_queue_entry *entry;
770 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
771 return 0; /* less than 5 seconds since last get message -> not hung */
773 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
775 if (entry->thread->queue == queue)
776 return 0; /* thread is waiting on queue -> not hung */
778 return 1;
781 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
783 struct msg_queue *queue = (struct msg_queue *)obj;
784 struct process *process = entry->thread->process;
786 /* a thread can only wait on its own queue */
787 if (entry->thread->queue != queue)
789 set_error( STATUS_ACCESS_DENIED );
790 return 0;
792 /* if waiting on the main process queue, set the idle event */
793 if (process->queue == queue)
795 if (process->idle_event) set_event( process->idle_event );
797 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
798 set_fd_events( queue->fd, POLLIN );
799 add_queue( obj, entry );
800 return 1;
803 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
805 struct msg_queue *queue = (struct msg_queue *)obj;
806 struct process *process = entry->thread->process;
808 remove_queue( obj, entry );
809 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
810 set_fd_events( queue->fd, 0 );
812 assert( entry->thread->queue == queue );
814 /* if waiting on the main process queue, reset the idle event */
815 if (process->queue == queue)
817 if (process->idle_event) reset_event( process->idle_event );
821 static void msg_queue_dump( struct object *obj, int verbose )
823 struct msg_queue *queue = (struct msg_queue *)obj;
824 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
825 queue->wake_bits, queue->wake_mask );
828 static int msg_queue_signaled( struct object *obj, struct thread *thread )
830 struct msg_queue *queue = (struct msg_queue *)obj;
831 int ret = 0;
833 if (queue->fd)
835 if ((ret = check_fd_events( queue->fd, POLLIN )))
836 /* stop waiting on select() if we are signaled */
837 set_fd_events( queue->fd, 0 );
838 else if (!list_empty( &obj->wait_queue ))
839 /* restart waiting on poll() if we are no longer signaled */
840 set_fd_events( queue->fd, POLLIN );
842 return ret || is_signaled( queue );
845 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
847 struct msg_queue *queue = (struct msg_queue *)obj;
848 queue->wake_mask = 0;
849 queue->changed_mask = 0;
850 return 0; /* Not abandoned */
853 static void msg_queue_destroy( struct object *obj )
855 struct msg_queue *queue = (struct msg_queue *)obj;
856 struct list *ptr;
857 int i;
859 cleanup_results( queue );
860 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
862 while ((ptr = list_head( &queue->pending_timers )))
864 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
865 list_remove( &timer->entry );
866 free( timer );
868 while ((ptr = list_head( &queue->expired_timers )))
870 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
871 list_remove( &timer->entry );
872 free( timer );
874 if (queue->timeout) remove_timeout_user( queue->timeout );
875 if (queue->input) release_object( queue->input );
876 if (queue->hooks) release_object( queue->hooks );
877 if (queue->fd) release_object( queue->fd );
880 static void msg_queue_poll_event( struct fd *fd, int event )
882 struct msg_queue *queue = get_fd_user( fd );
883 assert( queue->obj.ops == &msg_queue_ops );
885 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
886 wake_up( &queue->obj, 0 );
889 static void thread_input_dump( struct object *obj, int verbose )
891 struct thread_input *input = (struct thread_input *)obj;
892 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
893 input->focus, input->capture, input->active );
896 static void thread_input_destroy( struct object *obj )
898 struct thread_input *input = (struct thread_input *)obj;
900 if (foreground_input == input) foreground_input = NULL;
901 empty_msg_list( &input->msg_list );
902 if (input->desktop) release_object( input->desktop );
905 /* fix the thread input data when a window is destroyed */
906 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
908 struct thread_input *input = queue->input;
910 if (window == input->focus) input->focus = 0;
911 if (window == input->capture) input->capture = 0;
912 if (window == input->active) input->active = 0;
913 if (window == input->menu_owner) input->menu_owner = 0;
914 if (window == input->move_size) input->move_size = 0;
915 if (window == input->caret) set_caret_window( input, 0 );
918 /* check if the specified window can be set in the input data of a given queue */
919 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
921 struct thread *thread;
922 int ret = 0;
924 if (!window) return 1; /* we can always clear the data */
926 if ((thread = get_window_thread( window )))
928 ret = (queue->input == thread->queue->input);
929 if (!ret) set_error( STATUS_ACCESS_DENIED );
930 release_object( thread );
932 else set_error( STATUS_INVALID_HANDLE );
934 return ret;
937 /* make sure the specified thread has a queue */
938 int init_thread_queue( struct thread *thread )
940 if (thread->queue) return 1;
941 return (create_msg_queue( thread, NULL ) != NULL);
944 /* attach two thread input data structures */
945 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
947 struct desktop *desktop;
948 struct thread_input *input;
950 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
951 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
952 input = (struct thread_input *)grab_object( thread_to->queue->input );
953 if (input->desktop != desktop)
955 set_error( STATUS_ACCESS_DENIED );
956 release_object( input );
957 release_object( desktop );
958 return 0;
960 release_object( desktop );
962 if (thread_from->queue)
964 release_thread_input( thread_from );
965 thread_from->queue->input = input;
967 else
969 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
971 memset( input->keystate, 0, sizeof(input->keystate) );
972 return 1;
975 /* detach two thread input data structures */
976 void detach_thread_input( struct thread *thread_from )
978 struct thread_input *input;
980 if ((input = create_thread_input( thread_from )))
982 release_thread_input( thread_from );
983 thread_from->queue->input = input;
988 /* set the next timer to expire */
989 static void set_next_timer( struct msg_queue *queue )
991 struct list *ptr;
993 if (queue->timeout)
995 remove_timeout_user( queue->timeout );
996 queue->timeout = NULL;
998 if ((ptr = list_head( &queue->pending_timers )))
1000 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1001 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1003 /* set/clear QS_TIMER bit */
1004 if (list_empty( &queue->expired_timers ))
1005 clear_queue_bits( queue, QS_TIMER );
1006 else
1007 set_queue_bits( queue, QS_TIMER );
1010 /* find a timer from its window and id */
1011 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1012 unsigned int msg, unsigned long id )
1014 struct list *ptr;
1016 /* we need to search both lists */
1018 LIST_FOR_EACH( ptr, &queue->pending_timers )
1020 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1021 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1023 LIST_FOR_EACH( ptr, &queue->expired_timers )
1025 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1026 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1028 return NULL;
1031 /* callback for the next timer expiration */
1032 static void timer_callback( void *private )
1034 struct msg_queue *queue = private;
1035 struct list *ptr;
1037 queue->timeout = NULL;
1038 /* move on to the next timer */
1039 ptr = list_head( &queue->pending_timers );
1040 list_remove( ptr );
1041 list_add_tail( &queue->expired_timers, ptr );
1042 set_next_timer( queue );
1045 /* link a timer at its rightful place in the queue list */
1046 static void link_timer( struct msg_queue *queue, struct timer *timer )
1048 struct list *ptr;
1050 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1052 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1053 if (t->when >= timer->when) break;
1055 list_add_before( ptr, &timer->entry );
1058 /* remove a timer from the queue timer list and free it */
1059 static void free_timer( struct msg_queue *queue, struct timer *timer )
1061 list_remove( &timer->entry );
1062 free( timer );
1063 set_next_timer( queue );
1066 /* restart an expired timer */
1067 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1069 list_remove( &timer->entry );
1070 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1071 link_timer( queue, timer );
1072 set_next_timer( queue );
1075 /* find an expired timer matching the filtering parameters */
1076 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1077 unsigned int get_first, unsigned int get_last,
1078 int remove )
1080 struct list *ptr;
1082 LIST_FOR_EACH( ptr, &queue->expired_timers )
1084 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1085 if (win && timer->win != win) continue;
1086 if (check_msg_filter( timer->msg, get_first, get_last ))
1088 if (remove) restart_timer( queue, timer );
1089 return timer;
1092 return NULL;
1095 /* add a timer */
1096 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1098 struct timer *timer = mem_alloc( sizeof(*timer) );
1099 if (timer)
1101 timer->rate = max( rate, 1 );
1102 timer->when = current_time + (timeout_t)timer->rate * 10000;
1103 link_timer( queue, timer );
1104 /* check if we replaced the next timer */
1105 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1107 return timer;
1110 /* change the input key state for a given key */
1111 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1113 if (down)
1115 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1116 input->keystate[key] |= 0x80;
1118 else input->keystate[key] &= ~0x80;
1121 /* update the input key state for a keyboard message */
1122 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1124 unsigned char key;
1125 int down = 0, extended;
1127 switch (msg->msg)
1129 case WM_LBUTTONDOWN:
1130 down = 1;
1131 /* fall through */
1132 case WM_LBUTTONUP:
1133 set_input_key_state( input, VK_LBUTTON, down );
1134 break;
1135 case WM_MBUTTONDOWN:
1136 down = 1;
1137 /* fall through */
1138 case WM_MBUTTONUP:
1139 set_input_key_state( input, VK_MBUTTON, down );
1140 break;
1141 case WM_RBUTTONDOWN:
1142 down = 1;
1143 /* fall through */
1144 case WM_RBUTTONUP:
1145 set_input_key_state( input, VK_RBUTTON, down );
1146 break;
1147 case WM_XBUTTONDOWN:
1148 down = 1;
1149 /* fall through */
1150 case WM_XBUTTONUP:
1151 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1152 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1153 break;
1154 case WM_KEYDOWN:
1155 case WM_SYSKEYDOWN:
1156 down = 1;
1157 /* fall through */
1158 case WM_KEYUP:
1159 case WM_SYSKEYUP:
1160 key = (unsigned char)msg->wparam;
1161 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1162 set_input_key_state( input, key, down );
1163 switch(key)
1165 case VK_SHIFT:
1166 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1167 break;
1168 case VK_CONTROL:
1169 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1170 break;
1171 case VK_MENU:
1172 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1173 break;
1174 case VK_LCONTROL:
1175 case VK_RCONTROL:
1176 set_input_key_state( input, VK_CONTROL, down );
1177 break;
1178 case VK_LMENU:
1179 case VK_RMENU:
1180 set_input_key_state( input, VK_MENU, down );
1181 break;
1182 case VK_LSHIFT:
1183 case VK_RSHIFT:
1184 set_input_key_state( input, VK_SHIFT, down );
1185 break;
1187 break;
1191 /* release the hardware message currently being processed by the given thread */
1192 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1193 int remove, user_handle_t new_win )
1195 struct thread_input *input = queue->input;
1196 struct message *msg;
1198 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1200 if (msg->unique_id == hw_id) break;
1202 if (&msg->entry == &input->msg_list) return; /* not found */
1204 /* clear the queue bit for that message */
1205 if (remove || new_win)
1207 struct message *other;
1208 int clr_bit;
1210 clr_bit = get_hardware_msg_bit( msg );
1211 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1213 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1215 clr_bit = 0;
1216 break;
1219 if (clr_bit) clear_queue_bits( queue, clr_bit );
1222 if (new_win) /* set the new window */
1224 struct thread *owner = get_window_thread( new_win );
1225 if (owner)
1227 msg->win = new_win;
1228 if (owner->queue->input != input)
1230 list_remove( &msg->entry );
1231 if (msg->msg == WM_MOUSEMOVE && merge_message( owner->queue->input, msg ))
1233 free_message( msg );
1234 release_object( owner );
1235 return;
1237 list_add_tail( &owner->queue->input->msg_list, &msg->entry );
1239 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1240 remove = 0;
1241 release_object( owner );
1244 if (remove)
1246 update_input_key_state( input, msg );
1247 list_remove( &msg->entry );
1248 free_message( msg );
1252 /* find the window that should receive a given hardware message */
1253 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1254 unsigned int *msg_code )
1256 user_handle_t win = 0;
1258 *msg_code = msg->msg;
1259 if (is_keyboard_msg( msg ))
1261 if (input && !(win = input->focus))
1263 win = input->active;
1264 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1267 else /* mouse message */
1269 if (!input || !(win = input->capture))
1271 if (!(win = msg->win) || !is_window_visible( win ))
1273 if (input) win = window_from_point( input->desktop, msg->x, msg->y );
1277 return win;
1280 /* queue a hardware message into a given thread input */
1281 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1283 user_handle_t win;
1284 struct thread *thread;
1285 struct thread_input *input = queue ? queue->input : foreground_input;
1286 unsigned int msg_code;
1288 last_input_time = get_tick_count();
1289 win = find_hardware_message_window( input, msg, &msg_code );
1290 if (!win || !(thread = get_window_thread(win)))
1292 if (input) update_input_key_state( input, msg );
1293 free( msg );
1294 return;
1296 input = thread->queue->input;
1298 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1299 else
1301 msg->unique_id = 0; /* will be set once we return it to the app */
1302 list_add_tail( &input->msg_list, &msg->entry );
1303 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1305 release_object( thread );
1308 /* check message filter for a hardware message */
1309 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1310 user_handle_t filter_win, unsigned int first, unsigned int last )
1312 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1314 /* we can only test the window for a keyboard message since the
1315 * dest window for a mouse message depends on hittest */
1316 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1317 return 0;
1318 /* the message code is final for a keyboard message, we can simply check it */
1319 return check_msg_filter( msg_code, first, last );
1321 else /* mouse message */
1323 /* we need to check all possible values that the message can have in the end */
1325 if (check_msg_filter( msg_code, first, last )) return 1;
1326 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1328 /* all other messages can become non-client messages */
1329 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1331 /* clicks can become double-clicks or non-client double-clicks */
1332 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1333 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1335 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1336 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1338 return 0;
1343 /* find a hardware message for the given queue */
1344 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1345 unsigned int first, unsigned int last, struct get_message_reply *reply )
1347 struct thread_input *input = thread->queue->input;
1348 struct thread *win_thread;
1349 struct list *ptr;
1350 user_handle_t win;
1351 int clear_bits, got_one = 0;
1352 unsigned int msg_code;
1354 ptr = list_head( &input->msg_list );
1355 if (hw_id)
1357 while (ptr)
1359 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1360 if (msg->unique_id == hw_id) break;
1361 ptr = list_next( &input->msg_list, ptr );
1363 if (!ptr) ptr = list_head( &input->msg_list );
1364 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1367 if (ptr == list_head( &input->msg_list ))
1368 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1369 else
1370 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1372 while (ptr)
1374 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1375 ptr = list_next( &input->msg_list, ptr );
1376 win = find_hardware_message_window( input, msg, &msg_code );
1377 if (!win || !(win_thread = get_window_thread( win )))
1379 /* no window at all, remove it */
1380 update_input_key_state( input, msg );
1381 list_remove( &msg->entry );
1382 free_message( msg );
1383 continue;
1385 if (win_thread != thread)
1387 if (win_thread->queue->input == input)
1389 /* wake the other thread */
1390 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1391 got_one = 1;
1393 else
1395 /* for another thread input, drop it */
1396 update_input_key_state( input, msg );
1397 list_remove( &msg->entry );
1398 free_message( msg );
1400 release_object( win_thread );
1401 continue;
1403 release_object( win_thread );
1405 /* if we already got a message for another thread, or if it doesn't
1406 * match the filter we skip it */
1407 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1409 clear_bits &= ~get_hardware_msg_bit( msg );
1410 continue;
1412 /* now we can return it */
1413 if (!msg->unique_id) msg->unique_id = get_unique_id();
1414 reply->type = MSG_HARDWARE;
1415 reply->win = win;
1416 reply->msg = msg_code;
1417 reply->wparam = msg->wparam;
1418 reply->lparam = msg->lparam;
1419 reply->x = msg->x;
1420 reply->y = msg->y;
1421 reply->time = msg->time;
1422 reply->info = msg->info;
1423 reply->hw_id = msg->unique_id;
1424 return 1;
1426 /* nothing found, clear the hardware queue bits */
1427 clear_queue_bits( thread->queue, clear_bits );
1428 return 0;
1431 /* increment (or decrement if 'incr' is negative) the queue paint count */
1432 void inc_queue_paint_count( struct thread *thread, int incr )
1434 struct msg_queue *queue = thread->queue;
1436 assert( queue );
1438 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1440 if (queue->paint_count)
1441 set_queue_bits( queue, QS_PAINT );
1442 else
1443 clear_queue_bits( queue, QS_PAINT );
1447 /* remove all messages and timers belonging to a certain window */
1448 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1450 struct msg_queue *queue = thread->queue;
1451 struct list *ptr;
1452 int i;
1454 if (!queue) return;
1456 /* remove timers */
1458 ptr = list_head( &queue->pending_timers );
1459 while (ptr)
1461 struct list *next = list_next( &queue->pending_timers, ptr );
1462 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1463 if (timer->win == win) free_timer( queue, timer );
1464 ptr = next;
1466 ptr = list_head( &queue->expired_timers );
1467 while (ptr)
1469 struct list *next = list_next( &queue->expired_timers, ptr );
1470 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1471 if (timer->win == win) free_timer( queue, timer );
1472 ptr = next;
1475 /* remove messages */
1476 for (i = 0; i < NB_MSG_KINDS; i++)
1478 struct list *ptr, *next;
1480 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1482 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1483 if (msg->win == win) remove_queue_message( queue, msg, i );
1487 thread_input_cleanup_window( queue, win );
1490 /* post a message to a window; used by socket handling */
1491 void post_message( user_handle_t win, unsigned int message,
1492 unsigned long wparam, unsigned long lparam )
1494 struct message *msg;
1495 struct thread *thread = get_window_thread( win );
1497 if (!thread) return;
1499 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1501 msg->type = MSG_POSTED;
1502 msg->win = get_user_full_handle( win );
1503 msg->msg = message;
1504 msg->wparam = wparam;
1505 msg->lparam = lparam;
1506 msg->time = get_tick_count();
1507 msg->x = 0;
1508 msg->y = 0;
1509 msg->info = 0;
1510 msg->result = NULL;
1511 msg->data = NULL;
1512 msg->data_size = 0;
1514 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1515 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1517 release_object( thread );
1520 /* post a win event */
1521 void post_win_event( struct thread *thread, unsigned int event,
1522 user_handle_t win, unsigned int object_id,
1523 unsigned int child_id, void *hook_proc,
1524 const WCHAR *module, data_size_t module_size,
1525 user_handle_t hook)
1527 struct message *msg;
1529 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1531 struct winevent_msg_data *data;
1533 msg->type = MSG_WINEVENT;
1534 msg->win = get_user_full_handle( win );
1535 msg->msg = event;
1536 msg->wparam = object_id;
1537 msg->lparam = child_id;
1538 msg->time = get_tick_count();
1539 msg->x = 0;
1540 msg->y = 0;
1541 msg->info = 0;
1542 msg->result = NULL;
1544 if ((data = malloc( sizeof(*data) + module_size )))
1546 data->hook = hook;
1547 data->tid = get_thread_id( current );
1548 data->hook_proc = hook_proc;
1549 memcpy( data + 1, module, module_size );
1551 msg->data = data;
1552 msg->data_size = sizeof(*data) + module_size;
1554 if (debug_level > 1)
1555 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1556 get_thread_id(thread), event, win, object_id, child_id );
1557 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1558 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1560 else
1561 free( msg );
1566 /* check if the thread owning the window is hung */
1567 DECL_HANDLER(is_window_hung)
1569 struct thread *thread;
1571 thread = get_window_thread( req->win );
1572 if (thread)
1574 reply->is_hung = is_queue_hung( thread->queue );
1575 release_object( thread );
1577 else reply->is_hung = 0;
1581 /* get the message queue of the current thread */
1582 DECL_HANDLER(get_msg_queue)
1584 struct msg_queue *queue = get_current_queue();
1586 reply->handle = 0;
1587 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1591 /* set the file descriptor associated to the current thread queue */
1592 DECL_HANDLER(set_queue_fd)
1594 struct msg_queue *queue = get_current_queue();
1595 struct file *file;
1596 int unix_fd;
1598 if (queue->fd) /* fd can only be set once */
1600 set_error( STATUS_ACCESS_DENIED );
1601 return;
1603 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
1605 if ((unix_fd = get_file_unix_fd( file )) != -1)
1607 if ((unix_fd = dup( unix_fd )) != -1)
1608 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
1609 else
1610 file_set_error();
1612 release_object( file );
1616 /* set the current message queue wakeup mask */
1617 DECL_HANDLER(set_queue_mask)
1619 struct msg_queue *queue = get_current_queue();
1621 if (queue)
1623 queue->wake_mask = req->wake_mask;
1624 queue->changed_mask = req->changed_mask;
1625 reply->wake_bits = queue->wake_bits;
1626 reply->changed_bits = queue->changed_bits;
1627 if (is_signaled( queue ))
1629 /* if skip wait is set, do what would have been done in the subsequent wait */
1630 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1631 else wake_up( &queue->obj, 0 );
1637 /* get the current message queue status */
1638 DECL_HANDLER(get_queue_status)
1640 struct msg_queue *queue = current->queue;
1641 if (queue)
1643 reply->wake_bits = queue->wake_bits;
1644 reply->changed_bits = queue->changed_bits;
1645 if (req->clear) queue->changed_bits = 0;
1647 else reply->wake_bits = reply->changed_bits = 0;
1651 /* send a message to a thread queue */
1652 DECL_HANDLER(send_message)
1654 struct message *msg;
1655 struct msg_queue *send_queue = get_current_queue();
1656 struct msg_queue *recv_queue = NULL;
1657 struct thread *thread = NULL;
1659 if (!(thread = get_thread_from_id( req->id ))) return;
1661 if (!(recv_queue = thread->queue))
1663 set_error( STATUS_INVALID_PARAMETER );
1664 release_object( thread );
1665 return;
1667 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1669 set_error( STATUS_TIMEOUT );
1670 release_object( thread );
1671 return;
1674 if ((msg = mem_alloc( sizeof(*msg) )))
1676 msg->type = req->type;
1677 msg->win = get_user_full_handle( req->win );
1678 msg->msg = req->msg;
1679 msg->wparam = req->wparam;
1680 msg->lparam = req->lparam;
1681 msg->time = get_tick_count();
1682 msg->x = 0;
1683 msg->y = 0;
1684 msg->info = 0;
1685 msg->result = NULL;
1686 msg->data = NULL;
1687 msg->data_size = get_req_data_size();
1689 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1691 free( msg );
1692 release_object( thread );
1693 return;
1696 switch(msg->type)
1698 case MSG_OTHER_PROCESS:
1699 case MSG_ASCII:
1700 case MSG_UNICODE:
1701 case MSG_CALLBACK:
1702 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
1704 free_message( msg );
1705 break;
1707 /* fall through */
1708 case MSG_NOTIFY:
1709 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1710 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1711 break;
1712 case MSG_POSTED:
1713 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1714 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1715 break;
1716 case MSG_HARDWARE: /* should use send_hardware_message instead */
1717 case MSG_CALLBACK_RESULT: /* cannot send this one */
1718 default:
1719 set_error( STATUS_INVALID_PARAMETER );
1720 free( msg );
1721 break;
1724 release_object( thread );
1727 /* send a hardware message to a thread queue */
1728 DECL_HANDLER(send_hardware_message)
1730 struct message *msg;
1731 struct msg_queue *recv_queue = NULL;
1732 struct thread *thread = NULL;
1734 if (req->id)
1736 if (!(thread = get_thread_from_id( req->id ))) return;
1739 if (thread && !(recv_queue = thread->queue))
1741 set_error( STATUS_INVALID_PARAMETER );
1742 release_object( thread );
1743 return;
1746 if ((msg = mem_alloc( sizeof(*msg) )))
1748 msg->type = MSG_HARDWARE;
1749 msg->win = get_user_full_handle( req->win );
1750 msg->msg = req->msg;
1751 msg->wparam = req->wparam;
1752 msg->lparam = req->lparam;
1753 msg->time = req->time;
1754 msg->x = req->x;
1755 msg->y = req->y;
1756 msg->info = req->info;
1757 msg->result = NULL;
1758 msg->data = NULL;
1759 msg->data_size = 0;
1760 queue_hardware_message( recv_queue, msg );
1762 if (thread) release_object( thread );
1765 /* post a quit message to the current queue */
1766 DECL_HANDLER(post_quit_message)
1768 struct msg_queue *queue = get_current_queue();
1770 if (!queue)
1771 return;
1773 queue->quit_message = 1;
1774 queue->exit_code = req->exit_code;
1775 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1778 /* get a message from the current queue */
1779 DECL_HANDLER(get_message)
1781 struct timer *timer;
1782 struct list *ptr;
1783 struct msg_queue *queue = get_current_queue();
1784 user_handle_t get_win = get_user_full_handle( req->get_win );
1785 unsigned int filter = req->flags >> 16;
1787 reply->active_hooks = get_active_hooks();
1789 if (!queue) return;
1790 queue->last_get_msg = current_time;
1791 if (!filter) filter = QS_ALLINPUT;
1793 /* first check for sent messages */
1794 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1796 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1797 receive_message( queue, msg, reply );
1798 return;
1801 /* clear changed bits so we can wait on them if we don't find a message */
1802 if (filter & QS_POSTMESSAGE)
1804 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
1805 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
1807 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
1808 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
1810 /* then check for posted messages */
1811 if ((filter & QS_POSTMESSAGE) &&
1812 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1813 return;
1815 /* only check for quit messages if not posted messages pending.
1816 * note: the quit message isn't filtered */
1817 if (get_quit_message( queue, req->flags, reply ))
1818 return;
1820 /* then check for any raw hardware message */
1821 if ((filter & QS_INPUT) &&
1822 filter_contains_hw_range( req->get_first, req->get_last ) &&
1823 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1824 return;
1826 /* now check for WM_PAINT */
1827 if ((filter & QS_PAINT) &&
1828 queue->paint_count &&
1829 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1830 (reply->win = find_window_to_repaint( get_win, current )))
1832 reply->type = MSG_POSTED;
1833 reply->msg = WM_PAINT;
1834 reply->wparam = 0;
1835 reply->lparam = 0;
1836 reply->x = 0;
1837 reply->y = 0;
1838 reply->time = get_tick_count();
1839 reply->info = 0;
1840 return;
1843 /* now check for timer */
1844 if ((filter & QS_TIMER) &&
1845 (timer = find_expired_timer( queue, get_win, req->get_first,
1846 req->get_last, (req->flags & PM_REMOVE) )))
1848 reply->type = MSG_POSTED;
1849 reply->win = timer->win;
1850 reply->msg = timer->msg;
1851 reply->wparam = timer->id;
1852 reply->lparam = timer->lparam;
1853 reply->x = 0;
1854 reply->y = 0;
1855 reply->time = get_tick_count();
1856 reply->info = 0;
1857 return;
1860 queue->wake_mask = req->wake_mask;
1861 queue->changed_mask = req->changed_mask;
1862 set_error( STATUS_PENDING ); /* FIXME */
1866 /* reply to a sent message */
1867 DECL_HANDLER(reply_message)
1869 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1870 else if (current->queue->recv_result)
1871 reply_message( current->queue, req->result, 0, req->remove,
1872 get_req_data(), get_req_data_size() );
1876 /* accept the current hardware message */
1877 DECL_HANDLER(accept_hardware_message)
1879 if (current->queue)
1880 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1881 else
1882 set_error( STATUS_ACCESS_DENIED );
1886 /* retrieve the reply for the last message sent */
1887 DECL_HANDLER(get_message_reply)
1889 struct message_result *result;
1890 struct list *entry;
1891 struct msg_queue *queue = current->queue;
1893 if (queue)
1895 set_error( STATUS_PENDING );
1896 reply->result = 0;
1898 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1900 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1901 if (result->replied || req->cancel)
1903 if (result->replied)
1905 reply->result = result->result;
1906 set_error( result->error );
1907 if (result->data)
1909 data_size_t data_len = min( result->data_size, get_reply_max_size() );
1910 set_reply_data_ptr( result->data, data_len );
1911 result->data = NULL;
1912 result->data_size = 0;
1915 remove_result_from_sender( result );
1917 entry = list_head( &queue->send_result );
1918 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1919 else
1921 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1922 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1926 else set_error( STATUS_ACCESS_DENIED );
1930 /* set a window timer */
1931 DECL_HANDLER(set_win_timer)
1933 struct timer *timer;
1934 struct msg_queue *queue;
1935 struct thread *thread = NULL;
1936 user_handle_t win = 0;
1937 unsigned long id = req->id;
1939 if (req->win)
1941 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1943 set_error( STATUS_INVALID_HANDLE );
1944 return;
1946 if (thread->process != current->process)
1948 release_object( thread );
1949 set_error( STATUS_ACCESS_DENIED );
1950 return;
1952 queue = thread->queue;
1953 /* remove it if it existed already */
1954 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1956 else
1958 queue = get_current_queue();
1959 /* look for a timer with this id */
1960 if (id && (timer = find_timer( queue, NULL, req->msg, id )))
1962 /* free and reuse id */
1963 free_timer( queue, timer );
1965 else
1967 /* find a free id for it */
1970 id = queue->next_timer_id;
1971 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
1973 while (find_timer( queue, 0, req->msg, id ));
1977 if ((timer = set_timer( queue, req->rate )))
1979 timer->win = win;
1980 timer->msg = req->msg;
1981 timer->id = id;
1982 timer->lparam = req->lparam;
1983 reply->id = id;
1985 if (thread) release_object( thread );
1988 /* kill a window timer */
1989 DECL_HANDLER(kill_win_timer)
1991 struct timer *timer;
1992 struct thread *thread;
1993 user_handle_t win = 0;
1995 if (req->win)
1997 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1999 set_error( STATUS_INVALID_HANDLE );
2000 return;
2002 if (thread->process != current->process)
2004 release_object( thread );
2005 set_error( STATUS_ACCESS_DENIED );
2006 return;
2009 else thread = (struct thread *)grab_object( current );
2011 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2012 free_timer( thread->queue, timer );
2013 else
2014 set_error( STATUS_INVALID_PARAMETER );
2016 release_object( thread );
2020 /* attach (or detach) thread inputs */
2021 DECL_HANDLER(attach_thread_input)
2023 struct thread *thread_from = get_thread_from_id( req->tid_from );
2024 struct thread *thread_to = get_thread_from_id( req->tid_to );
2026 if (!thread_from || !thread_to)
2028 if (thread_from) release_object( thread_from );
2029 if (thread_to) release_object( thread_to );
2030 return;
2032 if (thread_from != thread_to)
2034 if (req->attach) attach_thread_input( thread_from, thread_to );
2035 else
2037 if (thread_from->queue && thread_to->queue &&
2038 thread_from->queue->input == thread_to->queue->input)
2039 detach_thread_input( thread_from );
2040 else
2041 set_error( STATUS_ACCESS_DENIED );
2044 else set_error( STATUS_ACCESS_DENIED );
2045 release_object( thread_from );
2046 release_object( thread_to );
2050 /* get thread input data */
2051 DECL_HANDLER(get_thread_input)
2053 struct thread *thread = NULL;
2054 struct thread_input *input;
2056 if (req->tid)
2058 if (!(thread = get_thread_from_id( req->tid ))) return;
2059 input = thread->queue ? thread->queue->input : NULL;
2061 else input = foreground_input; /* get the foreground thread info */
2063 if (input)
2065 reply->focus = input->focus;
2066 reply->capture = input->capture;
2067 reply->active = input->active;
2068 reply->menu_owner = input->menu_owner;
2069 reply->move_size = input->move_size;
2070 reply->caret = input->caret;
2071 reply->rect = input->caret_rect;
2073 else
2075 reply->focus = 0;
2076 reply->capture = 0;
2077 reply->active = 0;
2078 reply->menu_owner = 0;
2079 reply->move_size = 0;
2080 reply->caret = 0;
2081 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
2083 /* foreground window is active window of foreground thread */
2084 reply->foreground = foreground_input ? foreground_input->active : 0;
2085 if (thread) release_object( thread );
2089 /* retrieve queue keyboard state for a given thread */
2090 DECL_HANDLER(get_key_state)
2092 struct thread *thread;
2093 struct thread_input *input;
2095 if (!(thread = get_thread_from_id( req->tid ))) return;
2096 input = thread->queue ? thread->queue->input : NULL;
2097 if (input)
2099 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
2100 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
2102 release_object( thread );
2106 /* set queue keyboard state for a given thread */
2107 DECL_HANDLER(set_key_state)
2109 struct thread *thread = NULL;
2110 struct thread_input *input;
2112 if (!(thread = get_thread_from_id( req->tid ))) return;
2113 input = thread->queue ? thread->queue->input : NULL;
2114 if (input)
2116 data_size_t size = min( sizeof(input->keystate), get_req_data_size() );
2117 if (size) memcpy( input->keystate, get_req_data(), size );
2119 release_object( thread );
2123 /* set the system foreground window */
2124 DECL_HANDLER(set_foreground_window)
2126 struct thread *thread;
2127 struct msg_queue *queue = get_current_queue();
2129 reply->previous = foreground_input ? foreground_input->active : 0;
2130 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
2131 reply->send_msg_new = FALSE;
2133 if (is_top_level_window( req->handle ) &&
2134 ((thread = get_window_thread( req->handle ))))
2136 foreground_input = thread->queue->input;
2137 reply->send_msg_new = (foreground_input != queue->input);
2138 release_object( thread );
2140 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2144 /* set the current thread focus window */
2145 DECL_HANDLER(set_focus_window)
2147 struct msg_queue *queue = get_current_queue();
2149 reply->previous = 0;
2150 if (queue && check_queue_input_window( queue, req->handle ))
2152 reply->previous = queue->input->focus;
2153 queue->input->focus = get_user_full_handle( req->handle );
2158 /* set the current thread active window */
2159 DECL_HANDLER(set_active_window)
2161 struct msg_queue *queue = get_current_queue();
2163 reply->previous = 0;
2164 if (queue && check_queue_input_window( queue, req->handle ))
2166 if (!req->handle || make_window_active( req->handle ))
2168 reply->previous = queue->input->active;
2169 queue->input->active = get_user_full_handle( req->handle );
2171 else set_error( STATUS_INVALID_HANDLE );
2176 /* set the current thread capture window */
2177 DECL_HANDLER(set_capture_window)
2179 struct msg_queue *queue = get_current_queue();
2181 reply->previous = reply->full_handle = 0;
2182 if (queue && check_queue_input_window( queue, req->handle ))
2184 struct thread_input *input = queue->input;
2186 reply->previous = input->capture;
2187 input->capture = get_user_full_handle( req->handle );
2188 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2189 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2190 reply->full_handle = input->capture;
2195 /* Set the current thread caret window */
2196 DECL_HANDLER(set_caret_window)
2198 struct msg_queue *queue = get_current_queue();
2200 reply->previous = 0;
2201 if (queue && check_queue_input_window( queue, req->handle ))
2203 struct thread_input *input = queue->input;
2205 reply->previous = input->caret;
2206 reply->old_rect = input->caret_rect;
2207 reply->old_hide = input->caret_hide;
2208 reply->old_state = input->caret_state;
2210 set_caret_window( input, get_user_full_handle(req->handle) );
2211 input->caret_rect.right = input->caret_rect.left + req->width;
2212 input->caret_rect.bottom = input->caret_rect.top + req->height;
2217 /* Set the current thread caret information */
2218 DECL_HANDLER(set_caret_info)
2220 struct msg_queue *queue = get_current_queue();
2221 struct thread_input *input;
2223 if (!queue) return;
2224 input = queue->input;
2225 reply->full_handle = input->caret;
2226 reply->old_rect = input->caret_rect;
2227 reply->old_hide = input->caret_hide;
2228 reply->old_state = input->caret_state;
2230 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2232 set_error( STATUS_ACCESS_DENIED );
2233 return;
2235 if (req->flags & SET_CARET_POS)
2237 input->caret_rect.right += req->x - input->caret_rect.left;
2238 input->caret_rect.bottom += req->y - input->caret_rect.top;
2239 input->caret_rect.left = req->x;
2240 input->caret_rect.top = req->y;
2242 if (req->flags & SET_CARET_HIDE)
2244 input->caret_hide += req->hide;
2245 if (input->caret_hide < 0) input->caret_hide = 0;
2247 if (req->flags & SET_CARET_STATE)
2249 if (req->state == -1) input->caret_state = !input->caret_state;
2250 else input->caret_state = !!req->state;
2255 /* get the time of the last input event */
2256 DECL_HANDLER(get_last_input_time)
2258 reply->time = last_input_time;