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 */
128 struct timeval last_get_msg; /* time of last get message call */
131 static void msg_queue_dump( struct object *obj, int verbose );
132 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
133 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
134 static int msg_queue_signaled( struct object *obj, struct thread *thread );
135 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
136 static void msg_queue_destroy( struct object *obj );
137 static void thread_input_dump( struct object *obj, int verbose );
138 static void thread_input_destroy( struct object *obj );
139 static void timer_callback( void *private );
141 static const struct object_ops msg_queue_ops =
143 sizeof(struct msg_queue), /* size */
144 msg_queue_dump, /* dump */
145 msg_queue_add_queue, /* add_queue */
146 msg_queue_remove_queue, /* remove_queue */
147 msg_queue_signaled, /* signaled */
148 msg_queue_satisfied, /* satisfied */
149 no_get_fd, /* get_fd */
150 msg_queue_destroy /* destroy */
154 static const struct object_ops thread_input_ops =
156 sizeof(struct thread_input), /* size */
157 thread_input_dump, /* dump */
158 no_add_queue, /* add_queue */
159 NULL, /* remove_queue */
161 NULL, /* satisfied */
162 no_get_fd, /* get_fd */
163 thread_input_destroy /* destroy */
166 /* pointer to input structure of foreground thread */
167 static struct thread_input *foreground_input;
170 /* set the caret window in a given thread input */
171 static void set_caret_window( struct thread_input *input, user_handle_t win )
174 input->caret_rect.left = 0;
175 input->caret_rect.top = 0;
176 input->caret_rect.right = 0;
177 input->caret_rect.bottom = 0;
178 input->caret_hide = 1;
179 input->caret_state = 0;
182 /* create a thread input object */
183 static struct thread_input *create_thread_input(void)
185 struct thread_input *input;
187 if ((input = alloc_object( &thread_input_ops )))
192 input->menu_owner = 0;
193 input->move_size = 0;
195 input->msg_thread = NULL;
196 input->msg_list.first = input->msg_list.last = NULL;
197 set_caret_window( input, 0 );
198 memset( input->keystate, 0, sizeof(input->keystate) );
203 /* create a message queue object */
204 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
206 struct msg_queue *queue;
209 if (!input && !(input = create_thread_input())) return NULL;
210 if ((queue = alloc_object( &msg_queue_ops )))
212 queue->wake_bits = 0;
213 queue->wake_mask = 0;
214 queue->changed_bits = 0;
215 queue->changed_mask = 0;
216 queue->paint_count = 0;
217 queue->send_result = NULL;
218 queue->recv_result = NULL;
219 queue->first_timer = NULL;
220 queue->last_timer = NULL;
221 queue->next_timer = NULL;
222 queue->timeout = NULL;
223 queue->input = (struct thread_input *)grab_object( input );
225 gettimeofday( &queue->last_get_msg, NULL );
226 for (i = 0; i < NB_MSG_KINDS; i++)
227 queue->msg_list[i].first = queue->msg_list[i].last = NULL;
229 thread->queue = queue;
230 if (!thread->process->queue)
231 thread->process->queue = (struct msg_queue *)grab_object( queue );
233 release_object( input );
237 /* free the message queue of a thread at thread exit */
238 void free_msg_queue( struct thread *thread )
240 struct process *process = thread->process;
242 if (!thread->queue) return;
243 if (process->queue == thread->queue) /* is it the process main queue? */
245 release_object( process->queue );
246 process->queue = NULL;
247 if (process->idle_event)
249 set_event( process->idle_event );
250 release_object( process->idle_event );
251 process->idle_event = NULL;
254 release_object( thread->queue );
255 thread->queue = NULL;
258 /* get the hook table for a given thread */
259 struct hook_table *get_queue_hooks( struct thread *thread )
261 if (!thread->queue) return NULL;
262 return thread->queue->hooks;
265 /* set the hook table for a given thread, allocating the queue if needed */
266 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
268 struct msg_queue *queue = thread->queue;
269 if (!queue) queue = create_msg_queue( thread, NULL );
270 if (queue->hooks) release_object( queue->hooks );
271 queue->hooks = hooks;
274 /* check the queue status */
275 inline static int is_signaled( struct msg_queue *queue )
277 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
280 /* set some queue bits */
281 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
283 queue->wake_bits |= bits;
284 queue->changed_bits |= bits;
285 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
288 /* clear some queue bits */
289 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
291 queue->wake_bits &= ~bits;
292 queue->changed_bits &= ~bits;
295 /* check whether msg is a keyboard message */
296 inline static int is_keyboard_msg( struct message *msg )
298 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
301 /* get the QS_* bit corresponding to a given hardware message */
302 inline static int get_hardware_msg_bit( struct message *msg )
304 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
305 if (is_keyboard_msg( msg )) return QS_KEY;
306 return QS_MOUSEBUTTON;
309 /* get the current thread queue, creating it if needed */
310 inline static struct msg_queue *get_current_queue(void)
312 struct msg_queue *queue = current->queue;
313 if (!queue) queue = create_msg_queue( current, NULL );
317 /* append a message to the end of a list */
318 inline static void append_message( struct message_list *list, struct message *msg )
321 if ((msg->prev = list->last)) msg->prev->next = msg;
322 else list->first = msg;
326 /* unlink a message from a list it */
327 inline static void unlink_message( struct message_list *list, struct message *msg )
329 if (msg->next) msg->next->prev = msg->prev;
330 else list->last = msg->prev;
331 if (msg->prev) msg->prev->next = msg->next;
332 else list->first = msg->next;
335 /* try to merge a message with the last in the list; return 1 if successful */
336 static int merge_message( struct thread_input *input, const struct message *msg )
338 struct message *prev = input->msg_list.last;
341 if (input->msg == prev) return 0;
342 if (prev->result) return 0;
343 if (prev->win != msg->win) return 0;
344 if (prev->msg != msg->msg) return 0;
345 if (prev->type != msg->type) return 0;
346 /* now we can merge it */
347 prev->wparam = msg->wparam;
348 prev->lparam = msg->lparam;
351 prev->time = msg->time;
352 prev->info = msg->info;
356 /* free a result structure */
357 static void free_result( struct message_result *result )
359 if (result->timeout) remove_timeout_user( result->timeout );
360 if (result->data) free( result->data );
364 /* store the message result in the appropriate structure */
365 static void store_message_result( struct message_result *res, unsigned int result,
368 res->result = result;
373 remove_timeout_user( res->timeout );
376 /* wake sender queue if waiting on this result */
377 if (res->sender && res->sender->send_result == res)
378 set_queue_bits( res->sender, QS_SMRESULT );
381 /* free a message when deleting a queue or window */
382 static void free_message( struct message *msg )
384 struct message_result *result = msg->result;
389 result->receiver = NULL;
390 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
392 else free_result( result );
394 if (msg->data) free( msg->data );
398 /* remove (and free) a message from a message list */
399 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
400 enum message_kind kind )
402 unlink_message( &queue->msg_list[kind], msg );
406 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_SENDMESSAGE );
409 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_POSTMESSAGE );
415 /* message timed out without getting a reply */
416 static void result_timeout( void *private )
418 struct message_result *result = private;
420 assert( !result->replied );
422 result->timeout = NULL;
423 store_message_result( result, 0, STATUS_TIMEOUT );
426 /* allocate and fill a message result structure */
427 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
428 struct msg_queue *recv_queue,
429 unsigned int timeout )
431 struct message_result *result = mem_alloc( sizeof(*result) );
434 /* put the result on the sender result stack */
435 result->sender = send_queue;
436 result->receiver = recv_queue;
439 result->data_size = 0;
440 result->timeout = NULL;
441 result->send_next = send_queue->send_result;
442 send_queue->send_result = result;
446 gettimeofday( &when, 0 );
447 add_timeout( &when, timeout );
448 result->timeout = add_timeout_user( &when, result_timeout, result );
454 /* receive a message, removing it from the sent queue */
455 static void receive_message( struct msg_queue *queue, struct message *msg,
456 struct get_message_reply *reply )
458 struct message_result *result = msg->result;
460 reply->total = msg->data_size;
461 if (msg->data_size > get_reply_max_size())
463 set_error( STATUS_BUFFER_OVERFLOW );
466 reply->type = msg->type;
467 reply->win = msg->win;
468 reply->msg = msg->msg;
469 reply->wparam = msg->wparam;
470 reply->lparam = msg->lparam;
473 reply->time = msg->time;
474 reply->info = msg->info;
476 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
478 unlink_message( &queue->msg_list[SEND_MESSAGE], msg );
479 /* put the result on the receiver result stack */
482 result->recv_next = queue->recv_result;
483 queue->recv_result = result;
486 if (!queue->msg_list[SEND_MESSAGE].first) clear_queue_bits( queue, QS_SENDMESSAGE );
489 /* set the result of the current received message */
490 static void reply_message( struct msg_queue *queue, unsigned int result,
491 unsigned int error, int remove, const void *data, size_t len )
493 struct message_result *res = queue->recv_result;
497 queue->recv_result = res->recv_next;
498 res->receiver = NULL;
499 if (!res->sender) /* no one waiting for it */
507 if (len && (res->data = memdup( data, len ))) res->data_size = len;
508 store_message_result( res, result, error );
512 /* retrieve a posted message */
513 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
514 unsigned int first, unsigned int last, unsigned int flags,
515 struct get_message_reply *reply )
518 struct message_list *list = &queue->msg_list[POST_MESSAGE];
520 /* check against the filters */
521 for (msg = list->first; msg; msg = msg->next)
523 if (msg->msg == WM_QUIT) break; /* WM_QUIT is never filtered */
524 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
525 if (msg->msg < first) continue;
526 if (msg->msg > last) continue;
527 break; /* found one */
531 /* return it to the app */
533 reply->total = msg->data_size;
534 if (msg->data_size > get_reply_max_size())
536 set_error( STATUS_BUFFER_OVERFLOW );
539 reply->type = msg->type;
540 reply->win = msg->win;
541 reply->msg = msg->msg;
542 reply->wparam = msg->wparam;
543 reply->lparam = msg->lparam;
546 reply->time = msg->time;
547 reply->info = msg->info;
549 if (flags & GET_MSG_REMOVE)
553 set_reply_data_ptr( msg->data, msg->data_size );
557 remove_queue_message( queue, msg, POST_MESSAGE );
559 else if (msg->data) set_reply_data( msg->data, msg->data_size );
564 /* empty a message list and free all the messages */
565 static void empty_msg_list( struct message_list *list )
567 struct message *msg = list->first;
570 struct message *next = msg->next;
576 /* cleanup all pending results when deleting a queue */
577 static void cleanup_results( struct msg_queue *queue )
579 struct message_result *result, *next;
581 result = queue->send_result;
584 next = result->send_next;
585 result->sender = NULL;
586 if (!result->receiver) free_result( result );
590 while (queue->recv_result)
591 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
594 /* check if the thread owning the queue is hung (not checking for messages) */
595 static int is_queue_hung( struct msg_queue *queue )
598 struct wait_queue_entry *entry;
600 gettimeofday( &now, NULL );
601 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
602 return 0; /* less than 5 seconds since last get message -> not hung */
604 for (entry = queue->obj.head; entry; entry = entry->next)
606 if (entry->thread->queue == queue)
607 return 0; /* thread is waiting on queue -> not hung */
612 static int msg_queue_add_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 /* a thread can only wait on its own queue */
618 if (entry->thread->queue != queue)
620 set_error( STATUS_ACCESS_DENIED );
623 /* if waiting on the main process queue, set the idle event */
624 if (process->queue == queue)
626 if (process->idle_event) set_event( process->idle_event );
628 add_queue( obj, entry );
632 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
634 struct msg_queue *queue = (struct msg_queue *)obj;
635 struct process *process = entry->thread->process;
637 remove_queue( obj, entry );
639 assert( entry->thread->queue == queue );
641 /* if waiting on the main process queue, reset the idle event */
642 if (process->queue == queue)
644 if (process->idle_event) reset_event( process->idle_event );
648 static void msg_queue_dump( struct object *obj, int verbose )
650 struct msg_queue *queue = (struct msg_queue *)obj;
651 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
652 queue->wake_bits, queue->wake_mask );
655 static int msg_queue_signaled( struct object *obj, struct thread *thread )
657 struct msg_queue *queue = (struct msg_queue *)obj;
658 return is_signaled( queue );
661 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
663 struct msg_queue *queue = (struct msg_queue *)obj;
664 queue->wake_mask = 0;
665 queue->changed_mask = 0;
666 return 0; /* Not abandoned */
669 static void msg_queue_destroy( struct object *obj )
671 struct msg_queue *queue = (struct msg_queue *)obj;
672 struct timer *timer = queue->first_timer;
675 cleanup_results( queue );
676 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
680 struct timer *next = timer->next;
684 if (queue->timeout) remove_timeout_user( queue->timeout );
685 if (queue->input) release_object( queue->input );
686 if (queue->hooks) release_object( queue->hooks );
689 static void thread_input_dump( struct object *obj, int verbose )
691 struct thread_input *input = (struct thread_input *)obj;
692 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
693 input->focus, input->capture, input->active );
696 static void thread_input_destroy( struct object *obj )
698 struct thread_input *input = (struct thread_input *)obj;
700 if (foreground_input == input) foreground_input = NULL;
701 if (input->msg_thread) release_object( input->msg_thread );
702 empty_msg_list( &input->msg_list );
705 /* fix the thread input data when a window is destroyed */
706 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
708 struct thread_input *input = queue->input;
710 if (window == input->focus) input->focus = 0;
711 if (window == input->capture) input->capture = 0;
712 if (window == input->active) input->active = 0;
713 if (window == input->menu_owner) input->menu_owner = 0;
714 if (window == input->move_size) input->move_size = 0;
715 if (window == input->caret) set_caret_window( input, 0 );
718 /* check if the specified window can be set in the input data of a given queue */
719 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
721 struct thread *thread;
724 if (!window) return 1; /* we can always clear the data */
726 if ((thread = get_window_thread( window )))
728 ret = (queue->input == thread->queue->input);
729 if (!ret) set_error( STATUS_ACCESS_DENIED );
730 release_object( thread );
732 else set_error( STATUS_INVALID_HANDLE );
737 /* attach two thread input data structures */
738 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
740 struct thread_input *input;
742 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
743 input = (struct thread_input *)grab_object( thread_to->queue->input );
745 if (thread_from->queue)
747 release_object( thread_from->queue->input );
748 thread_from->queue->input = input;
752 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
754 memset( input->keystate, 0, sizeof(input->keystate) );
758 /* detach two thread input data structures */
759 static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
761 struct thread_input *input;
763 if (!thread_from->queue || !thread_to->queue ||
764 thread_from->queue->input != thread_to->queue->input)
766 set_error( STATUS_ACCESS_DENIED );
769 if ((input = create_thread_input()))
771 release_object( thread_from->queue->input );
772 thread_from->queue->input = input;
777 /* set the next timer to expire */
778 static void set_next_timer( struct msg_queue *queue, struct timer *timer )
782 remove_timeout_user( queue->timeout );
783 queue->timeout = NULL;
785 if ((queue->next_timer = timer))
786 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
788 /* set/clear QS_TIMER bit */
789 if (queue->next_timer == queue->first_timer)
790 clear_queue_bits( queue, QS_TIMER );
792 set_queue_bits( queue, QS_TIMER );
795 /* callback for the next timer expiration */
796 static void timer_callback( void *private )
798 struct msg_queue *queue = private;
800 queue->timeout = NULL;
801 /* move on to the next timer */
802 set_next_timer( queue, queue->next_timer->next );
805 /* link a timer at its rightful place in the queue list */
806 static void link_timer( struct msg_queue *queue, struct timer *timer )
808 struct timer *pos = queue->next_timer;
810 while (pos && time_before( &pos->when, &timer->when )) pos = pos->next;
812 if (pos) /* insert before pos */
814 if ((timer->prev = pos->prev)) timer->prev->next = timer;
815 else queue->first_timer = timer;
819 else /* insert at end */
822 timer->prev = queue->last_timer;
823 if (queue->last_timer) queue->last_timer->next = timer;
824 else queue->first_timer = timer;
825 queue->last_timer = timer;
827 /* check if we replaced the next timer */
828 if (pos == queue->next_timer) set_next_timer( queue, timer );
831 /* remove a timer from the queue timer list */
832 static void unlink_timer( struct msg_queue *queue, struct timer *timer )
834 if (timer->next) timer->next->prev = timer->prev;
835 else queue->last_timer = timer->prev;
836 if (timer->prev) timer->prev->next = timer->next;
837 else queue->first_timer = timer->next;
838 /* check if we removed the next timer */
839 if (queue->next_timer == timer) set_next_timer( queue, timer->next );
840 else if (queue->next_timer == queue->first_timer) clear_queue_bits( queue, QS_TIMER );
843 /* restart an expired timer */
844 static void restart_timer( struct msg_queue *queue, struct timer *timer )
847 unlink_timer( queue, timer );
848 gettimeofday( &now, 0 );
849 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
850 link_timer( queue, timer );
853 /* find an expired timer matching the filtering parameters */
854 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
855 unsigned int get_first, unsigned int get_last,
859 for (timer = queue->first_timer; (timer && timer != queue->next_timer); timer = timer->next)
861 if (win && timer->win != win) continue;
862 if (timer->msg >= get_first && timer->msg <= get_last)
864 if (remove) restart_timer( queue, timer );
872 static int kill_timer( struct msg_queue *queue, user_handle_t win,
873 unsigned int msg, unsigned int id )
877 for (timer = queue->first_timer; timer; timer = timer->next)
879 if (timer->win != win || timer->msg != msg || timer->id != id) continue;
880 unlink_timer( queue, timer );
888 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
890 struct timer *timer = mem_alloc( sizeof(*timer) );
894 gettimeofday( &timer->when, 0 );
895 add_timeout( &timer->when, rate );
896 link_timer( queue, timer );
901 /* change the input key state for a given key */
902 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
906 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
907 input->keystate[key] |= 0x80;
909 else input->keystate[key] &= ~0x80;
912 /* update the input key state for a keyboard message */
913 static void update_input_key_state( struct thread_input *input, const struct message *msg )
916 int down = 0, extended;
924 set_input_key_state( input, VK_LBUTTON, down );
930 set_input_key_state( input, VK_MBUTTON, down );
936 set_input_key_state( input, VK_RBUTTON, down );
944 key = (unsigned char)msg->wparam;
945 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
946 set_input_key_state( input, key, down );
950 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
953 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
956 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
963 /* release the hardware message currently being processed by the given thread */
964 static void release_hardware_message( struct thread *thread, int remove )
966 struct thread_input *input = thread->queue->input;
968 if (input->msg_thread != thread) return;
971 struct message *other;
974 update_input_key_state( input, input->msg );
975 unlink_message( &input->msg_list, input->msg );
976 clr_bit = get_hardware_msg_bit( input->msg );
977 for (other = input->msg_list.first; other; other = other->next)
978 if (get_hardware_msg_bit( other ) == clr_bit) break;
979 if (!other) clear_queue_bits( thread->queue, clr_bit );
980 free_message( input->msg );
982 release_object( input->msg_thread );
984 input->msg_thread = NULL;
987 /* find the window that should receive a given hardware message */
988 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
989 unsigned int *msg_code )
991 user_handle_t win = 0;
993 *msg_code = msg->msg;
994 if (is_keyboard_msg( msg ))
996 if (input && !(win = input->focus))
999 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1002 else /* mouse message */
1004 if (!input || !(win = input->capture))
1006 if (!(win = msg->win)) win = window_from_point( msg->x, msg->y );
1012 /* queue a hardware message into a given thread input */
1013 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1016 struct thread *thread;
1017 struct thread_input *input;
1018 unsigned int msg_code;
1020 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1021 if (!win || !(thread = get_window_thread(win)))
1026 input = thread->queue->input;
1028 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1031 append_message( &input->msg_list, msg );
1032 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1034 release_object( thread );
1037 /* find a hardware message for the given queue */
1038 static int get_hardware_message( struct thread *thread, struct message *first,
1039 user_handle_t filter_win, struct get_message_reply *reply )
1041 struct thread_input *input = thread->queue->input;
1042 struct thread *win_thread;
1043 struct message *msg;
1045 int clear_bits, got_one = 0;
1046 unsigned int msg_code;
1048 if (input->msg_thread && input->msg_thread != thread)
1049 return 0; /* locked by another thread */
1053 msg = input->msg_list.first;
1054 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1059 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1064 win = find_hardware_message_window( input, msg, &msg_code );
1065 if (!win || !(win_thread = get_window_thread( win )))
1067 /* no window at all, remove it */
1068 struct message *next = msg->next;
1069 update_input_key_state( input, msg );
1070 unlink_message( &input->msg_list, msg );
1071 free_message( msg );
1075 if (win_thread != thread)
1077 /* wake the other thread */
1078 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1079 release_object( win_thread );
1084 /* if we already got a message for another thread, or if it doesn't
1085 * match the filter we skip it (filter is only checked for keyboard
1086 * messages since the dest window for a mouse message depends on hittest)
1089 (filter_win && is_keyboard_msg(msg) &&
1090 win != filter_win && !is_child_window( filter_win, win )))
1092 clear_bits &= ~get_hardware_msg_bit( msg );
1093 release_object( win_thread );
1097 /* now we can return it */
1098 if (!input->msg_thread) input->msg_thread = win_thread;
1099 else release_object( win_thread );
1102 reply->type = MSG_HARDWARE;
1104 reply->msg = msg_code;
1105 reply->wparam = msg->wparam;
1106 reply->lparam = msg->lparam;
1109 reply->time = msg->time;
1110 reply->info = msg->info;
1113 /* nothing found, clear the hardware queue bits */
1114 clear_queue_bits( thread->queue, clear_bits );
1115 if (input->msg_thread) release_object( input->msg_thread );
1117 input->msg_thread = NULL;
1121 /* increment (or decrement if 'incr' is negative) the queue paint count */
1122 void inc_queue_paint_count( struct thread *thread, int incr )
1124 struct msg_queue *queue = thread->queue;
1128 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1130 if (queue->paint_count)
1131 set_queue_bits( queue, QS_PAINT );
1133 clear_queue_bits( queue, QS_PAINT );
1137 /* remove all messages and timers belonging to a certain window */
1138 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1140 struct msg_queue *queue = thread->queue;
1141 struct timer *timer;
1142 struct message *msg;
1148 timer = queue->first_timer;
1151 struct timer *next = timer->next;
1152 if (timer->win == win)
1154 unlink_timer( queue, timer );
1160 /* remove messages */
1161 for (i = 0; i < NB_MSG_KINDS; i++)
1163 msg = queue->msg_list[i].first;
1166 struct message *next = msg->next;
1167 if (msg->win == win) remove_queue_message( queue, msg, i );
1172 thread_input_cleanup_window( queue, win );
1175 /* post a message to a window; used by socket handling */
1176 void post_message( user_handle_t win, unsigned int message,
1177 unsigned int wparam, unsigned int lparam )
1179 struct message *msg;
1180 struct thread *thread = get_window_thread( win );
1182 if (!thread) return;
1184 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1186 msg->type = MSG_POSTED;
1187 msg->win = get_user_full_handle( win );
1189 msg->wparam = wparam;
1190 msg->lparam = lparam;
1191 msg->time = get_tick_count();
1199 append_message( &thread->queue->msg_list[POST_MESSAGE], msg );
1200 set_queue_bits( thread->queue, QS_POSTMESSAGE );
1202 release_object( thread );
1206 /* get the message queue of the current thread */
1207 DECL_HANDLER(get_msg_queue)
1209 struct msg_queue *queue = get_current_queue();
1212 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1216 /* set the current message queue wakeup mask */
1217 DECL_HANDLER(set_queue_mask)
1219 struct msg_queue *queue = get_current_queue();
1223 queue->wake_mask = req->wake_mask;
1224 queue->changed_mask = req->changed_mask;
1225 reply->wake_bits = queue->wake_bits;
1226 reply->changed_bits = queue->changed_bits;
1227 if (is_signaled( queue ))
1229 /* if skip wait is set, do what would have been done in the subsequent wait */
1230 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1231 else wake_up( &queue->obj, 0 );
1237 /* get the current message queue status */
1238 DECL_HANDLER(get_queue_status)
1240 struct msg_queue *queue = current->queue;
1243 reply->wake_bits = queue->wake_bits;
1244 reply->changed_bits = queue->changed_bits;
1245 if (req->clear) queue->changed_bits = 0;
1247 else reply->wake_bits = reply->changed_bits = 0;
1251 /* send a message to a thread queue */
1252 DECL_HANDLER(send_message)
1254 struct message *msg;
1255 struct msg_queue *send_queue = get_current_queue();
1256 struct msg_queue *recv_queue = NULL;
1257 struct thread *thread = NULL;
1261 if (!(thread = get_thread_from_id( req->id ))) return;
1263 else if (req->type != MSG_HARDWARE)
1265 /* only hardware messages are allowed without destination thread */
1266 set_error( STATUS_INVALID_PARAMETER );
1270 if (thread && !(recv_queue = thread->queue))
1272 set_error( STATUS_INVALID_PARAMETER );
1273 release_object( thread );
1276 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1278 set_error( STATUS_TIMEOUT );
1279 release_object( thread );
1283 if ((msg = mem_alloc( sizeof(*msg) )))
1285 msg->type = req->type;
1286 msg->win = get_user_full_handle( req->win );
1287 msg->msg = req->msg;
1288 msg->wparam = req->wparam;
1289 msg->lparam = req->lparam;
1290 msg->time = req->time;
1293 msg->info = req->info;
1300 case MSG_OTHER_PROCESS:
1301 msg->data_size = get_req_data_size();
1302 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1311 if (!(msg->result = alloc_message_result( send_queue, recv_queue, req->timeout )))
1318 append_message( &recv_queue->msg_list[SEND_MESSAGE], msg );
1319 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1322 /* needed for posted DDE messages */
1323 msg->data_size = get_req_data_size();
1324 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1329 append_message( &recv_queue->msg_list[POST_MESSAGE], msg );
1330 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1333 queue_hardware_message( recv_queue, msg );
1336 set_error( STATUS_INVALID_PARAMETER );
1341 if (thread) release_object( thread );
1345 /* get a message from the current queue */
1346 DECL_HANDLER(get_message)
1348 struct timer *timer;
1349 struct message *msg;
1350 struct message *first_hw_msg = NULL;
1351 struct msg_queue *queue = get_current_queue();
1352 user_handle_t get_win = get_user_full_handle( req->get_win );
1355 gettimeofday( &queue->last_get_msg, NULL );
1357 /* first of all release the hardware input lock if we own it */
1358 /* we'll grab it again if we find a hardware message */
1359 if (queue->input->msg_thread == current)
1361 first_hw_msg = queue->input->msg;
1362 release_hardware_message( current, 0 );
1365 /* first check for sent messages */
1366 if ((msg = queue->msg_list[SEND_MESSAGE].first))
1368 receive_message( queue, msg, reply );
1371 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1373 /* clear changed bits so we can wait on them if we don't find a message */
1374 queue->changed_bits = 0;
1376 /* then check for posted messages */
1377 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1380 /* then check for any raw hardware message */
1381 if (get_hardware_message( current, first_hw_msg, get_win, reply ))
1384 /* now check for WM_PAINT */
1385 if (queue->paint_count &&
1386 (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
1387 (reply->win = find_window_to_repaint( get_win, current )))
1389 reply->type = MSG_POSTED;
1390 reply->msg = WM_PAINT;
1395 reply->time = get_tick_count();
1400 /* now check for timer */
1401 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1402 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1404 reply->type = MSG_POSTED;
1405 reply->win = timer->win;
1406 reply->msg = timer->msg;
1407 reply->wparam = timer->id;
1408 reply->lparam = timer->lparam;
1411 reply->time = get_tick_count();
1417 set_error( STATUS_PENDING ); /* FIXME */
1421 /* reply to a sent message */
1422 DECL_HANDLER(reply_message)
1424 if (!current->queue)
1426 set_error( STATUS_ACCESS_DENIED );
1429 if (req->type == MSG_HARDWARE)
1431 struct thread_input *input = current->queue->input;
1432 if (input->msg_thread == current) release_hardware_message( current, req->remove );
1433 else set_error( STATUS_ACCESS_DENIED );
1435 else if (current->queue->recv_result)
1436 reply_message( current->queue, req->result, 0, req->remove,
1437 get_req_data(), get_req_data_size() );
1441 /* retrieve the reply for the last message sent */
1442 DECL_HANDLER(get_message_reply)
1444 struct msg_queue *queue = current->queue;
1448 struct message_result *result = queue->send_result;
1450 set_error( STATUS_PENDING );
1453 if (result && (result->replied || req->cancel))
1455 if (result->replied)
1457 reply->result = result->result;
1458 set_error( result->error );
1461 size_t data_len = min( result->data_size, get_reply_max_size() );
1462 set_reply_data_ptr( result->data, data_len );
1463 result->data = NULL;
1464 result->data_size = 0;
1467 queue->send_result = result->send_next;
1468 result->sender = NULL;
1469 if (!result->receiver) free_result( result );
1470 if (!queue->send_result || !queue->send_result->replied)
1471 clear_queue_bits( queue, QS_SMRESULT );
1474 else set_error( STATUS_ACCESS_DENIED );
1478 /* set a window timer */
1479 DECL_HANDLER(set_win_timer)
1481 struct timer *timer;
1482 struct msg_queue *queue = get_current_queue();
1483 user_handle_t win = get_user_full_handle( req->win );
1487 /* remove it if it existed already */
1488 if (win) kill_timer( queue, win, req->msg, req->id );
1490 if ((timer = set_timer( queue, req->rate )))
1493 timer->msg = req->msg;
1494 timer->id = req->id;
1495 timer->lparam = req->lparam;
1499 /* kill a window timer */
1500 DECL_HANDLER(kill_win_timer)
1502 struct msg_queue *queue = current->queue;
1504 if (!queue || !kill_timer( queue, get_user_full_handle(req->win), req->msg, req->id ))
1505 set_error( STATUS_INVALID_PARAMETER );
1509 /* attach (or detach) thread inputs */
1510 DECL_HANDLER(attach_thread_input)
1512 struct thread *thread_from = get_thread_from_id( req->tid_from );
1513 struct thread *thread_to = get_thread_from_id( req->tid_to );
1515 if (!thread_from || !thread_to)
1517 if (thread_from) release_object( thread_from );
1518 if (thread_to) release_object( thread_to );
1521 if (thread_from != thread_to)
1523 if (req->attach) attach_thread_input( thread_from, thread_to );
1524 else detach_thread_input( thread_from, thread_to );
1526 else set_error( STATUS_ACCESS_DENIED );
1527 release_object( thread_from );
1528 release_object( thread_to );
1532 /* get thread input data */
1533 DECL_HANDLER(get_thread_input)
1535 struct thread *thread = NULL;
1536 struct thread_input *input;
1540 if (!(thread = get_thread_from_id( req->tid ))) return;
1541 input = thread->queue ? thread->queue->input : NULL;
1543 else input = foreground_input; /* get the foreground thread info */
1547 reply->focus = input->focus;
1548 reply->capture = input->capture;
1549 reply->active = input->active;
1550 reply->menu_owner = input->menu_owner;
1551 reply->move_size = input->move_size;
1552 reply->caret = input->caret;
1553 reply->rect = input->caret_rect;
1560 reply->menu_owner = 0;
1561 reply->move_size = 0;
1563 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1565 /* foreground window is active window of foreground thread */
1566 reply->foreground = foreground_input ? foreground_input->active : 0;
1567 if (thread) release_object( thread );
1571 /* retrieve queue keyboard state for a given thread */
1572 DECL_HANDLER(get_key_state)
1574 struct thread *thread;
1575 struct thread_input *input;
1577 if (!(thread = get_thread_from_id( req->tid ))) return;
1578 input = thread->queue ? thread->queue->input : NULL;
1581 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1582 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1584 release_object( thread );
1588 /* set queue keyboard state for a given thread */
1589 DECL_HANDLER(set_key_state)
1591 struct thread *thread = NULL;
1592 struct thread_input *input;
1594 if (!(thread = get_thread_from_id( req->tid ))) return;
1595 input = thread->queue ? thread->queue->input : NULL;
1598 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1599 if (size) memcpy( input->keystate, get_req_data(), size );
1601 release_object( thread );
1605 /* set the system foreground window */
1606 DECL_HANDLER(set_foreground_window)
1608 struct msg_queue *queue = get_current_queue();
1610 reply->previous = foreground_input ? foreground_input->active : 0;
1611 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1612 reply->send_msg_new = FALSE;
1616 struct thread *thread;
1618 if (is_top_level_window( req->handle ) &&
1619 ((thread = get_window_thread( req->handle ))))
1621 foreground_input = thread->queue->input;
1622 reply->send_msg_new = (foreground_input != queue->input);
1623 release_object( thread );
1625 else set_error( STATUS_INVALID_HANDLE );
1627 else foreground_input = NULL;
1631 /* set the current thread focus window */
1632 DECL_HANDLER(set_focus_window)
1634 struct msg_queue *queue = get_current_queue();
1636 reply->previous = 0;
1637 if (queue && check_queue_input_window( queue, req->handle ))
1639 reply->previous = queue->input->focus;
1640 queue->input->focus = get_user_full_handle( req->handle );
1645 /* set the current thread active window */
1646 DECL_HANDLER(set_active_window)
1648 struct msg_queue *queue = get_current_queue();
1650 reply->previous = 0;
1651 if (queue && check_queue_input_window( queue, req->handle ))
1653 if (!req->handle || make_window_active( req->handle ))
1655 reply->previous = queue->input->active;
1656 queue->input->active = get_user_full_handle( req->handle );
1658 else set_error( STATUS_INVALID_HANDLE );
1663 /* set the current thread capture window */
1664 DECL_HANDLER(set_capture_window)
1666 struct msg_queue *queue = get_current_queue();
1668 reply->previous = reply->full_handle = 0;
1669 if (queue && check_queue_input_window( queue, req->handle ))
1671 struct thread_input *input = queue->input;
1673 reply->previous = input->capture;
1674 input->capture = get_user_full_handle( req->handle );
1675 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1676 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1677 reply->full_handle = input->capture;
1682 /* Set the current thread caret window */
1683 DECL_HANDLER(set_caret_window)
1685 struct msg_queue *queue = get_current_queue();
1687 reply->previous = 0;
1688 if (queue && check_queue_input_window( queue, req->handle ))
1690 struct thread_input *input = queue->input;
1692 reply->previous = input->caret;
1693 reply->old_rect = input->caret_rect;
1694 reply->old_hide = input->caret_hide;
1695 reply->old_state = input->caret_state;
1697 set_caret_window( input, get_user_full_handle(req->handle) );
1698 input->caret_rect.right = req->width;
1699 input->caret_rect.bottom = req->height;
1704 /* Set the current thread caret information */
1705 DECL_HANDLER(set_caret_info)
1707 struct msg_queue *queue = get_current_queue();
1708 struct thread_input *input;
1711 input = queue->input;
1712 reply->full_handle = input->caret;
1713 reply->old_rect = input->caret_rect;
1714 reply->old_hide = input->caret_hide;
1715 reply->old_state = input->caret_state;
1717 if (req->handle && get_user_full_handle(req->handle) != input->caret)
1719 set_error( STATUS_ACCESS_DENIED );
1722 if (req->flags & SET_CARET_POS)
1724 input->caret_rect.right += req->x - input->caret_rect.left;
1725 input->caret_rect.bottom += req->y - input->caret_rect.top;
1726 input->caret_rect.left = req->x;
1727 input->caret_rect.top = req->y;
1729 if (req->flags & SET_CARET_HIDE)
1731 input->caret_hide += req->hide;
1732 if (input->caret_hide < 0) input->caret_hide = 0;
1734 if (req->flags & SET_CARET_STATE)
1736 if (req->state == -1) input->caret_state = !input->caret_state;
1737 else input->caret_state = !!req->state;