kernel32/tests: Use todo_wine_if().
[wine.git] / server / queue.c
blobf82060fe9d64e42fac2fd9be160bf257b63a8cd8
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>
28 #ifdef HAVE_POLL_H
29 # include <poll.h>
30 #endif
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "winternl.h"
40 #include "handle.h"
41 #include "file.h"
42 #include "thread.h"
43 #include "process.h"
44 #include "request.h"
45 #include "user.h"
47 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
48 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
50 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
51 #define NB_MSG_KINDS (POST_MESSAGE+1)
54 struct message_result
56 struct list sender_entry; /* entry in sender list */
57 struct message *msg; /* message the result is for */
58 struct message_result *recv_next; /* next in receiver list */
59 struct msg_queue *sender; /* sender queue */
60 struct msg_queue *receiver; /* receiver queue */
61 int replied; /* has it been replied to? */
62 unsigned int error; /* error code to pass back to sender */
63 lparam_t result; /* reply result */
64 struct message *hardware_msg; /* hardware message if low-level hook result */
65 struct desktop *desktop; /* desktop for hardware message */
66 struct message *callback_msg; /* message to queue for callback */
67 void *data; /* message reply data */
68 unsigned int data_size; /* size of message reply data */
69 struct timeout_user *timeout; /* result timeout */
72 struct message
74 struct list entry; /* entry in message list */
75 enum message_type type; /* message type */
76 user_handle_t win; /* window handle */
77 unsigned int msg; /* message code */
78 lparam_t wparam; /* parameters */
79 lparam_t lparam; /* parameters */
80 int x; /* message position */
81 int y;
82 unsigned int time; /* message time */
83 void *data; /* message data for sent messages */
84 unsigned int data_size; /* size of message data */
85 unsigned int unique_id; /* unique id for nested hw message waits */
86 struct message_result *result; /* result in sender queue */
89 struct timer
91 struct list entry; /* entry in timer list */
92 timeout_t when; /* next expiration */
93 unsigned int rate; /* timer rate in ms */
94 user_handle_t win; /* window handle */
95 unsigned int msg; /* message to post */
96 lparam_t id; /* timer id */
97 lparam_t lparam; /* lparam for message */
100 struct thread_input
102 struct object obj; /* object header */
103 struct desktop *desktop; /* desktop that this thread input belongs to */
104 user_handle_t focus; /* focus window */
105 user_handle_t capture; /* capture window */
106 user_handle_t active; /* active window */
107 user_handle_t menu_owner; /* current menu owner window */
108 user_handle_t move_size; /* current moving/resizing window */
109 user_handle_t caret; /* caret window */
110 rectangle_t caret_rect; /* caret rectangle */
111 int caret_hide; /* caret hide count */
112 int caret_state; /* caret on/off state */
113 user_handle_t cursor; /* current cursor */
114 int cursor_count; /* cursor show count */
115 struct list msg_list; /* list of hardware messages */
116 unsigned char keystate[256]; /* state of each key */
119 struct msg_queue
121 struct object obj; /* object header */
122 struct fd *fd; /* optional file descriptor to poll */
123 unsigned int wake_bits; /* wakeup bits */
124 unsigned int wake_mask; /* wakeup mask */
125 unsigned int changed_bits; /* changed wakeup bits */
126 unsigned int changed_mask; /* changed wakeup mask */
127 int paint_count; /* pending paint messages count */
128 int hotkey_count; /* pending hotkey messages count */
129 int quit_message; /* is there a pending quit message? */
130 int exit_code; /* exit code of pending quit message */
131 int cursor_count; /* per-queue cursor show count */
132 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
133 struct list send_result; /* stack of sent messages waiting for result */
134 struct list callback_result; /* list of callback messages waiting for result */
135 struct message_result *recv_result; /* stack of received messages waiting for result */
136 struct list pending_timers; /* list of pending timers */
137 struct list expired_timers; /* list of expired timers */
138 lparam_t next_timer_id; /* id for the next timer with a 0 window */
139 struct timeout_user *timeout; /* timeout for next timer to expire */
140 struct thread_input *input; /* thread input descriptor */
141 struct hook_table *hooks; /* hook table */
142 timeout_t last_get_msg; /* time of last get message call */
145 struct hotkey
147 struct list entry; /* entry in desktop hotkey list */
148 struct msg_queue *queue; /* queue owning this hotkey */
149 user_handle_t win; /* window handle */
150 int id; /* hotkey id */
151 unsigned int vkey; /* virtual key code */
152 unsigned int flags; /* key modifiers */
155 static void msg_queue_dump( struct object *obj, int verbose );
156 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
157 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
158 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
159 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
160 static void msg_queue_destroy( struct object *obj );
161 static void msg_queue_poll_event( struct fd *fd, int event );
162 static void thread_input_dump( struct object *obj, int verbose );
163 static void thread_input_destroy( struct object *obj );
164 static void timer_callback( void *private );
166 static const struct object_ops msg_queue_ops =
168 sizeof(struct msg_queue), /* size */
169 msg_queue_dump, /* dump */
170 no_get_type, /* get_type */
171 msg_queue_add_queue, /* add_queue */
172 msg_queue_remove_queue, /* remove_queue */
173 msg_queue_signaled, /* signaled */
174 msg_queue_satisfied, /* satisfied */
175 no_signal, /* signal */
176 no_get_fd, /* get_fd */
177 no_map_access, /* map_access */
178 default_get_sd, /* get_sd */
179 default_set_sd, /* set_sd */
180 no_lookup_name, /* lookup_name */
181 no_link_name, /* link_name */
182 NULL, /* unlink_name */
183 no_open_file, /* open_file */
184 no_close_handle, /* close_handle */
185 msg_queue_destroy /* destroy */
188 static const struct fd_ops msg_queue_fd_ops =
190 NULL, /* get_poll_events */
191 msg_queue_poll_event, /* poll_event */
192 NULL, /* flush */
193 NULL, /* get_fd_type */
194 NULL, /* ioctl */
195 NULL, /* queue_async */
196 NULL, /* reselect_async */
197 NULL /* cancel async */
201 static const struct object_ops thread_input_ops =
203 sizeof(struct thread_input), /* size */
204 thread_input_dump, /* dump */
205 no_get_type, /* get_type */
206 no_add_queue, /* add_queue */
207 NULL, /* remove_queue */
208 NULL, /* signaled */
209 NULL, /* satisfied */
210 no_signal, /* signal */
211 no_get_fd, /* get_fd */
212 no_map_access, /* map_access */
213 default_get_sd, /* get_sd */
214 default_set_sd, /* set_sd */
215 no_lookup_name, /* lookup_name */
216 no_link_name, /* link_name */
217 NULL, /* unlink_name */
218 no_open_file, /* open_file */
219 no_close_handle, /* close_handle */
220 thread_input_destroy /* destroy */
223 /* pointer to input structure of foreground thread */
224 static unsigned int last_input_time;
226 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
227 static void free_message( struct message *msg );
229 /* set the caret window in a given thread input */
230 static void set_caret_window( struct thread_input *input, user_handle_t win )
232 if (!win || win != input->caret)
234 input->caret_rect.left = 0;
235 input->caret_rect.top = 0;
236 input->caret_rect.right = 0;
237 input->caret_rect.bottom = 0;
239 input->caret = win;
240 input->caret_hide = 1;
241 input->caret_state = 0;
244 /* create a thread input object */
245 static struct thread_input *create_thread_input( struct thread *thread )
247 struct thread_input *input;
249 if ((input = alloc_object( &thread_input_ops )))
251 input->focus = 0;
252 input->capture = 0;
253 input->active = 0;
254 input->menu_owner = 0;
255 input->move_size = 0;
256 input->cursor = 0;
257 input->cursor_count = 0;
258 list_init( &input->msg_list );
259 set_caret_window( input, 0 );
260 memset( input->keystate, 0, sizeof(input->keystate) );
262 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
264 release_object( input );
265 return NULL;
268 return input;
271 /* create a message queue object */
272 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
274 struct thread_input *new_input = NULL;
275 struct msg_queue *queue;
276 int i;
278 if (!input)
280 if (!(new_input = create_thread_input( thread ))) return NULL;
281 input = new_input;
284 if ((queue = alloc_object( &msg_queue_ops )))
286 queue->fd = NULL;
287 queue->wake_bits = 0;
288 queue->wake_mask = 0;
289 queue->changed_bits = 0;
290 queue->changed_mask = 0;
291 queue->paint_count = 0;
292 queue->hotkey_count = 0;
293 queue->quit_message = 0;
294 queue->cursor_count = 0;
295 queue->recv_result = NULL;
296 queue->next_timer_id = 0x7fff;
297 queue->timeout = NULL;
298 queue->input = (struct thread_input *)grab_object( input );
299 queue->hooks = NULL;
300 queue->last_get_msg = current_time;
301 list_init( &queue->send_result );
302 list_init( &queue->callback_result );
303 list_init( &queue->pending_timers );
304 list_init( &queue->expired_timers );
305 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
307 thread->queue = queue;
309 if (new_input) release_object( new_input );
310 return queue;
313 /* free the message queue of a thread at thread exit */
314 void free_msg_queue( struct thread *thread )
316 remove_thread_hooks( thread );
317 if (!thread->queue) return;
318 release_object( thread->queue );
319 thread->queue = NULL;
322 /* change the thread input data of a given thread */
323 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
325 struct msg_queue *queue = thread->queue;
327 if (!queue)
329 thread->queue = create_msg_queue( thread, new_input );
330 return thread->queue != NULL;
332 if (queue->input)
334 queue->input->cursor_count -= queue->cursor_count;
335 release_object( queue->input );
337 queue->input = (struct thread_input *)grab_object( new_input );
338 new_input->cursor_count += queue->cursor_count;
339 return 1;
342 /* set the cursor position and queue the corresponding mouse message */
343 static void set_cursor_pos( struct desktop *desktop, int x, int y )
345 struct hardware_msg_data *msg_data;
346 struct message *msg;
348 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
349 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
351 free( msg );
352 return;
354 memset( msg_data, 0, sizeof(*msg_data) );
356 msg->type = MSG_HARDWARE;
357 msg->win = 0;
358 msg->msg = WM_MOUSEMOVE;
359 msg->wparam = 0;
360 msg->lparam = 0;
361 msg->x = x;
362 msg->y = y;
363 msg->time = get_tick_count();
364 msg->result = NULL;
365 msg->data = msg_data;
366 msg->data_size = sizeof(*msg_data);
367 queue_hardware_message( desktop, msg, 1 );
370 /* retrieve default position and time for synthesized messages */
371 static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
373 struct desktop *desktop = queue->input->desktop;
375 *x = desktop->cursor.x;
376 *y = desktop->cursor.y;
377 *time = get_tick_count();
380 /* set the cursor clip rectangle */
381 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, int send_clip_msg )
383 rectangle_t top_rect;
384 int x, y;
386 get_top_window_rectangle( desktop, &top_rect );
387 if (rect)
389 rectangle_t new_rect = *rect;
390 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
391 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
392 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
393 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
394 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
395 desktop->cursor.clip = new_rect;
397 else desktop->cursor.clip = top_rect;
399 if (desktop->cursor.clip_msg && send_clip_msg)
400 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
402 /* warp the mouse to be inside the clip rect */
403 x = min( max( desktop->cursor.x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
404 y = min( max( desktop->cursor.y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
405 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
408 /* change the foreground input and reset the cursor clip rect */
409 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
411 if (desktop->foreground_input == input) return;
412 set_clip_rectangle( desktop, NULL, 1 );
413 desktop->foreground_input = input;
416 /* get the hook table for a given thread */
417 struct hook_table *get_queue_hooks( struct thread *thread )
419 if (!thread->queue) return NULL;
420 return thread->queue->hooks;
423 /* set the hook table for a given thread, allocating the queue if needed */
424 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
426 struct msg_queue *queue = thread->queue;
427 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
428 if (queue->hooks) release_object( queue->hooks );
429 queue->hooks = hooks;
432 /* check the queue status */
433 static inline int is_signaled( struct msg_queue *queue )
435 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
438 /* set some queue bits */
439 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
441 queue->wake_bits |= bits;
442 queue->changed_bits |= bits;
443 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
446 /* clear some queue bits */
447 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
449 queue->wake_bits &= ~bits;
450 queue->changed_bits &= ~bits;
453 /* check whether msg is a keyboard message */
454 static inline int is_keyboard_msg( struct message *msg )
456 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
459 /* check if message is matched by the filter */
460 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
462 return (msg >= first && msg <= last);
465 /* check whether a message filter contains at least one potential hardware message */
466 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
468 /* hardware message ranges are (in numerical order):
469 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
470 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
471 * WM_MOUSEFIRST .. WM_MOUSELAST
473 if (last < WM_NCMOUSEFIRST) return 0;
474 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
475 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
476 if (first > WM_MOUSELAST) return 0;
477 return 1;
480 /* get the QS_* bit corresponding to a given hardware message */
481 static inline int get_hardware_msg_bit( struct message *msg )
483 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
484 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
485 if (is_keyboard_msg( msg )) return QS_KEY;
486 return QS_MOUSEBUTTON;
489 /* get the current thread queue, creating it if needed */
490 static inline struct msg_queue *get_current_queue(void)
492 struct msg_queue *queue = current->queue;
493 if (!queue) queue = create_msg_queue( current, NULL );
494 return queue;
497 /* get a (pseudo-)unique id to tag hardware messages */
498 static inline unsigned int get_unique_id(void)
500 static unsigned int id;
501 if (!++id) id = 1; /* avoid an id of 0 */
502 return id;
505 /* try to merge a message with the last in the list; return 1 if successful */
506 static int merge_message( struct thread_input *input, const struct message *msg )
508 struct message *prev;
509 struct list *ptr;
511 if (msg->msg != WM_MOUSEMOVE) return 0;
512 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
514 prev = LIST_ENTRY( ptr, struct message, entry );
515 if (prev->msg != WM_INPUT) break;
517 if (!ptr) return 0;
518 if (prev->result) return 0;
519 if (prev->win && msg->win && prev->win != msg->win) return 0;
520 if (prev->msg != msg->msg) return 0;
521 if (prev->type != msg->type) return 0;
522 /* now we can merge it */
523 prev->wparam = msg->wparam;
524 prev->lparam = msg->lparam;
525 prev->x = msg->x;
526 prev->y = msg->y;
527 prev->time = msg->time;
528 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
530 struct hardware_msg_data *prev_data = prev->data;
531 struct hardware_msg_data *msg_data = msg->data;
532 prev_data->info = msg_data->info;
534 list_remove( ptr );
535 list_add_tail( &input->msg_list, ptr );
536 return 1;
539 /* free a result structure */
540 static void free_result( struct message_result *result )
542 if (result->timeout) remove_timeout_user( result->timeout );
543 free( result->data );
544 if (result->callback_msg) free_message( result->callback_msg );
545 if (result->hardware_msg) free_message( result->hardware_msg );
546 if (result->desktop) release_object( result->desktop );
547 free( result );
550 /* remove the result from the sender list it is on */
551 static inline void remove_result_from_sender( struct message_result *result )
553 assert( result->sender );
555 list_remove( &result->sender_entry );
556 result->sender = NULL;
557 if (!result->receiver) free_result( result );
560 /* store the message result in the appropriate structure */
561 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
563 res->result = result;
564 res->error = error;
565 res->replied = 1;
566 if (res->timeout)
568 remove_timeout_user( res->timeout );
569 res->timeout = NULL;
572 if (res->hardware_msg)
574 if (!error && result) /* rejected by the hook */
575 free_message( res->hardware_msg );
576 else
577 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
579 res->hardware_msg = NULL;
582 if (res->sender)
584 if (res->callback_msg)
586 /* queue the callback message in the sender queue */
587 struct callback_msg_data *data = res->callback_msg->data;
588 data->result = result;
589 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
590 set_queue_bits( res->sender, QS_SENDMESSAGE );
591 res->callback_msg = NULL;
592 remove_result_from_sender( res );
594 else
596 /* wake sender queue if waiting on this result */
597 if (list_head(&res->sender->send_result) == &res->sender_entry)
598 set_queue_bits( res->sender, QS_SMRESULT );
601 else if (!res->receiver) free_result( res );
604 /* free a message when deleting a queue or window */
605 static void free_message( struct message *msg )
607 struct message_result *result = msg->result;
608 if (result)
610 result->msg = NULL;
611 result->receiver = NULL;
612 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
614 free( msg->data );
615 free( msg );
618 /* remove (and free) a message from a message list */
619 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
620 enum message_kind kind )
622 list_remove( &msg->entry );
623 switch(kind)
625 case SEND_MESSAGE:
626 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
627 break;
628 case POST_MESSAGE:
629 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
630 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
631 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
632 clear_queue_bits( queue, QS_HOTKEY );
633 break;
635 free_message( msg );
638 /* message timed out without getting a reply */
639 static void result_timeout( void *private )
641 struct message_result *result = private;
643 assert( !result->replied );
645 result->timeout = NULL;
647 if (result->msg) /* not received yet */
649 struct message *msg = result->msg;
651 result->msg = NULL;
652 msg->result = NULL;
653 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
654 result->receiver = NULL;
656 store_message_result( result, 0, STATUS_TIMEOUT );
659 /* allocate and fill a message result structure */
660 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
661 struct msg_queue *recv_queue,
662 struct message *msg, timeout_t timeout )
664 struct message_result *result = mem_alloc( sizeof(*result) );
665 if (result)
667 result->msg = msg;
668 result->sender = send_queue;
669 result->receiver = recv_queue;
670 result->replied = 0;
671 result->data = NULL;
672 result->data_size = 0;
673 result->timeout = NULL;
674 result->hardware_msg = NULL;
675 result->desktop = NULL;
676 result->callback_msg = NULL;
678 if (msg->type == MSG_CALLBACK)
680 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
682 if (!callback_msg)
684 free( result );
685 return NULL;
687 callback_msg->type = MSG_CALLBACK_RESULT;
688 callback_msg->win = msg->win;
689 callback_msg->msg = msg->msg;
690 callback_msg->wparam = 0;
691 callback_msg->lparam = 0;
692 callback_msg->time = get_tick_count();
693 callback_msg->result = NULL;
694 /* steal the data from the original message */
695 callback_msg->data = msg->data;
696 callback_msg->data_size = msg->data_size;
697 msg->data = NULL;
698 msg->data_size = 0;
700 result->callback_msg = callback_msg;
701 list_add_head( &send_queue->callback_result, &result->sender_entry );
703 else if (send_queue)
705 list_add_head( &send_queue->send_result, &result->sender_entry );
706 clear_queue_bits( send_queue, QS_SMRESULT );
709 if (timeout != TIMEOUT_INFINITE)
710 result->timeout = add_timeout_user( timeout, result_timeout, result );
712 return result;
715 /* receive a message, removing it from the sent queue */
716 static void receive_message( struct msg_queue *queue, struct message *msg,
717 struct get_message_reply *reply )
719 struct message_result *result = msg->result;
721 reply->total = msg->data_size;
722 if (msg->data_size > get_reply_max_size())
724 set_error( STATUS_BUFFER_OVERFLOW );
725 return;
727 reply->type = msg->type;
728 reply->win = msg->win;
729 reply->msg = msg->msg;
730 reply->wparam = msg->wparam;
731 reply->lparam = msg->lparam;
732 reply->x = msg->x;
733 reply->y = msg->y;
734 reply->time = msg->time;
736 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
738 list_remove( &msg->entry );
739 /* put the result on the receiver result stack */
740 if (result)
742 result->msg = NULL;
743 result->recv_next = queue->recv_result;
744 queue->recv_result = result;
746 free( msg );
747 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
750 /* set the result of the current received message */
751 static void reply_message( struct msg_queue *queue, lparam_t result,
752 unsigned int error, int remove, const void *data, data_size_t len )
754 struct message_result *res = queue->recv_result;
756 if (remove)
758 queue->recv_result = res->recv_next;
759 res->receiver = NULL;
760 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
762 free_result( res );
763 return;
766 if (!res->replied)
768 if (len && (res->data = memdup( data, len ))) res->data_size = len;
769 store_message_result( res, result, error );
773 static int match_window( user_handle_t win, user_handle_t msg_win )
775 if (!win) return 1;
776 if (win == -1 || win == 1) return !msg_win;
777 if (msg_win == win) return 1;
778 return is_child_window( win, msg_win );
781 /* retrieve a posted message */
782 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
783 unsigned int first, unsigned int last, unsigned int flags,
784 struct get_message_reply *reply )
786 struct message *msg;
788 /* check against the filters */
789 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
791 if (!match_window( win, msg->win )) continue;
792 if (!check_msg_filter( msg->msg, first, last )) continue;
793 goto found; /* found one */
795 return 0;
797 /* return it to the app */
798 found:
799 reply->total = msg->data_size;
800 if (msg->data_size > get_reply_max_size())
802 set_error( STATUS_BUFFER_OVERFLOW );
803 return 1;
805 reply->type = msg->type;
806 reply->win = msg->win;
807 reply->msg = msg->msg;
808 reply->wparam = msg->wparam;
809 reply->lparam = msg->lparam;
810 reply->x = msg->x;
811 reply->y = msg->y;
812 reply->time = msg->time;
814 if (flags & PM_REMOVE)
816 if (msg->data)
818 set_reply_data_ptr( msg->data, msg->data_size );
819 msg->data = NULL;
820 msg->data_size = 0;
822 remove_queue_message( queue, msg, POST_MESSAGE );
824 else if (msg->data) set_reply_data( msg->data, msg->data_size );
826 return 1;
829 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
830 struct get_message_reply *reply )
832 if (queue->quit_message)
834 reply->total = 0;
835 reply->type = MSG_POSTED;
836 reply->win = 0;
837 reply->msg = WM_QUIT;
838 reply->wparam = queue->exit_code;
839 reply->lparam = 0;
841 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
843 if (flags & PM_REMOVE)
845 queue->quit_message = 0;
846 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
847 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
849 return 1;
851 else
852 return 0;
855 /* empty a message list and free all the messages */
856 static void empty_msg_list( struct list *list )
858 struct list *ptr;
860 while ((ptr = list_head( list )) != NULL)
862 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
863 list_remove( &msg->entry );
864 free_message( msg );
868 /* cleanup all pending results when deleting a queue */
869 static void cleanup_results( struct msg_queue *queue )
871 struct list *entry;
873 while ((entry = list_head( &queue->send_result )) != NULL)
875 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
878 while ((entry = list_head( &queue->callback_result )) != NULL)
880 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
883 while (queue->recv_result)
884 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
887 /* check if the thread owning the queue is hung (not checking for messages) */
888 static int is_queue_hung( struct msg_queue *queue )
890 struct wait_queue_entry *entry;
892 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
893 return 0; /* less than 5 seconds since last get message -> not hung */
895 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
897 if (get_wait_queue_thread(entry)->queue == queue)
898 return 0; /* thread is waiting on queue -> not hung */
900 return 1;
903 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
905 struct msg_queue *queue = (struct msg_queue *)obj;
906 struct process *process = get_wait_queue_thread(entry)->process;
908 /* a thread can only wait on its own queue */
909 if (get_wait_queue_thread(entry)->queue != queue)
911 set_error( STATUS_ACCESS_DENIED );
912 return 0;
914 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
916 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
917 set_fd_events( queue->fd, POLLIN );
918 add_queue( obj, entry );
919 return 1;
922 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
924 struct msg_queue *queue = (struct msg_queue *)obj;
926 remove_queue( obj, entry );
927 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
928 set_fd_events( queue->fd, 0 );
931 static void msg_queue_dump( struct object *obj, int verbose )
933 struct msg_queue *queue = (struct msg_queue *)obj;
934 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
935 queue->wake_bits, queue->wake_mask );
938 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
940 struct msg_queue *queue = (struct msg_queue *)obj;
941 int ret = 0;
943 if (queue->fd)
945 if ((ret = check_fd_events( queue->fd, POLLIN )))
946 /* stop waiting on select() if we are signaled */
947 set_fd_events( queue->fd, 0 );
948 else if (!list_empty( &obj->wait_queue ))
949 /* restart waiting on poll() if we are no longer signaled */
950 set_fd_events( queue->fd, POLLIN );
952 return ret || is_signaled( queue );
955 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
957 struct msg_queue *queue = (struct msg_queue *)obj;
958 queue->wake_mask = 0;
959 queue->changed_mask = 0;
962 static void msg_queue_destroy( struct object *obj )
964 struct msg_queue *queue = (struct msg_queue *)obj;
965 struct list *ptr;
966 struct hotkey *hotkey, *hotkey2;
967 int i;
969 cleanup_results( queue );
970 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
972 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
974 if (hotkey->queue == queue)
976 list_remove( &hotkey->entry );
977 free( hotkey );
981 while ((ptr = list_head( &queue->pending_timers )))
983 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
984 list_remove( &timer->entry );
985 free( timer );
987 while ((ptr = list_head( &queue->expired_timers )))
989 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
990 list_remove( &timer->entry );
991 free( timer );
993 if (queue->timeout) remove_timeout_user( queue->timeout );
994 queue->input->cursor_count -= queue->cursor_count;
995 release_object( queue->input );
996 if (queue->hooks) release_object( queue->hooks );
997 if (queue->fd) release_object( queue->fd );
1000 static void msg_queue_poll_event( struct fd *fd, int event )
1002 struct msg_queue *queue = get_fd_user( fd );
1003 assert( queue->obj.ops == &msg_queue_ops );
1005 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1006 else set_fd_events( queue->fd, 0 );
1007 wake_up( &queue->obj, 0 );
1010 static void thread_input_dump( struct object *obj, int verbose )
1012 struct thread_input *input = (struct thread_input *)obj;
1013 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
1014 input->focus, input->capture, input->active );
1017 static void thread_input_destroy( struct object *obj )
1019 struct thread_input *input = (struct thread_input *)obj;
1021 empty_msg_list( &input->msg_list );
1022 if (input->desktop)
1024 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
1025 release_object( input->desktop );
1029 /* fix the thread input data when a window is destroyed */
1030 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1032 struct thread_input *input = queue->input;
1034 if (window == input->focus) input->focus = 0;
1035 if (window == input->capture) input->capture = 0;
1036 if (window == input->active) input->active = 0;
1037 if (window == input->menu_owner) input->menu_owner = 0;
1038 if (window == input->move_size) input->move_size = 0;
1039 if (window == input->caret) set_caret_window( input, 0 );
1042 /* check if the specified window can be set in the input data of a given queue */
1043 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1045 struct thread *thread;
1046 int ret = 0;
1048 if (!window) return 1; /* we can always clear the data */
1050 if ((thread = get_window_thread( window )))
1052 ret = (queue->input == thread->queue->input);
1053 if (!ret) set_error( STATUS_ACCESS_DENIED );
1054 release_object( thread );
1056 else set_error( STATUS_INVALID_HANDLE );
1058 return ret;
1061 /* make sure the specified thread has a queue */
1062 int init_thread_queue( struct thread *thread )
1064 if (thread->queue) return 1;
1065 return (create_msg_queue( thread, NULL ) != NULL);
1068 /* attach two thread input data structures */
1069 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1071 struct desktop *desktop;
1072 struct thread_input *input;
1073 int ret;
1075 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1076 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1077 input = (struct thread_input *)grab_object( thread_to->queue->input );
1078 if (input->desktop != desktop)
1080 set_error( STATUS_ACCESS_DENIED );
1081 release_object( input );
1082 release_object( desktop );
1083 return 0;
1085 release_object( desktop );
1087 if (thread_from->queue)
1089 if (!input->focus) input->focus = thread_from->queue->input->focus;
1090 if (!input->active) input->active = thread_from->queue->input->active;
1093 ret = assign_thread_input( thread_from, input );
1094 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1095 release_object( input );
1096 return ret;
1099 /* detach two thread input data structures */
1100 void detach_thread_input( struct thread *thread_from )
1102 struct thread *thread;
1103 struct thread_input *input, *old_input = thread_from->queue->input;
1105 if ((input = create_thread_input( thread_from )))
1107 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1109 if (thread == thread_from)
1111 input->focus = old_input->focus;
1112 old_input->focus = 0;
1114 release_object( thread );
1116 if (old_input->active && (thread = get_window_thread( old_input->active )))
1118 if (thread == thread_from)
1120 input->active = old_input->active;
1121 old_input->active = 0;
1123 release_object( thread );
1125 assign_thread_input( thread_from, input );
1126 release_object( input );
1131 /* set the next timer to expire */
1132 static void set_next_timer( struct msg_queue *queue )
1134 struct list *ptr;
1136 if (queue->timeout)
1138 remove_timeout_user( queue->timeout );
1139 queue->timeout = NULL;
1141 if ((ptr = list_head( &queue->pending_timers )))
1143 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1144 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1146 /* set/clear QS_TIMER bit */
1147 if (list_empty( &queue->expired_timers ))
1148 clear_queue_bits( queue, QS_TIMER );
1149 else
1150 set_queue_bits( queue, QS_TIMER );
1153 /* find a timer from its window and id */
1154 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1155 unsigned int msg, lparam_t id )
1157 struct list *ptr;
1159 /* we need to search both lists */
1161 LIST_FOR_EACH( ptr, &queue->pending_timers )
1163 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1164 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1166 LIST_FOR_EACH( ptr, &queue->expired_timers )
1168 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1169 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1171 return NULL;
1174 /* callback for the next timer expiration */
1175 static void timer_callback( void *private )
1177 struct msg_queue *queue = private;
1178 struct list *ptr;
1180 queue->timeout = NULL;
1181 /* move on to the next timer */
1182 ptr = list_head( &queue->pending_timers );
1183 list_remove( ptr );
1184 list_add_tail( &queue->expired_timers, ptr );
1185 set_next_timer( queue );
1188 /* link a timer at its rightful place in the queue list */
1189 static void link_timer( struct msg_queue *queue, struct timer *timer )
1191 struct list *ptr;
1193 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1195 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1196 if (t->when >= timer->when) break;
1198 list_add_before( ptr, &timer->entry );
1201 /* remove a timer from the queue timer list and free it */
1202 static void free_timer( struct msg_queue *queue, struct timer *timer )
1204 list_remove( &timer->entry );
1205 free( timer );
1206 set_next_timer( queue );
1209 /* restart an expired timer */
1210 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1212 list_remove( &timer->entry );
1213 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1214 link_timer( queue, timer );
1215 set_next_timer( queue );
1218 /* find an expired timer matching the filtering parameters */
1219 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1220 unsigned int get_first, unsigned int get_last,
1221 int remove )
1223 struct list *ptr;
1225 LIST_FOR_EACH( ptr, &queue->expired_timers )
1227 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1228 if (win && timer->win != win) continue;
1229 if (check_msg_filter( timer->msg, get_first, get_last ))
1231 if (remove) restart_timer( queue, timer );
1232 return timer;
1235 return NULL;
1238 /* add a timer */
1239 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1241 struct timer *timer = mem_alloc( sizeof(*timer) );
1242 if (timer)
1244 timer->rate = max( rate, 1 );
1245 timer->when = current_time + (timeout_t)timer->rate * 10000;
1246 link_timer( queue, timer );
1247 /* check if we replaced the next timer */
1248 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1250 return timer;
1253 /* change the input key state for a given key */
1254 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1256 if (down)
1258 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1259 keystate[key] |= down;
1261 else keystate[key] &= ~0x80;
1264 /* update the input key state for a keyboard message */
1265 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1266 const struct message *msg )
1268 unsigned char key;
1269 int down = 0;
1271 switch (msg->msg)
1273 case WM_LBUTTONDOWN:
1274 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1275 /* fall through */
1276 case WM_LBUTTONUP:
1277 set_input_key_state( keystate, VK_LBUTTON, down );
1278 break;
1279 case WM_MBUTTONDOWN:
1280 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1281 /* fall through */
1282 case WM_MBUTTONUP:
1283 set_input_key_state( keystate, VK_MBUTTON, down );
1284 break;
1285 case WM_RBUTTONDOWN:
1286 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1287 /* fall through */
1288 case WM_RBUTTONUP:
1289 set_input_key_state( keystate, VK_RBUTTON, down );
1290 break;
1291 case WM_XBUTTONDOWN:
1292 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1293 /* fall through */
1294 case WM_XBUTTONUP:
1295 if (msg->wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1296 else if (msg->wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1297 break;
1298 case WM_KEYDOWN:
1299 case WM_SYSKEYDOWN:
1300 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1301 /* fall through */
1302 case WM_KEYUP:
1303 case WM_SYSKEYUP:
1304 key = (unsigned char)msg->wparam;
1305 set_input_key_state( keystate, key, down );
1306 switch(key)
1308 case VK_LCONTROL:
1309 case VK_RCONTROL:
1310 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1311 set_input_key_state( keystate, VK_CONTROL, down );
1312 break;
1313 case VK_LMENU:
1314 case VK_RMENU:
1315 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1316 set_input_key_state( keystate, VK_MENU, down );
1317 break;
1318 case VK_LSHIFT:
1319 case VK_RSHIFT:
1320 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1321 set_input_key_state( keystate, VK_SHIFT, down );
1322 break;
1324 break;
1328 /* release the hardware message currently being processed by the given thread */
1329 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1330 int remove )
1332 struct thread_input *input = queue->input;
1333 struct message *msg;
1335 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1337 if (msg->unique_id == hw_id) break;
1339 if (&msg->entry == &input->msg_list) return; /* not found */
1341 /* clear the queue bit for that message */
1342 if (remove)
1344 struct message *other;
1345 int clr_bit;
1347 clr_bit = get_hardware_msg_bit( msg );
1348 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1350 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1352 clr_bit = 0;
1353 break;
1356 if (clr_bit) clear_queue_bits( queue, clr_bit );
1358 update_input_key_state( input->desktop, input->keystate, msg );
1359 list_remove( &msg->entry );
1360 free_message( msg );
1364 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1366 struct hotkey *hotkey;
1367 unsigned int modifiers = 0;
1369 if (msg->msg != WM_KEYDOWN) return 0;
1371 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1372 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1373 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1374 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1376 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1378 if (hotkey->vkey != msg->wparam) continue;
1379 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1382 return 0;
1384 found:
1385 msg->type = MSG_POSTED;
1386 msg->win = hotkey->win;
1387 msg->msg = WM_HOTKEY;
1388 msg->wparam = hotkey->id;
1389 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1391 free( msg->data );
1392 msg->data = NULL;
1393 msg->data_size = 0;
1395 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1396 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1397 hotkey->queue->hotkey_count++;
1398 return 1;
1401 /* find the window that should receive a given hardware message */
1402 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1403 struct message *msg, unsigned int *msg_code,
1404 struct thread **thread )
1406 user_handle_t win = 0;
1408 *thread = NULL;
1409 *msg_code = msg->msg;
1410 if (msg->msg == WM_INPUT)
1412 if (!(win = msg->win) && input) win = input->focus;
1414 else if (is_keyboard_msg( msg ))
1416 if (input && !(win = input->focus))
1418 win = input->active;
1419 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1422 else if (!input || !(win = input->capture)) /* mouse message */
1424 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1425 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1427 *thread = window_thread_from_point( win, msg->x, msg->y );
1430 if (!*thread)
1431 *thread = get_window_thread( win );
1432 return win;
1435 static struct rawinput_device_entry *find_rawinput_device( unsigned short usage_page, unsigned short usage )
1437 struct rawinput_device_entry *e;
1439 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
1441 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1442 return e;
1445 return NULL;
1448 static void update_rawinput_device(const struct rawinput_device *device)
1450 struct rawinput_device_entry *e;
1452 if (!(e = find_rawinput_device( device->usage_page, device->usage )))
1454 if (!(e = mem_alloc( sizeof(*e) ))) return;
1455 list_add_tail( &current->process->rawinput_devices, &e->entry );
1458 if (device->flags & RIDEV_REMOVE)
1460 list_remove( &e->entry );
1461 free( e );
1462 return;
1465 e->device = *device;
1466 e->device.target = get_user_full_handle( e->device.target );
1469 /* queue a hardware message into a given thread input */
1470 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1472 user_handle_t win;
1473 struct thread *thread;
1474 struct thread_input *input;
1475 unsigned int msg_code;
1477 update_input_key_state( desktop, desktop->keystate, msg );
1478 last_input_time = get_tick_count();
1479 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1481 if (is_keyboard_msg( msg ))
1483 if (queue_hotkey_message( desktop, msg )) return;
1484 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1485 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1486 msg->lparam &= ~(KF_EXTENDED << 16);
1488 else if (msg->msg != WM_INPUT)
1490 if (msg->msg == WM_MOUSEMOVE)
1492 int x = min( max( msg->x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
1493 int y = min( max( msg->y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
1494 if (desktop->cursor.x != x || desktop->cursor.y != y) always_queue = 1;
1495 desktop->cursor.x = x;
1496 desktop->cursor.y = y;
1497 desktop->cursor.last_change = get_tick_count();
1499 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1500 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1501 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1502 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1503 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1504 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1505 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1507 msg->x = desktop->cursor.x;
1508 msg->y = desktop->cursor.y;
1510 if (msg->win && (thread = get_window_thread( msg->win )))
1512 input = thread->queue->input;
1513 release_object( thread );
1515 else input = desktop->foreground_input;
1517 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1518 if (!win || !thread)
1520 if (input) update_input_key_state( input->desktop, input->keystate, msg );
1521 free_message( msg );
1522 return;
1524 input = thread->queue->input;
1526 if (win != desktop->cursor.win) always_queue = 1;
1527 desktop->cursor.win = win;
1529 if (!always_queue || merge_message( input, msg )) free_message( msg );
1530 else
1532 msg->unique_id = 0; /* will be set once we return it to the app */
1533 list_add_tail( &input->msg_list, &msg->entry );
1534 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1536 release_object( thread );
1539 /* send the low-level hook message for a given hardware message */
1540 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1541 const hw_input_t *input, struct msg_queue *sender )
1543 struct thread *hook_thread;
1544 struct msg_queue *queue;
1545 struct message *msg;
1546 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1547 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1549 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1550 if (!(queue = hook_thread->queue)) return 0;
1551 if (is_queue_hung( queue )) return 0;
1553 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1555 msg->type = MSG_HOOK_LL;
1556 msg->win = 0;
1557 msg->msg = id;
1558 msg->wparam = hardware_msg->msg;
1559 msg->x = hardware_msg->x;
1560 msg->y = hardware_msg->y;
1561 msg->time = hardware_msg->time;
1562 msg->data_size = hardware_msg->data_size;
1563 msg->result = NULL;
1565 if (input->type == INPUT_KEYBOARD)
1567 unsigned short vkey = input->kbd.vkey;
1568 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1569 msg->lparam = (input->kbd.scan << 16) | vkey;
1571 else msg->lparam = input->mouse.data << 16;
1573 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1574 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1576 free_message( msg );
1577 return 0;
1579 msg->result->hardware_msg = hardware_msg;
1580 msg->result->desktop = (struct desktop *)grab_object( desktop );
1581 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1582 set_queue_bits( queue, QS_SENDMESSAGE );
1583 return 1;
1586 /* queue a hardware message for a mouse event */
1587 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1588 unsigned int hook_flags, struct msg_queue *sender )
1590 const struct rawinput_device *device;
1591 struct hardware_msg_data *msg_data;
1592 struct message *msg;
1593 unsigned int i, time, flags;
1594 int wait = 0, x, y;
1596 static const unsigned int messages[] =
1598 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1599 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1600 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1601 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1602 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1603 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1604 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1605 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1606 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1607 0, /* 0x0200 = unused */
1608 0, /* 0x0400 = unused */
1609 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1610 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1613 desktop->cursor.last_change = get_tick_count();
1614 flags = input->mouse.flags;
1615 time = input->mouse.time;
1616 if (!time) time = desktop->cursor.last_change;
1618 if (flags & MOUSEEVENTF_MOVE)
1620 if (flags & MOUSEEVENTF_ABSOLUTE)
1622 x = input->mouse.x;
1623 y = input->mouse.y;
1624 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1625 x == desktop->cursor.x && y == desktop->cursor.y)
1626 flags &= ~MOUSEEVENTF_MOVE;
1628 else
1630 x = desktop->cursor.x + input->mouse.x;
1631 y = desktop->cursor.y + input->mouse.y;
1634 else
1636 x = desktop->cursor.x;
1637 y = desktop->cursor.y;
1640 if ((device = current->process->rawinput_mouse))
1642 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1643 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1645 free( msg );
1646 return 0;
1649 msg->type = MSG_HARDWARE;
1650 msg->win = device->target;
1651 msg->msg = WM_INPUT;
1652 msg->wparam = RIM_INPUT;
1653 msg->lparam = 0;
1654 msg->time = time;
1655 msg->data = msg_data;
1656 msg->data_size = sizeof(*msg_data);
1657 msg->result = NULL;
1659 msg_data->info = input->mouse.info;
1660 msg_data->flags = flags;
1661 msg_data->rawinput.type = RIM_TYPEMOUSE;
1662 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1663 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1664 msg_data->rawinput.mouse.data = input->mouse.data;
1666 queue_hardware_message( desktop, msg, 0 );
1669 for (i = 0; i < sizeof(messages)/sizeof(messages[0]); i++)
1671 if (!messages[i]) continue;
1672 if (!(flags & (1 << i))) continue;
1673 flags &= ~(1 << i);
1675 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1676 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1678 free( msg );
1679 return 0;
1681 memset( msg_data, 0, sizeof(*msg_data) );
1683 msg->type = MSG_HARDWARE;
1684 msg->win = get_user_full_handle( win );
1685 msg->msg = messages[i];
1686 msg->wparam = input->mouse.data << 16;
1687 msg->lparam = 0;
1688 msg->x = x;
1689 msg->y = y;
1690 msg->time = time;
1691 msg->result = NULL;
1692 msg->data = msg_data;
1693 msg->data_size = sizeof(*msg_data);
1694 msg_data->info = input->mouse.info;
1695 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLMHF_INJECTED;
1697 /* specify a sender only when sending the last message */
1698 if (!(flags & ((1 << sizeof(messages)/sizeof(messages[0])) - 1)))
1700 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1701 queue_hardware_message( desktop, msg, 0 );
1703 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1704 queue_hardware_message( desktop, msg, 0 );
1706 return wait;
1709 /* queue a hardware message for a keyboard event */
1710 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1711 unsigned int hook_flags, struct msg_queue *sender )
1713 const struct rawinput_device *device;
1714 struct hardware_msg_data *msg_data;
1715 struct message *msg;
1716 unsigned char vkey = input->kbd.vkey;
1717 unsigned int message_code, time;
1718 int wait;
1720 if (!(time = input->kbd.time)) time = get_tick_count();
1722 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1724 switch (vkey)
1726 case VK_MENU:
1727 case VK_LMENU:
1728 case VK_RMENU:
1729 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1730 break;
1731 case VK_CONTROL:
1732 case VK_LCONTROL:
1733 case VK_RCONTROL:
1734 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1735 break;
1736 case VK_SHIFT:
1737 case VK_LSHIFT:
1738 case VK_RSHIFT:
1739 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1740 break;
1744 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1745 switch (vkey)
1747 case VK_LMENU:
1748 case VK_RMENU:
1749 if (input->kbd.flags & KEYEVENTF_KEYUP)
1751 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1752 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1753 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1754 message_code = WM_SYSKEYUP;
1755 desktop->keystate[VK_MENU] &= ~0x02;
1757 else
1759 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1760 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1761 message_code = WM_SYSKEYDOWN;
1762 desktop->keystate[VK_MENU] |= 0x02;
1764 break;
1766 case VK_LCONTROL:
1767 case VK_RCONTROL:
1768 /* send WM_SYSKEYUP on release if Alt still pressed */
1769 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1770 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1771 message_code = WM_SYSKEYUP;
1772 desktop->keystate[VK_MENU] &= ~0x02;
1773 break;
1775 default:
1776 /* send WM_SYSKEY for Alt-anykey and for F10 */
1777 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1778 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1779 /* fall through */
1780 case VK_F10:
1781 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1782 desktop->keystate[VK_MENU] &= ~0x02;
1783 break;
1786 if ((device = current->process->rawinput_kbd))
1788 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1789 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1791 free( msg );
1792 return 0;
1795 msg->type = MSG_HARDWARE;
1796 msg->win = device->target;
1797 msg->msg = WM_INPUT;
1798 msg->wparam = RIM_INPUT;
1799 msg->lparam = 0;
1800 msg->time = time;
1801 msg->data = msg_data;
1802 msg->data_size = sizeof(*msg_data);
1803 msg->result = NULL;
1805 msg_data->info = input->kbd.info;
1806 msg_data->flags = input->kbd.flags;
1807 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1808 msg_data->rawinput.kbd.message = message_code;
1809 msg_data->rawinput.kbd.vkey = vkey;
1810 msg_data->rawinput.kbd.scan = input->kbd.scan;
1812 queue_hardware_message( desktop, msg, 0 );
1815 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1816 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1818 free( msg );
1819 return 0;
1821 memset( msg_data, 0, sizeof(*msg_data) );
1823 msg->type = MSG_HARDWARE;
1824 msg->win = get_user_full_handle( win );
1825 msg->msg = message_code;
1826 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1827 msg->time = time;
1828 msg->result = NULL;
1829 msg->data = msg_data;
1830 msg->data_size = sizeof(*msg_data);
1831 msg_data->info = input->kbd.info;
1832 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLKHF_INJECTED;
1834 if (input->kbd.flags & KEYEVENTF_UNICODE)
1836 msg->wparam = VK_PACKET;
1838 else
1840 unsigned int flags = 0;
1841 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1842 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1843 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1844 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1846 msg->wparam = vkey;
1847 msg->lparam |= flags << 16;
1848 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1851 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1852 queue_hardware_message( desktop, msg, 1 );
1854 return wait;
1857 /* queue a hardware message for a custom type of event */
1858 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1859 const hw_input_t *input )
1861 struct hardware_msg_data *msg_data;
1862 struct message *msg;
1864 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
1865 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1867 free( msg );
1868 return;
1870 memset( msg_data, 0, sizeof(*msg_data) );
1872 msg->type = MSG_HARDWARE;
1873 msg->win = get_user_full_handle( win );
1874 msg->msg = input->hw.msg;
1875 msg->wparam = 0;
1876 msg->lparam = input->hw.lparam;
1877 msg->x = desktop->cursor.x;
1878 msg->y = desktop->cursor.y;
1879 msg->time = get_tick_count();
1880 msg->result = NULL;
1881 msg->data = msg_data;
1882 msg->data_size = sizeof(*msg_data);
1884 queue_hardware_message( desktop, msg, 1 );
1887 /* check message filter for a hardware message */
1888 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1889 user_handle_t filter_win, unsigned int first, unsigned int last )
1891 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1893 /* we can only test the window for a keyboard message since the
1894 * dest window for a mouse message depends on hittest */
1895 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1896 return 0;
1897 /* the message code is final for a keyboard message, we can simply check it */
1898 return check_msg_filter( msg_code, first, last );
1900 else /* mouse message */
1902 /* we need to check all possible values that the message can have in the end */
1904 if (check_msg_filter( msg_code, first, last )) return 1;
1905 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1907 /* all other messages can become non-client messages */
1908 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1910 /* clicks can become double-clicks or non-client double-clicks */
1911 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1912 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1914 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1915 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1917 return 0;
1922 /* find a hardware message for the given queue */
1923 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1924 unsigned int first, unsigned int last, unsigned int flags,
1925 struct get_message_reply *reply )
1927 struct thread_input *input = thread->queue->input;
1928 struct thread *win_thread;
1929 struct list *ptr;
1930 user_handle_t win;
1931 int clear_bits, got_one = 0;
1932 unsigned int msg_code;
1934 ptr = list_head( &input->msg_list );
1935 if (hw_id)
1937 while (ptr)
1939 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1940 if (msg->unique_id == hw_id) break;
1941 ptr = list_next( &input->msg_list, ptr );
1943 if (!ptr) ptr = list_head( &input->msg_list );
1944 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1947 if (ptr == list_head( &input->msg_list ))
1948 clear_bits = QS_INPUT;
1949 else
1950 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1952 while (ptr)
1954 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1955 struct hardware_msg_data *data = msg->data;
1957 ptr = list_next( &input->msg_list, ptr );
1958 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
1959 if (!win || !win_thread)
1961 /* no window at all, remove it */
1962 update_input_key_state( input->desktop, input->keystate, msg );
1963 list_remove( &msg->entry );
1964 free_message( msg );
1965 continue;
1967 if (win_thread != thread)
1969 if (win_thread->queue->input == input)
1971 /* wake the other thread */
1972 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1973 got_one = 1;
1975 else
1977 /* for another thread input, drop it */
1978 update_input_key_state( input->desktop, input->keystate, msg );
1979 list_remove( &msg->entry );
1980 free_message( msg );
1982 release_object( win_thread );
1983 continue;
1985 release_object( win_thread );
1987 /* if we already got a message for another thread, or if it doesn't
1988 * match the filter we skip it */
1989 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1991 clear_bits &= ~get_hardware_msg_bit( msg );
1992 continue;
1994 /* now we can return it */
1995 if (!msg->unique_id) msg->unique_id = get_unique_id();
1996 reply->type = MSG_HARDWARE;
1997 reply->win = win;
1998 reply->msg = msg_code;
1999 reply->wparam = msg->wparam;
2000 reply->lparam = msg->lparam;
2001 reply->x = msg->x;
2002 reply->y = msg->y;
2003 reply->time = msg->time;
2005 data->hw_id = msg->unique_id;
2006 set_reply_data( msg->data, msg->data_size );
2007 if (msg->msg == WM_INPUT && (flags & PM_REMOVE))
2008 release_hardware_message( current->queue, data->hw_id, 1 );
2009 return 1;
2011 /* nothing found, clear the hardware queue bits */
2012 clear_queue_bits( thread->queue, clear_bits );
2013 return 0;
2016 /* increment (or decrement if 'incr' is negative) the queue paint count */
2017 void inc_queue_paint_count( struct thread *thread, int incr )
2019 struct msg_queue *queue = thread->queue;
2021 assert( queue );
2023 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2025 if (queue->paint_count)
2026 set_queue_bits( queue, QS_PAINT );
2027 else
2028 clear_queue_bits( queue, QS_PAINT );
2032 /* remove all messages and timers belonging to a certain window */
2033 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2035 struct msg_queue *queue = thread->queue;
2036 struct list *ptr;
2037 int i;
2039 if (!queue) return;
2041 /* remove timers */
2043 ptr = list_head( &queue->pending_timers );
2044 while (ptr)
2046 struct list *next = list_next( &queue->pending_timers, ptr );
2047 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2048 if (timer->win == win) free_timer( queue, timer );
2049 ptr = next;
2051 ptr = list_head( &queue->expired_timers );
2052 while (ptr)
2054 struct list *next = list_next( &queue->expired_timers, ptr );
2055 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2056 if (timer->win == win) free_timer( queue, timer );
2057 ptr = next;
2060 /* remove messages */
2061 for (i = 0; i < NB_MSG_KINDS; i++)
2063 struct list *ptr, *next;
2065 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2067 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2068 if (msg->win == win)
2070 if (msg->msg == WM_QUIT && !queue->quit_message)
2072 queue->quit_message = 1;
2073 queue->exit_code = msg->wparam;
2075 remove_queue_message( queue, msg, i );
2080 thread_input_cleanup_window( queue, win );
2083 /* post a message to a window */
2084 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2086 struct message *msg;
2087 struct thread *thread = get_window_thread( win );
2089 if (!thread) return;
2091 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2093 msg->type = MSG_POSTED;
2094 msg->win = get_user_full_handle( win );
2095 msg->msg = message;
2096 msg->wparam = wparam;
2097 msg->lparam = lparam;
2098 msg->result = NULL;
2099 msg->data = NULL;
2100 msg->data_size = 0;
2102 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2104 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2105 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2106 if (message == WM_HOTKEY)
2108 set_queue_bits( thread->queue, QS_HOTKEY );
2109 thread->queue->hotkey_count++;
2112 release_object( thread );
2115 /* post a win event */
2116 void post_win_event( struct thread *thread, unsigned int event,
2117 user_handle_t win, unsigned int object_id,
2118 unsigned int child_id, client_ptr_t hook_proc,
2119 const WCHAR *module, data_size_t module_size,
2120 user_handle_t hook)
2122 struct message *msg;
2124 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2126 struct winevent_msg_data *data;
2128 msg->type = MSG_WINEVENT;
2129 msg->win = get_user_full_handle( win );
2130 msg->msg = event;
2131 msg->wparam = object_id;
2132 msg->lparam = child_id;
2133 msg->time = get_tick_count();
2134 msg->result = NULL;
2136 if ((data = malloc( sizeof(*data) + module_size )))
2138 data->hook = hook;
2139 data->tid = get_thread_id( current );
2140 data->hook_proc = hook_proc;
2141 memcpy( data + 1, module, module_size );
2143 msg->data = data;
2144 msg->data_size = sizeof(*data) + module_size;
2146 if (debug_level > 1)
2147 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2148 get_thread_id(thread), event, win, object_id, child_id );
2149 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2150 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2152 else
2153 free( msg );
2157 /* free all hotkeys on a desktop, optionally filtering by window */
2158 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2160 struct hotkey *hotkey, *hotkey2;
2162 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2164 if (!window || hotkey->win == window)
2166 list_remove( &hotkey->entry );
2167 free( hotkey );
2173 /* check if the thread owning the window is hung */
2174 DECL_HANDLER(is_window_hung)
2176 struct thread *thread;
2178 thread = get_window_thread( req->win );
2179 if (thread)
2181 reply->is_hung = is_queue_hung( thread->queue );
2182 release_object( thread );
2184 else reply->is_hung = 0;
2188 /* get the message queue of the current thread */
2189 DECL_HANDLER(get_msg_queue)
2191 struct msg_queue *queue = get_current_queue();
2193 reply->handle = 0;
2194 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2198 /* set the file descriptor associated to the current thread queue */
2199 DECL_HANDLER(set_queue_fd)
2201 struct msg_queue *queue = get_current_queue();
2202 struct file *file;
2203 int unix_fd;
2205 if (queue->fd) /* fd can only be set once */
2207 set_error( STATUS_ACCESS_DENIED );
2208 return;
2210 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2212 if ((unix_fd = get_file_unix_fd( file )) != -1)
2214 if ((unix_fd = dup( unix_fd )) != -1)
2215 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2216 else
2217 file_set_error();
2219 release_object( file );
2223 /* set the current message queue wakeup mask */
2224 DECL_HANDLER(set_queue_mask)
2226 struct msg_queue *queue = get_current_queue();
2228 if (queue)
2230 queue->wake_mask = req->wake_mask;
2231 queue->changed_mask = req->changed_mask;
2232 reply->wake_bits = queue->wake_bits;
2233 reply->changed_bits = queue->changed_bits;
2234 if (is_signaled( queue ))
2236 /* if skip wait is set, do what would have been done in the subsequent wait */
2237 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2238 else wake_up( &queue->obj, 0 );
2244 /* get the current message queue status */
2245 DECL_HANDLER(get_queue_status)
2247 struct msg_queue *queue = current->queue;
2248 if (queue)
2250 reply->wake_bits = queue->wake_bits;
2251 reply->changed_bits = queue->changed_bits;
2252 queue->changed_bits &= ~req->clear_bits;
2254 else reply->wake_bits = reply->changed_bits = 0;
2258 /* send a message to a thread queue */
2259 DECL_HANDLER(send_message)
2261 struct message *msg;
2262 struct msg_queue *send_queue = get_current_queue();
2263 struct msg_queue *recv_queue = NULL;
2264 struct thread *thread = NULL;
2266 if (!(thread = get_thread_from_id( req->id ))) return;
2268 if (!(recv_queue = thread->queue))
2270 set_error( STATUS_INVALID_PARAMETER );
2271 release_object( thread );
2272 return;
2274 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2276 set_error( STATUS_TIMEOUT );
2277 release_object( thread );
2278 return;
2281 if ((msg = mem_alloc( sizeof(*msg) )))
2283 msg->type = req->type;
2284 msg->win = get_user_full_handle( req->win );
2285 msg->msg = req->msg;
2286 msg->wparam = req->wparam;
2287 msg->lparam = req->lparam;
2288 msg->result = NULL;
2289 msg->data = NULL;
2290 msg->data_size = get_req_data_size();
2292 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2294 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2296 free( msg );
2297 release_object( thread );
2298 return;
2301 switch(msg->type)
2303 case MSG_OTHER_PROCESS:
2304 case MSG_ASCII:
2305 case MSG_UNICODE:
2306 case MSG_CALLBACK:
2307 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2309 free_message( msg );
2310 break;
2312 /* fall through */
2313 case MSG_NOTIFY:
2314 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2315 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2316 break;
2317 case MSG_POSTED:
2318 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2319 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2320 if (msg->msg == WM_HOTKEY)
2322 set_queue_bits( recv_queue, QS_HOTKEY );
2323 recv_queue->hotkey_count++;
2325 break;
2326 case MSG_HARDWARE: /* should use send_hardware_message instead */
2327 case MSG_CALLBACK_RESULT: /* cannot send this one */
2328 case MSG_HOOK_LL: /* generated internally */
2329 default:
2330 set_error( STATUS_INVALID_PARAMETER );
2331 free( msg );
2332 break;
2335 release_object( thread );
2338 /* send a hardware message to a thread queue */
2339 DECL_HANDLER(send_hardware_message)
2341 struct thread *thread = NULL;
2342 struct desktop *desktop;
2343 struct msg_queue *sender = get_current_queue();
2344 data_size_t size = min( 256, get_reply_max_size() );
2346 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2348 if (req->win)
2350 if (!(thread = get_window_thread( req->win ))) return;
2351 if (desktop != thread->queue->input->desktop)
2353 /* don't allow queuing events to a different desktop */
2354 release_object( desktop );
2355 return;
2359 reply->prev_x = desktop->cursor.x;
2360 reply->prev_y = desktop->cursor.y;
2362 switch (req->input.type)
2364 case INPUT_MOUSE:
2365 reply->wait = queue_mouse_message( desktop, req->win, &req->input, req->flags, sender );
2366 break;
2367 case INPUT_KEYBOARD:
2368 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, req->flags, sender );
2369 break;
2370 case INPUT_HARDWARE:
2371 queue_custom_hardware_message( desktop, req->win, &req->input );
2372 break;
2373 default:
2374 set_error( STATUS_INVALID_PARAMETER );
2376 if (thread) release_object( thread );
2378 reply->new_x = desktop->cursor.x;
2379 reply->new_y = desktop->cursor.y;
2380 set_reply_data( desktop->keystate, size );
2381 release_object( desktop );
2384 /* post a quit message to the current queue */
2385 DECL_HANDLER(post_quit_message)
2387 struct msg_queue *queue = get_current_queue();
2389 if (!queue)
2390 return;
2392 queue->quit_message = 1;
2393 queue->exit_code = req->exit_code;
2394 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2397 /* get a message from the current queue */
2398 DECL_HANDLER(get_message)
2400 struct timer *timer;
2401 struct list *ptr;
2402 struct msg_queue *queue = get_current_queue();
2403 user_handle_t get_win = get_user_full_handle( req->get_win );
2404 unsigned int filter = req->flags >> 16;
2406 reply->active_hooks = get_active_hooks();
2408 if (!queue) return;
2409 queue->last_get_msg = current_time;
2410 if (!filter) filter = QS_ALLINPUT;
2412 /* first check for sent messages */
2413 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2415 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2416 receive_message( queue, msg, reply );
2417 return;
2420 /* clear changed bits so we can wait on them if we don't find a message */
2421 if (filter & QS_POSTMESSAGE)
2423 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2424 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2426 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2427 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2429 /* then check for posted messages */
2430 if ((filter & QS_POSTMESSAGE) &&
2431 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2432 return;
2434 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2435 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2436 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2437 return;
2439 /* only check for quit messages if not posted messages pending */
2440 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2441 return;
2443 /* then check for any raw hardware message */
2444 if ((filter & QS_INPUT) &&
2445 filter_contains_hw_range( req->get_first, req->get_last ) &&
2446 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2447 return;
2449 /* now check for WM_PAINT */
2450 if ((filter & QS_PAINT) &&
2451 queue->paint_count &&
2452 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2453 (reply->win = find_window_to_repaint( get_win, current )))
2455 reply->type = MSG_POSTED;
2456 reply->msg = WM_PAINT;
2457 reply->wparam = 0;
2458 reply->lparam = 0;
2459 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2460 return;
2463 /* now check for timer */
2464 if ((filter & QS_TIMER) &&
2465 (timer = find_expired_timer( queue, get_win, req->get_first,
2466 req->get_last, (req->flags & PM_REMOVE) )))
2468 reply->type = MSG_POSTED;
2469 reply->win = timer->win;
2470 reply->msg = timer->msg;
2471 reply->wparam = timer->id;
2472 reply->lparam = timer->lparam;
2473 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2474 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2475 set_event( current->process->idle_event );
2476 return;
2479 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2480 queue->wake_mask = req->wake_mask;
2481 queue->changed_mask = req->changed_mask;
2482 set_error( STATUS_PENDING ); /* FIXME */
2486 /* reply to a sent message */
2487 DECL_HANDLER(reply_message)
2489 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2490 else if (current->queue->recv_result)
2491 reply_message( current->queue, req->result, 0, req->remove,
2492 get_req_data(), get_req_data_size() );
2496 /* accept the current hardware message */
2497 DECL_HANDLER(accept_hardware_message)
2499 if (current->queue)
2500 release_hardware_message( current->queue, req->hw_id, req->remove );
2501 else
2502 set_error( STATUS_ACCESS_DENIED );
2506 /* retrieve the reply for the last message sent */
2507 DECL_HANDLER(get_message_reply)
2509 struct message_result *result;
2510 struct list *entry;
2511 struct msg_queue *queue = current->queue;
2513 if (queue)
2515 set_error( STATUS_PENDING );
2516 reply->result = 0;
2518 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2520 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2521 if (result->replied || req->cancel)
2523 if (result->replied)
2525 reply->result = result->result;
2526 set_error( result->error );
2527 if (result->data)
2529 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2530 set_reply_data_ptr( result->data, data_len );
2531 result->data = NULL;
2532 result->data_size = 0;
2535 remove_result_from_sender( result );
2537 entry = list_head( &queue->send_result );
2538 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2539 else
2541 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2542 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2543 else clear_queue_bits( queue, QS_SMRESULT );
2547 else set_error( STATUS_ACCESS_DENIED );
2551 /* set a window timer */
2552 DECL_HANDLER(set_win_timer)
2554 struct timer *timer;
2555 struct msg_queue *queue;
2556 struct thread *thread = NULL;
2557 user_handle_t win = 0;
2558 lparam_t id = req->id;
2560 if (req->win)
2562 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2564 set_error( STATUS_INVALID_HANDLE );
2565 return;
2567 if (thread->process != current->process)
2569 release_object( thread );
2570 set_error( STATUS_ACCESS_DENIED );
2571 return;
2573 queue = thread->queue;
2574 /* remove it if it existed already */
2575 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2577 else
2579 queue = get_current_queue();
2580 /* look for a timer with this id */
2581 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2583 /* free and reuse id */
2584 free_timer( queue, timer );
2586 else
2588 lparam_t end_id = queue->next_timer_id;
2590 /* find a free id for it */
2591 while (1)
2593 id = queue->next_timer_id;
2594 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2596 if (!find_timer( queue, 0, req->msg, id )) break;
2597 if (queue->next_timer_id == end_id)
2599 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2600 return;
2606 if ((timer = set_timer( queue, req->rate )))
2608 timer->win = win;
2609 timer->msg = req->msg;
2610 timer->id = id;
2611 timer->lparam = req->lparam;
2612 reply->id = id;
2614 if (thread) release_object( thread );
2617 /* kill a window timer */
2618 DECL_HANDLER(kill_win_timer)
2620 struct timer *timer;
2621 struct thread *thread;
2622 user_handle_t win = 0;
2624 if (req->win)
2626 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2628 set_error( STATUS_INVALID_HANDLE );
2629 return;
2631 if (thread->process != current->process)
2633 release_object( thread );
2634 set_error( STATUS_ACCESS_DENIED );
2635 return;
2638 else thread = (struct thread *)grab_object( current );
2640 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2641 free_timer( thread->queue, timer );
2642 else
2643 set_error( STATUS_INVALID_PARAMETER );
2645 release_object( thread );
2648 DECL_HANDLER(register_hotkey)
2650 struct desktop *desktop;
2651 user_handle_t win_handle = req->window;
2652 struct hotkey *hotkey;
2653 struct hotkey *new_hotkey = NULL;
2654 struct thread *thread;
2655 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2657 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2659 if (win_handle)
2661 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2663 release_object( desktop );
2664 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2665 return;
2668 thread = get_window_thread( win_handle );
2669 if (thread) release_object( thread );
2671 if (thread != current)
2673 release_object( desktop );
2674 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2675 return;
2679 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2681 if (req->vkey == hotkey->vkey &&
2682 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2684 release_object( desktop );
2685 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2686 return;
2688 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2689 new_hotkey = hotkey;
2692 if (new_hotkey)
2694 reply->replaced = 1;
2695 reply->flags = new_hotkey->flags;
2696 reply->vkey = new_hotkey->vkey;
2698 else
2700 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2701 if (new_hotkey)
2703 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2704 new_hotkey->queue = current->queue;
2705 new_hotkey->win = win_handle;
2706 new_hotkey->id = req->id;
2710 if (new_hotkey)
2712 new_hotkey->flags = req->flags;
2713 new_hotkey->vkey = req->vkey;
2716 release_object( desktop );
2719 DECL_HANDLER(unregister_hotkey)
2721 struct desktop *desktop;
2722 user_handle_t win_handle = req->window;
2723 struct hotkey *hotkey;
2724 struct thread *thread;
2726 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2728 if (win_handle)
2730 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2732 release_object( desktop );
2733 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2734 return;
2737 thread = get_window_thread( win_handle );
2738 if (thread) release_object( thread );
2740 if (thread != current)
2742 release_object( desktop );
2743 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2744 return;
2748 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2750 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2751 goto found;
2754 release_object( desktop );
2755 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2756 return;
2758 found:
2759 reply->flags = hotkey->flags;
2760 reply->vkey = hotkey->vkey;
2761 list_remove( &hotkey->entry );
2762 free( hotkey );
2763 release_object( desktop );
2766 /* attach (or detach) thread inputs */
2767 DECL_HANDLER(attach_thread_input)
2769 struct thread *thread_from = get_thread_from_id( req->tid_from );
2770 struct thread *thread_to = get_thread_from_id( req->tid_to );
2772 if (!thread_from || !thread_to)
2774 if (thread_from) release_object( thread_from );
2775 if (thread_to) release_object( thread_to );
2776 return;
2778 if (thread_from != thread_to)
2780 if (req->attach)
2782 if ((thread_to->queue || thread_to == current) &&
2783 (thread_from->queue || thread_from == current))
2784 attach_thread_input( thread_from, thread_to );
2785 else
2786 set_error( STATUS_INVALID_PARAMETER );
2788 else
2790 if (thread_from->queue && thread_to->queue &&
2791 thread_from->queue->input == thread_to->queue->input)
2792 detach_thread_input( thread_from );
2793 else
2794 set_error( STATUS_ACCESS_DENIED );
2797 else set_error( STATUS_ACCESS_DENIED );
2798 release_object( thread_from );
2799 release_object( thread_to );
2803 /* get thread input data */
2804 DECL_HANDLER(get_thread_input)
2806 struct thread *thread = NULL;
2807 struct desktop *desktop;
2808 struct thread_input *input;
2810 if (req->tid)
2812 if (!(thread = get_thread_from_id( req->tid ))) return;
2813 if (!(desktop = get_thread_desktop( thread, 0 )))
2815 release_object( thread );
2816 return;
2818 input = thread->queue ? thread->queue->input : NULL;
2820 else
2822 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2823 input = desktop->foreground_input; /* get the foreground thread info */
2826 if (input)
2828 reply->focus = input->focus;
2829 reply->capture = input->capture;
2830 reply->active = input->active;
2831 reply->menu_owner = input->menu_owner;
2832 reply->move_size = input->move_size;
2833 reply->caret = input->caret;
2834 reply->cursor = input->cursor;
2835 reply->show_count = input->cursor_count;
2836 reply->rect = input->caret_rect;
2839 /* foreground window is active window of foreground thread */
2840 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2841 if (thread) release_object( thread );
2842 release_object( desktop );
2846 /* retrieve queue keyboard state for a given thread */
2847 DECL_HANDLER(get_key_state)
2849 struct thread *thread;
2850 struct desktop *desktop;
2851 data_size_t size = min( 256, get_reply_max_size() );
2853 if (!req->tid) /* get global async key state */
2855 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2856 if (req->key >= 0)
2858 reply->state = desktop->keystate[req->key & 0xff];
2859 desktop->keystate[req->key & 0xff] &= ~0x40;
2861 set_reply_data( desktop->keystate, size );
2862 release_object( desktop );
2864 else
2866 unsigned char *keystate;
2867 if (!(thread = get_thread_from_id( req->tid ))) return;
2868 if (thread->queue)
2870 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2871 set_reply_data( thread->queue->input->keystate, size );
2872 release_object( thread );
2873 return;
2875 release_object( thread );
2877 /* fallback to desktop keystate */
2878 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2879 if (req->key >= 0) reply->state = desktop->keystate[req->key & 0xff] & ~0x40;
2880 if ((keystate = set_reply_data_size( size )))
2882 unsigned int i;
2883 for (i = 0; i < size; i++) keystate[i] = desktop->keystate[i] & ~0x40;
2885 release_object( desktop );
2890 /* set queue keyboard state for a given thread */
2891 DECL_HANDLER(set_key_state)
2893 struct thread *thread;
2894 struct desktop *desktop;
2895 data_size_t size = min( 256, get_req_data_size() );
2897 if (!req->tid) /* set global async key state */
2899 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2900 memcpy( desktop->keystate, get_req_data(), size );
2901 release_object( desktop );
2903 else
2905 if (!(thread = get_thread_from_id( req->tid ))) return;
2906 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
2907 if (req->async && (desktop = get_thread_desktop( thread, 0 )))
2909 memcpy( desktop->keystate, get_req_data(), size );
2910 release_object( desktop );
2912 release_object( thread );
2917 /* set the system foreground window */
2918 DECL_HANDLER(set_foreground_window)
2920 struct thread *thread = NULL;
2921 struct desktop *desktop;
2922 struct msg_queue *queue = get_current_queue();
2924 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2925 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
2926 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
2927 reply->send_msg_new = FALSE;
2929 if (is_valid_foreground_window( req->handle ) &&
2930 (thread = get_window_thread( req->handle )) &&
2931 thread->queue->input->desktop == desktop)
2933 set_foreground_input( desktop, thread->queue->input );
2934 reply->send_msg_new = (desktop->foreground_input != queue->input);
2936 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2938 if (thread) release_object( thread );
2939 release_object( desktop );
2943 /* set the current thread focus window */
2944 DECL_HANDLER(set_focus_window)
2946 struct msg_queue *queue = get_current_queue();
2948 reply->previous = 0;
2949 if (queue && check_queue_input_window( queue, req->handle ))
2951 reply->previous = queue->input->focus;
2952 queue->input->focus = get_user_full_handle( req->handle );
2957 /* set the current thread active window */
2958 DECL_HANDLER(set_active_window)
2960 struct msg_queue *queue = get_current_queue();
2962 reply->previous = 0;
2963 if (queue && check_queue_input_window( queue, req->handle ))
2965 if (!req->handle || make_window_active( req->handle ))
2967 reply->previous = queue->input->active;
2968 queue->input->active = get_user_full_handle( req->handle );
2970 else set_error( STATUS_INVALID_HANDLE );
2975 /* set the current thread capture window */
2976 DECL_HANDLER(set_capture_window)
2978 struct msg_queue *queue = get_current_queue();
2980 reply->previous = reply->full_handle = 0;
2981 if (queue && check_queue_input_window( queue, req->handle ))
2983 struct thread_input *input = queue->input;
2985 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2986 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2988 set_error(STATUS_ACCESS_DENIED);
2989 return;
2991 reply->previous = input->capture;
2992 input->capture = get_user_full_handle( req->handle );
2993 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2994 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2995 reply->full_handle = input->capture;
3000 /* Set the current thread caret window */
3001 DECL_HANDLER(set_caret_window)
3003 struct msg_queue *queue = get_current_queue();
3005 reply->previous = 0;
3006 if (queue && check_queue_input_window( queue, req->handle ))
3008 struct thread_input *input = queue->input;
3010 reply->previous = input->caret;
3011 reply->old_rect = input->caret_rect;
3012 reply->old_hide = input->caret_hide;
3013 reply->old_state = input->caret_state;
3015 set_caret_window( input, get_user_full_handle(req->handle) );
3016 input->caret_rect.right = input->caret_rect.left + req->width;
3017 input->caret_rect.bottom = input->caret_rect.top + req->height;
3022 /* Set the current thread caret information */
3023 DECL_HANDLER(set_caret_info)
3025 struct msg_queue *queue = get_current_queue();
3026 struct thread_input *input;
3028 if (!queue) return;
3029 input = queue->input;
3030 reply->full_handle = input->caret;
3031 reply->old_rect = input->caret_rect;
3032 reply->old_hide = input->caret_hide;
3033 reply->old_state = input->caret_state;
3035 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3037 set_error( STATUS_ACCESS_DENIED );
3038 return;
3040 if (req->flags & SET_CARET_POS)
3042 input->caret_rect.right += req->x - input->caret_rect.left;
3043 input->caret_rect.bottom += req->y - input->caret_rect.top;
3044 input->caret_rect.left = req->x;
3045 input->caret_rect.top = req->y;
3047 if (req->flags & SET_CARET_HIDE)
3049 input->caret_hide += req->hide;
3050 if (input->caret_hide < 0) input->caret_hide = 0;
3052 if (req->flags & SET_CARET_STATE)
3054 switch (req->state)
3056 case CARET_STATE_OFF: input->caret_state = 0; break;
3057 case CARET_STATE_ON: input->caret_state = 1; break;
3058 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3059 case CARET_STATE_ON_IF_MOVED:
3060 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3061 break;
3067 /* get the time of the last input event */
3068 DECL_HANDLER(get_last_input_time)
3070 reply->time = last_input_time;
3073 /* set/get the current cursor */
3074 DECL_HANDLER(set_cursor)
3076 struct msg_queue *queue = get_current_queue();
3077 struct thread_input *input;
3079 if (!queue) return;
3080 input = queue->input;
3082 reply->prev_handle = input->cursor;
3083 reply->prev_count = input->cursor_count;
3084 reply->prev_x = input->desktop->cursor.x;
3085 reply->prev_y = input->desktop->cursor.y;
3087 if (req->flags & SET_CURSOR_HANDLE)
3089 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3091 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3092 return;
3094 input->cursor = req->handle;
3096 if (req->flags & SET_CURSOR_COUNT)
3098 queue->cursor_count += req->show_count;
3099 input->cursor_count += req->show_count;
3101 if (req->flags & SET_CURSOR_POS)
3103 set_cursor_pos( input->desktop, req->x, req->y );
3105 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3107 struct desktop *desktop = input->desktop;
3109 /* only the desktop owner can set the message */
3110 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3111 desktop->cursor.clip_msg = req->clip_msg;
3113 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip, 0 );
3116 reply->new_x = input->desktop->cursor.x;
3117 reply->new_y = input->desktop->cursor.y;
3118 reply->new_clip = input->desktop->cursor.clip;
3119 reply->last_change = input->desktop->cursor.last_change;
3122 DECL_HANDLER(update_rawinput_devices)
3124 const struct rawinput_device *devices = get_req_data();
3125 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3126 const struct rawinput_device_entry *e;
3127 unsigned int i;
3129 for (i = 0; i < device_count; ++i)
3131 update_rawinput_device(&devices[i]);
3134 e = find_rawinput_device( 1, 2 );
3135 current->process->rawinput_mouse = e ? &e->device : NULL;
3136 e = find_rawinput_device( 1, 6 );
3137 current->process->rawinput_kbd = e ? &e->device : NULL;