Don't dump content of output buffers.
[wine/wine-kai.git] / server / queue.c
blobd827c0fd133d72c98095e00db029077f5eefa202
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_result *recv_next; /* next in receiver list */
52 struct msg_queue *sender; /* sender queue */
53 struct msg_queue *receiver; /* receiver queue */
54 int replied; /* has it been replied to? */
55 unsigned int result; /* reply result */
56 unsigned int error; /* error code to pass back to sender */
57 struct message *callback_msg; /* message to queue for callback */
58 void *data; /* message reply data */
59 unsigned int data_size; /* size of message reply data */
60 struct timeout_user *timeout; /* result timeout */
63 struct message
65 struct list entry; /* entry in message list */
66 enum message_type type; /* message type */
67 user_handle_t win; /* window handle */
68 unsigned int msg; /* message code */
69 unsigned int wparam; /* parameters */
70 unsigned int lparam; /* parameters */
71 int x; /* x position */
72 int y; /* y position */
73 unsigned int time; /* message time */
74 unsigned int info; /* extra info */
75 user_handle_t hook; /* winevent hook handle */
76 void *hook_proc; /* winevent hook proc address */
77 void *data; /* message data for sent messages */
78 unsigned int data_size; /* size of message data */
79 unsigned int unique_id; /* unique id for nested hw message waits */
80 struct message_result *result; /* result in sender queue */
83 struct timer
85 struct list entry; /* entry in timer list */
86 struct timeval when; /* next expiration */
87 unsigned int rate; /* timer rate in ms */
88 user_handle_t win; /* window handle */
89 unsigned int msg; /* message to post */
90 unsigned int id; /* timer id */
91 unsigned int lparam; /* lparam for message */
94 struct thread_input
96 struct object obj; /* object header */
97 user_handle_t focus; /* focus window */
98 user_handle_t capture; /* capture window */
99 user_handle_t active; /* active window */
100 user_handle_t menu_owner; /* current menu owner window */
101 user_handle_t move_size; /* current moving/resizing window */
102 user_handle_t caret; /* caret window */
103 rectangle_t caret_rect; /* caret rectangle */
104 int caret_hide; /* caret hide count */
105 int caret_state; /* caret on/off state */
106 struct list msg_list; /* list of hardware messages */
107 unsigned char keystate[256]; /* state of each key */
110 struct msg_queue
112 struct object obj; /* object header */
113 unsigned int wake_bits; /* wakeup bits */
114 unsigned int wake_mask; /* wakeup mask */
115 unsigned int changed_bits; /* changed wakeup bits */
116 unsigned int changed_mask; /* changed wakeup mask */
117 int paint_count; /* pending paint messages count */
118 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
119 struct list send_result; /* stack of sent messages waiting for result */
120 struct list callback_result; /* list of callback messages waiting for result */
121 struct message_result *recv_result; /* stack of received messages waiting for result */
122 struct list pending_timers; /* list of pending timers */
123 struct list expired_timers; /* list of expired timers */
124 unsigned int next_timer_id; /* id for the next timer with a 0 window */
125 struct timeout_user *timeout; /* timeout for next timer to expire */
126 struct thread_input *input; /* thread input descriptor */
127 struct hook_table *hooks; /* hook table */
128 struct timeval last_get_msg; /* time of last get message call */
131 static void msg_queue_dump( struct object *obj, int verbose );
132 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
133 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
134 static int msg_queue_signaled( struct object *obj, struct thread *thread );
135 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
136 static void msg_queue_destroy( struct object *obj );
137 static void thread_input_dump( struct object *obj, int verbose );
138 static void thread_input_destroy( struct object *obj );
139 static void timer_callback( void *private );
141 static const struct object_ops msg_queue_ops =
143 sizeof(struct msg_queue), /* size */
144 msg_queue_dump, /* dump */
145 msg_queue_add_queue, /* add_queue */
146 msg_queue_remove_queue, /* remove_queue */
147 msg_queue_signaled, /* signaled */
148 msg_queue_satisfied, /* satisfied */
149 no_signal, /* signal */
150 no_get_fd, /* get_fd */
151 no_close_handle, /* close_handle */
152 msg_queue_destroy /* destroy */
156 static const struct object_ops thread_input_ops =
158 sizeof(struct thread_input), /* size */
159 thread_input_dump, /* dump */
160 no_add_queue, /* add_queue */
161 NULL, /* remove_queue */
162 NULL, /* signaled */
163 NULL, /* satisfied */
164 no_signal, /* signal */
165 no_get_fd, /* get_fd */
166 no_close_handle, /* close_handle */
167 thread_input_destroy /* destroy */
170 /* pointer to input structure of foreground thread */
171 static struct thread_input *foreground_input;
172 static unsigned int last_input_time;
175 /* set the caret window in a given thread input */
176 static void set_caret_window( struct thread_input *input, user_handle_t win )
178 if (!win || win != input->caret)
180 input->caret_rect.left = 0;
181 input->caret_rect.top = 0;
182 input->caret_rect.right = 0;
183 input->caret_rect.bottom = 0;
185 input->caret = win;
186 input->caret_hide = 1;
187 input->caret_state = 0;
190 /* create a thread input object */
191 static struct thread_input *create_thread_input(void)
193 struct thread_input *input;
195 if ((input = alloc_object( &thread_input_ops )))
197 input->focus = 0;
198 input->capture = 0;
199 input->active = 0;
200 input->menu_owner = 0;
201 input->move_size = 0;
202 list_init( &input->msg_list );
203 set_caret_window( input, 0 );
204 memset( input->keystate, 0, sizeof(input->keystate) );
206 return input;
209 /* release the thread input data of a given thread */
210 static inline void release_thread_input( struct thread *thread )
212 struct thread_input *input = thread->queue->input;
214 if (!input) return;
215 release_object( input );
216 thread->queue->input = NULL;
219 /* create a message queue object */
220 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
222 struct msg_queue *queue;
223 int i;
225 if (!input && !(input = create_thread_input())) return NULL;
226 if ((queue = alloc_object( &msg_queue_ops )))
228 queue->wake_bits = 0;
229 queue->wake_mask = 0;
230 queue->changed_bits = 0;
231 queue->changed_mask = 0;
232 queue->paint_count = 0;
233 queue->recv_result = NULL;
234 queue->next_timer_id = 1;
235 queue->timeout = NULL;
236 queue->input = (struct thread_input *)grab_object( input );
237 queue->hooks = NULL;
238 gettimeofday( &queue->last_get_msg, NULL );
239 list_init( &queue->send_result );
240 list_init( &queue->callback_result );
241 list_init( &queue->pending_timers );
242 list_init( &queue->expired_timers );
243 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
245 thread->queue = queue;
246 if (!thread->process->queue)
247 thread->process->queue = (struct msg_queue *)grab_object( queue );
249 release_object( input );
250 return queue;
253 /* free the message queue of a thread at thread exit */
254 void free_msg_queue( struct thread *thread )
256 struct process *process = thread->process;
257 struct thread_input *input;
259 remove_thread_hooks( thread );
260 if (!thread->queue) return;
261 if (process->queue == thread->queue) /* is it the process main queue? */
263 release_object( process->queue );
264 process->queue = NULL;
265 if (process->idle_event)
267 set_event( process->idle_event );
268 release_object( process->idle_event );
269 process->idle_event = NULL;
272 input = thread->queue->input;
273 release_object( thread->queue );
274 thread->queue = NULL;
277 /* get the hook table for a given thread */
278 struct hook_table *get_queue_hooks( struct thread *thread )
280 if (!thread->queue) return NULL;
281 return thread->queue->hooks;
284 /* set the hook table for a given thread, allocating the queue if needed */
285 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
287 struct msg_queue *queue = thread->queue;
288 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
289 if (queue->hooks) release_object( queue->hooks );
290 queue->hooks = hooks;
293 /* check the queue status */
294 inline static int is_signaled( struct msg_queue *queue )
296 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
299 /* set some queue bits */
300 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
302 queue->wake_bits |= bits;
303 queue->changed_bits |= bits;
304 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
307 /* clear some queue bits */
308 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
310 queue->wake_bits &= ~bits;
311 queue->changed_bits &= ~bits;
314 /* check whether msg is a keyboard message */
315 inline static int is_keyboard_msg( struct message *msg )
317 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
320 /* check if message is matched by the filter */
321 inline static int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
323 return (msg >= first && msg <= last);
326 /* check whether a message filter contains at least one potential hardware message */
327 inline static int filter_contains_hw_range( unsigned int first, unsigned int last )
329 /* hardware message ranges are (in numerical order):
330 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
331 * WM_KEYFIRST .. WM_KEYLAST
332 * WM_MOUSEFIRST .. WM_MOUSELAST
334 if (last < WM_NCMOUSEFIRST) return 0;
335 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
336 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
337 if (first > WM_MOUSELAST) return 0;
338 return 1;
341 /* get the QS_* bit corresponding to a given hardware message */
342 inline static int get_hardware_msg_bit( struct message *msg )
344 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
345 if (is_keyboard_msg( msg )) return QS_KEY;
346 return QS_MOUSEBUTTON;
349 /* get the current thread queue, creating it if needed */
350 inline static struct msg_queue *get_current_queue(void)
352 struct msg_queue *queue = current->queue;
353 if (!queue) queue = create_msg_queue( current, NULL );
354 return queue;
357 /* get a (pseudo-)unique id to tag hardware messages */
358 inline static unsigned int get_unique_id(void)
360 static unsigned int id;
361 if (!++id) id = 1; /* avoid an id of 0 */
362 return id;
365 /* try to merge a message with the last in the list; return 1 if successful */
366 static int merge_message( struct thread_input *input, const struct message *msg )
368 struct message *prev;
369 struct list *ptr = list_tail( &input->msg_list );
371 if (!ptr) return 0;
372 prev = LIST_ENTRY( ptr, struct message, entry );
373 if (prev->unique_id) return 0;
374 if (prev->result) return 0;
375 if (prev->win != msg->win) return 0;
376 if (prev->msg != msg->msg) return 0;
377 if (prev->type != msg->type) return 0;
378 /* now we can merge it */
379 prev->wparam = msg->wparam;
380 prev->lparam = msg->lparam;
381 prev->x = msg->x;
382 prev->y = msg->y;
383 prev->time = msg->time;
384 prev->info = msg->info;
385 return 1;
388 /* free a result structure */
389 static void free_result( struct message_result *result )
391 if (result->timeout) remove_timeout_user( result->timeout );
392 if (result->data) free( result->data );
393 if (result->callback_msg) free( result->callback_msg );
394 free( result );
397 /* remove the result from the sender list it is on */
398 static inline void remove_result_from_sender( struct message_result *result )
400 assert( result->sender );
402 list_remove( &result->sender_entry );
403 result->sender = NULL;
404 if (!result->receiver) free_result( result );
407 /* store the message result in the appropriate structure */
408 static void store_message_result( struct message_result *res, unsigned int result,
409 unsigned int error )
411 res->result = result;
412 res->error = error;
413 res->replied = 1;
414 if (res->timeout)
416 remove_timeout_user( res->timeout );
417 res->timeout = NULL;
419 if (res->sender)
421 if (res->callback_msg)
423 /* queue the callback message in the sender queue */
424 res->callback_msg->lparam = result;
425 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
426 set_queue_bits( res->sender, QS_SENDMESSAGE );
427 res->callback_msg = NULL;
428 remove_result_from_sender( res );
430 else
432 /* wake sender queue if waiting on this result */
433 if (list_head(&res->sender->send_result) == &res->sender_entry)
434 set_queue_bits( res->sender, QS_SMRESULT );
440 /* free a message when deleting a queue or window */
441 static void free_message( struct message *msg )
443 struct message_result *result = msg->result;
444 if (result)
446 if (result->sender)
448 result->receiver = NULL;
449 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
451 else free_result( result );
453 if (msg->data) free( msg->data );
454 free( msg );
457 /* remove (and free) a message from a message list */
458 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
459 enum message_kind kind )
461 list_remove( &msg->entry );
462 switch(kind)
464 case SEND_MESSAGE:
465 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
466 break;
467 case POST_MESSAGE:
468 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_POSTMESSAGE );
469 break;
471 free_message( msg );
474 /* message timed out without getting a reply */
475 static void result_timeout( void *private )
477 struct message_result *result = private;
479 assert( !result->replied );
481 result->timeout = NULL;
482 store_message_result( result, 0, STATUS_TIMEOUT );
485 /* allocate and fill a message result structure */
486 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
487 struct msg_queue *recv_queue,
488 struct message *msg, unsigned int timeout,
489 void *callback, unsigned int callback_data )
491 struct message_result *result = mem_alloc( sizeof(*result) );
492 if (result)
494 result->sender = send_queue;
495 result->receiver = recv_queue;
496 result->replied = 0;
497 result->data = NULL;
498 result->data_size = 0;
499 result->timeout = NULL;
501 if (msg->type == MSG_CALLBACK)
503 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
504 if (!callback_msg)
506 free( result );
507 return NULL;
509 callback_msg->type = MSG_CALLBACK_RESULT;
510 callback_msg->win = msg->win;
511 callback_msg->msg = msg->msg;
512 callback_msg->wparam = (unsigned int)callback;
513 callback_msg->lparam = 0;
514 callback_msg->time = get_tick_count();
515 callback_msg->x = 0;
516 callback_msg->y = 0;
517 callback_msg->info = callback_data;
518 callback_msg->hook = 0;
519 callback_msg->hook_proc = NULL;
520 callback_msg->result = NULL;
521 callback_msg->data = NULL;
522 callback_msg->data_size = 0;
524 result->callback_msg = callback_msg;
525 list_add_head( &send_queue->callback_result, &result->sender_entry );
527 else
529 result->callback_msg = NULL;
530 list_add_head( &send_queue->send_result, &result->sender_entry );
533 if (timeout != -1)
535 struct timeval when;
536 gettimeofday( &when, NULL );
537 add_timeout( &when, timeout );
538 result->timeout = add_timeout_user( &when, result_timeout, result );
541 return result;
544 /* receive a message, removing it from the sent queue */
545 static void receive_message( struct msg_queue *queue, struct message *msg,
546 struct get_message_reply *reply )
548 struct message_result *result = msg->result;
550 reply->total = msg->data_size;
551 if (msg->data_size > get_reply_max_size())
553 set_error( STATUS_BUFFER_OVERFLOW );
554 return;
556 reply->type = msg->type;
557 reply->win = msg->win;
558 reply->msg = msg->msg;
559 reply->wparam = msg->wparam;
560 reply->lparam = msg->lparam;
561 reply->x = msg->x;
562 reply->y = msg->y;
563 reply->time = msg->time;
564 reply->info = msg->info;
565 reply->hook = msg->hook;
566 reply->hook_proc = msg->hook_proc;
568 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
570 list_remove( &msg->entry );
571 /* put the result on the receiver result stack */
572 if (result)
574 result->recv_next = queue->recv_result;
575 queue->recv_result = result;
577 free( msg );
578 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
581 /* set the result of the current received message */
582 static void reply_message( struct msg_queue *queue, unsigned int result,
583 unsigned int error, int remove, const void *data, size_t len )
585 struct message_result *res = queue->recv_result;
587 if (remove)
589 queue->recv_result = res->recv_next;
590 res->receiver = NULL;
591 if (!res->sender) /* no one waiting for it */
593 free_result( res );
594 return;
597 if (!res->replied)
599 if (len && (res->data = memdup( data, len ))) res->data_size = len;
600 store_message_result( res, result, error );
604 /* retrieve a posted message */
605 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
606 unsigned int first, unsigned int last, unsigned int flags,
607 struct get_message_reply *reply )
609 struct message *msg;
611 /* check against the filters */
612 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
614 if (msg->msg == WM_QUIT) goto found; /* WM_QUIT is never filtered */
615 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
616 if (!check_msg_filter( msg->msg, first, last )) continue;
617 goto found; /* found one */
619 return 0;
621 /* return it to the app */
622 found:
623 reply->total = msg->data_size;
624 if (msg->data_size > get_reply_max_size())
626 set_error( STATUS_BUFFER_OVERFLOW );
627 return 1;
629 reply->type = msg->type;
630 reply->win = msg->win;
631 reply->msg = msg->msg;
632 reply->wparam = msg->wparam;
633 reply->lparam = msg->lparam;
634 reply->x = msg->x;
635 reply->y = msg->y;
636 reply->time = msg->time;
637 reply->info = msg->info;
639 if (flags & GET_MSG_REMOVE)
641 if (msg->data)
643 set_reply_data_ptr( msg->data, msg->data_size );
644 msg->data = NULL;
645 msg->data_size = 0;
647 remove_queue_message( queue, msg, POST_MESSAGE );
649 else if (msg->data) set_reply_data( msg->data, msg->data_size );
651 return 1;
654 /* empty a message list and free all the messages */
655 static void empty_msg_list( struct list *list )
657 struct list *ptr;
659 while ((ptr = list_head( list )) != NULL)
661 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
662 list_remove( &msg->entry );
663 free_message( msg );
667 /* cleanup all pending results when deleting a queue */
668 static void cleanup_results( struct msg_queue *queue )
670 struct list *entry;
672 while ((entry = list_head( &queue->send_result )) != NULL)
674 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
677 while ((entry = list_head( &queue->callback_result )) != NULL)
679 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
682 while (queue->recv_result)
683 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
686 /* check if the thread owning the queue is hung (not checking for messages) */
687 static int is_queue_hung( struct msg_queue *queue )
689 struct timeval now;
690 struct wait_queue_entry *entry;
692 gettimeofday( &now, NULL );
693 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
694 return 0; /* less than 5 seconds since last get message -> not hung */
696 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
698 if (entry->thread->queue == queue)
699 return 0; /* thread is waiting on queue -> not hung */
701 return 1;
704 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
706 struct msg_queue *queue = (struct msg_queue *)obj;
707 struct process *process = entry->thread->process;
709 /* a thread can only wait on its own queue */
710 if (entry->thread->queue != queue)
712 set_error( STATUS_ACCESS_DENIED );
713 return 0;
715 /* if waiting on the main process queue, set the idle event */
716 if (process->queue == queue)
718 if (process->idle_event) set_event( process->idle_event );
720 add_queue( obj, entry );
721 return 1;
724 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
726 struct msg_queue *queue = (struct msg_queue *)obj;
727 struct process *process = entry->thread->process;
729 remove_queue( obj, entry );
731 assert( entry->thread->queue == queue );
733 /* if waiting on the main process queue, reset the idle event */
734 if (process->queue == queue)
736 if (process->idle_event) reset_event( process->idle_event );
740 static void msg_queue_dump( struct object *obj, int verbose )
742 struct msg_queue *queue = (struct msg_queue *)obj;
743 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
744 queue->wake_bits, queue->wake_mask );
747 static int msg_queue_signaled( struct object *obj, struct thread *thread )
749 struct msg_queue *queue = (struct msg_queue *)obj;
750 return is_signaled( queue );
753 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
755 struct msg_queue *queue = (struct msg_queue *)obj;
756 queue->wake_mask = 0;
757 queue->changed_mask = 0;
758 return 0; /* Not abandoned */
761 static void msg_queue_destroy( struct object *obj )
763 struct msg_queue *queue = (struct msg_queue *)obj;
764 struct list *ptr;
765 int i;
767 cleanup_results( queue );
768 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
770 while ((ptr = list_head( &queue->pending_timers )))
772 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
773 list_remove( &timer->entry );
774 free( timer );
776 while ((ptr = list_head( &queue->expired_timers )))
778 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
779 list_remove( &timer->entry );
780 free( timer );
782 if (queue->timeout) remove_timeout_user( queue->timeout );
783 if (queue->input) release_object( queue->input );
784 if (queue->hooks) release_object( queue->hooks );
787 static void thread_input_dump( struct object *obj, int verbose )
789 struct thread_input *input = (struct thread_input *)obj;
790 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
791 input->focus, input->capture, input->active );
794 static void thread_input_destroy( struct object *obj )
796 struct thread_input *input = (struct thread_input *)obj;
798 if (foreground_input == input) foreground_input = NULL;
799 empty_msg_list( &input->msg_list );
802 /* fix the thread input data when a window is destroyed */
803 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
805 struct thread_input *input = queue->input;
807 if (window == input->focus) input->focus = 0;
808 if (window == input->capture) input->capture = 0;
809 if (window == input->active) input->active = 0;
810 if (window == input->menu_owner) input->menu_owner = 0;
811 if (window == input->move_size) input->move_size = 0;
812 if (window == input->caret) set_caret_window( input, 0 );
815 /* check if the specified window can be set in the input data of a given queue */
816 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
818 struct thread *thread;
819 int ret = 0;
821 if (!window) return 1; /* we can always clear the data */
823 if ((thread = get_window_thread( window )))
825 ret = (queue->input == thread->queue->input);
826 if (!ret) set_error( STATUS_ACCESS_DENIED );
827 release_object( thread );
829 else set_error( STATUS_INVALID_HANDLE );
831 return ret;
834 /* make sure the specified thread has a queue */
835 int init_thread_queue( struct thread *thread )
837 if (thread->queue) return 1;
838 return (create_msg_queue( thread, NULL ) != NULL);
841 /* attach two thread input data structures */
842 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
844 struct thread_input *input;
846 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
847 input = (struct thread_input *)grab_object( thread_to->queue->input );
849 if (thread_from->queue)
851 release_thread_input( thread_from );
852 thread_from->queue->input = input;
854 else
856 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
858 memset( input->keystate, 0, sizeof(input->keystate) );
859 return 1;
862 /* detach two thread input data structures */
863 static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
865 struct thread_input *input;
867 if (!thread_from->queue || !thread_to->queue ||
868 thread_from->queue->input != thread_to->queue->input)
870 set_error( STATUS_ACCESS_DENIED );
871 return;
873 if ((input = create_thread_input()))
875 release_thread_input( thread_from );
876 thread_from->queue->input = input;
881 /* set the next timer to expire */
882 static void set_next_timer( struct msg_queue *queue )
884 struct list *ptr;
886 if (queue->timeout)
888 remove_timeout_user( queue->timeout );
889 queue->timeout = NULL;
891 if ((ptr = list_head( &queue->pending_timers )))
893 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
894 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
896 /* set/clear QS_TIMER bit */
897 if (list_empty( &queue->expired_timers ))
898 clear_queue_bits( queue, QS_TIMER );
899 else
900 set_queue_bits( queue, QS_TIMER );
903 /* find a timer from its window and id */
904 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
905 unsigned int msg, unsigned int id )
907 struct list *ptr;
909 /* we need to search both lists */
911 LIST_FOR_EACH( ptr, &queue->pending_timers )
913 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
914 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
916 LIST_FOR_EACH( ptr, &queue->expired_timers )
918 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
919 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
921 return NULL;
924 /* callback for the next timer expiration */
925 static void timer_callback( void *private )
927 struct msg_queue *queue = private;
928 struct list *ptr;
930 queue->timeout = NULL;
931 /* move on to the next timer */
932 ptr = list_head( &queue->pending_timers );
933 list_remove( ptr );
934 list_add_tail( &queue->expired_timers, ptr );
935 set_next_timer( queue );
938 /* link a timer at its rightful place in the queue list */
939 static void link_timer( struct msg_queue *queue, struct timer *timer )
941 struct list *ptr;
943 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
945 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
946 if (!time_before( &t->when, &timer->when )) break;
948 list_add_before( ptr, &timer->entry );
951 /* remove a timer from the queue timer list and free it */
952 static void free_timer( struct msg_queue *queue, struct timer *timer )
954 list_remove( &timer->entry );
955 free( timer );
956 set_next_timer( queue );
959 /* restart an expired timer */
960 static void restart_timer( struct msg_queue *queue, struct timer *timer )
962 struct timeval now;
964 list_remove( &timer->entry );
965 gettimeofday( &now, NULL );
966 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
967 link_timer( queue, timer );
968 set_next_timer( queue );
971 /* find an expired timer matching the filtering parameters */
972 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
973 unsigned int get_first, unsigned int get_last,
974 int remove )
976 struct list *ptr;
978 LIST_FOR_EACH( ptr, &queue->expired_timers )
980 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
981 if (win && timer->win != win) continue;
982 if (check_msg_filter( timer->msg, get_first, get_last ))
984 if (remove) restart_timer( queue, timer );
985 return timer;
988 return NULL;
991 /* add a timer */
992 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
994 struct timer *timer = mem_alloc( sizeof(*timer) );
995 if (timer)
997 timer->rate = max( rate, 1 );
998 gettimeofday( &timer->when, NULL );
999 add_timeout( &timer->when, rate );
1000 link_timer( queue, timer );
1001 /* check if we replaced the next timer */
1002 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1004 return timer;
1007 /* change the input key state for a given key */
1008 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1010 if (down)
1012 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1013 input->keystate[key] |= 0x80;
1015 else input->keystate[key] &= ~0x80;
1018 /* update the input key state for a keyboard message */
1019 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1021 unsigned char key;
1022 int down = 0, extended;
1024 switch (msg->msg)
1026 case WM_LBUTTONDOWN:
1027 down = 1;
1028 /* fall through */
1029 case WM_LBUTTONUP:
1030 set_input_key_state( input, VK_LBUTTON, down );
1031 break;
1032 case WM_MBUTTONDOWN:
1033 down = 1;
1034 /* fall through */
1035 case WM_MBUTTONUP:
1036 set_input_key_state( input, VK_MBUTTON, down );
1037 break;
1038 case WM_RBUTTONDOWN:
1039 down = 1;
1040 /* fall through */
1041 case WM_RBUTTONUP:
1042 set_input_key_state( input, VK_RBUTTON, down );
1043 break;
1044 case WM_XBUTTONDOWN:
1045 down = 1;
1046 /* fall through */
1047 case WM_XBUTTONUP:
1048 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1049 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1050 break;
1051 case WM_KEYDOWN:
1052 case WM_SYSKEYDOWN:
1053 down = 1;
1054 /* fall through */
1055 case WM_KEYUP:
1056 case WM_SYSKEYUP:
1057 key = (unsigned char)msg->wparam;
1058 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1059 set_input_key_state( input, key, down );
1060 switch(key)
1062 case VK_SHIFT:
1063 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1064 break;
1065 case VK_CONTROL:
1066 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1067 break;
1068 case VK_MENU:
1069 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1070 break;
1072 break;
1076 /* release the hardware message currently being processed by the given thread */
1077 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1078 int remove, user_handle_t new_win )
1080 struct thread_input *input = queue->input;
1081 struct message *msg;
1083 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1085 if (msg->unique_id == hw_id) break;
1087 if (&msg->entry == &input->msg_list) return; /* not found */
1089 /* clear the queue bit for that message */
1090 if (remove || new_win)
1092 struct message *other;
1093 int clr_bit;
1095 clr_bit = get_hardware_msg_bit( msg );
1096 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1098 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1100 clr_bit = 0;
1101 break;
1104 if (clr_bit) clear_queue_bits( queue, clr_bit );
1107 if (new_win) /* set the new window */
1109 struct thread *owner = get_window_thread( new_win );
1110 if (owner)
1112 if (owner->queue->input == input)
1114 msg->win = new_win;
1115 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1116 remove = 0;
1118 release_object( owner );
1121 if (remove)
1123 update_input_key_state( input, msg );
1124 list_remove( &msg->entry );
1125 free_message( msg );
1129 /* find the window that should receive a given hardware message */
1130 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1131 unsigned int *msg_code )
1133 user_handle_t win = 0;
1135 *msg_code = msg->msg;
1136 if (is_keyboard_msg( msg ))
1138 if (input && !(win = input->focus))
1140 win = input->active;
1141 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1144 else /* mouse message */
1146 if (!input || !(win = input->capture))
1148 if (!(win = msg->win) || !is_window_visible( win ))
1149 win = window_from_point( msg->x, msg->y );
1152 return win;
1155 /* queue a hardware message into a given thread input */
1156 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1158 user_handle_t win;
1159 struct thread *thread;
1160 struct thread_input *input;
1161 unsigned int msg_code;
1163 last_input_time = get_tick_count();
1165 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1166 if (!win || !(thread = get_window_thread(win)))
1168 free( msg );
1169 return;
1171 input = thread->queue->input;
1173 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1174 else
1176 msg->unique_id = 0; /* will be set once we return it to the app */
1177 list_add_tail( &input->msg_list, &msg->entry );
1178 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1180 release_object( thread );
1183 /* check message filter for a hardware message */
1184 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1185 user_handle_t filter_win, unsigned int first, unsigned int last )
1187 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1189 /* we can only test the window for a keyboard message since the
1190 * dest window for a mouse message depends on hittest */
1191 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1192 return 0;
1193 /* the message code is final for a keyboard message, we can simply check it */
1194 return check_msg_filter( msg_code, first, last );
1196 else /* mouse message */
1198 /* we need to check all possible values that the message can have in the end */
1200 if (check_msg_filter( msg_code, first, last )) return 1;
1201 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1203 /* all other messages can become non-client messages */
1204 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1206 /* clicks can become double-clicks or non-client double-clicks */
1207 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1208 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1210 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1211 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1213 return 0;
1218 /* find a hardware message for the given queue */
1219 static int get_hardware_message( struct thread *thread, int hw_id, user_handle_t filter_win,
1220 unsigned int first, unsigned int last, struct get_message_reply *reply )
1222 struct thread_input *input = thread->queue->input;
1223 struct thread *win_thread;
1224 struct list *ptr;
1225 user_handle_t win;
1226 int clear_bits, got_one = 0;
1227 unsigned int msg_code;
1229 ptr = list_head( &input->msg_list );
1230 if (hw_id)
1232 while (ptr)
1234 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1235 if (msg->unique_id == hw_id) break;
1236 ptr = list_next( &input->msg_list, ptr );
1238 if (!ptr) ptr = list_head( &input->msg_list );
1239 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1242 if (ptr == list_head( &input->msg_list ))
1243 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1244 else
1245 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1247 while (ptr)
1249 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1250 ptr = list_next( &input->msg_list, ptr );
1251 win = find_hardware_message_window( input, msg, &msg_code );
1252 if (!win || !(win_thread = get_window_thread( win )))
1254 /* no window at all, remove it */
1255 update_input_key_state( input, msg );
1256 list_remove( &msg->entry );
1257 free_message( msg );
1258 continue;
1260 if (win_thread != thread)
1262 if (win_thread->queue->input == input)
1264 /* wake the other thread */
1265 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1266 got_one = 1;
1268 else
1270 /* for another thread input, drop it */
1271 update_input_key_state( input, msg );
1272 list_remove( &msg->entry );
1273 free_message( msg );
1275 release_object( win_thread );
1276 continue;
1278 release_object( win_thread );
1280 /* if we already got a message for another thread, or if it doesn't
1281 * match the filter we skip it */
1282 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1284 clear_bits &= ~get_hardware_msg_bit( msg );
1285 continue;
1287 /* now we can return it */
1288 if (!msg->unique_id) msg->unique_id = get_unique_id();
1289 reply->type = MSG_HARDWARE;
1290 reply->win = win;
1291 reply->msg = msg_code;
1292 reply->wparam = msg->wparam;
1293 reply->lparam = msg->lparam;
1294 reply->x = msg->x;
1295 reply->y = msg->y;
1296 reply->time = msg->time;
1297 reply->info = msg->info;
1298 reply->hw_id = msg->unique_id;
1299 return 1;
1301 /* nothing found, clear the hardware queue bits */
1302 clear_queue_bits( thread->queue, clear_bits );
1303 return 0;
1306 /* increment (or decrement if 'incr' is negative) the queue paint count */
1307 void inc_queue_paint_count( struct thread *thread, int incr )
1309 struct msg_queue *queue = thread->queue;
1311 assert( queue );
1313 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1315 if (queue->paint_count)
1316 set_queue_bits( queue, QS_PAINT );
1317 else
1318 clear_queue_bits( queue, QS_PAINT );
1322 /* remove all messages and timers belonging to a certain window */
1323 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1325 struct msg_queue *queue = thread->queue;
1326 struct list *ptr;
1327 int i;
1329 if (!queue) return;
1331 /* remove timers */
1333 ptr = list_head( &queue->pending_timers );
1334 while (ptr)
1336 struct list *next = list_next( &queue->pending_timers, ptr );
1337 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1338 if (timer->win == win) free_timer( queue, timer );
1339 ptr = next;
1341 ptr = list_head( &queue->expired_timers );
1342 while (ptr)
1344 struct list *next = list_next( &queue->expired_timers, ptr );
1345 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1346 if (timer->win == win) free_timer( queue, timer );
1347 ptr = next;
1350 /* remove messages */
1351 for (i = 0; i < NB_MSG_KINDS; i++)
1353 struct list *ptr, *next;
1355 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1357 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1358 if (msg->win == win) remove_queue_message( queue, msg, i );
1362 thread_input_cleanup_window( queue, win );
1365 /* post a message to a window; used by socket handling */
1366 void post_message( user_handle_t win, unsigned int message,
1367 unsigned int wparam, unsigned int lparam )
1369 struct message *msg;
1370 struct thread *thread = get_window_thread( win );
1372 if (!thread) return;
1374 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1376 msg->type = MSG_POSTED;
1377 msg->win = get_user_full_handle( win );
1378 msg->msg = message;
1379 msg->wparam = wparam;
1380 msg->lparam = lparam;
1381 msg->time = get_tick_count();
1382 msg->x = 0;
1383 msg->y = 0;
1384 msg->info = 0;
1385 msg->hook = 0;
1386 msg->hook_proc = NULL;
1387 msg->result = NULL;
1388 msg->data = NULL;
1389 msg->data_size = 0;
1391 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1392 set_queue_bits( thread->queue, QS_POSTMESSAGE );
1394 release_object( thread );
1397 /* post a win event */
1398 void post_win_event( struct thread *thread, unsigned int event,
1399 user_handle_t win, unsigned int object_id,
1400 unsigned int child_id, void *hook_proc,
1401 const WCHAR *module, size_t module_size,
1402 user_handle_t hook)
1404 struct message *msg;
1406 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1408 msg->type = MSG_WINEVENT;
1409 msg->win = get_user_full_handle( win );
1410 msg->msg = event;
1411 msg->wparam = object_id;
1412 msg->lparam = child_id;
1413 msg->time = get_tick_count();
1414 msg->x = 0;
1415 msg->y = 0;
1416 msg->info = get_thread_id( current );
1417 msg->result = NULL;
1418 msg->hook = hook;
1419 msg->hook_proc = hook_proc;
1421 if ((msg->data = malloc( module_size )))
1423 msg->data_size = module_size;
1424 memcpy( msg->data, module, module_size );
1426 if (debug_level > 1)
1427 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1428 get_thread_id(thread), event, win, object_id, child_id );
1429 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1430 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1432 else
1433 free( msg );
1437 /* get the message queue of the current thread */
1438 DECL_HANDLER(get_msg_queue)
1440 struct msg_queue *queue = get_current_queue();
1442 reply->handle = 0;
1443 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1447 /* set the current message queue wakeup mask */
1448 DECL_HANDLER(set_queue_mask)
1450 struct msg_queue *queue = get_current_queue();
1452 if (queue)
1454 queue->wake_mask = req->wake_mask;
1455 queue->changed_mask = req->changed_mask;
1456 reply->wake_bits = queue->wake_bits;
1457 reply->changed_bits = queue->changed_bits;
1458 if (is_signaled( queue ))
1460 /* if skip wait is set, do what would have been done in the subsequent wait */
1461 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1462 else wake_up( &queue->obj, 0 );
1468 /* get the current message queue status */
1469 DECL_HANDLER(get_queue_status)
1471 struct msg_queue *queue = current->queue;
1472 if (queue)
1474 reply->wake_bits = queue->wake_bits;
1475 reply->changed_bits = queue->changed_bits;
1476 if (req->clear) queue->changed_bits = 0;
1478 else reply->wake_bits = reply->changed_bits = 0;
1482 /* send a message to a thread queue */
1483 DECL_HANDLER(send_message)
1485 struct message *msg;
1486 struct msg_queue *send_queue = get_current_queue();
1487 struct msg_queue *recv_queue = NULL;
1488 struct thread *thread = NULL;
1490 if (req->id)
1492 if (!(thread = get_thread_from_id( req->id ))) return;
1494 else if (req->type != MSG_HARDWARE)
1496 /* only hardware messages are allowed without destination thread */
1497 set_error( STATUS_INVALID_PARAMETER );
1498 return;
1501 if (thread && !(recv_queue = thread->queue))
1503 set_error( STATUS_INVALID_PARAMETER );
1504 release_object( thread );
1505 return;
1507 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1509 set_error( STATUS_TIMEOUT );
1510 release_object( thread );
1511 return;
1514 if ((msg = mem_alloc( sizeof(*msg) )))
1516 msg->type = req->type;
1517 msg->win = get_user_full_handle( req->win );
1518 msg->msg = req->msg;
1519 msg->wparam = req->wparam;
1520 msg->lparam = req->lparam;
1521 msg->time = req->time;
1522 msg->x = req->x;
1523 msg->y = req->y;
1524 msg->info = req->info;
1525 msg->hook = 0;
1526 msg->hook_proc = NULL;
1527 msg->result = NULL;
1528 msg->data = NULL;
1529 msg->data_size = 0;
1531 switch(msg->type)
1533 case MSG_OTHER_PROCESS:
1534 msg->data_size = get_req_data_size();
1535 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1537 free( msg );
1538 break;
1540 /* fall through */
1541 case MSG_ASCII:
1542 case MSG_UNICODE:
1543 case MSG_CALLBACK:
1544 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1545 req->timeout, req->callback, req->info )))
1547 free_message( msg );
1548 break;
1550 /* fall through */
1551 case MSG_NOTIFY:
1552 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1553 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1554 break;
1555 case MSG_POSTED:
1556 /* needed for posted DDE messages */
1557 msg->data_size = get_req_data_size();
1558 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1560 free( msg );
1561 break;
1563 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1564 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1565 break;
1566 case MSG_HARDWARE:
1567 queue_hardware_message( recv_queue, msg );
1568 break;
1569 case MSG_CALLBACK_RESULT: /* cannot send this one */
1570 default:
1571 set_error( STATUS_INVALID_PARAMETER );
1572 free( msg );
1573 break;
1576 if (thread) release_object( thread );
1580 /* get a message from the current queue */
1581 DECL_HANDLER(get_message)
1583 struct timer *timer;
1584 struct list *ptr;
1585 struct msg_queue *queue = get_current_queue();
1586 user_handle_t get_win = get_user_full_handle( req->get_win );
1588 reply->active_hooks = get_active_hooks();
1590 if (!queue) return;
1591 gettimeofday( &queue->last_get_msg, NULL );
1593 /* first check for sent messages */
1594 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1596 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1597 receive_message( queue, msg, reply );
1598 return;
1600 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1602 /* clear changed bits so we can wait on them if we don't find a message */
1603 queue->changed_bits = 0;
1605 /* then check for posted messages */
1606 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1607 return;
1609 /* then check for any raw hardware message */
1610 if (filter_contains_hw_range( req->get_first, req->get_last ) &&
1611 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1612 return;
1614 /* now check for WM_PAINT */
1615 if (queue->paint_count &&
1616 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1617 (reply->win = find_window_to_repaint( get_win, current )))
1619 reply->type = MSG_POSTED;
1620 reply->msg = WM_PAINT;
1621 reply->wparam = 0;
1622 reply->lparam = 0;
1623 reply->x = 0;
1624 reply->y = 0;
1625 reply->time = get_tick_count();
1626 reply->info = 0;
1627 return;
1630 /* now check for timer */
1631 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1632 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1634 reply->type = MSG_POSTED;
1635 reply->win = timer->win;
1636 reply->msg = timer->msg;
1637 reply->wparam = timer->id;
1638 reply->lparam = timer->lparam;
1639 reply->x = 0;
1640 reply->y = 0;
1641 reply->time = get_tick_count();
1642 reply->info = 0;
1643 return;
1646 done:
1647 set_error( STATUS_PENDING ); /* FIXME */
1651 /* reply to a sent message */
1652 DECL_HANDLER(reply_message)
1654 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1655 else if (current->queue->recv_result)
1656 reply_message( current->queue, req->result, 0, req->remove,
1657 get_req_data(), get_req_data_size() );
1661 /* accept the current hardware message */
1662 DECL_HANDLER(accept_hardware_message)
1664 if (current->queue)
1665 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1666 else
1667 set_error( STATUS_ACCESS_DENIED );
1671 /* retrieve the reply for the last message sent */
1672 DECL_HANDLER(get_message_reply)
1674 struct message_result *result;
1675 struct list *entry;
1676 struct msg_queue *queue = current->queue;
1678 if (queue)
1680 set_error( STATUS_PENDING );
1681 reply->result = 0;
1683 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1685 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1686 if (result->replied || req->cancel)
1688 if (result->replied)
1690 reply->result = result->result;
1691 set_error( result->error );
1692 if (result->data)
1694 size_t data_len = min( result->data_size, get_reply_max_size() );
1695 set_reply_data_ptr( result->data, data_len );
1696 result->data = NULL;
1697 result->data_size = 0;
1700 remove_result_from_sender( result );
1702 entry = list_head( &queue->send_result );
1703 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1704 else
1706 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1707 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1711 else set_error( STATUS_ACCESS_DENIED );
1715 /* set a window timer */
1716 DECL_HANDLER(set_win_timer)
1718 struct timer *timer;
1719 struct msg_queue *queue;
1720 struct thread *thread = NULL;
1721 user_handle_t win = 0;
1722 unsigned int id = req->id;
1724 if (req->win)
1726 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1728 set_error( STATUS_INVALID_HANDLE );
1729 return;
1731 if (thread->process != current->process)
1733 release_object( thread );
1734 set_error( STATUS_ACCESS_DENIED );
1735 return;
1737 queue = thread->queue;
1738 /* remove it if it existed already */
1739 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1741 else
1743 queue = get_current_queue();
1744 /* find a free id for it */
1747 id = queue->next_timer_id;
1748 if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1750 while (find_timer( queue, 0, req->msg, id ));
1753 if ((timer = set_timer( queue, req->rate )))
1755 timer->win = win;
1756 timer->msg = req->msg;
1757 timer->id = id;
1758 timer->lparam = req->lparam;
1759 reply->id = id;
1761 if (thread) release_object( thread );
1764 /* kill a window timer */
1765 DECL_HANDLER(kill_win_timer)
1767 struct timer *timer;
1768 struct thread *thread;
1769 user_handle_t win = 0;
1771 if (req->win)
1773 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1775 set_error( STATUS_INVALID_HANDLE );
1776 return;
1778 if (thread->process != current->process)
1780 release_object( thread );
1781 set_error( STATUS_ACCESS_DENIED );
1782 return;
1785 else thread = (struct thread *)grab_object( current );
1787 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1788 free_timer( thread->queue, timer );
1789 else
1790 set_error( STATUS_INVALID_PARAMETER );
1792 release_object( thread );
1796 /* attach (or detach) thread inputs */
1797 DECL_HANDLER(attach_thread_input)
1799 struct thread *thread_from = get_thread_from_id( req->tid_from );
1800 struct thread *thread_to = get_thread_from_id( req->tid_to );
1802 if (!thread_from || !thread_to)
1804 if (thread_from) release_object( thread_from );
1805 if (thread_to) release_object( thread_to );
1806 return;
1808 if (thread_from != thread_to)
1810 if (req->attach) attach_thread_input( thread_from, thread_to );
1811 else detach_thread_input( thread_from, thread_to );
1813 else set_error( STATUS_ACCESS_DENIED );
1814 release_object( thread_from );
1815 release_object( thread_to );
1819 /* get thread input data */
1820 DECL_HANDLER(get_thread_input)
1822 struct thread *thread = NULL;
1823 struct thread_input *input;
1825 if (req->tid)
1827 if (!(thread = get_thread_from_id( req->tid ))) return;
1828 input = thread->queue ? thread->queue->input : NULL;
1830 else input = foreground_input; /* get the foreground thread info */
1832 if (input)
1834 reply->focus = input->focus;
1835 reply->capture = input->capture;
1836 reply->active = input->active;
1837 reply->menu_owner = input->menu_owner;
1838 reply->move_size = input->move_size;
1839 reply->caret = input->caret;
1840 reply->rect = input->caret_rect;
1842 else
1844 reply->focus = 0;
1845 reply->capture = 0;
1846 reply->active = 0;
1847 reply->menu_owner = 0;
1848 reply->move_size = 0;
1849 reply->caret = 0;
1850 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1852 /* foreground window is active window of foreground thread */
1853 reply->foreground = foreground_input ? foreground_input->active : 0;
1854 if (thread) release_object( thread );
1858 /* retrieve queue keyboard state for a given thread */
1859 DECL_HANDLER(get_key_state)
1861 struct thread *thread;
1862 struct thread_input *input;
1864 if (!(thread = get_thread_from_id( req->tid ))) return;
1865 input = thread->queue ? thread->queue->input : NULL;
1866 if (input)
1868 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1869 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1871 release_object( thread );
1875 /* set queue keyboard state for a given thread */
1876 DECL_HANDLER(set_key_state)
1878 struct thread *thread = NULL;
1879 struct thread_input *input;
1881 if (!(thread = get_thread_from_id( req->tid ))) return;
1882 input = thread->queue ? thread->queue->input : NULL;
1883 if (input)
1885 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1886 if (size) memcpy( input->keystate, get_req_data(), size );
1888 release_object( thread );
1892 /* set the system foreground window */
1893 DECL_HANDLER(set_foreground_window)
1895 struct msg_queue *queue = get_current_queue();
1897 reply->previous = foreground_input ? foreground_input->active : 0;
1898 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1899 reply->send_msg_new = FALSE;
1901 if (req->handle)
1903 struct thread *thread;
1905 if (is_top_level_window( req->handle ) &&
1906 ((thread = get_window_thread( req->handle ))))
1908 foreground_input = thread->queue->input;
1909 reply->send_msg_new = (foreground_input != queue->input);
1910 release_object( thread );
1912 else set_error( STATUS_INVALID_HANDLE );
1914 else foreground_input = NULL;
1918 /* set the current thread focus window */
1919 DECL_HANDLER(set_focus_window)
1921 struct msg_queue *queue = get_current_queue();
1923 reply->previous = 0;
1924 if (queue && check_queue_input_window( queue, req->handle ))
1926 reply->previous = queue->input->focus;
1927 queue->input->focus = get_user_full_handle( req->handle );
1932 /* set the current thread active window */
1933 DECL_HANDLER(set_active_window)
1935 struct msg_queue *queue = get_current_queue();
1937 reply->previous = 0;
1938 if (queue && check_queue_input_window( queue, req->handle ))
1940 if (!req->handle || make_window_active( req->handle ))
1942 reply->previous = queue->input->active;
1943 queue->input->active = get_user_full_handle( req->handle );
1945 else set_error( STATUS_INVALID_HANDLE );
1950 /* set the current thread capture window */
1951 DECL_HANDLER(set_capture_window)
1953 struct msg_queue *queue = get_current_queue();
1955 reply->previous = reply->full_handle = 0;
1956 if (queue && check_queue_input_window( queue, req->handle ))
1958 struct thread_input *input = queue->input;
1960 reply->previous = input->capture;
1961 input->capture = get_user_full_handle( req->handle );
1962 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1963 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1964 reply->full_handle = input->capture;
1969 /* Set the current thread caret window */
1970 DECL_HANDLER(set_caret_window)
1972 struct msg_queue *queue = get_current_queue();
1974 reply->previous = 0;
1975 if (queue && check_queue_input_window( queue, req->handle ))
1977 struct thread_input *input = queue->input;
1979 reply->previous = input->caret;
1980 reply->old_rect = input->caret_rect;
1981 reply->old_hide = input->caret_hide;
1982 reply->old_state = input->caret_state;
1984 set_caret_window( input, get_user_full_handle(req->handle) );
1985 input->caret_rect.right = input->caret_rect.left + req->width;
1986 input->caret_rect.bottom = input->caret_rect.top + req->height;
1991 /* Set the current thread caret information */
1992 DECL_HANDLER(set_caret_info)
1994 struct msg_queue *queue = get_current_queue();
1995 struct thread_input *input;
1997 if (!queue) return;
1998 input = queue->input;
1999 reply->full_handle = input->caret;
2000 reply->old_rect = input->caret_rect;
2001 reply->old_hide = input->caret_hide;
2002 reply->old_state = input->caret_state;
2004 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2006 set_error( STATUS_ACCESS_DENIED );
2007 return;
2009 if (req->flags & SET_CARET_POS)
2011 input->caret_rect.right += req->x - input->caret_rect.left;
2012 input->caret_rect.bottom += req->y - input->caret_rect.top;
2013 input->caret_rect.left = req->x;
2014 input->caret_rect.top = req->y;
2016 if (req->flags & SET_CARET_HIDE)
2018 input->caret_hide += req->hide;
2019 if (input->caret_hide < 0) input->caret_hide = 0;
2021 if (req->flags & SET_CARET_STATE)
2023 if (req->state == -1) input->caret_state = !input->caret_state;
2024 else input->caret_state = !!req->state;
2029 /* get the time of the last input event */
2030 DECL_HANDLER(get_last_input_time)
2032 reply->time = last_input_time;