server: Make the APC callback optional in async calls.
[wine/multimedia.git] / server / queue.c
blob350d45acfe7b531c3a97a12fce9f6c250d030116
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_input *input;
1100 if ((input = create_thread_input( thread_from )))
1102 assign_thread_input( thread_from, input );
1103 release_object( input );
1108 /* set the next timer to expire */
1109 static void set_next_timer( struct msg_queue *queue )
1111 struct list *ptr;
1113 if (queue->timeout)
1115 remove_timeout_user( queue->timeout );
1116 queue->timeout = NULL;
1118 if ((ptr = list_head( &queue->pending_timers )))
1120 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1121 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1123 /* set/clear QS_TIMER bit */
1124 if (list_empty( &queue->expired_timers ))
1125 clear_queue_bits( queue, QS_TIMER );
1126 else
1127 set_queue_bits( queue, QS_TIMER );
1130 /* find a timer from its window and id */
1131 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1132 unsigned int msg, lparam_t id )
1134 struct list *ptr;
1136 /* we need to search both lists */
1138 LIST_FOR_EACH( ptr, &queue->pending_timers )
1140 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1141 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1143 LIST_FOR_EACH( ptr, &queue->expired_timers )
1145 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1146 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1148 return NULL;
1151 /* callback for the next timer expiration */
1152 static void timer_callback( void *private )
1154 struct msg_queue *queue = private;
1155 struct list *ptr;
1157 queue->timeout = NULL;
1158 /* move on to the next timer */
1159 ptr = list_head( &queue->pending_timers );
1160 list_remove( ptr );
1161 list_add_tail( &queue->expired_timers, ptr );
1162 set_next_timer( queue );
1165 /* link a timer at its rightful place in the queue list */
1166 static void link_timer( struct msg_queue *queue, struct timer *timer )
1168 struct list *ptr;
1170 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1172 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1173 if (t->when >= timer->when) break;
1175 list_add_before( ptr, &timer->entry );
1178 /* remove a timer from the queue timer list and free it */
1179 static void free_timer( struct msg_queue *queue, struct timer *timer )
1181 list_remove( &timer->entry );
1182 free( timer );
1183 set_next_timer( queue );
1186 /* restart an expired timer */
1187 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1189 list_remove( &timer->entry );
1190 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1191 link_timer( queue, timer );
1192 set_next_timer( queue );
1195 /* find an expired timer matching the filtering parameters */
1196 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1197 unsigned int get_first, unsigned int get_last,
1198 int remove )
1200 struct list *ptr;
1202 LIST_FOR_EACH( ptr, &queue->expired_timers )
1204 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1205 if (win && timer->win != win) continue;
1206 if (check_msg_filter( timer->msg, get_first, get_last ))
1208 if (remove) restart_timer( queue, timer );
1209 return timer;
1212 return NULL;
1215 /* add a timer */
1216 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1218 struct timer *timer = mem_alloc( sizeof(*timer) );
1219 if (timer)
1221 timer->rate = max( rate, 1 );
1222 timer->when = current_time + (timeout_t)timer->rate * 10000;
1223 link_timer( queue, timer );
1224 /* check if we replaced the next timer */
1225 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1227 return timer;
1230 /* change the input key state for a given key */
1231 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1233 if (down)
1235 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1236 keystate[key] |= down;
1238 else keystate[key] &= ~0x80;
1241 /* update the input key state for a keyboard message */
1242 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1243 const struct message *msg )
1245 unsigned char key;
1246 int down = 0;
1248 switch (msg->msg)
1250 case WM_LBUTTONDOWN:
1251 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1252 /* fall through */
1253 case WM_LBUTTONUP:
1254 set_input_key_state( keystate, VK_LBUTTON, down );
1255 break;
1256 case WM_MBUTTONDOWN:
1257 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1258 /* fall through */
1259 case WM_MBUTTONUP:
1260 set_input_key_state( keystate, VK_MBUTTON, down );
1261 break;
1262 case WM_RBUTTONDOWN:
1263 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1264 /* fall through */
1265 case WM_RBUTTONUP:
1266 set_input_key_state( keystate, VK_RBUTTON, down );
1267 break;
1268 case WM_XBUTTONDOWN:
1269 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1270 /* fall through */
1271 case WM_XBUTTONUP:
1272 if (msg->wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1273 else if (msg->wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1274 break;
1275 case WM_KEYDOWN:
1276 case WM_SYSKEYDOWN:
1277 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1278 /* fall through */
1279 case WM_KEYUP:
1280 case WM_SYSKEYUP:
1281 key = (unsigned char)msg->wparam;
1282 set_input_key_state( keystate, key, down );
1283 switch(key)
1285 case VK_LCONTROL:
1286 case VK_RCONTROL:
1287 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1288 set_input_key_state( keystate, VK_CONTROL, down );
1289 break;
1290 case VK_LMENU:
1291 case VK_RMENU:
1292 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1293 set_input_key_state( keystate, VK_MENU, down );
1294 break;
1295 case VK_LSHIFT:
1296 case VK_RSHIFT:
1297 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1298 set_input_key_state( keystate, VK_SHIFT, down );
1299 break;
1301 break;
1305 /* release the hardware message currently being processed by the given thread */
1306 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1307 int remove )
1309 struct thread_input *input = queue->input;
1310 struct message *msg;
1312 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1314 if (msg->unique_id == hw_id) break;
1316 if (&msg->entry == &input->msg_list) return; /* not found */
1318 /* clear the queue bit for that message */
1319 if (remove)
1321 struct message *other;
1322 int clr_bit;
1324 clr_bit = get_hardware_msg_bit( msg );
1325 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1327 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1329 clr_bit = 0;
1330 break;
1333 if (clr_bit) clear_queue_bits( queue, clr_bit );
1335 update_input_key_state( input->desktop, input->keystate, msg );
1336 list_remove( &msg->entry );
1337 free_message( msg );
1341 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1343 struct hotkey *hotkey;
1344 unsigned int modifiers = 0;
1346 if (msg->msg != WM_KEYDOWN) return 0;
1348 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1349 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1350 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1351 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1353 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1355 if (hotkey->vkey != msg->wparam) continue;
1356 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1359 return 0;
1361 found:
1362 msg->type = MSG_POSTED;
1363 msg->win = hotkey->win;
1364 msg->msg = WM_HOTKEY;
1365 msg->wparam = hotkey->id;
1366 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1368 free( msg->data );
1369 msg->data = NULL;
1370 msg->data_size = 0;
1372 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1373 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1374 hotkey->queue->hotkey_count++;
1375 return 1;
1378 /* find the window that should receive a given hardware message */
1379 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1380 struct message *msg, unsigned int *msg_code,
1381 struct thread **thread )
1383 user_handle_t win = 0;
1385 *thread = NULL;
1386 *msg_code = msg->msg;
1387 if (msg->msg == WM_INPUT)
1389 if (!(win = msg->win) && input) win = input->focus;
1391 else if (is_keyboard_msg( msg ))
1393 if (input && !(win = input->focus))
1395 win = input->active;
1396 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1399 else if (!input || !(win = input->capture)) /* mouse message */
1401 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1402 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1404 *thread = window_thread_from_point( win, msg->x, msg->y );
1407 if (!*thread)
1408 *thread = get_window_thread( win );
1409 return win;
1412 static struct rawinput_device_entry *find_rawinput_device( unsigned short usage_page, unsigned short usage )
1414 struct rawinput_device_entry *e;
1416 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
1418 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1419 return e;
1422 return NULL;
1425 static void update_rawinput_device(const struct rawinput_device *device)
1427 struct rawinput_device_entry *e;
1429 if (!(e = find_rawinput_device( device->usage_page, device->usage )))
1431 if (!(e = mem_alloc( sizeof(*e) ))) return;
1432 list_add_tail( &current->process->rawinput_devices, &e->entry );
1435 if (device->flags & RIDEV_REMOVE)
1437 list_remove( &e->entry );
1438 free( e );
1439 return;
1442 e->device = *device;
1443 e->device.target = get_user_full_handle( e->device.target );
1446 /* queue a hardware message into a given thread input */
1447 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1449 user_handle_t win;
1450 struct thread *thread;
1451 struct thread_input *input;
1452 unsigned int msg_code;
1454 update_input_key_state( desktop, desktop->keystate, msg );
1455 last_input_time = get_tick_count();
1456 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1458 if (is_keyboard_msg( msg ))
1460 if (queue_hotkey_message( desktop, msg )) return;
1461 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1462 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1463 msg->lparam &= ~(KF_EXTENDED << 16);
1465 else if (msg->msg != WM_INPUT)
1467 if (msg->msg == WM_MOUSEMOVE)
1469 int x = min( max( msg->x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
1470 int y = min( max( msg->y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
1471 if (desktop->cursor.x != x || desktop->cursor.y != y) always_queue = 1;
1472 desktop->cursor.x = x;
1473 desktop->cursor.y = y;
1474 desktop->cursor.last_change = get_tick_count();
1476 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1477 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1478 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1479 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1480 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1481 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1482 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1484 msg->x = desktop->cursor.x;
1485 msg->y = desktop->cursor.y;
1487 if (msg->win && (thread = get_window_thread( msg->win )))
1489 input = thread->queue->input;
1490 release_object( thread );
1492 else input = desktop->foreground_input;
1494 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1495 if (!win || !thread)
1497 if (input) update_input_key_state( input->desktop, input->keystate, msg );
1498 free_message( msg );
1499 return;
1501 input = thread->queue->input;
1503 if (win != desktop->cursor.win) always_queue = 1;
1504 desktop->cursor.win = win;
1506 if (!always_queue || merge_message( input, msg )) free_message( msg );
1507 else
1509 msg->unique_id = 0; /* will be set once we return it to the app */
1510 list_add_tail( &input->msg_list, &msg->entry );
1511 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1513 release_object( thread );
1516 /* send the low-level hook message for a given hardware message */
1517 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1518 const hw_input_t *input, struct msg_queue *sender )
1520 struct thread *hook_thread;
1521 struct msg_queue *queue;
1522 struct message *msg;
1523 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1524 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1526 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1527 if (!(queue = hook_thread->queue)) return 0;
1528 if (is_queue_hung( queue )) return 0;
1530 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1532 msg->type = MSG_HOOK_LL;
1533 msg->win = 0;
1534 msg->msg = id;
1535 msg->wparam = hardware_msg->msg;
1536 msg->x = hardware_msg->x;
1537 msg->y = hardware_msg->y;
1538 msg->time = hardware_msg->time;
1539 msg->data_size = hardware_msg->data_size;
1540 msg->result = NULL;
1542 if (input->type == INPUT_KEYBOARD)
1544 unsigned short vkey = input->kbd.vkey;
1545 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1546 msg->lparam = (input->kbd.scan << 16) | vkey;
1548 else msg->lparam = input->mouse.data << 16;
1550 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1551 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1553 free_message( msg );
1554 return 0;
1556 msg->result->hardware_msg = hardware_msg;
1557 msg->result->desktop = (struct desktop *)grab_object( desktop );
1558 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1559 set_queue_bits( queue, QS_SENDMESSAGE );
1560 return 1;
1563 /* queue a hardware message for a mouse event */
1564 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1565 unsigned int hook_flags, struct msg_queue *sender )
1567 const struct rawinput_device *device;
1568 struct hardware_msg_data *msg_data;
1569 struct message *msg;
1570 unsigned int i, time, flags;
1571 int wait = 0, x, y;
1573 static const unsigned int messages[] =
1575 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1576 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1577 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1578 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1579 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1580 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1581 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1582 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1583 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1584 0, /* 0x0200 = unused */
1585 0, /* 0x0400 = unused */
1586 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1587 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1590 desktop->cursor.last_change = get_tick_count();
1591 flags = input->mouse.flags;
1592 time = input->mouse.time;
1593 if (!time) time = desktop->cursor.last_change;
1595 if (flags & MOUSEEVENTF_MOVE)
1597 if (flags & MOUSEEVENTF_ABSOLUTE)
1599 x = input->mouse.x;
1600 y = input->mouse.y;
1601 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1602 x == desktop->cursor.x && y == desktop->cursor.y)
1603 flags &= ~MOUSEEVENTF_MOVE;
1605 else
1607 x = desktop->cursor.x + input->mouse.x;
1608 y = desktop->cursor.y + input->mouse.y;
1611 else
1613 x = desktop->cursor.x;
1614 y = desktop->cursor.y;
1617 if ((device = current->process->rawinput_mouse))
1619 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1620 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1622 free( msg );
1623 return 0;
1626 msg->type = MSG_HARDWARE;
1627 msg->win = device->target;
1628 msg->msg = WM_INPUT;
1629 msg->wparam = RIM_INPUT;
1630 msg->lparam = 0;
1631 msg->time = time;
1632 msg->data = msg_data;
1633 msg->data_size = sizeof(*msg_data);
1634 msg->result = NULL;
1636 msg_data->info = input->mouse.info;
1637 msg_data->flags = flags;
1638 msg_data->rawinput.type = RIM_TYPEMOUSE;
1639 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1640 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1641 msg_data->rawinput.mouse.data = input->mouse.data;
1643 queue_hardware_message( desktop, msg, 0 );
1646 for (i = 0; i < sizeof(messages)/sizeof(messages[0]); i++)
1648 if (!messages[i]) continue;
1649 if (!(flags & (1 << i))) continue;
1650 flags &= ~(1 << i);
1652 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1653 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1655 free( msg );
1656 return 0;
1658 memset( msg_data, 0, sizeof(*msg_data) );
1660 msg->type = MSG_HARDWARE;
1661 msg->win = get_user_full_handle( win );
1662 msg->msg = messages[i];
1663 msg->wparam = input->mouse.data << 16;
1664 msg->lparam = 0;
1665 msg->x = x;
1666 msg->y = y;
1667 msg->time = time;
1668 msg->result = NULL;
1669 msg->data = msg_data;
1670 msg->data_size = sizeof(*msg_data);
1671 msg_data->info = input->mouse.info;
1672 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLMHF_INJECTED;
1674 /* specify a sender only when sending the last message */
1675 if (!(flags & ((1 << sizeof(messages)/sizeof(messages[0])) - 1)))
1677 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1678 queue_hardware_message( desktop, msg, 0 );
1680 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1681 queue_hardware_message( desktop, msg, 0 );
1683 return wait;
1686 /* queue a hardware message for a keyboard event */
1687 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1688 unsigned int hook_flags, struct msg_queue *sender )
1690 const struct rawinput_device *device;
1691 struct hardware_msg_data *msg_data;
1692 struct message *msg;
1693 unsigned char vkey = input->kbd.vkey;
1694 unsigned int message_code, time;
1695 int wait;
1697 if (!(time = input->kbd.time)) time = get_tick_count();
1699 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1701 switch (vkey)
1703 case VK_MENU:
1704 case VK_LMENU:
1705 case VK_RMENU:
1706 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1707 break;
1708 case VK_CONTROL:
1709 case VK_LCONTROL:
1710 case VK_RCONTROL:
1711 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1712 break;
1713 case VK_SHIFT:
1714 case VK_LSHIFT:
1715 case VK_RSHIFT:
1716 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1717 break;
1721 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1722 switch (vkey)
1724 case VK_LMENU:
1725 case VK_RMENU:
1726 if (input->kbd.flags & KEYEVENTF_KEYUP)
1728 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1729 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1730 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1731 message_code = WM_SYSKEYUP;
1732 desktop->keystate[VK_MENU] &= ~0x02;
1734 else
1736 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1737 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1738 message_code = WM_SYSKEYDOWN;
1739 desktop->keystate[VK_MENU] |= 0x02;
1741 break;
1743 case VK_LCONTROL:
1744 case VK_RCONTROL:
1745 /* send WM_SYSKEYUP on release if Alt still pressed */
1746 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1747 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1748 message_code = WM_SYSKEYUP;
1749 desktop->keystate[VK_MENU] &= ~0x02;
1750 break;
1752 default:
1753 /* send WM_SYSKEY for Alt-anykey and for F10 */
1754 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1755 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1756 /* fall through */
1757 case VK_F10:
1758 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1759 desktop->keystate[VK_MENU] &= ~0x02;
1760 break;
1763 if ((device = current->process->rawinput_kbd))
1765 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1766 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1768 free( msg );
1769 return 0;
1772 msg->type = MSG_HARDWARE;
1773 msg->win = device->target;
1774 msg->msg = WM_INPUT;
1775 msg->wparam = RIM_INPUT;
1776 msg->lparam = 0;
1777 msg->time = time;
1778 msg->data = msg_data;
1779 msg->data_size = sizeof(*msg_data);
1780 msg->result = NULL;
1782 msg_data->info = input->kbd.info;
1783 msg_data->flags = input->kbd.flags;
1784 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1785 msg_data->rawinput.kbd.message = message_code;
1786 msg_data->rawinput.kbd.vkey = vkey;
1787 msg_data->rawinput.kbd.scan = input->kbd.scan;
1789 queue_hardware_message( desktop, msg, 0 );
1792 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1793 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1795 free( msg );
1796 return 0;
1798 memset( msg_data, 0, sizeof(*msg_data) );
1800 msg->type = MSG_HARDWARE;
1801 msg->win = get_user_full_handle( win );
1802 msg->msg = message_code;
1803 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1804 msg->time = time;
1805 msg->result = NULL;
1806 msg->data = msg_data;
1807 msg->data_size = sizeof(*msg_data);
1808 msg_data->info = input->kbd.info;
1809 if (hook_flags & SEND_HWMSG_INJECTED) msg_data->flags = LLKHF_INJECTED;
1811 if (input->kbd.flags & KEYEVENTF_UNICODE)
1813 msg->wparam = VK_PACKET;
1815 else
1817 unsigned int flags = 0;
1818 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1819 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1820 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1821 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1823 msg->wparam = vkey;
1824 msg->lparam |= flags << 16;
1825 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1828 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1829 queue_hardware_message( desktop, msg, 1 );
1831 return wait;
1834 /* queue a hardware message for a custom type of event */
1835 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1836 const hw_input_t *input )
1838 struct hardware_msg_data *msg_data;
1839 struct message *msg;
1841 if (!(msg = mem_alloc( sizeof(*msg) ))) return;
1842 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
1844 free( msg );
1845 return;
1847 memset( msg_data, 0, sizeof(*msg_data) );
1849 msg->type = MSG_HARDWARE;
1850 msg->win = get_user_full_handle( win );
1851 msg->msg = input->hw.msg;
1852 msg->wparam = 0;
1853 msg->lparam = input->hw.lparam;
1854 msg->x = desktop->cursor.x;
1855 msg->y = desktop->cursor.y;
1856 msg->time = get_tick_count();
1857 msg->result = NULL;
1858 msg->data = msg_data;
1859 msg->data_size = sizeof(*msg_data);
1861 queue_hardware_message( desktop, msg, 1 );
1864 /* check message filter for a hardware message */
1865 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1866 user_handle_t filter_win, unsigned int first, unsigned int last )
1868 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1870 /* we can only test the window for a keyboard message since the
1871 * dest window for a mouse message depends on hittest */
1872 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1873 return 0;
1874 /* the message code is final for a keyboard message, we can simply check it */
1875 return check_msg_filter( msg_code, first, last );
1877 else /* mouse message */
1879 /* we need to check all possible values that the message can have in the end */
1881 if (check_msg_filter( msg_code, first, last )) return 1;
1882 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1884 /* all other messages can become non-client messages */
1885 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1887 /* clicks can become double-clicks or non-client double-clicks */
1888 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1889 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1891 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1892 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1894 return 0;
1899 /* find a hardware message for the given queue */
1900 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1901 unsigned int first, unsigned int last, unsigned int flags,
1902 struct get_message_reply *reply )
1904 struct thread_input *input = thread->queue->input;
1905 struct thread *win_thread;
1906 struct list *ptr;
1907 user_handle_t win;
1908 int clear_bits, got_one = 0;
1909 unsigned int msg_code;
1911 ptr = list_head( &input->msg_list );
1912 if (hw_id)
1914 while (ptr)
1916 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1917 if (msg->unique_id == hw_id) break;
1918 ptr = list_next( &input->msg_list, ptr );
1920 if (!ptr) ptr = list_head( &input->msg_list );
1921 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1924 if (ptr == list_head( &input->msg_list ))
1925 clear_bits = QS_INPUT;
1926 else
1927 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1929 while (ptr)
1931 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1932 struct hardware_msg_data *data = msg->data;
1934 ptr = list_next( &input->msg_list, ptr );
1935 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
1936 if (!win || !win_thread)
1938 /* no window at all, remove it */
1939 update_input_key_state( input->desktop, input->keystate, msg );
1940 list_remove( &msg->entry );
1941 free_message( msg );
1942 continue;
1944 if (win_thread != thread)
1946 if (win_thread->queue->input == input)
1948 /* wake the other thread */
1949 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1950 got_one = 1;
1952 else
1954 /* for another thread input, drop it */
1955 update_input_key_state( input->desktop, input->keystate, msg );
1956 list_remove( &msg->entry );
1957 free_message( msg );
1959 release_object( win_thread );
1960 continue;
1962 release_object( win_thread );
1964 /* if we already got a message for another thread, or if it doesn't
1965 * match the filter we skip it */
1966 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1968 clear_bits &= ~get_hardware_msg_bit( msg );
1969 continue;
1971 /* now we can return it */
1972 if (!msg->unique_id) msg->unique_id = get_unique_id();
1973 reply->type = MSG_HARDWARE;
1974 reply->win = win;
1975 reply->msg = msg_code;
1976 reply->wparam = msg->wparam;
1977 reply->lparam = msg->lparam;
1978 reply->x = msg->x;
1979 reply->y = msg->y;
1980 reply->time = msg->time;
1982 data->hw_id = msg->unique_id;
1983 set_reply_data( msg->data, msg->data_size );
1984 if (msg->msg == WM_INPUT && (flags & PM_REMOVE))
1985 release_hardware_message( current->queue, data->hw_id, 1 );
1986 return 1;
1988 /* nothing found, clear the hardware queue bits */
1989 clear_queue_bits( thread->queue, clear_bits );
1990 return 0;
1993 /* increment (or decrement if 'incr' is negative) the queue paint count */
1994 void inc_queue_paint_count( struct thread *thread, int incr )
1996 struct msg_queue *queue = thread->queue;
1998 assert( queue );
2000 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
2002 if (queue->paint_count)
2003 set_queue_bits( queue, QS_PAINT );
2004 else
2005 clear_queue_bits( queue, QS_PAINT );
2009 /* remove all messages and timers belonging to a certain window */
2010 void queue_cleanup_window( struct thread *thread, user_handle_t win )
2012 struct msg_queue *queue = thread->queue;
2013 struct list *ptr;
2014 int i;
2016 if (!queue) return;
2018 /* remove timers */
2020 ptr = list_head( &queue->pending_timers );
2021 while (ptr)
2023 struct list *next = list_next( &queue->pending_timers, ptr );
2024 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2025 if (timer->win == win) free_timer( queue, timer );
2026 ptr = next;
2028 ptr = list_head( &queue->expired_timers );
2029 while (ptr)
2031 struct list *next = list_next( &queue->expired_timers, ptr );
2032 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2033 if (timer->win == win) free_timer( queue, timer );
2034 ptr = next;
2037 /* remove messages */
2038 for (i = 0; i < NB_MSG_KINDS; i++)
2040 struct list *ptr, *next;
2042 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2044 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2045 if (msg->win == win)
2047 if (msg->msg == WM_QUIT && !queue->quit_message)
2049 queue->quit_message = 1;
2050 queue->exit_code = msg->wparam;
2052 remove_queue_message( queue, msg, i );
2057 thread_input_cleanup_window( queue, win );
2060 /* post a message to a window; used by socket handling */
2061 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2063 struct message *msg;
2064 struct thread *thread = get_window_thread( win );
2066 if (!thread) return;
2068 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2070 msg->type = MSG_POSTED;
2071 msg->win = get_user_full_handle( win );
2072 msg->msg = message;
2073 msg->wparam = wparam;
2074 msg->lparam = lparam;
2075 msg->result = NULL;
2076 msg->data = NULL;
2077 msg->data_size = 0;
2079 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2081 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2082 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2083 if (message == WM_HOTKEY)
2085 set_queue_bits( thread->queue, QS_HOTKEY );
2086 thread->queue->hotkey_count++;
2089 release_object( thread );
2092 /* post a win event */
2093 void post_win_event( struct thread *thread, unsigned int event,
2094 user_handle_t win, unsigned int object_id,
2095 unsigned int child_id, client_ptr_t hook_proc,
2096 const WCHAR *module, data_size_t module_size,
2097 user_handle_t hook)
2099 struct message *msg;
2101 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2103 struct winevent_msg_data *data;
2105 msg->type = MSG_WINEVENT;
2106 msg->win = get_user_full_handle( win );
2107 msg->msg = event;
2108 msg->wparam = object_id;
2109 msg->lparam = child_id;
2110 msg->time = get_tick_count();
2111 msg->result = NULL;
2113 if ((data = malloc( sizeof(*data) + module_size )))
2115 data->hook = hook;
2116 data->tid = get_thread_id( current );
2117 data->hook_proc = hook_proc;
2118 memcpy( data + 1, module, module_size );
2120 msg->data = data;
2121 msg->data_size = sizeof(*data) + module_size;
2123 if (debug_level > 1)
2124 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2125 get_thread_id(thread), event, win, object_id, child_id );
2126 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2127 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2129 else
2130 free( msg );
2134 /* free all hotkeys on a desktop, optionally filtering by window */
2135 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2137 struct hotkey *hotkey, *hotkey2;
2139 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2141 if (!window || hotkey->win == window)
2143 list_remove( &hotkey->entry );
2144 free( hotkey );
2150 /* check if the thread owning the window is hung */
2151 DECL_HANDLER(is_window_hung)
2153 struct thread *thread;
2155 thread = get_window_thread( req->win );
2156 if (thread)
2158 reply->is_hung = is_queue_hung( thread->queue );
2159 release_object( thread );
2161 else reply->is_hung = 0;
2165 /* get the message queue of the current thread */
2166 DECL_HANDLER(get_msg_queue)
2168 struct msg_queue *queue = get_current_queue();
2170 reply->handle = 0;
2171 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2175 /* set the file descriptor associated to the current thread queue */
2176 DECL_HANDLER(set_queue_fd)
2178 struct msg_queue *queue = get_current_queue();
2179 struct file *file;
2180 int unix_fd;
2182 if (queue->fd) /* fd can only be set once */
2184 set_error( STATUS_ACCESS_DENIED );
2185 return;
2187 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2189 if ((unix_fd = get_file_unix_fd( file )) != -1)
2191 if ((unix_fd = dup( unix_fd )) != -1)
2192 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2193 else
2194 file_set_error();
2196 release_object( file );
2200 /* set the current message queue wakeup mask */
2201 DECL_HANDLER(set_queue_mask)
2203 struct msg_queue *queue = get_current_queue();
2205 if (queue)
2207 queue->wake_mask = req->wake_mask;
2208 queue->changed_mask = req->changed_mask;
2209 reply->wake_bits = queue->wake_bits;
2210 reply->changed_bits = queue->changed_bits;
2211 if (is_signaled( queue ))
2213 /* if skip wait is set, do what would have been done in the subsequent wait */
2214 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2215 else wake_up( &queue->obj, 0 );
2221 /* get the current message queue status */
2222 DECL_HANDLER(get_queue_status)
2224 struct msg_queue *queue = current->queue;
2225 if (queue)
2227 reply->wake_bits = queue->wake_bits;
2228 reply->changed_bits = queue->changed_bits;
2229 queue->changed_bits &= ~req->clear_bits;
2231 else reply->wake_bits = reply->changed_bits = 0;
2235 /* send a message to a thread queue */
2236 DECL_HANDLER(send_message)
2238 struct message *msg;
2239 struct msg_queue *send_queue = get_current_queue();
2240 struct msg_queue *recv_queue = NULL;
2241 struct thread *thread = NULL;
2243 if (!(thread = get_thread_from_id( req->id ))) return;
2245 if (!(recv_queue = thread->queue))
2247 set_error( STATUS_INVALID_PARAMETER );
2248 release_object( thread );
2249 return;
2251 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2253 set_error( STATUS_TIMEOUT );
2254 release_object( thread );
2255 return;
2258 if ((msg = mem_alloc( sizeof(*msg) )))
2260 msg->type = req->type;
2261 msg->win = get_user_full_handle( req->win );
2262 msg->msg = req->msg;
2263 msg->wparam = req->wparam;
2264 msg->lparam = req->lparam;
2265 msg->result = NULL;
2266 msg->data = NULL;
2267 msg->data_size = get_req_data_size();
2269 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2271 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2273 free( msg );
2274 release_object( thread );
2275 return;
2278 switch(msg->type)
2280 case MSG_OTHER_PROCESS:
2281 case MSG_ASCII:
2282 case MSG_UNICODE:
2283 case MSG_CALLBACK:
2284 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2286 free_message( msg );
2287 break;
2289 /* fall through */
2290 case MSG_NOTIFY:
2291 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2292 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2293 break;
2294 case MSG_POSTED:
2295 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2296 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2297 if (msg->msg == WM_HOTKEY)
2299 set_queue_bits( recv_queue, QS_HOTKEY );
2300 recv_queue->hotkey_count++;
2302 break;
2303 case MSG_HARDWARE: /* should use send_hardware_message instead */
2304 case MSG_CALLBACK_RESULT: /* cannot send this one */
2305 case MSG_HOOK_LL: /* generated internally */
2306 default:
2307 set_error( STATUS_INVALID_PARAMETER );
2308 free( msg );
2309 break;
2312 release_object( thread );
2315 /* send a hardware message to a thread queue */
2316 DECL_HANDLER(send_hardware_message)
2318 struct thread *thread = NULL;
2319 struct desktop *desktop;
2320 struct msg_queue *sender = get_current_queue();
2321 data_size_t size = min( 256, get_reply_max_size() );
2323 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2325 if (req->win)
2327 if (!(thread = get_window_thread( req->win ))) return;
2328 if (desktop != thread->queue->input->desktop)
2330 /* don't allow queuing events to a different desktop */
2331 release_object( desktop );
2332 return;
2336 reply->prev_x = desktop->cursor.x;
2337 reply->prev_y = desktop->cursor.y;
2339 switch (req->input.type)
2341 case INPUT_MOUSE:
2342 reply->wait = queue_mouse_message( desktop, req->win, &req->input, req->flags, sender );
2343 break;
2344 case INPUT_KEYBOARD:
2345 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, req->flags, sender );
2346 break;
2347 case INPUT_HARDWARE:
2348 queue_custom_hardware_message( desktop, req->win, &req->input );
2349 break;
2350 default:
2351 set_error( STATUS_INVALID_PARAMETER );
2353 if (thread) release_object( thread );
2355 reply->new_x = desktop->cursor.x;
2356 reply->new_y = desktop->cursor.y;
2357 set_reply_data( desktop->keystate, size );
2358 release_object( desktop );
2361 /* post a quit message to the current queue */
2362 DECL_HANDLER(post_quit_message)
2364 struct msg_queue *queue = get_current_queue();
2366 if (!queue)
2367 return;
2369 queue->quit_message = 1;
2370 queue->exit_code = req->exit_code;
2371 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2374 /* get a message from the current queue */
2375 DECL_HANDLER(get_message)
2377 struct timer *timer;
2378 struct list *ptr;
2379 struct msg_queue *queue = get_current_queue();
2380 user_handle_t get_win = get_user_full_handle( req->get_win );
2381 unsigned int filter = req->flags >> 16;
2383 reply->active_hooks = get_active_hooks();
2385 if (!queue) return;
2386 queue->last_get_msg = current_time;
2387 if (!filter) filter = QS_ALLINPUT;
2389 /* first check for sent messages */
2390 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2392 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2393 receive_message( queue, msg, reply );
2394 return;
2397 /* clear changed bits so we can wait on them if we don't find a message */
2398 if (filter & QS_POSTMESSAGE)
2400 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2401 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2403 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2404 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2406 /* then check for posted messages */
2407 if ((filter & QS_POSTMESSAGE) &&
2408 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2409 return;
2411 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2412 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2413 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2414 return;
2416 /* only check for quit messages if not posted messages pending */
2417 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2418 return;
2420 /* then check for any raw hardware message */
2421 if ((filter & QS_INPUT) &&
2422 filter_contains_hw_range( req->get_first, req->get_last ) &&
2423 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2424 return;
2426 /* now check for WM_PAINT */
2427 if ((filter & QS_PAINT) &&
2428 queue->paint_count &&
2429 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2430 (reply->win = find_window_to_repaint( get_win, current )))
2432 reply->type = MSG_POSTED;
2433 reply->msg = WM_PAINT;
2434 reply->wparam = 0;
2435 reply->lparam = 0;
2436 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2437 return;
2440 /* now check for timer */
2441 if ((filter & QS_TIMER) &&
2442 (timer = find_expired_timer( queue, get_win, req->get_first,
2443 req->get_last, (req->flags & PM_REMOVE) )))
2445 reply->type = MSG_POSTED;
2446 reply->win = timer->win;
2447 reply->msg = timer->msg;
2448 reply->wparam = timer->id;
2449 reply->lparam = timer->lparam;
2450 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2451 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2452 set_event( current->process->idle_event );
2453 return;
2456 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2457 queue->wake_mask = req->wake_mask;
2458 queue->changed_mask = req->changed_mask;
2459 set_error( STATUS_PENDING ); /* FIXME */
2463 /* reply to a sent message */
2464 DECL_HANDLER(reply_message)
2466 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2467 else if (current->queue->recv_result)
2468 reply_message( current->queue, req->result, 0, req->remove,
2469 get_req_data(), get_req_data_size() );
2473 /* accept the current hardware message */
2474 DECL_HANDLER(accept_hardware_message)
2476 if (current->queue)
2477 release_hardware_message( current->queue, req->hw_id, req->remove );
2478 else
2479 set_error( STATUS_ACCESS_DENIED );
2483 /* retrieve the reply for the last message sent */
2484 DECL_HANDLER(get_message_reply)
2486 struct message_result *result;
2487 struct list *entry;
2488 struct msg_queue *queue = current->queue;
2490 if (queue)
2492 set_error( STATUS_PENDING );
2493 reply->result = 0;
2495 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2497 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2498 if (result->replied || req->cancel)
2500 if (result->replied)
2502 reply->result = result->result;
2503 set_error( result->error );
2504 if (result->data)
2506 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2507 set_reply_data_ptr( result->data, data_len );
2508 result->data = NULL;
2509 result->data_size = 0;
2512 remove_result_from_sender( result );
2514 entry = list_head( &queue->send_result );
2515 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2516 else
2518 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2519 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2520 else clear_queue_bits( queue, QS_SMRESULT );
2524 else set_error( STATUS_ACCESS_DENIED );
2528 /* set a window timer */
2529 DECL_HANDLER(set_win_timer)
2531 struct timer *timer;
2532 struct msg_queue *queue;
2533 struct thread *thread = NULL;
2534 user_handle_t win = 0;
2535 lparam_t id = req->id;
2537 if (req->win)
2539 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2541 set_error( STATUS_INVALID_HANDLE );
2542 return;
2544 if (thread->process != current->process)
2546 release_object( thread );
2547 set_error( STATUS_ACCESS_DENIED );
2548 return;
2550 queue = thread->queue;
2551 /* remove it if it existed already */
2552 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2554 else
2556 queue = get_current_queue();
2557 /* look for a timer with this id */
2558 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2560 /* free and reuse id */
2561 free_timer( queue, timer );
2563 else
2565 /* find a free id for it */
2568 id = queue->next_timer_id;
2569 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2571 while (find_timer( queue, 0, req->msg, id ));
2575 if ((timer = set_timer( queue, req->rate )))
2577 timer->win = win;
2578 timer->msg = req->msg;
2579 timer->id = id;
2580 timer->lparam = req->lparam;
2581 reply->id = id;
2583 if (thread) release_object( thread );
2586 /* kill a window timer */
2587 DECL_HANDLER(kill_win_timer)
2589 struct timer *timer;
2590 struct thread *thread;
2591 user_handle_t win = 0;
2593 if (req->win)
2595 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2597 set_error( STATUS_INVALID_HANDLE );
2598 return;
2600 if (thread->process != current->process)
2602 release_object( thread );
2603 set_error( STATUS_ACCESS_DENIED );
2604 return;
2607 else thread = (struct thread *)grab_object( current );
2609 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2610 free_timer( thread->queue, timer );
2611 else
2612 set_error( STATUS_INVALID_PARAMETER );
2614 release_object( thread );
2617 DECL_HANDLER(register_hotkey)
2619 struct desktop *desktop;
2620 user_handle_t win_handle = req->window;
2621 struct hotkey *hotkey;
2622 struct hotkey *new_hotkey = NULL;
2623 struct thread *thread;
2624 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2626 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2628 if (win_handle)
2630 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2632 release_object( desktop );
2633 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2634 return;
2637 thread = get_window_thread( win_handle );
2638 if (thread) release_object( thread );
2640 if (thread != current)
2642 release_object( desktop );
2643 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2644 return;
2648 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2650 if (req->vkey == hotkey->vkey &&
2651 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2653 release_object( desktop );
2654 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2655 return;
2657 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2658 new_hotkey = hotkey;
2661 if (new_hotkey)
2663 reply->replaced = 1;
2664 reply->flags = new_hotkey->flags;
2665 reply->vkey = new_hotkey->vkey;
2667 else
2669 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2670 if (new_hotkey)
2672 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2673 new_hotkey->queue = current->queue;
2674 new_hotkey->win = win_handle;
2675 new_hotkey->id = req->id;
2679 if (new_hotkey)
2681 new_hotkey->flags = req->flags;
2682 new_hotkey->vkey = req->vkey;
2685 release_object( desktop );
2688 DECL_HANDLER(unregister_hotkey)
2690 struct desktop *desktop;
2691 user_handle_t win_handle = req->window;
2692 struct hotkey *hotkey;
2693 struct thread *thread;
2695 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2697 if (win_handle)
2699 if (!get_user_object_handle( &win_handle, USER_WINDOW ))
2701 release_object( desktop );
2702 set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2703 return;
2706 thread = get_window_thread( win_handle );
2707 if (thread) release_object( thread );
2709 if (thread != current)
2711 release_object( desktop );
2712 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2713 return;
2717 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2719 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2720 goto found;
2723 release_object( desktop );
2724 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2725 return;
2727 found:
2728 reply->flags = hotkey->flags;
2729 reply->vkey = hotkey->vkey;
2730 list_remove( &hotkey->entry );
2731 free( hotkey );
2732 release_object( desktop );
2735 /* attach (or detach) thread inputs */
2736 DECL_HANDLER(attach_thread_input)
2738 struct thread *thread_from = get_thread_from_id( req->tid_from );
2739 struct thread *thread_to = get_thread_from_id( req->tid_to );
2741 if (!thread_from || !thread_to)
2743 if (thread_from) release_object( thread_from );
2744 if (thread_to) release_object( thread_to );
2745 return;
2747 if (thread_from != thread_to)
2749 if (req->attach) attach_thread_input( thread_from, thread_to );
2750 else
2752 if (thread_from->queue && thread_to->queue &&
2753 thread_from->queue->input == thread_to->queue->input)
2754 detach_thread_input( thread_from );
2755 else
2756 set_error( STATUS_ACCESS_DENIED );
2759 else set_error( STATUS_ACCESS_DENIED );
2760 release_object( thread_from );
2761 release_object( thread_to );
2765 /* get thread input data */
2766 DECL_HANDLER(get_thread_input)
2768 struct thread *thread = NULL;
2769 struct desktop *desktop;
2770 struct thread_input *input;
2772 if (req->tid)
2774 if (!(thread = get_thread_from_id( req->tid ))) return;
2775 if (!(desktop = get_thread_desktop( thread, 0 )))
2777 release_object( thread );
2778 return;
2780 input = thread->queue ? thread->queue->input : NULL;
2782 else
2784 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2785 input = desktop->foreground_input; /* get the foreground thread info */
2788 if (input)
2790 reply->focus = input->focus;
2791 reply->capture = input->capture;
2792 reply->active = input->active;
2793 reply->menu_owner = input->menu_owner;
2794 reply->move_size = input->move_size;
2795 reply->caret = input->caret;
2796 reply->cursor = input->cursor;
2797 reply->show_count = input->cursor_count;
2798 reply->rect = input->caret_rect;
2801 /* foreground window is active window of foreground thread */
2802 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2803 if (thread) release_object( thread );
2804 release_object( desktop );
2808 /* retrieve queue keyboard state for a given thread */
2809 DECL_HANDLER(get_key_state)
2811 struct thread *thread;
2812 struct desktop *desktop;
2813 data_size_t size = min( 256, get_reply_max_size() );
2815 if (!req->tid) /* get global async key state */
2817 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2818 if (req->key >= 0)
2820 reply->state = desktop->keystate[req->key & 0xff];
2821 desktop->keystate[req->key & 0xff] &= ~0x40;
2823 set_reply_data( desktop->keystate, size );
2824 release_object( desktop );
2826 else
2828 if (!(thread = get_thread_from_id( req->tid ))) return;
2829 if (thread->queue)
2831 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2832 set_reply_data( thread->queue->input->keystate, size );
2834 release_object( thread );
2839 /* set queue keyboard state for a given thread */
2840 DECL_HANDLER(set_key_state)
2842 struct thread *thread;
2843 struct desktop *desktop;
2844 data_size_t size = min( 256, get_req_data_size() );
2846 if (!req->tid) /* set global async key state */
2848 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2849 memcpy( desktop->keystate, get_req_data(), size );
2850 release_object( desktop );
2852 else
2854 if (!(thread = get_thread_from_id( req->tid ))) return;
2855 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
2856 if (req->async && (desktop = get_thread_desktop( thread, 0 )))
2858 memcpy( desktop->keystate, get_req_data(), size );
2859 release_object( desktop );
2861 release_object( thread );
2866 /* set the system foreground window */
2867 DECL_HANDLER(set_foreground_window)
2869 struct thread *thread = NULL;
2870 struct desktop *desktop;
2871 struct msg_queue *queue = get_current_queue();
2873 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2874 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
2875 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
2876 reply->send_msg_new = FALSE;
2878 if (is_valid_foreground_window( req->handle ) &&
2879 (thread = get_window_thread( req->handle )) &&
2880 thread->queue->input->desktop == desktop)
2882 set_foreground_input( desktop, thread->queue->input );
2883 reply->send_msg_new = (desktop->foreground_input != queue->input);
2885 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2887 if (thread) release_object( thread );
2888 release_object( desktop );
2892 /* set the current thread focus window */
2893 DECL_HANDLER(set_focus_window)
2895 struct msg_queue *queue = get_current_queue();
2897 reply->previous = 0;
2898 if (queue && check_queue_input_window( queue, req->handle ))
2900 reply->previous = queue->input->focus;
2901 queue->input->focus = get_user_full_handle( req->handle );
2906 /* set the current thread active window */
2907 DECL_HANDLER(set_active_window)
2909 struct msg_queue *queue = get_current_queue();
2911 reply->previous = 0;
2912 if (queue && check_queue_input_window( queue, req->handle ))
2914 if (!req->handle || make_window_active( req->handle ))
2916 reply->previous = queue->input->active;
2917 queue->input->active = get_user_full_handle( req->handle );
2919 else set_error( STATUS_INVALID_HANDLE );
2924 /* set the current thread capture window */
2925 DECL_HANDLER(set_capture_window)
2927 struct msg_queue *queue = get_current_queue();
2929 reply->previous = reply->full_handle = 0;
2930 if (queue && check_queue_input_window( queue, req->handle ))
2932 struct thread_input *input = queue->input;
2934 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2935 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2937 set_error(STATUS_ACCESS_DENIED);
2938 return;
2940 reply->previous = input->capture;
2941 input->capture = get_user_full_handle( req->handle );
2942 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2943 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2944 reply->full_handle = input->capture;
2949 /* Set the current thread caret window */
2950 DECL_HANDLER(set_caret_window)
2952 struct msg_queue *queue = get_current_queue();
2954 reply->previous = 0;
2955 if (queue && check_queue_input_window( queue, req->handle ))
2957 struct thread_input *input = queue->input;
2959 reply->previous = input->caret;
2960 reply->old_rect = input->caret_rect;
2961 reply->old_hide = input->caret_hide;
2962 reply->old_state = input->caret_state;
2964 set_caret_window( input, get_user_full_handle(req->handle) );
2965 input->caret_rect.right = input->caret_rect.left + req->width;
2966 input->caret_rect.bottom = input->caret_rect.top + req->height;
2971 /* Set the current thread caret information */
2972 DECL_HANDLER(set_caret_info)
2974 struct msg_queue *queue = get_current_queue();
2975 struct thread_input *input;
2977 if (!queue) return;
2978 input = queue->input;
2979 reply->full_handle = input->caret;
2980 reply->old_rect = input->caret_rect;
2981 reply->old_hide = input->caret_hide;
2982 reply->old_state = input->caret_state;
2984 if (req->handle && get_user_full_handle(req->handle) != input->caret)
2986 set_error( STATUS_ACCESS_DENIED );
2987 return;
2989 if (req->flags & SET_CARET_POS)
2991 input->caret_rect.right += req->x - input->caret_rect.left;
2992 input->caret_rect.bottom += req->y - input->caret_rect.top;
2993 input->caret_rect.left = req->x;
2994 input->caret_rect.top = req->y;
2996 if (req->flags & SET_CARET_HIDE)
2998 input->caret_hide += req->hide;
2999 if (input->caret_hide < 0) input->caret_hide = 0;
3001 if (req->flags & SET_CARET_STATE)
3003 if (req->state == -1) input->caret_state = !input->caret_state;
3004 else input->caret_state = !!req->state;
3009 /* get the time of the last input event */
3010 DECL_HANDLER(get_last_input_time)
3012 reply->time = last_input_time;
3015 /* set/get the current cursor */
3016 DECL_HANDLER(set_cursor)
3018 struct msg_queue *queue = get_current_queue();
3019 struct thread_input *input;
3021 if (!queue) return;
3022 input = queue->input;
3024 reply->prev_handle = input->cursor;
3025 reply->prev_count = input->cursor_count;
3026 reply->prev_x = input->desktop->cursor.x;
3027 reply->prev_y = input->desktop->cursor.y;
3029 if (req->flags & SET_CURSOR_HANDLE)
3031 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3033 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3034 return;
3036 input->cursor = req->handle;
3038 if (req->flags & SET_CURSOR_COUNT)
3040 queue->cursor_count += req->show_count;
3041 input->cursor_count += req->show_count;
3043 if (req->flags & SET_CURSOR_POS)
3045 set_cursor_pos( input->desktop, req->x, req->y );
3047 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3049 struct desktop *desktop = input->desktop;
3051 /* only the desktop owner can set the message */
3052 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3053 desktop->cursor.clip_msg = req->clip_msg;
3055 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip );
3058 reply->new_x = input->desktop->cursor.x;
3059 reply->new_y = input->desktop->cursor.y;
3060 reply->new_clip = input->desktop->cursor.clip;
3061 reply->last_change = input->desktop->cursor.last_change;
3064 DECL_HANDLER(update_rawinput_devices)
3066 const struct rawinput_device *devices = get_req_data();
3067 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3068 const struct rawinput_device_entry *e;
3069 unsigned int i;
3071 for (i = 0; i < device_count; ++i)
3073 update_rawinput_device(&devices[i]);
3076 e = find_rawinput_device( 1, 2 );
3077 current->process->rawinput_mouse = e ? &e->device : NULL;
3078 e = find_rawinput_device( 1, 6 );
3079 current->process->rawinput_kbd = e ? &e->device : NULL;