Reimplemented SearchPathW using ntdll functions.
[wine/multimedia.git] / server / queue.c
blob4868a23310fa0d738a015c64fa7e2b262737d7ce
1 /*
2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
34 #include "handle.h"
35 #include "file.h"
36 #include "thread.h"
37 #include "process.h"
38 #include "request.h"
39 #include "user.h"
41 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
42 #define NB_MSG_KINDS (POST_MESSAGE+1)
45 struct message_result
47 struct list sender_entry; /* entry in sender list */
48 struct message_result *recv_next; /* next in receiver list */
49 struct msg_queue *sender; /* sender queue */
50 struct msg_queue *receiver; /* receiver queue */
51 int replied; /* has it been replied to? */
52 unsigned int result; /* reply result */
53 unsigned int error; /* error code to pass back to sender */
54 struct message *callback_msg; /* message to queue for callback */
55 void *data; /* message reply data */
56 unsigned int data_size; /* size of message reply data */
57 struct timeout_user *timeout; /* result timeout */
60 struct message
62 struct message *next; /* next message in list */
63 struct message *prev; /* prev message in list */
64 enum message_type type; /* message type */
65 user_handle_t win; /* window handle */
66 unsigned int msg; /* message code */
67 unsigned int wparam; /* parameters */
68 unsigned int lparam; /* parameters */
69 int x; /* x position */
70 int y; /* y position */
71 unsigned int time; /* message time */
72 unsigned int info; /* extra info */
73 void *data; /* message data for sent messages */
74 unsigned int data_size; /* size of message data */
75 struct message_result *result; /* result in sender queue */
78 struct message_list
80 struct message *first; /* head of list */
81 struct message *last; /* tail of list */
84 struct timer
86 struct timer *next; /* next timer in list */
87 struct timer *prev; /* prev timer in list */
88 struct timeval when; /* next expiration */
89 unsigned int rate; /* timer rate in ms */
90 user_handle_t win; /* window handle */
91 unsigned int msg; /* message to post */
92 unsigned int id; /* timer id */
93 unsigned int lparam; /* lparam for message */
96 struct thread_input
98 struct object obj; /* object header */
99 user_handle_t focus; /* focus window */
100 user_handle_t capture; /* capture window */
101 user_handle_t active; /* active window */
102 user_handle_t menu_owner; /* current menu owner window */
103 user_handle_t move_size; /* current moving/resizing window */
104 user_handle_t caret; /* caret window */
105 rectangle_t caret_rect; /* caret rectangle */
106 int caret_hide; /* caret hide count */
107 int caret_state; /* caret on/off state */
108 struct message *msg; /* message currently processed */
109 struct thread *msg_thread; /* thread processing the message */
110 struct message_list msg_list; /* list of hardware messages */
111 unsigned char keystate[256]; /* state of each key */
114 struct msg_queue
116 struct object obj; /* object header */
117 unsigned int wake_bits; /* wakeup bits */
118 unsigned int wake_mask; /* wakeup mask */
119 unsigned int changed_bits; /* changed wakeup bits */
120 unsigned int changed_mask; /* changed wakeup mask */
121 int paint_count; /* pending paint messages count */
122 struct message_list msg_list[NB_MSG_KINDS]; /* lists of messages */
123 struct list send_result; /* stack of sent messages waiting for result */
124 struct list callback_result; /* list of callback messages waiting for result */
125 struct message_result *recv_result; /* stack of received messages waiting for result */
126 struct timer *first_timer; /* head of timer list */
127 struct timer *last_timer; /* tail of timer list */
128 struct timer *next_timer; /* next timer to expire */
129 struct timeout_user *timeout; /* timeout for next timer to expire */
130 struct thread_input *input; /* thread input descriptor */
131 struct hook_table *hooks; /* hook table */
132 struct timeval last_get_msg; /* time of last get message call */
135 static void msg_queue_dump( struct object *obj, int verbose );
136 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
137 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
138 static int msg_queue_signaled( struct object *obj, struct thread *thread );
139 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
140 static void msg_queue_destroy( struct object *obj );
141 static void thread_input_dump( struct object *obj, int verbose );
142 static void thread_input_destroy( struct object *obj );
143 static void timer_callback( void *private );
145 static const struct object_ops msg_queue_ops =
147 sizeof(struct msg_queue), /* size */
148 msg_queue_dump, /* dump */
149 msg_queue_add_queue, /* add_queue */
150 msg_queue_remove_queue, /* remove_queue */
151 msg_queue_signaled, /* signaled */
152 msg_queue_satisfied, /* satisfied */
153 no_get_fd, /* get_fd */
154 msg_queue_destroy /* destroy */
158 static const struct object_ops thread_input_ops =
160 sizeof(struct thread_input), /* size */
161 thread_input_dump, /* dump */
162 no_add_queue, /* add_queue */
163 NULL, /* remove_queue */
164 NULL, /* signaled */
165 NULL, /* satisfied */
166 no_get_fd, /* get_fd */
167 thread_input_destroy /* destroy */
170 /* pointer to input structure of foreground thread */
171 static struct thread_input *foreground_input;
174 /* set the caret window in a given thread input */
175 static void set_caret_window( struct thread_input *input, user_handle_t win )
177 input->caret = win;
178 input->caret_rect.left = 0;
179 input->caret_rect.top = 0;
180 input->caret_rect.right = 0;
181 input->caret_rect.bottom = 0;
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 input->msg_list.first = input->msg_list.last = NULL;
201 set_caret_window( input, 0 );
202 memset( input->keystate, 0, sizeof(input->keystate) );
204 return input;
207 /* create a message queue object */
208 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
210 struct msg_queue *queue;
211 int i;
213 if (!input && !(input = create_thread_input())) return NULL;
214 if ((queue = alloc_object( &msg_queue_ops )))
216 queue->wake_bits = 0;
217 queue->wake_mask = 0;
218 queue->changed_bits = 0;
219 queue->changed_mask = 0;
220 queue->paint_count = 0;
221 queue->recv_result = NULL;
222 queue->first_timer = NULL;
223 queue->last_timer = NULL;
224 queue->next_timer = NULL;
225 queue->timeout = NULL;
226 queue->input = (struct thread_input *)grab_object( input );
227 queue->hooks = NULL;
228 gettimeofday( &queue->last_get_msg, NULL );
229 list_init( &queue->send_result );
230 list_init( &queue->callback_result );
231 for (i = 0; i < NB_MSG_KINDS; i++)
232 queue->msg_list[i].first = queue->msg_list[i].last = NULL;
234 thread->queue = queue;
235 if (!thread->process->queue)
236 thread->process->queue = (struct msg_queue *)grab_object( queue );
238 release_object( input );
239 return queue;
242 /* free the message queue of a thread at thread exit */
243 void free_msg_queue( struct thread *thread )
245 struct process *process = thread->process;
247 remove_thread_hooks( thread );
248 if (!thread->queue) return;
249 if (process->queue == thread->queue) /* is it the process main queue? */
251 release_object( process->queue );
252 process->queue = NULL;
253 if (process->idle_event)
255 set_event( process->idle_event );
256 release_object( process->idle_event );
257 process->idle_event = NULL;
260 release_object( thread->queue );
261 thread->queue = NULL;
264 /* get the hook table for a given thread */
265 struct hook_table *get_queue_hooks( struct thread *thread )
267 if (!thread->queue) return NULL;
268 return thread->queue->hooks;
271 /* set the hook table for a given thread, allocating the queue if needed */
272 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
274 struct msg_queue *queue = thread->queue;
275 if (!queue) queue = create_msg_queue( thread, NULL );
276 if (queue->hooks) release_object( queue->hooks );
277 queue->hooks = hooks;
280 /* check the queue status */
281 inline static int is_signaled( struct msg_queue *queue )
283 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
286 /* set some queue bits */
287 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
289 queue->wake_bits |= bits;
290 queue->changed_bits |= bits;
291 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
294 /* clear some queue bits */
295 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
297 queue->wake_bits &= ~bits;
298 queue->changed_bits &= ~bits;
301 /* check whether msg is a keyboard message */
302 inline static int is_keyboard_msg( struct message *msg )
304 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
307 /* get the QS_* bit corresponding to a given hardware message */
308 inline static int get_hardware_msg_bit( struct message *msg )
310 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
311 if (is_keyboard_msg( msg )) return QS_KEY;
312 return QS_MOUSEBUTTON;
315 /* get the current thread queue, creating it if needed */
316 inline static struct msg_queue *get_current_queue(void)
318 struct msg_queue *queue = current->queue;
319 if (!queue) queue = create_msg_queue( current, NULL );
320 return queue;
323 /* append a message to the end of a list */
324 inline static void append_message( struct message_list *list, struct message *msg )
326 msg->next = NULL;
327 if ((msg->prev = list->last)) msg->prev->next = msg;
328 else list->first = msg;
329 list->last = msg;
332 /* unlink a message from a list it */
333 inline static void unlink_message( struct message_list *list, struct message *msg )
335 if (msg->next) msg->next->prev = msg->prev;
336 else list->last = msg->prev;
337 if (msg->prev) msg->prev->next = msg->next;
338 else list->first = msg->next;
341 /* try to merge a message with the last in the list; return 1 if successful */
342 static int merge_message( struct thread_input *input, const struct message *msg )
344 struct message *prev = input->msg_list.last;
346 if (!prev) return 0;
347 if (input->msg == prev) return 0;
348 if (prev->result) return 0;
349 if (prev->win != msg->win) return 0;
350 if (prev->msg != msg->msg) return 0;
351 if (prev->type != msg->type) return 0;
352 /* now we can merge it */
353 prev->wparam = msg->wparam;
354 prev->lparam = msg->lparam;
355 prev->x = msg->x;
356 prev->y = msg->y;
357 prev->time = msg->time;
358 prev->info = msg->info;
359 return 1;
362 /* free a result structure */
363 static void free_result( struct message_result *result )
365 if (result->timeout) remove_timeout_user( result->timeout );
366 if (result->data) free( result->data );
367 if (result->callback_msg) free( result->callback_msg );
368 free( result );
371 /* remove the result from the sender list it is on */
372 static inline void remove_result_from_sender( struct message_result *result )
374 assert( result->sender );
376 list_remove( &result->sender_entry );
377 result->sender = NULL;
378 if (!result->receiver) free_result( result );
381 /* store the message result in the appropriate structure */
382 static void store_message_result( struct message_result *res, unsigned int result,
383 unsigned int error )
385 res->result = result;
386 res->error = error;
387 res->replied = 1;
388 if (res->timeout)
390 remove_timeout_user( res->timeout );
391 res->timeout = NULL;
393 if (res->sender)
395 if (res->callback_msg)
397 /* queue the callback message in the sender queue */
398 res->callback_msg->lparam = result;
399 append_message( &res->sender->msg_list[SEND_MESSAGE], res->callback_msg );
400 set_queue_bits( res->sender, QS_SENDMESSAGE );
401 res->callback_msg = NULL;
402 remove_result_from_sender( res );
404 else
406 /* wake sender queue if waiting on this result */
407 if (list_head(&res->sender->send_result) == &res->sender_entry)
408 set_queue_bits( res->sender, QS_SMRESULT );
414 /* free a message when deleting a queue or window */
415 static void free_message( struct message *msg )
417 struct message_result *result = msg->result;
418 if (result)
420 if (result->sender)
422 result->receiver = NULL;
423 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
425 else free_result( result );
427 if (msg->data) free( msg->data );
428 free( msg );
431 /* remove (and free) a message from a message list */
432 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
433 enum message_kind kind )
435 unlink_message( &queue->msg_list[kind], msg );
436 switch(kind)
438 case SEND_MESSAGE:
439 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_SENDMESSAGE );
440 break;
441 case POST_MESSAGE:
442 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_POSTMESSAGE );
443 break;
445 free_message( msg );
448 /* message timed out without getting a reply */
449 static void result_timeout( void *private )
451 struct message_result *result = private;
453 assert( !result->replied );
455 result->timeout = NULL;
456 store_message_result( result, 0, STATUS_TIMEOUT );
459 /* allocate and fill a message result structure */
460 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
461 struct msg_queue *recv_queue,
462 struct message *msg, unsigned int timeout,
463 void *callback, unsigned int callback_data )
465 struct message_result *result = mem_alloc( sizeof(*result) );
466 if (result)
468 result->sender = send_queue;
469 result->receiver = recv_queue;
470 result->replied = 0;
471 result->data = NULL;
472 result->data_size = 0;
473 result->timeout = NULL;
475 if (msg->type == MSG_CALLBACK)
477 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
478 if (!callback_msg)
480 free( result );
481 return NULL;
483 callback_msg->type = MSG_CALLBACK_RESULT;
484 callback_msg->win = msg->win;
485 callback_msg->msg = msg->msg;
486 callback_msg->wparam = (unsigned int)callback;
487 callback_msg->lparam = 0;
488 callback_msg->time = get_tick_count();
489 callback_msg->x = 0;
490 callback_msg->y = 0;
491 callback_msg->info = callback_data;
492 callback_msg->result = NULL;
493 callback_msg->data = NULL;
494 callback_msg->data_size = 0;
496 result->callback_msg = callback_msg;
497 list_add_head( &send_queue->callback_result, &result->sender_entry );
499 else
501 result->callback_msg = NULL;
502 list_add_head( &send_queue->send_result, &result->sender_entry );
505 if (timeout != -1)
507 struct timeval when;
508 gettimeofday( &when, 0 );
509 add_timeout( &when, timeout );
510 result->timeout = add_timeout_user( &when, result_timeout, result );
513 return result;
516 /* receive a message, removing it from the sent queue */
517 static void receive_message( struct msg_queue *queue, struct message *msg,
518 struct get_message_reply *reply )
520 struct message_result *result = msg->result;
522 reply->total = msg->data_size;
523 if (msg->data_size > get_reply_max_size())
525 set_error( STATUS_BUFFER_OVERFLOW );
526 return;
528 reply->type = msg->type;
529 reply->win = msg->win;
530 reply->msg = msg->msg;
531 reply->wparam = msg->wparam;
532 reply->lparam = msg->lparam;
533 reply->x = msg->x;
534 reply->y = msg->y;
535 reply->time = msg->time;
536 reply->info = msg->info;
538 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
540 unlink_message( &queue->msg_list[SEND_MESSAGE], msg );
541 /* put the result on the receiver result stack */
542 if (result)
544 result->recv_next = queue->recv_result;
545 queue->recv_result = result;
547 free( msg );
548 if (!queue->msg_list[SEND_MESSAGE].first) clear_queue_bits( queue, QS_SENDMESSAGE );
551 /* set the result of the current received message */
552 static void reply_message( struct msg_queue *queue, unsigned int result,
553 unsigned int error, int remove, const void *data, size_t len )
555 struct message_result *res = queue->recv_result;
557 if (remove)
559 queue->recv_result = res->recv_next;
560 res->receiver = NULL;
561 if (!res->sender) /* no one waiting for it */
563 free_result( res );
564 return;
567 if (!res->replied)
569 if (len && (res->data = memdup( data, len ))) res->data_size = len;
570 store_message_result( res, result, error );
574 /* retrieve a posted message */
575 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
576 unsigned int first, unsigned int last, unsigned int flags,
577 struct get_message_reply *reply )
579 struct message *msg;
580 struct message_list *list = &queue->msg_list[POST_MESSAGE];
582 /* check against the filters */
583 for (msg = list->first; msg; msg = msg->next)
585 if (msg->msg == WM_QUIT) break; /* WM_QUIT is never filtered */
586 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
587 if (msg->msg < first) continue;
588 if (msg->msg > last) continue;
589 break; /* found one */
591 if (!msg) return 0;
593 /* return it to the app */
595 reply->total = msg->data_size;
596 if (msg->data_size > get_reply_max_size())
598 set_error( STATUS_BUFFER_OVERFLOW );
599 return 1;
601 reply->type = msg->type;
602 reply->win = msg->win;
603 reply->msg = msg->msg;
604 reply->wparam = msg->wparam;
605 reply->lparam = msg->lparam;
606 reply->x = msg->x;
607 reply->y = msg->y;
608 reply->time = msg->time;
609 reply->info = msg->info;
611 if (flags & GET_MSG_REMOVE)
613 if (msg->data)
615 set_reply_data_ptr( msg->data, msg->data_size );
616 msg->data = NULL;
617 msg->data_size = 0;
619 remove_queue_message( queue, msg, POST_MESSAGE );
621 else if (msg->data) set_reply_data( msg->data, msg->data_size );
623 return 1;
626 /* empty a message list and free all the messages */
627 static void empty_msg_list( struct message_list *list )
629 struct message *msg = list->first;
630 while (msg)
632 struct message *next = msg->next;
633 free_message( msg );
634 msg = next;
638 /* cleanup all pending results when deleting a queue */
639 static void cleanup_results( struct msg_queue *queue )
641 struct list *entry;
643 while ((entry = list_head( &queue->send_result )) != NULL)
645 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
648 while ((entry = list_head( &queue->callback_result )) != NULL)
650 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
653 while (queue->recv_result)
654 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
657 /* check if the thread owning the queue is hung (not checking for messages) */
658 static int is_queue_hung( struct msg_queue *queue )
660 struct timeval now;
661 struct wait_queue_entry *entry;
663 gettimeofday( &now, NULL );
664 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
665 return 0; /* less than 5 seconds since last get message -> not hung */
667 for (entry = queue->obj.head; entry; entry = entry->next)
669 if (entry->thread->queue == queue)
670 return 0; /* thread is waiting on queue -> not hung */
672 return 1;
675 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
677 struct msg_queue *queue = (struct msg_queue *)obj;
678 struct process *process = entry->thread->process;
680 /* a thread can only wait on its own queue */
681 if (entry->thread->queue != queue)
683 set_error( STATUS_ACCESS_DENIED );
684 return 0;
686 /* if waiting on the main process queue, set the idle event */
687 if (process->queue == queue)
689 if (process->idle_event) set_event( process->idle_event );
691 add_queue( obj, entry );
692 return 1;
695 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
697 struct msg_queue *queue = (struct msg_queue *)obj;
698 struct process *process = entry->thread->process;
700 remove_queue( obj, entry );
702 assert( entry->thread->queue == queue );
704 /* if waiting on the main process queue, reset the idle event */
705 if (process->queue == queue)
707 if (process->idle_event) reset_event( process->idle_event );
711 static void msg_queue_dump( struct object *obj, int verbose )
713 struct msg_queue *queue = (struct msg_queue *)obj;
714 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
715 queue->wake_bits, queue->wake_mask );
718 static int msg_queue_signaled( struct object *obj, struct thread *thread )
720 struct msg_queue *queue = (struct msg_queue *)obj;
721 return is_signaled( queue );
724 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
726 struct msg_queue *queue = (struct msg_queue *)obj;
727 queue->wake_mask = 0;
728 queue->changed_mask = 0;
729 return 0; /* Not abandoned */
732 static void msg_queue_destroy( struct object *obj )
734 struct msg_queue *queue = (struct msg_queue *)obj;
735 struct timer *timer = queue->first_timer;
736 int i;
738 cleanup_results( queue );
739 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
741 while (timer)
743 struct timer *next = timer->next;
744 free( timer );
745 timer = next;
747 if (queue->timeout) remove_timeout_user( queue->timeout );
748 if (queue->input) release_object( queue->input );
749 if (queue->hooks) release_object( queue->hooks );
752 static void thread_input_dump( struct object *obj, int verbose )
754 struct thread_input *input = (struct thread_input *)obj;
755 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
756 input->focus, input->capture, input->active );
759 static void thread_input_destroy( struct object *obj )
761 struct thread_input *input = (struct thread_input *)obj;
763 if (foreground_input == input) foreground_input = NULL;
764 if (input->msg_thread) release_object( input->msg_thread );
765 empty_msg_list( &input->msg_list );
768 /* fix the thread input data when a window is destroyed */
769 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
771 struct thread_input *input = queue->input;
773 if (window == input->focus) input->focus = 0;
774 if (window == input->capture) input->capture = 0;
775 if (window == input->active) input->active = 0;
776 if (window == input->menu_owner) input->menu_owner = 0;
777 if (window == input->move_size) input->move_size = 0;
778 if (window == input->caret) set_caret_window( input, 0 );
781 /* check if the specified window can be set in the input data of a given queue */
782 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
784 struct thread *thread;
785 int ret = 0;
787 if (!window) return 1; /* we can always clear the data */
789 if ((thread = get_window_thread( window )))
791 ret = (queue->input == thread->queue->input);
792 if (!ret) set_error( STATUS_ACCESS_DENIED );
793 release_object( thread );
795 else set_error( STATUS_INVALID_HANDLE );
797 return ret;
800 /* attach two thread input data structures */
801 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
803 struct thread_input *input;
805 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
806 input = (struct thread_input *)grab_object( thread_to->queue->input );
808 if (thread_from->queue)
810 release_object( thread_from->queue->input );
811 thread_from->queue->input = input;
813 else
815 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
817 memset( input->keystate, 0, sizeof(input->keystate) );
818 return 1;
821 /* detach two thread input data structures */
822 static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
824 struct thread_input *input;
826 if (!thread_from->queue || !thread_to->queue ||
827 thread_from->queue->input != thread_to->queue->input)
829 set_error( STATUS_ACCESS_DENIED );
830 return;
832 if ((input = create_thread_input()))
834 release_object( thread_from->queue->input );
835 thread_from->queue->input = input;
840 /* set the next timer to expire */
841 static void set_next_timer( struct msg_queue *queue, struct timer *timer )
843 if (queue->timeout)
845 remove_timeout_user( queue->timeout );
846 queue->timeout = NULL;
848 if ((queue->next_timer = timer))
849 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
851 /* set/clear QS_TIMER bit */
852 if (queue->next_timer == queue->first_timer)
853 clear_queue_bits( queue, QS_TIMER );
854 else
855 set_queue_bits( queue, QS_TIMER );
858 /* callback for the next timer expiration */
859 static void timer_callback( void *private )
861 struct msg_queue *queue = private;
863 queue->timeout = NULL;
864 /* move on to the next timer */
865 set_next_timer( queue, queue->next_timer->next );
868 /* link a timer at its rightful place in the queue list */
869 static void link_timer( struct msg_queue *queue, struct timer *timer )
871 struct timer *pos = queue->next_timer;
873 while (pos && time_before( &pos->when, &timer->when )) pos = pos->next;
875 if (pos) /* insert before pos */
877 if ((timer->prev = pos->prev)) timer->prev->next = timer;
878 else queue->first_timer = timer;
879 timer->next = pos;
880 pos->prev = timer;
882 else /* insert at end */
884 timer->next = NULL;
885 timer->prev = queue->last_timer;
886 if (queue->last_timer) queue->last_timer->next = timer;
887 else queue->first_timer = timer;
888 queue->last_timer = timer;
890 /* check if we replaced the next timer */
891 if (pos == queue->next_timer) set_next_timer( queue, timer );
894 /* remove a timer from the queue timer list */
895 static void unlink_timer( struct msg_queue *queue, struct timer *timer )
897 if (timer->next) timer->next->prev = timer->prev;
898 else queue->last_timer = timer->prev;
899 if (timer->prev) timer->prev->next = timer->next;
900 else queue->first_timer = timer->next;
901 /* check if we removed the next timer */
902 if (queue->next_timer == timer) set_next_timer( queue, timer->next );
903 else if (queue->next_timer == queue->first_timer) clear_queue_bits( queue, QS_TIMER );
906 /* restart an expired timer */
907 static void restart_timer( struct msg_queue *queue, struct timer *timer )
909 struct timeval now;
910 unlink_timer( queue, timer );
911 gettimeofday( &now, 0 );
912 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
913 link_timer( queue, timer );
916 /* find an expired timer matching the filtering parameters */
917 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
918 unsigned int get_first, unsigned int get_last,
919 int remove )
921 struct timer *timer;
922 for (timer = queue->first_timer; (timer && timer != queue->next_timer); timer = timer->next)
924 if (win && timer->win != win) continue;
925 if (timer->msg >= get_first && timer->msg <= get_last)
927 if (remove) restart_timer( queue, timer );
928 return timer;
931 return NULL;
934 /* kill a timer */
935 static int kill_timer( struct msg_queue *queue, user_handle_t win,
936 unsigned int msg, unsigned int id )
938 struct timer *timer;
940 for (timer = queue->first_timer; timer; timer = timer->next)
942 if (timer->win != win || timer->msg != msg || timer->id != id) continue;
943 unlink_timer( queue, timer );
944 free( timer );
945 return 1;
947 return 0;
950 /* add a timer */
951 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
953 struct timer *timer = mem_alloc( sizeof(*timer) );
954 if (timer)
956 timer->rate = rate;
957 gettimeofday( &timer->when, 0 );
958 add_timeout( &timer->when, rate );
959 link_timer( queue, timer );
961 return timer;
964 /* change the input key state for a given key */
965 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
967 if (down)
969 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
970 input->keystate[key] |= 0x80;
972 else input->keystate[key] &= ~0x80;
975 /* update the input key state for a keyboard message */
976 static void update_input_key_state( struct thread_input *input, const struct message *msg )
978 unsigned char key;
979 int down = 0, extended;
981 switch (msg->msg)
983 case WM_LBUTTONDOWN:
984 down = 1;
985 /* fall through */
986 case WM_LBUTTONUP:
987 set_input_key_state( input, VK_LBUTTON, down );
988 break;
989 case WM_MBUTTONDOWN:
990 down = 1;
991 /* fall through */
992 case WM_MBUTTONUP:
993 set_input_key_state( input, VK_MBUTTON, down );
994 break;
995 case WM_RBUTTONDOWN:
996 down = 1;
997 /* fall through */
998 case WM_RBUTTONUP:
999 set_input_key_state( input, VK_RBUTTON, down );
1000 break;
1001 case WM_KEYDOWN:
1002 case WM_SYSKEYDOWN:
1003 down = 1;
1004 /* fall through */
1005 case WM_KEYUP:
1006 case WM_SYSKEYUP:
1007 key = (unsigned char)msg->wparam;
1008 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1009 set_input_key_state( input, key, down );
1010 switch(key)
1012 case VK_SHIFT:
1013 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1014 break;
1015 case VK_CONTROL:
1016 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1017 break;
1018 case VK_MENU:
1019 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1020 break;
1022 break;
1026 /* release the hardware message currently being processed by the given thread */
1027 static void release_hardware_message( struct thread *thread, int remove )
1029 struct thread_input *input = thread->queue->input;
1031 if (input->msg_thread != thread) return;
1032 if (remove)
1034 struct message *other;
1035 int clr_bit;
1037 update_input_key_state( input, input->msg );
1038 unlink_message( &input->msg_list, input->msg );
1039 clr_bit = get_hardware_msg_bit( input->msg );
1040 for (other = input->msg_list.first; other; other = other->next)
1041 if (get_hardware_msg_bit( other ) == clr_bit) break;
1042 if (!other) clear_queue_bits( thread->queue, clr_bit );
1043 free_message( input->msg );
1045 release_object( input->msg_thread );
1046 input->msg = NULL;
1047 input->msg_thread = NULL;
1050 /* find the window that should receive a given hardware message */
1051 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1052 unsigned int *msg_code )
1054 user_handle_t win = 0;
1056 *msg_code = msg->msg;
1057 if (is_keyboard_msg( msg ))
1059 if (input && !(win = input->focus))
1061 win = input->active;
1062 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1065 else /* mouse message */
1067 if (!input || !(win = input->capture))
1069 if (!(win = msg->win)) win = window_from_point( msg->x, msg->y );
1072 return win;
1075 /* queue a hardware message into a given thread input */
1076 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1078 user_handle_t win;
1079 struct thread *thread;
1080 struct thread_input *input;
1081 unsigned int msg_code;
1083 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1084 if (!win || !(thread = get_window_thread(win)))
1086 free( msg );
1087 return;
1089 input = thread->queue->input;
1091 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1092 else
1094 append_message( &input->msg_list, msg );
1095 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1097 release_object( thread );
1100 /* find a hardware message for the given queue */
1101 static int get_hardware_message( struct thread *thread, struct message *first,
1102 user_handle_t filter_win, struct get_message_reply *reply )
1104 struct thread_input *input = thread->queue->input;
1105 struct thread *win_thread;
1106 struct message *msg;
1107 user_handle_t win;
1108 int clear_bits, got_one = 0;
1109 unsigned int msg_code;
1111 if (input->msg_thread && input->msg_thread != thread)
1112 return 0; /* locked by another thread */
1114 if (!first)
1116 msg = input->msg_list.first;
1117 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1119 else
1121 msg = first->next;
1122 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1125 while (msg)
1127 win = find_hardware_message_window( input, msg, &msg_code );
1128 if (!win || !(win_thread = get_window_thread( win )))
1130 /* no window at all, remove it */
1131 struct message *next = msg->next;
1132 update_input_key_state( input, msg );
1133 unlink_message( &input->msg_list, msg );
1134 free_message( msg );
1135 msg = next;
1136 continue;
1138 if (win_thread != thread)
1140 /* wake the other thread */
1141 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1142 release_object( win_thread );
1143 got_one = 1;
1144 msg = msg->next;
1145 continue;
1147 /* if we already got a message for another thread, or if it doesn't
1148 * match the filter we skip it (filter is only checked for keyboard
1149 * messages since the dest window for a mouse message depends on hittest)
1151 if (got_one ||
1152 (filter_win && is_keyboard_msg(msg) &&
1153 win != filter_win && !is_child_window( filter_win, win )))
1155 clear_bits &= ~get_hardware_msg_bit( msg );
1156 release_object( win_thread );
1157 msg = msg->next;
1158 continue;
1160 /* now we can return it */
1161 if (!input->msg_thread) input->msg_thread = win_thread;
1162 else release_object( win_thread );
1163 input->msg = msg;
1165 reply->type = MSG_HARDWARE;
1166 reply->win = win;
1167 reply->msg = msg_code;
1168 reply->wparam = msg->wparam;
1169 reply->lparam = msg->lparam;
1170 reply->x = msg->x;
1171 reply->y = msg->y;
1172 reply->time = msg->time;
1173 reply->info = msg->info;
1174 return 1;
1176 /* nothing found, clear the hardware queue bits */
1177 clear_queue_bits( thread->queue, clear_bits );
1178 if (input->msg_thread) release_object( input->msg_thread );
1179 input->msg = NULL;
1180 input->msg_thread = NULL;
1181 return 0;
1184 /* increment (or decrement if 'incr' is negative) the queue paint count */
1185 void inc_queue_paint_count( struct thread *thread, int incr )
1187 struct msg_queue *queue = thread->queue;
1189 assert( queue );
1191 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1193 if (queue->paint_count)
1194 set_queue_bits( queue, QS_PAINT );
1195 else
1196 clear_queue_bits( queue, QS_PAINT );
1200 /* remove all messages and timers belonging to a certain window */
1201 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1203 struct msg_queue *queue = thread->queue;
1204 struct timer *timer;
1205 struct message *msg;
1206 int i;
1208 if (!queue) return;
1210 /* remove timers */
1211 timer = queue->first_timer;
1212 while (timer)
1214 struct timer *next = timer->next;
1215 if (timer->win == win)
1217 unlink_timer( queue, timer );
1218 free( timer );
1220 timer = next;
1223 /* remove messages */
1224 for (i = 0; i < NB_MSG_KINDS; i++)
1226 msg = queue->msg_list[i].first;
1227 while (msg)
1229 struct message *next = msg->next;
1230 if (msg->win == win) remove_queue_message( queue, msg, i );
1231 msg = next;
1235 thread_input_cleanup_window( queue, win );
1238 /* post a message to a window; used by socket handling */
1239 void post_message( user_handle_t win, unsigned int message,
1240 unsigned int wparam, unsigned int lparam )
1242 struct message *msg;
1243 struct thread *thread = get_window_thread( win );
1245 if (!thread) return;
1247 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1249 msg->type = MSG_POSTED;
1250 msg->win = get_user_full_handle( win );
1251 msg->msg = message;
1252 msg->wparam = wparam;
1253 msg->lparam = lparam;
1254 msg->time = get_tick_count();
1255 msg->x = 0;
1256 msg->y = 0;
1257 msg->info = 0;
1258 msg->result = NULL;
1259 msg->data = NULL;
1260 msg->data_size = 0;
1262 append_message( &thread->queue->msg_list[POST_MESSAGE], msg );
1263 set_queue_bits( thread->queue, QS_POSTMESSAGE );
1265 release_object( thread );
1269 /* get the message queue of the current thread */
1270 DECL_HANDLER(get_msg_queue)
1272 struct msg_queue *queue = get_current_queue();
1274 reply->handle = 0;
1275 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1279 /* set the current message queue wakeup mask */
1280 DECL_HANDLER(set_queue_mask)
1282 struct msg_queue *queue = get_current_queue();
1284 if (queue)
1286 queue->wake_mask = req->wake_mask;
1287 queue->changed_mask = req->changed_mask;
1288 reply->wake_bits = queue->wake_bits;
1289 reply->changed_bits = queue->changed_bits;
1290 if (is_signaled( queue ))
1292 /* if skip wait is set, do what would have been done in the subsequent wait */
1293 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1294 else wake_up( &queue->obj, 0 );
1300 /* get the current message queue status */
1301 DECL_HANDLER(get_queue_status)
1303 struct msg_queue *queue = current->queue;
1304 if (queue)
1306 reply->wake_bits = queue->wake_bits;
1307 reply->changed_bits = queue->changed_bits;
1308 if (req->clear) queue->changed_bits = 0;
1310 else reply->wake_bits = reply->changed_bits = 0;
1314 /* send a message to a thread queue */
1315 DECL_HANDLER(send_message)
1317 struct message *msg;
1318 struct msg_queue *send_queue = get_current_queue();
1319 struct msg_queue *recv_queue = NULL;
1320 struct thread *thread = NULL;
1322 if (req->id)
1324 if (!(thread = get_thread_from_id( req->id ))) return;
1326 else if (req->type != MSG_HARDWARE)
1328 /* only hardware messages are allowed without destination thread */
1329 set_error( STATUS_INVALID_PARAMETER );
1330 return;
1333 if (thread && !(recv_queue = thread->queue))
1335 set_error( STATUS_INVALID_PARAMETER );
1336 release_object( thread );
1337 return;
1339 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1341 set_error( STATUS_TIMEOUT );
1342 release_object( thread );
1343 return;
1346 if ((msg = mem_alloc( sizeof(*msg) )))
1348 msg->type = req->type;
1349 msg->win = get_user_full_handle( req->win );
1350 msg->msg = req->msg;
1351 msg->wparam = req->wparam;
1352 msg->lparam = req->lparam;
1353 msg->time = req->time;
1354 msg->x = req->x;
1355 msg->y = req->y;
1356 msg->info = req->info;
1357 msg->result = NULL;
1358 msg->data = NULL;
1359 msg->data_size = 0;
1361 switch(msg->type)
1363 case MSG_OTHER_PROCESS:
1364 msg->data_size = get_req_data_size();
1365 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1367 free( msg );
1368 break;
1370 /* fall through */
1371 case MSG_ASCII:
1372 case MSG_UNICODE:
1373 case MSG_CALLBACK:
1374 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1375 req->timeout, req->callback, req->info )))
1377 free_message( msg );
1378 break;
1380 /* fall through */
1381 case MSG_NOTIFY:
1382 append_message( &recv_queue->msg_list[SEND_MESSAGE], msg );
1383 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1384 break;
1385 case MSG_POSTED:
1386 /* needed for posted DDE messages */
1387 msg->data_size = get_req_data_size();
1388 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1390 free( msg );
1391 break;
1393 append_message( &recv_queue->msg_list[POST_MESSAGE], msg );
1394 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1395 break;
1396 case MSG_HARDWARE:
1397 queue_hardware_message( recv_queue, msg );
1398 break;
1399 case MSG_CALLBACK_RESULT: /* cannot send this one */
1400 default:
1401 set_error( STATUS_INVALID_PARAMETER );
1402 free( msg );
1403 break;
1406 if (thread) release_object( thread );
1410 /* get a message from the current queue */
1411 DECL_HANDLER(get_message)
1413 struct timer *timer;
1414 struct message *msg;
1415 struct message *first_hw_msg = NULL;
1416 struct msg_queue *queue = get_current_queue();
1417 user_handle_t get_win = get_user_full_handle( req->get_win );
1419 if (!queue) return;
1420 gettimeofday( &queue->last_get_msg, NULL );
1422 /* first of all release the hardware input lock if we own it */
1423 /* we'll grab it again if we find a hardware message */
1424 if (queue->input->msg_thread == current)
1426 first_hw_msg = queue->input->msg;
1427 release_hardware_message( current, 0 );
1430 /* first check for sent messages */
1431 if ((msg = queue->msg_list[SEND_MESSAGE].first))
1433 receive_message( queue, msg, reply );
1434 return;
1436 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1438 /* clear changed bits so we can wait on them if we don't find a message */
1439 queue->changed_bits = 0;
1441 /* then check for posted messages */
1442 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1443 return;
1445 /* then check for any raw hardware message */
1446 if (get_hardware_message( current, first_hw_msg, get_win, reply ))
1447 return;
1449 /* now check for WM_PAINT */
1450 if (queue->paint_count &&
1451 (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
1452 (reply->win = find_window_to_repaint( get_win, current )))
1454 reply->type = MSG_POSTED;
1455 reply->msg = WM_PAINT;
1456 reply->wparam = 0;
1457 reply->lparam = 0;
1458 reply->x = 0;
1459 reply->y = 0;
1460 reply->time = get_tick_count();
1461 reply->info = 0;
1462 return;
1465 /* now check for timer */
1466 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1467 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1469 reply->type = MSG_POSTED;
1470 reply->win = timer->win;
1471 reply->msg = timer->msg;
1472 reply->wparam = timer->id;
1473 reply->lparam = timer->lparam;
1474 reply->x = 0;
1475 reply->y = 0;
1476 reply->time = get_tick_count();
1477 reply->info = 0;
1478 return;
1481 done:
1482 set_error( STATUS_PENDING ); /* FIXME */
1486 /* reply to a sent message */
1487 DECL_HANDLER(reply_message)
1489 if (!current->queue)
1491 set_error( STATUS_ACCESS_DENIED );
1492 return;
1494 if (req->type == MSG_HARDWARE)
1496 struct thread_input *input = current->queue->input;
1497 if (input->msg_thread == current) release_hardware_message( current, req->remove );
1498 else set_error( STATUS_ACCESS_DENIED );
1500 else if (current->queue->recv_result)
1501 reply_message( current->queue, req->result, 0, req->remove,
1502 get_req_data(), get_req_data_size() );
1506 /* retrieve the reply for the last message sent */
1507 DECL_HANDLER(get_message_reply)
1509 struct message_result *result;
1510 struct list *entry;
1511 struct msg_queue *queue = current->queue;
1513 if (queue)
1515 set_error( STATUS_PENDING );
1516 reply->result = 0;
1518 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1520 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1521 if (result->replied || req->cancel)
1523 if (result->replied)
1525 reply->result = result->result;
1526 set_error( result->error );
1527 if (result->data)
1529 size_t data_len = min( result->data_size, get_reply_max_size() );
1530 set_reply_data_ptr( result->data, data_len );
1531 result->data = NULL;
1532 result->data_size = 0;
1535 remove_result_from_sender( result );
1537 entry = list_head( &queue->send_result );
1538 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1539 else
1541 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1542 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1546 else set_error( STATUS_ACCESS_DENIED );
1550 /* set a window timer */
1551 DECL_HANDLER(set_win_timer)
1553 struct timer *timer;
1554 struct msg_queue *queue = get_current_queue();
1555 user_handle_t win = get_user_full_handle( req->win );
1557 if (!queue) return;
1559 /* remove it if it existed already */
1560 if (win) kill_timer( queue, win, req->msg, req->id );
1562 if ((timer = set_timer( queue, req->rate )))
1564 timer->win = win;
1565 timer->msg = req->msg;
1566 timer->id = req->id;
1567 timer->lparam = req->lparam;
1571 /* kill a window timer */
1572 DECL_HANDLER(kill_win_timer)
1574 struct msg_queue *queue = current->queue;
1576 if (!queue || !kill_timer( queue, get_user_full_handle(req->win), req->msg, req->id ))
1577 set_error( STATUS_INVALID_PARAMETER );
1581 /* attach (or detach) thread inputs */
1582 DECL_HANDLER(attach_thread_input)
1584 struct thread *thread_from = get_thread_from_id( req->tid_from );
1585 struct thread *thread_to = get_thread_from_id( req->tid_to );
1587 if (!thread_from || !thread_to)
1589 if (thread_from) release_object( thread_from );
1590 if (thread_to) release_object( thread_to );
1591 return;
1593 if (thread_from != thread_to)
1595 if (req->attach) attach_thread_input( thread_from, thread_to );
1596 else detach_thread_input( thread_from, thread_to );
1598 else set_error( STATUS_ACCESS_DENIED );
1599 release_object( thread_from );
1600 release_object( thread_to );
1604 /* get thread input data */
1605 DECL_HANDLER(get_thread_input)
1607 struct thread *thread = NULL;
1608 struct thread_input *input;
1610 if (req->tid)
1612 if (!(thread = get_thread_from_id( req->tid ))) return;
1613 input = thread->queue ? thread->queue->input : NULL;
1615 else input = foreground_input; /* get the foreground thread info */
1617 if (input)
1619 reply->focus = input->focus;
1620 reply->capture = input->capture;
1621 reply->active = input->active;
1622 reply->menu_owner = input->menu_owner;
1623 reply->move_size = input->move_size;
1624 reply->caret = input->caret;
1625 reply->rect = input->caret_rect;
1627 else
1629 reply->focus = 0;
1630 reply->capture = 0;
1631 reply->active = 0;
1632 reply->menu_owner = 0;
1633 reply->move_size = 0;
1634 reply->caret = 0;
1635 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1637 /* foreground window is active window of foreground thread */
1638 reply->foreground = foreground_input ? foreground_input->active : 0;
1639 if (thread) release_object( thread );
1643 /* retrieve queue keyboard state for a given thread */
1644 DECL_HANDLER(get_key_state)
1646 struct thread *thread;
1647 struct thread_input *input;
1649 if (!(thread = get_thread_from_id( req->tid ))) return;
1650 input = thread->queue ? thread->queue->input : NULL;
1651 if (input)
1653 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1654 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1656 release_object( thread );
1660 /* set queue keyboard state for a given thread */
1661 DECL_HANDLER(set_key_state)
1663 struct thread *thread = NULL;
1664 struct thread_input *input;
1666 if (!(thread = get_thread_from_id( req->tid ))) return;
1667 input = thread->queue ? thread->queue->input : NULL;
1668 if (input)
1670 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1671 if (size) memcpy( input->keystate, get_req_data(), size );
1673 release_object( thread );
1677 /* set the system foreground window */
1678 DECL_HANDLER(set_foreground_window)
1680 struct msg_queue *queue = get_current_queue();
1682 reply->previous = foreground_input ? foreground_input->active : 0;
1683 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1684 reply->send_msg_new = FALSE;
1686 if (req->handle)
1688 struct thread *thread;
1690 if (is_top_level_window( req->handle ) &&
1691 ((thread = get_window_thread( req->handle ))))
1693 foreground_input = thread->queue->input;
1694 reply->send_msg_new = (foreground_input != queue->input);
1695 release_object( thread );
1697 else set_error( STATUS_INVALID_HANDLE );
1699 else foreground_input = NULL;
1703 /* set the current thread focus window */
1704 DECL_HANDLER(set_focus_window)
1706 struct msg_queue *queue = get_current_queue();
1708 reply->previous = 0;
1709 if (queue && check_queue_input_window( queue, req->handle ))
1711 reply->previous = queue->input->focus;
1712 queue->input->focus = get_user_full_handle( req->handle );
1717 /* set the current thread active window */
1718 DECL_HANDLER(set_active_window)
1720 struct msg_queue *queue = get_current_queue();
1722 reply->previous = 0;
1723 if (queue && check_queue_input_window( queue, req->handle ))
1725 if (!req->handle || make_window_active( req->handle ))
1727 reply->previous = queue->input->active;
1728 queue->input->active = get_user_full_handle( req->handle );
1730 else set_error( STATUS_INVALID_HANDLE );
1735 /* set the current thread capture window */
1736 DECL_HANDLER(set_capture_window)
1738 struct msg_queue *queue = get_current_queue();
1740 reply->previous = reply->full_handle = 0;
1741 if (queue && check_queue_input_window( queue, req->handle ))
1743 struct thread_input *input = queue->input;
1745 reply->previous = input->capture;
1746 input->capture = get_user_full_handle( req->handle );
1747 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1748 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1749 reply->full_handle = input->capture;
1754 /* Set the current thread caret window */
1755 DECL_HANDLER(set_caret_window)
1757 struct msg_queue *queue = get_current_queue();
1759 reply->previous = 0;
1760 if (queue && check_queue_input_window( queue, req->handle ))
1762 struct thread_input *input = queue->input;
1764 reply->previous = input->caret;
1765 reply->old_rect = input->caret_rect;
1766 reply->old_hide = input->caret_hide;
1767 reply->old_state = input->caret_state;
1769 set_caret_window( input, get_user_full_handle(req->handle) );
1770 input->caret_rect.right = req->width;
1771 input->caret_rect.bottom = req->height;
1776 /* Set the current thread caret information */
1777 DECL_HANDLER(set_caret_info)
1779 struct msg_queue *queue = get_current_queue();
1780 struct thread_input *input;
1782 if (!queue) return;
1783 input = queue->input;
1784 reply->full_handle = input->caret;
1785 reply->old_rect = input->caret_rect;
1786 reply->old_hide = input->caret_hide;
1787 reply->old_state = input->caret_state;
1789 if (req->handle && get_user_full_handle(req->handle) != input->caret)
1791 set_error( STATUS_ACCESS_DENIED );
1792 return;
1794 if (req->flags & SET_CARET_POS)
1796 input->caret_rect.right += req->x - input->caret_rect.left;
1797 input->caret_rect.bottom += req->y - input->caret_rect.top;
1798 input->caret_rect.left = req->x;
1799 input->caret_rect.top = req->y;
1801 if (req->flags & SET_CARET_HIDE)
1803 input->caret_hide += req->hide;
1804 if (input->caret_hide < 0) input->caret_hide = 0;
1806 if (req->flags & SET_CARET_STATE)
1808 if (req->state == -1) input->caret_state = !input->caret_state;
1809 else input->caret_state = !!req->state;