winex11.drv: Take care of the destroyed drawable in wglFlush/wglFinish too.
[wine.git] / server / queue.c
blob2e1168d80a9014a6aad39999556cb487a40c26f5
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "winternl.h"
37 #include "handle.h"
38 #include "file.h"
39 #include "thread.h"
40 #include "process.h"
41 #include "request.h"
42 #include "user.h"
44 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
45 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
47 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
48 #define NB_MSG_KINDS (POST_MESSAGE+1)
51 struct message_result
53 struct list sender_entry; /* entry in sender list */
54 struct message *msg; /* message the result is for */
55 struct message_result *recv_next; /* next in receiver list */
56 struct msg_queue *sender; /* sender queue */
57 struct msg_queue *receiver; /* receiver queue */
58 int replied; /* has it been replied to? */
59 unsigned int error; /* error code to pass back to sender */
60 lparam_t result; /* reply result */
61 struct message *hardware_msg; /* hardware message if low-level hook result */
62 struct desktop *desktop; /* desktop for hardware message */
63 struct message *callback_msg; /* message to queue for callback */
64 void *data; /* message reply data */
65 unsigned int data_size; /* size of message reply data */
66 struct timeout_user *timeout; /* result timeout */
69 struct message
71 struct list entry; /* entry in message list */
72 enum message_type type; /* message type */
73 user_handle_t win; /* window handle */
74 unsigned int msg; /* message code */
75 lparam_t wparam; /* parameters */
76 lparam_t lparam; /* parameters */
77 unsigned int time; /* message time */
78 void *data; /* message data for sent messages */
79 unsigned int data_size; /* size of message data */
80 unsigned int unique_id; /* unique id for nested hw message waits */
81 struct message_result *result; /* result in sender queue */
84 struct timer
86 struct list entry; /* entry in timer list */
87 timeout_t when; /* next expiration */
88 unsigned int rate; /* timer rate in ms */
89 user_handle_t win; /* window handle */
90 unsigned int msg; /* message to post */
91 lparam_t id; /* timer id */
92 lparam_t lparam; /* lparam for message */
95 struct thread_input
97 struct object obj; /* object header */
98 struct desktop *desktop; /* desktop that this thread input belongs to */
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 user_handle_t cursor; /* current cursor */
109 int cursor_count; /* cursor show count */
110 struct 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 struct fd *fd; /* optional file descriptor to poll */
118 unsigned int wake_bits; /* wakeup bits */
119 unsigned int wake_mask; /* wakeup mask */
120 unsigned int changed_bits; /* changed wakeup bits */
121 unsigned int changed_mask; /* changed wakeup mask */
122 int paint_count; /* pending paint messages count */
123 int quit_message; /* is there a pending quit message? */
124 int exit_code; /* exit code of pending quit message */
125 int cursor_count; /* per-queue cursor show count */
126 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
127 struct list send_result; /* stack of sent messages waiting for result */
128 struct list callback_result; /* list of callback messages waiting for result */
129 struct message_result *recv_result; /* stack of received messages waiting for result */
130 struct list pending_timers; /* list of pending timers */
131 struct list expired_timers; /* list of expired timers */
132 lparam_t next_timer_id; /* id for the next timer with a 0 window */
133 struct timeout_user *timeout; /* timeout for next timer to expire */
134 struct thread_input *input; /* thread input descriptor */
135 struct hook_table *hooks; /* hook table */
136 timeout_t last_get_msg; /* time of last get message call */
139 static void msg_queue_dump( struct object *obj, int verbose );
140 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
141 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
142 static int msg_queue_signaled( struct object *obj, struct thread *thread );
143 static int msg_queue_satisfied( struct object *obj, struct thread *thread );
144 static void msg_queue_destroy( struct object *obj );
145 static void msg_queue_poll_event( struct fd *fd, int event );
146 static void thread_input_dump( struct object *obj, int verbose );
147 static void thread_input_destroy( struct object *obj );
148 static void timer_callback( void *private );
150 static const struct object_ops msg_queue_ops =
152 sizeof(struct msg_queue), /* size */
153 msg_queue_dump, /* dump */
154 no_get_type, /* get_type */
155 msg_queue_add_queue, /* add_queue */
156 msg_queue_remove_queue, /* remove_queue */
157 msg_queue_signaled, /* signaled */
158 msg_queue_satisfied, /* satisfied */
159 no_signal, /* signal */
160 no_get_fd, /* get_fd */
161 no_map_access, /* map_access */
162 default_get_sd, /* get_sd */
163 default_set_sd, /* set_sd */
164 no_lookup_name, /* lookup_name */
165 no_open_file, /* open_file */
166 no_close_handle, /* close_handle */
167 msg_queue_destroy /* destroy */
170 static const struct fd_ops msg_queue_fd_ops =
172 NULL, /* get_poll_events */
173 msg_queue_poll_event, /* poll_event */
174 NULL, /* flush */
175 NULL, /* get_fd_type */
176 NULL, /* ioctl */
177 NULL, /* queue_async */
178 NULL, /* reselect_async */
179 NULL /* cancel async */
183 static const struct object_ops thread_input_ops =
185 sizeof(struct thread_input), /* size */
186 thread_input_dump, /* dump */
187 no_get_type, /* get_type */
188 no_add_queue, /* add_queue */
189 NULL, /* remove_queue */
190 NULL, /* signaled */
191 NULL, /* satisfied */
192 no_signal, /* signal */
193 no_get_fd, /* get_fd */
194 no_map_access, /* map_access */
195 default_get_sd, /* get_sd */
196 default_set_sd, /* set_sd */
197 no_lookup_name, /* lookup_name */
198 no_open_file, /* open_file */
199 no_close_handle, /* close_handle */
200 thread_input_destroy /* destroy */
203 /* pointer to input structure of foreground thread */
204 static unsigned int last_input_time;
206 static void queue_hardware_message( struct desktop *desktop, struct message *msg );
207 static void free_message( struct message *msg );
209 /* set the caret window in a given thread input */
210 static void set_caret_window( struct thread_input *input, user_handle_t win )
212 if (!win || win != input->caret)
214 input->caret_rect.left = 0;
215 input->caret_rect.top = 0;
216 input->caret_rect.right = 0;
217 input->caret_rect.bottom = 0;
219 input->caret = win;
220 input->caret_hide = 1;
221 input->caret_state = 0;
224 /* create a thread input object */
225 static struct thread_input *create_thread_input( struct thread *thread )
227 struct thread_input *input;
229 if ((input = alloc_object( &thread_input_ops )))
231 input->focus = 0;
232 input->capture = 0;
233 input->active = 0;
234 input->menu_owner = 0;
235 input->move_size = 0;
236 input->cursor = 0;
237 input->cursor_count = 0;
238 list_init( &input->msg_list );
239 set_caret_window( input, 0 );
240 memset( input->keystate, 0, sizeof(input->keystate) );
242 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
244 release_object( input );
245 return NULL;
248 return input;
251 /* create a message queue object */
252 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
254 struct thread_input *new_input = NULL;
255 struct msg_queue *queue;
256 int i;
258 if (!input)
260 if (!(new_input = create_thread_input( thread ))) return NULL;
261 input = new_input;
264 if ((queue = alloc_object( &msg_queue_ops )))
266 queue->fd = NULL;
267 queue->wake_bits = 0;
268 queue->wake_mask = 0;
269 queue->changed_bits = 0;
270 queue->changed_mask = 0;
271 queue->paint_count = 0;
272 queue->quit_message = 0;
273 queue->cursor_count = 0;
274 queue->recv_result = NULL;
275 queue->next_timer_id = 0x7fff;
276 queue->timeout = NULL;
277 queue->input = (struct thread_input *)grab_object( input );
278 queue->hooks = NULL;
279 queue->last_get_msg = current_time;
280 list_init( &queue->send_result );
281 list_init( &queue->callback_result );
282 list_init( &queue->pending_timers );
283 list_init( &queue->expired_timers );
284 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
286 thread->queue = queue;
288 if (new_input) release_object( new_input );
289 return queue;
292 /* free the message queue of a thread at thread exit */
293 void free_msg_queue( struct thread *thread )
295 remove_thread_hooks( thread );
296 if (!thread->queue) return;
297 release_object( thread->queue );
298 thread->queue = NULL;
301 /* change the thread input data of a given thread */
302 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
304 struct msg_queue *queue = thread->queue;
306 if (!queue)
308 thread->queue = create_msg_queue( thread, new_input );
309 return thread->queue != NULL;
311 if (queue->input)
313 queue->input->cursor_count -= queue->cursor_count;
314 release_object( queue->input );
316 queue->input = (struct thread_input *)grab_object( new_input );
317 new_input->cursor_count += queue->cursor_count;
318 return 1;
321 /* change the foreground input and reset the cursor clip rect */
322 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
324 if (desktop->foreground_input == input) return;
325 get_top_window_rectangle( desktop, &desktop->cursor_clip );
326 desktop->foreground_input = input;
329 /* get the hook table for a given thread */
330 struct hook_table *get_queue_hooks( struct thread *thread )
332 if (!thread->queue) return NULL;
333 return thread->queue->hooks;
336 /* set the hook table for a given thread, allocating the queue if needed */
337 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
339 struct msg_queue *queue = thread->queue;
340 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
341 if (queue->hooks) release_object( queue->hooks );
342 queue->hooks = hooks;
345 /* check the queue status */
346 static inline int is_signaled( struct msg_queue *queue )
348 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
351 /* set some queue bits */
352 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
354 queue->wake_bits |= bits;
355 queue->changed_bits |= bits;
356 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
359 /* clear some queue bits */
360 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
362 queue->wake_bits &= ~bits;
363 queue->changed_bits &= ~bits;
366 /* check whether msg is a keyboard message */
367 static inline int is_keyboard_msg( struct message *msg )
369 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
372 /* check if message is matched by the filter */
373 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
375 return (msg >= first && msg <= last);
378 /* check whether a message filter contains at least one potential hardware message */
379 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
381 /* hardware message ranges are (in numerical order):
382 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
383 * WM_KEYFIRST .. WM_KEYLAST
384 * WM_MOUSEFIRST .. WM_MOUSELAST
386 if (last < WM_NCMOUSEFIRST) return 0;
387 if (first > WM_NCMOUSELAST && last < WM_KEYFIRST) return 0;
388 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
389 if (first > WM_MOUSELAST) return 0;
390 return 1;
393 /* get the QS_* bit corresponding to a given hardware message */
394 static inline int get_hardware_msg_bit( struct message *msg )
396 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
397 if (is_keyboard_msg( msg )) return QS_KEY;
398 return QS_MOUSEBUTTON;
401 /* get the current thread queue, creating it if needed */
402 static inline struct msg_queue *get_current_queue(void)
404 struct msg_queue *queue = current->queue;
405 if (!queue) queue = create_msg_queue( current, NULL );
406 return queue;
409 /* get a (pseudo-)unique id to tag hardware messages */
410 static inline unsigned int get_unique_id(void)
412 static unsigned int id;
413 if (!++id) id = 1; /* avoid an id of 0 */
414 return id;
417 /* try to merge a message with the last in the list; return 1 if successful */
418 static int merge_message( struct thread_input *input, const struct message *msg )
420 struct message *prev;
421 struct list *ptr = list_tail( &input->msg_list );
423 if (!ptr) return 0;
424 prev = LIST_ENTRY( ptr, struct message, entry );
425 if (prev->result) return 0;
426 if (prev->win && msg->win && prev->win != msg->win) return 0;
427 if (prev->msg != msg->msg) return 0;
428 if (prev->type != msg->type) return 0;
429 /* now we can merge it */
430 prev->wparam = msg->wparam;
431 prev->lparam = msg->lparam;
432 prev->time = msg->time;
433 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
435 struct hardware_msg_data *prev_data = prev->data;
436 struct hardware_msg_data *msg_data = msg->data;
437 prev_data->x = msg_data->x;
438 prev_data->y = msg_data->y;
439 prev_data->info = msg_data->info;
441 return 1;
444 /* free a result structure */
445 static void free_result( struct message_result *result )
447 if (result->timeout) remove_timeout_user( result->timeout );
448 free( result->data );
449 if (result->callback_msg) free_message( result->callback_msg );
450 if (result->hardware_msg) free_message( result->hardware_msg );
451 if (result->desktop) release_object( result->desktop );
452 free( result );
455 /* remove the result from the sender list it is on */
456 static inline void remove_result_from_sender( struct message_result *result )
458 assert( result->sender );
460 list_remove( &result->sender_entry );
461 result->sender = NULL;
462 if (!result->receiver) free_result( result );
465 /* store the message result in the appropriate structure */
466 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
468 res->result = result;
469 res->error = error;
470 res->replied = 1;
471 if (res->timeout)
473 remove_timeout_user( res->timeout );
474 res->timeout = NULL;
477 if (res->hardware_msg)
479 if (!error && result) /* rejected by the hook */
480 free_message( res->hardware_msg );
481 else
482 queue_hardware_message( res->desktop, res->hardware_msg );
484 res->hardware_msg = NULL;
487 if (res->sender)
489 if (res->callback_msg)
491 /* queue the callback message in the sender queue */
492 struct callback_msg_data *data = res->callback_msg->data;
493 data->result = result;
494 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
495 set_queue_bits( res->sender, QS_SENDMESSAGE );
496 res->callback_msg = NULL;
497 remove_result_from_sender( res );
499 else
501 /* wake sender queue if waiting on this result */
502 if (list_head(&res->sender->send_result) == &res->sender_entry)
503 set_queue_bits( res->sender, QS_SMRESULT );
506 else if (!res->receiver) free_result( res );
509 /* free a message when deleting a queue or window */
510 static void free_message( struct message *msg )
512 struct message_result *result = msg->result;
513 if (result)
515 result->msg = NULL;
516 result->receiver = NULL;
517 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
519 free( msg->data );
520 free( msg );
523 /* remove (and free) a message from a message list */
524 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
525 enum message_kind kind )
527 list_remove( &msg->entry );
528 switch(kind)
530 case SEND_MESSAGE:
531 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
532 break;
533 case POST_MESSAGE:
534 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
535 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
536 break;
538 free_message( msg );
541 /* message timed out without getting a reply */
542 static void result_timeout( void *private )
544 struct message_result *result = private;
546 assert( !result->replied );
548 result->timeout = NULL;
550 if (result->msg) /* not received yet */
552 struct message *msg = result->msg;
554 result->msg = NULL;
555 msg->result = NULL;
556 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
557 result->receiver = NULL;
559 store_message_result( result, 0, STATUS_TIMEOUT );
562 /* allocate and fill a message result structure */
563 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
564 struct msg_queue *recv_queue,
565 struct message *msg, timeout_t timeout )
567 struct message_result *result = mem_alloc( sizeof(*result) );
568 if (result)
570 result->msg = msg;
571 result->sender = send_queue;
572 result->receiver = recv_queue;
573 result->replied = 0;
574 result->data = NULL;
575 result->data_size = 0;
576 result->timeout = NULL;
577 result->hardware_msg = NULL;
578 result->desktop = NULL;
579 result->callback_msg = NULL;
581 if (msg->type == MSG_CALLBACK)
583 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
585 if (!callback_msg)
587 free( result );
588 return NULL;
590 callback_msg->type = MSG_CALLBACK_RESULT;
591 callback_msg->win = msg->win;
592 callback_msg->msg = msg->msg;
593 callback_msg->wparam = 0;
594 callback_msg->lparam = 0;
595 callback_msg->time = get_tick_count();
596 callback_msg->result = NULL;
597 /* steal the data from the original message */
598 callback_msg->data = msg->data;
599 callback_msg->data_size = msg->data_size;
600 msg->data = NULL;
601 msg->data_size = 0;
603 result->callback_msg = callback_msg;
604 list_add_head( &send_queue->callback_result, &result->sender_entry );
606 else if (send_queue) list_add_head( &send_queue->send_result, &result->sender_entry );
608 if (timeout != TIMEOUT_INFINITE)
609 result->timeout = add_timeout_user( timeout, result_timeout, result );
611 return result;
614 /* receive a message, removing it from the sent queue */
615 static void receive_message( struct msg_queue *queue, struct message *msg,
616 struct get_message_reply *reply )
618 struct message_result *result = msg->result;
620 reply->total = msg->data_size;
621 if (msg->data_size > get_reply_max_size())
623 set_error( STATUS_BUFFER_OVERFLOW );
624 return;
626 reply->type = msg->type;
627 reply->win = msg->win;
628 reply->msg = msg->msg;
629 reply->wparam = msg->wparam;
630 reply->lparam = msg->lparam;
631 reply->time = msg->time;
633 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
635 list_remove( &msg->entry );
636 /* put the result on the receiver result stack */
637 if (result)
639 result->msg = NULL;
640 result->recv_next = queue->recv_result;
641 queue->recv_result = result;
643 free( msg );
644 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
647 /* set the result of the current received message */
648 static void reply_message( struct msg_queue *queue, lparam_t result,
649 unsigned int error, int remove, const void *data, data_size_t len )
651 struct message_result *res = queue->recv_result;
653 if (remove)
655 queue->recv_result = res->recv_next;
656 res->receiver = NULL;
657 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
659 free_result( res );
660 return;
663 if (!res->replied)
665 if (len && (res->data = memdup( data, len ))) res->data_size = len;
666 store_message_result( res, result, error );
670 static int match_window( user_handle_t win, user_handle_t msg_win )
672 if (!win) return 1;
673 if (win == -1 || win == 1) return !msg_win;
674 if (msg_win == win) return 1;
675 return is_child_window( win, msg_win );
678 /* retrieve a posted message */
679 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
680 unsigned int first, unsigned int last, unsigned int flags,
681 struct get_message_reply *reply )
683 struct message *msg;
685 /* check against the filters */
686 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
688 if (!match_window( win, msg->win )) continue;
689 if (!check_msg_filter( msg->msg, first, last )) continue;
690 goto found; /* found one */
692 return 0;
694 /* return it to the app */
695 found:
696 reply->total = msg->data_size;
697 if (msg->data_size > get_reply_max_size())
699 set_error( STATUS_BUFFER_OVERFLOW );
700 return 1;
702 reply->type = msg->type;
703 reply->win = msg->win;
704 reply->msg = msg->msg;
705 reply->wparam = msg->wparam;
706 reply->lparam = msg->lparam;
707 reply->time = msg->time;
709 if (flags & PM_REMOVE)
711 if (msg->data)
713 set_reply_data_ptr( msg->data, msg->data_size );
714 msg->data = NULL;
715 msg->data_size = 0;
717 remove_queue_message( queue, msg, POST_MESSAGE );
719 else if (msg->data) set_reply_data( msg->data, msg->data_size );
721 return 1;
724 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
725 struct get_message_reply *reply )
727 if (queue->quit_message)
729 reply->total = 0;
730 reply->type = MSG_POSTED;
731 reply->win = 0;
732 reply->msg = WM_QUIT;
733 reply->wparam = queue->exit_code;
734 reply->lparam = 0;
735 reply->time = get_tick_count();
737 if (flags & PM_REMOVE)
739 queue->quit_message = 0;
740 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
741 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
743 return 1;
745 else
746 return 0;
749 /* empty a message list and free all the messages */
750 static void empty_msg_list( struct list *list )
752 struct list *ptr;
754 while ((ptr = list_head( list )) != NULL)
756 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
757 list_remove( &msg->entry );
758 free_message( msg );
762 /* cleanup all pending results when deleting a queue */
763 static void cleanup_results( struct msg_queue *queue )
765 struct list *entry;
767 while ((entry = list_head( &queue->send_result )) != NULL)
769 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
772 while ((entry = list_head( &queue->callback_result )) != NULL)
774 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
777 while (queue->recv_result)
778 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
781 /* check if the thread owning the queue is hung (not checking for messages) */
782 static int is_queue_hung( struct msg_queue *queue )
784 struct wait_queue_entry *entry;
786 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
787 return 0; /* less than 5 seconds since last get message -> not hung */
789 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
791 if (entry->thread->queue == queue)
792 return 0; /* thread is waiting on queue -> not hung */
794 return 1;
797 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
799 struct msg_queue *queue = (struct msg_queue *)obj;
800 struct process *process = entry->thread->process;
802 /* a thread can only wait on its own queue */
803 if (entry->thread->queue != queue)
805 set_error( STATUS_ACCESS_DENIED );
806 return 0;
808 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
810 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
811 set_fd_events( queue->fd, POLLIN );
812 add_queue( obj, entry );
813 return 1;
816 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
818 struct msg_queue *queue = (struct msg_queue *)obj;
820 remove_queue( obj, entry );
821 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
822 set_fd_events( queue->fd, 0 );
825 static void msg_queue_dump( struct object *obj, int verbose )
827 struct msg_queue *queue = (struct msg_queue *)obj;
828 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
829 queue->wake_bits, queue->wake_mask );
832 static int msg_queue_signaled( struct object *obj, struct thread *thread )
834 struct msg_queue *queue = (struct msg_queue *)obj;
835 int ret = 0;
837 if (queue->fd)
839 if ((ret = check_fd_events( queue->fd, POLLIN )))
840 /* stop waiting on select() if we are signaled */
841 set_fd_events( queue->fd, 0 );
842 else if (!list_empty( &obj->wait_queue ))
843 /* restart waiting on poll() if we are no longer signaled */
844 set_fd_events( queue->fd, POLLIN );
846 return ret || is_signaled( queue );
849 static int msg_queue_satisfied( struct object *obj, struct thread *thread )
851 struct msg_queue *queue = (struct msg_queue *)obj;
852 queue->wake_mask = 0;
853 queue->changed_mask = 0;
854 return 0; /* Not abandoned */
857 static void msg_queue_destroy( struct object *obj )
859 struct msg_queue *queue = (struct msg_queue *)obj;
860 struct list *ptr;
861 int i;
863 cleanup_results( queue );
864 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
866 while ((ptr = list_head( &queue->pending_timers )))
868 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
869 list_remove( &timer->entry );
870 free( timer );
872 while ((ptr = list_head( &queue->expired_timers )))
874 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
875 list_remove( &timer->entry );
876 free( timer );
878 if (queue->timeout) remove_timeout_user( queue->timeout );
879 if (queue->input)
881 queue->input->cursor_count -= queue->cursor_count;
882 release_object( queue->input );
884 if (queue->hooks) release_object( queue->hooks );
885 if (queue->fd) release_object( queue->fd );
888 static void msg_queue_poll_event( struct fd *fd, int event )
890 struct msg_queue *queue = get_fd_user( fd );
891 assert( queue->obj.ops == &msg_queue_ops );
893 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
894 else set_fd_events( queue->fd, 0 );
895 wake_up( &queue->obj, 0 );
898 static void thread_input_dump( struct object *obj, int verbose )
900 struct thread_input *input = (struct thread_input *)obj;
901 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
902 input->focus, input->capture, input->active );
905 static void thread_input_destroy( struct object *obj )
907 struct thread_input *input = (struct thread_input *)obj;
909 empty_msg_list( &input->msg_list );
910 if (input->desktop)
912 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
913 release_object( input->desktop );
917 /* fix the thread input data when a window is destroyed */
918 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
920 struct thread_input *input = queue->input;
922 if (window == input->focus) input->focus = 0;
923 if (window == input->capture) input->capture = 0;
924 if (window == input->active) input->active = 0;
925 if (window == input->menu_owner) input->menu_owner = 0;
926 if (window == input->move_size) input->move_size = 0;
927 if (window == input->caret) set_caret_window( input, 0 );
930 /* check if the specified window can be set in the input data of a given queue */
931 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
933 struct thread *thread;
934 int ret = 0;
936 if (!window) return 1; /* we can always clear the data */
938 if ((thread = get_window_thread( window )))
940 ret = (queue->input == thread->queue->input);
941 if (!ret) set_error( STATUS_ACCESS_DENIED );
942 release_object( thread );
944 else set_error( STATUS_INVALID_HANDLE );
946 return ret;
949 /* make sure the specified thread has a queue */
950 int init_thread_queue( struct thread *thread )
952 if (thread->queue) return 1;
953 return (create_msg_queue( thread, NULL ) != NULL);
956 /* attach two thread input data structures */
957 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
959 struct desktop *desktop;
960 struct thread_input *input;
961 int ret;
963 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
964 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
965 input = (struct thread_input *)grab_object( thread_to->queue->input );
966 if (input->desktop != desktop)
968 set_error( STATUS_ACCESS_DENIED );
969 release_object( input );
970 release_object( desktop );
971 return 0;
973 release_object( desktop );
975 ret = assign_thread_input( thread_from, input );
976 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
977 release_object( input );
978 return ret;
981 /* detach two thread input data structures */
982 void detach_thread_input( struct thread *thread_from )
984 struct thread_input *input;
986 if ((input = create_thread_input( thread_from )))
988 assign_thread_input( thread_from, input );
989 release_object( input );
994 /* set the next timer to expire */
995 static void set_next_timer( struct msg_queue *queue )
997 struct list *ptr;
999 if (queue->timeout)
1001 remove_timeout_user( queue->timeout );
1002 queue->timeout = NULL;
1004 if ((ptr = list_head( &queue->pending_timers )))
1006 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1007 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1009 /* set/clear QS_TIMER bit */
1010 if (list_empty( &queue->expired_timers ))
1011 clear_queue_bits( queue, QS_TIMER );
1012 else
1013 set_queue_bits( queue, QS_TIMER );
1016 /* find a timer from its window and id */
1017 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1018 unsigned int msg, lparam_t id )
1020 struct list *ptr;
1022 /* we need to search both lists */
1024 LIST_FOR_EACH( ptr, &queue->pending_timers )
1026 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1027 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1029 LIST_FOR_EACH( ptr, &queue->expired_timers )
1031 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1032 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1034 return NULL;
1037 /* callback for the next timer expiration */
1038 static void timer_callback( void *private )
1040 struct msg_queue *queue = private;
1041 struct list *ptr;
1043 queue->timeout = NULL;
1044 /* move on to the next timer */
1045 ptr = list_head( &queue->pending_timers );
1046 list_remove( ptr );
1047 list_add_tail( &queue->expired_timers, ptr );
1048 set_next_timer( queue );
1051 /* link a timer at its rightful place in the queue list */
1052 static void link_timer( struct msg_queue *queue, struct timer *timer )
1054 struct list *ptr;
1056 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1058 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1059 if (t->when >= timer->when) break;
1061 list_add_before( ptr, &timer->entry );
1064 /* remove a timer from the queue timer list and free it */
1065 static void free_timer( struct msg_queue *queue, struct timer *timer )
1067 list_remove( &timer->entry );
1068 free( timer );
1069 set_next_timer( queue );
1072 /* restart an expired timer */
1073 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1075 list_remove( &timer->entry );
1076 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1077 link_timer( queue, timer );
1078 set_next_timer( queue );
1081 /* find an expired timer matching the filtering parameters */
1082 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1083 unsigned int get_first, unsigned int get_last,
1084 int remove )
1086 struct list *ptr;
1088 LIST_FOR_EACH( ptr, &queue->expired_timers )
1090 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1091 if (win && timer->win != win) continue;
1092 if (check_msg_filter( timer->msg, get_first, get_last ))
1094 if (remove) restart_timer( queue, timer );
1095 return timer;
1098 return NULL;
1101 /* add a timer */
1102 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1104 struct timer *timer = mem_alloc( sizeof(*timer) );
1105 if (timer)
1107 timer->rate = max( rate, 1 );
1108 timer->when = current_time + (timeout_t)timer->rate * 10000;
1109 link_timer( queue, timer );
1110 /* check if we replaced the next timer */
1111 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1113 return timer;
1116 /* change the input key state for a given key */
1117 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1119 if (down)
1121 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1122 keystate[key] |= down;
1124 else keystate[key] &= ~0x80;
1127 /* update the input key state for a keyboard message */
1128 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1129 const struct message *msg )
1131 unsigned char key;
1132 int down = 0;
1134 switch (msg->msg)
1136 case WM_LBUTTONDOWN:
1137 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1138 /* fall through */
1139 case WM_LBUTTONUP:
1140 set_input_key_state( keystate, VK_LBUTTON, down );
1141 break;
1142 case WM_MBUTTONDOWN:
1143 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1144 /* fall through */
1145 case WM_MBUTTONUP:
1146 set_input_key_state( keystate, VK_MBUTTON, down );
1147 break;
1148 case WM_RBUTTONDOWN:
1149 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1150 /* fall through */
1151 case WM_RBUTTONUP:
1152 set_input_key_state( keystate, VK_RBUTTON, down );
1153 break;
1154 case WM_XBUTTONDOWN:
1155 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1156 /* fall through */
1157 case WM_XBUTTONUP:
1158 if (msg->wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1159 else if (msg->wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1160 break;
1161 case WM_KEYDOWN:
1162 case WM_SYSKEYDOWN:
1163 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1164 /* fall through */
1165 case WM_KEYUP:
1166 case WM_SYSKEYUP:
1167 key = (unsigned char)msg->wparam;
1168 set_input_key_state( keystate, key, down );
1169 switch(key)
1171 case VK_LCONTROL:
1172 case VK_RCONTROL:
1173 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1174 set_input_key_state( keystate, VK_CONTROL, down );
1175 break;
1176 case VK_LMENU:
1177 case VK_RMENU:
1178 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1179 set_input_key_state( keystate, VK_MENU, down );
1180 break;
1181 case VK_LSHIFT:
1182 case VK_RSHIFT:
1183 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1184 set_input_key_state( keystate, VK_SHIFT, down );
1185 break;
1187 break;
1191 /* release the hardware message currently being processed by the given thread */
1192 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1193 int remove, user_handle_t new_win )
1195 struct thread_input *input = queue->input;
1196 struct message *msg;
1198 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1200 if (msg->unique_id == hw_id) break;
1202 if (&msg->entry == &input->msg_list) return; /* not found */
1204 /* clear the queue bit for that message */
1205 if (remove || new_win)
1207 struct message *other;
1208 int clr_bit;
1210 clr_bit = get_hardware_msg_bit( msg );
1211 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1213 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1215 clr_bit = 0;
1216 break;
1219 if (clr_bit) clear_queue_bits( queue, clr_bit );
1222 if (new_win) /* set the new window */
1224 struct thread *owner = get_window_thread( new_win );
1225 if (owner)
1227 msg->win = new_win;
1228 if (owner->queue->input != input)
1230 list_remove( &msg->entry );
1231 if (msg->msg == WM_MOUSEMOVE && merge_message( owner->queue->input, msg ))
1233 free_message( msg );
1234 release_object( owner );
1235 return;
1237 list_add_tail( &owner->queue->input->msg_list, &msg->entry );
1239 set_queue_bits( owner->queue, get_hardware_msg_bit( msg ));
1240 remove = 0;
1241 release_object( owner );
1244 if (remove)
1246 update_input_key_state( input->desktop, input->keystate, msg );
1247 list_remove( &msg->entry );
1248 free_message( msg );
1252 /* find the window that should receive a given hardware message */
1253 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1254 struct message *msg, unsigned int *msg_code )
1256 struct hardware_msg_data *data = msg->data;
1257 user_handle_t win = 0;
1259 *msg_code = msg->msg;
1260 if (is_keyboard_msg( msg ))
1262 if (input && !(win = input->focus))
1264 win = input->active;
1265 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1268 else /* mouse message */
1270 if (!input || !(win = input->capture))
1272 if (!(win = msg->win) || !is_window_visible( win ) || is_window_transparent( win ))
1273 win = window_from_point( desktop, data->x, data->y );
1276 return win;
1279 /* set the cursor position, clipping to the cursor clip rect */
1280 static void set_cursor_pos( struct desktop *desktop, int x, int y )
1282 desktop->cursor_x = min( max( x, desktop->cursor_clip.left ), desktop->cursor_clip.right - 1 );
1283 desktop->cursor_y = min( max( y, desktop->cursor_clip.top ), desktop->cursor_clip.bottom - 1 );
1286 /* queue a hardware message into a given thread input */
1287 static void queue_hardware_message( struct desktop *desktop, struct message *msg )
1289 user_handle_t win;
1290 struct thread *thread;
1291 struct thread_input *input;
1292 unsigned int msg_code;
1293 struct hardware_msg_data *data = msg->data;
1295 update_input_key_state( desktop, desktop->keystate, msg );
1296 last_input_time = get_tick_count();
1298 if (is_keyboard_msg( msg ))
1300 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1301 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1302 msg->lparam &= ~(KF_EXTENDED << 16);
1304 else
1306 if (msg->msg == WM_MOUSEMOVE) set_cursor_pos( desktop, data->x, data->y );
1307 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1308 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1309 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1310 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1311 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1312 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1313 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1315 data->x = desktop->cursor_x;
1316 data->y = desktop->cursor_y;
1318 if (msg->win && (thread = get_window_thread( msg->win )))
1320 input = thread->queue->input;
1321 release_object( thread );
1323 else input = desktop->foreground_input;
1325 win = find_hardware_message_window( desktop, input, msg, &msg_code );
1326 if (!win || !(thread = get_window_thread(win)))
1328 if (input) update_input_key_state( input->desktop, input->keystate, msg );
1329 free( msg );
1330 return;
1332 input = thread->queue->input;
1334 if (msg->msg == WM_MOUSEMOVE && merge_message( input, msg )) free( msg );
1335 else
1337 msg->unique_id = 0; /* will be set once we return it to the app */
1338 list_add_tail( &input->msg_list, &msg->entry );
1339 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1341 release_object( thread );
1344 /* send the low-level hook message for a given hardware message */
1345 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1346 const hw_input_t *input, struct msg_queue *sender )
1348 struct thread *hook_thread;
1349 struct msg_queue *queue;
1350 struct message *msg;
1351 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1352 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1354 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1355 if (!(queue = hook_thread->queue)) return 0;
1357 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1359 msg->type = MSG_HOOK_LL;
1360 msg->win = 0;
1361 msg->msg = id;
1362 msg->wparam = hardware_msg->msg;
1363 msg->time = hardware_msg->time;
1364 msg->data_size = hardware_msg->data_size;
1365 msg->result = NULL;
1367 if (input->type == INPUT_KEYBOARD)
1369 unsigned short vkey = input->kbd.vkey;
1370 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1371 msg->lparam = (input->kbd.scan << 16) | vkey;
1373 else msg->lparam = input->mouse.data << 16;
1375 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1376 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1378 free_message( msg );
1379 return 0;
1381 msg->result->hardware_msg = hardware_msg;
1382 msg->result->desktop = (struct desktop *)grab_object( desktop );
1383 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1384 set_queue_bits( queue, QS_SENDMESSAGE );
1385 return 1;
1388 /* queue a hardware message for a mouse event */
1389 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1390 unsigned int hook_flags, struct msg_queue *sender )
1392 struct hardware_msg_data *msg_data;
1393 struct message *msg;
1394 unsigned int i, time, flags;
1395 int wait = 0, x, y;
1397 static const unsigned int messages[] =
1399 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1400 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1401 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1402 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1403 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1404 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1405 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1406 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1407 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1408 0, /* 0x0200 = unused */
1409 0, /* 0x0400 = unused */
1410 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1411 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1414 flags = input->mouse.flags;
1415 time = input->mouse.time;
1416 if (!time) time = get_tick_count();
1418 if (flags & MOUSEEVENTF_MOVE)
1420 if (flags & MOUSEEVENTF_ABSOLUTE)
1422 x = input->mouse.x;
1423 y = input->mouse.y;
1424 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1425 x == desktop->cursor_x && y == desktop->cursor_y)
1426 flags &= ~MOUSEEVENTF_MOVE;
1428 else
1430 x = desktop->cursor_x + input->mouse.x;
1431 y = desktop->cursor_y + input->mouse.y;
1434 else
1436 x = desktop->cursor_x;
1437 y = desktop->cursor_y;
1440 for (i = 0; i < sizeof(messages)/sizeof(messages[0]); i++)
1442 if (!messages[i]) continue;
1443 if (!(flags & (1 << i))) continue;
1444 flags &= ~(1 << i);
1446 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1447 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1449 free( msg );
1450 return 0;
1452 memset( msg_data, 0, sizeof(*msg_data) );
1454 msg->type = MSG_HARDWARE;
1455 msg->win = get_user_full_handle( win );
1456 msg->msg = messages[i];
1457 msg->wparam = input->mouse.data << 16;
1458 msg->lparam = 0;
1459 msg->time = time;
1460 msg->result = NULL;
1461 msg->data = msg_data;
1462 msg->data_size = sizeof(*msg_data);
1463 msg_data->x = x;
1464 msg_data->y = y;
1465 msg_data->info = input->mouse.info;
1466 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLMHF_INJECTED;
1468 /* specify a sender only when sending the last message */
1469 if (!(flags & ((1 << sizeof(messages)/sizeof(messages[0])) - 1)))
1471 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1472 queue_hardware_message( desktop, msg );
1474 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1475 queue_hardware_message( desktop, msg );
1477 return wait;
1480 /* queue a hardware message for a keyboard event */
1481 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1482 unsigned int hook_flags, struct msg_queue *sender )
1484 struct hardware_msg_data *msg_data;
1485 struct message *msg;
1486 unsigned char vkey = input->kbd.vkey;
1487 int wait;
1489 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1490 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1492 free( msg );
1493 return 0;
1495 memset( msg_data, 0, sizeof(*msg_data) );
1497 msg->type = MSG_HARDWARE;
1498 msg->win = get_user_full_handle( win );
1499 msg->lparam = (input->kbd.scan << 16) | 1; /* repeat count */
1500 msg->time = input->kbd.time;
1501 msg->result = NULL;
1502 msg->data = msg_data;
1503 msg->data_size = sizeof(*msg_data);
1504 msg_data->info = input->kbd.info;
1505 if (!msg->time) msg->time = get_tick_count();
1506 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLKHF_INJECTED;
1508 if (input->kbd.flags & KEYEVENTF_UNICODE)
1510 msg->wparam = VK_PACKET;
1512 else
1514 unsigned int flags = 0;
1515 switch (vkey)
1517 case VK_MENU:
1518 case VK_LMENU:
1519 case VK_RMENU:
1520 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1521 break;
1522 case VK_CONTROL:
1523 case VK_LCONTROL:
1524 case VK_RCONTROL:
1525 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1526 break;
1527 case VK_SHIFT:
1528 case VK_LSHIFT:
1529 case VK_RSHIFT:
1530 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1531 break;
1533 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1534 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1535 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1536 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1538 msg->wparam = vkey;
1539 msg->lparam |= flags << 16;
1540 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1543 msg->msg = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1545 switch (vkey)
1547 case VK_LMENU:
1548 case VK_RMENU:
1549 if (input->kbd.flags & KEYEVENTF_KEYUP)
1551 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1552 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1553 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1554 msg->msg = WM_SYSKEYUP;
1555 desktop->keystate[VK_MENU] &= ~0x02;
1557 else
1559 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1560 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1561 msg->msg = WM_SYSKEYDOWN;
1562 desktop->keystate[VK_MENU] |= 0x02;
1564 break;
1566 case VK_LCONTROL:
1567 case VK_RCONTROL:
1568 /* send WM_SYSKEYUP on release if Alt still pressed */
1569 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1570 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1571 msg->msg = WM_SYSKEYUP;
1572 desktop->keystate[VK_MENU] &= ~0x02;
1573 break;
1575 default:
1576 /* send WM_SYSKEY for Alt-anykey and for F10 */
1577 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1578 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1579 /* fall through */
1580 case VK_F10:
1581 msg->msg = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1582 desktop->keystate[VK_MENU] &= ~0x02;
1583 break;
1585 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1586 queue_hardware_message( desktop, msg );
1588 return wait;
1591 /* queue a hardware message for a custom type of event */
1592 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1593 const hw_input_t *input )
1595 struct hardware_msg_data *msg_data;
1596 struct message *msg;
1598 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
1599 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1601 free( msg );
1602 return;
1604 memset( msg_data, 0, sizeof(*msg_data) );
1606 msg->type = MSG_HARDWARE;
1607 msg->win = get_user_full_handle( win );
1608 msg->msg = input->hw.msg;
1609 msg->wparam = 0;
1610 msg->lparam = input->hw.lparam;
1611 msg->time = get_tick_count();
1612 msg->result = NULL;
1613 msg->data = msg_data;
1614 msg->data_size = sizeof(*msg_data);
1616 queue_hardware_message( desktop, msg );
1619 /* check message filter for a hardware message */
1620 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1621 user_handle_t filter_win, unsigned int first, unsigned int last )
1623 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1625 /* we can only test the window for a keyboard message since the
1626 * dest window for a mouse message depends on hittest */
1627 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1628 return 0;
1629 /* the message code is final for a keyboard message, we can simply check it */
1630 return check_msg_filter( msg_code, first, last );
1632 else /* mouse message */
1634 /* we need to check all possible values that the message can have in the end */
1636 if (check_msg_filter( msg_code, first, last )) return 1;
1637 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1639 /* all other messages can become non-client messages */
1640 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1642 /* clicks can become double-clicks or non-client double-clicks */
1643 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1644 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1646 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1647 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1649 return 0;
1654 /* find a hardware message for the given queue */
1655 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1656 unsigned int first, unsigned int last, struct get_message_reply *reply )
1658 struct thread_input *input = thread->queue->input;
1659 struct thread *win_thread;
1660 struct list *ptr;
1661 user_handle_t win;
1662 int clear_bits, got_one = 0;
1663 unsigned int msg_code;
1665 ptr = list_head( &input->msg_list );
1666 if (hw_id)
1668 while (ptr)
1670 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1671 if (msg->unique_id == hw_id) break;
1672 ptr = list_next( &input->msg_list, ptr );
1674 if (!ptr) ptr = list_head( &input->msg_list );
1675 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1678 if (ptr == list_head( &input->msg_list ))
1679 clear_bits = QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON;
1680 else
1681 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1683 while (ptr)
1685 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1686 struct hardware_msg_data *data = msg->data;
1688 ptr = list_next( &input->msg_list, ptr );
1689 win = find_hardware_message_window( input->desktop, input, msg, &msg_code );
1690 if (!win || !(win_thread = get_window_thread( win )))
1692 /* no window at all, remove it */
1693 update_input_key_state( input->desktop, input->keystate, msg );
1694 list_remove( &msg->entry );
1695 free_message( msg );
1696 continue;
1698 if (win_thread != thread)
1700 if (win_thread->queue->input == input)
1702 /* wake the other thread */
1703 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1704 got_one = 1;
1706 else
1708 /* for another thread input, drop it */
1709 update_input_key_state( input->desktop, input->keystate, msg );
1710 list_remove( &msg->entry );
1711 free_message( msg );
1713 release_object( win_thread );
1714 continue;
1716 release_object( win_thread );
1718 /* if we already got a message for another thread, or if it doesn't
1719 * match the filter we skip it */
1720 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1722 clear_bits &= ~get_hardware_msg_bit( msg );
1723 continue;
1725 /* now we can return it */
1726 if (!msg->unique_id) msg->unique_id = get_unique_id();
1727 reply->type = MSG_HARDWARE;
1728 reply->win = win;
1729 reply->msg = msg_code;
1730 reply->wparam = msg->wparam;
1731 reply->lparam = msg->lparam;
1732 reply->time = msg->time;
1734 data->hw_id = msg->unique_id;
1735 set_reply_data( msg->data, msg->data_size );
1736 return 1;
1738 /* nothing found, clear the hardware queue bits */
1739 clear_queue_bits( thread->queue, clear_bits );
1740 return 0;
1743 /* increment (or decrement if 'incr' is negative) the queue paint count */
1744 void inc_queue_paint_count( struct thread *thread, int incr )
1746 struct msg_queue *queue = thread->queue;
1748 assert( queue );
1750 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1752 if (queue->paint_count)
1753 set_queue_bits( queue, QS_PAINT );
1754 else
1755 clear_queue_bits( queue, QS_PAINT );
1759 /* remove all messages and timers belonging to a certain window */
1760 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1762 struct msg_queue *queue = thread->queue;
1763 struct list *ptr;
1764 int i;
1766 if (!queue) return;
1768 /* remove timers */
1770 ptr = list_head( &queue->pending_timers );
1771 while (ptr)
1773 struct list *next = list_next( &queue->pending_timers, ptr );
1774 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1775 if (timer->win == win) free_timer( queue, timer );
1776 ptr = next;
1778 ptr = list_head( &queue->expired_timers );
1779 while (ptr)
1781 struct list *next = list_next( &queue->expired_timers, ptr );
1782 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1783 if (timer->win == win) free_timer( queue, timer );
1784 ptr = next;
1787 /* remove messages */
1788 for (i = 0; i < NB_MSG_KINDS; i++)
1790 struct list *ptr, *next;
1792 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
1794 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1795 if (msg->win == win) remove_queue_message( queue, msg, i );
1799 thread_input_cleanup_window( queue, win );
1802 /* post a message to a window; used by socket handling */
1803 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
1805 struct message *msg;
1806 struct thread *thread = get_window_thread( win );
1808 if (!thread) return;
1810 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1812 msg->type = MSG_POSTED;
1813 msg->win = get_user_full_handle( win );
1814 msg->msg = message;
1815 msg->wparam = wparam;
1816 msg->lparam = lparam;
1817 msg->time = get_tick_count();
1818 msg->result = NULL;
1819 msg->data = NULL;
1820 msg->data_size = 0;
1822 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
1823 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
1825 release_object( thread );
1828 /* post a win event */
1829 void post_win_event( struct thread *thread, unsigned int event,
1830 user_handle_t win, unsigned int object_id,
1831 unsigned int child_id, client_ptr_t hook_proc,
1832 const WCHAR *module, data_size_t module_size,
1833 user_handle_t hook)
1835 struct message *msg;
1837 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
1839 struct winevent_msg_data *data;
1841 msg->type = MSG_WINEVENT;
1842 msg->win = get_user_full_handle( win );
1843 msg->msg = event;
1844 msg->wparam = object_id;
1845 msg->lparam = child_id;
1846 msg->time = get_tick_count();
1847 msg->result = NULL;
1849 if ((data = malloc( sizeof(*data) + module_size )))
1851 data->hook = hook;
1852 data->tid = get_thread_id( current );
1853 data->hook_proc = hook_proc;
1854 memcpy( data + 1, module, module_size );
1856 msg->data = data;
1857 msg->data_size = sizeof(*data) + module_size;
1859 if (debug_level > 1)
1860 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
1861 get_thread_id(thread), event, win, object_id, child_id );
1862 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
1863 set_queue_bits( thread->queue, QS_SENDMESSAGE );
1865 else
1866 free( msg );
1871 /* check if the thread owning the window is hung */
1872 DECL_HANDLER(is_window_hung)
1874 struct thread *thread;
1876 thread = get_window_thread( req->win );
1877 if (thread)
1879 reply->is_hung = is_queue_hung( thread->queue );
1880 release_object( thread );
1882 else reply->is_hung = 0;
1886 /* get the message queue of the current thread */
1887 DECL_HANDLER(get_msg_queue)
1889 struct msg_queue *queue = get_current_queue();
1891 reply->handle = 0;
1892 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
1896 /* set the file descriptor associated to the current thread queue */
1897 DECL_HANDLER(set_queue_fd)
1899 struct msg_queue *queue = get_current_queue();
1900 struct file *file;
1901 int unix_fd;
1903 if (queue->fd) /* fd can only be set once */
1905 set_error( STATUS_ACCESS_DENIED );
1906 return;
1908 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
1910 if ((unix_fd = get_file_unix_fd( file )) != -1)
1912 if ((unix_fd = dup( unix_fd )) != -1)
1913 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
1914 else
1915 file_set_error();
1917 release_object( file );
1921 /* set the current message queue wakeup mask */
1922 DECL_HANDLER(set_queue_mask)
1924 struct msg_queue *queue = get_current_queue();
1926 if (queue)
1928 queue->wake_mask = req->wake_mask;
1929 queue->changed_mask = req->changed_mask;
1930 reply->wake_bits = queue->wake_bits;
1931 reply->changed_bits = queue->changed_bits;
1932 if (is_signaled( queue ))
1934 /* if skip wait is set, do what would have been done in the subsequent wait */
1935 if (req->skip_wait) msg_queue_satisfied( &queue->obj, current );
1936 else wake_up( &queue->obj, 0 );
1942 /* get the current message queue status */
1943 DECL_HANDLER(get_queue_status)
1945 struct msg_queue *queue = current->queue;
1946 if (queue)
1948 reply->wake_bits = queue->wake_bits;
1949 reply->changed_bits = queue->changed_bits;
1950 if (req->clear) queue->changed_bits = 0;
1952 else reply->wake_bits = reply->changed_bits = 0;
1956 /* send a message to a thread queue */
1957 DECL_HANDLER(send_message)
1959 struct message *msg;
1960 struct msg_queue *send_queue = get_current_queue();
1961 struct msg_queue *recv_queue = NULL;
1962 struct thread *thread = NULL;
1964 if (!(thread = get_thread_from_id( req->id ))) return;
1966 if (!(recv_queue = thread->queue))
1968 set_error( STATUS_INVALID_PARAMETER );
1969 release_object( thread );
1970 return;
1972 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
1974 set_error( STATUS_TIMEOUT );
1975 release_object( thread );
1976 return;
1979 if ((msg = mem_alloc( sizeof(*msg) )))
1981 msg->type = req->type;
1982 msg->win = get_user_full_handle( req->win );
1983 msg->msg = req->msg;
1984 msg->wparam = req->wparam;
1985 msg->lparam = req->lparam;
1986 msg->time = get_tick_count();
1987 msg->result = NULL;
1988 msg->data = NULL;
1989 msg->data_size = get_req_data_size();
1991 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
1993 free( msg );
1994 release_object( thread );
1995 return;
1998 switch(msg->type)
2000 case MSG_OTHER_PROCESS:
2001 case MSG_ASCII:
2002 case MSG_UNICODE:
2003 case MSG_CALLBACK:
2004 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2006 free_message( msg );
2007 break;
2009 /* fall through */
2010 case MSG_NOTIFY:
2011 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2012 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2013 break;
2014 case MSG_POSTED:
2015 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2016 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2017 break;
2018 case MSG_HARDWARE: /* should use send_hardware_message instead */
2019 case MSG_CALLBACK_RESULT: /* cannot send this one */
2020 case MSG_HOOK_LL: /* generated internally */
2021 default:
2022 set_error( STATUS_INVALID_PARAMETER );
2023 free( msg );
2024 break;
2027 release_object( thread );
2030 /* send a hardware message to a thread queue */
2031 DECL_HANDLER(send_hardware_message)
2033 struct thread *thread = NULL;
2034 struct desktop *desktop;
2035 struct msg_queue *sender = get_current_queue();
2037 if (req->win)
2039 if (!(thread = get_window_thread( req->win ))) return;
2040 desktop = (struct desktop *)grab_object( thread->queue->input->desktop );
2042 else if (!(desktop = get_thread_desktop( current, 0 ))) return;
2044 switch (req->input.type)
2046 case INPUT_MOUSE:
2047 reply->wait = queue_mouse_message( desktop, req->win, &req->input, req->flags, sender );
2048 break;
2049 case INPUT_KEYBOARD:
2050 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, req->flags, sender );
2051 break;
2052 case INPUT_HARDWARE:
2053 queue_custom_hardware_message( desktop, req->win, &req->input );
2054 break;
2055 default:
2056 set_error( STATUS_INVALID_PARAMETER );
2058 if (thread) release_object( thread );
2059 release_object( desktop );
2062 /* post a quit message to the current queue */
2063 DECL_HANDLER(post_quit_message)
2065 struct msg_queue *queue = get_current_queue();
2067 if (!queue)
2068 return;
2070 queue->quit_message = 1;
2071 queue->exit_code = req->exit_code;
2072 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2075 /* get a message from the current queue */
2076 DECL_HANDLER(get_message)
2078 struct timer *timer;
2079 struct list *ptr;
2080 struct msg_queue *queue = get_current_queue();
2081 user_handle_t get_win = get_user_full_handle( req->get_win );
2082 unsigned int filter = req->flags >> 16;
2084 reply->active_hooks = get_active_hooks();
2086 if (!queue) return;
2087 queue->last_get_msg = current_time;
2088 if (!filter) filter = QS_ALLINPUT;
2090 /* first check for sent messages */
2091 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2093 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2094 receive_message( queue, msg, reply );
2095 return;
2098 /* clear changed bits so we can wait on them if we don't find a message */
2099 if (filter & QS_POSTMESSAGE)
2101 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2102 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2104 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2105 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2107 /* then check for posted messages */
2108 if ((filter & QS_POSTMESSAGE) &&
2109 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2110 return;
2112 /* only check for quit messages if not posted messages pending.
2113 * note: the quit message isn't filtered */
2114 if (get_quit_message( queue, req->flags, reply ))
2115 return;
2117 /* then check for any raw hardware message */
2118 if ((filter & QS_INPUT) &&
2119 filter_contains_hw_range( req->get_first, req->get_last ) &&
2120 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, reply ))
2121 return;
2123 /* now check for WM_PAINT */
2124 if ((filter & QS_PAINT) &&
2125 queue->paint_count &&
2126 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2127 (reply->win = find_window_to_repaint( get_win, current )))
2129 reply->type = MSG_POSTED;
2130 reply->msg = WM_PAINT;
2131 reply->wparam = 0;
2132 reply->lparam = 0;
2133 reply->time = get_tick_count();
2134 return;
2137 /* now check for timer */
2138 if ((filter & QS_TIMER) &&
2139 (timer = find_expired_timer( queue, get_win, req->get_first,
2140 req->get_last, (req->flags & PM_REMOVE) )))
2142 reply->type = MSG_POSTED;
2143 reply->win = timer->win;
2144 reply->msg = timer->msg;
2145 reply->wparam = timer->id;
2146 reply->lparam = timer->lparam;
2147 reply->time = get_tick_count();
2148 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2149 set_event( current->process->idle_event );
2150 return;
2153 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2154 queue->wake_mask = req->wake_mask;
2155 queue->changed_mask = req->changed_mask;
2156 set_error( STATUS_PENDING ); /* FIXME */
2160 /* reply to a sent message */
2161 DECL_HANDLER(reply_message)
2163 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2164 else if (current->queue->recv_result)
2165 reply_message( current->queue, req->result, 0, req->remove,
2166 get_req_data(), get_req_data_size() );
2170 /* accept the current hardware message */
2171 DECL_HANDLER(accept_hardware_message)
2173 if (current->queue)
2174 release_hardware_message( current->queue, req->hw_id, req->remove, req->new_win );
2175 else
2176 set_error( STATUS_ACCESS_DENIED );
2180 /* retrieve the reply for the last message sent */
2181 DECL_HANDLER(get_message_reply)
2183 struct message_result *result;
2184 struct list *entry;
2185 struct msg_queue *queue = current->queue;
2187 if (queue)
2189 set_error( STATUS_PENDING );
2190 reply->result = 0;
2192 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2194 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2195 if (result->replied || req->cancel)
2197 if (result->replied)
2199 reply->result = result->result;
2200 set_error( result->error );
2201 if (result->data)
2203 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2204 set_reply_data_ptr( result->data, data_len );
2205 result->data = NULL;
2206 result->data_size = 0;
2209 remove_result_from_sender( result );
2211 entry = list_head( &queue->send_result );
2212 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2213 else
2215 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2216 if (!result->replied) clear_queue_bits( queue, QS_SMRESULT );
2220 else set_error( STATUS_ACCESS_DENIED );
2224 /* set a window timer */
2225 DECL_HANDLER(set_win_timer)
2227 struct timer *timer;
2228 struct msg_queue *queue;
2229 struct thread *thread = NULL;
2230 user_handle_t win = 0;
2231 lparam_t id = req->id;
2233 if (req->win)
2235 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2237 set_error( STATUS_INVALID_HANDLE );
2238 return;
2240 if (thread->process != current->process)
2242 release_object( thread );
2243 set_error( STATUS_ACCESS_DENIED );
2244 return;
2246 queue = thread->queue;
2247 /* remove it if it existed already */
2248 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2250 else
2252 queue = get_current_queue();
2253 /* look for a timer with this id */
2254 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2256 /* free and reuse id */
2257 free_timer( queue, timer );
2259 else
2261 /* find a free id for it */
2264 id = queue->next_timer_id;
2265 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2267 while (find_timer( queue, 0, req->msg, id ));
2271 if ((timer = set_timer( queue, req->rate )))
2273 timer->win = win;
2274 timer->msg = req->msg;
2275 timer->id = id;
2276 timer->lparam = req->lparam;
2277 reply->id = id;
2279 if (thread) release_object( thread );
2282 /* kill a window timer */
2283 DECL_HANDLER(kill_win_timer)
2285 struct timer *timer;
2286 struct thread *thread;
2287 user_handle_t win = 0;
2289 if (req->win)
2291 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2293 set_error( STATUS_INVALID_HANDLE );
2294 return;
2296 if (thread->process != current->process)
2298 release_object( thread );
2299 set_error( STATUS_ACCESS_DENIED );
2300 return;
2303 else thread = (struct thread *)grab_object( current );
2305 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2306 free_timer( thread->queue, timer );
2307 else
2308 set_error( STATUS_INVALID_PARAMETER );
2310 release_object( thread );
2314 /* attach (or detach) thread inputs */
2315 DECL_HANDLER(attach_thread_input)
2317 struct thread *thread_from = get_thread_from_id( req->tid_from );
2318 struct thread *thread_to = get_thread_from_id( req->tid_to );
2320 if (!thread_from || !thread_to)
2322 if (thread_from) release_object( thread_from );
2323 if (thread_to) release_object( thread_to );
2324 return;
2326 if (thread_from != thread_to)
2328 if (req->attach) attach_thread_input( thread_from, thread_to );
2329 else
2331 if (thread_from->queue && thread_to->queue &&
2332 thread_from->queue->input == thread_to->queue->input)
2333 detach_thread_input( thread_from );
2334 else
2335 set_error( STATUS_ACCESS_DENIED );
2338 else set_error( STATUS_ACCESS_DENIED );
2339 release_object( thread_from );
2340 release_object( thread_to );
2344 /* get thread input data */
2345 DECL_HANDLER(get_thread_input)
2347 struct thread *thread = NULL;
2348 struct desktop *desktop;
2349 struct thread_input *input;
2351 if (req->tid)
2353 if (!(thread = get_thread_from_id( req->tid ))) return;
2354 if (!(desktop = get_thread_desktop( thread, 0 )))
2356 release_object( thread );
2357 return;
2359 input = thread->queue ? thread->queue->input : NULL;
2361 else
2363 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2364 input = desktop->foreground_input; /* get the foreground thread info */
2367 if (input)
2369 reply->focus = input->focus;
2370 reply->capture = input->capture;
2371 reply->active = input->active;
2372 reply->menu_owner = input->menu_owner;
2373 reply->move_size = input->move_size;
2374 reply->caret = input->caret;
2375 reply->cursor = input->cursor;
2376 reply->show_count = input->cursor_count;
2377 reply->rect = input->caret_rect;
2380 /* foreground window is active window of foreground thread */
2381 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2382 if (thread) release_object( thread );
2383 release_object( desktop );
2387 /* retrieve queue keyboard state for a given thread */
2388 DECL_HANDLER(get_key_state)
2390 struct thread *thread;
2391 struct desktop *desktop;
2392 data_size_t size = min( 256, get_reply_max_size() );
2394 if (!req->tid) /* get global async key state */
2396 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2397 if (req->key >= 0)
2399 reply->state = desktop->keystate[req->key & 0xff];
2400 desktop->keystate[req->key & 0xff] &= ~0x40;
2402 set_reply_data( desktop->keystate, size );
2403 release_object( desktop );
2405 else
2407 if (!(thread = get_thread_from_id( req->tid ))) return;
2408 if (thread->queue)
2410 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2411 set_reply_data( thread->queue->input->keystate, size );
2413 release_object( thread );
2418 /* set queue keyboard state for a given thread */
2419 DECL_HANDLER(set_key_state)
2421 struct thread *thread;
2422 struct desktop *desktop;
2423 data_size_t size = min( 256, get_req_data_size() );
2425 if (!req->tid) /* set global async key state */
2427 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2428 memcpy( desktop->keystate, get_req_data(), size );
2429 release_object( desktop );
2431 else
2433 if (!(thread = get_thread_from_id( req->tid ))) return;
2434 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
2435 release_object( thread );
2440 /* set the system foreground window */
2441 DECL_HANDLER(set_foreground_window)
2443 struct thread *thread = NULL;
2444 struct desktop *desktop;
2445 struct msg_queue *queue = get_current_queue();
2447 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2448 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
2449 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
2450 reply->send_msg_new = FALSE;
2452 if (is_top_level_window( req->handle ) &&
2453 ((thread = get_window_thread( req->handle ))) &&
2454 (thread->queue->input->desktop == desktop))
2456 set_foreground_input( desktop, thread->queue->input );
2457 reply->send_msg_new = (desktop->foreground_input != queue->input);
2459 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2461 if (thread) release_object( thread );
2462 release_object( desktop );
2466 /* set the current thread focus window */
2467 DECL_HANDLER(set_focus_window)
2469 struct msg_queue *queue = get_current_queue();
2471 reply->previous = 0;
2472 if (queue && check_queue_input_window( queue, req->handle ))
2474 reply->previous = queue->input->focus;
2475 queue->input->focus = get_user_full_handle( req->handle );
2480 /* set the current thread active window */
2481 DECL_HANDLER(set_active_window)
2483 struct msg_queue *queue = get_current_queue();
2485 reply->previous = 0;
2486 if (queue && check_queue_input_window( queue, req->handle ))
2488 if (!req->handle || make_window_active( req->handle ))
2490 reply->previous = queue->input->active;
2491 queue->input->active = get_user_full_handle( req->handle );
2493 else set_error( STATUS_INVALID_HANDLE );
2498 /* set the current thread capture window */
2499 DECL_HANDLER(set_capture_window)
2501 struct msg_queue *queue = get_current_queue();
2503 reply->previous = reply->full_handle = 0;
2504 if (queue && check_queue_input_window( queue, req->handle ))
2506 struct thread_input *input = queue->input;
2508 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2509 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2511 set_error(STATUS_ACCESS_DENIED);
2512 return;
2514 reply->previous = input->capture;
2515 input->capture = get_user_full_handle( req->handle );
2516 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2517 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2518 reply->full_handle = input->capture;
2523 /* Set the current thread caret window */
2524 DECL_HANDLER(set_caret_window)
2526 struct msg_queue *queue = get_current_queue();
2528 reply->previous = 0;
2529 if (queue && check_queue_input_window( queue, req->handle ))
2531 struct thread_input *input = queue->input;
2533 reply->previous = input->caret;
2534 reply->old_rect = input->caret_rect;
2535 reply->old_hide = input->caret_hide;
2536 reply->old_state = input->caret_state;
2538 set_caret_window( input, get_user_full_handle(req->handle) );
2539 input->caret_rect.right = input->caret_rect.left + req->width;
2540 input->caret_rect.bottom = input->caret_rect.top + req->height;
2545 /* Set the current thread caret information */
2546 DECL_HANDLER(set_caret_info)
2548 struct msg_queue *queue = get_current_queue();
2549 struct thread_input *input;
2551 if (!queue) return;
2552 input = queue->input;
2553 reply->full_handle = input->caret;
2554 reply->old_rect = input->caret_rect;
2555 reply->old_hide = input->caret_hide;
2556 reply->old_state = input->caret_state;
2558 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2560 set_error( STATUS_ACCESS_DENIED );
2561 return;
2563 if (req->flags & SET_CARET_POS)
2565 input->caret_rect.right += req->x - input->caret_rect.left;
2566 input->caret_rect.bottom += req->y - input->caret_rect.top;
2567 input->caret_rect.left = req->x;
2568 input->caret_rect.top = req->y;
2570 if (req->flags & SET_CARET_HIDE)
2572 input->caret_hide += req->hide;
2573 if (input->caret_hide < 0) input->caret_hide = 0;
2575 if (req->flags & SET_CARET_STATE)
2577 if (req->state == -1) input->caret_state = !input->caret_state;
2578 else input->caret_state = !!req->state;
2583 /* get the time of the last input event */
2584 DECL_HANDLER(get_last_input_time)
2586 reply->time = last_input_time;
2589 /* set/get the current cursor */
2590 DECL_HANDLER(set_cursor)
2592 struct msg_queue *queue = get_current_queue();
2593 struct thread_input *input;
2595 if (!queue) return;
2596 input = queue->input;
2598 reply->prev_handle = input->cursor;
2599 reply->prev_count = input->cursor_count;
2601 if (req->flags & SET_CURSOR_HANDLE)
2603 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
2605 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
2606 return;
2608 input->cursor = req->handle;
2610 if (req->flags & SET_CURSOR_COUNT)
2612 queue->cursor_count += req->show_count;
2613 input->cursor_count += req->show_count;
2615 if (req->flags & SET_CURSOR_POS)
2617 set_cursor_pos( input->desktop, req->x, req->y );
2619 if (req->flags & SET_CURSOR_CLIP)
2621 rectangle_t top_rect;
2622 get_top_window_rectangle( input->desktop, &top_rect );
2623 if (!intersect_rect( &input->desktop->cursor_clip, &top_rect, &req->clip ))
2624 input->desktop->cursor_clip = top_rect;
2627 reply->new_x = input->desktop->cursor_x;
2628 reply->new_y = input->desktop->cursor_y;
2629 reply->new_clip = input->desktop->cursor_clip;