widl: Use for_each_iface in process_tfs.
[wine.git] / server / queue.c
blob9dfbd1e294cdf8d1317f7bc46a0cd5c6d68f92e7
1 /*
2 * Server-side message queues
4 * Copyright (C) 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #ifdef HAVE_POLL_H
29 # include <poll.h>
30 #endif
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "winternl.h"
40 #include "handle.h"
41 #include "file.h"
42 #include "thread.h"
43 #include "process.h"
44 #include "request.h"
45 #include "user.h"
47 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
48 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
50 enum message_kind { SEND_MESSAGE, POST_MESSAGE };
51 #define NB_MSG_KINDS (POST_MESSAGE+1)
54 struct message_result
56 struct list sender_entry; /* entry in sender list */
57 struct message *msg; /* message the result is for */
58 struct message_result *recv_next; /* next in receiver list */
59 struct msg_queue *sender; /* sender queue */
60 struct msg_queue *receiver; /* receiver queue */
61 int replied; /* has it been replied to? */
62 unsigned int error; /* error code to pass back to sender */
63 lparam_t result; /* reply result */
64 struct message *hardware_msg; /* hardware message if low-level hook result */
65 struct desktop *desktop; /* desktop for hardware message */
66 struct message *callback_msg; /* message to queue for callback */
67 void *data; /* message reply data */
68 unsigned int data_size; /* size of message reply data */
69 struct timeout_user *timeout; /* result timeout */
72 struct message
74 struct list entry; /* entry in message list */
75 enum message_type type; /* message type */
76 user_handle_t win; /* window handle */
77 unsigned int msg; /* message code */
78 lparam_t wparam; /* parameters */
79 lparam_t lparam; /* parameters */
80 int x; /* message position */
81 int y;
82 unsigned int time; /* message time */
83 void *data; /* message data for sent messages */
84 unsigned int data_size; /* size of message data */
85 unsigned int unique_id; /* unique id for nested hw message waits */
86 struct message_result *result; /* result in sender queue */
89 struct timer
91 struct list entry; /* entry in timer list */
92 timeout_t when; /* next expiration */
93 unsigned int rate; /* timer rate in ms */
94 user_handle_t win; /* window handle */
95 unsigned int msg; /* message to post */
96 lparam_t id; /* timer id */
97 lparam_t lparam; /* lparam for message */
100 struct thread_input
102 struct object obj; /* object header */
103 struct desktop *desktop; /* desktop that this thread input belongs to */
104 user_handle_t focus; /* focus window */
105 user_handle_t capture; /* capture window */
106 user_handle_t active; /* active window */
107 user_handle_t menu_owner; /* current menu owner window */
108 user_handle_t move_size; /* current moving/resizing window */
109 user_handle_t caret; /* caret window */
110 rectangle_t caret_rect; /* caret rectangle */
111 int caret_hide; /* caret hide count */
112 int caret_state; /* caret on/off state */
113 user_handle_t cursor; /* current cursor */
114 int cursor_count; /* cursor show count */
115 struct list msg_list; /* list of hardware messages */
116 unsigned char keystate[256]; /* state of each key */
119 struct msg_queue
121 struct object obj; /* object header */
122 struct fd *fd; /* optional file descriptor to poll */
123 unsigned int wake_bits; /* wakeup bits */
124 unsigned int wake_mask; /* wakeup mask */
125 unsigned int changed_bits; /* changed wakeup bits */
126 unsigned int changed_mask; /* changed wakeup mask */
127 int paint_count; /* pending paint messages count */
128 int hotkey_count; /* pending hotkey messages count */
129 int quit_message; /* is there a pending quit message? */
130 int exit_code; /* exit code of pending quit message */
131 int cursor_count; /* per-queue cursor show count */
132 struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
133 struct list send_result; /* stack of sent messages waiting for result */
134 struct list callback_result; /* list of callback messages waiting for result */
135 struct message_result *recv_result; /* stack of received messages waiting for result */
136 struct list pending_timers; /* list of pending timers */
137 struct list expired_timers; /* list of expired timers */
138 lparam_t next_timer_id; /* id for the next timer with a 0 window */
139 struct timeout_user *timeout; /* timeout for next timer to expire */
140 struct thread_input *input; /* thread input descriptor */
141 struct hook_table *hooks; /* hook table */
142 timeout_t last_get_msg; /* time of last get message call */
145 struct hotkey
147 struct list entry; /* entry in desktop hotkey list */
148 struct msg_queue *queue; /* queue owning this hotkey */
149 user_handle_t win; /* window handle */
150 int id; /* hotkey id */
151 unsigned int vkey; /* virtual key code */
152 unsigned int flags; /* key modifiers */
155 static void msg_queue_dump( struct object *obj, int verbose );
156 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
157 static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
158 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
159 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
160 static void msg_queue_destroy( struct object *obj );
161 static void msg_queue_poll_event( struct fd *fd, int event );
162 static void thread_input_dump( struct object *obj, int verbose );
163 static void thread_input_destroy( struct object *obj );
164 static void timer_callback( void *private );
166 static const struct object_ops msg_queue_ops =
168 sizeof(struct msg_queue), /* size */
169 msg_queue_dump, /* dump */
170 no_get_type, /* get_type */
171 msg_queue_add_queue, /* add_queue */
172 msg_queue_remove_queue, /* remove_queue */
173 msg_queue_signaled, /* signaled */
174 msg_queue_satisfied, /* satisfied */
175 no_signal, /* signal */
176 no_get_fd, /* get_fd */
177 no_map_access, /* map_access */
178 default_get_sd, /* get_sd */
179 default_set_sd, /* set_sd */
180 no_lookup_name, /* lookup_name */
181 no_link_name, /* link_name */
182 NULL, /* unlink_name */
183 no_open_file, /* open_file */
184 no_close_handle, /* close_handle */
185 msg_queue_destroy /* destroy */
188 static const struct fd_ops msg_queue_fd_ops =
190 NULL, /* get_poll_events */
191 msg_queue_poll_event, /* poll_event */
192 NULL, /* flush */
193 NULL, /* get_fd_type */
194 NULL, /* ioctl */
195 NULL, /* queue_async */
196 NULL, /* reselect_async */
197 NULL /* cancel async */
201 static const struct object_ops thread_input_ops =
203 sizeof(struct thread_input), /* size */
204 thread_input_dump, /* dump */
205 no_get_type, /* get_type */
206 no_add_queue, /* add_queue */
207 NULL, /* remove_queue */
208 NULL, /* signaled */
209 NULL, /* satisfied */
210 no_signal, /* signal */
211 no_get_fd, /* get_fd */
212 no_map_access, /* map_access */
213 default_get_sd, /* get_sd */
214 default_set_sd, /* set_sd */
215 no_lookup_name, /* lookup_name */
216 no_link_name, /* link_name */
217 NULL, /* unlink_name */
218 no_open_file, /* open_file */
219 no_close_handle, /* close_handle */
220 thread_input_destroy /* destroy */
223 /* pointer to input structure of foreground thread */
224 static unsigned int last_input_time;
226 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
227 static void free_message( struct message *msg );
229 /* set the caret window in a given thread input */
230 static void set_caret_window( struct thread_input *input, user_handle_t win )
232 if (!win || win != input->caret)
234 input->caret_rect.left = 0;
235 input->caret_rect.top = 0;
236 input->caret_rect.right = 0;
237 input->caret_rect.bottom = 0;
239 input->caret = win;
240 input->caret_hide = 1;
241 input->caret_state = 0;
244 /* create a thread input object */
245 static struct thread_input *create_thread_input( struct thread *thread )
247 struct thread_input *input;
249 if ((input = alloc_object( &thread_input_ops )))
251 input->focus = 0;
252 input->capture = 0;
253 input->active = 0;
254 input->menu_owner = 0;
255 input->move_size = 0;
256 input->cursor = 0;
257 input->cursor_count = 0;
258 list_init( &input->msg_list );
259 set_caret_window( input, 0 );
260 memset( input->keystate, 0, sizeof(input->keystate) );
262 if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
264 release_object( input );
265 return NULL;
268 return input;
271 /* create a message queue object */
272 static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
274 struct thread_input *new_input = NULL;
275 struct msg_queue *queue;
276 int i;
278 if (!input)
280 if (!(new_input = create_thread_input( thread ))) return NULL;
281 input = new_input;
284 if ((queue = alloc_object( &msg_queue_ops )))
286 queue->fd = NULL;
287 queue->wake_bits = 0;
288 queue->wake_mask = 0;
289 queue->changed_bits = 0;
290 queue->changed_mask = 0;
291 queue->paint_count = 0;
292 queue->hotkey_count = 0;
293 queue->quit_message = 0;
294 queue->cursor_count = 0;
295 queue->recv_result = NULL;
296 queue->next_timer_id = 0x7fff;
297 queue->timeout = NULL;
298 queue->input = (struct thread_input *)grab_object( input );
299 queue->hooks = NULL;
300 queue->last_get_msg = current_time;
301 list_init( &queue->send_result );
302 list_init( &queue->callback_result );
303 list_init( &queue->pending_timers );
304 list_init( &queue->expired_timers );
305 for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
307 thread->queue = queue;
309 if (new_input) release_object( new_input );
310 return queue;
313 /* free the message queue of a thread at thread exit */
314 void free_msg_queue( struct thread *thread )
316 remove_thread_hooks( thread );
317 if (!thread->queue) return;
318 release_object( thread->queue );
319 thread->queue = NULL;
322 /* change the thread input data of a given thread */
323 static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
325 struct msg_queue *queue = thread->queue;
327 if (!queue)
329 thread->queue = create_msg_queue( thread, new_input );
330 return thread->queue != NULL;
332 if (queue->input)
334 queue->input->cursor_count -= queue->cursor_count;
335 release_object( queue->input );
337 queue->input = (struct thread_input *)grab_object( new_input );
338 new_input->cursor_count += queue->cursor_count;
339 return 1;
342 /* allocate a hardware message and its data */
343 static struct message *alloc_hardware_message( lparam_t info, struct hw_msg_source source,
344 unsigned int time )
346 struct hardware_msg_data *msg_data;
347 struct message *msg;
349 if (!(msg = mem_alloc( sizeof(*msg) ))) return NULL;
350 if (!(msg_data = mem_alloc( sizeof(*msg_data) )))
352 free( msg );
353 return NULL;
355 memset( msg, 0, sizeof(*msg) );
356 msg->type = MSG_HARDWARE;
357 msg->time = time;
358 msg->data = msg_data;
359 msg->data_size = sizeof(*msg_data);
361 memset( msg_data, 0, sizeof(*msg_data) );
362 msg_data->info = info;
363 msg_data->source = source;
364 return msg;
367 /* set the cursor position and queue the corresponding mouse message */
368 static void set_cursor_pos( struct desktop *desktop, int x, int y )
370 static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
371 struct message *msg;
373 if (!(msg = alloc_hardware_message( 0, source, get_tick_count() ))) return;
375 msg->msg = WM_MOUSEMOVE;
376 msg->x = x;
377 msg->y = y;
378 queue_hardware_message( desktop, msg, 1 );
381 /* retrieve default position and time for synthesized messages */
382 static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
384 struct desktop *desktop = queue->input->desktop;
386 *x = desktop->cursor.x;
387 *y = desktop->cursor.y;
388 *time = get_tick_count();
391 /* set the cursor clip rectangle */
392 static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, int send_clip_msg )
394 rectangle_t top_rect;
395 int x, y;
397 get_top_window_rectangle( desktop, &top_rect );
398 if (rect)
400 rectangle_t new_rect = *rect;
401 if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
402 if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
403 if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
404 if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
405 if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
406 desktop->cursor.clip = new_rect;
408 else desktop->cursor.clip = top_rect;
410 if (desktop->cursor.clip_msg && send_clip_msg)
411 post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
413 /* warp the mouse to be inside the clip rect */
414 x = min( max( desktop->cursor.x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
415 y = min( max( desktop->cursor.y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
416 if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
419 /* change the foreground input and reset the cursor clip rect */
420 static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
422 if (desktop->foreground_input == input) return;
423 set_clip_rectangle( desktop, NULL, 1 );
424 desktop->foreground_input = input;
427 /* get the hook table for a given thread */
428 struct hook_table *get_queue_hooks( struct thread *thread )
430 if (!thread->queue) return NULL;
431 return thread->queue->hooks;
434 /* set the hook table for a given thread, allocating the queue if needed */
435 void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
437 struct msg_queue *queue = thread->queue;
438 if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
439 if (queue->hooks) release_object( queue->hooks );
440 queue->hooks = hooks;
443 /* check the queue status */
444 static inline int is_signaled( struct msg_queue *queue )
446 return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
449 /* set some queue bits */
450 static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
452 queue->wake_bits |= bits;
453 queue->changed_bits |= bits;
454 if (is_signaled( queue )) wake_up( &queue->obj, 0 );
457 /* clear some queue bits */
458 static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
460 queue->wake_bits &= ~bits;
461 queue->changed_bits &= ~bits;
464 /* check whether msg is a keyboard message */
465 static inline int is_keyboard_msg( struct message *msg )
467 return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
470 /* check if message is matched by the filter */
471 static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
473 return (msg >= first && msg <= last);
476 /* check whether a message filter contains at least one potential hardware message */
477 static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
479 /* hardware message ranges are (in numerical order):
480 * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
481 * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
482 * WM_MOUSEFIRST .. WM_MOUSELAST
484 if (last < WM_NCMOUSEFIRST) return 0;
485 if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
486 if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
487 if (first > WM_MOUSELAST) return 0;
488 return 1;
491 /* get the QS_* bit corresponding to a given hardware message */
492 static inline int get_hardware_msg_bit( struct message *msg )
494 if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
495 if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
496 if (is_keyboard_msg( msg )) return QS_KEY;
497 return QS_MOUSEBUTTON;
500 /* get the current thread queue, creating it if needed */
501 static inline struct msg_queue *get_current_queue(void)
503 struct msg_queue *queue = current->queue;
504 if (!queue) queue = create_msg_queue( current, NULL );
505 return queue;
508 /* get a (pseudo-)unique id to tag hardware messages */
509 static inline unsigned int get_unique_id(void)
511 static unsigned int id;
512 if (!++id) id = 1; /* avoid an id of 0 */
513 return id;
516 /* try to merge a message with the last in the list; return 1 if successful */
517 static int merge_message( struct thread_input *input, const struct message *msg )
519 struct message *prev;
520 struct list *ptr;
522 if (msg->msg != WM_MOUSEMOVE) return 0;
523 for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
525 prev = LIST_ENTRY( ptr, struct message, entry );
526 if (prev->msg != WM_INPUT) break;
528 if (!ptr) return 0;
529 if (prev->result) return 0;
530 if (prev->win && msg->win && prev->win != msg->win) return 0;
531 if (prev->msg != msg->msg) return 0;
532 if (prev->type != msg->type) return 0;
533 /* now we can merge it */
534 prev->wparam = msg->wparam;
535 prev->lparam = msg->lparam;
536 prev->x = msg->x;
537 prev->y = msg->y;
538 prev->time = msg->time;
539 if (msg->type == MSG_HARDWARE && prev->data && msg->data)
541 struct hardware_msg_data *prev_data = prev->data;
542 struct hardware_msg_data *msg_data = msg->data;
543 prev_data->info = msg_data->info;
545 list_remove( ptr );
546 list_add_tail( &input->msg_list, ptr );
547 return 1;
550 /* free a result structure */
551 static void free_result( struct message_result *result )
553 if (result->timeout) remove_timeout_user( result->timeout );
554 free( result->data );
555 if (result->callback_msg) free_message( result->callback_msg );
556 if (result->hardware_msg) free_message( result->hardware_msg );
557 if (result->desktop) release_object( result->desktop );
558 free( result );
561 /* remove the result from the sender list it is on */
562 static inline void remove_result_from_sender( struct message_result *result )
564 assert( result->sender );
566 list_remove( &result->sender_entry );
567 result->sender = NULL;
568 if (!result->receiver) free_result( result );
571 /* store the message result in the appropriate structure */
572 static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
574 res->result = result;
575 res->error = error;
576 res->replied = 1;
577 if (res->timeout)
579 remove_timeout_user( res->timeout );
580 res->timeout = NULL;
583 if (res->hardware_msg)
585 if (!error && result) /* rejected by the hook */
586 free_message( res->hardware_msg );
587 else
588 queue_hardware_message( res->desktop, res->hardware_msg, 0 );
590 res->hardware_msg = NULL;
593 if (res->sender)
595 if (res->callback_msg)
597 /* queue the callback message in the sender queue */
598 struct callback_msg_data *data = res->callback_msg->data;
599 data->result = result;
600 list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
601 set_queue_bits( res->sender, QS_SENDMESSAGE );
602 res->callback_msg = NULL;
603 remove_result_from_sender( res );
605 else
607 /* wake sender queue if waiting on this result */
608 if (list_head(&res->sender->send_result) == &res->sender_entry)
609 set_queue_bits( res->sender, QS_SMRESULT );
612 else if (!res->receiver) free_result( res );
615 /* free a message when deleting a queue or window */
616 static void free_message( struct message *msg )
618 struct message_result *result = msg->result;
619 if (result)
621 result->msg = NULL;
622 result->receiver = NULL;
623 store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
625 free( msg->data );
626 free( msg );
629 /* remove (and free) a message from a message list */
630 static void remove_queue_message( struct msg_queue *queue, struct message *msg,
631 enum message_kind kind )
633 list_remove( &msg->entry );
634 switch(kind)
636 case SEND_MESSAGE:
637 if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
638 break;
639 case POST_MESSAGE:
640 if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
641 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
642 if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
643 clear_queue_bits( queue, QS_HOTKEY );
644 break;
646 free_message( msg );
649 /* message timed out without getting a reply */
650 static void result_timeout( void *private )
652 struct message_result *result = private;
654 assert( !result->replied );
656 result->timeout = NULL;
658 if (result->msg) /* not received yet */
660 struct message *msg = result->msg;
662 result->msg = NULL;
663 msg->result = NULL;
664 remove_queue_message( result->receiver, msg, SEND_MESSAGE );
665 result->receiver = NULL;
667 store_message_result( result, 0, STATUS_TIMEOUT );
670 /* allocate and fill a message result structure */
671 static struct message_result *alloc_message_result( struct msg_queue *send_queue,
672 struct msg_queue *recv_queue,
673 struct message *msg, timeout_t timeout )
675 struct message_result *result = mem_alloc( sizeof(*result) );
676 if (result)
678 result->msg = msg;
679 result->sender = send_queue;
680 result->receiver = recv_queue;
681 result->replied = 0;
682 result->data = NULL;
683 result->data_size = 0;
684 result->timeout = NULL;
685 result->hardware_msg = NULL;
686 result->desktop = NULL;
687 result->callback_msg = NULL;
689 if (msg->type == MSG_CALLBACK)
691 struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
693 if (!callback_msg)
695 free( result );
696 return NULL;
698 callback_msg->type = MSG_CALLBACK_RESULT;
699 callback_msg->win = msg->win;
700 callback_msg->msg = msg->msg;
701 callback_msg->wparam = 0;
702 callback_msg->lparam = 0;
703 callback_msg->time = get_tick_count();
704 callback_msg->result = NULL;
705 /* steal the data from the original message */
706 callback_msg->data = msg->data;
707 callback_msg->data_size = msg->data_size;
708 msg->data = NULL;
709 msg->data_size = 0;
711 result->callback_msg = callback_msg;
712 list_add_head( &send_queue->callback_result, &result->sender_entry );
714 else if (send_queue)
716 list_add_head( &send_queue->send_result, &result->sender_entry );
717 clear_queue_bits( send_queue, QS_SMRESULT );
720 if (timeout != TIMEOUT_INFINITE)
721 result->timeout = add_timeout_user( timeout, result_timeout, result );
723 return result;
726 /* receive a message, removing it from the sent queue */
727 static void receive_message( struct msg_queue *queue, struct message *msg,
728 struct get_message_reply *reply )
730 struct message_result *result = msg->result;
732 reply->total = msg->data_size;
733 if (msg->data_size > get_reply_max_size())
735 set_error( STATUS_BUFFER_OVERFLOW );
736 return;
738 reply->type = msg->type;
739 reply->win = msg->win;
740 reply->msg = msg->msg;
741 reply->wparam = msg->wparam;
742 reply->lparam = msg->lparam;
743 reply->x = msg->x;
744 reply->y = msg->y;
745 reply->time = msg->time;
747 if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
749 list_remove( &msg->entry );
750 /* put the result on the receiver result stack */
751 if (result)
753 result->msg = NULL;
754 result->recv_next = queue->recv_result;
755 queue->recv_result = result;
757 free( msg );
758 if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
761 /* set the result of the current received message */
762 static void reply_message( struct msg_queue *queue, lparam_t result,
763 unsigned int error, int remove, const void *data, data_size_t len )
765 struct message_result *res = queue->recv_result;
767 if (remove)
769 queue->recv_result = res->recv_next;
770 res->receiver = NULL;
771 if (!res->sender && !res->hardware_msg) /* no one waiting for it */
773 free_result( res );
774 return;
777 if (!res->replied)
779 if (len && (res->data = memdup( data, len ))) res->data_size = len;
780 store_message_result( res, result, error );
784 static int match_window( user_handle_t win, user_handle_t msg_win )
786 if (!win) return 1;
787 if (win == -1 || win == 1) return !msg_win;
788 if (msg_win == win) return 1;
789 return is_child_window( win, msg_win );
792 /* retrieve a posted message */
793 static int get_posted_message( struct msg_queue *queue, user_handle_t win,
794 unsigned int first, unsigned int last, unsigned int flags,
795 struct get_message_reply *reply )
797 struct message *msg;
799 /* check against the filters */
800 LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
802 if (!match_window( win, msg->win )) continue;
803 if (!check_msg_filter( msg->msg, first, last )) continue;
804 goto found; /* found one */
806 return 0;
808 /* return it to the app */
809 found:
810 reply->total = msg->data_size;
811 if (msg->data_size > get_reply_max_size())
813 set_error( STATUS_BUFFER_OVERFLOW );
814 return 1;
816 reply->type = msg->type;
817 reply->win = msg->win;
818 reply->msg = msg->msg;
819 reply->wparam = msg->wparam;
820 reply->lparam = msg->lparam;
821 reply->x = msg->x;
822 reply->y = msg->y;
823 reply->time = msg->time;
825 if (flags & PM_REMOVE)
827 if (msg->data)
829 set_reply_data_ptr( msg->data, msg->data_size );
830 msg->data = NULL;
831 msg->data_size = 0;
833 remove_queue_message( queue, msg, POST_MESSAGE );
835 else if (msg->data) set_reply_data( msg->data, msg->data_size );
837 return 1;
840 static int get_quit_message( struct msg_queue *queue, unsigned int flags,
841 struct get_message_reply *reply )
843 if (queue->quit_message)
845 reply->total = 0;
846 reply->type = MSG_POSTED;
847 reply->win = 0;
848 reply->msg = WM_QUIT;
849 reply->wparam = queue->exit_code;
850 reply->lparam = 0;
852 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
854 if (flags & PM_REMOVE)
856 queue->quit_message = 0;
857 if (list_empty( &queue->msg_list[POST_MESSAGE] ))
858 clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
860 return 1;
862 else
863 return 0;
866 /* empty a message list and free all the messages */
867 static void empty_msg_list( struct list *list )
869 struct list *ptr;
871 while ((ptr = list_head( list )) != NULL)
873 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
874 list_remove( &msg->entry );
875 free_message( msg );
879 /* cleanup all pending results when deleting a queue */
880 static void cleanup_results( struct msg_queue *queue )
882 struct list *entry;
884 while ((entry = list_head( &queue->send_result )) != NULL)
886 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
889 while ((entry = list_head( &queue->callback_result )) != NULL)
891 remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
894 while (queue->recv_result)
895 reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
898 /* check if the thread owning the queue is hung (not checking for messages) */
899 static int is_queue_hung( struct msg_queue *queue )
901 struct wait_queue_entry *entry;
903 if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
904 return 0; /* less than 5 seconds since last get message -> not hung */
906 LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
908 if (get_wait_queue_thread(entry)->queue == queue)
909 return 0; /* thread is waiting on queue -> not hung */
911 return 1;
914 static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
916 struct msg_queue *queue = (struct msg_queue *)obj;
917 struct process *process = get_wait_queue_thread(entry)->process;
919 /* a thread can only wait on its own queue */
920 if (get_wait_queue_thread(entry)->queue != queue)
922 set_error( STATUS_ACCESS_DENIED );
923 return 0;
925 if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
927 if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
928 set_fd_events( queue->fd, POLLIN );
929 add_queue( obj, entry );
930 return 1;
933 static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
935 struct msg_queue *queue = (struct msg_queue *)obj;
937 remove_queue( obj, entry );
938 if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
939 set_fd_events( queue->fd, 0 );
942 static void msg_queue_dump( struct object *obj, int verbose )
944 struct msg_queue *queue = (struct msg_queue *)obj;
945 fprintf( stderr, "Msg queue bits=%x mask=%x\n",
946 queue->wake_bits, queue->wake_mask );
949 static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
951 struct msg_queue *queue = (struct msg_queue *)obj;
952 int ret = 0;
954 if (queue->fd)
956 if ((ret = check_fd_events( queue->fd, POLLIN )))
957 /* stop waiting on select() if we are signaled */
958 set_fd_events( queue->fd, 0 );
959 else if (!list_empty( &obj->wait_queue ))
960 /* restart waiting on poll() if we are no longer signaled */
961 set_fd_events( queue->fd, POLLIN );
963 return ret || is_signaled( queue );
966 static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
968 struct msg_queue *queue = (struct msg_queue *)obj;
969 queue->wake_mask = 0;
970 queue->changed_mask = 0;
973 static void msg_queue_destroy( struct object *obj )
975 struct msg_queue *queue = (struct msg_queue *)obj;
976 struct list *ptr;
977 struct hotkey *hotkey, *hotkey2;
978 int i;
980 cleanup_results( queue );
981 for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
983 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
985 if (hotkey->queue == queue)
987 list_remove( &hotkey->entry );
988 free( hotkey );
992 while ((ptr = list_head( &queue->pending_timers )))
994 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
995 list_remove( &timer->entry );
996 free( timer );
998 while ((ptr = list_head( &queue->expired_timers )))
1000 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1001 list_remove( &timer->entry );
1002 free( timer );
1004 if (queue->timeout) remove_timeout_user( queue->timeout );
1005 queue->input->cursor_count -= queue->cursor_count;
1006 release_object( queue->input );
1007 if (queue->hooks) release_object( queue->hooks );
1008 if (queue->fd) release_object( queue->fd );
1011 static void msg_queue_poll_event( struct fd *fd, int event )
1013 struct msg_queue *queue = get_fd_user( fd );
1014 assert( queue->obj.ops == &msg_queue_ops );
1016 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1017 else set_fd_events( queue->fd, 0 );
1018 wake_up( &queue->obj, 0 );
1021 static void thread_input_dump( struct object *obj, int verbose )
1023 struct thread_input *input = (struct thread_input *)obj;
1024 fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
1025 input->focus, input->capture, input->active );
1028 static void thread_input_destroy( struct object *obj )
1030 struct thread_input *input = (struct thread_input *)obj;
1032 empty_msg_list( &input->msg_list );
1033 if (input->desktop)
1035 if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
1036 release_object( input->desktop );
1040 /* fix the thread input data when a window is destroyed */
1041 static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
1043 struct thread_input *input = queue->input;
1045 if (window == input->focus) input->focus = 0;
1046 if (window == input->capture) input->capture = 0;
1047 if (window == input->active) input->active = 0;
1048 if (window == input->menu_owner) input->menu_owner = 0;
1049 if (window == input->move_size) input->move_size = 0;
1050 if (window == input->caret) set_caret_window( input, 0 );
1053 /* check if the specified window can be set in the input data of a given queue */
1054 static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
1056 struct thread *thread;
1057 int ret = 0;
1059 if (!window) return 1; /* we can always clear the data */
1061 if ((thread = get_window_thread( window )))
1063 ret = (queue->input == thread->queue->input);
1064 if (!ret) set_error( STATUS_ACCESS_DENIED );
1065 release_object( thread );
1067 else set_error( STATUS_INVALID_HANDLE );
1069 return ret;
1072 /* make sure the specified thread has a queue */
1073 int init_thread_queue( struct thread *thread )
1075 if (thread->queue) return 1;
1076 return (create_msg_queue( thread, NULL ) != NULL);
1079 /* attach two thread input data structures */
1080 int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
1082 struct desktop *desktop;
1083 struct thread_input *input;
1084 int ret;
1086 if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
1087 if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
1088 input = (struct thread_input *)grab_object( thread_to->queue->input );
1089 if (input->desktop != desktop)
1091 set_error( STATUS_ACCESS_DENIED );
1092 release_object( input );
1093 release_object( desktop );
1094 return 0;
1096 release_object( desktop );
1098 if (thread_from->queue)
1100 if (!input->focus) input->focus = thread_from->queue->input->focus;
1101 if (!input->active) input->active = thread_from->queue->input->active;
1104 ret = assign_thread_input( thread_from, input );
1105 if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
1106 release_object( input );
1107 return ret;
1110 /* detach two thread input data structures */
1111 void detach_thread_input( struct thread *thread_from )
1113 struct thread *thread;
1114 struct thread_input *input, *old_input = thread_from->queue->input;
1116 if ((input = create_thread_input( thread_from )))
1118 if (old_input->focus && (thread = get_window_thread( old_input->focus )))
1120 if (thread == thread_from)
1122 input->focus = old_input->focus;
1123 old_input->focus = 0;
1125 release_object( thread );
1127 if (old_input->active && (thread = get_window_thread( old_input->active )))
1129 if (thread == thread_from)
1131 input->active = old_input->active;
1132 old_input->active = 0;
1134 release_object( thread );
1136 assign_thread_input( thread_from, input );
1137 release_object( input );
1142 /* set the next timer to expire */
1143 static void set_next_timer( struct msg_queue *queue )
1145 struct list *ptr;
1147 if (queue->timeout)
1149 remove_timeout_user( queue->timeout );
1150 queue->timeout = NULL;
1152 if ((ptr = list_head( &queue->pending_timers )))
1154 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1155 queue->timeout = add_timeout_user( timer->when, timer_callback, queue );
1157 /* set/clear QS_TIMER bit */
1158 if (list_empty( &queue->expired_timers ))
1159 clear_queue_bits( queue, QS_TIMER );
1160 else
1161 set_queue_bits( queue, QS_TIMER );
1164 /* find a timer from its window and id */
1165 static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
1166 unsigned int msg, lparam_t id )
1168 struct list *ptr;
1170 /* we need to search both lists */
1172 LIST_FOR_EACH( ptr, &queue->pending_timers )
1174 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1175 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1177 LIST_FOR_EACH( ptr, &queue->expired_timers )
1179 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1180 if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
1182 return NULL;
1185 /* callback for the next timer expiration */
1186 static void timer_callback( void *private )
1188 struct msg_queue *queue = private;
1189 struct list *ptr;
1191 queue->timeout = NULL;
1192 /* move on to the next timer */
1193 ptr = list_head( &queue->pending_timers );
1194 list_remove( ptr );
1195 list_add_tail( &queue->expired_timers, ptr );
1196 set_next_timer( queue );
1199 /* link a timer at its rightful place in the queue list */
1200 static void link_timer( struct msg_queue *queue, struct timer *timer )
1202 struct list *ptr;
1204 for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
1206 struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
1207 if (t->when >= timer->when) break;
1209 list_add_before( ptr, &timer->entry );
1212 /* remove a timer from the queue timer list and free it */
1213 static void free_timer( struct msg_queue *queue, struct timer *timer )
1215 list_remove( &timer->entry );
1216 free( timer );
1217 set_next_timer( queue );
1220 /* restart an expired timer */
1221 static void restart_timer( struct msg_queue *queue, struct timer *timer )
1223 list_remove( &timer->entry );
1224 while (timer->when <= current_time) timer->when += (timeout_t)timer->rate * 10000;
1225 link_timer( queue, timer );
1226 set_next_timer( queue );
1229 /* find an expired timer matching the filtering parameters */
1230 static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
1231 unsigned int get_first, unsigned int get_last,
1232 int remove )
1234 struct list *ptr;
1236 LIST_FOR_EACH( ptr, &queue->expired_timers )
1238 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
1239 if (win && timer->win != win) continue;
1240 if (check_msg_filter( timer->msg, get_first, get_last ))
1242 if (remove) restart_timer( queue, timer );
1243 return timer;
1246 return NULL;
1249 /* add a timer */
1250 static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
1252 struct timer *timer = mem_alloc( sizeof(*timer) );
1253 if (timer)
1255 timer->rate = max( rate, 1 );
1256 timer->when = current_time + (timeout_t)timer->rate * 10000;
1257 link_timer( queue, timer );
1258 /* check if we replaced the next timer */
1259 if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
1261 return timer;
1264 /* change the input key state for a given key */
1265 static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
1267 if (down)
1269 if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
1270 keystate[key] |= down;
1272 else keystate[key] &= ~0x80;
1275 /* update the input key state for a keyboard message */
1276 static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
1277 const struct message *msg )
1279 unsigned char key;
1280 int down = 0;
1282 switch (msg->msg)
1284 case WM_LBUTTONDOWN:
1285 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1286 /* fall through */
1287 case WM_LBUTTONUP:
1288 set_input_key_state( keystate, VK_LBUTTON, down );
1289 break;
1290 case WM_MBUTTONDOWN:
1291 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1292 /* fall through */
1293 case WM_MBUTTONUP:
1294 set_input_key_state( keystate, VK_MBUTTON, down );
1295 break;
1296 case WM_RBUTTONDOWN:
1297 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1298 /* fall through */
1299 case WM_RBUTTONUP:
1300 set_input_key_state( keystate, VK_RBUTTON, down );
1301 break;
1302 case WM_XBUTTONDOWN:
1303 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1304 /* fall through */
1305 case WM_XBUTTONUP:
1306 if (msg->wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
1307 else if (msg->wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
1308 break;
1309 case WM_KEYDOWN:
1310 case WM_SYSKEYDOWN:
1311 down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
1312 /* fall through */
1313 case WM_KEYUP:
1314 case WM_SYSKEYUP:
1315 key = (unsigned char)msg->wparam;
1316 set_input_key_state( keystate, key, down );
1317 switch(key)
1319 case VK_LCONTROL:
1320 case VK_RCONTROL:
1321 down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
1322 set_input_key_state( keystate, VK_CONTROL, down );
1323 break;
1324 case VK_LMENU:
1325 case VK_RMENU:
1326 down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
1327 set_input_key_state( keystate, VK_MENU, down );
1328 break;
1329 case VK_LSHIFT:
1330 case VK_RSHIFT:
1331 down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
1332 set_input_key_state( keystate, VK_SHIFT, down );
1333 break;
1335 break;
1339 /* release the hardware message currently being processed by the given thread */
1340 static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id,
1341 int remove )
1343 struct thread_input *input = queue->input;
1344 struct message *msg;
1346 LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
1348 if (msg->unique_id == hw_id) break;
1350 if (&msg->entry == &input->msg_list) return; /* not found */
1352 /* clear the queue bit for that message */
1353 if (remove)
1355 struct message *other;
1356 int clr_bit;
1358 clr_bit = get_hardware_msg_bit( msg );
1359 LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
1361 if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
1363 clr_bit = 0;
1364 break;
1367 if (clr_bit) clear_queue_bits( queue, clr_bit );
1369 update_input_key_state( input->desktop, input->keystate, msg );
1370 list_remove( &msg->entry );
1371 free_message( msg );
1375 static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
1377 struct hotkey *hotkey;
1378 unsigned int modifiers = 0;
1380 if (msg->msg != WM_KEYDOWN) return 0;
1382 if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
1383 if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
1384 if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
1385 if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
1387 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
1389 if (hotkey->vkey != msg->wparam) continue;
1390 if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
1393 return 0;
1395 found:
1396 msg->type = MSG_POSTED;
1397 msg->win = hotkey->win;
1398 msg->msg = WM_HOTKEY;
1399 msg->wparam = hotkey->id;
1400 msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
1402 free( msg->data );
1403 msg->data = NULL;
1404 msg->data_size = 0;
1406 list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
1407 set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
1408 hotkey->queue->hotkey_count++;
1409 return 1;
1412 /* find the window that should receive a given hardware message */
1413 static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
1414 struct message *msg, unsigned int *msg_code,
1415 struct thread **thread )
1417 user_handle_t win = 0;
1419 *thread = NULL;
1420 *msg_code = msg->msg;
1421 if (msg->msg == WM_INPUT)
1423 if (!(win = msg->win) && input) win = input->focus;
1425 else if (is_keyboard_msg( msg ))
1427 if (input && !(win = input->focus))
1429 win = input->active;
1430 if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
1433 else if (!input || !(win = input->capture)) /* mouse message */
1435 if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
1436 else win = shallow_window_from_point( desktop, msg->x, msg->y );
1438 *thread = window_thread_from_point( win, msg->x, msg->y );
1441 if (!*thread)
1442 *thread = get_window_thread( win );
1443 return win;
1446 static struct rawinput_device_entry *find_rawinput_device( unsigned short usage_page, unsigned short usage )
1448 struct rawinput_device_entry *e;
1450 LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
1452 if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
1453 return e;
1456 return NULL;
1459 static void update_rawinput_device(const struct rawinput_device *device)
1461 struct rawinput_device_entry *e;
1463 if (!(e = find_rawinput_device( device->usage_page, device->usage )))
1465 if (!(e = mem_alloc( sizeof(*e) ))) return;
1466 list_add_tail( &current->process->rawinput_devices, &e->entry );
1469 if (device->flags & RIDEV_REMOVE)
1471 list_remove( &e->entry );
1472 free( e );
1473 return;
1476 e->device = *device;
1477 e->device.target = get_user_full_handle( e->device.target );
1480 /* queue a hardware message into a given thread input */
1481 static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
1483 user_handle_t win;
1484 struct thread *thread;
1485 struct thread_input *input;
1486 unsigned int msg_code;
1488 update_input_key_state( desktop, desktop->keystate, msg );
1489 last_input_time = get_tick_count();
1490 if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
1492 if (is_keyboard_msg( msg ))
1494 if (queue_hotkey_message( desktop, msg )) return;
1495 if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
1496 if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
1497 msg->lparam &= ~(KF_EXTENDED << 16);
1499 else if (msg->msg != WM_INPUT)
1501 if (msg->msg == WM_MOUSEMOVE)
1503 int x = min( max( msg->x, desktop->cursor.clip.left ), desktop->cursor.clip.right-1 );
1504 int y = min( max( msg->y, desktop->cursor.clip.top ), desktop->cursor.clip.bottom-1 );
1505 if (desktop->cursor.x != x || desktop->cursor.y != y) always_queue = 1;
1506 desktop->cursor.x = x;
1507 desktop->cursor.y = y;
1508 desktop->cursor.last_change = get_tick_count();
1510 if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
1511 if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
1512 if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
1513 if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
1514 if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
1515 if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
1516 if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
1518 msg->x = desktop->cursor.x;
1519 msg->y = desktop->cursor.y;
1521 if (msg->win && (thread = get_window_thread( msg->win )))
1523 input = thread->queue->input;
1524 release_object( thread );
1526 else input = desktop->foreground_input;
1528 win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
1529 if (!win || !thread)
1531 if (input) update_input_key_state( input->desktop, input->keystate, msg );
1532 free_message( msg );
1533 return;
1535 input = thread->queue->input;
1537 if (win != desktop->cursor.win) always_queue = 1;
1538 desktop->cursor.win = win;
1540 if (!always_queue || merge_message( input, msg )) free_message( msg );
1541 else
1543 msg->unique_id = 0; /* will be set once we return it to the app */
1544 list_add_tail( &input->msg_list, &msg->entry );
1545 set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
1547 release_object( thread );
1550 /* send the low-level hook message for a given hardware message */
1551 static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
1552 const hw_input_t *input, struct msg_queue *sender )
1554 struct thread *hook_thread;
1555 struct msg_queue *queue;
1556 struct message *msg;
1557 timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
1558 int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
1560 if (!(hook_thread = get_first_global_hook( id ))) return 0;
1561 if (!(queue = hook_thread->queue)) return 0;
1562 if (is_queue_hung( queue )) return 0;
1564 if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
1566 msg->type = MSG_HOOK_LL;
1567 msg->win = 0;
1568 msg->msg = id;
1569 msg->wparam = hardware_msg->msg;
1570 msg->x = hardware_msg->x;
1571 msg->y = hardware_msg->y;
1572 msg->time = hardware_msg->time;
1573 msg->data_size = hardware_msg->data_size;
1574 msg->result = NULL;
1576 if (input->type == INPUT_KEYBOARD)
1578 unsigned short vkey = input->kbd.vkey;
1579 if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
1580 msg->lparam = (input->kbd.scan << 16) | vkey;
1582 else msg->lparam = input->mouse.data << 16;
1584 if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
1585 !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
1587 free_message( msg );
1588 return 0;
1590 msg->result->hardware_msg = hardware_msg;
1591 msg->result->desktop = (struct desktop *)grab_object( desktop );
1592 list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
1593 set_queue_bits( queue, QS_SENDMESSAGE );
1594 return 1;
1597 /* queue a hardware message for a mouse event */
1598 static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1599 unsigned int origin, struct msg_queue *sender )
1601 const struct rawinput_device *device;
1602 struct hardware_msg_data *msg_data;
1603 struct message *msg;
1604 unsigned int i, time, flags;
1605 struct hw_msg_source source = { IMDT_MOUSE, origin };
1606 int wait = 0, x, y;
1608 static const unsigned int messages[] =
1610 WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
1611 WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
1612 WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
1613 WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
1614 WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
1615 WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
1616 WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
1617 WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
1618 WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
1619 0, /* 0x0200 = unused */
1620 0, /* 0x0400 = unused */
1621 WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
1622 WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
1625 desktop->cursor.last_change = get_tick_count();
1626 flags = input->mouse.flags;
1627 time = input->mouse.time;
1628 if (!time) time = desktop->cursor.last_change;
1630 if (flags & MOUSEEVENTF_MOVE)
1632 if (flags & MOUSEEVENTF_ABSOLUTE)
1634 x = input->mouse.x;
1635 y = input->mouse.y;
1636 if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
1637 x == desktop->cursor.x && y == desktop->cursor.y)
1638 flags &= ~MOUSEEVENTF_MOVE;
1640 else
1642 x = desktop->cursor.x + input->mouse.x;
1643 y = desktop->cursor.y + input->mouse.y;
1646 else
1648 x = desktop->cursor.x;
1649 y = desktop->cursor.y;
1652 if ((device = current->process->rawinput_mouse))
1654 if (!(msg = alloc_hardware_message( input->mouse.info, source, time ))) return 0;
1655 msg_data = msg->data;
1657 msg->win = device->target;
1658 msg->msg = WM_INPUT;
1659 msg->wparam = RIM_INPUT;
1660 msg->lparam = 0;
1662 msg_data->flags = flags;
1663 msg_data->rawinput.type = RIM_TYPEMOUSE;
1664 msg_data->rawinput.mouse.x = x - desktop->cursor.x;
1665 msg_data->rawinput.mouse.y = y - desktop->cursor.y;
1666 msg_data->rawinput.mouse.data = input->mouse.data;
1668 queue_hardware_message( desktop, msg, 0 );
1671 for (i = 0; i < ARRAY_SIZE( messages ); i++)
1673 if (!messages[i]) continue;
1674 if (!(flags & (1 << i))) continue;
1675 flags &= ~(1 << i);
1677 if (!(msg = alloc_hardware_message( input->mouse.info, source, time ))) return 0;
1678 msg_data = msg->data;
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 if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
1688 /* specify a sender only when sending the last message */
1689 if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
1691 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1692 queue_hardware_message( desktop, msg, 0 );
1694 else if (!send_hook_ll_message( desktop, msg, input, NULL ))
1695 queue_hardware_message( desktop, msg, 0 );
1697 return wait;
1700 /* queue a hardware message for a keyboard event */
1701 static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
1702 unsigned int origin, struct msg_queue *sender )
1704 struct hw_msg_source source = { IMDT_KEYBOARD, origin };
1705 const struct rawinput_device *device;
1706 struct hardware_msg_data *msg_data;
1707 struct message *msg;
1708 unsigned char vkey = input->kbd.vkey;
1709 unsigned int message_code, time;
1710 int wait;
1712 if (!(time = input->kbd.time)) time = get_tick_count();
1714 if (!(input->kbd.flags & KEYEVENTF_UNICODE))
1716 switch (vkey)
1718 case VK_MENU:
1719 case VK_LMENU:
1720 case VK_RMENU:
1721 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
1722 break;
1723 case VK_CONTROL:
1724 case VK_LCONTROL:
1725 case VK_RCONTROL:
1726 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
1727 break;
1728 case VK_SHIFT:
1729 case VK_LSHIFT:
1730 case VK_RSHIFT:
1731 vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
1732 break;
1736 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
1737 switch (vkey)
1739 case VK_LMENU:
1740 case VK_RMENU:
1741 if (input->kbd.flags & KEYEVENTF_KEYUP)
1743 /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
1744 /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
1745 if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
1746 message_code = WM_SYSKEYUP;
1747 desktop->keystate[VK_MENU] &= ~0x02;
1749 else
1751 /* send WM_SYSKEYDOWN for Alt except with Ctrl */
1752 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1753 message_code = WM_SYSKEYDOWN;
1754 desktop->keystate[VK_MENU] |= 0x02;
1756 break;
1758 case VK_LCONTROL:
1759 case VK_RCONTROL:
1760 /* send WM_SYSKEYUP on release if Alt still pressed */
1761 if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
1762 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1763 message_code = WM_SYSKEYUP;
1764 desktop->keystate[VK_MENU] &= ~0x02;
1765 break;
1767 default:
1768 /* send WM_SYSKEY for Alt-anykey and for F10 */
1769 if (desktop->keystate[VK_CONTROL] & 0x80) break;
1770 if (!(desktop->keystate[VK_MENU] & 0x80)) break;
1771 /* fall through */
1772 case VK_F10:
1773 message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
1774 desktop->keystate[VK_MENU] &= ~0x02;
1775 break;
1778 if (!(msg = alloc_hardware_message( input->kbd.info, source, time ))) return 0;
1779 msg_data = msg->data;
1781 if ((device = current->process->rawinput_kbd))
1783 msg->win = device->target;
1784 msg->msg = WM_INPUT;
1785 msg->wparam = RIM_INPUT;
1787 msg_data->flags = input->kbd.flags;
1788 msg_data->rawinput.type = RIM_TYPEKEYBOARD;
1789 msg_data->rawinput.kbd.message = message_code;
1790 msg_data->rawinput.kbd.vkey = vkey;
1791 msg_data->rawinput.kbd.scan = input->kbd.scan;
1793 queue_hardware_message( desktop, msg, 0 );
1796 msg->win = get_user_full_handle( win );
1797 msg->msg = message_code;
1798 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1799 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
1801 if (input->kbd.flags & KEYEVENTF_UNICODE)
1803 msg->wparam = VK_PACKET;
1805 else
1807 unsigned int flags = 0;
1808 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1809 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1810 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1811 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1813 msg->wparam = vkey;
1814 msg->lparam |= flags << 16;
1815 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1818 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1819 queue_hardware_message( desktop, msg, 1 );
1821 return wait;
1824 /* queue a hardware message for a custom type of event */
1825 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1826 unsigned int origin, const hw_input_t *input )
1828 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
1829 struct message *msg;
1831 if (!(msg = alloc_hardware_message( 0, source, get_tick_count() ))) return;
1833 msg->win = get_user_full_handle( win );
1834 msg->msg = input->hw.msg;
1835 msg->wparam = 0;
1836 msg->lparam = input->hw.lparam;
1837 msg->x = desktop->cursor.x;
1838 msg->y = desktop->cursor.y;
1840 queue_hardware_message( desktop, msg, 1 );
1843 /* check message filter for a hardware message */
1844 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1845 user_handle_t filter_win, unsigned int first, unsigned int last )
1847 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1849 /* we can only test the window for a keyboard message since the
1850 * dest window for a mouse message depends on hittest */
1851 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1852 return 0;
1853 /* the message code is final for a keyboard message, we can simply check it */
1854 return check_msg_filter( msg_code, first, last );
1856 else /* mouse message */
1858 /* we need to check all possible values that the message can have in the end */
1860 if (check_msg_filter( msg_code, first, last )) return 1;
1861 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1863 /* all other messages can become non-client messages */
1864 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1866 /* clicks can become double-clicks or non-client double-clicks */
1867 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1868 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1870 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1871 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1873 return 0;
1878 /* find a hardware message for the given queue */
1879 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1880 unsigned int first, unsigned int last, unsigned int flags,
1881 struct get_message_reply *reply )
1883 struct thread_input *input = thread->queue->input;
1884 struct thread *win_thread;
1885 struct list *ptr;
1886 user_handle_t win;
1887 int clear_bits, got_one = 0;
1888 unsigned int msg_code;
1890 ptr = list_head( &input->msg_list );
1891 if (hw_id)
1893 while (ptr)
1895 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1896 if (msg->unique_id == hw_id) break;
1897 ptr = list_next( &input->msg_list, ptr );
1899 if (!ptr) ptr = list_head( &input->msg_list );
1900 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1903 if (ptr == list_head( &input->msg_list ))
1904 clear_bits = QS_INPUT;
1905 else
1906 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1908 while (ptr)
1910 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1911 struct hardware_msg_data *data = msg->data;
1913 ptr = list_next( &input->msg_list, ptr );
1914 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
1915 if (!win || !win_thread)
1917 /* no window at all, remove it */
1918 update_input_key_state( input->desktop, input->keystate, msg );
1919 list_remove( &msg->entry );
1920 free_message( msg );
1921 continue;
1923 if (win_thread != thread)
1925 if (win_thread->queue->input == input)
1927 /* wake the other thread */
1928 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1929 got_one = 1;
1931 else
1933 /* for another thread input, drop it */
1934 update_input_key_state( input->desktop, input->keystate, msg );
1935 list_remove( &msg->entry );
1936 free_message( msg );
1938 release_object( win_thread );
1939 continue;
1941 release_object( win_thread );
1943 /* if we already got a message for another thread, or if it doesn't
1944 * match the filter we skip it */
1945 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1947 clear_bits &= ~get_hardware_msg_bit( msg );
1948 continue;
1950 /* now we can return it */
1951 if (!msg->unique_id) msg->unique_id = get_unique_id();
1952 reply->type = MSG_HARDWARE;
1953 reply->win = win;
1954 reply->msg = msg_code;
1955 reply->wparam = msg->wparam;
1956 reply->lparam = msg->lparam;
1957 reply->x = msg->x;
1958 reply->y = msg->y;
1959 reply->time = msg->time;
1961 data->hw_id = msg->unique_id;
1962 set_reply_data( msg->data, msg->data_size );
1963 if (msg->msg == WM_INPUT && (flags & PM_REMOVE))
1964 release_hardware_message( current->queue, data->hw_id, 1 );
1965 return 1;
1967 /* nothing found, clear the hardware queue bits */
1968 clear_queue_bits( thread->queue, clear_bits );
1969 return 0;
1972 /* increment (or decrement if 'incr' is negative) the queue paint count */
1973 void inc_queue_paint_count( struct thread *thread, int incr )
1975 struct msg_queue *queue = thread->queue;
1977 assert( queue );
1979 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1981 if (queue->paint_count)
1982 set_queue_bits( queue, QS_PAINT );
1983 else
1984 clear_queue_bits( queue, QS_PAINT );
1988 /* remove all messages and timers belonging to a certain window */
1989 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1991 struct msg_queue *queue = thread->queue;
1992 struct list *ptr;
1993 int i;
1995 if (!queue) return;
1997 /* remove timers */
1999 ptr = list_head( &queue->pending_timers );
2000 while (ptr)
2002 struct list *next = list_next( &queue->pending_timers, ptr );
2003 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2004 if (timer->win == win) free_timer( queue, timer );
2005 ptr = next;
2007 ptr = list_head( &queue->expired_timers );
2008 while (ptr)
2010 struct list *next = list_next( &queue->expired_timers, ptr );
2011 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2012 if (timer->win == win) free_timer( queue, timer );
2013 ptr = next;
2016 /* remove messages */
2017 for (i = 0; i < NB_MSG_KINDS; i++)
2019 struct list *ptr, *next;
2021 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2023 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2024 if (msg->win == win)
2026 if (msg->msg == WM_QUIT && !queue->quit_message)
2028 queue->quit_message = 1;
2029 queue->exit_code = msg->wparam;
2031 remove_queue_message( queue, msg, i );
2036 thread_input_cleanup_window( queue, win );
2039 /* post a message to a window */
2040 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2042 struct message *msg;
2043 struct thread *thread = get_window_thread( win );
2045 if (!thread) return;
2047 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2049 msg->type = MSG_POSTED;
2050 msg->win = get_user_full_handle( win );
2051 msg->msg = message;
2052 msg->wparam = wparam;
2053 msg->lparam = lparam;
2054 msg->result = NULL;
2055 msg->data = NULL;
2056 msg->data_size = 0;
2058 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2060 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2061 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2062 if (message == WM_HOTKEY)
2064 set_queue_bits( thread->queue, QS_HOTKEY );
2065 thread->queue->hotkey_count++;
2068 release_object( thread );
2071 /* send a notify message to a window */
2072 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2074 struct message *msg;
2075 struct thread *thread = get_window_thread( win );
2077 if (!thread) return;
2079 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2081 msg->type = MSG_NOTIFY;
2082 msg->win = get_user_full_handle( win );
2083 msg->msg = message;
2084 msg->wparam = wparam;
2085 msg->lparam = lparam;
2086 msg->result = NULL;
2087 msg->data = NULL;
2088 msg->data_size = 0;
2090 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2092 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2093 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2095 release_object( thread );
2098 /* post a win event */
2099 void post_win_event( struct thread *thread, unsigned int event,
2100 user_handle_t win, unsigned int object_id,
2101 unsigned int child_id, client_ptr_t hook_proc,
2102 const WCHAR *module, data_size_t module_size,
2103 user_handle_t hook)
2105 struct message *msg;
2107 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2109 struct winevent_msg_data *data;
2111 msg->type = MSG_WINEVENT;
2112 msg->win = get_user_full_handle( win );
2113 msg->msg = event;
2114 msg->wparam = object_id;
2115 msg->lparam = child_id;
2116 msg->time = get_tick_count();
2117 msg->result = NULL;
2119 if ((data = malloc( sizeof(*data) + module_size )))
2121 data->hook = hook;
2122 data->tid = get_thread_id( current );
2123 data->hook_proc = hook_proc;
2124 memcpy( data + 1, module, module_size );
2126 msg->data = data;
2127 msg->data_size = sizeof(*data) + module_size;
2129 if (debug_level > 1)
2130 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2131 get_thread_id(thread), event, win, object_id, child_id );
2132 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2133 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2135 else
2136 free( msg );
2140 /* free all hotkeys on a desktop, optionally filtering by window */
2141 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2143 struct hotkey *hotkey, *hotkey2;
2145 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2147 if (!window || hotkey->win == window)
2149 list_remove( &hotkey->entry );
2150 free( hotkey );
2156 /* check if the thread owning the window is hung */
2157 DECL_HANDLER(is_window_hung)
2159 struct thread *thread;
2161 thread = get_window_thread( req->win );
2162 if (thread)
2164 reply->is_hung = is_queue_hung( thread->queue );
2165 release_object( thread );
2167 else reply->is_hung = 0;
2171 /* get the message queue of the current thread */
2172 DECL_HANDLER(get_msg_queue)
2174 struct msg_queue *queue = get_current_queue();
2176 reply->handle = 0;
2177 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2181 /* set the file descriptor associated to the current thread queue */
2182 DECL_HANDLER(set_queue_fd)
2184 struct msg_queue *queue = get_current_queue();
2185 struct file *file;
2186 int unix_fd;
2188 if (queue->fd) /* fd can only be set once */
2190 set_error( STATUS_ACCESS_DENIED );
2191 return;
2193 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2195 if ((unix_fd = get_file_unix_fd( file )) != -1)
2197 if ((unix_fd = dup( unix_fd )) != -1)
2198 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2199 else
2200 file_set_error();
2202 release_object( file );
2206 /* set the current message queue wakeup mask */
2207 DECL_HANDLER(set_queue_mask)
2209 struct msg_queue *queue = get_current_queue();
2211 if (queue)
2213 queue->wake_mask = req->wake_mask;
2214 queue->changed_mask = req->changed_mask;
2215 reply->wake_bits = queue->wake_bits;
2216 reply->changed_bits = queue->changed_bits;
2217 if (is_signaled( queue ))
2219 /* if skip wait is set, do what would have been done in the subsequent wait */
2220 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2221 else wake_up( &queue->obj, 0 );
2227 /* get the current message queue status */
2228 DECL_HANDLER(get_queue_status)
2230 struct msg_queue *queue = current->queue;
2231 if (queue)
2233 reply->wake_bits = queue->wake_bits;
2234 reply->changed_bits = queue->changed_bits;
2235 queue->changed_bits &= ~req->clear_bits;
2237 else reply->wake_bits = reply->changed_bits = 0;
2241 /* send a message to a thread queue */
2242 DECL_HANDLER(send_message)
2244 struct message *msg;
2245 struct msg_queue *send_queue = get_current_queue();
2246 struct msg_queue *recv_queue = NULL;
2247 struct thread *thread = NULL;
2249 if (!(thread = get_thread_from_id( req->id ))) return;
2251 if (!(recv_queue = thread->queue))
2253 set_error( STATUS_INVALID_PARAMETER );
2254 release_object( thread );
2255 return;
2257 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2259 set_error( STATUS_TIMEOUT );
2260 release_object( thread );
2261 return;
2264 if ((msg = mem_alloc( sizeof(*msg) )))
2266 msg->type = req->type;
2267 msg->win = get_user_full_handle( req->win );
2268 msg->msg = req->msg;
2269 msg->wparam = req->wparam;
2270 msg->lparam = req->lparam;
2271 msg->result = NULL;
2272 msg->data = NULL;
2273 msg->data_size = get_req_data_size();
2275 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2277 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2279 free( msg );
2280 release_object( thread );
2281 return;
2284 switch(msg->type)
2286 case MSG_OTHER_PROCESS:
2287 case MSG_ASCII:
2288 case MSG_UNICODE:
2289 case MSG_CALLBACK:
2290 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2292 free_message( msg );
2293 break;
2295 /* fall through */
2296 case MSG_NOTIFY:
2297 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2298 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2299 break;
2300 case MSG_POSTED:
2301 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2302 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2303 if (msg->msg == WM_HOTKEY)
2305 set_queue_bits( recv_queue, QS_HOTKEY );
2306 recv_queue->hotkey_count++;
2308 break;
2309 case MSG_HARDWARE: /* should use send_hardware_message instead */
2310 case MSG_CALLBACK_RESULT: /* cannot send this one */
2311 case MSG_HOOK_LL: /* generated internally */
2312 default:
2313 set_error( STATUS_INVALID_PARAMETER );
2314 free( msg );
2315 break;
2318 release_object( thread );
2321 /* send a hardware message to a thread queue */
2322 DECL_HANDLER(send_hardware_message)
2324 struct thread *thread = NULL;
2325 struct desktop *desktop;
2326 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2327 struct msg_queue *sender = get_current_queue();
2328 data_size_t size = min( 256, get_reply_max_size() );
2330 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2332 if (req->win)
2334 if (!(thread = get_window_thread( req->win ))) return;
2335 if (desktop != thread->queue->input->desktop)
2337 /* don't allow queuing events to a different desktop */
2338 release_object( desktop );
2339 return;
2343 reply->prev_x = desktop->cursor.x;
2344 reply->prev_y = desktop->cursor.y;
2346 switch (req->input.type)
2348 case INPUT_MOUSE:
2349 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2350 break;
2351 case INPUT_KEYBOARD:
2352 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2353 break;
2354 case INPUT_HARDWARE:
2355 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2356 break;
2357 default:
2358 set_error( STATUS_INVALID_PARAMETER );
2360 if (thread) release_object( thread );
2362 reply->new_x = desktop->cursor.x;
2363 reply->new_y = desktop->cursor.y;
2364 set_reply_data( desktop->keystate, size );
2365 release_object( desktop );
2368 /* post a quit message to the current queue */
2369 DECL_HANDLER(post_quit_message)
2371 struct msg_queue *queue = get_current_queue();
2373 if (!queue)
2374 return;
2376 queue->quit_message = 1;
2377 queue->exit_code = req->exit_code;
2378 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2381 /* get a message from the current queue */
2382 DECL_HANDLER(get_message)
2384 struct timer *timer;
2385 struct list *ptr;
2386 struct msg_queue *queue = get_current_queue();
2387 user_handle_t get_win = get_user_full_handle( req->get_win );
2388 unsigned int filter = req->flags >> 16;
2390 reply->active_hooks = get_active_hooks();
2392 if (!queue) return;
2393 queue->last_get_msg = current_time;
2394 if (!filter) filter = QS_ALLINPUT;
2396 /* first check for sent messages */
2397 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2399 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2400 receive_message( queue, msg, reply );
2401 return;
2404 /* clear changed bits so we can wait on them if we don't find a message */
2405 if (filter & QS_POSTMESSAGE)
2407 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2408 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2410 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2411 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2413 /* then check for posted messages */
2414 if ((filter & QS_POSTMESSAGE) &&
2415 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2416 return;
2418 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2419 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2420 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2421 return;
2423 /* only check for quit messages if not posted messages pending */
2424 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2425 return;
2427 /* then check for any raw hardware message */
2428 if ((filter & QS_INPUT) &&
2429 filter_contains_hw_range( req->get_first, req->get_last ) &&
2430 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2431 return;
2433 /* now check for WM_PAINT */
2434 if ((filter & QS_PAINT) &&
2435 queue->paint_count &&
2436 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2437 (reply->win = find_window_to_repaint( get_win, current )))
2439 reply->type = MSG_POSTED;
2440 reply->msg = WM_PAINT;
2441 reply->wparam = 0;
2442 reply->lparam = 0;
2443 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2444 return;
2447 /* now check for timer */
2448 if ((filter & QS_TIMER) &&
2449 (timer = find_expired_timer( queue, get_win, req->get_first,
2450 req->get_last, (req->flags & PM_REMOVE) )))
2452 reply->type = MSG_POSTED;
2453 reply->win = timer->win;
2454 reply->msg = timer->msg;
2455 reply->wparam = timer->id;
2456 reply->lparam = timer->lparam;
2457 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2458 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2459 set_event( current->process->idle_event );
2460 return;
2463 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2464 queue->wake_mask = req->wake_mask;
2465 queue->changed_mask = req->changed_mask;
2466 set_error( STATUS_PENDING ); /* FIXME */
2470 /* reply to a sent message */
2471 DECL_HANDLER(reply_message)
2473 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2474 else if (current->queue->recv_result)
2475 reply_message( current->queue, req->result, 0, req->remove,
2476 get_req_data(), get_req_data_size() );
2480 /* accept the current hardware message */
2481 DECL_HANDLER(accept_hardware_message)
2483 if (current->queue)
2484 release_hardware_message( current->queue, req->hw_id, req->remove );
2485 else
2486 set_error( STATUS_ACCESS_DENIED );
2490 /* retrieve the reply for the last message sent */
2491 DECL_HANDLER(get_message_reply)
2493 struct message_result *result;
2494 struct list *entry;
2495 struct msg_queue *queue = current->queue;
2497 if (queue)
2499 set_error( STATUS_PENDING );
2500 reply->result = 0;
2502 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2504 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2505 if (result->replied || req->cancel)
2507 if (result->replied)
2509 reply->result = result->result;
2510 set_error( result->error );
2511 if (result->data)
2513 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2514 set_reply_data_ptr( result->data, data_len );
2515 result->data = NULL;
2516 result->data_size = 0;
2519 remove_result_from_sender( result );
2521 entry = list_head( &queue->send_result );
2522 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2523 else
2525 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2526 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2527 else clear_queue_bits( queue, QS_SMRESULT );
2531 else set_error( STATUS_ACCESS_DENIED );
2535 /* set a window timer */
2536 DECL_HANDLER(set_win_timer)
2538 struct timer *timer;
2539 struct msg_queue *queue;
2540 struct thread *thread = NULL;
2541 user_handle_t win = 0;
2542 lparam_t id = req->id;
2544 if (req->win)
2546 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2548 set_error( STATUS_INVALID_HANDLE );
2549 return;
2551 if (thread->process != current->process)
2553 release_object( thread );
2554 set_error( STATUS_ACCESS_DENIED );
2555 return;
2557 queue = thread->queue;
2558 /* remove it if it existed already */
2559 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2561 else
2563 queue = get_current_queue();
2564 /* look for a timer with this id */
2565 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2567 /* free and reuse id */
2568 free_timer( queue, timer );
2570 else
2572 lparam_t end_id = queue->next_timer_id;
2574 /* find a free id for it */
2575 while (1)
2577 id = queue->next_timer_id;
2578 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2580 if (!find_timer( queue, 0, req->msg, id )) break;
2581 if (queue->next_timer_id == end_id)
2583 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2584 return;
2590 if ((timer = set_timer( queue, req->rate )))
2592 timer->win = win;
2593 timer->msg = req->msg;
2594 timer->id = id;
2595 timer->lparam = req->lparam;
2596 reply->id = id;
2598 if (thread) release_object( thread );
2601 /* kill a window timer */
2602 DECL_HANDLER(kill_win_timer)
2604 struct timer *timer;
2605 struct thread *thread;
2606 user_handle_t win = 0;
2608 if (req->win)
2610 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2612 set_error( STATUS_INVALID_HANDLE );
2613 return;
2615 if (thread->process != current->process)
2617 release_object( thread );
2618 set_error( STATUS_ACCESS_DENIED );
2619 return;
2622 else thread = (struct thread *)grab_object( current );
2624 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2625 free_timer( thread->queue, timer );
2626 else
2627 set_error( STATUS_INVALID_PARAMETER );
2629 release_object( thread );
2632 DECL_HANDLER(register_hotkey)
2634 struct desktop *desktop;
2635 user_handle_t win_handle = req->window;
2636 struct hotkey *hotkey;
2637 struct hotkey *new_hotkey = NULL;
2638 struct thread *thread;
2639 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2641 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2643 if (win_handle)
2645 if (!(win_handle = get_valid_window_handle( win_handle )))
2647 release_object( desktop );
2648 return;
2651 thread = get_window_thread( win_handle );
2652 if (thread) release_object( thread );
2654 if (thread != current)
2656 release_object( desktop );
2657 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2658 return;
2662 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2664 if (req->vkey == hotkey->vkey &&
2665 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2667 release_object( desktop );
2668 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2669 return;
2671 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2672 new_hotkey = hotkey;
2675 if (new_hotkey)
2677 reply->replaced = 1;
2678 reply->flags = new_hotkey->flags;
2679 reply->vkey = new_hotkey->vkey;
2681 else
2683 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2684 if (new_hotkey)
2686 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2687 new_hotkey->queue = current->queue;
2688 new_hotkey->win = win_handle;
2689 new_hotkey->id = req->id;
2693 if (new_hotkey)
2695 new_hotkey->flags = req->flags;
2696 new_hotkey->vkey = req->vkey;
2699 release_object( desktop );
2702 DECL_HANDLER(unregister_hotkey)
2704 struct desktop *desktop;
2705 user_handle_t win_handle = req->window;
2706 struct hotkey *hotkey;
2707 struct thread *thread;
2709 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2711 if (win_handle)
2713 if (!(win_handle = get_valid_window_handle( win_handle )))
2715 release_object( desktop );
2716 return;
2719 thread = get_window_thread( win_handle );
2720 if (thread) release_object( thread );
2722 if (thread != current)
2724 release_object( desktop );
2725 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2726 return;
2730 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2732 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2733 goto found;
2736 release_object( desktop );
2737 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2738 return;
2740 found:
2741 reply->flags = hotkey->flags;
2742 reply->vkey = hotkey->vkey;
2743 list_remove( &hotkey->entry );
2744 free( hotkey );
2745 release_object( desktop );
2748 /* attach (or detach) thread inputs */
2749 DECL_HANDLER(attach_thread_input)
2751 struct thread *thread_from = get_thread_from_id( req->tid_from );
2752 struct thread *thread_to = get_thread_from_id( req->tid_to );
2754 if (!thread_from || !thread_to)
2756 if (thread_from) release_object( thread_from );
2757 if (thread_to) release_object( thread_to );
2758 return;
2760 if (thread_from != thread_to)
2762 if (req->attach)
2764 if ((thread_to->queue || thread_to == current) &&
2765 (thread_from->queue || thread_from == current))
2766 attach_thread_input( thread_from, thread_to );
2767 else
2768 set_error( STATUS_INVALID_PARAMETER );
2770 else
2772 if (thread_from->queue && thread_to->queue &&
2773 thread_from->queue->input == thread_to->queue->input)
2774 detach_thread_input( thread_from );
2775 else
2776 set_error( STATUS_ACCESS_DENIED );
2779 else set_error( STATUS_ACCESS_DENIED );
2780 release_object( thread_from );
2781 release_object( thread_to );
2785 /* get thread input data */
2786 DECL_HANDLER(get_thread_input)
2788 struct thread *thread = NULL;
2789 struct desktop *desktop;
2790 struct thread_input *input;
2792 if (req->tid)
2794 if (!(thread = get_thread_from_id( req->tid ))) return;
2795 if (!(desktop = get_thread_desktop( thread, 0 )))
2797 release_object( thread );
2798 return;
2800 input = thread->queue ? thread->queue->input : NULL;
2802 else
2804 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2805 input = desktop->foreground_input; /* get the foreground thread info */
2808 if (input)
2810 reply->focus = input->focus;
2811 reply->capture = input->capture;
2812 reply->active = input->active;
2813 reply->menu_owner = input->menu_owner;
2814 reply->move_size = input->move_size;
2815 reply->caret = input->caret;
2816 reply->cursor = input->cursor;
2817 reply->show_count = input->cursor_count;
2818 reply->rect = input->caret_rect;
2821 /* foreground window is active window of foreground thread */
2822 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2823 if (thread) release_object( thread );
2824 release_object( desktop );
2828 /* retrieve queue keyboard state for a given thread */
2829 DECL_HANDLER(get_key_state)
2831 struct thread *thread;
2832 struct desktop *desktop;
2833 data_size_t size = min( 256, get_reply_max_size() );
2835 if (!req->tid) /* get global async key state */
2837 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2838 if (req->key >= 0)
2840 reply->state = desktop->keystate[req->key & 0xff];
2841 desktop->keystate[req->key & 0xff] &= ~0x40;
2843 set_reply_data( desktop->keystate, size );
2844 release_object( desktop );
2846 else
2848 unsigned char *keystate;
2849 if (!(thread = get_thread_from_id( req->tid ))) return;
2850 if (thread->queue)
2852 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2853 set_reply_data( thread->queue->input->keystate, size );
2854 release_object( thread );
2855 return;
2857 release_object( thread );
2859 /* fallback to desktop keystate */
2860 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2861 if (req->key >= 0) reply->state = desktop->keystate[req->key & 0xff] & ~0x40;
2862 if ((keystate = set_reply_data_size( size )))
2864 unsigned int i;
2865 for (i = 0; i < size; i++) keystate[i] = desktop->keystate[i] & ~0x40;
2867 release_object( desktop );
2872 /* set queue keyboard state for a given thread */
2873 DECL_HANDLER(set_key_state)
2875 struct thread *thread;
2876 struct desktop *desktop;
2877 data_size_t size = min( 256, get_req_data_size() );
2879 if (!req->tid) /* set global async key state */
2881 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2882 memcpy( desktop->keystate, get_req_data(), size );
2883 release_object( desktop );
2885 else
2887 if (!(thread = get_thread_from_id( req->tid ))) return;
2888 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
2889 if (req->async && (desktop = get_thread_desktop( thread, 0 )))
2891 memcpy( desktop->keystate, get_req_data(), size );
2892 release_object( desktop );
2894 release_object( thread );
2899 /* set the system foreground window */
2900 DECL_HANDLER(set_foreground_window)
2902 struct thread *thread = NULL;
2903 struct desktop *desktop;
2904 struct msg_queue *queue = get_current_queue();
2906 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2907 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
2908 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
2909 reply->send_msg_new = FALSE;
2911 if (is_valid_foreground_window( req->handle ) &&
2912 (thread = get_window_thread( req->handle )) &&
2913 thread->queue->input->desktop == desktop)
2915 set_foreground_input( desktop, thread->queue->input );
2916 reply->send_msg_new = (desktop->foreground_input != queue->input);
2918 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2920 if (thread) release_object( thread );
2921 release_object( desktop );
2925 /* set the current thread focus window */
2926 DECL_HANDLER(set_focus_window)
2928 struct msg_queue *queue = get_current_queue();
2930 reply->previous = 0;
2931 if (queue && check_queue_input_window( queue, req->handle ))
2933 reply->previous = queue->input->focus;
2934 queue->input->focus = get_user_full_handle( req->handle );
2939 /* set the current thread active window */
2940 DECL_HANDLER(set_active_window)
2942 struct msg_queue *queue = get_current_queue();
2944 reply->previous = 0;
2945 if (queue && check_queue_input_window( queue, req->handle ))
2947 if (!req->handle || make_window_active( req->handle ))
2949 reply->previous = queue->input->active;
2950 queue->input->active = get_user_full_handle( req->handle );
2952 else set_error( STATUS_INVALID_HANDLE );
2957 /* set the current thread capture window */
2958 DECL_HANDLER(set_capture_window)
2960 struct msg_queue *queue = get_current_queue();
2962 reply->previous = reply->full_handle = 0;
2963 if (queue && check_queue_input_window( queue, req->handle ))
2965 struct thread_input *input = queue->input;
2967 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2968 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2970 set_error(STATUS_ACCESS_DENIED);
2971 return;
2973 reply->previous = input->capture;
2974 input->capture = get_user_full_handle( req->handle );
2975 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2976 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2977 reply->full_handle = input->capture;
2982 /* Set the current thread caret window */
2983 DECL_HANDLER(set_caret_window)
2985 struct msg_queue *queue = get_current_queue();
2987 reply->previous = 0;
2988 if (queue && check_queue_input_window( queue, req->handle ))
2990 struct thread_input *input = queue->input;
2992 reply->previous = input->caret;
2993 reply->old_rect = input->caret_rect;
2994 reply->old_hide = input->caret_hide;
2995 reply->old_state = input->caret_state;
2997 set_caret_window( input, get_user_full_handle(req->handle) );
2998 input->caret_rect.right = input->caret_rect.left + req->width;
2999 input->caret_rect.bottom = input->caret_rect.top + req->height;
3004 /* Set the current thread caret information */
3005 DECL_HANDLER(set_caret_info)
3007 struct msg_queue *queue = get_current_queue();
3008 struct thread_input *input;
3010 if (!queue) return;
3011 input = queue->input;
3012 reply->full_handle = input->caret;
3013 reply->old_rect = input->caret_rect;
3014 reply->old_hide = input->caret_hide;
3015 reply->old_state = input->caret_state;
3017 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3019 set_error( STATUS_ACCESS_DENIED );
3020 return;
3022 if (req->flags & SET_CARET_POS)
3024 input->caret_rect.right += req->x - input->caret_rect.left;
3025 input->caret_rect.bottom += req->y - input->caret_rect.top;
3026 input->caret_rect.left = req->x;
3027 input->caret_rect.top = req->y;
3029 if (req->flags & SET_CARET_HIDE)
3031 input->caret_hide += req->hide;
3032 if (input->caret_hide < 0) input->caret_hide = 0;
3034 if (req->flags & SET_CARET_STATE)
3036 switch (req->state)
3038 case CARET_STATE_OFF: input->caret_state = 0; break;
3039 case CARET_STATE_ON: input->caret_state = 1; break;
3040 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3041 case CARET_STATE_ON_IF_MOVED:
3042 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3043 break;
3049 /* get the time of the last input event */
3050 DECL_HANDLER(get_last_input_time)
3052 reply->time = last_input_time;
3055 /* set/get the current cursor */
3056 DECL_HANDLER(set_cursor)
3058 struct msg_queue *queue = get_current_queue();
3059 struct thread_input *input;
3061 if (!queue) return;
3062 input = queue->input;
3064 reply->prev_handle = input->cursor;
3065 reply->prev_count = input->cursor_count;
3066 reply->prev_x = input->desktop->cursor.x;
3067 reply->prev_y = input->desktop->cursor.y;
3069 if (req->flags & SET_CURSOR_HANDLE)
3071 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3073 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3074 return;
3076 input->cursor = req->handle;
3078 if (req->flags & SET_CURSOR_COUNT)
3080 queue->cursor_count += req->show_count;
3081 input->cursor_count += req->show_count;
3083 if (req->flags & SET_CURSOR_POS)
3085 set_cursor_pos( input->desktop, req->x, req->y );
3087 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3089 struct desktop *desktop = input->desktop;
3091 /* only the desktop owner can set the message */
3092 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3093 desktop->cursor.clip_msg = req->clip_msg;
3095 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip, 0 );
3098 reply->new_x = input->desktop->cursor.x;
3099 reply->new_y = input->desktop->cursor.y;
3100 reply->new_clip = input->desktop->cursor.clip;
3101 reply->last_change = input->desktop->cursor.last_change;
3104 DECL_HANDLER(update_rawinput_devices)
3106 const struct rawinput_device *devices = get_req_data();
3107 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3108 const struct rawinput_device_entry *e;
3109 unsigned int i;
3111 for (i = 0; i < device_count; ++i)
3113 update_rawinput_device(&devices[i]);
3116 e = find_rawinput_device( 1, 2 );
3117 current->process->rawinput_mouse = e ? &e->device : NULL;
3118 e = find_rawinput_device( 1, 6 );
3119 current->process->rawinput_kbd = e ? &e->device : NULL;