user32: GetRawInputDeviceInfo uses characters for RIDI_DEVICENAME size.
[wine.git] / server / queue.c
blob2eec422c57539f2453b2de628a44c1d085e6e467
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 = max( min( desktop->cursor.x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
415 y = max( min( desktop->cursor.y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
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 = max( min( msg->x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
1504 int y = max( min( msg->y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
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 ((device = current->process->rawinput_kbd))
1780 if (!(msg = alloc_hardware_message( input->kbd.info, source, time ))) return 0;
1781 msg_data = msg->data;
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 if (!(msg = alloc_hardware_message( input->kbd.info, source, time ))) return 0;
1797 msg_data = msg->data;
1799 msg->win = get_user_full_handle( win );
1800 msg->msg = message_code;
1801 msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
1802 if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
1804 if (input->kbd.flags & KEYEVENTF_UNICODE)
1806 msg->wparam = VK_PACKET;
1808 else
1810 unsigned int flags = 0;
1811 if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
1812 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
1813 if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
1814 else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
1816 msg->wparam = vkey;
1817 msg->lparam |= flags << 16;
1818 msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
1821 if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
1822 queue_hardware_message( desktop, msg, 1 );
1824 return wait;
1827 /* queue a hardware message for a custom type of event */
1828 static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
1829 unsigned int origin, const hw_input_t *input )
1831 struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
1832 struct message *msg;
1834 if (!(msg = alloc_hardware_message( 0, source, get_tick_count() ))) return;
1836 msg->win = get_user_full_handle( win );
1837 msg->msg = input->hw.msg;
1838 msg->wparam = 0;
1839 msg->lparam = input->hw.lparam;
1840 msg->x = desktop->cursor.x;
1841 msg->y = desktop->cursor.y;
1843 queue_hardware_message( desktop, msg, 1 );
1846 /* check message filter for a hardware message */
1847 static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
1848 user_handle_t filter_win, unsigned int first, unsigned int last )
1850 if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
1852 /* we can only test the window for a keyboard message since the
1853 * dest window for a mouse message depends on hittest */
1854 if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
1855 return 0;
1856 /* the message code is final for a keyboard message, we can simply check it */
1857 return check_msg_filter( msg_code, first, last );
1859 else /* mouse message */
1861 /* we need to check all possible values that the message can have in the end */
1863 if (check_msg_filter( msg_code, first, last )) return 1;
1864 if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
1866 /* all other messages can become non-client messages */
1867 if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
1869 /* clicks can become double-clicks or non-client double-clicks */
1870 if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
1871 msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
1873 if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1874 if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
1876 return 0;
1881 /* find a hardware message for the given queue */
1882 static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
1883 unsigned int first, unsigned int last, unsigned int flags,
1884 struct get_message_reply *reply )
1886 struct thread_input *input = thread->queue->input;
1887 struct thread *win_thread;
1888 struct list *ptr;
1889 user_handle_t win;
1890 int clear_bits, got_one = 0;
1891 unsigned int msg_code;
1893 ptr = list_head( &input->msg_list );
1894 if (hw_id)
1896 while (ptr)
1898 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1899 if (msg->unique_id == hw_id) break;
1900 ptr = list_next( &input->msg_list, ptr );
1902 if (!ptr) ptr = list_head( &input->msg_list );
1903 else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
1906 if (ptr == list_head( &input->msg_list ))
1907 clear_bits = QS_INPUT;
1908 else
1909 clear_bits = 0; /* don't clear bits if we don't go through the whole list */
1911 while (ptr)
1913 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
1914 struct hardware_msg_data *data = msg->data;
1916 ptr = list_next( &input->msg_list, ptr );
1917 win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
1918 if (!win || !win_thread)
1920 /* no window at all, remove it */
1921 update_input_key_state( input->desktop, input->keystate, msg );
1922 list_remove( &msg->entry );
1923 free_message( msg );
1924 continue;
1926 if (win_thread != thread)
1928 if (win_thread->queue->input == input)
1930 /* wake the other thread */
1931 set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
1932 got_one = 1;
1934 else
1936 /* for another thread input, drop it */
1937 update_input_key_state( input->desktop, input->keystate, msg );
1938 list_remove( &msg->entry );
1939 free_message( msg );
1941 release_object( win_thread );
1942 continue;
1944 release_object( win_thread );
1946 /* if we already got a message for another thread, or if it doesn't
1947 * match the filter we skip it */
1948 if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
1950 clear_bits &= ~get_hardware_msg_bit( msg );
1951 continue;
1953 /* now we can return it */
1954 if (!msg->unique_id) msg->unique_id = get_unique_id();
1955 reply->type = MSG_HARDWARE;
1956 reply->win = win;
1957 reply->msg = msg_code;
1958 reply->wparam = msg->wparam;
1959 reply->lparam = msg->lparam;
1960 reply->x = msg->x;
1961 reply->y = msg->y;
1962 reply->time = msg->time;
1964 data->hw_id = msg->unique_id;
1965 set_reply_data( msg->data, msg->data_size );
1966 if (msg->msg == WM_INPUT && (flags & PM_REMOVE))
1967 release_hardware_message( current->queue, data->hw_id, 1 );
1968 return 1;
1970 /* nothing found, clear the hardware queue bits */
1971 clear_queue_bits( thread->queue, clear_bits );
1972 return 0;
1975 /* increment (or decrement if 'incr' is negative) the queue paint count */
1976 void inc_queue_paint_count( struct thread *thread, int incr )
1978 struct msg_queue *queue = thread->queue;
1980 assert( queue );
1982 if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
1984 if (queue->paint_count)
1985 set_queue_bits( queue, QS_PAINT );
1986 else
1987 clear_queue_bits( queue, QS_PAINT );
1991 /* remove all messages and timers belonging to a certain window */
1992 void queue_cleanup_window( struct thread *thread, user_handle_t win )
1994 struct msg_queue *queue = thread->queue;
1995 struct list *ptr;
1996 int i;
1998 if (!queue) return;
2000 /* remove timers */
2002 ptr = list_head( &queue->pending_timers );
2003 while (ptr)
2005 struct list *next = list_next( &queue->pending_timers, ptr );
2006 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2007 if (timer->win == win) free_timer( queue, timer );
2008 ptr = next;
2010 ptr = list_head( &queue->expired_timers );
2011 while (ptr)
2013 struct list *next = list_next( &queue->expired_timers, ptr );
2014 struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
2015 if (timer->win == win) free_timer( queue, timer );
2016 ptr = next;
2019 /* remove messages */
2020 for (i = 0; i < NB_MSG_KINDS; i++)
2022 struct list *ptr, *next;
2024 LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
2026 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2027 if (msg->win == win)
2029 if (msg->msg == WM_QUIT && !queue->quit_message)
2031 queue->quit_message = 1;
2032 queue->exit_code = msg->wparam;
2034 remove_queue_message( queue, msg, i );
2039 thread_input_cleanup_window( queue, win );
2042 /* post a message to a window */
2043 void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2045 struct message *msg;
2046 struct thread *thread = get_window_thread( win );
2048 if (!thread) return;
2050 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2052 msg->type = MSG_POSTED;
2053 msg->win = get_user_full_handle( win );
2054 msg->msg = message;
2055 msg->wparam = wparam;
2056 msg->lparam = lparam;
2057 msg->result = NULL;
2058 msg->data = NULL;
2059 msg->data_size = 0;
2061 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2063 list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
2064 set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2065 if (message == WM_HOTKEY)
2067 set_queue_bits( thread->queue, QS_HOTKEY );
2068 thread->queue->hotkey_count++;
2071 release_object( thread );
2074 /* send a notify message to a window */
2075 void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
2077 struct message *msg;
2078 struct thread *thread = get_window_thread( win );
2080 if (!thread) return;
2082 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2084 msg->type = MSG_NOTIFY;
2085 msg->win = get_user_full_handle( win );
2086 msg->msg = message;
2087 msg->wparam = wparam;
2088 msg->lparam = lparam;
2089 msg->result = NULL;
2090 msg->data = NULL;
2091 msg->data_size = 0;
2093 get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
2095 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2096 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2098 release_object( thread );
2101 /* post a win event */
2102 void post_win_event( struct thread *thread, unsigned int event,
2103 user_handle_t win, unsigned int object_id,
2104 unsigned int child_id, client_ptr_t hook_proc,
2105 const WCHAR *module, data_size_t module_size,
2106 user_handle_t hook)
2108 struct message *msg;
2110 if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
2112 struct winevent_msg_data *data;
2114 msg->type = MSG_WINEVENT;
2115 msg->win = get_user_full_handle( win );
2116 msg->msg = event;
2117 msg->wparam = object_id;
2118 msg->lparam = child_id;
2119 msg->time = get_tick_count();
2120 msg->result = NULL;
2122 if ((data = malloc( sizeof(*data) + module_size )))
2124 data->hook = hook;
2125 data->tid = get_thread_id( current );
2126 data->hook_proc = hook_proc;
2127 memcpy( data + 1, module, module_size );
2129 msg->data = data;
2130 msg->data_size = sizeof(*data) + module_size;
2132 if (debug_level > 1)
2133 fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
2134 get_thread_id(thread), event, win, object_id, child_id );
2135 list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
2136 set_queue_bits( thread->queue, QS_SENDMESSAGE );
2138 else
2139 free( msg );
2143 /* free all hotkeys on a desktop, optionally filtering by window */
2144 void free_hotkeys( struct desktop *desktop, user_handle_t window )
2146 struct hotkey *hotkey, *hotkey2;
2148 LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
2150 if (!window || hotkey->win == window)
2152 list_remove( &hotkey->entry );
2153 free( hotkey );
2159 /* check if the thread owning the window is hung */
2160 DECL_HANDLER(is_window_hung)
2162 struct thread *thread;
2164 thread = get_window_thread( req->win );
2165 if (thread)
2167 reply->is_hung = is_queue_hung( thread->queue );
2168 release_object( thread );
2170 else reply->is_hung = 0;
2174 /* get the message queue of the current thread */
2175 DECL_HANDLER(get_msg_queue)
2177 struct msg_queue *queue = get_current_queue();
2179 reply->handle = 0;
2180 if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
2184 /* set the file descriptor associated to the current thread queue */
2185 DECL_HANDLER(set_queue_fd)
2187 struct msg_queue *queue = get_current_queue();
2188 struct file *file;
2189 int unix_fd;
2191 if (queue->fd) /* fd can only be set once */
2193 set_error( STATUS_ACCESS_DENIED );
2194 return;
2196 if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
2198 if ((unix_fd = get_file_unix_fd( file )) != -1)
2200 if ((unix_fd = dup( unix_fd )) != -1)
2201 queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
2202 else
2203 file_set_error();
2205 release_object( file );
2209 /* set the current message queue wakeup mask */
2210 DECL_HANDLER(set_queue_mask)
2212 struct msg_queue *queue = get_current_queue();
2214 if (queue)
2216 queue->wake_mask = req->wake_mask;
2217 queue->changed_mask = req->changed_mask;
2218 reply->wake_bits = queue->wake_bits;
2219 reply->changed_bits = queue->changed_bits;
2220 if (is_signaled( queue ))
2222 /* if skip wait is set, do what would have been done in the subsequent wait */
2223 if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
2224 else wake_up( &queue->obj, 0 );
2230 /* get the current message queue status */
2231 DECL_HANDLER(get_queue_status)
2233 struct msg_queue *queue = current->queue;
2234 if (queue)
2236 reply->wake_bits = queue->wake_bits;
2237 reply->changed_bits = queue->changed_bits;
2238 queue->changed_bits &= ~req->clear_bits;
2240 else reply->wake_bits = reply->changed_bits = 0;
2244 /* send a message to a thread queue */
2245 DECL_HANDLER(send_message)
2247 struct message *msg;
2248 struct msg_queue *send_queue = get_current_queue();
2249 struct msg_queue *recv_queue = NULL;
2250 struct thread *thread = NULL;
2252 if (!(thread = get_thread_from_id( req->id ))) return;
2254 if (!(recv_queue = thread->queue))
2256 set_error( STATUS_INVALID_PARAMETER );
2257 release_object( thread );
2258 return;
2260 if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
2262 set_error( STATUS_TIMEOUT );
2263 release_object( thread );
2264 return;
2267 if ((msg = mem_alloc( sizeof(*msg) )))
2269 msg->type = req->type;
2270 msg->win = get_user_full_handle( req->win );
2271 msg->msg = req->msg;
2272 msg->wparam = req->wparam;
2273 msg->lparam = req->lparam;
2274 msg->result = NULL;
2275 msg->data = NULL;
2276 msg->data_size = get_req_data_size();
2278 get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
2280 if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
2282 free( msg );
2283 release_object( thread );
2284 return;
2287 switch(msg->type)
2289 case MSG_OTHER_PROCESS:
2290 case MSG_ASCII:
2291 case MSG_UNICODE:
2292 case MSG_CALLBACK:
2293 if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
2295 free_message( msg );
2296 break;
2298 /* fall through */
2299 case MSG_NOTIFY:
2300 list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
2301 set_queue_bits( recv_queue, QS_SENDMESSAGE );
2302 break;
2303 case MSG_POSTED:
2304 list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
2305 set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2306 if (msg->msg == WM_HOTKEY)
2308 set_queue_bits( recv_queue, QS_HOTKEY );
2309 recv_queue->hotkey_count++;
2311 break;
2312 case MSG_HARDWARE: /* should use send_hardware_message instead */
2313 case MSG_CALLBACK_RESULT: /* cannot send this one */
2314 case MSG_HOOK_LL: /* generated internally */
2315 default:
2316 set_error( STATUS_INVALID_PARAMETER );
2317 free( msg );
2318 break;
2321 release_object( thread );
2324 /* send a hardware message to a thread queue */
2325 DECL_HANDLER(send_hardware_message)
2327 struct thread *thread = NULL;
2328 struct desktop *desktop;
2329 unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
2330 struct msg_queue *sender = get_current_queue();
2331 data_size_t size = min( 256, get_reply_max_size() );
2333 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2335 if (req->win)
2337 if (!(thread = get_window_thread( req->win ))) return;
2338 if (desktop != thread->queue->input->desktop)
2340 /* don't allow queuing events to a different desktop */
2341 release_object( desktop );
2342 return;
2346 reply->prev_x = desktop->cursor.x;
2347 reply->prev_y = desktop->cursor.y;
2349 switch (req->input.type)
2351 case INPUT_MOUSE:
2352 reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
2353 break;
2354 case INPUT_KEYBOARD:
2355 reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
2356 break;
2357 case INPUT_HARDWARE:
2358 queue_custom_hardware_message( desktop, req->win, origin, &req->input );
2359 break;
2360 default:
2361 set_error( STATUS_INVALID_PARAMETER );
2363 if (thread) release_object( thread );
2365 reply->new_x = desktop->cursor.x;
2366 reply->new_y = desktop->cursor.y;
2367 set_reply_data( desktop->keystate, size );
2368 release_object( desktop );
2371 /* post a quit message to the current queue */
2372 DECL_HANDLER(post_quit_message)
2374 struct msg_queue *queue = get_current_queue();
2376 if (!queue)
2377 return;
2379 queue->quit_message = 1;
2380 queue->exit_code = req->exit_code;
2381 set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
2384 /* get a message from the current queue */
2385 DECL_HANDLER(get_message)
2387 struct timer *timer;
2388 struct list *ptr;
2389 struct msg_queue *queue = get_current_queue();
2390 user_handle_t get_win = get_user_full_handle( req->get_win );
2391 unsigned int filter = req->flags >> 16;
2393 reply->active_hooks = get_active_hooks();
2395 if (!queue) return;
2396 queue->last_get_msg = current_time;
2397 if (!filter) filter = QS_ALLINPUT;
2399 /* first check for sent messages */
2400 if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
2402 struct message *msg = LIST_ENTRY( ptr, struct message, entry );
2403 receive_message( queue, msg, reply );
2404 return;
2407 /* clear changed bits so we can wait on them if we don't find a message */
2408 if (filter & QS_POSTMESSAGE)
2410 queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
2411 if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
2413 if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
2414 if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
2416 /* then check for posted messages */
2417 if ((filter & QS_POSTMESSAGE) &&
2418 get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
2419 return;
2421 if ((filter & QS_HOTKEY) && queue->hotkey_count &&
2422 req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
2423 get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
2424 return;
2426 /* only check for quit messages if not posted messages pending */
2427 if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
2428 return;
2430 /* then check for any raw hardware message */
2431 if ((filter & QS_INPUT) &&
2432 filter_contains_hw_range( req->get_first, req->get_last ) &&
2433 get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
2434 return;
2436 /* now check for WM_PAINT */
2437 if ((filter & QS_PAINT) &&
2438 queue->paint_count &&
2439 check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
2440 (reply->win = find_window_to_repaint( get_win, current )))
2442 reply->type = MSG_POSTED;
2443 reply->msg = WM_PAINT;
2444 reply->wparam = 0;
2445 reply->lparam = 0;
2446 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2447 return;
2450 /* now check for timer */
2451 if ((filter & QS_TIMER) &&
2452 (timer = find_expired_timer( queue, get_win, req->get_first,
2453 req->get_last, (req->flags & PM_REMOVE) )))
2455 reply->type = MSG_POSTED;
2456 reply->win = timer->win;
2457 reply->msg = timer->msg;
2458 reply->wparam = timer->id;
2459 reply->lparam = timer->lparam;
2460 get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
2461 if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
2462 set_event( current->process->idle_event );
2463 return;
2466 if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
2467 queue->wake_mask = req->wake_mask;
2468 queue->changed_mask = req->changed_mask;
2469 set_error( STATUS_PENDING ); /* FIXME */
2473 /* reply to a sent message */
2474 DECL_HANDLER(reply_message)
2476 if (!current->queue) set_error( STATUS_ACCESS_DENIED );
2477 else if (current->queue->recv_result)
2478 reply_message( current->queue, req->result, 0, req->remove,
2479 get_req_data(), get_req_data_size() );
2483 /* accept the current hardware message */
2484 DECL_HANDLER(accept_hardware_message)
2486 if (current->queue)
2487 release_hardware_message( current->queue, req->hw_id, req->remove );
2488 else
2489 set_error( STATUS_ACCESS_DENIED );
2493 /* retrieve the reply for the last message sent */
2494 DECL_HANDLER(get_message_reply)
2496 struct message_result *result;
2497 struct list *entry;
2498 struct msg_queue *queue = current->queue;
2500 if (queue)
2502 set_error( STATUS_PENDING );
2503 reply->result = 0;
2505 if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
2507 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2508 if (result->replied || req->cancel)
2510 if (result->replied)
2512 reply->result = result->result;
2513 set_error( result->error );
2514 if (result->data)
2516 data_size_t data_len = min( result->data_size, get_reply_max_size() );
2517 set_reply_data_ptr( result->data, data_len );
2518 result->data = NULL;
2519 result->data_size = 0;
2522 remove_result_from_sender( result );
2524 entry = list_head( &queue->send_result );
2525 if (!entry) clear_queue_bits( queue, QS_SMRESULT );
2526 else
2528 result = LIST_ENTRY( entry, struct message_result, sender_entry );
2529 if (result->replied) set_queue_bits( queue, QS_SMRESULT );
2530 else clear_queue_bits( queue, QS_SMRESULT );
2534 else set_error( STATUS_ACCESS_DENIED );
2538 /* set a window timer */
2539 DECL_HANDLER(set_win_timer)
2541 struct timer *timer;
2542 struct msg_queue *queue;
2543 struct thread *thread = NULL;
2544 user_handle_t win = 0;
2545 lparam_t id = req->id;
2547 if (req->win)
2549 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2551 set_error( STATUS_INVALID_HANDLE );
2552 return;
2554 if (thread->process != current->process)
2556 release_object( thread );
2557 set_error( STATUS_ACCESS_DENIED );
2558 return;
2560 queue = thread->queue;
2561 /* remove it if it existed already */
2562 if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
2564 else
2566 queue = get_current_queue();
2567 /* look for a timer with this id */
2568 if (id && (timer = find_timer( queue, 0, req->msg, id )))
2570 /* free and reuse id */
2571 free_timer( queue, timer );
2573 else
2575 lparam_t end_id = queue->next_timer_id;
2577 /* find a free id for it */
2578 while (1)
2580 id = queue->next_timer_id;
2581 if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
2583 if (!find_timer( queue, 0, req->msg, id )) break;
2584 if (queue->next_timer_id == end_id)
2586 set_win32_error( ERROR_NO_MORE_USER_HANDLES );
2587 return;
2593 if ((timer = set_timer( queue, req->rate )))
2595 timer->win = win;
2596 timer->msg = req->msg;
2597 timer->id = id;
2598 timer->lparam = req->lparam;
2599 reply->id = id;
2601 if (thread) release_object( thread );
2604 /* kill a window timer */
2605 DECL_HANDLER(kill_win_timer)
2607 struct timer *timer;
2608 struct thread *thread;
2609 user_handle_t win = 0;
2611 if (req->win)
2613 if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
2615 set_error( STATUS_INVALID_HANDLE );
2616 return;
2618 if (thread->process != current->process)
2620 release_object( thread );
2621 set_error( STATUS_ACCESS_DENIED );
2622 return;
2625 else thread = (struct thread *)grab_object( current );
2627 if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
2628 free_timer( thread->queue, timer );
2629 else
2630 set_error( STATUS_INVALID_PARAMETER );
2632 release_object( thread );
2635 DECL_HANDLER(register_hotkey)
2637 struct desktop *desktop;
2638 user_handle_t win_handle = req->window;
2639 struct hotkey *hotkey;
2640 struct hotkey *new_hotkey = NULL;
2641 struct thread *thread;
2642 const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
2644 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2646 if (win_handle)
2648 if (!(win_handle = get_valid_window_handle( win_handle )))
2650 release_object( desktop );
2651 return;
2654 thread = get_window_thread( win_handle );
2655 if (thread) release_object( thread );
2657 if (thread != current)
2659 release_object( desktop );
2660 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2661 return;
2665 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2667 if (req->vkey == hotkey->vkey &&
2668 (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
2670 release_object( desktop );
2671 set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
2672 return;
2674 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2675 new_hotkey = hotkey;
2678 if (new_hotkey)
2680 reply->replaced = 1;
2681 reply->flags = new_hotkey->flags;
2682 reply->vkey = new_hotkey->vkey;
2684 else
2686 new_hotkey = mem_alloc( sizeof(*new_hotkey) );
2687 if (new_hotkey)
2689 list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
2690 new_hotkey->queue = current->queue;
2691 new_hotkey->win = win_handle;
2692 new_hotkey->id = req->id;
2696 if (new_hotkey)
2698 new_hotkey->flags = req->flags;
2699 new_hotkey->vkey = req->vkey;
2702 release_object( desktop );
2705 DECL_HANDLER(unregister_hotkey)
2707 struct desktop *desktop;
2708 user_handle_t win_handle = req->window;
2709 struct hotkey *hotkey;
2710 struct thread *thread;
2712 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2714 if (win_handle)
2716 if (!(win_handle = get_valid_window_handle( win_handle )))
2718 release_object( desktop );
2719 return;
2722 thread = get_window_thread( win_handle );
2723 if (thread) release_object( thread );
2725 if (thread != current)
2727 release_object( desktop );
2728 set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
2729 return;
2733 LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
2735 if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
2736 goto found;
2739 release_object( desktop );
2740 set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
2741 return;
2743 found:
2744 reply->flags = hotkey->flags;
2745 reply->vkey = hotkey->vkey;
2746 list_remove( &hotkey->entry );
2747 free( hotkey );
2748 release_object( desktop );
2751 /* attach (or detach) thread inputs */
2752 DECL_HANDLER(attach_thread_input)
2754 struct thread *thread_from = get_thread_from_id( req->tid_from );
2755 struct thread *thread_to = get_thread_from_id( req->tid_to );
2757 if (!thread_from || !thread_to)
2759 if (thread_from) release_object( thread_from );
2760 if (thread_to) release_object( thread_to );
2761 return;
2763 if (thread_from != thread_to)
2765 if (req->attach)
2767 if ((thread_to->queue || thread_to == current) &&
2768 (thread_from->queue || thread_from == current))
2769 attach_thread_input( thread_from, thread_to );
2770 else
2771 set_error( STATUS_INVALID_PARAMETER );
2773 else
2775 if (thread_from->queue && thread_to->queue &&
2776 thread_from->queue->input == thread_to->queue->input)
2777 detach_thread_input( thread_from );
2778 else
2779 set_error( STATUS_ACCESS_DENIED );
2782 else set_error( STATUS_ACCESS_DENIED );
2783 release_object( thread_from );
2784 release_object( thread_to );
2788 /* get thread input data */
2789 DECL_HANDLER(get_thread_input)
2791 struct thread *thread = NULL;
2792 struct desktop *desktop;
2793 struct thread_input *input;
2795 if (req->tid)
2797 if (!(thread = get_thread_from_id( req->tid ))) return;
2798 if (!(desktop = get_thread_desktop( thread, 0 )))
2800 release_object( thread );
2801 return;
2803 input = thread->queue ? thread->queue->input : NULL;
2805 else
2807 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2808 input = desktop->foreground_input; /* get the foreground thread info */
2811 if (input)
2813 reply->focus = input->focus;
2814 reply->capture = input->capture;
2815 reply->active = input->active;
2816 reply->menu_owner = input->menu_owner;
2817 reply->move_size = input->move_size;
2818 reply->caret = input->caret;
2819 reply->cursor = input->cursor;
2820 reply->show_count = input->cursor_count;
2821 reply->rect = input->caret_rect;
2824 /* foreground window is active window of foreground thread */
2825 reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
2826 if (thread) release_object( thread );
2827 release_object( desktop );
2831 /* retrieve queue keyboard state for a given thread */
2832 DECL_HANDLER(get_key_state)
2834 struct thread *thread;
2835 struct desktop *desktop;
2836 data_size_t size = min( 256, get_reply_max_size() );
2838 if (!req->tid) /* get global async key state */
2840 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2841 if (req->key >= 0)
2843 reply->state = desktop->keystate[req->key & 0xff];
2844 desktop->keystate[req->key & 0xff] &= ~0x40;
2846 set_reply_data( desktop->keystate, size );
2847 release_object( desktop );
2849 else
2851 unsigned char *keystate;
2852 if (!(thread = get_thread_from_id( req->tid ))) return;
2853 if (thread->queue)
2855 if (req->key >= 0) reply->state = thread->queue->input->keystate[req->key & 0xff];
2856 set_reply_data( thread->queue->input->keystate, size );
2857 release_object( thread );
2858 return;
2860 release_object( thread );
2862 /* fallback to desktop keystate */
2863 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2864 if (req->key >= 0) reply->state = desktop->keystate[req->key & 0xff] & ~0x40;
2865 if ((keystate = set_reply_data_size( size )))
2867 unsigned int i;
2868 for (i = 0; i < size; i++) keystate[i] = desktop->keystate[i] & ~0x40;
2870 release_object( desktop );
2875 /* set queue keyboard state for a given thread */
2876 DECL_HANDLER(set_key_state)
2878 struct thread *thread;
2879 struct desktop *desktop;
2880 data_size_t size = min( 256, get_req_data_size() );
2882 if (!req->tid) /* set global async key state */
2884 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2885 memcpy( desktop->keystate, get_req_data(), size );
2886 release_object( desktop );
2888 else
2890 if (!(thread = get_thread_from_id( req->tid ))) return;
2891 if (thread->queue) memcpy( thread->queue->input->keystate, get_req_data(), size );
2892 if (req->async && (desktop = get_thread_desktop( thread, 0 )))
2894 memcpy( desktop->keystate, get_req_data(), size );
2895 release_object( desktop );
2897 release_object( thread );
2902 /* set the system foreground window */
2903 DECL_HANDLER(set_foreground_window)
2905 struct thread *thread = NULL;
2906 struct desktop *desktop;
2907 struct msg_queue *queue = get_current_queue();
2909 if (!(desktop = get_thread_desktop( current, 0 ))) return;
2910 reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
2911 reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
2912 reply->send_msg_new = FALSE;
2914 if (is_valid_foreground_window( req->handle ) &&
2915 (thread = get_window_thread( req->handle )) &&
2916 thread->queue->input->desktop == desktop)
2918 set_foreground_input( desktop, thread->queue->input );
2919 reply->send_msg_new = (desktop->foreground_input != queue->input);
2921 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2923 if (thread) release_object( thread );
2924 release_object( desktop );
2928 /* set the current thread focus window */
2929 DECL_HANDLER(set_focus_window)
2931 struct msg_queue *queue = get_current_queue();
2933 reply->previous = 0;
2934 if (queue && check_queue_input_window( queue, req->handle ))
2936 reply->previous = queue->input->focus;
2937 queue->input->focus = get_user_full_handle( req->handle );
2942 /* set the current thread active window */
2943 DECL_HANDLER(set_active_window)
2945 struct msg_queue *queue = get_current_queue();
2947 reply->previous = 0;
2948 if (queue && check_queue_input_window( queue, req->handle ))
2950 if (!req->handle || make_window_active( req->handle ))
2952 reply->previous = queue->input->active;
2953 queue->input->active = get_user_full_handle( req->handle );
2955 else set_error( STATUS_INVALID_HANDLE );
2960 /* set the current thread capture window */
2961 DECL_HANDLER(set_capture_window)
2963 struct msg_queue *queue = get_current_queue();
2965 reply->previous = reply->full_handle = 0;
2966 if (queue && check_queue_input_window( queue, req->handle ))
2968 struct thread_input *input = queue->input;
2970 /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
2971 if (input->menu_owner && !(req->flags & CAPTURE_MENU))
2973 set_error(STATUS_ACCESS_DENIED);
2974 return;
2976 reply->previous = input->capture;
2977 input->capture = get_user_full_handle( req->handle );
2978 input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
2979 input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
2980 reply->full_handle = input->capture;
2985 /* Set the current thread caret window */
2986 DECL_HANDLER(set_caret_window)
2988 struct msg_queue *queue = get_current_queue();
2990 reply->previous = 0;
2991 if (queue && check_queue_input_window( queue, req->handle ))
2993 struct thread_input *input = queue->input;
2995 reply->previous = input->caret;
2996 reply->old_rect = input->caret_rect;
2997 reply->old_hide = input->caret_hide;
2998 reply->old_state = input->caret_state;
3000 set_caret_window( input, get_user_full_handle(req->handle) );
3001 input->caret_rect.right = input->caret_rect.left + req->width;
3002 input->caret_rect.bottom = input->caret_rect.top + req->height;
3007 /* Set the current thread caret information */
3008 DECL_HANDLER(set_caret_info)
3010 struct msg_queue *queue = get_current_queue();
3011 struct thread_input *input;
3013 if (!queue) return;
3014 input = queue->input;
3015 reply->full_handle = input->caret;
3016 reply->old_rect = input->caret_rect;
3017 reply->old_hide = input->caret_hide;
3018 reply->old_state = input->caret_state;
3020 if (req->handle && get_user_full_handle(req->handle) != input->caret)
3022 set_error( STATUS_ACCESS_DENIED );
3023 return;
3025 if (req->flags & SET_CARET_POS)
3027 input->caret_rect.right += req->x - input->caret_rect.left;
3028 input->caret_rect.bottom += req->y - input->caret_rect.top;
3029 input->caret_rect.left = req->x;
3030 input->caret_rect.top = req->y;
3032 if (req->flags & SET_CARET_HIDE)
3034 input->caret_hide += req->hide;
3035 if (input->caret_hide < 0) input->caret_hide = 0;
3037 if (req->flags & SET_CARET_STATE)
3039 switch (req->state)
3041 case CARET_STATE_OFF: input->caret_state = 0; break;
3042 case CARET_STATE_ON: input->caret_state = 1; break;
3043 case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
3044 case CARET_STATE_ON_IF_MOVED:
3045 if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
3046 break;
3052 /* get the time of the last input event */
3053 DECL_HANDLER(get_last_input_time)
3055 reply->time = last_input_time;
3058 /* set/get the current cursor */
3059 DECL_HANDLER(set_cursor)
3061 struct msg_queue *queue = get_current_queue();
3062 struct thread_input *input;
3064 if (!queue) return;
3065 input = queue->input;
3067 reply->prev_handle = input->cursor;
3068 reply->prev_count = input->cursor_count;
3069 reply->prev_x = input->desktop->cursor.x;
3070 reply->prev_y = input->desktop->cursor.y;
3072 if (req->flags & SET_CURSOR_HANDLE)
3074 if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
3076 set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
3077 return;
3079 input->cursor = req->handle;
3081 if (req->flags & SET_CURSOR_COUNT)
3083 queue->cursor_count += req->show_count;
3084 input->cursor_count += req->show_count;
3086 if (req->flags & SET_CURSOR_POS)
3088 set_cursor_pos( input->desktop, req->x, req->y );
3090 if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
3092 struct desktop *desktop = input->desktop;
3094 /* only the desktop owner can set the message */
3095 if (req->clip_msg && get_top_window_owner(desktop) == current->process)
3096 desktop->cursor.clip_msg = req->clip_msg;
3098 set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip, 0 );
3101 reply->new_x = input->desktop->cursor.x;
3102 reply->new_y = input->desktop->cursor.y;
3103 reply->new_clip = input->desktop->cursor.clip;
3104 reply->last_change = input->desktop->cursor.last_change;
3107 DECL_HANDLER(update_rawinput_devices)
3109 const struct rawinput_device *devices = get_req_data();
3110 unsigned int device_count = get_req_data_size() / sizeof (*devices);
3111 const struct rawinput_device_entry *e;
3112 unsigned int i;
3114 for (i = 0; i < device_count; ++i)
3116 update_rawinput_device(&devices[i]);
3119 e = find_rawinput_device( 1, 2 );
3120 current->process->rawinput_mouse = e ? &e->device : NULL;
3121 e = find_rawinput_device( 1, 6 );
3122 current->process->rawinput_kbd = e ? &e->device : NULL;