Already initialize the process in the first init_thread request
[wine.git] / server / queue.c
blob0b40a2a18f66edfbc482f6c8c6466a0e42bf5b3e
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 struct desktop *desktop; /* desktop that this thread input belongs to */
98 user_handle_t focus; /* focus window */
99 user_handle_t capture; /* capture window */
100 user_handle_t active; /* active window */
101 user_handle_t menu_owner; /* current menu owner window */
102 user_handle_t move_size; /* current moving/resizing window */
103 user_handle_t caret; /* caret window */
104 rectangle_t caret_rect; /* caret rectangle */
105 int caret_hide; /* caret hide count */
106 int caret_state; /* caret on/off state */
107 struct list msg_list; /* list of hardware messages */
108 unsigned char keystate[256]; /* state of each key */
111 struct msg_queue
113 struct object obj; /* object header */
114 unsigned int wake_bits; /* wakeup bits */
115 unsigned int wake_mask; /* wakeup mask */
116 unsigned int changed_bits; /* changed wakeup bits */
117 unsigned int changed_mask; /* changed wakeup mask */
118 int paint_count; /* pending paint messages count */
119 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
120 struct list send_result; /* stack of sent messages waiting for result */
121 struct list callback_result; /* list of callback messages waiting for result */
122 struct message_result *recv_result; /* stack of received messages waiting for result */
123 struct list pending_timers; /* list of pending timers */
124 struct list expired_timers; /* list of expired timers */
125 unsigned int next_timer_id; /* id for the next timer with a 0 window */
126 struct timeout_user *timeout; /* timeout for next timer to expire */
127 struct thread_input *input; /* thread input descriptor */
128 struct hook_table *hooks; /* hook table */
129 struct timeval last_get_msg; /* time of last get message call */
132 static void msg_queue_dump( struct object *obj, int verbose );
133 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
134 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
135 static int msg_queue_signaled( struct object *obj, struct thread *thread );
136 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
137 static void msg_queue_destroy( struct object *obj );
138 static void thread_input_dump( struct object *obj, int verbose );
139 static void thread_input_destroy( struct object *obj );
140 static void timer_callback( void *private );
142 static const struct object_ops msg_queue_ops =
144 sizeof(struct msg_queue), /* size */
145 msg_queue_dump, /* dump */
146 msg_queue_add_queue, /* add_queue */
147 msg_queue_remove_queue, /* remove_queue */
148 msg_queue_signaled, /* signaled */
149 msg_queue_satisfied, /* satisfied */
150 no_signal, /* signal */
151 no_get_fd, /* get_fd */
152 no_close_handle, /* close_handle */
153 msg_queue_destroy /* destroy */
157 static const struct object_ops thread_input_ops =
159 sizeof(struct thread_input), /* size */
160 thread_input_dump, /* dump */
161 no_add_queue, /* add_queue */
162 NULL, /* remove_queue */
163 NULL, /* signaled */
164 NULL, /* satisfied */
165 no_signal, /* signal */
166 no_get_fd, /* get_fd */
167 no_close_handle, /* close_handle */
168 thread_input_destroy /* destroy */
171 /* pointer to input structure of foreground thread */
172 static struct thread_input *foreground_input;
173 static unsigned int last_input_time;
176 /* set the caret window in a given thread input */
177 static void set_caret_window( struct thread_input *input, user_handle_t win )
179 if (!win || win != input->caret)
181 input->caret_rect.left = 0;
182 input->caret_rect.top = 0;
183 input->caret_rect.right = 0;
184 input->caret_rect.bottom = 0;
186 input->caret = win;
187 input->caret_hide = 1;
188 input->caret_state = 0;
191 /* create a thread input object */
192 static struct thread_input *create_thread_input( struct thread *thread )
194 struct thread_input *input;
196 if ((input = alloc_object( &thread_input_ops )))
198 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
200 free( input );
201 return NULL;
203 input->focus = 0;
204 input->capture = 0;
205 input->active = 0;
206 input->menu_owner = 0;
207 input->move_size = 0;
208 list_init( &input->msg_list );
209 set_caret_window( input, 0 );
210 memset( input->keystate, 0, sizeof(input->keystate) );
212 return input;
215 /* release the thread input data of a given thread */
216 static inline void release_thread_input( struct thread *thread )
218 struct thread_input *input = thread->queue->input;
220 if (!input) return;
221 release_object( input );
222 thread->queue->input = NULL;
225 /* create a message queue object */
226 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
228 struct msg_queue *queue;
229 int i;
231 if (!input && !(input = create_thread_input( thread ))) return NULL;
232 if ((queue = alloc_object( &msg_queue_ops )))
234 queue->wake_bits = 0;
235 queue->wake_mask = 0;
236 queue->changed_bits = 0;
237 queue->changed_mask = 0;
238 queue->paint_count = 0;
239 queue->recv_result = NULL;
240 queue->next_timer_id = 1;
241 queue->timeout = NULL;
242 queue->input = (struct thread_input *)grab_object( input );
243 queue->hooks = NULL;
244 gettimeofday( &queue->last_get_msg, NULL );
245 list_init( &queue->send_result );
246 list_init( &queue->callback_result );
247 list_init( &queue->pending_timers );
248 list_init( &queue->expired_timers );
249 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
251 thread->queue = queue;
252 if (!thread->process->queue)
253 thread->process->queue = (struct msg_queue *)grab_object( queue );
255 release_object( input );
256 return queue;
259 /* free the message queue of a thread at thread exit */
260 void free_msg_queue( struct thread *thread )
262 struct process *process = thread->process;
263 struct thread_input *input;
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 input = thread->queue->input;
279 release_object( thread->queue );
280 thread->queue = NULL;
283 /* get the hook table for a given thread */
284 struct hook_table *get_queue_hooks( struct thread *thread )
286 if (!thread->queue) return NULL;
287 return thread->queue->hooks;
290 /* set the hook table for a given thread, allocating the queue if needed */
291 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
293 struct msg_queue *queue = thread->queue;
294 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
295 if (queue->hooks) release_object( queue->hooks );
296 queue->hooks = hooks;
299 /* check the queue status */
300 inline static int is_signaled( struct msg_queue *queue )
302 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
305 /* set some queue bits */
306 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
308 queue->wake_bits |= bits;
309 queue->changed_bits |= bits;
310 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
313 /* clear some queue bits */
314 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
316 queue->wake_bits &= ~bits;
317 queue->changed_bits &= ~bits;
320 /* check whether msg is a keyboard message */
321 inline static int is_keyboard_msg( struct message *msg )
323 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
326 /* check if message is matched by the filter */
327 inline static int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
329 return (msg >= first && msg <= last);
332 /* check whether a message filter contains at least one potential hardware message */
333 inline static int filter_contains_hw_range( unsigned int first, unsigned int last )
335 /* hardware message ranges are (in numerical order):
336 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
337 * WM_KEYFIRST .. WM_KEYLAST
338 * WM_MOUSEFIRST .. WM_MOUSELAST
340 if (last < WM_NCMOUSEFIRST) return 0;
341 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
342 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
343 if (first > WM_MOUSELAST) return 0;
344 return 1;
347 /* get the QS_* bit corresponding to a given hardware message */
348 inline static int get_hardware_msg_bit( struct message *msg )
350 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
351 if (is_keyboard_msg( msg )) return QS_KEY;
352 return QS_MOUSEBUTTON;
355 /* get the current thread queue, creating it if needed */
356 inline static struct msg_queue *get_current_queue(void)
358 struct msg_queue *queue = current->queue;
359 if (!queue) queue = create_msg_queue( current, NULL );
360 return queue;
363 /* get a (pseudo-)unique id to tag hardware messages */
364 inline static unsigned int get_unique_id(void)
366 static unsigned int id;
367 if (!++id) id = 1; /* avoid an id of 0 */
368 return id;
371 /* try to merge a message with the last in the list; return 1 if successful */
372 static int merge_message( struct thread_input *input, const struct message *msg )
374 struct message *prev;
375 struct list *ptr = list_tail( &input->msg_list );
377 if (!ptr) return 0;
378 prev = LIST_ENTRY( ptr, struct message, entry );
379 if (prev->unique_id) return 0;
380 if (prev->result) return 0;
381 if (prev->win != msg->win) return 0;
382 if (prev->msg != msg->msg) return 0;
383 if (prev->type != msg->type) return 0;
384 /* now we can merge it */
385 prev->wparam = msg->wparam;
386 prev->lparam = msg->lparam;
387 prev->x = msg->x;
388 prev->y = msg->y;
389 prev->time = msg->time;
390 prev->info = msg->info;
391 return 1;
394 /* free a result structure */
395 static void free_result( struct message_result *result )
397 if (result->timeout) remove_timeout_user( result->timeout );
398 if (result->data) free( result->data );
399 if (result->callback_msg) free( result->callback_msg );
400 free( result );
403 /* remove the result from the sender list it is on */
404 static inline void remove_result_from_sender( struct message_result *result )
406 assert( result->sender );
408 list_remove( &result->sender_entry );
409 result->sender = NULL;
410 if (!result->receiver) free_result( result );
413 /* store the message result in the appropriate structure */
414 static void store_message_result( struct message_result *res, unsigned int result,
415 unsigned int error )
417 res->result = result;
418 res->error = error;
419 res->replied = 1;
420 if (res->timeout)
422 remove_timeout_user( res->timeout );
423 res->timeout = NULL;
425 if (res->sender)
427 if (res->callback_msg)
429 /* queue the callback message in the sender queue */
430 res->callback_msg->lparam = result;
431 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
432 set_queue_bits( res->sender, QS_SENDMESSAGE );
433 res->callback_msg = NULL;
434 remove_result_from_sender( res );
436 else
438 /* wake sender queue if waiting on this result */
439 if (list_head(&res->sender->send_result) == &res->sender_entry)
440 set_queue_bits( res->sender, QS_SMRESULT );
446 /* free a message when deleting a queue or window */
447 static void free_message( struct message *msg )
449 struct message_result *result = msg->result;
450 if (result)
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;
488 store_message_result( result, 0, STATUS_TIMEOUT );
491 /* allocate and fill a message result structure */
492 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
493 struct msg_queue *recv_queue,
494 struct message *msg, unsigned int timeout,
495 void *callback, unsigned int callback_data )
497 struct message_result *result = mem_alloc( sizeof(*result) );
498 if (result)
500 result->sender = send_queue;
501 result->receiver = recv_queue;
502 result->replied = 0;
503 result->data = NULL;
504 result->data_size = 0;
505 result->timeout = NULL;
507 if (msg->type == MSG_CALLBACK)
509 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
510 if (!callback_msg)
512 free( result );
513 return NULL;
515 callback_msg->type = MSG_CALLBACK_RESULT;
516 callback_msg->win = msg->win;
517 callback_msg->msg = msg->msg;
518 callback_msg->wparam = (unsigned int)callback;
519 callback_msg->lparam = 0;
520 callback_msg->time = get_tick_count();
521 callback_msg->x = 0;
522 callback_msg->y = 0;
523 callback_msg->info = callback_data;
524 callback_msg->hook = 0;
525 callback_msg->hook_proc = NULL;
526 callback_msg->result = NULL;
527 callback_msg->data = NULL;
528 callback_msg->data_size = 0;
530 result->callback_msg = callback_msg;
531 list_add_head( &send_queue->callback_result, &result->sender_entry );
533 else
535 result->callback_msg = NULL;
536 list_add_head( &send_queue->send_result, &result->sender_entry );
539 if (timeout != -1)
541 struct timeval when;
542 gettimeofday( &when, NULL );
543 add_timeout( &when, timeout );
544 result->timeout = add_timeout_user( &when, result_timeout, result );
547 return result;
550 /* receive a message, removing it from the sent queue */
551 static void receive_message( struct msg_queue *queue, struct message *msg,
552 struct get_message_reply *reply )
554 struct message_result *result = msg->result;
556 reply->total = msg->data_size;
557 if (msg->data_size > get_reply_max_size())
559 set_error( STATUS_BUFFER_OVERFLOW );
560 return;
562 reply->type = msg->type;
563 reply->win = msg->win;
564 reply->msg = msg->msg;
565 reply->wparam = msg->wparam;
566 reply->lparam = msg->lparam;
567 reply->x = msg->x;
568 reply->y = msg->y;
569 reply->time = msg->time;
570 reply->info = msg->info;
571 reply->hook = msg->hook;
572 reply->hook_proc = msg->hook_proc;
574 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
576 list_remove( &msg->entry );
577 /* put the result on the receiver result stack */
578 if (result)
580 result->recv_next = queue->recv_result;
581 queue->recv_result = result;
583 free( msg );
584 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
587 /* set the result of the current received message */
588 static void reply_message( struct msg_queue *queue, unsigned int result,
589 unsigned int error, int remove, const void *data, size_t len )
591 struct message_result *res = queue->recv_result;
593 if (remove)
595 queue->recv_result = res->recv_next;
596 res->receiver = NULL;
597 if (!res->sender) /* no one waiting for it */
599 free_result( res );
600 return;
603 if (!res->replied)
605 if (len && (res->data = memdup( data, len ))) res->data_size = len;
606 store_message_result( res, result, error );
610 /* retrieve a posted message */
611 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
612 unsigned int first, unsigned int last, unsigned int flags,
613 struct get_message_reply *reply )
615 struct message *msg;
617 /* check against the filters */
618 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
620 if (msg->msg == WM_QUIT) goto found; /* WM_QUIT is never filtered */
621 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
622 if (!check_msg_filter( msg->msg, first, last )) continue;
623 goto found; /* found one */
625 return 0;
627 /* return it to the app */
628 found:
629 reply->total = msg->data_size;
630 if (msg->data_size > get_reply_max_size())
632 set_error( STATUS_BUFFER_OVERFLOW );
633 return 1;
635 reply->type = msg->type;
636 reply->win = msg->win;
637 reply->msg = msg->msg;
638 reply->wparam = msg->wparam;
639 reply->lparam = msg->lparam;
640 reply->x = msg->x;
641 reply->y = msg->y;
642 reply->time = msg->time;
643 reply->info = msg->info;
645 if (flags & GET_MSG_REMOVE)
647 if (msg->data)
649 set_reply_data_ptr( msg->data, msg->data_size );
650 msg->data = NULL;
651 msg->data_size = 0;
653 remove_queue_message( queue, msg, POST_MESSAGE );
655 else if (msg->data) set_reply_data( msg->data, msg->data_size );
657 return 1;
660 /* empty a message list and free all the messages */
661 static void empty_msg_list( struct list *list )
663 struct list *ptr;
665 while ((ptr = list_head( list )) != NULL)
667 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
668 list_remove( &msg->entry );
669 free_message( msg );
673 /* cleanup all pending results when deleting a queue */
674 static void cleanup_results( struct msg_queue *queue )
676 struct list *entry;
678 while ((entry = list_head( &queue->send_result )) != NULL)
680 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
683 while ((entry = list_head( &queue->callback_result )) != NULL)
685 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
688 while (queue->recv_result)
689 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
692 /* check if the thread owning the queue is hung (not checking for messages) */
693 static int is_queue_hung( struct msg_queue *queue )
695 struct timeval now;
696 struct wait_queue_entry *entry;
698 gettimeofday( &now, NULL );
699 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
700 return 0; /* less than 5 seconds since last get message -> not hung */
702 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
704 if (entry->thread->queue == queue)
705 return 0; /* thread is waiting on queue -> not hung */
707 return 1;
710 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
712 struct msg_queue *queue = (struct msg_queue *)obj;
713 struct process *process = entry->thread->process;
715 /* a thread can only wait on its own queue */
716 if (entry->thread->queue != queue)
718 set_error( STATUS_ACCESS_DENIED );
719 return 0;
721 /* if waiting on the main process queue, set the idle event */
722 if (process->queue == queue)
724 if (process->idle_event) set_event( process->idle_event );
726 add_queue( obj, entry );
727 return 1;
730 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
732 struct msg_queue *queue = (struct msg_queue *)obj;
733 struct process *process = entry->thread->process;
735 remove_queue( obj, entry );
737 assert( entry->thread->queue == queue );
739 /* if waiting on the main process queue, reset the idle event */
740 if (process->queue == queue)
742 if (process->idle_event) reset_event( process->idle_event );
746 static void msg_queue_dump( struct object *obj, int verbose )
748 struct msg_queue *queue = (struct msg_queue *)obj;
749 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
750 queue->wake_bits, queue->wake_mask );
753 static int msg_queue_signaled( struct object *obj, struct thread *thread )
755 struct msg_queue *queue = (struct msg_queue *)obj;
756 return is_signaled( queue );
759 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
761 struct msg_queue *queue = (struct msg_queue *)obj;
762 queue->wake_mask = 0;
763 queue->changed_mask = 0;
764 return 0; /* Not abandoned */
767 static void msg_queue_destroy( struct object *obj )
769 struct msg_queue *queue = (struct msg_queue *)obj;
770 struct list *ptr;
771 int i;
773 cleanup_results( queue );
774 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
776 while ((ptr = list_head( &queue->pending_timers )))
778 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
779 list_remove( &timer->entry );
780 free( timer );
782 while ((ptr = list_head( &queue->expired_timers )))
784 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
785 list_remove( &timer->entry );
786 free( timer );
788 if (queue->timeout) remove_timeout_user( queue->timeout );
789 if (queue->input) release_object( queue->input );
790 if (queue->hooks) release_object( queue->hooks );
793 static void thread_input_dump( struct object *obj, int verbose )
795 struct thread_input *input = (struct thread_input *)obj;
796 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
797 input->focus, input->capture, input->active );
800 static void thread_input_destroy( struct object *obj )
802 struct thread_input *input = (struct thread_input *)obj;
804 if (foreground_input == input) foreground_input = NULL;
805 empty_msg_list( &input->msg_list );
806 release_object( input->desktop );
809 /* fix the thread input data when a window is destroyed */
810 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
812 struct thread_input *input = queue->input;
814 if (window == input->focus) input->focus = 0;
815 if (window == input->capture) input->capture = 0;
816 if (window == input->active) input->active = 0;
817 if (window == input->menu_owner) input->menu_owner = 0;
818 if (window == input->move_size) input->move_size = 0;
819 if (window == input->caret) set_caret_window( input, 0 );
822 /* check if the specified window can be set in the input data of a given queue */
823 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
825 struct thread *thread;
826 int ret = 0;
828 if (!window) return 1; /* we can always clear the data */
830 if ((thread = get_window_thread( window )))
832 ret = (queue->input == thread->queue->input);
833 if (!ret) set_error( STATUS_ACCESS_DENIED );
834 release_object( thread );
836 else set_error( STATUS_INVALID_HANDLE );
838 return ret;
841 /* make sure the specified thread has a queue */
842 int init_thread_queue( struct thread *thread )
844 if (thread->queue) return 1;
845 return (create_msg_queue( thread, NULL ) != NULL);
848 /* attach two thread input data structures */
849 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
851 struct desktop *desktop;
852 struct thread_input *input;
854 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
855 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
856 input = (struct thread_input *)grab_object( thread_to->queue->input );
857 if (input->desktop != desktop)
859 set_error( STATUS_ACCESS_DENIED );
860 release_object( input );
861 release_object( desktop );
862 return 0;
864 release_object( desktop );
866 if (thread_from->queue)
868 release_thread_input( thread_from );
869 thread_from->queue->input = input;
871 else
873 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
875 memset( input->keystate, 0, sizeof(input->keystate) );
876 return 1;
879 /* detach two thread input data structures */
880 void detach_thread_input( struct thread *thread_from )
882 struct thread_input *input;
884 if ((input = create_thread_input( thread_from )))
886 release_thread_input( thread_from );
887 thread_from->queue->input = input;
892 /* set the next timer to expire */
893 static void set_next_timer( struct msg_queue *queue )
895 struct list *ptr;
897 if (queue->timeout)
899 remove_timeout_user( queue->timeout );
900 queue->timeout = NULL;
902 if ((ptr = list_head( &queue->pending_timers )))
904 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
905 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
907 /* set/clear QS_TIMER bit */
908 if (list_empty( &queue->expired_timers ))
909 clear_queue_bits( queue, QS_TIMER );
910 else
911 set_queue_bits( queue, QS_TIMER );
914 /* find a timer from its window and id */
915 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
916 unsigned int msg, unsigned int id )
918 struct list *ptr;
920 /* we need to search both lists */
922 LIST_FOR_EACH( ptr, &queue->pending_timers )
924 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
925 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
927 LIST_FOR_EACH( ptr, &queue->expired_timers )
929 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
930 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
932 return NULL;
935 /* callback for the next timer expiration */
936 static void timer_callback( void *private )
938 struct msg_queue *queue = private;
939 struct list *ptr;
941 queue->timeout = NULL;
942 /* move on to the next timer */
943 ptr = list_head( &queue->pending_timers );
944 list_remove( ptr );
945 list_add_tail( &queue->expired_timers, ptr );
946 set_next_timer( queue );
949 /* link a timer at its rightful place in the queue list */
950 static void link_timer( struct msg_queue *queue, struct timer *timer )
952 struct list *ptr;
954 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
956 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
957 if (!time_before( &t->when, &timer->when )) break;
959 list_add_before( ptr, &timer->entry );
962 /* remove a timer from the queue timer list and free it */
963 static void free_timer( struct msg_queue *queue, struct timer *timer )
965 list_remove( &timer->entry );
966 free( timer );
967 set_next_timer( queue );
970 /* restart an expired timer */
971 static void restart_timer( struct msg_queue *queue, struct timer *timer )
973 struct timeval now;
975 list_remove( &timer->entry );
976 gettimeofday( &now, NULL );
977 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
978 link_timer( queue, timer );
979 set_next_timer( queue );
982 /* find an expired timer matching the filtering parameters */
983 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
984 unsigned int get_first, unsigned int get_last,
985 int remove )
987 struct list *ptr;
989 LIST_FOR_EACH( ptr, &queue->expired_timers )
991 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
992 if (win && timer->win != win) continue;
993 if (check_msg_filter( timer->msg, get_first, get_last ))
995 if (remove) restart_timer( queue, timer );
996 return timer;
999 return NULL;
1002 /* add a timer */
1003 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1005 struct timer *timer = mem_alloc( sizeof(*timer) );
1006 if (timer)
1008 timer->rate = max( rate, 1 );
1009 gettimeofday( &timer->when, NULL );
1010 add_timeout( &timer->when, rate );
1011 link_timer( queue, timer );
1012 /* check if we replaced the next timer */
1013 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1015 return timer;
1018 /* change the input key state for a given key */
1019 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1021 if (down)
1023 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1024 input->keystate[key] |= 0x80;
1026 else input->keystate[key] &= ~0x80;
1029 /* update the input key state for a keyboard message */
1030 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1032 unsigned char key;
1033 int down = 0, extended;
1035 switch (msg->msg)
1037 case WM_LBUTTONDOWN:
1038 down = 1;
1039 /* fall through */
1040 case WM_LBUTTONUP:
1041 set_input_key_state( input, VK_LBUTTON, down );
1042 break;
1043 case WM_MBUTTONDOWN:
1044 down = 1;
1045 /* fall through */
1046 case WM_MBUTTONUP:
1047 set_input_key_state( input, VK_MBUTTON, down );
1048 break;
1049 case WM_RBUTTONDOWN:
1050 down = 1;
1051 /* fall through */
1052 case WM_RBUTTONUP:
1053 set_input_key_state( input, VK_RBUTTON, down );
1054 break;
1055 case WM_XBUTTONDOWN:
1056 down = 1;
1057 /* fall through */
1058 case WM_XBUTTONUP:
1059 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1060 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1061 break;
1062 case WM_KEYDOWN:
1063 case WM_SYSKEYDOWN:
1064 down = 1;
1065 /* fall through */
1066 case WM_KEYUP:
1067 case WM_SYSKEYUP:
1068 key = (unsigned char)msg->wparam;
1069 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1070 set_input_key_state( input, key, down );
1071 switch(key)
1073 case VK_SHIFT:
1074 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1075 break;
1076 case VK_CONTROL:
1077 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1078 break;
1079 case VK_MENU:
1080 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1081 break;
1083 break;
1087 /* release the hardware message currently being processed by the given thread */
1088 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1089 int remove, user_handle_t new_win )
1091 struct thread_input *input = queue->input;
1092 struct message *msg;
1094 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1096 if (msg->unique_id == hw_id) break;
1098 if (&msg->entry == &input->msg_list) return; /* not found */
1100 /* clear the queue bit for that message */
1101 if (remove || new_win)
1103 struct message *other;
1104 int clr_bit;
1106 clr_bit = get_hardware_msg_bit( msg );
1107 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1109 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1111 clr_bit = 0;
1112 break;
1115 if (clr_bit) clear_queue_bits( queue, clr_bit );
1118 if (new_win) /* set the new window */
1120 struct thread *owner = get_window_thread( new_win );
1121 if (owner)
1123 if (owner->queue->input == input)
1125 msg->win = new_win;
1126 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1127 remove = 0;
1129 release_object( owner );
1132 if (remove)
1134 update_input_key_state( input, msg );
1135 list_remove( &msg->entry );
1136 free_message( msg );
1140 /* find the window that should receive a given hardware message */
1141 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1142 unsigned int *msg_code )
1144 user_handle_t win = 0;
1146 *msg_code = msg->msg;
1147 if (is_keyboard_msg( msg ))
1149 if (input && !(win = input->focus))
1151 win = input->active;
1152 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1155 else /* mouse message */
1157 if (!input || !(win = input->capture))
1159 if (!(win = msg->win) || !is_window_visible( win ))
1160 win = window_from_point( input->desktop, msg->x, msg->y );
1163 return win;
1166 /* queue a hardware message into a given thread input */
1167 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1169 user_handle_t win;
1170 struct thread *thread;
1171 struct thread_input *input;
1172 unsigned int msg_code;
1174 last_input_time = get_tick_count();
1176 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1177 if (!win || !(thread = get_window_thread(win)))
1179 free( msg );
1180 return;
1182 input = thread->queue->input;
1184 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1185 else
1187 msg->unique_id = 0; /* will be set once we return it to the app */
1188 list_add_tail( &input->msg_list, &msg->entry );
1189 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1191 release_object( thread );
1194 /* check message filter for a hardware message */
1195 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1196 user_handle_t filter_win, unsigned int first, unsigned int last )
1198 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1200 /* we can only test the window for a keyboard message since the
1201 * dest window for a mouse message depends on hittest */
1202 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1203 return 0;
1204 /* the message code is final for a keyboard message, we can simply check it */
1205 return check_msg_filter( msg_code, first, last );
1207 else /* mouse message */
1209 /* we need to check all possible values that the message can have in the end */
1211 if (check_msg_filter( msg_code, first, last )) return 1;
1212 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1214 /* all other messages can become non-client messages */
1215 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1217 /* clicks can become double-clicks or non-client double-clicks */
1218 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1219 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1221 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1222 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1224 return 0;
1229 /* find a hardware message for the given queue */
1230 static int get_hardware_message( struct thread *thread, int hw_id, user_handle_t filter_win,
1231 unsigned int first, unsigned int last, struct get_message_reply *reply )
1233 struct thread_input *input = thread->queue->input;
1234 struct thread *win_thread;
1235 struct list *ptr;
1236 user_handle_t win;
1237 int clear_bits, got_one = 0;
1238 unsigned int msg_code;
1240 ptr = list_head( &input->msg_list );
1241 if (hw_id)
1243 while (ptr)
1245 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1246 if (msg->unique_id == hw_id) break;
1247 ptr = list_next( &input->msg_list, ptr );
1249 if (!ptr) ptr = list_head( &input->msg_list );
1250 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1253 if (ptr == list_head( &input->msg_list ))
1254 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1255 else
1256 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1258 while (ptr)
1260 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1261 ptr = list_next( &input->msg_list, ptr );
1262 win = find_hardware_message_window( input, msg, &msg_code );
1263 if (!win || !(win_thread = get_window_thread( win )))
1265 /* no window at all, remove it */
1266 update_input_key_state( input, msg );
1267 list_remove( &msg->entry );
1268 free_message( msg );
1269 continue;
1271 if (win_thread != thread)
1273 if (win_thread->queue->input == input)
1275 /* wake the other thread */
1276 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1277 got_one = 1;
1279 else
1281 /* for another thread input, drop it */
1282 update_input_key_state( input, msg );
1283 list_remove( &msg->entry );
1284 free_message( msg );
1286 release_object( win_thread );
1287 continue;
1289 release_object( win_thread );
1291 /* if we already got a message for another thread, or if it doesn't
1292 * match the filter we skip it */
1293 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1295 clear_bits &= ~get_hardware_msg_bit( msg );
1296 continue;
1298 /* now we can return it */
1299 if (!msg->unique_id) msg->unique_id = get_unique_id();
1300 reply->type = MSG_HARDWARE;
1301 reply->win = win;
1302 reply->msg = msg_code;
1303 reply->wparam = msg->wparam;
1304 reply->lparam = msg->lparam;
1305 reply->x = msg->x;
1306 reply->y = msg->y;
1307 reply->time = msg->time;
1308 reply->info = msg->info;
1309 reply->hw_id = msg->unique_id;
1310 return 1;
1312 /* nothing found, clear the hardware queue bits */
1313 clear_queue_bits( thread->queue, clear_bits );
1314 return 0;
1317 /* increment (or decrement if 'incr' is negative) the queue paint count */
1318 void inc_queue_paint_count( struct thread *thread, int incr )
1320 struct msg_queue *queue = thread->queue;
1322 assert( queue );
1324 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1326 if (queue->paint_count)
1327 set_queue_bits( queue, QS_PAINT );
1328 else
1329 clear_queue_bits( queue, QS_PAINT );
1333 /* remove all messages and timers belonging to a certain window */
1334 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1336 struct msg_queue *queue = thread->queue;
1337 struct list *ptr;
1338 int i;
1340 if (!queue) return;
1342 /* remove timers */
1344 ptr = list_head( &queue->pending_timers );
1345 while (ptr)
1347 struct list *next = list_next( &queue->pending_timers, ptr );
1348 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1349 if (timer->win == win) free_timer( queue, timer );
1350 ptr = next;
1352 ptr = list_head( &queue->expired_timers );
1353 while (ptr)
1355 struct list *next = list_next( &queue->expired_timers, ptr );
1356 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1357 if (timer->win == win) free_timer( queue, timer );
1358 ptr = next;
1361 /* remove messages */
1362 for (i = 0; i < NB_MSG_KINDS; i++)
1364 struct list *ptr, *next;
1366 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1368 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1369 if (msg->win == win) remove_queue_message( queue, msg, i );
1373 thread_input_cleanup_window( queue, win );
1376 /* post a message to a window; used by socket handling */
1377 void post_message( user_handle_t win, unsigned int message,
1378 unsigned int wparam, unsigned int lparam )
1380 struct message *msg;
1381 struct thread *thread = get_window_thread( win );
1383 if (!thread) return;
1385 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1387 msg->type = MSG_POSTED;
1388 msg->win = get_user_full_handle( win );
1389 msg->msg = message;
1390 msg->wparam = wparam;
1391 msg->lparam = lparam;
1392 msg->time = get_tick_count();
1393 msg->x = 0;
1394 msg->y = 0;
1395 msg->info = 0;
1396 msg->hook = 0;
1397 msg->hook_proc = NULL;
1398 msg->result = NULL;
1399 msg->data = NULL;
1400 msg->data_size = 0;
1402 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1403 set_queue_bits( thread->queue, QS_POSTMESSAGE );
1405 release_object( thread );
1408 /* post a win event */
1409 void post_win_event( struct thread *thread, unsigned int event,
1410 user_handle_t win, unsigned int object_id,
1411 unsigned int child_id, void *hook_proc,
1412 const WCHAR *module, size_t module_size,
1413 user_handle_t hook)
1415 struct message *msg;
1417 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1419 msg->type = MSG_WINEVENT;
1420 msg->win = get_user_full_handle( win );
1421 msg->msg = event;
1422 msg->wparam = object_id;
1423 msg->lparam = child_id;
1424 msg->time = get_tick_count();
1425 msg->x = 0;
1426 msg->y = 0;
1427 msg->info = get_thread_id( current );
1428 msg->result = NULL;
1429 msg->hook = hook;
1430 msg->hook_proc = hook_proc;
1432 if ((msg->data = malloc( module_size )))
1434 msg->data_size = module_size;
1435 memcpy( msg->data, module, module_size );
1437 if (debug_level > 1)
1438 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1439 get_thread_id(thread), event, win, object_id, child_id );
1440 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1441 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1443 else
1444 free( msg );
1448 /* get the message queue of the current thread */
1449 DECL_HANDLER(get_msg_queue)
1451 struct msg_queue *queue = get_current_queue();
1453 reply->handle = 0;
1454 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1458 /* set the current message queue wakeup mask */
1459 DECL_HANDLER(set_queue_mask)
1461 struct msg_queue *queue = get_current_queue();
1463 if (queue)
1465 queue->wake_mask = req->wake_mask;
1466 queue->changed_mask = req->changed_mask;
1467 reply->wake_bits = queue->wake_bits;
1468 reply->changed_bits = queue->changed_bits;
1469 if (is_signaled( queue ))
1471 /* if skip wait is set, do what would have been done in the subsequent wait */
1472 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1473 else wake_up( &queue->obj, 0 );
1479 /* get the current message queue status */
1480 DECL_HANDLER(get_queue_status)
1482 struct msg_queue *queue = current->queue;
1483 if (queue)
1485 reply->wake_bits = queue->wake_bits;
1486 reply->changed_bits = queue->changed_bits;
1487 if (req->clear) queue->changed_bits = 0;
1489 else reply->wake_bits = reply->changed_bits = 0;
1493 /* send a message to a thread queue */
1494 DECL_HANDLER(send_message)
1496 struct message *msg;
1497 struct msg_queue *send_queue = get_current_queue();
1498 struct msg_queue *recv_queue = NULL;
1499 struct thread *thread = NULL;
1501 if (req->id)
1503 if (!(thread = get_thread_from_id( req->id ))) return;
1505 else if (req->type != MSG_HARDWARE)
1507 /* only hardware messages are allowed without destination thread */
1508 set_error( STATUS_INVALID_PARAMETER );
1509 return;
1512 if (thread && !(recv_queue = thread->queue))
1514 set_error( STATUS_INVALID_PARAMETER );
1515 release_object( thread );
1516 return;
1518 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1520 set_error( STATUS_TIMEOUT );
1521 release_object( thread );
1522 return;
1525 if ((msg = mem_alloc( sizeof(*msg) )))
1527 msg->type = req->type;
1528 msg->win = get_user_full_handle( req->win );
1529 msg->msg = req->msg;
1530 msg->wparam = req->wparam;
1531 msg->lparam = req->lparam;
1532 msg->time = req->time;
1533 msg->x = req->x;
1534 msg->y = req->y;
1535 msg->info = req->info;
1536 msg->hook = 0;
1537 msg->hook_proc = NULL;
1538 msg->result = NULL;
1539 msg->data = NULL;
1540 msg->data_size = 0;
1542 switch(msg->type)
1544 case MSG_OTHER_PROCESS:
1545 msg->data_size = get_req_data_size();
1546 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1548 free( msg );
1549 break;
1551 /* fall through */
1552 case MSG_ASCII:
1553 case MSG_UNICODE:
1554 case MSG_CALLBACK:
1555 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1556 req->timeout, req->callback, req->info )))
1558 free_message( msg );
1559 break;
1561 /* fall through */
1562 case MSG_NOTIFY:
1563 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1564 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1565 break;
1566 case MSG_POSTED:
1567 /* needed for posted DDE messages */
1568 msg->data_size = get_req_data_size();
1569 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1571 free( msg );
1572 break;
1574 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1575 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1576 break;
1577 case MSG_HARDWARE:
1578 queue_hardware_message( recv_queue, msg );
1579 break;
1580 case MSG_CALLBACK_RESULT: /* cannot send this one */
1581 default:
1582 set_error( STATUS_INVALID_PARAMETER );
1583 free( msg );
1584 break;
1587 if (thread) release_object( thread );
1591 /* get a message from the current queue */
1592 DECL_HANDLER(get_message)
1594 struct timer *timer;
1595 struct list *ptr;
1596 struct msg_queue *queue = get_current_queue();
1597 user_handle_t get_win = get_user_full_handle( req->get_win );
1599 reply->active_hooks = get_active_hooks();
1601 if (!queue) return;
1602 gettimeofday( &queue->last_get_msg, NULL );
1604 /* first check for sent messages */
1605 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1607 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1608 receive_message( queue, msg, reply );
1609 return;
1611 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1613 /* clear changed bits so we can wait on them if we don't find a message */
1614 queue->changed_bits = 0;
1616 /* then check for posted messages */
1617 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1618 return;
1620 /* then check for any raw hardware message */
1621 if (filter_contains_hw_range( req->get_first, req->get_last ) &&
1622 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1623 return;
1625 /* now check for WM_PAINT */
1626 if (queue->paint_count &&
1627 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1628 (reply->win = find_window_to_repaint( get_win, current )))
1630 reply->type = MSG_POSTED;
1631 reply->msg = WM_PAINT;
1632 reply->wparam = 0;
1633 reply->lparam = 0;
1634 reply->x = 0;
1635 reply->y = 0;
1636 reply->time = get_tick_count();
1637 reply->info = 0;
1638 return;
1641 /* now check for timer */
1642 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1643 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1645 reply->type = MSG_POSTED;
1646 reply->win = timer->win;
1647 reply->msg = timer->msg;
1648 reply->wparam = timer->id;
1649 reply->lparam = timer->lparam;
1650 reply->x = 0;
1651 reply->y = 0;
1652 reply->time = get_tick_count();
1653 reply->info = 0;
1654 return;
1657 done:
1658 set_error( STATUS_PENDING ); /* FIXME */
1662 /* reply to a sent message */
1663 DECL_HANDLER(reply_message)
1665 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1666 else if (current->queue->recv_result)
1667 reply_message( current->queue, req->result, 0, req->remove,
1668 get_req_data(), get_req_data_size() );
1672 /* accept the current hardware message */
1673 DECL_HANDLER(accept_hardware_message)
1675 if (current->queue)
1676 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1677 else
1678 set_error( STATUS_ACCESS_DENIED );
1682 /* retrieve the reply for the last message sent */
1683 DECL_HANDLER(get_message_reply)
1685 struct message_result *result;
1686 struct list *entry;
1687 struct msg_queue *queue = current->queue;
1689 if (queue)
1691 set_error( STATUS_PENDING );
1692 reply->result = 0;
1694 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1696 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1697 if (result->replied || req->cancel)
1699 if (result->replied)
1701 reply->result = result->result;
1702 set_error( result->error );
1703 if (result->data)
1705 size_t data_len = min( result->data_size, get_reply_max_size() );
1706 set_reply_data_ptr( result->data, data_len );
1707 result->data = NULL;
1708 result->data_size = 0;
1711 remove_result_from_sender( result );
1713 entry = list_head( &queue->send_result );
1714 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1715 else
1717 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1718 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1722 else set_error( STATUS_ACCESS_DENIED );
1726 /* set a window timer */
1727 DECL_HANDLER(set_win_timer)
1729 struct timer *timer;
1730 struct msg_queue *queue;
1731 struct thread *thread = NULL;
1732 user_handle_t win = 0;
1733 unsigned int id = req->id;
1735 if (req->win)
1737 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1739 set_error( STATUS_INVALID_HANDLE );
1740 return;
1742 if (thread->process != current->process)
1744 release_object( thread );
1745 set_error( STATUS_ACCESS_DENIED );
1746 return;
1748 queue = thread->queue;
1749 /* remove it if it existed already */
1750 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1752 else
1754 queue = get_current_queue();
1755 /* find a free id for it */
1758 id = queue->next_timer_id;
1759 if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1761 while (find_timer( queue, 0, req->msg, id ));
1764 if ((timer = set_timer( queue, req->rate )))
1766 timer->win = win;
1767 timer->msg = req->msg;
1768 timer->id = id;
1769 timer->lparam = req->lparam;
1770 reply->id = id;
1772 if (thread) release_object( thread );
1775 /* kill a window timer */
1776 DECL_HANDLER(kill_win_timer)
1778 struct timer *timer;
1779 struct thread *thread;
1780 user_handle_t win = 0;
1782 if (req->win)
1784 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1786 set_error( STATUS_INVALID_HANDLE );
1787 return;
1789 if (thread->process != current->process)
1791 release_object( thread );
1792 set_error( STATUS_ACCESS_DENIED );
1793 return;
1796 else thread = (struct thread *)grab_object( current );
1798 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1799 free_timer( thread->queue, timer );
1800 else
1801 set_error( STATUS_INVALID_PARAMETER );
1803 release_object( thread );
1807 /* attach (or detach) thread inputs */
1808 DECL_HANDLER(attach_thread_input)
1810 struct thread *thread_from = get_thread_from_id( req->tid_from );
1811 struct thread *thread_to = get_thread_from_id( req->tid_to );
1813 if (!thread_from || !thread_to)
1815 if (thread_from) release_object( thread_from );
1816 if (thread_to) release_object( thread_to );
1817 return;
1819 if (thread_from != thread_to)
1821 if (req->attach) attach_thread_input( thread_from, thread_to );
1822 else
1824 if (thread_from->queue && thread_to->queue &&
1825 thread_from->queue->input == thread_to->queue->input)
1826 detach_thread_input( thread_from );
1827 else
1828 set_error( STATUS_ACCESS_DENIED );
1831 else set_error( STATUS_ACCESS_DENIED );
1832 release_object( thread_from );
1833 release_object( thread_to );
1837 /* get thread input data */
1838 DECL_HANDLER(get_thread_input)
1840 struct thread *thread = NULL;
1841 struct thread_input *input;
1843 if (req->tid)
1845 if (!(thread = get_thread_from_id( req->tid ))) return;
1846 input = thread->queue ? thread->queue->input : NULL;
1848 else input = foreground_input; /* get the foreground thread info */
1850 if (input)
1852 reply->focus = input->focus;
1853 reply->capture = input->capture;
1854 reply->active = input->active;
1855 reply->menu_owner = input->menu_owner;
1856 reply->move_size = input->move_size;
1857 reply->caret = input->caret;
1858 reply->rect = input->caret_rect;
1860 else
1862 reply->focus = 0;
1863 reply->capture = 0;
1864 reply->active = 0;
1865 reply->menu_owner = 0;
1866 reply->move_size = 0;
1867 reply->caret = 0;
1868 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1870 /* foreground window is active window of foreground thread */
1871 reply->foreground = foreground_input ? foreground_input->active : 0;
1872 if (thread) release_object( thread );
1876 /* retrieve queue keyboard state for a given thread */
1877 DECL_HANDLER(get_key_state)
1879 struct thread *thread;
1880 struct thread_input *input;
1882 if (!(thread = get_thread_from_id( req->tid ))) return;
1883 input = thread->queue ? thread->queue->input : NULL;
1884 if (input)
1886 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1887 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1889 release_object( thread );
1893 /* set queue keyboard state for a given thread */
1894 DECL_HANDLER(set_key_state)
1896 struct thread *thread = NULL;
1897 struct thread_input *input;
1899 if (!(thread = get_thread_from_id( req->tid ))) return;
1900 input = thread->queue ? thread->queue->input : NULL;
1901 if (input)
1903 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1904 if (size) memcpy( input->keystate, get_req_data(), size );
1906 release_object( thread );
1910 /* set the system foreground window */
1911 DECL_HANDLER(set_foreground_window)
1913 struct msg_queue *queue = get_current_queue();
1915 reply->previous = foreground_input ? foreground_input->active : 0;
1916 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1917 reply->send_msg_new = FALSE;
1919 if (req->handle)
1921 struct thread *thread;
1923 if (is_top_level_window( req->handle ) &&
1924 ((thread = get_window_thread( req->handle ))))
1926 foreground_input = thread->queue->input;
1927 reply->send_msg_new = (foreground_input != queue->input);
1928 release_object( thread );
1930 else set_error( STATUS_INVALID_HANDLE );
1932 else foreground_input = NULL;
1936 /* set the current thread focus window */
1937 DECL_HANDLER(set_focus_window)
1939 struct msg_queue *queue = get_current_queue();
1941 reply->previous = 0;
1942 if (queue && check_queue_input_window( queue, req->handle ))
1944 reply->previous = queue->input->focus;
1945 queue->input->focus = get_user_full_handle( req->handle );
1950 /* set the current thread active window */
1951 DECL_HANDLER(set_active_window)
1953 struct msg_queue *queue = get_current_queue();
1955 reply->previous = 0;
1956 if (queue && check_queue_input_window( queue, req->handle ))
1958 if (!req->handle || make_window_active( req->handle ))
1960 reply->previous = queue->input->active;
1961 queue->input->active = get_user_full_handle( req->handle );
1963 else set_error( STATUS_INVALID_HANDLE );
1968 /* set the current thread capture window */
1969 DECL_HANDLER(set_capture_window)
1971 struct msg_queue *queue = get_current_queue();
1973 reply->previous = reply->full_handle = 0;
1974 if (queue && check_queue_input_window( queue, req->handle ))
1976 struct thread_input *input = queue->input;
1978 reply->previous = input->capture;
1979 input->capture = get_user_full_handle( req->handle );
1980 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1981 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1982 reply->full_handle = input->capture;
1987 /* Set the current thread caret window */
1988 DECL_HANDLER(set_caret_window)
1990 struct msg_queue *queue = get_current_queue();
1992 reply->previous = 0;
1993 if (queue && check_queue_input_window( queue, req->handle ))
1995 struct thread_input *input = queue->input;
1997 reply->previous = input->caret;
1998 reply->old_rect = input->caret_rect;
1999 reply->old_hide = input->caret_hide;
2000 reply->old_state = input->caret_state;
2002 set_caret_window( input, get_user_full_handle(req->handle) );
2003 input->caret_rect.right = input->caret_rect.left + req->width;
2004 input->caret_rect.bottom = input->caret_rect.top + req->height;
2009 /* Set the current thread caret information */
2010 DECL_HANDLER(set_caret_info)
2012 struct msg_queue *queue = get_current_queue();
2013 struct thread_input *input;
2015 if (!queue) return;
2016 input = queue->input;
2017 reply->full_handle = input->caret;
2018 reply->old_rect = input->caret_rect;
2019 reply->old_hide = input->caret_hide;
2020 reply->old_state = input->caret_state;
2022 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2024 set_error( STATUS_ACCESS_DENIED );
2025 return;
2027 if (req->flags & SET_CARET_POS)
2029 input->caret_rect.right += req->x - input->caret_rect.left;
2030 input->caret_rect.bottom += req->y - input->caret_rect.top;
2031 input->caret_rect.left = req->x;
2032 input->caret_rect.top = req->y;
2034 if (req->flags & SET_CARET_HIDE)
2036 input->caret_hide += req->hide;
2037 if (input->caret_hide < 0) input->caret_hide = 0;
2039 if (req->flags & SET_CARET_STATE)
2041 if (req->state == -1) input->caret_state = !input->caret_state;
2042 else input->caret_state = !!req->state;
2047 /* get the time of the last input event */
2048 DECL_HANDLER(get_last_input_time)
2050 reply->time = last_input_time;