- improve some parameter checking in WSAIoctl
[wine.git] / server / queue.c
blob65bb680fe31f5a3f21678d642b04480b94e1a2aa
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 <stdio.h>
26 #include <stdlib.h>
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
32 #include "handle.h"
33 #include "file.h"
34 #include "thread.h"
35 #include "process.h"
36 #include "request.h"
37 #include "user.h"
39 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
40 #define NB_MSG_KINDS (POST_MESSAGE+1)
43 struct message_result
45 struct list sender_entry; /* entry in sender list */
46 struct message_result *recv_next; /* next in receiver list */
47 struct msg_queue *sender; /* sender queue */
48 struct msg_queue *receiver; /* receiver queue */
49 int replied; /* has it been replied to? */
50 unsigned int result; /* reply result */
51 unsigned int error; /* error code to pass back to sender */
52 struct message *callback_msg; /* message to queue for callback */
53 void *data; /* message reply data */
54 unsigned int data_size; /* size of message reply data */
55 struct timeout_user *timeout; /* result timeout */
58 struct message
60 struct message *next; /* next message in list */
61 struct message *prev; /* prev message in list */
62 enum message_type type; /* message type */
63 user_handle_t win; /* window handle */
64 unsigned int msg; /* message code */
65 unsigned int wparam; /* parameters */
66 unsigned int lparam; /* parameters */
67 int x; /* x position */
68 int y; /* y position */
69 unsigned int time; /* message time */
70 unsigned int info; /* extra info */
71 void *data; /* message data for sent messages */
72 unsigned int data_size; /* size of message data */
73 struct message_result *result; /* result in sender queue */
76 struct message_list
78 struct message *first; /* head of list */
79 struct message *last; /* tail of list */
82 struct timer
84 struct timer *next; /* next timer in list */
85 struct timer *prev; /* prev timer in list */
86 struct timeval when; /* next expiration */
87 unsigned int rate; /* timer rate in ms */
88 user_handle_t win; /* window handle */
89 unsigned int msg; /* message to post */
90 unsigned int id; /* timer id */
91 unsigned int lparam; /* lparam for message */
94 struct thread_input
96 struct object obj; /* object header */
97 user_handle_t focus; /* focus window */
98 user_handle_t capture; /* capture window */
99 user_handle_t active; /* active window */
100 user_handle_t menu_owner; /* current menu owner window */
101 user_handle_t move_size; /* current moving/resizing window */
102 user_handle_t caret; /* caret window */
103 rectangle_t caret_rect; /* caret rectangle */
104 int caret_hide; /* caret hide count */
105 int caret_state; /* caret on/off state */
106 struct message *msg; /* message currently processed */
107 struct thread *msg_thread; /* thread processing the message */
108 struct message_list msg_list; /* list of hardware messages */
109 unsigned char keystate[256]; /* state of each key */
112 struct msg_queue
114 struct object obj; /* object header */
115 unsigned int wake_bits; /* wakeup bits */
116 unsigned int wake_mask; /* wakeup mask */
117 unsigned int changed_bits; /* changed wakeup bits */
118 unsigned int changed_mask; /* changed wakeup mask */
119 int paint_count; /* pending paint messages count */
120 struct message_list msg_list[NB_MSG_KINDS]; /* lists of messages */
121 struct list send_result; /* stack of sent messages waiting for result */
122 struct list callback_result; /* list of callback messages waiting for result */
123 struct message_result *recv_result; /* stack of received messages waiting for result */
124 struct timer *first_timer; /* head of timer list */
125 struct timer *last_timer; /* tail of timer list */
126 struct timer *next_timer; /* next timer to expire */
127 struct timeout_user *timeout; /* timeout for next timer to expire */
128 struct thread_input *input; /* thread input descriptor */
129 struct hook_table *hooks; /* hook table */
130 struct timeval last_get_msg; /* time of last get message call */
133 static void msg_queue_dump( struct object *obj, int verbose );
134 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
135 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
136 static int msg_queue_signaled( struct object *obj, struct thread *thread );
137 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
138 static void msg_queue_destroy( struct object *obj );
139 static void thread_input_dump( struct object *obj, int verbose );
140 static void thread_input_destroy( struct object *obj );
141 static void timer_callback( void *private );
143 static const struct object_ops msg_queue_ops =
145 sizeof(struct msg_queue), /* size */
146 msg_queue_dump, /* dump */
147 msg_queue_add_queue, /* add_queue */
148 msg_queue_remove_queue, /* remove_queue */
149 msg_queue_signaled, /* signaled */
150 msg_queue_satisfied, /* satisfied */
151 no_get_fd, /* get_fd */
152 msg_queue_destroy /* destroy */
156 static const struct object_ops thread_input_ops =
158 sizeof(struct thread_input), /* size */
159 thread_input_dump, /* dump */
160 no_add_queue, /* add_queue */
161 NULL, /* remove_queue */
162 NULL, /* signaled */
163 NULL, /* satisfied */
164 no_get_fd, /* get_fd */
165 thread_input_destroy /* destroy */
168 /* pointer to input structure of foreground thread */
169 static struct thread_input *foreground_input;
172 /* set the caret window in a given thread input */
173 static void set_caret_window( struct thread_input *input, user_handle_t win )
175 input->caret = win;
176 input->caret_rect.left = 0;
177 input->caret_rect.top = 0;
178 input->caret_rect.right = 0;
179 input->caret_rect.bottom = 0;
180 input->caret_hide = 1;
181 input->caret_state = 0;
184 /* create a thread input object */
185 static struct thread_input *create_thread_input(void)
187 struct thread_input *input;
189 if ((input = alloc_object( &thread_input_ops )))
191 input->focus = 0;
192 input->capture = 0;
193 input->active = 0;
194 input->menu_owner = 0;
195 input->move_size = 0;
196 input->msg = NULL;
197 input->msg_thread = NULL;
198 input->msg_list.first = input->msg_list.last = NULL;
199 set_caret_window( input, 0 );
200 memset( input->keystate, 0, sizeof(input->keystate) );
202 return input;
205 /* create a message queue object */
206 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
208 struct msg_queue *queue;
209 int i;
211 if (!input && !(input = create_thread_input())) return NULL;
212 if ((queue = alloc_object( &msg_queue_ops )))
214 queue->wake_bits = 0;
215 queue->wake_mask = 0;
216 queue->changed_bits = 0;
217 queue->changed_mask = 0;
218 queue->paint_count = 0;
219 queue->recv_result = NULL;
220 queue->first_timer = NULL;
221 queue->last_timer = NULL;
222 queue->next_timer = NULL;
223 queue->timeout = NULL;
224 queue->input = (struct thread_input *)grab_object( input );
225 queue->hooks = NULL;
226 gettimeofday( &queue->last_get_msg, NULL );
227 list_init( &queue->send_result );
228 list_init( &queue->callback_result );
229 for (i = 0; i < NB_MSG_KINDS; i++)
230 queue->msg_list[i].first = queue->msg_list[i].last = NULL;
232 thread->queue = queue;
233 if (!thread->process->queue)
234 thread->process->queue = (struct msg_queue *)grab_object( queue );
236 release_object( input );
237 return queue;
240 /* free the message queue of a thread at thread exit */
241 void free_msg_queue( struct thread *thread )
243 struct process *process = thread->process;
245 remove_thread_hooks( thread );
246 if (!thread->queue) return;
247 if (process->queue == thread->queue) /* is it the process main queue? */
249 release_object( process->queue );
250 process->queue = NULL;
251 if (process->idle_event)
253 set_event( process->idle_event );
254 release_object( process->idle_event );
255 process->idle_event = NULL;
258 release_object( thread->queue );
259 thread->queue = NULL;
262 /* get the hook table for a given thread */
263 struct hook_table *get_queue_hooks( struct thread *thread )
265 if (!thread->queue) return NULL;
266 return thread->queue->hooks;
269 /* set the hook table for a given thread, allocating the queue if needed */
270 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
272 struct msg_queue *queue = thread->queue;
273 if (!queue) queue = create_msg_queue( thread, NULL );
274 if (queue->hooks) release_object( queue->hooks );
275 queue->hooks = hooks;
278 /* check the queue status */
279 inline static int is_signaled( struct msg_queue *queue )
281 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
284 /* set some queue bits */
285 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
287 queue->wake_bits |= bits;
288 queue->changed_bits |= bits;
289 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
292 /* clear some queue bits */
293 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
295 queue->wake_bits &= ~bits;
296 queue->changed_bits &= ~bits;
299 /* check whether msg is a keyboard message */
300 inline static int is_keyboard_msg( struct message *msg )
302 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
305 /* get the QS_* bit corresponding to a given hardware message */
306 inline static int get_hardware_msg_bit( struct message *msg )
308 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
309 if (is_keyboard_msg( msg )) return QS_KEY;
310 return QS_MOUSEBUTTON;
313 /* get the current thread queue, creating it if needed */
314 inline static struct msg_queue *get_current_queue(void)
316 struct msg_queue *queue = current->queue;
317 if (!queue) queue = create_msg_queue( current, NULL );
318 return queue;
321 /* append a message to the end of a list */
322 inline static void append_message( struct message_list *list, struct message *msg )
324 msg->next = NULL;
325 if ((msg->prev = list->last)) msg->prev->next = msg;
326 else list->first = msg;
327 list->last = msg;
330 /* unlink a message from a list it */
331 inline static void unlink_message( struct message_list *list, struct message *msg )
333 if (msg->next) msg->next->prev = msg->prev;
334 else list->last = msg->prev;
335 if (msg->prev) msg->prev->next = msg->next;
336 else list->first = msg->next;
339 /* try to merge a message with the last in the list; return 1 if successful */
340 static int merge_message( struct thread_input *input, const struct message *msg )
342 struct message *prev = input->msg_list.last;
344 if (!prev) return 0;
345 if (input->msg == prev) return 0;
346 if (prev->result) return 0;
347 if (prev->win != msg->win) return 0;
348 if (prev->msg != msg->msg) return 0;
349 if (prev->type != msg->type) return 0;
350 /* now we can merge it */
351 prev->wparam = msg->wparam;
352 prev->lparam = msg->lparam;
353 prev->x = msg->x;
354 prev->y = msg->y;
355 prev->time = msg->time;
356 prev->info = msg->info;
357 return 1;
360 /* free a result structure */
361 static void free_result( struct message_result *result )
363 if (result->timeout) remove_timeout_user( result->timeout );
364 if (result->data) free( result->data );
365 if (result->callback_msg) free( result->callback_msg );
366 free( result );
369 /* remove the result from the sender list it is on */
370 static inline void remove_result_from_sender( struct message_result *result )
372 assert( result->sender );
374 list_remove( &result->sender_entry );
375 result->sender = NULL;
376 if (!result->receiver) free_result( result );
379 /* store the message result in the appropriate structure */
380 static void store_message_result( struct message_result *res, unsigned int result,
381 unsigned int error )
383 res->result = result;
384 res->error = error;
385 res->replied = 1;
386 if (res->timeout)
388 remove_timeout_user( res->timeout );
389 res->timeout = NULL;
391 if (res->sender)
393 if (res->callback_msg)
395 /* queue the callback message in the sender queue */
396 res->callback_msg->lparam = result;
397 append_message( &res->sender->msg_list[SEND_MESSAGE], res->callback_msg );
398 set_queue_bits( res->sender, QS_SENDMESSAGE );
399 res->callback_msg = NULL;
400 remove_result_from_sender( res );
402 else
404 /* wake sender queue if waiting on this result */
405 if (list_head(&res->sender->send_result) == &res->sender_entry)
406 set_queue_bits( res->sender, QS_SMRESULT );
412 /* free a message when deleting a queue or window */
413 static void free_message( struct message *msg )
415 struct message_result *result = msg->result;
416 if (result)
418 if (result->sender)
420 result->receiver = NULL;
421 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
423 else free_result( result );
425 if (msg->data) free( msg->data );
426 free( msg );
429 /* remove (and free) a message from a message list */
430 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
431 enum message_kind kind )
433 unlink_message( &queue->msg_list[kind], msg );
434 switch(kind)
436 case SEND_MESSAGE:
437 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_SENDMESSAGE );
438 break;
439 case POST_MESSAGE:
440 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_POSTMESSAGE );
441 break;
443 free_message( msg );
446 /* message timed out without getting a reply */
447 static void result_timeout( void *private )
449 struct message_result *result = private;
451 assert( !result->replied );
453 result->timeout = NULL;
454 store_message_result( result, 0, STATUS_TIMEOUT );
457 /* allocate and fill a message result structure */
458 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
459 struct msg_queue *recv_queue,
460 struct message *msg, unsigned int timeout,
461 void *callback, unsigned int callback_data )
463 struct message_result *result = mem_alloc( sizeof(*result) );
464 if (result)
466 result->sender = send_queue;
467 result->receiver = recv_queue;
468 result->replied = 0;
469 result->data = NULL;
470 result->data_size = 0;
471 result->timeout = NULL;
473 if (msg->type == MSG_CALLBACK)
475 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
476 if (!callback_msg)
478 free( result );
479 return NULL;
481 callback_msg->type = MSG_CALLBACK_RESULT;
482 callback_msg->win = msg->win;
483 callback_msg->msg = msg->msg;
484 callback_msg->wparam = (unsigned int)callback;
485 callback_msg->lparam = 0;
486 callback_msg->time = get_tick_count();
487 callback_msg->x = 0;
488 callback_msg->y = 0;
489 callback_msg->info = callback_data;
490 callback_msg->result = NULL;
491 callback_msg->data = NULL;
492 callback_msg->data_size = 0;
494 result->callback_msg = callback_msg;
495 list_add_head( &send_queue->callback_result, &result->sender_entry );
497 else
499 result->callback_msg = NULL;
500 list_add_head( &send_queue->send_result, &result->sender_entry );
503 if (timeout != -1)
505 struct timeval when;
506 gettimeofday( &when, 0 );
507 add_timeout( &when, timeout );
508 result->timeout = add_timeout_user( &when, result_timeout, result );
511 return result;
514 /* receive a message, removing it from the sent queue */
515 static void receive_message( struct msg_queue *queue, struct message *msg,
516 struct get_message_reply *reply )
518 struct message_result *result = msg->result;
520 reply->total = msg->data_size;
521 if (msg->data_size > get_reply_max_size())
523 set_error( STATUS_BUFFER_OVERFLOW );
524 return;
526 reply->type = msg->type;
527 reply->win = msg->win;
528 reply->msg = msg->msg;
529 reply->wparam = msg->wparam;
530 reply->lparam = msg->lparam;
531 reply->x = msg->x;
532 reply->y = msg->y;
533 reply->time = msg->time;
534 reply->info = msg->info;
536 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
538 unlink_message( &queue->msg_list[SEND_MESSAGE], msg );
539 /* put the result on the receiver result stack */
540 if (result)
542 result->recv_next = queue->recv_result;
543 queue->recv_result = result;
545 free( msg );
546 if (!queue->msg_list[SEND_MESSAGE].first) clear_queue_bits( queue, QS_SENDMESSAGE );
549 /* set the result of the current received message */
550 static void reply_message( struct msg_queue *queue, unsigned int result,
551 unsigned int error, int remove, const void *data, size_t len )
553 struct message_result *res = queue->recv_result;
555 if (remove)
557 queue->recv_result = res->recv_next;
558 res->receiver = NULL;
559 if (!res->sender) /* no one waiting for it */
561 free_result( res );
562 return;
565 if (!res->replied)
567 if (len && (res->data = memdup( data, len ))) res->data_size = len;
568 store_message_result( res, result, error );
572 /* retrieve a posted message */
573 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
574 unsigned int first, unsigned int last, unsigned int flags,
575 struct get_message_reply *reply )
577 struct message *msg;
578 struct message_list *list = &queue->msg_list[POST_MESSAGE];
580 /* check against the filters */
581 for (msg = list->first; msg; msg = msg->next)
583 if (msg->msg == WM_QUIT) break; /* WM_QUIT is never filtered */
584 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
585 if (msg->msg < first) continue;
586 if (msg->msg > last) continue;
587 break; /* found one */
589 if (!msg) return 0;
591 /* return it to the app */
593 reply->total = msg->data_size;
594 if (msg->data_size > get_reply_max_size())
596 set_error( STATUS_BUFFER_OVERFLOW );
597 return 1;
599 reply->type = msg->type;
600 reply->win = msg->win;
601 reply->msg = msg->msg;
602 reply->wparam = msg->wparam;
603 reply->lparam = msg->lparam;
604 reply->x = msg->x;
605 reply->y = msg->y;
606 reply->time = msg->time;
607 reply->info = msg->info;
609 if (flags & GET_MSG_REMOVE)
611 if (msg->data)
613 set_reply_data_ptr( msg->data, msg->data_size );
614 msg->data = NULL;
615 msg->data_size = 0;
617 remove_queue_message( queue, msg, POST_MESSAGE );
619 else if (msg->data) set_reply_data( msg->data, msg->data_size );
621 return 1;
624 /* empty a message list and free all the messages */
625 static void empty_msg_list( struct message_list *list )
627 struct message *msg = list->first;
628 while (msg)
630 struct message *next = msg->next;
631 free_message( msg );
632 msg = next;
636 /* cleanup all pending results when deleting a queue */
637 static void cleanup_results( struct msg_queue *queue )
639 struct list *entry;
641 while ((entry = list_head( &queue->send_result )) != NULL)
643 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
646 while ((entry = list_head( &queue->callback_result )) != NULL)
648 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
651 while (queue->recv_result)
652 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
655 /* check if the thread owning the queue is hung (not checking for messages) */
656 static int is_queue_hung( struct msg_queue *queue )
658 struct timeval now;
659 struct wait_queue_entry *entry;
661 gettimeofday( &now, NULL );
662 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
663 return 0; /* less than 5 seconds since last get message -> not hung */
665 for (entry = queue->obj.head; entry; entry = entry->next)
667 if (entry->thread->queue == queue)
668 return 0; /* thread is waiting on queue -> not hung */
670 return 1;
673 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
675 struct msg_queue *queue = (struct msg_queue *)obj;
676 struct process *process = entry->thread->process;
678 /* a thread can only wait on its own queue */
679 if (entry->thread->queue != queue)
681 set_error( STATUS_ACCESS_DENIED );
682 return 0;
684 /* if waiting on the main process queue, set the idle event */
685 if (process->queue == queue)
687 if (process->idle_event) set_event( process->idle_event );
689 add_queue( obj, entry );
690 return 1;
693 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
695 struct msg_queue *queue = (struct msg_queue *)obj;
696 struct process *process = entry->thread->process;
698 remove_queue( obj, entry );
700 assert( entry->thread->queue == queue );
702 /* if waiting on the main process queue, reset the idle event */
703 if (process->queue == queue)
705 if (process->idle_event) reset_event( process->idle_event );
709 static void msg_queue_dump( struct object *obj, int verbose )
711 struct msg_queue *queue = (struct msg_queue *)obj;
712 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
713 queue->wake_bits, queue->wake_mask );
716 static int msg_queue_signaled( struct object *obj, struct thread *thread )
718 struct msg_queue *queue = (struct msg_queue *)obj;
719 return is_signaled( queue );
722 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
724 struct msg_queue *queue = (struct msg_queue *)obj;
725 queue->wake_mask = 0;
726 queue->changed_mask = 0;
727 return 0; /* Not abandoned */
730 static void msg_queue_destroy( struct object *obj )
732 struct msg_queue *queue = (struct msg_queue *)obj;
733 struct timer *timer = queue->first_timer;
734 int i;
736 cleanup_results( queue );
737 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
739 while (timer)
741 struct timer *next = timer->next;
742 free( timer );
743 timer = next;
745 if (queue->timeout) remove_timeout_user( queue->timeout );
746 if (queue->input) release_object( queue->input );
747 if (queue->hooks) release_object( queue->hooks );
750 static void thread_input_dump( struct object *obj, int verbose )
752 struct thread_input *input = (struct thread_input *)obj;
753 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
754 input->focus, input->capture, input->active );
757 static void thread_input_destroy( struct object *obj )
759 struct thread_input *input = (struct thread_input *)obj;
761 if (foreground_input == input) foreground_input = NULL;
762 if (input->msg_thread) release_object( input->msg_thread );
763 empty_msg_list( &input->msg_list );
766 /* fix the thread input data when a window is destroyed */
767 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
769 struct thread_input *input = queue->input;
771 if (window == input->focus) input->focus = 0;
772 if (window == input->capture) input->capture = 0;
773 if (window == input->active) input->active = 0;
774 if (window == input->menu_owner) input->menu_owner = 0;
775 if (window == input->move_size) input->move_size = 0;
776 if (window == input->caret) set_caret_window( input, 0 );
779 /* check if the specified window can be set in the input data of a given queue */
780 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
782 struct thread *thread;
783 int ret = 0;
785 if (!window) return 1; /* we can always clear the data */
787 if ((thread = get_window_thread( window )))
789 ret = (queue->input == thread->queue->input);
790 if (!ret) set_error( STATUS_ACCESS_DENIED );
791 release_object( thread );
793 else set_error( STATUS_INVALID_HANDLE );
795 return ret;
798 /* attach two thread input data structures */
799 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
801 struct thread_input *input;
803 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
804 input = (struct thread_input *)grab_object( thread_to->queue->input );
806 if (thread_from->queue)
808 release_object( thread_from->queue->input );
809 thread_from->queue->input = input;
811 else
813 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
815 memset( input->keystate, 0, sizeof(input->keystate) );
816 return 1;
819 /* detach two thread input data structures */
820 static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
822 struct thread_input *input;
824 if (!thread_from->queue || !thread_to->queue ||
825 thread_from->queue->input != thread_to->queue->input)
827 set_error( STATUS_ACCESS_DENIED );
828 return;
830 if ((input = create_thread_input()))
832 release_object( thread_from->queue->input );
833 thread_from->queue->input = input;
838 /* set the next timer to expire */
839 static void set_next_timer( struct msg_queue *queue, struct timer *timer )
841 if (queue->timeout)
843 remove_timeout_user( queue->timeout );
844 queue->timeout = NULL;
846 if ((queue->next_timer = timer))
847 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
849 /* set/clear QS_TIMER bit */
850 if (queue->next_timer == queue->first_timer)
851 clear_queue_bits( queue, QS_TIMER );
852 else
853 set_queue_bits( queue, QS_TIMER );
856 /* callback for the next timer expiration */
857 static void timer_callback( void *private )
859 struct msg_queue *queue = private;
861 queue->timeout = NULL;
862 /* move on to the next timer */
863 set_next_timer( queue, queue->next_timer->next );
866 /* link a timer at its rightful place in the queue list */
867 static void link_timer( struct msg_queue *queue, struct timer *timer )
869 struct timer *pos = queue->next_timer;
871 while (pos && time_before( &pos->when, &timer->when )) pos = pos->next;
873 if (pos) /* insert before pos */
875 if ((timer->prev = pos->prev)) timer->prev->next = timer;
876 else queue->first_timer = timer;
877 timer->next = pos;
878 pos->prev = timer;
880 else /* insert at end */
882 timer->next = NULL;
883 timer->prev = queue->last_timer;
884 if (queue->last_timer) queue->last_timer->next = timer;
885 else queue->first_timer = timer;
886 queue->last_timer = timer;
888 /* check if we replaced the next timer */
889 if (pos == queue->next_timer) set_next_timer( queue, timer );
892 /* remove a timer from the queue timer list */
893 static void unlink_timer( struct msg_queue *queue, struct timer *timer )
895 if (timer->next) timer->next->prev = timer->prev;
896 else queue->last_timer = timer->prev;
897 if (timer->prev) timer->prev->next = timer->next;
898 else queue->first_timer = timer->next;
899 /* check if we removed the next timer */
900 if (queue->next_timer == timer) set_next_timer( queue, timer->next );
901 else if (queue->next_timer == queue->first_timer) clear_queue_bits( queue, QS_TIMER );
904 /* restart an expired timer */
905 static void restart_timer( struct msg_queue *queue, struct timer *timer )
907 struct timeval now;
908 unlink_timer( queue, timer );
909 gettimeofday( &now, 0 );
910 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
911 link_timer( queue, timer );
914 /* find an expired timer matching the filtering parameters */
915 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
916 unsigned int get_first, unsigned int get_last,
917 int remove )
919 struct timer *timer;
920 for (timer = queue->first_timer; (timer && timer != queue->next_timer); timer = timer->next)
922 if (win && timer->win != win) continue;
923 if (timer->msg >= get_first && timer->msg <= get_last)
925 if (remove) restart_timer( queue, timer );
926 return timer;
929 return NULL;
932 /* kill a timer */
933 static int kill_timer( struct msg_queue *queue, user_handle_t win,
934 unsigned int msg, unsigned int id )
936 struct timer *timer;
938 for (timer = queue->first_timer; timer; timer = timer->next)
940 if (timer->win != win || timer->msg != msg || timer->id != id) continue;
941 unlink_timer( queue, timer );
942 free( timer );
943 return 1;
945 return 0;
948 /* add a timer */
949 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
951 struct timer *timer = mem_alloc( sizeof(*timer) );
952 if (timer)
954 timer->rate = rate;
955 gettimeofday( &timer->when, 0 );
956 add_timeout( &timer->when, rate );
957 link_timer( queue, timer );
959 return timer;
962 /* change the input key state for a given key */
963 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
965 if (down)
967 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
968 input->keystate[key] |= 0x80;
970 else input->keystate[key] &= ~0x80;
973 /* update the input key state for a keyboard message */
974 static void update_input_key_state( struct thread_input *input, const struct message *msg )
976 unsigned char key;
977 int down = 0, extended;
979 switch (msg->msg)
981 case WM_LBUTTONDOWN:
982 down = 1;
983 /* fall through */
984 case WM_LBUTTONUP:
985 set_input_key_state( input, VK_LBUTTON, down );
986 break;
987 case WM_MBUTTONDOWN:
988 down = 1;
989 /* fall through */
990 case WM_MBUTTONUP:
991 set_input_key_state( input, VK_MBUTTON, down );
992 break;
993 case WM_RBUTTONDOWN:
994 down = 1;
995 /* fall through */
996 case WM_RBUTTONUP:
997 set_input_key_state( input, VK_RBUTTON, down );
998 break;
999 case WM_KEYDOWN:
1000 case WM_SYSKEYDOWN:
1001 down = 1;
1002 /* fall through */
1003 case WM_KEYUP:
1004 case WM_SYSKEYUP:
1005 key = (unsigned char)msg->wparam;
1006 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1007 set_input_key_state( input, key, down );
1008 switch(key)
1010 case VK_SHIFT:
1011 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1012 break;
1013 case VK_CONTROL:
1014 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1015 break;
1016 case VK_MENU:
1017 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1018 break;
1020 break;
1024 /* release the hardware message currently being processed by the given thread */
1025 static void release_hardware_message( struct thread *thread, int remove )
1027 struct thread_input *input = thread->queue->input;
1029 if (input->msg_thread != thread) return;
1030 if (remove)
1032 struct message *other;
1033 int clr_bit;
1035 update_input_key_state( input, input->msg );
1036 unlink_message( &input->msg_list, input->msg );
1037 clr_bit = get_hardware_msg_bit( input->msg );
1038 for (other = input->msg_list.first; other; other = other->next)
1039 if (get_hardware_msg_bit( other ) == clr_bit) break;
1040 if (!other) clear_queue_bits( thread->queue, clr_bit );
1041 free_message( input->msg );
1043 release_object( input->msg_thread );
1044 input->msg = NULL;
1045 input->msg_thread = NULL;
1048 /* find the window that should receive a given hardware message */
1049 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1050 unsigned int *msg_code )
1052 user_handle_t win = 0;
1054 *msg_code = msg->msg;
1055 if (is_keyboard_msg( msg ))
1057 if (input && !(win = input->focus))
1059 win = input->active;
1060 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1063 else /* mouse message */
1065 if (!input || !(win = input->capture))
1067 if (!(win = msg->win)) win = window_from_point( msg->x, msg->y );
1070 return win;
1073 /* queue a hardware message into a given thread input */
1074 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1076 user_handle_t win;
1077 struct thread *thread;
1078 struct thread_input *input;
1079 unsigned int msg_code;
1081 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1082 if (!win || !(thread = get_window_thread(win)))
1084 free( msg );
1085 return;
1087 input = thread->queue->input;
1089 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1090 else
1092 append_message( &input->msg_list, msg );
1093 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1095 release_object( thread );
1098 /* find a hardware message for the given queue */
1099 static int get_hardware_message( struct thread *thread, struct message *first,
1100 user_handle_t filter_win, struct get_message_reply *reply )
1102 struct thread_input *input = thread->queue->input;
1103 struct thread *win_thread;
1104 struct message *msg;
1105 user_handle_t win;
1106 int clear_bits, got_one = 0;
1107 unsigned int msg_code;
1109 if (input->msg_thread && input->msg_thread != thread)
1110 return 0; /* locked by another thread */
1112 if (!first)
1114 msg = input->msg_list.first;
1115 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1117 else
1119 msg = first->next;
1120 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1123 while (msg)
1125 win = find_hardware_message_window( input, msg, &msg_code );
1126 if (!win || !(win_thread = get_window_thread( win )))
1128 /* no window at all, remove it */
1129 struct message *next = msg->next;
1130 update_input_key_state( input, msg );
1131 unlink_message( &input->msg_list, msg );
1132 free_message( msg );
1133 msg = next;
1134 continue;
1136 if (win_thread != thread)
1138 /* wake the other thread */
1139 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1140 release_object( win_thread );
1141 got_one = 1;
1142 msg = msg->next;
1143 continue;
1145 /* if we already got a message for another thread, or if it doesn't
1146 * match the filter we skip it (filter is only checked for keyboard
1147 * messages since the dest window for a mouse message depends on hittest)
1149 if (got_one ||
1150 (filter_win && is_keyboard_msg(msg) &&
1151 win != filter_win && !is_child_window( filter_win, win )))
1153 clear_bits &= ~get_hardware_msg_bit( msg );
1154 release_object( win_thread );
1155 msg = msg->next;
1156 continue;
1158 /* now we can return it */
1159 if (!input->msg_thread) input->msg_thread = win_thread;
1160 else release_object( win_thread );
1161 input->msg = msg;
1163 reply->type = MSG_HARDWARE;
1164 reply->win = win;
1165 reply->msg = msg_code;
1166 reply->wparam = msg->wparam;
1167 reply->lparam = msg->lparam;
1168 reply->x = msg->x;
1169 reply->y = msg->y;
1170 reply->time = msg->time;
1171 reply->info = msg->info;
1172 return 1;
1174 /* nothing found, clear the hardware queue bits */
1175 clear_queue_bits( thread->queue, clear_bits );
1176 if (input->msg_thread) release_object( input->msg_thread );
1177 input->msg = NULL;
1178 input->msg_thread = NULL;
1179 return 0;
1182 /* increment (or decrement if 'incr' is negative) the queue paint count */
1183 void inc_queue_paint_count( struct thread *thread, int incr )
1185 struct msg_queue *queue = thread->queue;
1187 assert( queue );
1189 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1191 if (queue->paint_count)
1192 set_queue_bits( queue, QS_PAINT );
1193 else
1194 clear_queue_bits( queue, QS_PAINT );
1198 /* remove all messages and timers belonging to a certain window */
1199 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1201 struct msg_queue *queue = thread->queue;
1202 struct timer *timer;
1203 struct message *msg;
1204 int i;
1206 if (!queue) return;
1208 /* remove timers */
1209 timer = queue->first_timer;
1210 while (timer)
1212 struct timer *next = timer->next;
1213 if (timer->win == win)
1215 unlink_timer( queue, timer );
1216 free( timer );
1218 timer = next;
1221 /* remove messages */
1222 for (i = 0; i < NB_MSG_KINDS; i++)
1224 msg = queue->msg_list[i].first;
1225 while (msg)
1227 struct message *next = msg->next;
1228 if (msg->win == win) remove_queue_message( queue, msg, i );
1229 msg = next;
1233 thread_input_cleanup_window( queue, win );
1236 /* post a message to a window; used by socket handling */
1237 void post_message( user_handle_t win, unsigned int message,
1238 unsigned int wparam, unsigned int lparam )
1240 struct message *msg;
1241 struct thread *thread = get_window_thread( win );
1243 if (!thread) return;
1245 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1247 msg->type = MSG_POSTED;
1248 msg->win = get_user_full_handle( win );
1249 msg->msg = message;
1250 msg->wparam = wparam;
1251 msg->lparam = lparam;
1252 msg->time = get_tick_count();
1253 msg->x = 0;
1254 msg->y = 0;
1255 msg->info = 0;
1256 msg->result = NULL;
1257 msg->data = NULL;
1258 msg->data_size = 0;
1260 append_message( &thread->queue->msg_list[POST_MESSAGE], msg );
1261 set_queue_bits( thread->queue, QS_POSTMESSAGE );
1263 release_object( thread );
1267 /* get the message queue of the current thread */
1268 DECL_HANDLER(get_msg_queue)
1270 struct msg_queue *queue = get_current_queue();
1272 reply->handle = 0;
1273 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1277 /* set the current message queue wakeup mask */
1278 DECL_HANDLER(set_queue_mask)
1280 struct msg_queue *queue = get_current_queue();
1282 if (queue)
1284 queue->wake_mask = req->wake_mask;
1285 queue->changed_mask = req->changed_mask;
1286 reply->wake_bits = queue->wake_bits;
1287 reply->changed_bits = queue->changed_bits;
1288 if (is_signaled( queue ))
1290 /* if skip wait is set, do what would have been done in the subsequent wait */
1291 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1292 else wake_up( &queue->obj, 0 );
1298 /* get the current message queue status */
1299 DECL_HANDLER(get_queue_status)
1301 struct msg_queue *queue = current->queue;
1302 if (queue)
1304 reply->wake_bits = queue->wake_bits;
1305 reply->changed_bits = queue->changed_bits;
1306 if (req->clear) queue->changed_bits = 0;
1308 else reply->wake_bits = reply->changed_bits = 0;
1312 /* send a message to a thread queue */
1313 DECL_HANDLER(send_message)
1315 struct message *msg;
1316 struct msg_queue *send_queue = get_current_queue();
1317 struct msg_queue *recv_queue = NULL;
1318 struct thread *thread = NULL;
1320 if (req->id)
1322 if (!(thread = get_thread_from_id( req->id ))) return;
1324 else if (req->type != MSG_HARDWARE)
1326 /* only hardware messages are allowed without destination thread */
1327 set_error( STATUS_INVALID_PARAMETER );
1328 return;
1331 if (thread && !(recv_queue = thread->queue))
1333 set_error( STATUS_INVALID_PARAMETER );
1334 release_object( thread );
1335 return;
1337 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1339 set_error( STATUS_TIMEOUT );
1340 release_object( thread );
1341 return;
1344 if ((msg = mem_alloc( sizeof(*msg) )))
1346 msg->type = req->type;
1347 msg->win = get_user_full_handle( req->win );
1348 msg->msg = req->msg;
1349 msg->wparam = req->wparam;
1350 msg->lparam = req->lparam;
1351 msg->time = req->time;
1352 msg->x = req->x;
1353 msg->y = req->y;
1354 msg->info = req->info;
1355 msg->result = NULL;
1356 msg->data = NULL;
1357 msg->data_size = 0;
1359 switch(msg->type)
1361 case MSG_OTHER_PROCESS:
1362 msg->data_size = get_req_data_size();
1363 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1365 free( msg );
1366 break;
1368 /* fall through */
1369 case MSG_ASCII:
1370 case MSG_UNICODE:
1371 case MSG_CALLBACK:
1372 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1373 req->timeout, req->callback, req->info )))
1375 free_message( msg );
1376 break;
1378 /* fall through */
1379 case MSG_NOTIFY:
1380 append_message( &recv_queue->msg_list[SEND_MESSAGE], msg );
1381 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1382 break;
1383 case MSG_POSTED:
1384 /* needed for posted DDE messages */
1385 msg->data_size = get_req_data_size();
1386 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1388 free( msg );
1389 break;
1391 append_message( &recv_queue->msg_list[POST_MESSAGE], msg );
1392 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1393 break;
1394 case MSG_HARDWARE:
1395 queue_hardware_message( recv_queue, msg );
1396 break;
1397 case MSG_CALLBACK_RESULT: /* cannot send this one */
1398 default:
1399 set_error( STATUS_INVALID_PARAMETER );
1400 free( msg );
1401 break;
1404 if (thread) release_object( thread );
1408 /* get a message from the current queue */
1409 DECL_HANDLER(get_message)
1411 struct timer *timer;
1412 struct message *msg;
1413 struct message *first_hw_msg = NULL;
1414 struct msg_queue *queue = get_current_queue();
1415 user_handle_t get_win = get_user_full_handle( req->get_win );
1417 if (!queue) return;
1418 gettimeofday( &queue->last_get_msg, NULL );
1420 /* first of all release the hardware input lock if we own it */
1421 /* we'll grab it again if we find a hardware message */
1422 if (queue->input->msg_thread == current)
1424 first_hw_msg = queue->input->msg;
1425 release_hardware_message( current, 0 );
1428 /* first check for sent messages */
1429 if ((msg = queue->msg_list[SEND_MESSAGE].first))
1431 receive_message( queue, msg, reply );
1432 return;
1434 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1436 /* clear changed bits so we can wait on them if we don't find a message */
1437 queue->changed_bits = 0;
1439 /* then check for posted messages */
1440 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1441 return;
1443 /* then check for any raw hardware message */
1444 if (get_hardware_message( current, first_hw_msg, get_win, reply ))
1445 return;
1447 /* now check for WM_PAINT */
1448 if (queue->paint_count &&
1449 (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
1450 (reply->win = find_window_to_repaint( get_win, current )))
1452 reply->type = MSG_POSTED;
1453 reply->msg = WM_PAINT;
1454 reply->wparam = 0;
1455 reply->lparam = 0;
1456 reply->x = 0;
1457 reply->y = 0;
1458 reply->time = get_tick_count();
1459 reply->info = 0;
1460 return;
1463 /* now check for timer */
1464 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1465 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1467 reply->type = MSG_POSTED;
1468 reply->win = timer->win;
1469 reply->msg = timer->msg;
1470 reply->wparam = timer->id;
1471 reply->lparam = timer->lparam;
1472 reply->x = 0;
1473 reply->y = 0;
1474 reply->time = get_tick_count();
1475 reply->info = 0;
1476 return;
1479 done:
1480 set_error( STATUS_PENDING ); /* FIXME */
1484 /* reply to a sent message */
1485 DECL_HANDLER(reply_message)
1487 if (!current->queue)
1489 set_error( STATUS_ACCESS_DENIED );
1490 return;
1492 if (req->type == MSG_HARDWARE)
1494 struct thread_input *input = current->queue->input;
1495 if (input->msg_thread == current) release_hardware_message( current, req->remove );
1496 else set_error( STATUS_ACCESS_DENIED );
1498 else if (current->queue->recv_result)
1499 reply_message( current->queue, req->result, 0, req->remove,
1500 get_req_data(), get_req_data_size() );
1504 /* retrieve the reply for the last message sent */
1505 DECL_HANDLER(get_message_reply)
1507 struct message_result *result;
1508 struct list *entry;
1509 struct msg_queue *queue = current->queue;
1511 if (queue)
1513 set_error( STATUS_PENDING );
1514 reply->result = 0;
1516 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1518 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1519 if (result->replied || req->cancel)
1521 if (result->replied)
1523 reply->result = result->result;
1524 set_error( result->error );
1525 if (result->data)
1527 size_t data_len = min( result->data_size, get_reply_max_size() );
1528 set_reply_data_ptr( result->data, data_len );
1529 result->data = NULL;
1530 result->data_size = 0;
1533 remove_result_from_sender( result );
1535 entry = list_head( &queue->send_result );
1536 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1537 else
1539 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1540 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1544 else set_error( STATUS_ACCESS_DENIED );
1548 /* set a window timer */
1549 DECL_HANDLER(set_win_timer)
1551 struct timer *timer;
1552 struct msg_queue *queue = get_current_queue();
1553 user_handle_t win = get_user_full_handle( req->win );
1555 if (!queue) return;
1557 /* remove it if it existed already */
1558 if (win) kill_timer( queue, win, req->msg, req->id );
1560 if ((timer = set_timer( queue, req->rate )))
1562 timer->win = win;
1563 timer->msg = req->msg;
1564 timer->id = req->id;
1565 timer->lparam = req->lparam;
1569 /* kill a window timer */
1570 DECL_HANDLER(kill_win_timer)
1572 struct msg_queue *queue = current->queue;
1574 if (!queue || !kill_timer( queue, get_user_full_handle(req->win), req->msg, req->id ))
1575 set_error( STATUS_INVALID_PARAMETER );
1579 /* attach (or detach) thread inputs */
1580 DECL_HANDLER(attach_thread_input)
1582 struct thread *thread_from = get_thread_from_id( req->tid_from );
1583 struct thread *thread_to = get_thread_from_id( req->tid_to );
1585 if (!thread_from || !thread_to)
1587 if (thread_from) release_object( thread_from );
1588 if (thread_to) release_object( thread_to );
1589 return;
1591 if (thread_from != thread_to)
1593 if (req->attach) attach_thread_input( thread_from, thread_to );
1594 else detach_thread_input( thread_from, thread_to );
1596 else set_error( STATUS_ACCESS_DENIED );
1597 release_object( thread_from );
1598 release_object( thread_to );
1602 /* get thread input data */
1603 DECL_HANDLER(get_thread_input)
1605 struct thread *thread = NULL;
1606 struct thread_input *input;
1608 if (req->tid)
1610 if (!(thread = get_thread_from_id( req->tid ))) return;
1611 input = thread->queue ? thread->queue->input : NULL;
1613 else input = foreground_input; /* get the foreground thread info */
1615 if (input)
1617 reply->focus = input->focus;
1618 reply->capture = input->capture;
1619 reply->active = input->active;
1620 reply->menu_owner = input->menu_owner;
1621 reply->move_size = input->move_size;
1622 reply->caret = input->caret;
1623 reply->rect = input->caret_rect;
1625 else
1627 reply->focus = 0;
1628 reply->capture = 0;
1629 reply->active = 0;
1630 reply->menu_owner = 0;
1631 reply->move_size = 0;
1632 reply->caret = 0;
1633 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1635 /* foreground window is active window of foreground thread */
1636 reply->foreground = foreground_input ? foreground_input->active : 0;
1637 if (thread) release_object( thread );
1641 /* retrieve queue keyboard state for a given thread */
1642 DECL_HANDLER(get_key_state)
1644 struct thread *thread;
1645 struct thread_input *input;
1647 if (!(thread = get_thread_from_id( req->tid ))) return;
1648 input = thread->queue ? thread->queue->input : NULL;
1649 if (input)
1651 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1652 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1654 release_object( thread );
1658 /* set queue keyboard state for a given thread */
1659 DECL_HANDLER(set_key_state)
1661 struct thread *thread = NULL;
1662 struct thread_input *input;
1664 if (!(thread = get_thread_from_id( req->tid ))) return;
1665 input = thread->queue ? thread->queue->input : NULL;
1666 if (input)
1668 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1669 if (size) memcpy( input->keystate, get_req_data(), size );
1671 release_object( thread );
1675 /* set the system foreground window */
1676 DECL_HANDLER(set_foreground_window)
1678 struct msg_queue *queue = get_current_queue();
1680 reply->previous = foreground_input ? foreground_input->active : 0;
1681 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1682 reply->send_msg_new = FALSE;
1684 if (req->handle)
1686 struct thread *thread;
1688 if (is_top_level_window( req->handle ) &&
1689 ((thread = get_window_thread( req->handle ))))
1691 foreground_input = thread->queue->input;
1692 reply->send_msg_new = (foreground_input != queue->input);
1693 release_object( thread );
1695 else set_error( STATUS_INVALID_HANDLE );
1697 else foreground_input = NULL;
1701 /* set the current thread focus window */
1702 DECL_HANDLER(set_focus_window)
1704 struct msg_queue *queue = get_current_queue();
1706 reply->previous = 0;
1707 if (queue && check_queue_input_window( queue, req->handle ))
1709 reply->previous = queue->input->focus;
1710 queue->input->focus = get_user_full_handle( req->handle );
1715 /* set the current thread active window */
1716 DECL_HANDLER(set_active_window)
1718 struct msg_queue *queue = get_current_queue();
1720 reply->previous = 0;
1721 if (queue && check_queue_input_window( queue, req->handle ))
1723 if (!req->handle || make_window_active( req->handle ))
1725 reply->previous = queue->input->active;
1726 queue->input->active = get_user_full_handle( req->handle );
1728 else set_error( STATUS_INVALID_HANDLE );
1733 /* set the current thread capture window */
1734 DECL_HANDLER(set_capture_window)
1736 struct msg_queue *queue = get_current_queue();
1738 reply->previous = reply->full_handle = 0;
1739 if (queue && check_queue_input_window( queue, req->handle ))
1741 struct thread_input *input = queue->input;
1743 reply->previous = input->capture;
1744 input->capture = get_user_full_handle( req->handle );
1745 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1746 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1747 reply->full_handle = input->capture;
1752 /* Set the current thread caret window */
1753 DECL_HANDLER(set_caret_window)
1755 struct msg_queue *queue = get_current_queue();
1757 reply->previous = 0;
1758 if (queue && check_queue_input_window( queue, req->handle ))
1760 struct thread_input *input = queue->input;
1762 reply->previous = input->caret;
1763 reply->old_rect = input->caret_rect;
1764 reply->old_hide = input->caret_hide;
1765 reply->old_state = input->caret_state;
1767 set_caret_window( input, get_user_full_handle(req->handle) );
1768 input->caret_rect.right = req->width;
1769 input->caret_rect.bottom = req->height;
1774 /* Set the current thread caret information */
1775 DECL_HANDLER(set_caret_info)
1777 struct msg_queue *queue = get_current_queue();
1778 struct thread_input *input;
1780 if (!queue) return;
1781 input = queue->input;
1782 reply->full_handle = input->caret;
1783 reply->old_rect = input->caret_rect;
1784 reply->old_hide = input->caret_hide;
1785 reply->old_state = input->caret_state;
1787 if (req->handle && get_user_full_handle(req->handle) != input->caret)
1789 set_error( STATUS_ACCESS_DENIED );
1790 return;
1792 if (req->flags & SET_CARET_POS)
1794 input->caret_rect.right += req->x - input->caret_rect.left;
1795 input->caret_rect.bottom += req->y - input->caret_rect.top;
1796 input->caret_rect.left = req->x;
1797 input->caret_rect.top = req->y;
1799 if (req->flags & SET_CARET_HIDE)
1801 input->caret_hide += req->hide;
1802 if (input->caret_hide < 0) input->caret_hide = 0;
1804 if (req->flags & SET_CARET_STATE)
1806 if (req->state == -1) input->caret_state = !input->caret_state;
1807 else input->caret_state = !!req->state;