Fixed definition of MAX_COMPUTERNAME_LENGTH (spotted by Mike
[wine/multimedia.git] / server / queue.c
blob79460995578245412c9a641aee3cccda67b5dfb8
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 /* release the thread input data of a given thread */
208 static void release_thread_input( struct thread *thread )
210 struct thread_input *input = thread->queue->input;
212 if (!input) return;
213 if (input->msg_thread == thread)
215 release_object( input->msg_thread );
216 input->msg_thread = NULL;
217 input->msg = NULL;
219 release_object( input );
220 thread->queue->input = NULL;
223 /* create a message queue object */
224 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
226 struct msg_queue *queue;
227 int i;
229 if (!input && !(input = create_thread_input())) return NULL;
230 if ((queue = alloc_object( &msg_queue_ops )))
232 queue->wake_bits = 0;
233 queue->wake_mask = 0;
234 queue->changed_bits = 0;
235 queue->changed_mask = 0;
236 queue->paint_count = 0;
237 queue->recv_result = NULL;
238 queue->first_timer = NULL;
239 queue->last_timer = NULL;
240 queue->next_timer = NULL;
241 queue->timeout = NULL;
242 queue->input = (struct thread_input *)grab_object( input );
243 queue->hooks = NULL;
244 gettimeofday( &queue->last_get_msg, NULL );
245 list_init( &queue->send_result );
246 list_init( &queue->callback_result );
247 for (i = 0; i < NB_MSG_KINDS; i++)
248 queue->msg_list[i].first = queue->msg_list[i].last = NULL;
250 thread->queue = queue;
251 if (!thread->process->queue)
252 thread->process->queue = (struct msg_queue *)grab_object( queue );
254 release_object( input );
255 return queue;
258 /* free the message queue of a thread at thread exit */
259 void free_msg_queue( struct thread *thread )
261 struct process *process = thread->process;
263 remove_thread_hooks( thread );
264 if (!thread->queue) return;
265 if (process->queue == thread->queue) /* is it the process main queue? */
267 release_object( process->queue );
268 process->queue = NULL;
269 if (process->idle_event)
271 set_event( process->idle_event );
272 release_object( process->idle_event );
273 process->idle_event = NULL;
276 release_thread_input( thread );
277 release_object( thread->queue );
278 thread->queue = NULL;
281 /* get the hook table for a given thread */
282 struct hook_table *get_queue_hooks( struct thread *thread )
284 if (!thread->queue) return NULL;
285 return thread->queue->hooks;
288 /* set the hook table for a given thread, allocating the queue if needed */
289 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
291 struct msg_queue *queue = thread->queue;
292 if (!queue) queue = create_msg_queue( thread, NULL );
293 if (queue->hooks) release_object( queue->hooks );
294 queue->hooks = hooks;
297 /* check the queue status */
298 inline static int is_signaled( struct msg_queue *queue )
300 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
303 /* set some queue bits */
304 inline static void set_queue_bits( struct msg_queue *queue, unsigned int bits )
306 queue->wake_bits |= bits;
307 queue->changed_bits |= bits;
308 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
311 /* clear some queue bits */
312 inline static void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
314 queue->wake_bits &= ~bits;
315 queue->changed_bits &= ~bits;
318 /* check whether msg is a keyboard message */
319 inline static int is_keyboard_msg( struct message *msg )
321 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
324 /* get the QS_* bit corresponding to a given hardware message */
325 inline static int get_hardware_msg_bit( struct message *msg )
327 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
328 if (is_keyboard_msg( msg )) return QS_KEY;
329 return QS_MOUSEBUTTON;
332 /* get the current thread queue, creating it if needed */
333 inline static struct msg_queue *get_current_queue(void)
335 struct msg_queue *queue = current->queue;
336 if (!queue) queue = create_msg_queue( current, NULL );
337 return queue;
340 /* append a message to the end of a list */
341 inline static void append_message( struct message_list *list, struct message *msg )
343 msg->next = NULL;
344 if ((msg->prev = list->last)) msg->prev->next = msg;
345 else list->first = msg;
346 list->last = msg;
349 /* unlink a message from a list it */
350 inline static void unlink_message( struct message_list *list, struct message *msg )
352 if (msg->next) msg->next->prev = msg->prev;
353 else list->last = msg->prev;
354 if (msg->prev) msg->prev->next = msg->next;
355 else list->first = msg->next;
358 /* try to merge a message with the last in the list; return 1 if successful */
359 static int merge_message( struct thread_input *input, const struct message *msg )
361 struct message *prev = input->msg_list.last;
363 if (!prev) return 0;
364 if (input->msg == prev) return 0;
365 if (prev->result) return 0;
366 if (prev->win != msg->win) return 0;
367 if (prev->msg != msg->msg) return 0;
368 if (prev->type != msg->type) return 0;
369 /* now we can merge it */
370 prev->wparam = msg->wparam;
371 prev->lparam = msg->lparam;
372 prev->x = msg->x;
373 prev->y = msg->y;
374 prev->time = msg->time;
375 prev->info = msg->info;
376 return 1;
379 /* free a result structure */
380 static void free_result( struct message_result *result )
382 if (result->timeout) remove_timeout_user( result->timeout );
383 if (result->data) free( result->data );
384 if (result->callback_msg) free( result->callback_msg );
385 free( result );
388 /* remove the result from the sender list it is on */
389 static inline void remove_result_from_sender( struct message_result *result )
391 assert( result->sender );
393 list_remove( &result->sender_entry );
394 result->sender = NULL;
395 if (!result->receiver) free_result( result );
398 /* store the message result in the appropriate structure */
399 static void store_message_result( struct message_result *res, unsigned int result,
400 unsigned int error )
402 res->result = result;
403 res->error = error;
404 res->replied = 1;
405 if (res->timeout)
407 remove_timeout_user( res->timeout );
408 res->timeout = NULL;
410 if (res->sender)
412 if (res->callback_msg)
414 /* queue the callback message in the sender queue */
415 res->callback_msg->lparam = result;
416 append_message( &res->sender->msg_list[SEND_MESSAGE], res->callback_msg );
417 set_queue_bits( res->sender, QS_SENDMESSAGE );
418 res->callback_msg = NULL;
419 remove_result_from_sender( res );
421 else
423 /* wake sender queue if waiting on this result */
424 if (list_head(&res->sender->send_result) == &res->sender_entry)
425 set_queue_bits( res->sender, QS_SMRESULT );
431 /* free a message when deleting a queue or window */
432 static void free_message( struct message *msg )
434 struct message_result *result = msg->result;
435 if (result)
437 if (result->sender)
439 result->receiver = NULL;
440 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
442 else free_result( result );
444 if (msg->data) free( msg->data );
445 free( msg );
448 /* remove (and free) a message from a message list */
449 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
450 enum message_kind kind )
452 unlink_message( &queue->msg_list[kind], msg );
453 switch(kind)
455 case SEND_MESSAGE:
456 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_SENDMESSAGE );
457 break;
458 case POST_MESSAGE:
459 if (!queue->msg_list[kind].first) clear_queue_bits( queue, QS_POSTMESSAGE );
460 break;
462 free_message( msg );
465 /* message timed out without getting a reply */
466 static void result_timeout( void *private )
468 struct message_result *result = private;
470 assert( !result->replied );
472 result->timeout = NULL;
473 store_message_result( result, 0, STATUS_TIMEOUT );
476 /* allocate and fill a message result structure */
477 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
478 struct msg_queue *recv_queue,
479 struct message *msg, unsigned int timeout,
480 void *callback, unsigned int callback_data )
482 struct message_result *result = mem_alloc( sizeof(*result) );
483 if (result)
485 result->sender = send_queue;
486 result->receiver = recv_queue;
487 result->replied = 0;
488 result->data = NULL;
489 result->data_size = 0;
490 result->timeout = NULL;
492 if (msg->type == MSG_CALLBACK)
494 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
495 if (!callback_msg)
497 free( result );
498 return NULL;
500 callback_msg->type = MSG_CALLBACK_RESULT;
501 callback_msg->win = msg->win;
502 callback_msg->msg = msg->msg;
503 callback_msg->wparam = (unsigned int)callback;
504 callback_msg->lparam = 0;
505 callback_msg->time = get_tick_count();
506 callback_msg->x = 0;
507 callback_msg->y = 0;
508 callback_msg->info = callback_data;
509 callback_msg->result = NULL;
510 callback_msg->data = NULL;
511 callback_msg->data_size = 0;
513 result->callback_msg = callback_msg;
514 list_add_head( &send_queue->callback_result, &result->sender_entry );
516 else
518 result->callback_msg = NULL;
519 list_add_head( &send_queue->send_result, &result->sender_entry );
522 if (timeout != -1)
524 struct timeval when;
525 gettimeofday( &when, 0 );
526 add_timeout( &when, timeout );
527 result->timeout = add_timeout_user( &when, result_timeout, result );
530 return result;
533 /* receive a message, removing it from the sent queue */
534 static void receive_message( struct msg_queue *queue, struct message *msg,
535 struct get_message_reply *reply )
537 struct message_result *result = msg->result;
539 reply->total = msg->data_size;
540 if (msg->data_size > get_reply_max_size())
542 set_error( STATUS_BUFFER_OVERFLOW );
543 return;
545 reply->type = msg->type;
546 reply->win = msg->win;
547 reply->msg = msg->msg;
548 reply->wparam = msg->wparam;
549 reply->lparam = msg->lparam;
550 reply->x = msg->x;
551 reply->y = msg->y;
552 reply->time = msg->time;
553 reply->info = msg->info;
555 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
557 unlink_message( &queue->msg_list[SEND_MESSAGE], msg );
558 /* put the result on the receiver result stack */
559 if (result)
561 result->recv_next = queue->recv_result;
562 queue->recv_result = result;
564 free( msg );
565 if (!queue->msg_list[SEND_MESSAGE].first) clear_queue_bits( queue, QS_SENDMESSAGE );
568 /* set the result of the current received message */
569 static void reply_message( struct msg_queue *queue, unsigned int result,
570 unsigned int error, int remove, const void *data, size_t len )
572 struct message_result *res = queue->recv_result;
574 if (remove)
576 queue->recv_result = res->recv_next;
577 res->receiver = NULL;
578 if (!res->sender) /* no one waiting for it */
580 free_result( res );
581 return;
584 if (!res->replied)
586 if (len && (res->data = memdup( data, len ))) res->data_size = len;
587 store_message_result( res, result, error );
591 /* retrieve a posted message */
592 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
593 unsigned int first, unsigned int last, unsigned int flags,
594 struct get_message_reply *reply )
596 struct message *msg;
597 struct message_list *list = &queue->msg_list[POST_MESSAGE];
599 /* check against the filters */
600 for (msg = list->first; msg; msg = msg->next)
602 if (msg->msg == WM_QUIT) break; /* WM_QUIT is never filtered */
603 if (win && msg->win && msg->win != win && !is_child_window( win, msg->win )) continue;
604 if (msg->msg < first) continue;
605 if (msg->msg > last) continue;
606 break; /* found one */
608 if (!msg) return 0;
610 /* return it to the app */
612 reply->total = msg->data_size;
613 if (msg->data_size > get_reply_max_size())
615 set_error( STATUS_BUFFER_OVERFLOW );
616 return 1;
618 reply->type = msg->type;
619 reply->win = msg->win;
620 reply->msg = msg->msg;
621 reply->wparam = msg->wparam;
622 reply->lparam = msg->lparam;
623 reply->x = msg->x;
624 reply->y = msg->y;
625 reply->time = msg->time;
626 reply->info = msg->info;
628 if (flags & GET_MSG_REMOVE)
630 if (msg->data)
632 set_reply_data_ptr( msg->data, msg->data_size );
633 msg->data = NULL;
634 msg->data_size = 0;
636 remove_queue_message( queue, msg, POST_MESSAGE );
638 else if (msg->data) set_reply_data( msg->data, msg->data_size );
640 return 1;
643 /* empty a message list and free all the messages */
644 static void empty_msg_list( struct message_list *list )
646 struct message *msg = list->first;
647 while (msg)
649 struct message *next = msg->next;
650 free_message( msg );
651 msg = next;
655 /* cleanup all pending results when deleting a queue */
656 static void cleanup_results( struct msg_queue *queue )
658 struct list *entry;
660 while ((entry = list_head( &queue->send_result )) != NULL)
662 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
665 while ((entry = list_head( &queue->callback_result )) != NULL)
667 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
670 while (queue->recv_result)
671 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
674 /* check if the thread owning the queue is hung (not checking for messages) */
675 static int is_queue_hung( struct msg_queue *queue )
677 struct timeval now;
678 struct wait_queue_entry *entry;
680 gettimeofday( &now, NULL );
681 if (now.tv_sec - queue->last_get_msg.tv_sec <= 5)
682 return 0; /* less than 5 seconds since last get message -> not hung */
684 for (entry = queue->obj.head; entry; entry = entry->next)
686 if (entry->thread->queue == queue)
687 return 0; /* thread is waiting on queue -> not hung */
689 return 1;
692 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
694 struct msg_queue *queue = (struct msg_queue *)obj;
695 struct process *process = entry->thread->process;
697 /* a thread can only wait on its own queue */
698 if (entry->thread->queue != queue)
700 set_error( STATUS_ACCESS_DENIED );
701 return 0;
703 /* if waiting on the main process queue, set the idle event */
704 if (process->queue == queue)
706 if (process->idle_event) set_event( process->idle_event );
708 add_queue( obj, entry );
709 return 1;
712 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
714 struct msg_queue *queue = (struct msg_queue *)obj;
715 struct process *process = entry->thread->process;
717 remove_queue( obj, entry );
719 assert( entry->thread->queue == queue );
721 /* if waiting on the main process queue, reset the idle event */
722 if (process->queue == queue)
724 if (process->idle_event) reset_event( process->idle_event );
728 static void msg_queue_dump( struct object *obj, int verbose )
730 struct msg_queue *queue = (struct msg_queue *)obj;
731 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
732 queue->wake_bits, queue->wake_mask );
735 static int msg_queue_signaled( struct object *obj, struct thread *thread )
737 struct msg_queue *queue = (struct msg_queue *)obj;
738 return is_signaled( queue );
741 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
743 struct msg_queue *queue = (struct msg_queue *)obj;
744 queue->wake_mask = 0;
745 queue->changed_mask = 0;
746 return 0; /* Not abandoned */
749 static void msg_queue_destroy( struct object *obj )
751 struct msg_queue *queue = (struct msg_queue *)obj;
752 struct timer *timer = queue->first_timer;
753 int i;
755 cleanup_results( queue );
756 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
758 while (timer)
760 struct timer *next = timer->next;
761 free( timer );
762 timer = next;
764 if (queue->timeout) remove_timeout_user( queue->timeout );
765 if (queue->input) release_object( queue->input );
766 if (queue->hooks) release_object( queue->hooks );
769 static void thread_input_dump( struct object *obj, int verbose )
771 struct thread_input *input = (struct thread_input *)obj;
772 fprintf( stderr, "Thread input focus=%p capture=%p active=%p\n",
773 input->focus, input->capture, input->active );
776 static void thread_input_destroy( struct object *obj )
778 struct thread_input *input = (struct thread_input *)obj;
780 if (foreground_input == input) foreground_input = NULL;
781 if (input->msg_thread) release_object( input->msg_thread );
782 empty_msg_list( &input->msg_list );
785 /* fix the thread input data when a window is destroyed */
786 inline static void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
788 struct thread_input *input = queue->input;
790 if (window == input->focus) input->focus = 0;
791 if (window == input->capture) input->capture = 0;
792 if (window == input->active) input->active = 0;
793 if (window == input->menu_owner) input->menu_owner = 0;
794 if (window == input->move_size) input->move_size = 0;
795 if (window == input->caret) set_caret_window( input, 0 );
798 /* check if the specified window can be set in the input data of a given queue */
799 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
801 struct thread *thread;
802 int ret = 0;
804 if (!window) return 1; /* we can always clear the data */
806 if ((thread = get_window_thread( window )))
808 ret = (queue->input == thread->queue->input);
809 if (!ret) set_error( STATUS_ACCESS_DENIED );
810 release_object( thread );
812 else set_error( STATUS_INVALID_HANDLE );
814 return ret;
817 /* attach two thread input data structures */
818 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
820 struct thread_input *input;
822 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
823 input = (struct thread_input *)grab_object( thread_to->queue->input );
825 if (thread_from->queue)
827 release_thread_input( thread_from );
828 thread_from->queue->input = input;
830 else
832 if (!(thread_from->queue = create_msg_queue( thread_from, input ))) return 0;
834 memset( input->keystate, 0, sizeof(input->keystate) );
835 return 1;
838 /* detach two thread input data structures */
839 static void detach_thread_input( struct thread *thread_from, struct thread *thread_to )
841 struct thread_input *input;
843 if (!thread_from->queue || !thread_to->queue ||
844 thread_from->queue->input != thread_to->queue->input)
846 set_error( STATUS_ACCESS_DENIED );
847 return;
849 if ((input = create_thread_input()))
851 release_thread_input( thread_from );
852 thread_from->queue->input = input;
857 /* set the next timer to expire */
858 static void set_next_timer( struct msg_queue *queue, struct timer *timer )
860 if (queue->timeout)
862 remove_timeout_user( queue->timeout );
863 queue->timeout = NULL;
865 if ((queue->next_timer = timer))
866 queue->timeout = add_timeout_user( &timer->when, timer_callback, queue );
868 /* set/clear QS_TIMER bit */
869 if (queue->next_timer == queue->first_timer)
870 clear_queue_bits( queue, QS_TIMER );
871 else
872 set_queue_bits( queue, QS_TIMER );
875 /* callback for the next timer expiration */
876 static void timer_callback( void *private )
878 struct msg_queue *queue = private;
880 queue->timeout = NULL;
881 /* move on to the next timer */
882 set_next_timer( queue, queue->next_timer->next );
885 /* link a timer at its rightful place in the queue list */
886 static void link_timer( struct msg_queue *queue, struct timer *timer )
888 struct timer *pos = queue->next_timer;
890 while (pos && time_before( &pos->when, &timer->when )) pos = pos->next;
892 if (pos) /* insert before pos */
894 if ((timer->prev = pos->prev)) timer->prev->next = timer;
895 else queue->first_timer = timer;
896 timer->next = pos;
897 pos->prev = timer;
899 else /* insert at end */
901 timer->next = NULL;
902 timer->prev = queue->last_timer;
903 if (queue->last_timer) queue->last_timer->next = timer;
904 else queue->first_timer = timer;
905 queue->last_timer = timer;
907 /* check if we replaced the next timer */
908 if (pos == queue->next_timer) set_next_timer( queue, timer );
911 /* remove a timer from the queue timer list */
912 static void unlink_timer( struct msg_queue *queue, struct timer *timer )
914 if (timer->next) timer->next->prev = timer->prev;
915 else queue->last_timer = timer->prev;
916 if (timer->prev) timer->prev->next = timer->next;
917 else queue->first_timer = timer->next;
918 /* check if we removed the next timer */
919 if (queue->next_timer == timer) set_next_timer( queue, timer->next );
920 else if (queue->next_timer == queue->first_timer) clear_queue_bits( queue, QS_TIMER );
923 /* restart an expired timer */
924 static void restart_timer( struct msg_queue *queue, struct timer *timer )
926 struct timeval now;
927 unlink_timer( queue, timer );
928 gettimeofday( &now, 0 );
929 while (!time_before( &now, &timer->when )) add_timeout( &timer->when, timer->rate );
930 link_timer( queue, timer );
933 /* find an expired timer matching the filtering parameters */
934 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
935 unsigned int get_first, unsigned int get_last,
936 int remove )
938 struct timer *timer;
939 for (timer = queue->first_timer; (timer && timer != queue->next_timer); timer = timer->next)
941 if (win && timer->win != win) continue;
942 if (timer->msg >= get_first && timer->msg <= get_last)
944 if (remove) restart_timer( queue, timer );
945 return timer;
948 return NULL;
951 /* kill a timer */
952 static int kill_timer( struct msg_queue *queue, user_handle_t win,
953 unsigned int msg, unsigned int id )
955 struct timer *timer;
957 for (timer = queue->first_timer; timer; timer = timer->next)
959 if (timer->win != win || timer->msg != msg || timer->id != id) continue;
960 unlink_timer( queue, timer );
961 free( timer );
962 return 1;
964 return 0;
967 /* add a timer */
968 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
970 struct timer *timer = mem_alloc( sizeof(*timer) );
971 if (timer)
973 timer->rate = rate;
974 gettimeofday( &timer->when, 0 );
975 add_timeout( &timer->when, rate );
976 link_timer( queue, timer );
978 return timer;
981 /* change the input key state for a given key */
982 static void set_input_key_state( struct thread_input *input, unsigned char key, int down )
984 if (down)
986 if (!(input->keystate[key] & 0x80)) input->keystate[key] ^= 0x01;
987 input->keystate[key] |= 0x80;
989 else input->keystate[key] &= ~0x80;
992 /* update the input key state for a keyboard message */
993 static void update_input_key_state( struct thread_input *input, const struct message *msg )
995 unsigned char key;
996 int down = 0, extended;
998 switch (msg->msg)
1000 case WM_LBUTTONDOWN:
1001 down = 1;
1002 /* fall through */
1003 case WM_LBUTTONUP:
1004 set_input_key_state( input, VK_LBUTTON, down );
1005 break;
1006 case WM_MBUTTONDOWN:
1007 down = 1;
1008 /* fall through */
1009 case WM_MBUTTONUP:
1010 set_input_key_state( input, VK_MBUTTON, down );
1011 break;
1012 case WM_RBUTTONDOWN:
1013 down = 1;
1014 /* fall through */
1015 case WM_RBUTTONUP:
1016 set_input_key_state( input, VK_RBUTTON, down );
1017 break;
1018 case WM_KEYDOWN:
1019 case WM_SYSKEYDOWN:
1020 down = 1;
1021 /* fall through */
1022 case WM_KEYUP:
1023 case WM_SYSKEYUP:
1024 key = (unsigned char)msg->wparam;
1025 extended = ((msg->lparam >> 16) & KF_EXTENDED) != 0;
1026 set_input_key_state( input, key, down );
1027 switch(key)
1029 case VK_SHIFT:
1030 set_input_key_state( input, extended ? VK_RSHIFT : VK_LSHIFT, down );
1031 break;
1032 case VK_CONTROL:
1033 set_input_key_state( input, extended ? VK_RCONTROL : VK_LCONTROL, down );
1034 break;
1035 case VK_MENU:
1036 set_input_key_state( input, extended ? VK_RMENU : VK_LMENU, down );
1037 break;
1039 break;
1043 /* release the hardware message currently being processed by the given thread */
1044 static void release_hardware_message( struct thread *thread, int remove )
1046 struct thread_input *input = thread->queue->input;
1048 if (input->msg_thread != thread) return;
1049 if (remove)
1051 struct message *other;
1052 int clr_bit;
1054 update_input_key_state( input, input->msg );
1055 unlink_message( &input->msg_list, input->msg );
1056 clr_bit = get_hardware_msg_bit( input->msg );
1057 for (other = input->msg_list.first; other; other = other->next)
1058 if (get_hardware_msg_bit( other ) == clr_bit) break;
1059 if (!other) clear_queue_bits( thread->queue, clr_bit );
1060 free_message( input->msg );
1062 release_object( input->msg_thread );
1063 input->msg = NULL;
1064 input->msg_thread = NULL;
1067 /* find the window that should receive a given hardware message */
1068 static user_handle_t find_hardware_message_window( struct thread_input *input, struct message *msg,
1069 unsigned int *msg_code )
1071 user_handle_t win = 0;
1073 *msg_code = msg->msg;
1074 if (is_keyboard_msg( msg ))
1076 if (input && !(win = input->focus))
1078 win = input->active;
1079 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1082 else /* mouse message */
1084 if (!input || !(win = input->capture))
1086 if (!(win = msg->win)) win = window_from_point( msg->x, msg->y );
1089 return win;
1092 /* queue a hardware message into a given thread input */
1093 static void queue_hardware_message( struct msg_queue *queue, struct message *msg )
1095 user_handle_t win;
1096 struct thread *thread;
1097 struct thread_input *input;
1098 unsigned int msg_code;
1100 win = find_hardware_message_window( queue ? queue->input : foreground_input, msg, &msg_code );
1101 if (!win || !(thread = get_window_thread(win)))
1103 free( msg );
1104 return;
1106 input = thread->queue->input;
1108 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1109 else
1111 append_message( &input->msg_list, msg );
1112 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1114 release_object( thread );
1117 /* find a hardware message for the given queue */
1118 static int get_hardware_message( struct thread *thread, struct message *first,
1119 user_handle_t filter_win, struct get_message_reply *reply )
1121 struct thread_input *input = thread->queue->input;
1122 struct thread *win_thread;
1123 struct message *msg;
1124 user_handle_t win;
1125 int clear_bits, got_one = 0;
1126 unsigned int msg_code;
1128 if (input->msg_thread && input->msg_thread != thread)
1129 return 0; /* locked by another thread */
1131 if (!first)
1133 msg = input->msg_list.first;
1134 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1136 else
1138 msg = first->next;
1139 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1142 while (msg)
1144 win = find_hardware_message_window( input, msg, &msg_code );
1145 if (!win || !(win_thread = get_window_thread( win )))
1147 /* no window at all, remove it */
1148 struct message *next = msg->next;
1149 update_input_key_state( input, msg );
1150 unlink_message( &input->msg_list, msg );
1151 free_message( msg );
1152 msg = next;
1153 continue;
1155 if (win_thread != thread)
1157 /* wake the other thread */
1158 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1159 release_object( win_thread );
1160 got_one = 1;
1161 msg = msg->next;
1162 continue;
1164 /* if we already got a message for another thread, or if it doesn't
1165 * match the filter we skip it (filter is only checked for keyboard
1166 * messages since the dest window for a mouse message depends on hittest)
1168 if (got_one ||
1169 (filter_win && is_keyboard_msg(msg) &&
1170 win != filter_win && !is_child_window( filter_win, win )))
1172 clear_bits &= ~get_hardware_msg_bit( msg );
1173 release_object( win_thread );
1174 msg = msg->next;
1175 continue;
1177 /* now we can return it */
1178 if (!input->msg_thread) input->msg_thread = win_thread;
1179 else release_object( win_thread );
1180 input->msg = msg;
1182 reply->type = MSG_HARDWARE;
1183 reply->win = win;
1184 reply->msg = msg_code;
1185 reply->wparam = msg->wparam;
1186 reply->lparam = msg->lparam;
1187 reply->x = msg->x;
1188 reply->y = msg->y;
1189 reply->time = msg->time;
1190 reply->info = msg->info;
1191 return 1;
1193 /* nothing found, clear the hardware queue bits */
1194 clear_queue_bits( thread->queue, clear_bits );
1195 if (input->msg_thread) release_object( input->msg_thread );
1196 input->msg = NULL;
1197 input->msg_thread = NULL;
1198 return 0;
1201 /* increment (or decrement if 'incr' is negative) the queue paint count */
1202 void inc_queue_paint_count( struct thread *thread, int incr )
1204 struct msg_queue *queue = thread->queue;
1206 assert( queue );
1208 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1210 if (queue->paint_count)
1211 set_queue_bits( queue, QS_PAINT );
1212 else
1213 clear_queue_bits( queue, QS_PAINT );
1217 /* remove all messages and timers belonging to a certain window */
1218 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1220 struct msg_queue *queue = thread->queue;
1221 struct timer *timer;
1222 struct message *msg;
1223 int i;
1225 if (!queue) return;
1227 /* remove timers */
1228 timer = queue->first_timer;
1229 while (timer)
1231 struct timer *next = timer->next;
1232 if (timer->win == win)
1234 unlink_timer( queue, timer );
1235 free( timer );
1237 timer = next;
1240 /* remove messages */
1241 for (i = 0; i < NB_MSG_KINDS; i++)
1243 msg = queue->msg_list[i].first;
1244 while (msg)
1246 struct message *next = msg->next;
1247 if (msg->win == win) remove_queue_message( queue, msg, i );
1248 msg = next;
1252 thread_input_cleanup_window( queue, win );
1255 /* post a message to a window; used by socket handling */
1256 void post_message( user_handle_t win, unsigned int message,
1257 unsigned int wparam, unsigned int lparam )
1259 struct message *msg;
1260 struct thread *thread = get_window_thread( win );
1262 if (!thread) return;
1264 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1266 msg->type = MSG_POSTED;
1267 msg->win = get_user_full_handle( win );
1268 msg->msg = message;
1269 msg->wparam = wparam;
1270 msg->lparam = lparam;
1271 msg->time = get_tick_count();
1272 msg->x = 0;
1273 msg->y = 0;
1274 msg->info = 0;
1275 msg->result = NULL;
1276 msg->data = NULL;
1277 msg->data_size = 0;
1279 append_message( &thread->queue->msg_list[POST_MESSAGE], msg );
1280 set_queue_bits( thread->queue, QS_POSTMESSAGE );
1282 release_object( thread );
1286 /* get the message queue of the current thread */
1287 DECL_HANDLER(get_msg_queue)
1289 struct msg_queue *queue = get_current_queue();
1291 reply->handle = 0;
1292 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1296 /* set the current message queue wakeup mask */
1297 DECL_HANDLER(set_queue_mask)
1299 struct msg_queue *queue = get_current_queue();
1301 if (queue)
1303 queue->wake_mask = req->wake_mask;
1304 queue->changed_mask = req->changed_mask;
1305 reply->wake_bits = queue->wake_bits;
1306 reply->changed_bits = queue->changed_bits;
1307 if (is_signaled( queue ))
1309 /* if skip wait is set, do what would have been done in the subsequent wait */
1310 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1311 else wake_up( &queue->obj, 0 );
1317 /* get the current message queue status */
1318 DECL_HANDLER(get_queue_status)
1320 struct msg_queue *queue = current->queue;
1321 if (queue)
1323 reply->wake_bits = queue->wake_bits;
1324 reply->changed_bits = queue->changed_bits;
1325 if (req->clear) queue->changed_bits = 0;
1327 else reply->wake_bits = reply->changed_bits = 0;
1331 /* send a message to a thread queue */
1332 DECL_HANDLER(send_message)
1334 struct message *msg;
1335 struct msg_queue *send_queue = get_current_queue();
1336 struct msg_queue *recv_queue = NULL;
1337 struct thread *thread = NULL;
1339 if (req->id)
1341 if (!(thread = get_thread_from_id( req->id ))) return;
1343 else if (req->type != MSG_HARDWARE)
1345 /* only hardware messages are allowed without destination thread */
1346 set_error( STATUS_INVALID_PARAMETER );
1347 return;
1350 if (thread && !(recv_queue = thread->queue))
1352 set_error( STATUS_INVALID_PARAMETER );
1353 release_object( thread );
1354 return;
1356 if (recv_queue && (req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1358 set_error( STATUS_TIMEOUT );
1359 release_object( thread );
1360 return;
1363 if ((msg = mem_alloc( sizeof(*msg) )))
1365 msg->type = req->type;
1366 msg->win = get_user_full_handle( req->win );
1367 msg->msg = req->msg;
1368 msg->wparam = req->wparam;
1369 msg->lparam = req->lparam;
1370 msg->time = req->time;
1371 msg->x = req->x;
1372 msg->y = req->y;
1373 msg->info = req->info;
1374 msg->result = NULL;
1375 msg->data = NULL;
1376 msg->data_size = 0;
1378 switch(msg->type)
1380 case MSG_OTHER_PROCESS:
1381 msg->data_size = get_req_data_size();
1382 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1384 free( msg );
1385 break;
1387 /* fall through */
1388 case MSG_ASCII:
1389 case MSG_UNICODE:
1390 case MSG_CALLBACK:
1391 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg,
1392 req->timeout, req->callback, req->info )))
1394 free_message( msg );
1395 break;
1397 /* fall through */
1398 case MSG_NOTIFY:
1399 append_message( &recv_queue->msg_list[SEND_MESSAGE], msg );
1400 set_queue_bits( recv_queue, QS_SENDMESSAGE );
1401 break;
1402 case MSG_POSTED:
1403 /* needed for posted DDE messages */
1404 msg->data_size = get_req_data_size();
1405 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1407 free( msg );
1408 break;
1410 append_message( &recv_queue->msg_list[POST_MESSAGE], msg );
1411 set_queue_bits( recv_queue, QS_POSTMESSAGE );
1412 break;
1413 case MSG_HARDWARE:
1414 queue_hardware_message( recv_queue, msg );
1415 break;
1416 case MSG_CALLBACK_RESULT: /* cannot send this one */
1417 default:
1418 set_error( STATUS_INVALID_PARAMETER );
1419 free( msg );
1420 break;
1423 if (thread) release_object( thread );
1427 /* get a message from the current queue */
1428 DECL_HANDLER(get_message)
1430 struct timer *timer;
1431 struct message *msg;
1432 struct message *first_hw_msg = NULL;
1433 struct msg_queue *queue = get_current_queue();
1434 user_handle_t get_win = get_user_full_handle( req->get_win );
1436 if (!queue) return;
1437 gettimeofday( &queue->last_get_msg, NULL );
1439 /* first of all release the hardware input lock if we own it */
1440 /* we'll grab it again if we find a hardware message */
1441 if (queue->input->msg_thread == current)
1443 first_hw_msg = queue->input->msg;
1444 release_hardware_message( current, 0 );
1447 /* first check for sent messages */
1448 if ((msg = queue->msg_list[SEND_MESSAGE].first))
1450 receive_message( queue, msg, reply );
1451 return;
1453 if (req->flags & GET_MSG_SENT_ONLY) goto done; /* nothing else to check */
1455 /* clear changed bits so we can wait on them if we don't find a message */
1456 queue->changed_bits = 0;
1458 /* then check for posted messages */
1459 if (get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
1460 return;
1462 /* then check for any raw hardware message */
1463 if (get_hardware_message( current, first_hw_msg, get_win, reply ))
1464 return;
1466 /* now check for WM_PAINT */
1467 if (queue->paint_count &&
1468 (WM_PAINT >= req->get_first) && (WM_PAINT <= req->get_last) &&
1469 (reply->win = find_window_to_repaint( get_win, current )))
1471 reply->type = MSG_POSTED;
1472 reply->msg = WM_PAINT;
1473 reply->wparam = 0;
1474 reply->lparam = 0;
1475 reply->x = 0;
1476 reply->y = 0;
1477 reply->time = get_tick_count();
1478 reply->info = 0;
1479 return;
1482 /* now check for timer */
1483 if ((timer = find_expired_timer( queue, get_win, req->get_first,
1484 req->get_last, (req->flags & GET_MSG_REMOVE) )))
1486 reply->type = MSG_POSTED;
1487 reply->win = timer->win;
1488 reply->msg = timer->msg;
1489 reply->wparam = timer->id;
1490 reply->lparam = timer->lparam;
1491 reply->x = 0;
1492 reply->y = 0;
1493 reply->time = get_tick_count();
1494 reply->info = 0;
1495 return;
1498 done:
1499 set_error( STATUS_PENDING ); /* FIXME */
1503 /* reply to a sent message */
1504 DECL_HANDLER(reply_message)
1506 if (!current->queue)
1508 set_error( STATUS_ACCESS_DENIED );
1509 return;
1511 if (req->type == MSG_HARDWARE)
1513 struct thread_input *input = current->queue->input;
1514 if (input->msg_thread == current) release_hardware_message( current, req->remove );
1515 else set_error( STATUS_ACCESS_DENIED );
1517 else if (current->queue->recv_result)
1518 reply_message( current->queue, req->result, 0, req->remove,
1519 get_req_data(), get_req_data_size() );
1523 /* retrieve the reply for the last message sent */
1524 DECL_HANDLER(get_message_reply)
1526 struct message_result *result;
1527 struct list *entry;
1528 struct msg_queue *queue = current->queue;
1530 if (queue)
1532 set_error( STATUS_PENDING );
1533 reply->result = 0;
1535 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
1537 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1538 if (result->replied || req->cancel)
1540 if (result->replied)
1542 reply->result = result->result;
1543 set_error( result->error );
1544 if (result->data)
1546 size_t data_len = min( result->data_size, get_reply_max_size() );
1547 set_reply_data_ptr( result->data, data_len );
1548 result->data = NULL;
1549 result->data_size = 0;
1552 remove_result_from_sender( result );
1554 entry = list_head( &queue->send_result );
1555 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
1556 else
1558 result = LIST_ENTRY( entry, struct message_result, sender_entry );
1559 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
1563 else set_error( STATUS_ACCESS_DENIED );
1567 /* set a window timer */
1568 DECL_HANDLER(set_win_timer)
1570 struct timer *timer;
1571 struct msg_queue *queue = get_current_queue();
1572 user_handle_t win = get_user_full_handle( req->win );
1574 if (!queue) return;
1576 /* remove it if it existed already */
1577 if (win) kill_timer( queue, win, req->msg, req->id );
1579 if ((timer = set_timer( queue, req->rate )))
1581 timer->win = win;
1582 timer->msg = req->msg;
1583 timer->id = req->id;
1584 timer->lparam = req->lparam;
1588 /* kill a window timer */
1589 DECL_HANDLER(kill_win_timer)
1591 struct msg_queue *queue = current->queue;
1593 if (!queue || !kill_timer( queue, get_user_full_handle(req->win), req->msg, req->id ))
1594 set_error( STATUS_INVALID_PARAMETER );
1598 /* attach (or detach) thread inputs */
1599 DECL_HANDLER(attach_thread_input)
1601 struct thread *thread_from = get_thread_from_id( req->tid_from );
1602 struct thread *thread_to = get_thread_from_id( req->tid_to );
1604 if (!thread_from || !thread_to)
1606 if (thread_from) release_object( thread_from );
1607 if (thread_to) release_object( thread_to );
1608 return;
1610 if (thread_from != thread_to)
1612 if (req->attach) attach_thread_input( thread_from, thread_to );
1613 else detach_thread_input( thread_from, thread_to );
1615 else set_error( STATUS_ACCESS_DENIED );
1616 release_object( thread_from );
1617 release_object( thread_to );
1621 /* get thread input data */
1622 DECL_HANDLER(get_thread_input)
1624 struct thread *thread = NULL;
1625 struct thread_input *input;
1627 if (req->tid)
1629 if (!(thread = get_thread_from_id( req->tid ))) return;
1630 input = thread->queue ? thread->queue->input : NULL;
1632 else input = foreground_input; /* get the foreground thread info */
1634 if (input)
1636 reply->focus = input->focus;
1637 reply->capture = input->capture;
1638 reply->active = input->active;
1639 reply->menu_owner = input->menu_owner;
1640 reply->move_size = input->move_size;
1641 reply->caret = input->caret;
1642 reply->rect = input->caret_rect;
1644 else
1646 reply->focus = 0;
1647 reply->capture = 0;
1648 reply->active = 0;
1649 reply->menu_owner = 0;
1650 reply->move_size = 0;
1651 reply->caret = 0;
1652 reply->rect.left = reply->rect.top = reply->rect.right = reply->rect.bottom = 0;
1654 /* foreground window is active window of foreground thread */
1655 reply->foreground = foreground_input ? foreground_input->active : 0;
1656 if (thread) release_object( thread );
1660 /* retrieve queue keyboard state for a given thread */
1661 DECL_HANDLER(get_key_state)
1663 struct thread *thread;
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 if (req->key >= 0) reply->state = input->keystate[req->key & 0xff];
1671 set_reply_data( input->keystate, min( get_reply_max_size(), sizeof(input->keystate) ));
1673 release_object( thread );
1677 /* set queue keyboard state for a given thread */
1678 DECL_HANDLER(set_key_state)
1680 struct thread *thread = NULL;
1681 struct thread_input *input;
1683 if (!(thread = get_thread_from_id( req->tid ))) return;
1684 input = thread->queue ? thread->queue->input : NULL;
1685 if (input)
1687 size_t size = min( sizeof(input->keystate), get_req_data_size() );
1688 if (size) memcpy( input->keystate, get_req_data(), size );
1690 release_object( thread );
1694 /* set the system foreground window */
1695 DECL_HANDLER(set_foreground_window)
1697 struct msg_queue *queue = get_current_queue();
1699 reply->previous = foreground_input ? foreground_input->active : 0;
1700 reply->send_msg_old = (reply->previous && foreground_input != queue->input);
1701 reply->send_msg_new = FALSE;
1703 if (req->handle)
1705 struct thread *thread;
1707 if (is_top_level_window( req->handle ) &&
1708 ((thread = get_window_thread( req->handle ))))
1710 foreground_input = thread->queue->input;
1711 reply->send_msg_new = (foreground_input != queue->input);
1712 release_object( thread );
1714 else set_error( STATUS_INVALID_HANDLE );
1716 else foreground_input = NULL;
1720 /* set the current thread focus window */
1721 DECL_HANDLER(set_focus_window)
1723 struct msg_queue *queue = get_current_queue();
1725 reply->previous = 0;
1726 if (queue && check_queue_input_window( queue, req->handle ))
1728 reply->previous = queue->input->focus;
1729 queue->input->focus = get_user_full_handle( req->handle );
1734 /* set the current thread active window */
1735 DECL_HANDLER(set_active_window)
1737 struct msg_queue *queue = get_current_queue();
1739 reply->previous = 0;
1740 if (queue && check_queue_input_window( queue, req->handle ))
1742 if (!req->handle || make_window_active( req->handle ))
1744 reply->previous = queue->input->active;
1745 queue->input->active = get_user_full_handle( req->handle );
1747 else set_error( STATUS_INVALID_HANDLE );
1752 /* set the current thread capture window */
1753 DECL_HANDLER(set_capture_window)
1755 struct msg_queue *queue = get_current_queue();
1757 reply->previous = reply->full_handle = 0;
1758 if (queue && check_queue_input_window( queue, req->handle ))
1760 struct thread_input *input = queue->input;
1762 reply->previous = input->capture;
1763 input->capture = get_user_full_handle( req->handle );
1764 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
1765 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
1766 reply->full_handle = input->capture;
1771 /* Set the current thread caret window */
1772 DECL_HANDLER(set_caret_window)
1774 struct msg_queue *queue = get_current_queue();
1776 reply->previous = 0;
1777 if (queue && check_queue_input_window( queue, req->handle ))
1779 struct thread_input *input = queue->input;
1781 reply->previous = input->caret;
1782 reply->old_rect = input->caret_rect;
1783 reply->old_hide = input->caret_hide;
1784 reply->old_state = input->caret_state;
1786 set_caret_window( input, get_user_full_handle(req->handle) );
1787 input->caret_rect.right = req->width;
1788 input->caret_rect.bottom = req->height;
1793 /* Set the current thread caret information */
1794 DECL_HANDLER(set_caret_info)
1796 struct msg_queue *queue = get_current_queue();
1797 struct thread_input *input;
1799 if (!queue) return;
1800 input = queue->input;
1801 reply->full_handle = input->caret;
1802 reply->old_rect = input->caret_rect;
1803 reply->old_hide = input->caret_hide;
1804 reply->old_state = input->caret_state;
1806 if (req->handle && get_user_full_handle(req->handle) != input->caret)
1808 set_error( STATUS_ACCESS_DENIED );
1809 return;
1811 if (req->flags & SET_CARET_POS)
1813 input->caret_rect.right += req->x - input->caret_rect.left;
1814 input->caret_rect.bottom += req->y - input->caret_rect.top;
1815 input->caret_rect.left = req->x;
1816 input->caret_rect.top = req->y;
1818 if (req->flags & SET_CARET_HIDE)
1820 input->caret_hide += req->hide;
1821 if (input->caret_hide < 0) input->caret_hide = 0;
1823 if (req->flags & SET_CARET_STATE)
1825 if (req->state == -1) input->caret_state = !input->caret_state;
1826 else input->caret_state = !!req->state;