Fixed a few missing characters in winecfg.
[wine/wine-kai.git] / server / queue.c
blob8827795778f6fdb65efe949d4d9d54782f018c31
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 "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
34 #include "handle.h"
35 #include "file.h"
36 #include "thread.h"
37 #include "process.h"
38 #include "request.h"
39 #include "user.h"
41 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
42 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
44 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
45 #define NB_MSG_KINDS (POST_MESSAGE+1)
48 struct message_result
50 struct list sender_entry; /* entry in sender list */
51 struct message *msg; /* message the result is for */
52 struct message_result *recv_next; /* next in receiver list */
53 struct msg_queue *sender; /* sender queue */
54 struct msg_queue *receiver; /* receiver queue */
55 int replied; /* has it been replied to? */
56 unsigned int result; /* reply result */
57 unsigned int error; /* error code to pass back to sender */
58 struct message *callback_msg; /* message to queue for callback */
59 void *data; /* message reply data */
60 unsigned int data_size; /* size of message reply data */
61 struct timeout_user *timeout; /* result timeout */
64 struct message
66 struct list entry; /* entry in message list */
67 enum message_type type; /* message type */
68 user_handle_t win; /* window handle */
69 unsigned int msg; /* message code */
70 unsigned int wparam; /* parameters */
71 unsigned int lparam; /* parameters */
72 int x; /* x position */
73 int y; /* y position */
74 unsigned int time; /* message time */
75 unsigned int info; /* extra info */
76 user_handle_t hook; /* winevent hook handle */
77 void *hook_proc; /* winevent hook proc address */
78 void *data; /* message data for sent messages */
79 unsigned int data_size; /* size of message data */
80 unsigned int unique_id; /* unique id for nested hw message waits */
81 struct message_result *result; /* result in sender queue */
84 struct timer
86 struct list entry; /* entry in timer list */
87 struct timeval when; /* next expiration */
88 unsigned int rate; /* timer rate in ms */
89 user_handle_t win; /* window handle */
90 unsigned int msg; /* message to post */
91 unsigned int id; /* timer id */
92 unsigned int lparam; /* lparam for message */
95 struct thread_input
97 struct object obj; /* object header */
98 struct desktop *desktop; /* desktop that this thread input belongs to */
99 user_handle_t focus; /* focus window */
100 user_handle_t capture; /* capture window */
101 user_handle_t active; /* active window */
102 user_handle_t menu_owner; /* current menu owner window */
103 user_handle_t move_size; /* current moving/resizing window */
104 user_handle_t caret; /* caret window */
105 rectangle_t caret_rect; /* caret rectangle */
106 int caret_hide; /* caret hide count */
107 int caret_state; /* caret on/off state */
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 unsigned int wake_bits; /* wakeup bits */
116 unsigned int wake_mask; /* wakeup mask */
117 unsigned int changed_bits; /* changed wakeup bits */
118 unsigned int changed_mask; /* changed wakeup mask */
119 int paint_count; /* pending paint messages count */
120 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
121 struct list send_result; /* stack of sent messages waiting for result */
122 struct list callback_result; /* list of callback messages waiting for result */
123 struct message_result *recv_result; /* stack of received messages waiting for result */
124 struct list pending_timers; /* list of pending timers */
125 struct list expired_timers; /* list of expired timers */
126 unsigned int next_timer_id; /* id for the next timer with a 0 window */
127 struct timeout_user *timeout; /* timeout for next timer to expire */
128 struct thread_input *input; /* thread input descriptor */
129 struct hook_table *hooks; /* hook table */
130 struct timeval last_get_msg; /* time of last get message call */
133 static void msg_queue_dump( struct object *obj, int verbose );
134 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
135 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
136 static int msg_queue_signaled( struct object *obj, struct thread *thread );
137 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
138 static void msg_queue_destroy( struct object *obj );
139 static void thread_input_dump( struct object *obj, int verbose );
140 static void thread_input_destroy( struct object *obj );
141 static void timer_callback( void *private );
143 static const struct object_ops msg_queue_ops =
145 sizeof(struct msg_queue), /* size */
146 msg_queue_dump, /* dump */
147 msg_queue_add_queue, /* add_queue */
148 msg_queue_remove_queue, /* remove_queue */
149 msg_queue_signaled, /* signaled */
150 msg_queue_satisfied, /* satisfied */
151 no_signal, /* signal */
152 no_get_fd, /* get_fd */
153 no_close_handle, /* close_handle */
154 msg_queue_destroy /* destroy */
158 static const struct object_ops thread_input_ops =
160 sizeof(struct thread_input), /* size */
161 thread_input_dump, /* dump */
162 no_add_queue, /* add_queue */
163 NULL, /* remove_queue */
164 NULL, /* signaled */
165 NULL, /* satisfied */
166 no_signal, /* signal */
167 no_get_fd, /* get_fd */
168 no_close_handle, /* close_handle */
169 thread_input_destroy /* destroy */
172 /* pointer to input structure of foreground thread */
173 static struct thread_input *foreground_input;
174 static unsigned int last_input_time;
177 /* set the caret window in a given thread input */
178 static void set_caret_window( struct thread_input *input, user_handle_t win )
180 if (!win || win != input->caret)
182 input->caret_rect.left = 0;
183 input->caret_rect.top = 0;
184 input->caret_rect.right = 0;
185 input->caret_rect.bottom = 0;
187 input->caret = win;
188 input->caret_hide = 1;
189 input->caret_state = 0;
192 /* create a thread input object */
193 static struct thread_input *create_thread_input( struct thread *thread )
195 struct thread_input *input;
197 if ((input = alloc_object( &thread_input_ops )))
199 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
201 free( input );
202 return NULL;
204 input->focus = 0;
205 input->capture = 0;
206 input->active = 0;
207 input->menu_owner = 0;
208 input->move_size = 0;
209 list_init( &input->msg_list );
210 set_caret_window( input, 0 );
211 memset( input->keystate, 0, sizeof(input->keystate) );
213 return input;
216 /* release the thread input data of a given thread */
217 static inline void release_thread_input( struct thread *thread )
219 struct thread_input *input = thread->queue->input;
221 if (!input) return;
222 release_object( input );
223 thread->queue->input = NULL;
226 /* create a message queue object */
227 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
229 struct msg_queue *queue;
230 int i;
232 if (!input && !(input = create_thread_input( thread ))) return NULL;
233 if ((queue = alloc_object( &msg_queue_ops )))
235 queue->wake_bits = 0;
236 queue->wake_mask = 0;
237 queue->changed_bits = 0;
238 queue->changed_mask = 0;
239 queue->paint_count = 0;
240 queue->recv_result = NULL;
241 queue->next_timer_id = 1;
242 queue->timeout = NULL;
243 queue->input = (struct thread_input *)grab_object( input );
244 queue->hooks = NULL;
245 gettimeofday( &queue->last_get_msg, NULL );
246 list_init( &queue->send_result );
247 list_init( &queue->callback_result );
248 list_init( &queue->pending_timers );
249 list_init( &queue->expired_timers );
250 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
252 thread->queue = queue;
253 if (!thread->process->queue)
254 thread->process->queue = (struct msg_queue *)grab_object( queue );
256 release_object( input );
257 return queue;
260 /* free the message queue of a thread at thread exit */
261 void free_msg_queue( struct thread *thread )
263 struct process *process = thread->process;
265 remove_thread_hooks( thread );
266 if (!thread->queue) return;
267 if (process->queue == thread->queue) /* is it the process main queue? */
269 release_object( process->queue );
270 process->queue = NULL;
271 if (process->idle_event)
273 set_event( process->idle_event );
274 release_object( process->idle_event );
275 process->idle_event = NULL;
278 release_object( thread->queue );
279 thread->queue = NULL;
282 /* get the hook table for a given thread */
283 struct hook_table *get_queue_hooks( struct thread *thread )
285 if (!thread->queue) return NULL;
286 return thread->queue->hooks;
289 /* set the hook table for a given thread, allocating the queue if needed */
290 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
292 struct msg_queue *queue = thread->queue;
293 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
294 if (queue->hooks) release_object( queue->hooks );
295 queue->hooks = hooks;
298 /* check the queue status */
299 inline static int is_signaled( struct msg_queue *queue )
301 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
304 /* set some queue bits */
305 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
307 queue->wake_bits |= bits;
308 queue->changed_bits |= bits;
309 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
312 /* clear some queue bits */
313 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
315 queue->wake_bits &= ~bits;
316 queue->changed_bits &= ~bits;
319 /* check whether msg is a keyboard message */
320 inline static int is_keyboard_msg( struct message *msg )
322 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
325 /* check if message is matched by the filter */
326 inline static int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
328 return (msg >= first && msg <= last);
331 /* check whether a message filter contains at least one potential hardware message */
332 inline static int filter_contains_hw_range( unsigned int first, unsigned int last )
334 /* hardware message ranges are (in numerical order):
335 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
336 * WM_KEYFIRST .. WM_KEYLAST
337 * WM_MOUSEFIRST .. WM_MOUSELAST
339 if (last < WM_NCMOUSEFIRST) return 0;
340 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
341 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
342 if (first > WM_MOUSELAST) return 0;
343 return 1;
346 /* get the QS_* bit corresponding to a given hardware message */
347 inline static int get_hardware_msg_bit( struct message *msg )
349 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
350 if (is_keyboard_msg( msg )) return QS_KEY;
351 return QS_MOUSEBUTTON;
354 /* get the current thread queue, creating it if needed */
355 inline static struct msg_queue *get_current_queue(void)
357 struct msg_queue *queue = current->queue;
358 if (!queue) queue = create_msg_queue( current, NULL );
359 return queue;
362 /* get a (pseudo-)unique id to tag hardware messages */
363 inline static unsigned int get_unique_id(void)
365 static unsigned int id;
366 if (!++id) id = 1; /* avoid an id of 0 */
367 return id;
370 /* try to merge a message with the last in the list; return 1 if successful */
371 static int merge_message( struct thread_input *input, const struct message *msg )
373 struct message *prev;
374 struct list *ptr = list_tail( &input->msg_list );
376 if (!ptr) return 0;
377 prev = LIST_ENTRY( ptr, struct message, entry );
378 if (prev->unique_id) return 0;
379 if (prev->result) return 0;
380 if (prev->win != msg->win) return 0;
381 if (prev->msg != msg->msg) return 0;
382 if (prev->type != msg->type) return 0;
383 /* now we can merge it */
384 prev->wparam = msg->wparam;
385 prev->lparam = msg->lparam;
386 prev->x = msg->x;
387 prev->y = msg->y;
388 prev->time = msg->time;
389 prev->info = msg->info;
390 return 1;
393 /* free a result structure */
394 static void free_result( struct message_result *result )
396 if (result->timeout) remove_timeout_user( result->timeout );
397 if (result->data) free( result->data );
398 if (result->callback_msg) free( result->callback_msg );
399 free( result );
402 /* remove the result from the sender list it is on */
403 static inline void remove_result_from_sender( struct message_result *result )
405 assert( result->sender );
407 list_remove( &result->sender_entry );
408 result->sender = NULL;
409 if (!result->receiver) free_result( result );
412 /* store the message result in the appropriate structure */
413 static void store_message_result( struct message_result *res, unsigned int result,
414 unsigned int error )
416 res->result = result;
417 res->error = error;
418 res->replied = 1;
419 if (res->timeout)
421 remove_timeout_user( res->timeout );
422 res->timeout = NULL;
424 if (res->sender)
426 if (res->callback_msg)
428 /* queue the callback message in the sender queue */
429 res->callback_msg->lparam = result;
430 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
431 set_queue_bits( res->sender, QS_SENDMESSAGE );
432 res->callback_msg = NULL;
433 remove_result_from_sender( res );
435 else
437 /* wake sender queue if waiting on this result */
438 if (list_head(&res->sender->send_result) == &res->sender_entry)
439 set_queue_bits( res->sender, QS_SMRESULT );
445 /* free a message when deleting a queue or window */
446 static void free_message( struct message *msg )
448 struct message_result *result = msg->result;
449 if (result)
451 result->msg = NULL;
452 if (result->sender)
454 result->receiver = NULL;
455 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
457 else free_result( result );
459 if (msg->data) free( msg->data );
460 free( msg );
463 /* remove (and free) a message from a message list */
464 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
465 enum message_kind kind )
467 list_remove( &msg->entry );
468 switch(kind)
470 case SEND_MESSAGE:
471 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
472 break;
473 case POST_MESSAGE:
474 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_POSTMESSAGE );
475 break;
477 free_message( msg );
480 /* message timed out without getting a reply */
481 static void result_timeout( void *private )
483 struct message_result *result = private;
485 assert( !result->replied );
487 result->timeout = NULL;
489 if (result->msg) /* not received yet */
491 struct message *msg = result->msg;
493 result->msg = NULL;
494 msg->result = NULL;
495 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
496 result->receiver = NULL;
497 if (!result->sender)
499 free_result( result );
500 return;
504 store_message_result( result, 0, STATUS_TIMEOUT );
507 /* allocate and fill a message result structure */
508 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
509 struct msg_queue *recv_queue,
510 struct message *msg, int timeout,
511 void *callback, unsigned int callback_data )
513 struct message_result *result = mem_alloc( sizeof(*result) );
514 if (result)
516 result->msg = msg;
517 result->sender = send_queue;
518 result->receiver = recv_queue;
519 result->replied = 0;
520 result->data = NULL;
521 result->data_size = 0;
522 result->timeout = NULL;
524 if (msg->type == MSG_CALLBACK)
526 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
527 if (!callback_msg)
529 free( result );
530 return NULL;
532 callback_msg->type = MSG_CALLBACK_RESULT;
533 callback_msg->win = msg->win;
534 callback_msg->msg = msg->msg;
535 callback_msg->wparam = (unsigned int)callback;
536 callback_msg->lparam = 0;
537 callback_msg->time = get_tick_count();
538 callback_msg->x = 0;
539 callback_msg->y = 0;
540 callback_msg->info = callback_data;
541 callback_msg->hook = 0;
542 callback_msg->hook_proc = NULL;
543 callback_msg->result = NULL;
544 callback_msg->data = NULL;
545 callback_msg->data_size = 0;
547 result->callback_msg = callback_msg;
548 list_add_head( &send_queue->callback_result, &result->sender_entry );
550 else
552 result->callback_msg = NULL;
553 list_add_head( &send_queue->send_result, &result->sender_entry );
556 if (timeout)
558 struct timeval when;
559 gettimeofday( &when, NULL );
560 add_timeout( &when, timeout );
561 result->timeout = add_timeout_user( &when, result_timeout, result );
564 return result;
567 /* receive a message, removing it from the sent queue */
568 static void receive_message( struct msg_queue *queue, struct message *msg,
569 struct get_message_reply *reply )
571 struct message_result *result = msg->result;
573 reply->total = msg->data_size;
574 if (msg->data_size > get_reply_max_size())
576 set_error( STATUS_BUFFER_OVERFLOW );
577 return;
579 reply->type = msg->type;
580 reply->win = msg->win;
581 reply->msg = msg->msg;
582 reply->wparam = msg->wparam;
583 reply->lparam = msg->lparam;
584 reply->x = msg->x;
585 reply->y = msg->y;
586 reply->time = msg->time;
587 reply->info = msg->info;
588 reply->hook = msg->hook;
589 reply->hook_proc = msg->hook_proc;
591 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
593 list_remove( &msg->entry );
594 /* put the result on the receiver result stack */
595 if (result)
597 result->msg = NULL;
598 result->recv_next = queue->recv_result;
599 queue->recv_result = result;
601 free( msg );
602 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
605 /* set the result of the current received message */
606 static void reply_message( struct msg_queue *queue, unsigned int result,
607 unsigned int error, int remove, const void *data, size_t len )
609 struct message_result *res = queue->recv_result;
611 if (remove)
613 queue->recv_result = res->recv_next;
614 res->receiver = NULL;
615 if (!res->sender) /* no one waiting for it */
617 free_result( res );
618 return;
621 if (!res->replied)
623 if (len && (res->data = memdup( data, len ))) res->data_size = len;
624 store_message_result( res, result, error );
628 /* retrieve a posted message */
629 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
630 unsigned int first, unsigned int last, unsigned int flags,
631 struct get_message_reply *reply )
633 struct message *msg;
635 /* check against the filters */
636 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
638 if (msg->msg == WM_QUIT) goto found; /* WM_QUIT is never filtered */
639 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
640 if (!check_msg_filter( msg->msg, first, last )) continue;
641 goto found; /* found one */
643 return 0;
645 /* return it to the app */
646 found:
647 reply->total = msg->data_size;
648 if (msg->data_size > get_reply_max_size())
650 set_error( STATUS_BUFFER_OVERFLOW );
651 return 1;
653 reply->type = msg->type;
654 reply->win = msg->win;
655 reply->msg = msg->msg;
656 reply->wparam = msg->wparam;
657 reply->lparam = msg->lparam;
658 reply->x = msg->x;
659 reply->y = msg->y;
660 reply->time = msg->time;
661 reply->info = msg->info;
663 if (flags & GET_MSG_REMOVE)
665 if (msg->data)
667 set_reply_data_ptr( msg->data, msg->data_size );
668 msg->data = NULL;
669 msg->data_size = 0;
671 remove_queue_message( queue, msg, POST_MESSAGE );
673 else if (msg->data) set_reply_data( msg->data, msg->data_size );
675 return 1;
678 /* empty a message list and free all the messages */
679 static void empty_msg_list( struct list *list )
681 struct list *ptr;
683 while ((ptr = list_head( list )) != NULL)
685 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
686 list_remove( &msg->entry );
687 free_message( msg );
691 /* cleanup all pending results when deleting a queue */
692 static void cleanup_results( struct msg_queue *queue )
694 struct list *entry;
696 while ((entry = list_head( &queue->send_result )) != NULL)
698 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
701 while ((entry = list_head( &queue->callback_result )) != NULL)
703 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
706 while (queue->recv_result)
707 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
710 /* check if the thread owning the queue is hung (not checking for messages) */
711 static int is_queue_hung( struct msg_queue *queue )
713 struct timeval now;
714 struct wait_queue_entry *entry;
716 gettimeofday( &now, NULL );
717 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
718 return 0; /* less than 5 seconds since last get message -> not hung */
720 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
722 if (entry->thread->queue == queue)
723 return 0; /* thread is waiting on queue -> not hung */
725 return 1;
728 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
730 struct msg_queue *queue = (struct msg_queue *)obj;
731 struct process *process = entry->thread->process;
733 /* a thread can only wait on its own queue */
734 if (entry->thread->queue != queue)
736 set_error( STATUS_ACCESS_DENIED );
737 return 0;
739 /* if waiting on the main process queue, set the idle event */
740 if (process->queue == queue)
742 if (process->idle_event) set_event( process->idle_event );
744 add_queue( obj, entry );
745 return 1;
748 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
750 struct msg_queue *queue = (struct msg_queue *)obj;
751 struct process *process = entry->thread->process;
753 remove_queue( obj, entry );
755 assert( entry->thread->queue == queue );
757 /* if waiting on the main process queue, reset the idle event */
758 if (process->queue == queue)
760 if (process->idle_event) reset_event( process->idle_event );
764 static void msg_queue_dump( struct object *obj, int verbose )
766 struct msg_queue *queue = (struct msg_queue *)obj;
767 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
768 queue->wake_bits, queue->wake_mask );
771 static int msg_queue_signaled( struct object *obj, struct thread *thread )
773 struct msg_queue *queue = (struct msg_queue *)obj;
774 return is_signaled( queue );
777 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
779 struct msg_queue *queue = (struct msg_queue *)obj;
780 queue->wake_mask = 0;
781 queue->changed_mask = 0;
782 return 0; /* Not abandoned */
785 static void msg_queue_destroy( struct object *obj )
787 struct msg_queue *queue = (struct msg_queue *)obj;
788 struct list *ptr;
789 int i;
791 cleanup_results( queue );
792 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
794 while ((ptr = list_head( &queue->pending_timers )))
796 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
797 list_remove( &timer->entry );
798 free( timer );
800 while ((ptr = list_head( &queue->expired_timers )))
802 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
803 list_remove( &timer->entry );
804 free( timer );
806 if (queue->timeout) remove_timeout_user( queue->timeout );
807 if (queue->input) release_object( queue->input );
808 if (queue->hooks) release_object( queue->hooks );
811 static void thread_input_dump( struct object *obj, int verbose )
813 struct thread_input *input = (struct thread_input *)obj;
814 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
815 input->focus, input->capture, input->active );
818 static void thread_input_destroy( struct object *obj )
820 struct thread_input *input = (struct thread_input *)obj;
822 if (foreground_input == input) foreground_input = NULL;
823 empty_msg_list( &input->msg_list );
824 release_object( input->desktop );
827 /* fix the thread input data when a window is destroyed */
828 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
830 struct thread_input *input = queue->input;
832 if (window == input->focus) input->focus = 0;
833 if (window == input->capture) input->capture = 0;
834 if (window == input->active) input->active = 0;
835 if (window == input->menu_owner) input->menu_owner = 0;
836 if (window == input->move_size) input->move_size = 0;
837 if (window == input->caret) set_caret_window( input, 0 );
840 /* check if the specified window can be set in the input data of a given queue */
841 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
843 struct thread *thread;
844 int ret = 0;
846 if (!window) return 1; /* we can always clear the data */
848 if ((thread = get_window_thread( window )))
850 ret = (queue->input == thread->queue->input);
851 if (!ret) set_error( STATUS_ACCESS_DENIED );
852 release_object( thread );
854 else set_error( STATUS_INVALID_HANDLE );
856 return ret;
859 /* make sure the specified thread has a queue */
860 int init_thread_queue( struct thread *thread )
862 if (thread->queue) return 1;
863 return (create_msg_queue( thread, NULL ) != NULL);
866 /* attach two thread input data structures */
867 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
869 struct desktop *desktop;
870 struct thread_input *input;
872 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
873 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
874 input = (struct thread_input *)grab_object( thread_to->queue->input );
875 if (input->desktop != desktop)
877 set_error( STATUS_ACCESS_DENIED );
878 release_object( input );
879 release_object( desktop );
880 return 0;
882 release_object( desktop );
884 if (thread_from->queue)
886 release_thread_input( thread_from );
887 thread_from->queue->input = input;
889 else
891 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
893 memset( input->keystate, 0, sizeof(input->keystate) );
894 return 1;
897 /* detach two thread input data structures */
898 void detach_thread_input( struct thread *thread_from )
900 struct thread_input *input;
902 if ((input = create_thread_input( thread_from )))
904 release_thread_input( thread_from );
905 thread_from->queue->input = input;
910 /* set the next timer to expire */
911 static void set_next_timer( struct msg_queue *queue )
913 struct list *ptr;
915 if (queue->timeout)
917 remove_timeout_user( queue->timeout );
918 queue->timeout = NULL;
920 if ((ptr = list_head( &queue->pending_timers )))
922 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
923 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
925 /* set/clear QS_TIMER bit */
926 if (list_empty( &queue->expired_timers ))
927 clear_queue_bits( queue, QS_TIMER );
928 else
929 set_queue_bits( queue, QS_TIMER );
932 /* find a timer from its window and id */
933 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
934 unsigned int msg, unsigned int id )
936 struct list *ptr;
938 /* we need to search both lists */
940 LIST_FOR_EACH( ptr, &queue->pending_timers )
942 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
943 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
945 LIST_FOR_EACH( ptr, &queue->expired_timers )
947 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
948 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
950 return NULL;
953 /* callback for the next timer expiration */
954 static void timer_callback( void *private )
956 struct msg_queue *queue = private;
957 struct list *ptr;
959 queue->timeout = NULL;
960 /* move on to the next timer */
961 ptr = list_head( &queue->pending_timers );
962 list_remove( ptr );
963 list_add_tail( &queue->expired_timers, ptr );
964 set_next_timer( queue );
967 /* link a timer at its rightful place in the queue list */
968 static void link_timer( struct msg_queue *queue, struct timer *timer )
970 struct list *ptr;
972 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
974 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
975 if (!time_before( &t->when, &timer->when )) break;
977 list_add_before( ptr, &timer->entry );
980 /* remove a timer from the queue timer list and free it */
981 static void free_timer( struct msg_queue *queue, struct timer *timer )
983 list_remove( &timer->entry );
984 free( timer );
985 set_next_timer( queue );
988 /* restart an expired timer */
989 static void restart_timer( struct msg_queue *queue, struct timer *timer )
991 struct timeval now;
993 list_remove( &timer->entry );
994 gettimeofday( &now, NULL );
995 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
996 link_timer( queue, timer );
997 set_next_timer( queue );
1000 /* find an expired timer matching the filtering parameters */
1001 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1002 unsigned int get_first, unsigned int get_last,
1003 int remove )
1005 struct list *ptr;
1007 LIST_FOR_EACH( ptr, &queue->expired_timers )
1009 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1010 if (win && timer->win != win) continue;
1011 if (check_msg_filter( timer->msg, get_first, get_last ))
1013 if (remove) restart_timer( queue, timer );
1014 return timer;
1017 return NULL;
1020 /* add a timer */
1021 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1023 struct timer *timer = mem_alloc( sizeof(*timer) );
1024 if (timer)
1026 timer->rate = max( rate, 1 );
1027 gettimeofday( &timer->when, NULL );
1028 add_timeout( &timer->when, rate );
1029 link_timer( queue, timer );
1030 /* check if we replaced the next timer */
1031 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1033 return timer;
1036 /* change the input key state for a given key */
1037 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1039 if (down)
1041 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1042 input->keystate[key] |= 0x80;
1044 else input->keystate[key] &= ~0x80;
1047 /* update the input key state for a keyboard message */
1048 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1050 unsigned char key;
1051 int down = 0, extended;
1053 switch (msg->msg)
1055 case WM_LBUTTONDOWN:
1056 down = 1;
1057 /* fall through */
1058 case WM_LBUTTONUP:
1059 set_input_key_state( input, VK_LBUTTON, down );
1060 break;
1061 case WM_MBUTTONDOWN:
1062 down = 1;
1063 /* fall through */
1064 case WM_MBUTTONUP:
1065 set_input_key_state( input, VK_MBUTTON, down );
1066 break;
1067 case WM_RBUTTONDOWN:
1068 down = 1;
1069 /* fall through */
1070 case WM_RBUTTONUP:
1071 set_input_key_state( input, VK_RBUTTON, down );
1072 break;
1073 case WM_XBUTTONDOWN:
1074 down = 1;
1075 /* fall through */
1076 case WM_XBUTTONUP:
1077 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1078 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1079 break;
1080 case WM_KEYDOWN:
1081 case WM_SYSKEYDOWN:
1082 down = 1;
1083 /* fall through */
1084 case WM_KEYUP:
1085 case WM_SYSKEYUP:
1086 key = (unsigned char)msg->wparam;
1087 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1088 set_input_key_state( input, key, down );
1089 switch(key)
1091 case VK_SHIFT:
1092 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1093 break;
1094 case VK_CONTROL:
1095 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1096 break;
1097 case VK_MENU:
1098 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1099 break;
1101 break;
1105 /* release the hardware message currently being processed by the given thread */
1106 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1107 int remove, user_handle_t new_win )
1109 struct thread_input *input = queue->input;
1110 struct message *msg;
1112 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1114 if (msg->unique_id == hw_id) break;
1116 if (&msg->entry == &input->msg_list) return; /* not found */
1118 /* clear the queue bit for that message */
1119 if (remove || new_win)
1121 struct message *other;
1122 int clr_bit;
1124 clr_bit = get_hardware_msg_bit( msg );
1125 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1127 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1129 clr_bit = 0;
1130 break;
1133 if (clr_bit) clear_queue_bits( queue, clr_bit );
1136 if (new_win) /* set the new window */
1138 struct thread *owner = get_window_thread( new_win );
1139 if (owner)
1141 if (owner->queue->input == input)
1143 msg->win = new_win;
1144 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1145 remove = 0;
1147 release_object( owner );
1150 if (remove)
1152 update_input_key_state( input, msg );
1153 list_remove( &msg->entry );
1154 free_message( msg );
1158 /* find the window that should receive a given hardware message */
1159 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1160 unsigned int *msg_code )
1162 user_handle_t win = 0;
1164 *msg_code = msg->msg;
1165 if (is_keyboard_msg( msg ))
1167 if (input && !(win = input->focus))
1169 win = input->active;
1170 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1173 else /* mouse message */
1175 if (!input || !(win = input->capture))
1177 if (!(win = msg->win) || !is_window_visible( win ))
1179 if (input) win = window_from_point( input->desktop, msg->x, msg->y );
1183 return win;
1186 /* queue a hardware message into a given thread input */
1187 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1189 user_handle_t win;
1190 struct thread *thread;
1191 struct thread_input *input;
1192 unsigned int msg_code;
1194 last_input_time = get_tick_count();
1196 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1197 if (!win || !(thread = get_window_thread(win)))
1199 free( msg );
1200 return;
1202 input = thread->queue->input;
1204 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1205 else
1207 msg->unique_id = 0; /* will be set once we return it to the app */
1208 list_add_tail( &input->msg_list, &msg->entry );
1209 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1211 release_object( thread );
1214 /* check message filter for a hardware message */
1215 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1216 user_handle_t filter_win, unsigned int first, unsigned int last )
1218 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1220 /* we can only test the window for a keyboard message since the
1221 * dest window for a mouse message depends on hittest */
1222 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1223 return 0;
1224 /* the message code is final for a keyboard message, we can simply check it */
1225 return check_msg_filter( msg_code, first, last );
1227 else /* mouse message */
1229 /* we need to check all possible values that the message can have in the end */
1231 if (check_msg_filter( msg_code, first, last )) return 1;
1232 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1234 /* all other messages can become non-client messages */
1235 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1237 /* clicks can become double-clicks or non-client double-clicks */
1238 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1239 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1241 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1242 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1244 return 0;
1249 /* find a hardware message for the given queue */
1250 static int get_hardware_message( struct thread *thread, int hw_id, user_handle_t filter_win,
1251 unsigned int first, unsigned int last, struct get_message_reply *reply )
1253 struct thread_input *input = thread->queue->input;
1254 struct thread *win_thread;
1255 struct list *ptr;
1256 user_handle_t win;
1257 int clear_bits, got_one = 0;
1258 unsigned int msg_code;
1260 ptr = list_head( &input->msg_list );
1261 if (hw_id)
1263 while (ptr)
1265 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1266 if (msg->unique_id == hw_id) break;
1267 ptr = list_next( &input->msg_list, ptr );
1269 if (!ptr) ptr = list_head( &input->msg_list );
1270 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1273 if (ptr == list_head( &input->msg_list ))
1274 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1275 else
1276 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1278 while (ptr)
1280 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1281 ptr = list_next( &input->msg_list, ptr );
1282 win = find_hardware_message_window( input, msg, &msg_code );
1283 if (!win || !(win_thread = get_window_thread( win )))
1285 /* no window at all, remove it */
1286 update_input_key_state( input, msg );
1287 list_remove( &msg->entry );
1288 free_message( msg );
1289 continue;
1291 if (win_thread != thread)
1293 if (win_thread->queue->input == input)
1295 /* wake the other thread */
1296 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1297 got_one = 1;
1299 else
1301 /* for another thread input, drop it */
1302 update_input_key_state( input, msg );
1303 list_remove( &msg->entry );
1304 free_message( msg );
1306 release_object( win_thread );
1307 continue;
1309 release_object( win_thread );
1311 /* if we already got a message for another thread, or if it doesn't
1312 * match the filter we skip it */
1313 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1315 clear_bits &= ~get_hardware_msg_bit( msg );
1316 continue;
1318 /* now we can return it */
1319 if (!msg->unique_id) msg->unique_id = get_unique_id();
1320 reply->type = MSG_HARDWARE;
1321 reply->win = win;
1322 reply->msg = msg_code;
1323 reply->wparam = msg->wparam;
1324 reply->lparam = msg->lparam;
1325 reply->x = msg->x;
1326 reply->y = msg->y;
1327 reply->time = msg->time;
1328 reply->info = msg->info;
1329 reply->hw_id = msg->unique_id;
1330 return 1;
1332 /* nothing found, clear the hardware queue bits */
1333 clear_queue_bits( thread->queue, clear_bits );
1334 return 0;
1337 /* increment (or decrement if 'incr' is negative) the queue paint count */
1338 void inc_queue_paint_count( struct thread *thread, int incr )
1340 struct msg_queue *queue = thread->queue;
1342 assert( queue );
1344 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1346 if (queue->paint_count)
1347 set_queue_bits( queue, QS_PAINT );
1348 else
1349 clear_queue_bits( queue, QS_PAINT );
1353 /* remove all messages and timers belonging to a certain window */
1354 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1356 struct msg_queue *queue = thread->queue;
1357 struct list *ptr;
1358 int i;
1360 if (!queue) return;
1362 /* remove timers */
1364 ptr = list_head( &queue->pending_timers );
1365 while (ptr)
1367 struct list *next = list_next( &queue->pending_timers, ptr );
1368 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1369 if (timer->win == win) free_timer( queue, timer );
1370 ptr = next;
1372 ptr = list_head( &queue->expired_timers );
1373 while (ptr)
1375 struct list *next = list_next( &queue->expired_timers, ptr );
1376 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1377 if (timer->win == win) free_timer( queue, timer );
1378 ptr = next;
1381 /* remove messages */
1382 for (i = 0; i < NB_MSG_KINDS; i++)
1384 struct list *ptr, *next;
1386 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1388 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1389 if (msg->win == win) remove_queue_message( queue, msg, i );
1393 thread_input_cleanup_window( queue, win );
1396 /* post a message to a window; used by socket handling */
1397 void post_message( user_handle_t win, unsigned int message,
1398 unsigned int wparam, unsigned int lparam )
1400 struct message *msg;
1401 struct thread *thread = get_window_thread( win );
1403 if (!thread) return;
1405 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1407 msg->type = MSG_POSTED;
1408 msg->win = get_user_full_handle( win );
1409 msg->msg = message;
1410 msg->wparam = wparam;
1411 msg->lparam = lparam;
1412 msg->time = get_tick_count();
1413 msg->x = 0;
1414 msg->y = 0;
1415 msg->info = 0;
1416 msg->hook = 0;
1417 msg->hook_proc = NULL;
1418 msg->result = NULL;
1419 msg->data = NULL;
1420 msg->data_size = 0;
1422 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1423 set_queue_bits( thread->queue, QS_POSTMESSAGE );
1425 release_object( thread );
1428 /* post a win event */
1429 void post_win_event( struct thread *thread, unsigned int event,
1430 user_handle_t win, unsigned int object_id,
1431 unsigned int child_id, void *hook_proc,
1432 const WCHAR *module, size_t module_size,
1433 user_handle_t hook)
1435 struct message *msg;
1437 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1439 msg->type = MSG_WINEVENT;
1440 msg->win = get_user_full_handle( win );
1441 msg->msg = event;
1442 msg->wparam = object_id;
1443 msg->lparam = child_id;
1444 msg->time = get_tick_count();
1445 msg->x = 0;
1446 msg->y = 0;
1447 msg->info = get_thread_id( current );
1448 msg->result = NULL;
1449 msg->hook = hook;
1450 msg->hook_proc = hook_proc;
1452 if ((msg->data = malloc( module_size )))
1454 msg->data_size = module_size;
1455 memcpy( msg->data, module, module_size );
1457 if (debug_level > 1)
1458 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1459 get_thread_id(thread), event, win, object_id, child_id );
1460 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1461 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1463 else
1464 free( msg );
1468 /* get the message queue of the current thread */
1469 DECL_HANDLER(get_msg_queue)
1471 struct msg_queue *queue = get_current_queue();
1473 reply->handle = 0;
1474 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1478 /* set the current message queue wakeup mask */
1479 DECL_HANDLER(set_queue_mask)
1481 struct msg_queue *queue = get_current_queue();
1483 if (queue)
1485 queue->wake_mask = req->wake_mask;
1486 queue->changed_mask = req->changed_mask;
1487 reply->wake_bits = queue->wake_bits;
1488 reply->changed_bits = queue->changed_bits;
1489 if (is_signaled( queue ))
1491 /* if skip wait is set, do what would have been done in the subsequent wait */
1492 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1493 else wake_up( &queue->obj, 0 );
1499 /* get the current message queue status */
1500 DECL_HANDLER(get_queue_status)
1502 struct msg_queue *queue = current->queue;
1503 if (queue)
1505 reply->wake_bits = queue->wake_bits;
1506 reply->changed_bits = queue->changed_bits;
1507 if (req->clear) queue->changed_bits = 0;
1509 else reply->wake_bits = reply->changed_bits = 0;
1513 /* send a message to a thread queue */
1514 DECL_HANDLER(send_message)
1516 struct message *msg;
1517 struct msg_queue *send_queue = get_current_queue();
1518 struct msg_queue *recv_queue = NULL;
1519 struct thread *thread = NULL;
1521 if (req->id)
1523 if (!(thread = get_thread_from_id( req->id ))) return;
1525 else if (req->type != MSG_HARDWARE)
1527 /* only hardware messages are allowed without destination thread */
1528 set_error( STATUS_INVALID_PARAMETER );
1529 return;
1532 if (thread && !(recv_queue = thread->queue))
1534 set_error( STATUS_INVALID_PARAMETER );
1535 release_object( thread );
1536 return;
1538 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1540 set_error( STATUS_TIMEOUT );
1541 release_object( thread );
1542 return;
1545 if ((msg = mem_alloc( sizeof(*msg) )))
1547 msg->type = req->type;
1548 msg->win = get_user_full_handle( req->win );
1549 msg->msg = req->msg;
1550 msg->wparam = req->wparam;
1551 msg->lparam = req->lparam;
1552 msg->time = req->time;
1553 msg->x = req->x;
1554 msg->y = req->y;
1555 msg->info = req->info;
1556 msg->hook = 0;
1557 msg->hook_proc = NULL;
1558 msg->result = NULL;
1559 msg->data = NULL;
1560 msg->data_size = 0;
1562 switch(msg->type)
1564 case MSG_OTHER_PROCESS:
1565 msg->data_size = get_req_data_size();
1566 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1568 free( msg );
1569 break;
1571 /* fall through */
1572 case MSG_ASCII:
1573 case MSG_UNICODE:
1574 case MSG_CALLBACK:
1575 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1576 req->timeout, req->callback, req->info )))
1578 free_message( msg );
1579 break;
1581 /* fall through */
1582 case MSG_NOTIFY:
1583 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1584 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1585 break;
1586 case MSG_POSTED:
1587 /* needed for posted DDE messages */
1588 msg->data_size = get_req_data_size();
1589 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1591 free( msg );
1592 break;
1594 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1595 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1596 break;
1597 case MSG_HARDWARE:
1598 queue_hardware_message( recv_queue, msg );
1599 break;
1600 case MSG_CALLBACK_RESULT: /* cannot send this one */
1601 default:
1602 set_error( STATUS_INVALID_PARAMETER );
1603 free( msg );
1604 break;
1607 if (thread) release_object( thread );
1611 /* get a message from the current queue */
1612 DECL_HANDLER(get_message)
1614 struct timer *timer;
1615 struct list *ptr;
1616 struct msg_queue *queue = get_current_queue();
1617 user_handle_t get_win = get_user_full_handle( req->get_win );
1619 reply->active_hooks = get_active_hooks();
1621 if (!queue) return;
1622 gettimeofday( &queue->last_get_msg, NULL );
1624 /* first check for sent messages */
1625 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1627 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1628 receive_message( queue, msg, reply );
1629 return;
1631 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1633 /* clear changed bits so we can wait on them if we don't find a message */
1634 queue->changed_bits = 0;
1636 /* then check for posted messages */
1637 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1638 return;
1640 /* then check for any raw hardware message */
1641 if (filter_contains_hw_range( req->get_first, req->get_last ) &&
1642 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1643 return;
1645 /* now check for WM_PAINT */
1646 if (queue->paint_count &&
1647 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1648 (reply->win = find_window_to_repaint( get_win, current )))
1650 reply->type = MSG_POSTED;
1651 reply->msg = WM_PAINT;
1652 reply->wparam = 0;
1653 reply->lparam = 0;
1654 reply->x = 0;
1655 reply->y = 0;
1656 reply->time = get_tick_count();
1657 reply->info = 0;
1658 return;
1661 /* now check for timer */
1662 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1663 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1665 reply->type = MSG_POSTED;
1666 reply->win = timer->win;
1667 reply->msg = timer->msg;
1668 reply->wparam = timer->id;
1669 reply->lparam = timer->lparam;
1670 reply->x = 0;
1671 reply->y = 0;
1672 reply->time = get_tick_count();
1673 reply->info = 0;
1674 return;
1677 done:
1678 set_error( STATUS_PENDING ); /* FIXME */
1682 /* reply to a sent message */
1683 DECL_HANDLER(reply_message)
1685 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1686 else if (current->queue->recv_result)
1687 reply_message( current->queue, req->result, 0, req->remove,
1688 get_req_data(), get_req_data_size() );
1692 /* accept the current hardware message */
1693 DECL_HANDLER(accept_hardware_message)
1695 if (current->queue)
1696 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1697 else
1698 set_error( STATUS_ACCESS_DENIED );
1702 /* retrieve the reply for the last message sent */
1703 DECL_HANDLER(get_message_reply)
1705 struct message_result *result;
1706 struct list *entry;
1707 struct msg_queue *queue = current->queue;
1709 if (queue)
1711 set_error( STATUS_PENDING );
1712 reply->result = 0;
1714 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1716 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1717 if (result->replied || req->cancel)
1719 if (result->replied)
1721 reply->result = result->result;
1722 set_error( result->error );
1723 if (result->data)
1725 size_t data_len = min( result->data_size, get_reply_max_size() );
1726 set_reply_data_ptr( result->data, data_len );
1727 result->data = NULL;
1728 result->data_size = 0;
1731 remove_result_from_sender( result );
1733 entry = list_head( &queue->send_result );
1734 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1735 else
1737 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1738 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1742 else set_error( STATUS_ACCESS_DENIED );
1746 /* set a window timer */
1747 DECL_HANDLER(set_win_timer)
1749 struct timer *timer;
1750 struct msg_queue *queue;
1751 struct thread *thread = NULL;
1752 user_handle_t win = 0;
1753 unsigned int id = req->id;
1755 if (req->win)
1757 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1759 set_error( STATUS_INVALID_HANDLE );
1760 return;
1762 if (thread->process != current->process)
1764 release_object( thread );
1765 set_error( STATUS_ACCESS_DENIED );
1766 return;
1768 queue = thread->queue;
1769 /* remove it if it existed already */
1770 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1772 else
1774 queue = get_current_queue();
1775 /* find a free id for it */
1778 id = queue->next_timer_id;
1779 if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1781 while (find_timer( queue, 0, req->msg, id ));
1784 if ((timer = set_timer( queue, req->rate )))
1786 timer->win = win;
1787 timer->msg = req->msg;
1788 timer->id = id;
1789 timer->lparam = req->lparam;
1790 reply->id = id;
1792 if (thread) release_object( thread );
1795 /* kill a window timer */
1796 DECL_HANDLER(kill_win_timer)
1798 struct timer *timer;
1799 struct thread *thread;
1800 user_handle_t win = 0;
1802 if (req->win)
1804 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1806 set_error( STATUS_INVALID_HANDLE );
1807 return;
1809 if (thread->process != current->process)
1811 release_object( thread );
1812 set_error( STATUS_ACCESS_DENIED );
1813 return;
1816 else thread = (struct thread *)grab_object( current );
1818 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1819 free_timer( thread->queue, timer );
1820 else
1821 set_error( STATUS_INVALID_PARAMETER );
1823 release_object( thread );
1827 /* attach (or detach) thread inputs */
1828 DECL_HANDLER(attach_thread_input)
1830 struct thread *thread_from = get_thread_from_id( req->tid_from );
1831 struct thread *thread_to = get_thread_from_id( req->tid_to );
1833 if (!thread_from || !thread_to)
1835 if (thread_from) release_object( thread_from );
1836 if (thread_to) release_object( thread_to );
1837 return;
1839 if (thread_from != thread_to)
1841 if (req->attach) attach_thread_input( thread_from, thread_to );
1842 else
1844 if (thread_from->queue && thread_to->queue &&
1845 thread_from->queue->input == thread_to->queue->input)
1846 detach_thread_input( thread_from );
1847 else
1848 set_error( STATUS_ACCESS_DENIED );
1851 else set_error( STATUS_ACCESS_DENIED );
1852 release_object( thread_from );
1853 release_object( thread_to );
1857 /* get thread input data */
1858 DECL_HANDLER(get_thread_input)
1860 struct thread *thread = NULL;
1861 struct thread_input *input;
1863 if (req->tid)
1865 if (!(thread = get_thread_from_id( req->tid ))) return;
1866 input = thread->queue ? thread->queue->input : NULL;
1868 else input = foreground_input; /* get the foreground thread info */
1870 if (input)
1872 reply->focus = input->focus;
1873 reply->capture = input->capture;
1874 reply->active = input->active;
1875 reply->menu_owner = input->menu_owner;
1876 reply->move_size = input->move_size;
1877 reply->caret = input->caret;
1878 reply->rect = input->caret_rect;
1880 else
1882 reply->focus = 0;
1883 reply->capture = 0;
1884 reply->active = 0;
1885 reply->menu_owner = 0;
1886 reply->move_size = 0;
1887 reply->caret = 0;
1888 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1890 /* foreground window is active window of foreground thread */
1891 reply->foreground = foreground_input ? foreground_input->active : 0;
1892 if (thread) release_object( thread );
1896 /* retrieve queue keyboard state for a given thread */
1897 DECL_HANDLER(get_key_state)
1899 struct thread *thread;
1900 struct thread_input *input;
1902 if (!(thread = get_thread_from_id( req->tid ))) return;
1903 input = thread->queue ? thread->queue->input : NULL;
1904 if (input)
1906 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1907 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1909 release_object( thread );
1913 /* set queue keyboard state for a given thread */
1914 DECL_HANDLER(set_key_state)
1916 struct thread *thread = NULL;
1917 struct thread_input *input;
1919 if (!(thread = get_thread_from_id( req->tid ))) return;
1920 input = thread->queue ? thread->queue->input : NULL;
1921 if (input)
1923 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1924 if (size) memcpy( input->keystate, get_req_data(), size );
1926 release_object( thread );
1930 /* set the system foreground window */
1931 DECL_HANDLER(set_foreground_window)
1933 struct msg_queue *queue = get_current_queue();
1935 reply->previous = foreground_input ? foreground_input->active : 0;
1936 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1937 reply->send_msg_new = FALSE;
1939 if (req->handle)
1941 struct thread *thread;
1943 if (is_top_level_window( req->handle ) &&
1944 ((thread = get_window_thread( req->handle ))))
1946 foreground_input = thread->queue->input;
1947 reply->send_msg_new = (foreground_input != queue->input);
1948 release_object( thread );
1950 else set_error( STATUS_INVALID_HANDLE );
1952 else foreground_input = NULL;
1956 /* set the current thread focus window */
1957 DECL_HANDLER(set_focus_window)
1959 struct msg_queue *queue = get_current_queue();
1961 reply->previous = 0;
1962 if (queue && check_queue_input_window( queue, req->handle ))
1964 reply->previous = queue->input->focus;
1965 queue->input->focus = get_user_full_handle( req->handle );
1970 /* set the current thread active window */
1971 DECL_HANDLER(set_active_window)
1973 struct msg_queue *queue = get_current_queue();
1975 reply->previous = 0;
1976 if (queue && check_queue_input_window( queue, req->handle ))
1978 if (!req->handle || make_window_active( req->handle ))
1980 reply->previous = queue->input->active;
1981 queue->input->active = get_user_full_handle( req->handle );
1983 else set_error( STATUS_INVALID_HANDLE );
1988 /* set the current thread capture window */
1989 DECL_HANDLER(set_capture_window)
1991 struct msg_queue *queue = get_current_queue();
1993 reply->previous = reply->full_handle = 0;
1994 if (queue && check_queue_input_window( queue, req->handle ))
1996 struct thread_input *input = queue->input;
1998 reply->previous = input->capture;
1999 input->capture = get_user_full_handle( req->handle );
2000 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2001 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2002 reply->full_handle = input->capture;
2007 /* Set the current thread caret window */
2008 DECL_HANDLER(set_caret_window)
2010 struct msg_queue *queue = get_current_queue();
2012 reply->previous = 0;
2013 if (queue && check_queue_input_window( queue, req->handle ))
2015 struct thread_input *input = queue->input;
2017 reply->previous = input->caret;
2018 reply->old_rect = input->caret_rect;
2019 reply->old_hide = input->caret_hide;
2020 reply->old_state = input->caret_state;
2022 set_caret_window( input, get_user_full_handle(req->handle) );
2023 input->caret_rect.right = input->caret_rect.left + req->width;
2024 input->caret_rect.bottom = input->caret_rect.top + req->height;
2029 /* Set the current thread caret information */
2030 DECL_HANDLER(set_caret_info)
2032 struct msg_queue *queue = get_current_queue();
2033 struct thread_input *input;
2035 if (!queue) return;
2036 input = queue->input;
2037 reply->full_handle = input->caret;
2038 reply->old_rect = input->caret_rect;
2039 reply->old_hide = input->caret_hide;
2040 reply->old_state = input->caret_state;
2042 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2044 set_error( STATUS_ACCESS_DENIED );
2045 return;
2047 if (req->flags & SET_CARET_POS)
2049 input->caret_rect.right += req->x - input->caret_rect.left;
2050 input->caret_rect.bottom += req->y - input->caret_rect.top;
2051 input->caret_rect.left = req->x;
2052 input->caret_rect.top = req->y;
2054 if (req->flags & SET_CARET_HIDE)
2056 input->caret_hide += req->hide;
2057 if (input->caret_hide < 0) input->caret_hide = 0;
2059 if (req->flags & SET_CARET_STATE)
2061 if (req->state == -1) input->caret_state = !input->caret_state;
2062 else input->caret_state = !!req->state;
2067 /* get the time of the last input event */
2068 DECL_HANDLER(get_last_input_time)
2070 reply->time = last_input_time;