Beginning of a shell namespace extension to browse the unix
[wine/wine64.git] / server / queue.c
blobda2fe87737aa1e2dbda6ff5bbe290e851121cc5e
1 /*
2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
34 #include "handle.h"
35 #include "file.h"
36 #include "thread.h"
37 #include "process.h"
38 #include "request.h"
39 #include "user.h"
41 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
42 #define NB_MSG_KINDS (POST_MESSAGE+1)
45 struct message_result
47 struct list sender_entry; /* entry in sender list */
48 struct message_result *recv_next; /* next in receiver list */
49 struct msg_queue *sender; /* sender queue */
50 struct msg_queue *receiver; /* receiver queue */
51 int replied; /* has it been replied to? */
52 unsigned int result; /* reply result */
53 unsigned int error; /* error code to pass back to sender */
54 struct message *callback_msg; /* message to queue for callback */
55 void *data; /* message reply data */
56 unsigned int data_size; /* size of message reply data */
57 struct timeout_user *timeout; /* result timeout */
60 struct message
62 struct list entry; /* entry in message list */
63 enum message_type type; /* message type */
64 user_handle_t win; /* window handle */
65 unsigned int msg; /* message code */
66 unsigned int wparam; /* parameters */
67 unsigned int lparam; /* parameters */
68 int x; /* x position */
69 int y; /* y position */
70 unsigned int time; /* message time */
71 unsigned int info; /* extra info */
72 user_handle_t hook; /* winevent hook handle */
73 void *hook_proc; /* winevent hook proc address */
74 void *data; /* message data for sent messages */
75 unsigned int data_size; /* size of message data */
76 struct message_result *result; /* result in sender queue */
79 struct timer
81 struct list entry; /* entry in timer list */
82 struct timeval when; /* next expiration */
83 unsigned int rate; /* timer rate in ms */
84 user_handle_t win; /* window handle */
85 unsigned int msg; /* message to post */
86 unsigned int id; /* timer id */
87 unsigned int lparam; /* lparam for message */
90 struct thread_input
92 struct object obj; /* object header */
93 user_handle_t focus; /* focus window */
94 user_handle_t capture; /* capture window */
95 user_handle_t active; /* active window */
96 user_handle_t menu_owner; /* current menu owner window */
97 user_handle_t move_size; /* current moving/resizing window */
98 user_handle_t caret; /* caret window */
99 rectangle_t caret_rect; /* caret rectangle */
100 int caret_hide; /* caret hide count */
101 int caret_state; /* caret on/off state */
102 struct message *msg; /* message currently processed */
103 struct thread *msg_thread; /* thread processing the message */
104 struct list msg_list; /* list of hardware messages */
105 unsigned char keystate[256]; /* state of each key */
108 struct msg_queue
110 struct object obj; /* object header */
111 unsigned int wake_bits; /* wakeup bits */
112 unsigned int wake_mask; /* wakeup mask */
113 unsigned int changed_bits; /* changed wakeup bits */
114 unsigned int changed_mask; /* changed wakeup mask */
115 int paint_count; /* pending paint messages count */
116 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
117 struct list send_result; /* stack of sent messages waiting for result */
118 struct list callback_result; /* list of callback messages waiting for result */
119 struct message_result *recv_result; /* stack of received messages waiting for result */
120 struct list pending_timers; /* list of pending timers */
121 struct list expired_timers; /* list of expired timers */
122 unsigned int next_timer_id; /* id for the next timer with a 0 window */
123 struct timeout_user *timeout; /* timeout for next timer to expire */
124 struct thread_input *input; /* thread input descriptor */
125 struct hook_table *hooks; /* hook table */
126 struct timeval last_get_msg; /* time of last get message call */
129 static void msg_queue_dump( struct object *obj, int verbose );
130 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
131 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
132 static int msg_queue_signaled( struct object *obj, struct thread *thread );
133 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
134 static void msg_queue_destroy( struct object *obj );
135 static void thread_input_dump( struct object *obj, int verbose );
136 static void thread_input_destroy( struct object *obj );
137 static void timer_callback( void *private );
139 static const struct object_ops msg_queue_ops =
141 sizeof(struct msg_queue), /* size */
142 msg_queue_dump, /* dump */
143 msg_queue_add_queue, /* add_queue */
144 msg_queue_remove_queue, /* remove_queue */
145 msg_queue_signaled, /* signaled */
146 msg_queue_satisfied, /* satisfied */
147 no_get_fd, /* get_fd */
148 msg_queue_destroy /* destroy */
152 static const struct object_ops thread_input_ops =
154 sizeof(struct thread_input), /* size */
155 thread_input_dump, /* dump */
156 no_add_queue, /* add_queue */
157 NULL, /* remove_queue */
158 NULL, /* signaled */
159 NULL, /* satisfied */
160 no_get_fd, /* get_fd */
161 thread_input_destroy /* destroy */
164 /* pointer to input structure of foreground thread */
165 static struct thread_input *foreground_input;
168 /* set the caret window in a given thread input */
169 static void set_caret_window( struct thread_input *input, user_handle_t win )
171 if (!win || win != input->caret)
173 input->caret_rect.left = 0;
174 input->caret_rect.top = 0;
175 input->caret_rect.right = 0;
176 input->caret_rect.bottom = 0;
178 input->caret = win;
179 input->caret_hide = 1;
180 input->caret_state = 0;
183 /* create a thread input object */
184 static struct thread_input *create_thread_input(void)
186 struct thread_input *input;
188 if ((input = alloc_object( &thread_input_ops )))
190 input->focus = 0;
191 input->capture = 0;
192 input->active = 0;
193 input->menu_owner = 0;
194 input->move_size = 0;
195 input->msg = NULL;
196 input->msg_thread = NULL;
197 list_init( &input->msg_list );
198 set_caret_window( input, 0 );
199 memset( input->keystate, 0, sizeof(input->keystate) );
201 return input;
204 /* release the thread input data of a given thread */
205 static void release_thread_input( struct thread *thread )
207 struct thread_input *input = thread->queue->input;
209 if (!input) return;
210 if (input->msg_thread == thread)
212 release_object( input->msg_thread );
213 input->msg_thread = NULL;
214 input->msg = NULL;
216 release_object( input );
217 thread->queue->input = NULL;
220 /* create a message queue object */
221 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
223 struct msg_queue *queue;
224 int i;
226 if (!input && !(input = create_thread_input())) return NULL;
227 if ((queue = alloc_object( &msg_queue_ops )))
229 queue->wake_bits = 0;
230 queue->wake_mask = 0;
231 queue->changed_bits = 0;
232 queue->changed_mask = 0;
233 queue->paint_count = 0;
234 queue->recv_result = NULL;
235 queue->next_timer_id = 1;
236 queue->timeout = NULL;
237 queue->input = (struct thread_input *)grab_object( input );
238 queue->hooks = NULL;
239 gettimeofday( &queue->last_get_msg, NULL );
240 list_init( &queue->send_result );
241 list_init( &queue->callback_result );
242 list_init( &queue->pending_timers );
243 list_init( &queue->expired_timers );
244 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
246 thread->queue = queue;
247 if (!thread->process->queue)
248 thread->process->queue = (struct msg_queue *)grab_object( queue );
250 release_object( input );
251 return queue;
254 /* free the message queue of a thread at thread exit */
255 void free_msg_queue( struct thread *thread )
257 struct process *process = thread->process;
258 struct thread_input *input;
260 remove_thread_hooks( thread );
261 if (!thread->queue) return;
262 if (process->queue == thread->queue) /* is it the process main queue? */
264 release_object( process->queue );
265 process->queue = NULL;
266 if (process->idle_event)
268 set_event( process->idle_event );
269 release_object( process->idle_event );
270 process->idle_event = NULL;
273 input = thread->queue->input;
274 if (input->msg_thread == thread)
276 release_object( input->msg_thread );
277 input->msg_thread = NULL;
278 input->msg = 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 );
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 /* get the QS_* bit corresponding to a given hardware message */
328 inline static int get_hardware_msg_bit( struct message *msg )
330 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
331 if (is_keyboard_msg( msg )) return QS_KEY;
332 return QS_MOUSEBUTTON;
335 /* get the current thread queue, creating it if needed */
336 inline static struct msg_queue *get_current_queue(void)
338 struct msg_queue *queue = current->queue;
339 if (!queue) queue = create_msg_queue( current, NULL );
340 return queue;
343 /* try to merge a message with the last in the list; return 1 if successful */
344 static int merge_message( struct thread_input *input, const struct message *msg )
346 struct message *prev;
347 struct list *ptr = list_tail( &input->msg_list );
349 if (!ptr) return 0;
350 prev = LIST_ENTRY( ptr, struct message, entry );
351 if (input->msg == prev) return 0;
352 if (prev->result) return 0;
353 if (prev->win != msg->win) return 0;
354 if (prev->msg != msg->msg) return 0;
355 if (prev->type != msg->type) return 0;
356 /* now we can merge it */
357 prev->wparam = msg->wparam;
358 prev->lparam = msg->lparam;
359 prev->x = msg->x;
360 prev->y = msg->y;
361 prev->time = msg->time;
362 prev->info = msg->info;
363 return 1;
366 /* free a result structure */
367 static void free_result( struct message_result *result )
369 if (result->timeout) remove_timeout_user( result->timeout );
370 if (result->data) free( result->data );
371 if (result->callback_msg) free( result->callback_msg );
372 free( result );
375 /* remove the result from the sender list it is on */
376 static inline void remove_result_from_sender( struct message_result *result )
378 assert( result->sender );
380 list_remove( &result->sender_entry );
381 result->sender = NULL;
382 if (!result->receiver) free_result( result );
385 /* store the message result in the appropriate structure */
386 static void store_message_result( struct message_result *res, unsigned int result,
387 unsigned int error )
389 res->result = result;
390 res->error = error;
391 res->replied = 1;
392 if (res->timeout)
394 remove_timeout_user( res->timeout );
395 res->timeout = NULL;
397 if (res->sender)
399 if (res->callback_msg)
401 /* queue the callback message in the sender queue */
402 res->callback_msg->lparam = result;
403 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
404 set_queue_bits( res->sender, QS_SENDMESSAGE );
405 res->callback_msg = NULL;
406 remove_result_from_sender( res );
408 else
410 /* wake sender queue if waiting on this result */
411 if (list_head(&res->sender->send_result) == &res->sender_entry)
412 set_queue_bits( res->sender, QS_SMRESULT );
418 /* free a message when deleting a queue or window */
419 static void free_message( struct message *msg )
421 struct message_result *result = msg->result;
422 if (result)
424 if (result->sender)
426 result->receiver = NULL;
427 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
429 else free_result( result );
431 if (msg->data) free( msg->data );
432 free( msg );
435 /* remove (and free) a message from a message list */
436 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
437 enum message_kind kind )
439 list_remove( &msg->entry );
440 switch(kind)
442 case SEND_MESSAGE:
443 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
444 break;
445 case POST_MESSAGE:
446 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_POSTMESSAGE );
447 break;
449 free_message( msg );
452 /* message timed out without getting a reply */
453 static void result_timeout( void *private )
455 struct message_result *result = private;
457 assert( !result->replied );
459 result->timeout = NULL;
460 store_message_result( result, 0, STATUS_TIMEOUT );
463 /* allocate and fill a message result structure */
464 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
465 struct msg_queue *recv_queue,
466 struct message *msg, unsigned int timeout,
467 void *callback, unsigned int callback_data )
469 struct message_result *result = mem_alloc( sizeof(*result) );
470 if (result)
472 result->sender = send_queue;
473 result->receiver = recv_queue;
474 result->replied = 0;
475 result->data = NULL;
476 result->data_size = 0;
477 result->timeout = NULL;
479 if (msg->type == MSG_CALLBACK)
481 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
482 if (!callback_msg)
484 free( result );
485 return NULL;
487 callback_msg->type = MSG_CALLBACK_RESULT;
488 callback_msg->win = msg->win;
489 callback_msg->msg = msg->msg;
490 callback_msg->wparam = (unsigned int)callback;
491 callback_msg->lparam = 0;
492 callback_msg->time = get_tick_count();
493 callback_msg->x = 0;
494 callback_msg->y = 0;
495 callback_msg->info = callback_data;
496 callback_msg->hook = 0;
497 callback_msg->hook_proc = NULL;
498 callback_msg->result = NULL;
499 callback_msg->data = NULL;
500 callback_msg->data_size = 0;
502 result->callback_msg = callback_msg;
503 list_add_head( &send_queue->callback_result, &result->sender_entry );
505 else
507 result->callback_msg = NULL;
508 list_add_head( &send_queue->send_result, &result->sender_entry );
511 if (timeout != -1)
513 struct timeval when;
514 gettimeofday( &when, 0 );
515 add_timeout( &when, timeout );
516 result->timeout = add_timeout_user( &when, result_timeout, result );
519 return result;
522 /* receive a message, removing it from the sent queue */
523 static void receive_message( struct msg_queue *queue, struct message *msg,
524 struct get_message_reply *reply )
526 struct message_result *result = msg->result;
528 reply->total = msg->data_size;
529 if (msg->data_size > get_reply_max_size())
531 set_error( STATUS_BUFFER_OVERFLOW );
532 return;
534 reply->type = msg->type;
535 reply->win = msg->win;
536 reply->msg = msg->msg;
537 reply->wparam = msg->wparam;
538 reply->lparam = msg->lparam;
539 reply->x = msg->x;
540 reply->y = msg->y;
541 reply->time = msg->time;
542 reply->info = msg->info;
543 reply->hook = msg->hook;
544 reply->hook_proc = msg->hook_proc;
546 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
548 list_remove( &msg->entry );
549 /* put the result on the receiver result stack */
550 if (result)
552 result->recv_next = queue->recv_result;
553 queue->recv_result = result;
555 free( msg );
556 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
559 /* set the result of the current received message */
560 static void reply_message( struct msg_queue *queue, unsigned int result,
561 unsigned int error, int remove, const void *data, size_t len )
563 struct message_result *res = queue->recv_result;
565 if (remove)
567 queue->recv_result = res->recv_next;
568 res->receiver = NULL;
569 if (!res->sender) /* no one waiting for it */
571 free_result( res );
572 return;
575 if (!res->replied)
577 if (len && (res->data = memdup( data, len ))) res->data_size = len;
578 store_message_result( res, result, error );
582 /* retrieve a posted message */
583 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
584 unsigned int first, unsigned int last, unsigned int flags,
585 struct get_message_reply *reply )
587 struct message *msg;
589 /* check against the filters */
590 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
592 if (msg->msg == WM_QUIT) goto found; /* WM_QUIT is never filtered */
593 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
594 if (msg->msg < first) continue;
595 if (msg->msg > last) continue;
596 goto found; /* found one */
598 return 0;
600 /* return it to the app */
601 found:
602 reply->total = msg->data_size;
603 if (msg->data_size > get_reply_max_size())
605 set_error( STATUS_BUFFER_OVERFLOW );
606 return 1;
608 reply->type = msg->type;
609 reply->win = msg->win;
610 reply->msg = msg->msg;
611 reply->wparam = msg->wparam;
612 reply->lparam = msg->lparam;
613 reply->x = msg->x;
614 reply->y = msg->y;
615 reply->time = msg->time;
616 reply->info = msg->info;
618 if (flags & GET_MSG_REMOVE)
620 if (msg->data)
622 set_reply_data_ptr( msg->data, msg->data_size );
623 msg->data = NULL;
624 msg->data_size = 0;
626 remove_queue_message( queue, msg, POST_MESSAGE );
628 else if (msg->data) set_reply_data( msg->data, msg->data_size );
630 return 1;
633 /* empty a message list and free all the messages */
634 static void empty_msg_list( struct list *list )
636 struct list *ptr;
638 while ((ptr = list_head( list )) != NULL)
640 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
641 list_remove( &msg->entry );
642 free_message( msg );
646 /* cleanup all pending results when deleting a queue */
647 static void cleanup_results( struct msg_queue *queue )
649 struct list *entry;
651 while ((entry = list_head( &queue->send_result )) != NULL)
653 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
656 while ((entry = list_head( &queue->callback_result )) != NULL)
658 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
661 while (queue->recv_result)
662 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
665 /* check if the thread owning the queue is hung (not checking for messages) */
666 static int is_queue_hung( struct msg_queue *queue )
668 struct timeval now;
669 struct wait_queue_entry *entry;
671 gettimeofday( &now, NULL );
672 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
673 return 0; /* less than 5 seconds since last get message -> not hung */
675 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
677 if (entry->thread->queue == queue)
678 return 0; /* thread is waiting on queue -> not hung */
680 return 1;
683 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
685 struct msg_queue *queue = (struct msg_queue *)obj;
686 struct process *process = entry->thread->process;
688 /* a thread can only wait on its own queue */
689 if (entry->thread->queue != queue)
691 set_error( STATUS_ACCESS_DENIED );
692 return 0;
694 /* if waiting on the main process queue, set the idle event */
695 if (process->queue == queue)
697 if (process->idle_event) set_event( process->idle_event );
699 add_queue( obj, entry );
700 return 1;
703 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
705 struct msg_queue *queue = (struct msg_queue *)obj;
706 struct process *process = entry->thread->process;
708 remove_queue( obj, entry );
710 assert( entry->thread->queue == queue );
712 /* if waiting on the main process queue, reset the idle event */
713 if (process->queue == queue)
715 if (process->idle_event) reset_event( process->idle_event );
719 static void msg_queue_dump( struct object *obj, int verbose )
721 struct msg_queue *queue = (struct msg_queue *)obj;
722 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
723 queue->wake_bits, queue->wake_mask );
726 static int msg_queue_signaled( struct object *obj, struct thread *thread )
728 struct msg_queue *queue = (struct msg_queue *)obj;
729 return is_signaled( queue );
732 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
734 struct msg_queue *queue = (struct msg_queue *)obj;
735 queue->wake_mask = 0;
736 queue->changed_mask = 0;
737 return 0; /* Not abandoned */
740 static void msg_queue_destroy( struct object *obj )
742 struct msg_queue *queue = (struct msg_queue *)obj;
743 struct list *ptr;
744 int i;
746 cleanup_results( queue );
747 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
749 while ((ptr = list_head( &queue->pending_timers )))
751 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
752 list_remove( &timer->entry );
753 free( timer );
755 while ((ptr = list_head( &queue->expired_timers )))
757 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
758 list_remove( &timer->entry );
759 free( timer );
761 if (queue->timeout) remove_timeout_user( queue->timeout );
762 if (queue->input) release_object( queue->input );
763 if (queue->hooks) release_object( queue->hooks );
766 static void thread_input_dump( struct object *obj, int verbose )
768 struct thread_input *input = (struct thread_input *)obj;
769 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
770 input->focus, input->capture, input->active );
773 static void thread_input_destroy( struct object *obj )
775 struct thread_input *input = (struct thread_input *)obj;
777 if (foreground_input == input) foreground_input = NULL;
778 if (input->msg_thread) release_object( input->msg_thread );
779 empty_msg_list( &input->msg_list );
782 /* fix the thread input data when a window is destroyed */
783 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
785 struct thread_input *input = queue->input;
787 if (window == input->focus) input->focus = 0;
788 if (window == input->capture) input->capture = 0;
789 if (window == input->active) input->active = 0;
790 if (window == input->menu_owner) input->menu_owner = 0;
791 if (window == input->move_size) input->move_size = 0;
792 if (window == input->caret) set_caret_window( input, 0 );
795 /* check if the specified window can be set in the input data of a given queue */
796 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
798 struct thread *thread;
799 int ret = 0;
801 if (!window) return 1; /* we can always clear the data */
803 if ((thread = get_window_thread( window )))
805 ret = (queue->input == thread->queue->input);
806 if (!ret) set_error( STATUS_ACCESS_DENIED );
807 release_object( thread );
809 else set_error( STATUS_INVALID_HANDLE );
811 return ret;
814 /* attach two thread input data structures */
815 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
817 struct thread_input *input;
819 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
820 input = (struct thread_input *)grab_object( thread_to->queue->input );
822 if (thread_from->queue)
824 release_thread_input( thread_from );
825 thread_from->queue->input = input;
827 else
829 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
831 memset( input->keystate, 0, sizeof(input->keystate) );
832 return 1;
835 /* detach two thread input data structures */
836 static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
838 struct thread_input *input;
840 if (!thread_from->queue || !thread_to->queue ||
841 thread_from->queue->input != thread_to->queue->input)
843 set_error( STATUS_ACCESS_DENIED );
844 return;
846 if ((input = create_thread_input()))
848 release_thread_input( thread_from );
849 thread_from->queue->input = input;
854 /* set the next timer to expire */
855 static void set_next_timer( struct msg_queue *queue )
857 struct list *ptr;
859 if (queue->timeout)
861 remove_timeout_user( queue->timeout );
862 queue->timeout = NULL;
864 if ((ptr = list_head( &queue->pending_timers )))
866 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
867 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
869 /* set/clear QS_TIMER bit */
870 if (list_empty( &queue->expired_timers ))
871 clear_queue_bits( queue, QS_TIMER );
872 else
873 set_queue_bits( queue, QS_TIMER );
876 /* find a timer from its window and id */
877 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
878 unsigned int msg, unsigned int id )
880 struct list *ptr;
882 /* we need to search both lists */
884 LIST_FOR_EACH( ptr, &queue->pending_timers )
886 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
887 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
889 LIST_FOR_EACH( ptr, &queue->expired_timers )
891 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
892 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
894 return NULL;
897 /* callback for the next timer expiration */
898 static void timer_callback( void *private )
900 struct msg_queue *queue = private;
901 struct list *ptr;
903 queue->timeout = NULL;
904 /* move on to the next timer */
905 ptr = list_head( &queue->pending_timers );
906 list_remove( ptr );
907 list_add_tail( &queue->expired_timers, ptr );
908 set_next_timer( queue );
911 /* link a timer at its rightful place in the queue list */
912 static void link_timer( struct msg_queue *queue, struct timer *timer )
914 struct list *ptr;
916 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
918 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
919 if (!time_before( &t->when, &timer->when )) break;
921 list_add_before( ptr, &timer->entry );
924 /* remove a timer from the queue timer list and free it */
925 static void free_timer( struct msg_queue *queue, struct timer *timer )
927 list_remove( &timer->entry );
928 free( timer );
929 set_next_timer( queue );
932 /* restart an expired timer */
933 static void restart_timer( struct msg_queue *queue, struct timer *timer )
935 struct timeval now;
937 list_remove( &timer->entry );
938 gettimeofday( &now, 0 );
939 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
940 link_timer( queue, timer );
941 set_next_timer( queue );
944 /* find an expired timer matching the filtering parameters */
945 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
946 unsigned int get_first, unsigned int get_last,
947 int remove )
949 struct list *ptr;
951 LIST_FOR_EACH( ptr, &queue->expired_timers )
953 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
954 if (win && timer->win != win) continue;
955 if (timer->msg >= get_first && timer->msg <= get_last)
957 if (remove) restart_timer( queue, timer );
958 return timer;
961 return NULL;
964 /* add a timer */
965 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
967 struct timer *timer = mem_alloc( sizeof(*timer) );
968 if (timer)
970 timer->rate = max( rate, 1 );
971 gettimeofday( &timer->when, 0 );
972 add_timeout( &timer->when, rate );
973 link_timer( queue, timer );
974 /* check if we replaced the next timer */
975 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
977 return timer;
980 /* change the input key state for a given key */
981 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
983 if (down)
985 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
986 input->keystate[key] |= 0x80;
988 else input->keystate[key] &= ~0x80;
991 /* update the input key state for a keyboard message */
992 static void update_input_key_state( struct thread_input *input, const struct message *msg )
994 unsigned char key;
995 int down = 0, extended;
997 switch (msg->msg)
999 case WM_LBUTTONDOWN:
1000 down = 1;
1001 /* fall through */
1002 case WM_LBUTTONUP:
1003 set_input_key_state( input, VK_LBUTTON, down );
1004 break;
1005 case WM_MBUTTONDOWN:
1006 down = 1;
1007 /* fall through */
1008 case WM_MBUTTONUP:
1009 set_input_key_state( input, VK_MBUTTON, down );
1010 break;
1011 case WM_RBUTTONDOWN:
1012 down = 1;
1013 /* fall through */
1014 case WM_RBUTTONUP:
1015 set_input_key_state( input, VK_RBUTTON, down );
1016 break;
1017 case WM_KEYDOWN:
1018 case WM_SYSKEYDOWN:
1019 down = 1;
1020 /* fall through */
1021 case WM_KEYUP:
1022 case WM_SYSKEYUP:
1023 key = (unsigned char)msg->wparam;
1024 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1025 set_input_key_state( input, key, down );
1026 switch(key)
1028 case VK_SHIFT:
1029 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1030 break;
1031 case VK_CONTROL:
1032 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1033 break;
1034 case VK_MENU:
1035 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1036 break;
1038 break;
1042 /* release the hardware message currently being processed by the given thread */
1043 static void release_hardware_message( struct thread *thread, int remove )
1045 struct thread_input *input = thread->queue->input;
1047 if (input->msg_thread != thread) return;
1048 if (remove)
1050 struct message *other;
1051 int clr_bit;
1053 update_input_key_state( input, input->msg );
1054 list_remove( &input->msg->entry );
1055 clr_bit = get_hardware_msg_bit( input->msg );
1056 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1058 if (get_hardware_msg_bit( other ) == clr_bit)
1060 clr_bit = 0;
1061 break;
1064 if (clr_bit) clear_queue_bits( thread->queue, clr_bit );
1065 free_message( input->msg );
1067 release_object( input->msg_thread );
1068 input->msg = NULL;
1069 input->msg_thread = NULL;
1072 /* find the window that should receive a given hardware message */
1073 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1074 unsigned int *msg_code )
1076 user_handle_t win = 0;
1078 *msg_code = msg->msg;
1079 if (is_keyboard_msg( msg ))
1081 if (input && !(win = input->focus))
1083 win = input->active;
1084 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1087 else /* mouse message */
1089 if (!input || !(win = input->capture))
1091 if (!(win = msg->win) || !is_window_visible( win ))
1092 win = window_from_point( msg->x, msg->y );
1095 return win;
1098 /* queue a hardware message into a given thread input */
1099 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1101 user_handle_t win;
1102 struct thread *thread;
1103 struct thread_input *input;
1104 unsigned int msg_code;
1106 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1107 if (!win || !(thread = get_window_thread(win)))
1109 free( msg );
1110 return;
1112 input = thread->queue->input;
1114 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1115 else
1117 list_add_tail( &input->msg_list, &msg->entry );
1118 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1120 release_object( thread );
1123 /* find a hardware message for the given queue */
1124 static int get_hardware_message( struct thread *thread, struct message *first,
1125 user_handle_t filter_win, struct get_message_reply *reply )
1127 struct thread_input *input = thread->queue->input;
1128 struct thread *win_thread;
1129 struct list *ptr;
1130 user_handle_t win;
1131 int clear_bits, got_one = 0;
1132 unsigned int msg_code;
1134 if (input->msg_thread && input->msg_thread != thread)
1135 return 0; /* locked by another thread */
1137 if (!first)
1139 ptr = list_head( &input->msg_list );
1140 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1142 else
1144 ptr = list_next( &input->msg_list, &first->entry );
1145 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1148 while (ptr)
1150 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1151 win = find_hardware_message_window( input, msg, &msg_code );
1152 if (!win || !(win_thread = get_window_thread( win )))
1154 /* no window at all, remove it */
1155 ptr = list_next( &input->msg_list, ptr );
1156 update_input_key_state( input, msg );
1157 list_remove( &msg->entry );
1158 free_message( msg );
1159 continue;
1161 if (win_thread != thread)
1163 /* wake the other thread */
1164 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1165 release_object( win_thread );
1166 got_one = 1;
1167 ptr = list_next( &input->msg_list, ptr );
1168 continue;
1170 /* if we already got a message for another thread, or if it doesn't
1171 * match the filter we skip it (filter is only checked for keyboard
1172 * messages since the dest window for a mouse message depends on hittest)
1174 if (got_one ||
1175 (filter_win && is_keyboard_msg(msg) &&
1176 win != filter_win && !is_child_window( filter_win, win )))
1178 clear_bits &= ~get_hardware_msg_bit( msg );
1179 release_object( win_thread );
1180 ptr = list_next( &input->msg_list, ptr );
1181 continue;
1183 /* now we can return it */
1184 if (!input->msg_thread) input->msg_thread = win_thread;
1185 else release_object( win_thread );
1186 input->msg = msg;
1188 reply->type = MSG_HARDWARE;
1189 reply->win = win;
1190 reply->msg = msg_code;
1191 reply->wparam = msg->wparam;
1192 reply->lparam = msg->lparam;
1193 reply->x = msg->x;
1194 reply->y = msg->y;
1195 reply->time = msg->time;
1196 reply->info = msg->info;
1197 return 1;
1199 /* nothing found, clear the hardware queue bits */
1200 clear_queue_bits( thread->queue, clear_bits );
1201 if (input->msg_thread) release_object( input->msg_thread );
1202 input->msg = NULL;
1203 input->msg_thread = NULL;
1204 return 0;
1207 /* increment (or decrement if 'incr' is negative) the queue paint count */
1208 void inc_queue_paint_count( struct thread *thread, int incr )
1210 struct msg_queue *queue = thread->queue;
1212 assert( queue );
1214 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1216 if (queue->paint_count)
1217 set_queue_bits( queue, QS_PAINT );
1218 else
1219 clear_queue_bits( queue, QS_PAINT );
1223 /* remove all messages and timers belonging to a certain window */
1224 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1226 struct msg_queue *queue = thread->queue;
1227 struct list *ptr;
1228 int i;
1230 if (!queue) return;
1232 /* remove timers */
1234 ptr = list_head( &queue->pending_timers );
1235 while (ptr)
1237 struct list *next = list_next( &queue->pending_timers, ptr );
1238 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1239 if (timer->win == win) free_timer( queue, timer );
1240 ptr = next;
1242 ptr = list_head( &queue->expired_timers );
1243 while (ptr)
1245 struct list *next = list_next( &queue->expired_timers, ptr );
1246 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1247 if (timer->win == win) free_timer( queue, timer );
1248 ptr = next;
1251 /* remove messages */
1252 for (i = 0; i < NB_MSG_KINDS; i++)
1254 struct list *ptr, *next;
1256 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1258 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1259 if (msg->win == win) remove_queue_message( queue, msg, i );
1263 thread_input_cleanup_window( queue, win );
1266 /* post a message to a window; used by socket handling */
1267 void post_message( user_handle_t win, unsigned int message,
1268 unsigned int wparam, unsigned int lparam )
1270 struct message *msg;
1271 struct thread *thread = get_window_thread( win );
1273 if (!thread) return;
1275 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1277 msg->type = MSG_POSTED;
1278 msg->win = get_user_full_handle( win );
1279 msg->msg = message;
1280 msg->wparam = wparam;
1281 msg->lparam = lparam;
1282 msg->time = get_tick_count();
1283 msg->x = 0;
1284 msg->y = 0;
1285 msg->info = 0;
1286 msg->hook = 0;
1287 msg->hook_proc = NULL;
1288 msg->result = NULL;
1289 msg->data = NULL;
1290 msg->data_size = 0;
1292 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1293 set_queue_bits( thread->queue, QS_POSTMESSAGE );
1295 release_object( thread );
1298 /* post a win event */
1299 void post_win_event( struct thread *thread, unsigned int event,
1300 user_handle_t win, unsigned int object_id,
1301 unsigned int child_id, void *hook_proc,
1302 const WCHAR *module, size_t module_size,
1303 user_handle_t hook)
1305 struct message *msg;
1307 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1309 msg->type = MSG_WINEVENT;
1310 msg->win = get_user_full_handle( win );
1311 msg->msg = event;
1312 msg->wparam = object_id;
1313 msg->lparam = child_id;
1314 msg->time = get_tick_count();
1315 msg->x = 0;
1316 msg->y = 0;
1317 msg->info = get_thread_id( current );
1318 msg->result = NULL;
1319 msg->hook = hook;
1320 msg->hook_proc = hook_proc;
1322 if ((msg->data = malloc( module_size )))
1324 msg->data_size = module_size;
1325 memcpy( msg->data, module, module_size );
1327 if (debug_level > 1)
1328 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1329 get_thread_id(thread), event, win, object_id, child_id );
1330 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1331 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1333 else
1334 free( msg );
1338 /* get the message queue of the current thread */
1339 DECL_HANDLER(get_msg_queue)
1341 struct msg_queue *queue = get_current_queue();
1343 reply->handle = 0;
1344 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1348 /* set the current message queue wakeup mask */
1349 DECL_HANDLER(set_queue_mask)
1351 struct msg_queue *queue = get_current_queue();
1353 if (queue)
1355 queue->wake_mask = req->wake_mask;
1356 queue->changed_mask = req->changed_mask;
1357 reply->wake_bits = queue->wake_bits;
1358 reply->changed_bits = queue->changed_bits;
1359 if (is_signaled( queue ))
1361 /* if skip wait is set, do what would have been done in the subsequent wait */
1362 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1363 else wake_up( &queue->obj, 0 );
1369 /* get the current message queue status */
1370 DECL_HANDLER(get_queue_status)
1372 struct msg_queue *queue = current->queue;
1373 if (queue)
1375 reply->wake_bits = queue->wake_bits;
1376 reply->changed_bits = queue->changed_bits;
1377 if (req->clear) queue->changed_bits = 0;
1379 else reply->wake_bits = reply->changed_bits = 0;
1383 /* send a message to a thread queue */
1384 DECL_HANDLER(send_message)
1386 struct message *msg;
1387 struct msg_queue *send_queue = get_current_queue();
1388 struct msg_queue *recv_queue = NULL;
1389 struct thread *thread = NULL;
1391 if (req->id)
1393 if (!(thread = get_thread_from_id( req->id ))) return;
1395 else if (req->type != MSG_HARDWARE)
1397 /* only hardware messages are allowed without destination thread */
1398 set_error( STATUS_INVALID_PARAMETER );
1399 return;
1402 if (thread && !(recv_queue = thread->queue))
1404 set_error( STATUS_INVALID_PARAMETER );
1405 release_object( thread );
1406 return;
1408 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1410 set_error( STATUS_TIMEOUT );
1411 release_object( thread );
1412 return;
1415 if ((msg = mem_alloc( sizeof(*msg) )))
1417 msg->type = req->type;
1418 msg->win = get_user_full_handle( req->win );
1419 msg->msg = req->msg;
1420 msg->wparam = req->wparam;
1421 msg->lparam = req->lparam;
1422 msg->time = req->time;
1423 msg->x = req->x;
1424 msg->y = req->y;
1425 msg->info = req->info;
1426 msg->hook = 0;
1427 msg->hook_proc = NULL;
1428 msg->result = NULL;
1429 msg->data = NULL;
1430 msg->data_size = 0;
1432 switch(msg->type)
1434 case MSG_OTHER_PROCESS:
1435 msg->data_size = get_req_data_size();
1436 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1438 free( msg );
1439 break;
1441 /* fall through */
1442 case MSG_ASCII:
1443 case MSG_UNICODE:
1444 case MSG_CALLBACK:
1445 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1446 req->timeout, req->callback, req->info )))
1448 free_message( msg );
1449 break;
1451 /* fall through */
1452 case MSG_NOTIFY:
1453 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1454 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1455 break;
1456 case MSG_POSTED:
1457 /* needed for posted DDE messages */
1458 msg->data_size = get_req_data_size();
1459 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1461 free( msg );
1462 break;
1464 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1465 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1466 break;
1467 case MSG_HARDWARE:
1468 queue_hardware_message( recv_queue, msg );
1469 break;
1470 case MSG_CALLBACK_RESULT: /* cannot send this one */
1471 default:
1472 set_error( STATUS_INVALID_PARAMETER );
1473 free( msg );
1474 break;
1477 if (thread) release_object( thread );
1481 /* get a message from the current queue */
1482 DECL_HANDLER(get_message)
1484 struct timer *timer;
1485 struct list *ptr;
1486 struct message *first_hw_msg = NULL;
1487 struct msg_queue *queue = get_current_queue();
1488 user_handle_t get_win = get_user_full_handle( req->get_win );
1490 if (!queue) return;
1491 gettimeofday( &queue->last_get_msg, NULL );
1493 /* first of all release the hardware input lock if we own it */
1494 /* we'll grab it again if we find a hardware message */
1495 if (queue->input->msg_thread == current)
1497 first_hw_msg = queue->input->msg;
1498 release_hardware_message( current, 0 );
1501 /* first check for sent messages */
1502 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1504 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1505 receive_message( queue, msg, reply );
1506 return;
1508 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1510 /* clear changed bits so we can wait on them if we don't find a message */
1511 queue->changed_bits = 0;
1513 /* then check for posted messages */
1514 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1515 return;
1517 /* then check for any raw hardware message */
1518 if (get_hardware_message( current, first_hw_msg, get_win, reply ))
1519 return;
1521 /* now check for WM_PAINT */
1522 if (queue->paint_count &&
1523 (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
1524 (reply->win = find_window_to_repaint( get_win, current )))
1526 reply->type = MSG_POSTED;
1527 reply->msg = WM_PAINT;
1528 reply->wparam = 0;
1529 reply->lparam = 0;
1530 reply->x = 0;
1531 reply->y = 0;
1532 reply->time = get_tick_count();
1533 reply->info = 0;
1534 return;
1537 /* now check for timer */
1538 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1539 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1541 reply->type = MSG_POSTED;
1542 reply->win = timer->win;
1543 reply->msg = timer->msg;
1544 reply->wparam = timer->id;
1545 reply->lparam = timer->lparam;
1546 reply->x = 0;
1547 reply->y = 0;
1548 reply->time = get_tick_count();
1549 reply->info = 0;
1550 return;
1553 done:
1554 set_error( STATUS_PENDING ); /* FIXME */
1558 /* reply to a sent message */
1559 DECL_HANDLER(reply_message)
1561 if (!current->queue)
1563 set_error( STATUS_ACCESS_DENIED );
1564 return;
1566 if (req->type == MSG_HARDWARE)
1568 struct thread_input *input = current->queue->input;
1569 if (input->msg_thread == current) release_hardware_message( current, req->remove );
1570 else set_error( STATUS_ACCESS_DENIED );
1572 else if (current->queue->recv_result)
1573 reply_message( current->queue, req->result, 0, req->remove,
1574 get_req_data(), get_req_data_size() );
1578 /* retrieve the reply for the last message sent */
1579 DECL_HANDLER(get_message_reply)
1581 struct message_result *result;
1582 struct list *entry;
1583 struct msg_queue *queue = current->queue;
1585 if (queue)
1587 set_error( STATUS_PENDING );
1588 reply->result = 0;
1590 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1592 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1593 if (result->replied || req->cancel)
1595 if (result->replied)
1597 reply->result = result->result;
1598 set_error( result->error );
1599 if (result->data)
1601 size_t data_len = min( result->data_size, get_reply_max_size() );
1602 set_reply_data_ptr( result->data, data_len );
1603 result->data = NULL;
1604 result->data_size = 0;
1607 remove_result_from_sender( result );
1609 entry = list_head( &queue->send_result );
1610 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1611 else
1613 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1614 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1618 else set_error( STATUS_ACCESS_DENIED );
1622 /* set a window timer */
1623 DECL_HANDLER(set_win_timer)
1625 struct timer *timer;
1626 struct msg_queue *queue;
1627 struct thread *thread = NULL;
1628 user_handle_t win = 0;
1629 unsigned int id = req->id;
1631 if (req->win)
1633 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1635 set_error( STATUS_INVALID_HANDLE );
1636 return;
1638 if (thread->process != current->process)
1640 release_object( thread );
1641 set_error( STATUS_ACCESS_DENIED );
1642 return;
1644 queue = thread->queue;
1645 /* remove it if it existed already */
1646 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1648 else
1650 queue = get_current_queue();
1651 /* find a free id for it */
1654 id = queue->next_timer_id;
1655 if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1657 while (find_timer( queue, 0, req->msg, id ));
1660 if ((timer = set_timer( queue, req->rate )))
1662 timer->win = win;
1663 timer->msg = req->msg;
1664 timer->id = id;
1665 timer->lparam = req->lparam;
1666 reply->id = id;
1668 if (thread) release_object( thread );
1671 /* kill a window timer */
1672 DECL_HANDLER(kill_win_timer)
1674 struct timer *timer;
1675 struct thread *thread;
1676 user_handle_t win = 0;
1678 if (req->win)
1680 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1682 set_error( STATUS_INVALID_HANDLE );
1683 return;
1685 if (thread->process != current->process)
1687 release_object( thread );
1688 set_error( STATUS_ACCESS_DENIED );
1689 return;
1692 else thread = (struct thread *)grab_object( current );
1694 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1695 free_timer( thread->queue, timer );
1696 else
1697 set_error( STATUS_INVALID_PARAMETER );
1699 release_object( thread );
1703 /* attach (or detach) thread inputs */
1704 DECL_HANDLER(attach_thread_input)
1706 struct thread *thread_from = get_thread_from_id( req->tid_from );
1707 struct thread *thread_to = get_thread_from_id( req->tid_to );
1709 if (!thread_from || !thread_to)
1711 if (thread_from) release_object( thread_from );
1712 if (thread_to) release_object( thread_to );
1713 return;
1715 if (thread_from != thread_to)
1717 if (req->attach) attach_thread_input( thread_from, thread_to );
1718 else detach_thread_input( thread_from, thread_to );
1720 else set_error( STATUS_ACCESS_DENIED );
1721 release_object( thread_from );
1722 release_object( thread_to );
1726 /* get thread input data */
1727 DECL_HANDLER(get_thread_input)
1729 struct thread *thread = NULL;
1730 struct thread_input *input;
1732 if (req->tid)
1734 if (!(thread = get_thread_from_id( req->tid ))) return;
1735 input = thread->queue ? thread->queue->input : NULL;
1737 else input = foreground_input; /* get the foreground thread info */
1739 if (input)
1741 reply->focus = input->focus;
1742 reply->capture = input->capture;
1743 reply->active = input->active;
1744 reply->menu_owner = input->menu_owner;
1745 reply->move_size = input->move_size;
1746 reply->caret = input->caret;
1747 reply->rect = input->caret_rect;
1749 else
1751 reply->focus = 0;
1752 reply->capture = 0;
1753 reply->active = 0;
1754 reply->menu_owner = 0;
1755 reply->move_size = 0;
1756 reply->caret = 0;
1757 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1759 /* foreground window is active window of foreground thread */
1760 reply->foreground = foreground_input ? foreground_input->active : 0;
1761 if (thread) release_object( thread );
1765 /* retrieve queue keyboard state for a given thread */
1766 DECL_HANDLER(get_key_state)
1768 struct thread *thread;
1769 struct thread_input *input;
1771 if (!(thread = get_thread_from_id( req->tid ))) return;
1772 input = thread->queue ? thread->queue->input : NULL;
1773 if (input)
1775 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1776 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1778 release_object( thread );
1782 /* set queue keyboard state for a given thread */
1783 DECL_HANDLER(set_key_state)
1785 struct thread *thread = NULL;
1786 struct thread_input *input;
1788 if (!(thread = get_thread_from_id( req->tid ))) return;
1789 input = thread->queue ? thread->queue->input : NULL;
1790 if (input)
1792 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1793 if (size) memcpy( input->keystate, get_req_data(), size );
1795 release_object( thread );
1799 /* set the system foreground window */
1800 DECL_HANDLER(set_foreground_window)
1802 struct msg_queue *queue = get_current_queue();
1804 reply->previous = foreground_input ? foreground_input->active : 0;
1805 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1806 reply->send_msg_new = FALSE;
1808 if (req->handle)
1810 struct thread *thread;
1812 if (is_top_level_window( req->handle ) &&
1813 ((thread = get_window_thread( req->handle ))))
1815 foreground_input = thread->queue->input;
1816 reply->send_msg_new = (foreground_input != queue->input);
1817 release_object( thread );
1819 else set_error( STATUS_INVALID_HANDLE );
1821 else foreground_input = NULL;
1825 /* set the current thread focus window */
1826 DECL_HANDLER(set_focus_window)
1828 struct msg_queue *queue = get_current_queue();
1830 reply->previous = 0;
1831 if (queue && check_queue_input_window( queue, req->handle ))
1833 reply->previous = queue->input->focus;
1834 queue->input->focus = get_user_full_handle( req->handle );
1839 /* set the current thread active window */
1840 DECL_HANDLER(set_active_window)
1842 struct msg_queue *queue = get_current_queue();
1844 reply->previous = 0;
1845 if (queue && check_queue_input_window( queue, req->handle ))
1847 if (!req->handle || make_window_active( req->handle ))
1849 reply->previous = queue->input->active;
1850 queue->input->active = get_user_full_handle( req->handle );
1852 else set_error( STATUS_INVALID_HANDLE );
1857 /* set the current thread capture window */
1858 DECL_HANDLER(set_capture_window)
1860 struct msg_queue *queue = get_current_queue();
1862 reply->previous = reply->full_handle = 0;
1863 if (queue && check_queue_input_window( queue, req->handle ))
1865 struct thread_input *input = queue->input;
1867 reply->previous = input->capture;
1868 input->capture = get_user_full_handle( req->handle );
1869 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1870 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1871 reply->full_handle = input->capture;
1876 /* Set the current thread caret window */
1877 DECL_HANDLER(set_caret_window)
1879 struct msg_queue *queue = get_current_queue();
1881 reply->previous = 0;
1882 if (queue && check_queue_input_window( queue, req->handle ))
1884 struct thread_input *input = queue->input;
1886 reply->previous = input->caret;
1887 reply->old_rect = input->caret_rect;
1888 reply->old_hide = input->caret_hide;
1889 reply->old_state = input->caret_state;
1891 set_caret_window( input, get_user_full_handle(req->handle) );
1892 input->caret_rect.right = input->caret_rect.left + req->width;
1893 input->caret_rect.bottom = input->caret_rect.top + req->height;
1898 /* Set the current thread caret information */
1899 DECL_HANDLER(set_caret_info)
1901 struct msg_queue *queue = get_current_queue();
1902 struct thread_input *input;
1904 if (!queue) return;
1905 input = queue->input;
1906 reply->full_handle = input->caret;
1907 reply->old_rect = input->caret_rect;
1908 reply->old_hide = input->caret_hide;
1909 reply->old_state = input->caret_state;
1911 if (req->handle && get_user_full_handle(req->handle) != input->caret)
1913 set_error( STATUS_ACCESS_DENIED );
1914 return;
1916 if (req->flags & SET_CARET_POS)
1918 input->caret_rect.right += req->x - input->caret_rect.left;
1919 input->caret_rect.bottom += req->y - input->caret_rect.top;
1920 input->caret_rect.left = req->x;
1921 input->caret_rect.top = req->y;
1923 if (req->flags & SET_CARET_HIDE)
1925 input->caret_hide += req->hide;
1926 if (input->caret_hide < 0) input->caret_hide = 0;
1928 if (req->flags & SET_CARET_STATE)
1930 if (req->state == -1) input->caret_state = !input->caret_state;
1931 else input->caret_state = !!req->state;