2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
39 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
40 #define NB_MSG_KINDS (POST_MESSAGE+1)
45 struct message_result *send_next; /* next in sender list */
46 struct message_result *recv_next; /* next in receiver list */
47 struct msg_queue *sender; /* sender queue */
48 struct msg_queue *receiver; /* receiver queue */
49 int replied; /* has it been replied to? */
50 unsigned int result; /* reply result */
51 unsigned int error; /* error code to pass back to sender */
52 void *data; /* message reply data */
53 unsigned int data_size; /* size of message reply data */
54 struct timeout_user *timeout; /* result timeout */
59 struct message *next; /* next message in list */
60 struct message *prev; /* prev message in list */
61 enum message_type type; /* message type */
62 user_handle_t win; /* window handle */
63 unsigned int msg; /* message code */
64 unsigned int wparam; /* parameters */
65 unsigned int lparam; /* parameters */
66 int x; /* x position */
67 int y; /* y position */
68 unsigned int time; /* message time */
69 unsigned int info; /* extra info */
70 void *data; /* message data for sent messages */
71 unsigned int data_size; /* size of message data */
72 struct message_result *result; /* result in sender queue */
77 struct message *first; /* head of list */
78 struct message *last; /* tail of list */
83 struct timer *next; /* next timer in list */
84 struct timer *prev; /* prev timer in list */
85 struct timeval 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 unsigned int id; /* timer id */
90 unsigned int lparam; /* lparam for message */
95 struct object obj; /* object header */
96 user_handle_t focus; /* focus window */
97 user_handle_t capture; /* capture window */
98 user_handle_t active; /* active window */
99 user_handle_t menu_owner; /* current menu owner window */
100 user_handle_t move_size; /* current moving/resizing window */
101 user_handle_t caret; /* caret window */
102 rectangle_t caret_rect; /* caret rectangle */
103 int caret_hide; /* caret hide count */
104 int caret_state; /* caret on/off state */
105 struct message *msg; /* message currently processed */
106 struct thread *msg_thread; /* thread processing the message */
107 struct message_list msg_list; /* list of hardware messages */
108 unsigned char keystate[256]; /* state of each key */
113 struct object obj; /* object header */
114 unsigned int wake_bits; /* wakeup bits */
115 unsigned int wake_mask; /* wakeup mask */
116 unsigned int changed_bits; /* changed wakeup bits */
117 unsigned int changed_mask; /* changed wakeup mask */
118 int paint_count; /* pending paint messages count */
119 struct message_list msg_list[NB_MSG_KINDS]; /* lists of messages */
120 struct message_result *send_result; /* stack of sent messages waiting for result */
121 struct message_result *recv_result; /* stack of received messages waiting for result */
122 struct timer *first_timer; /* head of timer list */
123 struct timer *last_timer; /* tail of timer list */
124 struct timer *next_timer; /* next timer to expire */
125 struct timeout_user *timeout; /* timeout for next timer to expire */
126 struct thread_input *input; /* thread input descriptor */
127 struct hook_table *hooks; /* hook table */
130 static void msg_queue_dump( struct object *obj, int verbose );
131 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
132 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
133 static int msg_queue_signaled( struct object *obj, struct thread *thread );
134 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
135 static void msg_queue_destroy( struct object *obj );
136 static void thread_input_dump( struct object *obj, int verbose );
137 static void thread_input_destroy( struct object *obj );
138 static void timer_callback( void *private );
140 static const struct object_ops msg_queue_ops =
142 sizeof(struct msg_queue), /* size */
143 msg_queue_dump, /* dump */
144 msg_queue_add_queue, /* add_queue */
145 msg_queue_remove_queue, /* remove_queue */
146 msg_queue_signaled, /* signaled */
147 msg_queue_satisfied, /* satisfied */
148 no_get_fd, /* get_fd */
149 msg_queue_destroy /* destroy */
153 static const struct object_ops thread_input_ops =
155 sizeof(struct thread_input), /* size */
156 thread_input_dump, /* dump */
157 no_add_queue, /* add_queue */
158 NULL, /* remove_queue */
160 NULL, /* satisfied */
161 no_get_fd, /* get_fd */
162 thread_input_destroy /* destroy */
165 /* pointer to input structure of foreground thread */
166 static struct thread_input *foreground_input;
169 /* set the caret window in a given thread input */
170 static void set_caret_window( struct thread_input *input, user_handle_t win )
173 input->caret_rect.left = 0;
174 input->caret_rect.top = 0;
175 input->caret_rect.right = 0;
176 input->caret_rect.bottom = 0;
177 input->caret_hide = 1;
178 input->caret_state = 0;
181 /* create a thread input object */
182 static struct thread_input *create_thread_input(void)
184 struct thread_input *input;
186 if ((input = alloc_object( &thread_input_ops )))
191 input->menu_owner = 0;
192 input->move_size = 0;
194 input->msg_thread = NULL;
195 input->msg_list.first = input->msg_list.last = NULL;
196 set_caret_window( input, 0 );
197 memset( input->keystate, 0, sizeof(input->keystate) );
202 /* create a message queue object */
203 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
205 struct msg_queue *queue;
208 if (!input && !(input = create_thread_input())) return NULL;
209 if ((queue = alloc_object( &msg_queue_ops )))
211 queue->wake_bits = 0;
212 queue->wake_mask = 0;
213 queue->changed_bits = 0;
214 queue->changed_mask = 0;
215 queue->paint_count = 0;
216 queue->send_result = NULL;
217 queue->recv_result = NULL;
218 queue->first_timer = NULL;
219 queue->last_timer = NULL;
220 queue->next_timer = NULL;
221 queue->timeout = NULL;
222 queue->input = (struct thread_input *)grab_object( input );
224 for (i = 0; i < NB_MSG_KINDS; i++)
225 queue->msg_list[i].first = queue->msg_list[i].last = NULL;
227 thread->queue = queue;
228 if (!thread->process->queue)
229 thread->process->queue = (struct msg_queue *)grab_object( queue );
231 release_object( input );
235 /* free the message queue of a thread at thread exit */
236 void free_msg_queue( struct thread *thread )
238 struct process *process = thread->process;
240 if (!thread->queue) return;
241 if (process->queue == thread->queue) /* is it the process main queue? */
243 release_object( process->queue );
244 process->queue = NULL;
245 if (process->idle_event)
247 set_event( process->idle_event );
248 release_object( process->idle_event );
249 process->idle_event = NULL;
252 release_object( thread->queue );
253 thread->queue = NULL;
256 /* get the hook table for a given thread */
257 struct hook_table *get_queue_hooks( struct thread *thread )
259 if (!thread->queue) return NULL;
260 return thread->queue->hooks;
263 /* set the hook table for a given thread, allocating the queue if needed */
264 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
266 struct msg_queue *queue = thread->queue;
267 if (!queue) queue = create_msg_queue( thread, NULL );
268 if (queue->hooks) release_object( queue->hooks );
269 queue->hooks = hooks;
272 /* check the queue status */
273 inline static int is_signaled( struct msg_queue *queue )
275 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
278 /* set some queue bits */
279 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
281 queue->wake_bits |= bits;
282 queue->changed_bits |= bits;
283 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
286 /* clear some queue bits */
287 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
289 queue->wake_bits &= ~bits;
290 queue->changed_bits &= ~bits;
293 /* check whether msg is a keyboard message */
294 inline static int is_keyboard_msg( struct message *msg )
296 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
299 /* get the QS_* bit corresponding to a given hardware message */
300 inline static int get_hardware_msg_bit( struct message *msg )
302 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
303 if (is_keyboard_msg( msg )) return QS_KEY;
304 return QS_MOUSEBUTTON;
307 /* get the current thread queue, creating it if needed */
308 inline static struct msg_queue *get_current_queue(void)
310 struct msg_queue *queue = current->queue;
311 if (!queue) queue = create_msg_queue( current, NULL );
315 /* append a message to the end of a list */
316 inline static void append_message( struct message_list *list, struct message *msg )
319 if ((msg->prev = list->last)) msg->prev->next = msg;
320 else list->first = msg;
324 /* unlink a message from a list it */
325 inline static void unlink_message( struct message_list *list, struct message *msg )
327 if (msg->next) msg->next->prev = msg->prev;
328 else list->last = msg->prev;
329 if (msg->prev) msg->prev->next = msg->next;
330 else list->first = msg->next;
333 /* try to merge a message with the last in the list; return 1 if successful */
334 static int merge_message( struct thread_input *input, const struct message *msg )
336 struct message *prev = input->msg_list.last;
339 if (input->msg == prev) return 0;
340 if (prev->result) return 0;
341 if (prev->win != msg->win) return 0;
342 if (prev->msg != msg->msg) return 0;
343 if (prev->type != msg->type) return 0;
344 /* now we can merge it */
345 prev->wparam = msg->wparam;
346 prev->lparam = msg->lparam;
349 prev->time = msg->time;
350 prev->info = msg->info;
354 /* free a result structure */
355 static void free_result( struct message_result *result )
357 if (result->timeout) remove_timeout_user( result->timeout );
358 if (result->data) free( result->data );
362 /* store the message result in the appropriate structure */
363 static void store_message_result( struct message_result *res, unsigned int result,
366 res->result = result;
371 remove_timeout_user( res->timeout );
374 /* wake sender queue if waiting on this result */
375 if (res->sender && res->sender->send_result == res)
376 set_queue_bits( res->sender, QS_SMRESULT );
379 /* free a message when deleting a queue or window */
380 static void free_message( struct message *msg )
382 struct message_result *result = msg->result;
387 result->receiver = NULL;
388 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
390 else free_result( result );
392 if (msg->data) free( msg->data );
396 /* remove (and free) a message from a message list */
397 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
398 enum message_kind kind )
400 unlink_message( &queue->msg_list[kind], msg );
404 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_SENDMESSAGE );
407 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_POSTMESSAGE );
413 /* message timed out without getting a reply */
414 static void result_timeout( void *private )
416 struct message_result *result = private;
418 assert( !result->replied );
420 result->timeout = NULL;
421 store_message_result( result, 0, STATUS_TIMEOUT );
424 /* allocate and fill a message result structure */
425 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
426 struct msg_queue *recv_queue,
427 unsigned int timeout )
429 struct message_result *result = mem_alloc( sizeof(*result) );
432 /* put the result on the sender result stack */
433 result->sender = send_queue;
434 result->receiver = recv_queue;
437 result->data_size = 0;
438 result->timeout = NULL;
439 result->send_next = send_queue->send_result;
440 send_queue->send_result = result;
444 gettimeofday( &when, 0 );
445 add_timeout( &when, timeout );
446 result->timeout = add_timeout_user( &when, result_timeout, result );
452 /* receive a message, removing it from the sent queue */
453 static void receive_message( struct msg_queue *queue, struct message *msg,
454 struct get_message_reply *reply )
456 struct message_result *result = msg->result;
458 reply->total = msg->data_size;
459 if (msg->data_size > get_reply_max_size())
461 set_error( STATUS_BUFFER_OVERFLOW );
464 reply->type = msg->type;
465 reply->win = msg->win;
466 reply->msg = msg->msg;
467 reply->wparam = msg->wparam;
468 reply->lparam = msg->lparam;
471 reply->time = msg->time;
472 reply->info = msg->info;
474 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
476 unlink_message( &queue->msg_list[SEND_MESSAGE], msg );
477 /* put the result on the receiver result stack */
480 result->recv_next = queue->recv_result;
481 queue->recv_result = result;
484 if (!queue->msg_list[SEND_MESSAGE].first) clear_queue_bits( queue, QS_SENDMESSAGE );
487 /* set the result of the current received message */
488 static void reply_message( struct msg_queue *queue, unsigned int result,
489 unsigned int error, int remove, const void *data, size_t len )
491 struct message_result *res = queue->recv_result;
495 queue->recv_result = res->recv_next;
496 res->receiver = NULL;
497 if (!res->sender) /* no one waiting for it */
505 if (len && (res->data = memdup( data, len ))) res->data_size = len;
506 store_message_result( res, result, error );
510 /* retrieve a posted message */
511 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
512 unsigned int first, unsigned int last, unsigned int flags,
513 struct get_message_reply *reply )
516 struct message_list *list = &queue->msg_list[POST_MESSAGE];
518 /* check against the filters */
519 for (msg = list->first; msg; msg = msg->next)
521 if (msg->msg == WM_QUIT) break; /* WM_QUIT is never filtered */
522 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
523 if (msg->msg < first) continue;
524 if (msg->msg > last) continue;
525 break; /* found one */
529 /* return it to the app */
531 reply->total = msg->data_size;
532 if (msg->data_size > get_reply_max_size())
534 set_error( STATUS_BUFFER_OVERFLOW );
537 reply->type = msg->type;
538 reply->win = msg->win;
539 reply->msg = msg->msg;
540 reply->wparam = msg->wparam;
541 reply->lparam = msg->lparam;
544 reply->time = msg->time;
545 reply->info = msg->info;
547 if (flags & GET_MSG_REMOVE)
551 set_reply_data_ptr( msg->data, msg->data_size );
555 remove_queue_message( queue, msg, POST_MESSAGE );
557 else if (msg->data) set_reply_data( msg->data, msg->data_size );
562 /* empty a message list and free all the messages */
563 static void empty_msg_list( struct message_list *list )
565 struct message *msg = list->first;
568 struct message *next = msg->next;
574 /* cleanup all pending results when deleting a queue */
575 static void cleanup_results( struct msg_queue *queue )
577 struct message_result *result, *next;
579 result = queue->send_result;
582 next = result->send_next;
583 result->sender = NULL;
584 if (!result->receiver) free_result( result );
588 while (queue->recv_result)
589 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
592 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
594 struct msg_queue *queue = (struct msg_queue *)obj;
595 struct process *process = entry->thread->process;
597 /* a thread can only wait on its own queue */
598 if (entry->thread->queue != queue)
600 set_error( STATUS_ACCESS_DENIED );
603 /* if waiting on the main process queue, set the idle event */
604 if (process->queue == queue)
606 if (process->idle_event) set_event( process->idle_event );
608 add_queue( obj, entry );
612 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
614 struct msg_queue *queue = (struct msg_queue *)obj;
615 struct process *process = entry->thread->process;
617 remove_queue( obj, entry );
619 assert( entry->thread->queue == queue );
621 /* if waiting on the main process queue, reset the idle event */
622 if (process->queue == queue)
624 if (process->idle_event) reset_event( process->idle_event );
628 static void msg_queue_dump( struct object *obj, int verbose )
630 struct msg_queue *queue = (struct msg_queue *)obj;
631 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
632 queue->wake_bits, queue->wake_mask );
635 static int msg_queue_signaled( struct object *obj, struct thread *thread )
637 struct msg_queue *queue = (struct msg_queue *)obj;
638 return is_signaled( queue );
641 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
643 struct msg_queue *queue = (struct msg_queue *)obj;
644 queue->wake_mask = 0;
645 queue->changed_mask = 0;
646 return 0; /* Not abandoned */
649 static void msg_queue_destroy( struct object *obj )
651 struct msg_queue *queue = (struct msg_queue *)obj;
652 struct timer *timer = queue->first_timer;
655 cleanup_results( queue );
656 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
660 struct timer *next = timer->next;
664 if (queue->timeout) remove_timeout_user( queue->timeout );
665 if (queue->input) release_object( queue->input );
666 if (queue->hooks) release_object( queue->hooks );
669 static void thread_input_dump( struct object *obj, int verbose )
671 struct thread_input *input = (struct thread_input *)obj;
672 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
673 input->focus, input->capture, input->active );
676 static void thread_input_destroy( struct object *obj )
678 struct thread_input *input = (struct thread_input *)obj;
680 if (foreground_input == input) foreground_input = NULL;
681 if (input->msg_thread) release_object( input->msg_thread );
682 empty_msg_list( &input->msg_list );
685 /* fix the thread input data when a window is destroyed */
686 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
688 struct thread_input *input = queue->input;
690 if (window == input->focus) input->focus = 0;
691 if (window == input->capture) input->capture = 0;
692 if (window == input->active) input->active = 0;
693 if (window == input->menu_owner) input->menu_owner = 0;
694 if (window == input->move_size) input->move_size = 0;
695 if (window == input->caret) set_caret_window( input, 0 );
698 /* check if the specified window can be set in the input data of a given queue */
699 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
701 struct thread *thread;
704 if (!window) return 1; /* we can always clear the data */
706 if ((thread = get_window_thread( window )))
708 ret = (queue->input == thread->queue->input);
709 if (!ret) set_error( STATUS_ACCESS_DENIED );
710 release_object( thread );
712 else set_error( STATUS_INVALID_HANDLE );
717 /* attach two thread input data structures */
718 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
720 struct thread_input *input;
722 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
723 input = (struct thread_input *)grab_object( thread_to->queue->input );
725 if (thread_from->queue)
727 release_object( thread_from->queue->input );
728 thread_from->queue->input = input;
732 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
734 memset( input->keystate, 0, sizeof(input->keystate) );
738 /* detach two thread input data structures */
739 static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
741 struct thread_input *input;
743 if (!thread_from->queue || !thread_to->queue ||
744 thread_from->queue->input != thread_to->queue->input)
746 set_error( STATUS_ACCESS_DENIED );
749 if ((input = create_thread_input()))
751 release_object( thread_from->queue->input );
752 thread_from->queue->input = input;
757 /* set the next timer to expire */
758 static void set_next_timer( struct msg_queue *queue, struct timer *timer )
762 remove_timeout_user( queue->timeout );
763 queue->timeout = NULL;
765 if ((queue->next_timer = timer))
766 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
768 /* set/clear QS_TIMER bit */
769 if (queue->next_timer == queue->first_timer)
770 clear_queue_bits( queue, QS_TIMER );
772 set_queue_bits( queue, QS_TIMER );
775 /* callback for the next timer expiration */
776 static void timer_callback( void *private )
778 struct msg_queue *queue = private;
780 queue->timeout = NULL;
781 /* move on to the next timer */
782 set_next_timer( queue, queue->next_timer->next );
785 /* link a timer at its rightful place in the queue list */
786 static void link_timer( struct msg_queue *queue, struct timer *timer )
788 struct timer *pos = queue->next_timer;
790 while (pos && time_before( &pos->when, &timer->when )) pos = pos->next;
792 if (pos) /* insert before pos */
794 if ((timer->prev = pos->prev)) timer->prev->next = timer;
795 else queue->first_timer = timer;
799 else /* insert at end */
802 timer->prev = queue->last_timer;
803 if (queue->last_timer) queue->last_timer->next = timer;
804 else queue->first_timer = timer;
805 queue->last_timer = timer;
807 /* check if we replaced the next timer */
808 if (pos == queue->next_timer) set_next_timer( queue, timer );
811 /* remove a timer from the queue timer list */
812 static void unlink_timer( struct msg_queue *queue, struct timer *timer )
814 if (timer->next) timer->next->prev = timer->prev;
815 else queue->last_timer = timer->prev;
816 if (timer->prev) timer->prev->next = timer->next;
817 else queue->first_timer = timer->next;
818 /* check if we removed the next timer */
819 if (queue->next_timer == timer) set_next_timer( queue, timer->next );
820 else if (queue->next_timer == queue->first_timer) clear_queue_bits( queue, QS_TIMER );
823 /* restart an expired timer */
824 static void restart_timer( struct msg_queue *queue, struct timer *timer )
827 unlink_timer( queue, timer );
828 gettimeofday( &now, 0 );
829 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
830 link_timer( queue, timer );
833 /* find an expired timer matching the filtering parameters */
834 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
835 unsigned int get_first, unsigned int get_last,
839 for (timer = queue->first_timer; (timer && timer != queue->next_timer); timer = timer->next)
841 if (win && timer->win != win) continue;
842 if (timer->msg >= get_first && timer->msg <= get_last)
844 if (remove) restart_timer( queue, timer );
852 static int kill_timer( struct msg_queue *queue, user_handle_t win,
853 unsigned int msg, unsigned int id )
857 for (timer = queue->first_timer; timer; timer = timer->next)
859 if (timer->win != win || timer->msg != msg || timer->id != id) continue;
860 unlink_timer( queue, timer );
868 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
870 struct timer *timer = mem_alloc( sizeof(*timer) );
874 gettimeofday( &timer->when, 0 );
875 add_timeout( &timer->when, rate );
876 link_timer( queue, timer );
881 /* change the input key state for a given key */
882 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
886 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
887 input->keystate[key] |= 0x80;
889 else input->keystate[key] &= ~0x80;
892 /* update the input key state for a keyboard message */
893 static void update_input_key_state( struct thread_input *input, const struct message *msg )
896 int down = 0, extended;
904 set_input_key_state( input, VK_LBUTTON, down );
910 set_input_key_state( input, VK_MBUTTON, down );
916 set_input_key_state( input, VK_RBUTTON, down );
924 key = (unsigned char)msg->wparam;
925 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
926 set_input_key_state( input, key, down );
930 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
933 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
936 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
943 /* release the hardware message currently being processed by the given thread */
944 static void release_hardware_message( struct thread *thread, int remove )
946 struct thread_input *input = thread->queue->input;
948 if (input->msg_thread != thread) return;
951 struct message *other;
954 update_input_key_state( input, input->msg );
955 unlink_message( &input->msg_list, input->msg );
956 clr_bit = get_hardware_msg_bit( input->msg );
957 for (other = input->msg_list.first; other; other = other->next)
958 if (get_hardware_msg_bit( other ) == clr_bit) break;
959 if (!other) clear_queue_bits( thread->queue, clr_bit );
960 free_message( input->msg );
962 release_object( input->msg_thread );
964 input->msg_thread = NULL;
967 /* find the window that should receive a given hardware message */
968 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
969 unsigned int *msg_code )
971 user_handle_t win = 0;
973 *msg_code = msg->msg;
974 if (is_keyboard_msg( msg ))
976 if (input && !(win = input->focus))
979 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
982 else /* mouse message */
984 if (!input || !(win = input->capture))
986 if (!(win = msg->win)) win = window_from_point( msg->x, msg->y );
992 /* queue a hardware message into a given thread input */
993 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
996 struct thread *thread;
997 struct thread_input *input;
998 unsigned int msg_code;
1000 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1001 if (!win || !(thread = get_window_thread(win)))
1006 input = thread->queue->input;
1008 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1011 append_message( &input->msg_list, msg );
1012 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1014 release_object( thread );
1017 /* find a hardware message for the given queue */
1018 static int get_hardware_message( struct thread *thread, struct message *first,
1019 user_handle_t filter_win, struct get_message_reply *reply )
1021 struct thread_input *input = thread->queue->input;
1022 struct thread *win_thread;
1023 struct message *msg;
1025 int clear_bits, got_one = 0;
1026 unsigned int msg_code;
1028 if (input->msg_thread && input->msg_thread != thread)
1029 return 0; /* locked by another thread */
1033 msg = input->msg_list.first;
1034 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1039 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1044 win = find_hardware_message_window( input, msg, &msg_code );
1045 if (!win || !(win_thread = get_window_thread( win )))
1047 /* no window at all, remove it */
1048 struct message *next = msg->next;
1049 update_input_key_state( input, msg );
1050 unlink_message( &input->msg_list, msg );
1051 free_message( msg );
1055 if (win_thread != thread)
1057 /* wake the other thread */
1058 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1059 release_object( win_thread );
1064 /* if we already got a message for another thread, or if it doesn't
1065 * match the filter we skip it (filter is only checked for keyboard
1066 * messages since the dest window for a mouse message depends on hittest)
1069 (filter_win && is_keyboard_msg(msg) &&
1070 win != filter_win && !is_child_window( filter_win, win )))
1072 clear_bits &= ~get_hardware_msg_bit( msg );
1073 release_object( win_thread );
1077 /* now we can return it */
1078 if (!input->msg_thread) input->msg_thread = win_thread;
1079 else release_object( win_thread );
1082 reply->type = MSG_HARDWARE;
1084 reply->msg = msg_code;
1085 reply->wparam = msg->wparam;
1086 reply->lparam = msg->lparam;
1089 reply->time = msg->time;
1090 reply->info = msg->info;
1093 /* nothing found, clear the hardware queue bits */
1094 clear_queue_bits( thread->queue, clear_bits );
1095 if (input->msg_thread) release_object( input->msg_thread );
1097 input->msg_thread = NULL;
1101 /* increment (or decrement if 'incr' is negative) the queue paint count */
1102 void inc_queue_paint_count( struct thread *thread, int incr )
1104 struct msg_queue *queue = thread->queue;
1108 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1110 if (queue->paint_count)
1111 set_queue_bits( queue, QS_PAINT );
1113 clear_queue_bits( queue, QS_PAINT );
1117 /* remove all messages and timers belonging to a certain window */
1118 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1120 struct msg_queue *queue = thread->queue;
1121 struct timer *timer;
1122 struct message *msg;
1128 timer = queue->first_timer;
1131 struct timer *next = timer->next;
1132 if (timer->win == win)
1134 unlink_timer( queue, timer );
1140 /* remove messages */
1141 for (i = 0; i < NB_MSG_KINDS; i++)
1143 msg = queue->msg_list[i].first;
1146 struct message *next = msg->next;
1147 if (msg->win == win) remove_queue_message( queue, msg, i );
1152 thread_input_cleanup_window( queue, win );
1155 /* post a message to a window; used by socket handling */
1156 void post_message( user_handle_t win, unsigned int message,
1157 unsigned int wparam, unsigned int lparam )
1159 struct message *msg;
1160 struct thread *thread = get_window_thread( win );
1162 if (!thread) return;
1164 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1166 msg->type = MSG_POSTED;
1167 msg->win = get_user_full_handle( win );
1169 msg->wparam = wparam;
1170 msg->lparam = lparam;
1171 msg->time = get_tick_count();
1179 append_message( &thread->queue->msg_list[POST_MESSAGE], msg );
1180 set_queue_bits( thread->queue, QS_POSTMESSAGE );
1182 release_object( thread );
1186 /* get the message queue of the current thread */
1187 DECL_HANDLER(get_msg_queue)
1189 struct msg_queue *queue = get_current_queue();
1192 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1196 /* set the current message queue wakeup mask */
1197 DECL_HANDLER(set_queue_mask)
1199 struct msg_queue *queue = get_current_queue();
1203 queue->wake_mask = req->wake_mask;
1204 queue->changed_mask = req->changed_mask;
1205 reply->wake_bits = queue->wake_bits;
1206 reply->changed_bits = queue->changed_bits;
1207 if (is_signaled( queue ))
1209 /* if skip wait is set, do what would have been done in the subsequent wait */
1210 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1211 else wake_up( &queue->obj, 0 );
1217 /* get the current message queue status */
1218 DECL_HANDLER(get_queue_status)
1220 struct msg_queue *queue = current->queue;
1223 reply->wake_bits = queue->wake_bits;
1224 reply->changed_bits = queue->changed_bits;
1225 if (req->clear) queue->changed_bits = 0;
1227 else reply->wake_bits = reply->changed_bits = 0;
1231 /* send a message to a thread queue */
1232 DECL_HANDLER(send_message)
1234 struct message *msg;
1235 struct msg_queue *send_queue = get_current_queue();
1236 struct msg_queue *recv_queue = NULL;
1237 struct thread *thread = NULL;
1241 if (!(thread = get_thread_from_id( req->id ))) return;
1243 else if (req->type != MSG_HARDWARE)
1245 /* only hardware messages are allowed without destination thread */
1246 set_error( STATUS_INVALID_PARAMETER );
1250 if (thread && !(recv_queue = thread->queue))
1252 set_error( STATUS_INVALID_PARAMETER );
1253 release_object( thread );
1257 if ((msg = mem_alloc( sizeof(*msg) )))
1259 msg->type = req->type;
1260 msg->win = get_user_full_handle( req->win );
1261 msg->msg = req->msg;
1262 msg->wparam = req->wparam;
1263 msg->lparam = req->lparam;
1264 msg->time = req->time;
1267 msg->info = req->info;
1274 case MSG_OTHER_PROCESS:
1275 msg->data_size = get_req_data_size();
1276 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1285 if (!(msg->result = alloc_message_result( send_queue, recv_queue, req->timeout )))
1292 append_message( &recv_queue->msg_list[SEND_MESSAGE], msg );
1293 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1296 /* needed for posted DDE messages */
1297 msg->data_size = get_req_data_size();
1298 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1303 append_message( &recv_queue->msg_list[POST_MESSAGE], msg );
1304 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1307 queue_hardware_message( recv_queue, msg );
1310 set_error( STATUS_INVALID_PARAMETER );
1315 if (thread) release_object( thread );
1319 /* get a message from the current queue */
1320 DECL_HANDLER(get_message)
1322 struct timer *timer;
1323 struct message *msg;
1324 struct message *first_hw_msg = NULL;
1325 struct msg_queue *queue = get_current_queue();
1326 user_handle_t get_win = get_user_full_handle( req->get_win );
1330 /* first of all release the hardware input lock if we own it */
1331 /* we'll grab it again if we find a hardware message */
1332 if (queue->input->msg_thread == current)
1334 first_hw_msg = queue->input->msg;
1335 release_hardware_message( current, 0 );
1338 /* first check for sent messages */
1339 if ((msg = queue->msg_list[SEND_MESSAGE].first))
1341 receive_message( queue, msg, reply );
1344 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1346 /* clear changed bits so we can wait on them if we don't find a message */
1347 queue->changed_bits = 0;
1349 /* then check for posted messages */
1350 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1353 /* then check for any raw hardware message */
1354 if (get_hardware_message( current, first_hw_msg, get_win, reply ))
1357 /* now check for WM_PAINT */
1358 if (queue->paint_count &&
1359 (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
1360 (reply->win = find_window_to_repaint( get_win, current )))
1362 reply->type = MSG_POSTED;
1363 reply->msg = WM_PAINT;
1368 reply->time = get_tick_count();
1373 /* now check for timer */
1374 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1375 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1377 reply->type = MSG_POSTED;
1378 reply->win = timer->win;
1379 reply->msg = timer->msg;
1380 reply->wparam = timer->id;
1381 reply->lparam = timer->lparam;
1384 reply->time = get_tick_count();
1390 set_error( STATUS_PENDING ); /* FIXME */
1394 /* reply to a sent message */
1395 DECL_HANDLER(reply_message)
1397 if (!current->queue)
1399 set_error( STATUS_ACCESS_DENIED );
1402 if (req->type == MSG_HARDWARE)
1404 struct thread_input *input = current->queue->input;
1405 if (input->msg_thread == current) release_hardware_message( current, req->remove );
1406 else set_error( STATUS_ACCESS_DENIED );
1408 else if (current->queue->recv_result)
1409 reply_message( current->queue, req->result, 0, req->remove,
1410 get_req_data(), get_req_data_size() );
1414 /* retrieve the reply for the last message sent */
1415 DECL_HANDLER(get_message_reply)
1417 struct msg_queue *queue = current->queue;
1421 struct message_result *result = queue->send_result;
1423 set_error( STATUS_PENDING );
1426 if (result && (result->replied || req->cancel))
1428 if (result->replied)
1430 reply->result = result->result;
1431 set_error( result->error );
1434 size_t data_len = min( result->data_size, get_reply_max_size() );
1435 set_reply_data_ptr( result->data, data_len );
1436 result->data = NULL;
1437 result->data_size = 0;
1440 queue->send_result = result->send_next;
1441 result->sender = NULL;
1442 if (!result->receiver) free_result( result );
1443 if (!queue->send_result || !queue->send_result->replied)
1444 clear_queue_bits( queue, QS_SMRESULT );
1447 else set_error( STATUS_ACCESS_DENIED );
1451 /* set a window timer */
1452 DECL_HANDLER(set_win_timer)
1454 struct timer *timer;
1455 struct msg_queue *queue = get_current_queue();
1456 user_handle_t win = get_user_full_handle( req->win );
1460 /* remove it if it existed already */
1461 if (win) kill_timer( queue, win, req->msg, req->id );
1463 if ((timer = set_timer( queue, req->rate )))
1466 timer->msg = req->msg;
1467 timer->id = req->id;
1468 timer->lparam = req->lparam;
1472 /* kill a window timer */
1473 DECL_HANDLER(kill_win_timer)
1475 struct msg_queue *queue = current->queue;
1477 if (!queue || !kill_timer( queue, get_user_full_handle(req->win), req->msg, req->id ))
1478 set_error( STATUS_INVALID_PARAMETER );
1482 /* attach (or detach) thread inputs */
1483 DECL_HANDLER(attach_thread_input)
1485 struct thread *thread_from = get_thread_from_id( req->tid_from );
1486 struct thread *thread_to = get_thread_from_id( req->tid_to );
1488 if (!thread_from || !thread_to)
1490 if (thread_from) release_object( thread_from );
1491 if (thread_to) release_object( thread_to );
1494 if (thread_from != thread_to)
1496 if (req->attach) attach_thread_input( thread_from, thread_to );
1497 else detach_thread_input( thread_from, thread_to );
1499 else set_error( STATUS_ACCESS_DENIED );
1500 release_object( thread_from );
1501 release_object( thread_to );
1505 /* get thread input data */
1506 DECL_HANDLER(get_thread_input)
1508 struct thread *thread = NULL;
1509 struct thread_input *input;
1513 if (!(thread = get_thread_from_id( req->tid ))) return;
1514 input = thread->queue ? thread->queue->input : NULL;
1516 else input = foreground_input; /* get the foreground thread info */
1520 reply->focus = input->focus;
1521 reply->capture = input->capture;
1522 reply->active = input->active;
1523 reply->menu_owner = input->menu_owner;
1524 reply->move_size = input->move_size;
1525 reply->caret = input->caret;
1526 reply->rect = input->caret_rect;
1533 reply->menu_owner = 0;
1534 reply->move_size = 0;
1536 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1538 /* foreground window is active window of foreground thread */
1539 reply->foreground = foreground_input ? foreground_input->active : 0;
1540 if (thread) release_object( thread );
1544 /* retrieve queue keyboard state for a given thread */
1545 DECL_HANDLER(get_key_state)
1547 struct thread *thread;
1548 struct thread_input *input;
1550 if (!(thread = get_thread_from_id( req->tid ))) return;
1551 input = thread->queue ? thread->queue->input : NULL;
1554 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1555 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1557 release_object( thread );
1561 /* set queue keyboard state for a given thread */
1562 DECL_HANDLER(set_key_state)
1564 struct thread *thread = NULL;
1565 struct thread_input *input;
1567 if (!(thread = get_thread_from_id( req->tid ))) return;
1568 input = thread->queue ? thread->queue->input : NULL;
1571 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1572 if (size) memcpy( input->keystate, get_req_data(), size );
1574 release_object( thread );
1578 /* set the system foreground window */
1579 DECL_HANDLER(set_foreground_window)
1581 struct msg_queue *queue = get_current_queue();
1583 reply->previous = foreground_input ? foreground_input->active : 0;
1584 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1585 reply->send_msg_new = FALSE;
1589 struct thread *thread;
1591 if (is_top_level_window( req->handle ) &&
1592 ((thread = get_window_thread( req->handle ))))
1594 foreground_input = thread->queue->input;
1595 reply->send_msg_new = (foreground_input != queue->input);
1596 release_object( thread );
1598 else set_error( STATUS_INVALID_HANDLE );
1600 else foreground_input = NULL;
1604 /* set the current thread focus window */
1605 DECL_HANDLER(set_focus_window)
1607 struct msg_queue *queue = get_current_queue();
1609 reply->previous = 0;
1610 if (queue && check_queue_input_window( queue, req->handle ))
1612 reply->previous = queue->input->focus;
1613 queue->input->focus = get_user_full_handle( req->handle );
1618 /* set the current thread active window */
1619 DECL_HANDLER(set_active_window)
1621 struct msg_queue *queue = get_current_queue();
1623 reply->previous = 0;
1624 if (queue && check_queue_input_window( queue, req->handle ))
1626 if (!req->handle || make_window_active( req->handle ))
1628 reply->previous = queue->input->active;
1629 queue->input->active = get_user_full_handle( req->handle );
1631 else set_error( STATUS_INVALID_HANDLE );
1636 /* set the current thread capture window */
1637 DECL_HANDLER(set_capture_window)
1639 struct msg_queue *queue = get_current_queue();
1641 reply->previous = reply->full_handle = 0;
1642 if (queue && check_queue_input_window( queue, req->handle ))
1644 struct thread_input *input = queue->input;
1646 reply->previous = input->capture;
1647 input->capture = get_user_full_handle( req->handle );
1648 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1649 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1650 reply->full_handle = input->capture;
1655 /* Set the current thread caret window */
1656 DECL_HANDLER(set_caret_window)
1658 struct msg_queue *queue = get_current_queue();
1660 reply->previous = 0;
1661 if (queue && check_queue_input_window( queue, req->handle ))
1663 struct thread_input *input = queue->input;
1665 reply->previous = input->caret;
1666 reply->old_rect = input->caret_rect;
1667 reply->old_hide = input->caret_hide;
1668 reply->old_state = input->caret_state;
1670 set_caret_window( input, get_user_full_handle(req->handle) );
1671 input->caret_rect.right = req->width;
1672 input->caret_rect.bottom = req->height;
1677 /* Set the current thread caret information */
1678 DECL_HANDLER(set_caret_info)
1680 struct msg_queue *queue = get_current_queue();
1681 struct thread_input *input;
1684 input = queue->input;
1685 reply->full_handle = input->caret;
1686 reply->old_rect = input->caret_rect;
1687 reply->old_hide = input->caret_hide;
1688 reply->old_state = input->caret_state;
1690 if (req->handle && get_user_full_handle(req->handle) != input->caret)
1692 set_error( STATUS_ACCESS_DENIED );
1695 if (req->flags & SET_CARET_POS)
1697 input->caret_rect.right += req->x - input->caret_rect.left;
1698 input->caret_rect.bottom += req->y - input->caret_rect.top;
1699 input->caret_rect.left = req->x;
1700 input->caret_rect.top = req->y;
1702 if (req->flags & SET_CARET_HIDE)
1704 input->caret_hide += req->hide;
1705 if (input->caret_hide < 0) input->caret_hide = 0;
1707 if (req->flags & SET_CARET_STATE)
1709 if (req->state == -1) input->caret_state = !input->caret_state;
1710 else input->caret_state = !!req->state;