Store the hardware messages in the thread input structure, not in the
[wine/multimedia.git] / server / queue.c
blobb913116364cda7875fb744cda888bcc524ed90e0
1 /*
2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
32 #include "handle.h"
33 #include "thread.h"
34 #include "process.h"
35 #include "request.h"
36 #include "user.h"
38 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
39 #define NB_MSG_KINDS (POST_MESSAGE+1)
42 struct message_result
44 struct message_result *send_next; /* next in sender list */
45 struct message_result *recv_next; /* next in receiver list */
46 struct msg_queue *sender; /* sender queue */
47 struct msg_queue *receiver; /* receiver queue */
48 int replied; /* has it been replied to? */
49 unsigned int result; /* reply result */
50 unsigned int error; /* error code to pass back to sender */
51 void *data; /* message reply data */
52 unsigned int data_size; /* size of message reply data */
53 struct timeout_user *timeout; /* result timeout */
56 struct message
58 struct message *next; /* next message in list */
59 struct message *prev; /* prev message in list */
60 enum message_type type; /* message type */
61 user_handle_t win; /* window handle */
62 unsigned int msg; /* message code */
63 unsigned int wparam; /* parameters */
64 unsigned int lparam; /* parameters */
65 int x; /* x position */
66 int y; /* y position */
67 unsigned int time; /* message time */
68 unsigned int info; /* extra info */
69 void *data; /* message data for sent messages */
70 unsigned int data_size; /* size of message data */
71 struct message_result *result; /* result in sender queue */
74 struct message_list
76 struct message *first; /* head of list */
77 struct message *last; /* tail of list */
80 struct timer
82 struct timer *next; /* next timer in list */
83 struct timer *prev; /* prev timer in list */
84 struct timeval when; /* next expiration */
85 unsigned int rate; /* timer rate in ms */
86 user_handle_t win; /* window handle */
87 unsigned int msg; /* message to post */
88 unsigned int id; /* timer id */
89 unsigned int lparam; /* lparam for message */
92 struct thread_input
94 struct object obj; /* object header */
95 user_handle_t focus; /* focus window */
96 user_handle_t capture; /* capture window */
97 user_handle_t active; /* active window */
98 user_handle_t menu_owner; /* current menu owner window */
99 user_handle_t move_size; /* current moving/resizing window */
100 user_handle_t caret; /* caret window */
101 rectangle_t caret_rect; /* caret rectangle */
102 int caret_hide; /* caret hide count */
103 int caret_state; /* caret on/off state */
104 struct message *msg; /* message currently processed */
105 struct thread *msg_thread; /* thread processing the message */
106 struct message_list msg_list; /* list of hardware messages */
107 unsigned char keystate[256]; /* state of each key */
110 struct msg_queue
112 struct object obj; /* object header */
113 unsigned int wake_bits; /* wakeup bits */
114 unsigned int wake_mask; /* wakeup mask */
115 unsigned int changed_bits; /* changed wakeup bits */
116 unsigned int changed_mask; /* changed wakeup mask */
117 int paint_count; /* pending paint messages count */
118 struct message_list msg_list[NB_MSG_KINDS]; /* lists of messages */
119 struct message_result *send_result; /* stack of sent messages waiting for result */
120 struct message_result *recv_result; /* stack of received messages waiting for result */
121 struct timer *first_timer; /* head of timer list */
122 struct timer *last_timer; /* tail of timer list */
123 struct timer *next_timer; /* next timer to expire */
124 struct timeout_user *timeout; /* timeout for next timer to expire */
125 struct thread_input *input; /* thread input descriptor */
128 static void msg_queue_dump( struct object *obj, int verbose );
129 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
130 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
131 static int msg_queue_signaled( struct object *obj, struct thread *thread );
132 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
133 static void msg_queue_destroy( struct object *obj );
134 static void thread_input_dump( struct object *obj, int verbose );
135 static void thread_input_destroy( struct object *obj );
136 static void timer_callback( void *private );
138 static const struct object_ops msg_queue_ops =
140 sizeof(struct msg_queue), /* size */
141 msg_queue_dump, /* dump */
142 msg_queue_add_queue, /* add_queue */
143 msg_queue_remove_queue, /* remove_queue */
144 msg_queue_signaled, /* signaled */
145 msg_queue_satisfied, /* satisfied */
146 NULL, /* get_poll_events */
147 NULL, /* poll_event */
148 no_get_fd, /* get_fd */
149 no_flush, /* flush */
150 no_get_file_info, /* get_file_info */
151 NULL, /* queue_async */
152 msg_queue_destroy /* destroy */
156 static const struct object_ops thread_input_ops =
158 sizeof(struct thread_input), /* size */
159 thread_input_dump, /* dump */
160 no_add_queue, /* add_queue */
161 NULL, /* remove_queue */
162 NULL, /* signaled */
163 NULL, /* satisfied */
164 NULL, /* get_poll_events */
165 NULL, /* poll_event */
166 no_get_fd, /* get_fd */
167 no_flush, /* flush */
168 no_get_file_info, /* get_file_info */
169 NULL, /* queue_async */
170 thread_input_destroy /* destroy */
173 /* pointer to input structure of foreground thread */
174 static struct thread_input *foreground_input;
177 /* set the caret window in a given thread input */
178 static void set_caret_window( struct thread_input *input, user_handle_t win )
180 input->caret = win;
181 input->caret_rect.left = 0;
182 input->caret_rect.top = 0;
183 input->caret_rect.right = 0;
184 input->caret_rect.bottom = 0;
185 input->caret_hide = 1;
186 input->caret_state = 0;
189 /* create a thread input object */
190 static struct thread_input *create_thread_input(void)
192 struct thread_input *input;
194 if ((input = alloc_object( &thread_input_ops, -1 )))
196 input->focus = 0;
197 input->capture = 0;
198 input->active = 0;
199 input->menu_owner = 0;
200 input->move_size = 0;
201 input->msg = NULL;
202 input->msg_thread = NULL;
203 input->msg_list.first = input->msg_list.last = NULL;
204 set_caret_window( input, 0 );
205 memset( input->keystate, 0, sizeof(input->keystate) );
207 return input;
210 /* create a message queue object */
211 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
213 struct msg_queue *queue;
214 int i;
216 if (!input && !(input = create_thread_input())) return NULL;
217 if ((queue = alloc_object( &msg_queue_ops, -1 )))
219 queue->wake_bits = 0;
220 queue->wake_mask = 0;
221 queue->changed_bits = 0;
222 queue->changed_mask = 0;
223 queue->paint_count = 0;
224 queue->send_result = NULL;
225 queue->recv_result = NULL;
226 queue->first_timer = NULL;
227 queue->last_timer = NULL;
228 queue->next_timer = NULL;
229 queue->timeout = NULL;
230 queue->input = (struct thread_input *)grab_object( input );
231 for (i = 0; i < NB_MSG_KINDS; i++)
232 queue->msg_list[i].first = queue->msg_list[i].last = NULL;
234 thread->queue = queue;
235 if (!thread->process->queue)
236 thread->process->queue = (struct msg_queue *)grab_object( queue );
238 release_object( input );
239 return queue;
242 /* free the message queue of a thread at thread exit */
243 void free_msg_queue( struct thread *thread )
245 struct process *process = thread->process;
247 if (!thread->queue) return;
248 if (process->queue == thread->queue) /* is it the process main queue? */
250 release_object( process->queue );
251 process->queue = NULL;
252 if (process->idle_event)
254 set_event( process->idle_event );
255 release_object( process->idle_event );
256 process->idle_event = NULL;
259 release_object( thread->queue );
260 thread->queue = NULL;
263 /* check the queue status */
264 inline static int is_signaled( struct msg_queue *queue )
266 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
269 /* set some queue bits */
270 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
272 queue->wake_bits |= bits;
273 queue->changed_bits |= bits;
274 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
277 /* clear some queue bits */
278 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
280 queue->wake_bits &= ~bits;
281 queue->changed_bits &= ~bits;
284 /* check whether msg is a keyboard message */
285 inline static int is_keyboard_msg( struct message *msg )
287 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
290 /* get the QS_* bit corresponding to a given hardware message */
291 inline static int get_hardware_msg_bit( struct message *msg )
293 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
294 if (is_keyboard_msg( msg )) return QS_KEY;
295 return QS_MOUSEBUTTON;
298 /* get the current thread queue, creating it if needed */
299 inline static struct msg_queue *get_current_queue(void)
301 struct msg_queue *queue = current->queue;
302 if (!queue) queue = create_msg_queue( current, NULL );
303 return queue;
306 /* append a message to the end of a list */
307 inline static void append_message( struct message_list *list, struct message *msg )
309 msg->next = NULL;
310 if ((msg->prev = list->last)) msg->prev->next = msg;
311 else list->first = msg;
312 list->last = msg;
315 /* unlink a message from a list it */
316 inline static void unlink_message( struct message_list *list, struct message *msg )
318 if (msg->next) msg->next->prev = msg->prev;
319 else list->last = msg->prev;
320 if (msg->prev) msg->prev->next = msg->next;
321 else list->first = msg->next;
324 /* try to merge a message with the last in the list; return 1 if successful */
325 static int merge_message( struct thread_input *input, const struct message *msg )
327 struct message *prev = input->msg_list.last;
329 if (!prev) return 0;
330 if (input->msg == prev) return 0;
331 if (prev->result) return 0;
332 if (prev->win != msg->win) return 0;
333 if (prev->msg != msg->msg) return 0;
334 if (prev->type != msg->type) return 0;
335 /* now we can merge it */
336 prev->wparam = msg->wparam;
337 prev->lparam = msg->lparam;
338 prev->x = msg->x;
339 prev->y = msg->y;
340 prev->time = msg->time;
341 prev->info = msg->info;
342 return 1;
345 /* free a result structure */
346 static void free_result( struct message_result *result )
348 if (result->timeout) remove_timeout_user( result->timeout );
349 if (result->data) free( result->data );
350 free( result );
353 /* store the message result in the appropriate structure */
354 static void store_message_result( struct message_result *res, unsigned int result,
355 unsigned int error )
357 res->result = result;
358 res->error = error;
359 res->replied = 1;
360 if (res->timeout)
362 remove_timeout_user( res->timeout );
363 res->timeout = NULL;
365 /* wake sender queue if waiting on this result */
366 if (res->sender && res->sender->send_result == res)
367 set_queue_bits( res->sender, QS_SMRESULT );
370 /* free a message when deleting a queue or window */
371 static void free_message( struct message *msg )
373 struct message_result *result = msg->result;
374 if (result)
376 if (result->sender)
378 result->receiver = NULL;
379 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
381 else free_result( result );
383 if (msg->data) free( msg->data );
384 free( msg );
387 /* remove (and free) a message from a message list */
388 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
389 enum message_kind kind )
391 unlink_message( &queue->msg_list[kind], msg );
392 switch(kind)
394 case SEND_MESSAGE:
395 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_SENDMESSAGE );
396 break;
397 case POST_MESSAGE:
398 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_POSTMESSAGE );
399 break;
401 free_message( msg );
404 /* message timed out without getting a reply */
405 static void result_timeout( void *private )
407 struct message_result *result = private;
409 assert( !result->replied );
411 result->timeout = NULL;
412 store_message_result( result, 0, STATUS_TIMEOUT );
415 /* allocate and fill a message result structure */
416 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
417 struct msg_queue *recv_queue,
418 unsigned int timeout )
420 struct message_result *result = mem_alloc( sizeof(*result) );
421 if (result)
423 /* put the result on the sender result stack */
424 result->sender = send_queue;
425 result->receiver = recv_queue;
426 result->replied = 0;
427 result->data = NULL;
428 result->data_size = 0;
429 result->timeout = NULL;
430 result->send_next = send_queue->send_result;
431 send_queue->send_result = result;
432 if (timeout != -1)
434 struct timeval when;
435 gettimeofday( &when, 0 );
436 add_timeout( &when, timeout );
437 result->timeout = add_timeout_user( &when, result_timeout, result );
440 return result;
443 /* receive a message, removing it from the sent queue */
444 static void receive_message( struct msg_queue *queue, struct message *msg,
445 struct get_message_reply *reply )
447 struct message_result *result = msg->result;
449 reply->total = msg->data_size;
450 if (msg->data_size > get_reply_max_size())
452 set_error( STATUS_BUFFER_OVERFLOW );
453 return;
455 reply->type = msg->type;
456 reply->win = msg->win;
457 reply->msg = msg->msg;
458 reply->wparam = msg->wparam;
459 reply->lparam = msg->lparam;
460 reply->x = msg->x;
461 reply->y = msg->y;
462 reply->time = msg->time;
463 reply->info = msg->info;
465 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
467 unlink_message( &queue->msg_list[SEND_MESSAGE], msg );
468 /* put the result on the receiver result stack */
469 if (result)
471 result->recv_next = queue->recv_result;
472 queue->recv_result = result;
474 free( msg );
475 if (!queue->msg_list[SEND_MESSAGE].first) clear_queue_bits( queue, QS_SENDMESSAGE );
478 /* set the result of the current received message */
479 static void reply_message( struct msg_queue *queue, unsigned int result,
480 unsigned int error, int remove, const void *data, size_t len )
482 struct message_result *res = queue->recv_result;
484 if (remove)
486 queue->recv_result = res->recv_next;
487 res->receiver = NULL;
488 if (!res->sender) /* no one waiting for it */
490 free_result( res );
491 return;
494 if (!res->replied)
496 if (len && (res->data = memdup( data, len ))) res->data_size = len;
497 store_message_result( res, result, error );
501 /* retrieve a posted message */
502 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
503 unsigned int first, unsigned int last, unsigned int flags,
504 struct get_message_reply *reply )
506 struct message *msg;
507 struct message_list *list = &queue->msg_list[POST_MESSAGE];
509 /* check against the filters */
510 for (msg = list->first; msg; msg = msg->next)
512 if (msg->msg == WM_QUIT) break; /* WM_QUIT is never filtered */
513 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
514 if (msg->msg < first) continue;
515 if (msg->msg > last) continue;
516 break; /* found one */
518 if (!msg) return 0;
520 /* return it to the app */
522 reply->total = msg->data_size;
523 if (msg->data_size > get_reply_max_size())
525 set_error( STATUS_BUFFER_OVERFLOW );
526 return 1;
528 reply->type = msg->type;
529 reply->win = msg->win;
530 reply->msg = msg->msg;
531 reply->wparam = msg->wparam;
532 reply->lparam = msg->lparam;
533 reply->x = msg->x;
534 reply->y = msg->y;
535 reply->time = msg->time;
536 reply->info = msg->info;
538 if (flags & GET_MSG_REMOVE)
540 if (msg->data)
542 set_reply_data_ptr( msg->data, msg->data_size );
543 msg->data = NULL;
544 msg->data_size = 0;
546 remove_queue_message( queue, msg, POST_MESSAGE );
548 else if (msg->data) set_reply_data( msg->data, msg->data_size );
550 return 1;
553 /* empty a message list and free all the messages */
554 static void empty_msg_list( struct message_list *list )
556 struct message *msg = list->first;
557 while (msg)
559 struct message *next = msg->next;
560 free_message( msg );
561 msg = next;
565 /* cleanup all pending results when deleting a queue */
566 static void cleanup_results( struct msg_queue *queue )
568 struct message_result *result, *next;
570 result = queue->send_result;
571 while (result)
573 next = result->send_next;
574 result->sender = NULL;
575 if (!result->receiver) free_result( result );
576 result = next;
579 while (queue->recv_result)
580 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
583 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
585 struct msg_queue *queue = (struct msg_queue *)obj;
586 struct process *process = entry->thread->process;
588 /* a thread can only wait on its own queue */
589 if (entry->thread->queue != queue)
591 set_error( STATUS_ACCESS_DENIED );
592 return 0;
594 /* if waiting on the main process queue, set the idle event */
595 if (process->queue == queue)
597 if (process->idle_event) set_event( process->idle_event );
599 add_queue( obj, entry );
600 return 1;
603 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
605 struct msg_queue *queue = (struct msg_queue *)obj;
606 struct process *process = entry->thread->process;
608 remove_queue( obj, entry );
610 assert( entry->thread->queue == queue );
612 /* if waiting on the main process queue, reset the idle event */
613 if (process->queue == queue)
615 if (process->idle_event) reset_event( process->idle_event );
619 static void msg_queue_dump( struct object *obj, int verbose )
621 struct msg_queue *queue = (struct msg_queue *)obj;
622 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
623 queue->wake_bits, queue->wake_mask );
626 static int msg_queue_signaled( struct object *obj, struct thread *thread )
628 struct msg_queue *queue = (struct msg_queue *)obj;
629 return is_signaled( queue );
632 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
634 struct msg_queue *queue = (struct msg_queue *)obj;
635 queue->wake_mask = 0;
636 queue->changed_mask = 0;
637 return 0; /* Not abandoned */
640 static void msg_queue_destroy( struct object *obj )
642 struct msg_queue *queue = (struct msg_queue *)obj;
643 struct timer *timer = queue->first_timer;
644 int i;
646 cleanup_results( queue );
647 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
649 while (timer)
651 struct timer *next = timer->next;
652 free( timer );
653 timer = next;
655 if (queue->timeout) remove_timeout_user( queue->timeout );
656 if (queue->input) release_object( queue->input );
659 static void thread_input_dump( struct object *obj, int verbose )
661 struct thread_input *input = (struct thread_input *)obj;
662 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
663 input->focus, input->capture, input->active );
666 static void thread_input_destroy( struct object *obj )
668 struct thread_input *input = (struct thread_input *)obj;
670 if (foreground_input == input) foreground_input = NULL;
671 if (input->msg_thread) release_object( input->msg_thread );
672 empty_msg_list( &input->msg_list );
675 /* fix the thread input data when a window is destroyed */
676 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
678 struct thread_input *input = queue->input;
680 if (window == input->focus) input->focus = 0;
681 if (window == input->capture) input->capture = 0;
682 if (window == input->active) input->active = 0;
683 if (window == input->menu_owner) input->menu_owner = 0;
684 if (window == input->move_size) input->move_size = 0;
685 if (window == input->caret) set_caret_window( input, 0 );
688 /* check if the specified window can be set in the input data of a given queue */
689 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
691 struct thread *thread;
692 int ret = 0;
694 if (!window) return 1; /* we can always clear the data */
696 if ((thread = get_window_thread( window )))
698 ret = (queue->input == thread->queue->input);
699 if (!ret) set_error( STATUS_ACCESS_DENIED );
700 release_object( thread );
702 else set_error( STATUS_INVALID_HANDLE );
704 return ret;
707 /* attach two thread input data structures */
708 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
710 struct thread_input *input;
712 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
713 input = (struct thread_input *)grab_object( thread_to->queue->input );
715 if (thread_from->queue)
717 release_object( thread_from->queue->input );
718 thread_from->queue->input = input;
720 else
722 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
724 memset( input->keystate, 0, sizeof(input->keystate) );
725 return 1;
728 /* detach two thread input data structures */
729 static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
731 struct thread_input *input;
733 if (!thread_from->queue || !thread_to->queue ||
734 thread_from->queue->input != thread_to->queue->input)
736 set_error( STATUS_ACCESS_DENIED );
737 return;
739 if ((input = create_thread_input()))
741 release_object( thread_from->queue->input );
742 thread_from->queue->input = input;
747 /* set the next timer to expire */
748 static void set_next_timer( struct msg_queue *queue, struct timer *timer )
750 if (queue->timeout)
752 remove_timeout_user( queue->timeout );
753 queue->timeout = NULL;
755 if ((queue->next_timer = timer))
756 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
758 /* set/clear QS_TIMER bit */
759 if (queue->next_timer == queue->first_timer)
760 clear_queue_bits( queue, QS_TIMER );
761 else
762 set_queue_bits( queue, QS_TIMER );
765 /* callback for the next timer expiration */
766 static void timer_callback( void *private )
768 struct msg_queue *queue = private;
770 queue->timeout = NULL;
771 /* move on to the next timer */
772 set_next_timer( queue, queue->next_timer->next );
775 /* link a timer at its rightful place in the queue list */
776 static void link_timer( struct msg_queue *queue, struct timer *timer )
778 struct timer *pos = queue->next_timer;
780 while (pos && time_before( &pos->when, &timer->when )) pos = pos->next;
782 if (pos) /* insert before pos */
784 if ((timer->prev = pos->prev)) timer->prev->next = timer;
785 else queue->first_timer = timer;
786 timer->next = pos;
787 pos->prev = timer;
789 else /* insert at end */
791 timer->next = NULL;
792 timer->prev = queue->last_timer;
793 if (queue->last_timer) queue->last_timer->next = timer;
794 else queue->first_timer = timer;
795 queue->last_timer = timer;
797 /* check if we replaced the next timer */
798 if (pos == queue->next_timer) set_next_timer( queue, timer );
801 /* remove a timer from the queue timer list */
802 static void unlink_timer( struct msg_queue *queue, struct timer *timer )
804 if (timer->next) timer->next->prev = timer->prev;
805 else queue->last_timer = timer->prev;
806 if (timer->prev) timer->prev->next = timer->next;
807 else queue->first_timer = timer->next;
808 /* check if we removed the next timer */
809 if (queue->next_timer == timer) set_next_timer( queue, timer->next );
810 else if (queue->next_timer == queue->first_timer) clear_queue_bits( queue, QS_TIMER );
813 /* restart an expired timer */
814 static void restart_timer( struct msg_queue *queue, struct timer *timer )
816 struct timeval now;
817 unlink_timer( queue, timer );
818 gettimeofday( &now, 0 );
819 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
820 link_timer( queue, timer );
823 /* find an expired timer matching the filtering parameters */
824 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
825 unsigned int get_first, unsigned int get_last,
826 int remove )
828 struct timer *timer;
829 for (timer = queue->first_timer; (timer && timer != queue->next_timer); timer = timer->next)
831 if (win && timer->win != win) continue;
832 if (timer->msg >= get_first && timer->msg <= get_last)
834 if (remove) restart_timer( queue, timer );
835 return timer;
838 return NULL;
841 /* kill a timer */
842 static int kill_timer( struct msg_queue *queue, user_handle_t win,
843 unsigned int msg, unsigned int id )
845 struct timer *timer;
847 for (timer = queue->first_timer; timer; timer = timer->next)
849 if (timer->win != win || timer->msg != msg || timer->id != id) continue;
850 unlink_timer( queue, timer );
851 free( timer );
852 return 1;
854 return 0;
857 /* add a timer */
858 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
860 struct timer *timer = mem_alloc( sizeof(*timer) );
861 if (timer)
863 timer->rate = rate;
864 gettimeofday( &timer->when, 0 );
865 add_timeout( &timer->when, rate );
866 link_timer( queue, timer );
868 return timer;
871 /* release the hardware message currently being processed by the given thread */
872 static void release_hardware_message( struct thread *thread, int remove )
874 struct thread_input *input = thread->queue->input;
876 if (input->msg_thread != thread) return;
877 if (remove)
879 struct message *other;
880 int clr_bit;
882 unlink_message( &input->msg_list, input->msg );
883 clr_bit = get_hardware_msg_bit( input->msg );
884 for (other = input->msg_list.first; other; other = other->next)
885 if (get_hardware_msg_bit( other ) == clr_bit) break;
886 if (!other) clear_queue_bits( thread->queue, clr_bit );
887 free_message( input->msg );
889 release_object( input->msg_thread );
890 input->msg = NULL;
891 input->msg_thread = NULL;
894 /* find the window that should receive a given hardware message */
895 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg )
897 user_handle_t win = 0;
899 if (is_keyboard_msg( msg ))
901 if (input && !(win = input->focus)) win = input->active;
903 else /* mouse message */
905 if (!input || !(win = input->capture))
907 if (!(win = msg->win)) win = window_from_point( msg->x, msg->y );
910 return win;
913 /* queue a hardware message into a given thread input */
914 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
916 user_handle_t win;
917 struct thread *thread;
918 struct thread_input *input;
920 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg );
921 if (!win || !(thread = get_window_thread(win)))
923 free( msg );
924 return;
926 input = thread->queue->input;
928 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
929 else
931 append_message( &input->msg_list, msg );
932 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
934 release_object( thread );
937 /* find a hardware message for the given queue */
938 static int get_hardware_message( struct thread *thread, struct message *first,
939 user_handle_t filter_win, struct get_message_reply *reply )
941 struct thread_input *input = thread->queue->input;
942 struct thread *win_thread;
943 struct message *msg;
944 user_handle_t win;
945 int clear_bits, got_one = 0;
947 if (input->msg_thread && input->msg_thread != thread)
948 return 0; /* locked by another thread */
950 if (!first)
952 msg = input->msg_list.first;
953 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
955 else
957 msg = first->next;
958 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
961 while (msg)
963 win = find_hardware_message_window( input, msg );
964 if (!win || !(win_thread = get_window_thread( win )))
966 /* no window at all, remove it */
967 struct message *next = msg->next;
968 unlink_message( &input->msg_list, msg );
969 free_message( msg );
970 msg = next;
971 continue;
973 if (win_thread != thread)
975 /* wake the other thread */
976 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
977 release_object( win_thread );
978 got_one = 1;
979 msg = msg->next;
980 continue;
982 /* if we already got a message for another thread, or if it doesn't
983 * match the filter we skip it (filter is only checked for keyboard
984 * messages since the dest window for a mouse message depends on hittest)
986 if (got_one ||
987 (filter_win && is_keyboard_msg(msg) &&
988 win != filter_win && !is_child_window( filter_win, win )))
990 clear_bits &= ~get_hardware_msg_bit( msg );
991 msg = msg->next;
992 continue;
994 /* now we can return it */
995 if (!input->msg_thread) input->msg_thread = win_thread;
996 else release_object( win_thread );
997 input->msg = msg;
999 reply->type = MSG_HARDWARE;
1000 reply->win = win;
1001 reply->msg = msg->msg;
1002 reply->wparam = msg->wparam;
1003 reply->lparam = msg->lparam;
1004 reply->x = msg->x;
1005 reply->y = msg->y;
1006 reply->time = msg->time;
1007 reply->info = msg->info;
1008 return 1;
1010 /* nothing found, clear the hardware queue bits */
1011 clear_queue_bits( thread->queue, clear_bits );
1012 if (input->msg_thread) release_object( input->msg_thread );
1013 input->msg = NULL;
1014 input->msg_thread = NULL;
1015 return 0;
1018 /* increment (or decrement if 'incr' is negative) the queue paint count */
1019 void inc_queue_paint_count( struct thread *thread, int incr )
1021 struct msg_queue *queue = thread->queue;
1023 assert( queue );
1025 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1027 if (queue->paint_count)
1028 set_queue_bits( queue, QS_PAINT );
1029 else
1030 clear_queue_bits( queue, QS_PAINT );
1034 /* remove all messages and timers belonging to a certain window */
1035 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1037 struct msg_queue *queue = thread->queue;
1038 struct timer *timer;
1039 struct message *msg;
1040 int i;
1042 if (!queue) return;
1044 /* remove timers */
1045 timer = queue->first_timer;
1046 while (timer)
1048 struct timer *next = timer->next;
1049 if (timer->win == win)
1051 unlink_timer( queue, timer );
1052 free( timer );
1054 timer = next;
1057 /* remove messages */
1058 for (i = 0; i < NB_MSG_KINDS; i++)
1060 msg = queue->msg_list[i].first;
1061 while (msg)
1063 struct message *next = msg->next;
1064 if (msg->win == win) remove_queue_message( queue, msg, i );
1065 msg = next;
1069 thread_input_cleanup_window( queue, win );
1072 /* post a message to a window; used by socket handling */
1073 void post_message( user_handle_t win, unsigned int message,
1074 unsigned int wparam, unsigned int lparam )
1076 struct message *msg;
1077 struct thread *thread = get_window_thread( win );
1079 if (!thread) return;
1081 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1083 msg->type = MSG_POSTED;
1084 msg->win = get_user_full_handle( win );
1085 msg->msg = message;
1086 msg->wparam = wparam;
1087 msg->lparam = lparam;
1088 msg->time = get_tick_count();
1089 msg->x = 0;
1090 msg->y = 0;
1091 msg->info = 0;
1092 msg->result = NULL;
1093 msg->data = NULL;
1094 msg->data_size = 0;
1096 append_message( &thread->queue->msg_list[POST_MESSAGE], msg );
1097 set_queue_bits( thread->queue, QS_POSTMESSAGE );
1099 release_object( thread );
1103 /* get the message queue of the current thread */
1104 DECL_HANDLER(get_msg_queue)
1106 struct msg_queue *queue = get_current_queue();
1108 reply->handle = 0;
1109 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1113 /* set the current message queue wakeup mask */
1114 DECL_HANDLER(set_queue_mask)
1116 struct msg_queue *queue = get_current_queue();
1118 if (queue)
1120 queue->wake_mask = req->wake_mask;
1121 queue->changed_mask = req->changed_mask;
1122 reply->wake_bits = queue->wake_bits;
1123 reply->changed_bits = queue->changed_bits;
1124 if (is_signaled( queue ))
1126 /* if skip wait is set, do what would have been done in the subsequent wait */
1127 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1128 else wake_up( &queue->obj, 0 );
1134 /* get the current message queue status */
1135 DECL_HANDLER(get_queue_status)
1137 struct msg_queue *queue = current->queue;
1138 if (queue)
1140 reply->wake_bits = queue->wake_bits;
1141 reply->changed_bits = queue->changed_bits;
1142 if (req->clear) queue->changed_bits = 0;
1144 else reply->wake_bits = reply->changed_bits = 0;
1148 /* send a message to a thread queue */
1149 DECL_HANDLER(send_message)
1151 struct message *msg;
1152 struct msg_queue *send_queue = get_current_queue();
1153 struct msg_queue *recv_queue = NULL;
1154 struct thread *thread = NULL;
1156 if (req->id)
1158 if (!(thread = get_thread_from_id( req->id ))) return;
1160 else if (req->type != MSG_HARDWARE)
1162 /* only hardware messages are allowed without destination thread */
1163 set_error( STATUS_INVALID_PARAMETER );
1164 return;
1167 if (thread && !(recv_queue = thread->queue))
1169 set_error( STATUS_INVALID_PARAMETER );
1170 release_object( thread );
1171 return;
1174 if ((msg = mem_alloc( sizeof(*msg) )))
1176 msg->type = req->type;
1177 msg->win = get_user_full_handle( req->win );
1178 msg->msg = req->msg;
1179 msg->wparam = req->wparam;
1180 msg->lparam = req->lparam;
1181 msg->time = req->time;
1182 msg->x = req->x;
1183 msg->y = req->y;
1184 msg->info = req->info;
1185 msg->result = NULL;
1186 msg->data = NULL;
1187 msg->data_size = 0;
1189 switch(msg->type)
1191 case MSG_OTHER_PROCESS:
1192 msg->data_size = get_req_data_size();
1193 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1195 free( msg );
1196 break;
1198 /* fall through */
1199 case MSG_ASCII:
1200 case MSG_UNICODE:
1201 case MSG_CALLBACK:
1202 if (!(msg->result = alloc_message_result( send_queue, recv_queue, req->timeout )))
1204 free( msg );
1205 break;
1207 /* fall through */
1208 case MSG_NOTIFY:
1209 append_message( &recv_queue->msg_list[SEND_MESSAGE], msg );
1210 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1211 break;
1212 case MSG_POSTED:
1213 /* needed for posted DDE messages */
1214 msg->data_size = get_req_data_size();
1215 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1217 free( msg );
1218 break;
1220 append_message( &recv_queue->msg_list[POST_MESSAGE], msg );
1221 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1222 break;
1223 case MSG_HARDWARE:
1224 queue_hardware_message( recv_queue, msg );
1225 break;
1226 default:
1227 set_error( STATUS_INVALID_PARAMETER );
1228 free( msg );
1229 break;
1232 if (thread) release_object( thread );
1236 /* get a message from the current queue */
1237 DECL_HANDLER(get_message)
1239 struct timer *timer;
1240 struct message *msg;
1241 struct message *first_hw_msg = NULL;
1242 struct msg_queue *queue = get_current_queue();
1243 user_handle_t get_win = get_user_full_handle( req->get_win );
1245 if (!queue) return;
1247 /* first of all release the hardware input lock if we own it */
1248 /* we'll grab it again if we find a hardware message */
1249 if (queue->input->msg_thread == current)
1251 first_hw_msg = queue->input->msg;
1252 release_hardware_message( current, 0 );
1255 /* first check for sent messages */
1256 if ((msg = queue->msg_list[SEND_MESSAGE].first))
1258 receive_message( queue, msg, reply );
1259 return;
1261 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1263 /* clear changed bits so we can wait on them if we don't find a message */
1264 queue->changed_bits = 0;
1266 /* then check for posted messages */
1267 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1268 return;
1270 /* then check for any raw hardware message */
1271 if (get_hardware_message( current, first_hw_msg, get_win, reply ))
1272 return;
1274 /* now check for WM_PAINT */
1275 if (queue->paint_count &&
1276 (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
1277 (reply->win = find_window_to_repaint( get_win, current )))
1279 reply->type = MSG_POSTED;
1280 reply->msg = WM_PAINT;
1281 reply->wparam = 0;
1282 reply->lparam = 0;
1283 reply->x = 0;
1284 reply->y = 0;
1285 reply->time = get_tick_count();
1286 reply->info = 0;
1287 return;
1290 /* now check for timer */
1291 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1292 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1294 reply->type = MSG_POSTED;
1295 reply->win = timer->win;
1296 reply->msg = timer->msg;
1297 reply->wparam = timer->id;
1298 reply->lparam = timer->lparam;
1299 reply->x = 0;
1300 reply->y = 0;
1301 reply->time = get_tick_count();
1302 reply->info = 0;
1303 return;
1306 done:
1307 set_error( STATUS_PENDING ); /* FIXME */
1311 /* reply to a sent message */
1312 DECL_HANDLER(reply_message)
1314 if (!current->queue)
1316 set_error( STATUS_ACCESS_DENIED );
1317 return;
1319 if (current->queue->recv_result)
1320 reply_message( current->queue, req->result, 0, req->remove,
1321 get_req_data(), get_req_data_size() );
1322 else
1324 struct thread_input *input = current->queue->input;
1325 if (input->msg_thread == current) release_hardware_message( current, req->remove );
1326 else set_error( STATUS_ACCESS_DENIED );
1331 /* retrieve the reply for the last message sent */
1332 DECL_HANDLER(get_message_reply)
1334 struct msg_queue *queue = current->queue;
1336 if (queue)
1338 struct message_result *result = queue->send_result;
1340 set_error( STATUS_PENDING );
1341 reply->result = 0;
1343 if (result && (result->replied || req->cancel))
1345 if (result->replied)
1347 reply->result = result->result;
1348 set_error( result->error );
1349 if (result->data)
1351 size_t data_len = min( result->data_size, get_reply_max_size() );
1352 set_reply_data_ptr( result->data, data_len );
1353 result->data = NULL;
1354 result->data_size = 0;
1357 queue->send_result = result->send_next;
1358 result->sender = NULL;
1359 if (!result->receiver) free_result( result );
1360 if (!queue->send_result || !queue->send_result->replied)
1361 clear_queue_bits( queue, QS_SMRESULT );
1364 else set_error( STATUS_ACCESS_DENIED );
1368 /* set a window timer */
1369 DECL_HANDLER(set_win_timer)
1371 struct timer *timer;
1372 struct msg_queue *queue = get_current_queue();
1373 user_handle_t win = get_user_full_handle( req->win );
1375 if (!queue) return;
1377 /* remove it if it existed already */
1378 if (win) kill_timer( queue, win, req->msg, req->id );
1380 if ((timer = set_timer( queue, req->rate )))
1382 timer->win = win;
1383 timer->msg = req->msg;
1384 timer->id = req->id;
1385 timer->lparam = req->lparam;
1389 /* kill a window timer */
1390 DECL_HANDLER(kill_win_timer)
1392 struct msg_queue *queue = current->queue;
1394 if (!queue || !kill_timer( queue, get_user_full_handle(req->win), req->msg, req->id ))
1395 set_error( STATUS_INVALID_PARAMETER );
1399 /* attach (or detach) thread inputs */
1400 DECL_HANDLER(attach_thread_input)
1402 struct thread *thread_from = get_thread_from_id( req->tid_from );
1403 struct thread *thread_to = get_thread_from_id( req->tid_to );
1405 if (!thread_from || !thread_to)
1407 if (thread_from) release_object( thread_from );
1408 if (thread_to) release_object( thread_to );
1409 return;
1411 if (thread_from != thread_to)
1413 if (req->attach) attach_thread_input( thread_from, thread_to );
1414 else detach_thread_input( thread_from, thread_to );
1416 else set_error( STATUS_ACCESS_DENIED );
1417 release_object( thread_from );
1418 release_object( thread_to );
1422 /* get thread input data */
1423 DECL_HANDLER(get_thread_input)
1425 struct thread *thread = NULL;
1426 struct thread_input *input;
1428 if (req->tid)
1430 if (!(thread = get_thread_from_id( req->tid ))) return;
1431 input = thread->queue ? thread->queue->input : NULL;
1433 else input = foreground_input; /* get the foreground thread info */
1435 if (input)
1437 reply->focus = input->focus;
1438 reply->capture = input->capture;
1439 reply->active = input->active;
1440 reply->menu_owner = input->menu_owner;
1441 reply->move_size = input->move_size;
1442 reply->caret = input->caret;
1443 reply->rect = input->caret_rect;
1445 else
1447 reply->focus = 0;
1448 reply->capture = 0;
1449 reply->active = 0;
1450 reply->menu_owner = 0;
1451 reply->move_size = 0;
1452 reply->caret = 0;
1453 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1455 /* foreground window is active window of foreground thread */
1456 reply->foreground = foreground_input ? foreground_input->active : 0;
1457 if (thread) release_object( thread );
1461 /* set the system foreground window */
1462 DECL_HANDLER(set_foreground_window)
1464 struct msg_queue *queue = get_current_queue();
1466 reply->previous = foreground_input ? foreground_input->active : 0;
1467 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1468 reply->send_msg_new = FALSE;
1470 if (req->handle)
1472 struct thread *thread;
1474 if (is_top_level_window( req->handle ) &&
1475 ((thread = get_window_thread( req->handle ))))
1477 foreground_input = thread->queue->input;
1478 reply->send_msg_new = (foreground_input != queue->input);
1479 release_object( thread );
1481 else set_error( STATUS_INVALID_HANDLE );
1483 else foreground_input = NULL;
1487 /* set the current thread focus window */
1488 DECL_HANDLER(set_focus_window)
1490 struct msg_queue *queue = get_current_queue();
1492 reply->previous = 0;
1493 if (queue && check_queue_input_window( queue, req->handle ))
1495 reply->previous = queue->input->focus;
1496 queue->input->focus = get_user_full_handle( req->handle );
1501 /* set the current thread active window */
1502 DECL_HANDLER(set_active_window)
1504 struct msg_queue *queue = get_current_queue();
1506 reply->previous = 0;
1507 if (queue && check_queue_input_window( queue, req->handle ))
1509 if (!req->handle || make_window_active( req->handle ))
1511 reply->previous = queue->input->active;
1512 queue->input->active = get_user_full_handle( req->handle );
1514 else set_error( STATUS_INVALID_HANDLE );
1519 /* set the current thread capture window */
1520 DECL_HANDLER(set_capture_window)
1522 struct msg_queue *queue = get_current_queue();
1524 reply->previous = reply->full_handle = 0;
1525 if (queue && check_queue_input_window( queue, req->handle ))
1527 struct thread_input *input = queue->input;
1529 reply->previous = input->capture;
1530 input->capture = get_user_full_handle( req->handle );
1531 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1532 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1533 reply->full_handle = input->capture;
1538 /* Set the current thread caret window */
1539 DECL_HANDLER(set_caret_window)
1541 struct msg_queue *queue = get_current_queue();
1543 reply->previous = 0;
1544 if (queue && check_queue_input_window( queue, req->handle ))
1546 struct thread_input *input = queue->input;
1548 reply->previous = input->caret;
1549 reply->old_rect = input->caret_rect;
1550 reply->old_hide = input->caret_hide;
1551 reply->old_state = input->caret_state;
1553 set_caret_window( input, get_user_full_handle(req->handle) );
1554 input->caret_rect.right = req->width;
1555 input->caret_rect.bottom = req->height;
1560 /* Set the current thread caret information */
1561 DECL_HANDLER(set_caret_info)
1563 struct msg_queue *queue = get_current_queue();
1564 struct thread_input *input;
1566 if (!queue) return;
1567 input = queue->input;
1568 reply->full_handle = input->caret;
1569 reply->old_rect = input->caret_rect;
1570 reply->old_hide = input->caret_hide;
1571 reply->old_state = input->caret_state;
1573 if (req->handle && get_user_full_handle(req->handle) != input->caret)
1575 set_error( STATUS_ACCESS_DENIED );
1576 return;
1578 if (req->flags & SET_CARET_POS)
1580 input->caret_rect.right += req->x - input->caret_rect.left;
1581 input->caret_rect.bottom += req->y - input->caret_rect.top;
1582 input->caret_rect.left = req->x;
1583 input->caret_rect.top = req->y;
1585 if (req->flags & SET_CARET_HIDE)
1587 input->caret_hide += req->hide;
1588 if (input->caret_hide < 0) input->caret_hide = 0;
1590 if (req->flags & SET_CARET_STATE)
1592 if (req->state == -1) input->caret_state = !input->caret_state;
1593 else input->caret_state = !!req->state;