Added a warning for DllGetVersion and DllInstall not being declared
[wine/wine-kai.git] / server / queue.c
blob8cee4ab41fd47e42185f6bec4d1904a7c4e54fd9
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;
264 remove_thread_hooks( thread );
265 if (!thread->queue) return;
266 if (process->queue == thread->queue) /* is it the process main queue? */
268 release_object( process->queue );
269 process->queue = NULL;
270 if (process->idle_event)
272 set_event( process->idle_event );
273 release_object( process->idle_event );
274 process->idle_event = NULL;
277 release_object( thread->queue );
278 thread->queue = NULL;
281 /* get the hook table for a given thread */
282 struct hook_table *get_queue_hooks( struct thread *thread )
284 if (!thread->queue) return NULL;
285 return thread->queue->hooks;
288 /* set the hook table for a given thread, allocating the queue if needed */
289 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
291 struct msg_queue *queue = thread->queue;
292 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
293 if (queue->hooks) release_object( queue->hooks );
294 queue->hooks = hooks;
297 /* check the queue status */
298 inline static int is_signaled( struct msg_queue *queue )
300 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
303 /* set some queue bits */
304 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
306 queue->wake_bits |= bits;
307 queue->changed_bits |= bits;
308 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
311 /* clear some queue bits */
312 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
314 queue->wake_bits &= ~bits;
315 queue->changed_bits &= ~bits;
318 /* check whether msg is a keyboard message */
319 inline static int is_keyboard_msg( struct message *msg )
321 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
324 /* check if message is matched by the filter */
325 inline static int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
327 return (msg >= first && msg <= last);
330 /* check whether a message filter contains at least one potential hardware message */
331 inline static int filter_contains_hw_range( unsigned int first, unsigned int last )
333 /* hardware message ranges are (in numerical order):
334 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
335 * WM_KEYFIRST .. WM_KEYLAST
336 * WM_MOUSEFIRST .. WM_MOUSELAST
338 if (last < WM_NCMOUSEFIRST) return 0;
339 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
340 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
341 if (first > WM_MOUSELAST) return 0;
342 return 1;
345 /* get the QS_* bit corresponding to a given hardware message */
346 inline static int get_hardware_msg_bit( struct message *msg )
348 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
349 if (is_keyboard_msg( msg )) return QS_KEY;
350 return QS_MOUSEBUTTON;
353 /* get the current thread queue, creating it if needed */
354 inline static struct msg_queue *get_current_queue(void)
356 struct msg_queue *queue = current->queue;
357 if (!queue) queue = create_msg_queue( current, NULL );
358 return queue;
361 /* get a (pseudo-)unique id to tag hardware messages */
362 inline static unsigned int get_unique_id(void)
364 static unsigned int id;
365 if (!++id) id = 1; /* avoid an id of 0 */
366 return id;
369 /* try to merge a message with the last in the list; return 1 if successful */
370 static int merge_message( struct thread_input *input, const struct message *msg )
372 struct message *prev;
373 struct list *ptr = list_tail( &input->msg_list );
375 if (!ptr) return 0;
376 prev = LIST_ENTRY( ptr, struct message, entry );
377 if (prev->unique_id) return 0;
378 if (prev->result) return 0;
379 if (prev->win != msg->win) return 0;
380 if (prev->msg != msg->msg) return 0;
381 if (prev->type != msg->type) return 0;
382 /* now we can merge it */
383 prev->wparam = msg->wparam;
384 prev->lparam = msg->lparam;
385 prev->x = msg->x;
386 prev->y = msg->y;
387 prev->time = msg->time;
388 prev->info = msg->info;
389 return 1;
392 /* free a result structure */
393 static void free_result( struct message_result *result )
395 if (result->timeout) remove_timeout_user( result->timeout );
396 if (result->data) free( result->data );
397 if (result->callback_msg) free( result->callback_msg );
398 free( result );
401 /* remove the result from the sender list it is on */
402 static inline void remove_result_from_sender( struct message_result *result )
404 assert( result->sender );
406 list_remove( &result->sender_entry );
407 result->sender = NULL;
408 if (!result->receiver) free_result( result );
411 /* store the message result in the appropriate structure */
412 static void store_message_result( struct message_result *res, unsigned int result,
413 unsigned int error )
415 res->result = result;
416 res->error = error;
417 res->replied = 1;
418 if (res->timeout)
420 remove_timeout_user( res->timeout );
421 res->timeout = NULL;
423 if (res->sender)
425 if (res->callback_msg)
427 /* queue the callback message in the sender queue */
428 res->callback_msg->lparam = result;
429 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
430 set_queue_bits( res->sender, QS_SENDMESSAGE );
431 res->callback_msg = NULL;
432 remove_result_from_sender( res );
434 else
436 /* wake sender queue if waiting on this result */
437 if (list_head(&res->sender->send_result) == &res->sender_entry)
438 set_queue_bits( res->sender, QS_SMRESULT );
444 /* free a message when deleting a queue or window */
445 static void free_message( struct message *msg )
447 struct message_result *result = msg->result;
448 if (result)
450 if (result->sender)
452 result->receiver = NULL;
453 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
455 else free_result( result );
457 if (msg->data) free( msg->data );
458 free( msg );
461 /* remove (and free) a message from a message list */
462 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
463 enum message_kind kind )
465 list_remove( &msg->entry );
466 switch(kind)
468 case SEND_MESSAGE:
469 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
470 break;
471 case POST_MESSAGE:
472 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_POSTMESSAGE );
473 break;
475 free_message( msg );
478 /* message timed out without getting a reply */
479 static void result_timeout( void *private )
481 struct message_result *result = private;
483 assert( !result->replied );
485 result->timeout = NULL;
486 store_message_result( result, 0, STATUS_TIMEOUT );
489 /* allocate and fill a message result structure */
490 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
491 struct msg_queue *recv_queue,
492 struct message *msg, unsigned int timeout,
493 void *callback, unsigned int callback_data )
495 struct message_result *result = mem_alloc( sizeof(*result) );
496 if (result)
498 result->sender = send_queue;
499 result->receiver = recv_queue;
500 result->replied = 0;
501 result->data = NULL;
502 result->data_size = 0;
503 result->timeout = NULL;
505 if (msg->type == MSG_CALLBACK)
507 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
508 if (!callback_msg)
510 free( result );
511 return NULL;
513 callback_msg->type = MSG_CALLBACK_RESULT;
514 callback_msg->win = msg->win;
515 callback_msg->msg = msg->msg;
516 callback_msg->wparam = (unsigned int)callback;
517 callback_msg->lparam = 0;
518 callback_msg->time = get_tick_count();
519 callback_msg->x = 0;
520 callback_msg->y = 0;
521 callback_msg->info = callback_data;
522 callback_msg->hook = 0;
523 callback_msg->hook_proc = NULL;
524 callback_msg->result = NULL;
525 callback_msg->data = NULL;
526 callback_msg->data_size = 0;
528 result->callback_msg = callback_msg;
529 list_add_head( &send_queue->callback_result, &result->sender_entry );
531 else
533 result->callback_msg = NULL;
534 list_add_head( &send_queue->send_result, &result->sender_entry );
537 if (timeout != -1)
539 struct timeval when;
540 gettimeofday( &when, NULL );
541 add_timeout( &when, timeout );
542 result->timeout = add_timeout_user( &when, result_timeout, result );
545 return result;
548 /* receive a message, removing it from the sent queue */
549 static void receive_message( struct msg_queue *queue, struct message *msg,
550 struct get_message_reply *reply )
552 struct message_result *result = msg->result;
554 reply->total = msg->data_size;
555 if (msg->data_size > get_reply_max_size())
557 set_error( STATUS_BUFFER_OVERFLOW );
558 return;
560 reply->type = msg->type;
561 reply->win = msg->win;
562 reply->msg = msg->msg;
563 reply->wparam = msg->wparam;
564 reply->lparam = msg->lparam;
565 reply->x = msg->x;
566 reply->y = msg->y;
567 reply->time = msg->time;
568 reply->info = msg->info;
569 reply->hook = msg->hook;
570 reply->hook_proc = msg->hook_proc;
572 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
574 list_remove( &msg->entry );
575 /* put the result on the receiver result stack */
576 if (result)
578 result->recv_next = queue->recv_result;
579 queue->recv_result = result;
581 free( msg );
582 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
585 /* set the result of the current received message */
586 static void reply_message( struct msg_queue *queue, unsigned int result,
587 unsigned int error, int remove, const void *data, size_t len )
589 struct message_result *res = queue->recv_result;
591 if (remove)
593 queue->recv_result = res->recv_next;
594 res->receiver = NULL;
595 if (!res->sender) /* no one waiting for it */
597 free_result( res );
598 return;
601 if (!res->replied)
603 if (len && (res->data = memdup( data, len ))) res->data_size = len;
604 store_message_result( res, result, error );
608 /* retrieve a posted message */
609 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
610 unsigned int first, unsigned int last, unsigned int flags,
611 struct get_message_reply *reply )
613 struct message *msg;
615 /* check against the filters */
616 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
618 if (msg->msg == WM_QUIT) goto found; /* WM_QUIT is never filtered */
619 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
620 if (!check_msg_filter( msg->msg, first, last )) continue;
621 goto found; /* found one */
623 return 0;
625 /* return it to the app */
626 found:
627 reply->total = msg->data_size;
628 if (msg->data_size > get_reply_max_size())
630 set_error( STATUS_BUFFER_OVERFLOW );
631 return 1;
633 reply->type = msg->type;
634 reply->win = msg->win;
635 reply->msg = msg->msg;
636 reply->wparam = msg->wparam;
637 reply->lparam = msg->lparam;
638 reply->x = msg->x;
639 reply->y = msg->y;
640 reply->time = msg->time;
641 reply->info = msg->info;
643 if (flags & GET_MSG_REMOVE)
645 if (msg->data)
647 set_reply_data_ptr( msg->data, msg->data_size );
648 msg->data = NULL;
649 msg->data_size = 0;
651 remove_queue_message( queue, msg, POST_MESSAGE );
653 else if (msg->data) set_reply_data( msg->data, msg->data_size );
655 return 1;
658 /* empty a message list and free all the messages */
659 static void empty_msg_list( struct list *list )
661 struct list *ptr;
663 while ((ptr = list_head( list )) != NULL)
665 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
666 list_remove( &msg->entry );
667 free_message( msg );
671 /* cleanup all pending results when deleting a queue */
672 static void cleanup_results( struct msg_queue *queue )
674 struct list *entry;
676 while ((entry = list_head( &queue->send_result )) != NULL)
678 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
681 while ((entry = list_head( &queue->callback_result )) != NULL)
683 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
686 while (queue->recv_result)
687 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
690 /* check if the thread owning the queue is hung (not checking for messages) */
691 static int is_queue_hung( struct msg_queue *queue )
693 struct timeval now;
694 struct wait_queue_entry *entry;
696 gettimeofday( &now, NULL );
697 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
698 return 0; /* less than 5 seconds since last get message -> not hung */
700 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
702 if (entry->thread->queue == queue)
703 return 0; /* thread is waiting on queue -> not hung */
705 return 1;
708 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
710 struct msg_queue *queue = (struct msg_queue *)obj;
711 struct process *process = entry->thread->process;
713 /* a thread can only wait on its own queue */
714 if (entry->thread->queue != queue)
716 set_error( STATUS_ACCESS_DENIED );
717 return 0;
719 /* if waiting on the main process queue, set the idle event */
720 if (process->queue == queue)
722 if (process->idle_event) set_event( process->idle_event );
724 add_queue( obj, entry );
725 return 1;
728 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
730 struct msg_queue *queue = (struct msg_queue *)obj;
731 struct process *process = entry->thread->process;
733 remove_queue( obj, entry );
735 assert( entry->thread->queue == queue );
737 /* if waiting on the main process queue, reset the idle event */
738 if (process->queue == queue)
740 if (process->idle_event) reset_event( process->idle_event );
744 static void msg_queue_dump( struct object *obj, int verbose )
746 struct msg_queue *queue = (struct msg_queue *)obj;
747 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
748 queue->wake_bits, queue->wake_mask );
751 static int msg_queue_signaled( struct object *obj, struct thread *thread )
753 struct msg_queue *queue = (struct msg_queue *)obj;
754 return is_signaled( queue );
757 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
759 struct msg_queue *queue = (struct msg_queue *)obj;
760 queue->wake_mask = 0;
761 queue->changed_mask = 0;
762 return 0; /* Not abandoned */
765 static void msg_queue_destroy( struct object *obj )
767 struct msg_queue *queue = (struct msg_queue *)obj;
768 struct list *ptr;
769 int i;
771 cleanup_results( queue );
772 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
774 while ((ptr = list_head( &queue->pending_timers )))
776 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
777 list_remove( &timer->entry );
778 free( timer );
780 while ((ptr = list_head( &queue->expired_timers )))
782 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
783 list_remove( &timer->entry );
784 free( timer );
786 if (queue->timeout) remove_timeout_user( queue->timeout );
787 if (queue->input) release_object( queue->input );
788 if (queue->hooks) release_object( queue->hooks );
791 static void thread_input_dump( struct object *obj, int verbose )
793 struct thread_input *input = (struct thread_input *)obj;
794 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
795 input->focus, input->capture, input->active );
798 static void thread_input_destroy( struct object *obj )
800 struct thread_input *input = (struct thread_input *)obj;
802 if (foreground_input == input) foreground_input = NULL;
803 empty_msg_list( &input->msg_list );
804 release_object( input->desktop );
807 /* fix the thread input data when a window is destroyed */
808 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
810 struct thread_input *input = queue->input;
812 if (window == input->focus) input->focus = 0;
813 if (window == input->capture) input->capture = 0;
814 if (window == input->active) input->active = 0;
815 if (window == input->menu_owner) input->menu_owner = 0;
816 if (window == input->move_size) input->move_size = 0;
817 if (window == input->caret) set_caret_window( input, 0 );
820 /* check if the specified window can be set in the input data of a given queue */
821 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
823 struct thread *thread;
824 int ret = 0;
826 if (!window) return 1; /* we can always clear the data */
828 if ((thread = get_window_thread( window )))
830 ret = (queue->input == thread->queue->input);
831 if (!ret) set_error( STATUS_ACCESS_DENIED );
832 release_object( thread );
834 else set_error( STATUS_INVALID_HANDLE );
836 return ret;
839 /* make sure the specified thread has a queue */
840 int init_thread_queue( struct thread *thread )
842 if (thread->queue) return 1;
843 return (create_msg_queue( thread, NULL ) != NULL);
846 /* attach two thread input data structures */
847 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
849 struct desktop *desktop;
850 struct thread_input *input;
852 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
853 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
854 input = (struct thread_input *)grab_object( thread_to->queue->input );
855 if (input->desktop != desktop)
857 set_error( STATUS_ACCESS_DENIED );
858 release_object( input );
859 release_object( desktop );
860 return 0;
862 release_object( desktop );
864 if (thread_from->queue)
866 release_thread_input( thread_from );
867 thread_from->queue->input = input;
869 else
871 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
873 memset( input->keystate, 0, sizeof(input->keystate) );
874 return 1;
877 /* detach two thread input data structures */
878 void detach_thread_input( struct thread *thread_from )
880 struct thread_input *input;
882 if ((input = create_thread_input( thread_from )))
884 release_thread_input( thread_from );
885 thread_from->queue->input = input;
890 /* set the next timer to expire */
891 static void set_next_timer( struct msg_queue *queue )
893 struct list *ptr;
895 if (queue->timeout)
897 remove_timeout_user( queue->timeout );
898 queue->timeout = NULL;
900 if ((ptr = list_head( &queue->pending_timers )))
902 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
903 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
905 /* set/clear QS_TIMER bit */
906 if (list_empty( &queue->expired_timers ))
907 clear_queue_bits( queue, QS_TIMER );
908 else
909 set_queue_bits( queue, QS_TIMER );
912 /* find a timer from its window and id */
913 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
914 unsigned int msg, unsigned int id )
916 struct list *ptr;
918 /* we need to search both lists */
920 LIST_FOR_EACH( ptr, &queue->pending_timers )
922 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
923 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
925 LIST_FOR_EACH( ptr, &queue->expired_timers )
927 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
928 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
930 return NULL;
933 /* callback for the next timer expiration */
934 static void timer_callback( void *private )
936 struct msg_queue *queue = private;
937 struct list *ptr;
939 queue->timeout = NULL;
940 /* move on to the next timer */
941 ptr = list_head( &queue->pending_timers );
942 list_remove( ptr );
943 list_add_tail( &queue->expired_timers, ptr );
944 set_next_timer( queue );
947 /* link a timer at its rightful place in the queue list */
948 static void link_timer( struct msg_queue *queue, struct timer *timer )
950 struct list *ptr;
952 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
954 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
955 if (!time_before( &t->when, &timer->when )) break;
957 list_add_before( ptr, &timer->entry );
960 /* remove a timer from the queue timer list and free it */
961 static void free_timer( struct msg_queue *queue, struct timer *timer )
963 list_remove( &timer->entry );
964 free( timer );
965 set_next_timer( queue );
968 /* restart an expired timer */
969 static void restart_timer( struct msg_queue *queue, struct timer *timer )
971 struct timeval now;
973 list_remove( &timer->entry );
974 gettimeofday( &now, NULL );
975 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
976 link_timer( queue, timer );
977 set_next_timer( queue );
980 /* find an expired timer matching the filtering parameters */
981 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
982 unsigned int get_first, unsigned int get_last,
983 int remove )
985 struct list *ptr;
987 LIST_FOR_EACH( ptr, &queue->expired_timers )
989 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
990 if (win && timer->win != win) continue;
991 if (check_msg_filter( timer->msg, get_first, get_last ))
993 if (remove) restart_timer( queue, timer );
994 return timer;
997 return NULL;
1000 /* add a timer */
1001 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1003 struct timer *timer = mem_alloc( sizeof(*timer) );
1004 if (timer)
1006 timer->rate = max( rate, 1 );
1007 gettimeofday( &timer->when, NULL );
1008 add_timeout( &timer->when, rate );
1009 link_timer( queue, timer );
1010 /* check if we replaced the next timer */
1011 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1013 return timer;
1016 /* change the input key state for a given key */
1017 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1019 if (down)
1021 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1022 input->keystate[key] |= 0x80;
1024 else input->keystate[key] &= ~0x80;
1027 /* update the input key state for a keyboard message */
1028 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1030 unsigned char key;
1031 int down = 0, extended;
1033 switch (msg->msg)
1035 case WM_LBUTTONDOWN:
1036 down = 1;
1037 /* fall through */
1038 case WM_LBUTTONUP:
1039 set_input_key_state( input, VK_LBUTTON, down );
1040 break;
1041 case WM_MBUTTONDOWN:
1042 down = 1;
1043 /* fall through */
1044 case WM_MBUTTONUP:
1045 set_input_key_state( input, VK_MBUTTON, down );
1046 break;
1047 case WM_RBUTTONDOWN:
1048 down = 1;
1049 /* fall through */
1050 case WM_RBUTTONUP:
1051 set_input_key_state( input, VK_RBUTTON, down );
1052 break;
1053 case WM_XBUTTONDOWN:
1054 down = 1;
1055 /* fall through */
1056 case WM_XBUTTONUP:
1057 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1058 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1059 break;
1060 case WM_KEYDOWN:
1061 case WM_SYSKEYDOWN:
1062 down = 1;
1063 /* fall through */
1064 case WM_KEYUP:
1065 case WM_SYSKEYUP:
1066 key = (unsigned char)msg->wparam;
1067 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1068 set_input_key_state( input, key, down );
1069 switch(key)
1071 case VK_SHIFT:
1072 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1073 break;
1074 case VK_CONTROL:
1075 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1076 break;
1077 case VK_MENU:
1078 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1079 break;
1081 break;
1085 /* release the hardware message currently being processed by the given thread */
1086 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1087 int remove, user_handle_t new_win )
1089 struct thread_input *input = queue->input;
1090 struct message *msg;
1092 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1094 if (msg->unique_id == hw_id) break;
1096 if (&msg->entry == &input->msg_list) return; /* not found */
1098 /* clear the queue bit for that message */
1099 if (remove || new_win)
1101 struct message *other;
1102 int clr_bit;
1104 clr_bit = get_hardware_msg_bit( msg );
1105 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1107 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1109 clr_bit = 0;
1110 break;
1113 if (clr_bit) clear_queue_bits( queue, clr_bit );
1116 if (new_win) /* set the new window */
1118 struct thread *owner = get_window_thread( new_win );
1119 if (owner)
1121 if (owner->queue->input == input)
1123 msg->win = new_win;
1124 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1125 remove = 0;
1127 release_object( owner );
1130 if (remove)
1132 update_input_key_state( input, msg );
1133 list_remove( &msg->entry );
1134 free_message( msg );
1138 /* find the window that should receive a given hardware message */
1139 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1140 unsigned int *msg_code )
1142 user_handle_t win = 0;
1144 *msg_code = msg->msg;
1145 if (is_keyboard_msg( msg ))
1147 if (input && !(win = input->focus))
1149 win = input->active;
1150 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1153 else /* mouse message */
1155 if (!input || !(win = input->capture))
1157 if (!(win = msg->win) || !is_window_visible( win ))
1158 win = window_from_point( input->desktop, msg->x, msg->y );
1161 return win;
1164 /* queue a hardware message into a given thread input */
1165 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1167 user_handle_t win;
1168 struct thread *thread;
1169 struct thread_input *input;
1170 unsigned int msg_code;
1172 last_input_time = get_tick_count();
1174 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1175 if (!win || !(thread = get_window_thread(win)))
1177 free( msg );
1178 return;
1180 input = thread->queue->input;
1182 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1183 else
1185 msg->unique_id = 0; /* will be set once we return it to the app */
1186 list_add_tail( &input->msg_list, &msg->entry );
1187 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1189 release_object( thread );
1192 /* check message filter for a hardware message */
1193 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1194 user_handle_t filter_win, unsigned int first, unsigned int last )
1196 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1198 /* we can only test the window for a keyboard message since the
1199 * dest window for a mouse message depends on hittest */
1200 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1201 return 0;
1202 /* the message code is final for a keyboard message, we can simply check it */
1203 return check_msg_filter( msg_code, first, last );
1205 else /* mouse message */
1207 /* we need to check all possible values that the message can have in the end */
1209 if (check_msg_filter( msg_code, first, last )) return 1;
1210 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1212 /* all other messages can become non-client messages */
1213 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1215 /* clicks can become double-clicks or non-client double-clicks */
1216 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1217 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1219 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1220 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1222 return 0;
1227 /* find a hardware message for the given queue */
1228 static int get_hardware_message( struct thread *thread, int hw_id, user_handle_t filter_win,
1229 unsigned int first, unsigned int last, struct get_message_reply *reply )
1231 struct thread_input *input = thread->queue->input;
1232 struct thread *win_thread;
1233 struct list *ptr;
1234 user_handle_t win;
1235 int clear_bits, got_one = 0;
1236 unsigned int msg_code;
1238 ptr = list_head( &input->msg_list );
1239 if (hw_id)
1241 while (ptr)
1243 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1244 if (msg->unique_id == hw_id) break;
1245 ptr = list_next( &input->msg_list, ptr );
1247 if (!ptr) ptr = list_head( &input->msg_list );
1248 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1251 if (ptr == list_head( &input->msg_list ))
1252 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1253 else
1254 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1256 while (ptr)
1258 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1259 ptr = list_next( &input->msg_list, ptr );
1260 win = find_hardware_message_window( input, msg, &msg_code );
1261 if (!win || !(win_thread = get_window_thread( win )))
1263 /* no window at all, remove it */
1264 update_input_key_state( input, msg );
1265 list_remove( &msg->entry );
1266 free_message( msg );
1267 continue;
1269 if (win_thread != thread)
1271 if (win_thread->queue->input == input)
1273 /* wake the other thread */
1274 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1275 got_one = 1;
1277 else
1279 /* for another thread input, drop it */
1280 update_input_key_state( input, msg );
1281 list_remove( &msg->entry );
1282 free_message( msg );
1284 release_object( win_thread );
1285 continue;
1287 release_object( win_thread );
1289 /* if we already got a message for another thread, or if it doesn't
1290 * match the filter we skip it */
1291 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1293 clear_bits &= ~get_hardware_msg_bit( msg );
1294 continue;
1296 /* now we can return it */
1297 if (!msg->unique_id) msg->unique_id = get_unique_id();
1298 reply->type = MSG_HARDWARE;
1299 reply->win = win;
1300 reply->msg = msg_code;
1301 reply->wparam = msg->wparam;
1302 reply->lparam = msg->lparam;
1303 reply->x = msg->x;
1304 reply->y = msg->y;
1305 reply->time = msg->time;
1306 reply->info = msg->info;
1307 reply->hw_id = msg->unique_id;
1308 return 1;
1310 /* nothing found, clear the hardware queue bits */
1311 clear_queue_bits( thread->queue, clear_bits );
1312 return 0;
1315 /* increment (or decrement if 'incr' is negative) the queue paint count */
1316 void inc_queue_paint_count( struct thread *thread, int incr )
1318 struct msg_queue *queue = thread->queue;
1320 assert( queue );
1322 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1324 if (queue->paint_count)
1325 set_queue_bits( queue, QS_PAINT );
1326 else
1327 clear_queue_bits( queue, QS_PAINT );
1331 /* remove all messages and timers belonging to a certain window */
1332 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1334 struct msg_queue *queue = thread->queue;
1335 struct list *ptr;
1336 int i;
1338 if (!queue) return;
1340 /* remove timers */
1342 ptr = list_head( &queue->pending_timers );
1343 while (ptr)
1345 struct list *next = list_next( &queue->pending_timers, ptr );
1346 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1347 if (timer->win == win) free_timer( queue, timer );
1348 ptr = next;
1350 ptr = list_head( &queue->expired_timers );
1351 while (ptr)
1353 struct list *next = list_next( &queue->expired_timers, ptr );
1354 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1355 if (timer->win == win) free_timer( queue, timer );
1356 ptr = next;
1359 /* remove messages */
1360 for (i = 0; i < NB_MSG_KINDS; i++)
1362 struct list *ptr, *next;
1364 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1366 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1367 if (msg->win == win) remove_queue_message( queue, msg, i );
1371 thread_input_cleanup_window( queue, win );
1374 /* post a message to a window; used by socket handling */
1375 void post_message( user_handle_t win, unsigned int message,
1376 unsigned int wparam, unsigned int lparam )
1378 struct message *msg;
1379 struct thread *thread = get_window_thread( win );
1381 if (!thread) return;
1383 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1385 msg->type = MSG_POSTED;
1386 msg->win = get_user_full_handle( win );
1387 msg->msg = message;
1388 msg->wparam = wparam;
1389 msg->lparam = lparam;
1390 msg->time = get_tick_count();
1391 msg->x = 0;
1392 msg->y = 0;
1393 msg->info = 0;
1394 msg->hook = 0;
1395 msg->hook_proc = NULL;
1396 msg->result = NULL;
1397 msg->data = NULL;
1398 msg->data_size = 0;
1400 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1401 set_queue_bits( thread->queue, QS_POSTMESSAGE );
1403 release_object( thread );
1406 /* post a win event */
1407 void post_win_event( struct thread *thread, unsigned int event,
1408 user_handle_t win, unsigned int object_id,
1409 unsigned int child_id, void *hook_proc,
1410 const WCHAR *module, size_t module_size,
1411 user_handle_t hook)
1413 struct message *msg;
1415 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1417 msg->type = MSG_WINEVENT;
1418 msg->win = get_user_full_handle( win );
1419 msg->msg = event;
1420 msg->wparam = object_id;
1421 msg->lparam = child_id;
1422 msg->time = get_tick_count();
1423 msg->x = 0;
1424 msg->y = 0;
1425 msg->info = get_thread_id( current );
1426 msg->result = NULL;
1427 msg->hook = hook;
1428 msg->hook_proc = hook_proc;
1430 if ((msg->data = malloc( module_size )))
1432 msg->data_size = module_size;
1433 memcpy( msg->data, module, module_size );
1435 if (debug_level > 1)
1436 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1437 get_thread_id(thread), event, win, object_id, child_id );
1438 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1439 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1441 else
1442 free( msg );
1446 /* get the message queue of the current thread */
1447 DECL_HANDLER(get_msg_queue)
1449 struct msg_queue *queue = get_current_queue();
1451 reply->handle = 0;
1452 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1456 /* set the current message queue wakeup mask */
1457 DECL_HANDLER(set_queue_mask)
1459 struct msg_queue *queue = get_current_queue();
1461 if (queue)
1463 queue->wake_mask = req->wake_mask;
1464 queue->changed_mask = req->changed_mask;
1465 reply->wake_bits = queue->wake_bits;
1466 reply->changed_bits = queue->changed_bits;
1467 if (is_signaled( queue ))
1469 /* if skip wait is set, do what would have been done in the subsequent wait */
1470 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1471 else wake_up( &queue->obj, 0 );
1477 /* get the current message queue status */
1478 DECL_HANDLER(get_queue_status)
1480 struct msg_queue *queue = current->queue;
1481 if (queue)
1483 reply->wake_bits = queue->wake_bits;
1484 reply->changed_bits = queue->changed_bits;
1485 if (req->clear) queue->changed_bits = 0;
1487 else reply->wake_bits = reply->changed_bits = 0;
1491 /* send a message to a thread queue */
1492 DECL_HANDLER(send_message)
1494 struct message *msg;
1495 struct msg_queue *send_queue = get_current_queue();
1496 struct msg_queue *recv_queue = NULL;
1497 struct thread *thread = NULL;
1499 if (req->id)
1501 if (!(thread = get_thread_from_id( req->id ))) return;
1503 else if (req->type != MSG_HARDWARE)
1505 /* only hardware messages are allowed without destination thread */
1506 set_error( STATUS_INVALID_PARAMETER );
1507 return;
1510 if (thread && !(recv_queue = thread->queue))
1512 set_error( STATUS_INVALID_PARAMETER );
1513 release_object( thread );
1514 return;
1516 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1518 set_error( STATUS_TIMEOUT );
1519 release_object( thread );
1520 return;
1523 if ((msg = mem_alloc( sizeof(*msg) )))
1525 msg->type = req->type;
1526 msg->win = get_user_full_handle( req->win );
1527 msg->msg = req->msg;
1528 msg->wparam = req->wparam;
1529 msg->lparam = req->lparam;
1530 msg->time = req->time;
1531 msg->x = req->x;
1532 msg->y = req->y;
1533 msg->info = req->info;
1534 msg->hook = 0;
1535 msg->hook_proc = NULL;
1536 msg->result = NULL;
1537 msg->data = NULL;
1538 msg->data_size = 0;
1540 switch(msg->type)
1542 case MSG_OTHER_PROCESS:
1543 msg->data_size = get_req_data_size();
1544 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1546 free( msg );
1547 break;
1549 /* fall through */
1550 case MSG_ASCII:
1551 case MSG_UNICODE:
1552 case MSG_CALLBACK:
1553 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1554 req->timeout, req->callback, req->info )))
1556 free_message( msg );
1557 break;
1559 /* fall through */
1560 case MSG_NOTIFY:
1561 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1562 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1563 break;
1564 case MSG_POSTED:
1565 /* needed for posted DDE messages */
1566 msg->data_size = get_req_data_size();
1567 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1569 free( msg );
1570 break;
1572 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1573 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1574 break;
1575 case MSG_HARDWARE:
1576 queue_hardware_message( recv_queue, msg );
1577 break;
1578 case MSG_CALLBACK_RESULT: /* cannot send this one */
1579 default:
1580 set_error( STATUS_INVALID_PARAMETER );
1581 free( msg );
1582 break;
1585 if (thread) release_object( thread );
1589 /* get a message from the current queue */
1590 DECL_HANDLER(get_message)
1592 struct timer *timer;
1593 struct list *ptr;
1594 struct msg_queue *queue = get_current_queue();
1595 user_handle_t get_win = get_user_full_handle( req->get_win );
1597 reply->active_hooks = get_active_hooks();
1599 if (!queue) return;
1600 gettimeofday( &queue->last_get_msg, NULL );
1602 /* first check for sent messages */
1603 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1605 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1606 receive_message( queue, msg, reply );
1607 return;
1609 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1611 /* clear changed bits so we can wait on them if we don't find a message */
1612 queue->changed_bits = 0;
1614 /* then check for posted messages */
1615 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1616 return;
1618 /* then check for any raw hardware message */
1619 if (filter_contains_hw_range( req->get_first, req->get_last ) &&
1620 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1621 return;
1623 /* now check for WM_PAINT */
1624 if (queue->paint_count &&
1625 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1626 (reply->win = find_window_to_repaint( get_win, current )))
1628 reply->type = MSG_POSTED;
1629 reply->msg = WM_PAINT;
1630 reply->wparam = 0;
1631 reply->lparam = 0;
1632 reply->x = 0;
1633 reply->y = 0;
1634 reply->time = get_tick_count();
1635 reply->info = 0;
1636 return;
1639 /* now check for timer */
1640 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1641 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1643 reply->type = MSG_POSTED;
1644 reply->win = timer->win;
1645 reply->msg = timer->msg;
1646 reply->wparam = timer->id;
1647 reply->lparam = timer->lparam;
1648 reply->x = 0;
1649 reply->y = 0;
1650 reply->time = get_tick_count();
1651 reply->info = 0;
1652 return;
1655 done:
1656 set_error( STATUS_PENDING ); /* FIXME */
1660 /* reply to a sent message */
1661 DECL_HANDLER(reply_message)
1663 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1664 else if (current->queue->recv_result)
1665 reply_message( current->queue, req->result, 0, req->remove,
1666 get_req_data(), get_req_data_size() );
1670 /* accept the current hardware message */
1671 DECL_HANDLER(accept_hardware_message)
1673 if (current->queue)
1674 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1675 else
1676 set_error( STATUS_ACCESS_DENIED );
1680 /* retrieve the reply for the last message sent */
1681 DECL_HANDLER(get_message_reply)
1683 struct message_result *result;
1684 struct list *entry;
1685 struct msg_queue *queue = current->queue;
1687 if (queue)
1689 set_error( STATUS_PENDING );
1690 reply->result = 0;
1692 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1694 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1695 if (result->replied || req->cancel)
1697 if (result->replied)
1699 reply->result = result->result;
1700 set_error( result->error );
1701 if (result->data)
1703 size_t data_len = min( result->data_size, get_reply_max_size() );
1704 set_reply_data_ptr( result->data, data_len );
1705 result->data = NULL;
1706 result->data_size = 0;
1709 remove_result_from_sender( result );
1711 entry = list_head( &queue->send_result );
1712 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1713 else
1715 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1716 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1720 else set_error( STATUS_ACCESS_DENIED );
1724 /* set a window timer */
1725 DECL_HANDLER(set_win_timer)
1727 struct timer *timer;
1728 struct msg_queue *queue;
1729 struct thread *thread = NULL;
1730 user_handle_t win = 0;
1731 unsigned int id = req->id;
1733 if (req->win)
1735 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1737 set_error( STATUS_INVALID_HANDLE );
1738 return;
1740 if (thread->process != current->process)
1742 release_object( thread );
1743 set_error( STATUS_ACCESS_DENIED );
1744 return;
1746 queue = thread->queue;
1747 /* remove it if it existed already */
1748 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1750 else
1752 queue = get_current_queue();
1753 /* find a free id for it */
1756 id = queue->next_timer_id;
1757 if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1759 while (find_timer( queue, 0, req->msg, id ));
1762 if ((timer = set_timer( queue, req->rate )))
1764 timer->win = win;
1765 timer->msg = req->msg;
1766 timer->id = id;
1767 timer->lparam = req->lparam;
1768 reply->id = id;
1770 if (thread) release_object( thread );
1773 /* kill a window timer */
1774 DECL_HANDLER(kill_win_timer)
1776 struct timer *timer;
1777 struct thread *thread;
1778 user_handle_t win = 0;
1780 if (req->win)
1782 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1784 set_error( STATUS_INVALID_HANDLE );
1785 return;
1787 if (thread->process != current->process)
1789 release_object( thread );
1790 set_error( STATUS_ACCESS_DENIED );
1791 return;
1794 else thread = (struct thread *)grab_object( current );
1796 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1797 free_timer( thread->queue, timer );
1798 else
1799 set_error( STATUS_INVALID_PARAMETER );
1801 release_object( thread );
1805 /* attach (or detach) thread inputs */
1806 DECL_HANDLER(attach_thread_input)
1808 struct thread *thread_from = get_thread_from_id( req->tid_from );
1809 struct thread *thread_to = get_thread_from_id( req->tid_to );
1811 if (!thread_from || !thread_to)
1813 if (thread_from) release_object( thread_from );
1814 if (thread_to) release_object( thread_to );
1815 return;
1817 if (thread_from != thread_to)
1819 if (req->attach) attach_thread_input( thread_from, thread_to );
1820 else
1822 if (thread_from->queue && thread_to->queue &&
1823 thread_from->queue->input == thread_to->queue->input)
1824 detach_thread_input( thread_from );
1825 else
1826 set_error( STATUS_ACCESS_DENIED );
1829 else set_error( STATUS_ACCESS_DENIED );
1830 release_object( thread_from );
1831 release_object( thread_to );
1835 /* get thread input data */
1836 DECL_HANDLER(get_thread_input)
1838 struct thread *thread = NULL;
1839 struct thread_input *input;
1841 if (req->tid)
1843 if (!(thread = get_thread_from_id( req->tid ))) return;
1844 input = thread->queue ? thread->queue->input : NULL;
1846 else input = foreground_input; /* get the foreground thread info */
1848 if (input)
1850 reply->focus = input->focus;
1851 reply->capture = input->capture;
1852 reply->active = input->active;
1853 reply->menu_owner = input->menu_owner;
1854 reply->move_size = input->move_size;
1855 reply->caret = input->caret;
1856 reply->rect = input->caret_rect;
1858 else
1860 reply->focus = 0;
1861 reply->capture = 0;
1862 reply->active = 0;
1863 reply->menu_owner = 0;
1864 reply->move_size = 0;
1865 reply->caret = 0;
1866 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1868 /* foreground window is active window of foreground thread */
1869 reply->foreground = foreground_input ? foreground_input->active : 0;
1870 if (thread) release_object( thread );
1874 /* retrieve queue keyboard state for a given thread */
1875 DECL_HANDLER(get_key_state)
1877 struct thread *thread;
1878 struct thread_input *input;
1880 if (!(thread = get_thread_from_id( req->tid ))) return;
1881 input = thread->queue ? thread->queue->input : NULL;
1882 if (input)
1884 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1885 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1887 release_object( thread );
1891 /* set queue keyboard state for a given thread */
1892 DECL_HANDLER(set_key_state)
1894 struct thread *thread = NULL;
1895 struct thread_input *input;
1897 if (!(thread = get_thread_from_id( req->tid ))) return;
1898 input = thread->queue ? thread->queue->input : NULL;
1899 if (input)
1901 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1902 if (size) memcpy( input->keystate, get_req_data(), size );
1904 release_object( thread );
1908 /* set the system foreground window */
1909 DECL_HANDLER(set_foreground_window)
1911 struct msg_queue *queue = get_current_queue();
1913 reply->previous = foreground_input ? foreground_input->active : 0;
1914 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1915 reply->send_msg_new = FALSE;
1917 if (req->handle)
1919 struct thread *thread;
1921 if (is_top_level_window( req->handle ) &&
1922 ((thread = get_window_thread( req->handle ))))
1924 foreground_input = thread->queue->input;
1925 reply->send_msg_new = (foreground_input != queue->input);
1926 release_object( thread );
1928 else set_error( STATUS_INVALID_HANDLE );
1930 else foreground_input = NULL;
1934 /* set the current thread focus window */
1935 DECL_HANDLER(set_focus_window)
1937 struct msg_queue *queue = get_current_queue();
1939 reply->previous = 0;
1940 if (queue && check_queue_input_window( queue, req->handle ))
1942 reply->previous = queue->input->focus;
1943 queue->input->focus = get_user_full_handle( req->handle );
1948 /* set the current thread active window */
1949 DECL_HANDLER(set_active_window)
1951 struct msg_queue *queue = get_current_queue();
1953 reply->previous = 0;
1954 if (queue && check_queue_input_window( queue, req->handle ))
1956 if (!req->handle || make_window_active( req->handle ))
1958 reply->previous = queue->input->active;
1959 queue->input->active = get_user_full_handle( req->handle );
1961 else set_error( STATUS_INVALID_HANDLE );
1966 /* set the current thread capture window */
1967 DECL_HANDLER(set_capture_window)
1969 struct msg_queue *queue = get_current_queue();
1971 reply->previous = reply->full_handle = 0;
1972 if (queue && check_queue_input_window( queue, req->handle ))
1974 struct thread_input *input = queue->input;
1976 reply->previous = input->capture;
1977 input->capture = get_user_full_handle( req->handle );
1978 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1979 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1980 reply->full_handle = input->capture;
1985 /* Set the current thread caret window */
1986 DECL_HANDLER(set_caret_window)
1988 struct msg_queue *queue = get_current_queue();
1990 reply->previous = 0;
1991 if (queue && check_queue_input_window( queue, req->handle ))
1993 struct thread_input *input = queue->input;
1995 reply->previous = input->caret;
1996 reply->old_rect = input->caret_rect;
1997 reply->old_hide = input->caret_hide;
1998 reply->old_state = input->caret_state;
2000 set_caret_window( input, get_user_full_handle(req->handle) );
2001 input->caret_rect.right = input->caret_rect.left + req->width;
2002 input->caret_rect.bottom = input->caret_rect.top + req->height;
2007 /* Set the current thread caret information */
2008 DECL_HANDLER(set_caret_info)
2010 struct msg_queue *queue = get_current_queue();
2011 struct thread_input *input;
2013 if (!queue) return;
2014 input = queue->input;
2015 reply->full_handle = input->caret;
2016 reply->old_rect = input->caret_rect;
2017 reply->old_hide = input->caret_hide;
2018 reply->old_state = input->caret_state;
2020 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2022 set_error( STATUS_ACCESS_DENIED );
2023 return;
2025 if (req->flags & SET_CARET_POS)
2027 input->caret_rect.right += req->x - input->caret_rect.left;
2028 input->caret_rect.bottom += req->y - input->caret_rect.top;
2029 input->caret_rect.left = req->x;
2030 input->caret_rect.top = req->y;
2032 if (req->flags & SET_CARET_HIDE)
2034 input->caret_hide += req->hide;
2035 if (input->caret_hide < 0) input->caret_hide = 0;
2037 if (req->flags & SET_CARET_STATE)
2039 if (req->state == -1) input->caret_state = !input->caret_state;
2040 else input->caret_state = !!req->state;
2045 /* get the time of the last input event */
2046 DECL_HANDLER(get_last_input_time)
2048 reply->time = last_input_time;