hal: Add appropriate stdcall decorations to assembly functions.
[wine.git] / server / queue.c
blobc5841c939be87fb165bff0d078edbbe566a412ef
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 struct list msg_list; /* list of hardware messages */
107 unsigned char keystate[256]; /* state of each key */
110 struct msg_queue
112 struct object obj; /* object header */
113 struct fd *fd; /* optional file descriptor to poll */
114 unsigned int wake_bits; /* wakeup bits */
115 unsigned int wake_mask; /* wakeup mask */
116 unsigned int changed_bits; /* changed wakeup bits */
117 unsigned int changed_mask; /* changed wakeup mask */
118 int paint_count; /* pending paint messages count */
119 int quit_message; /* is there a pending quit message? */
120 int exit_code; /* exit code of pending quit message */
121 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
122 struct list send_result; /* stack of sent messages waiting for result */
123 struct list callback_result; /* list of callback messages waiting for result */
124 struct message_result *recv_result; /* stack of received messages waiting for result */
125 struct list pending_timers; /* list of pending timers */
126 struct list expired_timers; /* list of expired timers */
127 lparam_t next_timer_id; /* id for the next timer with a 0 window */
128 struct timeout_user *timeout; /* timeout for next timer to expire */
129 struct thread_input *input; /* thread input descriptor */
130 struct hook_table *hooks; /* hook table */
131 timeout_t last_get_msg; /* time of last get message call */
134 static void msg_queue_dump( struct object *obj, int verbose );
135 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
136 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
137 static int msg_queue_signaled( struct object *obj, struct thread *thread );
138 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
139 static void msg_queue_destroy( struct object *obj );
140 static void msg_queue_poll_event( struct fd *fd, int event );
141 static void thread_input_dump( struct object *obj, int verbose );
142 static void thread_input_destroy( struct object *obj );
143 static void timer_callback( void *private );
145 static const struct object_ops msg_queue_ops =
147 sizeof(struct msg_queue), /* size */
148 msg_queue_dump, /* dump */
149 no_get_type, /* get_type */
150 msg_queue_add_queue, /* add_queue */
151 msg_queue_remove_queue, /* remove_queue */
152 msg_queue_signaled, /* signaled */
153 msg_queue_satisfied, /* satisfied */
154 no_signal, /* signal */
155 no_get_fd, /* get_fd */
156 no_map_access, /* map_access */
157 default_get_sd, /* get_sd */
158 default_set_sd, /* set_sd */
159 no_lookup_name, /* lookup_name */
160 no_open_file, /* open_file */
161 no_close_handle, /* close_handle */
162 msg_queue_destroy /* destroy */
165 static const struct fd_ops msg_queue_fd_ops =
167 NULL, /* get_poll_events */
168 msg_queue_poll_event, /* poll_event */
169 NULL, /* flush */
170 NULL, /* get_fd_type */
171 NULL, /* ioctl */
172 NULL, /* queue_async */
173 NULL, /* reselect_async */
174 NULL /* cancel async */
178 static const struct object_ops thread_input_ops =
180 sizeof(struct thread_input), /* size */
181 thread_input_dump, /* dump */
182 no_get_type, /* get_type */
183 no_add_queue, /* add_queue */
184 NULL, /* remove_queue */
185 NULL, /* signaled */
186 NULL, /* satisfied */
187 no_signal, /* signal */
188 no_get_fd, /* get_fd */
189 no_map_access, /* map_access */
190 default_get_sd, /* get_sd */
191 default_set_sd, /* set_sd */
192 no_lookup_name, /* lookup_name */
193 no_open_file, /* open_file */
194 no_close_handle, /* close_handle */
195 thread_input_destroy /* destroy */
198 /* pointer to input structure of foreground thread */
199 static struct thread_input *foreground_input;
200 static unsigned int last_input_time;
202 static void free_message( struct message *msg );
204 /* set the caret window in a given thread input */
205 static void set_caret_window( struct thread_input *input, user_handle_t win )
207 if (!win || win != input->caret)
209 input->caret_rect.left = 0;
210 input->caret_rect.top = 0;
211 input->caret_rect.right = 0;
212 input->caret_rect.bottom = 0;
214 input->caret = win;
215 input->caret_hide = 1;
216 input->caret_state = 0;
219 /* create a thread input object */
220 static struct thread_input *create_thread_input( struct thread *thread )
222 struct thread_input *input;
224 if ((input = alloc_object( &thread_input_ops )))
226 input->focus = 0;
227 input->capture = 0;
228 input->active = 0;
229 input->menu_owner = 0;
230 input->move_size = 0;
231 list_init( &input->msg_list );
232 set_caret_window( input, 0 );
233 memset( input->keystate, 0, sizeof(input->keystate) );
235 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
237 release_object( input );
238 return NULL;
241 return input;
244 /* release the thread input data of a given thread */
245 static inline void release_thread_input( struct thread *thread )
247 struct thread_input *input = thread->queue->input;
249 if (!input) return;
250 release_object( input );
251 thread->queue->input = NULL;
254 /* create a message queue object */
255 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
257 struct msg_queue *queue;
258 int i;
260 if (!input && !(input = create_thread_input( thread ))) return NULL;
261 if ((queue = alloc_object( &msg_queue_ops )))
263 queue->fd = NULL;
264 queue->wake_bits = 0;
265 queue->wake_mask = 0;
266 queue->changed_bits = 0;
267 queue->changed_mask = 0;
268 queue->paint_count = 0;
269 queue->quit_message = 0;
270 queue->recv_result = NULL;
271 queue->next_timer_id = 0x7fff;
272 queue->timeout = NULL;
273 queue->input = (struct thread_input *)grab_object( input );
274 queue->hooks = NULL;
275 queue->last_get_msg = current_time;
276 list_init( &queue->send_result );
277 list_init( &queue->callback_result );
278 list_init( &queue->pending_timers );
279 list_init( &queue->expired_timers );
280 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
282 thread->queue = queue;
283 if (!thread->process->queue)
284 thread->process->queue = (struct msg_queue *)grab_object( queue );
286 release_object( input );
287 return queue;
290 /* free the message queue of a thread at thread exit */
291 void free_msg_queue( struct thread *thread )
293 struct process *process = thread->process;
295 remove_thread_hooks( thread );
296 if (!thread->queue) return;
297 if (process->queue == thread->queue) /* is it the process main queue? */
299 release_object( process->queue );
300 process->queue = NULL;
301 if (process->idle_event)
303 set_event( process->idle_event );
304 release_object( process->idle_event );
305 process->idle_event = NULL;
308 release_object( thread->queue );
309 thread->queue = NULL;
312 /* get the hook table for a given thread */
313 struct hook_table *get_queue_hooks( struct thread *thread )
315 if (!thread->queue) return NULL;
316 return thread->queue->hooks;
319 /* set the hook table for a given thread, allocating the queue if needed */
320 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
322 struct msg_queue *queue = thread->queue;
323 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
324 if (queue->hooks) release_object( queue->hooks );
325 queue->hooks = hooks;
328 /* check the queue status */
329 static inline int is_signaled( struct msg_queue *queue )
331 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
334 /* set some queue bits */
335 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
337 queue->wake_bits |= bits;
338 queue->changed_bits |= bits;
339 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
342 /* clear some queue bits */
343 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
345 queue->wake_bits &= ~bits;
346 queue->changed_bits &= ~bits;
349 /* check whether msg is a keyboard message */
350 static inline int is_keyboard_msg( struct message *msg )
352 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
355 /* check if message is matched by the filter */
356 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
358 return (msg >= first && msg <= last);
361 /* check whether a message filter contains at least one potential hardware message */
362 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
364 /* hardware message ranges are (in numerical order):
365 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
366 * WM_KEYFIRST .. WM_KEYLAST
367 * WM_MOUSEFIRST .. WM_MOUSELAST
369 if (last < WM_NCMOUSEFIRST) return 0;
370 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
371 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
372 if (first > WM_MOUSELAST) return 0;
373 return 1;
376 /* get the QS_* bit corresponding to a given hardware message */
377 static inline int get_hardware_msg_bit( struct message *msg )
379 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
380 if (is_keyboard_msg( msg )) return QS_KEY;
381 return QS_MOUSEBUTTON;
384 /* get the current thread queue, creating it if needed */
385 static inline struct msg_queue *get_current_queue(void)
387 struct msg_queue *queue = current->queue;
388 if (!queue) queue = create_msg_queue( current, NULL );
389 return queue;
392 /* get a (pseudo-)unique id to tag hardware messages */
393 static inline unsigned int get_unique_id(void)
395 static unsigned int id;
396 if (!++id) id = 1; /* avoid an id of 0 */
397 return id;
400 /* try to merge a message with the last in the list; return 1 if successful */
401 static int merge_message( struct thread_input *input, const struct message *msg )
403 struct message *prev;
404 struct list *ptr = list_tail( &input->msg_list );
406 if (!ptr) return 0;
407 prev = LIST_ENTRY( ptr, struct message, entry );
408 if (prev->result) return 0;
409 if (prev->win && msg->win && prev->win != msg->win) return 0;
410 if (prev->msg != msg->msg) return 0;
411 if (prev->type != msg->type) return 0;
412 /* now we can merge it */
413 prev->wparam = msg->wparam;
414 prev->lparam = msg->lparam;
415 prev->time = msg->time;
416 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
418 struct hardware_msg_data *prev_data = prev->data;
419 struct hardware_msg_data *msg_data = msg->data;
420 prev_data->x = msg_data->x;
421 prev_data->y = msg_data->y;
422 prev_data->info = msg_data->info;
424 return 1;
427 /* free a result structure */
428 static void free_result( struct message_result *result )
430 if (result->timeout) remove_timeout_user( result->timeout );
431 free( result->data );
432 if (result->callback_msg) free_message( result->callback_msg );
433 free( result );
436 /* remove the result from the sender list it is on */
437 static inline void remove_result_from_sender( struct message_result *result )
439 assert( result->sender );
441 list_remove( &result->sender_entry );
442 result->sender = NULL;
443 if (!result->receiver) free_result( result );
446 /* store the message result in the appropriate structure */
447 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
449 res->result = result;
450 res->error = error;
451 res->replied = 1;
452 if (res->timeout)
454 remove_timeout_user( res->timeout );
455 res->timeout = NULL;
457 if (res->sender)
459 if (res->callback_msg)
461 /* queue the callback message in the sender queue */
462 struct callback_msg_data *data = res->callback_msg->data;
463 data->result = result;
464 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
465 set_queue_bits( res->sender, QS_SENDMESSAGE );
466 res->callback_msg = NULL;
467 remove_result_from_sender( res );
469 else
471 /* wake sender queue if waiting on this result */
472 if (list_head(&res->sender->send_result) == &res->sender_entry)
473 set_queue_bits( res->sender, QS_SMRESULT );
479 /* free a message when deleting a queue or window */
480 static void free_message( struct message *msg )
482 struct message_result *result = msg->result;
483 if (result)
485 result->msg = NULL;
486 if (result->sender)
488 result->receiver = NULL;
489 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
491 else free_result( result );
493 free( msg->data );
494 free( msg );
497 /* remove (and free) a message from a message list */
498 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
499 enum message_kind kind )
501 list_remove( &msg->entry );
502 switch(kind)
504 case SEND_MESSAGE:
505 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
506 break;
507 case POST_MESSAGE:
508 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
509 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
510 break;
512 free_message( msg );
515 /* message timed out without getting a reply */
516 static void result_timeout( void *private )
518 struct message_result *result = private;
520 assert( !result->replied );
522 result->timeout = NULL;
524 if (result->msg) /* not received yet */
526 struct message *msg = result->msg;
528 result->msg = NULL;
529 msg->result = NULL;
530 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
531 result->receiver = NULL;
532 if (!result->sender)
534 free_result( result );
535 return;
539 store_message_result( result, 0, STATUS_TIMEOUT );
542 /* allocate and fill a message result structure */
543 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
544 struct msg_queue *recv_queue,
545 struct message *msg, timeout_t timeout )
547 struct message_result *result = mem_alloc( sizeof(*result) );
548 if (result)
550 result->msg = msg;
551 result->sender = send_queue;
552 result->receiver = recv_queue;
553 result->replied = 0;
554 result->data = NULL;
555 result->data_size = 0;
556 result->timeout = NULL;
558 if (msg->type == MSG_CALLBACK)
560 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
562 if (!callback_msg)
564 free( result );
565 return NULL;
567 callback_msg->type = MSG_CALLBACK_RESULT;
568 callback_msg->win = msg->win;
569 callback_msg->msg = msg->msg;
570 callback_msg->wparam = 0;
571 callback_msg->lparam = 0;
572 callback_msg->time = get_tick_count();
573 callback_msg->result = NULL;
574 /* steal the data from the original message */
575 callback_msg->data = msg->data;
576 callback_msg->data_size = msg->data_size;
577 msg->data = NULL;
578 msg->data_size = 0;
580 result->callback_msg = callback_msg;
581 list_add_head( &send_queue->callback_result, &result->sender_entry );
583 else
585 result->callback_msg = NULL;
586 list_add_head( &send_queue->send_result, &result->sender_entry );
589 if (timeout != TIMEOUT_INFINITE)
590 result->timeout = add_timeout_user( timeout, result_timeout, result );
592 return result;
595 /* receive a message, removing it from the sent queue */
596 static void receive_message( struct msg_queue *queue, struct message *msg,
597 struct get_message_reply *reply )
599 struct message_result *result = msg->result;
601 reply->total = msg->data_size;
602 if (msg->data_size > get_reply_max_size())
604 set_error( STATUS_BUFFER_OVERFLOW );
605 return;
607 reply->type = msg->type;
608 reply->win = msg->win;
609 reply->msg = msg->msg;
610 reply->wparam = msg->wparam;
611 reply->lparam = msg->lparam;
612 reply->time = msg->time;
614 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
616 list_remove( &msg->entry );
617 /* put the result on the receiver result stack */
618 if (result)
620 result->msg = NULL;
621 result->recv_next = queue->recv_result;
622 queue->recv_result = result;
624 free( msg );
625 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
628 /* set the result of the current received message */
629 static void reply_message( struct msg_queue *queue, lparam_t result,
630 unsigned int error, int remove, const void *data, data_size_t len )
632 struct message_result *res = queue->recv_result;
634 if (remove)
636 queue->recv_result = res->recv_next;
637 res->receiver = NULL;
638 if (!res->sender) /* no one waiting for it */
640 free_result( res );
641 return;
644 if (!res->replied)
646 if (len && (res->data = memdup( data, len ))) res->data_size = len;
647 store_message_result( res, result, error );
651 static int match_window( user_handle_t win, user_handle_t msg_win )
653 if (!win) return 1;
654 if (win == (user_handle_t)-1) return !msg_win;
655 if (msg_win == win) return 1;
656 return is_child_window( win, msg_win );
659 /* retrieve a posted message */
660 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
661 unsigned int first, unsigned int last, unsigned int flags,
662 struct get_message_reply *reply )
664 struct message *msg;
666 /* check against the filters */
667 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
669 if (!match_window( win, msg->win )) continue;
670 if (!check_msg_filter( msg->msg, first, last )) continue;
671 goto found; /* found one */
673 return 0;
675 /* return it to the app */
676 found:
677 reply->total = msg->data_size;
678 if (msg->data_size > get_reply_max_size())
680 set_error( STATUS_BUFFER_OVERFLOW );
681 return 1;
683 reply->type = msg->type;
684 reply->win = msg->win;
685 reply->msg = msg->msg;
686 reply->wparam = msg->wparam;
687 reply->lparam = msg->lparam;
688 reply->time = msg->time;
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 = 0;
713 reply->msg = WM_QUIT;
714 reply->wparam = queue->exit_code;
715 reply->lparam = 0;
716 reply->time = get_tick_count();
718 if (flags & PM_REMOVE)
720 queue->quit_message = 0;
721 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
722 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
724 return 1;
726 else
727 return 0;
730 /* empty a message list and free all the messages */
731 static void empty_msg_list( struct list *list )
733 struct list *ptr;
735 while ((ptr = list_head( list )) != NULL)
737 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
738 list_remove( &msg->entry );
739 free_message( msg );
743 /* cleanup all pending results when deleting a queue */
744 static void cleanup_results( struct msg_queue *queue )
746 struct list *entry;
748 while ((entry = list_head( &queue->send_result )) != NULL)
750 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
753 while ((entry = list_head( &queue->callback_result )) != NULL)
755 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
758 while (queue->recv_result)
759 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
762 /* check if the thread owning the queue is hung (not checking for messages) */
763 static int is_queue_hung( struct msg_queue *queue )
765 struct wait_queue_entry *entry;
767 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
768 return 0; /* less than 5 seconds since last get message -> not hung */
770 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
772 if (entry->thread->queue == queue)
773 return 0; /* thread is waiting on queue -> not hung */
775 return 1;
778 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
780 struct msg_queue *queue = (struct msg_queue *)obj;
781 struct process *process = entry->thread->process;
783 /* a thread can only wait on its own queue */
784 if (entry->thread->queue != queue)
786 set_error( STATUS_ACCESS_DENIED );
787 return 0;
789 /* if waiting on the main process queue, set the idle event */
790 if (process->queue == queue)
792 if (process->idle_event) set_event( process->idle_event );
794 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
795 set_fd_events( queue->fd, POLLIN );
796 add_queue( obj, entry );
797 return 1;
800 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
802 struct msg_queue *queue = (struct msg_queue *)obj;
803 struct process *process = entry->thread->process;
805 remove_queue( obj, entry );
806 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
807 set_fd_events( queue->fd, 0 );
809 assert( entry->thread->queue == queue );
811 /* if waiting on the main process queue, reset the idle event */
812 if (process->queue == queue)
814 if (process->idle_event) reset_event( process->idle_event );
818 static void msg_queue_dump( struct object *obj, int verbose )
820 struct msg_queue *queue = (struct msg_queue *)obj;
821 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
822 queue->wake_bits, queue->wake_mask );
825 static int msg_queue_signaled( struct object *obj, struct thread *thread )
827 struct msg_queue *queue = (struct msg_queue *)obj;
828 int ret = 0;
830 if (queue->fd)
832 if ((ret = check_fd_events( queue->fd, POLLIN )))
833 /* stop waiting on select() if we are signaled */
834 set_fd_events( queue->fd, 0 );
835 else if (!list_empty( &obj->wait_queue ))
836 /* restart waiting on poll() if we are no longer signaled */
837 set_fd_events( queue->fd, POLLIN );
839 return ret || is_signaled( queue );
842 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
844 struct msg_queue *queue = (struct msg_queue *)obj;
845 queue->wake_mask = 0;
846 queue->changed_mask = 0;
847 return 0; /* Not abandoned */
850 static void msg_queue_destroy( struct object *obj )
852 struct msg_queue *queue = (struct msg_queue *)obj;
853 struct list *ptr;
854 int i;
856 cleanup_results( queue );
857 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
859 while ((ptr = list_head( &queue->pending_timers )))
861 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
862 list_remove( &timer->entry );
863 free( timer );
865 while ((ptr = list_head( &queue->expired_timers )))
867 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
868 list_remove( &timer->entry );
869 free( timer );
871 if (queue->timeout) remove_timeout_user( queue->timeout );
872 if (queue->input) release_object( queue->input );
873 if (queue->hooks) release_object( queue->hooks );
874 if (queue->fd) release_object( queue->fd );
877 static void msg_queue_poll_event( struct fd *fd, int event )
879 struct msg_queue *queue = get_fd_user( fd );
880 assert( queue->obj.ops == &msg_queue_ops );
882 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
883 else set_fd_events( queue->fd, 0 );
884 wake_up( &queue->obj, 0 );
887 static void thread_input_dump( struct object *obj, int verbose )
889 struct thread_input *input = (struct thread_input *)obj;
890 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
891 input->focus, input->capture, input->active );
894 static void thread_input_destroy( struct object *obj )
896 struct thread_input *input = (struct thread_input *)obj;
898 if (foreground_input == input) foreground_input = NULL;
899 empty_msg_list( &input->msg_list );
900 if (input->desktop) release_object( input->desktop );
903 /* fix the thread input data when a window is destroyed */
904 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
906 struct thread_input *input = queue->input;
908 if (window == input->focus) input->focus = 0;
909 if (window == input->capture) input->capture = 0;
910 if (window == input->active) input->active = 0;
911 if (window == input->menu_owner) input->menu_owner = 0;
912 if (window == input->move_size) input->move_size = 0;
913 if (window == input->caret) set_caret_window( input, 0 );
916 /* check if the specified window can be set in the input data of a given queue */
917 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
919 struct thread *thread;
920 int ret = 0;
922 if (!window) return 1; /* we can always clear the data */
924 if ((thread = get_window_thread( window )))
926 ret = (queue->input == thread->queue->input);
927 if (!ret) set_error( STATUS_ACCESS_DENIED );
928 release_object( thread );
930 else set_error( STATUS_INVALID_HANDLE );
932 return ret;
935 /* make sure the specified thread has a queue */
936 int init_thread_queue( struct thread *thread )
938 if (thread->queue) return 1;
939 return (create_msg_queue( thread, NULL ) != NULL);
942 /* attach two thread input data structures */
943 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
945 struct desktop *desktop;
946 struct thread_input *input;
948 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
949 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
950 input = (struct thread_input *)grab_object( thread_to->queue->input );
951 if (input->desktop != desktop)
953 set_error( STATUS_ACCESS_DENIED );
954 release_object( input );
955 release_object( desktop );
956 return 0;
958 release_object( desktop );
960 if (thread_from->queue)
962 release_thread_input( thread_from );
963 thread_from->queue->input = input;
965 else
967 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
969 memset( input->keystate, 0, sizeof(input->keystate) );
970 return 1;
973 /* detach two thread input data structures */
974 void detach_thread_input( struct thread *thread_from )
976 struct thread_input *input;
978 if ((input = create_thread_input( thread_from )))
980 release_thread_input( thread_from );
981 thread_from->queue->input = input;
986 /* set the next timer to expire */
987 static void set_next_timer( struct msg_queue *queue )
989 struct list *ptr;
991 if (queue->timeout)
993 remove_timeout_user( queue->timeout );
994 queue->timeout = NULL;
996 if ((ptr = list_head( &queue->pending_timers )))
998 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
999 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1001 /* set/clear QS_TIMER bit */
1002 if (list_empty( &queue->expired_timers ))
1003 clear_queue_bits( queue, QS_TIMER );
1004 else
1005 set_queue_bits( queue, QS_TIMER );
1008 /* find a timer from its window and id */
1009 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1010 unsigned int msg, lparam_t id )
1012 struct list *ptr;
1014 /* we need to search both lists */
1016 LIST_FOR_EACH( ptr, &queue->pending_timers )
1018 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1019 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1021 LIST_FOR_EACH( ptr, &queue->expired_timers )
1023 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1024 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1026 return NULL;
1029 /* callback for the next timer expiration */
1030 static void timer_callback( void *private )
1032 struct msg_queue *queue = private;
1033 struct list *ptr;
1035 queue->timeout = NULL;
1036 /* move on to the next timer */
1037 ptr = list_head( &queue->pending_timers );
1038 list_remove( ptr );
1039 list_add_tail( &queue->expired_timers, ptr );
1040 set_next_timer( queue );
1043 /* link a timer at its rightful place in the queue list */
1044 static void link_timer( struct msg_queue *queue, struct timer *timer )
1046 struct list *ptr;
1048 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1050 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1051 if (t->when >= timer->when) break;
1053 list_add_before( ptr, &timer->entry );
1056 /* remove a timer from the queue timer list and free it */
1057 static void free_timer( struct msg_queue *queue, struct timer *timer )
1059 list_remove( &timer->entry );
1060 free( timer );
1061 set_next_timer( queue );
1064 /* restart an expired timer */
1065 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1067 list_remove( &timer->entry );
1068 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1069 link_timer( queue, timer );
1070 set_next_timer( queue );
1073 /* find an expired timer matching the filtering parameters */
1074 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1075 unsigned int get_first, unsigned int get_last,
1076 int remove )
1078 struct list *ptr;
1080 LIST_FOR_EACH( ptr, &queue->expired_timers )
1082 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1083 if (win && timer->win != win) continue;
1084 if (check_msg_filter( timer->msg, get_first, get_last ))
1086 if (remove) restart_timer( queue, timer );
1087 return timer;
1090 return NULL;
1093 /* add a timer */
1094 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1096 struct timer *timer = mem_alloc( sizeof(*timer) );
1097 if (timer)
1099 timer->rate = max( rate, 1 );
1100 timer->when = current_time + (timeout_t)timer->rate * 10000;
1101 link_timer( queue, timer );
1102 /* check if we replaced the next timer */
1103 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1105 return timer;
1108 /* change the input key state for a given key */
1109 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1111 if (down)
1113 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1114 input->keystate[key] |= 0x80;
1116 else input->keystate[key] &= ~0x80;
1119 /* update the input key state for a keyboard message */
1120 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1122 unsigned char key;
1123 int down = 0;
1125 switch (msg->msg)
1127 case WM_LBUTTONDOWN:
1128 down = 1;
1129 /* fall through */
1130 case WM_LBUTTONUP:
1131 set_input_key_state( input, VK_LBUTTON, down );
1132 break;
1133 case WM_MBUTTONDOWN:
1134 down = 1;
1135 /* fall through */
1136 case WM_MBUTTONUP:
1137 set_input_key_state( input, VK_MBUTTON, down );
1138 break;
1139 case WM_RBUTTONDOWN:
1140 down = 1;
1141 /* fall through */
1142 case WM_RBUTTONUP:
1143 set_input_key_state( input, VK_RBUTTON, down );
1144 break;
1145 case WM_XBUTTONDOWN:
1146 down = 1;
1147 /* fall through */
1148 case WM_XBUTTONUP:
1149 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1150 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1151 break;
1152 case WM_KEYDOWN:
1153 case WM_SYSKEYDOWN:
1154 down = 1;
1155 /* fall through */
1156 case WM_KEYUP:
1157 case WM_SYSKEYUP:
1158 key = (unsigned char)msg->wparam;
1159 set_input_key_state( input, key, down );
1160 switch(key)
1162 case VK_LCONTROL:
1163 case VK_RCONTROL:
1164 down = (input->keystate[VK_LCONTROL] | input->keystate[VK_RCONTROL]) & 0x80;
1165 set_input_key_state( input, VK_CONTROL, down );
1166 break;
1167 case VK_LMENU:
1168 case VK_RMENU:
1169 down = (input->keystate[VK_LMENU] | input->keystate[VK_RMENU]) & 0x80;
1170 set_input_key_state( input, VK_MENU, down );
1171 break;
1172 case VK_LSHIFT:
1173 case VK_RSHIFT:
1174 down = (input->keystate[VK_LSHIFT] | input->keystate[VK_RSHIFT]) & 0x80;
1175 set_input_key_state( input, VK_SHIFT, down );
1176 break;
1178 break;
1182 /* release the hardware message currently being processed by the given thread */
1183 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1184 int remove, user_handle_t new_win )
1186 struct thread_input *input = queue->input;
1187 struct message *msg;
1189 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1191 if (msg->unique_id == hw_id) break;
1193 if (&msg->entry == &input->msg_list) return; /* not found */
1195 /* clear the queue bit for that message */
1196 if (remove || new_win)
1198 struct message *other;
1199 int clr_bit;
1201 clr_bit = get_hardware_msg_bit( msg );
1202 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1204 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1206 clr_bit = 0;
1207 break;
1210 if (clr_bit) clear_queue_bits( queue, clr_bit );
1213 if (new_win) /* set the new window */
1215 struct thread *owner = get_window_thread( new_win );
1216 if (owner)
1218 msg->win = new_win;
1219 if (owner->queue->input != input)
1221 list_remove( &msg->entry );
1222 if (msg->msg == WM_MOUSEMOVE && merge_message( owner->queue->input, msg ))
1224 free_message( msg );
1225 release_object( owner );
1226 return;
1228 list_add_tail( &owner->queue->input->msg_list, &msg->entry );
1230 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1231 remove = 0;
1232 release_object( owner );
1235 if (remove)
1237 update_input_key_state( input, msg );
1238 list_remove( &msg->entry );
1239 free_message( msg );
1243 /* find the window that should receive a given hardware message */
1244 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1245 struct hardware_msg_data *data, unsigned int *msg_code )
1247 user_handle_t win = 0;
1249 *msg_code = msg->msg;
1250 if (is_keyboard_msg( msg ))
1252 if (input && !(win = input->focus))
1254 win = input->active;
1255 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1258 else /* mouse message */
1260 if (!input || !(win = input->capture))
1262 if (!(win = msg->win) || !is_window_visible( win ))
1264 if (input) win = window_from_point( input->desktop, data->x, data->y );
1268 return win;
1271 /* queue a hardware message into a given thread input */
1272 static void queue_hardware_message( struct msg_queue *queue, struct message *msg,
1273 struct hardware_msg_data *data )
1275 user_handle_t win;
1276 struct thread *thread;
1277 struct thread_input *input = queue ? queue->input : foreground_input;
1278 unsigned int msg_code;
1280 last_input_time = get_tick_count();
1281 win = find_hardware_message_window( input, msg, data, &msg_code );
1282 if (!win || !(thread = get_window_thread(win)))
1284 if (input) update_input_key_state( input, msg );
1285 free( msg );
1286 return;
1288 input = thread->queue->input;
1290 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1291 else
1293 msg->unique_id = 0; /* will be set once we return it to the app */
1294 list_add_tail( &input->msg_list, &msg->entry );
1295 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1297 release_object( thread );
1300 /* check message filter for a hardware message */
1301 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1302 user_handle_t filter_win, unsigned int first, unsigned int last )
1304 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1306 /* we can only test the window for a keyboard message since the
1307 * dest window for a mouse message depends on hittest */
1308 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1309 return 0;
1310 /* the message code is final for a keyboard message, we can simply check it */
1311 return check_msg_filter( msg_code, first, last );
1313 else /* mouse message */
1315 /* we need to check all possible values that the message can have in the end */
1317 if (check_msg_filter( msg_code, first, last )) return 1;
1318 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1320 /* all other messages can become non-client messages */
1321 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1323 /* clicks can become double-clicks or non-client double-clicks */
1324 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1325 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1327 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1328 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1330 return 0;
1335 /* find a hardware message for the given queue */
1336 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1337 unsigned int first, unsigned int last, struct get_message_reply *reply )
1339 struct thread_input *input = thread->queue->input;
1340 struct thread *win_thread;
1341 struct list *ptr;
1342 user_handle_t win;
1343 int clear_bits, got_one = 0;
1344 unsigned int msg_code;
1346 ptr = list_head( &input->msg_list );
1347 if (hw_id)
1349 while (ptr)
1351 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1352 if (msg->unique_id == hw_id) break;
1353 ptr = list_next( &input->msg_list, ptr );
1355 if (!ptr) ptr = list_head( &input->msg_list );
1356 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1359 if (ptr == list_head( &input->msg_list ))
1360 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1361 else
1362 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1364 while (ptr)
1366 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1367 struct hardware_msg_data *data = msg->data;
1369 ptr = list_next( &input->msg_list, ptr );
1370 win = find_hardware_message_window( input, msg, data, &msg_code );
1371 if (!win || !(win_thread = get_window_thread( win )))
1373 /* no window at all, remove it */
1374 update_input_key_state( input, msg );
1375 list_remove( &msg->entry );
1376 free_message( msg );
1377 continue;
1379 if (win_thread != thread)
1381 if (win_thread->queue->input == input)
1383 /* wake the other thread */
1384 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1385 got_one = 1;
1387 else
1389 /* for another thread input, drop it */
1390 update_input_key_state( input, msg );
1391 list_remove( &msg->entry );
1392 free_message( msg );
1394 release_object( win_thread );
1395 continue;
1397 release_object( win_thread );
1399 /* if we already got a message for another thread, or if it doesn't
1400 * match the filter we skip it */
1401 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1403 clear_bits &= ~get_hardware_msg_bit( msg );
1404 continue;
1406 /* now we can return it */
1407 if (!msg->unique_id) msg->unique_id = get_unique_id();
1408 reply->type = MSG_HARDWARE;
1409 reply->win = win;
1410 reply->msg = msg_code;
1411 reply->wparam = msg->wparam;
1412 reply->lparam = msg->lparam;
1413 reply->time = msg->time;
1415 data->hw_id = msg->unique_id;
1416 set_reply_data( msg->data, msg->data_size );
1417 return 1;
1419 /* nothing found, clear the hardware queue bits */
1420 clear_queue_bits( thread->queue, clear_bits );
1421 return 0;
1424 /* increment (or decrement if 'incr' is negative) the queue paint count */
1425 void inc_queue_paint_count( struct thread *thread, int incr )
1427 struct msg_queue *queue = thread->queue;
1429 assert( queue );
1431 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1433 if (queue->paint_count)
1434 set_queue_bits( queue, QS_PAINT );
1435 else
1436 clear_queue_bits( queue, QS_PAINT );
1440 /* remove all messages and timers belonging to a certain window */
1441 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1443 struct msg_queue *queue = thread->queue;
1444 struct list *ptr;
1445 int i;
1447 if (!queue) return;
1449 /* remove timers */
1451 ptr = list_head( &queue->pending_timers );
1452 while (ptr)
1454 struct list *next = list_next( &queue->pending_timers, ptr );
1455 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1456 if (timer->win == win) free_timer( queue, timer );
1457 ptr = next;
1459 ptr = list_head( &queue->expired_timers );
1460 while (ptr)
1462 struct list *next = list_next( &queue->expired_timers, ptr );
1463 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1464 if (timer->win == win) free_timer( queue, timer );
1465 ptr = next;
1468 /* remove messages */
1469 for (i = 0; i < NB_MSG_KINDS; i++)
1471 struct list *ptr, *next;
1473 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1475 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1476 if (msg->win == win) remove_queue_message( queue, msg, i );
1480 thread_input_cleanup_window( queue, win );
1483 /* post a message to a window; used by socket handling */
1484 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
1486 struct message *msg;
1487 struct thread *thread = get_window_thread( win );
1489 if (!thread) return;
1491 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1493 msg->type = MSG_POSTED;
1494 msg->win = get_user_full_handle( win );
1495 msg->msg = message;
1496 msg->wparam = wparam;
1497 msg->lparam = lparam;
1498 msg->time = get_tick_count();
1499 msg->result = NULL;
1500 msg->data = NULL;
1501 msg->data_size = 0;
1503 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1504 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1506 release_object( thread );
1509 /* post a win event */
1510 void post_win_event( struct thread *thread, unsigned int event,
1511 user_handle_t win, unsigned int object_id,
1512 unsigned int child_id, client_ptr_t hook_proc,
1513 const WCHAR *module, data_size_t module_size,
1514 user_handle_t hook)
1516 struct message *msg;
1518 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1520 struct winevent_msg_data *data;
1522 msg->type = MSG_WINEVENT;
1523 msg->win = get_user_full_handle( win );
1524 msg->msg = event;
1525 msg->wparam = object_id;
1526 msg->lparam = child_id;
1527 msg->time = get_tick_count();
1528 msg->result = NULL;
1530 if ((data = malloc( sizeof(*data) + module_size )))
1532 data->hook = hook;
1533 data->tid = get_thread_id( current );
1534 data->hook_proc = hook_proc;
1535 memcpy( data + 1, module, module_size );
1537 msg->data = data;
1538 msg->data_size = sizeof(*data) + module_size;
1540 if (debug_level > 1)
1541 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
1542 get_thread_id(thread), event, win, object_id, child_id );
1543 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1544 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1546 else
1547 free( msg );
1552 /* check if the thread owning the window is hung */
1553 DECL_HANDLER(is_window_hung)
1555 struct thread *thread;
1557 thread = get_window_thread( req->win );
1558 if (thread)
1560 reply->is_hung = is_queue_hung( thread->queue );
1561 release_object( thread );
1563 else reply->is_hung = 0;
1567 /* get the message queue of the current thread */
1568 DECL_HANDLER(get_msg_queue)
1570 struct msg_queue *queue = get_current_queue();
1572 reply->handle = 0;
1573 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1577 /* set the file descriptor associated to the current thread queue */
1578 DECL_HANDLER(set_queue_fd)
1580 struct msg_queue *queue = get_current_queue();
1581 struct file *file;
1582 int unix_fd;
1584 if (queue->fd) /* fd can only be set once */
1586 set_error( STATUS_ACCESS_DENIED );
1587 return;
1589 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
1591 if ((unix_fd = get_file_unix_fd( file )) != -1)
1593 if ((unix_fd = dup( unix_fd )) != -1)
1594 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
1595 else
1596 file_set_error();
1598 release_object( file );
1602 /* set the current message queue wakeup mask */
1603 DECL_HANDLER(set_queue_mask)
1605 struct msg_queue *queue = get_current_queue();
1607 if (queue)
1609 queue->wake_mask = req->wake_mask;
1610 queue->changed_mask = req->changed_mask;
1611 reply->wake_bits = queue->wake_bits;
1612 reply->changed_bits = queue->changed_bits;
1613 if (is_signaled( queue ))
1615 /* if skip wait is set, do what would have been done in the subsequent wait */
1616 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1617 else wake_up( &queue->obj, 0 );
1623 /* get the current message queue status */
1624 DECL_HANDLER(get_queue_status)
1626 struct msg_queue *queue = current->queue;
1627 if (queue)
1629 reply->wake_bits = queue->wake_bits;
1630 reply->changed_bits = queue->changed_bits;
1631 if (req->clear) queue->changed_bits = 0;
1633 else reply->wake_bits = reply->changed_bits = 0;
1637 /* send a message to a thread queue */
1638 DECL_HANDLER(send_message)
1640 struct message *msg;
1641 struct msg_queue *send_queue = get_current_queue();
1642 struct msg_queue *recv_queue = NULL;
1643 struct thread *thread = NULL;
1645 if (!(thread = get_thread_from_id( req->id ))) return;
1647 if (!(recv_queue = thread->queue))
1649 set_error( STATUS_INVALID_PARAMETER );
1650 release_object( thread );
1651 return;
1653 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1655 set_error( STATUS_TIMEOUT );
1656 release_object( thread );
1657 return;
1660 if ((msg = mem_alloc( sizeof(*msg) )))
1662 msg->type = req->type;
1663 msg->win = get_user_full_handle( req->win );
1664 msg->msg = req->msg;
1665 msg->wparam = req->wparam;
1666 msg->lparam = req->lparam;
1667 msg->time = get_tick_count();
1668 msg->result = NULL;
1669 msg->data = NULL;
1670 msg->data_size = get_req_data_size();
1672 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1674 free( msg );
1675 release_object( thread );
1676 return;
1679 switch(msg->type)
1681 case MSG_OTHER_PROCESS:
1682 case MSG_ASCII:
1683 case MSG_UNICODE:
1684 case MSG_CALLBACK:
1685 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
1687 free_message( msg );
1688 break;
1690 /* fall through */
1691 case MSG_NOTIFY:
1692 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1693 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1694 break;
1695 case MSG_POSTED:
1696 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1697 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1698 break;
1699 case MSG_HARDWARE: /* should use send_hardware_message instead */
1700 case MSG_CALLBACK_RESULT: /* cannot send this one */
1701 default:
1702 set_error( STATUS_INVALID_PARAMETER );
1703 free( msg );
1704 break;
1707 release_object( thread );
1710 /* send a hardware message to a thread queue */
1711 DECL_HANDLER(send_hardware_message)
1713 struct message *msg;
1714 struct msg_queue *recv_queue = NULL;
1715 struct thread *thread = NULL;
1716 struct hardware_msg_data *data;
1718 if (req->id)
1720 if (!(thread = get_thread_from_id( req->id ))) return;
1723 if (thread && !(recv_queue = thread->queue))
1725 set_error( STATUS_INVALID_PARAMETER );
1726 release_object( thread );
1727 return;
1730 if (!(data = mem_alloc( sizeof(*data) )))
1732 if (thread) release_object( thread );
1733 return;
1735 memset( data, 0, sizeof(*data) );
1736 data->x = req->x;
1737 data->y = req->y;
1738 data->info = req->info;
1740 if ((msg = mem_alloc( sizeof(*msg) )))
1742 msg->type = MSG_HARDWARE;
1743 msg->win = get_user_full_handle( req->win );
1744 msg->msg = req->msg;
1745 msg->wparam = req->wparam;
1746 msg->lparam = req->lparam;
1747 msg->time = req->time;
1748 msg->result = NULL;
1749 msg->data = data;
1750 msg->data_size = sizeof(*data);
1751 queue_hardware_message( recv_queue, msg, data );
1753 else free( data );
1755 if (thread) release_object( thread );
1758 /* post a quit message to the current queue */
1759 DECL_HANDLER(post_quit_message)
1761 struct msg_queue *queue = get_current_queue();
1763 if (!queue)
1764 return;
1766 queue->quit_message = 1;
1767 queue->exit_code = req->exit_code;
1768 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1771 /* get a message from the current queue */
1772 DECL_HANDLER(get_message)
1774 struct timer *timer;
1775 struct list *ptr;
1776 struct msg_queue *queue = get_current_queue();
1777 user_handle_t get_win = get_user_full_handle( req->get_win );
1778 unsigned int filter = req->flags >> 16;
1780 reply->active_hooks = get_active_hooks();
1782 if (!queue) return;
1783 queue->last_get_msg = current_time;
1784 if (!filter) filter = QS_ALLINPUT;
1786 /* first check for sent messages */
1787 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1789 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1790 receive_message( queue, msg, reply );
1791 return;
1794 /* clear changed bits so we can wait on them if we don't find a message */
1795 if (filter & QS_POSTMESSAGE)
1797 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
1798 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
1800 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
1801 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
1803 /* then check for posted messages */
1804 if ((filter & QS_POSTMESSAGE) &&
1805 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1806 return;
1808 /* only check for quit messages if not posted messages pending.
1809 * note: the quit message isn't filtered */
1810 if (get_quit_message( queue, req->flags, reply ))
1811 return;
1813 /* then check for any raw hardware message */
1814 if ((filter & QS_INPUT) &&
1815 filter_contains_hw_range( req->get_first, req->get_last ) &&
1816 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1817 return;
1819 /* now check for WM_PAINT */
1820 if ((filter & QS_PAINT) &&
1821 queue->paint_count &&
1822 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1823 (reply->win = find_window_to_repaint( get_win, current )))
1825 reply->type = MSG_POSTED;
1826 reply->msg = WM_PAINT;
1827 reply->wparam = 0;
1828 reply->lparam = 0;
1829 reply->time = get_tick_count();
1830 return;
1833 /* now check for timer */
1834 if ((filter & QS_TIMER) &&
1835 (timer = find_expired_timer( queue, get_win, req->get_first,
1836 req->get_last, (req->flags & PM_REMOVE) )))
1838 reply->type = MSG_POSTED;
1839 reply->win = timer->win;
1840 reply->msg = timer->msg;
1841 reply->wparam = timer->id;
1842 reply->lparam = timer->lparam;
1843 reply->time = get_tick_count();
1844 return;
1847 queue->wake_mask = req->wake_mask;
1848 queue->changed_mask = req->changed_mask;
1849 set_error( STATUS_PENDING ); /* FIXME */
1853 /* reply to a sent message */
1854 DECL_HANDLER(reply_message)
1856 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1857 else if (current->queue->recv_result)
1858 reply_message( current->queue, req->result, 0, req->remove,
1859 get_req_data(), get_req_data_size() );
1863 /* accept the current hardware message */
1864 DECL_HANDLER(accept_hardware_message)
1866 if (current->queue)
1867 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1868 else
1869 set_error( STATUS_ACCESS_DENIED );
1873 /* retrieve the reply for the last message sent */
1874 DECL_HANDLER(get_message_reply)
1876 struct message_result *result;
1877 struct list *entry;
1878 struct msg_queue *queue = current->queue;
1880 if (queue)
1882 set_error( STATUS_PENDING );
1883 reply->result = 0;
1885 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1887 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1888 if (result->replied || req->cancel)
1890 if (result->replied)
1892 reply->result = result->result;
1893 set_error( result->error );
1894 if (result->data)
1896 data_size_t data_len = min( result->data_size, get_reply_max_size() );
1897 set_reply_data_ptr( result->data, data_len );
1898 result->data = NULL;
1899 result->data_size = 0;
1902 remove_result_from_sender( result );
1904 entry = list_head( &queue->send_result );
1905 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1906 else
1908 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1909 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1913 else set_error( STATUS_ACCESS_DENIED );
1917 /* set a window timer */
1918 DECL_HANDLER(set_win_timer)
1920 struct timer *timer;
1921 struct msg_queue *queue;
1922 struct thread *thread = NULL;
1923 user_handle_t win = 0;
1924 lparam_t id = req->id;
1926 if (req->win)
1928 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1930 set_error( STATUS_INVALID_HANDLE );
1931 return;
1933 if (thread->process != current->process)
1935 release_object( thread );
1936 set_error( STATUS_ACCESS_DENIED );
1937 return;
1939 queue = thread->queue;
1940 /* remove it if it existed already */
1941 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1943 else
1945 queue = get_current_queue();
1946 /* look for a timer with this id */
1947 if (id && (timer = find_timer( queue, 0, req->msg, id )))
1949 /* free and reuse id */
1950 free_timer( queue, timer );
1952 else
1954 /* find a free id for it */
1957 id = queue->next_timer_id;
1958 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
1960 while (find_timer( queue, 0, req->msg, id ));
1964 if ((timer = set_timer( queue, req->rate )))
1966 timer->win = win;
1967 timer->msg = req->msg;
1968 timer->id = id;
1969 timer->lparam = req->lparam;
1970 reply->id = id;
1972 if (thread) release_object( thread );
1975 /* kill a window timer */
1976 DECL_HANDLER(kill_win_timer)
1978 struct timer *timer;
1979 struct thread *thread;
1980 user_handle_t win = 0;
1982 if (req->win)
1984 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1986 set_error( STATUS_INVALID_HANDLE );
1987 return;
1989 if (thread->process != current->process)
1991 release_object( thread );
1992 set_error( STATUS_ACCESS_DENIED );
1993 return;
1996 else thread = (struct thread *)grab_object( current );
1998 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1999 free_timer( thread->queue, timer );
2000 else
2001 set_error( STATUS_INVALID_PARAMETER );
2003 release_object( thread );
2007 /* attach (or detach) thread inputs */
2008 DECL_HANDLER(attach_thread_input)
2010 struct thread *thread_from = get_thread_from_id( req->tid_from );
2011 struct thread *thread_to = get_thread_from_id( req->tid_to );
2013 if (!thread_from || !thread_to)
2015 if (thread_from) release_object( thread_from );
2016 if (thread_to) release_object( thread_to );
2017 return;
2019 if (thread_from != thread_to)
2021 if (req->attach) attach_thread_input( thread_from, thread_to );
2022 else
2024 if (thread_from->queue && thread_to->queue &&
2025 thread_from->queue->input == thread_to->queue->input)
2026 detach_thread_input( thread_from );
2027 else
2028 set_error( STATUS_ACCESS_DENIED );
2031 else set_error( STATUS_ACCESS_DENIED );
2032 release_object( thread_from );
2033 release_object( thread_to );
2037 /* get thread input data */
2038 DECL_HANDLER(get_thread_input)
2040 struct thread *thread = NULL;
2041 struct thread_input *input;
2043 if (req->tid)
2045 if (!(thread = get_thread_from_id( req->tid ))) return;
2046 input = thread->queue ? thread->queue->input : NULL;
2048 else input = foreground_input; /* get the foreground thread info */
2050 if (input)
2052 reply->focus = input->focus;
2053 reply->capture = input->capture;
2054 reply->active = input->active;
2055 reply->menu_owner = input->menu_owner;
2056 reply->move_size = input->move_size;
2057 reply->caret = input->caret;
2058 reply->rect = input->caret_rect;
2060 else
2062 reply->focus = 0;
2063 reply->capture = 0;
2064 reply->active = 0;
2065 reply->menu_owner = 0;
2066 reply->move_size = 0;
2067 reply->caret = 0;
2068 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
2070 /* foreground window is active window of foreground thread */
2071 reply->foreground = foreground_input ? foreground_input->active : 0;
2072 if (thread) release_object( thread );
2076 /* retrieve queue keyboard state for a given thread */
2077 DECL_HANDLER(get_key_state)
2079 struct thread *thread;
2080 struct thread_input *input;
2082 if (!(thread = get_thread_from_id( req->tid ))) return;
2083 input = thread->queue ? thread->queue->input : NULL;
2084 if (input)
2086 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
2087 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
2089 release_object( thread );
2093 /* set queue keyboard state for a given thread */
2094 DECL_HANDLER(set_key_state)
2096 struct thread *thread = NULL;
2097 struct thread_input *input;
2099 if (!(thread = get_thread_from_id( req->tid ))) return;
2100 input = thread->queue ? thread->queue->input : NULL;
2101 if (input)
2103 data_size_t size = min( sizeof(input->keystate), get_req_data_size() );
2104 if (size) memcpy( input->keystate, get_req_data(), size );
2106 release_object( thread );
2110 /* set the system foreground window */
2111 DECL_HANDLER(set_foreground_window)
2113 struct thread *thread;
2114 struct msg_queue *queue = get_current_queue();
2116 reply->previous = foreground_input ? foreground_input->active : 0;
2117 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
2118 reply->send_msg_new = FALSE;
2120 if (is_top_level_window( req->handle ) &&
2121 ((thread = get_window_thread( req->handle ))))
2123 foreground_input = thread->queue->input;
2124 reply->send_msg_new = (foreground_input != queue->input);
2125 release_object( thread );
2127 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2131 /* set the current thread focus window */
2132 DECL_HANDLER(set_focus_window)
2134 struct msg_queue *queue = get_current_queue();
2136 reply->previous = 0;
2137 if (queue && check_queue_input_window( queue, req->handle ))
2139 reply->previous = queue->input->focus;
2140 queue->input->focus = get_user_full_handle( req->handle );
2145 /* set the current thread active window */
2146 DECL_HANDLER(set_active_window)
2148 struct msg_queue *queue = get_current_queue();
2150 reply->previous = 0;
2151 if (queue && check_queue_input_window( queue, req->handle ))
2153 if (!req->handle || make_window_active( req->handle ))
2155 reply->previous = queue->input->active;
2156 queue->input->active = get_user_full_handle( req->handle );
2158 else set_error( STATUS_INVALID_HANDLE );
2163 /* set the current thread capture window */
2164 DECL_HANDLER(set_capture_window)
2166 struct msg_queue *queue = get_current_queue();
2168 reply->previous = reply->full_handle = 0;
2169 if (queue && check_queue_input_window( queue, req->handle ))
2171 struct thread_input *input = queue->input;
2173 reply->previous = input->capture;
2174 input->capture = get_user_full_handle( req->handle );
2175 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2176 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2177 reply->full_handle = input->capture;
2182 /* Set the current thread caret window */
2183 DECL_HANDLER(set_caret_window)
2185 struct msg_queue *queue = get_current_queue();
2187 reply->previous = 0;
2188 if (queue && check_queue_input_window( queue, req->handle ))
2190 struct thread_input *input = queue->input;
2192 reply->previous = input->caret;
2193 reply->old_rect = input->caret_rect;
2194 reply->old_hide = input->caret_hide;
2195 reply->old_state = input->caret_state;
2197 set_caret_window( input, get_user_full_handle(req->handle) );
2198 input->caret_rect.right = input->caret_rect.left + req->width;
2199 input->caret_rect.bottom = input->caret_rect.top + req->height;
2204 /* Set the current thread caret information */
2205 DECL_HANDLER(set_caret_info)
2207 struct msg_queue *queue = get_current_queue();
2208 struct thread_input *input;
2210 if (!queue) return;
2211 input = queue->input;
2212 reply->full_handle = input->caret;
2213 reply->old_rect = input->caret_rect;
2214 reply->old_hide = input->caret_hide;
2215 reply->old_state = input->caret_state;
2217 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2219 set_error( STATUS_ACCESS_DENIED );
2220 return;
2222 if (req->flags & SET_CARET_POS)
2224 input->caret_rect.right += req->x - input->caret_rect.left;
2225 input->caret_rect.bottom += req->y - input->caret_rect.top;
2226 input->caret_rect.left = req->x;
2227 input->caret_rect.top = req->y;
2229 if (req->flags & SET_CARET_HIDE)
2231 input->caret_hide += req->hide;
2232 if (input->caret_hide < 0) input->caret_hide = 0;
2234 if (req->flags & SET_CARET_STATE)
2236 if (req->state == -1) input->caret_state = !input->caret_state;
2237 else input->caret_state = !!req->state;
2242 /* get the time of the last input event */
2243 DECL_HANDLER(get_last_input_time)
2245 reply->time = last_input_time;