localspl/tests: Add tests for XcvDataPort_DeletePort.
[wine/wine64.git] / server / queue.c
blob7efdd78f03f1cfb1f95bbba8ec58d88637d02891
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 long wparam; /* parameters */
74 unsigned long lparam; /* parameters */
75 unsigned long info; /* extra info */
76 int x; /* x position */
77 int y; /* y position */
78 unsigned int time; /* message time */
79 void *data; /* message data for sent messages */
80 unsigned int data_size; /* size of message data */
81 unsigned int unique_id; /* unique id for nested hw message waits */
82 struct message_result *result; /* result in sender queue */
85 struct timer
87 struct list entry; /* entry in timer list */
88 struct timeval when; /* next expiration */
89 unsigned int rate; /* timer rate in ms */
90 user_handle_t win; /* window handle */
91 unsigned int msg; /* message to post */
92 unsigned long id; /* timer id */
93 unsigned long lparam; /* lparam for message */
96 struct thread_input
98 struct object obj; /* object header */
99 struct desktop *desktop; /* desktop that this thread input belongs to */
100 user_handle_t focus; /* focus window */
101 user_handle_t capture; /* capture window */
102 user_handle_t active; /* active window */
103 user_handle_t menu_owner; /* current menu owner window */
104 user_handle_t move_size; /* current moving/resizing window */
105 user_handle_t caret; /* caret window */
106 rectangle_t caret_rect; /* caret rectangle */
107 int caret_hide; /* caret hide count */
108 int caret_state; /* caret on/off state */
109 struct list msg_list; /* list of hardware messages */
110 unsigned char keystate[256]; /* state of each key */
113 struct msg_queue
115 struct object obj; /* object header */
116 unsigned int wake_bits; /* wakeup bits */
117 unsigned int wake_mask; /* wakeup mask */
118 unsigned int changed_bits; /* changed wakeup bits */
119 unsigned int changed_mask; /* changed wakeup mask */
120 int paint_count; /* pending paint messages count */
121 int quit_message; /* is there a pending quit message? */
122 int exit_code; /* exit code of pending quit message */
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 long 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_map_access, /* map_access */
157 no_lookup_name, /* lookup_name */
158 no_close_handle, /* close_handle */
159 msg_queue_destroy /* destroy */
163 static const struct object_ops thread_input_ops =
165 sizeof(struct thread_input), /* size */
166 thread_input_dump, /* dump */
167 no_add_queue, /* add_queue */
168 NULL, /* remove_queue */
169 NULL, /* signaled */
170 NULL, /* satisfied */
171 no_signal, /* signal */
172 no_get_fd, /* get_fd */
173 no_map_access, /* map_access */
174 no_lookup_name, /* lookup_name */
175 no_close_handle, /* close_handle */
176 thread_input_destroy /* destroy */
179 /* pointer to input structure of foreground thread */
180 static struct thread_input *foreground_input;
181 static unsigned int last_input_time;
183 static void free_message( struct message *msg );
185 /* set the caret window in a given thread input */
186 static void set_caret_window( struct thread_input *input, user_handle_t win )
188 if (!win || win != input->caret)
190 input->caret_rect.left = 0;
191 input->caret_rect.top = 0;
192 input->caret_rect.right = 0;
193 input->caret_rect.bottom = 0;
195 input->caret = win;
196 input->caret_hide = 1;
197 input->caret_state = 0;
200 /* create a thread input object */
201 static struct thread_input *create_thread_input( struct thread *thread )
203 struct thread_input *input;
205 if ((input = alloc_object( &thread_input_ops )))
207 input->focus = 0;
208 input->capture = 0;
209 input->active = 0;
210 input->menu_owner = 0;
211 input->move_size = 0;
212 list_init( &input->msg_list );
213 set_caret_window( input, 0 );
214 memset( input->keystate, 0, sizeof(input->keystate) );
216 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
218 release_object( input );
219 return NULL;
222 return input;
225 /* release the thread input data of a given thread */
226 static inline void release_thread_input( struct thread *thread )
228 struct thread_input *input = thread->queue->input;
230 if (!input) return;
231 release_object( input );
232 thread->queue->input = NULL;
235 /* create a message queue object */
236 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
238 struct msg_queue *queue;
239 int i;
241 if (!input && !(input = create_thread_input( thread ))) return NULL;
242 if ((queue = alloc_object( &msg_queue_ops )))
244 queue->wake_bits = 0;
245 queue->wake_mask = 0;
246 queue->changed_bits = 0;
247 queue->changed_mask = 0;
248 queue->paint_count = 0;
249 queue->quit_message = 0;
250 queue->recv_result = NULL;
251 queue->next_timer_id = 1;
252 queue->timeout = NULL;
253 queue->input = (struct thread_input *)grab_object( input );
254 queue->hooks = NULL;
255 queue->last_get_msg = current_time;
256 list_init( &queue->send_result );
257 list_init( &queue->callback_result );
258 list_init( &queue->pending_timers );
259 list_init( &queue->expired_timers );
260 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
262 thread->queue = queue;
263 if (!thread->process->queue)
264 thread->process->queue = (struct msg_queue *)grab_object( queue );
266 release_object( input );
267 return queue;
270 /* free the message queue of a thread at thread exit */
271 void free_msg_queue( struct thread *thread )
273 struct process *process = thread->process;
275 remove_thread_hooks( thread );
276 if (!thread->queue) return;
277 if (process->queue == thread->queue) /* is it the process main queue? */
279 release_object( process->queue );
280 process->queue = NULL;
281 if (process->idle_event)
283 set_event( process->idle_event );
284 release_object( process->idle_event );
285 process->idle_event = NULL;
288 release_object( thread->queue );
289 thread->queue = NULL;
292 /* get the hook table for a given thread */
293 struct hook_table *get_queue_hooks( struct thread *thread )
295 if (!thread->queue) return NULL;
296 return thread->queue->hooks;
299 /* set the hook table for a given thread, allocating the queue if needed */
300 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
302 struct msg_queue *queue = thread->queue;
303 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
304 if (queue->hooks) release_object( queue->hooks );
305 queue->hooks = hooks;
308 /* check the queue status */
309 inline static int is_signaled( struct msg_queue *queue )
311 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
314 /* set some queue bits */
315 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
317 queue->wake_bits |= bits;
318 queue->changed_bits |= bits;
319 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
322 /* clear some queue bits */
323 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
325 queue->wake_bits &= ~bits;
326 queue->changed_bits &= ~bits;
329 /* check whether msg is a keyboard message */
330 inline static int is_keyboard_msg( struct message *msg )
332 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
335 /* check if message is matched by the filter */
336 inline static int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
338 return (msg >= first && msg <= last);
341 /* check whether a message filter contains at least one potential hardware message */
342 inline static int filter_contains_hw_range( unsigned int first, unsigned int last )
344 /* hardware message ranges are (in numerical order):
345 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
346 * WM_KEYFIRST .. WM_KEYLAST
347 * WM_MOUSEFIRST .. WM_MOUSELAST
349 if (last < WM_NCMOUSEFIRST) return 0;
350 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
351 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
352 if (first > WM_MOUSELAST) return 0;
353 return 1;
356 /* get the QS_* bit corresponding to a given hardware message */
357 inline static int get_hardware_msg_bit( struct message *msg )
359 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
360 if (is_keyboard_msg( msg )) return QS_KEY;
361 return QS_MOUSEBUTTON;
364 /* get the current thread queue, creating it if needed */
365 inline static struct msg_queue *get_current_queue(void)
367 struct msg_queue *queue = current->queue;
368 if (!queue) queue = create_msg_queue( current, NULL );
369 return queue;
372 /* get a (pseudo-)unique id to tag hardware messages */
373 inline static unsigned int get_unique_id(void)
375 static unsigned int id;
376 if (!++id) id = 1; /* avoid an id of 0 */
377 return id;
380 /* try to merge a message with the last in the list; return 1 if successful */
381 static int merge_message( struct thread_input *input, const struct message *msg )
383 struct message *prev;
384 struct list *ptr = list_tail( &input->msg_list );
386 if (!ptr) return 0;
387 prev = LIST_ENTRY( ptr, struct message, entry );
388 if (prev->unique_id) return 0;
389 if (prev->result) return 0;
390 if (prev->win != msg->win) return 0;
391 if (prev->msg != msg->msg) return 0;
392 if (prev->type != msg->type) return 0;
393 /* now we can merge it */
394 prev->wparam = msg->wparam;
395 prev->lparam = msg->lparam;
396 prev->x = msg->x;
397 prev->y = msg->y;
398 prev->time = msg->time;
399 prev->info = msg->info;
400 return 1;
403 /* free a result structure */
404 static void free_result( struct message_result *result )
406 if (result->timeout) remove_timeout_user( result->timeout );
407 free( result->data );
408 if (result->callback_msg) free_message( result->callback_msg );
409 free( result );
412 /* remove the result from the sender list it is on */
413 static inline void remove_result_from_sender( struct message_result *result )
415 assert( result->sender );
417 list_remove( &result->sender_entry );
418 result->sender = NULL;
419 if (!result->receiver) free_result( result );
422 /* store the message result in the appropriate structure */
423 static void store_message_result( struct message_result *res, unsigned int result,
424 unsigned int error )
426 res->result = result;
427 res->error = error;
428 res->replied = 1;
429 if (res->timeout)
431 remove_timeout_user( res->timeout );
432 res->timeout = NULL;
434 if (res->sender)
436 if (res->callback_msg)
438 /* queue the callback message in the sender queue */
439 struct callback_msg_data *data = res->callback_msg->data;
440 data->result = result;
441 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
442 set_queue_bits( res->sender, QS_SENDMESSAGE );
443 res->callback_msg = NULL;
444 remove_result_from_sender( res );
446 else
448 /* wake sender queue if waiting on this result */
449 if (list_head(&res->sender->send_result) == &res->sender_entry)
450 set_queue_bits( res->sender, QS_SMRESULT );
456 /* free a message when deleting a queue or window */
457 static void free_message( struct message *msg )
459 struct message_result *result = msg->result;
460 if (result)
462 result->msg = NULL;
463 if (result->sender)
465 result->receiver = NULL;
466 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
468 else free_result( result );
470 free( msg->data );
471 free( msg );
474 /* remove (and free) a message from a message list */
475 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
476 enum message_kind kind )
478 list_remove( &msg->entry );
479 switch(kind)
481 case SEND_MESSAGE:
482 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
483 break;
484 case POST_MESSAGE:
485 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
486 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
487 break;
489 free_message( msg );
492 /* message timed out without getting a reply */
493 static void result_timeout( void *private )
495 struct message_result *result = private;
497 assert( !result->replied );
499 result->timeout = NULL;
501 if (result->msg) /* not received yet */
503 struct message *msg = result->msg;
505 result->msg = NULL;
506 msg->result = NULL;
507 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
508 result->receiver = NULL;
509 if (!result->sender)
511 free_result( result );
512 return;
516 store_message_result( result, 0, STATUS_TIMEOUT );
519 /* allocate and fill a message result structure */
520 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
521 struct msg_queue *recv_queue,
522 struct message *msg, int timeout )
524 struct message_result *result = mem_alloc( sizeof(*result) );
525 if (result)
527 result->msg = msg;
528 result->sender = send_queue;
529 result->receiver = recv_queue;
530 result->replied = 0;
531 result->data = NULL;
532 result->data_size = 0;
533 result->timeout = NULL;
535 if (msg->type == MSG_CALLBACK)
537 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
539 if (!callback_msg)
541 free( result );
542 return NULL;
544 callback_msg->type = MSG_CALLBACK_RESULT;
545 callback_msg->win = msg->win;
546 callback_msg->msg = msg->msg;
547 callback_msg->wparam = 0;
548 callback_msg->lparam = 0;
549 callback_msg->time = get_tick_count();
550 callback_msg->x = 0;
551 callback_msg->y = 0;
552 callback_msg->info = 0;
553 callback_msg->result = NULL;
554 /* steal the data from the original message */
555 callback_msg->data = msg->data;
556 callback_msg->data_size = msg->data_size;
557 msg->data = NULL;
558 msg->data_size = 0;
560 result->callback_msg = callback_msg;
561 list_add_head( &send_queue->callback_result, &result->sender_entry );
563 else
565 result->callback_msg = NULL;
566 list_add_head( &send_queue->send_result, &result->sender_entry );
569 if (timeout)
571 struct timeval when = current_time;
572 add_timeout( &when, timeout );
573 result->timeout = add_timeout_user( &when, result_timeout, result );
576 return result;
579 /* receive a message, removing it from the sent queue */
580 static void receive_message( struct msg_queue *queue, struct message *msg,
581 struct get_message_reply *reply )
583 struct message_result *result = msg->result;
585 reply->total = msg->data_size;
586 if (msg->data_size > get_reply_max_size())
588 set_error( STATUS_BUFFER_OVERFLOW );
589 return;
591 reply->type = msg->type;
592 reply->win = msg->win;
593 reply->msg = msg->msg;
594 reply->wparam = msg->wparam;
595 reply->lparam = msg->lparam;
596 reply->x = msg->x;
597 reply->y = msg->y;
598 reply->time = msg->time;
599 reply->info = msg->info;
601 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
603 list_remove( &msg->entry );
604 /* put the result on the receiver result stack */
605 if (result)
607 result->msg = NULL;
608 result->recv_next = queue->recv_result;
609 queue->recv_result = result;
611 free( msg );
612 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
615 /* set the result of the current received message */
616 static void reply_message( struct msg_queue *queue, unsigned int result,
617 unsigned int error, int remove, const void *data, data_size_t len )
619 struct message_result *res = queue->recv_result;
621 if (remove)
623 queue->recv_result = res->recv_next;
624 res->receiver = NULL;
625 if (!res->sender) /* no one waiting for it */
627 free_result( res );
628 return;
631 if (!res->replied)
633 if (len && (res->data = memdup( data, len ))) res->data_size = len;
634 store_message_result( res, result, error );
638 /* retrieve a posted message */
639 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
640 unsigned int first, unsigned int last, unsigned int flags,
641 struct get_message_reply *reply )
643 struct message *msg;
645 /* check against the filters */
646 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
648 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
649 if (!check_msg_filter( msg->msg, first, last )) continue;
650 goto found; /* found one */
652 return 0;
654 /* return it to the app */
655 found:
656 reply->total = msg->data_size;
657 if (msg->data_size > get_reply_max_size())
659 set_error( STATUS_BUFFER_OVERFLOW );
660 return 1;
662 reply->type = msg->type;
663 reply->win = msg->win;
664 reply->msg = msg->msg;
665 reply->wparam = msg->wparam;
666 reply->lparam = msg->lparam;
667 reply->x = msg->x;
668 reply->y = msg->y;
669 reply->time = msg->time;
670 reply->info = msg->info;
672 if (flags & PM_REMOVE)
674 if (msg->data)
676 set_reply_data_ptr( msg->data, msg->data_size );
677 msg->data = NULL;
678 msg->data_size = 0;
680 remove_queue_message( queue, msg, POST_MESSAGE );
682 else if (msg->data) set_reply_data( msg->data, msg->data_size );
684 return 1;
687 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
688 struct get_message_reply *reply )
690 if (queue->quit_message)
692 reply->total = 0;
693 reply->type = MSG_POSTED;
694 reply->win = NULL;
695 reply->msg = WM_QUIT;
696 reply->wparam = queue->exit_code;
697 reply->lparam = 0;
698 reply->x = 0;
699 reply->y = 0;
700 reply->time = get_tick_count();
701 reply->info = 0;
703 if (flags & PM_REMOVE)
705 queue->quit_message = 0;
706 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
707 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
709 return 1;
711 else
712 return 0;
715 /* empty a message list and free all the messages */
716 static void empty_msg_list( struct list *list )
718 struct list *ptr;
720 while ((ptr = list_head( list )) != NULL)
722 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
723 list_remove( &msg->entry );
724 free_message( msg );
728 /* cleanup all pending results when deleting a queue */
729 static void cleanup_results( struct msg_queue *queue )
731 struct list *entry;
733 while ((entry = list_head( &queue->send_result )) != NULL)
735 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
738 while ((entry = list_head( &queue->callback_result )) != NULL)
740 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
743 while (queue->recv_result)
744 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
747 /* check if the thread owning the queue is hung (not checking for messages) */
748 static int is_queue_hung( struct msg_queue *queue )
750 struct wait_queue_entry *entry;
752 if (current_time.tv_sec - queue->last_get_msg.tv_sec <= 5)
753 return 0; /* less than 5 seconds since last get message -> not hung */
755 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
757 if (entry->thread->queue == queue)
758 return 0; /* thread is waiting on queue -> not hung */
760 return 1;
763 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
765 struct msg_queue *queue = (struct msg_queue *)obj;
766 struct process *process = entry->thread->process;
768 /* a thread can only wait on its own queue */
769 if (entry->thread->queue != queue)
771 set_error( STATUS_ACCESS_DENIED );
772 return 0;
774 /* if waiting on the main process queue, set the idle event */
775 if (process->queue == queue)
777 if (process->idle_event) set_event( process->idle_event );
779 add_queue( obj, entry );
780 return 1;
783 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
785 struct msg_queue *queue = (struct msg_queue *)obj;
786 struct process *process = entry->thread->process;
788 remove_queue( obj, entry );
790 assert( entry->thread->queue == queue );
792 /* if waiting on the main process queue, reset the idle event */
793 if (process->queue == queue)
795 if (process->idle_event) reset_event( process->idle_event );
799 static void msg_queue_dump( struct object *obj, int verbose )
801 struct msg_queue *queue = (struct msg_queue *)obj;
802 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
803 queue->wake_bits, queue->wake_mask );
806 static int msg_queue_signaled( struct object *obj, struct thread *thread )
808 struct msg_queue *queue = (struct msg_queue *)obj;
809 return is_signaled( queue );
812 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
814 struct msg_queue *queue = (struct msg_queue *)obj;
815 queue->wake_mask = 0;
816 queue->changed_mask = 0;
817 return 0; /* Not abandoned */
820 static void msg_queue_destroy( struct object *obj )
822 struct msg_queue *queue = (struct msg_queue *)obj;
823 struct list *ptr;
824 int i;
826 cleanup_results( queue );
827 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
829 while ((ptr = list_head( &queue->pending_timers )))
831 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
832 list_remove( &timer->entry );
833 free( timer );
835 while ((ptr = list_head( &queue->expired_timers )))
837 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
838 list_remove( &timer->entry );
839 free( timer );
841 if (queue->timeout) remove_timeout_user( queue->timeout );
842 if (queue->input) release_object( queue->input );
843 if (queue->hooks) release_object( queue->hooks );
846 static void thread_input_dump( struct object *obj, int verbose )
848 struct thread_input *input = (struct thread_input *)obj;
849 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
850 input->focus, input->capture, input->active );
853 static void thread_input_destroy( struct object *obj )
855 struct thread_input *input = (struct thread_input *)obj;
857 if (foreground_input == input) foreground_input = NULL;
858 empty_msg_list( &input->msg_list );
859 if (input->desktop) release_object( input->desktop );
862 /* fix the thread input data when a window is destroyed */
863 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
865 struct thread_input *input = queue->input;
867 if (window == input->focus) input->focus = 0;
868 if (window == input->capture) input->capture = 0;
869 if (window == input->active) input->active = 0;
870 if (window == input->menu_owner) input->menu_owner = 0;
871 if (window == input->move_size) input->move_size = 0;
872 if (window == input->caret) set_caret_window( input, 0 );
875 /* check if the specified window can be set in the input data of a given queue */
876 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
878 struct thread *thread;
879 int ret = 0;
881 if (!window) return 1; /* we can always clear the data */
883 if ((thread = get_window_thread( window )))
885 ret = (queue->input == thread->queue->input);
886 if (!ret) set_error( STATUS_ACCESS_DENIED );
887 release_object( thread );
889 else set_error( STATUS_INVALID_HANDLE );
891 return ret;
894 /* make sure the specified thread has a queue */
895 int init_thread_queue( struct thread *thread )
897 if (thread->queue) return 1;
898 return (create_msg_queue( thread, NULL ) != NULL);
901 /* attach two thread input data structures */
902 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
904 struct desktop *desktop;
905 struct thread_input *input;
907 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
908 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
909 input = (struct thread_input *)grab_object( thread_to->queue->input );
910 if (input->desktop != desktop)
912 set_error( STATUS_ACCESS_DENIED );
913 release_object( input );
914 release_object( desktop );
915 return 0;
917 release_object( desktop );
919 if (thread_from->queue)
921 release_thread_input( thread_from );
922 thread_from->queue->input = input;
924 else
926 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
928 memset( input->keystate, 0, sizeof(input->keystate) );
929 return 1;
932 /* detach two thread input data structures */
933 void detach_thread_input( struct thread *thread_from )
935 struct thread_input *input;
937 if ((input = create_thread_input( thread_from )))
939 release_thread_input( thread_from );
940 thread_from->queue->input = input;
945 /* set the next timer to expire */
946 static void set_next_timer( struct msg_queue *queue )
948 struct list *ptr;
950 if (queue->timeout)
952 remove_timeout_user( queue->timeout );
953 queue->timeout = NULL;
955 if ((ptr = list_head( &queue->pending_timers )))
957 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
958 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
960 /* set/clear QS_TIMER bit */
961 if (list_empty( &queue->expired_timers ))
962 clear_queue_bits( queue, QS_TIMER );
963 else
964 set_queue_bits( queue, QS_TIMER );
967 /* find a timer from its window and id */
968 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
969 unsigned int msg, unsigned long id )
971 struct list *ptr;
973 /* we need to search both lists */
975 LIST_FOR_EACH( ptr, &queue->pending_timers )
977 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
978 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
980 LIST_FOR_EACH( ptr, &queue->expired_timers )
982 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
983 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
985 return NULL;
988 /* callback for the next timer expiration */
989 static void timer_callback( void *private )
991 struct msg_queue *queue = private;
992 struct list *ptr;
994 queue->timeout = NULL;
995 /* move on to the next timer */
996 ptr = list_head( &queue->pending_timers );
997 list_remove( ptr );
998 list_add_tail( &queue->expired_timers, ptr );
999 set_next_timer( queue );
1002 /* link a timer at its rightful place in the queue list */
1003 static void link_timer( struct msg_queue *queue, struct timer *timer )
1005 struct list *ptr;
1007 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1009 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1010 if (!time_before( &t->when, &timer->when )) break;
1012 list_add_before( ptr, &timer->entry );
1015 /* remove a timer from the queue timer list and free it */
1016 static void free_timer( struct msg_queue *queue, struct timer *timer )
1018 list_remove( &timer->entry );
1019 free( timer );
1020 set_next_timer( queue );
1023 /* restart an expired timer */
1024 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1026 list_remove( &timer->entry );
1027 while (!time_before( &current_time, &timer->when )) add_timeout( &timer->when, timer->rate );
1028 link_timer( queue, timer );
1029 set_next_timer( queue );
1032 /* find an expired timer matching the filtering parameters */
1033 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1034 unsigned int get_first, unsigned int get_last,
1035 int remove )
1037 struct list *ptr;
1039 LIST_FOR_EACH( ptr, &queue->expired_timers )
1041 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1042 if (win && timer->win != win) continue;
1043 if (check_msg_filter( timer->msg, get_first, get_last ))
1045 if (remove) restart_timer( queue, timer );
1046 return timer;
1049 return NULL;
1052 /* add a timer */
1053 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1055 struct timer *timer = mem_alloc( sizeof(*timer) );
1056 if (timer)
1058 timer->rate = max( rate, 1 );
1059 timer->when = current_time;
1060 add_timeout( &timer->when, rate );
1061 link_timer( queue, timer );
1062 /* check if we replaced the next timer */
1063 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1065 return timer;
1068 /* change the input key state for a given key */
1069 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1071 if (down)
1073 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1074 input->keystate[key] |= 0x80;
1076 else input->keystate[key] &= ~0x80;
1079 /* update the input key state for a keyboard message */
1080 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1082 unsigned char key;
1083 int down = 0, extended;
1085 switch (msg->msg)
1087 case WM_LBUTTONDOWN:
1088 down = 1;
1089 /* fall through */
1090 case WM_LBUTTONUP:
1091 set_input_key_state( input, VK_LBUTTON, down );
1092 break;
1093 case WM_MBUTTONDOWN:
1094 down = 1;
1095 /* fall through */
1096 case WM_MBUTTONUP:
1097 set_input_key_state( input, VK_MBUTTON, down );
1098 break;
1099 case WM_RBUTTONDOWN:
1100 down = 1;
1101 /* fall through */
1102 case WM_RBUTTONUP:
1103 set_input_key_state( input, VK_RBUTTON, down );
1104 break;
1105 case WM_XBUTTONDOWN:
1106 down = 1;
1107 /* fall through */
1108 case WM_XBUTTONUP:
1109 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1110 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1111 break;
1112 case WM_KEYDOWN:
1113 case WM_SYSKEYDOWN:
1114 down = 1;
1115 /* fall through */
1116 case WM_KEYUP:
1117 case WM_SYSKEYUP:
1118 key = (unsigned char)msg->wparam;
1119 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1120 set_input_key_state( input, key, down );
1121 switch(key)
1123 case VK_SHIFT:
1124 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1125 break;
1126 case VK_CONTROL:
1127 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1128 break;
1129 case VK_MENU:
1130 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1131 break;
1132 case VK_LCONTROL:
1133 case VK_RCONTROL:
1134 set_input_key_state( input, VK_CONTROL, down );
1135 break;
1136 case VK_LMENU:
1137 case VK_RMENU:
1138 set_input_key_state( input, VK_MENU, down );
1139 break;
1140 case VK_LSHIFT:
1141 case VK_RSHIFT:
1142 set_input_key_state( input, VK_SHIFT, down );
1143 break;
1145 break;
1149 /* release the hardware message currently being processed by the given thread */
1150 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1151 int remove, user_handle_t new_win )
1153 struct thread_input *input = queue->input;
1154 struct message *msg;
1156 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1158 if (msg->unique_id == hw_id) break;
1160 if (&msg->entry == &input->msg_list) return; /* not found */
1162 /* clear the queue bit for that message */
1163 if (remove || new_win)
1165 struct message *other;
1166 int clr_bit;
1168 clr_bit = get_hardware_msg_bit( msg );
1169 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1171 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1173 clr_bit = 0;
1174 break;
1177 if (clr_bit) clear_queue_bits( queue, clr_bit );
1180 if (new_win) /* set the new window */
1182 struct thread *owner = get_window_thread( new_win );
1183 if (owner)
1185 if (owner->queue->input == input)
1187 msg->win = new_win;
1188 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1189 remove = 0;
1191 release_object( owner );
1194 if (remove)
1196 update_input_key_state( input, msg );
1197 list_remove( &msg->entry );
1198 free_message( msg );
1202 /* find the window that should receive a given hardware message */
1203 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1204 unsigned int *msg_code )
1206 user_handle_t win = 0;
1208 *msg_code = msg->msg;
1209 if (is_keyboard_msg( msg ))
1211 if (input && !(win = input->focus))
1213 win = input->active;
1214 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1217 else /* mouse message */
1219 if (!input || !(win = input->capture))
1221 if (!(win = msg->win) || !is_window_visible( win ))
1223 if (input) win = window_from_point( input->desktop, msg->x, msg->y );
1227 return win;
1230 /* queue a hardware message into a given thread input */
1231 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1233 user_handle_t win;
1234 struct thread *thread;
1235 struct thread_input *input = queue ? queue->input : foreground_input;
1236 unsigned int msg_code;
1238 last_input_time = get_tick_count();
1239 win = find_hardware_message_window( input, msg, &msg_code );
1240 if (!win || !(thread = get_window_thread(win)))
1242 if (input) update_input_key_state( input, msg );
1243 free( msg );
1244 return;
1246 input = thread->queue->input;
1248 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1249 else
1251 msg->unique_id = 0; /* will be set once we return it to the app */
1252 list_add_tail( &input->msg_list, &msg->entry );
1253 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1255 release_object( thread );
1258 /* check message filter for a hardware message */
1259 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1260 user_handle_t filter_win, unsigned int first, unsigned int last )
1262 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1264 /* we can only test the window for a keyboard message since the
1265 * dest window for a mouse message depends on hittest */
1266 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1267 return 0;
1268 /* the message code is final for a keyboard message, we can simply check it */
1269 return check_msg_filter( msg_code, first, last );
1271 else /* mouse message */
1273 /* we need to check all possible values that the message can have in the end */
1275 if (check_msg_filter( msg_code, first, last )) return 1;
1276 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1278 /* all other messages can become non-client messages */
1279 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1281 /* clicks can become double-clicks or non-client double-clicks */
1282 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1283 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1285 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1286 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1288 return 0;
1293 /* find a hardware message for the given queue */
1294 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1295 unsigned int first, unsigned int last, struct get_message_reply *reply )
1297 struct thread_input *input = thread->queue->input;
1298 struct thread *win_thread;
1299 struct list *ptr;
1300 user_handle_t win;
1301 int clear_bits, got_one = 0;
1302 unsigned int msg_code;
1304 ptr = list_head( &input->msg_list );
1305 if (hw_id)
1307 while (ptr)
1309 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1310 if (msg->unique_id == hw_id) break;
1311 ptr = list_next( &input->msg_list, ptr );
1313 if (!ptr) ptr = list_head( &input->msg_list );
1314 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1317 if (ptr == list_head( &input->msg_list ))
1318 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1319 else
1320 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1322 while (ptr)
1324 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1325 ptr = list_next( &input->msg_list, ptr );
1326 win = find_hardware_message_window( input, msg, &msg_code );
1327 if (!win || !(win_thread = get_window_thread( win )))
1329 /* no window at all, remove it */
1330 update_input_key_state( input, msg );
1331 list_remove( &msg->entry );
1332 free_message( msg );
1333 continue;
1335 if (win_thread != thread)
1337 if (win_thread->queue->input == input)
1339 /* wake the other thread */
1340 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1341 got_one = 1;
1343 else
1345 /* for another thread input, drop it */
1346 update_input_key_state( input, msg );
1347 list_remove( &msg->entry );
1348 free_message( msg );
1350 release_object( win_thread );
1351 continue;
1353 release_object( win_thread );
1355 /* if we already got a message for another thread, or if it doesn't
1356 * match the filter we skip it */
1357 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1359 clear_bits &= ~get_hardware_msg_bit( msg );
1360 continue;
1362 /* now we can return it */
1363 if (!msg->unique_id) msg->unique_id = get_unique_id();
1364 reply->type = MSG_HARDWARE;
1365 reply->win = win;
1366 reply->msg = msg_code;
1367 reply->wparam = msg->wparam;
1368 reply->lparam = msg->lparam;
1369 reply->x = msg->x;
1370 reply->y = msg->y;
1371 reply->time = msg->time;
1372 reply->info = msg->info;
1373 reply->hw_id = msg->unique_id;
1374 return 1;
1376 /* nothing found, clear the hardware queue bits */
1377 clear_queue_bits( thread->queue, clear_bits );
1378 return 0;
1381 /* increment (or decrement if 'incr' is negative) the queue paint count */
1382 void inc_queue_paint_count( struct thread *thread, int incr )
1384 struct msg_queue *queue = thread->queue;
1386 assert( queue );
1388 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1390 if (queue->paint_count)
1391 set_queue_bits( queue, QS_PAINT );
1392 else
1393 clear_queue_bits( queue, QS_PAINT );
1397 /* remove all messages and timers belonging to a certain window */
1398 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1400 struct msg_queue *queue = thread->queue;
1401 struct list *ptr;
1402 int i;
1404 if (!queue) return;
1406 /* remove timers */
1408 ptr = list_head( &queue->pending_timers );
1409 while (ptr)
1411 struct list *next = list_next( &queue->pending_timers, ptr );
1412 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1413 if (timer->win == win) free_timer( queue, timer );
1414 ptr = next;
1416 ptr = list_head( &queue->expired_timers );
1417 while (ptr)
1419 struct list *next = list_next( &queue->expired_timers, ptr );
1420 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1421 if (timer->win == win) free_timer( queue, timer );
1422 ptr = next;
1425 /* remove messages */
1426 for (i = 0; i < NB_MSG_KINDS; i++)
1428 struct list *ptr, *next;
1430 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1432 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1433 if (msg->win == win) remove_queue_message( queue, msg, i );
1437 thread_input_cleanup_window( queue, win );
1440 /* post a message to a window; used by socket handling */
1441 void post_message( user_handle_t win, unsigned int message,
1442 unsigned long wparam, unsigned long lparam )
1444 struct message *msg;
1445 struct thread *thread = get_window_thread( win );
1447 if (!thread) return;
1449 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1451 msg->type = MSG_POSTED;
1452 msg->win = get_user_full_handle( win );
1453 msg->msg = message;
1454 msg->wparam = wparam;
1455 msg->lparam = lparam;
1456 msg->time = get_tick_count();
1457 msg->x = 0;
1458 msg->y = 0;
1459 msg->info = 0;
1460 msg->result = NULL;
1461 msg->data = NULL;
1462 msg->data_size = 0;
1464 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1465 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1467 release_object( thread );
1470 /* post a win event */
1471 void post_win_event( struct thread *thread, unsigned int event,
1472 user_handle_t win, unsigned int object_id,
1473 unsigned int child_id, void *hook_proc,
1474 const WCHAR *module, data_size_t module_size,
1475 user_handle_t hook)
1477 struct message *msg;
1479 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1481 struct winevent_msg_data *data;
1483 msg->type = MSG_WINEVENT;
1484 msg->win = get_user_full_handle( win );
1485 msg->msg = event;
1486 msg->wparam = object_id;
1487 msg->lparam = child_id;
1488 msg->time = get_tick_count();
1489 msg->x = 0;
1490 msg->y = 0;
1491 msg->info = 0;
1492 msg->result = NULL;
1494 if ((data = malloc( sizeof(*data) + module_size )))
1496 data->hook = hook;
1497 data->tid = get_thread_id( current );
1498 data->hook_proc = hook_proc;
1499 memcpy( data + 1, module, module_size );
1501 msg->data = data;
1502 msg->data_size = sizeof(*data) + module_size;
1504 if (debug_level > 1)
1505 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1506 get_thread_id(thread), event, win, object_id, child_id );
1507 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1508 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1510 else
1511 free( msg );
1515 /* get the message queue of the current thread */
1516 DECL_HANDLER(get_msg_queue)
1518 struct msg_queue *queue = get_current_queue();
1520 reply->handle = 0;
1521 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1525 /* set the current message queue wakeup mask */
1526 DECL_HANDLER(set_queue_mask)
1528 struct msg_queue *queue = get_current_queue();
1530 if (queue)
1532 queue->wake_mask = req->wake_mask;
1533 queue->changed_mask = req->changed_mask;
1534 reply->wake_bits = queue->wake_bits;
1535 reply->changed_bits = queue->changed_bits;
1536 if (is_signaled( queue ))
1538 /* if skip wait is set, do what would have been done in the subsequent wait */
1539 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1540 else wake_up( &queue->obj, 0 );
1546 /* get the current message queue status */
1547 DECL_HANDLER(get_queue_status)
1549 struct msg_queue *queue = current->queue;
1550 if (queue)
1552 reply->wake_bits = queue->wake_bits;
1553 reply->changed_bits = queue->changed_bits;
1554 if (req->clear) queue->changed_bits = 0;
1556 else reply->wake_bits = reply->changed_bits = 0;
1560 /* send a message to a thread queue */
1561 DECL_HANDLER(send_message)
1563 struct message *msg;
1564 struct msg_queue *send_queue = get_current_queue();
1565 struct msg_queue *recv_queue = NULL;
1566 struct thread *thread = NULL;
1568 if (!(thread = get_thread_from_id( req->id ))) return;
1570 if (!(recv_queue = thread->queue))
1572 set_error( STATUS_INVALID_PARAMETER );
1573 release_object( thread );
1574 return;
1576 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1578 set_error( STATUS_TIMEOUT );
1579 release_object( thread );
1580 return;
1583 if ((msg = mem_alloc( sizeof(*msg) )))
1585 msg->type = req->type;
1586 msg->win = get_user_full_handle( req->win );
1587 msg->msg = req->msg;
1588 msg->wparam = req->wparam;
1589 msg->lparam = req->lparam;
1590 msg->time = get_tick_count();
1591 msg->x = 0;
1592 msg->y = 0;
1593 msg->info = 0;
1594 msg->result = NULL;
1595 msg->data = NULL;
1596 msg->data_size = get_req_data_size();
1598 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1600 free( msg );
1601 release_object( thread );
1602 return;
1605 switch(msg->type)
1607 case MSG_OTHER_PROCESS:
1608 case MSG_ASCII:
1609 case MSG_UNICODE:
1610 case MSG_CALLBACK:
1611 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
1613 free_message( msg );
1614 break;
1616 /* fall through */
1617 case MSG_NOTIFY:
1618 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1619 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1620 break;
1621 case MSG_POSTED:
1622 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1623 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1624 break;
1625 case MSG_HARDWARE: /* should use send_hardware_message instead */
1626 case MSG_CALLBACK_RESULT: /* cannot send this one */
1627 default:
1628 set_error( STATUS_INVALID_PARAMETER );
1629 free( msg );
1630 break;
1633 release_object( thread );
1636 /* send a hardware message to a thread queue */
1637 DECL_HANDLER(send_hardware_message)
1639 struct message *msg;
1640 struct msg_queue *recv_queue = NULL;
1641 struct thread *thread = NULL;
1643 if (req->id)
1645 if (!(thread = get_thread_from_id( req->id ))) return;
1648 if (thread && !(recv_queue = thread->queue))
1650 set_error( STATUS_INVALID_PARAMETER );
1651 release_object( thread );
1652 return;
1655 if ((msg = mem_alloc( sizeof(*msg) )))
1657 msg->type = MSG_HARDWARE;
1658 msg->win = get_user_full_handle( req->win );
1659 msg->msg = req->msg;
1660 msg->wparam = req->wparam;
1661 msg->lparam = req->lparam;
1662 msg->time = req->time;
1663 msg->x = req->x;
1664 msg->y = req->y;
1665 msg->info = req->info;
1666 msg->result = NULL;
1667 msg->data = NULL;
1668 msg->data_size = 0;
1669 queue_hardware_message( recv_queue, msg );
1671 if (thread) release_object( thread );
1674 /* post a quit message to the current queue */
1675 DECL_HANDLER(post_quit_message)
1677 struct msg_queue *queue = get_current_queue();
1679 if (!queue)
1680 return;
1682 queue->quit_message = 1;
1683 queue->exit_code = req->exit_code;
1684 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1687 /* get a message from the current queue */
1688 DECL_HANDLER(get_message)
1690 struct timer *timer;
1691 struct list *ptr;
1692 struct msg_queue *queue = get_current_queue();
1693 user_handle_t get_win = get_user_full_handle( req->get_win );
1694 unsigned int filter = req->flags >> 16;
1696 reply->active_hooks = get_active_hooks();
1698 if (!queue) return;
1699 queue->last_get_msg = current_time;
1700 if (!filter) filter = QS_ALLINPUT;
1702 /* first check for sent messages */
1703 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1705 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1706 receive_message( queue, msg, reply );
1707 return;
1710 /* clear changed bits so we can wait on them if we don't find a message */
1711 if (filter & QS_POSTMESSAGE)
1713 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
1714 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
1716 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
1717 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
1719 /* then check for posted messages */
1720 if ((filter & QS_POSTMESSAGE) &&
1721 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1722 return;
1724 /* only check for quit messages if not posted messages pending.
1725 * note: the quit message isn't filtered */
1726 if (get_quit_message( queue, req->flags, reply ))
1727 return;
1729 /* then check for any raw hardware message */
1730 if ((filter & QS_INPUT) &&
1731 filter_contains_hw_range( req->get_first, req->get_last ) &&
1732 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1733 return;
1735 /* now check for WM_PAINT */
1736 if ((filter & QS_PAINT) &&
1737 queue->paint_count &&
1738 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1739 (reply->win = find_window_to_repaint( get_win, current )))
1741 reply->type = MSG_POSTED;
1742 reply->msg = WM_PAINT;
1743 reply->wparam = 0;
1744 reply->lparam = 0;
1745 reply->x = 0;
1746 reply->y = 0;
1747 reply->time = get_tick_count();
1748 reply->info = 0;
1749 return;
1752 /* now check for timer */
1753 if ((filter & QS_TIMER) &&
1754 (timer = find_expired_timer( queue, get_win, req->get_first,
1755 req->get_last, (req->flags & PM_REMOVE) )))
1757 reply->type = MSG_POSTED;
1758 reply->win = timer->win;
1759 reply->msg = timer->msg;
1760 reply->wparam = timer->id;
1761 reply->lparam = timer->lparam;
1762 reply->x = 0;
1763 reply->y = 0;
1764 reply->time = get_tick_count();
1765 reply->info = 0;
1766 return;
1769 set_error( STATUS_PENDING ); /* FIXME */
1773 /* reply to a sent message */
1774 DECL_HANDLER(reply_message)
1776 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1777 else if (current->queue->recv_result)
1778 reply_message( current->queue, req->result, 0, req->remove,
1779 get_req_data(), get_req_data_size() );
1783 /* accept the current hardware message */
1784 DECL_HANDLER(accept_hardware_message)
1786 if (current->queue)
1787 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1788 else
1789 set_error( STATUS_ACCESS_DENIED );
1793 /* retrieve the reply for the last message sent */
1794 DECL_HANDLER(get_message_reply)
1796 struct message_result *result;
1797 struct list *entry;
1798 struct msg_queue *queue = current->queue;
1800 if (queue)
1802 set_error( STATUS_PENDING );
1803 reply->result = 0;
1805 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1807 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1808 if (result->replied || req->cancel)
1810 if (result->replied)
1812 reply->result = result->result;
1813 set_error( result->error );
1814 if (result->data)
1816 data_size_t data_len = min( result->data_size, get_reply_max_size() );
1817 set_reply_data_ptr( result->data, data_len );
1818 result->data = NULL;
1819 result->data_size = 0;
1822 remove_result_from_sender( result );
1824 entry = list_head( &queue->send_result );
1825 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1826 else
1828 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1829 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1833 else set_error( STATUS_ACCESS_DENIED );
1837 /* set a window timer */
1838 DECL_HANDLER(set_win_timer)
1840 struct timer *timer;
1841 struct msg_queue *queue;
1842 struct thread *thread = NULL;
1843 user_handle_t win = 0;
1844 unsigned long id = req->id;
1846 if (req->win)
1848 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1850 set_error( STATUS_INVALID_HANDLE );
1851 return;
1853 if (thread->process != current->process)
1855 release_object( thread );
1856 set_error( STATUS_ACCESS_DENIED );
1857 return;
1859 queue = thread->queue;
1860 /* remove it if it existed already */
1861 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1863 else
1865 queue = get_current_queue();
1866 /* find a free id for it */
1869 id = queue->next_timer_id;
1870 if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1872 while (find_timer( queue, 0, req->msg, id ));
1875 if ((timer = set_timer( queue, req->rate )))
1877 timer->win = win;
1878 timer->msg = req->msg;
1879 timer->id = id;
1880 timer->lparam = req->lparam;
1881 reply->id = id;
1883 if (thread) release_object( thread );
1886 /* kill a window timer */
1887 DECL_HANDLER(kill_win_timer)
1889 struct timer *timer;
1890 struct thread *thread;
1891 user_handle_t win = 0;
1893 if (req->win)
1895 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1897 set_error( STATUS_INVALID_HANDLE );
1898 return;
1900 if (thread->process != current->process)
1902 release_object( thread );
1903 set_error( STATUS_ACCESS_DENIED );
1904 return;
1907 else thread = (struct thread *)grab_object( current );
1909 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1910 free_timer( thread->queue, timer );
1911 else
1912 set_error( STATUS_INVALID_PARAMETER );
1914 release_object( thread );
1918 /* attach (or detach) thread inputs */
1919 DECL_HANDLER(attach_thread_input)
1921 struct thread *thread_from = get_thread_from_id( req->tid_from );
1922 struct thread *thread_to = get_thread_from_id( req->tid_to );
1924 if (!thread_from || !thread_to)
1926 if (thread_from) release_object( thread_from );
1927 if (thread_to) release_object( thread_to );
1928 return;
1930 if (thread_from != thread_to)
1932 if (req->attach) attach_thread_input( thread_from, thread_to );
1933 else
1935 if (thread_from->queue && thread_to->queue &&
1936 thread_from->queue->input == thread_to->queue->input)
1937 detach_thread_input( thread_from );
1938 else
1939 set_error( STATUS_ACCESS_DENIED );
1942 else set_error( STATUS_ACCESS_DENIED );
1943 release_object( thread_from );
1944 release_object( thread_to );
1948 /* get thread input data */
1949 DECL_HANDLER(get_thread_input)
1951 struct thread *thread = NULL;
1952 struct thread_input *input;
1954 if (req->tid)
1956 if (!(thread = get_thread_from_id( req->tid ))) return;
1957 input = thread->queue ? thread->queue->input : NULL;
1959 else input = foreground_input; /* get the foreground thread info */
1961 if (input)
1963 reply->focus = input->focus;
1964 reply->capture = input->capture;
1965 reply->active = input->active;
1966 reply->menu_owner = input->menu_owner;
1967 reply->move_size = input->move_size;
1968 reply->caret = input->caret;
1969 reply->rect = input->caret_rect;
1971 else
1973 reply->focus = 0;
1974 reply->capture = 0;
1975 reply->active = 0;
1976 reply->menu_owner = 0;
1977 reply->move_size = 0;
1978 reply->caret = 0;
1979 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1981 /* foreground window is active window of foreground thread */
1982 reply->foreground = foreground_input ? foreground_input->active : 0;
1983 if (thread) release_object( thread );
1987 /* retrieve queue keyboard state for a given thread */
1988 DECL_HANDLER(get_key_state)
1990 struct thread *thread;
1991 struct thread_input *input;
1993 if (!(thread = get_thread_from_id( req->tid ))) return;
1994 input = thread->queue ? thread->queue->input : NULL;
1995 if (input)
1997 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1998 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
2000 release_object( thread );
2004 /* set queue keyboard state for a given thread */
2005 DECL_HANDLER(set_key_state)
2007 struct thread *thread = NULL;
2008 struct thread_input *input;
2010 if (!(thread = get_thread_from_id( req->tid ))) return;
2011 input = thread->queue ? thread->queue->input : NULL;
2012 if (input)
2014 data_size_t size = min( sizeof(input->keystate), get_req_data_size() );
2015 if (size) memcpy( input->keystate, get_req_data(), size );
2017 release_object( thread );
2021 /* set the system foreground window */
2022 DECL_HANDLER(set_foreground_window)
2024 struct thread *thread;
2025 struct msg_queue *queue = get_current_queue();
2027 reply->previous = foreground_input ? foreground_input->active : 0;
2028 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
2029 reply->send_msg_new = FALSE;
2031 if (is_top_level_window( req->handle ) &&
2032 ((thread = get_window_thread( req->handle ))))
2034 foreground_input = thread->queue->input;
2035 reply->send_msg_new = (foreground_input != queue->input);
2036 release_object( thread );
2038 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2042 /* set the current thread focus window */
2043 DECL_HANDLER(set_focus_window)
2045 struct msg_queue *queue = get_current_queue();
2047 reply->previous = 0;
2048 if (queue && check_queue_input_window( queue, req->handle ))
2050 reply->previous = queue->input->focus;
2051 queue->input->focus = get_user_full_handle( req->handle );
2056 /* set the current thread active window */
2057 DECL_HANDLER(set_active_window)
2059 struct msg_queue *queue = get_current_queue();
2061 reply->previous = 0;
2062 if (queue && check_queue_input_window( queue, req->handle ))
2064 if (!req->handle || make_window_active( req->handle ))
2066 reply->previous = queue->input->active;
2067 queue->input->active = get_user_full_handle( req->handle );
2069 else set_error( STATUS_INVALID_HANDLE );
2074 /* set the current thread capture window */
2075 DECL_HANDLER(set_capture_window)
2077 struct msg_queue *queue = get_current_queue();
2079 reply->previous = reply->full_handle = 0;
2080 if (queue && check_queue_input_window( queue, req->handle ))
2082 struct thread_input *input = queue->input;
2084 reply->previous = input->capture;
2085 input->capture = get_user_full_handle( req->handle );
2086 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2087 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2088 reply->full_handle = input->capture;
2093 /* Set the current thread caret window */
2094 DECL_HANDLER(set_caret_window)
2096 struct msg_queue *queue = get_current_queue();
2098 reply->previous = 0;
2099 if (queue && check_queue_input_window( queue, req->handle ))
2101 struct thread_input *input = queue->input;
2103 reply->previous = input->caret;
2104 reply->old_rect = input->caret_rect;
2105 reply->old_hide = input->caret_hide;
2106 reply->old_state = input->caret_state;
2108 set_caret_window( input, get_user_full_handle(req->handle) );
2109 input->caret_rect.right = input->caret_rect.left + req->width;
2110 input->caret_rect.bottom = input->caret_rect.top + req->height;
2115 /* Set the current thread caret information */
2116 DECL_HANDLER(set_caret_info)
2118 struct msg_queue *queue = get_current_queue();
2119 struct thread_input *input;
2121 if (!queue) return;
2122 input = queue->input;
2123 reply->full_handle = input->caret;
2124 reply->old_rect = input->caret_rect;
2125 reply->old_hide = input->caret_hide;
2126 reply->old_state = input->caret_state;
2128 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2130 set_error( STATUS_ACCESS_DENIED );
2131 return;
2133 if (req->flags & SET_CARET_POS)
2135 input->caret_rect.right += req->x - input->caret_rect.left;
2136 input->caret_rect.bottom += req->y - input->caret_rect.top;
2137 input->caret_rect.left = req->x;
2138 input->caret_rect.top = req->y;
2140 if (req->flags & SET_CARET_HIDE)
2142 input->caret_hide += req->hide;
2143 if (input->caret_hide < 0) input->caret_hide = 0;
2145 if (req->flags & SET_CARET_STATE)
2147 if (req->state == -1) input->caret_state = !input->caret_state;
2148 else input->caret_state = !!req->state;
2153 /* get the time of the last input event */
2154 DECL_HANDLER(get_last_input_time)
2156 reply->time = last_input_time;