include: New ddk/hidtypes.h include file.
[wine.git] / server / queue.c
bloba19af02cd622da8937674f9e1bf9b8513d321f42
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_open_file, /* open_file */
182 no_close_handle, /* close_handle */
183 msg_queue_destroy /* destroy */
186 static const struct fd_ops msg_queue_fd_ops =
188 NULL, /* get_poll_events */
189 msg_queue_poll_event, /* poll_event */
190 NULL, /* flush */
191 NULL, /* get_fd_type */
192 NULL, /* ioctl */
193 NULL, /* queue_async */
194 NULL, /* reselect_async */
195 NULL /* cancel async */
199 static const struct object_ops thread_input_ops =
201 sizeof(struct thread_input), /* size */
202 thread_input_dump, /* dump */
203 no_get_type, /* get_type */
204 no_add_queue, /* add_queue */
205 NULL, /* remove_queue */
206 NULL, /* signaled */
207 NULL, /* satisfied */
208 no_signal, /* signal */
209 no_get_fd, /* get_fd */
210 no_map_access, /* map_access */
211 default_get_sd, /* get_sd */
212 default_set_sd, /* set_sd */
213 no_lookup_name, /* lookup_name */
214 no_open_file, /* open_file */
215 no_close_handle, /* close_handle */
216 thread_input_destroy /* destroy */
219 /* pointer to input structure of foreground thread */
220 static unsigned int last_input_time;
222 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
223 static void free_message( struct message *msg );
225 /* set the caret window in a given thread input */
226 static void set_caret_window( struct thread_input *input, user_handle_t win )
228 if (!win || win != input->caret)
230 input->caret_rect.left = 0;
231 input->caret_rect.top = 0;
232 input->caret_rect.right = 0;
233 input->caret_rect.bottom = 0;
235 input->caret = win;
236 input->caret_hide = 1;
237 input->caret_state = 0;
240 /* create a thread input object */
241 static struct thread_input *create_thread_input( struct thread *thread )
243 struct thread_input *input;
245 if ((input = alloc_object( &thread_input_ops )))
247 input->focus = 0;
248 input->capture = 0;
249 input->active = 0;
250 input->menu_owner = 0;
251 input->move_size = 0;
252 input->cursor = 0;
253 input->cursor_count = 0;
254 list_init( &input->msg_list );
255 set_caret_window( input, 0 );
256 memset( input->keystate, 0, sizeof(input->keystate) );
258 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
260 release_object( input );
261 return NULL;
264 return input;
267 /* create a message queue object */
268 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
270 struct thread_input *new_input = NULL;
271 struct msg_queue *queue;
272 int i;
274 if (!input)
276 if (!(new_input = create_thread_input( thread ))) return NULL;
277 input = new_input;
280 if ((queue = alloc_object( &msg_queue_ops )))
282 queue->fd = NULL;
283 queue->wake_bits = 0;
284 queue->wake_mask = 0;
285 queue->changed_bits = 0;
286 queue->changed_mask = 0;
287 queue->paint_count = 0;
288 queue->hotkey_count = 0;
289 queue->quit_message = 0;
290 queue->cursor_count = 0;
291 queue->recv_result = NULL;
292 queue->next_timer_id = 0x7fff;
293 queue->timeout = NULL;
294 queue->input = (struct thread_input *)grab_object( input );
295 queue->hooks = NULL;
296 queue->last_get_msg = current_time;
297 list_init( &queue->send_result );
298 list_init( &queue->callback_result );
299 list_init( &queue->pending_timers );
300 list_init( &queue->expired_timers );
301 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
303 thread->queue = queue;
305 if (new_input) release_object( new_input );
306 return queue;
309 /* free the message queue of a thread at thread exit */
310 void free_msg_queue( struct thread *thread )
312 remove_thread_hooks( thread );
313 if (!thread->queue) return;
314 release_object( thread->queue );
315 thread->queue = NULL;
318 /* change the thread input data of a given thread */
319 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
321 struct msg_queue *queue = thread->queue;
323 if (!queue)
325 thread->queue = create_msg_queue( thread, new_input );
326 return thread->queue != NULL;
328 if (queue->input)
330 queue->input->cursor_count -= queue->cursor_count;
331 release_object( queue->input );
333 queue->input = (struct thread_input *)grab_object( new_input );
334 new_input->cursor_count += queue->cursor_count;
335 return 1;
338 /* set the cursor position and queue the corresponding mouse message */
339 static void set_cursor_pos( struct desktop *desktop, int x, int y )
341 struct hardware_msg_data *msg_data;
342 struct message *msg;
344 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
345 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
347 free( msg );
348 return;
350 memset( msg_data, 0, sizeof(*msg_data) );
352 msg->type = MSG_HARDWARE;
353 msg->win = 0;
354 msg->msg = WM_MOUSEMOVE;
355 msg->wparam = 0;
356 msg->lparam = 0;
357 msg->x = x;
358 msg->y = y;
359 msg->time = get_tick_count();
360 msg->result = NULL;
361 msg->data = msg_data;
362 msg->data_size = sizeof(*msg_data);
363 queue_hardware_message( desktop, msg, 1 );
366 /* retrieve default position and time for synthesized messages */
367 static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
369 struct desktop *desktop = queue->input->desktop;
371 *x = desktop->cursor.x;
372 *y = desktop->cursor.y;
373 *time = get_tick_count();
376 /* set the cursor clip rectangle */
377 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect )
379 rectangle_t top_rect;
380 int x, y;
382 get_top_window_rectangle( desktop, &top_rect );
383 if (rect)
385 rectangle_t new_rect = *rect;
386 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
387 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
388 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
389 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
390 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
391 desktop->cursor.clip = new_rect;
393 else desktop->cursor.clip = top_rect;
395 if (desktop->cursor.clip_msg)
396 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
398 /* warp the mouse to be inside the clip rect */
399 x = min( max( desktop->cursor.x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
400 y = min( max( desktop->cursor.y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
401 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
404 /* change the foreground input and reset the cursor clip rect */
405 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
407 if (desktop->foreground_input == input) return;
408 set_clip_rectangle( desktop, NULL );
409 desktop->foreground_input = input;
412 /* get the hook table for a given thread */
413 struct hook_table *get_queue_hooks( struct thread *thread )
415 if (!thread->queue) return NULL;
416 return thread->queue->hooks;
419 /* set the hook table for a given thread, allocating the queue if needed */
420 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
422 struct msg_queue *queue = thread->queue;
423 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
424 if (queue->hooks) release_object( queue->hooks );
425 queue->hooks = hooks;
428 /* check the queue status */
429 static inline int is_signaled( struct msg_queue *queue )
431 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
434 /* set some queue bits */
435 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
437 queue->wake_bits |= bits;
438 queue->changed_bits |= bits;
439 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
442 /* clear some queue bits */
443 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
445 queue->wake_bits &= ~bits;
446 queue->changed_bits &= ~bits;
449 /* check whether msg is a keyboard message */
450 static inline int is_keyboard_msg( struct message *msg )
452 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
455 /* check if message is matched by the filter */
456 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
458 return (msg >= first && msg <= last);
461 /* check whether a message filter contains at least one potential hardware message */
462 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
464 /* hardware message ranges are (in numerical order):
465 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
466 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
467 * WM_MOUSEFIRST .. WM_MOUSELAST
469 if (last < WM_NCMOUSEFIRST) return 0;
470 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
471 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
472 if (first > WM_MOUSELAST) return 0;
473 return 1;
476 /* get the QS_* bit corresponding to a given hardware message */
477 static inline int get_hardware_msg_bit( struct message *msg )
479 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
480 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
481 if (is_keyboard_msg( msg )) return QS_KEY;
482 return QS_MOUSEBUTTON;
485 /* get the current thread queue, creating it if needed */
486 static inline struct msg_queue *get_current_queue(void)
488 struct msg_queue *queue = current->queue;
489 if (!queue) queue = create_msg_queue( current, NULL );
490 return queue;
493 /* get a (pseudo-)unique id to tag hardware messages */
494 static inline unsigned int get_unique_id(void)
496 static unsigned int id;
497 if (!++id) id = 1; /* avoid an id of 0 */
498 return id;
501 /* try to merge a message with the last in the list; return 1 if successful */
502 static int merge_message( struct thread_input *input, const struct message *msg )
504 struct message *prev;
505 struct list *ptr;
507 if (msg->msg != WM_MOUSEMOVE) return 0;
508 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
510 prev = LIST_ENTRY( ptr, struct message, entry );
511 if (prev->msg != WM_INPUT) break;
513 if (!ptr) return 0;
514 if (prev->result) return 0;
515 if (prev->win && msg->win && prev->win != msg->win) return 0;
516 if (prev->msg != msg->msg) return 0;
517 if (prev->type != msg->type) return 0;
518 /* now we can merge it */
519 prev->wparam = msg->wparam;
520 prev->lparam = msg->lparam;
521 prev->x = msg->x;
522 prev->y = msg->y;
523 prev->time = msg->time;
524 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
526 struct hardware_msg_data *prev_data = prev->data;
527 struct hardware_msg_data *msg_data = msg->data;
528 prev_data->info = msg_data->info;
530 list_remove( ptr );
531 list_add_tail( &input->msg_list, ptr );
532 return 1;
535 /* free a result structure */
536 static void free_result( struct message_result *result )
538 if (result->timeout) remove_timeout_user( result->timeout );
539 free( result->data );
540 if (result->callback_msg) free_message( result->callback_msg );
541 if (result->hardware_msg) free_message( result->hardware_msg );
542 if (result->desktop) release_object( result->desktop );
543 free( result );
546 /* remove the result from the sender list it is on */
547 static inline void remove_result_from_sender( struct message_result *result )
549 assert( result->sender );
551 list_remove( &result->sender_entry );
552 result->sender = NULL;
553 if (!result->receiver) free_result( result );
556 /* store the message result in the appropriate structure */
557 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
559 res->result = result;
560 res->error = error;
561 res->replied = 1;
562 if (res->timeout)
564 remove_timeout_user( res->timeout );
565 res->timeout = NULL;
568 if (res->hardware_msg)
570 if (!error && result) /* rejected by the hook */
571 free_message( res->hardware_msg );
572 else
573 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
575 res->hardware_msg = NULL;
578 if (res->sender)
580 if (res->callback_msg)
582 /* queue the callback message in the sender queue */
583 struct callback_msg_data *data = res->callback_msg->data;
584 data->result = result;
585 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
586 set_queue_bits( res->sender, QS_SENDMESSAGE );
587 res->callback_msg = NULL;
588 remove_result_from_sender( res );
590 else
592 /* wake sender queue if waiting on this result */
593 if (list_head(&res->sender->send_result) == &res->sender_entry)
594 set_queue_bits( res->sender, QS_SMRESULT );
597 else if (!res->receiver) free_result( res );
600 /* free a message when deleting a queue or window */
601 static void free_message( struct message *msg )
603 struct message_result *result = msg->result;
604 if (result)
606 result->msg = NULL;
607 result->receiver = NULL;
608 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
610 free( msg->data );
611 free( msg );
614 /* remove (and free) a message from a message list */
615 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
616 enum message_kind kind )
618 list_remove( &msg->entry );
619 switch(kind)
621 case SEND_MESSAGE:
622 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
623 break;
624 case POST_MESSAGE:
625 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
626 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
627 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
628 clear_queue_bits( queue, QS_HOTKEY );
629 break;
631 free_message( msg );
634 /* message timed out without getting a reply */
635 static void result_timeout( void *private )
637 struct message_result *result = private;
639 assert( !result->replied );
641 result->timeout = NULL;
643 if (result->msg) /* not received yet */
645 struct message *msg = result->msg;
647 result->msg = NULL;
648 msg->result = NULL;
649 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
650 result->receiver = NULL;
652 store_message_result( result, 0, STATUS_TIMEOUT );
655 /* allocate and fill a message result structure */
656 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
657 struct msg_queue *recv_queue,
658 struct message *msg, timeout_t timeout )
660 struct message_result *result = mem_alloc( sizeof(*result) );
661 if (result)
663 result->msg = msg;
664 result->sender = send_queue;
665 result->receiver = recv_queue;
666 result->replied = 0;
667 result->data = NULL;
668 result->data_size = 0;
669 result->timeout = NULL;
670 result->hardware_msg = NULL;
671 result->desktop = NULL;
672 result->callback_msg = NULL;
674 if (msg->type == MSG_CALLBACK)
676 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
678 if (!callback_msg)
680 free( result );
681 return NULL;
683 callback_msg->type = MSG_CALLBACK_RESULT;
684 callback_msg->win = msg->win;
685 callback_msg->msg = msg->msg;
686 callback_msg->wparam = 0;
687 callback_msg->lparam = 0;
688 callback_msg->time = get_tick_count();
689 callback_msg->result = NULL;
690 /* steal the data from the original message */
691 callback_msg->data = msg->data;
692 callback_msg->data_size = msg->data_size;
693 msg->data = NULL;
694 msg->data_size = 0;
696 result->callback_msg = callback_msg;
697 list_add_head( &send_queue->callback_result, &result->sender_entry );
699 else if (send_queue)
701 list_add_head( &send_queue->send_result, &result->sender_entry );
702 clear_queue_bits( send_queue, QS_SMRESULT );
705 if (timeout != TIMEOUT_INFINITE)
706 result->timeout = add_timeout_user( timeout, result_timeout, result );
708 return result;
711 /* receive a message, removing it from the sent queue */
712 static void receive_message( struct msg_queue *queue, struct message *msg,
713 struct get_message_reply *reply )
715 struct message_result *result = msg->result;
717 reply->total = msg->data_size;
718 if (msg->data_size > get_reply_max_size())
720 set_error( STATUS_BUFFER_OVERFLOW );
721 return;
723 reply->type = msg->type;
724 reply->win = msg->win;
725 reply->msg = msg->msg;
726 reply->wparam = msg->wparam;
727 reply->lparam = msg->lparam;
728 reply->x = msg->x;
729 reply->y = msg->y;
730 reply->time = msg->time;
732 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
734 list_remove( &msg->entry );
735 /* put the result on the receiver result stack */
736 if (result)
738 result->msg = NULL;
739 result->recv_next = queue->recv_result;
740 queue->recv_result = result;
742 free( msg );
743 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
746 /* set the result of the current received message */
747 static void reply_message( struct msg_queue *queue, lparam_t result,
748 unsigned int error, int remove, const void *data, data_size_t len )
750 struct message_result *res = queue->recv_result;
752 if (remove)
754 queue->recv_result = res->recv_next;
755 res->receiver = NULL;
756 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
758 free_result( res );
759 return;
762 if (!res->replied)
764 if (len && (res->data = memdup( data, len ))) res->data_size = len;
765 store_message_result( res, result, error );
769 static int match_window( user_handle_t win, user_handle_t msg_win )
771 if (!win) return 1;
772 if (win == -1 || win == 1) return !msg_win;
773 if (msg_win == win) return 1;
774 return is_child_window( win, msg_win );
777 /* retrieve a posted message */
778 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
779 unsigned int first, unsigned int last, unsigned int flags,
780 struct get_message_reply *reply )
782 struct message *msg;
784 /* check against the filters */
785 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
787 if (!match_window( win, msg->win )) continue;
788 if (!check_msg_filter( msg->msg, first, last )) continue;
789 goto found; /* found one */
791 return 0;
793 /* return it to the app */
794 found:
795 reply->total = msg->data_size;
796 if (msg->data_size > get_reply_max_size())
798 set_error( STATUS_BUFFER_OVERFLOW );
799 return 1;
801 reply->type = msg->type;
802 reply->win = msg->win;
803 reply->msg = msg->msg;
804 reply->wparam = msg->wparam;
805 reply->lparam = msg->lparam;
806 reply->x = msg->x;
807 reply->y = msg->y;
808 reply->time = msg->time;
810 if (flags & PM_REMOVE)
812 if (msg->data)
814 set_reply_data_ptr( msg->data, msg->data_size );
815 msg->data = NULL;
816 msg->data_size = 0;
818 remove_queue_message( queue, msg, POST_MESSAGE );
820 else if (msg->data) set_reply_data( msg->data, msg->data_size );
822 return 1;
825 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
826 struct get_message_reply *reply )
828 if (queue->quit_message)
830 reply->total = 0;
831 reply->type = MSG_POSTED;
832 reply->win = 0;
833 reply->msg = WM_QUIT;
834 reply->wparam = queue->exit_code;
835 reply->lparam = 0;
837 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
839 if (flags & PM_REMOVE)
841 queue->quit_message = 0;
842 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
843 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
845 return 1;
847 else
848 return 0;
851 /* empty a message list and free all the messages */
852 static void empty_msg_list( struct list *list )
854 struct list *ptr;
856 while ((ptr = list_head( list )) != NULL)
858 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
859 list_remove( &msg->entry );
860 free_message( msg );
864 /* cleanup all pending results when deleting a queue */
865 static void cleanup_results( struct msg_queue *queue )
867 struct list *entry;
869 while ((entry = list_head( &queue->send_result )) != NULL)
871 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
874 while ((entry = list_head( &queue->callback_result )) != NULL)
876 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
879 while (queue->recv_result)
880 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
883 /* check if the thread owning the queue is hung (not checking for messages) */
884 static int is_queue_hung( struct msg_queue *queue )
886 struct wait_queue_entry *entry;
888 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
889 return 0; /* less than 5 seconds since last get message -> not hung */
891 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
893 if (get_wait_queue_thread(entry)->queue == queue)
894 return 0; /* thread is waiting on queue -> not hung */
896 return 1;
899 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
901 struct msg_queue *queue = (struct msg_queue *)obj;
902 struct process *process = get_wait_queue_thread(entry)->process;
904 /* a thread can only wait on its own queue */
905 if (get_wait_queue_thread(entry)->queue != queue)
907 set_error( STATUS_ACCESS_DENIED );
908 return 0;
910 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
912 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
913 set_fd_events( queue->fd, POLLIN );
914 add_queue( obj, entry );
915 return 1;
918 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
920 struct msg_queue *queue = (struct msg_queue *)obj;
922 remove_queue( obj, entry );
923 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
924 set_fd_events( queue->fd, 0 );
927 static void msg_queue_dump( struct object *obj, int verbose )
929 struct msg_queue *queue = (struct msg_queue *)obj;
930 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
931 queue->wake_bits, queue->wake_mask );
934 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
936 struct msg_queue *queue = (struct msg_queue *)obj;
937 int ret = 0;
939 if (queue->fd)
941 if ((ret = check_fd_events( queue->fd, POLLIN )))
942 /* stop waiting on select() if we are signaled */
943 set_fd_events( queue->fd, 0 );
944 else if (!list_empty( &obj->wait_queue ))
945 /* restart waiting on poll() if we are no longer signaled */
946 set_fd_events( queue->fd, POLLIN );
948 return ret || is_signaled( queue );
951 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
953 struct msg_queue *queue = (struct msg_queue *)obj;
954 queue->wake_mask = 0;
955 queue->changed_mask = 0;
958 static void msg_queue_destroy( struct object *obj )
960 struct msg_queue *queue = (struct msg_queue *)obj;
961 struct list *ptr;
962 struct hotkey *hotkey, *hotkey2;
963 int i;
965 cleanup_results( queue );
966 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
968 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
970 if (hotkey->queue == queue)
972 list_remove( &hotkey->entry );
973 free( hotkey );
977 while ((ptr = list_head( &queue->pending_timers )))
979 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
980 list_remove( &timer->entry );
981 free( timer );
983 while ((ptr = list_head( &queue->expired_timers )))
985 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
986 list_remove( &timer->entry );
987 free( timer );
989 if (queue->timeout) remove_timeout_user( queue->timeout );
990 queue->input->cursor_count -= queue->cursor_count;
991 release_object( queue->input );
992 if (queue->hooks) release_object( queue->hooks );
993 if (queue->fd) release_object( queue->fd );
996 static void msg_queue_poll_event( struct fd *fd, int event )
998 struct msg_queue *queue = get_fd_user( fd );
999 assert( queue->obj.ops == &msg_queue_ops );
1001 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1002 else set_fd_events( queue->fd, 0 );
1003 wake_up( &queue->obj, 0 );
1006 static void thread_input_dump( struct object *obj, int verbose )
1008 struct thread_input *input = (struct thread_input *)obj;
1009 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
1010 input->focus, input->capture, input->active );
1013 static void thread_input_destroy( struct object *obj )
1015 struct thread_input *input = (struct thread_input *)obj;
1017 empty_msg_list( &input->msg_list );
1018 if (input->desktop)
1020 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
1021 release_object( input->desktop );
1025 /* fix the thread input data when a window is destroyed */
1026 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1028 struct thread_input *input = queue->input;
1030 if (window == input->focus) input->focus = 0;
1031 if (window == input->capture) input->capture = 0;
1032 if (window == input->active) input->active = 0;
1033 if (window == input->menu_owner) input->menu_owner = 0;
1034 if (window == input->move_size) input->move_size = 0;
1035 if (window == input->caret) set_caret_window( input, 0 );
1038 /* check if the specified window can be set in the input data of a given queue */
1039 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1041 struct thread *thread;
1042 int ret = 0;
1044 if (!window) return 1; /* we can always clear the data */
1046 if ((thread = get_window_thread( window )))
1048 ret = (queue->input == thread->queue->input);
1049 if (!ret) set_error( STATUS_ACCESS_DENIED );
1050 release_object( thread );
1052 else set_error( STATUS_INVALID_HANDLE );
1054 return ret;
1057 /* make sure the specified thread has a queue */
1058 int init_thread_queue( struct thread *thread )
1060 if (thread->queue) return 1;
1061 return (create_msg_queue( thread, NULL ) != NULL);
1064 /* attach two thread input data structures */
1065 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1067 struct desktop *desktop;
1068 struct thread_input *input;
1069 int ret;
1071 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1072 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1073 input = (struct thread_input *)grab_object( thread_to->queue->input );
1074 if (input->desktop != desktop)
1076 set_error( STATUS_ACCESS_DENIED );
1077 release_object( input );
1078 release_object( desktop );
1079 return 0;
1081 release_object( desktop );
1083 if (thread_from->queue)
1085 if (!input->focus) input->focus = thread_from->queue->input->focus;
1086 if (!input->active) input->active = thread_from->queue->input->active;
1089 ret = assign_thread_input( thread_from, input );
1090 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1091 release_object( input );
1092 return ret;
1095 /* detach two thread input data structures */
1096 void detach_thread_input( struct thread *thread_from )
1098 struct thread *thread;
1099 struct thread_input *input, *old_input = thread_from->queue->input;
1101 if ((input = create_thread_input( thread_from )))
1103 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1105 if (thread == thread_from)
1107 input->focus = old_input->focus;
1108 old_input->focus = 0;
1110 release_object( thread );
1112 if (old_input->active && (thread = get_window_thread( old_input->active )))
1114 if (thread == thread_from)
1116 input->active = old_input->active;
1117 old_input->active = 0;
1119 release_object( thread );
1121 assign_thread_input( thread_from, input );
1122 release_object( input );
1127 /* set the next timer to expire */
1128 static void set_next_timer( struct msg_queue *queue )
1130 struct list *ptr;
1132 if (queue->timeout)
1134 remove_timeout_user( queue->timeout );
1135 queue->timeout = NULL;
1137 if ((ptr = list_head( &queue->pending_timers )))
1139 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1140 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1142 /* set/clear QS_TIMER bit */
1143 if (list_empty( &queue->expired_timers ))
1144 clear_queue_bits( queue, QS_TIMER );
1145 else
1146 set_queue_bits( queue, QS_TIMER );
1149 /* find a timer from its window and id */
1150 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1151 unsigned int msg, lparam_t id )
1153 struct list *ptr;
1155 /* we need to search both lists */
1157 LIST_FOR_EACH( ptr, &queue->pending_timers )
1159 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1160 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1162 LIST_FOR_EACH( ptr, &queue->expired_timers )
1164 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1165 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1167 return NULL;
1170 /* callback for the next timer expiration */
1171 static void timer_callback( void *private )
1173 struct msg_queue *queue = private;
1174 struct list *ptr;
1176 queue->timeout = NULL;
1177 /* move on to the next timer */
1178 ptr = list_head( &queue->pending_timers );
1179 list_remove( ptr );
1180 list_add_tail( &queue->expired_timers, ptr );
1181 set_next_timer( queue );
1184 /* link a timer at its rightful place in the queue list */
1185 static void link_timer( struct msg_queue *queue, struct timer *timer )
1187 struct list *ptr;
1189 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1191 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1192 if (t->when >= timer->when) break;
1194 list_add_before( ptr, &timer->entry );
1197 /* remove a timer from the queue timer list and free it */
1198 static void free_timer( struct msg_queue *queue, struct timer *timer )
1200 list_remove( &timer->entry );
1201 free( timer );
1202 set_next_timer( queue );
1205 /* restart an expired timer */
1206 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1208 list_remove( &timer->entry );
1209 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1210 link_timer( queue, timer );
1211 set_next_timer( queue );
1214 /* find an expired timer matching the filtering parameters */
1215 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1216 unsigned int get_first, unsigned int get_last,
1217 int remove )
1219 struct list *ptr;
1221 LIST_FOR_EACH( ptr, &queue->expired_timers )
1223 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1224 if (win && timer->win != win) continue;
1225 if (check_msg_filter( timer->msg, get_first, get_last ))
1227 if (remove) restart_timer( queue, timer );
1228 return timer;
1231 return NULL;
1234 /* add a timer */
1235 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1237 struct timer *timer = mem_alloc( sizeof(*timer) );
1238 if (timer)
1240 timer->rate = max( rate, 1 );
1241 timer->when = current_time + (timeout_t)timer->rate * 10000;
1242 link_timer( queue, timer );
1243 /* check if we replaced the next timer */
1244 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1246 return timer;
1249 /* change the input key state for a given key */
1250 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1252 if (down)
1254 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1255 keystate[key] |= down;
1257 else keystate[key] &= ~0x80;
1260 /* update the input key state for a keyboard message */
1261 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1262 const struct message *msg )
1264 unsigned char key;
1265 int down = 0;
1267 switch (msg->msg)
1269 case WM_LBUTTONDOWN:
1270 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1271 /* fall through */
1272 case WM_LBUTTONUP:
1273 set_input_key_state( keystate, VK_LBUTTON, down );
1274 break;
1275 case WM_MBUTTONDOWN:
1276 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1277 /* fall through */
1278 case WM_MBUTTONUP:
1279 set_input_key_state( keystate, VK_MBUTTON, down );
1280 break;
1281 case WM_RBUTTONDOWN:
1282 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1283 /* fall through */
1284 case WM_RBUTTONUP:
1285 set_input_key_state( keystate, VK_RBUTTON, down );
1286 break;
1287 case WM_XBUTTONDOWN:
1288 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1289 /* fall through */
1290 case WM_XBUTTONUP:
1291 if (msg->wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1292 else if (msg->wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1293 break;
1294 case WM_KEYDOWN:
1295 case WM_SYSKEYDOWN:
1296 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1297 /* fall through */
1298 case WM_KEYUP:
1299 case WM_SYSKEYUP:
1300 key = (unsigned char)msg->wparam;
1301 set_input_key_state( keystate, key, down );
1302 switch(key)
1304 case VK_LCONTROL:
1305 case VK_RCONTROL:
1306 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1307 set_input_key_state( keystate, VK_CONTROL, down );
1308 break;
1309 case VK_LMENU:
1310 case VK_RMENU:
1311 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1312 set_input_key_state( keystate, VK_MENU, down );
1313 break;
1314 case VK_LSHIFT:
1315 case VK_RSHIFT:
1316 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1317 set_input_key_state( keystate, VK_SHIFT, down );
1318 break;
1320 break;
1324 /* release the hardware message currently being processed by the given thread */
1325 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1326 int remove )
1328 struct thread_input *input = queue->input;
1329 struct message *msg;
1331 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1333 if (msg->unique_id == hw_id) break;
1335 if (&msg->entry == &input->msg_list) return; /* not found */
1337 /* clear the queue bit for that message */
1338 if (remove)
1340 struct message *other;
1341 int clr_bit;
1343 clr_bit = get_hardware_msg_bit( msg );
1344 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1346 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1348 clr_bit = 0;
1349 break;
1352 if (clr_bit) clear_queue_bits( queue, clr_bit );
1354 update_input_key_state( input->desktop, input->keystate, msg );
1355 list_remove( &msg->entry );
1356 free_message( msg );
1360 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1362 struct hotkey *hotkey;
1363 unsigned int modifiers = 0;
1365 if (msg->msg != WM_KEYDOWN) return 0;
1367 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1368 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1369 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1370 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1372 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1374 if (hotkey->vkey != msg->wparam) continue;
1375 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1378 return 0;
1380 found:
1381 msg->type = MSG_POSTED;
1382 msg->win = hotkey->win;
1383 msg->msg = WM_HOTKEY;
1384 msg->wparam = hotkey->id;
1385 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1387 free( msg->data );
1388 msg->data = NULL;
1389 msg->data_size = 0;
1391 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1392 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1393 hotkey->queue->hotkey_count++;
1394 return 1;
1397 /* find the window that should receive a given hardware message */
1398 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1399 struct message *msg, unsigned int *msg_code,
1400 struct thread **thread )
1402 user_handle_t win = 0;
1404 *thread = NULL;
1405 *msg_code = msg->msg;
1406 if (msg->msg == WM_INPUT)
1408 if (!(win = msg->win) && input) win = input->focus;
1410 else if (is_keyboard_msg( msg ))
1412 if (input && !(win = input->focus))
1414 win = input->active;
1415 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1418 else if (!input || !(win = input->capture)) /* mouse message */
1420 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1421 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1423 *thread = window_thread_from_point( win, msg->x, msg->y );
1426 if (!*thread)
1427 *thread = get_window_thread( win );
1428 return win;
1431 static struct rawinput_device_entry *find_rawinput_device( unsigned short usage_page, unsigned short usage )
1433 struct rawinput_device_entry *e;
1435 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
1437 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1438 return e;
1441 return NULL;
1444 static void update_rawinput_device(const struct rawinput_device *device)
1446 struct rawinput_device_entry *e;
1448 if (!(e = find_rawinput_device( device->usage_page, device->usage )))
1450 if (!(e = mem_alloc( sizeof(*e) ))) return;
1451 list_add_tail( &current->process->rawinput_devices, &e->entry );
1454 if (device->flags & RIDEV_REMOVE)
1456 list_remove( &e->entry );
1457 free( e );
1458 return;
1461 e->device = *device;
1462 e->device.target = get_user_full_handle( e->device.target );
1465 /* queue a hardware message into a given thread input */
1466 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1468 user_handle_t win;
1469 struct thread *thread;
1470 struct thread_input *input;
1471 unsigned int msg_code;
1473 update_input_key_state( desktop, desktop->keystate, msg );
1474 last_input_time = get_tick_count();
1475 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1477 if (is_keyboard_msg( msg ))
1479 if (queue_hotkey_message( desktop, msg )) return;
1480 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1481 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1482 msg->lparam &= ~(KF_EXTENDED << 16);
1484 else if (msg->msg != WM_INPUT)
1486 if (msg->msg == WM_MOUSEMOVE)
1488 int x = min( max( msg->x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
1489 int y = min( max( msg->y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
1490 if (desktop->cursor.x != x || desktop->cursor.y != y) always_queue = 1;
1491 desktop->cursor.x = x;
1492 desktop->cursor.y = y;
1493 desktop->cursor.last_change = get_tick_count();
1495 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1496 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1497 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1498 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1499 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1500 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1501 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1503 msg->x = desktop->cursor.x;
1504 msg->y = desktop->cursor.y;
1506 if (msg->win && (thread = get_window_thread( msg->win )))
1508 input = thread->queue->input;
1509 release_object( thread );
1511 else input = desktop->foreground_input;
1513 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1514 if (!win || !thread)
1516 if (input) update_input_key_state( input->desktop, input->keystate, msg );
1517 free_message( msg );
1518 return;
1520 input = thread->queue->input;
1522 if (win != desktop->cursor.win) always_queue = 1;
1523 desktop->cursor.win = win;
1525 if (!always_queue || merge_message( input, msg )) free_message( msg );
1526 else
1528 msg->unique_id = 0; /* will be set once we return it to the app */
1529 list_add_tail( &input->msg_list, &msg->entry );
1530 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1532 release_object( thread );
1535 /* send the low-level hook message for a given hardware message */
1536 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1537 const hw_input_t *input, struct msg_queue *sender )
1539 struct thread *hook_thread;
1540 struct msg_queue *queue;
1541 struct message *msg;
1542 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1543 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1545 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1546 if (!(queue = hook_thread->queue)) return 0;
1547 if (is_queue_hung( queue )) return 0;
1549 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1551 msg->type = MSG_HOOK_LL;
1552 msg->win = 0;
1553 msg->msg = id;
1554 msg->wparam = hardware_msg->msg;
1555 msg->x = hardware_msg->x;
1556 msg->y = hardware_msg->y;
1557 msg->time = hardware_msg->time;
1558 msg->data_size = hardware_msg->data_size;
1559 msg->result = NULL;
1561 if (input->type == INPUT_KEYBOARD)
1563 unsigned short vkey = input->kbd.vkey;
1564 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1565 msg->lparam = (input->kbd.scan << 16) | vkey;
1567 else msg->lparam = input->mouse.data << 16;
1569 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1570 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1572 free_message( msg );
1573 return 0;
1575 msg->result->hardware_msg = hardware_msg;
1576 msg->result->desktop = (struct desktop *)grab_object( desktop );
1577 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1578 set_queue_bits( queue, QS_SENDMESSAGE );
1579 return 1;
1582 /* queue a hardware message for a mouse event */
1583 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1584 unsigned int hook_flags, struct msg_queue *sender )
1586 const struct rawinput_device *device;
1587 struct hardware_msg_data *msg_data;
1588 struct message *msg;
1589 unsigned int i, time, flags;
1590 int wait = 0, x, y;
1592 static const unsigned int messages[] =
1594 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1595 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1596 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1597 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1598 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1599 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1600 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1601 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1602 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1603 0, /* 0x0200 = unused */
1604 0, /* 0x0400 = unused */
1605 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1606 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1609 desktop->cursor.last_change = get_tick_count();
1610 flags = input->mouse.flags;
1611 time = input->mouse.time;
1612 if (!time) time = desktop->cursor.last_change;
1614 if (flags & MOUSEEVENTF_MOVE)
1616 if (flags & MOUSEEVENTF_ABSOLUTE)
1618 x = input->mouse.x;
1619 y = input->mouse.y;
1620 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1621 x == desktop->cursor.x && y == desktop->cursor.y)
1622 flags &= ~MOUSEEVENTF_MOVE;
1624 else
1626 x = desktop->cursor.x + input->mouse.x;
1627 y = desktop->cursor.y + input->mouse.y;
1630 else
1632 x = desktop->cursor.x;
1633 y = desktop->cursor.y;
1636 if ((device = current->process->rawinput_mouse))
1638 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1639 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1641 free( msg );
1642 return 0;
1645 msg->type = MSG_HARDWARE;
1646 msg->win = device->target;
1647 msg->msg = WM_INPUT;
1648 msg->wparam = RIM_INPUT;
1649 msg->lparam = 0;
1650 msg->time = time;
1651 msg->data = msg_data;
1652 msg->data_size = sizeof(*msg_data);
1653 msg->result = NULL;
1655 msg_data->info = input->mouse.info;
1656 msg_data->flags = flags;
1657 msg_data->rawinput.type = RIM_TYPEMOUSE;
1658 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1659 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1660 msg_data->rawinput.mouse.data = input->mouse.data;
1662 queue_hardware_message( desktop, msg, 0 );
1665 for (i = 0; i < sizeof(messages)/sizeof(messages[0]); i++)
1667 if (!messages[i]) continue;
1668 if (!(flags & (1 << i))) continue;
1669 flags &= ~(1 << i);
1671 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1672 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1674 free( msg );
1675 return 0;
1677 memset( msg_data, 0, sizeof(*msg_data) );
1679 msg->type = MSG_HARDWARE;
1680 msg->win = get_user_full_handle( win );
1681 msg->msg = messages[i];
1682 msg->wparam = input->mouse.data << 16;
1683 msg->lparam = 0;
1684 msg->x = x;
1685 msg->y = y;
1686 msg->time = time;
1687 msg->result = NULL;
1688 msg->data = msg_data;
1689 msg->data_size = sizeof(*msg_data);
1690 msg_data->info = input->mouse.info;
1691 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLMHF_INJECTED;
1693 /* specify a sender only when sending the last message */
1694 if (!(flags & ((1 << sizeof(messages)/sizeof(messages[0])) - 1)))
1696 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1697 queue_hardware_message( desktop, msg, 0 );
1699 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1700 queue_hardware_message( desktop, msg, 0 );
1702 return wait;
1705 /* queue a hardware message for a keyboard event */
1706 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1707 unsigned int hook_flags, struct msg_queue *sender )
1709 const struct rawinput_device *device;
1710 struct hardware_msg_data *msg_data;
1711 struct message *msg;
1712 unsigned char vkey = input->kbd.vkey;
1713 unsigned int message_code, time;
1714 int wait;
1716 if (!(time = input->kbd.time)) time = get_tick_count();
1718 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1720 switch (vkey)
1722 case VK_MENU:
1723 case VK_LMENU:
1724 case VK_RMENU:
1725 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1726 break;
1727 case VK_CONTROL:
1728 case VK_LCONTROL:
1729 case VK_RCONTROL:
1730 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1731 break;
1732 case VK_SHIFT:
1733 case VK_LSHIFT:
1734 case VK_RSHIFT:
1735 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1736 break;
1740 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1741 switch (vkey)
1743 case VK_LMENU:
1744 case VK_RMENU:
1745 if (input->kbd.flags & KEYEVENTF_KEYUP)
1747 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1748 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1749 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1750 message_code = WM_SYSKEYUP;
1751 desktop->keystate[VK_MENU] &= ~0x02;
1753 else
1755 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1756 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1757 message_code = WM_SYSKEYDOWN;
1758 desktop->keystate[VK_MENU] |= 0x02;
1760 break;
1762 case VK_LCONTROL:
1763 case VK_RCONTROL:
1764 /* send WM_SYSKEYUP on release if Alt still pressed */
1765 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1766 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1767 message_code = WM_SYSKEYUP;
1768 desktop->keystate[VK_MENU] &= ~0x02;
1769 break;
1771 default:
1772 /* send WM_SYSKEY for Alt-anykey and for F10 */
1773 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1774 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1775 /* fall through */
1776 case VK_F10:
1777 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1778 desktop->keystate[VK_MENU] &= ~0x02;
1779 break;
1782 if ((device = current->process->rawinput_kbd))
1784 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1785 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1787 free( msg );
1788 return 0;
1791 msg->type = MSG_HARDWARE;
1792 msg->win = device->target;
1793 msg->msg = WM_INPUT;
1794 msg->wparam = RIM_INPUT;
1795 msg->lparam = 0;
1796 msg->time = time;
1797 msg->data = msg_data;
1798 msg->data_size = sizeof(*msg_data);
1799 msg->result = NULL;
1801 msg_data->info = input->kbd.info;
1802 msg_data->flags = input->kbd.flags;
1803 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1804 msg_data->rawinput.kbd.message = message_code;
1805 msg_data->rawinput.kbd.vkey = vkey;
1806 msg_data->rawinput.kbd.scan = input->kbd.scan;
1808 queue_hardware_message( desktop, msg, 0 );
1811 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1812 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1814 free( msg );
1815 return 0;
1817 memset( msg_data, 0, sizeof(*msg_data) );
1819 msg->type = MSG_HARDWARE;
1820 msg->win = get_user_full_handle( win );
1821 msg->msg = message_code;
1822 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1823 msg->time = time;
1824 msg->result = NULL;
1825 msg->data = msg_data;
1826 msg->data_size = sizeof(*msg_data);
1827 msg_data->info = input->kbd.info;
1828 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLKHF_INJECTED;
1830 if (input->kbd.flags & KEYEVENTF_UNICODE)
1832 msg->wparam = VK_PACKET;
1834 else
1836 unsigned int flags = 0;
1837 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1838 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1839 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1840 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1842 msg->wparam = vkey;
1843 msg->lparam |= flags << 16;
1844 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1847 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1848 queue_hardware_message( desktop, msg, 1 );
1850 return wait;
1853 /* queue a hardware message for a custom type of event */
1854 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1855 const hw_input_t *input )
1857 struct hardware_msg_data *msg_data;
1858 struct message *msg;
1860 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
1861 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1863 free( msg );
1864 return;
1866 memset( msg_data, 0, sizeof(*msg_data) );
1868 msg->type = MSG_HARDWARE;
1869 msg->win = get_user_full_handle( win );
1870 msg->msg = input->hw.msg;
1871 msg->wparam = 0;
1872 msg->lparam = input->hw.lparam;
1873 msg->x = desktop->cursor.x;
1874 msg->y = desktop->cursor.y;
1875 msg->time = get_tick_count();
1876 msg->result = NULL;
1877 msg->data = msg_data;
1878 msg->data_size = sizeof(*msg_data);
1880 queue_hardware_message( desktop, msg, 1 );
1883 /* check message filter for a hardware message */
1884 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1885 user_handle_t filter_win, unsigned int first, unsigned int last )
1887 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1889 /* we can only test the window for a keyboard message since the
1890 * dest window for a mouse message depends on hittest */
1891 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1892 return 0;
1893 /* the message code is final for a keyboard message, we can simply check it */
1894 return check_msg_filter( msg_code, first, last );
1896 else /* mouse message */
1898 /* we need to check all possible values that the message can have in the end */
1900 if (check_msg_filter( msg_code, first, last )) return 1;
1901 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1903 /* all other messages can become non-client messages */
1904 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1906 /* clicks can become double-clicks or non-client double-clicks */
1907 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1908 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1910 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1911 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1913 return 0;
1918 /* find a hardware message for the given queue */
1919 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1920 unsigned int first, unsigned int last, unsigned int flags,
1921 struct get_message_reply *reply )
1923 struct thread_input *input = thread->queue->input;
1924 struct thread *win_thread;
1925 struct list *ptr;
1926 user_handle_t win;
1927 int clear_bits, got_one = 0;
1928 unsigned int msg_code;
1930 ptr = list_head( &input->msg_list );
1931 if (hw_id)
1933 while (ptr)
1935 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1936 if (msg->unique_id == hw_id) break;
1937 ptr = list_next( &input->msg_list, ptr );
1939 if (!ptr) ptr = list_head( &input->msg_list );
1940 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1943 if (ptr == list_head( &input->msg_list ))
1944 clear_bits = QS_INPUT;
1945 else
1946 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1948 while (ptr)
1950 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1951 struct hardware_msg_data *data = msg->data;
1953 ptr = list_next( &input->msg_list, ptr );
1954 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
1955 if (!win || !win_thread)
1957 /* no window at all, remove it */
1958 update_input_key_state( input->desktop, input->keystate, msg );
1959 list_remove( &msg->entry );
1960 free_message( msg );
1961 continue;
1963 if (win_thread != thread)
1965 if (win_thread->queue->input == input)
1967 /* wake the other thread */
1968 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1969 got_one = 1;
1971 else
1973 /* for another thread input, drop it */
1974 update_input_key_state( input->desktop, input->keystate, msg );
1975 list_remove( &msg->entry );
1976 free_message( msg );
1978 release_object( win_thread );
1979 continue;
1981 release_object( win_thread );
1983 /* if we already got a message for another thread, or if it doesn't
1984 * match the filter we skip it */
1985 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1987 clear_bits &= ~get_hardware_msg_bit( msg );
1988 continue;
1990 /* now we can return it */
1991 if (!msg->unique_id) msg->unique_id = get_unique_id();
1992 reply->type = MSG_HARDWARE;
1993 reply->win = win;
1994 reply->msg = msg_code;
1995 reply->wparam = msg->wparam;
1996 reply->lparam = msg->lparam;
1997 reply->x = msg->x;
1998 reply->y = msg->y;
1999 reply->time = msg->time;
2001 data->hw_id = msg->unique_id;
2002 set_reply_data( msg->data, msg->data_size );
2003 if (msg->msg == WM_INPUT && (flags & PM_REMOVE))
2004 release_hardware_message( current->queue, data->hw_id, 1 );
2005 return 1;
2007 /* nothing found, clear the hardware queue bits */
2008 clear_queue_bits( thread->queue, clear_bits );
2009 return 0;
2012 /* increment (or decrement if 'incr' is negative) the queue paint count */
2013 void inc_queue_paint_count( struct thread *thread, int incr )
2015 struct msg_queue *queue = thread->queue;
2017 assert( queue );
2019 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2021 if (queue->paint_count)
2022 set_queue_bits( queue, QS_PAINT );
2023 else
2024 clear_queue_bits( queue, QS_PAINT );
2028 /* remove all messages and timers belonging to a certain window */
2029 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2031 struct msg_queue *queue = thread->queue;
2032 struct list *ptr;
2033 int i;
2035 if (!queue) return;
2037 /* remove timers */
2039 ptr = list_head( &queue->pending_timers );
2040 while (ptr)
2042 struct list *next = list_next( &queue->pending_timers, ptr );
2043 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2044 if (timer->win == win) free_timer( queue, timer );
2045 ptr = next;
2047 ptr = list_head( &queue->expired_timers );
2048 while (ptr)
2050 struct list *next = list_next( &queue->expired_timers, ptr );
2051 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2052 if (timer->win == win) free_timer( queue, timer );
2053 ptr = next;
2056 /* remove messages */
2057 for (i = 0; i < NB_MSG_KINDS; i++)
2059 struct list *ptr, *next;
2061 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2063 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2064 if (msg->win == win)
2066 if (msg->msg == WM_QUIT && !queue->quit_message)
2068 queue->quit_message = 1;
2069 queue->exit_code = msg->wparam;
2071 remove_queue_message( queue, msg, i );
2076 thread_input_cleanup_window( queue, win );
2079 /* post a message to a window; used by socket handling */
2080 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2082 struct message *msg;
2083 struct thread *thread = get_window_thread( win );
2085 if (!thread) return;
2087 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2089 msg->type = MSG_POSTED;
2090 msg->win = get_user_full_handle( win );
2091 msg->msg = message;
2092 msg->wparam = wparam;
2093 msg->lparam = lparam;
2094 msg->result = NULL;
2095 msg->data = NULL;
2096 msg->data_size = 0;
2098 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2100 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2101 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2102 if (message == WM_HOTKEY)
2104 set_queue_bits( thread->queue, QS_HOTKEY );
2105 thread->queue->hotkey_count++;
2108 release_object( thread );
2111 /* post a win event */
2112 void post_win_event( struct thread *thread, unsigned int event,
2113 user_handle_t win, unsigned int object_id,
2114 unsigned int child_id, client_ptr_t hook_proc,
2115 const WCHAR *module, data_size_t module_size,
2116 user_handle_t hook)
2118 struct message *msg;
2120 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2122 struct winevent_msg_data *data;
2124 msg->type = MSG_WINEVENT;
2125 msg->win = get_user_full_handle( win );
2126 msg->msg = event;
2127 msg->wparam = object_id;
2128 msg->lparam = child_id;
2129 msg->time = get_tick_count();
2130 msg->result = NULL;
2132 if ((data = malloc( sizeof(*data) + module_size )))
2134 data->hook = hook;
2135 data->tid = get_thread_id( current );
2136 data->hook_proc = hook_proc;
2137 memcpy( data + 1, module, module_size );
2139 msg->data = data;
2140 msg->data_size = sizeof(*data) + module_size;
2142 if (debug_level > 1)
2143 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2144 get_thread_id(thread), event, win, object_id, child_id );
2145 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2146 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2148 else
2149 free( msg );
2153 /* free all hotkeys on a desktop, optionally filtering by window */
2154 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2156 struct hotkey *hotkey, *hotkey2;
2158 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2160 if (!window || hotkey->win == window)
2162 list_remove( &hotkey->entry );
2163 free( hotkey );
2169 /* check if the thread owning the window is hung */
2170 DECL_HANDLER(is_window_hung)
2172 struct thread *thread;
2174 thread = get_window_thread( req->win );
2175 if (thread)
2177 reply->is_hung = is_queue_hung( thread->queue );
2178 release_object( thread );
2180 else reply->is_hung = 0;
2184 /* get the message queue of the current thread */
2185 DECL_HANDLER(get_msg_queue)
2187 struct msg_queue *queue = get_current_queue();
2189 reply->handle = 0;
2190 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2194 /* set the file descriptor associated to the current thread queue */
2195 DECL_HANDLER(set_queue_fd)
2197 struct msg_queue *queue = get_current_queue();
2198 struct file *file;
2199 int unix_fd;
2201 if (queue->fd) /* fd can only be set once */
2203 set_error( STATUS_ACCESS_DENIED );
2204 return;
2206 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2208 if ((unix_fd = get_file_unix_fd( file )) != -1)
2210 if ((unix_fd = dup( unix_fd )) != -1)
2211 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2212 else
2213 file_set_error();
2215 release_object( file );
2219 /* set the current message queue wakeup mask */
2220 DECL_HANDLER(set_queue_mask)
2222 struct msg_queue *queue = get_current_queue();
2224 if (queue)
2226 queue->wake_mask = req->wake_mask;
2227 queue->changed_mask = req->changed_mask;
2228 reply->wake_bits = queue->wake_bits;
2229 reply->changed_bits = queue->changed_bits;
2230 if (is_signaled( queue ))
2232 /* if skip wait is set, do what would have been done in the subsequent wait */
2233 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2234 else wake_up( &queue->obj, 0 );
2240 /* get the current message queue status */
2241 DECL_HANDLER(get_queue_status)
2243 struct msg_queue *queue = current->queue;
2244 if (queue)
2246 reply->wake_bits = queue->wake_bits;
2247 reply->changed_bits = queue->changed_bits;
2248 queue->changed_bits &= ~req->clear_bits;
2250 else reply->wake_bits = reply->changed_bits = 0;
2254 /* send a message to a thread queue */
2255 DECL_HANDLER(send_message)
2257 struct message *msg;
2258 struct msg_queue *send_queue = get_current_queue();
2259 struct msg_queue *recv_queue = NULL;
2260 struct thread *thread = NULL;
2262 if (!(thread = get_thread_from_id( req->id ))) return;
2264 if (!(recv_queue = thread->queue))
2266 set_error( STATUS_INVALID_PARAMETER );
2267 release_object( thread );
2268 return;
2270 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2272 set_error( STATUS_TIMEOUT );
2273 release_object( thread );
2274 return;
2277 if ((msg = mem_alloc( sizeof(*msg) )))
2279 msg->type = req->type;
2280 msg->win = get_user_full_handle( req->win );
2281 msg->msg = req->msg;
2282 msg->wparam = req->wparam;
2283 msg->lparam = req->lparam;
2284 msg->result = NULL;
2285 msg->data = NULL;
2286 msg->data_size = get_req_data_size();
2288 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2290 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2292 free( msg );
2293 release_object( thread );
2294 return;
2297 switch(msg->type)
2299 case MSG_OTHER_PROCESS:
2300 case MSG_ASCII:
2301 case MSG_UNICODE:
2302 case MSG_CALLBACK:
2303 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2305 free_message( msg );
2306 break;
2308 /* fall through */
2309 case MSG_NOTIFY:
2310 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2311 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2312 break;
2313 case MSG_POSTED:
2314 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2315 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2316 if (msg->msg == WM_HOTKEY)
2318 set_queue_bits( recv_queue, QS_HOTKEY );
2319 recv_queue->hotkey_count++;
2321 break;
2322 case MSG_HARDWARE: /* should use send_hardware_message instead */
2323 case MSG_CALLBACK_RESULT: /* cannot send this one */
2324 case MSG_HOOK_LL: /* generated internally */
2325 default:
2326 set_error( STATUS_INVALID_PARAMETER );
2327 free( msg );
2328 break;
2331 release_object( thread );
2334 /* send a hardware message to a thread queue */
2335 DECL_HANDLER(send_hardware_message)
2337 struct thread *thread = NULL;
2338 struct desktop *desktop;
2339 struct msg_queue *sender = get_current_queue();
2340 data_size_t size = min( 256, get_reply_max_size() );
2342 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2344 if (req->win)
2346 if (!(thread = get_window_thread( req->win ))) return;
2347 if (desktop != thread->queue->input->desktop)
2349 /* don't allow queuing events to a different desktop */
2350 release_object( desktop );
2351 return;
2355 reply->prev_x = desktop->cursor.x;
2356 reply->prev_y = desktop->cursor.y;
2358 switch (req->input.type)
2360 case INPUT_MOUSE:
2361 reply->wait = queue_mouse_message( desktop, req->win, &req->input, req->flags, sender );
2362 break;
2363 case INPUT_KEYBOARD:
2364 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, req->flags, sender );
2365 break;
2366 case INPUT_HARDWARE:
2367 queue_custom_hardware_message( desktop, req->win, &req->input );
2368 break;
2369 default:
2370 set_error( STATUS_INVALID_PARAMETER );
2372 if (thread) release_object( thread );
2374 reply->new_x = desktop->cursor.x;
2375 reply->new_y = desktop->cursor.y;
2376 set_reply_data( desktop->keystate, size );
2377 release_object( desktop );
2380 /* post a quit message to the current queue */
2381 DECL_HANDLER(post_quit_message)
2383 struct msg_queue *queue = get_current_queue();
2385 if (!queue)
2386 return;
2388 queue->quit_message = 1;
2389 queue->exit_code = req->exit_code;
2390 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2393 /* get a message from the current queue */
2394 DECL_HANDLER(get_message)
2396 struct timer *timer;
2397 struct list *ptr;
2398 struct msg_queue *queue = get_current_queue();
2399 user_handle_t get_win = get_user_full_handle( req->get_win );
2400 unsigned int filter = req->flags >> 16;
2402 reply->active_hooks = get_active_hooks();
2404 if (!queue) return;
2405 queue->last_get_msg = current_time;
2406 if (!filter) filter = QS_ALLINPUT;
2408 /* first check for sent messages */
2409 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2411 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2412 receive_message( queue, msg, reply );
2413 return;
2416 /* clear changed bits so we can wait on them if we don't find a message */
2417 if (filter & QS_POSTMESSAGE)
2419 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2420 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2422 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2423 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2425 /* then check for posted messages */
2426 if ((filter & QS_POSTMESSAGE) &&
2427 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2428 return;
2430 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2431 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2432 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2433 return;
2435 /* only check for quit messages if not posted messages pending */
2436 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2437 return;
2439 /* then check for any raw hardware message */
2440 if ((filter & QS_INPUT) &&
2441 filter_contains_hw_range( req->get_first, req->get_last ) &&
2442 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2443 return;
2445 /* now check for WM_PAINT */
2446 if ((filter & QS_PAINT) &&
2447 queue->paint_count &&
2448 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2449 (reply->win = find_window_to_repaint( get_win, current )))
2451 reply->type = MSG_POSTED;
2452 reply->msg = WM_PAINT;
2453 reply->wparam = 0;
2454 reply->lparam = 0;
2455 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2456 return;
2459 /* now check for timer */
2460 if ((filter & QS_TIMER) &&
2461 (timer = find_expired_timer( queue, get_win, req->get_first,
2462 req->get_last, (req->flags & PM_REMOVE) )))
2464 reply->type = MSG_POSTED;
2465 reply->win = timer->win;
2466 reply->msg = timer->msg;
2467 reply->wparam = timer->id;
2468 reply->lparam = timer->lparam;
2469 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2470 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2471 set_event( current->process->idle_event );
2472 return;
2475 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2476 queue->wake_mask = req->wake_mask;
2477 queue->changed_mask = req->changed_mask;
2478 set_error( STATUS_PENDING ); /* FIXME */
2482 /* reply to a sent message */
2483 DECL_HANDLER(reply_message)
2485 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2486 else if (current->queue->recv_result)
2487 reply_message( current->queue, req->result, 0, req->remove,
2488 get_req_data(), get_req_data_size() );
2492 /* accept the current hardware message */
2493 DECL_HANDLER(accept_hardware_message)
2495 if (current->queue)
2496 release_hardware_message( current->queue, req->hw_id, req->remove );
2497 else
2498 set_error( STATUS_ACCESS_DENIED );
2502 /* retrieve the reply for the last message sent */
2503 DECL_HANDLER(get_message_reply)
2505 struct message_result *result;
2506 struct list *entry;
2507 struct msg_queue *queue = current->queue;
2509 if (queue)
2511 set_error( STATUS_PENDING );
2512 reply->result = 0;
2514 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2516 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2517 if (result->replied || req->cancel)
2519 if (result->replied)
2521 reply->result = result->result;
2522 set_error( result->error );
2523 if (result->data)
2525 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2526 set_reply_data_ptr( result->data, data_len );
2527 result->data = NULL;
2528 result->data_size = 0;
2531 remove_result_from_sender( result );
2533 entry = list_head( &queue->send_result );
2534 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2535 else
2537 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2538 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2539 else clear_queue_bits( queue, QS_SMRESULT );
2543 else set_error( STATUS_ACCESS_DENIED );
2547 /* set a window timer */
2548 DECL_HANDLER(set_win_timer)
2550 struct timer *timer;
2551 struct msg_queue *queue;
2552 struct thread *thread = NULL;
2553 user_handle_t win = 0;
2554 lparam_t id = req->id;
2556 if (req->win)
2558 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2560 set_error( STATUS_INVALID_HANDLE );
2561 return;
2563 if (thread->process != current->process)
2565 release_object( thread );
2566 set_error( STATUS_ACCESS_DENIED );
2567 return;
2569 queue = thread->queue;
2570 /* remove it if it existed already */
2571 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2573 else
2575 queue = get_current_queue();
2576 /* look for a timer with this id */
2577 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2579 /* free and reuse id */
2580 free_timer( queue, timer );
2582 else
2584 /* find a free id for it */
2587 id = queue->next_timer_id;
2588 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2590 while (find_timer( queue, 0, req->msg, id ));
2594 if ((timer = set_timer( queue, req->rate )))
2596 timer->win = win;
2597 timer->msg = req->msg;
2598 timer->id = id;
2599 timer->lparam = req->lparam;
2600 reply->id = id;
2602 if (thread) release_object( thread );
2605 /* kill a window timer */
2606 DECL_HANDLER(kill_win_timer)
2608 struct timer *timer;
2609 struct thread *thread;
2610 user_handle_t win = 0;
2612 if (req->win)
2614 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2616 set_error( STATUS_INVALID_HANDLE );
2617 return;
2619 if (thread->process != current->process)
2621 release_object( thread );
2622 set_error( STATUS_ACCESS_DENIED );
2623 return;
2626 else thread = (struct thread *)grab_object( current );
2628 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2629 free_timer( thread->queue, timer );
2630 else
2631 set_error( STATUS_INVALID_PARAMETER );
2633 release_object( thread );
2636 DECL_HANDLER(register_hotkey)
2638 struct desktop *desktop;
2639 user_handle_t win_handle = req->window;
2640 struct hotkey *hotkey;
2641 struct hotkey *new_hotkey = NULL;
2642 struct thread *thread;
2643 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2645 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2647 if (win_handle)
2649 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2651 release_object( desktop );
2652 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2653 return;
2656 thread = get_window_thread( win_handle );
2657 if (thread) release_object( thread );
2659 if (thread != current)
2661 release_object( desktop );
2662 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2663 return;
2667 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2669 if (req->vkey == hotkey->vkey &&
2670 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2672 release_object( desktop );
2673 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2674 return;
2676 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2677 new_hotkey = hotkey;
2680 if (new_hotkey)
2682 reply->replaced = 1;
2683 reply->flags = new_hotkey->flags;
2684 reply->vkey = new_hotkey->vkey;
2686 else
2688 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2689 if (new_hotkey)
2691 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2692 new_hotkey->queue = current->queue;
2693 new_hotkey->win = win_handle;
2694 new_hotkey->id = req->id;
2698 if (new_hotkey)
2700 new_hotkey->flags = req->flags;
2701 new_hotkey->vkey = req->vkey;
2704 release_object( desktop );
2707 DECL_HANDLER(unregister_hotkey)
2709 struct desktop *desktop;
2710 user_handle_t win_handle = req->window;
2711 struct hotkey *hotkey;
2712 struct thread *thread;
2714 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2716 if (win_handle)
2718 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2720 release_object( desktop );
2721 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2722 return;
2725 thread = get_window_thread( win_handle );
2726 if (thread) release_object( thread );
2728 if (thread != current)
2730 release_object( desktop );
2731 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2732 return;
2736 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2738 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2739 goto found;
2742 release_object( desktop );
2743 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2744 return;
2746 found:
2747 reply->flags = hotkey->flags;
2748 reply->vkey = hotkey->vkey;
2749 list_remove( &hotkey->entry );
2750 free( hotkey );
2751 release_object( desktop );
2754 /* attach (or detach) thread inputs */
2755 DECL_HANDLER(attach_thread_input)
2757 struct thread *thread_from = get_thread_from_id( req->tid_from );
2758 struct thread *thread_to = get_thread_from_id( req->tid_to );
2760 if (!thread_from || !thread_to)
2762 if (thread_from) release_object( thread_from );
2763 if (thread_to) release_object( thread_to );
2764 return;
2766 if (thread_from != thread_to)
2768 if (req->attach)
2770 if ((thread_to->queue || thread_to == current) &&
2771 (thread_from->queue || thread_from == current))
2772 attach_thread_input( thread_from, thread_to );
2773 else
2774 set_error( STATUS_INVALID_PARAMETER );
2776 else
2778 if (thread_from->queue && thread_to->queue &&
2779 thread_from->queue->input == thread_to->queue->input)
2780 detach_thread_input( thread_from );
2781 else
2782 set_error( STATUS_ACCESS_DENIED );
2785 else set_error( STATUS_ACCESS_DENIED );
2786 release_object( thread_from );
2787 release_object( thread_to );
2791 /* get thread input data */
2792 DECL_HANDLER(get_thread_input)
2794 struct thread *thread = NULL;
2795 struct desktop *desktop;
2796 struct thread_input *input;
2798 if (req->tid)
2800 if (!(thread = get_thread_from_id( req->tid ))) return;
2801 if (!(desktop = get_thread_desktop( thread, 0 )))
2803 release_object( thread );
2804 return;
2806 input = thread->queue ? thread->queue->input : NULL;
2808 else
2810 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2811 input = desktop->foreground_input; /* get the foreground thread info */
2814 if (input)
2816 reply->focus = input->focus;
2817 reply->capture = input->capture;
2818 reply->active = input->active;
2819 reply->menu_owner = input->menu_owner;
2820 reply->move_size = input->move_size;
2821 reply->caret = input->caret;
2822 reply->cursor = input->cursor;
2823 reply->show_count = input->cursor_count;
2824 reply->rect = input->caret_rect;
2827 /* foreground window is active window of foreground thread */
2828 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2829 if (thread) release_object( thread );
2830 release_object( desktop );
2834 /* retrieve queue keyboard state for a given thread */
2835 DECL_HANDLER(get_key_state)
2837 struct thread *thread;
2838 struct desktop *desktop;
2839 data_size_t size = min( 256, get_reply_max_size() );
2841 if (!req->tid) /* get global async key state */
2843 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2844 if (req->key >= 0)
2846 reply->state = desktop->keystate[req->key & 0xff];
2847 desktop->keystate[req->key & 0xff] &= ~0x40;
2849 set_reply_data( desktop->keystate, size );
2850 release_object( desktop );
2852 else
2854 if (!(thread = get_thread_from_id( req->tid ))) return;
2855 if (thread->queue)
2857 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2858 set_reply_data( thread->queue->input->keystate, size );
2860 release_object( thread );
2865 /* set queue keyboard state for a given thread */
2866 DECL_HANDLER(set_key_state)
2868 struct thread *thread;
2869 struct desktop *desktop;
2870 data_size_t size = min( 256, get_req_data_size() );
2872 if (!req->tid) /* set global async key state */
2874 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2875 memcpy( desktop->keystate, get_req_data(), size );
2876 release_object( desktop );
2878 else
2880 if (!(thread = get_thread_from_id( req->tid ))) return;
2881 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
2882 if (req->async && (desktop = get_thread_desktop( thread, 0 )))
2884 memcpy( desktop->keystate, get_req_data(), size );
2885 release_object( desktop );
2887 release_object( thread );
2892 /* set the system foreground window */
2893 DECL_HANDLER(set_foreground_window)
2895 struct thread *thread = NULL;
2896 struct desktop *desktop;
2897 struct msg_queue *queue = get_current_queue();
2899 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2900 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
2901 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
2902 reply->send_msg_new = FALSE;
2904 if (is_valid_foreground_window( req->handle ) &&
2905 (thread = get_window_thread( req->handle )) &&
2906 thread->queue->input->desktop == desktop)
2908 set_foreground_input( desktop, thread->queue->input );
2909 reply->send_msg_new = (desktop->foreground_input != queue->input);
2911 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2913 if (thread) release_object( thread );
2914 release_object( desktop );
2918 /* set the current thread focus window */
2919 DECL_HANDLER(set_focus_window)
2921 struct msg_queue *queue = get_current_queue();
2923 reply->previous = 0;
2924 if (queue && check_queue_input_window( queue, req->handle ))
2926 reply->previous = queue->input->focus;
2927 queue->input->focus = get_user_full_handle( req->handle );
2932 /* set the current thread active window */
2933 DECL_HANDLER(set_active_window)
2935 struct msg_queue *queue = get_current_queue();
2937 reply->previous = 0;
2938 if (queue && check_queue_input_window( queue, req->handle ))
2940 if (!req->handle || make_window_active( req->handle ))
2942 reply->previous = queue->input->active;
2943 queue->input->active = get_user_full_handle( req->handle );
2945 else set_error( STATUS_INVALID_HANDLE );
2950 /* set the current thread capture window */
2951 DECL_HANDLER(set_capture_window)
2953 struct msg_queue *queue = get_current_queue();
2955 reply->previous = reply->full_handle = 0;
2956 if (queue && check_queue_input_window( queue, req->handle ))
2958 struct thread_input *input = queue->input;
2960 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2961 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2963 set_error(STATUS_ACCESS_DENIED);
2964 return;
2966 reply->previous = input->capture;
2967 input->capture = get_user_full_handle( req->handle );
2968 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2969 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2970 reply->full_handle = input->capture;
2975 /* Set the current thread caret window */
2976 DECL_HANDLER(set_caret_window)
2978 struct msg_queue *queue = get_current_queue();
2980 reply->previous = 0;
2981 if (queue && check_queue_input_window( queue, req->handle ))
2983 struct thread_input *input = queue->input;
2985 reply->previous = input->caret;
2986 reply->old_rect = input->caret_rect;
2987 reply->old_hide = input->caret_hide;
2988 reply->old_state = input->caret_state;
2990 set_caret_window( input, get_user_full_handle(req->handle) );
2991 input->caret_rect.right = input->caret_rect.left + req->width;
2992 input->caret_rect.bottom = input->caret_rect.top + req->height;
2997 /* Set the current thread caret information */
2998 DECL_HANDLER(set_caret_info)
3000 struct msg_queue *queue = get_current_queue();
3001 struct thread_input *input;
3003 if (!queue) return;
3004 input = queue->input;
3005 reply->full_handle = input->caret;
3006 reply->old_rect = input->caret_rect;
3007 reply->old_hide = input->caret_hide;
3008 reply->old_state = input->caret_state;
3010 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3012 set_error( STATUS_ACCESS_DENIED );
3013 return;
3015 if (req->flags & SET_CARET_POS)
3017 input->caret_rect.right += req->x - input->caret_rect.left;
3018 input->caret_rect.bottom += req->y - input->caret_rect.top;
3019 input->caret_rect.left = req->x;
3020 input->caret_rect.top = req->y;
3022 if (req->flags & SET_CARET_HIDE)
3024 input->caret_hide += req->hide;
3025 if (input->caret_hide < 0) input->caret_hide = 0;
3027 if (req->flags & SET_CARET_STATE)
3029 if (req->state == -1) input->caret_state = !input->caret_state;
3030 else input->caret_state = !!req->state;
3035 /* get the time of the last input event */
3036 DECL_HANDLER(get_last_input_time)
3038 reply->time = last_input_time;
3041 /* set/get the current cursor */
3042 DECL_HANDLER(set_cursor)
3044 struct msg_queue *queue = get_current_queue();
3045 struct thread_input *input;
3047 if (!queue) return;
3048 input = queue->input;
3050 reply->prev_handle = input->cursor;
3051 reply->prev_count = input->cursor_count;
3052 reply->prev_x = input->desktop->cursor.x;
3053 reply->prev_y = input->desktop->cursor.y;
3055 if (req->flags & SET_CURSOR_HANDLE)
3057 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3059 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3060 return;
3062 input->cursor = req->handle;
3064 if (req->flags & SET_CURSOR_COUNT)
3066 queue->cursor_count += req->show_count;
3067 input->cursor_count += req->show_count;
3069 if (req->flags & SET_CURSOR_POS)
3071 set_cursor_pos( input->desktop, req->x, req->y );
3073 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3075 struct desktop *desktop = input->desktop;
3077 /* only the desktop owner can set the message */
3078 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3079 desktop->cursor.clip_msg = req->clip_msg;
3081 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip );
3084 reply->new_x = input->desktop->cursor.x;
3085 reply->new_y = input->desktop->cursor.y;
3086 reply->new_clip = input->desktop->cursor.clip;
3087 reply->last_change = input->desktop->cursor.last_change;
3090 DECL_HANDLER(update_rawinput_devices)
3092 const struct rawinput_device *devices = get_req_data();
3093 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3094 const struct rawinput_device_entry *e;
3095 unsigned int i;
3097 for (i = 0; i < device_count; ++i)
3099 update_rawinput_device(&devices[i]);
3102 e = find_rawinput_device( 1, 2 );
3103 current->process->rawinput_mouse = e ? &e->device : NULL;
3104 e = find_rawinput_device( 1, 6 );
3105 current->process->rawinput_kbd = e ? &e->device : NULL;