oledlg: Update Italian translation.
[wine/hacks.git] / server / queue.c
blob755d3c19cbffddd93e96625f18aa839b703b0def
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 thread_input *input, struct message *msg,
1266 struct hardware_msg_data *data )
1268 user_handle_t win;
1269 struct thread *thread;
1270 unsigned int msg_code;
1272 last_input_time = get_tick_count();
1273 win = find_hardware_message_window( input, msg, data, &msg_code );
1274 if (!win || !(thread = get_window_thread(win)))
1276 if (input) update_input_key_state( input, msg );
1277 free( msg );
1278 return;
1280 input = thread->queue->input;
1282 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1283 else
1285 msg->unique_id = 0; /* will be set once we return it to the app */
1286 list_add_tail( &input->msg_list, &msg->entry );
1287 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1289 release_object( thread );
1292 /* check message filter for a hardware message */
1293 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1294 user_handle_t filter_win, unsigned int first, unsigned int last )
1296 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1298 /* we can only test the window for a keyboard message since the
1299 * dest window for a mouse message depends on hittest */
1300 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1301 return 0;
1302 /* the message code is final for a keyboard message, we can simply check it */
1303 return check_msg_filter( msg_code, first, last );
1305 else /* mouse message */
1307 /* we need to check all possible values that the message can have in the end */
1309 if (check_msg_filter( msg_code, first, last )) return 1;
1310 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1312 /* all other messages can become non-client messages */
1313 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1315 /* clicks can become double-clicks or non-client double-clicks */
1316 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1317 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1319 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1320 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1322 return 0;
1327 /* find a hardware message for the given queue */
1328 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1329 unsigned int first, unsigned int last, struct get_message_reply *reply )
1331 struct thread_input *input = thread->queue->input;
1332 struct thread *win_thread;
1333 struct list *ptr;
1334 user_handle_t win;
1335 int clear_bits, got_one = 0;
1336 unsigned int msg_code;
1338 ptr = list_head( &input->msg_list );
1339 if (hw_id)
1341 while (ptr)
1343 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1344 if (msg->unique_id == hw_id) break;
1345 ptr = list_next( &input->msg_list, ptr );
1347 if (!ptr) ptr = list_head( &input->msg_list );
1348 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1351 if (ptr == list_head( &input->msg_list ))
1352 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1353 else
1354 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1356 while (ptr)
1358 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1359 struct hardware_msg_data *data = msg->data;
1361 ptr = list_next( &input->msg_list, ptr );
1362 win = find_hardware_message_window( input, msg, data, &msg_code );
1363 if (!win || !(win_thread = get_window_thread( win )))
1365 /* no window at all, remove it */
1366 update_input_key_state( input, msg );
1367 list_remove( &msg->entry );
1368 free_message( msg );
1369 continue;
1371 if (win_thread != thread)
1373 if (win_thread->queue->input == input)
1375 /* wake the other thread */
1376 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1377 got_one = 1;
1379 else
1381 /* for another thread input, drop it */
1382 update_input_key_state( input, msg );
1383 list_remove( &msg->entry );
1384 free_message( msg );
1386 release_object( win_thread );
1387 continue;
1389 release_object( win_thread );
1391 /* if we already got a message for another thread, or if it doesn't
1392 * match the filter we skip it */
1393 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1395 clear_bits &= ~get_hardware_msg_bit( msg );
1396 continue;
1398 /* now we can return it */
1399 if (!msg->unique_id) msg->unique_id = get_unique_id();
1400 reply->type = MSG_HARDWARE;
1401 reply->win = win;
1402 reply->msg = msg_code;
1403 reply->wparam = msg->wparam;
1404 reply->lparam = msg->lparam;
1405 reply->time = msg->time;
1407 data->hw_id = msg->unique_id;
1408 set_reply_data( msg->data, msg->data_size );
1409 return 1;
1411 /* nothing found, clear the hardware queue bits */
1412 clear_queue_bits( thread->queue, clear_bits );
1413 return 0;
1416 /* increment (or decrement if 'incr' is negative) the queue paint count */
1417 void inc_queue_paint_count( struct thread *thread, int incr )
1419 struct msg_queue *queue = thread->queue;
1421 assert( queue );
1423 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1425 if (queue->paint_count)
1426 set_queue_bits( queue, QS_PAINT );
1427 else
1428 clear_queue_bits( queue, QS_PAINT );
1432 /* remove all messages and timers belonging to a certain window */
1433 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1435 struct msg_queue *queue = thread->queue;
1436 struct list *ptr;
1437 int i;
1439 if (!queue) return;
1441 /* remove timers */
1443 ptr = list_head( &queue->pending_timers );
1444 while (ptr)
1446 struct list *next = list_next( &queue->pending_timers, ptr );
1447 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1448 if (timer->win == win) free_timer( queue, timer );
1449 ptr = next;
1451 ptr = list_head( &queue->expired_timers );
1452 while (ptr)
1454 struct list *next = list_next( &queue->expired_timers, ptr );
1455 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1456 if (timer->win == win) free_timer( queue, timer );
1457 ptr = next;
1460 /* remove messages */
1461 for (i = 0; i < NB_MSG_KINDS; i++)
1463 struct list *ptr, *next;
1465 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1467 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1468 if (msg->win == win) remove_queue_message( queue, msg, i );
1472 thread_input_cleanup_window( queue, win );
1475 /* post a message to a window; used by socket handling */
1476 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
1478 struct message *msg;
1479 struct thread *thread = get_window_thread( win );
1481 if (!thread) return;
1483 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1485 msg->type = MSG_POSTED;
1486 msg->win = get_user_full_handle( win );
1487 msg->msg = message;
1488 msg->wparam = wparam;
1489 msg->lparam = lparam;
1490 msg->time = get_tick_count();
1491 msg->result = NULL;
1492 msg->data = NULL;
1493 msg->data_size = 0;
1495 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1496 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1498 release_object( thread );
1501 /* post a win event */
1502 void post_win_event( struct thread *thread, unsigned int event,
1503 user_handle_t win, unsigned int object_id,
1504 unsigned int child_id, client_ptr_t hook_proc,
1505 const WCHAR *module, data_size_t module_size,
1506 user_handle_t hook)
1508 struct message *msg;
1510 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1512 struct winevent_msg_data *data;
1514 msg->type = MSG_WINEVENT;
1515 msg->win = get_user_full_handle( win );
1516 msg->msg = event;
1517 msg->wparam = object_id;
1518 msg->lparam = child_id;
1519 msg->time = get_tick_count();
1520 msg->result = NULL;
1522 if ((data = malloc( sizeof(*data) + module_size )))
1524 data->hook = hook;
1525 data->tid = get_thread_id( current );
1526 data->hook_proc = hook_proc;
1527 memcpy( data + 1, module, module_size );
1529 msg->data = data;
1530 msg->data_size = sizeof(*data) + module_size;
1532 if (debug_level > 1)
1533 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
1534 get_thread_id(thread), event, win, object_id, child_id );
1535 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1536 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1538 else
1539 free( msg );
1544 /* check if the thread owning the window is hung */
1545 DECL_HANDLER(is_window_hung)
1547 struct thread *thread;
1549 thread = get_window_thread( req->win );
1550 if (thread)
1552 reply->is_hung = is_queue_hung( thread->queue );
1553 release_object( thread );
1555 else reply->is_hung = 0;
1559 /* get the message queue of the current thread */
1560 DECL_HANDLER(get_msg_queue)
1562 struct msg_queue *queue = get_current_queue();
1564 reply->handle = 0;
1565 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1569 /* set the file descriptor associated to the current thread queue */
1570 DECL_HANDLER(set_queue_fd)
1572 struct msg_queue *queue = get_current_queue();
1573 struct file *file;
1574 int unix_fd;
1576 if (queue->fd) /* fd can only be set once */
1578 set_error( STATUS_ACCESS_DENIED );
1579 return;
1581 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
1583 if ((unix_fd = get_file_unix_fd( file )) != -1)
1585 if ((unix_fd = dup( unix_fd )) != -1)
1586 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
1587 else
1588 file_set_error();
1590 release_object( file );
1594 /* set the current message queue wakeup mask */
1595 DECL_HANDLER(set_queue_mask)
1597 struct msg_queue *queue = get_current_queue();
1599 if (queue)
1601 queue->wake_mask = req->wake_mask;
1602 queue->changed_mask = req->changed_mask;
1603 reply->wake_bits = queue->wake_bits;
1604 reply->changed_bits = queue->changed_bits;
1605 if (is_signaled( queue ))
1607 /* if skip wait is set, do what would have been done in the subsequent wait */
1608 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1609 else wake_up( &queue->obj, 0 );
1615 /* get the current message queue status */
1616 DECL_HANDLER(get_queue_status)
1618 struct msg_queue *queue = current->queue;
1619 if (queue)
1621 reply->wake_bits = queue->wake_bits;
1622 reply->changed_bits = queue->changed_bits;
1623 if (req->clear) queue->changed_bits = 0;
1625 else reply->wake_bits = reply->changed_bits = 0;
1629 /* send a message to a thread queue */
1630 DECL_HANDLER(send_message)
1632 struct message *msg;
1633 struct msg_queue *send_queue = get_current_queue();
1634 struct msg_queue *recv_queue = NULL;
1635 struct thread *thread = NULL;
1637 if (!(thread = get_thread_from_id( req->id ))) return;
1639 if (!(recv_queue = thread->queue))
1641 set_error( STATUS_INVALID_PARAMETER );
1642 release_object( thread );
1643 return;
1645 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1647 set_error( STATUS_TIMEOUT );
1648 release_object( thread );
1649 return;
1652 if ((msg = mem_alloc( sizeof(*msg) )))
1654 msg->type = req->type;
1655 msg->win = get_user_full_handle( req->win );
1656 msg->msg = req->msg;
1657 msg->wparam = req->wparam;
1658 msg->lparam = req->lparam;
1659 msg->time = get_tick_count();
1660 msg->result = NULL;
1661 msg->data = NULL;
1662 msg->data_size = get_req_data_size();
1664 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1666 free( msg );
1667 release_object( thread );
1668 return;
1671 switch(msg->type)
1673 case MSG_OTHER_PROCESS:
1674 case MSG_ASCII:
1675 case MSG_UNICODE:
1676 case MSG_CALLBACK:
1677 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
1679 free_message( msg );
1680 break;
1682 /* fall through */
1683 case MSG_NOTIFY:
1684 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1685 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1686 break;
1687 case MSG_POSTED:
1688 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1689 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1690 break;
1691 case MSG_HARDWARE: /* should use send_hardware_message instead */
1692 case MSG_CALLBACK_RESULT: /* cannot send this one */
1693 default:
1694 set_error( STATUS_INVALID_PARAMETER );
1695 free( msg );
1696 break;
1699 release_object( thread );
1702 /* send a hardware message to a thread queue */
1703 DECL_HANDLER(send_hardware_message)
1705 struct message *msg;
1706 struct thread *thread = NULL;
1707 struct hardware_msg_data *data;
1708 struct thread_input *input = foreground_input;
1710 if (req->id)
1712 if (!(thread = get_thread_from_id( req->id ))) return;
1713 if (!thread->queue)
1715 set_error( STATUS_INVALID_PARAMETER );
1716 release_object( thread );
1717 return;
1719 input = thread->queue->input;
1720 reply->cursor = input->cursor;
1721 reply->count = input->cursor_count;
1724 if (!req->msg || !(data = mem_alloc( sizeof(*data) )))
1726 if (thread) release_object( thread );
1727 return;
1729 memset( data, 0, sizeof(*data) );
1730 data->x = req->x;
1731 data->y = req->y;
1732 data->info = req->info;
1734 if ((msg = mem_alloc( sizeof(*msg) )))
1736 msg->type = MSG_HARDWARE;
1737 msg->win = get_user_full_handle( req->win );
1738 msg->msg = req->msg;
1739 msg->wparam = req->wparam;
1740 msg->lparam = req->lparam;
1741 msg->time = req->time;
1742 msg->result = NULL;
1743 msg->data = data;
1744 msg->data_size = sizeof(*data);
1745 queue_hardware_message( input, msg, data );
1747 else free( data );
1749 if (thread) release_object( thread );
1752 /* post a quit message to the current queue */
1753 DECL_HANDLER(post_quit_message)
1755 struct msg_queue *queue = get_current_queue();
1757 if (!queue)
1758 return;
1760 queue->quit_message = 1;
1761 queue->exit_code = req->exit_code;
1762 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1765 /* get a message from the current queue */
1766 DECL_HANDLER(get_message)
1768 struct timer *timer;
1769 struct list *ptr;
1770 struct msg_queue *queue = get_current_queue();
1771 user_handle_t get_win = get_user_full_handle( req->get_win );
1772 unsigned int filter = req->flags >> 16;
1774 reply->active_hooks = get_active_hooks();
1776 if (!queue) return;
1777 queue->last_get_msg = current_time;
1778 if (!filter) filter = QS_ALLINPUT;
1780 /* first check for sent messages */
1781 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1783 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1784 receive_message( queue, msg, reply );
1785 return;
1788 /* clear changed bits so we can wait on them if we don't find a message */
1789 if (filter & QS_POSTMESSAGE)
1791 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
1792 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
1794 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
1795 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
1797 /* then check for posted messages */
1798 if ((filter & QS_POSTMESSAGE) &&
1799 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1800 return;
1802 /* only check for quit messages if not posted messages pending.
1803 * note: the quit message isn't filtered */
1804 if (get_quit_message( queue, req->flags, reply ))
1805 return;
1807 /* then check for any raw hardware message */
1808 if ((filter & QS_INPUT) &&
1809 filter_contains_hw_range( req->get_first, req->get_last ) &&
1810 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1811 return;
1813 /* now check for WM_PAINT */
1814 if ((filter & QS_PAINT) &&
1815 queue->paint_count &&
1816 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1817 (reply->win = find_window_to_repaint( get_win, current )))
1819 reply->type = MSG_POSTED;
1820 reply->msg = WM_PAINT;
1821 reply->wparam = 0;
1822 reply->lparam = 0;
1823 reply->time = get_tick_count();
1824 return;
1827 /* now check for timer */
1828 if ((filter & QS_TIMER) &&
1829 (timer = find_expired_timer( queue, get_win, req->get_first,
1830 req->get_last, (req->flags & PM_REMOVE) )))
1832 reply->type = MSG_POSTED;
1833 reply->win = timer->win;
1834 reply->msg = timer->msg;
1835 reply->wparam = timer->id;
1836 reply->lparam = timer->lparam;
1837 reply->time = get_tick_count();
1838 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
1839 set_event( current->process->idle_event );
1840 return;
1843 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
1844 queue->wake_mask = req->wake_mask;
1845 queue->changed_mask = req->changed_mask;
1846 set_error( STATUS_PENDING ); /* FIXME */
1850 /* reply to a sent message */
1851 DECL_HANDLER(reply_message)
1853 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1854 else if (current->queue->recv_result)
1855 reply_message( current->queue, req->result, 0, req->remove,
1856 get_req_data(), get_req_data_size() );
1860 /* accept the current hardware message */
1861 DECL_HANDLER(accept_hardware_message)
1863 if (current->queue)
1864 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1865 else
1866 set_error( STATUS_ACCESS_DENIED );
1870 /* retrieve the reply for the last message sent */
1871 DECL_HANDLER(get_message_reply)
1873 struct message_result *result;
1874 struct list *entry;
1875 struct msg_queue *queue = current->queue;
1877 if (queue)
1879 set_error( STATUS_PENDING );
1880 reply->result = 0;
1882 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1884 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1885 if (result->replied || req->cancel)
1887 if (result->replied)
1889 reply->result = result->result;
1890 set_error( result->error );
1891 if (result->data)
1893 data_size_t data_len = min( result->data_size, get_reply_max_size() );
1894 set_reply_data_ptr( result->data, data_len );
1895 result->data = NULL;
1896 result->data_size = 0;
1899 remove_result_from_sender( result );
1901 entry = list_head( &queue->send_result );
1902 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1903 else
1905 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1906 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1910 else set_error( STATUS_ACCESS_DENIED );
1914 /* set a window timer */
1915 DECL_HANDLER(set_win_timer)
1917 struct timer *timer;
1918 struct msg_queue *queue;
1919 struct thread *thread = NULL;
1920 user_handle_t win = 0;
1921 lparam_t id = req->id;
1923 if (req->win)
1925 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1927 set_error( STATUS_INVALID_HANDLE );
1928 return;
1930 if (thread->process != current->process)
1932 release_object( thread );
1933 set_error( STATUS_ACCESS_DENIED );
1934 return;
1936 queue = thread->queue;
1937 /* remove it if it existed already */
1938 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1940 else
1942 queue = get_current_queue();
1943 /* look for a timer with this id */
1944 if (id && (timer = find_timer( queue, 0, req->msg, id )))
1946 /* free and reuse id */
1947 free_timer( queue, timer );
1949 else
1951 /* find a free id for it */
1954 id = queue->next_timer_id;
1955 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
1957 while (find_timer( queue, 0, req->msg, id ));
1961 if ((timer = set_timer( queue, req->rate )))
1963 timer->win = win;
1964 timer->msg = req->msg;
1965 timer->id = id;
1966 timer->lparam = req->lparam;
1967 reply->id = id;
1969 if (thread) release_object( thread );
1972 /* kill a window timer */
1973 DECL_HANDLER(kill_win_timer)
1975 struct timer *timer;
1976 struct thread *thread;
1977 user_handle_t win = 0;
1979 if (req->win)
1981 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1983 set_error( STATUS_INVALID_HANDLE );
1984 return;
1986 if (thread->process != current->process)
1988 release_object( thread );
1989 set_error( STATUS_ACCESS_DENIED );
1990 return;
1993 else thread = (struct thread *)grab_object( current );
1995 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1996 free_timer( thread->queue, timer );
1997 else
1998 set_error( STATUS_INVALID_PARAMETER );
2000 release_object( thread );
2004 /* attach (or detach) thread inputs */
2005 DECL_HANDLER(attach_thread_input)
2007 struct thread *thread_from = get_thread_from_id( req->tid_from );
2008 struct thread *thread_to = get_thread_from_id( req->tid_to );
2010 if (!thread_from || !thread_to)
2012 if (thread_from) release_object( thread_from );
2013 if (thread_to) release_object( thread_to );
2014 return;
2016 if (thread_from != thread_to)
2018 if (req->attach) attach_thread_input( thread_from, thread_to );
2019 else
2021 if (thread_from->queue && thread_to->queue &&
2022 thread_from->queue->input == thread_to->queue->input)
2023 detach_thread_input( thread_from );
2024 else
2025 set_error( STATUS_ACCESS_DENIED );
2028 else set_error( STATUS_ACCESS_DENIED );
2029 release_object( thread_from );
2030 release_object( thread_to );
2034 /* get thread input data */
2035 DECL_HANDLER(get_thread_input)
2037 struct thread *thread = NULL;
2038 struct thread_input *input;
2040 if (req->tid)
2042 if (!(thread = get_thread_from_id( req->tid ))) return;
2043 input = thread->queue ? thread->queue->input : NULL;
2045 else input = foreground_input; /* get the foreground thread info */
2047 if (input)
2049 reply->focus = input->focus;
2050 reply->capture = input->capture;
2051 reply->active = input->active;
2052 reply->menu_owner = input->menu_owner;
2053 reply->move_size = input->move_size;
2054 reply->caret = input->caret;
2055 reply->cursor = input->cursor;
2056 reply->show_count = input->cursor_count;
2057 reply->rect = input->caret_rect;
2060 /* foreground window is active window of foreground thread */
2061 reply->foreground = foreground_input ? foreground_input->active : 0;
2062 if (thread) release_object( thread );
2066 /* retrieve queue keyboard state for a given thread */
2067 DECL_HANDLER(get_key_state)
2069 struct thread *thread;
2070 struct thread_input *input;
2072 if (!(thread = get_thread_from_id( req->tid ))) return;
2073 input = thread->queue ? thread->queue->input : NULL;
2074 if (input)
2076 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
2077 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
2079 release_object( thread );
2083 /* set queue keyboard state for a given thread */
2084 DECL_HANDLER(set_key_state)
2086 struct thread *thread = NULL;
2087 struct thread_input *input;
2089 if (!(thread = get_thread_from_id( req->tid ))) return;
2090 input = thread->queue ? thread->queue->input : NULL;
2091 if (input)
2093 data_size_t size = min( sizeof(input->keystate), get_req_data_size() );
2094 if (size) memcpy( input->keystate, get_req_data(), size );
2096 release_object( thread );
2100 /* set the system foreground window */
2101 DECL_HANDLER(set_foreground_window)
2103 struct thread *thread;
2104 struct msg_queue *queue = get_current_queue();
2106 reply->previous = foreground_input ? foreground_input->active : 0;
2107 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
2108 reply->send_msg_new = FALSE;
2110 if (is_top_level_window( req->handle ) &&
2111 ((thread = get_window_thread( req->handle ))))
2113 foreground_input = thread->queue->input;
2114 reply->send_msg_new = (foreground_input != queue->input);
2115 release_object( thread );
2117 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2121 /* set the current thread focus window */
2122 DECL_HANDLER(set_focus_window)
2124 struct msg_queue *queue = get_current_queue();
2126 reply->previous = 0;
2127 if (queue && check_queue_input_window( queue, req->handle ))
2129 reply->previous = queue->input->focus;
2130 queue->input->focus = get_user_full_handle( req->handle );
2135 /* set the current thread active window */
2136 DECL_HANDLER(set_active_window)
2138 struct msg_queue *queue = get_current_queue();
2140 reply->previous = 0;
2141 if (queue && check_queue_input_window( queue, req->handle ))
2143 if (!req->handle || make_window_active( req->handle ))
2145 reply->previous = queue->input->active;
2146 queue->input->active = get_user_full_handle( req->handle );
2148 else set_error( STATUS_INVALID_HANDLE );
2153 /* set the current thread capture window */
2154 DECL_HANDLER(set_capture_window)
2156 struct msg_queue *queue = get_current_queue();
2158 reply->previous = reply->full_handle = 0;
2159 if (queue && check_queue_input_window( queue, req->handle ))
2161 struct thread_input *input = queue->input;
2163 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2164 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2166 set_error(STATUS_ACCESS_DENIED);
2167 return;
2169 reply->previous = input->capture;
2170 input->capture = get_user_full_handle( req->handle );
2171 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2172 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2173 reply->full_handle = input->capture;
2178 /* Set the current thread caret window */
2179 DECL_HANDLER(set_caret_window)
2181 struct msg_queue *queue = get_current_queue();
2183 reply->previous = 0;
2184 if (queue && check_queue_input_window( queue, req->handle ))
2186 struct thread_input *input = queue->input;
2188 reply->previous = input->caret;
2189 reply->old_rect = input->caret_rect;
2190 reply->old_hide = input->caret_hide;
2191 reply->old_state = input->caret_state;
2193 set_caret_window( input, get_user_full_handle(req->handle) );
2194 input->caret_rect.right = input->caret_rect.left + req->width;
2195 input->caret_rect.bottom = input->caret_rect.top + req->height;
2200 /* Set the current thread caret information */
2201 DECL_HANDLER(set_caret_info)
2203 struct msg_queue *queue = get_current_queue();
2204 struct thread_input *input;
2206 if (!queue) return;
2207 input = queue->input;
2208 reply->full_handle = input->caret;
2209 reply->old_rect = input->caret_rect;
2210 reply->old_hide = input->caret_hide;
2211 reply->old_state = input->caret_state;
2213 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2215 set_error( STATUS_ACCESS_DENIED );
2216 return;
2218 if (req->flags & SET_CARET_POS)
2220 input->caret_rect.right += req->x - input->caret_rect.left;
2221 input->caret_rect.bottom += req->y - input->caret_rect.top;
2222 input->caret_rect.left = req->x;
2223 input->caret_rect.top = req->y;
2225 if (req->flags & SET_CARET_HIDE)
2227 input->caret_hide += req->hide;
2228 if (input->caret_hide < 0) input->caret_hide = 0;
2230 if (req->flags & SET_CARET_STATE)
2232 if (req->state == -1) input->caret_state = !input->caret_state;
2233 else input->caret_state = !!req->state;
2238 /* get the time of the last input event */
2239 DECL_HANDLER(get_last_input_time)
2241 reply->time = last_input_time;
2244 /* set/get the current cursor */
2245 DECL_HANDLER(set_cursor)
2247 struct msg_queue *queue = get_current_queue();
2248 struct thread_input *input;
2250 if (!queue) return;
2251 input = queue->input;
2253 reply->prev_handle = input->cursor;
2254 reply->prev_count = input->cursor_count;
2256 if (req->flags & SET_CURSOR_HANDLE)
2258 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
2260 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
2261 return;
2263 input->cursor = req->handle;
2266 if (req->flags & SET_CURSOR_COUNT)
2268 queue->cursor_count += req->show_count;
2269 input->cursor_count += req->show_count;