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 */
129 static void msg_queue_dump( struct object *obj, int verbose );
130 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
131 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
132 static int msg_queue_signaled( struct object *obj, struct thread *thread );
133 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
134 static void msg_queue_destroy( struct object *obj );
135 static void thread_input_dump( struct object *obj, int verbose );
136 static void thread_input_destroy( struct object *obj );
137 static void timer_callback( void *private );
139 static const struct object_ops msg_queue_ops =
141 sizeof(struct msg_queue), /* size */
142 msg_queue_dump, /* dump */
143 msg_queue_add_queue, /* add_queue */
144 msg_queue_remove_queue, /* remove_queue */
145 msg_queue_signaled, /* signaled */
146 msg_queue_satisfied, /* satisfied */
147 no_get_fd, /* get_fd */
148 msg_queue_destroy /* destroy */
152 static const struct object_ops thread_input_ops =
154 sizeof(struct thread_input), /* size */
155 thread_input_dump, /* dump */
156 no_add_queue, /* add_queue */
157 NULL, /* remove_queue */
159 NULL, /* satisfied */
160 no_get_fd, /* get_fd */
161 thread_input_destroy /* destroy */
164 /* pointer to input structure of foreground thread */
165 static struct thread_input *foreground_input;
168 /* set the caret window in a given thread input */
169 static void set_caret_window( struct thread_input *input, user_handle_t win )
172 input->caret_rect.left = 0;
173 input->caret_rect.top = 0;
174 input->caret_rect.right = 0;
175 input->caret_rect.bottom = 0;
176 input->caret_hide = 1;
177 input->caret_state = 0;
180 /* create a thread input object */
181 static struct thread_input *create_thread_input(void)
183 struct thread_input *input;
185 if ((input = alloc_object( &thread_input_ops )))
190 input->menu_owner = 0;
191 input->move_size = 0;
193 input->msg_thread = NULL;
194 input->msg_list.first = input->msg_list.last = NULL;
195 set_caret_window( input, 0 );
196 memset( input->keystate, 0, sizeof(input->keystate) );
201 /* create a message queue object */
202 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
204 struct msg_queue *queue;
207 if (!input && !(input = create_thread_input())) return NULL;
208 if ((queue = alloc_object( &msg_queue_ops )))
210 queue->wake_bits = 0;
211 queue->wake_mask = 0;
212 queue->changed_bits = 0;
213 queue->changed_mask = 0;
214 queue->paint_count = 0;
215 queue->send_result = NULL;
216 queue->recv_result = NULL;
217 queue->first_timer = NULL;
218 queue->last_timer = NULL;
219 queue->next_timer = NULL;
220 queue->timeout = NULL;
221 queue->input = (struct thread_input *)grab_object( input );
222 for (i = 0; i < NB_MSG_KINDS; i++)
223 queue->msg_list[i].first = queue->msg_list[i].last = NULL;
225 thread->queue = queue;
226 if (!thread->process->queue)
227 thread->process->queue = (struct msg_queue *)grab_object( queue );
229 release_object( input );
233 /* free the message queue of a thread at thread exit */
234 void free_msg_queue( struct thread *thread )
236 struct process *process = thread->process;
238 if (!thread->queue) return;
239 if (process->queue == thread->queue) /* is it the process main queue? */
241 release_object( process->queue );
242 process->queue = NULL;
243 if (process->idle_event)
245 set_event( process->idle_event );
246 release_object( process->idle_event );
247 process->idle_event = NULL;
250 release_object( thread->queue );
251 thread->queue = NULL;
254 /* check the queue status */
255 inline static int is_signaled( struct msg_queue *queue )
257 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
260 /* set some queue bits */
261 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
263 queue->wake_bits |= bits;
264 queue->changed_bits |= bits;
265 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
268 /* clear some queue bits */
269 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
271 queue->wake_bits &= ~bits;
272 queue->changed_bits &= ~bits;
275 /* check whether msg is a keyboard message */
276 inline static int is_keyboard_msg( struct message *msg )
278 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
281 /* get the QS_* bit corresponding to a given hardware message */
282 inline static int get_hardware_msg_bit( struct message *msg )
284 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
285 if (is_keyboard_msg( msg )) return QS_KEY;
286 return QS_MOUSEBUTTON;
289 /* get the current thread queue, creating it if needed */
290 inline static struct msg_queue *get_current_queue(void)
292 struct msg_queue *queue = current->queue;
293 if (!queue) queue = create_msg_queue( current, NULL );
297 /* append a message to the end of a list */
298 inline static void append_message( struct message_list *list, struct message *msg )
301 if ((msg->prev = list->last)) msg->prev->next = msg;
302 else list->first = msg;
306 /* unlink a message from a list it */
307 inline static void unlink_message( struct message_list *list, struct message *msg )
309 if (msg->next) msg->next->prev = msg->prev;
310 else list->last = msg->prev;
311 if (msg->prev) msg->prev->next = msg->next;
312 else list->first = msg->next;
315 /* try to merge a message with the last in the list; return 1 if successful */
316 static int merge_message( struct thread_input *input, const struct message *msg )
318 struct message *prev = input->msg_list.last;
321 if (input->msg == prev) return 0;
322 if (prev->result) return 0;
323 if (prev->win != msg->win) return 0;
324 if (prev->msg != msg->msg) return 0;
325 if (prev->type != msg->type) return 0;
326 /* now we can merge it */
327 prev->wparam = msg->wparam;
328 prev->lparam = msg->lparam;
331 prev->time = msg->time;
332 prev->info = msg->info;
336 /* free a result structure */
337 static void free_result( struct message_result *result )
339 if (result->timeout) remove_timeout_user( result->timeout );
340 if (result->data) free( result->data );
344 /* store the message result in the appropriate structure */
345 static void store_message_result( struct message_result *res, unsigned int result,
348 res->result = result;
353 remove_timeout_user( res->timeout );
356 /* wake sender queue if waiting on this result */
357 if (res->sender && res->sender->send_result == res)
358 set_queue_bits( res->sender, QS_SMRESULT );
361 /* free a message when deleting a queue or window */
362 static void free_message( struct message *msg )
364 struct message_result *result = msg->result;
369 result->receiver = NULL;
370 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
372 else free_result( result );
374 if (msg->data) free( msg->data );
378 /* remove (and free) a message from a message list */
379 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
380 enum message_kind kind )
382 unlink_message( &queue->msg_list[kind], msg );
386 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_SENDMESSAGE );
389 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_POSTMESSAGE );
395 /* message timed out without getting a reply */
396 static void result_timeout( void *private )
398 struct message_result *result = private;
400 assert( !result->replied );
402 result->timeout = NULL;
403 store_message_result( result, 0, STATUS_TIMEOUT );
406 /* allocate and fill a message result structure */
407 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
408 struct msg_queue *recv_queue,
409 unsigned int timeout )
411 struct message_result *result = mem_alloc( sizeof(*result) );
414 /* put the result on the sender result stack */
415 result->sender = send_queue;
416 result->receiver = recv_queue;
419 result->data_size = 0;
420 result->timeout = NULL;
421 result->send_next = send_queue->send_result;
422 send_queue->send_result = result;
426 gettimeofday( &when, 0 );
427 add_timeout( &when, timeout );
428 result->timeout = add_timeout_user( &when, result_timeout, result );
434 /* receive a message, removing it from the sent queue */
435 static void receive_message( struct msg_queue *queue, struct message *msg,
436 struct get_message_reply *reply )
438 struct message_result *result = msg->result;
440 reply->total = msg->data_size;
441 if (msg->data_size > get_reply_max_size())
443 set_error( STATUS_BUFFER_OVERFLOW );
446 reply->type = msg->type;
447 reply->win = msg->win;
448 reply->msg = msg->msg;
449 reply->wparam = msg->wparam;
450 reply->lparam = msg->lparam;
453 reply->time = msg->time;
454 reply->info = msg->info;
456 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
458 unlink_message( &queue->msg_list[SEND_MESSAGE], msg );
459 /* put the result on the receiver result stack */
462 result->recv_next = queue->recv_result;
463 queue->recv_result = result;
466 if (!queue->msg_list[SEND_MESSAGE].first) clear_queue_bits( queue, QS_SENDMESSAGE );
469 /* set the result of the current received message */
470 static void reply_message( struct msg_queue *queue, unsigned int result,
471 unsigned int error, int remove, const void *data, size_t len )
473 struct message_result *res = queue->recv_result;
477 queue->recv_result = res->recv_next;
478 res->receiver = NULL;
479 if (!res->sender) /* no one waiting for it */
487 if (len && (res->data = memdup( data, len ))) res->data_size = len;
488 store_message_result( res, result, error );
492 /* retrieve a posted message */
493 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
494 unsigned int first, unsigned int last, unsigned int flags,
495 struct get_message_reply *reply )
498 struct message_list *list = &queue->msg_list[POST_MESSAGE];
500 /* check against the filters */
501 for (msg = list->first; msg; msg = msg->next)
503 if (msg->msg == WM_QUIT) break; /* WM_QUIT is never filtered */
504 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
505 if (msg->msg < first) continue;
506 if (msg->msg > last) continue;
507 break; /* found one */
511 /* return it to the app */
513 reply->total = msg->data_size;
514 if (msg->data_size > get_reply_max_size())
516 set_error( STATUS_BUFFER_OVERFLOW );
519 reply->type = msg->type;
520 reply->win = msg->win;
521 reply->msg = msg->msg;
522 reply->wparam = msg->wparam;
523 reply->lparam = msg->lparam;
526 reply->time = msg->time;
527 reply->info = msg->info;
529 if (flags & GET_MSG_REMOVE)
533 set_reply_data_ptr( msg->data, msg->data_size );
537 remove_queue_message( queue, msg, POST_MESSAGE );
539 else if (msg->data) set_reply_data( msg->data, msg->data_size );
544 /* empty a message list and free all the messages */
545 static void empty_msg_list( struct message_list *list )
547 struct message *msg = list->first;
550 struct message *next = msg->next;
556 /* cleanup all pending results when deleting a queue */
557 static void cleanup_results( struct msg_queue *queue )
559 struct message_result *result, *next;
561 result = queue->send_result;
564 next = result->send_next;
565 result->sender = NULL;
566 if (!result->receiver) free_result( result );
570 while (queue->recv_result)
571 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
574 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
576 struct msg_queue *queue = (struct msg_queue *)obj;
577 struct process *process = entry->thread->process;
579 /* a thread can only wait on its own queue */
580 if (entry->thread->queue != queue)
582 set_error( STATUS_ACCESS_DENIED );
585 /* if waiting on the main process queue, set the idle event */
586 if (process->queue == queue)
588 if (process->idle_event) set_event( process->idle_event );
590 add_queue( obj, entry );
594 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
596 struct msg_queue *queue = (struct msg_queue *)obj;
597 struct process *process = entry->thread->process;
599 remove_queue( obj, entry );
601 assert( entry->thread->queue == queue );
603 /* if waiting on the main process queue, reset the idle event */
604 if (process->queue == queue)
606 if (process->idle_event) reset_event( process->idle_event );
610 static void msg_queue_dump( struct object *obj, int verbose )
612 struct msg_queue *queue = (struct msg_queue *)obj;
613 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
614 queue->wake_bits, queue->wake_mask );
617 static int msg_queue_signaled( struct object *obj, struct thread *thread )
619 struct msg_queue *queue = (struct msg_queue *)obj;
620 return is_signaled( queue );
623 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
625 struct msg_queue *queue = (struct msg_queue *)obj;
626 queue->wake_mask = 0;
627 queue->changed_mask = 0;
628 return 0; /* Not abandoned */
631 static void msg_queue_destroy( struct object *obj )
633 struct msg_queue *queue = (struct msg_queue *)obj;
634 struct timer *timer = queue->first_timer;
637 cleanup_results( queue );
638 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
642 struct timer *next = timer->next;
646 if (queue->timeout) remove_timeout_user( queue->timeout );
647 if (queue->input) release_object( queue->input );
650 static void thread_input_dump( struct object *obj, int verbose )
652 struct thread_input *input = (struct thread_input *)obj;
653 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
654 input->focus, input->capture, input->active );
657 static void thread_input_destroy( struct object *obj )
659 struct thread_input *input = (struct thread_input *)obj;
661 if (foreground_input == input) foreground_input = NULL;
662 if (input->msg_thread) release_object( input->msg_thread );
663 empty_msg_list( &input->msg_list );
666 /* fix the thread input data when a window is destroyed */
667 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
669 struct thread_input *input = queue->input;
671 if (window == input->focus) input->focus = 0;
672 if (window == input->capture) input->capture = 0;
673 if (window == input->active) input->active = 0;
674 if (window == input->menu_owner) input->menu_owner = 0;
675 if (window == input->move_size) input->move_size = 0;
676 if (window == input->caret) set_caret_window( input, 0 );
679 /* check if the specified window can be set in the input data of a given queue */
680 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
682 struct thread *thread;
685 if (!window) return 1; /* we can always clear the data */
687 if ((thread = get_window_thread( window )))
689 ret = (queue->input == thread->queue->input);
690 if (!ret) set_error( STATUS_ACCESS_DENIED );
691 release_object( thread );
693 else set_error( STATUS_INVALID_HANDLE );
698 /* attach two thread input data structures */
699 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
701 struct thread_input *input;
703 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
704 input = (struct thread_input *)grab_object( thread_to->queue->input );
706 if (thread_from->queue)
708 release_object( thread_from->queue->input );
709 thread_from->queue->input = input;
713 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
715 memset( input->keystate, 0, sizeof(input->keystate) );
719 /* detach two thread input data structures */
720 static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
722 struct thread_input *input;
724 if (!thread_from->queue || !thread_to->queue ||
725 thread_from->queue->input != thread_to->queue->input)
727 set_error( STATUS_ACCESS_DENIED );
730 if ((input = create_thread_input()))
732 release_object( thread_from->queue->input );
733 thread_from->queue->input = input;
738 /* set the next timer to expire */
739 static void set_next_timer( struct msg_queue *queue, struct timer *timer )
743 remove_timeout_user( queue->timeout );
744 queue->timeout = NULL;
746 if ((queue->next_timer = timer))
747 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
749 /* set/clear QS_TIMER bit */
750 if (queue->next_timer == queue->first_timer)
751 clear_queue_bits( queue, QS_TIMER );
753 set_queue_bits( queue, QS_TIMER );
756 /* callback for the next timer expiration */
757 static void timer_callback( void *private )
759 struct msg_queue *queue = private;
761 queue->timeout = NULL;
762 /* move on to the next timer */
763 set_next_timer( queue, queue->next_timer->next );
766 /* link a timer at its rightful place in the queue list */
767 static void link_timer( struct msg_queue *queue, struct timer *timer )
769 struct timer *pos = queue->next_timer;
771 while (pos && time_before( &pos->when, &timer->when )) pos = pos->next;
773 if (pos) /* insert before pos */
775 if ((timer->prev = pos->prev)) timer->prev->next = timer;
776 else queue->first_timer = timer;
780 else /* insert at end */
783 timer->prev = queue->last_timer;
784 if (queue->last_timer) queue->last_timer->next = timer;
785 else queue->first_timer = timer;
786 queue->last_timer = timer;
788 /* check if we replaced the next timer */
789 if (pos == queue->next_timer) set_next_timer( queue, timer );
792 /* remove a timer from the queue timer list */
793 static void unlink_timer( struct msg_queue *queue, struct timer *timer )
795 if (timer->next) timer->next->prev = timer->prev;
796 else queue->last_timer = timer->prev;
797 if (timer->prev) timer->prev->next = timer->next;
798 else queue->first_timer = timer->next;
799 /* check if we removed the next timer */
800 if (queue->next_timer == timer) set_next_timer( queue, timer->next );
801 else if (queue->next_timer == queue->first_timer) clear_queue_bits( queue, QS_TIMER );
804 /* restart an expired timer */
805 static void restart_timer( struct msg_queue *queue, struct timer *timer )
808 unlink_timer( queue, timer );
809 gettimeofday( &now, 0 );
810 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
811 link_timer( queue, timer );
814 /* find an expired timer matching the filtering parameters */
815 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
816 unsigned int get_first, unsigned int get_last,
820 for (timer = queue->first_timer; (timer && timer != queue->next_timer); timer = timer->next)
822 if (win && timer->win != win) continue;
823 if (timer->msg >= get_first && timer->msg <= get_last)
825 if (remove) restart_timer( queue, timer );
833 static int kill_timer( struct msg_queue *queue, user_handle_t win,
834 unsigned int msg, unsigned int id )
838 for (timer = queue->first_timer; timer; timer = timer->next)
840 if (timer->win != win || timer->msg != msg || timer->id != id) continue;
841 unlink_timer( queue, timer );
849 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
851 struct timer *timer = mem_alloc( sizeof(*timer) );
855 gettimeofday( &timer->when, 0 );
856 add_timeout( &timer->when, rate );
857 link_timer( queue, timer );
862 /* change the input key state for a given key */
863 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
867 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
868 input->keystate[key] |= 0x80;
870 else input->keystate[key] &= ~0x80;
873 /* update the input key state for a keyboard message */
874 static void update_input_key_state( struct thread_input *input, const struct message *msg )
877 int down = 0, extended;
885 set_input_key_state( input, VK_LBUTTON, down );
891 set_input_key_state( input, VK_MBUTTON, down );
897 set_input_key_state( input, VK_RBUTTON, down );
905 key = (unsigned char)msg->wparam;
906 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
907 set_input_key_state( input, key, down );
911 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
914 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
917 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
924 /* release the hardware message currently being processed by the given thread */
925 static void release_hardware_message( struct thread *thread, int remove )
927 struct thread_input *input = thread->queue->input;
929 if (input->msg_thread != thread) return;
932 struct message *other;
935 update_input_key_state( input, input->msg );
936 unlink_message( &input->msg_list, input->msg );
937 clr_bit = get_hardware_msg_bit( input->msg );
938 for (other = input->msg_list.first; other; other = other->next)
939 if (get_hardware_msg_bit( other ) == clr_bit) break;
940 if (!other) clear_queue_bits( thread->queue, clr_bit );
941 free_message( input->msg );
943 release_object( input->msg_thread );
945 input->msg_thread = NULL;
948 /* find the window that should receive a given hardware message */
949 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
950 unsigned int *msg_code )
952 user_handle_t win = 0;
954 *msg_code = msg->msg;
955 if (is_keyboard_msg( msg ))
957 if (input && !(win = input->focus))
960 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
963 else /* mouse message */
965 if (!input || !(win = input->capture))
967 if (!(win = msg->win)) win = window_from_point( msg->x, msg->y );
973 /* queue a hardware message into a given thread input */
974 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
977 struct thread *thread;
978 struct thread_input *input;
979 unsigned int msg_code;
981 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
982 if (!win || !(thread = get_window_thread(win)))
987 input = thread->queue->input;
989 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
992 append_message( &input->msg_list, msg );
993 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
995 release_object( thread );
998 /* find a hardware message for the given queue */
999 static int get_hardware_message( struct thread *thread, struct message *first,
1000 user_handle_t filter_win, struct get_message_reply *reply )
1002 struct thread_input *input = thread->queue->input;
1003 struct thread *win_thread;
1004 struct message *msg;
1006 int clear_bits, got_one = 0;
1007 unsigned int msg_code;
1009 if (input->msg_thread && input->msg_thread != thread)
1010 return 0; /* locked by another thread */
1014 msg = input->msg_list.first;
1015 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1020 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1025 win = find_hardware_message_window( input, msg, &msg_code );
1026 if (!win || !(win_thread = get_window_thread( win )))
1028 /* no window at all, remove it */
1029 struct message *next = msg->next;
1030 update_input_key_state( input, msg );
1031 unlink_message( &input->msg_list, msg );
1032 free_message( msg );
1036 if (win_thread != thread)
1038 /* wake the other thread */
1039 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1040 release_object( win_thread );
1045 /* if we already got a message for another thread, or if it doesn't
1046 * match the filter we skip it (filter is only checked for keyboard
1047 * messages since the dest window for a mouse message depends on hittest)
1050 (filter_win && is_keyboard_msg(msg) &&
1051 win != filter_win && !is_child_window( filter_win, win )))
1053 clear_bits &= ~get_hardware_msg_bit( msg );
1057 /* now we can return it */
1058 if (!input->msg_thread) input->msg_thread = win_thread;
1059 else release_object( win_thread );
1062 reply->type = MSG_HARDWARE;
1064 reply->msg = msg_code;
1065 reply->wparam = msg->wparam;
1066 reply->lparam = msg->lparam;
1069 reply->time = msg->time;
1070 reply->info = msg->info;
1073 /* nothing found, clear the hardware queue bits */
1074 clear_queue_bits( thread->queue, clear_bits );
1075 if (input->msg_thread) release_object( input->msg_thread );
1077 input->msg_thread = NULL;
1081 /* increment (or decrement if 'incr' is negative) the queue paint count */
1082 void inc_queue_paint_count( struct thread *thread, int incr )
1084 struct msg_queue *queue = thread->queue;
1088 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1090 if (queue->paint_count)
1091 set_queue_bits( queue, QS_PAINT );
1093 clear_queue_bits( queue, QS_PAINT );
1097 /* remove all messages and timers belonging to a certain window */
1098 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1100 struct msg_queue *queue = thread->queue;
1101 struct timer *timer;
1102 struct message *msg;
1108 timer = queue->first_timer;
1111 struct timer *next = timer->next;
1112 if (timer->win == win)
1114 unlink_timer( queue, timer );
1120 /* remove messages */
1121 for (i = 0; i < NB_MSG_KINDS; i++)
1123 msg = queue->msg_list[i].first;
1126 struct message *next = msg->next;
1127 if (msg->win == win) remove_queue_message( queue, msg, i );
1132 thread_input_cleanup_window( queue, win );
1135 /* post a message to a window; used by socket handling */
1136 void post_message( user_handle_t win, unsigned int message,
1137 unsigned int wparam, unsigned int lparam )
1139 struct message *msg;
1140 struct thread *thread = get_window_thread( win );
1142 if (!thread) return;
1144 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1146 msg->type = MSG_POSTED;
1147 msg->win = get_user_full_handle( win );
1149 msg->wparam = wparam;
1150 msg->lparam = lparam;
1151 msg->time = get_tick_count();
1159 append_message( &thread->queue->msg_list[POST_MESSAGE], msg );
1160 set_queue_bits( thread->queue, QS_POSTMESSAGE );
1162 release_object( thread );
1166 /* get the message queue of the current thread */
1167 DECL_HANDLER(get_msg_queue)
1169 struct msg_queue *queue = get_current_queue();
1172 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1176 /* set the current message queue wakeup mask */
1177 DECL_HANDLER(set_queue_mask)
1179 struct msg_queue *queue = get_current_queue();
1183 queue->wake_mask = req->wake_mask;
1184 queue->changed_mask = req->changed_mask;
1185 reply->wake_bits = queue->wake_bits;
1186 reply->changed_bits = queue->changed_bits;
1187 if (is_signaled( queue ))
1189 /* if skip wait is set, do what would have been done in the subsequent wait */
1190 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1191 else wake_up( &queue->obj, 0 );
1197 /* get the current message queue status */
1198 DECL_HANDLER(get_queue_status)
1200 struct msg_queue *queue = current->queue;
1203 reply->wake_bits = queue->wake_bits;
1204 reply->changed_bits = queue->changed_bits;
1205 if (req->clear) queue->changed_bits = 0;
1207 else reply->wake_bits = reply->changed_bits = 0;
1211 /* send a message to a thread queue */
1212 DECL_HANDLER(send_message)
1214 struct message *msg;
1215 struct msg_queue *send_queue = get_current_queue();
1216 struct msg_queue *recv_queue = NULL;
1217 struct thread *thread = NULL;
1221 if (!(thread = get_thread_from_id( req->id ))) return;
1223 else if (req->type != MSG_HARDWARE)
1225 /* only hardware messages are allowed without destination thread */
1226 set_error( STATUS_INVALID_PARAMETER );
1230 if (thread && !(recv_queue = thread->queue))
1232 set_error( STATUS_INVALID_PARAMETER );
1233 release_object( thread );
1237 if ((msg = mem_alloc( sizeof(*msg) )))
1239 msg->type = req->type;
1240 msg->win = get_user_full_handle( req->win );
1241 msg->msg = req->msg;
1242 msg->wparam = req->wparam;
1243 msg->lparam = req->lparam;
1244 msg->time = req->time;
1247 msg->info = req->info;
1254 case MSG_OTHER_PROCESS:
1255 msg->data_size = get_req_data_size();
1256 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1265 if (!(msg->result = alloc_message_result( send_queue, recv_queue, req->timeout )))
1272 append_message( &recv_queue->msg_list[SEND_MESSAGE], msg );
1273 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1276 /* needed for posted DDE messages */
1277 msg->data_size = get_req_data_size();
1278 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1283 append_message( &recv_queue->msg_list[POST_MESSAGE], msg );
1284 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1287 queue_hardware_message( recv_queue, msg );
1290 set_error( STATUS_INVALID_PARAMETER );
1295 if (thread) release_object( thread );
1299 /* get a message from the current queue */
1300 DECL_HANDLER(get_message)
1302 struct timer *timer;
1303 struct message *msg;
1304 struct message *first_hw_msg = NULL;
1305 struct msg_queue *queue = get_current_queue();
1306 user_handle_t get_win = get_user_full_handle( req->get_win );
1310 /* first of all release the hardware input lock if we own it */
1311 /* we'll grab it again if we find a hardware message */
1312 if (queue->input->msg_thread == current)
1314 first_hw_msg = queue->input->msg;
1315 release_hardware_message( current, 0 );
1318 /* first check for sent messages */
1319 if ((msg = queue->msg_list[SEND_MESSAGE].first))
1321 receive_message( queue, msg, reply );
1324 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1326 /* clear changed bits so we can wait on them if we don't find a message */
1327 queue->changed_bits = 0;
1329 /* then check for posted messages */
1330 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1333 /* then check for any raw hardware message */
1334 if (get_hardware_message( current, first_hw_msg, get_win, reply ))
1337 /* now check for WM_PAINT */
1338 if (queue->paint_count &&
1339 (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
1340 (reply->win = find_window_to_repaint( get_win, current )))
1342 reply->type = MSG_POSTED;
1343 reply->msg = WM_PAINT;
1348 reply->time = get_tick_count();
1353 /* now check for timer */
1354 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1355 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1357 reply->type = MSG_POSTED;
1358 reply->win = timer->win;
1359 reply->msg = timer->msg;
1360 reply->wparam = timer->id;
1361 reply->lparam = timer->lparam;
1364 reply->time = get_tick_count();
1370 set_error( STATUS_PENDING ); /* FIXME */
1374 /* reply to a sent message */
1375 DECL_HANDLER(reply_message)
1377 if (!current->queue)
1379 set_error( STATUS_ACCESS_DENIED );
1382 if (current->queue->recv_result)
1383 reply_message( current->queue, req->result, 0, req->remove,
1384 get_req_data(), get_req_data_size() );
1387 struct thread_input *input = current->queue->input;
1388 if (input->msg_thread == current) release_hardware_message( current, req->remove );
1389 else set_error( STATUS_ACCESS_DENIED );
1394 /* retrieve the reply for the last message sent */
1395 DECL_HANDLER(get_message_reply)
1397 struct msg_queue *queue = current->queue;
1401 struct message_result *result = queue->send_result;
1403 set_error( STATUS_PENDING );
1406 if (result && (result->replied || req->cancel))
1408 if (result->replied)
1410 reply->result = result->result;
1411 set_error( result->error );
1414 size_t data_len = min( result->data_size, get_reply_max_size() );
1415 set_reply_data_ptr( result->data, data_len );
1416 result->data = NULL;
1417 result->data_size = 0;
1420 queue->send_result = result->send_next;
1421 result->sender = NULL;
1422 if (!result->receiver) free_result( result );
1423 if (!queue->send_result || !queue->send_result->replied)
1424 clear_queue_bits( queue, QS_SMRESULT );
1427 else set_error( STATUS_ACCESS_DENIED );
1431 /* set a window timer */
1432 DECL_HANDLER(set_win_timer)
1434 struct timer *timer;
1435 struct msg_queue *queue = get_current_queue();
1436 user_handle_t win = get_user_full_handle( req->win );
1440 /* remove it if it existed already */
1441 if (win) kill_timer( queue, win, req->msg, req->id );
1443 if ((timer = set_timer( queue, req->rate )))
1446 timer->msg = req->msg;
1447 timer->id = req->id;
1448 timer->lparam = req->lparam;
1452 /* kill a window timer */
1453 DECL_HANDLER(kill_win_timer)
1455 struct msg_queue *queue = current->queue;
1457 if (!queue || !kill_timer( queue, get_user_full_handle(req->win), req->msg, req->id ))
1458 set_error( STATUS_INVALID_PARAMETER );
1462 /* attach (or detach) thread inputs */
1463 DECL_HANDLER(attach_thread_input)
1465 struct thread *thread_from = get_thread_from_id( req->tid_from );
1466 struct thread *thread_to = get_thread_from_id( req->tid_to );
1468 if (!thread_from || !thread_to)
1470 if (thread_from) release_object( thread_from );
1471 if (thread_to) release_object( thread_to );
1474 if (thread_from != thread_to)
1476 if (req->attach) attach_thread_input( thread_from, thread_to );
1477 else detach_thread_input( thread_from, thread_to );
1479 else set_error( STATUS_ACCESS_DENIED );
1480 release_object( thread_from );
1481 release_object( thread_to );
1485 /* get thread input data */
1486 DECL_HANDLER(get_thread_input)
1488 struct thread *thread = NULL;
1489 struct thread_input *input;
1493 if (!(thread = get_thread_from_id( req->tid ))) return;
1494 input = thread->queue ? thread->queue->input : NULL;
1496 else input = foreground_input; /* get the foreground thread info */
1500 reply->focus = input->focus;
1501 reply->capture = input->capture;
1502 reply->active = input->active;
1503 reply->menu_owner = input->menu_owner;
1504 reply->move_size = input->move_size;
1505 reply->caret = input->caret;
1506 reply->rect = input->caret_rect;
1513 reply->menu_owner = 0;
1514 reply->move_size = 0;
1516 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1518 /* foreground window is active window of foreground thread */
1519 reply->foreground = foreground_input ? foreground_input->active : 0;
1520 if (thread) release_object( thread );
1524 /* retrieve queue keyboard state for a given thread */
1525 DECL_HANDLER(get_key_state)
1527 struct thread *thread;
1528 struct thread_input *input;
1530 if (!(thread = get_thread_from_id( req->tid ))) return;
1531 input = thread->queue ? thread->queue->input : NULL;
1534 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1535 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1537 release_object( thread );
1541 /* set queue keyboard state for a given thread */
1542 DECL_HANDLER(set_key_state)
1544 struct thread *thread = NULL;
1545 struct thread_input *input;
1547 if (!(thread = get_thread_from_id( req->tid ))) return;
1548 input = thread->queue ? thread->queue->input : NULL;
1551 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1552 if (size) memcpy( input->keystate, get_req_data(), size );
1554 release_object( thread );
1558 /* set the system foreground window */
1559 DECL_HANDLER(set_foreground_window)
1561 struct msg_queue *queue = get_current_queue();
1563 reply->previous = foreground_input ? foreground_input->active : 0;
1564 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1565 reply->send_msg_new = FALSE;
1569 struct thread *thread;
1571 if (is_top_level_window( req->handle ) &&
1572 ((thread = get_window_thread( req->handle ))))
1574 foreground_input = thread->queue->input;
1575 reply->send_msg_new = (foreground_input != queue->input);
1576 release_object( thread );
1578 else set_error( STATUS_INVALID_HANDLE );
1580 else foreground_input = NULL;
1584 /* set the current thread focus window */
1585 DECL_HANDLER(set_focus_window)
1587 struct msg_queue *queue = get_current_queue();
1589 reply->previous = 0;
1590 if (queue && check_queue_input_window( queue, req->handle ))
1592 reply->previous = queue->input->focus;
1593 queue->input->focus = get_user_full_handle( req->handle );
1598 /* set the current thread active window */
1599 DECL_HANDLER(set_active_window)
1601 struct msg_queue *queue = get_current_queue();
1603 reply->previous = 0;
1604 if (queue && check_queue_input_window( queue, req->handle ))
1606 if (!req->handle || make_window_active( req->handle ))
1608 reply->previous = queue->input->active;
1609 queue->input->active = get_user_full_handle( req->handle );
1611 else set_error( STATUS_INVALID_HANDLE );
1616 /* set the current thread capture window */
1617 DECL_HANDLER(set_capture_window)
1619 struct msg_queue *queue = get_current_queue();
1621 reply->previous = reply->full_handle = 0;
1622 if (queue && check_queue_input_window( queue, req->handle ))
1624 struct thread_input *input = queue->input;
1626 reply->previous = input->capture;
1627 input->capture = get_user_full_handle( req->handle );
1628 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1629 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1630 reply->full_handle = input->capture;
1635 /* Set the current thread caret window */
1636 DECL_HANDLER(set_caret_window)
1638 struct msg_queue *queue = get_current_queue();
1640 reply->previous = 0;
1641 if (queue && check_queue_input_window( queue, req->handle ))
1643 struct thread_input *input = queue->input;
1645 reply->previous = input->caret;
1646 reply->old_rect = input->caret_rect;
1647 reply->old_hide = input->caret_hide;
1648 reply->old_state = input->caret_state;
1650 set_caret_window( input, get_user_full_handle(req->handle) );
1651 input->caret_rect.right = req->width;
1652 input->caret_rect.bottom = req->height;
1657 /* Set the current thread caret information */
1658 DECL_HANDLER(set_caret_info)
1660 struct msg_queue *queue = get_current_queue();
1661 struct thread_input *input;
1664 input = queue->input;
1665 reply->full_handle = input->caret;
1666 reply->old_rect = input->caret_rect;
1667 reply->old_hide = input->caret_hide;
1668 reply->old_state = input->caret_state;
1670 if (req->handle && get_user_full_handle(req->handle) != input->caret)
1672 set_error( STATUS_ACCESS_DENIED );
1675 if (req->flags & SET_CARET_POS)
1677 input->caret_rect.right += req->x - input->caret_rect.left;
1678 input->caret_rect.bottom += req->y - input->caret_rect.top;
1679 input->caret_rect.left = req->x;
1680 input->caret_rect.top = req->y;
1682 if (req->flags & SET_CARET_HIDE)
1684 input->caret_hide += req->hide;
1685 if (input->caret_hide < 0) input->caret_hide = 0;
1687 if (req->flags & SET_CARET_STATE)
1689 if (req->state == -1) input->caret_state = !input->caret_state;
1690 else input->caret_state = !!req->state;