2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
41 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
42 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
44 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
45 #define NB_MSG_KINDS (POST_MESSAGE+1)
50 struct list sender_entry; /* entry in sender list */
51 struct message *msg; /* message the result is for */
52 struct message_result *recv_next; /* next in receiver list */
53 struct msg_queue *sender; /* sender queue */
54 struct msg_queue *receiver; /* receiver queue */
55 int replied; /* has it been replied to? */
56 unsigned int result; /* reply result */
57 unsigned int error; /* error code to pass back to sender */
58 struct message *callback_msg; /* message to queue for callback */
59 void *data; /* message reply data */
60 unsigned int data_size; /* size of message reply data */
61 struct timeout_user *timeout; /* result timeout */
66 struct list entry; /* entry in message list */
67 enum message_type type; /* message type */
68 user_handle_t win; /* window handle */
69 unsigned int msg; /* message code */
70 unsigned int wparam; /* parameters */
71 unsigned int lparam; /* parameters */
72 int x; /* x position */
73 int y; /* y position */
74 unsigned int time; /* message time */
75 unsigned int info; /* extra info */
76 user_handle_t hook; /* winevent hook handle */
77 void *hook_proc; /* winevent hook proc address */
78 void *data; /* message data for sent messages */
79 unsigned int data_size; /* size of message data */
80 unsigned int unique_id; /* unique id for nested hw message waits */
81 struct message_result *result; /* result in sender queue */
86 struct list entry; /* entry in timer list */
87 struct timeval when; /* next expiration */
88 unsigned int rate; /* timer rate in ms */
89 user_handle_t win; /* window handle */
90 unsigned int msg; /* message to post */
91 unsigned int id; /* timer id */
92 unsigned int lparam; /* lparam for message */
97 struct object obj; /* object header */
98 struct desktop *desktop; /* desktop that this thread input belongs to */
99 user_handle_t focus; /* focus window */
100 user_handle_t capture; /* capture window */
101 user_handle_t active; /* active window */
102 user_handle_t menu_owner; /* current menu owner window */
103 user_handle_t move_size; /* current moving/resizing window */
104 user_handle_t caret; /* caret window */
105 rectangle_t caret_rect; /* caret rectangle */
106 int caret_hide; /* caret hide count */
107 int caret_state; /* caret on/off state */
108 struct list msg_list; /* list of hardware messages */
109 unsigned char keystate[256]; /* state of each key */
114 struct object obj; /* object header */
115 unsigned int wake_bits; /* wakeup bits */
116 unsigned int wake_mask; /* wakeup mask */
117 unsigned int changed_bits; /* changed wakeup bits */
118 unsigned int changed_mask; /* changed wakeup mask */
119 int paint_count; /* pending paint messages count */
120 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
121 struct list send_result; /* stack of sent messages waiting for result */
122 struct list callback_result; /* list of callback messages waiting for result */
123 struct message_result *recv_result; /* stack of received messages waiting for result */
124 struct list pending_timers; /* list of pending timers */
125 struct list expired_timers; /* list of expired timers */
126 unsigned int next_timer_id; /* id for the next timer with a 0 window */
127 struct timeout_user *timeout; /* timeout for next timer to expire */
128 struct thread_input *input; /* thread input descriptor */
129 struct hook_table *hooks; /* hook table */
130 struct timeval last_get_msg; /* time of last get message call */
133 static void msg_queue_dump( struct object *obj, int verbose );
134 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
135 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
136 static int msg_queue_signaled( struct object *obj, struct thread *thread );
137 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
138 static void msg_queue_destroy( struct object *obj );
139 static void thread_input_dump( struct object *obj, int verbose );
140 static void thread_input_destroy( struct object *obj );
141 static void timer_callback( void *private );
143 static const struct object_ops msg_queue_ops =
145 sizeof(struct msg_queue), /* size */
146 msg_queue_dump, /* dump */
147 msg_queue_add_queue, /* add_queue */
148 msg_queue_remove_queue, /* remove_queue */
149 msg_queue_signaled, /* signaled */
150 msg_queue_satisfied, /* satisfied */
151 no_signal, /* signal */
152 no_get_fd, /* get_fd */
153 no_lookup_name, /* lookup_name */
154 no_close_handle, /* close_handle */
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 */
166 NULL, /* satisfied */
167 no_signal, /* signal */
168 no_get_fd, /* get_fd */
169 no_lookup_name, /* lookup_name */
170 no_close_handle, /* close_handle */
171 thread_input_destroy /* destroy */
174 /* pointer to input structure of foreground thread */
175 static struct thread_input *foreground_input;
176 static unsigned int last_input_time;
179 /* set the caret window in a given thread input */
180 static void set_caret_window( struct thread_input *input, user_handle_t win )
182 if (!win || win != input->caret)
184 input->caret_rect.left = 0;
185 input->caret_rect.top = 0;
186 input->caret_rect.right = 0;
187 input->caret_rect.bottom = 0;
190 input->caret_hide = 1;
191 input->caret_state = 0;
194 /* create a thread input object */
195 static struct thread_input *create_thread_input( struct thread *thread )
197 struct thread_input *input;
199 if ((input = alloc_object( &thread_input_ops )))
201 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
209 input->menu_owner = 0;
210 input->move_size = 0;
211 list_init( &input->msg_list );
212 set_caret_window( input, 0 );
213 memset( input->keystate, 0, sizeof(input->keystate) );
218 /* release the thread input data of a given thread */
219 static inline void release_thread_input( struct thread *thread )
221 struct thread_input *input = thread->queue->input;
224 release_object( input );
225 thread->queue->input = NULL;
228 /* create a message queue object */
229 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
231 struct msg_queue *queue;
234 if (!input && !(input = create_thread_input( thread ))) return NULL;
235 if ((queue = alloc_object( &msg_queue_ops )))
237 queue->wake_bits = 0;
238 queue->wake_mask = 0;
239 queue->changed_bits = 0;
240 queue->changed_mask = 0;
241 queue->paint_count = 0;
242 queue->recv_result = NULL;
243 queue->next_timer_id = 1;
244 queue->timeout = NULL;
245 queue->input = (struct thread_input *)grab_object( input );
247 gettimeofday( &queue->last_get_msg, NULL );
248 list_init( &queue->send_result );
249 list_init( &queue->callback_result );
250 list_init( &queue->pending_timers );
251 list_init( &queue->expired_timers );
252 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
254 thread->queue = queue;
255 if (!thread->process->queue)
256 thread->process->queue = (struct msg_queue *)grab_object( queue );
258 release_object( input );
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_object( thread->queue );
281 thread->queue = NULL;
284 /* get the hook table for a given thread */
285 struct hook_table *get_queue_hooks( struct thread *thread )
287 if (!thread->queue) return NULL;
288 return thread->queue->hooks;
291 /* set the hook table for a given thread, allocating the queue if needed */
292 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
294 struct msg_queue *queue = thread->queue;
295 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
296 if (queue->hooks) release_object( queue->hooks );
297 queue->hooks = hooks;
300 /* check the queue status */
301 inline static int is_signaled( struct msg_queue *queue )
303 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
306 /* set some queue bits */
307 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
309 queue->wake_bits |= bits;
310 queue->changed_bits |= bits;
311 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
314 /* clear some queue bits */
315 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
317 queue->wake_bits &= ~bits;
318 queue->changed_bits &= ~bits;
321 /* check whether msg is a keyboard message */
322 inline static int is_keyboard_msg( struct message *msg )
324 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
327 /* check if message is matched by the filter */
328 inline static int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
330 return (msg >= first && msg <= last);
333 /* check whether a message filter contains at least one potential hardware message */
334 inline static int filter_contains_hw_range( unsigned int first, unsigned int last )
336 /* hardware message ranges are (in numerical order):
337 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
338 * WM_KEYFIRST .. WM_KEYLAST
339 * WM_MOUSEFIRST .. WM_MOUSELAST
341 if (last < WM_NCMOUSEFIRST) return 0;
342 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
343 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
344 if (first > WM_MOUSELAST) return 0;
348 /* get the QS_* bit corresponding to a given hardware message */
349 inline static int get_hardware_msg_bit( struct message *msg )
351 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
352 if (is_keyboard_msg( msg )) return QS_KEY;
353 return QS_MOUSEBUTTON;
356 /* get the current thread queue, creating it if needed */
357 inline static struct msg_queue *get_current_queue(void)
359 struct msg_queue *queue = current->queue;
360 if (!queue) queue = create_msg_queue( current, NULL );
364 /* get a (pseudo-)unique id to tag hardware messages */
365 inline static unsigned int get_unique_id(void)
367 static unsigned int id;
368 if (!++id) id = 1; /* avoid an id of 0 */
372 /* try to merge a message with the last in the list; return 1 if successful */
373 static int merge_message( struct thread_input *input, const struct message *msg )
375 struct message *prev;
376 struct list *ptr = list_tail( &input->msg_list );
379 prev = LIST_ENTRY( ptr, struct message, entry );
380 if (prev->unique_id) return 0;
381 if (prev->result) return 0;
382 if (prev->win != msg->win) return 0;
383 if (prev->msg != msg->msg) return 0;
384 if (prev->type != msg->type) return 0;
385 /* now we can merge it */
386 prev->wparam = msg->wparam;
387 prev->lparam = msg->lparam;
390 prev->time = msg->time;
391 prev->info = msg->info;
395 /* free a result structure */
396 static void free_result( struct message_result *result )
398 if (result->timeout) remove_timeout_user( result->timeout );
399 if (result->data) free( result->data );
400 if (result->callback_msg) free( result->callback_msg );
404 /* remove the result from the sender list it is on */
405 static inline void remove_result_from_sender( struct message_result *result )
407 assert( result->sender );
409 list_remove( &result->sender_entry );
410 result->sender = NULL;
411 if (!result->receiver) free_result( result );
414 /* store the message result in the appropriate structure */
415 static void store_message_result( struct message_result *res, unsigned int result,
418 res->result = result;
423 remove_timeout_user( res->timeout );
428 if (res->callback_msg)
430 /* queue the callback message in the sender queue */
431 res->callback_msg->lparam = result;
432 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
433 set_queue_bits( res->sender, QS_SENDMESSAGE );
434 res->callback_msg = NULL;
435 remove_result_from_sender( res );
439 /* wake sender queue if waiting on this result */
440 if (list_head(&res->sender->send_result) == &res->sender_entry)
441 set_queue_bits( res->sender, QS_SMRESULT );
447 /* free a message when deleting a queue or window */
448 static void free_message( struct message *msg )
450 struct message_result *result = msg->result;
456 result->receiver = NULL;
457 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
459 else free_result( result );
461 if (msg->data) free( msg->data );
465 /* remove (and free) a message from a message list */
466 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
467 enum message_kind kind )
469 list_remove( &msg->entry );
473 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
476 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
482 /* message timed out without getting a reply */
483 static void result_timeout( void *private )
485 struct message_result *result = private;
487 assert( !result->replied );
489 result->timeout = NULL;
491 if (result->msg) /* not received yet */
493 struct message *msg = result->msg;
497 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
498 result->receiver = NULL;
501 free_result( result );
506 store_message_result( result, 0, STATUS_TIMEOUT );
509 /* allocate and fill a message result structure */
510 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
511 struct msg_queue *recv_queue,
512 struct message *msg, int timeout,
513 void *callback, unsigned int callback_data )
515 struct message_result *result = mem_alloc( sizeof(*result) );
519 result->sender = send_queue;
520 result->receiver = recv_queue;
523 result->data_size = 0;
524 result->timeout = NULL;
526 if (msg->type == MSG_CALLBACK)
528 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
534 callback_msg->type = MSG_CALLBACK_RESULT;
535 callback_msg->win = msg->win;
536 callback_msg->msg = msg->msg;
537 callback_msg->wparam = (unsigned int)callback;
538 callback_msg->lparam = 0;
539 callback_msg->time = get_tick_count();
542 callback_msg->info = callback_data;
543 callback_msg->hook = 0;
544 callback_msg->hook_proc = NULL;
545 callback_msg->result = NULL;
546 callback_msg->data = NULL;
547 callback_msg->data_size = 0;
549 result->callback_msg = callback_msg;
550 list_add_head( &send_queue->callback_result, &result->sender_entry );
554 result->callback_msg = NULL;
555 list_add_head( &send_queue->send_result, &result->sender_entry );
561 gettimeofday( &when, NULL );
562 add_timeout( &when, timeout );
563 result->timeout = add_timeout_user( &when, result_timeout, result );
569 /* receive a message, removing it from the sent queue */
570 static void receive_message( struct msg_queue *queue, struct message *msg,
571 struct get_message_reply *reply )
573 struct message_result *result = msg->result;
575 reply->total = msg->data_size;
576 if (msg->data_size > get_reply_max_size())
578 set_error( STATUS_BUFFER_OVERFLOW );
581 reply->type = msg->type;
582 reply->win = msg->win;
583 reply->msg = msg->msg;
584 reply->wparam = msg->wparam;
585 reply->lparam = msg->lparam;
588 reply->time = msg->time;
589 reply->info = msg->info;
590 reply->hook = msg->hook;
591 reply->hook_proc = msg->hook_proc;
593 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
595 list_remove( &msg->entry );
596 /* put the result on the receiver result stack */
600 result->recv_next = queue->recv_result;
601 queue->recv_result = result;
604 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
607 /* set the result of the current received message */
608 static void reply_message( struct msg_queue *queue, unsigned int result,
609 unsigned int error, int remove, const void *data, size_t len )
611 struct message_result *res = queue->recv_result;
615 queue->recv_result = res->recv_next;
616 res->receiver = NULL;
617 if (!res->sender) /* no one waiting for it */
625 if (len && (res->data = memdup( data, len ))) res->data_size = len;
626 store_message_result( res, result, error );
630 /* retrieve a posted message */
631 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
632 unsigned int first, unsigned int last, unsigned int flags,
633 struct get_message_reply *reply )
637 /* check against the filters */
638 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
640 if (msg->msg == WM_QUIT) goto found; /* WM_QUIT is never filtered */
641 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
642 if (!check_msg_filter( msg->msg, first, last )) continue;
643 goto found; /* found one */
647 /* return it to the app */
649 reply->total = msg->data_size;
650 if (msg->data_size > get_reply_max_size())
652 set_error( STATUS_BUFFER_OVERFLOW );
655 reply->type = msg->type;
656 reply->win = msg->win;
657 reply->msg = msg->msg;
658 reply->wparam = msg->wparam;
659 reply->lparam = msg->lparam;
662 reply->time = msg->time;
663 reply->info = msg->info;
665 if (flags & GET_MSG_REMOVE)
669 set_reply_data_ptr( msg->data, msg->data_size );
673 remove_queue_message( queue, msg, POST_MESSAGE );
675 else if (msg->data) set_reply_data( msg->data, msg->data_size );
680 /* empty a message list and free all the messages */
681 static void empty_msg_list( struct list *list )
685 while ((ptr = list_head( list )) != NULL)
687 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
688 list_remove( &msg->entry );
693 /* cleanup all pending results when deleting a queue */
694 static void cleanup_results( struct msg_queue *queue )
698 while ((entry = list_head( &queue->send_result )) != NULL)
700 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
703 while ((entry = list_head( &queue->callback_result )) != NULL)
705 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
708 while (queue->recv_result)
709 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
712 /* check if the thread owning the queue is hung (not checking for messages) */
713 static int is_queue_hung( struct msg_queue *queue )
716 struct wait_queue_entry *entry;
718 gettimeofday( &now, NULL );
719 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
720 return 0; /* less than 5 seconds since last get message -> not hung */
722 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
724 if (entry->thread->queue == queue)
725 return 0; /* thread is waiting on queue -> not hung */
730 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
732 struct msg_queue *queue = (struct msg_queue *)obj;
733 struct process *process = entry->thread->process;
735 /* a thread can only wait on its own queue */
736 if (entry->thread->queue != queue)
738 set_error( STATUS_ACCESS_DENIED );
741 /* if waiting on the main process queue, set the idle event */
742 if (process->queue == queue)
744 if (process->idle_event) set_event( process->idle_event );
746 add_queue( obj, entry );
750 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
752 struct msg_queue *queue = (struct msg_queue *)obj;
753 struct process *process = entry->thread->process;
755 remove_queue( obj, entry );
757 assert( entry->thread->queue == queue );
759 /* if waiting on the main process queue, reset the idle event */
760 if (process->queue == queue)
762 if (process->idle_event) reset_event( process->idle_event );
766 static void msg_queue_dump( struct object *obj, int verbose )
768 struct msg_queue *queue = (struct msg_queue *)obj;
769 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
770 queue->wake_bits, queue->wake_mask );
773 static int msg_queue_signaled( struct object *obj, struct thread *thread )
775 struct msg_queue *queue = (struct msg_queue *)obj;
776 return is_signaled( queue );
779 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
781 struct msg_queue *queue = (struct msg_queue *)obj;
782 queue->wake_mask = 0;
783 queue->changed_mask = 0;
784 return 0; /* Not abandoned */
787 static void msg_queue_destroy( struct object *obj )
789 struct msg_queue *queue = (struct msg_queue *)obj;
793 cleanup_results( queue );
794 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
796 while ((ptr = list_head( &queue->pending_timers )))
798 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
799 list_remove( &timer->entry );
802 while ((ptr = list_head( &queue->expired_timers )))
804 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
805 list_remove( &timer->entry );
808 if (queue->timeout) remove_timeout_user( queue->timeout );
809 if (queue->input) release_object( queue->input );
810 if (queue->hooks) release_object( queue->hooks );
813 static void thread_input_dump( struct object *obj, int verbose )
815 struct thread_input *input = (struct thread_input *)obj;
816 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
817 input->focus, input->capture, input->active );
820 static void thread_input_destroy( struct object *obj )
822 struct thread_input *input = (struct thread_input *)obj;
824 if (foreground_input == input) foreground_input = NULL;
825 empty_msg_list( &input->msg_list );
826 release_object( input->desktop );
829 /* fix the thread input data when a window is destroyed */
830 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
832 struct thread_input *input = queue->input;
834 if (window == input->focus) input->focus = 0;
835 if (window == input->capture) input->capture = 0;
836 if (window == input->active) input->active = 0;
837 if (window == input->menu_owner) input->menu_owner = 0;
838 if (window == input->move_size) input->move_size = 0;
839 if (window == input->caret) set_caret_window( input, 0 );
842 /* check if the specified window can be set in the input data of a given queue */
843 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
845 struct thread *thread;
848 if (!window) return 1; /* we can always clear the data */
850 if ((thread = get_window_thread( window )))
852 ret = (queue->input == thread->queue->input);
853 if (!ret) set_error( STATUS_ACCESS_DENIED );
854 release_object( thread );
856 else set_error( STATUS_INVALID_HANDLE );
861 /* make sure the specified thread has a queue */
862 int init_thread_queue( struct thread *thread )
864 if (thread->queue) return 1;
865 return (create_msg_queue( thread, NULL ) != NULL);
868 /* attach two thread input data structures */
869 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
871 struct desktop *desktop;
872 struct thread_input *input;
874 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
875 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
876 input = (struct thread_input *)grab_object( thread_to->queue->input );
877 if (input->desktop != desktop)
879 set_error( STATUS_ACCESS_DENIED );
880 release_object( input );
881 release_object( desktop );
884 release_object( desktop );
886 if (thread_from->queue)
888 release_thread_input( thread_from );
889 thread_from->queue->input = input;
893 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
895 memset( input->keystate, 0, sizeof(input->keystate) );
899 /* detach two thread input data structures */
900 void detach_thread_input( struct thread *thread_from )
902 struct thread_input *input;
904 if ((input = create_thread_input( thread_from )))
906 release_thread_input( thread_from );
907 thread_from->queue->input = input;
912 /* set the next timer to expire */
913 static void set_next_timer( struct msg_queue *queue )
919 remove_timeout_user( queue->timeout );
920 queue->timeout = NULL;
922 if ((ptr = list_head( &queue->pending_timers )))
924 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
925 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
927 /* set/clear QS_TIMER bit */
928 if (list_empty( &queue->expired_timers ))
929 clear_queue_bits( queue, QS_TIMER );
931 set_queue_bits( queue, QS_TIMER );
934 /* find a timer from its window and id */
935 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
936 unsigned int msg, unsigned int id )
940 /* we need to search both lists */
942 LIST_FOR_EACH( ptr, &queue->pending_timers )
944 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
945 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
947 LIST_FOR_EACH( ptr, &queue->expired_timers )
949 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
950 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
955 /* callback for the next timer expiration */
956 static void timer_callback( void *private )
958 struct msg_queue *queue = private;
961 queue->timeout = NULL;
962 /* move on to the next timer */
963 ptr = list_head( &queue->pending_timers );
965 list_add_tail( &queue->expired_timers, ptr );
966 set_next_timer( queue );
969 /* link a timer at its rightful place in the queue list */
970 static void link_timer( struct msg_queue *queue, struct timer *timer )
974 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
976 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
977 if (!time_before( &t->when, &timer->when )) break;
979 list_add_before( ptr, &timer->entry );
982 /* remove a timer from the queue timer list and free it */
983 static void free_timer( struct msg_queue *queue, struct timer *timer )
985 list_remove( &timer->entry );
987 set_next_timer( queue );
990 /* restart an expired timer */
991 static void restart_timer( struct msg_queue *queue, struct timer *timer )
995 list_remove( &timer->entry );
996 gettimeofday( &now, NULL );
997 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
998 link_timer( queue, timer );
999 set_next_timer( queue );
1002 /* find an expired timer matching the filtering parameters */
1003 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1004 unsigned int get_first, unsigned int get_last,
1009 LIST_FOR_EACH( ptr, &queue->expired_timers )
1011 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1012 if (win && timer->win != win) continue;
1013 if (check_msg_filter( timer->msg, get_first, get_last ))
1015 if (remove) restart_timer( queue, timer );
1023 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1025 struct timer *timer = mem_alloc( sizeof(*timer) );
1028 timer->rate = max( rate, 1 );
1029 gettimeofday( &timer->when, NULL );
1030 add_timeout( &timer->when, rate );
1031 link_timer( queue, timer );
1032 /* check if we replaced the next timer */
1033 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1038 /* change the input key state for a given key */
1039 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1043 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1044 input->keystate[key] |= 0x80;
1046 else input->keystate[key] &= ~0x80;
1049 /* update the input key state for a keyboard message */
1050 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1053 int down = 0, extended;
1057 case WM_LBUTTONDOWN:
1061 set_input_key_state( input, VK_LBUTTON, down );
1063 case WM_MBUTTONDOWN:
1067 set_input_key_state( input, VK_MBUTTON, down );
1069 case WM_RBUTTONDOWN:
1073 set_input_key_state( input, VK_RBUTTON, down );
1075 case WM_XBUTTONDOWN:
1079 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1080 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1088 key = (unsigned char)msg->wparam;
1089 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1090 set_input_key_state( input, key, down );
1094 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1097 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1100 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1107 /* release the hardware message currently being processed by the given thread */
1108 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1109 int remove, user_handle_t new_win )
1111 struct thread_input *input = queue->input;
1112 struct message *msg;
1114 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1116 if (msg->unique_id == hw_id) break;
1118 if (&msg->entry == &input->msg_list) return; /* not found */
1120 /* clear the queue bit for that message */
1121 if (remove || new_win)
1123 struct message *other;
1126 clr_bit = get_hardware_msg_bit( msg );
1127 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1129 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1135 if (clr_bit) clear_queue_bits( queue, clr_bit );
1138 if (new_win) /* set the new window */
1140 struct thread *owner = get_window_thread( new_win );
1143 if (owner->queue->input == input)
1146 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1149 release_object( owner );
1154 update_input_key_state( input, msg );
1155 list_remove( &msg->entry );
1156 free_message( msg );
1160 /* find the window that should receive a given hardware message */
1161 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1162 unsigned int *msg_code )
1164 user_handle_t win = 0;
1166 *msg_code = msg->msg;
1167 if (is_keyboard_msg( msg ))
1169 if (input && !(win = input->focus))
1171 win = input->active;
1172 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1175 else /* mouse message */
1177 if (!input || !(win = input->capture))
1179 if (!(win = msg->win) || !is_window_visible( win ))
1181 if (input) win = window_from_point( input->desktop, msg->x, msg->y );
1188 /* queue a hardware message into a given thread input */
1189 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1192 struct thread *thread;
1193 struct thread_input *input;
1194 unsigned int msg_code;
1196 last_input_time = get_tick_count();
1198 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1199 if (!win || !(thread = get_window_thread(win)))
1204 input = thread->queue->input;
1206 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1209 msg->unique_id = 0; /* will be set once we return it to the app */
1210 list_add_tail( &input->msg_list, &msg->entry );
1211 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1213 release_object( thread );
1216 /* check message filter for a hardware message */
1217 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1218 user_handle_t filter_win, unsigned int first, unsigned int last )
1220 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1222 /* we can only test the window for a keyboard message since the
1223 * dest window for a mouse message depends on hittest */
1224 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1226 /* the message code is final for a keyboard message, we can simply check it */
1227 return check_msg_filter( msg_code, first, last );
1229 else /* mouse message */
1231 /* we need to check all possible values that the message can have in the end */
1233 if (check_msg_filter( msg_code, first, last )) return 1;
1234 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1236 /* all other messages can become non-client messages */
1237 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1239 /* clicks can become double-clicks or non-client double-clicks */
1240 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1241 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1243 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1244 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1251 /* find a hardware message for the given queue */
1252 static int get_hardware_message( struct thread *thread, int hw_id, user_handle_t filter_win,
1253 unsigned int first, unsigned int last, struct get_message_reply *reply )
1255 struct thread_input *input = thread->queue->input;
1256 struct thread *win_thread;
1259 int clear_bits, got_one = 0;
1260 unsigned int msg_code;
1262 ptr = list_head( &input->msg_list );
1267 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1268 if (msg->unique_id == hw_id) break;
1269 ptr = list_next( &input->msg_list, ptr );
1271 if (!ptr) ptr = list_head( &input->msg_list );
1272 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1275 if (ptr == list_head( &input->msg_list ))
1276 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1278 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1282 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1283 ptr = list_next( &input->msg_list, ptr );
1284 win = find_hardware_message_window( input, msg, &msg_code );
1285 if (!win || !(win_thread = get_window_thread( win )))
1287 /* no window at all, remove it */
1288 update_input_key_state( input, msg );
1289 list_remove( &msg->entry );
1290 free_message( msg );
1293 if (win_thread != thread)
1295 if (win_thread->queue->input == input)
1297 /* wake the other thread */
1298 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1303 /* for another thread input, drop it */
1304 update_input_key_state( input, msg );
1305 list_remove( &msg->entry );
1306 free_message( msg );
1308 release_object( win_thread );
1311 release_object( win_thread );
1313 /* if we already got a message for another thread, or if it doesn't
1314 * match the filter we skip it */
1315 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1317 clear_bits &= ~get_hardware_msg_bit( msg );
1320 /* now we can return it */
1321 if (!msg->unique_id) msg->unique_id = get_unique_id();
1322 reply->type = MSG_HARDWARE;
1324 reply->msg = msg_code;
1325 reply->wparam = msg->wparam;
1326 reply->lparam = msg->lparam;
1329 reply->time = msg->time;
1330 reply->info = msg->info;
1331 reply->hw_id = msg->unique_id;
1334 /* nothing found, clear the hardware queue bits */
1335 clear_queue_bits( thread->queue, clear_bits );
1339 /* increment (or decrement if 'incr' is negative) the queue paint count */
1340 void inc_queue_paint_count( struct thread *thread, int incr )
1342 struct msg_queue *queue = thread->queue;
1346 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1348 if (queue->paint_count)
1349 set_queue_bits( queue, QS_PAINT );
1351 clear_queue_bits( queue, QS_PAINT );
1355 /* remove all messages and timers belonging to a certain window */
1356 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1358 struct msg_queue *queue = thread->queue;
1366 ptr = list_head( &queue->pending_timers );
1369 struct list *next = list_next( &queue->pending_timers, ptr );
1370 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1371 if (timer->win == win) free_timer( queue, timer );
1374 ptr = list_head( &queue->expired_timers );
1377 struct list *next = list_next( &queue->expired_timers, ptr );
1378 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1379 if (timer->win == win) free_timer( queue, timer );
1383 /* remove messages */
1384 for (i = 0; i < NB_MSG_KINDS; i++)
1386 struct list *ptr, *next;
1388 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1390 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1391 if (msg->win == win) remove_queue_message( queue, msg, i );
1395 thread_input_cleanup_window( queue, win );
1398 /* post a message to a window; used by socket handling */
1399 void post_message( user_handle_t win, unsigned int message,
1400 unsigned int wparam, unsigned int lparam )
1402 struct message *msg;
1403 struct thread *thread = get_window_thread( win );
1405 if (!thread) return;
1407 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1409 msg->type = MSG_POSTED;
1410 msg->win = get_user_full_handle( win );
1412 msg->wparam = wparam;
1413 msg->lparam = lparam;
1414 msg->time = get_tick_count();
1419 msg->hook_proc = NULL;
1424 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1425 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1427 release_object( thread );
1430 /* post a win event */
1431 void post_win_event( struct thread *thread, unsigned int event,
1432 user_handle_t win, unsigned int object_id,
1433 unsigned int child_id, void *hook_proc,
1434 const WCHAR *module, size_t module_size,
1437 struct message *msg;
1439 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1441 msg->type = MSG_WINEVENT;
1442 msg->win = get_user_full_handle( win );
1444 msg->wparam = object_id;
1445 msg->lparam = child_id;
1446 msg->time = get_tick_count();
1449 msg->info = get_thread_id( current );
1452 msg->hook_proc = hook_proc;
1454 if ((msg->data = malloc( module_size )))
1456 msg->data_size = module_size;
1457 memcpy( msg->data, module, module_size );
1459 if (debug_level > 1)
1460 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1461 get_thread_id(thread), event, win, object_id, child_id );
1462 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1463 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1470 /* get the message queue of the current thread */
1471 DECL_HANDLER(get_msg_queue)
1473 struct msg_queue *queue = get_current_queue();
1476 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1480 /* set the current message queue wakeup mask */
1481 DECL_HANDLER(set_queue_mask)
1483 struct msg_queue *queue = get_current_queue();
1487 queue->wake_mask = req->wake_mask;
1488 queue->changed_mask = req->changed_mask;
1489 reply->wake_bits = queue->wake_bits;
1490 reply->changed_bits = queue->changed_bits;
1491 if (is_signaled( queue ))
1493 /* if skip wait is set, do what would have been done in the subsequent wait */
1494 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1495 else wake_up( &queue->obj, 0 );
1501 /* get the current message queue status */
1502 DECL_HANDLER(get_queue_status)
1504 struct msg_queue *queue = current->queue;
1507 reply->wake_bits = queue->wake_bits;
1508 reply->changed_bits = queue->changed_bits;
1509 if (req->clear) queue->changed_bits = 0;
1511 else reply->wake_bits = reply->changed_bits = 0;
1515 /* send a message to a thread queue */
1516 DECL_HANDLER(send_message)
1518 struct message *msg;
1519 struct msg_queue *send_queue = get_current_queue();
1520 struct msg_queue *recv_queue = NULL;
1521 struct thread *thread = NULL;
1525 if (!(thread = get_thread_from_id( req->id ))) return;
1527 else if (req->type != MSG_HARDWARE)
1529 /* only hardware messages are allowed without destination thread */
1530 set_error( STATUS_INVALID_PARAMETER );
1534 if (thread && !(recv_queue = thread->queue))
1536 set_error( STATUS_INVALID_PARAMETER );
1537 release_object( thread );
1540 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1542 set_error( STATUS_TIMEOUT );
1543 release_object( thread );
1547 if ((msg = mem_alloc( sizeof(*msg) )))
1549 msg->type = req->type;
1550 msg->win = get_user_full_handle( req->win );
1551 msg->msg = req->msg;
1552 msg->wparam = req->wparam;
1553 msg->lparam = req->lparam;
1554 msg->time = req->time;
1557 msg->info = req->info;
1559 msg->hook_proc = NULL;
1566 case MSG_OTHER_PROCESS:
1567 msg->data_size = get_req_data_size();
1568 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1577 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1578 req->timeout, req->callback, req->info )))
1580 free_message( msg );
1585 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1586 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1589 /* needed for posted DDE messages */
1590 msg->data_size = get_req_data_size();
1591 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1596 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1597 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1600 queue_hardware_message( recv_queue, msg );
1602 case MSG_CALLBACK_RESULT: /* cannot send this one */
1604 set_error( STATUS_INVALID_PARAMETER );
1609 if (thread) release_object( thread );
1613 /* get a message from the current queue */
1614 DECL_HANDLER(get_message)
1616 struct timer *timer;
1618 struct msg_queue *queue = get_current_queue();
1619 user_handle_t get_win = get_user_full_handle( req->get_win );
1621 reply->active_hooks = get_active_hooks();
1624 gettimeofday( &queue->last_get_msg, NULL );
1626 /* first check for sent messages */
1627 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1629 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1630 receive_message( queue, msg, reply );
1633 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1635 /* clear changed bits so we can wait on them if we don't find a message */
1636 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits = 0;
1637 else queue->changed_bits &= QS_ALLPOSTMESSAGE;
1639 /* then check for posted messages */
1640 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1643 /* then check for any raw hardware message */
1644 if (filter_contains_hw_range( req->get_first, req->get_last ) &&
1645 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1648 /* now check for WM_PAINT */
1649 if (queue->paint_count &&
1650 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1651 (reply->win = find_window_to_repaint( get_win, current )))
1653 reply->type = MSG_POSTED;
1654 reply->msg = WM_PAINT;
1659 reply->time = get_tick_count();
1664 /* now check for timer */
1665 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1666 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1668 reply->type = MSG_POSTED;
1669 reply->win = timer->win;
1670 reply->msg = timer->msg;
1671 reply->wparam = timer->id;
1672 reply->lparam = timer->lparam;
1675 reply->time = get_tick_count();
1681 set_error( STATUS_PENDING ); /* FIXME */
1685 /* reply to a sent message */
1686 DECL_HANDLER(reply_message)
1688 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1689 else if (current->queue->recv_result)
1690 reply_message( current->queue, req->result, 0, req->remove,
1691 get_req_data(), get_req_data_size() );
1695 /* accept the current hardware message */
1696 DECL_HANDLER(accept_hardware_message)
1699 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1701 set_error( STATUS_ACCESS_DENIED );
1705 /* retrieve the reply for the last message sent */
1706 DECL_HANDLER(get_message_reply)
1708 struct message_result *result;
1710 struct msg_queue *queue = current->queue;
1714 set_error( STATUS_PENDING );
1717 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1719 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1720 if (result->replied || req->cancel)
1722 if (result->replied)
1724 reply->result = result->result;
1725 set_error( result->error );
1728 size_t data_len = min( result->data_size, get_reply_max_size() );
1729 set_reply_data_ptr( result->data, data_len );
1730 result->data = NULL;
1731 result->data_size = 0;
1734 remove_result_from_sender( result );
1736 entry = list_head( &queue->send_result );
1737 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1740 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1741 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1745 else set_error( STATUS_ACCESS_DENIED );
1749 /* set a window timer */
1750 DECL_HANDLER(set_win_timer)
1752 struct timer *timer;
1753 struct msg_queue *queue;
1754 struct thread *thread = NULL;
1755 user_handle_t win = 0;
1756 unsigned int id = req->id;
1760 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1762 set_error( STATUS_INVALID_HANDLE );
1765 if (thread->process != current->process)
1767 release_object( thread );
1768 set_error( STATUS_ACCESS_DENIED );
1771 queue = thread->queue;
1772 /* remove it if it existed already */
1773 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1777 queue = get_current_queue();
1778 /* find a free id for it */
1781 id = queue->next_timer_id;
1782 if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1784 while (find_timer( queue, 0, req->msg, id ));
1787 if ((timer = set_timer( queue, req->rate )))
1790 timer->msg = req->msg;
1792 timer->lparam = req->lparam;
1795 if (thread) release_object( thread );
1798 /* kill a window timer */
1799 DECL_HANDLER(kill_win_timer)
1801 struct timer *timer;
1802 struct thread *thread;
1803 user_handle_t win = 0;
1807 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1809 set_error( STATUS_INVALID_HANDLE );
1812 if (thread->process != current->process)
1814 release_object( thread );
1815 set_error( STATUS_ACCESS_DENIED );
1819 else thread = (struct thread *)grab_object( current );
1821 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1822 free_timer( thread->queue, timer );
1824 set_error( STATUS_INVALID_PARAMETER );
1826 release_object( thread );
1830 /* attach (or detach) thread inputs */
1831 DECL_HANDLER(attach_thread_input)
1833 struct thread *thread_from = get_thread_from_id( req->tid_from );
1834 struct thread *thread_to = get_thread_from_id( req->tid_to );
1836 if (!thread_from || !thread_to)
1838 if (thread_from) release_object( thread_from );
1839 if (thread_to) release_object( thread_to );
1842 if (thread_from != thread_to)
1844 if (req->attach) attach_thread_input( thread_from, thread_to );
1847 if (thread_from->queue && thread_to->queue &&
1848 thread_from->queue->input == thread_to->queue->input)
1849 detach_thread_input( thread_from );
1851 set_error( STATUS_ACCESS_DENIED );
1854 else set_error( STATUS_ACCESS_DENIED );
1855 release_object( thread_from );
1856 release_object( thread_to );
1860 /* get thread input data */
1861 DECL_HANDLER(get_thread_input)
1863 struct thread *thread = NULL;
1864 struct thread_input *input;
1868 if (!(thread = get_thread_from_id( req->tid ))) return;
1869 input = thread->queue ? thread->queue->input : NULL;
1871 else input = foreground_input; /* get the foreground thread info */
1875 reply->focus = input->focus;
1876 reply->capture = input->capture;
1877 reply->active = input->active;
1878 reply->menu_owner = input->menu_owner;
1879 reply->move_size = input->move_size;
1880 reply->caret = input->caret;
1881 reply->rect = input->caret_rect;
1888 reply->menu_owner = 0;
1889 reply->move_size = 0;
1891 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1893 /* foreground window is active window of foreground thread */
1894 reply->foreground = foreground_input ? foreground_input->active : 0;
1895 if (thread) release_object( thread );
1899 /* retrieve queue keyboard state for a given thread */
1900 DECL_HANDLER(get_key_state)
1902 struct thread *thread;
1903 struct thread_input *input;
1905 if (!(thread = get_thread_from_id( req->tid ))) return;
1906 input = thread->queue ? thread->queue->input : NULL;
1909 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1910 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1912 release_object( thread );
1916 /* set queue keyboard state for a given thread */
1917 DECL_HANDLER(set_key_state)
1919 struct thread *thread = NULL;
1920 struct thread_input *input;
1922 if (!(thread = get_thread_from_id( req->tid ))) return;
1923 input = thread->queue ? thread->queue->input : NULL;
1926 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1927 if (size) memcpy( input->keystate, get_req_data(), size );
1929 release_object( thread );
1933 /* set the system foreground window */
1934 DECL_HANDLER(set_foreground_window)
1936 struct msg_queue *queue = get_current_queue();
1938 reply->previous = foreground_input ? foreground_input->active : 0;
1939 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1940 reply->send_msg_new = FALSE;
1944 struct thread *thread;
1946 if (is_top_level_window( req->handle ) &&
1947 ((thread = get_window_thread( req->handle ))))
1949 foreground_input = thread->queue->input;
1950 reply->send_msg_new = (foreground_input != queue->input);
1951 release_object( thread );
1953 else set_error( STATUS_INVALID_HANDLE );
1955 else foreground_input = NULL;
1959 /* set the current thread focus window */
1960 DECL_HANDLER(set_focus_window)
1962 struct msg_queue *queue = get_current_queue();
1964 reply->previous = 0;
1965 if (queue && check_queue_input_window( queue, req->handle ))
1967 reply->previous = queue->input->focus;
1968 queue->input->focus = get_user_full_handle( req->handle );
1973 /* set the current thread active window */
1974 DECL_HANDLER(set_active_window)
1976 struct msg_queue *queue = get_current_queue();
1978 reply->previous = 0;
1979 if (queue && check_queue_input_window( queue, req->handle ))
1981 if (!req->handle || make_window_active( req->handle ))
1983 reply->previous = queue->input->active;
1984 queue->input->active = get_user_full_handle( req->handle );
1986 else set_error( STATUS_INVALID_HANDLE );
1991 /* set the current thread capture window */
1992 DECL_HANDLER(set_capture_window)
1994 struct msg_queue *queue = get_current_queue();
1996 reply->previous = reply->full_handle = 0;
1997 if (queue && check_queue_input_window( queue, req->handle ))
1999 struct thread_input *input = queue->input;
2001 reply->previous = input->capture;
2002 input->capture = get_user_full_handle( req->handle );
2003 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2004 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2005 reply->full_handle = input->capture;
2010 /* Set the current thread caret window */
2011 DECL_HANDLER(set_caret_window)
2013 struct msg_queue *queue = get_current_queue();
2015 reply->previous = 0;
2016 if (queue && check_queue_input_window( queue, req->handle ))
2018 struct thread_input *input = queue->input;
2020 reply->previous = input->caret;
2021 reply->old_rect = input->caret_rect;
2022 reply->old_hide = input->caret_hide;
2023 reply->old_state = input->caret_state;
2025 set_caret_window( input, get_user_full_handle(req->handle) );
2026 input->caret_rect.right = input->caret_rect.left + req->width;
2027 input->caret_rect.bottom = input->caret_rect.top + req->height;
2032 /* Set the current thread caret information */
2033 DECL_HANDLER(set_caret_info)
2035 struct msg_queue *queue = get_current_queue();
2036 struct thread_input *input;
2039 input = queue->input;
2040 reply->full_handle = input->caret;
2041 reply->old_rect = input->caret_rect;
2042 reply->old_hide = input->caret_hide;
2043 reply->old_state = input->caret_state;
2045 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2047 set_error( STATUS_ACCESS_DENIED );
2050 if (req->flags & SET_CARET_POS)
2052 input->caret_rect.right += req->x - input->caret_rect.left;
2053 input->caret_rect.bottom += req->y - input->caret_rect.top;
2054 input->caret_rect.left = req->x;
2055 input->caret_rect.top = req->y;
2057 if (req->flags & SET_CARET_HIDE)
2059 input->caret_hide += req->hide;
2060 if (input->caret_hide < 0) input->caret_hide = 0;
2062 if (req->flags & SET_CARET_STATE)
2064 if (req->state == -1) input->caret_state = !input->caret_state;
2065 else input->caret_state = !!req->state;
2070 /* get the time of the last input event */
2071 DECL_HANDLER(get_last_input_time)
2073 reply->time = last_input_time;