wpp: Get rid of wpp_parse_temp, temp file management is better left to callers.
[wine/multimedia.git] / server / queue.c
blobc49c3e138c66162814d424adaf2b3f3fb95741e8
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 lparam_t 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 lparam_t wparam; /* parameters */
74 lparam_t lparam; /* parameters */
75 unsigned int time; /* message time */
76 void *data; /* message data for sent messages */
77 unsigned int data_size; /* size of message data */
78 unsigned int unique_id; /* unique id for nested hw message waits */
79 struct message_result *result; /* result in sender queue */
82 struct timer
84 struct list entry; /* entry in timer list */
85 timeout_t when; /* next expiration */
86 unsigned int rate; /* timer rate in ms */
87 user_handle_t win; /* window handle */
88 unsigned int msg; /* message to post */
89 lparam_t id; /* timer id */
90 lparam_t lparam; /* lparam for message */
93 struct thread_input
95 struct object obj; /* object header */
96 struct desktop *desktop; /* desktop that this thread input belongs to */
97 user_handle_t focus; /* focus window */
98 user_handle_t capture; /* capture window */
99 user_handle_t active; /* active window */
100 user_handle_t menu_owner; /* current menu owner window */
101 user_handle_t move_size; /* current moving/resizing window */
102 user_handle_t caret; /* caret window */
103 rectangle_t caret_rect; /* caret rectangle */
104 int caret_hide; /* caret hide count */
105 int caret_state; /* caret on/off state */
106 user_handle_t cursor; /* current cursor */
107 int cursor_count; /* cursor show count */
108 struct list msg_list; /* list of hardware messages */
109 unsigned char keystate[256]; /* state of each key */
112 struct msg_queue
114 struct object obj; /* object header */
115 struct fd *fd; /* optional file descriptor to poll */
116 unsigned int wake_bits; /* wakeup bits */
117 unsigned int wake_mask; /* wakeup mask */
118 unsigned int changed_bits; /* changed wakeup bits */
119 unsigned int changed_mask; /* changed wakeup mask */
120 int paint_count; /* pending paint messages count */
121 int quit_message; /* is there a pending quit message? */
122 int exit_code; /* exit code of pending quit message */
123 int cursor_count; /* per-queue cursor show count */
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 lparam_t 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 input->cursor = 0;
235 input->cursor_count = 0;
236 list_init( &input->msg_list );
237 set_caret_window( input, 0 );
238 memset( input->keystate, 0, sizeof(input->keystate) );
240 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
242 release_object( input );
243 return NULL;
246 return input;
249 /* create a message queue object */
250 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
252 struct thread_input *new_input = NULL;
253 struct msg_queue *queue;
254 int i;
256 if (!input)
258 if (!(new_input = create_thread_input( thread ))) return NULL;
259 input = new_input;
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->cursor_count = 0;
272 queue->recv_result = NULL;
273 queue->next_timer_id = 0x7fff;
274 queue->timeout = NULL;
275 queue->input = (struct thread_input *)grab_object( input );
276 queue->hooks = NULL;
277 queue->last_get_msg = current_time;
278 list_init( &queue->send_result );
279 list_init( &queue->callback_result );
280 list_init( &queue->pending_timers );
281 list_init( &queue->expired_timers );
282 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
284 thread->queue = queue;
286 if (new_input) release_object( new_input );
287 return queue;
290 /* free the message queue of a thread at thread exit */
291 void free_msg_queue( struct thread *thread )
293 remove_thread_hooks( thread );
294 if (!thread->queue) return;
295 release_object( thread->queue );
296 thread->queue = NULL;
299 /* change the thread input data of a given thread */
300 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
302 struct msg_queue *queue = thread->queue;
304 if (!queue)
306 thread->queue = create_msg_queue( thread, new_input );
307 return thread->queue != NULL;
309 if (queue->input)
311 queue->input->cursor_count -= queue->cursor_count;
312 release_object( queue->input );
314 queue->input = (struct thread_input *)grab_object( new_input );
315 new_input->cursor_count += queue->cursor_count;
316 return 1;
319 /* get the hook table for a given thread */
320 struct hook_table *get_queue_hooks( struct thread *thread )
322 if (!thread->queue) return NULL;
323 return thread->queue->hooks;
326 /* set the hook table for a given thread, allocating the queue if needed */
327 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
329 struct msg_queue *queue = thread->queue;
330 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
331 if (queue->hooks) release_object( queue->hooks );
332 queue->hooks = hooks;
335 /* check the queue status */
336 static inline int is_signaled( struct msg_queue *queue )
338 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
341 /* set some queue bits */
342 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
344 queue->wake_bits |= bits;
345 queue->changed_bits |= bits;
346 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
349 /* clear some queue bits */
350 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
352 queue->wake_bits &= ~bits;
353 queue->changed_bits &= ~bits;
356 /* check whether msg is a keyboard message */
357 static inline int is_keyboard_msg( struct message *msg )
359 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
362 /* check if message is matched by the filter */
363 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
365 return (msg >= first && msg <= last);
368 /* check whether a message filter contains at least one potential hardware message */
369 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
371 /* hardware message ranges are (in numerical order):
372 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
373 * WM_KEYFIRST .. WM_KEYLAST
374 * WM_MOUSEFIRST .. WM_MOUSELAST
376 if (last < WM_NCMOUSEFIRST) return 0;
377 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
378 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
379 if (first > WM_MOUSELAST) return 0;
380 return 1;
383 /* get the QS_* bit corresponding to a given hardware message */
384 static inline int get_hardware_msg_bit( struct message *msg )
386 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
387 if (is_keyboard_msg( msg )) return QS_KEY;
388 return QS_MOUSEBUTTON;
391 /* get the current thread queue, creating it if needed */
392 static inline struct msg_queue *get_current_queue(void)
394 struct msg_queue *queue = current->queue;
395 if (!queue) queue = create_msg_queue( current, NULL );
396 return queue;
399 /* get a (pseudo-)unique id to tag hardware messages */
400 static inline unsigned int get_unique_id(void)
402 static unsigned int id;
403 if (!++id) id = 1; /* avoid an id of 0 */
404 return id;
407 /* try to merge a message with the last in the list; return 1 if successful */
408 static int merge_message( struct thread_input *input, const struct message *msg )
410 struct message *prev;
411 struct list *ptr = list_tail( &input->msg_list );
413 if (!ptr) return 0;
414 prev = LIST_ENTRY( ptr, struct message, entry );
415 if (prev->result) return 0;
416 if (prev->win && msg->win && prev->win != msg->win) return 0;
417 if (prev->msg != msg->msg) return 0;
418 if (prev->type != msg->type) return 0;
419 /* now we can merge it */
420 prev->wparam = msg->wparam;
421 prev->lparam = msg->lparam;
422 prev->time = msg->time;
423 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
425 struct hardware_msg_data *prev_data = prev->data;
426 struct hardware_msg_data *msg_data = msg->data;
427 prev_data->x = msg_data->x;
428 prev_data->y = msg_data->y;
429 prev_data->info = msg_data->info;
431 return 1;
434 /* free a result structure */
435 static void free_result( struct message_result *result )
437 if (result->timeout) remove_timeout_user( result->timeout );
438 free( result->data );
439 if (result->callback_msg) free_message( result->callback_msg );
440 free( result );
443 /* remove the result from the sender list it is on */
444 static inline void remove_result_from_sender( struct message_result *result )
446 assert( result->sender );
448 list_remove( &result->sender_entry );
449 result->sender = NULL;
450 if (!result->receiver) free_result( result );
453 /* store the message result in the appropriate structure */
454 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
456 res->result = result;
457 res->error = error;
458 res->replied = 1;
459 if (res->timeout)
461 remove_timeout_user( res->timeout );
462 res->timeout = NULL;
464 if (res->sender)
466 if (res->callback_msg)
468 /* queue the callback message in the sender queue */
469 struct callback_msg_data *data = res->callback_msg->data;
470 data->result = result;
471 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
472 set_queue_bits( res->sender, QS_SENDMESSAGE );
473 res->callback_msg = NULL;
474 remove_result_from_sender( res );
476 else
478 /* wake sender queue if waiting on this result */
479 if (list_head(&res->sender->send_result) == &res->sender_entry)
480 set_queue_bits( res->sender, QS_SMRESULT );
486 /* free a message when deleting a queue or window */
487 static void free_message( struct message *msg )
489 struct message_result *result = msg->result;
490 if (result)
492 result->msg = NULL;
493 if (result->sender)
495 result->receiver = NULL;
496 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
498 else free_result( result );
500 free( msg->data );
501 free( msg );
504 /* remove (and free) a message from a message list */
505 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
506 enum message_kind kind )
508 list_remove( &msg->entry );
509 switch(kind)
511 case SEND_MESSAGE:
512 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
513 break;
514 case POST_MESSAGE:
515 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
516 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
517 break;
519 free_message( msg );
522 /* message timed out without getting a reply */
523 static void result_timeout( void *private )
525 struct message_result *result = private;
527 assert( !result->replied );
529 result->timeout = NULL;
531 if (result->msg) /* not received yet */
533 struct message *msg = result->msg;
535 result->msg = NULL;
536 msg->result = NULL;
537 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
538 result->receiver = NULL;
539 if (!result->sender)
541 free_result( result );
542 return;
546 store_message_result( result, 0, STATUS_TIMEOUT );
549 /* allocate and fill a message result structure */
550 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
551 struct msg_queue *recv_queue,
552 struct message *msg, timeout_t timeout )
554 struct message_result *result = mem_alloc( sizeof(*result) );
555 if (result)
557 result->msg = msg;
558 result->sender = send_queue;
559 result->receiver = recv_queue;
560 result->replied = 0;
561 result->data = NULL;
562 result->data_size = 0;
563 result->timeout = NULL;
565 if (msg->type == MSG_CALLBACK)
567 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
569 if (!callback_msg)
571 free( result );
572 return NULL;
574 callback_msg->type = MSG_CALLBACK_RESULT;
575 callback_msg->win = msg->win;
576 callback_msg->msg = msg->msg;
577 callback_msg->wparam = 0;
578 callback_msg->lparam = 0;
579 callback_msg->time = get_tick_count();
580 callback_msg->result = NULL;
581 /* steal the data from the original message */
582 callback_msg->data = msg->data;
583 callback_msg->data_size = msg->data_size;
584 msg->data = NULL;
585 msg->data_size = 0;
587 result->callback_msg = callback_msg;
588 list_add_head( &send_queue->callback_result, &result->sender_entry );
590 else
592 result->callback_msg = NULL;
593 list_add_head( &send_queue->send_result, &result->sender_entry );
596 if (timeout != TIMEOUT_INFINITE)
597 result->timeout = add_timeout_user( timeout, result_timeout, result );
599 return result;
602 /* receive a message, removing it from the sent queue */
603 static void receive_message( struct msg_queue *queue, struct message *msg,
604 struct get_message_reply *reply )
606 struct message_result *result = msg->result;
608 reply->total = msg->data_size;
609 if (msg->data_size > get_reply_max_size())
611 set_error( STATUS_BUFFER_OVERFLOW );
612 return;
614 reply->type = msg->type;
615 reply->win = msg->win;
616 reply->msg = msg->msg;
617 reply->wparam = msg->wparam;
618 reply->lparam = msg->lparam;
619 reply->time = msg->time;
621 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
623 list_remove( &msg->entry );
624 /* put the result on the receiver result stack */
625 if (result)
627 result->msg = NULL;
628 result->recv_next = queue->recv_result;
629 queue->recv_result = result;
631 free( msg );
632 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
635 /* set the result of the current received message */
636 static void reply_message( struct msg_queue *queue, lparam_t result,
637 unsigned int error, int remove, const void *data, data_size_t len )
639 struct message_result *res = queue->recv_result;
641 if (remove)
643 queue->recv_result = res->recv_next;
644 res->receiver = NULL;
645 if (!res->sender) /* no one waiting for it */
647 free_result( res );
648 return;
651 if (!res->replied)
653 if (len && (res->data = memdup( data, len ))) res->data_size = len;
654 store_message_result( res, result, error );
658 static int match_window( user_handle_t win, user_handle_t msg_win )
660 if (!win) return 1;
661 if (win == -1 || win == 1) return !msg_win;
662 if (msg_win == win) return 1;
663 return is_child_window( win, msg_win );
666 /* retrieve a posted message */
667 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
668 unsigned int first, unsigned int last, unsigned int flags,
669 struct get_message_reply *reply )
671 struct message *msg;
673 /* check against the filters */
674 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
676 if (!match_window( win, msg->win )) continue;
677 if (!check_msg_filter( msg->msg, first, last )) continue;
678 goto found; /* found one */
680 return 0;
682 /* return it to the app */
683 found:
684 reply->total = msg->data_size;
685 if (msg->data_size > get_reply_max_size())
687 set_error( STATUS_BUFFER_OVERFLOW );
688 return 1;
690 reply->type = msg->type;
691 reply->win = msg->win;
692 reply->msg = msg->msg;
693 reply->wparam = msg->wparam;
694 reply->lparam = msg->lparam;
695 reply->time = msg->time;
697 if (flags & PM_REMOVE)
699 if (msg->data)
701 set_reply_data_ptr( msg->data, msg->data_size );
702 msg->data = NULL;
703 msg->data_size = 0;
705 remove_queue_message( queue, msg, POST_MESSAGE );
707 else if (msg->data) set_reply_data( msg->data, msg->data_size );
709 return 1;
712 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
713 struct get_message_reply *reply )
715 if (queue->quit_message)
717 reply->total = 0;
718 reply->type = MSG_POSTED;
719 reply->win = 0;
720 reply->msg = WM_QUIT;
721 reply->wparam = queue->exit_code;
722 reply->lparam = 0;
723 reply->time = get_tick_count();
725 if (flags & PM_REMOVE)
727 queue->quit_message = 0;
728 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
729 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
731 return 1;
733 else
734 return 0;
737 /* empty a message list and free all the messages */
738 static void empty_msg_list( struct list *list )
740 struct list *ptr;
742 while ((ptr = list_head( list )) != NULL)
744 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
745 list_remove( &msg->entry );
746 free_message( msg );
750 /* cleanup all pending results when deleting a queue */
751 static void cleanup_results( struct msg_queue *queue )
753 struct list *entry;
755 while ((entry = list_head( &queue->send_result )) != NULL)
757 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
760 while ((entry = list_head( &queue->callback_result )) != NULL)
762 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
765 while (queue->recv_result)
766 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
769 /* check if the thread owning the queue is hung (not checking for messages) */
770 static int is_queue_hung( struct msg_queue *queue )
772 struct wait_queue_entry *entry;
774 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
775 return 0; /* less than 5 seconds since last get message -> not hung */
777 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
779 if (entry->thread->queue == queue)
780 return 0; /* thread is waiting on queue -> not hung */
782 return 1;
785 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
787 struct msg_queue *queue = (struct msg_queue *)obj;
788 struct process *process = entry->thread->process;
790 /* a thread can only wait on its own queue */
791 if (entry->thread->queue != queue)
793 set_error( STATUS_ACCESS_DENIED );
794 return 0;
796 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
798 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
799 set_fd_events( queue->fd, POLLIN );
800 add_queue( obj, entry );
801 return 1;
804 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
806 struct msg_queue *queue = (struct msg_queue *)obj;
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 );
813 static void msg_queue_dump( struct object *obj, int verbose )
815 struct msg_queue *queue = (struct msg_queue *)obj;
816 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
817 queue->wake_bits, queue->wake_mask );
820 static int msg_queue_signaled( struct object *obj, struct thread *thread )
822 struct msg_queue *queue = (struct msg_queue *)obj;
823 int ret = 0;
825 if (queue->fd)
827 if ((ret = check_fd_events( queue->fd, POLLIN )))
828 /* stop waiting on select() if we are signaled */
829 set_fd_events( queue->fd, 0 );
830 else if (!list_empty( &obj->wait_queue ))
831 /* restart waiting on poll() if we are no longer signaled */
832 set_fd_events( queue->fd, POLLIN );
834 return ret || is_signaled( queue );
837 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
839 struct msg_queue *queue = (struct msg_queue *)obj;
840 queue->wake_mask = 0;
841 queue->changed_mask = 0;
842 return 0; /* Not abandoned */
845 static void msg_queue_destroy( struct object *obj )
847 struct msg_queue *queue = (struct msg_queue *)obj;
848 struct list *ptr;
849 int i;
851 cleanup_results( queue );
852 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
854 while ((ptr = list_head( &queue->pending_timers )))
856 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
857 list_remove( &timer->entry );
858 free( timer );
860 while ((ptr = list_head( &queue->expired_timers )))
862 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
863 list_remove( &timer->entry );
864 free( timer );
866 if (queue->timeout) remove_timeout_user( queue->timeout );
867 if (queue->input)
869 queue->input->cursor_count -= queue->cursor_count;
870 release_object( queue->input );
872 if (queue->hooks) release_object( queue->hooks );
873 if (queue->fd) release_object( queue->fd );
876 static void msg_queue_poll_event( struct fd *fd, int event )
878 struct msg_queue *queue = get_fd_user( fd );
879 assert( queue->obj.ops == &msg_queue_ops );
881 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
882 else set_fd_events( queue->fd, 0 );
883 wake_up( &queue->obj, 0 );
886 static void thread_input_dump( struct object *obj, int verbose )
888 struct thread_input *input = (struct thread_input *)obj;
889 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
890 input->focus, input->capture, input->active );
893 static void thread_input_destroy( struct object *obj )
895 struct thread_input *input = (struct thread_input *)obj;
897 if (foreground_input == input) foreground_input = NULL;
898 empty_msg_list( &input->msg_list );
899 if (input->desktop) release_object( input->desktop );
902 /* fix the thread input data when a window is destroyed */
903 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
905 struct thread_input *input = queue->input;
907 if (window == input->focus) input->focus = 0;
908 if (window == input->capture) input->capture = 0;
909 if (window == input->active) input->active = 0;
910 if (window == input->menu_owner) input->menu_owner = 0;
911 if (window == input->move_size) input->move_size = 0;
912 if (window == input->caret) set_caret_window( input, 0 );
915 /* check if the specified window can be set in the input data of a given queue */
916 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
918 struct thread *thread;
919 int ret = 0;
921 if (!window) return 1; /* we can always clear the data */
923 if ((thread = get_window_thread( window )))
925 ret = (queue->input == thread->queue->input);
926 if (!ret) set_error( STATUS_ACCESS_DENIED );
927 release_object( thread );
929 else set_error( STATUS_INVALID_HANDLE );
931 return ret;
934 /* make sure the specified thread has a queue */
935 int init_thread_queue( struct thread *thread )
937 if (thread->queue) return 1;
938 return (create_msg_queue( thread, NULL ) != NULL);
941 /* attach two thread input data structures */
942 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
944 struct desktop *desktop;
945 struct thread_input *input;
946 int ret;
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 ret = assign_thread_input( thread_from, input );
961 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
962 release_object( input );
963 return ret;
966 /* detach two thread input data structures */
967 void detach_thread_input( struct thread *thread_from )
969 struct thread_input *input;
971 if ((input = create_thread_input( thread_from )))
973 assign_thread_input( thread_from, input );
974 release_object( input );
979 /* set the next timer to expire */
980 static void set_next_timer( struct msg_queue *queue )
982 struct list *ptr;
984 if (queue->timeout)
986 remove_timeout_user( queue->timeout );
987 queue->timeout = NULL;
989 if ((ptr = list_head( &queue->pending_timers )))
991 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
992 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
994 /* set/clear QS_TIMER bit */
995 if (list_empty( &queue->expired_timers ))
996 clear_queue_bits( queue, QS_TIMER );
997 else
998 set_queue_bits( queue, QS_TIMER );
1001 /* find a timer from its window and id */
1002 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1003 unsigned int msg, lparam_t id )
1005 struct list *ptr;
1007 /* we need to search both lists */
1009 LIST_FOR_EACH( ptr, &queue->pending_timers )
1011 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1012 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1014 LIST_FOR_EACH( ptr, &queue->expired_timers )
1016 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1017 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1019 return NULL;
1022 /* callback for the next timer expiration */
1023 static void timer_callback( void *private )
1025 struct msg_queue *queue = private;
1026 struct list *ptr;
1028 queue->timeout = NULL;
1029 /* move on to the next timer */
1030 ptr = list_head( &queue->pending_timers );
1031 list_remove( ptr );
1032 list_add_tail( &queue->expired_timers, ptr );
1033 set_next_timer( queue );
1036 /* link a timer at its rightful place in the queue list */
1037 static void link_timer( struct msg_queue *queue, struct timer *timer )
1039 struct list *ptr;
1041 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1043 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1044 if (t->when >= timer->when) break;
1046 list_add_before( ptr, &timer->entry );
1049 /* remove a timer from the queue timer list and free it */
1050 static void free_timer( struct msg_queue *queue, struct timer *timer )
1052 list_remove( &timer->entry );
1053 free( timer );
1054 set_next_timer( queue );
1057 /* restart an expired timer */
1058 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1060 list_remove( &timer->entry );
1061 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1062 link_timer( queue, timer );
1063 set_next_timer( queue );
1066 /* find an expired timer matching the filtering parameters */
1067 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1068 unsigned int get_first, unsigned int get_last,
1069 int remove )
1071 struct list *ptr;
1073 LIST_FOR_EACH( ptr, &queue->expired_timers )
1075 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1076 if (win && timer->win != win) continue;
1077 if (check_msg_filter( timer->msg, get_first, get_last ))
1079 if (remove) restart_timer( queue, timer );
1080 return timer;
1083 return NULL;
1086 /* add a timer */
1087 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1089 struct timer *timer = mem_alloc( sizeof(*timer) );
1090 if (timer)
1092 timer->rate = max( rate, 1 );
1093 timer->when = current_time + (timeout_t)timer->rate * 10000;
1094 link_timer( queue, timer );
1095 /* check if we replaced the next timer */
1096 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1098 return timer;
1101 /* change the input key state for a given key */
1102 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1104 if (down)
1106 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1107 input->keystate[key] |= 0x80;
1109 else input->keystate[key] &= ~0x80;
1112 /* update the input key state for a keyboard message */
1113 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1115 unsigned char key;
1116 int down = 0;
1118 switch (msg->msg)
1120 case WM_LBUTTONDOWN:
1121 down = 1;
1122 /* fall through */
1123 case WM_LBUTTONUP:
1124 set_input_key_state( input, VK_LBUTTON, down );
1125 break;
1126 case WM_MBUTTONDOWN:
1127 down = 1;
1128 /* fall through */
1129 case WM_MBUTTONUP:
1130 set_input_key_state( input, VK_MBUTTON, down );
1131 break;
1132 case WM_RBUTTONDOWN:
1133 down = 1;
1134 /* fall through */
1135 case WM_RBUTTONUP:
1136 set_input_key_state( input, VK_RBUTTON, down );
1137 break;
1138 case WM_XBUTTONDOWN:
1139 down = 1;
1140 /* fall through */
1141 case WM_XBUTTONUP:
1142 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1143 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1144 break;
1145 case WM_KEYDOWN:
1146 case WM_SYSKEYDOWN:
1147 down = 1;
1148 /* fall through */
1149 case WM_KEYUP:
1150 case WM_SYSKEYUP:
1151 key = (unsigned char)msg->wparam;
1152 set_input_key_state( input, key, down );
1153 switch(key)
1155 case VK_LCONTROL:
1156 case VK_RCONTROL:
1157 down = (input->keystate[VK_LCONTROL] | input->keystate[VK_RCONTROL]) & 0x80;
1158 set_input_key_state( input, VK_CONTROL, down );
1159 break;
1160 case VK_LMENU:
1161 case VK_RMENU:
1162 down = (input->keystate[VK_LMENU] | input->keystate[VK_RMENU]) & 0x80;
1163 set_input_key_state( input, VK_MENU, down );
1164 break;
1165 case VK_LSHIFT:
1166 case VK_RSHIFT:
1167 down = (input->keystate[VK_LSHIFT] | input->keystate[VK_RSHIFT]) & 0x80;
1168 set_input_key_state( input, VK_SHIFT, down );
1169 break;
1171 break;
1175 /* release the hardware message currently being processed by the given thread */
1176 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1177 int remove, user_handle_t new_win )
1179 struct thread_input *input = queue->input;
1180 struct message *msg;
1182 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1184 if (msg->unique_id == hw_id) break;
1186 if (&msg->entry == &input->msg_list) return; /* not found */
1188 /* clear the queue bit for that message */
1189 if (remove || new_win)
1191 struct message *other;
1192 int clr_bit;
1194 clr_bit = get_hardware_msg_bit( msg );
1195 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1197 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1199 clr_bit = 0;
1200 break;
1203 if (clr_bit) clear_queue_bits( queue, clr_bit );
1206 if (new_win) /* set the new window */
1208 struct thread *owner = get_window_thread( new_win );
1209 if (owner)
1211 msg->win = new_win;
1212 if (owner->queue->input != input)
1214 list_remove( &msg->entry );
1215 if (msg->msg == WM_MOUSEMOVE && merge_message( owner->queue->input, msg ))
1217 free_message( msg );
1218 release_object( owner );
1219 return;
1221 list_add_tail( &owner->queue->input->msg_list, &msg->entry );
1223 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1224 remove = 0;
1225 release_object( owner );
1228 if (remove)
1230 update_input_key_state( input, msg );
1231 list_remove( &msg->entry );
1232 free_message( msg );
1236 /* find the window that should receive a given hardware message */
1237 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1238 struct hardware_msg_data *data, unsigned int *msg_code )
1240 user_handle_t win = 0;
1242 *msg_code = msg->msg;
1243 if (is_keyboard_msg( msg ))
1245 if (input && !(win = input->focus))
1247 win = input->active;
1248 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1251 else /* mouse message */
1253 if (!input || !(win = input->capture))
1255 if (!(win = msg->win) || !is_window_visible( win ))
1257 if (input) win = window_from_point( input->desktop, data->x, data->y );
1261 return win;
1264 /* queue a hardware message into a given thread input */
1265 static void queue_hardware_message( struct msg_queue *queue, struct message *msg,
1266 struct hardware_msg_data *data )
1268 user_handle_t win;
1269 struct thread *thread;
1270 struct thread_input *input = queue ? queue->input : foreground_input;
1271 unsigned int msg_code;
1273 last_input_time = get_tick_count();
1274 win = find_hardware_message_window( input, msg, data, &msg_code );
1275 if (!win || !(thread = get_window_thread(win)))
1277 if (input) update_input_key_state( input, msg );
1278 free( msg );
1279 return;
1281 input = thread->queue->input;
1283 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1284 else
1286 msg->unique_id = 0; /* will be set once we return it to the app */
1287 list_add_tail( &input->msg_list, &msg->entry );
1288 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1290 release_object( thread );
1293 /* check message filter for a hardware message */
1294 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1295 user_handle_t filter_win, unsigned int first, unsigned int last )
1297 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1299 /* we can only test the window for a keyboard message since the
1300 * dest window for a mouse message depends on hittest */
1301 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1302 return 0;
1303 /* the message code is final for a keyboard message, we can simply check it */
1304 return check_msg_filter( msg_code, first, last );
1306 else /* mouse message */
1308 /* we need to check all possible values that the message can have in the end */
1310 if (check_msg_filter( msg_code, first, last )) return 1;
1311 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1313 /* all other messages can become non-client messages */
1314 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1316 /* clicks can become double-clicks or non-client double-clicks */
1317 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1318 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1320 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1321 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1323 return 0;
1328 /* find a hardware message for the given queue */
1329 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1330 unsigned int first, unsigned int last, struct get_message_reply *reply )
1332 struct thread_input *input = thread->queue->input;
1333 struct thread *win_thread;
1334 struct list *ptr;
1335 user_handle_t win;
1336 int clear_bits, got_one = 0;
1337 unsigned int msg_code;
1339 ptr = list_head( &input->msg_list );
1340 if (hw_id)
1342 while (ptr)
1344 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1345 if (msg->unique_id == hw_id) break;
1346 ptr = list_next( &input->msg_list, ptr );
1348 if (!ptr) ptr = list_head( &input->msg_list );
1349 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1352 if (ptr == list_head( &input->msg_list ))
1353 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1354 else
1355 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1357 while (ptr)
1359 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1360 struct hardware_msg_data *data = msg->data;
1362 ptr = list_next( &input->msg_list, ptr );
1363 win = find_hardware_message_window( input, msg, data, &msg_code );
1364 if (!win || !(win_thread = get_window_thread( win )))
1366 /* no window at all, remove it */
1367 update_input_key_state( input, msg );
1368 list_remove( &msg->entry );
1369 free_message( msg );
1370 continue;
1372 if (win_thread != thread)
1374 if (win_thread->queue->input == input)
1376 /* wake the other thread */
1377 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1378 got_one = 1;
1380 else
1382 /* for another thread input, drop it */
1383 update_input_key_state( input, msg );
1384 list_remove( &msg->entry );
1385 free_message( msg );
1387 release_object( win_thread );
1388 continue;
1390 release_object( win_thread );
1392 /* if we already got a message for another thread, or if it doesn't
1393 * match the filter we skip it */
1394 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1396 clear_bits &= ~get_hardware_msg_bit( msg );
1397 continue;
1399 /* now we can return it */
1400 if (!msg->unique_id) msg->unique_id = get_unique_id();
1401 reply->type = MSG_HARDWARE;
1402 reply->win = win;
1403 reply->msg = msg_code;
1404 reply->wparam = msg->wparam;
1405 reply->lparam = msg->lparam;
1406 reply->time = msg->time;
1408 data->hw_id = msg->unique_id;
1409 set_reply_data( msg->data, msg->data_size );
1410 return 1;
1412 /* nothing found, clear the hardware queue bits */
1413 clear_queue_bits( thread->queue, clear_bits );
1414 return 0;
1417 /* increment (or decrement if 'incr' is negative) the queue paint count */
1418 void inc_queue_paint_count( struct thread *thread, int incr )
1420 struct msg_queue *queue = thread->queue;
1422 assert( queue );
1424 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1426 if (queue->paint_count)
1427 set_queue_bits( queue, QS_PAINT );
1428 else
1429 clear_queue_bits( queue, QS_PAINT );
1433 /* remove all messages and timers belonging to a certain window */
1434 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1436 struct msg_queue *queue = thread->queue;
1437 struct list *ptr;
1438 int i;
1440 if (!queue) return;
1442 /* remove timers */
1444 ptr = list_head( &queue->pending_timers );
1445 while (ptr)
1447 struct list *next = list_next( &queue->pending_timers, ptr );
1448 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1449 if (timer->win == win) free_timer( queue, timer );
1450 ptr = next;
1452 ptr = list_head( &queue->expired_timers );
1453 while (ptr)
1455 struct list *next = list_next( &queue->expired_timers, ptr );
1456 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1457 if (timer->win == win) free_timer( queue, timer );
1458 ptr = next;
1461 /* remove messages */
1462 for (i = 0; i < NB_MSG_KINDS; i++)
1464 struct list *ptr, *next;
1466 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1468 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1469 if (msg->win == win) remove_queue_message( queue, msg, i );
1473 thread_input_cleanup_window( queue, win );
1476 /* post a message to a window; used by socket handling */
1477 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
1479 struct message *msg;
1480 struct thread *thread = get_window_thread( win );
1482 if (!thread) return;
1484 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1486 msg->type = MSG_POSTED;
1487 msg->win = get_user_full_handle( win );
1488 msg->msg = message;
1489 msg->wparam = wparam;
1490 msg->lparam = lparam;
1491 msg->time = get_tick_count();
1492 msg->result = NULL;
1493 msg->data = NULL;
1494 msg->data_size = 0;
1496 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1497 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1499 release_object( thread );
1502 /* post a win event */
1503 void post_win_event( struct thread *thread, unsigned int event,
1504 user_handle_t win, unsigned int object_id,
1505 unsigned int child_id, client_ptr_t hook_proc,
1506 const WCHAR *module, data_size_t module_size,
1507 user_handle_t hook)
1509 struct message *msg;
1511 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1513 struct winevent_msg_data *data;
1515 msg->type = MSG_WINEVENT;
1516 msg->win = get_user_full_handle( win );
1517 msg->msg = event;
1518 msg->wparam = object_id;
1519 msg->lparam = child_id;
1520 msg->time = get_tick_count();
1521 msg->result = NULL;
1523 if ((data = malloc( sizeof(*data) + module_size )))
1525 data->hook = hook;
1526 data->tid = get_thread_id( current );
1527 data->hook_proc = hook_proc;
1528 memcpy( data + 1, module, module_size );
1530 msg->data = data;
1531 msg->data_size = sizeof(*data) + module_size;
1533 if (debug_level > 1)
1534 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
1535 get_thread_id(thread), event, win, object_id, child_id );
1536 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1537 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1539 else
1540 free( msg );
1545 /* check if the thread owning the window is hung */
1546 DECL_HANDLER(is_window_hung)
1548 struct thread *thread;
1550 thread = get_window_thread( req->win );
1551 if (thread)
1553 reply->is_hung = is_queue_hung( thread->queue );
1554 release_object( thread );
1556 else reply->is_hung = 0;
1560 /* get the message queue of the current thread */
1561 DECL_HANDLER(get_msg_queue)
1563 struct msg_queue *queue = get_current_queue();
1565 reply->handle = 0;
1566 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1570 /* set the file descriptor associated to the current thread queue */
1571 DECL_HANDLER(set_queue_fd)
1573 struct msg_queue *queue = get_current_queue();
1574 struct file *file;
1575 int unix_fd;
1577 if (queue->fd) /* fd can only be set once */
1579 set_error( STATUS_ACCESS_DENIED );
1580 return;
1582 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
1584 if ((unix_fd = get_file_unix_fd( file )) != -1)
1586 if ((unix_fd = dup( unix_fd )) != -1)
1587 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
1588 else
1589 file_set_error();
1591 release_object( file );
1595 /* set the current message queue wakeup mask */
1596 DECL_HANDLER(set_queue_mask)
1598 struct msg_queue *queue = get_current_queue();
1600 if (queue)
1602 queue->wake_mask = req->wake_mask;
1603 queue->changed_mask = req->changed_mask;
1604 reply->wake_bits = queue->wake_bits;
1605 reply->changed_bits = queue->changed_bits;
1606 if (is_signaled( queue ))
1608 /* if skip wait is set, do what would have been done in the subsequent wait */
1609 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1610 else wake_up( &queue->obj, 0 );
1616 /* get the current message queue status */
1617 DECL_HANDLER(get_queue_status)
1619 struct msg_queue *queue = current->queue;
1620 if (queue)
1622 reply->wake_bits = queue->wake_bits;
1623 reply->changed_bits = queue->changed_bits;
1624 if (req->clear) queue->changed_bits = 0;
1626 else reply->wake_bits = reply->changed_bits = 0;
1630 /* send a message to a thread queue */
1631 DECL_HANDLER(send_message)
1633 struct message *msg;
1634 struct msg_queue *send_queue = get_current_queue();
1635 struct msg_queue *recv_queue = NULL;
1636 struct thread *thread = NULL;
1638 if (!(thread = get_thread_from_id( req->id ))) return;
1640 if (!(recv_queue = thread->queue))
1642 set_error( STATUS_INVALID_PARAMETER );
1643 release_object( thread );
1644 return;
1646 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1648 set_error( STATUS_TIMEOUT );
1649 release_object( thread );
1650 return;
1653 if ((msg = mem_alloc( sizeof(*msg) )))
1655 msg->type = req->type;
1656 msg->win = get_user_full_handle( req->win );
1657 msg->msg = req->msg;
1658 msg->wparam = req->wparam;
1659 msg->lparam = req->lparam;
1660 msg->time = get_tick_count();
1661 msg->result = NULL;
1662 msg->data = NULL;
1663 msg->data_size = get_req_data_size();
1665 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1667 free( msg );
1668 release_object( thread );
1669 return;
1672 switch(msg->type)
1674 case MSG_OTHER_PROCESS:
1675 case MSG_ASCII:
1676 case MSG_UNICODE:
1677 case MSG_CALLBACK:
1678 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
1680 free_message( msg );
1681 break;
1683 /* fall through */
1684 case MSG_NOTIFY:
1685 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1686 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1687 break;
1688 case MSG_POSTED:
1689 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1690 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1691 break;
1692 case MSG_HARDWARE: /* should use send_hardware_message instead */
1693 case MSG_CALLBACK_RESULT: /* cannot send this one */
1694 default:
1695 set_error( STATUS_INVALID_PARAMETER );
1696 free( msg );
1697 break;
1700 release_object( thread );
1703 /* send a hardware message to a thread queue */
1704 DECL_HANDLER(send_hardware_message)
1706 struct message *msg;
1707 struct msg_queue *recv_queue = NULL;
1708 struct thread *thread = NULL;
1709 struct hardware_msg_data *data;
1711 if (req->id)
1713 if (!(thread = get_thread_from_id( req->id ))) return;
1716 if (thread && !(recv_queue = thread->queue))
1718 set_error( STATUS_INVALID_PARAMETER );
1719 release_object( thread );
1720 return;
1723 if (!(data = mem_alloc( sizeof(*data) )))
1725 if (thread) release_object( thread );
1726 return;
1728 memset( data, 0, sizeof(*data) );
1729 data->x = req->x;
1730 data->y = req->y;
1731 data->info = req->info;
1733 if ((msg = mem_alloc( sizeof(*msg) )))
1735 msg->type = MSG_HARDWARE;
1736 msg->win = get_user_full_handle( req->win );
1737 msg->msg = req->msg;
1738 msg->wparam = req->wparam;
1739 msg->lparam = req->lparam;
1740 msg->time = req->time;
1741 msg->result = NULL;
1742 msg->data = data;
1743 msg->data_size = sizeof(*data);
1744 queue_hardware_message( recv_queue, msg, data );
1746 else free( data );
1748 if (thread) release_object( thread );
1751 /* post a quit message to the current queue */
1752 DECL_HANDLER(post_quit_message)
1754 struct msg_queue *queue = get_current_queue();
1756 if (!queue)
1757 return;
1759 queue->quit_message = 1;
1760 queue->exit_code = req->exit_code;
1761 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1764 /* get a message from the current queue */
1765 DECL_HANDLER(get_message)
1767 struct timer *timer;
1768 struct list *ptr;
1769 struct msg_queue *queue = get_current_queue();
1770 user_handle_t get_win = get_user_full_handle( req->get_win );
1771 unsigned int filter = req->flags >> 16;
1773 reply->active_hooks = get_active_hooks();
1775 if (!queue) return;
1776 queue->last_get_msg = current_time;
1777 if (!filter) filter = QS_ALLINPUT;
1779 /* first check for sent messages */
1780 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1782 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1783 receive_message( queue, msg, reply );
1784 return;
1787 /* clear changed bits so we can wait on them if we don't find a message */
1788 if (filter & QS_POSTMESSAGE)
1790 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
1791 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
1793 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
1794 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
1796 /* then check for posted messages */
1797 if ((filter & QS_POSTMESSAGE) &&
1798 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1799 return;
1801 /* only check for quit messages if not posted messages pending.
1802 * note: the quit message isn't filtered */
1803 if (get_quit_message( queue, req->flags, reply ))
1804 return;
1806 /* then check for any raw hardware message */
1807 if ((filter & QS_INPUT) &&
1808 filter_contains_hw_range( req->get_first, req->get_last ) &&
1809 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1810 return;
1812 /* now check for WM_PAINT */
1813 if ((filter & QS_PAINT) &&
1814 queue->paint_count &&
1815 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1816 (reply->win = find_window_to_repaint( get_win, current )))
1818 reply->type = MSG_POSTED;
1819 reply->msg = WM_PAINT;
1820 reply->wparam = 0;
1821 reply->lparam = 0;
1822 reply->time = get_tick_count();
1823 return;
1826 /* now check for timer */
1827 if ((filter & QS_TIMER) &&
1828 (timer = find_expired_timer( queue, get_win, req->get_first,
1829 req->get_last, (req->flags & PM_REMOVE) )))
1831 reply->type = MSG_POSTED;
1832 reply->win = timer->win;
1833 reply->msg = timer->msg;
1834 reply->wparam = timer->id;
1835 reply->lparam = timer->lparam;
1836 reply->time = get_tick_count();
1837 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
1838 set_event( current->process->idle_event );
1839 return;
1842 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
1843 queue->wake_mask = req->wake_mask;
1844 queue->changed_mask = req->changed_mask;
1845 set_error( STATUS_PENDING ); /* FIXME */
1849 /* reply to a sent message */
1850 DECL_HANDLER(reply_message)
1852 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1853 else if (current->queue->recv_result)
1854 reply_message( current->queue, req->result, 0, req->remove,
1855 get_req_data(), get_req_data_size() );
1859 /* accept the current hardware message */
1860 DECL_HANDLER(accept_hardware_message)
1862 if (current->queue)
1863 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1864 else
1865 set_error( STATUS_ACCESS_DENIED );
1869 /* retrieve the reply for the last message sent */
1870 DECL_HANDLER(get_message_reply)
1872 struct message_result *result;
1873 struct list *entry;
1874 struct msg_queue *queue = current->queue;
1876 if (queue)
1878 set_error( STATUS_PENDING );
1879 reply->result = 0;
1881 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1883 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1884 if (result->replied || req->cancel)
1886 if (result->replied)
1888 reply->result = result->result;
1889 set_error( result->error );
1890 if (result->data)
1892 data_size_t data_len = min( result->data_size, get_reply_max_size() );
1893 set_reply_data_ptr( result->data, data_len );
1894 result->data = NULL;
1895 result->data_size = 0;
1898 remove_result_from_sender( result );
1900 entry = list_head( &queue->send_result );
1901 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1902 else
1904 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1905 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1909 else set_error( STATUS_ACCESS_DENIED );
1913 /* set a window timer */
1914 DECL_HANDLER(set_win_timer)
1916 struct timer *timer;
1917 struct msg_queue *queue;
1918 struct thread *thread = NULL;
1919 user_handle_t win = 0;
1920 lparam_t id = req->id;
1922 if (req->win)
1924 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1926 set_error( STATUS_INVALID_HANDLE );
1927 return;
1929 if (thread->process != current->process)
1931 release_object( thread );
1932 set_error( STATUS_ACCESS_DENIED );
1933 return;
1935 queue = thread->queue;
1936 /* remove it if it existed already */
1937 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1939 else
1941 queue = get_current_queue();
1942 /* look for a timer with this id */
1943 if (id && (timer = find_timer( queue, 0, req->msg, id )))
1945 /* free and reuse id */
1946 free_timer( queue, timer );
1948 else
1950 /* find a free id for it */
1953 id = queue->next_timer_id;
1954 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
1956 while (find_timer( queue, 0, req->msg, id ));
1960 if ((timer = set_timer( queue, req->rate )))
1962 timer->win = win;
1963 timer->msg = req->msg;
1964 timer->id = id;
1965 timer->lparam = req->lparam;
1966 reply->id = id;
1968 if (thread) release_object( thread );
1971 /* kill a window timer */
1972 DECL_HANDLER(kill_win_timer)
1974 struct timer *timer;
1975 struct thread *thread;
1976 user_handle_t win = 0;
1978 if (req->win)
1980 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1982 set_error( STATUS_INVALID_HANDLE );
1983 return;
1985 if (thread->process != current->process)
1987 release_object( thread );
1988 set_error( STATUS_ACCESS_DENIED );
1989 return;
1992 else thread = (struct thread *)grab_object( current );
1994 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1995 free_timer( thread->queue, timer );
1996 else
1997 set_error( STATUS_INVALID_PARAMETER );
1999 release_object( thread );
2003 /* attach (or detach) thread inputs */
2004 DECL_HANDLER(attach_thread_input)
2006 struct thread *thread_from = get_thread_from_id( req->tid_from );
2007 struct thread *thread_to = get_thread_from_id( req->tid_to );
2009 if (!thread_from || !thread_to)
2011 if (thread_from) release_object( thread_from );
2012 if (thread_to) release_object( thread_to );
2013 return;
2015 if (thread_from != thread_to)
2017 if (req->attach) attach_thread_input( thread_from, thread_to );
2018 else
2020 if (thread_from->queue && thread_to->queue &&
2021 thread_from->queue->input == thread_to->queue->input)
2022 detach_thread_input( thread_from );
2023 else
2024 set_error( STATUS_ACCESS_DENIED );
2027 else set_error( STATUS_ACCESS_DENIED );
2028 release_object( thread_from );
2029 release_object( thread_to );
2033 /* get thread input data */
2034 DECL_HANDLER(get_thread_input)
2036 struct thread *thread = NULL;
2037 struct thread_input *input;
2039 if (req->tid)
2041 if (!(thread = get_thread_from_id( req->tid ))) return;
2042 input = thread->queue ? thread->queue->input : NULL;
2044 else input = foreground_input; /* get the foreground thread info */
2046 if (input)
2048 reply->focus = input->focus;
2049 reply->capture = input->capture;
2050 reply->active = input->active;
2051 reply->menu_owner = input->menu_owner;
2052 reply->move_size = input->move_size;
2053 reply->caret = input->caret;
2054 reply->rect = input->caret_rect;
2056 else
2058 reply->focus = 0;
2059 reply->capture = 0;
2060 reply->active = 0;
2061 reply->menu_owner = 0;
2062 reply->move_size = 0;
2063 reply->caret = 0;
2064 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
2066 /* foreground window is active window of foreground thread */
2067 reply->foreground = foreground_input ? foreground_input->active : 0;
2068 if (thread) release_object( thread );
2072 /* retrieve queue keyboard state for a given thread */
2073 DECL_HANDLER(get_key_state)
2075 struct thread *thread;
2076 struct thread_input *input;
2078 if (!(thread = get_thread_from_id( req->tid ))) return;
2079 input = thread->queue ? thread->queue->input : NULL;
2080 if (input)
2082 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
2083 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
2085 release_object( thread );
2089 /* set queue keyboard state for a given thread */
2090 DECL_HANDLER(set_key_state)
2092 struct thread *thread = NULL;
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 data_size_t size = min( sizeof(input->keystate), get_req_data_size() );
2100 if (size) memcpy( input->keystate, get_req_data(), size );
2102 release_object( thread );
2106 /* set the system foreground window */
2107 DECL_HANDLER(set_foreground_window)
2109 struct thread *thread;
2110 struct msg_queue *queue = get_current_queue();
2112 reply->previous = foreground_input ? foreground_input->active : 0;
2113 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
2114 reply->send_msg_new = FALSE;
2116 if (is_top_level_window( req->handle ) &&
2117 ((thread = get_window_thread( req->handle ))))
2119 foreground_input = thread->queue->input;
2120 reply->send_msg_new = (foreground_input != queue->input);
2121 release_object( thread );
2123 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2127 /* set the current thread focus window */
2128 DECL_HANDLER(set_focus_window)
2130 struct msg_queue *queue = get_current_queue();
2132 reply->previous = 0;
2133 if (queue && check_queue_input_window( queue, req->handle ))
2135 reply->previous = queue->input->focus;
2136 queue->input->focus = get_user_full_handle( req->handle );
2141 /* set the current thread active window */
2142 DECL_HANDLER(set_active_window)
2144 struct msg_queue *queue = get_current_queue();
2146 reply->previous = 0;
2147 if (queue && check_queue_input_window( queue, req->handle ))
2149 if (!req->handle || make_window_active( req->handle ))
2151 reply->previous = queue->input->active;
2152 queue->input->active = get_user_full_handle( req->handle );
2154 else set_error( STATUS_INVALID_HANDLE );
2159 /* set the current thread capture window */
2160 DECL_HANDLER(set_capture_window)
2162 struct msg_queue *queue = get_current_queue();
2164 reply->previous = reply->full_handle = 0;
2165 if (queue && check_queue_input_window( queue, req->handle ))
2167 struct thread_input *input = queue->input;
2169 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2170 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2172 set_error(STATUS_ACCESS_DENIED);
2173 return;
2175 reply->previous = input->capture;
2176 input->capture = get_user_full_handle( req->handle );
2177 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2178 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2179 reply->full_handle = input->capture;
2184 /* Set the current thread caret window */
2185 DECL_HANDLER(set_caret_window)
2187 struct msg_queue *queue = get_current_queue();
2189 reply->previous = 0;
2190 if (queue && check_queue_input_window( queue, req->handle ))
2192 struct thread_input *input = queue->input;
2194 reply->previous = input->caret;
2195 reply->old_rect = input->caret_rect;
2196 reply->old_hide = input->caret_hide;
2197 reply->old_state = input->caret_state;
2199 set_caret_window( input, get_user_full_handle(req->handle) );
2200 input->caret_rect.right = input->caret_rect.left + req->width;
2201 input->caret_rect.bottom = input->caret_rect.top + req->height;
2206 /* Set the current thread caret information */
2207 DECL_HANDLER(set_caret_info)
2209 struct msg_queue *queue = get_current_queue();
2210 struct thread_input *input;
2212 if (!queue) return;
2213 input = queue->input;
2214 reply->full_handle = input->caret;
2215 reply->old_rect = input->caret_rect;
2216 reply->old_hide = input->caret_hide;
2217 reply->old_state = input->caret_state;
2219 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2221 set_error( STATUS_ACCESS_DENIED );
2222 return;
2224 if (req->flags & SET_CARET_POS)
2226 input->caret_rect.right += req->x - input->caret_rect.left;
2227 input->caret_rect.bottom += req->y - input->caret_rect.top;
2228 input->caret_rect.left = req->x;
2229 input->caret_rect.top = req->y;
2231 if (req->flags & SET_CARET_HIDE)
2233 input->caret_hide += req->hide;
2234 if (input->caret_hide < 0) input->caret_hide = 0;
2236 if (req->flags & SET_CARET_STATE)
2238 if (req->state == -1) input->caret_state = !input->caret_state;
2239 else input->caret_state = !!req->state;
2244 /* get the time of the last input event */
2245 DECL_HANDLER(get_last_input_time)
2247 reply->time = last_input_time;
2250 /* set/get the current cursor */
2251 DECL_HANDLER(set_cursor)
2253 struct msg_queue *queue = get_current_queue();
2254 struct thread_input *input;
2256 if (!queue) return;
2257 input = queue->input;
2259 reply->prev_handle = input->cursor;
2260 reply->prev_count = input->cursor_count;
2262 if (req->flags & SET_CURSOR_HANDLE)
2264 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
2266 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
2267 return;
2269 input->cursor = req->handle;
2272 if (req->flags & SET_CURSOR_COUNT)
2274 queue->cursor_count += req->show_count;
2275 input->cursor_count += req->show_count;