Regular files are always ready for I/O, no need to poll on them.
[wine/gsoc_dplay.git] / server / queue.c
blobb78ad3c8110527db0aba173d3da416ba39b9f4cb
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 <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
34 #include "handle.h"
35 #include "file.h"
36 #include "thread.h"
37 #include "process.h"
38 #include "request.h"
39 #include "user.h"
41 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
42 #define NB_MSG_KINDS (POST_MESSAGE+1)
45 struct message_result
47 struct list sender_entry; /* entry in sender list */
48 struct message_result *recv_next; /* next in receiver list */
49 struct msg_queue *sender; /* sender queue */
50 struct msg_queue *receiver; /* receiver queue */
51 int replied; /* has it been replied to? */
52 unsigned int result; /* reply result */
53 unsigned int error; /* error code to pass back to sender */
54 struct message *callback_msg; /* message to queue for callback */
55 void *data; /* message reply data */
56 unsigned int data_size; /* size of message reply data */
57 struct timeout_user *timeout; /* result timeout */
60 struct message
62 struct message *next; /* next message in list */
63 struct message *prev; /* prev message in list */
64 enum message_type type; /* message type */
65 user_handle_t win; /* window handle */
66 unsigned int msg; /* message code */
67 unsigned int wparam; /* parameters */
68 unsigned int lparam; /* parameters */
69 int x; /* x position */
70 int y; /* y position */
71 unsigned int time; /* message time */
72 unsigned int info; /* extra info */
73 user_handle_t hook; /* winevent hook handle */
74 void *hook_proc; /* winevent hook proc address */
75 void *data; /* message data for sent messages */
76 unsigned int data_size; /* size of message data */
77 struct message_result *result; /* result in sender queue */
80 struct message_list
82 struct message *first; /* head of list */
83 struct message *last; /* tail of list */
86 struct timer
88 struct list entry; /* entry in timer list */
89 struct timeval when; /* next expiration */
90 unsigned int rate; /* timer rate in ms */
91 user_handle_t win; /* window handle */
92 unsigned int msg; /* message to post */
93 unsigned int id; /* timer id */
94 unsigned int lparam; /* lparam for message */
97 struct thread_input
99 struct object obj; /* object header */
100 user_handle_t focus; /* focus window */
101 user_handle_t capture; /* capture window */
102 user_handle_t active; /* active window */
103 user_handle_t menu_owner; /* current menu owner window */
104 user_handle_t move_size; /* current moving/resizing window */
105 user_handle_t caret; /* caret window */
106 rectangle_t caret_rect; /* caret rectangle */
107 int caret_hide; /* caret hide count */
108 int caret_state; /* caret on/off state */
109 struct message *msg; /* message currently processed */
110 struct thread *msg_thread; /* thread processing the message */
111 struct message_list msg_list; /* list of hardware messages */
112 unsigned char keystate[256]; /* state of each key */
115 struct msg_queue
117 struct object obj; /* object header */
118 unsigned int wake_bits; /* wakeup bits */
119 unsigned int wake_mask; /* wakeup mask */
120 unsigned int changed_bits; /* changed wakeup bits */
121 unsigned int changed_mask; /* changed wakeup mask */
122 int paint_count; /* pending paint messages count */
123 struct message_list msg_list[NB_MSG_KINDS]; /* lists of messages */
124 struct list send_result; /* stack of sent messages waiting for result */
125 struct list callback_result; /* list of callback messages waiting for result */
126 struct message_result *recv_result; /* stack of received messages waiting for result */
127 struct list pending_timers; /* list of pending timers */
128 struct list expired_timers; /* list of expired timers */
129 unsigned int next_timer_id; /* id for the next timer with a 0 window */
130 struct timeout_user *timeout; /* timeout for next timer to expire */
131 struct thread_input *input; /* thread input descriptor */
132 struct hook_table *hooks; /* hook table */
133 struct timeval last_get_msg; /* time of last get message call */
136 static void msg_queue_dump( struct object *obj, int verbose );
137 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
138 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
139 static int msg_queue_signaled( struct object *obj, struct thread *thread );
140 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
141 static void msg_queue_destroy( struct object *obj );
142 static void thread_input_dump( struct object *obj, int verbose );
143 static void thread_input_destroy( struct object *obj );
144 static void timer_callback( void *private );
146 static const struct object_ops msg_queue_ops =
148 sizeof(struct msg_queue), /* size */
149 msg_queue_dump, /* dump */
150 msg_queue_add_queue, /* add_queue */
151 msg_queue_remove_queue, /* remove_queue */
152 msg_queue_signaled, /* signaled */
153 msg_queue_satisfied, /* satisfied */
154 no_get_fd, /* get_fd */
155 msg_queue_destroy /* destroy */
159 static const struct object_ops thread_input_ops =
161 sizeof(struct thread_input), /* size */
162 thread_input_dump, /* dump */
163 no_add_queue, /* add_queue */
164 NULL, /* remove_queue */
165 NULL, /* signaled */
166 NULL, /* satisfied */
167 no_get_fd, /* get_fd */
168 thread_input_destroy /* destroy */
171 /* pointer to input structure of foreground thread */
172 static struct thread_input *foreground_input;
175 /* set the caret window in a given thread input */
176 static void set_caret_window( struct thread_input *input, user_handle_t win )
178 if (!win || win != input->caret)
180 input->caret_rect.left = 0;
181 input->caret_rect.top = 0;
182 input->caret_rect.right = 0;
183 input->caret_rect.bottom = 0;
185 input->caret = win;
186 input->caret_hide = 1;
187 input->caret_state = 0;
190 /* create a thread input object */
191 static struct thread_input *create_thread_input(void)
193 struct thread_input *input;
195 if ((input = alloc_object( &thread_input_ops )))
197 input->focus = 0;
198 input->capture = 0;
199 input->active = 0;
200 input->menu_owner = 0;
201 input->move_size = 0;
202 input->msg = NULL;
203 input->msg_thread = NULL;
204 input->msg_list.first = input->msg_list.last = NULL;
205 set_caret_window( input, 0 );
206 memset( input->keystate, 0, sizeof(input->keystate) );
208 return input;
211 /* release the thread input data of a given thread */
212 static void release_thread_input( struct thread *thread )
214 struct thread_input *input = thread->queue->input;
216 if (!input) return;
217 if (input->msg_thread == thread)
219 release_object( input->msg_thread );
220 input->msg_thread = NULL;
221 input->msg = NULL;
223 release_object( input );
224 thread->queue->input = NULL;
227 /* create a message queue object */
228 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
230 struct msg_queue *queue;
231 int i;
233 if (!input && !(input = create_thread_input())) return NULL;
234 if ((queue = alloc_object( &msg_queue_ops )))
236 queue->wake_bits = 0;
237 queue->wake_mask = 0;
238 queue->changed_bits = 0;
239 queue->changed_mask = 0;
240 queue->paint_count = 0;
241 queue->recv_result = NULL;
242 queue->next_timer_id = 1;
243 queue->timeout = NULL;
244 queue->input = (struct thread_input *)grab_object( input );
245 queue->hooks = NULL;
246 gettimeofday( &queue->last_get_msg, NULL );
247 list_init( &queue->send_result );
248 list_init( &queue->callback_result );
249 list_init( &queue->pending_timers );
250 list_init( &queue->expired_timers );
251 for (i = 0; i < NB_MSG_KINDS; i++)
252 queue->msg_list[i].first = queue->msg_list[i].last = NULL;
254 thread->queue = queue;
255 if (!thread->process->queue)
256 thread->process->queue = (struct msg_queue *)grab_object( queue );
258 release_object( input );
259 return queue;
262 /* free the message queue of a thread at thread exit */
263 void free_msg_queue( struct thread *thread )
265 struct process *process = thread->process;
267 remove_thread_hooks( thread );
268 if (!thread->queue) return;
269 if (process->queue == thread->queue) /* is it the process main queue? */
271 release_object( process->queue );
272 process->queue = NULL;
273 if (process->idle_event)
275 set_event( process->idle_event );
276 release_object( process->idle_event );
277 process->idle_event = NULL;
280 release_thread_input( thread );
281 release_object( thread->queue );
282 thread->queue = NULL;
285 /* get the hook table for a given thread */
286 struct hook_table *get_queue_hooks( struct thread *thread )
288 if (!thread->queue) return NULL;
289 return thread->queue->hooks;
292 /* set the hook table for a given thread, allocating the queue if needed */
293 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
295 struct msg_queue *queue = thread->queue;
296 if (!queue) queue = create_msg_queue( thread, NULL );
297 if (queue->hooks) release_object( queue->hooks );
298 queue->hooks = hooks;
301 /* check the queue status */
302 inline static int is_signaled( struct msg_queue *queue )
304 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
307 /* set some queue bits */
308 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
310 queue->wake_bits |= bits;
311 queue->changed_bits |= bits;
312 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
315 /* clear some queue bits */
316 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
318 queue->wake_bits &= ~bits;
319 queue->changed_bits &= ~bits;
322 /* check whether msg is a keyboard message */
323 inline static int is_keyboard_msg( struct message *msg )
325 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
328 /* get the QS_* bit corresponding to a given hardware message */
329 inline static int get_hardware_msg_bit( struct message *msg )
331 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
332 if (is_keyboard_msg( msg )) return QS_KEY;
333 return QS_MOUSEBUTTON;
336 /* get the current thread queue, creating it if needed */
337 inline static struct msg_queue *get_current_queue(void)
339 struct msg_queue *queue = current->queue;
340 if (!queue) queue = create_msg_queue( current, NULL );
341 return queue;
344 /* append a message to the end of a list */
345 inline static void append_message( struct message_list *list, struct message *msg )
347 msg->next = NULL;
348 if ((msg->prev = list->last)) msg->prev->next = msg;
349 else list->first = msg;
350 list->last = msg;
353 /* unlink a message from a list it */
354 inline static void unlink_message( struct message_list *list, struct message *msg )
356 if (msg->next) msg->next->prev = msg->prev;
357 else list->last = msg->prev;
358 if (msg->prev) msg->prev->next = msg->next;
359 else list->first = msg->next;
362 /* try to merge a message with the last in the list; return 1 if successful */
363 static int merge_message( struct thread_input *input, const struct message *msg )
365 struct message *prev = input->msg_list.last;
367 if (!prev) return 0;
368 if (input->msg == prev) return 0;
369 if (prev->result) return 0;
370 if (prev->win != msg->win) return 0;
371 if (prev->msg != msg->msg) return 0;
372 if (prev->type != msg->type) return 0;
373 /* now we can merge it */
374 prev->wparam = msg->wparam;
375 prev->lparam = msg->lparam;
376 prev->x = msg->x;
377 prev->y = msg->y;
378 prev->time = msg->time;
379 prev->info = msg->info;
380 return 1;
383 /* free a result structure */
384 static void free_result( struct message_result *result )
386 if (result->timeout) remove_timeout_user( result->timeout );
387 if (result->data) free( result->data );
388 if (result->callback_msg) free( result->callback_msg );
389 free( result );
392 /* remove the result from the sender list it is on */
393 static inline void remove_result_from_sender( struct message_result *result )
395 assert( result->sender );
397 list_remove( &result->sender_entry );
398 result->sender = NULL;
399 if (!result->receiver) free_result( result );
402 /* store the message result in the appropriate structure */
403 static void store_message_result( struct message_result *res, unsigned int result,
404 unsigned int error )
406 res->result = result;
407 res->error = error;
408 res->replied = 1;
409 if (res->timeout)
411 remove_timeout_user( res->timeout );
412 res->timeout = NULL;
414 if (res->sender)
416 if (res->callback_msg)
418 /* queue the callback message in the sender queue */
419 res->callback_msg->lparam = result;
420 append_message( &res->sender->msg_list[SEND_MESSAGE], res->callback_msg );
421 set_queue_bits( res->sender, QS_SENDMESSAGE );
422 res->callback_msg = NULL;
423 remove_result_from_sender( res );
425 else
427 /* wake sender queue if waiting on this result */
428 if (list_head(&res->sender->send_result) == &res->sender_entry)
429 set_queue_bits( res->sender, QS_SMRESULT );
435 /* free a message when deleting a queue or window */
436 static void free_message( struct message *msg )
438 struct message_result *result = msg->result;
439 if (result)
441 if (result->sender)
443 result->receiver = NULL;
444 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
446 else free_result( result );
448 if (msg->data) free( msg->data );
449 free( msg );
452 /* remove (and free) a message from a message list */
453 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
454 enum message_kind kind )
456 unlink_message( &queue->msg_list[kind], msg );
457 switch(kind)
459 case SEND_MESSAGE:
460 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_SENDMESSAGE );
461 break;
462 case POST_MESSAGE:
463 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_POSTMESSAGE );
464 break;
466 free_message( msg );
469 /* message timed out without getting a reply */
470 static void result_timeout( void *private )
472 struct message_result *result = private;
474 assert( !result->replied );
476 result->timeout = NULL;
477 store_message_result( result, 0, STATUS_TIMEOUT );
480 /* allocate and fill a message result structure */
481 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
482 struct msg_queue *recv_queue,
483 struct message *msg, unsigned int timeout,
484 void *callback, unsigned int callback_data )
486 struct message_result *result = mem_alloc( sizeof(*result) );
487 if (result)
489 result->sender = send_queue;
490 result->receiver = recv_queue;
491 result->replied = 0;
492 result->data = NULL;
493 result->data_size = 0;
494 result->timeout = NULL;
496 if (msg->type == MSG_CALLBACK)
498 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
499 if (!callback_msg)
501 free( result );
502 return NULL;
504 callback_msg->type = MSG_CALLBACK_RESULT;
505 callback_msg->win = msg->win;
506 callback_msg->msg = msg->msg;
507 callback_msg->wparam = (unsigned int)callback;
508 callback_msg->lparam = 0;
509 callback_msg->time = get_tick_count();
510 callback_msg->x = 0;
511 callback_msg->y = 0;
512 callback_msg->info = callback_data;
513 callback_msg->result = NULL;
514 callback_msg->data = NULL;
515 callback_msg->data_size = 0;
517 result->callback_msg = callback_msg;
518 list_add_head( &send_queue->callback_result, &result->sender_entry );
520 else
522 result->callback_msg = NULL;
523 list_add_head( &send_queue->send_result, &result->sender_entry );
526 if (timeout != -1)
528 struct timeval when;
529 gettimeofday( &when, 0 );
530 add_timeout( &when, timeout );
531 result->timeout = add_timeout_user( &when, result_timeout, result );
534 return result;
537 /* receive a message, removing it from the sent queue */
538 static void receive_message( struct msg_queue *queue, struct message *msg,
539 struct get_message_reply *reply )
541 struct message_result *result = msg->result;
543 reply->total = msg->data_size;
544 if (msg->data_size > get_reply_max_size())
546 set_error( STATUS_BUFFER_OVERFLOW );
547 return;
549 reply->type = msg->type;
550 reply->win = msg->win;
551 reply->msg = msg->msg;
552 reply->wparam = msg->wparam;
553 reply->lparam = msg->lparam;
554 reply->x = msg->x;
555 reply->y = msg->y;
556 reply->time = msg->time;
557 reply->info = msg->info;
558 reply->hook = msg->hook;
559 reply->hook_proc = msg->hook_proc;
561 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
563 unlink_message( &queue->msg_list[SEND_MESSAGE], msg );
564 /* put the result on the receiver result stack */
565 if (result)
567 result->recv_next = queue->recv_result;
568 queue->recv_result = result;
570 free( msg );
571 if (!queue->msg_list[SEND_MESSAGE].first) clear_queue_bits( queue, QS_SENDMESSAGE );
574 /* set the result of the current received message */
575 static void reply_message( struct msg_queue *queue, unsigned int result,
576 unsigned int error, int remove, const void *data, size_t len )
578 struct message_result *res = queue->recv_result;
580 if (remove)
582 queue->recv_result = res->recv_next;
583 res->receiver = NULL;
584 if (!res->sender) /* no one waiting for it */
586 free_result( res );
587 return;
590 if (!res->replied)
592 if (len && (res->data = memdup( data, len ))) res->data_size = len;
593 store_message_result( res, result, error );
597 /* retrieve a posted message */
598 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
599 unsigned int first, unsigned int last, unsigned int flags,
600 struct get_message_reply *reply )
602 struct message *msg;
603 struct message_list *list = &queue->msg_list[POST_MESSAGE];
605 /* check against the filters */
606 for (msg = list->first; msg; msg = msg->next)
608 if (msg->msg == WM_QUIT) break; /* WM_QUIT is never filtered */
609 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
610 if (msg->msg < first) continue;
611 if (msg->msg > last) continue;
612 break; /* found one */
614 if (!msg) return 0;
616 /* return it to the app */
618 reply->total = msg->data_size;
619 if (msg->data_size > get_reply_max_size())
621 set_error( STATUS_BUFFER_OVERFLOW );
622 return 1;
624 reply->type = msg->type;
625 reply->win = msg->win;
626 reply->msg = msg->msg;
627 reply->wparam = msg->wparam;
628 reply->lparam = msg->lparam;
629 reply->x = msg->x;
630 reply->y = msg->y;
631 reply->time = msg->time;
632 reply->info = msg->info;
634 if (flags & GET_MSG_REMOVE)
636 if (msg->data)
638 set_reply_data_ptr( msg->data, msg->data_size );
639 msg->data = NULL;
640 msg->data_size = 0;
642 remove_queue_message( queue, msg, POST_MESSAGE );
644 else if (msg->data) set_reply_data( msg->data, msg->data_size );
646 return 1;
649 /* empty a message list and free all the messages */
650 static void empty_msg_list( struct message_list *list )
652 struct message *msg = list->first;
653 while (msg)
655 struct message *next = msg->next;
656 free_message( msg );
657 msg = next;
661 /* cleanup all pending results when deleting a queue */
662 static void cleanup_results( struct msg_queue *queue )
664 struct list *entry;
666 while ((entry = list_head( &queue->send_result )) != NULL)
668 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
671 while ((entry = list_head( &queue->callback_result )) != NULL)
673 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
676 while (queue->recv_result)
677 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
680 /* check if the thread owning the queue is hung (not checking for messages) */
681 static int is_queue_hung( struct msg_queue *queue )
683 struct timeval now;
684 struct wait_queue_entry *entry;
686 gettimeofday( &now, NULL );
687 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
688 return 0; /* less than 5 seconds since last get message -> not hung */
690 for (entry = queue->obj.head; entry; entry = entry->next)
692 if (entry->thread->queue == queue)
693 return 0; /* thread is waiting on queue -> not hung */
695 return 1;
698 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
700 struct msg_queue *queue = (struct msg_queue *)obj;
701 struct process *process = entry->thread->process;
703 /* a thread can only wait on its own queue */
704 if (entry->thread->queue != queue)
706 set_error( STATUS_ACCESS_DENIED );
707 return 0;
709 /* if waiting on the main process queue, set the idle event */
710 if (process->queue == queue)
712 if (process->idle_event) set_event( process->idle_event );
714 add_queue( obj, entry );
715 return 1;
718 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
720 struct msg_queue *queue = (struct msg_queue *)obj;
721 struct process *process = entry->thread->process;
723 remove_queue( obj, entry );
725 assert( entry->thread->queue == queue );
727 /* if waiting on the main process queue, reset the idle event */
728 if (process->queue == queue)
730 if (process->idle_event) reset_event( process->idle_event );
734 static void msg_queue_dump( struct object *obj, int verbose )
736 struct msg_queue *queue = (struct msg_queue *)obj;
737 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
738 queue->wake_bits, queue->wake_mask );
741 static int msg_queue_signaled( struct object *obj, struct thread *thread )
743 struct msg_queue *queue = (struct msg_queue *)obj;
744 return is_signaled( queue );
747 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
749 struct msg_queue *queue = (struct msg_queue *)obj;
750 queue->wake_mask = 0;
751 queue->changed_mask = 0;
752 return 0; /* Not abandoned */
755 static void msg_queue_destroy( struct object *obj )
757 struct msg_queue *queue = (struct msg_queue *)obj;
758 struct list *ptr;
759 int i;
761 cleanup_results( queue );
762 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
764 while ((ptr = list_head( &queue->pending_timers )))
766 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
767 list_remove( &timer->entry );
768 free( timer );
770 while ((ptr = list_head( &queue->expired_timers )))
772 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
773 list_remove( &timer->entry );
774 free( timer );
776 if (queue->timeout) remove_timeout_user( queue->timeout );
777 if (queue->input) release_object( queue->input );
778 if (queue->hooks) release_object( queue->hooks );
781 static void thread_input_dump( struct object *obj, int verbose )
783 struct thread_input *input = (struct thread_input *)obj;
784 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
785 input->focus, input->capture, input->active );
788 static void thread_input_destroy( struct object *obj )
790 struct thread_input *input = (struct thread_input *)obj;
792 if (foreground_input == input) foreground_input = NULL;
793 if (input->msg_thread) release_object( input->msg_thread );
794 empty_msg_list( &input->msg_list );
797 /* fix the thread input data when a window is destroyed */
798 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
800 struct thread_input *input = queue->input;
802 if (window == input->focus) input->focus = 0;
803 if (window == input->capture) input->capture = 0;
804 if (window == input->active) input->active = 0;
805 if (window == input->menu_owner) input->menu_owner = 0;
806 if (window == input->move_size) input->move_size = 0;
807 if (window == input->caret) set_caret_window( input, 0 );
810 /* check if the specified window can be set in the input data of a given queue */
811 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
813 struct thread *thread;
814 int ret = 0;
816 if (!window) return 1; /* we can always clear the data */
818 if ((thread = get_window_thread( window )))
820 ret = (queue->input == thread->queue->input);
821 if (!ret) set_error( STATUS_ACCESS_DENIED );
822 release_object( thread );
824 else set_error( STATUS_INVALID_HANDLE );
826 return ret;
829 /* attach two thread input data structures */
830 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
832 struct thread_input *input;
834 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
835 input = (struct thread_input *)grab_object( thread_to->queue->input );
837 if (thread_from->queue)
839 release_thread_input( thread_from );
840 thread_from->queue->input = input;
842 else
844 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
846 memset( input->keystate, 0, sizeof(input->keystate) );
847 return 1;
850 /* detach two thread input data structures */
851 static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
853 struct thread_input *input;
855 if (!thread_from->queue || !thread_to->queue ||
856 thread_from->queue->input != thread_to->queue->input)
858 set_error( STATUS_ACCESS_DENIED );
859 return;
861 if ((input = create_thread_input()))
863 release_thread_input( thread_from );
864 thread_from->queue->input = input;
869 /* set the next timer to expire */
870 static void set_next_timer( struct msg_queue *queue )
872 struct list *ptr;
874 if (queue->timeout)
876 remove_timeout_user( queue->timeout );
877 queue->timeout = NULL;
879 if ((ptr = list_head( &queue->pending_timers )))
881 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
882 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
884 /* set/clear QS_TIMER bit */
885 if (list_empty( &queue->expired_timers ))
886 clear_queue_bits( queue, QS_TIMER );
887 else
888 set_queue_bits( queue, QS_TIMER );
891 /* find a timer from its window and id */
892 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
893 unsigned int msg, unsigned int id )
895 struct list *ptr;
897 /* we need to search both lists */
899 LIST_FOR_EACH( ptr, &queue->pending_timers )
901 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
902 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
904 LIST_FOR_EACH( ptr, &queue->expired_timers )
906 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
907 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
909 return NULL;
912 /* callback for the next timer expiration */
913 static void timer_callback( void *private )
915 struct msg_queue *queue = private;
916 struct list *ptr;
918 queue->timeout = NULL;
919 /* move on to the next timer */
920 ptr = list_head( &queue->pending_timers );
921 list_remove( ptr );
922 list_add_tail( &queue->expired_timers, ptr );
923 set_next_timer( queue );
926 /* link a timer at its rightful place in the queue list */
927 static void link_timer( struct msg_queue *queue, struct timer *timer )
929 struct list *ptr;
931 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
933 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
934 if (!time_before( &t->when, &timer->when )) break;
936 list_add_before( ptr, &timer->entry );
939 /* remove a timer from the queue timer list and free it */
940 static void free_timer( struct msg_queue *queue, struct timer *timer )
942 list_remove( &timer->entry );
943 free( timer );
944 set_next_timer( queue );
947 /* restart an expired timer */
948 static void restart_timer( struct msg_queue *queue, struct timer *timer )
950 struct timeval now;
952 list_remove( &timer->entry );
953 gettimeofday( &now, 0 );
954 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
955 link_timer( queue, timer );
956 set_next_timer( queue );
959 /* find an expired timer matching the filtering parameters */
960 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
961 unsigned int get_first, unsigned int get_last,
962 int remove )
964 struct list *ptr;
966 LIST_FOR_EACH( ptr, &queue->expired_timers )
968 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
969 if (win && timer->win != win) continue;
970 if (timer->msg >= get_first && timer->msg <= get_last)
972 if (remove) restart_timer( queue, timer );
973 return timer;
976 return NULL;
979 /* add a timer */
980 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
982 struct timer *timer = mem_alloc( sizeof(*timer) );
983 if (timer)
985 timer->rate = max( rate, 1 );
986 gettimeofday( &timer->when, 0 );
987 add_timeout( &timer->when, rate );
988 link_timer( queue, timer );
989 /* check if we replaced the next timer */
990 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
992 return timer;
995 /* change the input key state for a given key */
996 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
998 if (down)
1000 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1001 input->keystate[key] |= 0x80;
1003 else input->keystate[key] &= ~0x80;
1006 /* update the input key state for a keyboard message */
1007 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1009 unsigned char key;
1010 int down = 0, extended;
1012 switch (msg->msg)
1014 case WM_LBUTTONDOWN:
1015 down = 1;
1016 /* fall through */
1017 case WM_LBUTTONUP:
1018 set_input_key_state( input, VK_LBUTTON, down );
1019 break;
1020 case WM_MBUTTONDOWN:
1021 down = 1;
1022 /* fall through */
1023 case WM_MBUTTONUP:
1024 set_input_key_state( input, VK_MBUTTON, down );
1025 break;
1026 case WM_RBUTTONDOWN:
1027 down = 1;
1028 /* fall through */
1029 case WM_RBUTTONUP:
1030 set_input_key_state( input, VK_RBUTTON, down );
1031 break;
1032 case WM_KEYDOWN:
1033 case WM_SYSKEYDOWN:
1034 down = 1;
1035 /* fall through */
1036 case WM_KEYUP:
1037 case WM_SYSKEYUP:
1038 key = (unsigned char)msg->wparam;
1039 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1040 set_input_key_state( input, key, down );
1041 switch(key)
1043 case VK_SHIFT:
1044 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1045 break;
1046 case VK_CONTROL:
1047 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1048 break;
1049 case VK_MENU:
1050 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1051 break;
1053 break;
1057 /* release the hardware message currently being processed by the given thread */
1058 static void release_hardware_message( struct thread *thread, int remove )
1060 struct thread_input *input = thread->queue->input;
1062 if (input->msg_thread != thread) return;
1063 if (remove)
1065 struct message *other;
1066 int clr_bit;
1068 update_input_key_state( input, input->msg );
1069 unlink_message( &input->msg_list, input->msg );
1070 clr_bit = get_hardware_msg_bit( input->msg );
1071 for (other = input->msg_list.first; other; other = other->next)
1072 if (get_hardware_msg_bit( other ) == clr_bit) break;
1073 if (!other) clear_queue_bits( thread->queue, clr_bit );
1074 free_message( input->msg );
1076 release_object( input->msg_thread );
1077 input->msg = NULL;
1078 input->msg_thread = NULL;
1081 /* find the window that should receive a given hardware message */
1082 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1083 unsigned int *msg_code )
1085 user_handle_t win = 0;
1087 *msg_code = msg->msg;
1088 if (is_keyboard_msg( msg ))
1090 if (input && !(win = input->focus))
1092 win = input->active;
1093 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1096 else /* mouse message */
1098 if (!input || !(win = input->capture))
1100 if (!(win = msg->win) || !is_window_visible( win ))
1101 win = window_from_point( msg->x, msg->y );
1104 return win;
1107 /* queue a hardware message into a given thread input */
1108 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1110 user_handle_t win;
1111 struct thread *thread;
1112 struct thread_input *input;
1113 unsigned int msg_code;
1115 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1116 if (!win || !(thread = get_window_thread(win)))
1118 free( msg );
1119 return;
1121 input = thread->queue->input;
1123 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1124 else
1126 append_message( &input->msg_list, msg );
1127 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1129 release_object( thread );
1132 /* find a hardware message for the given queue */
1133 static int get_hardware_message( struct thread *thread, struct message *first,
1134 user_handle_t filter_win, struct get_message_reply *reply )
1136 struct thread_input *input = thread->queue->input;
1137 struct thread *win_thread;
1138 struct message *msg;
1139 user_handle_t win;
1140 int clear_bits, got_one = 0;
1141 unsigned int msg_code;
1143 if (input->msg_thread && input->msg_thread != thread)
1144 return 0; /* locked by another thread */
1146 if (!first)
1148 msg = input->msg_list.first;
1149 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1151 else
1153 msg = first->next;
1154 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1157 while (msg)
1159 win = find_hardware_message_window( input, msg, &msg_code );
1160 if (!win || !(win_thread = get_window_thread( win )))
1162 /* no window at all, remove it */
1163 struct message *next = msg->next;
1164 update_input_key_state( input, msg );
1165 unlink_message( &input->msg_list, msg );
1166 free_message( msg );
1167 msg = next;
1168 continue;
1170 if (win_thread != thread)
1172 /* wake the other thread */
1173 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1174 release_object( win_thread );
1175 got_one = 1;
1176 msg = msg->next;
1177 continue;
1179 /* if we already got a message for another thread, or if it doesn't
1180 * match the filter we skip it (filter is only checked for keyboard
1181 * messages since the dest window for a mouse message depends on hittest)
1183 if (got_one ||
1184 (filter_win && is_keyboard_msg(msg) &&
1185 win != filter_win && !is_child_window( filter_win, win )))
1187 clear_bits &= ~get_hardware_msg_bit( msg );
1188 release_object( win_thread );
1189 msg = msg->next;
1190 continue;
1192 /* now we can return it */
1193 if (!input->msg_thread) input->msg_thread = win_thread;
1194 else release_object( win_thread );
1195 input->msg = msg;
1197 reply->type = MSG_HARDWARE;
1198 reply->win = win;
1199 reply->msg = msg_code;
1200 reply->wparam = msg->wparam;
1201 reply->lparam = msg->lparam;
1202 reply->x = msg->x;
1203 reply->y = msg->y;
1204 reply->time = msg->time;
1205 reply->info = msg->info;
1206 return 1;
1208 /* nothing found, clear the hardware queue bits */
1209 clear_queue_bits( thread->queue, clear_bits );
1210 if (input->msg_thread) release_object( input->msg_thread );
1211 input->msg = NULL;
1212 input->msg_thread = NULL;
1213 return 0;
1216 /* increment (or decrement if 'incr' is negative) the queue paint count */
1217 void inc_queue_paint_count( struct thread *thread, int incr )
1219 struct msg_queue *queue = thread->queue;
1221 assert( queue );
1223 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1225 if (queue->paint_count)
1226 set_queue_bits( queue, QS_PAINT );
1227 else
1228 clear_queue_bits( queue, QS_PAINT );
1232 /* remove all messages and timers belonging to a certain window */
1233 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1235 struct msg_queue *queue = thread->queue;
1236 struct list *ptr;
1237 struct message *msg;
1238 int i;
1240 if (!queue) return;
1242 /* remove timers */
1244 ptr = list_head( &queue->pending_timers );
1245 while (ptr)
1247 struct list *next = list_next( &queue->pending_timers, ptr );
1248 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1249 if (timer->win == win) free_timer( queue, timer );
1250 ptr = next;
1252 ptr = list_head( &queue->expired_timers );
1253 while (ptr)
1255 struct list *next = list_next( &queue->expired_timers, ptr );
1256 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1257 if (timer->win == win) free_timer( queue, timer );
1258 ptr = next;
1261 /* remove messages */
1262 for (i = 0; i < NB_MSG_KINDS; i++)
1264 msg = queue->msg_list[i].first;
1265 while (msg)
1267 struct message *next = msg->next;
1268 if (msg->win == win) remove_queue_message( queue, msg, i );
1269 msg = next;
1273 thread_input_cleanup_window( queue, win );
1276 /* post a message to a window; used by socket handling */
1277 void post_message( user_handle_t win, unsigned int message,
1278 unsigned int wparam, unsigned int lparam )
1280 struct message *msg;
1281 struct thread *thread = get_window_thread( win );
1283 if (!thread) return;
1285 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1287 msg->type = MSG_POSTED;
1288 msg->win = get_user_full_handle( win );
1289 msg->msg = message;
1290 msg->wparam = wparam;
1291 msg->lparam = lparam;
1292 msg->time = get_tick_count();
1293 msg->x = 0;
1294 msg->y = 0;
1295 msg->info = 0;
1296 msg->result = NULL;
1297 msg->data = NULL;
1298 msg->data_size = 0;
1300 append_message( &thread->queue->msg_list[POST_MESSAGE], msg );
1301 set_queue_bits( thread->queue, QS_POSTMESSAGE );
1303 release_object( thread );
1306 /* post a win event */
1307 void post_win_event( struct thread *thread, unsigned int event,
1308 user_handle_t win, unsigned int object_id,
1309 unsigned int child_id, void *hook_proc,
1310 const WCHAR *module, size_t module_size,
1311 user_handle_t hook)
1313 struct message *msg;
1315 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1317 msg->type = MSG_WINEVENT;
1318 msg->win = get_user_full_handle( win );
1319 msg->msg = event;
1320 msg->wparam = object_id;
1321 msg->lparam = child_id;
1322 msg->time = get_tick_count();
1323 msg->x = 0;
1324 msg->y = 0;
1325 msg->info = get_thread_id( current );
1326 msg->result = NULL;
1327 msg->hook = hook;
1328 msg->hook_proc = hook_proc;
1330 if ((msg->data = malloc( module_size )))
1332 msg->data_size = module_size;
1333 memcpy( msg->data, module, module_size );
1335 if (debug_level > 1)
1336 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1337 get_thread_id(thread), event, win, object_id, child_id );
1338 append_message( &thread->queue->msg_list[SEND_MESSAGE], msg );
1339 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1341 else
1342 free( msg );
1346 /* get the message queue of the current thread */
1347 DECL_HANDLER(get_msg_queue)
1349 struct msg_queue *queue = get_current_queue();
1351 reply->handle = 0;
1352 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1356 /* set the current message queue wakeup mask */
1357 DECL_HANDLER(set_queue_mask)
1359 struct msg_queue *queue = get_current_queue();
1361 if (queue)
1363 queue->wake_mask = req->wake_mask;
1364 queue->changed_mask = req->changed_mask;
1365 reply->wake_bits = queue->wake_bits;
1366 reply->changed_bits = queue->changed_bits;
1367 if (is_signaled( queue ))
1369 /* if skip wait is set, do what would have been done in the subsequent wait */
1370 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1371 else wake_up( &queue->obj, 0 );
1377 /* get the current message queue status */
1378 DECL_HANDLER(get_queue_status)
1380 struct msg_queue *queue = current->queue;
1381 if (queue)
1383 reply->wake_bits = queue->wake_bits;
1384 reply->changed_bits = queue->changed_bits;
1385 if (req->clear) queue->changed_bits = 0;
1387 else reply->wake_bits = reply->changed_bits = 0;
1391 /* send a message to a thread queue */
1392 DECL_HANDLER(send_message)
1394 struct message *msg;
1395 struct msg_queue *send_queue = get_current_queue();
1396 struct msg_queue *recv_queue = NULL;
1397 struct thread *thread = NULL;
1399 if (req->id)
1401 if (!(thread = get_thread_from_id( req->id ))) return;
1403 else if (req->type != MSG_HARDWARE)
1405 /* only hardware messages are allowed without destination thread */
1406 set_error( STATUS_INVALID_PARAMETER );
1407 return;
1410 if (thread && !(recv_queue = thread->queue))
1412 set_error( STATUS_INVALID_PARAMETER );
1413 release_object( thread );
1414 return;
1416 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1418 set_error( STATUS_TIMEOUT );
1419 release_object( thread );
1420 return;
1423 if ((msg = mem_alloc( sizeof(*msg) )))
1425 msg->type = req->type;
1426 msg->win = get_user_full_handle( req->win );
1427 msg->msg = req->msg;
1428 msg->wparam = req->wparam;
1429 msg->lparam = req->lparam;
1430 msg->time = req->time;
1431 msg->x = req->x;
1432 msg->y = req->y;
1433 msg->info = req->info;
1434 msg->result = NULL;
1435 msg->data = NULL;
1436 msg->data_size = 0;
1438 switch(msg->type)
1440 case MSG_OTHER_PROCESS:
1441 msg->data_size = get_req_data_size();
1442 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1444 free( msg );
1445 break;
1447 /* fall through */
1448 case MSG_ASCII:
1449 case MSG_UNICODE:
1450 case MSG_CALLBACK:
1451 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1452 req->timeout, req->callback, req->info )))
1454 free_message( msg );
1455 break;
1457 /* fall through */
1458 case MSG_NOTIFY:
1459 append_message( &recv_queue->msg_list[SEND_MESSAGE], msg );
1460 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1461 break;
1462 case MSG_POSTED:
1463 /* needed for posted DDE messages */
1464 msg->data_size = get_req_data_size();
1465 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1467 free( msg );
1468 break;
1470 append_message( &recv_queue->msg_list[POST_MESSAGE], msg );
1471 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1472 break;
1473 case MSG_HARDWARE:
1474 queue_hardware_message( recv_queue, msg );
1475 break;
1476 case MSG_CALLBACK_RESULT: /* cannot send this one */
1477 default:
1478 set_error( STATUS_INVALID_PARAMETER );
1479 free( msg );
1480 break;
1483 if (thread) release_object( thread );
1487 /* get a message from the current queue */
1488 DECL_HANDLER(get_message)
1490 struct timer *timer;
1491 struct message *msg;
1492 struct message *first_hw_msg = NULL;
1493 struct msg_queue *queue = get_current_queue();
1494 user_handle_t get_win = get_user_full_handle( req->get_win );
1496 if (!queue) return;
1497 gettimeofday( &queue->last_get_msg, NULL );
1499 /* first of all release the hardware input lock if we own it */
1500 /* we'll grab it again if we find a hardware message */
1501 if (queue->input->msg_thread == current)
1503 first_hw_msg = queue->input->msg;
1504 release_hardware_message( current, 0 );
1507 /* first check for sent messages */
1508 if ((msg = queue->msg_list[SEND_MESSAGE].first))
1510 receive_message( queue, msg, reply );
1511 return;
1513 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1515 /* clear changed bits so we can wait on them if we don't find a message */
1516 queue->changed_bits = 0;
1518 /* then check for posted messages */
1519 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1520 return;
1522 /* then check for any raw hardware message */
1523 if (get_hardware_message( current, first_hw_msg, get_win, reply ))
1524 return;
1526 /* now check for WM_PAINT */
1527 if (queue->paint_count &&
1528 (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
1529 (reply->win = find_window_to_repaint( get_win, current )))
1531 reply->type = MSG_POSTED;
1532 reply->msg = WM_PAINT;
1533 reply->wparam = 0;
1534 reply->lparam = 0;
1535 reply->x = 0;
1536 reply->y = 0;
1537 reply->time = get_tick_count();
1538 reply->info = 0;
1539 return;
1542 /* now check for timer */
1543 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1544 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1546 reply->type = MSG_POSTED;
1547 reply->win = timer->win;
1548 reply->msg = timer->msg;
1549 reply->wparam = timer->id;
1550 reply->lparam = timer->lparam;
1551 reply->x = 0;
1552 reply->y = 0;
1553 reply->time = get_tick_count();
1554 reply->info = 0;
1555 return;
1558 done:
1559 set_error( STATUS_PENDING ); /* FIXME */
1563 /* reply to a sent message */
1564 DECL_HANDLER(reply_message)
1566 if (!current->queue)
1568 set_error( STATUS_ACCESS_DENIED );
1569 return;
1571 if (req->type == MSG_HARDWARE)
1573 struct thread_input *input = current->queue->input;
1574 if (input->msg_thread == current) release_hardware_message( current, req->remove );
1575 else set_error( STATUS_ACCESS_DENIED );
1577 else if (current->queue->recv_result)
1578 reply_message( current->queue, req->result, 0, req->remove,
1579 get_req_data(), get_req_data_size() );
1583 /* retrieve the reply for the last message sent */
1584 DECL_HANDLER(get_message_reply)
1586 struct message_result *result;
1587 struct list *entry;
1588 struct msg_queue *queue = current->queue;
1590 if (queue)
1592 set_error( STATUS_PENDING );
1593 reply->result = 0;
1595 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1597 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1598 if (result->replied || req->cancel)
1600 if (result->replied)
1602 reply->result = result->result;
1603 set_error( result->error );
1604 if (result->data)
1606 size_t data_len = min( result->data_size, get_reply_max_size() );
1607 set_reply_data_ptr( result->data, data_len );
1608 result->data = NULL;
1609 result->data_size = 0;
1612 remove_result_from_sender( result );
1614 entry = list_head( &queue->send_result );
1615 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1616 else
1618 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1619 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1623 else set_error( STATUS_ACCESS_DENIED );
1627 /* set a window timer */
1628 DECL_HANDLER(set_win_timer)
1630 struct timer *timer;
1631 struct msg_queue *queue;
1632 struct thread *thread = NULL;
1633 user_handle_t win = 0;
1634 unsigned int id = req->id;
1636 if (req->win)
1638 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1640 set_error( STATUS_INVALID_HANDLE );
1641 return;
1643 if (thread->process != current->process)
1645 release_object( thread );
1646 set_error( STATUS_ACCESS_DENIED );
1647 return;
1649 queue = thread->queue;
1650 /* remove it if it existed already */
1651 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1653 else
1655 queue = get_current_queue();
1656 /* find a free id for it */
1659 id = queue->next_timer_id;
1660 if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1662 while (find_timer( queue, 0, req->msg, id ));
1665 if ((timer = set_timer( queue, req->rate )))
1667 timer->win = win;
1668 timer->msg = req->msg;
1669 timer->id = id;
1670 timer->lparam = req->lparam;
1671 reply->id = id;
1673 if (thread) release_object( thread );
1676 /* kill a window timer */
1677 DECL_HANDLER(kill_win_timer)
1679 struct timer *timer;
1680 struct thread *thread;
1681 user_handle_t win = 0;
1683 if (req->win)
1685 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1687 set_error( STATUS_INVALID_HANDLE );
1688 return;
1690 if (thread->process != current->process)
1692 release_object( thread );
1693 set_error( STATUS_ACCESS_DENIED );
1694 return;
1697 else thread = (struct thread *)grab_object( current );
1699 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1700 free_timer( thread->queue, timer );
1701 else
1702 set_error( STATUS_INVALID_PARAMETER );
1704 release_object( thread );
1708 /* attach (or detach) thread inputs */
1709 DECL_HANDLER(attach_thread_input)
1711 struct thread *thread_from = get_thread_from_id( req->tid_from );
1712 struct thread *thread_to = get_thread_from_id( req->tid_to );
1714 if (!thread_from || !thread_to)
1716 if (thread_from) release_object( thread_from );
1717 if (thread_to) release_object( thread_to );
1718 return;
1720 if (thread_from != thread_to)
1722 if (req->attach) attach_thread_input( thread_from, thread_to );
1723 else detach_thread_input( thread_from, thread_to );
1725 else set_error( STATUS_ACCESS_DENIED );
1726 release_object( thread_from );
1727 release_object( thread_to );
1731 /* get thread input data */
1732 DECL_HANDLER(get_thread_input)
1734 struct thread *thread = NULL;
1735 struct thread_input *input;
1737 if (req->tid)
1739 if (!(thread = get_thread_from_id( req->tid ))) return;
1740 input = thread->queue ? thread->queue->input : NULL;
1742 else input = foreground_input; /* get the foreground thread info */
1744 if (input)
1746 reply->focus = input->focus;
1747 reply->capture = input->capture;
1748 reply->active = input->active;
1749 reply->menu_owner = input->menu_owner;
1750 reply->move_size = input->move_size;
1751 reply->caret = input->caret;
1752 reply->rect = input->caret_rect;
1754 else
1756 reply->focus = 0;
1757 reply->capture = 0;
1758 reply->active = 0;
1759 reply->menu_owner = 0;
1760 reply->move_size = 0;
1761 reply->caret = 0;
1762 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1764 /* foreground window is active window of foreground thread */
1765 reply->foreground = foreground_input ? foreground_input->active : 0;
1766 if (thread) release_object( thread );
1770 /* retrieve queue keyboard state for a given thread */
1771 DECL_HANDLER(get_key_state)
1773 struct thread *thread;
1774 struct thread_input *input;
1776 if (!(thread = get_thread_from_id( req->tid ))) return;
1777 input = thread->queue ? thread->queue->input : NULL;
1778 if (input)
1780 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1781 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1783 release_object( thread );
1787 /* set queue keyboard state for a given thread */
1788 DECL_HANDLER(set_key_state)
1790 struct thread *thread = NULL;
1791 struct thread_input *input;
1793 if (!(thread = get_thread_from_id( req->tid ))) return;
1794 input = thread->queue ? thread->queue->input : NULL;
1795 if (input)
1797 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1798 if (size) memcpy( input->keystate, get_req_data(), size );
1800 release_object( thread );
1804 /* set the system foreground window */
1805 DECL_HANDLER(set_foreground_window)
1807 struct msg_queue *queue = get_current_queue();
1809 reply->previous = foreground_input ? foreground_input->active : 0;
1810 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1811 reply->send_msg_new = FALSE;
1813 if (req->handle)
1815 struct thread *thread;
1817 if (is_top_level_window( req->handle ) &&
1818 ((thread = get_window_thread( req->handle ))))
1820 foreground_input = thread->queue->input;
1821 reply->send_msg_new = (foreground_input != queue->input);
1822 release_object( thread );
1824 else set_error( STATUS_INVALID_HANDLE );
1826 else foreground_input = NULL;
1830 /* set the current thread focus window */
1831 DECL_HANDLER(set_focus_window)
1833 struct msg_queue *queue = get_current_queue();
1835 reply->previous = 0;
1836 if (queue && check_queue_input_window( queue, req->handle ))
1838 reply->previous = queue->input->focus;
1839 queue->input->focus = get_user_full_handle( req->handle );
1844 /* set the current thread active window */
1845 DECL_HANDLER(set_active_window)
1847 struct msg_queue *queue = get_current_queue();
1849 reply->previous = 0;
1850 if (queue && check_queue_input_window( queue, req->handle ))
1852 if (!req->handle || make_window_active( req->handle ))
1854 reply->previous = queue->input->active;
1855 queue->input->active = get_user_full_handle( req->handle );
1857 else set_error( STATUS_INVALID_HANDLE );
1862 /* set the current thread capture window */
1863 DECL_HANDLER(set_capture_window)
1865 struct msg_queue *queue = get_current_queue();
1867 reply->previous = reply->full_handle = 0;
1868 if (queue && check_queue_input_window( queue, req->handle ))
1870 struct thread_input *input = queue->input;
1872 reply->previous = input->capture;
1873 input->capture = get_user_full_handle( req->handle );
1874 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1875 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1876 reply->full_handle = input->capture;
1881 /* Set the current thread caret window */
1882 DECL_HANDLER(set_caret_window)
1884 struct msg_queue *queue = get_current_queue();
1886 reply->previous = 0;
1887 if (queue && check_queue_input_window( queue, req->handle ))
1889 struct thread_input *input = queue->input;
1891 reply->previous = input->caret;
1892 reply->old_rect = input->caret_rect;
1893 reply->old_hide = input->caret_hide;
1894 reply->old_state = input->caret_state;
1896 set_caret_window( input, get_user_full_handle(req->handle) );
1897 input->caret_rect.right = input->caret_rect.left + req->width;
1898 input->caret_rect.bottom = input->caret_rect.top + req->height;
1903 /* Set the current thread caret information */
1904 DECL_HANDLER(set_caret_info)
1906 struct msg_queue *queue = get_current_queue();
1907 struct thread_input *input;
1909 if (!queue) return;
1910 input = queue->input;
1911 reply->full_handle = input->caret;
1912 reply->old_rect = input->caret_rect;
1913 reply->old_hide = input->caret_hide;
1914 reply->old_state = input->caret_state;
1916 if (req->handle && get_user_full_handle(req->handle) != input->caret)
1918 set_error( STATUS_ACCESS_DENIED );
1919 return;
1921 if (req->flags & SET_CARET_POS)
1923 input->caret_rect.right += req->x - input->caret_rect.left;
1924 input->caret_rect.bottom += req->y - input->caret_rect.top;
1925 input->caret_rect.left = req->x;
1926 input->caret_rect.top = req->y;
1928 if (req->flags & SET_CARET_HIDE)
1930 input->caret_hide += req->hide;
1931 if (input->caret_hide < 0) input->caret_hide = 0;
1933 if (req->flags & SET_CARET_STATE)
1935 if (req->state == -1) input->caret_state = !input->caret_state;
1936 else input->caret_state = !!req->state;