Avoid returning hardware messages if they have no chance to match the
[wine/wine64.git] / server / queue.c
blob05f4c7c9383dabda649737967b79c475f49948c6
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 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
42 #define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK
44 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
45 #define NB_MSG_KINDS (POST_MESSAGE+1)
48 struct message_result
50 struct list sender_entry; /* entry in sender list */
51 struct message_result *recv_next; /* next in receiver list */
52 struct msg_queue *sender; /* sender queue */
53 struct msg_queue *receiver; /* receiver queue */
54 int replied; /* has it been replied to? */
55 unsigned int result; /* reply result */
56 unsigned int error; /* error code to pass back to sender */
57 struct message *callback_msg; /* message to queue for callback */
58 void *data; /* message reply data */
59 unsigned int data_size; /* size of message reply data */
60 struct timeout_user *timeout; /* result timeout */
63 struct message
65 struct list entry; /* entry in message list */
66 enum message_type type; /* message type */
67 user_handle_t win; /* window handle */
68 unsigned int msg; /* message code */
69 unsigned int wparam; /* parameters */
70 unsigned int lparam; /* parameters */
71 int x; /* x position */
72 int y; /* y position */
73 unsigned int time; /* message time */
74 unsigned int info; /* extra info */
75 user_handle_t hook; /* winevent hook handle */
76 void *hook_proc; /* winevent hook proc address */
77 void *data; /* message data for sent messages */
78 unsigned int data_size; /* size of message data */
79 struct message_result *result; /* result in sender queue */
82 struct timer
84 struct list entry; /* entry in timer list */
85 struct timeval when; /* next expiration */
86 unsigned int rate; /* timer rate in ms */
87 user_handle_t win; /* window handle */
88 unsigned int msg; /* message to post */
89 unsigned int id; /* timer id */
90 unsigned int lparam; /* lparam for message */
93 struct thread_input
95 struct object obj; /* object header */
96 user_handle_t focus; /* focus window */
97 user_handle_t capture; /* capture window */
98 user_handle_t active; /* active window */
99 user_handle_t menu_owner; /* current menu owner window */
100 user_handle_t move_size; /* current moving/resizing window */
101 user_handle_t caret; /* caret window */
102 rectangle_t caret_rect; /* caret rectangle */
103 int caret_hide; /* caret hide count */
104 int caret_state; /* caret on/off state */
105 struct message *msg; /* message currently processed */
106 struct thread *msg_thread; /* thread processing the message */
107 struct list msg_list; /* list of hardware messages */
108 unsigned char keystate[256]; /* state of each key */
111 struct msg_queue
113 struct object obj; /* object header */
114 unsigned int wake_bits; /* wakeup bits */
115 unsigned int wake_mask; /* wakeup mask */
116 unsigned int changed_bits; /* changed wakeup bits */
117 unsigned int changed_mask; /* changed wakeup mask */
118 int paint_count; /* pending paint messages count */
119 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
120 struct list send_result; /* stack of sent messages waiting for result */
121 struct list callback_result; /* list of callback messages waiting for result */
122 struct message_result *recv_result; /* stack of received messages waiting for result */
123 struct list pending_timers; /* list of pending timers */
124 struct list expired_timers; /* list of expired timers */
125 unsigned int next_timer_id; /* id for the next timer with a 0 window */
126 struct timeout_user *timeout; /* timeout for next timer to expire */
127 struct thread_input *input; /* thread input descriptor */
128 struct hook_table *hooks; /* hook table */
129 struct timeval last_get_msg; /* time of last get message call */
132 static void msg_queue_dump( struct object *obj, int verbose );
133 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
134 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
135 static int msg_queue_signaled( struct object *obj, struct thread *thread );
136 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
137 static void msg_queue_destroy( struct object *obj );
138 static void thread_input_dump( struct object *obj, int verbose );
139 static void thread_input_destroy( struct object *obj );
140 static void timer_callback( void *private );
142 static const struct object_ops msg_queue_ops =
144 sizeof(struct msg_queue), /* size */
145 msg_queue_dump, /* dump */
146 msg_queue_add_queue, /* add_queue */
147 msg_queue_remove_queue, /* remove_queue */
148 msg_queue_signaled, /* signaled */
149 msg_queue_satisfied, /* satisfied */
150 no_get_fd, /* get_fd */
151 msg_queue_destroy /* destroy */
155 static const struct object_ops thread_input_ops =
157 sizeof(struct thread_input), /* size */
158 thread_input_dump, /* dump */
159 no_add_queue, /* add_queue */
160 NULL, /* remove_queue */
161 NULL, /* signaled */
162 NULL, /* satisfied */
163 no_get_fd, /* get_fd */
164 thread_input_destroy /* destroy */
167 /* pointer to input structure of foreground thread */
168 static struct thread_input *foreground_input;
171 /* set the caret window in a given thread input */
172 static void set_caret_window( struct thread_input *input, user_handle_t win )
174 if (!win || win != input->caret)
176 input->caret_rect.left = 0;
177 input->caret_rect.top = 0;
178 input->caret_rect.right = 0;
179 input->caret_rect.bottom = 0;
181 input->caret = win;
182 input->caret_hide = 1;
183 input->caret_state = 0;
186 /* create a thread input object */
187 static struct thread_input *create_thread_input(void)
189 struct thread_input *input;
191 if ((input = alloc_object( &thread_input_ops )))
193 input->focus = 0;
194 input->capture = 0;
195 input->active = 0;
196 input->menu_owner = 0;
197 input->move_size = 0;
198 input->msg = NULL;
199 input->msg_thread = NULL;
200 list_init( &input->msg_list );
201 set_caret_window( input, 0 );
202 memset( input->keystate, 0, sizeof(input->keystate) );
204 return input;
207 /* release the thread input data of a given thread */
208 static void release_thread_input( struct thread *thread )
210 struct thread_input *input = thread->queue->input;
212 if (!input) return;
213 if (input->msg_thread == thread)
215 release_object( input->msg_thread );
216 input->msg_thread = NULL;
217 input->msg = NULL;
219 release_object( input );
220 thread->queue->input = NULL;
223 /* create a message queue object */
224 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
226 struct msg_queue *queue;
227 int i;
229 if (!input && !(input = create_thread_input())) return NULL;
230 if ((queue = alloc_object( &msg_queue_ops )))
232 queue->wake_bits = 0;
233 queue->wake_mask = 0;
234 queue->changed_bits = 0;
235 queue->changed_mask = 0;
236 queue->paint_count = 0;
237 queue->recv_result = NULL;
238 queue->next_timer_id = 1;
239 queue->timeout = NULL;
240 queue->input = (struct thread_input *)grab_object( input );
241 queue->hooks = NULL;
242 gettimeofday( &queue->last_get_msg, NULL );
243 list_init( &queue->send_result );
244 list_init( &queue->callback_result );
245 list_init( &queue->pending_timers );
246 list_init( &queue->expired_timers );
247 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
249 thread->queue = queue;
250 if (!thread->process->queue)
251 thread->process->queue = (struct msg_queue *)grab_object( queue );
253 release_object( input );
254 return queue;
257 /* free the message queue of a thread at thread exit */
258 void free_msg_queue( struct thread *thread )
260 struct process *process = thread->process;
261 struct thread_input *input;
263 remove_thread_hooks( thread );
264 if (!thread->queue) return;
265 if (process->queue == thread->queue) /* is it the process main queue? */
267 release_object( process->queue );
268 process->queue = NULL;
269 if (process->idle_event)
271 set_event( process->idle_event );
272 release_object( process->idle_event );
273 process->idle_event = NULL;
276 input = thread->queue->input;
277 if (input->msg_thread == thread)
279 release_object( input->msg_thread );
280 input->msg_thread = NULL;
281 input->msg = NULL;
283 release_object( thread->queue );
284 thread->queue = NULL;
287 /* get the hook table for a given thread */
288 struct hook_table *get_queue_hooks( struct thread *thread )
290 if (!thread->queue) return NULL;
291 return thread->queue->hooks;
294 /* set the hook table for a given thread, allocating the queue if needed */
295 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
297 struct msg_queue *queue = thread->queue;
298 if (!queue) queue = create_msg_queue( thread, NULL );
299 if (queue->hooks) release_object( queue->hooks );
300 queue->hooks = hooks;
303 /* check the queue status */
304 inline static int is_signaled( struct msg_queue *queue )
306 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
309 /* set some queue bits */
310 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
312 queue->wake_bits |= bits;
313 queue->changed_bits |= bits;
314 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
317 /* clear some queue bits */
318 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
320 queue->wake_bits &= ~bits;
321 queue->changed_bits &= ~bits;
324 /* check whether msg is a keyboard message */
325 inline static int is_keyboard_msg( struct message *msg )
327 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
330 /* check whether a message filter contains at least one potential hardware message */
331 inline static int filter_contains_hw_range( unsigned int first, unsigned int last )
333 /* hardware message ranges are (in numerical order):
334 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
335 * WM_KEYFIRST .. WM_KEYLAST
336 * WM_MOUSEFIRST .. WM_MOUSELAST
338 if (last < WM_NCMOUSEFIRST) return 0;
339 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
340 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
341 if (first > WM_MOUSELAST) return 0;
342 return 1;
345 /* get the QS_* bit corresponding to a given hardware message */
346 inline static int get_hardware_msg_bit( struct message *msg )
348 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
349 if (is_keyboard_msg( msg )) return QS_KEY;
350 return QS_MOUSEBUTTON;
353 /* get the current thread queue, creating it if needed */
354 inline static struct msg_queue *get_current_queue(void)
356 struct msg_queue *queue = current->queue;
357 if (!queue) queue = create_msg_queue( current, NULL );
358 return queue;
361 /* try to merge a message with the last in the list; return 1 if successful */
362 static int merge_message( struct thread_input *input, const struct message *msg )
364 struct message *prev;
365 struct list *ptr = list_tail( &input->msg_list );
367 if (!ptr) return 0;
368 prev = LIST_ENTRY( ptr, struct message, entry );
369 if (input->msg == prev) return 0;
370 if (prev->result) return 0;
371 if (prev->win != msg->win) return 0;
372 if (prev->msg != msg->msg) return 0;
373 if (prev->type != msg->type) return 0;
374 /* now we can merge it */
375 prev->wparam = msg->wparam;
376 prev->lparam = msg->lparam;
377 prev->x = msg->x;
378 prev->y = msg->y;
379 prev->time = msg->time;
380 prev->info = msg->info;
381 return 1;
384 /* free a result structure */
385 static void free_result( struct message_result *result )
387 if (result->timeout) remove_timeout_user( result->timeout );
388 if (result->data) free( result->data );
389 if (result->callback_msg) free( result->callback_msg );
390 free( result );
393 /* remove the result from the sender list it is on */
394 static inline void remove_result_from_sender( struct message_result *result )
396 assert( result->sender );
398 list_remove( &result->sender_entry );
399 result->sender = NULL;
400 if (!result->receiver) free_result( result );
403 /* store the message result in the appropriate structure */
404 static void store_message_result( struct message_result *res, unsigned int result,
405 unsigned int error )
407 res->result = result;
408 res->error = error;
409 res->replied = 1;
410 if (res->timeout)
412 remove_timeout_user( res->timeout );
413 res->timeout = NULL;
415 if (res->sender)
417 if (res->callback_msg)
419 /* queue the callback message in the sender queue */
420 res->callback_msg->lparam = result;
421 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
422 set_queue_bits( res->sender, QS_SENDMESSAGE );
423 res->callback_msg = NULL;
424 remove_result_from_sender( res );
426 else
428 /* wake sender queue if waiting on this result */
429 if (list_head(&res->sender->send_result) == &res->sender_entry)
430 set_queue_bits( res->sender, QS_SMRESULT );
436 /* free a message when deleting a queue or window */
437 static void free_message( struct message *msg )
439 struct message_result *result = msg->result;
440 if (result)
442 if (result->sender)
444 result->receiver = NULL;
445 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
447 else free_result( result );
449 if (msg->data) free( msg->data );
450 free( msg );
453 /* remove (and free) a message from a message list */
454 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
455 enum message_kind kind )
457 list_remove( &msg->entry );
458 switch(kind)
460 case SEND_MESSAGE:
461 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
462 break;
463 case POST_MESSAGE:
464 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_POSTMESSAGE );
465 break;
467 free_message( msg );
470 /* message timed out without getting a reply */
471 static void result_timeout( void *private )
473 struct message_result *result = private;
475 assert( !result->replied );
477 result->timeout = NULL;
478 store_message_result( result, 0, STATUS_TIMEOUT );
481 /* allocate and fill a message result structure */
482 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
483 struct msg_queue *recv_queue,
484 struct message *msg, unsigned int timeout,
485 void *callback, unsigned int callback_data )
487 struct message_result *result = mem_alloc( sizeof(*result) );
488 if (result)
490 result->sender = send_queue;
491 result->receiver = recv_queue;
492 result->replied = 0;
493 result->data = NULL;
494 result->data_size = 0;
495 result->timeout = NULL;
497 if (msg->type == MSG_CALLBACK)
499 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
500 if (!callback_msg)
502 free( result );
503 return NULL;
505 callback_msg->type = MSG_CALLBACK_RESULT;
506 callback_msg->win = msg->win;
507 callback_msg->msg = msg->msg;
508 callback_msg->wparam = (unsigned int)callback;
509 callback_msg->lparam = 0;
510 callback_msg->time = get_tick_count();
511 callback_msg->x = 0;
512 callback_msg->y = 0;
513 callback_msg->info = callback_data;
514 callback_msg->hook = 0;
515 callback_msg->hook_proc = NULL;
516 callback_msg->result = NULL;
517 callback_msg->data = NULL;
518 callback_msg->data_size = 0;
520 result->callback_msg = callback_msg;
521 list_add_head( &send_queue->callback_result, &result->sender_entry );
523 else
525 result->callback_msg = NULL;
526 list_add_head( &send_queue->send_result, &result->sender_entry );
529 if (timeout != -1)
531 struct timeval when;
532 gettimeofday( &when, 0 );
533 add_timeout( &when, timeout );
534 result->timeout = add_timeout_user( &when, result_timeout, result );
537 return result;
540 /* receive a message, removing it from the sent queue */
541 static void receive_message( struct msg_queue *queue, struct message *msg,
542 struct get_message_reply *reply )
544 struct message_result *result = msg->result;
546 reply->total = msg->data_size;
547 if (msg->data_size > get_reply_max_size())
549 set_error( STATUS_BUFFER_OVERFLOW );
550 return;
552 reply->type = msg->type;
553 reply->win = msg->win;
554 reply->msg = msg->msg;
555 reply->wparam = msg->wparam;
556 reply->lparam = msg->lparam;
557 reply->x = msg->x;
558 reply->y = msg->y;
559 reply->time = msg->time;
560 reply->info = msg->info;
561 reply->hook = msg->hook;
562 reply->hook_proc = msg->hook_proc;
564 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
566 list_remove( &msg->entry );
567 /* put the result on the receiver result stack */
568 if (result)
570 result->recv_next = queue->recv_result;
571 queue->recv_result = result;
573 free( msg );
574 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
577 /* set the result of the current received message */
578 static void reply_message( struct msg_queue *queue, unsigned int result,
579 unsigned int error, int remove, const void *data, size_t len )
581 struct message_result *res = queue->recv_result;
583 if (remove)
585 queue->recv_result = res->recv_next;
586 res->receiver = NULL;
587 if (!res->sender) /* no one waiting for it */
589 free_result( res );
590 return;
593 if (!res->replied)
595 if (len && (res->data = memdup( data, len ))) res->data_size = len;
596 store_message_result( res, result, error );
600 /* retrieve a posted message */
601 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
602 unsigned int first, unsigned int last, unsigned int flags,
603 struct get_message_reply *reply )
605 struct message *msg;
607 /* check against the filters */
608 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
610 if (msg->msg == WM_QUIT) goto found; /* WM_QUIT is never filtered */
611 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
612 if (msg->msg < first) continue;
613 if (msg->msg > last) continue;
614 goto found; /* found one */
616 return 0;
618 /* return it to the app */
619 found:
620 reply->total = msg->data_size;
621 if (msg->data_size > get_reply_max_size())
623 set_error( STATUS_BUFFER_OVERFLOW );
624 return 1;
626 reply->type = msg->type;
627 reply->win = msg->win;
628 reply->msg = msg->msg;
629 reply->wparam = msg->wparam;
630 reply->lparam = msg->lparam;
631 reply->x = msg->x;
632 reply->y = msg->y;
633 reply->time = msg->time;
634 reply->info = msg->info;
636 if (flags & GET_MSG_REMOVE)
638 if (msg->data)
640 set_reply_data_ptr( msg->data, msg->data_size );
641 msg->data = NULL;
642 msg->data_size = 0;
644 remove_queue_message( queue, msg, POST_MESSAGE );
646 else if (msg->data) set_reply_data( msg->data, msg->data_size );
648 return 1;
651 /* empty a message list and free all the messages */
652 static void empty_msg_list( struct list *list )
654 struct list *ptr;
656 while ((ptr = list_head( list )) != NULL)
658 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
659 list_remove( &msg->entry );
660 free_message( msg );
664 /* cleanup all pending results when deleting a queue */
665 static void cleanup_results( struct msg_queue *queue )
667 struct list *entry;
669 while ((entry = list_head( &queue->send_result )) != NULL)
671 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
674 while ((entry = list_head( &queue->callback_result )) != NULL)
676 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
679 while (queue->recv_result)
680 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
683 /* check if the thread owning the queue is hung (not checking for messages) */
684 static int is_queue_hung( struct msg_queue *queue )
686 struct timeval now;
687 struct wait_queue_entry *entry;
689 gettimeofday( &now, NULL );
690 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
691 return 0; /* less than 5 seconds since last get message -> not hung */
693 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
695 if (entry->thread->queue == queue)
696 return 0; /* thread is waiting on queue -> not hung */
698 return 1;
701 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
703 struct msg_queue *queue = (struct msg_queue *)obj;
704 struct process *process = entry->thread->process;
706 /* a thread can only wait on its own queue */
707 if (entry->thread->queue != queue)
709 set_error( STATUS_ACCESS_DENIED );
710 return 0;
712 /* if waiting on the main process queue, set the idle event */
713 if (process->queue == queue)
715 if (process->idle_event) set_event( process->idle_event );
717 add_queue( obj, entry );
718 return 1;
721 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
723 struct msg_queue *queue = (struct msg_queue *)obj;
724 struct process *process = entry->thread->process;
726 remove_queue( obj, entry );
728 assert( entry->thread->queue == queue );
730 /* if waiting on the main process queue, reset the idle event */
731 if (process->queue == queue)
733 if (process->idle_event) reset_event( process->idle_event );
737 static void msg_queue_dump( struct object *obj, int verbose )
739 struct msg_queue *queue = (struct msg_queue *)obj;
740 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
741 queue->wake_bits, queue->wake_mask );
744 static int msg_queue_signaled( struct object *obj, struct thread *thread )
746 struct msg_queue *queue = (struct msg_queue *)obj;
747 return is_signaled( queue );
750 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
752 struct msg_queue *queue = (struct msg_queue *)obj;
753 queue->wake_mask = 0;
754 queue->changed_mask = 0;
755 return 0; /* Not abandoned */
758 static void msg_queue_destroy( struct object *obj )
760 struct msg_queue *queue = (struct msg_queue *)obj;
761 struct list *ptr;
762 int i;
764 cleanup_results( queue );
765 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
767 while ((ptr = list_head( &queue->pending_timers )))
769 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
770 list_remove( &timer->entry );
771 free( timer );
773 while ((ptr = list_head( &queue->expired_timers )))
775 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
776 list_remove( &timer->entry );
777 free( timer );
779 if (queue->timeout) remove_timeout_user( queue->timeout );
780 if (queue->input) release_object( queue->input );
781 if (queue->hooks) release_object( queue->hooks );
784 static void thread_input_dump( struct object *obj, int verbose )
786 struct thread_input *input = (struct thread_input *)obj;
787 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
788 input->focus, input->capture, input->active );
791 static void thread_input_destroy( struct object *obj )
793 struct thread_input *input = (struct thread_input *)obj;
795 if (foreground_input == input) foreground_input = NULL;
796 if (input->msg_thread) release_object( input->msg_thread );
797 empty_msg_list( &input->msg_list );
800 /* fix the thread input data when a window is destroyed */
801 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
803 struct thread_input *input = queue->input;
805 if (window == input->focus) input->focus = 0;
806 if (window == input->capture) input->capture = 0;
807 if (window == input->active) input->active = 0;
808 if (window == input->menu_owner) input->menu_owner = 0;
809 if (window == input->move_size) input->move_size = 0;
810 if (window == input->caret) set_caret_window( input, 0 );
813 /* check if the specified window can be set in the input data of a given queue */
814 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
816 struct thread *thread;
817 int ret = 0;
819 if (!window) return 1; /* we can always clear the data */
821 if ((thread = get_window_thread( window )))
823 ret = (queue->input == thread->queue->input);
824 if (!ret) set_error( STATUS_ACCESS_DENIED );
825 release_object( thread );
827 else set_error( STATUS_INVALID_HANDLE );
829 return ret;
832 /* attach two thread input data structures */
833 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
835 struct thread_input *input;
837 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
838 input = (struct thread_input *)grab_object( thread_to->queue->input );
840 if (thread_from->queue)
842 release_thread_input( thread_from );
843 thread_from->queue->input = input;
845 else
847 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
849 memset( input->keystate, 0, sizeof(input->keystate) );
850 return 1;
853 /* detach two thread input data structures */
854 static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
856 struct thread_input *input;
858 if (!thread_from->queue || !thread_to->queue ||
859 thread_from->queue->input != thread_to->queue->input)
861 set_error( STATUS_ACCESS_DENIED );
862 return;
864 if ((input = create_thread_input()))
866 release_thread_input( thread_from );
867 thread_from->queue->input = input;
872 /* set the next timer to expire */
873 static void set_next_timer( struct msg_queue *queue )
875 struct list *ptr;
877 if (queue->timeout)
879 remove_timeout_user( queue->timeout );
880 queue->timeout = NULL;
882 if ((ptr = list_head( &queue->pending_timers )))
884 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
885 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
887 /* set/clear QS_TIMER bit */
888 if (list_empty( &queue->expired_timers ))
889 clear_queue_bits( queue, QS_TIMER );
890 else
891 set_queue_bits( queue, QS_TIMER );
894 /* find a timer from its window and id */
895 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
896 unsigned int msg, unsigned int id )
898 struct list *ptr;
900 /* we need to search both lists */
902 LIST_FOR_EACH( ptr, &queue->pending_timers )
904 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
905 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
907 LIST_FOR_EACH( ptr, &queue->expired_timers )
909 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
910 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
912 return NULL;
915 /* callback for the next timer expiration */
916 static void timer_callback( void *private )
918 struct msg_queue *queue = private;
919 struct list *ptr;
921 queue->timeout = NULL;
922 /* move on to the next timer */
923 ptr = list_head( &queue->pending_timers );
924 list_remove( ptr );
925 list_add_tail( &queue->expired_timers, ptr );
926 set_next_timer( queue );
929 /* link a timer at its rightful place in the queue list */
930 static void link_timer( struct msg_queue *queue, struct timer *timer )
932 struct list *ptr;
934 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
936 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
937 if (!time_before( &t->when, &timer->when )) break;
939 list_add_before( ptr, &timer->entry );
942 /* remove a timer from the queue timer list and free it */
943 static void free_timer( struct msg_queue *queue, struct timer *timer )
945 list_remove( &timer->entry );
946 free( timer );
947 set_next_timer( queue );
950 /* restart an expired timer */
951 static void restart_timer( struct msg_queue *queue, struct timer *timer )
953 struct timeval now;
955 list_remove( &timer->entry );
956 gettimeofday( &now, 0 );
957 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
958 link_timer( queue, timer );
959 set_next_timer( queue );
962 /* find an expired timer matching the filtering parameters */
963 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
964 unsigned int get_first, unsigned int get_last,
965 int remove )
967 struct list *ptr;
969 LIST_FOR_EACH( ptr, &queue->expired_timers )
971 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
972 if (win && timer->win != win) continue;
973 if (timer->msg >= get_first && timer->msg <= get_last)
975 if (remove) restart_timer( queue, timer );
976 return timer;
979 return NULL;
982 /* add a timer */
983 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
985 struct timer *timer = mem_alloc( sizeof(*timer) );
986 if (timer)
988 timer->rate = max( rate, 1 );
989 gettimeofday( &timer->when, 0 );
990 add_timeout( &timer->when, rate );
991 link_timer( queue, timer );
992 /* check if we replaced the next timer */
993 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
995 return timer;
998 /* change the input key state for a given key */
999 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
1001 if (down)
1003 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
1004 input->keystate[key] |= 0x80;
1006 else input->keystate[key] &= ~0x80;
1009 /* update the input key state for a keyboard message */
1010 static void update_input_key_state( struct thread_input *input, const struct message *msg )
1012 unsigned char key;
1013 int down = 0, extended;
1015 switch (msg->msg)
1017 case WM_LBUTTONDOWN:
1018 down = 1;
1019 /* fall through */
1020 case WM_LBUTTONUP:
1021 set_input_key_state( input, VK_LBUTTON, down );
1022 break;
1023 case WM_MBUTTONDOWN:
1024 down = 1;
1025 /* fall through */
1026 case WM_MBUTTONUP:
1027 set_input_key_state( input, VK_MBUTTON, down );
1028 break;
1029 case WM_RBUTTONDOWN:
1030 down = 1;
1031 /* fall through */
1032 case WM_RBUTTONUP:
1033 set_input_key_state( input, VK_RBUTTON, down );
1034 break;
1035 case WM_KEYDOWN:
1036 case WM_SYSKEYDOWN:
1037 down = 1;
1038 /* fall through */
1039 case WM_KEYUP:
1040 case WM_SYSKEYUP:
1041 key = (unsigned char)msg->wparam;
1042 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1043 set_input_key_state( input, key, down );
1044 switch(key)
1046 case VK_SHIFT:
1047 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1048 break;
1049 case VK_CONTROL:
1050 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1051 break;
1052 case VK_MENU:
1053 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1054 break;
1056 break;
1060 /* release the hardware message currently being processed by the given thread */
1061 static void release_hardware_message( struct thread *thread, int remove )
1063 struct thread_input *input = thread->queue->input;
1065 if (input->msg_thread != thread) return;
1066 if (remove)
1068 struct message *other;
1069 int clr_bit;
1071 update_input_key_state( input, input->msg );
1072 list_remove( &input->msg->entry );
1073 clr_bit = get_hardware_msg_bit( input->msg );
1074 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1076 if (get_hardware_msg_bit( other ) == clr_bit)
1078 clr_bit = 0;
1079 break;
1082 if (clr_bit) clear_queue_bits( thread->queue, clr_bit );
1083 free_message( input->msg );
1085 release_object( input->msg_thread );
1086 input->msg = NULL;
1087 input->msg_thread = NULL;
1090 /* find the window that should receive a given hardware message */
1091 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1092 unsigned int *msg_code )
1094 user_handle_t win = 0;
1096 *msg_code = msg->msg;
1097 if (is_keyboard_msg( msg ))
1099 if (input && !(win = input->focus))
1101 win = input->active;
1102 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1105 else /* mouse message */
1107 if (!input || !(win = input->capture))
1109 if (!(win = msg->win) || !is_window_visible( win ))
1110 win = window_from_point( msg->x, msg->y );
1113 return win;
1116 /* queue a hardware message into a given thread input */
1117 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1119 user_handle_t win;
1120 struct thread *thread;
1121 struct thread_input *input;
1122 unsigned int msg_code;
1124 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1125 if (!win || !(thread = get_window_thread(win)))
1127 free( msg );
1128 return;
1130 input = thread->queue->input;
1132 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1133 else
1135 list_add_tail( &input->msg_list, &msg->entry );
1136 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1138 release_object( thread );
1141 /* find a hardware message for the given queue */
1142 static int get_hardware_message( struct thread *thread, int get_next_hw, struct message *first,
1143 user_handle_t filter_win, struct get_message_reply *reply )
1145 struct thread_input *input = thread->queue->input;
1146 struct thread *win_thread;
1147 struct list *ptr;
1148 user_handle_t win;
1149 int clear_bits, got_one = 0;
1150 unsigned int msg_code;
1152 if (input->msg_thread && ((input->msg_thread != thread) || !get_next_hw))
1153 return 0; /* locked by another thread, or another recursion level of the same thread */
1155 if (!first)
1157 ptr = list_head( &input->msg_list );
1158 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1160 else
1162 ptr = list_next( &input->msg_list, &first->entry );
1163 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1166 while (ptr)
1168 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1169 win = find_hardware_message_window( input, msg, &msg_code );
1170 if (!win || !(win_thread = get_window_thread( win )))
1172 /* no window at all, remove it */
1173 ptr = list_next( &input->msg_list, ptr );
1174 update_input_key_state( input, msg );
1175 list_remove( &msg->entry );
1176 free_message( msg );
1177 continue;
1179 if (win_thread != thread)
1181 /* wake the other thread */
1182 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1183 release_object( win_thread );
1184 got_one = 1;
1185 ptr = list_next( &input->msg_list, ptr );
1186 continue;
1188 /* if we already got a message for another thread, or if it doesn't
1189 * match the filter we skip it (filter is only checked for keyboard
1190 * messages since the dest window for a mouse message depends on hittest)
1192 if (got_one ||
1193 (filter_win && is_keyboard_msg(msg) &&
1194 win != filter_win && !is_child_window( filter_win, win )))
1196 clear_bits &= ~get_hardware_msg_bit( msg );
1197 release_object( win_thread );
1198 ptr = list_next( &input->msg_list, ptr );
1199 continue;
1201 /* now we can return it */
1202 if (!input->msg_thread) input->msg_thread = win_thread;
1203 else release_object( win_thread );
1204 input->msg = msg;
1206 reply->type = MSG_HARDWARE;
1207 reply->win = win;
1208 reply->msg = msg_code;
1209 reply->wparam = msg->wparam;
1210 reply->lparam = msg->lparam;
1211 reply->x = msg->x;
1212 reply->y = msg->y;
1213 reply->time = msg->time;
1214 reply->info = msg->info;
1215 return 1;
1217 /* nothing found, clear the hardware queue bits */
1218 clear_queue_bits( thread->queue, clear_bits );
1219 if (input->msg_thread) release_object( input->msg_thread );
1220 input->msg = NULL;
1221 input->msg_thread = NULL;
1222 return 0;
1225 /* increment (or decrement if 'incr' is negative) the queue paint count */
1226 void inc_queue_paint_count( struct thread *thread, int incr )
1228 struct msg_queue *queue = thread->queue;
1230 assert( queue );
1232 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1234 if (queue->paint_count)
1235 set_queue_bits( queue, QS_PAINT );
1236 else
1237 clear_queue_bits( queue, QS_PAINT );
1241 /* remove all messages and timers belonging to a certain window */
1242 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1244 struct msg_queue *queue = thread->queue;
1245 struct list *ptr;
1246 int i;
1248 if (!queue) return;
1250 /* remove timers */
1252 ptr = list_head( &queue->pending_timers );
1253 while (ptr)
1255 struct list *next = list_next( &queue->pending_timers, ptr );
1256 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1257 if (timer->win == win) free_timer( queue, timer );
1258 ptr = next;
1260 ptr = list_head( &queue->expired_timers );
1261 while (ptr)
1263 struct list *next = list_next( &queue->expired_timers, ptr );
1264 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1265 if (timer->win == win) free_timer( queue, timer );
1266 ptr = next;
1269 /* remove messages */
1270 for (i = 0; i < NB_MSG_KINDS; i++)
1272 struct list *ptr, *next;
1274 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1276 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1277 if (msg->win == win) remove_queue_message( queue, msg, i );
1281 thread_input_cleanup_window( queue, win );
1284 /* post a message to a window; used by socket handling */
1285 void post_message( user_handle_t win, unsigned int message,
1286 unsigned int wparam, unsigned int lparam )
1288 struct message *msg;
1289 struct thread *thread = get_window_thread( win );
1291 if (!thread) return;
1293 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1295 msg->type = MSG_POSTED;
1296 msg->win = get_user_full_handle( win );
1297 msg->msg = message;
1298 msg->wparam = wparam;
1299 msg->lparam = lparam;
1300 msg->time = get_tick_count();
1301 msg->x = 0;
1302 msg->y = 0;
1303 msg->info = 0;
1304 msg->hook = 0;
1305 msg->hook_proc = NULL;
1306 msg->result = NULL;
1307 msg->data = NULL;
1308 msg->data_size = 0;
1310 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1311 set_queue_bits( thread->queue, QS_POSTMESSAGE );
1313 release_object( thread );
1316 /* post a win event */
1317 void post_win_event( struct thread *thread, unsigned int event,
1318 user_handle_t win, unsigned int object_id,
1319 unsigned int child_id, void *hook_proc,
1320 const WCHAR *module, size_t module_size,
1321 user_handle_t hook)
1323 struct message *msg;
1325 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1327 msg->type = MSG_WINEVENT;
1328 msg->win = get_user_full_handle( win );
1329 msg->msg = event;
1330 msg->wparam = object_id;
1331 msg->lparam = child_id;
1332 msg->time = get_tick_count();
1333 msg->x = 0;
1334 msg->y = 0;
1335 msg->info = get_thread_id( current );
1336 msg->result = NULL;
1337 msg->hook = hook;
1338 msg->hook_proc = hook_proc;
1340 if ((msg->data = malloc( module_size )))
1342 msg->data_size = module_size;
1343 memcpy( msg->data, module, module_size );
1345 if (debug_level > 1)
1346 fprintf( stderr, "post_win_event: tid %04x event %04x win %p object_id %d child_id %d\n",
1347 get_thread_id(thread), event, win, object_id, child_id );
1348 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1349 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1351 else
1352 free( msg );
1356 /* get the message queue of the current thread */
1357 DECL_HANDLER(get_msg_queue)
1359 struct msg_queue *queue = get_current_queue();
1361 reply->handle = 0;
1362 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1366 /* set the current message queue wakeup mask */
1367 DECL_HANDLER(set_queue_mask)
1369 struct msg_queue *queue = get_current_queue();
1371 if (queue)
1373 queue->wake_mask = req->wake_mask;
1374 queue->changed_mask = req->changed_mask;
1375 reply->wake_bits = queue->wake_bits;
1376 reply->changed_bits = queue->changed_bits;
1377 if (is_signaled( queue ))
1379 /* if skip wait is set, do what would have been done in the subsequent wait */
1380 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1381 else wake_up( &queue->obj, 0 );
1387 /* get the current message queue status */
1388 DECL_HANDLER(get_queue_status)
1390 struct msg_queue *queue = current->queue;
1391 if (queue)
1393 reply->wake_bits = queue->wake_bits;
1394 reply->changed_bits = queue->changed_bits;
1395 if (req->clear) queue->changed_bits = 0;
1397 else reply->wake_bits = reply->changed_bits = 0;
1401 /* send a message to a thread queue */
1402 DECL_HANDLER(send_message)
1404 struct message *msg;
1405 struct msg_queue *send_queue = get_current_queue();
1406 struct msg_queue *recv_queue = NULL;
1407 struct thread *thread = NULL;
1409 if (req->id)
1411 if (!(thread = get_thread_from_id( req->id ))) return;
1413 else if (req->type != MSG_HARDWARE)
1415 /* only hardware messages are allowed without destination thread */
1416 set_error( STATUS_INVALID_PARAMETER );
1417 return;
1420 if (thread && !(recv_queue = thread->queue))
1422 set_error( STATUS_INVALID_PARAMETER );
1423 release_object( thread );
1424 return;
1426 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1428 set_error( STATUS_TIMEOUT );
1429 release_object( thread );
1430 return;
1433 if ((msg = mem_alloc( sizeof(*msg) )))
1435 msg->type = req->type;
1436 msg->win = get_user_full_handle( req->win );
1437 msg->msg = req->msg;
1438 msg->wparam = req->wparam;
1439 msg->lparam = req->lparam;
1440 msg->time = req->time;
1441 msg->x = req->x;
1442 msg->y = req->y;
1443 msg->info = req->info;
1444 msg->hook = 0;
1445 msg->hook_proc = NULL;
1446 msg->result = NULL;
1447 msg->data = NULL;
1448 msg->data_size = 0;
1450 switch(msg->type)
1452 case MSG_OTHER_PROCESS:
1453 msg->data_size = get_req_data_size();
1454 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1456 free( msg );
1457 break;
1459 /* fall through */
1460 case MSG_ASCII:
1461 case MSG_UNICODE:
1462 case MSG_CALLBACK:
1463 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1464 req->timeout, req->callback, req->info )))
1466 free_message( msg );
1467 break;
1469 /* fall through */
1470 case MSG_NOTIFY:
1471 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
1472 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1473 break;
1474 case MSG_POSTED:
1475 /* needed for posted DDE messages */
1476 msg->data_size = get_req_data_size();
1477 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1479 free( msg );
1480 break;
1482 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
1483 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1484 break;
1485 case MSG_HARDWARE:
1486 queue_hardware_message( recv_queue, msg );
1487 break;
1488 case MSG_CALLBACK_RESULT: /* cannot send this one */
1489 default:
1490 set_error( STATUS_INVALID_PARAMETER );
1491 free( msg );
1492 break;
1495 if (thread) release_object( thread );
1499 /* get a message from the current queue */
1500 DECL_HANDLER(get_message)
1502 struct timer *timer;
1503 struct list *ptr;
1504 struct message *first_hw_msg = NULL;
1505 struct msg_queue *queue = get_current_queue();
1506 user_handle_t get_win = get_user_full_handle( req->get_win );
1508 if (!queue) return;
1509 gettimeofday( &queue->last_get_msg, NULL );
1511 /* first of all release the hardware input lock if we own it */
1512 /* we'll grab it again if we find a hardware message */
1513 if (queue->input->msg_thread == current && req->get_next_hw)
1515 first_hw_msg = queue->input->msg;
1516 release_hardware_message( current, 0 );
1519 /* first check for sent messages */
1520 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
1522 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1523 receive_message( queue, msg, reply );
1524 return;
1526 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1528 /* clear changed bits so we can wait on them if we don't find a message */
1529 queue->changed_bits = 0;
1531 /* then check for posted messages */
1532 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1533 return;
1535 /* then check for any raw hardware message */
1536 if (filter_contains_hw_range( req->get_first, req->get_last ) &&
1537 get_hardware_message( current, req->get_next_hw, first_hw_msg, get_win, reply ))
1538 return;
1540 /* now check for WM_PAINT */
1541 if (queue->paint_count &&
1542 (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
1543 (reply->win = find_window_to_repaint( get_win, current )))
1545 reply->type = MSG_POSTED;
1546 reply->msg = WM_PAINT;
1547 reply->wparam = 0;
1548 reply->lparam = 0;
1549 reply->x = 0;
1550 reply->y = 0;
1551 reply->time = get_tick_count();
1552 reply->info = 0;
1553 return;
1556 /* now check for timer */
1557 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1558 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1560 reply->type = MSG_POSTED;
1561 reply->win = timer->win;
1562 reply->msg = timer->msg;
1563 reply->wparam = timer->id;
1564 reply->lparam = timer->lparam;
1565 reply->x = 0;
1566 reply->y = 0;
1567 reply->time = get_tick_count();
1568 reply->info = 0;
1569 return;
1572 done:
1573 set_error( STATUS_PENDING ); /* FIXME */
1577 /* reply to a sent message */
1578 DECL_HANDLER(reply_message)
1580 if (!current->queue)
1582 set_error( STATUS_ACCESS_DENIED );
1583 return;
1585 if (req->type == MSG_HARDWARE)
1587 struct thread_input *input = current->queue->input;
1588 if (input->msg_thread == current) release_hardware_message( current, req->remove );
1589 else set_error( STATUS_ACCESS_DENIED );
1591 else if (current->queue->recv_result)
1592 reply_message( current->queue, req->result, 0, req->remove,
1593 get_req_data(), get_req_data_size() );
1597 /* retrieve the reply for the last message sent */
1598 DECL_HANDLER(get_message_reply)
1600 struct message_result *result;
1601 struct list *entry;
1602 struct msg_queue *queue = current->queue;
1604 if (queue)
1606 set_error( STATUS_PENDING );
1607 reply->result = 0;
1609 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1611 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1612 if (result->replied || req->cancel)
1614 if (result->replied)
1616 reply->result = result->result;
1617 set_error( result->error );
1618 if (result->data)
1620 size_t data_len = min( result->data_size, get_reply_max_size() );
1621 set_reply_data_ptr( result->data, data_len );
1622 result->data = NULL;
1623 result->data_size = 0;
1626 remove_result_from_sender( result );
1628 entry = list_head( &queue->send_result );
1629 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1630 else
1632 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1633 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1637 else set_error( STATUS_ACCESS_DENIED );
1641 /* set a window timer */
1642 DECL_HANDLER(set_win_timer)
1644 struct timer *timer;
1645 struct msg_queue *queue;
1646 struct thread *thread = NULL;
1647 user_handle_t win = 0;
1648 unsigned int id = req->id;
1650 if (req->win)
1652 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1654 set_error( STATUS_INVALID_HANDLE );
1655 return;
1657 if (thread->process != current->process)
1659 release_object( thread );
1660 set_error( STATUS_ACCESS_DENIED );
1661 return;
1663 queue = thread->queue;
1664 /* remove it if it existed already */
1665 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
1667 else
1669 queue = get_current_queue();
1670 /* find a free id for it */
1673 id = queue->next_timer_id;
1674 if (++queue->next_timer_id >= 0x10000) queue->next_timer_id = 1;
1676 while (find_timer( queue, 0, req->msg, id ));
1679 if ((timer = set_timer( queue, req->rate )))
1681 timer->win = win;
1682 timer->msg = req->msg;
1683 timer->id = id;
1684 timer->lparam = req->lparam;
1685 reply->id = id;
1687 if (thread) release_object( thread );
1690 /* kill a window timer */
1691 DECL_HANDLER(kill_win_timer)
1693 struct timer *timer;
1694 struct thread *thread;
1695 user_handle_t win = 0;
1697 if (req->win)
1699 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
1701 set_error( STATUS_INVALID_HANDLE );
1702 return;
1704 if (thread->process != current->process)
1706 release_object( thread );
1707 set_error( STATUS_ACCESS_DENIED );
1708 return;
1711 else thread = (struct thread *)grab_object( current );
1713 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
1714 free_timer( thread->queue, timer );
1715 else
1716 set_error( STATUS_INVALID_PARAMETER );
1718 release_object( thread );
1722 /* attach (or detach) thread inputs */
1723 DECL_HANDLER(attach_thread_input)
1725 struct thread *thread_from = get_thread_from_id( req->tid_from );
1726 struct thread *thread_to = get_thread_from_id( req->tid_to );
1728 if (!thread_from || !thread_to)
1730 if (thread_from) release_object( thread_from );
1731 if (thread_to) release_object( thread_to );
1732 return;
1734 if (thread_from != thread_to)
1736 if (req->attach) attach_thread_input( thread_from, thread_to );
1737 else detach_thread_input( thread_from, thread_to );
1739 else set_error( STATUS_ACCESS_DENIED );
1740 release_object( thread_from );
1741 release_object( thread_to );
1745 /* get thread input data */
1746 DECL_HANDLER(get_thread_input)
1748 struct thread *thread = NULL;
1749 struct thread_input *input;
1751 if (req->tid)
1753 if (!(thread = get_thread_from_id( req->tid ))) return;
1754 input = thread->queue ? thread->queue->input : NULL;
1756 else input = foreground_input; /* get the foreground thread info */
1758 if (input)
1760 reply->focus = input->focus;
1761 reply->capture = input->capture;
1762 reply->active = input->active;
1763 reply->menu_owner = input->menu_owner;
1764 reply->move_size = input->move_size;
1765 reply->caret = input->caret;
1766 reply->rect = input->caret_rect;
1768 else
1770 reply->focus = 0;
1771 reply->capture = 0;
1772 reply->active = 0;
1773 reply->menu_owner = 0;
1774 reply->move_size = 0;
1775 reply->caret = 0;
1776 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1778 /* foreground window is active window of foreground thread */
1779 reply->foreground = foreground_input ? foreground_input->active : 0;
1780 if (thread) release_object( thread );
1784 /* retrieve queue keyboard state for a given thread */
1785 DECL_HANDLER(get_key_state)
1787 struct thread *thread;
1788 struct thread_input *input;
1790 if (!(thread = get_thread_from_id( req->tid ))) return;
1791 input = thread->queue ? thread->queue->input : NULL;
1792 if (input)
1794 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1795 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1797 release_object( thread );
1801 /* set queue keyboard state for a given thread */
1802 DECL_HANDLER(set_key_state)
1804 struct thread *thread = NULL;
1805 struct thread_input *input;
1807 if (!(thread = get_thread_from_id( req->tid ))) return;
1808 input = thread->queue ? thread->queue->input : NULL;
1809 if (input)
1811 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1812 if (size) memcpy( input->keystate, get_req_data(), size );
1814 release_object( thread );
1818 /* set the system foreground window */
1819 DECL_HANDLER(set_foreground_window)
1821 struct msg_queue *queue = get_current_queue();
1823 reply->previous = foreground_input ? foreground_input->active : 0;
1824 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1825 reply->send_msg_new = FALSE;
1827 if (req->handle)
1829 struct thread *thread;
1831 if (is_top_level_window( req->handle ) &&
1832 ((thread = get_window_thread( req->handle ))))
1834 foreground_input = thread->queue->input;
1835 reply->send_msg_new = (foreground_input != queue->input);
1836 release_object( thread );
1838 else set_error( STATUS_INVALID_HANDLE );
1840 else foreground_input = NULL;
1844 /* set the current thread focus window */
1845 DECL_HANDLER(set_focus_window)
1847 struct msg_queue *queue = get_current_queue();
1849 reply->previous = 0;
1850 if (queue && check_queue_input_window( queue, req->handle ))
1852 reply->previous = queue->input->focus;
1853 queue->input->focus = get_user_full_handle( req->handle );
1858 /* set the current thread active window */
1859 DECL_HANDLER(set_active_window)
1861 struct msg_queue *queue = get_current_queue();
1863 reply->previous = 0;
1864 if (queue && check_queue_input_window( queue, req->handle ))
1866 if (!req->handle || make_window_active( req->handle ))
1868 reply->previous = queue->input->active;
1869 queue->input->active = get_user_full_handle( req->handle );
1871 else set_error( STATUS_INVALID_HANDLE );
1876 /* set the current thread capture window */
1877 DECL_HANDLER(set_capture_window)
1879 struct msg_queue *queue = get_current_queue();
1881 reply->previous = reply->full_handle = 0;
1882 if (queue && check_queue_input_window( queue, req->handle ))
1884 struct thread_input *input = queue->input;
1886 reply->previous = input->capture;
1887 input->capture = get_user_full_handle( req->handle );
1888 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1889 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1890 reply->full_handle = input->capture;
1895 /* Set the current thread caret window */
1896 DECL_HANDLER(set_caret_window)
1898 struct msg_queue *queue = get_current_queue();
1900 reply->previous = 0;
1901 if (queue && check_queue_input_window( queue, req->handle ))
1903 struct thread_input *input = queue->input;
1905 reply->previous = input->caret;
1906 reply->old_rect = input->caret_rect;
1907 reply->old_hide = input->caret_hide;
1908 reply->old_state = input->caret_state;
1910 set_caret_window( input, get_user_full_handle(req->handle) );
1911 input->caret_rect.right = input->caret_rect.left + req->width;
1912 input->caret_rect.bottom = input->caret_rect.top + req->height;
1917 /* Set the current thread caret information */
1918 DECL_HANDLER(set_caret_info)
1920 struct msg_queue *queue = get_current_queue();
1921 struct thread_input *input;
1923 if (!queue) return;
1924 input = queue->input;
1925 reply->full_handle = input->caret;
1926 reply->old_rect = input->caret_rect;
1927 reply->old_hide = input->caret_hide;
1928 reply->old_state = input->caret_state;
1930 if (req->handle && get_user_full_handle(req->handle) != input->caret)
1932 set_error( STATUS_ACCESS_DENIED );
1933 return;
1935 if (req->flags & SET_CARET_POS)
1937 input->caret_rect.right += req->x - input->caret_rect.left;
1938 input->caret_rect.bottom += req->y - input->caret_rect.top;
1939 input->caret_rect.left = req->x;
1940 input->caret_rect.top = req->y;
1942 if (req->flags & SET_CARET_HIDE)
1944 input->caret_hide += req->hide;
1945 if (input->caret_hide < 0) input->caret_hide = 0;
1947 if (req->flags & SET_CARET_STATE)
1949 if (req->state == -1) input->caret_state = !input->caret_state;
1950 else input->caret_state = !!req->state;