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"
30 #define WIN32_NO_STATUS
44 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
45 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
47 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
48 #define NB_MSG_KINDS (POST_MESSAGE+1)
53 struct list sender_entry; /* entry in sender list */
54 struct message *msg; /* message the result is for */
55 struct message_result *recv_next; /* next in receiver list */
56 struct msg_queue *sender; /* sender queue */
57 struct msg_queue *receiver; /* receiver queue */
58 int replied; /* has it been replied to? */
59 unsigned int result; /* reply result */
60 unsigned int error; /* error code to pass back to sender */
61 struct message *callback_msg; /* message to queue for callback */
62 void *data; /* message reply data */
63 unsigned int data_size; /* size of message reply data */
64 struct timeout_user *timeout; /* result timeout */
69 struct list entry; /* entry in message list */
70 enum message_type type; /* message type */
71 user_handle_t win; /* window handle */
72 unsigned int msg; /* message code */
73 unsigned int wparam; /* parameters */
74 unsigned int lparam; /* parameters */
75 int x; /* x position */
76 int y; /* y position */
77 unsigned int time; /* message time */
78 unsigned int info; /* extra info */
79 user_handle_t hook; /* winevent hook handle */
80 void *hook_proc; /* winevent hook proc address */
81 void *data; /* message data for sent messages */
82 unsigned int data_size; /* size of message data */
83 unsigned int unique_id; /* unique id for nested hw message waits */
84 struct message_result *result; /* result in sender queue */
89 struct list entry; /* entry in timer list */
90 struct timeval when; /* next expiration */
91 unsigned int rate; /* timer rate in ms */
92 user_handle_t win; /* window handle */
93 unsigned int msg; /* message to post */
94 unsigned int id; /* timer id */
95 unsigned int lparam; /* lparam for message */
100 struct object obj; /* object header */
101 struct desktop *desktop; /* desktop that this thread input belongs to */
102 user_handle_t focus; /* focus window */
103 user_handle_t capture; /* capture window */
104 user_handle_t active; /* active window */
105 user_handle_t menu_owner; /* current menu owner window */
106 user_handle_t move_size; /* current moving/resizing window */
107 user_handle_t caret; /* caret window */
108 rectangle_t caret_rect; /* caret rectangle */
109 int caret_hide; /* caret hide count */
110 int caret_state; /* caret on/off state */
111 struct list msg_list; /* list of hardware messages */
112 unsigned char keystate[256]; /* state of each key */
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 int quit_message; /* is there a pending quit message? */
124 int exit_code; /* exit code of pending quit message */
125 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
126 struct list send_result; /* stack of sent messages waiting for result */
127 struct list callback_result; /* list of callback messages waiting for result */
128 struct message_result *recv_result; /* stack of received messages waiting for result */
129 struct list pending_timers; /* list of pending timers */
130 struct list expired_timers; /* list of expired timers */
131 unsigned int next_timer_id; /* id for the next timer with a 0 window */
132 struct timeout_user *timeout; /* timeout for next timer to expire */
133 struct thread_input *input; /* thread input descriptor */
134 struct hook_table *hooks; /* hook table */
135 struct timeval last_get_msg; /* time of last get message call */
138 static void msg_queue_dump( struct object *obj, int verbose );
139 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
140 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
141 static int msg_queue_signaled( struct object *obj, struct thread *thread );
142 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
143 static void msg_queue_destroy( struct object *obj );
144 static void thread_input_dump( struct object *obj, int verbose );
145 static void thread_input_destroy( struct object *obj );
146 static void timer_callback( void *private );
148 static const struct object_ops msg_queue_ops =
150 sizeof(struct msg_queue), /* size */
151 msg_queue_dump, /* dump */
152 msg_queue_add_queue, /* add_queue */
153 msg_queue_remove_queue, /* remove_queue */
154 msg_queue_signaled, /* signaled */
155 msg_queue_satisfied, /* satisfied */
156 no_signal, /* signal */
157 no_get_fd, /* get_fd */
158 no_map_access, /* map_access */
159 no_lookup_name, /* lookup_name */
160 no_close_handle, /* close_handle */
161 msg_queue_destroy /* destroy */
165 static const struct object_ops thread_input_ops =
167 sizeof(struct thread_input), /* size */
168 thread_input_dump, /* dump */
169 no_add_queue, /* add_queue */
170 NULL, /* remove_queue */
172 NULL, /* satisfied */
173 no_signal, /* signal */
174 no_get_fd, /* get_fd */
175 no_map_access, /* map_access */
176 no_lookup_name, /* lookup_name */
177 no_close_handle, /* close_handle */
178 thread_input_destroy /* destroy */
181 /* pointer to input structure of foreground thread */
182 static struct thread_input *foreground_input;
183 static unsigned int last_input_time;
186 /* set the caret window in a given thread input */
187 static void set_caret_window( struct thread_input *input, user_handle_t win )
189 if (!win || win != input->caret)
191 input->caret_rect.left = 0;
192 input->caret_rect.top = 0;
193 input->caret_rect.right = 0;
194 input->caret_rect.bottom = 0;
197 input->caret_hide = 1;
198 input->caret_state = 0;
201 /* create a thread input object */
202 static struct thread_input *create_thread_input( struct thread *thread )
204 struct thread_input *input;
206 if ((input = alloc_object( &thread_input_ops )))
208 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
216 input->menu_owner = 0;
217 input->move_size = 0;
218 list_init( &input->msg_list );
219 set_caret_window( input, 0 );
220 memset( input->keystate, 0, sizeof(input->keystate) );
225 /* release the thread input data of a given thread */
226 static inline void release_thread_input( struct thread *thread )
228 struct thread_input *input = thread->queue->input;
231 release_object( input );
232 thread->queue->input = NULL;
235 /* create a message queue object */
236 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
238 struct msg_queue *queue;
241 if (!input && !(input = create_thread_input( thread ))) return NULL;
242 if ((queue = alloc_object( &msg_queue_ops )))
244 queue->wake_bits = 0;
245 queue->wake_mask = 0;
246 queue->changed_bits = 0;
247 queue->changed_mask = 0;
248 queue->paint_count = 0;
249 queue->quit_message = 0;
250 queue->recv_result = NULL;
251 queue->next_timer_id = 1;
252 queue->timeout = NULL;
253 queue->input = (struct thread_input *)grab_object( input );
255 gettimeofday( &queue->last_get_msg, NULL );
256 list_init( &queue->send_result );
257 list_init( &queue->callback_result );
258 list_init( &queue->pending_timers );
259 list_init( &queue->expired_timers );
260 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
262 thread->queue = queue;
263 if (!thread->process->queue)
264 thread->process->queue = (struct msg_queue *)grab_object( queue );
266 release_object( input );
270 /* free the message queue of a thread at thread exit */
271 void free_msg_queue( struct thread *thread )
273 struct process *process = thread->process;
275 remove_thread_hooks( thread );
276 if (!thread->queue) return;
277 if (process->queue == thread->queue) /* is it the process main queue? */
279 release_object( process->queue );
280 process->queue = NULL;
281 if (process->idle_event)
283 set_event( process->idle_event );
284 release_object( process->idle_event );
285 process->idle_event = NULL;
288 release_object( thread->queue );
289 thread->queue = NULL;
292 /* get the hook table for a given thread */
293 struct hook_table *get_queue_hooks( struct thread *thread )
295 if (!thread->queue) return NULL;
296 return thread->queue->hooks;
299 /* set the hook table for a given thread, allocating the queue if needed */
300 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
302 struct msg_queue *queue = thread->queue;
303 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
304 if (queue->hooks) release_object( queue->hooks );
305 queue->hooks = hooks;
308 /* check the queue status */
309 inline static int is_signaled( struct msg_queue *queue )
311 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
314 /* set some queue bits */
315 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
317 queue->wake_bits |= bits;
318 queue->changed_bits |= bits;
319 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
322 /* clear some queue bits */
323 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
325 queue->wake_bits &= ~bits;
326 queue->changed_bits &= ~bits;
329 /* check whether msg is a keyboard message */
330 inline static int is_keyboard_msg( struct message *msg )
332 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
335 /* check if message is matched by the filter */
336 inline static int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
338 return (msg >= first && msg <= last);
341 /* check whether a message filter contains at least one potential hardware message */
342 inline static int filter_contains_hw_range( unsigned int first, unsigned int last )
344 /* hardware message ranges are (in numerical order):
345 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
346 * WM_KEYFIRST .. WM_KEYLAST
347 * WM_MOUSEFIRST .. WM_MOUSELAST
349 if (last < WM_NCMOUSEFIRST) return 0;
350 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
351 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
352 if (first > WM_MOUSELAST) return 0;
356 /* get the QS_* bit corresponding to a given hardware message */
357 inline static int get_hardware_msg_bit( struct message *msg )
359 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
360 if (is_keyboard_msg( msg )) return QS_KEY;
361 return QS_MOUSEBUTTON;
364 /* get the current thread queue, creating it if needed */
365 inline static struct msg_queue *get_current_queue(void)
367 struct msg_queue *queue = current->queue;
368 if (!queue) queue = create_msg_queue( current, NULL );
372 /* get a (pseudo-)unique id to tag hardware messages */
373 inline static unsigned int get_unique_id(void)
375 static unsigned int id;
376 if (!++id) id = 1; /* avoid an id of 0 */
380 /* try to merge a message with the last in the list; return 1 if successful */
381 static int merge_message( struct thread_input *input, const struct message *msg )
383 struct message *prev;
384 struct list *ptr = list_tail( &input->msg_list );
387 prev = LIST_ENTRY( ptr, struct message, entry );
388 if (prev->unique_id) return 0;
389 if (prev->result) return 0;
390 if (prev->win != msg->win) return 0;
391 if (prev->msg != msg->msg) return 0;
392 if (prev->type != msg->type) return 0;
393 /* now we can merge it */
394 prev->wparam = msg->wparam;
395 prev->lparam = msg->lparam;
398 prev->time = msg->time;
399 prev->info = msg->info;
403 /* free a result structure */
404 static void free_result( struct message_result *result )
406 if (result->timeout) remove_timeout_user( result->timeout );
407 if (result->data) free( result->data );
408 if (result->callback_msg) free( result->callback_msg );
412 /* remove the result from the sender list it is on */
413 static inline void remove_result_from_sender( struct message_result *result )
415 assert( result->sender );
417 list_remove( &result->sender_entry );
418 result->sender = NULL;
419 if (!result->receiver) free_result( result );
422 /* store the message result in the appropriate structure */
423 static void store_message_result( struct message_result *res, unsigned int result,
426 res->result = result;
431 remove_timeout_user( res->timeout );
436 if (res->callback_msg)
438 /* queue the callback message in the sender queue */
439 res->callback_msg->lparam = result;
440 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
441 set_queue_bits( res->sender, QS_SENDMESSAGE );
442 res->callback_msg = NULL;
443 remove_result_from_sender( res );
447 /* wake sender queue if waiting on this result */
448 if (list_head(&res->sender->send_result) == &res->sender_entry)
449 set_queue_bits( res->sender, QS_SMRESULT );
455 /* free a message when deleting a queue or window */
456 static void free_message( struct message *msg )
458 struct message_result *result = msg->result;
464 result->receiver = NULL;
465 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
467 else free_result( result );
469 if (msg->data) free( msg->data );
473 /* remove (and free) a message from a message list */
474 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
475 enum message_kind kind )
477 list_remove( &msg->entry );
481 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
484 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
490 /* message timed out without getting a reply */
491 static void result_timeout( void *private )
493 struct message_result *result = private;
495 assert( !result->replied );
497 result->timeout = NULL;
499 if (result->msg) /* not received yet */
501 struct message *msg = result->msg;
505 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
506 result->receiver = NULL;
509 free_result( result );
514 store_message_result( result, 0, STATUS_TIMEOUT );
517 /* allocate and fill a message result structure */
518 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
519 struct msg_queue *recv_queue,
520 struct message *msg, int timeout,
521 void *callback, unsigned int callback_data )
523 struct message_result *result = mem_alloc( sizeof(*result) );
527 result->sender = send_queue;
528 result->receiver = recv_queue;
531 result->data_size = 0;
532 result->timeout = NULL;
534 if (msg->type == MSG_CALLBACK)
536 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
542 callback_msg->type = MSG_CALLBACK_RESULT;
543 callback_msg->win = msg->win;
544 callback_msg->msg = msg->msg;
545 callback_msg->wparam = (unsigned int)callback;
546 callback_msg->lparam = 0;
547 callback_msg->time = get_tick_count();
550 callback_msg->info = callback_data;
551 callback_msg->hook = 0;
552 callback_msg->hook_proc = NULL;
553 callback_msg->result = NULL;
554 callback_msg->data = NULL;
555 callback_msg->data_size = 0;
557 result->callback_msg = callback_msg;
558 list_add_head( &send_queue->callback_result, &result->sender_entry );
562 result->callback_msg = NULL;
563 list_add_head( &send_queue->send_result, &result->sender_entry );
569 gettimeofday( &when, NULL );
570 add_timeout( &when, timeout );
571 result->timeout = add_timeout_user( &when, result_timeout, result );
577 /* receive a message, removing it from the sent queue */
578 static void receive_message( struct msg_queue *queue, struct message *msg,
579 struct get_message_reply *reply )
581 struct message_result *result = msg->result;
583 reply->total = msg->data_size;
584 if (msg->data_size > get_reply_max_size())
586 set_error( STATUS_BUFFER_OVERFLOW );
589 reply->type = msg->type;
590 reply->win = msg->win;
591 reply->msg = msg->msg;
592 reply->wparam = msg->wparam;
593 reply->lparam = msg->lparam;
596 reply->time = msg->time;
597 reply->info = msg->info;
598 reply->hook = msg->hook;
599 reply->hook_proc = msg->hook_proc;
601 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
603 list_remove( &msg->entry );
604 /* put the result on the receiver result stack */
608 result->recv_next = queue->recv_result;
609 queue->recv_result = result;
612 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
615 /* set the result of the current received message */
616 static void reply_message( struct msg_queue *queue, unsigned int result,
617 unsigned int error, int remove, const void *data, size_t len )
619 struct message_result *res = queue->recv_result;
623 queue->recv_result = res->recv_next;
624 res->receiver = NULL;
625 if (!res->sender) /* no one waiting for it */
633 if (len && (res->data = memdup( data, len ))) res->data_size = len;
634 store_message_result( res, result, error );
638 /* retrieve a posted message */
639 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
640 unsigned int first, unsigned int last, unsigned int flags,
641 struct get_message_reply *reply )
645 /* check against the filters */
646 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
648 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
649 if (!check_msg_filter( msg->msg, first, last )) continue;
650 goto found; /* found one */
654 /* return it to the app */
656 reply->total = msg->data_size;
657 if (msg->data_size > get_reply_max_size())
659 set_error( STATUS_BUFFER_OVERFLOW );
662 reply->type = msg->type;
663 reply->win = msg->win;
664 reply->msg = msg->msg;
665 reply->wparam = msg->wparam;
666 reply->lparam = msg->lparam;
669 reply->time = msg->time;
670 reply->info = msg->info;
672 if (flags & GET_MSG_REMOVE)
676 set_reply_data_ptr( msg->data, msg->data_size );
680 remove_queue_message( queue, msg, POST_MESSAGE );
682 else if (msg->data) set_reply_data( msg->data, msg->data_size );
687 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
688 struct get_message_reply *reply )
690 if (queue->quit_message)
693 reply->type = MSG_POSTED;
695 reply->msg = WM_QUIT;
696 reply->wparam = queue->exit_code;
700 reply->time = get_tick_count();
703 if (flags & GET_MSG_REMOVE)
704 queue->quit_message = 0;
712 /* empty a message list and free all the messages */
713 static void empty_msg_list( struct list *list )
717 while ((ptr = list_head( list )) != NULL)
719 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
720 list_remove( &msg->entry );
725 /* cleanup all pending results when deleting a queue */
726 static void cleanup_results( struct msg_queue *queue )
730 while ((entry = list_head( &queue->send_result )) != NULL)
732 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
735 while ((entry = list_head( &queue->callback_result )) != NULL)
737 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
740 while (queue->recv_result)
741 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
744 /* check if the thread owning the queue is hung (not checking for messages) */
745 static int is_queue_hung( struct msg_queue *queue )
748 struct wait_queue_entry *entry;
750 gettimeofday( &now, NULL );
751 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
752 return 0; /* less than 5 seconds since last get message -> not hung */
754 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
756 if (entry->thread->queue == queue)
757 return 0; /* thread is waiting on queue -> not hung */
762 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
764 struct msg_queue *queue = (struct msg_queue *)obj;
765 struct process *process = entry->thread->process;
767 /* a thread can only wait on its own queue */
768 if (entry->thread->queue != queue)
770 set_error( STATUS_ACCESS_DENIED );
773 /* if waiting on the main process queue, set the idle event */
774 if (process->queue == queue)
776 if (process->idle_event) set_event( process->idle_event );
778 add_queue( obj, entry );
782 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
784 struct msg_queue *queue = (struct msg_queue *)obj;
785 struct process *process = entry->thread->process;
787 remove_queue( obj, entry );
789 assert( entry->thread->queue == queue );
791 /* if waiting on the main process queue, reset the idle event */
792 if (process->queue == queue)
794 if (process->idle_event) reset_event( process->idle_event );
798 static void msg_queue_dump( struct object *obj, int verbose )
800 struct msg_queue *queue = (struct msg_queue *)obj;
801 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
802 queue->wake_bits, queue->wake_mask );
805 static int msg_queue_signaled( struct object *obj, struct thread *thread )
807 struct msg_queue *queue = (struct msg_queue *)obj;
808 return is_signaled( queue );
811 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
813 struct msg_queue *queue = (struct msg_queue *)obj;
814 queue->wake_mask = 0;
815 queue->changed_mask = 0;
816 return 0; /* Not abandoned */
819 static void msg_queue_destroy( struct object *obj )
821 struct msg_queue *queue = (struct msg_queue *)obj;
825 cleanup_results( queue );
826 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
828 while ((ptr = list_head( &queue->pending_timers )))
830 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
831 list_remove( &timer->entry );
834 while ((ptr = list_head( &queue->expired_timers )))
836 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
837 list_remove( &timer->entry );
840 if (queue->timeout) remove_timeout_user( queue->timeout );
841 if (queue->input) release_object( queue->input );
842 if (queue->hooks) release_object( queue->hooks );
845 static void thread_input_dump( struct object *obj, int verbose )
847 struct thread_input *input = (struct thread_input *)obj;
848 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
849 input->focus, input->capture, input->active );
852 static void thread_input_destroy( struct object *obj )
854 struct thread_input *input = (struct thread_input *)obj;
856 if (foreground_input == input) foreground_input = NULL;
857 empty_msg_list( &input->msg_list );
858 release_object( input->desktop );
861 /* fix the thread input data when a window is destroyed */
862 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
864 struct thread_input *input = queue->input;
866 if (window == input->focus) input->focus = 0;
867 if (window == input->capture) input->capture = 0;
868 if (window == input->active) input->active = 0;
869 if (window == input->menu_owner) input->menu_owner = 0;
870 if (window == input->move_size) input->move_size = 0;
871 if (window == input->caret) set_caret_window( input, 0 );
874 /* check if the specified window can be set in the input data of a given queue */
875 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
877 struct thread *thread;
880 if (!window) return 1; /* we can always clear the data */
882 if ((thread = get_window_thread( window )))
884 ret = (queue->input == thread->queue->input);
885 if (!ret) set_error( STATUS_ACCESS_DENIED );
886 release_object( thread );
888 else set_error( STATUS_INVALID_HANDLE );
893 /* make sure the specified thread has a queue */
894 int init_thread_queue( struct thread *thread )
896 if (thread->queue) return 1;
897 return (create_msg_queue( thread, NULL ) != NULL);
900 /* attach two thread input data structures */
901 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
903 struct desktop *desktop;
904 struct thread_input *input;
906 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
907 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
908 input = (struct thread_input *)grab_object( thread_to->queue->input );
909 if (input->desktop != desktop)
911 set_error( STATUS_ACCESS_DENIED );
912 release_object( input );
913 release_object( desktop );
916 release_object( desktop );
918 if (thread_from->queue)
920 release_thread_input( thread_from );
921 thread_from->queue->input = input;
925 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
927 memset( input->keystate, 0, sizeof(input->keystate) );
931 /* detach two thread input data structures */
932 void detach_thread_input( struct thread *thread_from )
934 struct thread_input *input;
936 if ((input = create_thread_input( thread_from )))
938 release_thread_input( thread_from );
939 thread_from->queue->input = input;
944 /* set the next timer to expire */
945 static void set_next_timer( struct msg_queue *queue )
951 remove_timeout_user( queue->timeout );
952 queue->timeout = NULL;
954 if ((ptr = list_head( &queue->pending_timers )))
956 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
957 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
959 /* set/clear QS_TIMER bit */
960 if (list_empty( &queue->expired_timers ))
961 clear_queue_bits( queue, QS_TIMER );
963 set_queue_bits( queue, QS_TIMER );
966 /* find a timer from its window and id */
967 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
968 unsigned int msg, unsigned int id )
972 /* we need to search both lists */
974 LIST_FOR_EACH( ptr, &queue->pending_timers )
976 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
977 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
979 LIST_FOR_EACH( ptr, &queue->expired_timers )
981 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
982 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
987 /* callback for the next timer expiration */
988 static void timer_callback( void *private )
990 struct msg_queue *queue = private;
993 queue->timeout = NULL;
994 /* move on to the next timer */
995 ptr = list_head( &queue->pending_timers );
997 list_add_tail( &queue->expired_timers, ptr );
998 set_next_timer( queue );
1001 /* link a timer at its rightful place in the queue list */
1002 static void link_timer( struct msg_queue *queue, struct timer *timer )
1006 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1008 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1009 if (!time_before( &t->when, &timer->when )) break;
1011 list_add_before( ptr, &timer->entry );
1014 /* remove a timer from the queue timer list and free it */
1015 static void free_timer( struct msg_queue *queue, struct timer *timer )
1017 list_remove( &timer->entry );
1019 set_next_timer( queue );
1022 /* restart an expired timer */
1023 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1027 list_remove( &timer->entry );
1028 gettimeofday( &now, NULL );
1029 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
1030 link_timer( queue, timer );
1031 set_next_timer( queue );
1034 /* find an expired timer matching the filtering parameters */
1035 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1036 unsigned int get_first, unsigned int get_last,
1041 LIST_FOR_EACH( ptr, &queue->expired_timers )
1043 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1044 if (win && timer->win != win) continue;
1045 if (check_msg_filter( timer->msg, get_first, get_last ))
1047 if (remove) restart_timer( queue, timer );
1055 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1057 struct timer *timer = mem_alloc( sizeof(*timer) );
1060 timer->rate = max( rate, 1 );
1061 gettimeofday( &timer->when, NULL );
1062 add_timeout( &timer->when, rate );
1063 link_timer( queue, timer );
1064 /* check if we replaced the next timer */
1065 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1070 /* change the input key state for a given key */
1071 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1075 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1076 input->keystate[key] |= 0x80;
1078 else input->keystate[key] &= ~0x80;
1081 /* update the input key state for a keyboard message */
1082 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1085 int down = 0, extended;
1089 case WM_LBUTTONDOWN:
1093 set_input_key_state( input, VK_LBUTTON, down );
1095 case WM_MBUTTONDOWN:
1099 set_input_key_state( input, VK_MBUTTON, down );
1101 case WM_RBUTTONDOWN:
1105 set_input_key_state( input, VK_RBUTTON, down );
1107 case WM_XBUTTONDOWN:
1111 if (msg->wparam == XBUTTON1) set_input_key_state( input, VK_XBUTTON1, down );
1112 else if (msg->wparam == XBUTTON2) set_input_key_state( input, VK_XBUTTON2, down );
1120 key = (unsigned char)msg->wparam;
1121 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1122 set_input_key_state( input, key, down );
1126 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1129 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1132 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1139 /* release the hardware message currently being processed by the given thread */
1140 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1141 int remove, user_handle_t new_win )
1143 struct thread_input *input = queue->input;
1144 struct message *msg;
1146 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1148 if (msg->unique_id == hw_id) break;
1150 if (&msg->entry == &input->msg_list) return; /* not found */
1152 /* clear the queue bit for that message */
1153 if (remove || new_win)
1155 struct message *other;
1158 clr_bit = get_hardware_msg_bit( msg );
1159 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1161 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1167 if (clr_bit) clear_queue_bits( queue, clr_bit );
1170 if (new_win) /* set the new window */
1172 struct thread *owner = get_window_thread( new_win );
1175 if (owner->queue->input == input)
1178 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1181 release_object( owner );
1186 update_input_key_state( input, msg );
1187 list_remove( &msg->entry );
1188 free_message( msg );
1192 /* find the window that should receive a given hardware message */
1193 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1194 unsigned int *msg_code )
1196 user_handle_t win = 0;
1198 *msg_code = msg->msg;
1199 if (is_keyboard_msg( msg ))
1201 if (input && !(win = input->focus))
1203 win = input->active;
1204 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1207 else /* mouse message */
1209 if (!input || !(win = input->capture))
1211 if (!(win = msg->win) || !is_window_visible( win ))
1213 if (input) win = window_from_point( input->desktop, msg->x, msg->y );
1220 /* queue a hardware message into a given thread input */
1221 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1224 struct thread *thread;
1225 struct thread_input *input;
1226 unsigned int msg_code;
1228 last_input_time = get_tick_count();
1230 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1231 if (!win || !(thread = get_window_thread(win)))
1236 input = thread->queue->input;
1238 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1241 msg->unique_id = 0; /* will be set once we return it to the app */
1242 list_add_tail( &input->msg_list, &msg->entry );
1243 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1245 release_object( thread );
1248 /* check message filter for a hardware message */
1249 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1250 user_handle_t filter_win, unsigned int first, unsigned int last )
1252 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1254 /* we can only test the window for a keyboard message since the
1255 * dest window for a mouse message depends on hittest */
1256 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1258 /* the message code is final for a keyboard message, we can simply check it */
1259 return check_msg_filter( msg_code, first, last );
1261 else /* mouse message */
1263 /* we need to check all possible values that the message can have in the end */
1265 if (check_msg_filter( msg_code, first, last )) return 1;
1266 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1268 /* all other messages can become non-client messages */
1269 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1271 /* clicks can become double-clicks or non-client double-clicks */
1272 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1273 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1275 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1276 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1283 /* find a hardware message for the given queue */
1284 static int get_hardware_message( struct thread *thread, int hw_id, user_handle_t filter_win,
1285 unsigned int first, unsigned int last, struct get_message_reply *reply )
1287 struct thread_input *input = thread->queue->input;
1288 struct thread *win_thread;
1291 int clear_bits, got_one = 0;
1292 unsigned int msg_code;
1294 ptr = list_head( &input->msg_list );
1299 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1300 if (msg->unique_id == hw_id) break;
1301 ptr = list_next( &input->msg_list, ptr );
1303 if (!ptr) ptr = list_head( &input->msg_list );
1304 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1307 if (ptr == list_head( &input->msg_list ))
1308 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1310 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1314 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1315 ptr = list_next( &input->msg_list, ptr );
1316 win = find_hardware_message_window( input, msg, &msg_code );
1317 if (!win || !(win_thread = get_window_thread( win )))
1319 /* no window at all, remove it */
1320 update_input_key_state( input, msg );
1321 list_remove( &msg->entry );
1322 free_message( msg );
1325 if (win_thread != thread)
1327 if (win_thread->queue->input == input)
1329 /* wake the other thread */
1330 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1335 /* for another thread input, drop it */
1336 update_input_key_state( input, msg );
1337 list_remove( &msg->entry );
1338 free_message( msg );
1340 release_object( win_thread );
1343 release_object( win_thread );
1345 /* if we already got a message for another thread, or if it doesn't
1346 * match the filter we skip it */
1347 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1349 clear_bits &= ~get_hardware_msg_bit( msg );
1352 /* now we can return it */
1353 if (!msg->unique_id) msg->unique_id = get_unique_id();
1354 reply->type = MSG_HARDWARE;
1356 reply->msg = msg_code;
1357 reply->wparam = msg->wparam;
1358 reply->lparam = msg->lparam;
1361 reply->time = msg->time;
1362 reply->info = msg->info;
1363 reply->hw_id = msg->unique_id;
1366 /* nothing found, clear the hardware queue bits */
1367 clear_queue_bits( thread->queue, clear_bits );
1371 /* increment (or decrement if 'incr' is negative) the queue paint count */
1372 void inc_queue_paint_count( struct thread *thread, int incr )
1374 struct msg_queue *queue = thread->queue;
1378 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1380 if (queue->paint_count)
1381 set_queue_bits( queue, QS_PAINT );
1383 clear_queue_bits( queue, QS_PAINT );
1387 /* remove all messages and timers belonging to a certain window */
1388 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1390 struct msg_queue *queue = thread->queue;
1398 ptr = list_head( &queue->pending_timers );
1401 struct list *next = list_next( &queue->pending_timers, ptr );
1402 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1403 if (timer->win == win) free_timer( queue, timer );
1406 ptr = list_head( &queue->expired_timers );
1409 struct list *next = list_next( &queue->expired_timers, ptr );
1410 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1411 if (timer->win == win) free_timer( queue, timer );
1415 /* remove messages */
1416 for (i = 0; i < NB_MSG_KINDS; i++)
1418 struct list *ptr, *next;
1420 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1422 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1423 if (msg->win == win) remove_queue_message( queue, msg, i );
1427 thread_input_cleanup_window( queue, win );
1430 /* post a message to a window; used by socket handling */
1431 void post_message( user_handle_t win, unsigned int message,
1432 unsigned int wparam, unsigned int lparam )
1434 struct message *msg;
1435 struct thread *thread = get_window_thread( win );
1437 if (!thread) return;
1439 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1441 msg->type = MSG_POSTED;
1442 msg->win = get_user_full_handle( win );
1444 msg->wparam = wparam;
1445 msg->lparam = lparam;
1446 msg->time = get_tick_count();
1451 msg->hook_proc = NULL;
1456 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1457 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1459 release_object( thread );
1462 /* post a win event */
1463 void post_win_event( struct thread *thread, unsigned int event,
1464 user_handle_t win, unsigned int object_id,
1465 unsigned int child_id, void *hook_proc,
1466 const WCHAR *module, size_t module_size,
1469 struct message *msg;
1471 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1473 msg->type = MSG_WINEVENT;
1474 msg->win = get_user_full_handle( win );
1476 msg->wparam = object_id;
1477 msg->lparam = child_id;
1478 msg->time = get_tick_count();
1481 msg->info = get_thread_id( current );
1484 msg->hook_proc = hook_proc;
1486 if ((msg->data = malloc( module_size )))
1488 msg->data_size = module_size;
1489 memcpy( msg->data, module, module_size );
1491 if (debug_level > 1)
1492 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1493 get_thread_id(thread), event, win, object_id, child_id );
1494 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1495 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1502 /* get the message queue of the current thread */
1503 DECL_HANDLER(get_msg_queue)
1505 struct msg_queue *queue = get_current_queue();
1508 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1512 /* set the current message queue wakeup mask */
1513 DECL_HANDLER(set_queue_mask)
1515 struct msg_queue *queue = get_current_queue();
1519 queue->wake_mask = req->wake_mask;
1520 queue->changed_mask = req->changed_mask;
1521 reply->wake_bits = queue->wake_bits;
1522 reply->changed_bits = queue->changed_bits;
1523 if (is_signaled( queue ))
1525 /* if skip wait is set, do what would have been done in the subsequent wait */
1526 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1527 else wake_up( &queue->obj, 0 );
1533 /* get the current message queue status */
1534 DECL_HANDLER(get_queue_status)
1536 struct msg_queue *queue = current->queue;
1539 reply->wake_bits = queue->wake_bits;
1540 reply->changed_bits = queue->changed_bits;
1541 if (req->clear) queue->changed_bits = 0;
1543 else reply->wake_bits = reply->changed_bits = 0;
1547 /* send a message to a thread queue */
1548 DECL_HANDLER(send_message)
1550 struct message *msg;
1551 struct msg_queue *send_queue = get_current_queue();
1552 struct msg_queue *recv_queue = NULL;
1553 struct thread *thread = NULL;
1557 if (!(thread = get_thread_from_id( req->id ))) return;
1559 else if (req->type != MSG_HARDWARE)
1561 /* only hardware messages are allowed without destination thread */
1562 set_error( STATUS_INVALID_PARAMETER );
1566 if (thread && !(recv_queue = thread->queue))
1568 set_error( STATUS_INVALID_PARAMETER );
1569 release_object( thread );
1572 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1574 set_error( STATUS_TIMEOUT );
1575 release_object( thread );
1579 if ((msg = mem_alloc( sizeof(*msg) )))
1581 msg->type = req->type;
1582 msg->win = get_user_full_handle( req->win );
1583 msg->msg = req->msg;
1584 msg->wparam = req->wparam;
1585 msg->lparam = req->lparam;
1586 msg->time = req->time;
1589 msg->info = req->info;
1591 msg->hook_proc = NULL;
1598 case MSG_OTHER_PROCESS:
1599 msg->data_size = get_req_data_size();
1600 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1609 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1610 req->timeout, req->callback, req->info )))
1612 free_message( msg );
1617 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1618 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1621 /* needed for posted DDE messages */
1622 msg->data_size = get_req_data_size();
1623 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1628 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1629 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1632 queue_hardware_message( recv_queue, msg );
1634 case MSG_CALLBACK_RESULT: /* cannot send this one */
1636 set_error( STATUS_INVALID_PARAMETER );
1641 if (thread) release_object( thread );
1644 /* post a quit message to the current queue */
1645 DECL_HANDLER(post_quit_message)
1647 struct msg_queue *queue = get_current_queue();
1652 queue->quit_message = 1;
1653 queue->exit_code = req->exit_code;
1654 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1657 /* get a message from the current queue */
1658 DECL_HANDLER(get_message)
1660 struct timer *timer;
1662 struct msg_queue *queue = get_current_queue();
1663 user_handle_t get_win = get_user_full_handle( req->get_win );
1665 reply->active_hooks = get_active_hooks();
1668 gettimeofday( &queue->last_get_msg, NULL );
1670 /* first check for sent messages */
1671 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1673 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1674 receive_message( queue, msg, reply );
1677 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1679 /* clear changed bits so we can wait on them if we don't find a message */
1680 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits = 0;
1681 else queue->changed_bits &= QS_ALLPOSTMESSAGE;
1683 /* then check for posted messages */
1684 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1687 /* only check for quit messages if not posted messages pending.
1688 * note: the quit message isn't filtered */
1689 if (get_quit_message( queue, req->flags, reply ))
1692 /* then check for any raw hardware message */
1693 if (filter_contains_hw_range( req->get_first, req->get_last ) &&
1694 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
1697 /* now check for WM_PAINT */
1698 if (queue->paint_count &&
1699 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
1700 (reply->win = find_window_to_repaint( get_win, current )))
1702 reply->type = MSG_POSTED;
1703 reply->msg = WM_PAINT;
1708 reply->time = get_tick_count();
1713 /* now check for timer */
1714 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1715 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1717 reply->type = MSG_POSTED;
1718 reply->win = timer->win;
1719 reply->msg = timer->msg;
1720 reply->wparam = timer->id;
1721 reply->lparam = timer->lparam;
1724 reply->time = get_tick_count();
1730 set_error( STATUS_PENDING ); /* FIXME */
1734 /* reply to a sent message */
1735 DECL_HANDLER(reply_message)
1737 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
1738 else if (current->queue->recv_result)
1739 reply_message( current->queue, req->result, 0, req->remove,
1740 get_req_data(), get_req_data_size() );
1744 /* accept the current hardware message */
1745 DECL_HANDLER(accept_hardware_message)
1748 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
1750 set_error( STATUS_ACCESS_DENIED );
1754 /* retrieve the reply for the last message sent */
1755 DECL_HANDLER(get_message_reply)
1757 struct message_result *result;
1759 struct msg_queue *queue = current->queue;
1763 set_error( STATUS_PENDING );
1766 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1768 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1769 if (result->replied || req->cancel)
1771 if (result->replied)
1773 reply->result = result->result;
1774 set_error( result->error );
1777 size_t data_len = min( result->data_size, get_reply_max_size() );
1778 set_reply_data_ptr( result->data, data_len );
1779 result->data = NULL;
1780 result->data_size = 0;
1783 remove_result_from_sender( result );
1785 entry = list_head( &queue->send_result );
1786 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1789 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1790 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1794 else set_error( STATUS_ACCESS_DENIED );
1798 /* set a window timer */
1799 DECL_HANDLER(set_win_timer)
1801 struct timer *timer;
1802 struct msg_queue *queue;
1803 struct thread *thread = NULL;
1804 user_handle_t win = 0;
1805 unsigned int id = req->id;
1809 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1811 set_error( STATUS_INVALID_HANDLE );
1814 if (thread->process != current->process)
1816 release_object( thread );
1817 set_error( STATUS_ACCESS_DENIED );
1820 queue = thread->queue;
1821 /* remove it if it existed already */
1822 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1826 queue = get_current_queue();
1827 /* find a free id for it */
1830 id = queue->next_timer_id;
1831 if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1833 while (find_timer( queue, 0, req->msg, id ));
1836 if ((timer = set_timer( queue, req->rate )))
1839 timer->msg = req->msg;
1841 timer->lparam = req->lparam;
1844 if (thread) release_object( thread );
1847 /* kill a window timer */
1848 DECL_HANDLER(kill_win_timer)
1850 struct timer *timer;
1851 struct thread *thread;
1852 user_handle_t win = 0;
1856 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1858 set_error( STATUS_INVALID_HANDLE );
1861 if (thread->process != current->process)
1863 release_object( thread );
1864 set_error( STATUS_ACCESS_DENIED );
1868 else thread = (struct thread *)grab_object( current );
1870 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1871 free_timer( thread->queue, timer );
1873 set_error( STATUS_INVALID_PARAMETER );
1875 release_object( thread );
1879 /* attach (or detach) thread inputs */
1880 DECL_HANDLER(attach_thread_input)
1882 struct thread *thread_from = get_thread_from_id( req->tid_from );
1883 struct thread *thread_to = get_thread_from_id( req->tid_to );
1885 if (!thread_from || !thread_to)
1887 if (thread_from) release_object( thread_from );
1888 if (thread_to) release_object( thread_to );
1891 if (thread_from != thread_to)
1893 if (req->attach) attach_thread_input( thread_from, thread_to );
1896 if (thread_from->queue && thread_to->queue &&
1897 thread_from->queue->input == thread_to->queue->input)
1898 detach_thread_input( thread_from );
1900 set_error( STATUS_ACCESS_DENIED );
1903 else set_error( STATUS_ACCESS_DENIED );
1904 release_object( thread_from );
1905 release_object( thread_to );
1909 /* get thread input data */
1910 DECL_HANDLER(get_thread_input)
1912 struct thread *thread = NULL;
1913 struct thread_input *input;
1917 if (!(thread = get_thread_from_id( req->tid ))) return;
1918 input = thread->queue ? thread->queue->input : NULL;
1920 else input = foreground_input; /* get the foreground thread info */
1924 reply->focus = input->focus;
1925 reply->capture = input->capture;
1926 reply->active = input->active;
1927 reply->menu_owner = input->menu_owner;
1928 reply->move_size = input->move_size;
1929 reply->caret = input->caret;
1930 reply->rect = input->caret_rect;
1937 reply->menu_owner = 0;
1938 reply->move_size = 0;
1940 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1942 /* foreground window is active window of foreground thread */
1943 reply->foreground = foreground_input ? foreground_input->active : 0;
1944 if (thread) release_object( thread );
1948 /* retrieve queue keyboard state for a given thread */
1949 DECL_HANDLER(get_key_state)
1951 struct thread *thread;
1952 struct thread_input *input;
1954 if (!(thread = get_thread_from_id( req->tid ))) return;
1955 input = thread->queue ? thread->queue->input : NULL;
1958 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1959 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1961 release_object( thread );
1965 /* set queue keyboard state for a given thread */
1966 DECL_HANDLER(set_key_state)
1968 struct thread *thread = NULL;
1969 struct thread_input *input;
1971 if (!(thread = get_thread_from_id( req->tid ))) return;
1972 input = thread->queue ? thread->queue->input : NULL;
1975 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1976 if (size) memcpy( input->keystate, get_req_data(), size );
1978 release_object( thread );
1982 /* set the system foreground window */
1983 DECL_HANDLER(set_foreground_window)
1985 struct msg_queue *queue = get_current_queue();
1987 reply->previous = foreground_input ? foreground_input->active : 0;
1988 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1989 reply->send_msg_new = FALSE;
1993 struct thread *thread;
1995 if (is_top_level_window( req->handle ) &&
1996 ((thread = get_window_thread( req->handle ))))
1998 foreground_input = thread->queue->input;
1999 reply->send_msg_new = (foreground_input != queue->input);
2000 release_object( thread );
2002 else set_error( STATUS_INVALID_HANDLE );
2004 else foreground_input = NULL;
2008 /* set the current thread focus window */
2009 DECL_HANDLER(set_focus_window)
2011 struct msg_queue *queue = get_current_queue();
2013 reply->previous = 0;
2014 if (queue && check_queue_input_window( queue, req->handle ))
2016 reply->previous = queue->input->focus;
2017 queue->input->focus = get_user_full_handle( req->handle );
2022 /* set the current thread active window */
2023 DECL_HANDLER(set_active_window)
2025 struct msg_queue *queue = get_current_queue();
2027 reply->previous = 0;
2028 if (queue && check_queue_input_window( queue, req->handle ))
2030 if (!req->handle || make_window_active( req->handle ))
2032 reply->previous = queue->input->active;
2033 queue->input->active = get_user_full_handle( req->handle );
2035 else set_error( STATUS_INVALID_HANDLE );
2040 /* set the current thread capture window */
2041 DECL_HANDLER(set_capture_window)
2043 struct msg_queue *queue = get_current_queue();
2045 reply->previous = reply->full_handle = 0;
2046 if (queue && check_queue_input_window( queue, req->handle ))
2048 struct thread_input *input = queue->input;
2050 reply->previous = input->capture;
2051 input->capture = get_user_full_handle( req->handle );
2052 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2053 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2054 reply->full_handle = input->capture;
2059 /* Set the current thread caret window */
2060 DECL_HANDLER(set_caret_window)
2062 struct msg_queue *queue = get_current_queue();
2064 reply->previous = 0;
2065 if (queue && check_queue_input_window( queue, req->handle ))
2067 struct thread_input *input = queue->input;
2069 reply->previous = input->caret;
2070 reply->old_rect = input->caret_rect;
2071 reply->old_hide = input->caret_hide;
2072 reply->old_state = input->caret_state;
2074 set_caret_window( input, get_user_full_handle(req->handle) );
2075 input->caret_rect.right = input->caret_rect.left + req->width;
2076 input->caret_rect.bottom = input->caret_rect.top + req->height;
2081 /* Set the current thread caret information */
2082 DECL_HANDLER(set_caret_info)
2084 struct msg_queue *queue = get_current_queue();
2085 struct thread_input *input;
2088 input = queue->input;
2089 reply->full_handle = input->caret;
2090 reply->old_rect = input->caret_rect;
2091 reply->old_hide = input->caret_hide;
2092 reply->old_state = input->caret_state;
2094 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2096 set_error( STATUS_ACCESS_DENIED );
2099 if (req->flags & SET_CARET_POS)
2101 input->caret_rect.right += req->x - input->caret_rect.left;
2102 input->caret_rect.bottom += req->y - input->caret_rect.top;
2103 input->caret_rect.left = req->x;
2104 input->caret_rect.top = req->y;
2106 if (req->flags & SET_CARET_HIDE)
2108 input->caret_hide += req->hide;
2109 if (input->caret_hide < 0) input->caret_hide = 0;
2111 if (req->flags & SET_CARET_STATE)
2113 if (req->state == -1) input->caret_state = !input->caret_state;
2114 else input->caret_state = !!req->state;
2119 /* get the time of the last input event */
2120 DECL_HANDLER(get_last_input_time)
2122 reply->time = last_input_time;