Remove unused include X11/IntrinsicP.h.
[wine/wine64.git] / server / queue.c
blob0453b0e2bb390b86f3be9bcae5447c85a75570b7
1 /*
2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "winternl.h"
37 #include "handle.h"
38 #include "file.h"
39 #include "thread.h"
40 #include "process.h"
41 #include "request.h"
42 #include "user.h"
44 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
45 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
47 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
48 #define NB_MSG_KINDS (POST_MESSAGE+1)
51 struct message_result
53 struct list sender_entry; /* entry in sender list */
54 struct message *msg; /* message the result is for */
55 struct message_result *recv_next; /* next in receiver list */
56 struct msg_queue *sender; /* sender queue */
57 struct msg_queue *receiver; /* receiver queue */
58 int replied; /* has it been replied to? */
59 unsigned int result; /* reply result */
60 unsigned int error; /* error code to pass back to sender */
61 struct message *callback_msg; /* message to queue for callback */
62 void *data; /* message reply data */
63 unsigned int data_size; /* size of message reply data */
64 struct timeout_user *timeout; /* result timeout */
67 struct message
69 struct list entry; /* entry in message list */
70 enum message_type type; /* message type */
71 user_handle_t win; /* window handle */
72 unsigned int msg; /* message code */
73 unsigned int wparam; /* parameters */
74 unsigned int lparam; /* parameters */
75 int x; /* x position */
76 int y; /* y position */
77 unsigned int time; /* message time */
78 unsigned int info; /* extra info */
79 user_handle_t hook; /* winevent hook handle */
80 void *hook_proc; /* winevent hook proc address */
81 void *data; /* message data for sent messages */
82 unsigned int data_size; /* size of message data */
83 unsigned int unique_id; /* unique id for nested hw message waits */
84 struct message_result *result; /* result in sender queue */
87 struct timer
89 struct list entry; /* entry in timer list */
90 struct timeval when; /* next expiration */
91 unsigned int rate; /* timer rate in ms */
92 user_handle_t win; /* window handle */
93 unsigned int msg; /* message to post */
94 unsigned int id; /* timer id */
95 unsigned int lparam; /* lparam for message */
98 struct thread_input
100 struct object obj; /* object header */
101 struct desktop *desktop; /* desktop that this thread input belongs to */
102 user_handle_t focus; /* focus window */
103 user_handle_t capture; /* capture window */
104 user_handle_t active; /* active window */
105 user_handle_t menu_owner; /* current menu owner window */
106 user_handle_t move_size; /* current moving/resizing window */
107 user_handle_t caret; /* caret window */
108 rectangle_t caret_rect; /* caret rectangle */
109 int caret_hide; /* caret hide count */
110 int caret_state; /* caret on/off state */
111 struct list msg_list; /* list of hardware messages */
112 unsigned char keystate[256]; /* state of each key */
115 struct msg_queue
117 struct object obj; /* object header */
118 unsigned int wake_bits; /* wakeup bits */
119 unsigned int wake_mask; /* wakeup mask */
120 unsigned int changed_bits; /* changed wakeup bits */
121 unsigned int changed_mask; /* changed wakeup mask */
122 int paint_count; /* pending paint messages count */
123 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
124 struct list send_result; /* stack of sent messages waiting for result */
125 struct list callback_result; /* list of callback messages waiting for result */
126 struct message_result *recv_result; /* stack of received messages waiting for result */
127 struct list pending_timers; /* list of pending timers */
128 struct list expired_timers; /* list of expired timers */
129 unsigned int next_timer_id; /* id for the next timer with a 0 window */
130 struct timeout_user *timeout; /* timeout for next timer to expire */
131 struct thread_input *input; /* thread input descriptor */
132 struct hook_table *hooks; /* hook table */
133 struct timeval last_get_msg; /* time of last get message call */
136 static void msg_queue_dump( struct object *obj, int verbose );
137 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
138 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
139 static int msg_queue_signaled( struct object *obj, struct thread *thread );
140 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
141 static void msg_queue_destroy( struct object *obj );
142 static void thread_input_dump( struct object *obj, int verbose );
143 static void thread_input_destroy( struct object *obj );
144 static void timer_callback( void *private );
146 static const struct object_ops msg_queue_ops =
148 sizeof(struct msg_queue), /* size */
149 msg_queue_dump, /* dump */
150 msg_queue_add_queue, /* add_queue */
151 msg_queue_remove_queue, /* remove_queue */
152 msg_queue_signaled, /* signaled */
153 msg_queue_satisfied, /* satisfied */
154 no_signal, /* signal */
155 no_get_fd, /* get_fd */
156 no_lookup_name, /* lookup_name */
157 no_close_handle, /* close_handle */
158 msg_queue_destroy /* destroy */
162 static const struct object_ops thread_input_ops =
164 sizeof(struct thread_input), /* size */
165 thread_input_dump, /* dump */
166 no_add_queue, /* add_queue */
167 NULL, /* remove_queue */
168 NULL, /* signaled */
169 NULL, /* satisfied */
170 no_signal, /* signal */
171 no_get_fd, /* get_fd */
172 no_lookup_name, /* lookup_name */
173 no_close_handle, /* close_handle */
174 thread_input_destroy /* destroy */
177 /* pointer to input structure of foreground thread */
178 static struct thread_input *foreground_input;
179 static unsigned int last_input_time;
182 /* set the caret window in a given thread input */
183 static void set_caret_window( struct thread_input *input, user_handle_t win )
185 if (!win || win != input->caret)
187 input->caret_rect.left = 0;
188 input->caret_rect.top = 0;
189 input->caret_rect.right = 0;
190 input->caret_rect.bottom = 0;
192 input->caret = win;
193 input->caret_hide = 1;
194 input->caret_state = 0;
197 /* create a thread input object */
198 static struct thread_input *create_thread_input( struct thread *thread )
200 struct thread_input *input;
202 if ((input = alloc_object( &thread_input_ops )))
204 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
206 free( input );
207 return NULL;
209 input->focus = 0;
210 input->capture = 0;
211 input->active = 0;
212 input->menu_owner = 0;
213 input->move_size = 0;
214 list_init( &input->msg_list );
215 set_caret_window( input, 0 );
216 memset( input->keystate, 0, sizeof(input->keystate) );
218 return input;
221 /* release the thread input data of a given thread */
222 static inline void release_thread_input( struct thread *thread )
224 struct thread_input *input = thread->queue->input;
226 if (!input) return;
227 release_object( input );
228 thread->queue->input = NULL;
231 /* create a message queue object */
232 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
234 struct msg_queue *queue;
235 int i;
237 if (!input && !(input = create_thread_input( thread ))) return NULL;
238 if ((queue = alloc_object( &msg_queue_ops )))
240 queue->wake_bits = 0;
241 queue->wake_mask = 0;
242 queue->changed_bits = 0;
243 queue->changed_mask = 0;
244 queue->paint_count = 0;
245 queue->recv_result = NULL;
246 queue->next_timer_id = 1;
247 queue->timeout = NULL;
248 queue->input = (struct thread_input *)grab_object( input );
249 queue->hooks = NULL;
250 gettimeofday( &queue->last_get_msg, NULL );
251 list_init( &queue->send_result );
252 list_init( &queue->callback_result );
253 list_init( &queue->pending_timers );
254 list_init( &queue->expired_timers );
255 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
257 thread->queue = queue;
258 if (!thread->process->queue)
259 thread->process->queue = (struct msg_queue *)grab_object( queue );
261 release_object( input );
262 return queue;
265 /* free the message queue of a thread at thread exit */
266 void free_msg_queue( struct thread *thread )
268 struct process *process = thread->process;
270 remove_thread_hooks( thread );
271 if (!thread->queue) return;
272 if (process->queue == thread->queue) /* is it the process main queue? */
274 release_object( process->queue );
275 process->queue = NULL;
276 if (process->idle_event)
278 set_event( process->idle_event );
279 release_object( process->idle_event );
280 process->idle_event = NULL;
283 release_object( thread->queue );
284 thread->queue = NULL;
287 /* get the hook table for a given thread */
288 struct hook_table *get_queue_hooks( struct thread *thread )
290 if (!thread->queue) return NULL;
291 return thread->queue->hooks;
294 /* set the hook table for a given thread, allocating the queue if needed */
295 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
297 struct msg_queue *queue = thread->queue;
298 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
299 if (queue->hooks) release_object( queue->hooks );
300 queue->hooks = hooks;
303 /* check the queue status */
304 inline static int is_signaled( struct msg_queue *queue )
306 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
309 /* set some queue bits */
310 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
312 queue->wake_bits |= bits;
313 queue->changed_bits |= bits;
314 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
317 /* clear some queue bits */
318 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
320 queue->wake_bits &= ~bits;
321 queue->changed_bits &= ~bits;
324 /* check whether msg is a keyboard message */
325 inline static int is_keyboard_msg( struct message *msg )
327 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
330 /* check if message is matched by the filter */
331 inline static int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
333 return (msg >= first && msg <= last);
336 /* check whether a message filter contains at least one potential hardware message */
337 inline static int filter_contains_hw_range( unsigned int first, unsigned int last )
339 /* hardware message ranges are (in numerical order):
340 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
341 * WM_KEYFIRST .. WM_KEYLAST
342 * WM_MOUSEFIRST .. WM_MOUSELAST
344 if (last < WM_NCMOUSEFIRST) return 0;
345 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
346 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
347 if (first > WM_MOUSELAST) return 0;
348 return 1;
351 /* get the QS_* bit corresponding to a given hardware message */
352 inline static int get_hardware_msg_bit( struct message *msg )
354 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
355 if (is_keyboard_msg( msg )) return QS_KEY;
356 return QS_MOUSEBUTTON;
359 /* get the current thread queue, creating it if needed */
360 inline static struct msg_queue *get_current_queue(void)
362 struct msg_queue *queue = current->queue;
363 if (!queue) queue = create_msg_queue( current, NULL );
364 return queue;
367 /* get a (pseudo-)unique id to tag hardware messages */
368 inline static unsigned int get_unique_id(void)
370 static unsigned int id;
371 if (!++id) id = 1; /* avoid an id of 0 */
372 return id;
375 /* try to merge a message with the last in the list; return 1 if successful */
376 static int merge_message( struct thread_input *input, const struct message *msg )
378 struct message *prev;
379 struct list *ptr = list_tail( &input->msg_list );
381 if (!ptr) return 0;
382 prev = LIST_ENTRY( ptr, struct message, entry );
383 if (prev->unique_id) return 0;
384 if (prev->result) return 0;
385 if (prev->win != msg->win) return 0;
386 if (prev->msg != msg->msg) return 0;
387 if (prev->type != msg->type) return 0;
388 /* now we can merge it */
389 prev->wparam = msg->wparam;
390 prev->lparam = msg->lparam;
391 prev->x = msg->x;
392 prev->y = msg->y;
393 prev->time = msg->time;
394 prev->info = msg->info;
395 return 1;
398 /* free a result structure */
399 static void free_result( struct message_result *result )
401 if (result->timeout) remove_timeout_user( result->timeout );
402 if (result->data) free( result->data );
403 if (result->callback_msg) free( result->callback_msg );
404 free( result );
407 /* remove the result from the sender list it is on */
408 static inline void remove_result_from_sender( struct message_result *result )
410 assert( result->sender );
412 list_remove( &result->sender_entry );
413 result->sender = NULL;
414 if (!result->receiver) free_result( result );
417 /* store the message result in the appropriate structure */
418 static void store_message_result( struct message_result *res, unsigned int result,
419 unsigned int error )
421 res->result = result;
422 res->error = error;
423 res->replied = 1;
424 if (res->timeout)
426 remove_timeout_user( res->timeout );
427 res->timeout = NULL;
429 if (res->sender)
431 if (res->callback_msg)
433 /* queue the callback message in the sender queue */
434 res->callback_msg->lparam = result;
435 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
436 set_queue_bits( res->sender, QS_SENDMESSAGE );
437 res->callback_msg = NULL;
438 remove_result_from_sender( res );
440 else
442 /* wake sender queue if waiting on this result */
443 if (list_head(&res->sender->send_result) == &res->sender_entry)
444 set_queue_bits( res->sender, QS_SMRESULT );
450 /* free a message when deleting a queue or window */
451 static void free_message( struct message *msg )
453 struct message_result *result = msg->result;
454 if (result)
456 result->msg = NULL;
457 if (result->sender)
459 result->receiver = NULL;
460 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
462 else free_result( result );
464 if (msg->data) free( msg->data );
465 free( msg );
468 /* remove (and free) a message from a message list */
469 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
470 enum message_kind kind )
472 list_remove( &msg->entry );
473 switch(kind)
475 case SEND_MESSAGE:
476 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
477 break;
478 case POST_MESSAGE:
479 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
480 break;
482 free_message( msg );
485 /* message timed out without getting a reply */
486 static void result_timeout( void *private )
488 struct message_result *result = private;
490 assert( !result->replied );
492 result->timeout = NULL;
494 if (result->msg) /* not received yet */
496 struct message *msg = result->msg;
498 result->msg = NULL;
499 msg->result = NULL;
500 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
501 result->receiver = NULL;
502 if (!result->sender)
504 free_result( result );
505 return;
509 store_message_result( result, 0, STATUS_TIMEOUT );
512 /* allocate and fill a message result structure */
513 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
514 struct msg_queue *recv_queue,
515 struct message *msg, int timeout,
516 void *callback, unsigned int callback_data )
518 struct message_result *result = mem_alloc( sizeof(*result) );
519 if (result)
521 result->msg = msg;
522 result->sender = send_queue;
523 result->receiver = recv_queue;
524 result->replied = 0;
525 result->data = NULL;
526 result->data_size = 0;
527 result->timeout = NULL;
529 if (msg->type == MSG_CALLBACK)
531 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
532 if (!callback_msg)
534 free( result );
535 return NULL;
537 callback_msg->type = MSG_CALLBACK_RESULT;
538 callback_msg->win = msg->win;
539 callback_msg->msg = msg->msg;
540 callback_msg->wparam = (unsigned int)callback;
541 callback_msg->lparam = 0;
542 callback_msg->time = get_tick_count();
543 callback_msg->x = 0;
544 callback_msg->y = 0;
545 callback_msg->info = callback_data;
546 callback_msg->hook = 0;
547 callback_msg->hook_proc = NULL;
548 callback_msg->result = NULL;
549 callback_msg->data = NULL;
550 callback_msg->data_size = 0;
552 result->callback_msg = callback_msg;
553 list_add_head( &send_queue->callback_result, &result->sender_entry );
555 else
557 result->callback_msg = NULL;
558 list_add_head( &send_queue->send_result, &result->sender_entry );
561 if (timeout)
563 struct timeval when;
564 gettimeofday( &when, NULL );
565 add_timeout( &when, timeout );
566 result->timeout = add_timeout_user( &when, result_timeout, result );
569 return result;
572 /* receive a message, removing it from the sent queue */
573 static void receive_message( struct msg_queue *queue, struct message *msg,
574 struct get_message_reply *reply )
576 struct message_result *result = msg->result;
578 reply->total = msg->data_size;
579 if (msg->data_size > get_reply_max_size())
581 set_error( STATUS_BUFFER_OVERFLOW );
582 return;
584 reply->type = msg->type;
585 reply->win = msg->win;
586 reply->msg = msg->msg;
587 reply->wparam = msg->wparam;
588 reply->lparam = msg->lparam;
589 reply->x = msg->x;
590 reply->y = msg->y;
591 reply->time = msg->time;
592 reply->info = msg->info;
593 reply->hook = msg->hook;
594 reply->hook_proc = msg->hook_proc;
596 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
598 list_remove( &msg->entry );
599 /* put the result on the receiver result stack */
600 if (result)
602 result->msg = NULL;
603 result->recv_next = queue->recv_result;
604 queue->recv_result = result;
606 free( msg );
607 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
610 /* set the result of the current received message */
611 static void reply_message( struct msg_queue *queue, unsigned int result,
612 unsigned int error, int remove, const void *data, size_t len )
614 struct message_result *res = queue->recv_result;
616 if (remove)
618 queue->recv_result = res->recv_next;
619 res->receiver = NULL;
620 if (!res->sender) /* no one waiting for it */
622 free_result( res );
623 return;
626 if (!res->replied)
628 if (len && (res->data = memdup( data, len ))) res->data_size = len;
629 store_message_result( res, result, error );
633 /* retrieve a posted message */
634 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
635 unsigned int first, unsigned int last, unsigned int flags,
636 struct get_message_reply *reply )
638 struct message *msg;
640 /* check against the filters */
641 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
643 if (msg->msg == WM_QUIT) goto found; /* WM_QUIT is never filtered */
644 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
645 if (!check_msg_filter( msg->msg, first, last )) continue;
646 goto found; /* found one */
648 return 0;
650 /* return it to the app */
651 found:
652 reply->total = msg->data_size;
653 if (msg->data_size > get_reply_max_size())
655 set_error( STATUS_BUFFER_OVERFLOW );
656 return 1;
658 reply->type = msg->type;
659 reply->win = msg->win;
660 reply->msg = msg->msg;
661 reply->wparam = msg->wparam;
662 reply->lparam = msg->lparam;
663 reply->x = msg->x;
664 reply->y = msg->y;
665 reply->time = msg->time;
666 reply->info = msg->info;
668 if (flags & GET_MSG_REMOVE)
670 if (msg->data)
672 set_reply_data_ptr( msg->data, msg->data_size );
673 msg->data = NULL;
674 msg->data_size = 0;
676 remove_queue_message( queue, msg, POST_MESSAGE );
678 else if (msg->data) set_reply_data( msg->data, msg->data_size );
680 return 1;
683 /* empty a message list and free all the messages */
684 static void empty_msg_list( struct list *list )
686 struct list *ptr;
688 while ((ptr = list_head( list )) != NULL)
690 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
691 list_remove( &msg->entry );
692 free_message( msg );
696 /* cleanup all pending results when deleting a queue */
697 static void cleanup_results( struct msg_queue *queue )
699 struct list *entry;
701 while ((entry = list_head( &queue->send_result )) != NULL)
703 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
706 while ((entry = list_head( &queue->callback_result )) != NULL)
708 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
711 while (queue->recv_result)
712 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
715 /* check if the thread owning the queue is hung (not checking for messages) */
716 static int is_queue_hung( struct msg_queue *queue )
718 struct timeval now;
719 struct wait_queue_entry *entry;
721 gettimeofday( &now, NULL );
722 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
723 return 0; /* less than 5 seconds since last get message -> not hung */
725 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
727 if (entry->thread->queue == queue)
728 return 0; /* thread is waiting on queue -> not hung */
730 return 1;
733 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
735 struct msg_queue *queue = (struct msg_queue *)obj;
736 struct process *process = entry->thread->process;
738 /* a thread can only wait on its own queue */
739 if (entry->thread->queue != queue)
741 set_error( STATUS_ACCESS_DENIED );
742 return 0;
744 /* if waiting on the main process queue, set the idle event */
745 if (process->queue == queue)
747 if (process->idle_event) set_event( process->idle_event );
749 add_queue( obj, entry );
750 return 1;
753 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
755 struct msg_queue *queue = (struct msg_queue *)obj;
756 struct process *process = entry->thread->process;
758 remove_queue( obj, entry );
760 assert( entry->thread->queue == queue );
762 /* if waiting on the main process queue, reset the idle event */
763 if (process->queue == queue)
765 if (process->idle_event) reset_event( process->idle_event );
769 static void msg_queue_dump( struct object *obj, int verbose )
771 struct msg_queue *queue = (struct msg_queue *)obj;
772 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
773 queue->wake_bits, queue->wake_mask );
776 static int msg_queue_signaled( struct object *obj, struct thread *thread )
778 struct msg_queue *queue = (struct msg_queue *)obj;
779 return is_signaled( queue );
782 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
784 struct msg_queue *queue = (struct msg_queue *)obj;
785 queue->wake_mask = 0;
786 queue->changed_mask = 0;
787 return 0; /* Not abandoned */
790 static void msg_queue_destroy( struct object *obj )
792 struct msg_queue *queue = (struct msg_queue *)obj;
793 struct list *ptr;
794 int i;
796 cleanup_results( queue );
797 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
799 while ((ptr = list_head( &queue->pending_timers )))
801 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
802 list_remove( &timer->entry );
803 free( timer );
805 while ((ptr = list_head( &queue->expired_timers )))
807 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
808 list_remove( &timer->entry );
809 free( timer );
811 if (queue->timeout) remove_timeout_user( queue->timeout );
812 if (queue->input) release_object( queue->input );
813 if (queue->hooks) release_object( queue->hooks );
816 static void thread_input_dump( struct object *obj, int verbose )
818 struct thread_input *input = (struct thread_input *)obj;
819 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
820 input->focus, input->capture, input->active );
823 static void thread_input_destroy( struct object *obj )
825 struct thread_input *input = (struct thread_input *)obj;
827 if (foreground_input == input) foreground_input = NULL;
828 empty_msg_list( &input->msg_list );
829 release_object( input->desktop );
832 /* fix the thread input data when a window is destroyed */
833 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
835 struct thread_input *input = queue->input;
837 if (window == input->focus) input->focus = 0;
838 if (window == input->capture) input->capture = 0;
839 if (window == input->active) input->active = 0;
840 if (window == input->menu_owner) input->menu_owner = 0;
841 if (window == input->move_size) input->move_size = 0;
842 if (window == input->caret) set_caret_window( input, 0 );
845 /* check if the specified window can be set in the input data of a given queue */
846 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
848 struct thread *thread;
849 int ret = 0;
851 if (!window) return 1; /* we can always clear the data */
853 if ((thread = get_window_thread( window )))
855 ret = (queue->input == thread->queue->input);
856 if (!ret) set_error( STATUS_ACCESS_DENIED );
857 release_object( thread );
859 else set_error( STATUS_INVALID_HANDLE );
861 return ret;
864 /* make sure the specified thread has a queue */
865 int init_thread_queue( struct thread *thread )
867 if (thread->queue) return 1;
868 return (create_msg_queue( thread, NULL ) != NULL);
871 /* attach two thread input data structures */
872 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
874 struct desktop *desktop;
875 struct thread_input *input;
877 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
878 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
879 input = (struct thread_input *)grab_object( thread_to->queue->input );
880 if (input->desktop != desktop)
882 set_error( STATUS_ACCESS_DENIED );
883 release_object( input );
884 release_object( desktop );
885 return 0;
887 release_object( desktop );
889 if (thread_from->queue)
891 release_thread_input( thread_from );
892 thread_from->queue->input = input;
894 else
896 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
898 memset( input->keystate, 0, sizeof(input->keystate) );
899 return 1;
902 /* detach two thread input data structures */
903 void detach_thread_input( struct thread *thread_from )
905 struct thread_input *input;
907 if ((input = create_thread_input( thread_from )))
909 release_thread_input( thread_from );
910 thread_from->queue->input = input;
915 /* set the next timer to expire */
916 static void set_next_timer( struct msg_queue *queue )
918 struct list *ptr;
920 if (queue->timeout)
922 remove_timeout_user( queue->timeout );
923 queue->timeout = NULL;
925 if ((ptr = list_head( &queue->pending_timers )))
927 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
928 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
930 /* set/clear QS_TIMER bit */
931 if (list_empty( &queue->expired_timers ))
932 clear_queue_bits( queue, QS_TIMER );
933 else
934 set_queue_bits( queue, QS_TIMER );
937 /* find a timer from its window and id */
938 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
939 unsigned int msg, unsigned int id )
941 struct list *ptr;
943 /* we need to search both lists */
945 LIST_FOR_EACH( ptr, &queue->pending_timers )
947 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
948 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
950 LIST_FOR_EACH( ptr, &queue->expired_timers )
952 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
953 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
955 return NULL;
958 /* callback for the next timer expiration */
959 static void timer_callback( void *private )
961 struct msg_queue *queue = private;
962 struct list *ptr;
964 queue->timeout = NULL;
965 /* move on to the next timer */
966 ptr = list_head( &queue->pending_timers );
967 list_remove( ptr );
968 list_add_tail( &queue->expired_timers, ptr );
969 set_next_timer( queue );
972 /* link a timer at its rightful place in the queue list */
973 static void link_timer( struct msg_queue *queue, struct timer *timer )
975 struct list *ptr;
977 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
979 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
980 if (!time_before( &t->when, &timer->when )) break;
982 list_add_before( ptr, &timer->entry );
985 /* remove a timer from the queue timer list and free it */
986 static void free_timer( struct msg_queue *queue, struct timer *timer )
988 list_remove( &timer->entry );
989 free( timer );
990 set_next_timer( queue );
993 /* restart an expired timer */
994 static void restart_timer( struct msg_queue *queue, struct timer *timer )
996 struct timeval now;
998 list_remove( &timer->entry );
999 gettimeofday( &now, NULL );
1000 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
1001 link_timer( queue, timer );
1002 set_next_timer( queue );
1005 /* find an expired timer matching the filtering parameters */
1006 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1007 unsigned int get_first, unsigned int get_last,
1008 int remove )
1010 struct list *ptr;
1012 LIST_FOR_EACH( ptr, &queue->expired_timers )
1014 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1015 if (win && timer->win != win) continue;
1016 if (check_msg_filter( timer->msg, get_first, get_last ))
1018 if (remove) restart_timer( queue, timer );
1019 return timer;
1022 return NULL;
1025 /* add a timer */
1026 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1028 struct timer *timer = mem_alloc( sizeof(*timer) );
1029 if (timer)
1031 timer->rate = max( rate, 1 );
1032 gettimeofday( &timer->when, NULL );
1033 add_timeout( &timer->when, rate );
1034 link_timer( queue, timer );
1035 /* check if we replaced the next timer */
1036 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1038 return timer;
1041 /* change the input key state for a given key */
1042 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1044 if (down)
1046 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1047 input->keystate[key] |= 0x80;
1049 else input->keystate[key] &= ~0x80;
1052 /* update the input key state for a keyboard message */
1053 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1055 unsigned char key;
1056 int down = 0, extended;
1058 switch (msg->msg)
1060 case WM_LBUTTONDOWN:
1061 down = 1;
1062 /* fall through */
1063 case WM_LBUTTONUP:
1064 set_input_key_state( input, VK_LBUTTON, down );
1065 break;
1066 case WM_MBUTTONDOWN:
1067 down = 1;
1068 /* fall through */
1069 case WM_MBUTTONUP:
1070 set_input_key_state( input, VK_MBUTTON, down );
1071 break;
1072 case WM_RBUTTONDOWN:
1073 down = 1;
1074 /* fall through */
1075 case WM_RBUTTONUP:
1076 set_input_key_state( input, VK_RBUTTON, down );
1077 break;
1078 case WM_XBUTTONDOWN:
1079 down = 1;
1080 /* fall through */
1081 case WM_XBUTTONUP:
1082 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1083 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1084 break;
1085 case WM_KEYDOWN:
1086 case WM_SYSKEYDOWN:
1087 down = 1;
1088 /* fall through */
1089 case WM_KEYUP:
1090 case WM_SYSKEYUP:
1091 key = (unsigned char)msg->wparam;
1092 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1093 set_input_key_state( input, key, down );
1094 switch(key)
1096 case VK_SHIFT:
1097 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1098 break;
1099 case VK_CONTROL:
1100 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1101 break;
1102 case VK_MENU:
1103 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1104 break;
1106 break;
1110 /* release the hardware message currently being processed by the given thread */
1111 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1112 int remove, user_handle_t new_win )
1114 struct thread_input *input = queue->input;
1115 struct message *msg;
1117 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1119 if (msg->unique_id == hw_id) break;
1121 if (&msg->entry == &input->msg_list) return; /* not found */
1123 /* clear the queue bit for that message */
1124 if (remove || new_win)
1126 struct message *other;
1127 int clr_bit;
1129 clr_bit = get_hardware_msg_bit( msg );
1130 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1132 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1134 clr_bit = 0;
1135 break;
1138 if (clr_bit) clear_queue_bits( queue, clr_bit );
1141 if (new_win) /* set the new window */
1143 struct thread *owner = get_window_thread( new_win );
1144 if (owner)
1146 if (owner->queue->input == input)
1148 msg->win = new_win;
1149 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1150 remove = 0;
1152 release_object( owner );
1155 if (remove)
1157 update_input_key_state( input, msg );
1158 list_remove( &msg->entry );
1159 free_message( msg );
1163 /* find the window that should receive a given hardware message */
1164 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1165 unsigned int *msg_code )
1167 user_handle_t win = 0;
1169 *msg_code = msg->msg;
1170 if (is_keyboard_msg( msg ))
1172 if (input && !(win = input->focus))
1174 win = input->active;
1175 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1178 else /* mouse message */
1180 if (!input || !(win = input->capture))
1182 if (!(win = msg->win) || !is_window_visible( win ))
1184 if (input) win = window_from_point( input->desktop, msg->x, msg->y );
1188 return win;
1191 /* queue a hardware message into a given thread input */
1192 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1194 user_handle_t win;
1195 struct thread *thread;
1196 struct thread_input *input;
1197 unsigned int msg_code;
1199 last_input_time = get_tick_count();
1201 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1202 if (!win || !(thread = get_window_thread(win)))
1204 free( msg );
1205 return;
1207 input = thread->queue->input;
1209 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1210 else
1212 msg->unique_id = 0; /* will be set once we return it to the app */
1213 list_add_tail( &input->msg_list, &msg->entry );
1214 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1216 release_object( thread );
1219 /* check message filter for a hardware message */
1220 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1221 user_handle_t filter_win, unsigned int first, unsigned int last )
1223 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1225 /* we can only test the window for a keyboard message since the
1226 * dest window for a mouse message depends on hittest */
1227 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1228 return 0;
1229 /* the message code is final for a keyboard message, we can simply check it */
1230 return check_msg_filter( msg_code, first, last );
1232 else /* mouse message */
1234 /* we need to check all possible values that the message can have in the end */
1236 if (check_msg_filter( msg_code, first, last )) return 1;
1237 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1239 /* all other messages can become non-client messages */
1240 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1242 /* clicks can become double-clicks or non-client double-clicks */
1243 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1244 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1246 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1247 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1249 return 0;
1254 /* find a hardware message for the given queue */
1255 static int get_hardware_message( struct thread *thread, int hw_id, user_handle_t filter_win,
1256 unsigned int first, unsigned int last, struct get_message_reply *reply )
1258 struct thread_input *input = thread->queue->input;
1259 struct thread *win_thread;
1260 struct list *ptr;
1261 user_handle_t win;
1262 int clear_bits, got_one = 0;
1263 unsigned int msg_code;
1265 ptr = list_head( &input->msg_list );
1266 if (hw_id)
1268 while (ptr)
1270 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1271 if (msg->unique_id == hw_id) break;
1272 ptr = list_next( &input->msg_list, ptr );
1274 if (!ptr) ptr = list_head( &input->msg_list );
1275 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1278 if (ptr == list_head( &input->msg_list ))
1279 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1280 else
1281 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1283 while (ptr)
1285 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1286 ptr = list_next( &input->msg_list, ptr );
1287 win = find_hardware_message_window( input, msg, &msg_code );
1288 if (!win || !(win_thread = get_window_thread( win )))
1290 /* no window at all, remove it */
1291 update_input_key_state( input, msg );
1292 list_remove( &msg->entry );
1293 free_message( msg );
1294 continue;
1296 if (win_thread != thread)
1298 if (win_thread->queue->input == input)
1300 /* wake the other thread */
1301 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1302 got_one = 1;
1304 else
1306 /* for another thread input, drop it */
1307 update_input_key_state( input, msg );
1308 list_remove( &msg->entry );
1309 free_message( msg );
1311 release_object( win_thread );
1312 continue;
1314 release_object( win_thread );
1316 /* if we already got a message for another thread, or if it doesn't
1317 * match the filter we skip it */
1318 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1320 clear_bits &= ~get_hardware_msg_bit( msg );
1321 continue;
1323 /* now we can return it */
1324 if (!msg->unique_id) msg->unique_id = get_unique_id();
1325 reply->type = MSG_HARDWARE;
1326 reply->win = win;
1327 reply->msg = msg_code;
1328 reply->wparam = msg->wparam;
1329 reply->lparam = msg->lparam;
1330 reply->x = msg->x;
1331 reply->y = msg->y;
1332 reply->time = msg->time;
1333 reply->info = msg->info;
1334 reply->hw_id = msg->unique_id;
1335 return 1;
1337 /* nothing found, clear the hardware queue bits */
1338 clear_queue_bits( thread->queue, clear_bits );
1339 return 0;
1342 /* increment (or decrement if 'incr' is negative) the queue paint count */
1343 void inc_queue_paint_count( struct thread *thread, int incr )
1345 struct msg_queue *queue = thread->queue;
1347 assert( queue );
1349 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1351 if (queue->paint_count)
1352 set_queue_bits( queue, QS_PAINT );
1353 else
1354 clear_queue_bits( queue, QS_PAINT );
1358 /* remove all messages and timers belonging to a certain window */
1359 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1361 struct msg_queue *queue = thread->queue;
1362 struct list *ptr;
1363 int i;
1365 if (!queue) return;
1367 /* remove timers */
1369 ptr = list_head( &queue->pending_timers );
1370 while (ptr)
1372 struct list *next = list_next( &queue->pending_timers, ptr );
1373 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1374 if (timer->win == win) free_timer( queue, timer );
1375 ptr = next;
1377 ptr = list_head( &queue->expired_timers );
1378 while (ptr)
1380 struct list *next = list_next( &queue->expired_timers, ptr );
1381 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1382 if (timer->win == win) free_timer( queue, timer );
1383 ptr = next;
1386 /* remove messages */
1387 for (i = 0; i < NB_MSG_KINDS; i++)
1389 struct list *ptr, *next;
1391 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1393 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1394 if (msg->win == win) remove_queue_message( queue, msg, i );
1398 thread_input_cleanup_window( queue, win );
1401 /* post a message to a window; used by socket handling */
1402 void post_message( user_handle_t win, unsigned int message,
1403 unsigned int wparam, unsigned int lparam )
1405 struct message *msg;
1406 struct thread *thread = get_window_thread( win );
1408 if (!thread) return;
1410 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1412 msg->type = MSG_POSTED;
1413 msg->win = get_user_full_handle( win );
1414 msg->msg = message;
1415 msg->wparam = wparam;
1416 msg->lparam = lparam;
1417 msg->time = get_tick_count();
1418 msg->x = 0;
1419 msg->y = 0;
1420 msg->info = 0;
1421 msg->hook = 0;
1422 msg->hook_proc = NULL;
1423 msg->result = NULL;
1424 msg->data = NULL;
1425 msg->data_size = 0;
1427 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1428 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1430 release_object( thread );
1433 /* post a win event */
1434 void post_win_event( struct thread *thread, unsigned int event,
1435 user_handle_t win, unsigned int object_id,
1436 unsigned int child_id, void *hook_proc,
1437 const WCHAR *module, size_t module_size,
1438 user_handle_t hook)
1440 struct message *msg;
1442 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1444 msg->type = MSG_WINEVENT;
1445 msg->win = get_user_full_handle( win );
1446 msg->msg = event;
1447 msg->wparam = object_id;
1448 msg->lparam = child_id;
1449 msg->time = get_tick_count();
1450 msg->x = 0;
1451 msg->y = 0;
1452 msg->info = get_thread_id( current );
1453 msg->result = NULL;
1454 msg->hook = hook;
1455 msg->hook_proc = hook_proc;
1457 if ((msg->data = malloc( module_size )))
1459 msg->data_size = module_size;
1460 memcpy( msg->data, module, module_size );
1462 if (debug_level > 1)
1463 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1464 get_thread_id(thread), event, win, object_id, child_id );
1465 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1466 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1468 else
1469 free( msg );
1473 /* get the message queue of the current thread */
1474 DECL_HANDLER(get_msg_queue)
1476 struct msg_queue *queue = get_current_queue();
1478 reply->handle = 0;
1479 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1483 /* set the current message queue wakeup mask */
1484 DECL_HANDLER(set_queue_mask)
1486 struct msg_queue *queue = get_current_queue();
1488 if (queue)
1490 queue->wake_mask = req->wake_mask;
1491 queue->changed_mask = req->changed_mask;
1492 reply->wake_bits = queue->wake_bits;
1493 reply->changed_bits = queue->changed_bits;
1494 if (is_signaled( queue ))
1496 /* if skip wait is set, do what would have been done in the subsequent wait */
1497 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1498 else wake_up( &queue->obj, 0 );
1504 /* get the current message queue status */
1505 DECL_HANDLER(get_queue_status)
1507 struct msg_queue *queue = current->queue;
1508 if (queue)
1510 reply->wake_bits = queue->wake_bits;
1511 reply->changed_bits = queue->changed_bits;
1512 if (req->clear) queue->changed_bits = 0;
1514 else reply->wake_bits = reply->changed_bits = 0;
1518 /* send a message to a thread queue */
1519 DECL_HANDLER(send_message)
1521 struct message *msg;
1522 struct msg_queue *send_queue = get_current_queue();
1523 struct msg_queue *recv_queue = NULL;
1524 struct thread *thread = NULL;
1526 if (req->id)
1528 if (!(thread = get_thread_from_id( req->id ))) return;
1530 else if (req->type != MSG_HARDWARE)
1532 /* only hardware messages are allowed without destination thread */
1533 set_error( STATUS_INVALID_PARAMETER );
1534 return;
1537 if (thread && !(recv_queue = thread->queue))
1539 set_error( STATUS_INVALID_PARAMETER );
1540 release_object( thread );
1541 return;
1543 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1545 set_error( STATUS_TIMEOUT );
1546 release_object( thread );
1547 return;
1550 if ((msg = mem_alloc( sizeof(*msg) )))
1552 msg->type = req->type;
1553 msg->win = get_user_full_handle( req->win );
1554 msg->msg = req->msg;
1555 msg->wparam = req->wparam;
1556 msg->lparam = req->lparam;
1557 msg->time = req->time;
1558 msg->x = req->x;
1559 msg->y = req->y;
1560 msg->info = req->info;
1561 msg->hook = 0;
1562 msg->hook_proc = NULL;
1563 msg->result = NULL;
1564 msg->data = NULL;
1565 msg->data_size = 0;
1567 switch(msg->type)
1569 case MSG_OTHER_PROCESS:
1570 msg->data_size = get_req_data_size();
1571 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1573 free( msg );
1574 break;
1576 /* fall through */
1577 case MSG_ASCII:
1578 case MSG_UNICODE:
1579 case MSG_CALLBACK:
1580 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1581 req->timeout, req->callback, req->info )))
1583 free_message( msg );
1584 break;
1586 /* fall through */
1587 case MSG_NOTIFY:
1588 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1589 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1590 break;
1591 case MSG_POSTED:
1592 /* needed for posted DDE messages */
1593 msg->data_size = get_req_data_size();
1594 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1596 free( msg );
1597 break;
1599 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1600 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1601 break;
1602 case MSG_HARDWARE:
1603 queue_hardware_message( recv_queue, msg );
1604 break;
1605 case MSG_CALLBACK_RESULT: /* cannot send this one */
1606 default:
1607 set_error( STATUS_INVALID_PARAMETER );
1608 free( msg );
1609 break;
1612 if (thread) release_object( thread );
1616 /* get a message from the current queue */
1617 DECL_HANDLER(get_message)
1619 struct timer *timer;
1620 struct list *ptr;
1621 struct msg_queue *queue = get_current_queue();
1622 user_handle_t get_win = get_user_full_handle( req->get_win );
1624 reply->active_hooks = get_active_hooks();
1626 if (!queue) return;
1627 gettimeofday( &queue->last_get_msg, NULL );
1629 /* first check for sent messages */
1630 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1632 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1633 receive_message( queue, msg, reply );
1634 return;
1636 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1638 /* clear changed bits so we can wait on them if we don't find a message */
1639 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits = 0;
1640 else queue->changed_bits &= QS_ALLPOSTMESSAGE;
1642 /* then check for posted messages */
1643 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1644 return;
1646 /* then check for any raw hardware message */
1647 if (filter_contains_hw_range( req->get_first, req->get_last ) &&
1648 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1649 return;
1651 /* now check for WM_PAINT */
1652 if (queue->paint_count &&
1653 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1654 (reply->win = find_window_to_repaint( get_win, current )))
1656 reply->type = MSG_POSTED;
1657 reply->msg = WM_PAINT;
1658 reply->wparam = 0;
1659 reply->lparam = 0;
1660 reply->x = 0;
1661 reply->y = 0;
1662 reply->time = get_tick_count();
1663 reply->info = 0;
1664 return;
1667 /* now check for timer */
1668 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1669 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1671 reply->type = MSG_POSTED;
1672 reply->win = timer->win;
1673 reply->msg = timer->msg;
1674 reply->wparam = timer->id;
1675 reply->lparam = timer->lparam;
1676 reply->x = 0;
1677 reply->y = 0;
1678 reply->time = get_tick_count();
1679 reply->info = 0;
1680 return;
1683 done:
1684 set_error( STATUS_PENDING ); /* FIXME */
1688 /* reply to a sent message */
1689 DECL_HANDLER(reply_message)
1691 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1692 else if (current->queue->recv_result)
1693 reply_message( current->queue, req->result, 0, req->remove,
1694 get_req_data(), get_req_data_size() );
1698 /* accept the current hardware message */
1699 DECL_HANDLER(accept_hardware_message)
1701 if (current->queue)
1702 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1703 else
1704 set_error( STATUS_ACCESS_DENIED );
1708 /* retrieve the reply for the last message sent */
1709 DECL_HANDLER(get_message_reply)
1711 struct message_result *result;
1712 struct list *entry;
1713 struct msg_queue *queue = current->queue;
1715 if (queue)
1717 set_error( STATUS_PENDING );
1718 reply->result = 0;
1720 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1722 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1723 if (result->replied || req->cancel)
1725 if (result->replied)
1727 reply->result = result->result;
1728 set_error( result->error );
1729 if (result->data)
1731 size_t data_len = min( result->data_size, get_reply_max_size() );
1732 set_reply_data_ptr( result->data, data_len );
1733 result->data = NULL;
1734 result->data_size = 0;
1737 remove_result_from_sender( result );
1739 entry = list_head( &queue->send_result );
1740 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1741 else
1743 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1744 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1748 else set_error( STATUS_ACCESS_DENIED );
1752 /* set a window timer */
1753 DECL_HANDLER(set_win_timer)
1755 struct timer *timer;
1756 struct msg_queue *queue;
1757 struct thread *thread = NULL;
1758 user_handle_t win = 0;
1759 unsigned int id = req->id;
1761 if (req->win)
1763 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1765 set_error( STATUS_INVALID_HANDLE );
1766 return;
1768 if (thread->process != current->process)
1770 release_object( thread );
1771 set_error( STATUS_ACCESS_DENIED );
1772 return;
1774 queue = thread->queue;
1775 /* remove it if it existed already */
1776 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1778 else
1780 queue = get_current_queue();
1781 /* find a free id for it */
1784 id = queue->next_timer_id;
1785 if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1787 while (find_timer( queue, 0, req->msg, id ));
1790 if ((timer = set_timer( queue, req->rate )))
1792 timer->win = win;
1793 timer->msg = req->msg;
1794 timer->id = id;
1795 timer->lparam = req->lparam;
1796 reply->id = id;
1798 if (thread) release_object( thread );
1801 /* kill a window timer */
1802 DECL_HANDLER(kill_win_timer)
1804 struct timer *timer;
1805 struct thread *thread;
1806 user_handle_t win = 0;
1808 if (req->win)
1810 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1812 set_error( STATUS_INVALID_HANDLE );
1813 return;
1815 if (thread->process != current->process)
1817 release_object( thread );
1818 set_error( STATUS_ACCESS_DENIED );
1819 return;
1822 else thread = (struct thread *)grab_object( current );
1824 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1825 free_timer( thread->queue, timer );
1826 else
1827 set_error( STATUS_INVALID_PARAMETER );
1829 release_object( thread );
1833 /* attach (or detach) thread inputs */
1834 DECL_HANDLER(attach_thread_input)
1836 struct thread *thread_from = get_thread_from_id( req->tid_from );
1837 struct thread *thread_to = get_thread_from_id( req->tid_to );
1839 if (!thread_from || !thread_to)
1841 if (thread_from) release_object( thread_from );
1842 if (thread_to) release_object( thread_to );
1843 return;
1845 if (thread_from != thread_to)
1847 if (req->attach) attach_thread_input( thread_from, thread_to );
1848 else
1850 if (thread_from->queue && thread_to->queue &&
1851 thread_from->queue->input == thread_to->queue->input)
1852 detach_thread_input( thread_from );
1853 else
1854 set_error( STATUS_ACCESS_DENIED );
1857 else set_error( STATUS_ACCESS_DENIED );
1858 release_object( thread_from );
1859 release_object( thread_to );
1863 /* get thread input data */
1864 DECL_HANDLER(get_thread_input)
1866 struct thread *thread = NULL;
1867 struct thread_input *input;
1869 if (req->tid)
1871 if (!(thread = get_thread_from_id( req->tid ))) return;
1872 input = thread->queue ? thread->queue->input : NULL;
1874 else input = foreground_input; /* get the foreground thread info */
1876 if (input)
1878 reply->focus = input->focus;
1879 reply->capture = input->capture;
1880 reply->active = input->active;
1881 reply->menu_owner = input->menu_owner;
1882 reply->move_size = input->move_size;
1883 reply->caret = input->caret;
1884 reply->rect = input->caret_rect;
1886 else
1888 reply->focus = 0;
1889 reply->capture = 0;
1890 reply->active = 0;
1891 reply->menu_owner = 0;
1892 reply->move_size = 0;
1893 reply->caret = 0;
1894 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1896 /* foreground window is active window of foreground thread */
1897 reply->foreground = foreground_input ? foreground_input->active : 0;
1898 if (thread) release_object( thread );
1902 /* retrieve queue keyboard state for a given thread */
1903 DECL_HANDLER(get_key_state)
1905 struct thread *thread;
1906 struct thread_input *input;
1908 if (!(thread = get_thread_from_id( req->tid ))) return;
1909 input = thread->queue ? thread->queue->input : NULL;
1910 if (input)
1912 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1913 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1915 release_object( thread );
1919 /* set queue keyboard state for a given thread */
1920 DECL_HANDLER(set_key_state)
1922 struct thread *thread = NULL;
1923 struct thread_input *input;
1925 if (!(thread = get_thread_from_id( req->tid ))) return;
1926 input = thread->queue ? thread->queue->input : NULL;
1927 if (input)
1929 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1930 if (size) memcpy( input->keystate, get_req_data(), size );
1932 release_object( thread );
1936 /* set the system foreground window */
1937 DECL_HANDLER(set_foreground_window)
1939 struct msg_queue *queue = get_current_queue();
1941 reply->previous = foreground_input ? foreground_input->active : 0;
1942 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1943 reply->send_msg_new = FALSE;
1945 if (req->handle)
1947 struct thread *thread;
1949 if (is_top_level_window( req->handle ) &&
1950 ((thread = get_window_thread( req->handle ))))
1952 foreground_input = thread->queue->input;
1953 reply->send_msg_new = (foreground_input != queue->input);
1954 release_object( thread );
1956 else set_error( STATUS_INVALID_HANDLE );
1958 else foreground_input = NULL;
1962 /* set the current thread focus window */
1963 DECL_HANDLER(set_focus_window)
1965 struct msg_queue *queue = get_current_queue();
1967 reply->previous = 0;
1968 if (queue && check_queue_input_window( queue, req->handle ))
1970 reply->previous = queue->input->focus;
1971 queue->input->focus = get_user_full_handle( req->handle );
1976 /* set the current thread active window */
1977 DECL_HANDLER(set_active_window)
1979 struct msg_queue *queue = get_current_queue();
1981 reply->previous = 0;
1982 if (queue && check_queue_input_window( queue, req->handle ))
1984 if (!req->handle || make_window_active( req->handle ))
1986 reply->previous = queue->input->active;
1987 queue->input->active = get_user_full_handle( req->handle );
1989 else set_error( STATUS_INVALID_HANDLE );
1994 /* set the current thread capture window */
1995 DECL_HANDLER(set_capture_window)
1997 struct msg_queue *queue = get_current_queue();
1999 reply->previous = reply->full_handle = 0;
2000 if (queue && check_queue_input_window( queue, req->handle ))
2002 struct thread_input *input = queue->input;
2004 reply->previous = input->capture;
2005 input->capture = get_user_full_handle( req->handle );
2006 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2007 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2008 reply->full_handle = input->capture;
2013 /* Set the current thread caret window */
2014 DECL_HANDLER(set_caret_window)
2016 struct msg_queue *queue = get_current_queue();
2018 reply->previous = 0;
2019 if (queue && check_queue_input_window( queue, req->handle ))
2021 struct thread_input *input = queue->input;
2023 reply->previous = input->caret;
2024 reply->old_rect = input->caret_rect;
2025 reply->old_hide = input->caret_hide;
2026 reply->old_state = input->caret_state;
2028 set_caret_window( input, get_user_full_handle(req->handle) );
2029 input->caret_rect.right = input->caret_rect.left + req->width;
2030 input->caret_rect.bottom = input->caret_rect.top + req->height;
2035 /* Set the current thread caret information */
2036 DECL_HANDLER(set_caret_info)
2038 struct msg_queue *queue = get_current_queue();
2039 struct thread_input *input;
2041 if (!queue) return;
2042 input = queue->input;
2043 reply->full_handle = input->caret;
2044 reply->old_rect = input->caret_rect;
2045 reply->old_hide = input->caret_hide;
2046 reply->old_state = input->caret_state;
2048 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2050 set_error( STATUS_ACCESS_DENIED );
2051 return;
2053 if (req->flags & SET_CARET_POS)
2055 input->caret_rect.right += req->x - input->caret_rect.left;
2056 input->caret_rect.bottom += req->y - input->caret_rect.top;
2057 input->caret_rect.left = req->x;
2058 input->caret_rect.top = req->y;
2060 if (req->flags & SET_CARET_HIDE)
2062 input->caret_hide += req->hide;
2063 if (input->caret_hide < 0) input->caret_hide = 0;
2065 if (req->flags & SET_CARET_STATE)
2067 if (req->state == -1) input->caret_state = !input->caret_state;
2068 else input->caret_state = !!req->state;
2073 /* get the time of the last input event */
2074 DECL_HANDLER(get_last_input_time)
2076 reply->time = last_input_time;