ntdll: Check file size when mapping image sections to avoid SIGBUS errors.
[wine/multimedia.git] / server / queue.c
blob3bc64d485b97424362d78c8eafcbf4a040961693
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 result; /* reply result */
60 unsigned int error; /* error code to pass back to sender */
61 struct message *callback_msg; /* message to queue for callback */
62 void *data; /* message reply data */
63 unsigned int data_size; /* size of message reply data */
64 struct timeout_user *timeout; /* result timeout */
67 struct message
69 struct list entry; /* entry in message list */
70 enum message_type type; /* message type */
71 user_handle_t win; /* window handle */
72 unsigned int msg; /* message code */
73 unsigned int wparam; /* parameters */
74 unsigned int lparam; /* parameters */
75 int x; /* x position */
76 int y; /* y position */
77 unsigned int time; /* message time */
78 unsigned int info; /* extra info */
79 user_handle_t hook; /* winevent hook handle */
80 void *hook_proc; /* winevent hook proc address */
81 void *data; /* message data for sent messages */
82 unsigned int data_size; /* size of message data */
83 unsigned int unique_id; /* unique id for nested hw message waits */
84 struct message_result *result; /* result in sender queue */
87 struct timer
89 struct list entry; /* entry in timer list */
90 struct timeval when; /* next expiration */
91 unsigned int rate; /* timer rate in ms */
92 user_handle_t win; /* window handle */
93 unsigned int msg; /* message to post */
94 unsigned int id; /* timer id */
95 unsigned int lparam; /* lparam for message */
98 struct thread_input
100 struct object obj; /* object header */
101 struct desktop *desktop; /* desktop that this thread input belongs to */
102 user_handle_t focus; /* focus window */
103 user_handle_t capture; /* capture window */
104 user_handle_t active; /* active window */
105 user_handle_t menu_owner; /* current menu owner window */
106 user_handle_t move_size; /* current moving/resizing window */
107 user_handle_t caret; /* caret window */
108 rectangle_t caret_rect; /* caret rectangle */
109 int caret_hide; /* caret hide count */
110 int caret_state; /* caret on/off state */
111 struct list msg_list; /* list of hardware messages */
112 unsigned char keystate[256]; /* state of each key */
115 struct msg_queue
117 struct object obj; /* object header */
118 unsigned int wake_bits; /* wakeup bits */
119 unsigned int wake_mask; /* wakeup mask */
120 unsigned int changed_bits; /* changed wakeup bits */
121 unsigned int changed_mask; /* changed wakeup mask */
122 int paint_count; /* pending paint messages count */
123 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
124 struct list send_result; /* stack of sent messages waiting for result */
125 struct list callback_result; /* list of callback messages waiting for result */
126 struct message_result *recv_result; /* stack of received messages waiting for result */
127 struct list pending_timers; /* list of pending timers */
128 struct list expired_timers; /* list of expired timers */
129 unsigned int next_timer_id; /* id for the next timer with a 0 window */
130 struct timeout_user *timeout; /* timeout for next timer to expire */
131 struct thread_input *input; /* thread input descriptor */
132 struct hook_table *hooks; /* hook table */
133 struct timeval last_get_msg; /* time of last get message call */
136 static void msg_queue_dump( struct object *obj, int verbose );
137 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
138 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
139 static int msg_queue_signaled( struct object *obj, struct thread *thread );
140 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
141 static void msg_queue_destroy( struct object *obj );
142 static void thread_input_dump( struct object *obj, int verbose );
143 static void thread_input_destroy( struct object *obj );
144 static void timer_callback( void *private );
146 static const struct object_ops msg_queue_ops =
148 sizeof(struct msg_queue), /* size */
149 msg_queue_dump, /* dump */
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 no_lookup_name, /* lookup_name */
158 no_close_handle, /* close_handle */
159 msg_queue_destroy /* destroy */
163 static const struct object_ops thread_input_ops =
165 sizeof(struct thread_input), /* size */
166 thread_input_dump, /* dump */
167 no_add_queue, /* add_queue */
168 NULL, /* remove_queue */
169 NULL, /* signaled */
170 NULL, /* satisfied */
171 no_signal, /* signal */
172 no_get_fd, /* get_fd */
173 no_map_access, /* map_access */
174 no_lookup_name, /* lookup_name */
175 no_close_handle, /* close_handle */
176 thread_input_destroy /* destroy */
179 /* pointer to input structure of foreground thread */
180 static struct thread_input *foreground_input;
181 static unsigned int last_input_time;
184 /* set the caret window in a given thread input */
185 static void set_caret_window( struct thread_input *input, user_handle_t win )
187 if (!win || win != input->caret)
189 input->caret_rect.left = 0;
190 input->caret_rect.top = 0;
191 input->caret_rect.right = 0;
192 input->caret_rect.bottom = 0;
194 input->caret = win;
195 input->caret_hide = 1;
196 input->caret_state = 0;
199 /* create a thread input object */
200 static struct thread_input *create_thread_input( struct thread *thread )
202 struct thread_input *input;
204 if ((input = alloc_object( &thread_input_ops )))
206 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
208 free( input );
209 return NULL;
211 input->focus = 0;
212 input->capture = 0;
213 input->active = 0;
214 input->menu_owner = 0;
215 input->move_size = 0;
216 list_init( &input->msg_list );
217 set_caret_window( input, 0 );
218 memset( input->keystate, 0, sizeof(input->keystate) );
220 return input;
223 /* release the thread input data of a given thread */
224 static inline void release_thread_input( struct thread *thread )
226 struct thread_input *input = thread->queue->input;
228 if (!input) return;
229 release_object( input );
230 thread->queue->input = NULL;
233 /* create a message queue object */
234 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
236 struct msg_queue *queue;
237 int i;
239 if (!input && !(input = create_thread_input( thread ))) return NULL;
240 if ((queue = alloc_object( &msg_queue_ops )))
242 queue->wake_bits = 0;
243 queue->wake_mask = 0;
244 queue->changed_bits = 0;
245 queue->changed_mask = 0;
246 queue->paint_count = 0;
247 queue->recv_result = NULL;
248 queue->next_timer_id = 1;
249 queue->timeout = NULL;
250 queue->input = (struct thread_input *)grab_object( input );
251 queue->hooks = NULL;
252 gettimeofday( &queue->last_get_msg, NULL );
253 list_init( &queue->send_result );
254 list_init( &queue->callback_result );
255 list_init( &queue->pending_timers );
256 list_init( &queue->expired_timers );
257 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
259 thread->queue = queue;
260 if (!thread->process->queue)
261 thread->process->queue = (struct msg_queue *)grab_object( queue );
263 release_object( input );
264 return queue;
267 /* free the message queue of a thread at thread exit */
268 void free_msg_queue( struct thread *thread )
270 struct process *process = thread->process;
272 remove_thread_hooks( thread );
273 if (!thread->queue) return;
274 if (process->queue == thread->queue) /* is it the process main queue? */
276 release_object( process->queue );
277 process->queue = NULL;
278 if (process->idle_event)
280 set_event( process->idle_event );
281 release_object( process->idle_event );
282 process->idle_event = NULL;
285 release_object( thread->queue );
286 thread->queue = NULL;
289 /* get the hook table for a given thread */
290 struct hook_table *get_queue_hooks( struct thread *thread )
292 if (!thread->queue) return NULL;
293 return thread->queue->hooks;
296 /* set the hook table for a given thread, allocating the queue if needed */
297 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
299 struct msg_queue *queue = thread->queue;
300 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
301 if (queue->hooks) release_object( queue->hooks );
302 queue->hooks = hooks;
305 /* check the queue status */
306 inline static int is_signaled( struct msg_queue *queue )
308 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
311 /* set some queue bits */
312 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
314 queue->wake_bits |= bits;
315 queue->changed_bits |= bits;
316 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
319 /* clear some queue bits */
320 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
322 queue->wake_bits &= ~bits;
323 queue->changed_bits &= ~bits;
326 /* check whether msg is a keyboard message */
327 inline static int is_keyboard_msg( struct message *msg )
329 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
332 /* check if message is matched by the filter */
333 inline static int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
335 return (msg >= first && msg <= last);
338 /* check whether a message filter contains at least one potential hardware message */
339 inline static int filter_contains_hw_range( unsigned int first, unsigned int last )
341 /* hardware message ranges are (in numerical order):
342 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
343 * WM_KEYFIRST .. WM_KEYLAST
344 * WM_MOUSEFIRST .. WM_MOUSELAST
346 if (last < WM_NCMOUSEFIRST) return 0;
347 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
348 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
349 if (first > WM_MOUSELAST) return 0;
350 return 1;
353 /* get the QS_* bit corresponding to a given hardware message */
354 inline static int get_hardware_msg_bit( struct message *msg )
356 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
357 if (is_keyboard_msg( msg )) return QS_KEY;
358 return QS_MOUSEBUTTON;
361 /* get the current thread queue, creating it if needed */
362 inline static struct msg_queue *get_current_queue(void)
364 struct msg_queue *queue = current->queue;
365 if (!queue) queue = create_msg_queue( current, NULL );
366 return queue;
369 /* get a (pseudo-)unique id to tag hardware messages */
370 inline static unsigned int get_unique_id(void)
372 static unsigned int id;
373 if (!++id) id = 1; /* avoid an id of 0 */
374 return id;
377 /* try to merge a message with the last in the list; return 1 if successful */
378 static int merge_message( struct thread_input *input, const struct message *msg )
380 struct message *prev;
381 struct list *ptr = list_tail( &input->msg_list );
383 if (!ptr) return 0;
384 prev = LIST_ENTRY( ptr, struct message, entry );
385 if (prev->unique_id) return 0;
386 if (prev->result) return 0;
387 if (prev->win != msg->win) return 0;
388 if (prev->msg != msg->msg) return 0;
389 if (prev->type != msg->type) return 0;
390 /* now we can merge it */
391 prev->wparam = msg->wparam;
392 prev->lparam = msg->lparam;
393 prev->x = msg->x;
394 prev->y = msg->y;
395 prev->time = msg->time;
396 prev->info = msg->info;
397 return 1;
400 /* free a result structure */
401 static void free_result( struct message_result *result )
403 if (result->timeout) remove_timeout_user( result->timeout );
404 if (result->data) free( result->data );
405 if (result->callback_msg) free( result->callback_msg );
406 free( result );
409 /* remove the result from the sender list it is on */
410 static inline void remove_result_from_sender( struct message_result *result )
412 assert( result->sender );
414 list_remove( &result->sender_entry );
415 result->sender = NULL;
416 if (!result->receiver) free_result( result );
419 /* store the message result in the appropriate structure */
420 static void store_message_result( struct message_result *res, unsigned int result,
421 unsigned int error )
423 res->result = result;
424 res->error = error;
425 res->replied = 1;
426 if (res->timeout)
428 remove_timeout_user( res->timeout );
429 res->timeout = NULL;
431 if (res->sender)
433 if (res->callback_msg)
435 /* queue the callback message in the sender queue */
436 res->callback_msg->lparam = result;
437 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
438 set_queue_bits( res->sender, QS_SENDMESSAGE );
439 res->callback_msg = NULL;
440 remove_result_from_sender( res );
442 else
444 /* wake sender queue if waiting on this result */
445 if (list_head(&res->sender->send_result) == &res->sender_entry)
446 set_queue_bits( res->sender, QS_SMRESULT );
452 /* free a message when deleting a queue or window */
453 static void free_message( struct message *msg )
455 struct message_result *result = msg->result;
456 if (result)
458 result->msg = NULL;
459 if (result->sender)
461 result->receiver = NULL;
462 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
464 else free_result( result );
466 if (msg->data) free( msg->data );
467 free( msg );
470 /* remove (and free) a message from a message list */
471 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
472 enum message_kind kind )
474 list_remove( &msg->entry );
475 switch(kind)
477 case SEND_MESSAGE:
478 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
479 break;
480 case POST_MESSAGE:
481 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
482 break;
484 free_message( msg );
487 /* message timed out without getting a reply */
488 static void result_timeout( void *private )
490 struct message_result *result = private;
492 assert( !result->replied );
494 result->timeout = NULL;
496 if (result->msg) /* not received yet */
498 struct message *msg = result->msg;
500 result->msg = NULL;
501 msg->result = NULL;
502 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
503 result->receiver = NULL;
504 if (!result->sender)
506 free_result( result );
507 return;
511 store_message_result( result, 0, STATUS_TIMEOUT );
514 /* allocate and fill a message result structure */
515 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
516 struct msg_queue *recv_queue,
517 struct message *msg, int timeout,
518 void *callback, unsigned int callback_data )
520 struct message_result *result = mem_alloc( sizeof(*result) );
521 if (result)
523 result->msg = msg;
524 result->sender = send_queue;
525 result->receiver = recv_queue;
526 result->replied = 0;
527 result->data = NULL;
528 result->data_size = 0;
529 result->timeout = NULL;
531 if (msg->type == MSG_CALLBACK)
533 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
534 if (!callback_msg)
536 free( result );
537 return NULL;
539 callback_msg->type = MSG_CALLBACK_RESULT;
540 callback_msg->win = msg->win;
541 callback_msg->msg = msg->msg;
542 callback_msg->wparam = (unsigned int)callback;
543 callback_msg->lparam = 0;
544 callback_msg->time = get_tick_count();
545 callback_msg->x = 0;
546 callback_msg->y = 0;
547 callback_msg->info = callback_data;
548 callback_msg->hook = 0;
549 callback_msg->hook_proc = NULL;
550 callback_msg->result = NULL;
551 callback_msg->data = NULL;
552 callback_msg->data_size = 0;
554 result->callback_msg = callback_msg;
555 list_add_head( &send_queue->callback_result, &result->sender_entry );
557 else
559 result->callback_msg = NULL;
560 list_add_head( &send_queue->send_result, &result->sender_entry );
563 if (timeout)
565 struct timeval when;
566 gettimeofday( &when, NULL );
567 add_timeout( &when, timeout );
568 result->timeout = add_timeout_user( &when, result_timeout, result );
571 return result;
574 /* receive a message, removing it from the sent queue */
575 static void receive_message( struct msg_queue *queue, struct message *msg,
576 struct get_message_reply *reply )
578 struct message_result *result = msg->result;
580 reply->total = msg->data_size;
581 if (msg->data_size > get_reply_max_size())
583 set_error( STATUS_BUFFER_OVERFLOW );
584 return;
586 reply->type = msg->type;
587 reply->win = msg->win;
588 reply->msg = msg->msg;
589 reply->wparam = msg->wparam;
590 reply->lparam = msg->lparam;
591 reply->x = msg->x;
592 reply->y = msg->y;
593 reply->time = msg->time;
594 reply->info = msg->info;
595 reply->hook = msg->hook;
596 reply->hook_proc = msg->hook_proc;
598 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
600 list_remove( &msg->entry );
601 /* put the result on the receiver result stack */
602 if (result)
604 result->msg = NULL;
605 result->recv_next = queue->recv_result;
606 queue->recv_result = result;
608 free( msg );
609 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
612 /* set the result of the current received message */
613 static void reply_message( struct msg_queue *queue, unsigned int result,
614 unsigned int error, int remove, const void *data, size_t len )
616 struct message_result *res = queue->recv_result;
618 if (remove)
620 queue->recv_result = res->recv_next;
621 res->receiver = NULL;
622 if (!res->sender) /* no one waiting for it */
624 free_result( res );
625 return;
628 if (!res->replied)
630 if (len && (res->data = memdup( data, len ))) res->data_size = len;
631 store_message_result( res, result, error );
635 /* retrieve a posted message */
636 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
637 unsigned int first, unsigned int last, unsigned int flags,
638 struct get_message_reply *reply )
640 struct message *msg;
642 /* check against the filters */
643 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
645 if (msg->msg == WM_QUIT) goto found; /* WM_QUIT is never filtered */
646 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
647 if (!check_msg_filter( msg->msg, first, last )) continue;
648 goto found; /* found one */
650 return 0;
652 /* return it to the app */
653 found:
654 reply->total = msg->data_size;
655 if (msg->data_size > get_reply_max_size())
657 set_error( STATUS_BUFFER_OVERFLOW );
658 return 1;
660 reply->type = msg->type;
661 reply->win = msg->win;
662 reply->msg = msg->msg;
663 reply->wparam = msg->wparam;
664 reply->lparam = msg->lparam;
665 reply->x = msg->x;
666 reply->y = msg->y;
667 reply->time = msg->time;
668 reply->info = msg->info;
670 if (flags & GET_MSG_REMOVE)
672 if (msg->data)
674 set_reply_data_ptr( msg->data, msg->data_size );
675 msg->data = NULL;
676 msg->data_size = 0;
678 remove_queue_message( queue, msg, POST_MESSAGE );
680 else if (msg->data) set_reply_data( msg->data, msg->data_size );
682 return 1;
685 /* empty a message list and free all the messages */
686 static void empty_msg_list( struct list *list )
688 struct list *ptr;
690 while ((ptr = list_head( list )) != NULL)
692 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
693 list_remove( &msg->entry );
694 free_message( msg );
698 /* cleanup all pending results when deleting a queue */
699 static void cleanup_results( struct msg_queue *queue )
701 struct list *entry;
703 while ((entry = list_head( &queue->send_result )) != NULL)
705 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
708 while ((entry = list_head( &queue->callback_result )) != NULL)
710 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
713 while (queue->recv_result)
714 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
717 /* check if the thread owning the queue is hung (not checking for messages) */
718 static int is_queue_hung( struct msg_queue *queue )
720 struct timeval now;
721 struct wait_queue_entry *entry;
723 gettimeofday( &now, NULL );
724 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
725 return 0; /* less than 5 seconds since last get message -> not hung */
727 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
729 if (entry->thread->queue == queue)
730 return 0; /* thread is waiting on queue -> not hung */
732 return 1;
735 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
737 struct msg_queue *queue = (struct msg_queue *)obj;
738 struct process *process = entry->thread->process;
740 /* a thread can only wait on its own queue */
741 if (entry->thread->queue != queue)
743 set_error( STATUS_ACCESS_DENIED );
744 return 0;
746 /* if waiting on the main process queue, set the idle event */
747 if (process->queue == queue)
749 if (process->idle_event) set_event( process->idle_event );
751 add_queue( obj, entry );
752 return 1;
755 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
757 struct msg_queue *queue = (struct msg_queue *)obj;
758 struct process *process = entry->thread->process;
760 remove_queue( obj, entry );
762 assert( entry->thread->queue == queue );
764 /* if waiting on the main process queue, reset the idle event */
765 if (process->queue == queue)
767 if (process->idle_event) reset_event( process->idle_event );
771 static void msg_queue_dump( struct object *obj, int verbose )
773 struct msg_queue *queue = (struct msg_queue *)obj;
774 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
775 queue->wake_bits, queue->wake_mask );
778 static int msg_queue_signaled( struct object *obj, struct thread *thread )
780 struct msg_queue *queue = (struct msg_queue *)obj;
781 return is_signaled( queue );
784 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
786 struct msg_queue *queue = (struct msg_queue *)obj;
787 queue->wake_mask = 0;
788 queue->changed_mask = 0;
789 return 0; /* Not abandoned */
792 static void msg_queue_destroy( struct object *obj )
794 struct msg_queue *queue = (struct msg_queue *)obj;
795 struct list *ptr;
796 int i;
798 cleanup_results( queue );
799 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
801 while ((ptr = list_head( &queue->pending_timers )))
803 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
804 list_remove( &timer->entry );
805 free( timer );
807 while ((ptr = list_head( &queue->expired_timers )))
809 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
810 list_remove( &timer->entry );
811 free( timer );
813 if (queue->timeout) remove_timeout_user( queue->timeout );
814 if (queue->input) release_object( queue->input );
815 if (queue->hooks) release_object( queue->hooks );
818 static void thread_input_dump( struct object *obj, int verbose )
820 struct thread_input *input = (struct thread_input *)obj;
821 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
822 input->focus, input->capture, input->active );
825 static void thread_input_destroy( struct object *obj )
827 struct thread_input *input = (struct thread_input *)obj;
829 if (foreground_input == input) foreground_input = NULL;
830 empty_msg_list( &input->msg_list );
831 release_object( input->desktop );
834 /* fix the thread input data when a window is destroyed */
835 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
837 struct thread_input *input = queue->input;
839 if (window == input->focus) input->focus = 0;
840 if (window == input->capture) input->capture = 0;
841 if (window == input->active) input->active = 0;
842 if (window == input->menu_owner) input->menu_owner = 0;
843 if (window == input->move_size) input->move_size = 0;
844 if (window == input->caret) set_caret_window( input, 0 );
847 /* check if the specified window can be set in the input data of a given queue */
848 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
850 struct thread *thread;
851 int ret = 0;
853 if (!window) return 1; /* we can always clear the data */
855 if ((thread = get_window_thread( window )))
857 ret = (queue->input == thread->queue->input);
858 if (!ret) set_error( STATUS_ACCESS_DENIED );
859 release_object( thread );
861 else set_error( STATUS_INVALID_HANDLE );
863 return ret;
866 /* make sure the specified thread has a queue */
867 int init_thread_queue( struct thread *thread )
869 if (thread->queue) return 1;
870 return (create_msg_queue( thread, NULL ) != NULL);
873 /* attach two thread input data structures */
874 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
876 struct desktop *desktop;
877 struct thread_input *input;
879 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
880 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
881 input = (struct thread_input *)grab_object( thread_to->queue->input );
882 if (input->desktop != desktop)
884 set_error( STATUS_ACCESS_DENIED );
885 release_object( input );
886 release_object( desktop );
887 return 0;
889 release_object( desktop );
891 if (thread_from->queue)
893 release_thread_input( thread_from );
894 thread_from->queue->input = input;
896 else
898 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
900 memset( input->keystate, 0, sizeof(input->keystate) );
901 return 1;
904 /* detach two thread input data structures */
905 void detach_thread_input( struct thread *thread_from )
907 struct thread_input *input;
909 if ((input = create_thread_input( thread_from )))
911 release_thread_input( thread_from );
912 thread_from->queue->input = input;
917 /* set the next timer to expire */
918 static void set_next_timer( struct msg_queue *queue )
920 struct list *ptr;
922 if (queue->timeout)
924 remove_timeout_user( queue->timeout );
925 queue->timeout = NULL;
927 if ((ptr = list_head( &queue->pending_timers )))
929 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
930 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
932 /* set/clear QS_TIMER bit */
933 if (list_empty( &queue->expired_timers ))
934 clear_queue_bits( queue, QS_TIMER );
935 else
936 set_queue_bits( queue, QS_TIMER );
939 /* find a timer from its window and id */
940 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
941 unsigned int msg, unsigned int id )
943 struct list *ptr;
945 /* we need to search both lists */
947 LIST_FOR_EACH( ptr, &queue->pending_timers )
949 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
950 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
952 LIST_FOR_EACH( ptr, &queue->expired_timers )
954 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
955 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
957 return NULL;
960 /* callback for the next timer expiration */
961 static void timer_callback( void *private )
963 struct msg_queue *queue = private;
964 struct list *ptr;
966 queue->timeout = NULL;
967 /* move on to the next timer */
968 ptr = list_head( &queue->pending_timers );
969 list_remove( ptr );
970 list_add_tail( &queue->expired_timers, ptr );
971 set_next_timer( queue );
974 /* link a timer at its rightful place in the queue list */
975 static void link_timer( struct msg_queue *queue, struct timer *timer )
977 struct list *ptr;
979 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
981 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
982 if (!time_before( &t->when, &timer->when )) break;
984 list_add_before( ptr, &timer->entry );
987 /* remove a timer from the queue timer list and free it */
988 static void free_timer( struct msg_queue *queue, struct timer *timer )
990 list_remove( &timer->entry );
991 free( timer );
992 set_next_timer( queue );
995 /* restart an expired timer */
996 static void restart_timer( struct msg_queue *queue, struct timer *timer )
998 struct timeval now;
1000 list_remove( &timer->entry );
1001 gettimeofday( &now, NULL );
1002 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
1003 link_timer( queue, timer );
1004 set_next_timer( queue );
1007 /* find an expired timer matching the filtering parameters */
1008 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1009 unsigned int get_first, unsigned int get_last,
1010 int remove )
1012 struct list *ptr;
1014 LIST_FOR_EACH( ptr, &queue->expired_timers )
1016 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1017 if (win && timer->win != win) continue;
1018 if (check_msg_filter( timer->msg, get_first, get_last ))
1020 if (remove) restart_timer( queue, timer );
1021 return timer;
1024 return NULL;
1027 /* add a timer */
1028 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1030 struct timer *timer = mem_alloc( sizeof(*timer) );
1031 if (timer)
1033 timer->rate = max( rate, 1 );
1034 gettimeofday( &timer->when, NULL );
1035 add_timeout( &timer->when, rate );
1036 link_timer( queue, timer );
1037 /* check if we replaced the next timer */
1038 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1040 return timer;
1043 /* change the input key state for a given key */
1044 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1046 if (down)
1048 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1049 input->keystate[key] |= 0x80;
1051 else input->keystate[key] &= ~0x80;
1054 /* update the input key state for a keyboard message */
1055 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1057 unsigned char key;
1058 int down = 0, extended;
1060 switch (msg->msg)
1062 case WM_LBUTTONDOWN:
1063 down = 1;
1064 /* fall through */
1065 case WM_LBUTTONUP:
1066 set_input_key_state( input, VK_LBUTTON, down );
1067 break;
1068 case WM_MBUTTONDOWN:
1069 down = 1;
1070 /* fall through */
1071 case WM_MBUTTONUP:
1072 set_input_key_state( input, VK_MBUTTON, down );
1073 break;
1074 case WM_RBUTTONDOWN:
1075 down = 1;
1076 /* fall through */
1077 case WM_RBUTTONUP:
1078 set_input_key_state( input, VK_RBUTTON, down );
1079 break;
1080 case WM_XBUTTONDOWN:
1081 down = 1;
1082 /* fall through */
1083 case WM_XBUTTONUP:
1084 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1085 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1086 break;
1087 case WM_KEYDOWN:
1088 case WM_SYSKEYDOWN:
1089 down = 1;
1090 /* fall through */
1091 case WM_KEYUP:
1092 case WM_SYSKEYUP:
1093 key = (unsigned char)msg->wparam;
1094 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1095 set_input_key_state( input, key, down );
1096 switch(key)
1098 case VK_SHIFT:
1099 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1100 break;
1101 case VK_CONTROL:
1102 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1103 break;
1104 case VK_MENU:
1105 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1106 break;
1108 break;
1112 /* release the hardware message currently being processed by the given thread */
1113 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1114 int remove, user_handle_t new_win )
1116 struct thread_input *input = queue->input;
1117 struct message *msg;
1119 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1121 if (msg->unique_id == hw_id) break;
1123 if (&msg->entry == &input->msg_list) return; /* not found */
1125 /* clear the queue bit for that message */
1126 if (remove || new_win)
1128 struct message *other;
1129 int clr_bit;
1131 clr_bit = get_hardware_msg_bit( msg );
1132 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1134 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1136 clr_bit = 0;
1137 break;
1140 if (clr_bit) clear_queue_bits( queue, clr_bit );
1143 if (new_win) /* set the new window */
1145 struct thread *owner = get_window_thread( new_win );
1146 if (owner)
1148 if (owner->queue->input == input)
1150 msg->win = new_win;
1151 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1152 remove = 0;
1154 release_object( owner );
1157 if (remove)
1159 update_input_key_state( input, msg );
1160 list_remove( &msg->entry );
1161 free_message( msg );
1165 /* find the window that should receive a given hardware message */
1166 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1167 unsigned int *msg_code )
1169 user_handle_t win = 0;
1171 *msg_code = msg->msg;
1172 if (is_keyboard_msg( msg ))
1174 if (input && !(win = input->focus))
1176 win = input->active;
1177 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1180 else /* mouse message */
1182 if (!input || !(win = input->capture))
1184 if (!(win = msg->win) || !is_window_visible( win ))
1186 if (input) win = window_from_point( input->desktop, msg->x, msg->y );
1190 return win;
1193 /* queue a hardware message into a given thread input */
1194 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1196 user_handle_t win;
1197 struct thread *thread;
1198 struct thread_input *input;
1199 unsigned int msg_code;
1201 last_input_time = get_tick_count();
1203 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1204 if (!win || !(thread = get_window_thread(win)))
1206 free( msg );
1207 return;
1209 input = thread->queue->input;
1211 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1212 else
1214 msg->unique_id = 0; /* will be set once we return it to the app */
1215 list_add_tail( &input->msg_list, &msg->entry );
1216 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1218 release_object( thread );
1221 /* check message filter for a hardware message */
1222 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1223 user_handle_t filter_win, unsigned int first, unsigned int last )
1225 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1227 /* we can only test the window for a keyboard message since the
1228 * dest window for a mouse message depends on hittest */
1229 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1230 return 0;
1231 /* the message code is final for a keyboard message, we can simply check it */
1232 return check_msg_filter( msg_code, first, last );
1234 else /* mouse message */
1236 /* we need to check all possible values that the message can have in the end */
1238 if (check_msg_filter( msg_code, first, last )) return 1;
1239 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1241 /* all other messages can become non-client messages */
1242 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1244 /* clicks can become double-clicks or non-client double-clicks */
1245 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1246 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1248 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1249 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1251 return 0;
1256 /* find a hardware message for the given queue */
1257 static int get_hardware_message( struct thread *thread, int hw_id, user_handle_t filter_win,
1258 unsigned int first, unsigned int last, struct get_message_reply *reply )
1260 struct thread_input *input = thread->queue->input;
1261 struct thread *win_thread;
1262 struct list *ptr;
1263 user_handle_t win;
1264 int clear_bits, got_one = 0;
1265 unsigned int msg_code;
1267 ptr = list_head( &input->msg_list );
1268 if (hw_id)
1270 while (ptr)
1272 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1273 if (msg->unique_id == hw_id) break;
1274 ptr = list_next( &input->msg_list, ptr );
1276 if (!ptr) ptr = list_head( &input->msg_list );
1277 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1280 if (ptr == list_head( &input->msg_list ))
1281 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1282 else
1283 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1285 while (ptr)
1287 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1288 ptr = list_next( &input->msg_list, ptr );
1289 win = find_hardware_message_window( input, msg, &msg_code );
1290 if (!win || !(win_thread = get_window_thread( win )))
1292 /* no window at all, remove it */
1293 update_input_key_state( input, msg );
1294 list_remove( &msg->entry );
1295 free_message( msg );
1296 continue;
1298 if (win_thread != thread)
1300 if (win_thread->queue->input == input)
1302 /* wake the other thread */
1303 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1304 got_one = 1;
1306 else
1308 /* for another thread input, drop it */
1309 update_input_key_state( input, msg );
1310 list_remove( &msg->entry );
1311 free_message( msg );
1313 release_object( win_thread );
1314 continue;
1316 release_object( win_thread );
1318 /* if we already got a message for another thread, or if it doesn't
1319 * match the filter we skip it */
1320 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1322 clear_bits &= ~get_hardware_msg_bit( msg );
1323 continue;
1325 /* now we can return it */
1326 if (!msg->unique_id) msg->unique_id = get_unique_id();
1327 reply->type = MSG_HARDWARE;
1328 reply->win = win;
1329 reply->msg = msg_code;
1330 reply->wparam = msg->wparam;
1331 reply->lparam = msg->lparam;
1332 reply->x = msg->x;
1333 reply->y = msg->y;
1334 reply->time = msg->time;
1335 reply->info = msg->info;
1336 reply->hw_id = msg->unique_id;
1337 return 1;
1339 /* nothing found, clear the hardware queue bits */
1340 clear_queue_bits( thread->queue, clear_bits );
1341 return 0;
1344 /* increment (or decrement if 'incr' is negative) the queue paint count */
1345 void inc_queue_paint_count( struct thread *thread, int incr )
1347 struct msg_queue *queue = thread->queue;
1349 assert( queue );
1351 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1353 if (queue->paint_count)
1354 set_queue_bits( queue, QS_PAINT );
1355 else
1356 clear_queue_bits( queue, QS_PAINT );
1360 /* remove all messages and timers belonging to a certain window */
1361 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1363 struct msg_queue *queue = thread->queue;
1364 struct list *ptr;
1365 int i;
1367 if (!queue) return;
1369 /* remove timers */
1371 ptr = list_head( &queue->pending_timers );
1372 while (ptr)
1374 struct list *next = list_next( &queue->pending_timers, ptr );
1375 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1376 if (timer->win == win) free_timer( queue, timer );
1377 ptr = next;
1379 ptr = list_head( &queue->expired_timers );
1380 while (ptr)
1382 struct list *next = list_next( &queue->expired_timers, ptr );
1383 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1384 if (timer->win == win) free_timer( queue, timer );
1385 ptr = next;
1388 /* remove messages */
1389 for (i = 0; i < NB_MSG_KINDS; i++)
1391 struct list *ptr, *next;
1393 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1395 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1396 if (msg->win == win) remove_queue_message( queue, msg, i );
1400 thread_input_cleanup_window( queue, win );
1403 /* post a message to a window; used by socket handling */
1404 void post_message( user_handle_t win, unsigned int message,
1405 unsigned int wparam, unsigned int lparam )
1407 struct message *msg;
1408 struct thread *thread = get_window_thread( win );
1410 if (!thread) return;
1412 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1414 msg->type = MSG_POSTED;
1415 msg->win = get_user_full_handle( win );
1416 msg->msg = message;
1417 msg->wparam = wparam;
1418 msg->lparam = lparam;
1419 msg->time = get_tick_count();
1420 msg->x = 0;
1421 msg->y = 0;
1422 msg->info = 0;
1423 msg->hook = 0;
1424 msg->hook_proc = NULL;
1425 msg->result = NULL;
1426 msg->data = NULL;
1427 msg->data_size = 0;
1429 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1430 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1432 release_object( thread );
1435 /* post a win event */
1436 void post_win_event( struct thread *thread, unsigned int event,
1437 user_handle_t win, unsigned int object_id,
1438 unsigned int child_id, void *hook_proc,
1439 const WCHAR *module, size_t module_size,
1440 user_handle_t hook)
1442 struct message *msg;
1444 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1446 msg->type = MSG_WINEVENT;
1447 msg->win = get_user_full_handle( win );
1448 msg->msg = event;
1449 msg->wparam = object_id;
1450 msg->lparam = child_id;
1451 msg->time = get_tick_count();
1452 msg->x = 0;
1453 msg->y = 0;
1454 msg->info = get_thread_id( current );
1455 msg->result = NULL;
1456 msg->hook = hook;
1457 msg->hook_proc = hook_proc;
1459 if ((msg->data = malloc( module_size )))
1461 msg->data_size = module_size;
1462 memcpy( msg->data, module, module_size );
1464 if (debug_level > 1)
1465 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1466 get_thread_id(thread), event, win, object_id, child_id );
1467 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1468 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1470 else
1471 free( msg );
1475 /* get the message queue of the current thread */
1476 DECL_HANDLER(get_msg_queue)
1478 struct msg_queue *queue = get_current_queue();
1480 reply->handle = 0;
1481 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1485 /* set the current message queue wakeup mask */
1486 DECL_HANDLER(set_queue_mask)
1488 struct msg_queue *queue = get_current_queue();
1490 if (queue)
1492 queue->wake_mask = req->wake_mask;
1493 queue->changed_mask = req->changed_mask;
1494 reply->wake_bits = queue->wake_bits;
1495 reply->changed_bits = queue->changed_bits;
1496 if (is_signaled( queue ))
1498 /* if skip wait is set, do what would have been done in the subsequent wait */
1499 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1500 else wake_up( &queue->obj, 0 );
1506 /* get the current message queue status */
1507 DECL_HANDLER(get_queue_status)
1509 struct msg_queue *queue = current->queue;
1510 if (queue)
1512 reply->wake_bits = queue->wake_bits;
1513 reply->changed_bits = queue->changed_bits;
1514 if (req->clear) queue->changed_bits = 0;
1516 else reply->wake_bits = reply->changed_bits = 0;
1520 /* send a message to a thread queue */
1521 DECL_HANDLER(send_message)
1523 struct message *msg;
1524 struct msg_queue *send_queue = get_current_queue();
1525 struct msg_queue *recv_queue = NULL;
1526 struct thread *thread = NULL;
1528 if (req->id)
1530 if (!(thread = get_thread_from_id( req->id ))) return;
1532 else if (req->type != MSG_HARDWARE)
1534 /* only hardware messages are allowed without destination thread */
1535 set_error( STATUS_INVALID_PARAMETER );
1536 return;
1539 if (thread && !(recv_queue = thread->queue))
1541 set_error( STATUS_INVALID_PARAMETER );
1542 release_object( thread );
1543 return;
1545 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1547 set_error( STATUS_TIMEOUT );
1548 release_object( thread );
1549 return;
1552 if ((msg = mem_alloc( sizeof(*msg) )))
1554 msg->type = req->type;
1555 msg->win = get_user_full_handle( req->win );
1556 msg->msg = req->msg;
1557 msg->wparam = req->wparam;
1558 msg->lparam = req->lparam;
1559 msg->time = req->time;
1560 msg->x = req->x;
1561 msg->y = req->y;
1562 msg->info = req->info;
1563 msg->hook = 0;
1564 msg->hook_proc = NULL;
1565 msg->result = NULL;
1566 msg->data = NULL;
1567 msg->data_size = 0;
1569 switch(msg->type)
1571 case MSG_OTHER_PROCESS:
1572 msg->data_size = get_req_data_size();
1573 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1575 free( msg );
1576 break;
1578 /* fall through */
1579 case MSG_ASCII:
1580 case MSG_UNICODE:
1581 case MSG_CALLBACK:
1582 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1583 req->timeout, req->callback, req->info )))
1585 free_message( msg );
1586 break;
1588 /* fall through */
1589 case MSG_NOTIFY:
1590 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1591 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1592 break;
1593 case MSG_POSTED:
1594 /* needed for posted DDE messages */
1595 msg->data_size = get_req_data_size();
1596 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1598 free( msg );
1599 break;
1601 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1602 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1603 break;
1604 case MSG_HARDWARE:
1605 queue_hardware_message( recv_queue, msg );
1606 break;
1607 case MSG_CALLBACK_RESULT: /* cannot send this one */
1608 default:
1609 set_error( STATUS_INVALID_PARAMETER );
1610 free( msg );
1611 break;
1614 if (thread) release_object( thread );
1618 /* get a message from the current queue */
1619 DECL_HANDLER(get_message)
1621 struct timer *timer;
1622 struct list *ptr;
1623 struct msg_queue *queue = get_current_queue();
1624 user_handle_t get_win = get_user_full_handle( req->get_win );
1626 reply->active_hooks = get_active_hooks();
1628 if (!queue) return;
1629 gettimeofday( &queue->last_get_msg, NULL );
1631 /* first check for sent messages */
1632 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1634 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1635 receive_message( queue, msg, reply );
1636 return;
1638 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1640 /* clear changed bits so we can wait on them if we don't find a message */
1641 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits = 0;
1642 else queue->changed_bits &= QS_ALLPOSTMESSAGE;
1644 /* then check for posted messages */
1645 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1646 return;
1648 /* then check for any raw hardware message */
1649 if (filter_contains_hw_range( req->get_first, req->get_last ) &&
1650 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1651 return;
1653 /* now check for WM_PAINT */
1654 if (queue->paint_count &&
1655 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1656 (reply->win = find_window_to_repaint( get_win, current )))
1658 reply->type = MSG_POSTED;
1659 reply->msg = WM_PAINT;
1660 reply->wparam = 0;
1661 reply->lparam = 0;
1662 reply->x = 0;
1663 reply->y = 0;
1664 reply->time = get_tick_count();
1665 reply->info = 0;
1666 return;
1669 /* now check for timer */
1670 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1671 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1673 reply->type = MSG_POSTED;
1674 reply->win = timer->win;
1675 reply->msg = timer->msg;
1676 reply->wparam = timer->id;
1677 reply->lparam = timer->lparam;
1678 reply->x = 0;
1679 reply->y = 0;
1680 reply->time = get_tick_count();
1681 reply->info = 0;
1682 return;
1685 done:
1686 set_error( STATUS_PENDING ); /* FIXME */
1690 /* reply to a sent message */
1691 DECL_HANDLER(reply_message)
1693 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1694 else if (current->queue->recv_result)
1695 reply_message( current->queue, req->result, 0, req->remove,
1696 get_req_data(), get_req_data_size() );
1700 /* accept the current hardware message */
1701 DECL_HANDLER(accept_hardware_message)
1703 if (current->queue)
1704 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1705 else
1706 set_error( STATUS_ACCESS_DENIED );
1710 /* retrieve the reply for the last message sent */
1711 DECL_HANDLER(get_message_reply)
1713 struct message_result *result;
1714 struct list *entry;
1715 struct msg_queue *queue = current->queue;
1717 if (queue)
1719 set_error( STATUS_PENDING );
1720 reply->result = 0;
1722 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1724 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1725 if (result->replied || req->cancel)
1727 if (result->replied)
1729 reply->result = result->result;
1730 set_error( result->error );
1731 if (result->data)
1733 size_t data_len = min( result->data_size, get_reply_max_size() );
1734 set_reply_data_ptr( result->data, data_len );
1735 result->data = NULL;
1736 result->data_size = 0;
1739 remove_result_from_sender( result );
1741 entry = list_head( &queue->send_result );
1742 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1743 else
1745 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1746 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1750 else set_error( STATUS_ACCESS_DENIED );
1754 /* set a window timer */
1755 DECL_HANDLER(set_win_timer)
1757 struct timer *timer;
1758 struct msg_queue *queue;
1759 struct thread *thread = NULL;
1760 user_handle_t win = 0;
1761 unsigned int id = req->id;
1763 if (req->win)
1765 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1767 set_error( STATUS_INVALID_HANDLE );
1768 return;
1770 if (thread->process != current->process)
1772 release_object( thread );
1773 set_error( STATUS_ACCESS_DENIED );
1774 return;
1776 queue = thread->queue;
1777 /* remove it if it existed already */
1778 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1780 else
1782 queue = get_current_queue();
1783 /* find a free id for it */
1786 id = queue->next_timer_id;
1787 if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1789 while (find_timer( queue, 0, req->msg, id ));
1792 if ((timer = set_timer( queue, req->rate )))
1794 timer->win = win;
1795 timer->msg = req->msg;
1796 timer->id = id;
1797 timer->lparam = req->lparam;
1798 reply->id = id;
1800 if (thread) release_object( thread );
1803 /* kill a window timer */
1804 DECL_HANDLER(kill_win_timer)
1806 struct timer *timer;
1807 struct thread *thread;
1808 user_handle_t win = 0;
1810 if (req->win)
1812 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1814 set_error( STATUS_INVALID_HANDLE );
1815 return;
1817 if (thread->process != current->process)
1819 release_object( thread );
1820 set_error( STATUS_ACCESS_DENIED );
1821 return;
1824 else thread = (struct thread *)grab_object( current );
1826 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1827 free_timer( thread->queue, timer );
1828 else
1829 set_error( STATUS_INVALID_PARAMETER );
1831 release_object( thread );
1835 /* attach (or detach) thread inputs */
1836 DECL_HANDLER(attach_thread_input)
1838 struct thread *thread_from = get_thread_from_id( req->tid_from );
1839 struct thread *thread_to = get_thread_from_id( req->tid_to );
1841 if (!thread_from || !thread_to)
1843 if (thread_from) release_object( thread_from );
1844 if (thread_to) release_object( thread_to );
1845 return;
1847 if (thread_from != thread_to)
1849 if (req->attach) attach_thread_input( thread_from, thread_to );
1850 else
1852 if (thread_from->queue && thread_to->queue &&
1853 thread_from->queue->input == thread_to->queue->input)
1854 detach_thread_input( thread_from );
1855 else
1856 set_error( STATUS_ACCESS_DENIED );
1859 else set_error( STATUS_ACCESS_DENIED );
1860 release_object( thread_from );
1861 release_object( thread_to );
1865 /* get thread input data */
1866 DECL_HANDLER(get_thread_input)
1868 struct thread *thread = NULL;
1869 struct thread_input *input;
1871 if (req->tid)
1873 if (!(thread = get_thread_from_id( req->tid ))) return;
1874 input = thread->queue ? thread->queue->input : NULL;
1876 else input = foreground_input; /* get the foreground thread info */
1878 if (input)
1880 reply->focus = input->focus;
1881 reply->capture = input->capture;
1882 reply->active = input->active;
1883 reply->menu_owner = input->menu_owner;
1884 reply->move_size = input->move_size;
1885 reply->caret = input->caret;
1886 reply->rect = input->caret_rect;
1888 else
1890 reply->focus = 0;
1891 reply->capture = 0;
1892 reply->active = 0;
1893 reply->menu_owner = 0;
1894 reply->move_size = 0;
1895 reply->caret = 0;
1896 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1898 /* foreground window is active window of foreground thread */
1899 reply->foreground = foreground_input ? foreground_input->active : 0;
1900 if (thread) release_object( thread );
1904 /* retrieve queue keyboard state for a given thread */
1905 DECL_HANDLER(get_key_state)
1907 struct thread *thread;
1908 struct thread_input *input;
1910 if (!(thread = get_thread_from_id( req->tid ))) return;
1911 input = thread->queue ? thread->queue->input : NULL;
1912 if (input)
1914 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1915 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1917 release_object( thread );
1921 /* set queue keyboard state for a given thread */
1922 DECL_HANDLER(set_key_state)
1924 struct thread *thread = NULL;
1925 struct thread_input *input;
1927 if (!(thread = get_thread_from_id( req->tid ))) return;
1928 input = thread->queue ? thread->queue->input : NULL;
1929 if (input)
1931 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1932 if (size) memcpy( input->keystate, get_req_data(), size );
1934 release_object( thread );
1938 /* set the system foreground window */
1939 DECL_HANDLER(set_foreground_window)
1941 struct msg_queue *queue = get_current_queue();
1943 reply->previous = foreground_input ? foreground_input->active : 0;
1944 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1945 reply->send_msg_new = FALSE;
1947 if (req->handle)
1949 struct thread *thread;
1951 if (is_top_level_window( req->handle ) &&
1952 ((thread = get_window_thread( req->handle ))))
1954 foreground_input = thread->queue->input;
1955 reply->send_msg_new = (foreground_input != queue->input);
1956 release_object( thread );
1958 else set_error( STATUS_INVALID_HANDLE );
1960 else foreground_input = NULL;
1964 /* set the current thread focus window */
1965 DECL_HANDLER(set_focus_window)
1967 struct msg_queue *queue = get_current_queue();
1969 reply->previous = 0;
1970 if (queue && check_queue_input_window( queue, req->handle ))
1972 reply->previous = queue->input->focus;
1973 queue->input->focus = get_user_full_handle( req->handle );
1978 /* set the current thread active window */
1979 DECL_HANDLER(set_active_window)
1981 struct msg_queue *queue = get_current_queue();
1983 reply->previous = 0;
1984 if (queue && check_queue_input_window( queue, req->handle ))
1986 if (!req->handle || make_window_active( req->handle ))
1988 reply->previous = queue->input->active;
1989 queue->input->active = get_user_full_handle( req->handle );
1991 else set_error( STATUS_INVALID_HANDLE );
1996 /* set the current thread capture window */
1997 DECL_HANDLER(set_capture_window)
1999 struct msg_queue *queue = get_current_queue();
2001 reply->previous = reply->full_handle = 0;
2002 if (queue && check_queue_input_window( queue, req->handle ))
2004 struct thread_input *input = queue->input;
2006 reply->previous = input->capture;
2007 input->capture = get_user_full_handle( req->handle );
2008 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2009 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2010 reply->full_handle = input->capture;
2015 /* Set the current thread caret window */
2016 DECL_HANDLER(set_caret_window)
2018 struct msg_queue *queue = get_current_queue();
2020 reply->previous = 0;
2021 if (queue && check_queue_input_window( queue, req->handle ))
2023 struct thread_input *input = queue->input;
2025 reply->previous = input->caret;
2026 reply->old_rect = input->caret_rect;
2027 reply->old_hide = input->caret_hide;
2028 reply->old_state = input->caret_state;
2030 set_caret_window( input, get_user_full_handle(req->handle) );
2031 input->caret_rect.right = input->caret_rect.left + req->width;
2032 input->caret_rect.bottom = input->caret_rect.top + req->height;
2037 /* Set the current thread caret information */
2038 DECL_HANDLER(set_caret_info)
2040 struct msg_queue *queue = get_current_queue();
2041 struct thread_input *input;
2043 if (!queue) return;
2044 input = queue->input;
2045 reply->full_handle = input->caret;
2046 reply->old_rect = input->caret_rect;
2047 reply->old_hide = input->caret_hide;
2048 reply->old_state = input->caret_state;
2050 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2052 set_error( STATUS_ACCESS_DENIED );
2053 return;
2055 if (req->flags & SET_CARET_POS)
2057 input->caret_rect.right += req->x - input->caret_rect.left;
2058 input->caret_rect.bottom += req->y - input->caret_rect.top;
2059 input->caret_rect.left = req->x;
2060 input->caret_rect.top = req->y;
2062 if (req->flags & SET_CARET_HIDE)
2064 input->caret_hide += req->hide;
2065 if (input->caret_hide < 0) input->caret_hide = 0;
2067 if (req->flags & SET_CARET_STATE)
2069 if (req->state == -1) input->caret_state = !input->caret_state;
2070 else input->caret_state = !!req->state;
2075 /* get the time of the last input event */
2076 DECL_HANDLER(get_last_input_time)
2078 reply->time = last_input_time;