msi/tests: Remove win9x hacks.
[wine/multimedia.git] / server / queue.c
blob281c8bc72971a5f0825237a28e66c344101175f0
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 error; /* error code to pass back to sender */
60 lparam_t result; /* reply result */
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 lparam_t wparam; /* parameters */
74 lparam_t lparam; /* parameters */
75 unsigned int time; /* message time */
76 void *data; /* message data for sent messages */
77 unsigned int data_size; /* size of message data */
78 unsigned int unique_id; /* unique id for nested hw message waits */
79 struct message_result *result; /* result in sender queue */
82 struct timer
84 struct list entry; /* entry in timer list */
85 timeout_t when; /* next expiration */
86 unsigned int rate; /* timer rate in ms */
87 user_handle_t win; /* window handle */
88 unsigned int msg; /* message to post */
89 lparam_t id; /* timer id */
90 lparam_t lparam; /* lparam for message */
93 struct thread_input
95 struct object obj; /* object header */
96 struct desktop *desktop; /* desktop that this thread input belongs to */
97 user_handle_t focus; /* focus window */
98 user_handle_t capture; /* capture window */
99 user_handle_t active; /* active window */
100 user_handle_t menu_owner; /* current menu owner window */
101 user_handle_t move_size; /* current moving/resizing window */
102 user_handle_t caret; /* caret window */
103 rectangle_t caret_rect; /* caret rectangle */
104 int caret_hide; /* caret hide count */
105 int caret_state; /* caret on/off state */
106 user_handle_t cursor; /* current cursor */
107 int cursor_count; /* cursor show count */
108 struct list msg_list; /* list of hardware messages */
109 unsigned char keystate[256]; /* state of each key */
112 struct msg_queue
114 struct object obj; /* object header */
115 struct fd *fd; /* optional file descriptor to poll */
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 int cursor_count; /* per-queue cursor show count */
124 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
125 struct list send_result; /* stack of sent messages waiting for result */
126 struct list callback_result; /* list of callback messages waiting for result */
127 struct message_result *recv_result; /* stack of received messages waiting for result */
128 struct list pending_timers; /* list of pending timers */
129 struct list expired_timers; /* list of expired timers */
130 lparam_t next_timer_id; /* id for the next timer with a 0 window */
131 struct timeout_user *timeout; /* timeout for next timer to expire */
132 struct thread_input *input; /* thread input descriptor */
133 struct hook_table *hooks; /* hook table */
134 timeout_t last_get_msg; /* time of last get message call */
137 static void msg_queue_dump( struct object *obj, int verbose );
138 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
139 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
140 static int msg_queue_signaled( struct object *obj, struct thread *thread );
141 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
142 static void msg_queue_destroy( struct object *obj );
143 static void msg_queue_poll_event( struct fd *fd, int event );
144 static void thread_input_dump( struct object *obj, int verbose );
145 static void thread_input_destroy( struct object *obj );
146 static void timer_callback( void *private );
148 static const struct object_ops msg_queue_ops =
150 sizeof(struct msg_queue), /* size */
151 msg_queue_dump, /* dump */
152 no_get_type, /* get_type */
153 msg_queue_add_queue, /* add_queue */
154 msg_queue_remove_queue, /* remove_queue */
155 msg_queue_signaled, /* signaled */
156 msg_queue_satisfied, /* satisfied */
157 no_signal, /* signal */
158 no_get_fd, /* get_fd */
159 no_map_access, /* map_access */
160 default_get_sd, /* get_sd */
161 default_set_sd, /* set_sd */
162 no_lookup_name, /* lookup_name */
163 no_open_file, /* open_file */
164 no_close_handle, /* close_handle */
165 msg_queue_destroy /* destroy */
168 static const struct fd_ops msg_queue_fd_ops =
170 NULL, /* get_poll_events */
171 msg_queue_poll_event, /* poll_event */
172 NULL, /* flush */
173 NULL, /* get_fd_type */
174 NULL, /* ioctl */
175 NULL, /* queue_async */
176 NULL, /* reselect_async */
177 NULL /* cancel async */
181 static const struct object_ops thread_input_ops =
183 sizeof(struct thread_input), /* size */
184 thread_input_dump, /* dump */
185 no_get_type, /* get_type */
186 no_add_queue, /* add_queue */
187 NULL, /* remove_queue */
188 NULL, /* signaled */
189 NULL, /* satisfied */
190 no_signal, /* signal */
191 no_get_fd, /* get_fd */
192 no_map_access, /* map_access */
193 default_get_sd, /* get_sd */
194 default_set_sd, /* set_sd */
195 no_lookup_name, /* lookup_name */
196 no_open_file, /* open_file */
197 no_close_handle, /* close_handle */
198 thread_input_destroy /* destroy */
201 /* pointer to input structure of foreground thread */
202 static unsigned int last_input_time;
204 static void free_message( struct message *msg );
206 /* set the caret window in a given thread input */
207 static void set_caret_window( struct thread_input *input, user_handle_t win )
209 if (!win || win != input->caret)
211 input->caret_rect.left = 0;
212 input->caret_rect.top = 0;
213 input->caret_rect.right = 0;
214 input->caret_rect.bottom = 0;
216 input->caret = win;
217 input->caret_hide = 1;
218 input->caret_state = 0;
221 /* create a thread input object */
222 static struct thread_input *create_thread_input( struct thread *thread )
224 struct thread_input *input;
226 if ((input = alloc_object( &thread_input_ops )))
228 input->focus = 0;
229 input->capture = 0;
230 input->active = 0;
231 input->menu_owner = 0;
232 input->move_size = 0;
233 input->cursor = 0;
234 input->cursor_count = 0;
235 list_init( &input->msg_list );
236 set_caret_window( input, 0 );
237 memset( input->keystate, 0, sizeof(input->keystate) );
239 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
241 release_object( input );
242 return NULL;
245 return input;
248 /* create a message queue object */
249 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
251 struct thread_input *new_input = NULL;
252 struct msg_queue *queue;
253 int i;
255 if (!input)
257 if (!(new_input = create_thread_input( thread ))) return NULL;
258 input = new_input;
261 if ((queue = alloc_object( &msg_queue_ops )))
263 queue->fd = NULL;
264 queue->wake_bits = 0;
265 queue->wake_mask = 0;
266 queue->changed_bits = 0;
267 queue->changed_mask = 0;
268 queue->paint_count = 0;
269 queue->quit_message = 0;
270 queue->cursor_count = 0;
271 queue->recv_result = NULL;
272 queue->next_timer_id = 0x7fff;
273 queue->timeout = NULL;
274 queue->input = (struct thread_input *)grab_object( input );
275 queue->hooks = NULL;
276 queue->last_get_msg = current_time;
277 list_init( &queue->send_result );
278 list_init( &queue->callback_result );
279 list_init( &queue->pending_timers );
280 list_init( &queue->expired_timers );
281 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
283 thread->queue = queue;
285 if (new_input) release_object( new_input );
286 return queue;
289 /* free the message queue of a thread at thread exit */
290 void free_msg_queue( struct thread *thread )
292 remove_thread_hooks( thread );
293 if (!thread->queue) return;
294 release_object( thread->queue );
295 thread->queue = NULL;
298 /* change the thread input data of a given thread */
299 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
301 struct msg_queue *queue = thread->queue;
303 if (!queue)
305 thread->queue = create_msg_queue( thread, new_input );
306 return thread->queue != NULL;
308 if (queue->input)
310 queue->input->cursor_count -= queue->cursor_count;
311 release_object( queue->input );
313 queue->input = (struct thread_input *)grab_object( new_input );
314 new_input->cursor_count += queue->cursor_count;
315 return 1;
318 /* get the hook table for a given thread */
319 struct hook_table *get_queue_hooks( struct thread *thread )
321 if (!thread->queue) return NULL;
322 return thread->queue->hooks;
325 /* set the hook table for a given thread, allocating the queue if needed */
326 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
328 struct msg_queue *queue = thread->queue;
329 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
330 if (queue->hooks) release_object( queue->hooks );
331 queue->hooks = hooks;
334 /* check the queue status */
335 static inline int is_signaled( struct msg_queue *queue )
337 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
340 /* set some queue bits */
341 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
343 queue->wake_bits |= bits;
344 queue->changed_bits |= bits;
345 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
348 /* clear some queue bits */
349 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
351 queue->wake_bits &= ~bits;
352 queue->changed_bits &= ~bits;
355 /* check whether msg is a keyboard message */
356 static inline int is_keyboard_msg( struct message *msg )
358 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
361 /* check if message is matched by the filter */
362 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
364 return (msg >= first && msg <= last);
367 /* check whether a message filter contains at least one potential hardware message */
368 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
370 /* hardware message ranges are (in numerical order):
371 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
372 * WM_KEYFIRST .. WM_KEYLAST
373 * WM_MOUSEFIRST .. WM_MOUSELAST
375 if (last < WM_NCMOUSEFIRST) return 0;
376 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
377 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
378 if (first > WM_MOUSELAST) return 0;
379 return 1;
382 /* get the QS_* bit corresponding to a given hardware message */
383 static inline int get_hardware_msg_bit( struct message *msg )
385 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
386 if (is_keyboard_msg( msg )) return QS_KEY;
387 return QS_MOUSEBUTTON;
390 /* get the current thread queue, creating it if needed */
391 static inline struct msg_queue *get_current_queue(void)
393 struct msg_queue *queue = current->queue;
394 if (!queue) queue = create_msg_queue( current, NULL );
395 return queue;
398 /* get a (pseudo-)unique id to tag hardware messages */
399 static inline unsigned int get_unique_id(void)
401 static unsigned int id;
402 if (!++id) id = 1; /* avoid an id of 0 */
403 return id;
406 /* try to merge a message with the last in the list; return 1 if successful */
407 static int merge_message( struct thread_input *input, const struct message *msg )
409 struct message *prev;
410 struct list *ptr = list_tail( &input->msg_list );
412 if (!ptr) return 0;
413 prev = LIST_ENTRY( ptr, struct message, entry );
414 if (prev->result) return 0;
415 if (prev->win && msg->win && prev->win != msg->win) return 0;
416 if (prev->msg != msg->msg) return 0;
417 if (prev->type != msg->type) return 0;
418 /* now we can merge it */
419 prev->wparam = msg->wparam;
420 prev->lparam = msg->lparam;
421 prev->time = msg->time;
422 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
424 struct hardware_msg_data *prev_data = prev->data;
425 struct hardware_msg_data *msg_data = msg->data;
426 prev_data->x = msg_data->x;
427 prev_data->y = msg_data->y;
428 prev_data->info = msg_data->info;
430 return 1;
433 /* free a result structure */
434 static void free_result( struct message_result *result )
436 if (result->timeout) remove_timeout_user( result->timeout );
437 free( result->data );
438 if (result->callback_msg) free_message( result->callback_msg );
439 free( result );
442 /* remove the result from the sender list it is on */
443 static inline void remove_result_from_sender( struct message_result *result )
445 assert( result->sender );
447 list_remove( &result->sender_entry );
448 result->sender = NULL;
449 if (!result->receiver) free_result( result );
452 /* store the message result in the appropriate structure */
453 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
455 res->result = result;
456 res->error = error;
457 res->replied = 1;
458 if (res->timeout)
460 remove_timeout_user( res->timeout );
461 res->timeout = NULL;
463 if (res->sender)
465 if (res->callback_msg)
467 /* queue the callback message in the sender queue */
468 struct callback_msg_data *data = res->callback_msg->data;
469 data->result = result;
470 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
471 set_queue_bits( res->sender, QS_SENDMESSAGE );
472 res->callback_msg = NULL;
473 remove_result_from_sender( res );
475 else
477 /* wake sender queue if waiting on this result */
478 if (list_head(&res->sender->send_result) == &res->sender_entry)
479 set_queue_bits( res->sender, QS_SMRESULT );
485 /* free a message when deleting a queue or window */
486 static void free_message( struct message *msg )
488 struct message_result *result = msg->result;
489 if (result)
491 result->msg = NULL;
492 if (result->sender)
494 result->receiver = NULL;
495 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
497 else free_result( result );
499 free( msg->data );
500 free( msg );
503 /* remove (and free) a message from a message list */
504 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
505 enum message_kind kind )
507 list_remove( &msg->entry );
508 switch(kind)
510 case SEND_MESSAGE:
511 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
512 break;
513 case POST_MESSAGE:
514 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
515 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
516 break;
518 free_message( msg );
521 /* message timed out without getting a reply */
522 static void result_timeout( void *private )
524 struct message_result *result = private;
526 assert( !result->replied );
528 result->timeout = NULL;
530 if (result->msg) /* not received yet */
532 struct message *msg = result->msg;
534 result->msg = NULL;
535 msg->result = NULL;
536 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
537 result->receiver = NULL;
538 if (!result->sender)
540 free_result( result );
541 return;
545 store_message_result( result, 0, STATUS_TIMEOUT );
548 /* allocate and fill a message result structure */
549 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
550 struct msg_queue *recv_queue,
551 struct message *msg, timeout_t timeout )
553 struct message_result *result = mem_alloc( sizeof(*result) );
554 if (result)
556 result->msg = msg;
557 result->sender = send_queue;
558 result->receiver = recv_queue;
559 result->replied = 0;
560 result->data = NULL;
561 result->data_size = 0;
562 result->timeout = NULL;
564 if (msg->type == MSG_CALLBACK)
566 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
568 if (!callback_msg)
570 free( result );
571 return NULL;
573 callback_msg->type = MSG_CALLBACK_RESULT;
574 callback_msg->win = msg->win;
575 callback_msg->msg = msg->msg;
576 callback_msg->wparam = 0;
577 callback_msg->lparam = 0;
578 callback_msg->time = get_tick_count();
579 callback_msg->result = NULL;
580 /* steal the data from the original message */
581 callback_msg->data = msg->data;
582 callback_msg->data_size = msg->data_size;
583 msg->data = NULL;
584 msg->data_size = 0;
586 result->callback_msg = callback_msg;
587 list_add_head( &send_queue->callback_result, &result->sender_entry );
589 else
591 result->callback_msg = NULL;
592 list_add_head( &send_queue->send_result, &result->sender_entry );
595 if (timeout != TIMEOUT_INFINITE)
596 result->timeout = add_timeout_user( timeout, result_timeout, result );
598 return result;
601 /* receive a message, removing it from the sent queue */
602 static void receive_message( struct msg_queue *queue, struct message *msg,
603 struct get_message_reply *reply )
605 struct message_result *result = msg->result;
607 reply->total = msg->data_size;
608 if (msg->data_size > get_reply_max_size())
610 set_error( STATUS_BUFFER_OVERFLOW );
611 return;
613 reply->type = msg->type;
614 reply->win = msg->win;
615 reply->msg = msg->msg;
616 reply->wparam = msg->wparam;
617 reply->lparam = msg->lparam;
618 reply->time = msg->time;
620 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
622 list_remove( &msg->entry );
623 /* put the result on the receiver result stack */
624 if (result)
626 result->msg = NULL;
627 result->recv_next = queue->recv_result;
628 queue->recv_result = result;
630 free( msg );
631 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
634 /* set the result of the current received message */
635 static void reply_message( struct msg_queue *queue, lparam_t result,
636 unsigned int error, int remove, const void *data, data_size_t len )
638 struct message_result *res = queue->recv_result;
640 if (remove)
642 queue->recv_result = res->recv_next;
643 res->receiver = NULL;
644 if (!res->sender) /* no one waiting for it */
646 free_result( res );
647 return;
650 if (!res->replied)
652 if (len && (res->data = memdup( data, len ))) res->data_size = len;
653 store_message_result( res, result, error );
657 static int match_window( user_handle_t win, user_handle_t msg_win )
659 if (!win) return 1;
660 if (win == -1 || win == 1) return !msg_win;
661 if (msg_win == win) return 1;
662 return is_child_window( win, msg_win );
665 /* retrieve a posted message */
666 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
667 unsigned int first, unsigned int last, unsigned int flags,
668 struct get_message_reply *reply )
670 struct message *msg;
672 /* check against the filters */
673 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
675 if (!match_window( win, msg->win )) continue;
676 if (!check_msg_filter( msg->msg, first, last )) continue;
677 goto found; /* found one */
679 return 0;
681 /* return it to the app */
682 found:
683 reply->total = msg->data_size;
684 if (msg->data_size > get_reply_max_size())
686 set_error( STATUS_BUFFER_OVERFLOW );
687 return 1;
689 reply->type = msg->type;
690 reply->win = msg->win;
691 reply->msg = msg->msg;
692 reply->wparam = msg->wparam;
693 reply->lparam = msg->lparam;
694 reply->time = msg->time;
696 if (flags & PM_REMOVE)
698 if (msg->data)
700 set_reply_data_ptr( msg->data, msg->data_size );
701 msg->data = NULL;
702 msg->data_size = 0;
704 remove_queue_message( queue, msg, POST_MESSAGE );
706 else if (msg->data) set_reply_data( msg->data, msg->data_size );
708 return 1;
711 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
712 struct get_message_reply *reply )
714 if (queue->quit_message)
716 reply->total = 0;
717 reply->type = MSG_POSTED;
718 reply->win = 0;
719 reply->msg = WM_QUIT;
720 reply->wparam = queue->exit_code;
721 reply->lparam = 0;
722 reply->time = get_tick_count();
724 if (flags & PM_REMOVE)
726 queue->quit_message = 0;
727 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
728 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
730 return 1;
732 else
733 return 0;
736 /* empty a message list and free all the messages */
737 static void empty_msg_list( struct list *list )
739 struct list *ptr;
741 while ((ptr = list_head( list )) != NULL)
743 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
744 list_remove( &msg->entry );
745 free_message( msg );
749 /* cleanup all pending results when deleting a queue */
750 static void cleanup_results( struct msg_queue *queue )
752 struct list *entry;
754 while ((entry = list_head( &queue->send_result )) != NULL)
756 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
759 while ((entry = list_head( &queue->callback_result )) != NULL)
761 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
764 while (queue->recv_result)
765 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
768 /* check if the thread owning the queue is hung (not checking for messages) */
769 static int is_queue_hung( struct msg_queue *queue )
771 struct wait_queue_entry *entry;
773 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
774 return 0; /* less than 5 seconds since last get message -> not hung */
776 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
778 if (entry->thread->queue == queue)
779 return 0; /* thread is waiting on queue -> not hung */
781 return 1;
784 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
786 struct msg_queue *queue = (struct msg_queue *)obj;
787 struct process *process = entry->thread->process;
789 /* a thread can only wait on its own queue */
790 if (entry->thread->queue != queue)
792 set_error( STATUS_ACCESS_DENIED );
793 return 0;
795 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
797 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
798 set_fd_events( queue->fd, POLLIN );
799 add_queue( obj, entry );
800 return 1;
803 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
805 struct msg_queue *queue = (struct msg_queue *)obj;
807 remove_queue( obj, entry );
808 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
809 set_fd_events( queue->fd, 0 );
812 static void msg_queue_dump( struct object *obj, int verbose )
814 struct msg_queue *queue = (struct msg_queue *)obj;
815 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
816 queue->wake_bits, queue->wake_mask );
819 static int msg_queue_signaled( struct object *obj, struct thread *thread )
821 struct msg_queue *queue = (struct msg_queue *)obj;
822 int ret = 0;
824 if (queue->fd)
826 if ((ret = check_fd_events( queue->fd, POLLIN )))
827 /* stop waiting on select() if we are signaled */
828 set_fd_events( queue->fd, 0 );
829 else if (!list_empty( &obj->wait_queue ))
830 /* restart waiting on poll() if we are no longer signaled */
831 set_fd_events( queue->fd, POLLIN );
833 return ret || is_signaled( queue );
836 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
838 struct msg_queue *queue = (struct msg_queue *)obj;
839 queue->wake_mask = 0;
840 queue->changed_mask = 0;
841 return 0; /* Not abandoned */
844 static void msg_queue_destroy( struct object *obj )
846 struct msg_queue *queue = (struct msg_queue *)obj;
847 struct list *ptr;
848 int i;
850 cleanup_results( queue );
851 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
853 while ((ptr = list_head( &queue->pending_timers )))
855 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
856 list_remove( &timer->entry );
857 free( timer );
859 while ((ptr = list_head( &queue->expired_timers )))
861 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
862 list_remove( &timer->entry );
863 free( timer );
865 if (queue->timeout) remove_timeout_user( queue->timeout );
866 if (queue->input)
868 queue->input->cursor_count -= queue->cursor_count;
869 release_object( queue->input );
871 if (queue->hooks) release_object( queue->hooks );
872 if (queue->fd) release_object( queue->fd );
875 static void msg_queue_poll_event( struct fd *fd, int event )
877 struct msg_queue *queue = get_fd_user( fd );
878 assert( queue->obj.ops == &msg_queue_ops );
880 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
881 else set_fd_events( queue->fd, 0 );
882 wake_up( &queue->obj, 0 );
885 static void thread_input_dump( struct object *obj, int verbose )
887 struct thread_input *input = (struct thread_input *)obj;
888 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
889 input->focus, input->capture, input->active );
892 static void thread_input_destroy( struct object *obj )
894 struct thread_input *input = (struct thread_input *)obj;
896 empty_msg_list( &input->msg_list );
897 if (input->desktop)
899 if (input->desktop->foreground_input == input) input->desktop->foreground_input = NULL;
900 release_object( input->desktop );
904 /* fix the thread input data when a window is destroyed */
905 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
907 struct thread_input *input = queue->input;
909 if (window == input->focus) input->focus = 0;
910 if (window == input->capture) input->capture = 0;
911 if (window == input->active) input->active = 0;
912 if (window == input->menu_owner) input->menu_owner = 0;
913 if (window == input->move_size) input->move_size = 0;
914 if (window == input->caret) set_caret_window( input, 0 );
917 /* check if the specified window can be set in the input data of a given queue */
918 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
920 struct thread *thread;
921 int ret = 0;
923 if (!window) return 1; /* we can always clear the data */
925 if ((thread = get_window_thread( window )))
927 ret = (queue->input == thread->queue->input);
928 if (!ret) set_error( STATUS_ACCESS_DENIED );
929 release_object( thread );
931 else set_error( STATUS_INVALID_HANDLE );
933 return ret;
936 /* make sure the specified thread has a queue */
937 int init_thread_queue( struct thread *thread )
939 if (thread->queue) return 1;
940 return (create_msg_queue( thread, NULL ) != NULL);
943 /* attach two thread input data structures */
944 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
946 struct desktop *desktop;
947 struct thread_input *input;
948 int ret;
950 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
951 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
952 input = (struct thread_input *)grab_object( thread_to->queue->input );
953 if (input->desktop != desktop)
955 set_error( STATUS_ACCESS_DENIED );
956 release_object( input );
957 release_object( desktop );
958 return 0;
960 release_object( desktop );
962 ret = assign_thread_input( thread_from, input );
963 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
964 release_object( input );
965 return ret;
968 /* detach two thread input data structures */
969 void detach_thread_input( struct thread *thread_from )
971 struct thread_input *input;
973 if ((input = create_thread_input( thread_from )))
975 assign_thread_input( thread_from, input );
976 release_object( input );
981 /* set the next timer to expire */
982 static void set_next_timer( struct msg_queue *queue )
984 struct list *ptr;
986 if (queue->timeout)
988 remove_timeout_user( queue->timeout );
989 queue->timeout = NULL;
991 if ((ptr = list_head( &queue->pending_timers )))
993 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
994 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
996 /* set/clear QS_TIMER bit */
997 if (list_empty( &queue->expired_timers ))
998 clear_queue_bits( queue, QS_TIMER );
999 else
1000 set_queue_bits( queue, QS_TIMER );
1003 /* find a timer from its window and id */
1004 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1005 unsigned int msg, lparam_t id )
1007 struct list *ptr;
1009 /* we need to search both lists */
1011 LIST_FOR_EACH( ptr, &queue->pending_timers )
1013 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1014 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1016 LIST_FOR_EACH( ptr, &queue->expired_timers )
1018 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1019 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1021 return NULL;
1024 /* callback for the next timer expiration */
1025 static void timer_callback( void *private )
1027 struct msg_queue *queue = private;
1028 struct list *ptr;
1030 queue->timeout = NULL;
1031 /* move on to the next timer */
1032 ptr = list_head( &queue->pending_timers );
1033 list_remove( ptr );
1034 list_add_tail( &queue->expired_timers, ptr );
1035 set_next_timer( queue );
1038 /* link a timer at its rightful place in the queue list */
1039 static void link_timer( struct msg_queue *queue, struct timer *timer )
1041 struct list *ptr;
1043 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1045 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1046 if (t->when >= timer->when) break;
1048 list_add_before( ptr, &timer->entry );
1051 /* remove a timer from the queue timer list and free it */
1052 static void free_timer( struct msg_queue *queue, struct timer *timer )
1054 list_remove( &timer->entry );
1055 free( timer );
1056 set_next_timer( queue );
1059 /* restart an expired timer */
1060 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1062 list_remove( &timer->entry );
1063 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1064 link_timer( queue, timer );
1065 set_next_timer( queue );
1068 /* find an expired timer matching the filtering parameters */
1069 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1070 unsigned int get_first, unsigned int get_last,
1071 int remove )
1073 struct list *ptr;
1075 LIST_FOR_EACH( ptr, &queue->expired_timers )
1077 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1078 if (win && timer->win != win) continue;
1079 if (check_msg_filter( timer->msg, get_first, get_last ))
1081 if (remove) restart_timer( queue, timer );
1082 return timer;
1085 return NULL;
1088 /* add a timer */
1089 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1091 struct timer *timer = mem_alloc( sizeof(*timer) );
1092 if (timer)
1094 timer->rate = max( rate, 1 );
1095 timer->when = current_time + (timeout_t)timer->rate * 10000;
1096 link_timer( queue, timer );
1097 /* check if we replaced the next timer */
1098 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1100 return timer;
1103 /* change the input key state for a given key */
1104 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1106 if (down)
1108 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1109 keystate[key] |= 0x80;
1111 else keystate[key] &= ~0x80;
1114 /* update the input key state for a keyboard message */
1115 static void update_input_key_state( unsigned char *keystate, const struct message *msg )
1117 unsigned char key;
1118 int down = 0;
1120 switch (msg->msg)
1122 case WM_LBUTTONDOWN:
1123 down = 1;
1124 /* fall through */
1125 case WM_LBUTTONUP:
1126 set_input_key_state( keystate, VK_LBUTTON, down );
1127 break;
1128 case WM_MBUTTONDOWN:
1129 down = 1;
1130 /* fall through */
1131 case WM_MBUTTONUP:
1132 set_input_key_state( keystate, VK_MBUTTON, down );
1133 break;
1134 case WM_RBUTTONDOWN:
1135 down = 1;
1136 /* fall through */
1137 case WM_RBUTTONUP:
1138 set_input_key_state( keystate, VK_RBUTTON, down );
1139 break;
1140 case WM_XBUTTONDOWN:
1141 down = 1;
1142 /* fall through */
1143 case WM_XBUTTONUP:
1144 if (msg->wparam == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1145 else if (msg->wparam == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1146 break;
1147 case WM_KEYDOWN:
1148 case WM_SYSKEYDOWN:
1149 down = 1;
1150 /* fall through */
1151 case WM_KEYUP:
1152 case WM_SYSKEYUP:
1153 key = (unsigned char)msg->wparam;
1154 set_input_key_state( keystate, key, down );
1155 switch(key)
1157 case VK_LCONTROL:
1158 case VK_RCONTROL:
1159 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1160 set_input_key_state( keystate, VK_CONTROL, down );
1161 break;
1162 case VK_LMENU:
1163 case VK_RMENU:
1164 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1165 set_input_key_state( keystate, VK_MENU, down );
1166 break;
1167 case VK_LSHIFT:
1168 case VK_RSHIFT:
1169 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1170 set_input_key_state( keystate, VK_SHIFT, down );
1171 break;
1173 break;
1177 /* release the hardware message currently being processed by the given thread */
1178 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1179 int remove, user_handle_t new_win )
1181 struct thread_input *input = queue->input;
1182 struct message *msg;
1184 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1186 if (msg->unique_id == hw_id) break;
1188 if (&msg->entry == &input->msg_list) return; /* not found */
1190 /* clear the queue bit for that message */
1191 if (remove || new_win)
1193 struct message *other;
1194 int clr_bit;
1196 clr_bit = get_hardware_msg_bit( msg );
1197 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1199 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1201 clr_bit = 0;
1202 break;
1205 if (clr_bit) clear_queue_bits( queue, clr_bit );
1208 if (new_win) /* set the new window */
1210 struct thread *owner = get_window_thread( new_win );
1211 if (owner)
1213 msg->win = new_win;
1214 if (owner->queue->input != input)
1216 list_remove( &msg->entry );
1217 if (msg->msg == WM_MOUSEMOVE && merge_message( owner->queue->input, msg ))
1219 free_message( msg );
1220 release_object( owner );
1221 return;
1223 list_add_tail( &owner->queue->input->msg_list, &msg->entry );
1225 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1226 remove = 0;
1227 release_object( owner );
1230 if (remove)
1232 update_input_key_state( input->keystate, msg );
1233 list_remove( &msg->entry );
1234 free_message( msg );
1238 /* find the window that should receive a given hardware message */
1239 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1240 unsigned int *msg_code )
1242 struct hardware_msg_data *data = msg->data;
1243 user_handle_t win = 0;
1245 *msg_code = msg->msg;
1246 if (is_keyboard_msg( msg ))
1248 if (input && !(win = input->focus))
1250 win = input->active;
1251 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1254 else /* mouse message */
1256 if (!input || !(win = input->capture))
1258 if (!(win = msg->win) || !is_window_visible( win ) || is_window_transparent( win ))
1260 if (input) win = window_from_point( input->desktop, data->x, data->y );
1264 return win;
1267 /* set the cursor position, clipping to the cursor clip rect */
1268 static void set_cursor_pos( struct desktop *desktop, int x, int y )
1270 desktop->cursor_x = min( max( x, desktop->cursor_clip.left ), desktop->cursor_clip.right - 1 );
1271 desktop->cursor_y = min( max( y, desktop->cursor_clip.top ), desktop->cursor_clip.bottom - 1 );
1274 /* queue a hardware message into a given thread input */
1275 static void queue_hardware_message( struct desktop *desktop, struct thread_input *input,
1276 struct message *msg )
1278 user_handle_t win;
1279 struct thread *thread;
1280 unsigned int msg_code;
1281 struct hardware_msg_data *data = msg->data;
1283 if (msg->msg == WM_MOUSEMOVE) set_cursor_pos( desktop, data->x, data->y );
1284 data->x = desktop->cursor_x;
1285 data->y = desktop->cursor_y;
1286 update_input_key_state( desktop->keystate, msg );
1287 last_input_time = get_tick_count();
1288 win = find_hardware_message_window( input, msg, &msg_code );
1289 if (!win || !(thread = get_window_thread(win)))
1291 if (input) update_input_key_state( input->keystate, msg );
1292 free( msg );
1293 return;
1295 input = thread->queue->input;
1297 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1298 else
1300 msg->unique_id = 0; /* will be set once we return it to the app */
1301 list_add_tail( &input->msg_list, &msg->entry );
1302 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1304 release_object( thread );
1307 /* check message filter for a hardware message */
1308 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1309 user_handle_t filter_win, unsigned int first, unsigned int last )
1311 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1313 /* we can only test the window for a keyboard message since the
1314 * dest window for a mouse message depends on hittest */
1315 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1316 return 0;
1317 /* the message code is final for a keyboard message, we can simply check it */
1318 return check_msg_filter( msg_code, first, last );
1320 else /* mouse message */
1322 /* we need to check all possible values that the message can have in the end */
1324 if (check_msg_filter( msg_code, first, last )) return 1;
1325 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1327 /* all other messages can become non-client messages */
1328 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1330 /* clicks can become double-clicks or non-client double-clicks */
1331 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1332 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1334 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1335 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1337 return 0;
1342 /* find a hardware message for the given queue */
1343 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1344 unsigned int first, unsigned int last, struct get_message_reply *reply )
1346 struct thread_input *input = thread->queue->input;
1347 struct thread *win_thread;
1348 struct list *ptr;
1349 user_handle_t win;
1350 int clear_bits, got_one = 0;
1351 unsigned int msg_code;
1353 ptr = list_head( &input->msg_list );
1354 if (hw_id)
1356 while (ptr)
1358 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1359 if (msg->unique_id == hw_id) break;
1360 ptr = list_next( &input->msg_list, ptr );
1362 if (!ptr) ptr = list_head( &input->msg_list );
1363 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1366 if (ptr == list_head( &input->msg_list ))
1367 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1368 else
1369 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1371 while (ptr)
1373 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1374 struct hardware_msg_data *data = msg->data;
1376 ptr = list_next( &input->msg_list, ptr );
1377 win = find_hardware_message_window( input, msg, &msg_code );
1378 if (!win || !(win_thread = get_window_thread( win )))
1380 /* no window at all, remove it */
1381 update_input_key_state( input->keystate, msg );
1382 list_remove( &msg->entry );
1383 free_message( msg );
1384 continue;
1386 if (win_thread != thread)
1388 if (win_thread->queue->input == input)
1390 /* wake the other thread */
1391 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1392 got_one = 1;
1394 else
1396 /* for another thread input, drop it */
1397 update_input_key_state( input->keystate, msg );
1398 list_remove( &msg->entry );
1399 free_message( msg );
1401 release_object( win_thread );
1402 continue;
1404 release_object( win_thread );
1406 /* if we already got a message for another thread, or if it doesn't
1407 * match the filter we skip it */
1408 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1410 clear_bits &= ~get_hardware_msg_bit( msg );
1411 continue;
1413 /* now we can return it */
1414 if (!msg->unique_id) msg->unique_id = get_unique_id();
1415 reply->type = MSG_HARDWARE;
1416 reply->win = win;
1417 reply->msg = msg_code;
1418 reply->wparam = msg->wparam;
1419 reply->lparam = msg->lparam;
1420 reply->time = msg->time;
1422 data->hw_id = msg->unique_id;
1423 set_reply_data( msg->data, msg->data_size );
1424 return 1;
1426 /* nothing found, clear the hardware queue bits */
1427 clear_queue_bits( thread->queue, clear_bits );
1428 return 0;
1431 /* increment (or decrement if 'incr' is negative) the queue paint count */
1432 void inc_queue_paint_count( struct thread *thread, int incr )
1434 struct msg_queue *queue = thread->queue;
1436 assert( queue );
1438 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1440 if (queue->paint_count)
1441 set_queue_bits( queue, QS_PAINT );
1442 else
1443 clear_queue_bits( queue, QS_PAINT );
1447 /* remove all messages and timers belonging to a certain window */
1448 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1450 struct msg_queue *queue = thread->queue;
1451 struct list *ptr;
1452 int i;
1454 if (!queue) return;
1456 /* remove timers */
1458 ptr = list_head( &queue->pending_timers );
1459 while (ptr)
1461 struct list *next = list_next( &queue->pending_timers, ptr );
1462 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1463 if (timer->win == win) free_timer( queue, timer );
1464 ptr = next;
1466 ptr = list_head( &queue->expired_timers );
1467 while (ptr)
1469 struct list *next = list_next( &queue->expired_timers, ptr );
1470 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1471 if (timer->win == win) free_timer( queue, timer );
1472 ptr = next;
1475 /* remove messages */
1476 for (i = 0; i < NB_MSG_KINDS; i++)
1478 struct list *ptr, *next;
1480 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1482 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1483 if (msg->win == win) remove_queue_message( queue, msg, i );
1487 thread_input_cleanup_window( queue, win );
1490 /* post a message to a window; used by socket handling */
1491 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
1493 struct message *msg;
1494 struct thread *thread = get_window_thread( win );
1496 if (!thread) return;
1498 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1500 msg->type = MSG_POSTED;
1501 msg->win = get_user_full_handle( win );
1502 msg->msg = message;
1503 msg->wparam = wparam;
1504 msg->lparam = lparam;
1505 msg->time = get_tick_count();
1506 msg->result = NULL;
1507 msg->data = NULL;
1508 msg->data_size = 0;
1510 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1511 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1513 release_object( thread );
1516 /* post a win event */
1517 void post_win_event( struct thread *thread, unsigned int event,
1518 user_handle_t win, unsigned int object_id,
1519 unsigned int child_id, client_ptr_t hook_proc,
1520 const WCHAR *module, data_size_t module_size,
1521 user_handle_t hook)
1523 struct message *msg;
1525 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1527 struct winevent_msg_data *data;
1529 msg->type = MSG_WINEVENT;
1530 msg->win = get_user_full_handle( win );
1531 msg->msg = event;
1532 msg->wparam = object_id;
1533 msg->lparam = child_id;
1534 msg->time = get_tick_count();
1535 msg->result = NULL;
1537 if ((data = malloc( sizeof(*data) + module_size )))
1539 data->hook = hook;
1540 data->tid = get_thread_id( current );
1541 data->hook_proc = hook_proc;
1542 memcpy( data + 1, module, module_size );
1544 msg->data = data;
1545 msg->data_size = sizeof(*data) + module_size;
1547 if (debug_level > 1)
1548 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
1549 get_thread_id(thread), event, win, object_id, child_id );
1550 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1551 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1553 else
1554 free( msg );
1559 /* check if the thread owning the window is hung */
1560 DECL_HANDLER(is_window_hung)
1562 struct thread *thread;
1564 thread = get_window_thread( req->win );
1565 if (thread)
1567 reply->is_hung = is_queue_hung( thread->queue );
1568 release_object( thread );
1570 else reply->is_hung = 0;
1574 /* get the message queue of the current thread */
1575 DECL_HANDLER(get_msg_queue)
1577 struct msg_queue *queue = get_current_queue();
1579 reply->handle = 0;
1580 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1584 /* set the file descriptor associated to the current thread queue */
1585 DECL_HANDLER(set_queue_fd)
1587 struct msg_queue *queue = get_current_queue();
1588 struct file *file;
1589 int unix_fd;
1591 if (queue->fd) /* fd can only be set once */
1593 set_error( STATUS_ACCESS_DENIED );
1594 return;
1596 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
1598 if ((unix_fd = get_file_unix_fd( file )) != -1)
1600 if ((unix_fd = dup( unix_fd )) != -1)
1601 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
1602 else
1603 file_set_error();
1605 release_object( file );
1609 /* set the current message queue wakeup mask */
1610 DECL_HANDLER(set_queue_mask)
1612 struct msg_queue *queue = get_current_queue();
1614 if (queue)
1616 queue->wake_mask = req->wake_mask;
1617 queue->changed_mask = req->changed_mask;
1618 reply->wake_bits = queue->wake_bits;
1619 reply->changed_bits = queue->changed_bits;
1620 if (is_signaled( queue ))
1622 /* if skip wait is set, do what would have been done in the subsequent wait */
1623 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1624 else wake_up( &queue->obj, 0 );
1630 /* get the current message queue status */
1631 DECL_HANDLER(get_queue_status)
1633 struct msg_queue *queue = current->queue;
1634 if (queue)
1636 reply->wake_bits = queue->wake_bits;
1637 reply->changed_bits = queue->changed_bits;
1638 if (req->clear) queue->changed_bits = 0;
1640 else reply->wake_bits = reply->changed_bits = 0;
1644 /* send a message to a thread queue */
1645 DECL_HANDLER(send_message)
1647 struct message *msg;
1648 struct msg_queue *send_queue = get_current_queue();
1649 struct msg_queue *recv_queue = NULL;
1650 struct thread *thread = NULL;
1652 if (!(thread = get_thread_from_id( req->id ))) return;
1654 if (!(recv_queue = thread->queue))
1656 set_error( STATUS_INVALID_PARAMETER );
1657 release_object( thread );
1658 return;
1660 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1662 set_error( STATUS_TIMEOUT );
1663 release_object( thread );
1664 return;
1667 if ((msg = mem_alloc( sizeof(*msg) )))
1669 msg->type = req->type;
1670 msg->win = get_user_full_handle( req->win );
1671 msg->msg = req->msg;
1672 msg->wparam = req->wparam;
1673 msg->lparam = req->lparam;
1674 msg->time = get_tick_count();
1675 msg->result = NULL;
1676 msg->data = NULL;
1677 msg->data_size = get_req_data_size();
1679 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1681 free( msg );
1682 release_object( thread );
1683 return;
1686 switch(msg->type)
1688 case MSG_OTHER_PROCESS:
1689 case MSG_ASCII:
1690 case MSG_UNICODE:
1691 case MSG_CALLBACK:
1692 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
1694 free_message( msg );
1695 break;
1697 /* fall through */
1698 case MSG_NOTIFY:
1699 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1700 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1701 break;
1702 case MSG_POSTED:
1703 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1704 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1705 break;
1706 case MSG_HARDWARE: /* should use send_hardware_message instead */
1707 case MSG_CALLBACK_RESULT: /* cannot send this one */
1708 default:
1709 set_error( STATUS_INVALID_PARAMETER );
1710 free( msg );
1711 break;
1714 release_object( thread );
1717 /* send a hardware message to a thread queue */
1718 DECL_HANDLER(send_hardware_message)
1720 struct message *msg;
1721 struct thread *thread = NULL;
1722 struct desktop *desktop = NULL;
1723 struct hardware_msg_data *data;
1724 struct thread_input *input;
1726 if (req->id)
1728 if (!(thread = get_thread_from_id( req->id ))) return;
1729 if (!thread->queue)
1731 set_error( STATUS_INVALID_PARAMETER );
1732 release_object( thread );
1733 return;
1735 input = thread->queue->input;
1736 desktop = (struct desktop *)grab_object( input->desktop );
1737 reply->cursor = input->cursor;
1738 reply->count = input->cursor_count;
1740 else
1742 if (!(desktop = get_thread_desktop( current, 0 ))) return;
1743 input = desktop->foreground_input;
1746 if (!req->msg || !(data = mem_alloc( sizeof(*data) ))) goto done;
1748 memset( data, 0, sizeof(*data) );
1749 data->x = req->x;
1750 data->y = req->y;
1751 data->info = req->info;
1753 if ((msg = mem_alloc( sizeof(*msg) )))
1755 msg->type = MSG_HARDWARE;
1756 msg->win = get_user_full_handle( req->win );
1757 msg->msg = req->msg;
1758 msg->wparam = req->wparam;
1759 msg->lparam = req->lparam;
1760 msg->time = req->time;
1761 msg->result = NULL;
1762 msg->data = data;
1763 msg->data_size = sizeof(*data);
1764 queue_hardware_message( desktop, input, msg );
1766 else free( data );
1768 done:
1769 if (thread) release_object( thread );
1770 release_object( desktop );
1773 /* post a quit message to the current queue */
1774 DECL_HANDLER(post_quit_message)
1776 struct msg_queue *queue = get_current_queue();
1778 if (!queue)
1779 return;
1781 queue->quit_message = 1;
1782 queue->exit_code = req->exit_code;
1783 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1786 /* get a message from the current queue */
1787 DECL_HANDLER(get_message)
1789 struct timer *timer;
1790 struct list *ptr;
1791 struct msg_queue *queue = get_current_queue();
1792 user_handle_t get_win = get_user_full_handle( req->get_win );
1793 unsigned int filter = req->flags >> 16;
1795 reply->active_hooks = get_active_hooks();
1797 if (!queue) return;
1798 queue->last_get_msg = current_time;
1799 if (!filter) filter = QS_ALLINPUT;
1801 /* first check for sent messages */
1802 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1804 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1805 receive_message( queue, msg, reply );
1806 return;
1809 /* clear changed bits so we can wait on them if we don't find a message */
1810 if (filter & QS_POSTMESSAGE)
1812 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
1813 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
1815 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
1816 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
1818 /* then check for posted messages */
1819 if ((filter & QS_POSTMESSAGE) &&
1820 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1821 return;
1823 /* only check for quit messages if not posted messages pending.
1824 * note: the quit message isn't filtered */
1825 if (get_quit_message( queue, req->flags, reply ))
1826 return;
1828 /* then check for any raw hardware message */
1829 if ((filter & QS_INPUT) &&
1830 filter_contains_hw_range( req->get_first, req->get_last ) &&
1831 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1832 return;
1834 /* now check for WM_PAINT */
1835 if ((filter & QS_PAINT) &&
1836 queue->paint_count &&
1837 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1838 (reply->win = find_window_to_repaint( get_win, current )))
1840 reply->type = MSG_POSTED;
1841 reply->msg = WM_PAINT;
1842 reply->wparam = 0;
1843 reply->lparam = 0;
1844 reply->time = get_tick_count();
1845 return;
1848 /* now check for timer */
1849 if ((filter & QS_TIMER) &&
1850 (timer = find_expired_timer( queue, get_win, req->get_first,
1851 req->get_last, (req->flags & PM_REMOVE) )))
1853 reply->type = MSG_POSTED;
1854 reply->win = timer->win;
1855 reply->msg = timer->msg;
1856 reply->wparam = timer->id;
1857 reply->lparam = timer->lparam;
1858 reply->time = get_tick_count();
1859 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
1860 set_event( current->process->idle_event );
1861 return;
1864 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
1865 queue->wake_mask = req->wake_mask;
1866 queue->changed_mask = req->changed_mask;
1867 set_error( STATUS_PENDING ); /* FIXME */
1871 /* reply to a sent message */
1872 DECL_HANDLER(reply_message)
1874 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1875 else if (current->queue->recv_result)
1876 reply_message( current->queue, req->result, 0, req->remove,
1877 get_req_data(), get_req_data_size() );
1881 /* accept the current hardware message */
1882 DECL_HANDLER(accept_hardware_message)
1884 if (current->queue)
1885 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1886 else
1887 set_error( STATUS_ACCESS_DENIED );
1891 /* retrieve the reply for the last message sent */
1892 DECL_HANDLER(get_message_reply)
1894 struct message_result *result;
1895 struct list *entry;
1896 struct msg_queue *queue = current->queue;
1898 if (queue)
1900 set_error( STATUS_PENDING );
1901 reply->result = 0;
1903 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1905 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1906 if (result->replied || req->cancel)
1908 if (result->replied)
1910 reply->result = result->result;
1911 set_error( result->error );
1912 if (result->data)
1914 data_size_t data_len = min( result->data_size, get_reply_max_size() );
1915 set_reply_data_ptr( result->data, data_len );
1916 result->data = NULL;
1917 result->data_size = 0;
1920 remove_result_from_sender( result );
1922 entry = list_head( &queue->send_result );
1923 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1924 else
1926 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1927 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1931 else set_error( STATUS_ACCESS_DENIED );
1935 /* set a window timer */
1936 DECL_HANDLER(set_win_timer)
1938 struct timer *timer;
1939 struct msg_queue *queue;
1940 struct thread *thread = NULL;
1941 user_handle_t win = 0;
1942 lparam_t id = req->id;
1944 if (req->win)
1946 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1948 set_error( STATUS_INVALID_HANDLE );
1949 return;
1951 if (thread->process != current->process)
1953 release_object( thread );
1954 set_error( STATUS_ACCESS_DENIED );
1955 return;
1957 queue = thread->queue;
1958 /* remove it if it existed already */
1959 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1961 else
1963 queue = get_current_queue();
1964 /* look for a timer with this id */
1965 if (id && (timer = find_timer( queue, 0, req->msg, id )))
1967 /* free and reuse id */
1968 free_timer( queue, timer );
1970 else
1972 /* find a free id for it */
1975 id = queue->next_timer_id;
1976 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
1978 while (find_timer( queue, 0, req->msg, id ));
1982 if ((timer = set_timer( queue, req->rate )))
1984 timer->win = win;
1985 timer->msg = req->msg;
1986 timer->id = id;
1987 timer->lparam = req->lparam;
1988 reply->id = id;
1990 if (thread) release_object( thread );
1993 /* kill a window timer */
1994 DECL_HANDLER(kill_win_timer)
1996 struct timer *timer;
1997 struct thread *thread;
1998 user_handle_t win = 0;
2000 if (req->win)
2002 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2004 set_error( STATUS_INVALID_HANDLE );
2005 return;
2007 if (thread->process != current->process)
2009 release_object( thread );
2010 set_error( STATUS_ACCESS_DENIED );
2011 return;
2014 else thread = (struct thread *)grab_object( current );
2016 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2017 free_timer( thread->queue, timer );
2018 else
2019 set_error( STATUS_INVALID_PARAMETER );
2021 release_object( thread );
2025 /* attach (or detach) thread inputs */
2026 DECL_HANDLER(attach_thread_input)
2028 struct thread *thread_from = get_thread_from_id( req->tid_from );
2029 struct thread *thread_to = get_thread_from_id( req->tid_to );
2031 if (!thread_from || !thread_to)
2033 if (thread_from) release_object( thread_from );
2034 if (thread_to) release_object( thread_to );
2035 return;
2037 if (thread_from != thread_to)
2039 if (req->attach) attach_thread_input( thread_from, thread_to );
2040 else
2042 if (thread_from->queue && thread_to->queue &&
2043 thread_from->queue->input == thread_to->queue->input)
2044 detach_thread_input( thread_from );
2045 else
2046 set_error( STATUS_ACCESS_DENIED );
2049 else set_error( STATUS_ACCESS_DENIED );
2050 release_object( thread_from );
2051 release_object( thread_to );
2055 /* get thread input data */
2056 DECL_HANDLER(get_thread_input)
2058 struct thread *thread = NULL;
2059 struct desktop *desktop;
2060 struct thread_input *input;
2062 if (req->tid)
2064 if (!(thread = get_thread_from_id( req->tid ))) return;
2065 if (!(desktop = get_thread_desktop( thread, 0 )))
2067 release_object( thread );
2068 return;
2070 input = thread->queue ? thread->queue->input : NULL;
2072 else
2074 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2075 input = desktop->foreground_input; /* get the foreground thread info */
2078 if (input)
2080 reply->focus = input->focus;
2081 reply->capture = input->capture;
2082 reply->active = input->active;
2083 reply->menu_owner = input->menu_owner;
2084 reply->move_size = input->move_size;
2085 reply->caret = input->caret;
2086 reply->cursor = input->cursor;
2087 reply->show_count = input->cursor_count;
2088 reply->rect = input->caret_rect;
2091 /* foreground window is active window of foreground thread */
2092 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2093 if (thread) release_object( thread );
2094 release_object( desktop );
2098 /* retrieve queue keyboard state for a given thread */
2099 DECL_HANDLER(get_key_state)
2101 struct thread *thread;
2102 struct desktop *desktop;
2103 data_size_t size = min( 256, get_reply_max_size() );
2105 if (!req->tid) /* get global async key state */
2107 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2108 if (req->key >= 0) reply->state = desktop->keystate[req->key & 0xff];
2109 set_reply_data( desktop->keystate, size );
2110 release_object( desktop );
2112 else
2114 if (!(thread = get_thread_from_id( req->tid ))) return;
2115 if (thread->queue)
2117 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2118 set_reply_data( thread->queue->input->keystate, size );
2120 release_object( thread );
2125 /* set queue keyboard state for a given thread */
2126 DECL_HANDLER(set_key_state)
2128 struct thread *thread;
2129 struct desktop *desktop;
2130 data_size_t size = min( 256, get_req_data_size() );
2132 if (!req->tid) /* set global async key state */
2134 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2135 memcpy( desktop->keystate, get_req_data(), size );
2136 release_object( desktop );
2138 else
2140 if (!(thread = get_thread_from_id( req->tid ))) return;
2141 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
2142 release_object( thread );
2147 /* set the system foreground window */
2148 DECL_HANDLER(set_foreground_window)
2150 struct thread *thread = NULL;
2151 struct desktop *desktop;
2152 struct msg_queue *queue = get_current_queue();
2154 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2155 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
2156 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
2157 reply->send_msg_new = FALSE;
2159 if (is_top_level_window( req->handle ) &&
2160 ((thread = get_window_thread( req->handle ))) &&
2161 (thread->queue->input->desktop == desktop))
2163 desktop->foreground_input = thread->queue->input;
2164 reply->send_msg_new = (desktop->foreground_input != queue->input);
2166 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2168 if (thread) release_object( thread );
2169 release_object( desktop );
2173 /* set the current thread focus window */
2174 DECL_HANDLER(set_focus_window)
2176 struct msg_queue *queue = get_current_queue();
2178 reply->previous = 0;
2179 if (queue && check_queue_input_window( queue, req->handle ))
2181 reply->previous = queue->input->focus;
2182 queue->input->focus = get_user_full_handle( req->handle );
2187 /* set the current thread active window */
2188 DECL_HANDLER(set_active_window)
2190 struct msg_queue *queue = get_current_queue();
2192 reply->previous = 0;
2193 if (queue && check_queue_input_window( queue, req->handle ))
2195 if (!req->handle || make_window_active( req->handle ))
2197 reply->previous = queue->input->active;
2198 queue->input->active = get_user_full_handle( req->handle );
2200 else set_error( STATUS_INVALID_HANDLE );
2205 /* set the current thread capture window */
2206 DECL_HANDLER(set_capture_window)
2208 struct msg_queue *queue = get_current_queue();
2210 reply->previous = reply->full_handle = 0;
2211 if (queue && check_queue_input_window( queue, req->handle ))
2213 struct thread_input *input = queue->input;
2215 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2216 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2218 set_error(STATUS_ACCESS_DENIED);
2219 return;
2221 reply->previous = input->capture;
2222 input->capture = get_user_full_handle( req->handle );
2223 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2224 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2225 reply->full_handle = input->capture;
2230 /* Set the current thread caret window */
2231 DECL_HANDLER(set_caret_window)
2233 struct msg_queue *queue = get_current_queue();
2235 reply->previous = 0;
2236 if (queue && check_queue_input_window( queue, req->handle ))
2238 struct thread_input *input = queue->input;
2240 reply->previous = input->caret;
2241 reply->old_rect = input->caret_rect;
2242 reply->old_hide = input->caret_hide;
2243 reply->old_state = input->caret_state;
2245 set_caret_window( input, get_user_full_handle(req->handle) );
2246 input->caret_rect.right = input->caret_rect.left + req->width;
2247 input->caret_rect.bottom = input->caret_rect.top + req->height;
2252 /* Set the current thread caret information */
2253 DECL_HANDLER(set_caret_info)
2255 struct msg_queue *queue = get_current_queue();
2256 struct thread_input *input;
2258 if (!queue) return;
2259 input = queue->input;
2260 reply->full_handle = input->caret;
2261 reply->old_rect = input->caret_rect;
2262 reply->old_hide = input->caret_hide;
2263 reply->old_state = input->caret_state;
2265 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2267 set_error( STATUS_ACCESS_DENIED );
2268 return;
2270 if (req->flags & SET_CARET_POS)
2272 input->caret_rect.right += req->x - input->caret_rect.left;
2273 input->caret_rect.bottom += req->y - input->caret_rect.top;
2274 input->caret_rect.left = req->x;
2275 input->caret_rect.top = req->y;
2277 if (req->flags & SET_CARET_HIDE)
2279 input->caret_hide += req->hide;
2280 if (input->caret_hide < 0) input->caret_hide = 0;
2282 if (req->flags & SET_CARET_STATE)
2284 if (req->state == -1) input->caret_state = !input->caret_state;
2285 else input->caret_state = !!req->state;
2290 /* get the time of the last input event */
2291 DECL_HANDLER(get_last_input_time)
2293 reply->time = last_input_time;
2296 /* set/get the current cursor */
2297 DECL_HANDLER(set_cursor)
2299 struct msg_queue *queue = get_current_queue();
2300 struct thread_input *input;
2302 if (!queue) return;
2303 input = queue->input;
2305 reply->prev_handle = input->cursor;
2306 reply->prev_count = input->cursor_count;
2308 if (req->flags & SET_CURSOR_HANDLE)
2310 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
2312 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
2313 return;
2315 input->cursor = req->handle;
2317 if (req->flags & SET_CURSOR_COUNT)
2319 queue->cursor_count += req->show_count;
2320 input->cursor_count += req->show_count;
2322 if (req->flags & SET_CURSOR_POS)
2324 set_cursor_pos( input->desktop, req->x, req->y );
2326 if (req->flags & SET_CURSOR_CLIP)
2328 rectangle_t top_rect;
2329 get_top_window_rectangle( input->desktop, &top_rect );
2330 if (!intersect_rect( &input->desktop->cursor_clip, &top_rect, &req->clip ))
2331 input->desktop->cursor_clip = top_rect;
2334 reply->new_x = input->desktop->cursor_x;
2335 reply->new_y = input->desktop->cursor_y;
2336 reply->new_clip = input->desktop->cursor_clip;